root_container.hpp 13 KB

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