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::map<std::string,std::string> input_filenames_with_labels;
49  /* std::vector<TFile*> input_files; */
50  std::string output_filename;
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& output_filename, const std::string input_filename)
69  :DataSet(),
70  input_filenames_with_labels({ {input_filename, "signal"} }),
71  output_filename(output_filename),
72  next_entry(0) {
73  TChain* chain = new TChain("tree");
74  chain->Add(input_filename.c_str());
75  Init(chain);
76  nentries = fChain->GetEntries();
77  output_file = TFile::Open(output_filename.c_str(), "RECREATE");
78  this->fChain->SetBranchStatus("*", false);
79  }
80 
81  MiniTreeDataSet(const std::string& output_filename, const std::map<std::string,std::string>& filenames_with_labels)
82  :DataSet(),
83  input_filenames_with_labels(filenames_with_labels),
84  output_filename(output_filename),
85  next_entry(0) {
86  TChain* chain = new TChain("tree");
87  for(auto& p : filenames_with_labels){
88  std::string filename;
89  std::tie(filename, std::ignore) = p;
90  chain->Add(filename.c_str());
91  }
92  Init(chain);
93  nentries = fChain->GetEntries();
94  output_file = TFile::Open(output_filename.c_str(), "RECREATE");
95  this->fChain->SetBranchStatus("*", false);
96  }
97 
98  ~MiniTreeDataSet(){
99  output_file->Close();
100  }
101 
102  const std::string& get_current_event_label() const{
103  TFile* file = fChain->GetFile();
104  std::string filename = file->GetName();
105  return input_filenames_with_labels.at(filename);
106  }
107 
108  template <typename T>
109  Value<T>* track_branch(const std::string& bname){
110  TBranch* branch = fChain->GetBranch(bname.c_str());
111  if (branch == nullptr){
112  CRITICAL("Branch: " << bname << " does not exist in input tree.", -1);
113  }
114  T* bref = (T*) branch->GetAddress();
115  fChain->SetBranchStatus(bname.c_str(), true);
116  INFO("Registering branch \"" << bname
117  << "\" with address " << bref
118  << " and type " << typeid(bref).name());
119  return new ObservedValue<T>(bname, bref);
120  }
121 
122  template <typename T>
123  Value<T*>* track_branch_ptr(const std::string& bname){
124  TBranch* branch = fChain->GetBranch(bname.c_str());
125  if (branch == nullptr){
126  CRITICAL("Branch: " << bname << " does not exist in input tree.", -1);
127  }
128  T* bref = (T*) branch->GetAddress();
129  fChain->SetBranchStatus(bname.c_str(), true);
130  INFO("Registering pointer branch \"" << bname
131  << "\" with address " << bref
132  << " and type " << typeid(bref).name());
133  return new PointerValue<T>(bname, bref);
134  }
135 
136  template <typename T>
137  decltype(auto) track_branch_vec(const std::string& size_bname, const std::string& value_bname){
138  track_branch_ptr<T>(value_bname);
139  return wrapper_vector<T>(lookup<int>(size_bname), lookup<T*>(value_bname), value_bname);
140  }
141 
142  void save_all(){
143  output_file->cd();
144 
145  // Save the value names for each container to enable looking up
146  // what values are plotted
147  std::map<string,string> value_lookup = this->get_container_name_value_map();
148  gDirectory->WriteObjectAny(&value_lookup, "std::map<std::string,std::string>", "_value_lookup");
149 
150  std::map<string,string> fn_impl_lookup = this->get_function_name_impl_map();
151  gDirectory->WriteObjectAny(&fn_impl_lookup, "std::map<std::string,std::string>", "_function_impl_lookup");
152 
153  for(auto container : containers){
154  container.second->save_as("outfile", SaveOption::ROOT);
155  }
156  }
157 };
158 #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:1209