1 #ifndef root_container_hpp 2 #define root_container_hpp 23 void save_as(TObject* container,
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
25 auto save_img = [](TObject* container,
const std::string& fname){
26 TCanvas* c1 =
new TCanvas(
"c1");
29 c1->SaveAs(fname.c_str());
33 auto save_bin = [](TObject* container){
34 INFO(
"Saving object: " << container->GetName() <<
" into file " << gDirectory->GetName());
35 container->Write(container->GetName(), TObject::kOverwrite);
40 save_img(container, fname+
".png");
break;
42 save_img(container, fname+
".pdf");
break;
44 save_bin(container);
break;
62 void save_as_stl(
void* container,
const std::string& type_name,
63 const std::string& obj_name,
67 INFO(
"Cannot save STL container " << type_name <<
" as png");
70 INFO(
"Cannot save STL container " << type_name <<
" as pdf");
73 gDirectory->WriteObjectAny(container, type_name.c_str(), obj_name.c_str());
84 class _ContainerTH1 :
public Container<TH1>{
87 if (container ==
nullptr){
88 if (value ==
nullptr){
89 CRITICAL(
"Container: \"" << get_name() <<
"\" has a null Value object. " 90 <<
"Probably built with imcompatible type",-1);
92 this->container =
new TH1D(this->get_name().c_str(), this->title.c_str(),
93 this->nbins, this->low, this->high);
94 this->container->SetXTitle(label_x.c_str());
95 this->container->SetYTitle(label_y.c_str());
109 virtual void _do_fill() = 0;
112 explicit _ContainerTH1(
const std::string &name,
const std::string& title, Value<V>* value,
113 int nbins,
double low,
double high,
114 const std::string& label_x =
"",
115 const std::string& label_y =
"")
116 :Container<TH1>(name, nullptr),
117 title(title), nbins(nbins), low(low), high(high),
118 label_x(label_x), label_y(label_y),
121 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
122 util::save_as(get_container(), fname, option);
126 template <
typename V>
127 class ContainerTH1 :
public _ContainerTH1<V>{
128 using _ContainerTH1<V>::_ContainerTH1;
130 this->container->Fill(this->value->get_value());
134 template <
typename V>
135 class ContainerTH1Many :
public _ContainerTH1<std::vector<V>>{
136 using _ContainerTH1<std::vector<V>>::_ContainerTH1;
138 for(V x : this->value->get_value())
139 this->container->Fill(x);
145 template <
typename V>
146 class _ContainerTH2 :
public Container<TH2>{
149 if (container ==
nullptr){
150 if (value ==
nullptr){
151 CRITICAL(
"Container: \"" << get_name() <<
"\" has a null Value object. " 152 <<
"Probably built with imcompatible type",-1);
154 this->container =
new TH2D(this->get_name().c_str(), this->title.c_str(),
155 this->nbins_x, this->low_x, this->high_x,
156 this->nbins_y, this->low_y, this->high_y);
157 this->container->SetXTitle(label_x.c_str());
158 this->container->SetYTitle(label_y.c_str());
160 _do_fill(value->get_value());
173 Value<std::pair<V,V>> *value;
175 virtual void _do_fill(std::pair<V,V>& val) = 0;
178 explicit _ContainerTH2(
const std::string& name,
const std::string& title,
179 Value<std::pair<V, V>>* value,
180 int nbins_x,
double low_x,
double high_x,
181 int nbins_y,
double low_y,
double high_y,
182 const std::string& label_x =
"",
183 const std::string& label_y =
"")
184 :Container<TH2>(name, nullptr),
186 nbins_x(nbins_x), low_x(low_x), high_x(high_x),
187 nbins_y(nbins_y), low_y(low_y), high_y(high_y),
188 label_x(label_x), label_y(label_y),
191 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
192 util::save_as(get_container(), fname, option);
196 template <
typename V>
197 class ContainerTH2 :
public _ContainerTH2<V>{
198 using _ContainerTH2<V>::_ContainerTH2;
199 void _do_fill(std::pair<V,V>& val){
200 this->container->Fill(val.first,val.second);
204 template <
typename V>
205 class ContainerTH2Many :
public _ContainerTH2<std::vector<V>>{
206 using _ContainerTH2<std::vector<V>>::_ContainerTH2;
207 void _do_fill(std::pair<std::vector<V>,std::vector<V>>& val){
208 int min_size = std::min(val.first.size(), val.second.size());
209 for(
int i=0; i<min_size; i++)
210 this->container->Fill(val.first[i],val.second[i]);
214 class ContainerTGraph :
public Container<TGraph>{
216 Value<std::pair<int, int> > *value;
217 std::vector<int> x_data;
218 std::vector<int> y_data;
222 auto val = value->get_value();
223 x_data.push_back(val.first);
224 y_data.push_back(val.second);
225 data_modified =
true;
228 ContainerTGraph(
const std::string& name,
const std::string& title, Value<std::pair<int, int>>* value)
229 :Container<TGraph>(name, new TGraph()),
231 data_modified(false){ }
233 TGraph* get_container(){
236 container =
new TGraph(x_data.size(), x_data.data(), y_data.data());
237 container->SetName(get_name().c_str());
238 container->SetTitle(title.c_str());
239 data_modified =
false;
243 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
244 util::save_as(get_container(), fname, option);
248 template <
typename T>
249 class Vector :
public Container<std::vector<T> >{
254 this->container->push_back(value->get_value());
257 Vector(
const std::string& name, std::vector<T>* container, Value<T>* value)
258 :Container<
std::vector<T>>(name, container), value(value){ }
260 Vector(
const std::string& name, Value<T>* value)
261 :Container<
std::vector<T>>(name, nullptr), value(value){
262 this->container =
new std::vector<T>();
265 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
266 std::string type_name =
"std::vector<"+fv::util::get_type_name(
typeid(T))+
">";
267 util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
271 template <
typename V,
typename D>
272 class _Counter :
public Container<std::map<D,int>>{
276 explicit _Counter(
const std::string& name, Value<V>* value)
277 :Container<
std::map<D,int>>(name, new
std::map<D,int>()),
280 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
281 std::string type_name =
"std::map<"+fv::util::get_type_name(
typeid(D))+
",int>";
282 util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
292 template <
typename V>
294 using _Counter<V,V>::_Counter;
296 (*this->container)[this->value->get_value()]++;
303 template <
typename V>
305 using _Counter<std::vector<V>,V>::_Counter;
307 for(V& val : this->value->get_value())
308 (*this->container)[val]++;
312 #endif // root_container_hpp Definition: container.hpp:16
Same as Counter but accepts multiple values per fill.
Definition: container.hpp:304
A Counter that keeps a mapping of the number of occurances of each input value.
Definition: container.hpp:293
SaveOption
Enumeration of different options that can be used to save Containers.
Definition: container.hpp:67