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