TTTT Analysis  0.1
container.hpp
1 #ifndef root_container_hpp
2 #define root_container_hpp
3 #include <iostream>
4 #include <utility>
5 #include <map>
6 
7 #include "TROOT.h"
8 #include "TFile.h"
9 #include "TCanvas.h"
10 #include "TGraph.h"
11 #include "TH1.h"
12 #include "TH2.h"
13 
14 #include "TMVA/Factory.h"
15 #include "TMVA/DataLoader.h"
16 
17 #include "filval/container.hpp"
18 
19 namespace fv::root::util{
25  void save_as(TObject* container, const std::string& fname, const SaveOption& option = SaveOption::PNG) {
26 
27  auto save_img = [](TObject* container, const std::string& fname){
28  TCanvas* c1 = new TCanvas("c1");
29  container->Draw();
30  c1->Draw();
31  c1->SaveAs(fname.c_str());
32  delete c1;
33  };
34 
35  auto save_bin = [](TObject* container){
36  INFO("Saving object: " << container->GetName() << " into file " << gDirectory->GetName());
37  container->Write(container->GetName(), TObject::kOverwrite);
38  };
39 
40  switch(option){
41  case PNG:
42  save_img(container, fname+".png"); break;
43  case PDF:
44  save_img(container, fname+".pdf"); break;
45  case ROOT:
46  save_bin(container); break;
47  default:
48  break;
49  }
50  }
51 
52 
64  void save_as_stl(void* container, const std::string& type_name,
65  const std::string& obj_name,
66  const SaveOption& option = SaveOption::PNG) {
67  switch(option){
68  case PNG:
69  INFO("Cannot save STL container " << type_name <<" as png");
70  break;
71  case PDF:
72  INFO("Cannot save STL container " << type_name <<" as pdf");
73  break;
74  case ROOT:
75  gDirectory->WriteObjectAny(container, type_name.c_str(), obj_name.c_str());
76  break;
77  default:
78  break;
79  }
80  }
81 }
82 
83 namespace fv::root {
84 
85 template <typename V>
86 class _ContainerTH1 : public Container<TH1,V>{
87  private:
88  void _fill(){
89  if (this->container == nullptr){
90  if (this->value == nullptr){
91  CRITICAL("Container: \"" << this->get_name() << "\" has a null Value object. "
92  << "Probably built with imcompatible type",-1);
93  }
94  this->container = new TH1D(this->get_name().c_str(), this->title.c_str(),
95  this->nbins, this->low, this->high);
96  this->container->SetXTitle(label_x.c_str());
97  this->container->SetYTitle(label_y.c_str());
98  }
99  _do_fill();
100  }
101 
102  protected:
103  std::string title;
104  std::string label_x;
105  std::string label_y;
106  int nbins;
107  double low;
108  double high;
109 
110  virtual void _do_fill() = 0;
111 
112  public:
113  explicit _ContainerTH1(const std::string &name, const std::string& title, Value<V>* value,
114  int nbins, double low, double high,
115  const std::string& label_x = "",
116  const std::string& label_y = "")
117  :Container<TH1,V>(name, value),
118  title(title), nbins(nbins), low(low), high(high),
119  label_x(label_x), label_y(label_y) { }
120 
121  void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
122  util::save_as(this->get_container(), fname, option);
123  }
124 };
125 
126 template <typename V>
127 class ContainerTH1 : public _ContainerTH1<V>{
128  using _ContainerTH1<V>::_ContainerTH1;
129  void _do_fill(){
130  this->container->Fill(this->value->get_value());
131  }
132 };
133 
134 template <typename V>
135 class ContainerTH1Many : public _ContainerTH1<std::vector<V>>{
136  using _ContainerTH1<std::vector<V>>::_ContainerTH1;
137  void _do_fill(){
138  for(V x : this->value->get_value())
139  this->container->Fill(x);
140  }
141 };
142 
143 
144 
145 template <typename V>
146 class _ContainerTH2 : public Container<TH2,std::pair<V,V>>{
147  private:
148  void _fill(){
149  if (this->container == nullptr){
150  if (this->value == nullptr){
151  CRITICAL("Container: \"" << this->get_name() << "\" has a null Value object. "
152  << "Probably built with imcompatible type",-1);
153  }
154  this->container = new TH2D(this->get_name().c_str(), this->title.c_str(),
155  this->nbins_x, this->low_x, this->high_x,
156  this->nbins_y, this->low_y, this->high_y);
157  this->container->SetXTitle(label_x.c_str());
158  this->container->SetYTitle(label_y.c_str());
159  }
160  _do_fill(this->value->get_value());
161  }
162 
163  protected:
164  std::string title;
165  std::string label_x;
166  std::string label_y;
167  int nbins_x;
168  int nbins_y;
169  double low_x;
170  double low_y;
171  double high_x;
172  double high_y;
173 
174  virtual void _do_fill(std::pair<V,V>& val) = 0;
175 
176  public:
177  explicit _ContainerTH2(const std::string& name, const std::string& title,
178  Value<std::pair<V, V>>* value,
179  int nbins_x, double low_x, double high_x,
180  int nbins_y, double low_y, double high_y,
181  const std::string& label_x = "",
182  const std::string& label_y = "")
183  :Container<TH2,std::pair<V,V>>(name, value),
184  title(title),
185  nbins_x(nbins_x), low_x(low_x), high_x(high_x),
186  nbins_y(nbins_y), low_y(low_y), high_y(high_y),
187  label_x(label_x), label_y(label_y) { }
188 
189  void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
190  util::save_as(this->get_container(), fname, option);
191  }
192 };
193 
194 template <typename V>
195 class ContainerTH2 : public _ContainerTH2<V>{
196  using _ContainerTH2<V>::_ContainerTH2;
197  void _do_fill(std::pair<V,V>& val){
198  this->container->Fill(val.first,val.second);
199  }
200 };
201 
202 template <typename V>
203 class ContainerTH2Many : public _ContainerTH2<std::vector<V>>{
204  using _ContainerTH2<std::vector<V>>::_ContainerTH2;
205  void _do_fill(std::pair<std::vector<V>,std::vector<V>>& val){
206  int min_size = std::min(val.first.size(), val.second.size());
207  for(int i=0; i<min_size; i++)
208  this->container->Fill(val.first[i],val.second[i]);
209  }
210 };
211 
212 template <typename V>
213 class ContainerTGraph : public Container<TGraph,std::pair<V,V>>{
214  private:
215  std::vector<V> x_data;
216  std::vector<V> y_data;
217  std::string title;
218  bool data_modified;
219  void _fill(){
220  auto val = this->value->get_value();
221  x_data.push_back(val.first);
222  y_data.push_back(val.second);
223  data_modified = true;
224  }
225  public:
226  ContainerTGraph(const std::string& name, const std::string& title, Value<std::pair<V, V>>* value)
227  :Container<TGraph,std::pair<V,V>>(name, value),
228  data_modified(false){
229  this->container = new TGraph();
230  }
231 
232  TGraph* get_container(){
233  if (data_modified){
234  delete this->container;
235  this->container = new TGraph(x_data.size(), x_data.data(), y_data.data());
236  this->container->SetName(this->get_name().c_str());
237  this->container->SetTitle(title.c_str());
238  data_modified = false;
239  }
240  return this->container;
241  }
242  void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
243  util::save_as(get_container(), fname, option);
244  }
245 };
246 
247 template <typename T>
248 class Vector : public Container<std::vector<T>,T>{
249  private:
250 
251  void _fill(){
252  this->container->push_back(this->value->get_value());
253  }
254  public:
255  Vector(const std::string& name, Value<T>* value)
256  :Container<std::vector<T>,T>(name, value){
257  this->container = new std::vector<T>;
258  }
259 
260  void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
261  std::string type_name = "std::vector<"+fv::util::get_type_name(typeid(T))+">";
262  util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
263  }
264 };
265 
266 template <typename V, typename D>
267 class _Counter : public Container<std::map<D,int>,V>{
268  public:
269  explicit _Counter(const std::string& name, Value<V>* value)
270  :Container<std::map<D,int>,V>(name, value) {
271  this->container = new std::map<D,int>;
272  }
273 
274  void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
275  std::string type_name = "std::map<"+fv::util::get_type_name(typeid(D))+",int>";
276  util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
277  }
278 
279 };
280 
281 
286 template <typename V>
287 class Counter : public _Counter<V,V>{
288  using _Counter<V,V>::_Counter;
289  void _fill(){
290  (*this->container)[this->value->get_value()]++;
291  }
292 };
293 
297 template <typename V>
298 class CounterMany : public _Counter<std::vector<V>,V>{
299  using _Counter<std::vector<V>,V>::_Counter;
300  void _fill(){
301  for(V& val : this->value->get_value())
302  (*this->container)[val]++;
303  }
304 };
305 
306 // TMVA::DataLoader *dataloader=new TMVA::DataLoader("dataset");
307 
308 template <typename... ArgTypes>
309 class MVA : public Container<TMVA::DataLoader,std::tuple<ArgTypes...>>{
310  private:
311 
312  void _fill(){
313  this->container->push_back(this->value->get_value());
314  std::vector<double>& v = t2v<double>(this->value->get_value());
315  this->container->AddSignalTrainingEvent(v, 1);
316  //TODO: Make custom Value type that includes explicit truth info as
317  //well as weights and ablity to specify testing/training dataset.
318  }
319  public:
320  MVA(const std::string& name, Value<std::tuple<ArgTypes...>>* value, const std::vector<std::string>& labels=std::vector<std::string>())
321  :Container<TMVA::DataLoader,std::tuple<ArgTypes...>(name, value){
322  this->container = new DataLoader(name);
323  if (labels.size() != sizeof...(ArgTypes)){
324  CRITICAL("Length of labels vector ("<<labels.size()<<") not equal to number of MVA arguments ("<<sizeof...(ArgTypes)<<")",-1)
325  }
326  for(std::string& label : labels){
327  this->container->AddVariable(label, 'F');
328  }
329  }
330 
331  void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
332  std::string type_name = "std::vector<"+fv::util::get_type_name(typeid(T))+">";
333  util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
334  }
335 };
336 }
337 #endif // root_container_hpp
Same as Counter but accepts multiple values per fill.
Definition: container.hpp:298
A Counter that keeps a mapping of the number of occurances of each input value.
Definition: container.hpp:287
STL namespace.
SaveOption
Enumeration of different options that can be used to save Containers.
Definition: container.hpp:69
A templated value.
Definition: value.hpp:265
Definition: api.hpp:8
A class that is used to "hold" values.
Definition: container.hpp:132