1 #ifndef root_container_hpp 2 #define root_container_hpp 14 #include "filval/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,
64 const SaveOption& option = SaveOption::PNG) {
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());
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;
113 int nbins,
double low,
double high,
114 const std::string& label_x =
"",
115 const std::string& label_y =
"")
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>
130 this->container->Fill(this->value->get_value());
134 template <
typename V>
138 for(V x : this->value->get_value())
139 this->container->Fill(x);
145 template <
typename V>
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());
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 =
"")
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>
199 void _do_fill(std::pair<V,V>& val){
200 this->container->Fill(val.first,val.second);
204 template <
typename V>
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]);
217 std::vector<int> x_data;
218 std::vector<int> y_data;
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)
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>
254 this->container->push_back(value->
get_value());
257 Vector(
const std::string& name, std::vector<T>* container,
Value<T>* 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>
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>
296 (*this->container)[this->value->get_value()]++;
303 template <
typename V>
307 for(V& val : this->value->get_value())
308 (*this->container)[val]++;
312 #endif // root_container_hpp Definition: container.hpp:272
Definition: container.hpp:16
Definition: container.hpp:127
Same as Counter but accepts multiple values per fill.
Definition: container.hpp:304
Definition: container.hpp:146
A Counter that keeps a mapping of the number of occurances of each input value.
Definition: container.hpp:293
Definition: container.hpp:84
Definition: container.hpp:135
Definition: container.hpp:249
Definition: container.hpp:205
Definition: container.hpp:197
virtual T & get_value()=0
Calculate, if necessary, and return the value held by this object.
Definition: container.hpp:214
Definition: container.hpp:78