1 #ifndef root_container_hpp 2 #define root_container_hpp 14 #include "TMVA/Factory.h" 15 #include "TMVA/DataLoader.h" 25 void save_as(TObject* container,
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
27 auto save_img = [](TObject* container,
const std::string& fname){
28 TCanvas* c1 =
new TCanvas(
"c1");
31 c1->SaveAs(fname.c_str());
35 auto save_bin = [](TObject* container){
36 INFO(
"Saving object: " << container->GetName() <<
" into file " << gDirectory->GetName());
37 container->Write(container->GetName(), TObject::kOverwrite);
42 save_img(container, fname+
".png");
break;
44 save_img(container, fname+
".pdf");
break;
46 save_bin(container);
break;
64 void save_as_stl(
void* container,
const std::string& type_name,
65 const std::string& obj_name,
69 INFO(
"Cannot save STL container " << type_name <<
" as png");
72 INFO(
"Cannot save STL container " << type_name <<
" as pdf");
75 gDirectory->WriteObjectAny(container, type_name.c_str(), obj_name.c_str());
86 class _ContainerTH1 :
public Container<TH1,V>{
89 if (this->container ==
nullptr){
90 if (this->value ==
nullptr){
91 CRITICAL(
"Container: \"" << this->get_name() <<
"\" has a null Value object. " 92 <<
"Probably built with imcompatible type",-1);
94 this->container =
new TH1D(this->get_name().c_str(), this->title.c_str(),
95 this->nbins, this->low, this->high);
96 this->container->SetXTitle(label_x.c_str());
97 this->container->SetYTitle(label_y.c_str());
110 virtual void _do_fill() = 0;
113 explicit _ContainerTH1(
const std::string &name,
const std::string& title, Value<V>* value,
114 int nbins,
double low,
double high,
115 const std::string& label_x =
"",
116 const std::string& label_y =
"")
117 :Container<TH1,V>(name, value),
118 title(title), nbins(nbins), low(low), high(high),
119 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(this->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,std::pair<V,V>>{
149 if (this->container ==
nullptr){
150 if (this->value ==
nullptr){
151 CRITICAL(
"Container: \"" << this->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(this->value->get_value());
174 virtual void _do_fill(std::pair<V,V>& val) = 0;
177 explicit _ContainerTH2(
const std::string& name,
const std::string& title,
178 Value<std::pair<V, V>>* value,
179 int nbins_x,
double low_x,
double high_x,
180 int nbins_y,
double low_y,
double high_y,
181 const std::string& label_x =
"",
182 const std::string& label_y =
"")
183 :Container<TH2,
std::pair<V,V>>(name, value),
185 nbins_x(nbins_x), low_x(low_x), high_x(high_x),
186 nbins_y(nbins_y), low_y(low_y), high_y(high_y),
187 label_x(label_x), label_y(label_y) { }
189 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
190 util::save_as(this->get_container(), fname, option);
194 template <
typename V>
195 class ContainerTH2 :
public _ContainerTH2<V>{
196 using _ContainerTH2<V>::_ContainerTH2;
197 void _do_fill(std::pair<V,V>& val){
198 this->container->Fill(val.first,val.second);
202 template <
typename V>
203 class ContainerTH2Many :
public _ContainerTH2<std::vector<V>>{
204 using _ContainerTH2<std::vector<V>>::_ContainerTH2;
205 void _do_fill(std::pair<std::vector<V>,std::vector<V>>& val){
206 int min_size = std::min(val.first.size(), val.second.size());
207 for(
int i=0; i<min_size; i++)
208 this->container->Fill(val.first[i],val.second[i]);
212 template <
typename V>
213 class ContainerTGraph :
public Container<TGraph,std::pair<V,V>>{
215 std::vector<V> x_data;
216 std::vector<V> y_data;
220 auto val = this->value->get_value();
221 x_data.push_back(val.first);
222 y_data.push_back(val.second);
223 data_modified =
true;
226 ContainerTGraph(
const std::string& name,
const std::string& title, Value<std::pair<V, V>>* value)
227 :Container<TGraph,
std::pair<V,V>>(name, value),
228 data_modified(false){
229 this->container =
new TGraph();
232 TGraph* get_container(){
234 delete this->container;
235 this->container =
new TGraph(x_data.size(), x_data.data(), y_data.data());
236 this->container->SetName(this->get_name().c_str());
237 this->container->SetTitle(title.c_str());
238 data_modified =
false;
240 return this->container;
242 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
243 util::save_as(get_container(), fname, option);
247 template <
typename T>
248 class Vector :
public Container<std::vector<T>,T>{
252 this->container->push_back(this->value->get_value());
255 Vector(
const std::string& name, Value<T>* value)
256 :Container<
std::vector<T>,T>(name, value){
257 this->container =
new std::vector<T>;
260 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
261 std::string type_name =
"std::vector<"+fv::util::get_type_name(
typeid(T))+
">";
262 util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
266 template <
typename V,
typename D>
267 class _Counter :
public Container<std::map<D,int>,V>{
269 explicit _Counter(
const std::string& name, Value<V>* value)
270 :Container<
std::map<D,int>,V>(name, value) {
271 this->container =
new std::map<D,int>;
274 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
275 std::string type_name =
"std::map<"+fv::util::get_type_name(
typeid(D))+
",int>";
276 util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
286 template <
typename V>
288 using _Counter<V,V>::_Counter;
290 (*this->container)[this->value->get_value()]++;
297 template <
typename V>
299 using _Counter<std::vector<V>,V>::_Counter;
301 for(V& val : this->value->get_value())
302 (*this->container)[val]++;
308 template <
typename... ArgTypes>
309 class MVA :
public Container<TMVA::DataLoader,std::tuple<ArgTypes...>>{
313 this->container->push_back(this->value->get_value());
314 std::vector<double>& v = t2v<double>(this->value->get_value());
315 this->container->AddSignalTrainingEvent(v, 1);
320 MVA(
const std::string& name,
Value<std::tuple<ArgTypes...>>* value,
const std::vector<std::string>& labels=std::vector<std::string>())
321 :
Container<TMVA::DataLoader,std::tuple<ArgTypes...>(name, value){
322 this->container =
new DataLoader(name);
323 if (labels.size() !=
sizeof...(ArgTypes)){
324 CRITICAL(
"Length of labels vector ("<<labels.size()<<
") not equal to number of MVA arguments ("<<
sizeof...(ArgTypes)<<
")",-1)
326 for(std::string& label : labels){
327 this->container->AddVariable(label,
'F');
331 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
332 std::string type_name =
"std::vector<"+fv::util::get_type_name(
typeid(T))+
">";
333 util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
337 #endif // root_container_hpp
Same as Counter but accepts multiple values per fill.
A Counter that keeps a mapping of the number of occurances of each input value.
SaveOption
Enumeration of different options that can be used to save Containers.
A class that is used to "hold" values.