TTTT Analysis 0.1

MiniTreeDataSet.hpp
Go to the documentation of this file.
1
31 #ifndef minitreedataset_h
32 #define minitreedataset_h
33
34 #include <string>
35 #include <tuple>
36
37 #include "filval/filval.hpp"
38 #include "filval_root/filval_root.hpp"
39 #include "MiniTree.hpp"
40
41 using namespace std;
42 using namespace fv;
43 using namespace fv::root;
44
45 class MiniTreeDataSet : public DataSet,
46 public MiniTree{
47 private:
48 std::string input_filename;
49 std::string output_filename;
50 TFile* input_file;
51 TFile* output_file;
52 long next_entry;
53 long nentries;
54 bool load_next(){
55 if (next_entry >= nentries) return false;
56 fChain->GetEntry(next_entry);
57 ++next_entry;
58 return true;
59 }
60 int get_events(){
61 return nentries;
62 }
63 int get_current_event(){
64 return next_entry-1;
65 }
66
67 public:
68 MiniTreeDataSet(const std::string& input_filename, const std::string output_filename)
69 :input_filename(input_filename),
70 output_filename(output_filename),
71 next_entry(0) {
72 input_file = TFile::Open(input_filename.c_str());
73 Init((TTree*) input_file->Get("tree"));
74 nentries = fChain->GetEntriesFast();
75 output_file = TFile::Open(output_filename.c_str(), "RECREATE");
76 this->fChain->SetBranchStatus("*", false);
77 }
78
79 ~MiniTreeDataSet(){
80 input_file->Close();
81 output_file->Close();
82 }
83
84 template <typename T>
85 Value<T>* track_branch(const std::string& bname){
86 TBranch* branch = fChain->GetBranch(bname.c_str());
87 if (branch == nullptr){
88 CRITICAL("Branch: " << bname << " does not exist in input tree.", -1);
89 }
90 T* bref = (T*) branch->GetAddress();
91 fChain->SetBranchStatus(bname.c_str(), true);
92 INFO("Registering branch \"" << bname
93 << "\" with address " << bref
94 << " and type " << typeid(bref).name());
95 return new ObservedValue<T>(bname, bref);
96 }
97
98 template <typename T>
99 Value<T*>* track_branch_ptr(const std::string& bname){
100 TBranch* branch = fChain->GetBranch(bname.c_str());
101 if (branch == nullptr){
102 CRITICAL("Branch: " << bname << " does not exist in input tree.", -1);
103 }
104 T* bref = (T*) branch->GetAddress();
105 fChain->SetBranchStatus(bname.c_str(), true);
106 INFO("Registering pointer branch \"" << bname
107 << "\" with address " << bref
108 << " and type " << typeid(bref).name());
109 return new PointerValue<T>(bname, bref);
110 }
111
112 template <typename T>
113 decltype(auto) track_branch_vec(const std::string& size_bname, const std::string& value_bname){
114 track_branch_ptr<T>(value_bname);
115 return wrapper_vector<T>(lookup<int>(size_bname), lookup<T*>(value_bname), value_bname);
116 }
117
118 void save_all(){
119 output_file->cd();
120 for(auto container : containers){
121 container.second->save_as("outfile", SaveOption::ROOT);
122 }
123 // Save the value names for each container to enable looking up
124 // what values are plotted
125 std::map<string,string> value_lookup = this->get_container_name_value_map();
126 gDirectory->WriteObjectAny(&value_lookup, "std::map<std::string,std::string>", "_value_lookup");
127
128 std::map<string,string> fn_impl_lookup = this->get_function_name_impl_map();
129 gDirectory->WriteObjectAny(&fn_impl_lookup, "std::map<std::string,std::string>", "_function_impl_lookup");
130 }
131 };
132 #endif // minitreedataset_h
std
fv::ObservedValue
A value supplied by the dataset, not derived.
Definition: value.hpp:448
fv
The namespace containing all filval classes and functions.
Definition: api.hpp:38
fv::Value
A templated value.
Definition: value.hpp:415
fv::root
Definition: api.hpp:8
MiniTree.hpp
fv::PointerValue
A Value of a pointer.
Definition: value.hpp:1208