1 #ifndef root_container_hpp 2 #define root_container_hpp 16 void _save_img(TObject* container,
const std::string& fname){
17 TCanvas* c1 =
new TCanvas(
"c1");
20 c1->SaveAs(fname.c_str());
24 void _save_bin(TObject* container){
25 INFO(
"Saving object: " << container->GetName() <<
" into file " << gDirectory->GetName());
26 container->Write(container->GetName(), TObject::kOverwrite);
29 void _save_as(TObject* container,
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
32 _save_img(container, fname+
".png");
break;
34 _save_img(container, fname+
".pdf");
break;
36 _save_bin(container);
break;
42 void _save_as_stl(
void* container,
const std::string& fname,
const std::string& type_name,
43 const std::string& obj_name,
44 const SaveOption& option = SaveOption::PNG) {
47 INFO(
"Cannot save STL container " << type_name <<
" as png");
50 INFO(
"Cannot save STL container " << type_name <<
" as pdf");
53 std::cout <<
"writing object of type \"" << type_name <<
"\"" << std::endl;
54 gDirectory->WriteObjectAny(container, type_name.c_str(), obj_name.c_str());
68 if (container ==
nullptr){
69 if (value ==
nullptr){
70 CRITICAL(
"Container: \"" << get_name() <<
"\" has a null Value object. " 71 <<
"Probably built with imcompatible type",-1);
73 this->container =
new TH1D(this->get_name().c_str(), this->title.c_str(),
74 this->nbins, this->low, this->high);
86 virtual void _do_fill() = 0;
90 int nbins,
double low,
double high)
92 title(title), nbins(nbins), low(low), high(high),
93 value(
dynamic_cast<Value<V>*
>(value)) { }
95 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
96 util::_save_as(get_container(), fname, option);
100 template <
typename V>
104 this->container->Fill(this->value->get_value());
108 template <
typename V>
112 for(V x : this->value->get_value())
113 this->container->Fill(x);
119 template <
typename V>
123 if (container ==
nullptr){
124 if (value ==
nullptr){
125 CRITICAL(
"Container: \"" << get_name() <<
"\" has a null Value object. " 126 <<
"Probably built with imcompatible type",-1);
128 this->container =
new TH2D(this->get_name().c_str(), this->title.c_str(),
129 this->nbins_x, this->low_x, this->high_x,
130 this->nbins_y, this->low_y, this->high_y);
132 _do_fill(value->get_value());
145 virtual void _do_fill(std::pair<V,V>& val) = 0;
148 explicit _ContainerTH2(
const std::string& name,
const std::string& title,
150 int nbins_x,
double low_x,
double high_x,
151 int nbins_y,
double low_y,
double high_y)
153 nbins_x(nbins_x), low_x(low_x), high_x(high_x),
154 nbins_y(nbins_y), low_y(low_y), high_y(high_y),
157 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
158 util::_save_as(get_container(), fname, option);
162 template <
typename V>
165 void _do_fill(std::pair<V,V>& val){
166 this->container->Fill(val.first,val.second);
170 template <
typename V>
173 void _do_fill(std::pair<std::vector<V>,std::vector<V>>& val){
174 int min_size = std::min(val.first.size(), val.second.size());
175 for(
int i=0; i<min_size; i++)
176 this->container->Fill(val.first[i],val.second[i]);
183 std::vector<int> x_data;
184 std::vector<int> y_data;
189 x_data.push_back(val.first);
190 y_data.push_back(val.second);
191 data_modified =
true;
197 data_modified(
false){ }
199 TGraph* get_container(){
202 container =
new TGraph(x_data.size(), x_data.data(), y_data.data());
203 container->SetName(get_name().c_str());
204 container->SetTitle(title.c_str());
205 data_modified =
false;
209 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
210 util::_save_as(get_container(), fname, option);
215 template <
typename V,
typename D>
219 std::string type_name;
221 explicit _Counter(
const std::string& name,
GenValue* value,
const std::string& type_name =
"")
223 value(
dynamic_cast<Value<V>*
>(value)),
224 type_name(type_name){ }
226 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
228 util::_save_as_stl(this->get_container(), fname, type_name, this->get_name(), option);
234 template <
typename V>
238 V& val = this->value->get_value();
239 int curr = (*this->container)[val];
240 (*this->container)[val] = curr + 1;
244 template <
typename V>
248 for(V& val : this->value->get_value()){
249 std::map<V,int>* m= this->container;
251 if (m->find(val) == m->end()){
256 m->emplace(val, count);
268 #endif // root_container_hpp Definition: value.hpp:183
Definition: container.hpp:216
Definition: container.hpp:15
Definition: container.hpp:101
Definition: container.hpp:245
Definition: container.hpp:120
Definition: container.hpp:235
Definition: container.hpp:65
Definition: container.hpp:261
Definition: container.hpp:109
Definition: container.hpp:171
Definition: container.hpp:163
virtual T & get_value()=0
Calculate, if necessary, and return the value held by this object.
Definition: container.hpp:180
Definition: container.hpp:54