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