root_container.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. #ifndef root_container_hpp
  2. #define root_container_hpp
  3. #include <iostream>
  4. #include <utility>
  5. #include <map>
  6. #include "TROOT.h"
  7. #include "TFile.h"
  8. #include "TCanvas.h"
  9. #include "TGraph.h"
  10. #include "TH1.h"
  11. #include "TH2.h"
  12. #include "TMVA/Factory.h"
  13. #include "TMVA/DataLoader.h"
  14. #include "TMVA/DataSetInfo.h"
  15. #include "filval/container.hpp"
  16. namespace fv::root::util{
  17. /**
  18. * Save a TObject. The TObject will typically be a Histogram or Graph object,
  19. * but can really be any TObject. The SaveOption can be used to specify how to
  20. * save the file.
  21. */
  22. void save_as(TObject* container, const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  23. auto save_img = [](TObject* container, const std::string& fname){
  24. TCanvas* c1 = new TCanvas("c1");
  25. container->Draw();
  26. c1->Draw();
  27. c1->SaveAs(fname.c_str());
  28. delete c1;
  29. };
  30. auto save_bin = [](TObject* container){
  31. INFO("Saving object: " << container->GetName() << " into file " << gDirectory->GetName());
  32. container->Write(container->GetName(), TObject::kOverwrite);
  33. };
  34. switch(option){
  35. case PNG:
  36. save_img(container, fname+".png"); break;
  37. case PDF:
  38. save_img(container, fname+".pdf"); break;
  39. case ROOT:
  40. save_bin(container); break;
  41. default:
  42. break;
  43. }
  44. }
  45. /**
  46. * Saves an STL container into a ROOT file. ROOT knows how to serialize STL
  47. * containers, but it needs the *name* of the type of the container, eg.
  48. * std::map<int,int> to be able to do this. In order to generate this name at
  49. * run-time, the fv::util::get_type_name function uses RTTI to get type info
  50. * and use it to look up the proper name.
  51. *
  52. * For nexted containers, it is necessary to generate the CLING dictionaries
  53. * for each type at compile time to enable serialization. To do this, add the
  54. * type definition into the LinkDef.hpp header file.
  55. */
  56. void save_as_stl(void* container, const std::string& type_name,
  57. const std::string& obj_name,
  58. const SaveOption& option = SaveOption::PNG) {
  59. switch(option){
  60. case PNG:
  61. INFO("Cannot save STL container " << type_name <<" as png");
  62. break;
  63. case PDF:
  64. INFO("Cannot save STL container " << type_name <<" as pdf");
  65. break;
  66. case ROOT:
  67. /* DEBUG("Writing object \"" << obj_name << "\" of type \"" << type_name << "\"\n"); */
  68. gDirectory->WriteObjectAny(container, type_name.c_str(), obj_name.c_str());
  69. break;
  70. default:
  71. break;
  72. }
  73. }
  74. }
  75. namespace fv::root {
  76. template <typename V>
  77. class _ContainerTH1 : public Container<TH1,V>{
  78. private:
  79. void _fill(){
  80. if (this->container == nullptr){
  81. if (this->value == nullptr){
  82. CRITICAL("Container: \"" << this->get_name() << "\" has a null Value object. "
  83. << "Probably built with imcompatible type",-1);
  84. }
  85. this->container = new TH1D(this->get_name().c_str(), this->title.c_str(),
  86. this->nbins, this->low, this->high);
  87. this->container->SetXTitle(label_x.c_str());
  88. this->container->SetYTitle(label_y.c_str());
  89. }
  90. _do_fill();
  91. }
  92. protected:
  93. std::string title;
  94. std::string label_x;
  95. std::string label_y;
  96. int nbins;
  97. double low;
  98. double high;
  99. virtual void _do_fill() = 0;
  100. public:
  101. explicit _ContainerTH1(const std::string &name, const std::string& title, Value<V>* value,
  102. int nbins, double low, double high,
  103. const std::string& label_x = "",
  104. const std::string& label_y = "")
  105. :Container<TH1,V>(name, value),
  106. title(title), nbins(nbins), low(low), high(high),
  107. label_x(label_x), label_y(label_y) { }
  108. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  109. util::save_as(this->get_container(), fname, option);
  110. }
  111. };
  112. template <typename V>
  113. class ContainerTH1 : public _ContainerTH1<V>{
  114. using _ContainerTH1<V>::_ContainerTH1;
  115. void _do_fill(){
  116. this->container->Fill(this->value->get_value());
  117. }
  118. public:
  119. GenContainer* clone_as(const std::string& new_name){
  120. return new ContainerTH1<V>(new_name, this->title, this->value, this->nbins, this->low, this->high, this->label_x, this->label_y);
  121. }
  122. };
  123. template <typename V>
  124. class ContainerTH1Many : public _ContainerTH1<std::vector<V>>{
  125. using _ContainerTH1<std::vector<V>>::_ContainerTH1;
  126. void _do_fill(){
  127. for(V x : this->value->get_value())
  128. this->container->Fill(x);
  129. }
  130. public:
  131. GenContainer* clone_as(const std::string& new_name){
  132. return new ContainerTH1Many<V>(new_name, this->title, this->value, this->nbins, this->low, this->high, this->label_x, this->label_y);
  133. }
  134. };
  135. template <typename V>
  136. class _ContainerTH2 : public Container<TH2,std::pair<V,V>>{
  137. private:
  138. void _fill(){
  139. if (this->container == nullptr){
  140. if (this->value == nullptr){
  141. CRITICAL("Container: \"" << this->get_name() << "\" has a null Value object. "
  142. << "Probably built with imcompatible type",-1);
  143. }
  144. this->container = new TH2D(this->get_name().c_str(), this->title.c_str(),
  145. this->nbins_x, this->low_x, this->high_x,
  146. this->nbins_y, this->low_y, this->high_y);
  147. this->container->SetXTitle(label_x.c_str());
  148. this->container->SetYTitle(label_y.c_str());
  149. }
  150. _do_fill(this->value->get_value());
  151. }
  152. protected:
  153. std::string title;
  154. std::string label_x;
  155. std::string label_y;
  156. int nbins_x;
  157. int nbins_y;
  158. double low_x;
  159. double low_y;
  160. double high_x;
  161. double high_y;
  162. virtual void _do_fill(std::pair<V,V>& val) = 0;
  163. public:
  164. explicit _ContainerTH2(const std::string& name, const std::string& title,
  165. Value<std::pair<V, V>>* value,
  166. int nbins_x, double low_x, double high_x,
  167. int nbins_y, double low_y, double high_y,
  168. const std::string& label_x = "",
  169. const std::string& label_y = "")
  170. :Container<TH2,std::pair<V,V>>(name, value),
  171. title(title),
  172. nbins_x(nbins_x), low_x(low_x), high_x(high_x),
  173. nbins_y(nbins_y), low_y(low_y), high_y(high_y),
  174. label_x(label_x), label_y(label_y) { }
  175. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  176. util::save_as(this->get_container(), fname, option);
  177. }
  178. };
  179. template <typename V>
  180. class ContainerTH2 : public _ContainerTH2<V>{
  181. using _ContainerTH2<V>::_ContainerTH2;
  182. void _do_fill(std::pair<V,V>& val){
  183. this->container->Fill(val.first,val.second);
  184. }
  185. public:
  186. GenContainer* clone_as(const std::string& new_name){
  187. return new ContainerTH2<V>(new_name, this->title, this->value, this->nbins_x, this->low_x, this->high_x,
  188. this->nbins_y, this->low_y, this->high_y, this->label_x, this->label_y);
  189. }
  190. };
  191. template <typename V>
  192. class ContainerTH2Many : public _ContainerTH2<std::vector<V>>{
  193. using _ContainerTH2<std::vector<V>>::_ContainerTH2;
  194. void _do_fill(std::pair<std::vector<V>,std::vector<V>>& val){
  195. int min_size = std::min(val.first.size(), val.second.size());
  196. for(int i=0; i<min_size; i++)
  197. this->container->Fill(val.first[i],val.second[i]);
  198. }
  199. public:
  200. GenContainer* clone_as(const std::string& new_name){
  201. return new ContainerTH2Many<V>(new_name, this->title, this->value, this->nbins_x, this->low_x, this->high_x,
  202. this->nbins_y, this->low_y, this->high_y, this->label_x, this->label_y);
  203. }
  204. };
  205. template <typename V>
  206. class ContainerTGraph : public Container<TGraph,std::pair<V,V>>{
  207. private:
  208. std::vector<V> x_data;
  209. std::vector<V> y_data;
  210. std::string title;
  211. bool data_modified;
  212. void _fill(){
  213. auto val = this->value->get_value();
  214. x_data.push_back(val.first);
  215. y_data.push_back(val.second);
  216. data_modified = true;
  217. }
  218. public:
  219. ContainerTGraph(const std::string& name, const std::string& title, Value<std::pair<V, V>>* value)
  220. :Container<TGraph,std::pair<V,V>>(name, value),
  221. data_modified(false){
  222. this->container = new TGraph();
  223. }
  224. TGraph* get_container(){
  225. if (data_modified){
  226. delete this->container;
  227. this->container = new TGraph(x_data.size(), x_data.data(), y_data.data());
  228. this->container->SetName(this->get_name().c_str());
  229. this->container->SetTitle(title.c_str());
  230. data_modified = false;
  231. }
  232. return this->container;
  233. }
  234. GenContainer* clone_as(const std::string& new_name){
  235. return new ContainerTGraph<V>(new_name, this->title, this->value);
  236. }
  237. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  238. util::save_as(get_container(), fname, option);
  239. }
  240. };
  241. template <typename V>
  242. class Vector : public Container<std::vector<V>,V>{
  243. private:
  244. void _fill(){
  245. this->container->push_back(this->value->get_value());
  246. }
  247. public:
  248. Vector(const std::string& name, Value<V>* value)
  249. :Container<std::vector<V>,V>(name, value){
  250. this->container = new std::vector<V>;
  251. }
  252. GenContainer* clone_as(const std::string& new_name){
  253. return new Vector<V>(new_name, this->value);
  254. }
  255. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  256. std::string type_name = "std::vector<"+fv::util::get_type_name(typeid(V))+">";
  257. util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
  258. }
  259. };
  260. template <typename V, typename D>
  261. class _Counter : public Container<std::map<D,int>,V>{
  262. public:
  263. explicit _Counter(const std::string& name, Value<V>* value)
  264. :Container<std::map<D,int>,V>(name, value) {
  265. this->container = new std::map<D,int>;
  266. }
  267. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  268. std::string type_name = "std::map<"+fv::util::get_type_name(typeid(D))+",int>";
  269. util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
  270. }
  271. };
  272. /**
  273. * A Counter that keeps a mapping of the number of occurances of each input
  274. * value.
  275. */
  276. template <typename V>
  277. class Counter : public _Counter<V,V>{
  278. using _Counter<V,V>::_Counter;
  279. void _fill(){
  280. (*this->container)[this->value->get_value()]++;
  281. }
  282. public:
  283. GenContainer* clone_as(const std::string& new_name){
  284. return new Counter<V>(new_name, this->value);
  285. }
  286. };
  287. /**
  288. * Same as Counter but accepts multiple values per fill.
  289. */
  290. template <typename V>
  291. class CounterMany : public _Counter<std::vector<V>,V>{
  292. using _Counter<std::vector<V>,V>::_Counter;
  293. void _fill(){
  294. for(V& val : this->value->get_value())
  295. (*this->container)[val]++;
  296. }
  297. public:
  298. GenContainer* clone_as(const std::string& new_name){
  299. return new CounterMany<V>(new_name, this->value);
  300. }
  301. };
  302. class PassCount : public Container<int,bool>{
  303. private:
  304. void _fill(){
  305. if(this->value->get_value()){
  306. (*this->container)++;
  307. }
  308. }
  309. public:
  310. PassCount(const std::string& name, Value<bool>* value)
  311. :Container<int,bool>(name, value){
  312. this->container = new int(0);
  313. }
  314. GenContainer* clone_as(const std::string& new_name){
  315. return new PassCount(new_name, this->value);
  316. }
  317. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  318. //ROOT(hilariously) cannot serialize basic data types, we wrap this
  319. //in a vector.
  320. std::vector<int> v({*this->get_container()});
  321. util::save_as_stl(&v, "std::vector<int>", this->get_name(), option);
  322. }
  323. };
  324. template <typename... ArgTypes>
  325. class MVA : public Container<TMVA::DataLoader,typename MVAData<ArgTypes...>::type>{
  326. private:
  327. std::vector<std::pair<std::string,std::string>> methods;
  328. std::string cut;
  329. std::string opt;
  330. void _fill(){
  331. std::tuple<ArgTypes...> t;
  332. typename MVAData<ArgTypes...>::type& event = this->value->get_value();
  333. bool is_training, is_signal;
  334. double weight;
  335. std::tie(is_training, is_signal, weight, t) = event;
  336. std::vector<double> v = t2v<double>(t);
  337. if (is_signal){
  338. if (is_training){
  339. this->container->AddSignalTrainingEvent(v, weight);
  340. } else {
  341. this->container->AddSignalTestEvent(v, weight);
  342. }
  343. } else {
  344. if (is_training){
  345. this->container->AddBackgroundTrainingEvent(v, weight);
  346. } else {
  347. this->container->AddBackgroundTestEvent(v, weight);
  348. }
  349. }
  350. }
  351. public:
  352. MVA(const std::string& name, MVAData<ArgTypes...>* value, const std::string& cut = "", const std::string& opt = "")
  353. :Container<TMVA::DataLoader,typename MVAData<ArgTypes...>::type>(name, value),
  354. cut(cut), opt(opt) {
  355. this->container = new TMVA::DataLoader(name);
  356. for (std::pair<std::string,char>& p : value->get_label_types()){
  357. this->container->AddVariable(p.first, p.second);
  358. }
  359. }
  360. void add_method(const std::string& method_name, const std::string& method_params) {
  361. methods.push_back(std::make_pair(method_name, method_params));
  362. }
  363. GenContainer* clone_as(const std::string& new_name){
  364. auto mva = new MVA<ArgTypes...>(new_name, (MVAData<ArgTypes...>*)this->value, this->cut, this->opt);
  365. mva->methods = methods;
  366. return mva;
  367. }
  368. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  369. TFile* outputFile = gDirectory->GetFile();
  370. this->container->PrepareTrainingAndTestTree(cut.c_str(), opt.c_str());
  371. TMVA::Factory *factory = new TMVA::Factory("TMVAClassification", outputFile,
  372. "!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification");
  373. TMVA::Types& types = TMVA::Types::Instance();
  374. for(auto& p : methods){
  375. std::string method_name, method_params;
  376. std::tie(method_name, method_params) = p;
  377. TMVA::Types::EMVA method_type = types.GetMethodType(method_name);
  378. factory->BookMethod(this->container, method_type, method_name, method_params);
  379. }
  380. // Train MVAs using the set of training events
  381. factory->TrainAllMethods();
  382. // Evaluate all MVAs using the set of test events
  383. factory->TestAllMethods();
  384. // Evaluate and compare performance of all configured MVAs
  385. factory->EvaluateAllMethods();
  386. delete factory;
  387. }
  388. };
  389. }
  390. #endif // root_container_hpp