root_container.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 TH1Params {
  82. std::string label_x;
  83. int nbins;
  84. double low;
  85. double high;
  86. std::string label_y;
  87. static TH1Params lookup(const std::string &&param_key) {
  88. auto hist_params = fv_util::the_config->get("hist-params");
  89. if (!hist_params[param_key]) {
  90. CRITICAL(
  91. "Key \"" << param_key << "\" does not exist under hist-params in supplied config file. Add it!",
  92. true);
  93. } else {
  94. auto params = hist_params[param_key];
  95. return TH1Params({params["label_x"].as<std::string>(),
  96. params["nbins"].as<int>(),
  97. params["low"].as<double>(),
  98. params["high"].as<double>(),
  99. params["label_y"].as<std::string>()
  100. });
  101. }
  102. }
  103. };
  104. template<typename V>
  105. class ContainerTH1 : public Container<TH1, V> {
  106. protected:
  107. std::string title;
  108. TH1Params params;
  109. public:
  110. ContainerTH1(const std::string &name, const std::string &title, const TH1Params &params)
  111. : Container<TH1, V>(name), title(title), params(params) {
  112. this->container = new TH1D(this->get_name().c_str(), this->title.c_str(),
  113. params.nbins, params.low, params.high);
  114. this->container->SetXTitle(params.label_x.c_str());
  115. this->container->SetYTitle(params.label_y.c_str());
  116. }
  117. ContainerTH1(const std::string &name, const TH1Params &params)
  118. : ContainerTH1<V>(name, name, params) { }
  119. void fill(const V &v) {
  120. this->container->Fill(v);
  121. }
  122. void fill_weight(const V &v, const float &weight) {
  123. this->container->Fill(v, weight);
  124. }
  125. void fill_many(const std::vector<V> &vs) {
  126. for (const V &v : vs) {
  127. this->container->Fill(v);
  128. }
  129. }
  130. void save_as(const std::string &fname, const SaveOption &option = SaveOption::PNG) {
  131. if (this->get_container() == nullptr) {
  132. WARNING("Container " << this->get_name() << " never filled, cowardly refusing to save");
  133. } else {
  134. fv_root_util::save_as(this->get_container(), fname, option);
  135. }
  136. }
  137. };
  138. struct TH2Params {
  139. std::string label_x;
  140. int nbins_x;
  141. double low_x;
  142. double high_x;
  143. std::string label_y;
  144. int nbins_y;
  145. double low_y;
  146. double high_y;
  147. static TH2Params lookup(const std::string &&param_key) {
  148. auto hist_params = fv_util::the_config->get("hist-params");
  149. if (!hist_params[param_key]) {
  150. CRITICAL(
  151. "Key \"" << param_key << "\" does not exist under hist-params in supplied config file. Add it!",
  152. true);
  153. } else {
  154. auto params = hist_params[param_key];
  155. return TH2Params({params["label_x"].as<std::string>(),
  156. params["nbins_x"].as<int>(),
  157. params["low_x"].as<double>(),
  158. params["high_x"].as<double>(),
  159. params["label_y"].as<std::string>(),
  160. params["nbins_y"].as<int>(),
  161. params["low_y"].as<double>(),
  162. params["high_y"].as<double>()
  163. });
  164. }
  165. }
  166. };
  167. template<typename V>
  168. class ContainerTH2 : public Container<TH2, V> {
  169. private:
  170. std::string title;
  171. TH2Params params;
  172. public:
  173. ContainerTH2(const std::string &name, const std::string &title, TH2Params params)
  174. : Container<TH2, V>(name), title(title), params(params) {
  175. this->container = new TH2D(this->get_name().c_str(), this->title.c_str(),
  176. params.nbins_x, params.low_x, params.high_x,
  177. params.nbins_y, params.low_y, params.high_y);
  178. this->container->SetXTitle(params.label_x.c_str());
  179. this->container->SetYTitle(params.label_y.c_str());
  180. }
  181. ContainerTH2(const std::string &name, const TH2Params &params)
  182. : ContainerTH2<V>(name, name, params) { }
  183. void fill(const V& x, const V& y) {
  184. this->container->Fill(x, y);
  185. }
  186. void fill_weight(const V& x, const V& y, const V& weight) {
  187. this->container->Fill(x, y, weight);
  188. }
  189. void save_as(const std::string &fname, const SaveOption &option = SaveOption::PNG) {
  190. fv_root_util::save_as(this->get_container(), fname, option);
  191. }
  192. };
  193. template<typename V>
  194. class ContainerTGraph : public Container<TGraph, V> {
  195. private:
  196. std::vector<V> x_data;
  197. std::vector<V> y_data;
  198. std::string title;
  199. bool data_modified;
  200. public:
  201. ContainerTGraph(const std::string &name, const std::string &title)
  202. : Container<TGraph, V>(name), data_modified(false) {
  203. this->container = new TGraph();
  204. }
  205. TGraph *get_container() {
  206. if (data_modified) {
  207. delete this->container;
  208. this->container = new TGraph(x_data.size(), x_data.data(), y_data.data());
  209. this->container->SetName(this->get_name().c_str());
  210. this->container->SetTitle(title.c_str());
  211. data_modified = false;
  212. }
  213. return this->container;
  214. }
  215. void save_as(const std::string &fname, const SaveOption &option = SaveOption::PNG) {
  216. fv_root_util::save_as(get_container(), fname, option);
  217. }
  218. void fill(const V& x, const V& y) {
  219. x_data.push_back(x);
  220. y_data.push_back(y);
  221. data_modified = true;
  222. }
  223. };
  224. template<typename V>
  225. class Vector : public Container<std::vector<V>, V> {
  226. public:
  227. Vector(const std::string &name)
  228. : Container<std::vector<V>, V>(name) {
  229. this->container = new std::vector<V>;
  230. }
  231. void fill(const V& v) {
  232. this->container->push_back(v);
  233. }
  234. void save_as(const std::string &fname, const SaveOption &option = SaveOption::PNG) {
  235. std::string type_name = "std::vector<" + fv::util::get_type_name(typeid(V)) + ">";
  236. fv_root_util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
  237. }
  238. };
  239. /**
  240. * A Counter that keeps a mapping of the number of occurances of each input
  241. * value.
  242. */
  243. template<typename V>
  244. class Counter : public Container<std::map<V, size_t>, V> {
  245. public:
  246. Counter(const std::string &name)
  247. : Container<std::map<V, size_t>, V>(name) {
  248. this->container = new std::map<V, size_t>;
  249. }
  250. void save_as(const std::string &fname, const SaveOption &option = SaveOption::PNG) {
  251. std::string type_name = "std::map<" + fv::util::get_type_name(typeid(V)) + ",int>";
  252. fv_root_util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
  253. }
  254. void fill(const V& v) {
  255. (*this->container)[v]++;
  256. }
  257. };
  258. template<typename V>
  259. class EfficiencyContainer : public Container<TH1F, V> {
  260. private:
  261. TH1F num;
  262. TH1F den;
  263. TH1F eff;
  264. TH1Params params;
  265. public:
  266. EfficiencyContainer(const std::string &name, TH1Params params)
  267. : Container<TH1F, V>(name),
  268. num{(name + "_num").c_str(), (name + "_num").c_str(), params.nbins, params.low, params.high},
  269. den{(name + "_den").c_str(), (name + "_den").c_str(), params.nbins, params.low, params.high},
  270. eff{name.c_str(), name.c_str(), params.nbins, params.low, params.high}, params(params) {
  271. num.SetXTitle(params.label_x.c_str());
  272. num.SetYTitle(params.label_y.c_str());
  273. den.SetXTitle(params.label_x.c_str());
  274. den.SetYTitle(params.label_y.c_str());
  275. eff.SetXTitle(params.label_x.c_str());
  276. eff.SetYTitle(params.label_y.c_str());
  277. this->container = &eff;
  278. }
  279. TH1F *get_container() {
  280. eff.Sumw2();
  281. eff.Divide(&num, &den, 1, 1, "B");
  282. return this->container;
  283. }
  284. void save_as(const std::string &fname, const SaveOption &option = SaveOption::PNG) {
  285. fv_root_util::save_as(this->get_container(), fname, option);
  286. fv_root_util::save_as(&num, fname, option);
  287. fv_root_util::save_as(&den, fname, option);
  288. }
  289. void fill(const V& v, bool pass) {
  290. den.Fill(v);
  291. if (pass) num.Fill(v);
  292. }
  293. };
  294. }
  295. #endif // root_container_hpp