TTTT Analysis  0.1
container.hpp
1 #ifndef root_container_hpp
2 #define root_container_hpp
3 #include <utility>
4 #include <iostream>
5 #include "TFile.h"
6 #include "TH1.h"
7 #include "TH2.h"
8 #include "TGraph.h"
9 #include "TROOT.h"
10 
11 #include "filval.hpp"
12 
13 namespace fv::root::util{
14 void _save_img(TObject* container, const std::string& fname){
15  TCanvas* c1 = new TCanvas("c1");
16  container->Draw();
17  c1->Draw();
18  c1->SaveAs(fname.c_str());
19  delete c1;
20 }
21 
22 void _save_bin(TObject* container){
23  INFO("Saving object: " << container->GetName() << " into file " << gDirectory->GetName());
24  /* TFile* f = TFile::Open(fname.c_str(), "UPDATE"); */
25  container->Write(container->GetName(), TObject::kOverwrite);
26  /* f->Close(); */
27 }
28 
29 void _save_as(TObject* container, const std::string& fname, const SaveOption& option = SaveOption::PNG) {
30  switch(option){
31  case PNG:
32  _save_img(container, fname+".png"); break;
33  case PDF:
34  _save_img(container, fname+".pdf"); break;
35  case ROOT:
36  _save_bin(container); break;
37  default:
38  break;
39  }
40 }
41 }
42 
43 namespace fv::root {
44 
45 template <typename V, typename D>
46 class ContainerTH1 : public Container<TH1>{
47  private:
48  void _fill(){
49  if (container == nullptr){
50  if (value == nullptr){
51  CRITICAL("Container: \"" << get_name() << "\" has a null Value object. "
52  << "Probably built with imcompatible type",-1);
53  }
54  init_TH1();
55  }
56  _do_fill(value->get_value());
57  }
58 
59  protected:
60  std::string title;
61  int nbins;
62  D low;
63  D high;
64  Value<V> *value;
65  virtual void init_TH1() = 0;
66  virtual void _do_fill(V& val) = 0;
67 
68  public:
69  explicit ContainerTH1(const std::string &name, const std::string& title, GenValue *value,
70  int nbins, D low, D high)
71  :Container<TH1>(name, nullptr),
72  title(title), nbins(nbins), low(low), high(high),
73  value(dynamic_cast<Value<V>*>(value)) { }
74 
75  void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
76  util::_save_as(get_container(), fname, option);
77  }
78 };
79 
80 template <typename V>
81 class _ContainerTH1D : public ContainerTH1<V, double>{
83  void init_TH1(){
84  this->container = new TH1D(this->get_name().c_str(), this->title.c_str(),
85  this->nbins, this->low, this->high);
86  }
87 };
88 
89 class ContainerTH1D : public _ContainerTH1D<double>{
91  void _do_fill(double& val){
92  this->container->Fill(val);
93  }
94 };
95 
96 class ContainerTH1DMany : public _ContainerTH1D<std::vector<double>>{
98  void _do_fill(std::vector<double>& val){
99  for(double x : val)
100  this->container->Fill(x);
101  }
102 };
103 
104 
105 template <typename V>
106 class _ContainerTH1F : public ContainerTH1<V, float>{
108  void init_TH1(){
109  this->container = new TH1F(this->get_name().c_str(), this->title.c_str(),
110  this->nbins, this->low, this->high);
111  }
112 };
113 
114 class ContainerTH1F : public _ContainerTH1F<float>{
116  void _do_fill(float& val){
117  this->container->Fill(val);
118  }
119 };
120 
121 class ContainerTH1FMany : public _ContainerTH1F<std::vector<float>>{
123  void _do_fill(std::vector<float>& val){
124  for(float x : val)
125  this->container->Fill(x);
126  }
127 };
128 
129 
130 template <typename V>
131 class _ContainerTH1I : public ContainerTH1<V, int>{
133  void init_TH1(){
134  this->container = new TH1I(this->get_name().c_str(), this->title.c_str(),
135  this->nbins, this->low, this->high);
136  }
137 };
138 
139 class ContainerTH1I : public _ContainerTH1I<int>{
141  void _do_fill(int& val){
142  this->container->Fill(val);
143  }
144 };
145 
146 class ContainerTH1IMany : public _ContainerTH1I<std::vector<int>>{
148  void _do_fill(std::vector<int>& val){
149  for(int x : val)
150  this->container->Fill(x);
151  }
152 };
153 
154 
155 template <typename V, typename D>
156 class ContainerTH2 : public Container<TH2>{
157  private:
158  void _fill(){
159  if (container == nullptr){
160  if (value == nullptr){
161  CRITICAL("Container: \"" << get_name() << "\" has a null Value object. "
162  << "Probably built with imcompatible type",-1);
163  }
164  init_TH2();
165  }
166  _do_fill(value->get_value());
167  }
168 
169  protected:
170  std::string title;
171  int nbins_x;
172  int nbins_y;
173  D low_x;
174  D low_y;
175  D high_x;
176  D high_y;
177  Value<std::pair<V,V>> *value;
178  virtual void init_TH2() = 0;
179  virtual void _do_fill(std::pair<V,V>& val) = 0;
180 
181  public:
182  explicit ContainerTH2(const std::string& name, const std::string& title,
183  GenValue* value,
184  int nbins_x, D low_x, D high_x,
185  int nbins_y, D low_y, D high_y)
186  :Container<TH2>(name, nullptr),
187  nbins_x(nbins_x), low_x(low_x), high_x(high_x),
188  nbins_y(nbins_y), low_y(low_y), high_y(high_y),
189  value(dynamic_cast<Value<std::pair<V, V>>*>(value)) { }
190 
191  void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
192  util::_save_as(get_container(), fname, option);
193  }
194 };
195 
196 template <typename V>
197 class _ContainerTH2D : public ContainerTH2<V, double>{
199  void init_TH2(){
200  this->container = new TH2D(this->get_name().c_str(), this->title.c_str(),
201  this->nbins_x, this->low_x, this->high_x,
202  this->nbins_y, this->low_y, this->high_y);
203  }
204 };
205 
206 class ContainerTH2D : public _ContainerTH2D<double>{
208  void _do_fill(std::pair<double,double>& val){
209  this->container->Fill(val.first, val.second);
210  }
211 };
212 
213 class ContainerTH2DMany : public _ContainerTH2D<std::vector<double>>{
215  void _do_fill(std::pair<std::vector<double>,std::vector<double>>& val){
216  int min_size = std::min(val.first.size(), val.second.size());
217  for(int i=0; i<min_size; i++)
218  this->container->Fill(val.first[i],val.second[i]);
219  }
220 };
221 
222 template <typename V>
223 class _ContainerTH2F : public ContainerTH2<V, float>{
225  void init_TH2(){
226  this->container = new TH2F(this->get_name().c_str(), this->title.c_str(),
227  this->nbins_x, this->low_x, this->high_x,
228  this->nbins_y, this->low_y, this->high_y);
229  }
230 };
231 
232 class ContainerTH2F : public _ContainerTH2F<float>{
234  void _do_fill(std::pair<float,float>& val){
235  this->container->Fill(val.first, val.second);
236  }
237 };
238 
239 class ContainerTH2FMany : public _ContainerTH2F<std::vector<float>>{
241  void _do_fill(std::pair<std::vector<float>,std::vector<float>>& val){
242  int min_size = std::min(val.first.size(), val.second.size());
243  for(int i=0; i<min_size; i++)
244  this->container->Fill(val.first[i],val.second[i]);
245  }
246 };
247 
248 template <typename V>
249 class _ContainerTH2I : public ContainerTH2<V, int>{
251  void init_TH2(){
252  this->container = new TH2I(this->get_name().c_str(), this->title.c_str(),
253  this->nbins_x, this->low_x, this->high_x,
254  this->nbins_y, this->low_y, this->high_y);
255  }
256 };
257 
258 class ContainerTH2I : public _ContainerTH2I<int>{
260  void _do_fill(std::pair<int,int>& val){
261  this->container->Fill(val.first, val.second);
262  }
263 };
264 
265 class ContainerTH2IMany : public _ContainerTH2I<std::vector<int>>{
267  void _do_fill(std::pair<std::vector<int>,std::vector<int>>& val){
268  int min_size = std::min(val.first.size(), val.second.size());
269  for(int i=0; i<min_size; i++)
270  this->container->Fill(val.first[i],val.second[i]);
271  }
272 };
273 
274 class ContainerTGraph : public Container<TGraph>{
275  private:
276  Value<std::pair<int, int> > *value;
277  std::vector<int> x_data;
278  std::vector<int> y_data;
279  bool data_modified;
280  void _fill(){
281  auto val = value->get_value();
282  x_data.push_back(val.first);
283  y_data.push_back(val.second);
284  data_modified = true;
285  }
286  public:
287  ContainerTGraph(const std::string &name, GenValue* value)
288  :Container<TGraph>(name, new TGraph()),
289  value(dynamic_cast<Value<std::pair<int, int> >*>(value)),
290  data_modified(false){ }
291 
292  TGraph* get_container(){
293  if (data_modified){
294  delete container;
295  container = new TGraph(x_data.size(), x_data.data(), y_data.data());
296  container->SetName(get_name().c_str());
297  data_modified = false;
298  }
299  return container;
300  }
301  void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
302  util::_save_as(get_container(), fname, option);
303  }
304 };
305 
306 }
307 #endif // root_container_hpp
Definition: value.hpp:184
Definition: container.hpp:13
Definition: container.hpp:46
Definition: container.hpp:232
Definition: container.hpp:89
Definition: container.hpp:258
Definition: container.hpp:131
Definition: container.hpp:96
Definition: container.hpp:81
Definition: container.hpp:121
Definition: container.hpp:223
Definition: container.hpp:146
Definition: container.hpp:206
Definition: container.hpp:213
Definition: container.hpp:114
Definition: container.hpp:249
Definition: container.hpp:156
Definition: container.hpp:139
Definition: container.hpp:197
Definition: container.hpp:239
Definition: container.hpp:106
virtual T & get_value()=0
Calculate, if necessary, and return the value held by this object.
Definition: container.hpp:274
Definition: container.hpp:54
Definition: container.hpp:265