1 #ifndef root_container_hpp 2 #define root_container_hpp 22 void save_as(TObject* container,
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
24 auto save_img = [](TObject* container,
const std::string& fname){
25 TCanvas* c1 =
new TCanvas(
"c1");
28 c1->SaveAs(fname.c_str());
32 auto save_bin = [](TObject* container){
33 INFO(
"Saving object: " << container->GetName() <<
" into file " << gDirectory->GetName());
34 container->Write(container->GetName(), TObject::kOverwrite);
39 save_img(container, fname+
".png");
break;
41 save_img(container, fname+
".pdf");
break;
43 save_bin(container);
break;
61 void save_as_stl(
void* container,
const std::string& type_name,
62 const std::string& obj_name,
66 INFO(
"Cannot save STL container " << type_name <<
" as png");
69 INFO(
"Cannot save STL container " << type_name <<
" as pdf");
72 gDirectory->WriteObjectAny(container, type_name.c_str(), obj_name.c_str());
83 class _ContainerTH1 :
public Container<TH1,V>{
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);
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());
107 virtual void _do_fill() = 0;
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) { }
118 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
119 util::save_as(this->get_container(), fname, option);
123 template <
typename V>
124 class ContainerTH1 :
public _ContainerTH1<V>{
125 using _ContainerTH1<V>::_ContainerTH1;
127 this->container->Fill(this->value->get_value());
131 template <
typename V>
132 class ContainerTH1Many :
public _ContainerTH1<std::vector<V>>{
133 using _ContainerTH1<std::vector<V>>::_ContainerTH1;
135 for(V x : this->value->get_value())
136 this->container->Fill(x);
142 template <
typename V>
143 class _ContainerTH2 :
public Container<TH2,std::pair<V,V>>{
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);
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());
157 _do_fill(this->value->get_value());
171 virtual void _do_fill(std::pair<V,V>& val) = 0;
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),
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) { }
186 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
187 util::save_as(this->get_container(), fname, option);
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);
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]);
209 template <
typename V>
210 class ContainerTGraph :
public Container<TGraph,std::pair<V,V>>{
212 std::vector<V> x_data;
213 std::vector<V> y_data;
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;
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();
229 TGraph* get_container(){
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;
237 return this->container;
239 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
240 util::save_as(get_container(), fname, option);
244 template <
typename T>
245 class Vector :
public Container<std::vector<T>,T>{
249 this->container->push_back(this->value->get_value());
252 Vector(
const std::string& name, Value<T>* value)
253 :Container<
std::vector<T>,T>(name, value){
254 this->container =
new std::vector<T>;
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);
263 template <
typename V,
typename D>
264 class _Counter :
public Container<std::map<D,int>,V>{
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>;
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);
283 template <
typename V>
285 using _Counter<V,V>::_Counter;
287 (*this->container)[this->value->get_value()]++;
294 template <
typename V>
296 using _Counter<std::vector<V>,V>::_Counter;
298 for(V& val : this->value->get_value())
299 (*this->container)[val]++;
303 #endif // root_container_hpp Definition: container.hpp:16
Same as Counter but accepts multiple values per fill.
Definition: container.hpp:295
A Counter that keeps a mapping of the number of occurances of each input value.
Definition: container.hpp:284
SaveOption
Enumeration of different options that can be used to save Containers.
Definition: container.hpp:69