root_container.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 "container.hpp"
  16. namespace fv_root_util {
  17. using namespace fv;
  18. /**
  19. * Save a TObject. The TObject will typically be a Histogram or Graph object,
  20. * but can really be any TObject. The SaveOption can be used to specify how to
  21. * save the file.
  22. */
  23. void save_as(TObject *container, const std::string &fname, const SaveOption &option = SaveOption::PNG) {
  24. auto save_img = [](TObject *container, const std::string &fname) {
  25. TCanvas *c1 = new TCanvas("c1");
  26. container->Draw();
  27. c1->Draw();
  28. c1->SaveAs(fname.c_str());
  29. delete c1;
  30. };
  31. auto save_bin = [](TObject *container) {
  32. INFO("Saving object: " << container->GetName() << " into file " << gDirectory->GetName());
  33. container->Write(container->GetName(), TObject::kOverwrite);
  34. };
  35. switch (option) {
  36. case SaveOption::PNG:
  37. save_img(container, fname + ".png");
  38. break;
  39. case SaveOption::PDF:
  40. save_img(container, fname + ".pdf");
  41. break;
  42. case SaveOption::ROOT:
  43. save_bin(container);
  44. break;
  45. default:
  46. break;
  47. }
  48. }
  49. /**
  50. * Saves an STL container into a ROOT file. ROOT knows how to serialize STL
  51. * containers, but it needs the *name* of the type of the container, eg.
  52. * std::map<int,int> to be able to do this. In order to generate this name at
  53. * run-time, the fv_util::get_type_name function uses RTTI to get type info
  54. * and use it to look up the proper name.
  55. *
  56. * For nexted containers, it is necessary to generate the CLING dictionaries
  57. * for each type at compile time to enable serialization. To do this, add the
  58. * type definition into the LinkDef.hpp header file.
  59. */
  60. void save_as_stl(void *container, const std::string &type_name,
  61. const std::string &obj_name,
  62. const SaveOption &option = SaveOption::PNG) {
  63. switch (option) {
  64. case SaveOption::PNG:
  65. INFO("Cannot save STL container " << type_name << " as png");
  66. break;
  67. case SaveOption::PDF:
  68. INFO("Cannot save STL container " << type_name << " as pdf");
  69. break;
  70. case SaveOption::ROOT:
  71. /* DEBUG("Writing object \"" << obj_name << "\" of type \"" << type_name << "\"\n"); */
  72. gDirectory->WriteObjectAny(container, type_name.c_str(), obj_name.c_str());
  73. break;
  74. default:
  75. break;
  76. }
  77. }
  78. }
  79. namespace fv_root {
  80. using namespace fv;
  81. struct THParams {
  82. std::string label_x;
  83. int nbins_x;
  84. double low_x;
  85. double high_x;
  86. std::string label_y;
  87. int nbins_y;
  88. double low_y;
  89. double high_y;
  90. THParams(int nbins_x, double low_x, double high_x, int nbins_y=0, double low_y=0, double high_y=0)
  91. :nbins_x(nbins_x), low_x(low_x), high_x(high_x), nbins_y(nbins_y), low_y(low_y), high_y(high_y){}
  92. THParams(const std::string label_x, int nbins_x, double low_x, double high_x,
  93. const std::string label_y = "", int nbins_y=0, double low_y=0, double high_y=0)
  94. :label_x(label_x), nbins_x(nbins_x), low_x(low_x), high_x(high_x),
  95. label_y(label_y), nbins_y(nbins_y), low_y(low_y), high_y(high_y){}
  96. THParams() {
  97. label_x = "";
  98. nbins_x = 20;
  99. low_x = 0;
  100. high_x = 1;
  101. label_y = "";
  102. nbins_y = 20;
  103. low_y = 0;
  104. high_y = 1;
  105. }
  106. static THParams lookup(const std::string &&param_key) {
  107. auto hist_params = fv_util::the_config->get("hist-params");
  108. if (!hist_params[param_key]) {
  109. CRITICAL("Key \"" << param_key
  110. << "\" does not exist under hist-params in supplied config file. Add it!");
  111. std::string empty{""};
  112. return {};
  113. } else {
  114. auto params = hist_params[param_key];
  115. return {params["label_x"].as<std::string>(""),
  116. params["nbins_x"].as<int>(20),
  117. params["low_x"].as<double>(0),
  118. params["high_x"].as<double>(1),
  119. params["label_y"].as<std::string>(""),
  120. params["nbins_y"].as<int>(20),
  121. params["low_y"].as<double>(0),
  122. params["high_y"].as<double>(1)
  123. };
  124. }
  125. }
  126. };
  127. template<typename V>
  128. class ContainerTH1 : public Container<TH1, V> {
  129. protected:
  130. std::string title;
  131. THParams params;
  132. public:
  133. ContainerTH1(const std::string &name, const std::string &title, const THParams &params)
  134. : Container<TH1, V>(name), title(title), params(params) {
  135. this->container = new TH1D(this->get_name().c_str(), this->title.c_str(),
  136. params.nbins_x, params.low_x, params.high_x);
  137. this->container->SetXTitle(params.label_x.c_str());
  138. this->container->SetYTitle(params.label_y.c_str());
  139. }
  140. ContainerTH1(const std::string &name, const THParams &params)
  141. : ContainerTH1<V>(name, name, params) { }
  142. void fill(const V &v) {
  143. this->container->Fill(v);
  144. }
  145. void fill_weight(const V &v, const float &weight) {
  146. this->container->Fill(v, weight);
  147. }
  148. void fill_many(const std::vector<V> &vs) {
  149. for (const V &v : vs) {
  150. this->container->Fill(v);
  151. }
  152. }
  153. void save_as(const std::string &fname, const SaveOption &option = SaveOption::PNG) {
  154. if (this->get_container() == nullptr) {
  155. WARNING("Container " << this->get_name() << " never filled, cowardly refusing to save");
  156. } else {
  157. fv_root_util::save_as(this->get_container(), fname, option);
  158. }
  159. }
  160. };
  161. template<typename V>
  162. class ContainerTH2 : public Container<TH2, V> {
  163. private:
  164. std::string title;
  165. THParams params;
  166. public:
  167. ContainerTH2(const std::string &name, const std::string &title, THParams params)
  168. : Container<TH2, V>(name), title(title), params(params) {
  169. this->container = new TH2D(this->get_name().c_str(), this->title.c_str(),
  170. params.nbins_x, params.low_x, params.high_x,
  171. params.nbins_y, params.low_y, params.high_y);
  172. this->container->SetXTitle(params.label_x.c_str());
  173. this->container->SetYTitle(params.label_y.c_str());
  174. }
  175. ContainerTH2(const std::string &name, const THParams &params)
  176. : ContainerTH2<V>(name, name, params) { }
  177. void fill(const V& x, const V& y) {
  178. this->container->Fill(x, y);
  179. }
  180. void fill_weight(const V& x, const V& y, const V& weight) {
  181. this->container->Fill(x, y, weight);
  182. }
  183. void save_as(const std::string &fname, const SaveOption &option = SaveOption::PNG) {
  184. fv_root_util::save_as(this->get_container(), fname, option);
  185. }
  186. };
  187. template<typename V>
  188. class ContainerTGraph : public Container<TGraph, V> {
  189. private:
  190. std::vector<V> x_data;
  191. std::vector<V> y_data;
  192. std::string title;
  193. bool data_modified;
  194. public:
  195. ContainerTGraph(const std::string &name, const std::string &title)
  196. : Container<TGraph, V>(name), data_modified(false) {
  197. this->container = new TGraph();
  198. }
  199. TGraph *get_container() {
  200. if (data_modified) {
  201. delete this->container;
  202. this->container = new TGraph(x_data.size(), x_data.data(), y_data.data());
  203. this->container->SetName(this->get_name().c_str());
  204. this->container->SetTitle(title.c_str());
  205. data_modified = false;
  206. }
  207. return this->container;
  208. }
  209. void save_as(const std::string &fname, const SaveOption &option = SaveOption::PNG) {
  210. fv_root_util::save_as(get_container(), fname, option);
  211. }
  212. void fill(const V& x, const V& y) {
  213. x_data.push_back(x);
  214. y_data.push_back(y);
  215. data_modified = true;
  216. }
  217. };
  218. template<typename V>
  219. class Vector : public Container<std::vector<V>, V> {
  220. public:
  221. Vector(const std::string &name)
  222. : Container<std::vector<V>, V>(name) {
  223. this->container = new std::vector<V>;
  224. }
  225. void fill(const V& v) {
  226. this->container->push_back(v);
  227. }
  228. void save_as(const std::string &fname, const SaveOption &option = SaveOption::PNG) {
  229. std::string type_name = "std::vector<" + fv_util::get_type_name(typeid(V)) + ">";
  230. fv_root_util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
  231. }
  232. };
  233. /**
  234. * A Counter that keeps a mapping of the number of occurances of each input
  235. * value.
  236. */
  237. template<typename V>
  238. class Counter : public Container<std::map<V, size_t>, V> {
  239. public:
  240. Counter(const std::string &name)
  241. : Container<std::map<V, size_t>, V>(name) {
  242. this->container = new std::map<V, size_t>;
  243. }
  244. void save_as(const std::string &fname, const SaveOption &option = SaveOption::PNG) {
  245. std::string type_name = "std::map<" + fv_util::get_type_name(typeid(V)) + ",int>";
  246. fv_root_util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
  247. }
  248. void fill(const V& v) {
  249. (*this->container)[v]++;
  250. }
  251. };
  252. template<typename V>
  253. class EfficiencyContainer : public Container<TH1F, V> {
  254. private:
  255. TH1F num;
  256. TH1F den;
  257. TH1F eff;
  258. THParams params;
  259. public:
  260. EfficiencyContainer(const std::string &name, THParams params)
  261. : Container<TH1F, V>(name),
  262. num{(name + "_num").c_str(), (name + "_num").c_str(), params.nbins_x, params.low_x, params.high_x},
  263. den{(name + "_den").c_str(), (name + "_den").c_str(), params.nbins_x, params.low_x, params.high_x},
  264. eff{name.c_str(), name.c_str(), params.nbins_x, params.low_x, params.high_x}, params(params) {
  265. num.SetXTitle(params.label_x.c_str());
  266. num.SetYTitle(params.label_y.c_str());
  267. den.SetXTitle(params.label_x.c_str());
  268. den.SetYTitle(params.label_y.c_str());
  269. eff.SetXTitle(params.label_x.c_str());
  270. eff.SetYTitle(params.label_y.c_str());
  271. this->container = &eff;
  272. }
  273. TH1F *get_container() {
  274. eff.Sumw2();
  275. eff.Divide(&num, &den, 1, 1, "B");
  276. return this->container;
  277. }
  278. void save_as(const std::string &fname, const SaveOption &option = SaveOption::PNG) {
  279. fv_root_util::save_as(this->get_container(), fname, option);
  280. fv_root_util::save_as(&num, fname, option);
  281. fv_root_util::save_as(&den, fname, option);
  282. }
  283. void fill(const V& v, bool pass) {
  284. den.Fill(v);
  285. if (pass) num.Fill(v);
  286. }
  287. };
  288. template<typename V>
  289. class EfficiencyContainer2D : public Container<TH2F, V> {
  290. private:
  291. TH2F num;
  292. TH2F den;
  293. TH2F eff;
  294. THParams params;
  295. public:
  296. EfficiencyContainer2D(const std::string &name, THParams params)
  297. : Container<TH2F, V>(name),
  298. num{(name + "_num").c_str(), (name + "_num").c_str(),
  299. params.nbins_x, params.low_x, params.high_x, params.nbins_y, params.low_y, params.high_y},
  300. den{(name + "_den").c_str(), (name + "_den").c_str(),
  301. params.nbins_x, params.low_x, params.high_x, params.nbins_y, params.low_y, params.high_y},
  302. eff{name.c_str(), name.c_str(),
  303. params.nbins_x, params.low_x, params.high_x, params.nbins_y, params.low_y, params.high_y},
  304. params(params) {
  305. num.SetXTitle(params.label_x.c_str());
  306. num.SetYTitle(params.label_y.c_str());
  307. den.SetXTitle(params.label_x.c_str());
  308. den.SetYTitle(params.label_y.c_str());
  309. eff.SetXTitle(params.label_x.c_str());
  310. eff.SetYTitle(params.label_y.c_str());
  311. this->container = &eff;
  312. }
  313. TH2F *get_container() {
  314. eff.Sumw2();
  315. eff.Divide(&num, &den, 1, 1, "B");
  316. return this->container;
  317. }
  318. void save_as(const std::string &fname, const SaveOption &option = SaveOption::PNG) {
  319. fv_root_util::save_as(this->get_container(), fname, option);
  320. fv_root_util::save_as(&num, fname, option);
  321. fv_root_util::save_as(&den, fname, option);
  322. }
  323. void fill(const V& x,const V& y, bool pass) {
  324. den.Fill(x, y);
  325. if (pass) num.Fill(x, y);
  326. }
  327. };
  328. }
  329. #endif // root_container_hpp