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,V>{
87 if (this->container ==
nullptr){
88 if (this->value ==
nullptr){
89 CRITICAL(
"Container: \"" << this->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());
108 virtual void _do_fill() = 0;
111 explicit _ContainerTH1(
const std::string &name,
const std::string& title, Value<V>* value,
112 int nbins,
double low,
double high,
113 const std::string& label_x =
"",
114 const std::string& label_y =
"")
115 :Container<TH1,V>(name, value),
116 title(title), nbins(nbins), low(low), high(high),
117 label_x(label_x), label_y(label_y) { }
119 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
120 util::save_as(this->get_container(), fname, option);
124 template <
typename V>
125 class ContainerTH1 :
public _ContainerTH1<V>{
126 using _ContainerTH1<V>::_ContainerTH1;
128 this->container->Fill(this->value->get_value());
132 template <
typename V>
133 class ContainerTH1Many :
public _ContainerTH1<std::vector<V>>{
134 using _ContainerTH1<std::vector<V>>::_ContainerTH1;
136 for(V x : this->value->get_value())
137 this->container->Fill(x);
143 template <
typename V>
144 class _ContainerTH2 :
public Container<TH2,std::pair<V,V>>{
147 if (this->container ==
nullptr){
148 if (this->value ==
nullptr){
149 CRITICAL(
"Container: \"" << this->get_name() <<
"\" has a null Value object. " 150 <<
"Probably built with imcompatible type",-1);
152 this->container =
new TH2D(this->get_name().c_str(), this->title.c_str(),
153 this->nbins_x, this->low_x, this->high_x,
154 this->nbins_y, this->low_y, this->high_y);
155 this->container->SetXTitle(label_x.c_str());
156 this->container->SetYTitle(label_y.c_str());
158 _do_fill(this->value->get_value());
172 virtual void _do_fill(std::pair<V,V>& val) = 0;
175 explicit _ContainerTH2(
const std::string& name,
const std::string& title,
176 Value<std::pair<V, V>>* value,
177 int nbins_x,
double low_x,
double high_x,
178 int nbins_y,
double low_y,
double high_y,
179 const std::string& label_x =
"",
180 const std::string& label_y =
"")
181 :Container<TH2,
std::pair<V,V>>(name, value),
183 nbins_x(nbins_x), low_x(low_x), high_x(high_x),
184 nbins_y(nbins_y), low_y(low_y), high_y(high_y),
185 label_x(label_x), label_y(label_y) { }
187 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
188 util::save_as(this->get_container(), fname, option);
192 template <
typename V>
193 class ContainerTH2 :
public _ContainerTH2<V>{
194 using _ContainerTH2<V>::_ContainerTH2;
195 void _do_fill(std::pair<V,V>& val){
196 this->container->Fill(val.first,val.second);
200 template <
typename V>
201 class ContainerTH2Many :
public _ContainerTH2<std::vector<V>>{
202 using _ContainerTH2<std::vector<V>>::_ContainerTH2;
203 void _do_fill(std::pair<std::vector<V>,std::vector<V>>& val){
204 int min_size = std::min(val.first.size(), val.second.size());
205 for(
int i=0; i<min_size; i++)
206 this->container->Fill(val.first[i],val.second[i]);
210 template <
typename V>
211 class ContainerTGraph :
public Container<TGraph,std::pair<V,V>>{
213 std::vector<V> x_data;
214 std::vector<V> y_data;
218 auto val = this->value->get_value();
219 x_data.push_back(val.first);
220 y_data.push_back(val.second);
221 data_modified =
true;
224 ContainerTGraph(
const std::string& name,
const std::string& title, Value<std::pair<V, V>>* value)
225 :Container<TGraph,
std::pair<V,V>>(name, value),
226 data_modified(false){
227 this->container =
new TGraph();
230 TGraph* get_container(){
232 delete this->container;
233 this->container =
new TGraph(x_data.size(), x_data.data(), y_data.data());
234 this->container->SetName(this->get_name().c_str());
235 this->container->SetTitle(title.c_str());
236 data_modified =
false;
238 return this->container;
240 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
241 util::save_as(get_container(), fname, option);
245 template <
typename T>
246 class Vector :
public Container<std::vector<T>,T>{
250 this->container->push_back(this->value->get_value());
253 Vector(
const std::string& name, Value<T>* value)
254 :Container<
std::vector<T>,T>(name, value){
255 this->container =
new std::vector<T>;
258 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
259 std::string type_name =
"std::vector<"+fv::util::get_type_name(
typeid(T))+
">";
260 util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
264 template <
typename V,
typename D>
265 class _Counter :
public Container<std::map<D,int>,V>{
267 explicit _Counter(
const std::string& name, Value<V>* value)
268 :Container<
std::map<D,int>,V>(name, value) {
269 this->container =
new std::map<D,int>;
272 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
273 std::string type_name =
"std::map<"+fv::util::get_type_name(
typeid(D))+
",int>";
274 util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
284 template <
typename V>
286 using _Counter<V,V>::_Counter;
288 (*this->container)[this->value->get_value()]++;
295 template <
typename V>
297 using _Counter<std::vector<V>,V>::_Counter;
299 for(V& val : this->value->get_value())
300 (*this->container)[val]++;
304 #endif // root_container_hpp Definition: container.hpp:16
Same as Counter but accepts multiple values per fill.
Definition: container.hpp:296
A Counter that keeps a mapping of the number of occurances of each input value.
Definition: container.hpp:285
SaveOption
Enumeration of different options that can be used to save Containers.
Definition: container.hpp:69