1 #ifndef root_container_hpp 2 #define root_container_hpp 14 #include "TMVA/Factory.h" 15 #include "TMVA/DataLoader.h" 16 #include "TMVA/DataSetInfo.h" 26 void save_as(TObject* container,
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
28 auto save_img = [](TObject* container,
const std::string& fname){
29 TCanvas* c1 =
new TCanvas(
"c1");
32 c1->SaveAs(fname.c_str());
36 auto save_bin = [](TObject* container){
37 INFO(
"Saving object: " << container->GetName() <<
" into file " << gDirectory->GetName());
38 container->Write(container->GetName(), TObject::kOverwrite);
43 save_img(container, fname+
".png");
break;
45 save_img(container, fname+
".pdf");
break;
47 save_bin(container);
break;
65 void save_as_stl(
void* container,
const std::string& type_name,
66 const std::string& obj_name,
70 INFO(
"Cannot save STL container " << type_name <<
" as png");
73 INFO(
"Cannot save STL container " << type_name <<
" as pdf");
77 gDirectory->WriteObjectAny(container, type_name.c_str(), obj_name.c_str());
88 class _ContainerTH1 :
public Container<TH1,V>{
91 if (this->container ==
nullptr){
92 if (this->value ==
nullptr){
93 CRITICAL(
"Container: \"" << this->get_name() <<
"\" has a null Value object. " 94 <<
"Probably built with imcompatible type",-1);
96 this->container =
new TH1D(this->get_name().c_str(), this->title.c_str(),
97 this->nbins, this->low, this->high);
98 this->container->SetXTitle(label_x.c_str());
99 this->container->SetYTitle(label_y.c_str());
112 virtual void _do_fill() = 0;
115 explicit _ContainerTH1(
const std::string &name,
const std::string& title, Value<V>* value,
116 int nbins,
double low,
double high,
117 const std::string& label_x =
"",
118 const std::string& label_y =
"")
119 :Container<TH1,V>(name, value),
120 title(title), nbins(nbins), low(low), high(high),
121 label_x(label_x), label_y(label_y) { }
123 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
124 util::save_as(this->get_container(), fname, option);
128 template <
typename V>
129 class ContainerTH1 :
public _ContainerTH1<V>{
130 using _ContainerTH1<V>::_ContainerTH1;
132 this->container->Fill(this->value->get_value());
135 GenContainer* clone_as(
const std::string& new_name){
136 return new ContainerTH1<V>(new_name, this->title, this->value, this->nbins, this->low, this->high, this->label_x, this->label_y);
140 template <
typename V>
141 class ContainerTH1Many :
public _ContainerTH1<std::vector<V>>{
142 using _ContainerTH1<std::vector<V>>::_ContainerTH1;
144 for(V x : this->value->get_value())
145 this->container->Fill(x);
148 GenContainer* clone_as(
const std::string& new_name){
149 return new ContainerTH1Many<V>(new_name, this->title, this->value, this->nbins, this->low, this->high, this->label_x, this->label_y);
155 template <
typename V>
156 class _ContainerTH2 :
public Container<TH2,std::pair<V,V>>{
159 if (this->container ==
nullptr){
160 if (this->value ==
nullptr){
161 CRITICAL(
"Container: \"" << this->get_name() <<
"\" has a null Value object. " 162 <<
"Probably built with imcompatible type",-1);
164 this->container =
new TH2D(this->get_name().c_str(), this->title.c_str(),
165 this->nbins_x, this->low_x, this->high_x,
166 this->nbins_y, this->low_y, this->high_y);
167 this->container->SetXTitle(label_x.c_str());
168 this->container->SetYTitle(label_y.c_str());
170 _do_fill(this->value->get_value());
184 virtual void _do_fill(std::pair<V,V>& val) = 0;
187 explicit _ContainerTH2(
const std::string& name,
const std::string& title,
188 Value<std::pair<V, V>>* value,
189 int nbins_x,
double low_x,
double high_x,
190 int nbins_y,
double low_y,
double high_y,
191 const std::string& label_x =
"",
192 const std::string& label_y =
"")
193 :Container<TH2,
std::pair<V,V>>(name, value),
195 nbins_x(nbins_x), low_x(low_x), high_x(high_x),
196 nbins_y(nbins_y), low_y(low_y), high_y(high_y),
197 label_x(label_x), label_y(label_y) { }
199 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
200 util::save_as(this->get_container(), fname, option);
204 template <
typename V>
205 class ContainerTH2 :
public _ContainerTH2<V>{
206 using _ContainerTH2<V>::_ContainerTH2;
207 void _do_fill(std::pair<V,V>& val){
208 this->container->Fill(val.first,val.second);
211 GenContainer* clone_as(
const std::string& new_name){
212 return new ContainerTH2<V>(new_name, this->title, this->value, this->nbins_x, this->low_x, this->high_x,
213 this->nbins_y, this->low_y, this->high_y, this->label_x, this->label_y);
217 template <
typename V>
218 class ContainerTH2Many :
public _ContainerTH2<std::vector<V>>{
219 using _ContainerTH2<std::vector<V>>::_ContainerTH2;
220 void _do_fill(std::pair<std::vector<V>,std::vector<V>>& val){
221 int min_size = std::min(val.first.size(), val.second.size());
222 for(
int i=0; i<min_size; i++)
223 this->container->Fill(val.first[i],val.second[i]);
226 GenContainer* clone_as(
const std::string& new_name){
227 return new ContainerTH2Many<V>(new_name, this->title, this->value, this->nbins_x, this->low_x, this->high_x,
228 this->nbins_y, this->low_y, this->high_y, this->label_x, this->label_y);
232 template <
typename V>
233 class ContainerTGraph :
public Container<TGraph,std::pair<V,V>>{
235 std::vector<V> x_data;
236 std::vector<V> y_data;
240 auto val = this->value->get_value();
241 x_data.push_back(val.first);
242 y_data.push_back(val.second);
243 data_modified =
true;
246 ContainerTGraph(
const std::string& name,
const std::string& title, Value<std::pair<V, V>>* value)
247 :Container<TGraph,
std::pair<V,V>>(name, value),
248 data_modified(false){
249 this->container =
new TGraph();
252 TGraph* get_container(){
254 delete this->container;
255 this->container =
new TGraph(x_data.size(), x_data.data(), y_data.data());
256 this->container->SetName(this->get_name().c_str());
257 this->container->SetTitle(title.c_str());
258 data_modified =
false;
260 return this->container;
263 GenContainer* clone_as(
const std::string& new_name){
264 return new ContainerTGraph<V>(new_name, this->title, this->value);
267 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
268 util::save_as(get_container(), fname, option);
272 template <
typename V>
273 class Vector :
public Container<std::vector<V>,V>{
277 this->container->push_back(this->value->get_value());
280 Vector(
const std::string& name, Value<V>* value)
281 :Container<
std::vector<V>,V>(name, value){
282 this->container =
new std::vector<V>;
285 GenContainer* clone_as(
const std::string& new_name){
286 return new Vector<V>(new_name, this->value);
289 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
290 std::string type_name =
"std::vector<"+fv::util::get_type_name(
typeid(V))+
">";
291 util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
295 template <
typename V,
typename D>
296 class _Counter :
public Container<std::map<D,int>,V>{
298 explicit _Counter(
const std::string& name, Value<V>* value)
299 :Container<
std::map<D,int>,V>(name, value) {
300 this->container =
new std::map<D,int>;
303 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
304 std::string type_name =
"std::map<"+fv::util::get_type_name(
typeid(D))+
",int>";
305 util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
314 template <
typename V>
316 using _Counter<V,V>::_Counter;
318 (*this->container)[this->value->get_value()]++;
329 template <
typename V>
331 using _Counter<std::vector<V>,V>::_Counter;
333 for(V& val : this->value->get_value())
334 (*this->container)[val]++;
342 class PassCount :
public Container<int,bool>{
346 if(this->value->get_value()){
347 (*this->container)++;
351 PassCount(
const std::string& name,
Value<bool>* value)
353 this->container =
new int(0);
357 return new PassCount(new_name, this->value);
360 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
363 std::vector<int> v({*this->get_container()});
364 util::save_as_stl(&v,
"std::vector<int>", this->get_name(), option);
369 template <
typename... ArgTypes>
370 class MVA :
public Container<TMVA::DataLoader,typename MVAData<ArgTypes...>::type>{
372 std::vector<std::pair<std::string,std::string>> methods;
378 std::tuple<ArgTypes...> t;
379 typename MVAData<ArgTypes...>::type&
event = this->value->get_value();
380 bool is_training, is_signal;
382 std::tie(is_training, is_signal, weight, t) = event;
383 std::vector<double> v = t2v<double>(t);
386 this->container->AddSignalTrainingEvent(v, weight);
388 this->container->AddSignalTestEvent(v, weight);
392 this->container->AddBackgroundTrainingEvent(v, weight);
394 this->container->AddBackgroundTestEvent(v, weight);
399 MVA(
const std::string& name, MVAData<ArgTypes...>* value,
const std::string& cut =
"",
const std::string& opt =
"")
400 :
Container<TMVA::DataLoader,
typename MVAData<ArgTypes...>::type>(name, value),
402 this->container =
new TMVA::DataLoader(name);
403 for (std::pair<std::string,char>& p : value->get_label_types()){
404 this->container->AddVariable(p.first, p.second);
408 void add_method(
const std::string& method_name,
const std::string& method_params) {
409 methods.push_back(std::make_pair(method_name, method_params));
413 auto mva =
new MVA<ArgTypes...>(new_name, (MVAData<ArgTypes...>*)this->value, this->cut, this->opt);
414 mva->methods = methods;
418 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
419 TFile* outputFile = gDirectory->GetFile();
421 this->container->PrepareTrainingAndTestTree(cut.c_str(), opt.c_str());
422 TMVA::Factory *factory =
new TMVA::Factory(
"TMVAClassification", outputFile,
423 "!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification");
425 TMVA::Types& types = TMVA::Types::Instance();
426 for(
auto& p : methods){
427 std::string method_name, method_params;
428 std::tie(method_name, method_params) = p;
429 TMVA::Types::EMVA method_type = types.GetMethodType(method_name);
430 factory->BookMethod(this->container, method_type, method_name, method_params);
434 factory->TrainAllMethods();
436 factory->TestAllMethods();
438 factory->EvaluateAllMethods();
444 #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.
Generic, untyped parent class of Container.
A class that is used to "hold" values.