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 #include "TFile.h"
7 #include "TH1.h"
8 #include "TH2.h"
9 #include "TGraph.h"
10 #include "TCanvas.h"
11 #include "TROOT.h"
12 
13 #include "filval.hpp"
14 
15 namespace fv::root::util{
16 void _save_img(TObject* container, const std::string& fname){
17  TCanvas* c1 = new TCanvas("c1");
18  container->Draw();
19  c1->Draw();
20  c1->SaveAs(fname.c_str());
21  delete c1;
22 }
23 
24 void _save_bin(TObject* container){
25  INFO("Saving object: " << container->GetName() << " into file " << gDirectory->GetName());
26  container->Write(container->GetName(), TObject::kOverwrite);
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 void _save_as_stl(void* container, const std::string& fname, const std::string& type_name,
43  const std::string& obj_name,
44  const SaveOption& option = SaveOption::PNG) {
45  switch(option){
46  case PNG:
47  INFO("Cannot save STL container " << type_name <<" as png");
48  break;
49  case PDF:
50  INFO("Cannot save STL container " << type_name <<" as pdf");
51  break;
52  case ROOT:
53  std::cout << "writing object of type \"" << type_name << "\"" << std::endl;
54  gDirectory->WriteObjectAny(container, type_name.c_str(), obj_name.c_str());
55  break;
56  default:
57  break;
58  }
59 }
60 }
61 
62 namespace fv::root {
63 
64 template <typename V>
65 class _ContainerTH1 : public Container<TH1>{
66  private:
67  void _fill(){
68  if (container == nullptr){
69  if (value == nullptr){
70  CRITICAL("Container: \"" << get_name() << "\" has a null Value object. "
71  << "Probably built with imcompatible type",-1);
72  }
73  this->container = new TH1D(this->get_name().c_str(), this->title.c_str(),
74  this->nbins, this->low, this->high);
75  }
76  _do_fill();
77  }
78 
79  protected:
80  std::string title;
81  int nbins;
82  double low;
83  double high;
84  Value<V> *value;
85 
86  virtual void _do_fill() = 0;
87 
88  public:
89  explicit _ContainerTH1(const std::string &name, const std::string& title, GenValue *value,
90  int nbins, double low, double high)
91  :Container<TH1>(name, nullptr),
92  title(title), nbins(nbins), low(low), high(high),
93  value(dynamic_cast<Value<V>*>(value)) { }
94 
95  void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
96  util::_save_as(get_container(), fname, option);
97  }
98 };
99 
100 template <typename V>
101 class ContainerTH1 : public _ContainerTH1<V>{
103  void _do_fill(){
104  this->container->Fill(this->value->get_value());
105  }
106 };
107 
108 template <typename V>
109 class ContainerTH1Many : public _ContainerTH1<std::vector<V>>{
111  void _do_fill(){
112  for(V x : this->value->get_value())
113  this->container->Fill(x);
114  }
115 };
116 
117 
118 
119 template <typename V>
120 class _ContainerTH2 : public Container<TH2>{
121  private:
122  void _fill(){
123  if (container == nullptr){
124  if (value == nullptr){
125  CRITICAL("Container: \"" << get_name() << "\" has a null Value object. "
126  << "Probably built with imcompatible type",-1);
127  }
128  this->container = new TH2D(this->get_name().c_str(), this->title.c_str(),
129  this->nbins_x, this->low_x, this->high_x,
130  this->nbins_y, this->low_y, this->high_y);
131  }
132  _do_fill(value->get_value());
133  }
134 
135  protected:
136  std::string title;
137  int nbins_x;
138  int nbins_y;
139  double low_x;
140  double low_y;
141  double high_x;
142  double high_y;
143  Value<std::pair<V,V>> *value;
144 
145  virtual void _do_fill(std::pair<V,V>& val) = 0;
146 
147  public:
148  explicit _ContainerTH2(const std::string& name, const std::string& title,
149  GenValue* value,
150  int nbins_x, double low_x, double high_x,
151  int nbins_y, double low_y, double high_y)
152  :Container<TH2>(name, nullptr),
153  nbins_x(nbins_x), low_x(low_x), high_x(high_x),
154  nbins_y(nbins_y), low_y(low_y), high_y(high_y),
155  value(dynamic_cast<Value<std::pair<V, V>>*>(value)) { }
156 
157  void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
158  util::_save_as(get_container(), fname, option);
159  }
160 };
161 
162 template <typename V>
163 class ContainerTH2 : public _ContainerTH2<std::vector<V>>{
165  void _do_fill(std::pair<V,V>& val){
166  this->container->Fill(val.first,val.second);
167  }
168 };
169 
170 template <typename V>
171 class ContainerTH2Many : public _ContainerTH2<std::vector<V>>{
173  void _do_fill(std::pair<std::vector<V>,std::vector<V>>& val){
174  int min_size = std::min(val.first.size(), val.second.size());
175  for(int i=0; i<min_size; i++)
176  this->container->Fill(val.first[i],val.second[i]);
177  }
178 };
179 
180 class ContainerTGraph : public Container<TGraph>{
181  private:
182  Value<std::pair<int, int> > *value;
183  std::vector<int> x_data;
184  std::vector<int> y_data;
185  std::string title;
186  bool data_modified;
187  void _fill(){
188  auto val = value->get_value();
189  x_data.push_back(val.first);
190  y_data.push_back(val.second);
191  data_modified = true;
192  }
193  public:
194  ContainerTGraph(const std::string& name, const std::string& title, GenValue* value)
195  :Container<TGraph>(name, new TGraph()),
196  value(dynamic_cast<Value<std::pair<int, int> >*>(value)),
197  data_modified(false){ }
198 
199  TGraph* get_container(){
200  if (data_modified){
201  delete container;
202  container = new TGraph(x_data.size(), x_data.data(), y_data.data());
203  container->SetName(get_name().c_str());
204  container->SetTitle(title.c_str());
205  data_modified = false;
206  }
207  return container;
208  }
209  void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
210  util::_save_as(get_container(), fname, option);
211  }
212 };
213 
214 
215 template <typename V, typename D>
216 class _Counter : public Container<std::map<D,int>>{
217  protected:
218  Value<V>* value;
219  std::string type_name;
220  public:
221  explicit _Counter(const std::string& name, GenValue* value, const std::string& type_name = "")
222  :Container<std::map<D,int>>(name, new std::map<D,int>()),
223  value(dynamic_cast<Value<V>*>(value)),
224  type_name(type_name){ }
225 
226  void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
227  if(type_name != ""){
228  util::_save_as_stl(this->get_container(), fname, type_name, this->get_name(), option);
229  }
230  }
231 
232 };
233 
234 template <typename V>
235 class Counter : public _Counter<V,V>{
237  void _fill(){
238  V& val = this->value->get_value();
239  int curr = (*this->container)[val];
240  (*this->container)[val] = curr + 1;
241  }
242 };
243 
244 template <typename V>
245 class CounterMany : public _Counter<std::vector<V>,V>{
247  void _fill(){
248  for(V& val : this->value->get_value()){
249  std::map<V,int>* m= this->container;
250  int count;
251  if (m->find(val) == m->end()){
252  count = 1;
253  } else{
254  count = m->at(val);
255  }
256  m->emplace(val, count);
257  }
258  }
259 };
260 
261 class CounterManyI : public CounterMany<int>{
262  public:
263  CounterManyI(const std::string& name, GenValue* value)
264  :CounterMany<int>(name, value, "std::map<int,int>"){ }
265 };
266 
267 }
268 #endif // root_container_hpp
Definition: value.hpp:183
Definition: container.hpp:216
Definition: container.hpp:15
Definition: container.hpp:101
Definition: container.hpp:245
Definition: container.hpp:120
Definition: container.hpp:235
Definition: container.hpp:65
Definition: container.hpp:261
Definition: container.hpp:109
Definition: container.hpp:171
Definition: container.hpp:163
virtual T & get_value()=0
Calculate, if necessary, and return the value held by this object.
Definition: container.hpp:180
Definition: container.hpp:54