TTTT Analysis  0.1
container.hpp
1 #ifndef root_container_hpp
2 #define root_container_hpp
3 #include <utility>
4 #include "../filval/filval.hpp"
5 #include "TH1.h"
6 #include "TH2.h"
7 #include "TGraph.h"
8 #include <iostream>
9 
10 namespace filval::root {
11 
12 template <typename T>
13 class ContainerTH1 : public Container<TH1>{
14  private:
15  void _fill(){
16  container->Fill(value->get_value());
17  }
18  protected:
19  Value<T> *value;
20  ContainerTH1(TH1* container, const std::string &name, Value<T> *value)
21  :Container<TH1>(container, name),
22  value(value){ }
23 };
24 
25 class ContainerTH1D : public ContainerTH1<double>{
26  public:
27  ContainerTH1D(const std::string& name, const std::string& title,
28  int nbins, double low, double high, GenValue* value)
29  :ContainerTH1<double>(NULL, name, dynamic_cast<Value<double>*>(value)){
30  this->container = new TH1D(name.c_str(), title.c_str(), nbins, low, high);
31  }
32 };
33 
34 class ContainerTH1F : public ContainerTH1<float>{
35  public:
36  ContainerTH1F(const std::string& name, const std::string& title,
37  int nbins, float low, float high, GenValue* value)
38  :ContainerTH1<float>(NULL, name, dynamic_cast<Value<float>*>(value)){
39  this->container = new TH1D(name.c_str(), title.c_str(), nbins, low, high);
40  }
41 };
42 
43 class ContainerTH1I : public ContainerTH1<int>{
44  public:
45  ContainerTH1I(const std::string& name, const std::string& title,
46  int nbins, int low, int high, GenValue* value)
47  :ContainerTH1<int>(NULL, name, dynamic_cast<Value<int>*>(value)){
48  this->container = new TH1I(name.c_str(), title.c_str(), nbins, low, high);
49  }
50 };
51 
52 
53 template <typename T>
54 class ContainerTH2 : public Container<TH2>{
55  private:
56  void _fill(){
57  std::pair<T, T> val = value->get_value();
58  container->Fill(val.first, val.second);
59  }
60  protected:
61  Value<std::pair<T, T> > *value;
62  ContainerTH2(TH2* container, const std::string &name, Value<std::pair<T, T> > *value)
63  :Container<TH2>(container, name),
64  value(value){ }
65 };
66 
67 class ContainerTH2D : public ContainerTH2<double>{
68  public:
69  ContainerTH2D(const std::string& name, const std::string& title,
70  int nbins_x, double low_x, double high_x,
71  int nbins_y, double low_y, double high_y,
72  GenValue* value)
73  :ContainerTH2<double>(NULL, name, dynamic_cast<Value<std::pair<double, double> >*>(value)){
74  this->container = new TH2D(name.c_str(), title.c_str(), nbins_x, low_x, high_x, nbins_y, low_y, high_y);
75  }
76 };
77 
78 class ContainerTH2I : public ContainerTH2<int>{
79  public:
80  ContainerTH2I(const std::string& name, const std::string& title,
81  int nbins_x, int low_x, int high_x,
82  int nbins_y, int low_y, int high_y,
83  GenValue* value)
84  :ContainerTH2<int>(NULL, name, dynamic_cast<Value<std::pair<int, int> >*>(value)){
85  this->container = new TH2I(name.c_str(), title.c_str(), nbins_x, low_x, high_x, nbins_y, low_y, high_y);
86  }
87 };
88 
89 class ContainerTGraph : public Container<TGraph>{
90  private:
92  std::vector<int> x_data;
93  std::vector<int> y_data;
94  bool data_modified;
95  void _fill(){
96  auto val = value->get_value();
97  x_data.push_back(val.first);
98  y_data.push_back(val.second);
99  /* std::cout << x_data.size() << std::endl; */
100  data_modified = true;
101  }
102  public:
103  ContainerTGraph(const std::string &name, GenValue* value)
104  :Container<TGraph>(new TGraph(), name),
105  value(dynamic_cast<Value<std::pair<int, int> >*>(value)),
106  data_modified(false){ }
107 
108  TGraph* get_container(){
109  if (data_modified){
110  delete container;
111  container = new TGraph(x_data.size(), x_data.data(), y_data.data());
112  data_modified = false;
113  }
114  return container;
115  }
116 };
117 
118 }
119 #endif // root_container_hpp
Definition: container.hpp:25
virtual T & get_value()=0
Calculate, if necessary, and return the value held by this object.
A type-agnostic value.
Definition: value.hpp:64
Definition: container.hpp:10
Definition: container.hpp:78
Definition: container.hpp:54
Definition: container.hpp:39
Definition: container.hpp:67
Definition: container.hpp:34
Definition: container.hpp:13
A generic value.
Definition: value.hpp:124
Definition: container.hpp:43
Definition: container.hpp:89