Browse Source

Modifies map to work on a value instead of a tuple. Adds ability to
track object whose location in memory changes.

Caleb Fangmeier 7 years ago
parent
commit
7c5a06c426
4 changed files with 71 additions and 17 deletions
  1. 12 4
      api.hpp
  2. 1 1
      root/container.hpp
  3. 29 3
      root/dataset.hpp
  4. 29 9
      value.hpp

+ 12 - 4
api.hpp

@@ -45,6 +45,8 @@
 #include "filval/value.hpp"
 namespace fv{
 
+    
+
     template<typename T>
     Value<T>* lookup(const std::string& name){
         Value<T>* tv = GenValue::get_value<T>(name);
@@ -92,15 +94,15 @@ namespace fv{
             return (Value<type>*)new Zip<ArgTypes...>(args..., alias);
     }
 
-    template <typename Ret, typename... ArgTypes>
+    template <typename Ret, typename ArgType>
     decltype(auto)
-    map(Function<Ret(ArgTypes...)>& fn, Value<std::vector<std::tuple<ArgTypes...>>>* arg, const std::string& alias=""){
+    map(Function<Ret(ArgType)>& fn, Value<std::vector<ArgType>>* arg, const std::string& alias=""){
         typedef std::vector<Ret> type;
-        const std::string& name = Map<Ret(ArgTypes...)>::fmt_name(fn, arg);
+        const std::string& name = Map<Ret(ArgType)>::fmt_name(fn, arg);
         if (check_exists<type>(name))
             return lookup<type>(name);
         else
-            return (Value<type>*)new Map<Ret(ArgTypes...)>(fn, arg, alias);
+            return (Value<type>*)new Map<Ret(ArgType)>(fn, arg, alias);
     }
 
     template <typename... ArgTypes>
@@ -353,5 +355,11 @@ namespace fv{
     ObsFilter* obs_filter(const std::string& name, std::function<bool()> filter_function, const std::string& impl=""){
         return new ObsFilter(name, filter_function, impl);
     }
+
+    template <typename T>
+    static Function<T>& func(const std::string& name, std::function<T> f, const std::string& impl){
+        return GenFunction::reg_func(name, f, impl);
+    }
+
 }
 #endif // API_HPP

+ 1 - 1
root/container.hpp

@@ -141,7 +141,7 @@ template <typename V>
 class ContainerTH1Many : public _ContainerTH1<std::vector<V>>{
     using _ContainerTH1<std::vector<V>>::_ContainerTH1;
     void _do_fill(){
-        for(V x : this->value->get_value())
+        for(const V &x : this->value->get_value())
             this->container->Fill(x);
     }
     public:

+ 29 - 3
root/dataset.hpp

@@ -28,8 +28,8 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
-#ifndef minitreedataset_h
-#define minitreedataset_h
+#ifndef root_dataset_h
+#define root_dataset_h
 
 #include <string>
 #include <tuple>
@@ -58,6 +58,7 @@ class TreeDataSet : public DataSet{
 
         bool load_next(){
             if (next_entry >= nentries) return false;
+            tree_obj->LoadTree(next_entry);
             tree_obj->GetEntry(next_entry);
             ++next_entry;
             return true;
@@ -110,8 +111,19 @@ class TreeDataSet : public DataSet{
             nentries = tree_obj->fChain->GetEntries();
             output_file = TFile::Open(output_filename.c_str(), "RECREATE");
             tree_obj->fChain->SetBranchStatus("*", false);
+            /* print_branch_statuses(); */
           }
 
+        void print_branch_statuses(){
+            TObjArray* obj_arr = tree_obj->fChain->GetListOfBranches();
+            for(TIter iter=obj_arr->begin(); iter!= obj_arr->end(); ++iter){
+                TBranch* br = (TBranch*)*iter;
+                std::cout << br->GetName() << ": " 
+                    << tree_obj->fChain->GetBranchStatus(br->GetName()) << std::endl;
+            }
+
+        }
+
         // TODO: Rewrite this constructor
         /* MiniTreeDataSet(const std::string& output_filename, const std::map<std::string,std::string>& filenames_with_labels) */
         /*   :DataSet(), */
@@ -169,6 +181,20 @@ class TreeDataSet : public DataSet{
             return new PointerValue<T>(bname, bref);
         }
 
+        template <typename T>
+        Value<T>* track_branch_obj(const std::string& bname){
+            TBranch* branch = tree_obj->fChain->GetBranch(bname.c_str());
+            if (branch == nullptr){
+                CRITICAL("Branch: " << bname << " does not exist in input tree.", -1);
+            }
+            T** bref = (T**) branch->GetAddress();
+            tree_obj->fChain->SetBranchStatus(bname.c_str(), true);
+            INFO("Registering object branch \"" << bname
+                 << "\" with address " << bref
+                 << " and type " << typeid(bref).name());
+            return new ObjectValue<T>(bname, bref);
+        }
+
         template <typename T>
         decltype(auto) track_branch_vec(const std::string& size_bname, const std::string& value_bname){
             track_branch_ptr<T>(value_bname);
@@ -191,4 +217,4 @@ class TreeDataSet : public DataSet{
             }
         }
 };
-#endif // minitreedataset_h
+#endif // root_dataset_h

+ 29 - 9
value.hpp

@@ -660,7 +660,7 @@ namespace impl {
  */
 template <typename... ArgTypes>
 class Zip : public DerivedValue<std::vector<std::tuple<ArgTypes...>>>,
-             private _Zip<ArgTypes...>{
+            private _Zip<ArgTypes...>{
     protected:
         void update_value(){
             this->value.clear();
@@ -688,26 +688,26 @@ template<typename> class Map; // undefined
  * tuple should contain two floats. The Value object required by Map will
  * typically be created as a Zip.
  */
-template <typename Ret, typename... ArgTypes>
-class Map<Ret(ArgTypes...)> : public DerivedValue<std::vector<Ret>>{
+template <typename Ret, typename ArgType>
+class Map<Ret(ArgType)> : public DerivedValue<std::vector<Ret>>{
     private:
-        typedef Value<std::vector<std::tuple<ArgTypes...>>> arg_type;
-        Function<Ret(ArgTypes...)>& fn;
+        typedef Value<std::vector<ArgType>> arg_type;
+        Function<Ret(ArgType)>& fn;
         arg_type* arg;
 
         void update_value(){
             this->value.clear();
-            for(auto tup : arg->get_value()){
-                this->value.push_back(call(fn,tup));
+            for(auto v : arg->get_value()){
+                this->value.push_back(fn(v));
             }
         }
 
     public:
-        static std::string fmt_name(Function<Ret(ArgTypes...)>& fn, arg_type* arg){
+        static std::string fmt_name(Function<Ret(ArgType)>& fn, arg_type* arg){
             return "map("+fn.get_name()+":"+arg->get_name()+")";
         }
 
-        Map(Function<Ret(ArgTypes...)>& fn, arg_type* arg, const std::string& alias)
+        Map(Function<Ret(ArgType)>& fn, arg_type* arg, const std::string& alias)
           :DerivedValue<std::vector<Ret>>(fmt_name(fn, arg), alias),
            fn(fn), arg(arg){ }
 
@@ -1221,6 +1221,26 @@ class PointerValue : public DerivedValue<T*>{
         }
 };
 
+/**
+ * Used for when the value is an object whose location in memory is changing,
+ * but one has a pointer to a pointer that points to the always updated
+ * location. (Mainly when reading objects such as stl containers from a root
+ * TTree)
+ */
+template <typename T>
+class ObjectValue : public DerivedValue<T>{
+    protected:
+        T** obj_pointer;
+        void update_value(){
+            this->value = **obj_pointer;
+        }
+
+    public:
+        ObjectValue(const std::string& name, T **ptr, const std::string alias="")
+          :DerivedValue<T>(name, alias),
+           obj_pointer(ptr){ }
+};
+
 /**
  * A Value which always returns the same value, supplied in the constructor.
  */