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());
136 template <
typename V>
137 class ContainerTH1Many :
public _ContainerTH1<std::vector<V>>{
138 using _ContainerTH1<std::vector<V>>::_ContainerTH1;
140 for(V x : this->value->get_value())
141 this->container->Fill(x);
147 template <
typename V>
148 class _ContainerTH2 :
public Container<TH2,std::pair<V,V>>{
151 if (this->container ==
nullptr){
152 if (this->value ==
nullptr){
153 CRITICAL(
"Container: \"" << this->get_name() <<
"\" has a null Value object. " 154 <<
"Probably built with imcompatible type",-1);
156 this->container =
new TH2D(this->get_name().c_str(), this->title.c_str(),
157 this->nbins_x, this->low_x, this->high_x,
158 this->nbins_y, this->low_y, this->high_y);
159 this->container->SetXTitle(label_x.c_str());
160 this->container->SetYTitle(label_y.c_str());
162 _do_fill(this->value->get_value());
176 virtual void _do_fill(std::pair<V,V>& val) = 0;
179 explicit _ContainerTH2(
const std::string& name,
const std::string& title,
180 Value<std::pair<V, V>>* value,
181 int nbins_x,
double low_x,
double high_x,
182 int nbins_y,
double low_y,
double high_y,
183 const std::string& label_x =
"",
184 const std::string& label_y =
"")
185 :Container<TH2,
std::pair<V,V>>(name, value),
187 nbins_x(nbins_x), low_x(low_x), high_x(high_x),
188 nbins_y(nbins_y), low_y(low_y), high_y(high_y),
189 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(this->get_container(), fname, option);
196 template <
typename V>
197 class ContainerTH2 :
public _ContainerTH2<V>{
198 using _ContainerTH2<V>::_ContainerTH2;
199 void _do_fill(std::pair<V,V>& val){
200 this->container->Fill(val.first,val.second);
204 template <
typename V>
205 class ContainerTH2Many :
public _ContainerTH2<std::vector<V>>{
206 using _ContainerTH2<std::vector<V>>::_ContainerTH2;
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]);
214 template <
typename V>
215 class ContainerTGraph :
public Container<TGraph,std::pair<V,V>>{
217 std::vector<V> x_data;
218 std::vector<V> y_data;
222 auto val = this->value->get_value();
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<V, V>>* value)
229 :Container<TGraph,
std::pair<V,V>>(name, value),
230 data_modified(false){
231 this->container =
new TGraph();
234 TGraph* get_container(){
236 delete this->container;
237 this->container =
new TGraph(x_data.size(), x_data.data(), y_data.data());
238 this->container->SetName(this->get_name().c_str());
239 this->container->SetTitle(title.c_str());
240 data_modified =
false;
242 return this->container;
244 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
245 util::save_as(get_container(), fname, option);
249 template <
typename T>
250 class Vector :
public Container<std::vector<T>,T>{
254 this->container->push_back(this->value->get_value());
257 Vector(
const std::string& name, Value<T>* value)
258 :Container<
std::vector<T>,T>(name, value){
259 this->container =
new std::vector<T>;
262 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
263 std::string type_name =
"std::vector<"+fv::util::get_type_name(
typeid(T))+
">";
264 util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
268 template <
typename V,
typename D>
269 class _Counter :
public Container<std::map<D,int>,V>{
271 explicit _Counter(
const std::string& name, Value<V>* value)
272 :Container<
std::map<D,int>,V>(name, value) {
273 this->container =
new std::map<D,int>;
276 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
277 std::string type_name =
"std::map<"+fv::util::get_type_name(
typeid(D))+
",int>";
278 util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
287 template <
typename V>
289 using _Counter<V,V>::_Counter;
291 (*this->container)[this->value->get_value()]++;
298 template <
typename V>
300 using _Counter<std::vector<V>,V>::_Counter;
302 for(V& val : this->value->get_value())
303 (*this->container)[val]++;
307 class PassCount :
public Container<int,bool>{
311 if(this->value->get_value()){
312 (*this->container)++;
316 PassCount(
const std::string& name,
Value<bool>* value)
318 this->container =
new int(0);
321 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
324 std::vector<int> v({*this->get_container()});
325 util::save_as_stl(&v,
"std::vector<int>", this->get_name(), option);
330 template <
typename... ArgTypes>
331 class MVA :
public Container<TMVA::DataLoader,typename MVAData<ArgTypes...>::type>{
333 std::vector<std::pair<std::string,std::string>> methods;
339 std::tuple<ArgTypes...> t;
340 typename MVAData<ArgTypes...>::type&
event = this->value->get_value();
341 bool is_training, is_signal;
343 std::tie(is_training, is_signal, weight, t) = event;
344 std::vector<double> v = t2v<double>(t);
347 this->container->AddSignalTrainingEvent(v, weight);
349 this->container->AddSignalTestEvent(v, weight);
353 this->container->AddBackgroundTrainingEvent(v, weight);
355 this->container->AddBackgroundTestEvent(v, weight);
360 MVA(
const std::string& name, MVAData<ArgTypes...>* value,
const std::string& cut =
"",
const std::string& opt =
"")
361 :
Container<TMVA::DataLoader,
typename MVAData<ArgTypes...>::type>(name, value),
362 cut(cut.c_str()), opt(opt.c_str()) {
363 this->container =
new TMVA::DataLoader(name);
364 for (std::pair<std::string,char>& p : value->get_label_types()){
365 this->container->AddVariable(p.first, p.second);
369 void add_method(
const std::string& method_name,
const std::string& method_params) {
370 methods.push_back(std::make_pair(method_name, method_params));
373 void save_as(
const std::string& fname,
const SaveOption& option = SaveOption::PNG) {
374 TFile* outputFile = gDirectory->GetFile();
376 this->container->PrepareTrainingAndTestTree(cut, opt);
377 TMVA::Factory *factory =
new TMVA::Factory(
"TMVAClassification", outputFile,
378 "!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification");
380 TMVA::Types& types = TMVA::Types::Instance();
381 for(
auto& p : methods){
382 std::string method_name, method_params;
383 std::tie(method_name, method_params) = p;
384 TMVA::Types::EMVA method_type = types.GetMethodType(method_name);
385 factory->BookMethod(this->container, method_type, method_name, method_params);
389 factory->TrainAllMethods();
391 factory->TestAllMethods();
393 factory->EvaluateAllMethods();
399 #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.