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>{
85  private:
86  void _fill(){
87  if (container == nullptr){
88  if (value == nullptr){
89  CRITICAL("Container: \"" << 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  Value<V> *value;
108 
109  virtual void _do_fill() = 0;
110 
111  public:
112  explicit _ContainerTH1(const std::string &name, const std::string& title, Value<V>* value,
113  int nbins, double low, double high,
114  const std::string& label_x = "",
115  const std::string& label_y = "")
116  :Container<TH1>(name, nullptr),
117  title(title), nbins(nbins), low(low), high(high),
118  label_x(label_x), label_y(label_y),
119  value(value) { }
120 
121  void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
122  util::save_as(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>{
147  private:
148  void _fill(){
149  if (container == nullptr){
150  if (value == nullptr){
151  CRITICAL("Container: \"" << 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(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  Value<std::pair<V,V>> *value;
174 
175  virtual void _do_fill(std::pair<V,V>& val) = 0;
176 
177  public:
178  explicit _ContainerTH2(const std::string& name, const std::string& title,
179  Value<std::pair<V, V>>* value,
180  int nbins_x, double low_x, double high_x,
181  int nbins_y, double low_y, double high_y,
182  const std::string& label_x = "",
183  const std::string& label_y = "")
184  :Container<TH2>(name, nullptr),
185  title(title),
186  nbins_x(nbins_x), low_x(low_x), high_x(high_x),
187  nbins_y(nbins_y), low_y(low_y), high_y(high_y),
188  label_x(label_x), label_y(label_y),
189  value(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 ContainerTH2 : public _ContainerTH2<V>{
198  using _ContainerTH2<V>::_ContainerTH2;
199  void _do_fill(std::pair<V,V>& val){
200  this->container->Fill(val.first,val.second);
201  }
202 };
203 
204 template <typename V>
205 class ContainerTH2Many : public _ContainerTH2<std::vector<V>>{
206  using _ContainerTH2<std::vector<V>>::_ContainerTH2;
207  void _do_fill(std::pair<std::vector<V>,std::vector<V>>& val){
208  int min_size = std::min(val.first.size(), val.second.size());
209  for(int i=0; i<min_size; i++)
210  this->container->Fill(val.first[i],val.second[i]);
211  }
212 };
213 
214 class ContainerTGraph : public Container<TGraph>{
215  private:
216  Value<std::pair<int, int> > *value;
217  std::vector<int> x_data;
218  std::vector<int> y_data;
219  std::string title;
220  bool data_modified;
221  void _fill(){
222  auto val = value->get_value();
223  x_data.push_back(val.first);
224  y_data.push_back(val.second);
225  data_modified = true;
226  }
227  public:
228  ContainerTGraph(const std::string& name, const std::string& title, Value<std::pair<int, int>>* value)
229  :Container<TGraph>(name, new TGraph()),
230  value(value),
231  data_modified(false){ }
232 
233  TGraph* get_container(){
234  if (data_modified){
235  delete container;
236  container = new TGraph(x_data.size(), x_data.data(), y_data.data());
237  container->SetName(get_name().c_str());
238  container->SetTitle(title.c_str());
239  data_modified = false;
240  }
241  return container;
242  }
243  void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
244  util::save_as(get_container(), fname, option);
245  }
246 };
247 
248 template <typename T>
249 class Vector : public Container<std::vector<T> >{
250  private:
251  Value<T>* value;
252 
253  void _fill(){
254  this->container->push_back(value->get_value());
255  }
256  public:
257  Vector(const std::string& name, std::vector<T>* container, Value<T>* value)
258  :Container<std::vector<T>>(name, container), value(value){ }
259 
260  Vector(const std::string& name, Value<T>* value)
261  :Container<std::vector<T>>(name, nullptr), value(value){
262  this->container = new std::vector<T>();
263  }
264 
265  void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
266  std::string type_name = "std::vector<"+fv::util::get_type_name(typeid(T))+">";
267  util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
268  }
269 };
270 
271 template <typename V, typename D>
272 class _Counter : public Container<std::map<D,int>>{
273  protected:
274  Value<V>* value;
275  public:
276  explicit _Counter(const std::string& name, Value<V>* value)
277  :Container<std::map<D,int>>(name, new std::map<D,int>()),
278  value(value) { }
279 
280  void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
281  std::string type_name = "std::map<"+fv::util::get_type_name(typeid(D))+",int>";
282  util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
283  }
284 
285 };
286 
287 
292 template <typename V>
293 class Counter : public _Counter<V,V>{
294  using _Counter<V,V>::_Counter;
295  void _fill(){
296  (*this->container)[this->value->get_value()]++;
297  }
298 };
299 
303 template <typename V>
304 class CounterMany : public _Counter<std::vector<V>,V>{
305  using _Counter<std::vector<V>,V>::_Counter;
306  void _fill(){
307  for(V& val : this->value->get_value())
308  (*this->container)[val]++;
309  }
310 };
311 }
312 #endif // root_container_hpp
Definition: container.hpp:16
Same as Counter but accepts multiple values per fill.
Definition: container.hpp:304
A Counter that keeps a mapping of the number of occurances of each input value.
Definition: container.hpp:293
SaveOption
Enumeration of different options that can be used to save Containers.
Definition: container.hpp:67
Definition: api.hpp:8