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 
58 void save_as_stl(void* container, const std::string& type_name,
59  const std::string& obj_name,
60  const SaveOption& option = SaveOption::PNG) {
61  switch(option){
62  case PNG:
63  INFO("Cannot save STL container " << type_name <<" as png");
64  break;
65  case PDF:
66  INFO("Cannot save STL container " << type_name <<" as pdf");
67  break;
68  case ROOT:
69  gDirectory->WriteObjectAny(container, type_name.c_str(), obj_name.c_str());
70  break;
71  default:
72  break;
73  }
74 }
75 }
76 
77 namespace fv::root {
78 
79 template <typename V>
80 class _ContainerTH1 : public Container<TH1>{
81  private:
82  void _fill(){
83  if (container == nullptr){
84  if (value == nullptr){
85  CRITICAL("Container: \"" << get_name() << "\" has a null Value object. "
86  << "Probably built with imcompatible type",-1);
87  }
88  this->container = new TH1D(this->get_name().c_str(), this->title.c_str(),
89  this->nbins, this->low, this->high);
90  }
91  _do_fill();
92  }
93 
94  protected:
95  std::string title;
96  int nbins;
97  double low;
98  double high;
99  Value<V> *value;
100 
101  virtual void _do_fill() = 0;
102 
103  public:
104  explicit _ContainerTH1(const std::string &name, const std::string& title, Value<V>* value,
105  int nbins, double low, double high)
106  :Container<TH1>(name, nullptr),
107  title(title), nbins(nbins), low(low), high(high),
108  value(value) { }
109 
110  void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
111  util::save_as(get_container(), fname, option);
112  }
113 };
114 
115 template <typename V>
116 class ContainerTH1 : public _ContainerTH1<V>{
118  void _do_fill(){
119  this->container->Fill(this->value->get_value());
120  }
121 };
122 
123 template <typename V>
124 class ContainerTH1Many : public _ContainerTH1<std::vector<V>>{
126  void _do_fill(){
127  for(V x : this->value->get_value())
128  this->container->Fill(x);
129  }
130 };
131 
132 
133 
134 template <typename V>
135 class _ContainerTH2 : public Container<TH2>{
136  private:
137  void _fill(){
138  if (container == nullptr){
139  if (value == nullptr){
140  CRITICAL("Container: \"" << get_name() << "\" has a null Value object. "
141  << "Probably built with imcompatible type",-1);
142  }
143  this->container = new TH2D(this->get_name().c_str(), this->title.c_str(),
144  this->nbins_x, this->low_x, this->high_x,
145  this->nbins_y, this->low_y, this->high_y);
146  }
147  _do_fill(value->get_value());
148  }
149 
150  protected:
151  std::string title;
152  int nbins_x;
153  int nbins_y;
154  double low_x;
155  double low_y;
156  double high_x;
157  double high_y;
158  Value<std::pair<V,V>> *value;
159 
160  virtual void _do_fill(std::pair<V,V>& val) = 0;
161 
162  public:
163  explicit _ContainerTH2(const std::string& name, const std::string& title,
164  Value<std::pair<V, V>>* value,
165  int nbins_x, double low_x, double high_x,
166  int nbins_y, double low_y, double high_y)
167  :Container<TH2>(name, nullptr),
168  nbins_x(nbins_x), low_x(low_x), high_x(high_x),
169  nbins_y(nbins_y), low_y(low_y), high_y(high_y),
170  value(value) { }
171 
172  void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
173  util::save_as(get_container(), fname, option);
174  }
175 };
176 
177 template <typename V>
178 class ContainerTH2 : public _ContainerTH2<std::vector<V>>{
180  void _do_fill(std::pair<V,V>& val){
181  this->container->Fill(val.first,val.second);
182  }
183 };
184 
185 template <typename V>
186 class ContainerTH2Many : public _ContainerTH2<std::vector<V>>{
188  void _do_fill(std::pair<std::vector<V>,std::vector<V>>& val){
189  int min_size = std::min(val.first.size(), val.second.size());
190  for(int i=0; i<min_size; i++)
191  this->container->Fill(val.first[i],val.second[i]);
192  }
193 };
194 
195 class ContainerTGraph : public Container<TGraph>{
196  private:
197  Value<std::pair<int, int> > *value;
198  std::vector<int> x_data;
199  std::vector<int> y_data;
200  std::string title;
201  bool data_modified;
202  void _fill(){
203  auto val = value->get_value();
204  x_data.push_back(val.first);
205  y_data.push_back(val.second);
206  data_modified = true;
207  }
208  public:
209  ContainerTGraph(const std::string& name, const std::string& title, Value<std::pair<int, int>>* value)
210  :Container<TGraph>(name, new TGraph()),
211  value(value),
212  data_modified(false){ }
213 
214  TGraph* get_container(){
215  if (data_modified){
216  delete container;
217  container = new TGraph(x_data.size(), x_data.data(), y_data.data());
218  container->SetName(get_name().c_str());
219  container->SetTitle(title.c_str());
220  data_modified = false;
221  }
222  return container;
223  }
224  void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
225  util::save_as(get_container(), fname, option);
226  }
227 };
228 
229 
230 template <typename V, typename D>
231 class _Counter : public Container<std::map<D,int>>{
232  protected:
233  Value<V>* value;
234  public:
235  explicit _Counter(const std::string& name, Value<V>* value)
236  :Container<std::map<D,int>>(name, new std::map<D,int>()),
237  value(value) { }
238 
239  void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
240  std::string type_name = "std::map<"+fv::util::get_type_name(typeid(D))+",int>";
241  util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
242  }
243 
244 };
245 
250 template <typename V>
251 class Counter : public _Counter<V,V>{
253  void _fill(){
254  (*this->container)[this->value->get_value()]++;
255  }
256 };
257 
261 template <typename V>
262 class CounterMany : public _Counter<std::vector<V>,V>{
264  void _fill(){
265  for(V& val : this->value->get_value())
266  (*this->container)[val]++;
267  }
268 };
269 }
270 #endif // root_container_hpp
Definition: container.hpp:231
Definition: container.hpp:16
Definition: container.hpp:116
Same as Counter but accepts multiple values per fill.
Definition: container.hpp:262
Definition: container.hpp:135
A Counter that keeps a mapping of the number of occurances of each input value.
Definition: container.hpp:251
Definition: container.hpp:80
Definition: container.hpp:124
Definition: container.hpp:186
Definition: api.hpp:8
Definition: container.hpp:178
virtual T & get_value()=0
Calculate, if necessary, and return the value held by this object.
Definition: container.hpp:195
Definition: container.hpp:73