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  // Maps filenames to data category. Either "signal" or "background"
49  std::map<std::string,std::string> input_categories;
50  // Maps filenames to data label, eg. "TTTT", or "TTZ"
51  std::map<std::string,std::string> input_labels;
52  std::string output_filename;
53  TFile* output_file;
54  long next_entry;
55  long nentries;
56  bool load_next(){
57  if (next_entry >= nentries) return false;
58  fChain->GetEntry(next_entry);
59  ++next_entry;
60  return true;
61  }
62  int get_events(){
63  return nentries;
64  }
65  int get_current_event(){
66  return next_entry-1;
67  }
68 
69  void save_event_count_and_xsection(){
70  std::map<std::string,int> event_counts;
71  std::map<std::string,float> xsecs;
72  string fname, label;
73  for(auto& p : input_labels){
74  std::tie(fname, label) = p;
75  TFile f(fname.c_str());
76  TH1D* count = (TH1D*)f.Get("Count");
77  event_counts[label] = (int)count->GetBinContent(1);
78 
79  TTree* tree = (TTree*)f.Get("tree");
80  TBranch* b = tree->GetBranch("xsec");
81  float xsec;
82  b->SetAddress(&xsec);
83  b->GetEntry(1);
84  xsecs[label] = xsec;
85  }
86  output_file->cd();
87  gDirectory->WriteObjectAny(&event_counts, "std::map<std::string,int>", "_event_counts");
88  gDirectory->WriteObjectAny(&xsecs, "std::map<std::string,float>", "_xsecs");
89  }
90 
91  public:
92  MiniTreeDataSet(const std::string& output_filename, const std::string input_filename, const std::string data_label)
93  :DataSet(),
94  input_categories({ {input_filename, "signal"} }),
95  input_labels({ {input_filename, data_label} }),
96  output_filename(output_filename),
97  next_entry(0) {
98  TChain* chain = new TChain("tree");
99  chain->Add(input_filename.c_str());
100  Init(chain);
101  nentries = fChain->GetEntries();
102  output_file = TFile::Open(output_filename.c_str(), "RECREATE");
103  this->fChain->SetBranchStatus("*", false);
104  }
105 
106  MiniTreeDataSet(const std::string& output_filename, const std::map<std::string,std::string>& filenames_with_labels)
107  :DataSet(),
108  input_categories(filenames_with_labels),
109  output_filename(output_filename),
110  next_entry(0) {
111  TChain* chain = new TChain("tree");
112  for(auto& p : filenames_with_labels){
113  std::string filename;
114  std::tie(filename, std::ignore) = p;
115  chain->Add(filename.c_str());
116  }
117  Init(chain);
118  nentries = fChain->GetEntries();
119  output_file = TFile::Open(output_filename.c_str(), "RECREATE");
120  this->fChain->SetBranchStatus("*", false);
121  }
122 
123  ~MiniTreeDataSet(){
124  save_event_count_and_xsection();
125  output_file->Close();
126  }
127 
128  const std::string& get_current_event_label() const{
129  TFile* file = fChain->GetFile();
130  std::string filename = file->GetName();
131  return input_categories.at(filename);
132  }
133 
134  template <typename T>
135  Value<T>* track_branch(const std::string& bname){
136  TBranch* branch = fChain->GetBranch(bname.c_str());
137  if (branch == nullptr){
138  CRITICAL("Branch: " << bname << " does not exist in input tree.", -1);
139  }
140  T* bref = (T*) branch->GetAddress();
141  fChain->SetBranchStatus(bname.c_str(), true);
142  INFO("Registering branch \"" << bname
143  << "\" with address " << bref
144  << " and type " << typeid(bref).name());
145  return new ObservedValue<T>(bname, bref);
146  }
147 
148  template <typename T>
149  Value<T*>* track_branch_ptr(const std::string& bname){
150  TBranch* branch = fChain->GetBranch(bname.c_str());
151  if (branch == nullptr){
152  CRITICAL("Branch: " << bname << " does not exist in input tree.", -1);
153  }
154  T* bref = (T*) branch->GetAddress();
155  fChain->SetBranchStatus(bname.c_str(), true);
156  INFO("Registering pointer branch \"" << bname
157  << "\" with address " << bref
158  << " and type " << typeid(bref).name());
159  return new PointerValue<T>(bname, bref);
160  }
161 
162  template <typename T>
163  decltype(auto) track_branch_vec(const std::string& size_bname, const std::string& value_bname){
164  track_branch_ptr<T>(value_bname);
165  return wrapper_vector<T>(lookup<int>(size_bname), lookup<T*>(value_bname), value_bname);
166  }
167 
168  void save_all(){
169  output_file->cd();
170 
171  // Save the value names for each container to enable looking up
172  // what values are plotted
173  std::map<string,string> value_lookup = this->get_container_name_value_map();
174  gDirectory->WriteObjectAny(&value_lookup, "std::map<std::string,std::string>", "_value_lookup");
175 
176  std::map<string,string> fn_impl_lookup = this->get_function_name_impl_map();
177  gDirectory->WriteObjectAny(&fn_impl_lookup, "std::map<std::string,std::string>", "_function_impl_lookup");
178 
179  for(auto container : containers){
180  container.second->save_as("outfile", SaveOption::ROOT);
181  }
182  }
183 };
184 #endif // minitreedataset_h
STL namespace.
A value supplied by the dataset, not derived.
Definition: value.hpp:450
The namespace containing all filval classes and functions.
Definition: api.hpp:46
A templated value.
Definition: value.hpp:265
Definition: api.hpp:8
A Value of a pointer.
Definition: value.hpp:1213