root_container.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. void fill(const V &v) {
  118. this->container->Fill(v);
  119. }
  120. void fill_weight(const V &v, const float &weight) {
  121. this->container->Fill(v, weight);
  122. }
  123. void fill_many(const std::vector<V> &vs) {
  124. for (const V &v : vs) {
  125. this->container->Fill(v);
  126. }
  127. }
  128. void save_as(const std::string &fname, const SaveOption &option = SaveOption::PNG) {
  129. if (this->get_container() == nullptr) {
  130. WARNING("Container " << this->get_name() << " never filled, cowardly refusing to save");
  131. } else {
  132. fv_root_util::save_as(this->get_container(), fname, option);
  133. }
  134. }
  135. GenContainer *clone_as(const std::string &new_name) {
  136. return new ContainerTH1<V>(new_name, this->title, this->params);
  137. }
  138. };
  139. struct TH2Params {
  140. std::string label_x;
  141. int nbins_x;
  142. double low_x;
  143. double high_x;
  144. std::string label_y;
  145. int nbins_y;
  146. double low_y;
  147. double high_y;
  148. static TH2Params lookup(const std::string &&param_key) {
  149. auto hist_params = fv_util::the_config->get("hist-params");
  150. if (!hist_params[param_key]) {
  151. CRITICAL(
  152. "Key \"" << param_key << "\" does not exist under hist-params in supplied config file. Add it!",
  153. true);
  154. } else {
  155. auto params = hist_params[param_key];
  156. return TH2Params({params["label_x"].as<std::string>(),
  157. params["nbins_x"].as<int>(),
  158. params["low_x"].as<double>(),
  159. params["high_x"].as<double>(),
  160. params["label_y"].as<std::string>(),
  161. params["nbins_y"].as<int>(),
  162. params["low_y"].as<double>(),
  163. params["high_y"].as<double>()
  164. });
  165. }
  166. }
  167. };
  168. template<typename V>
  169. class ContainerTH2 : public Container<TH2, V> {
  170. private:
  171. std::string title;
  172. TH2Params params;
  173. public:
  174. ContainerTH2(const std::string &name, const std::string &title, TH2Params params)
  175. : Container<TH2, V>(name), title(title), params(params) {
  176. this->container = new TH2D(this->get_name().c_str(), this->title.c_str(),
  177. params.nbins_x, params.low_x, params.high_x,
  178. params.nbins_y, params.low_y, params.high_y);
  179. this->container->SetXTitle(params.label_x.c_str());
  180. this->container->SetYTitle(params.label_y.c_str());
  181. }
  182. void fill(const V& x, const V& y) {
  183. this->container->Fill(x, y);
  184. }
  185. void fill_weight(const V& x, const V& y, const V& weight) {
  186. this->container->Fill(x, y, weight);
  187. }
  188. void save_as(const std::string &fname, const SaveOption &option = SaveOption::PNG) {
  189. fv_root_util::save_as(this->get_container(), fname, option);
  190. }
  191. };
  192. template<typename V>
  193. class ContainerTGraph : public Container<TGraph, V> {
  194. private:
  195. std::vector<V> x_data;
  196. std::vector<V> y_data;
  197. std::string title;
  198. bool data_modified;
  199. public:
  200. ContainerTGraph(const std::string &name, const std::string &title)
  201. : Container<TGraph, V>(name), data_modified(false) {
  202. this->container = new TGraph();
  203. }
  204. TGraph *get_container() {
  205. if (data_modified) {
  206. delete this->container;
  207. this->container = new TGraph(x_data.size(), x_data.data(), y_data.data());
  208. this->container->SetName(this->get_name().c_str());
  209. this->container->SetTitle(title.c_str());
  210. data_modified = false;
  211. }
  212. return this->container;
  213. }
  214. GenContainer *clone_as(const std::string &new_name) {
  215. return new ContainerTGraph<V>(new_name, this->title);
  216. }
  217. void save_as(const std::string &fname, const SaveOption &option = SaveOption::PNG) {
  218. fv_root_util::save_as(get_container(), fname, option);
  219. }
  220. void fill(const V& x, const V& y) {
  221. x_data.push_back(x);
  222. y_data.push_back(y);
  223. data_modified = true;
  224. }
  225. };
  226. template<typename V>
  227. class Vector : public Container<std::vector<V>, V> {
  228. public:
  229. Vector(const std::string &name)
  230. : Container<std::vector<V>, V>(name) {
  231. this->container = new std::vector<V>;
  232. }
  233. void fill(const V& v) {
  234. this->container->push_back(v);
  235. }
  236. GenContainer *clone_as(const std::string &new_name) {
  237. return new Vector<V>(new_name);
  238. }
  239. void save_as(const std::string &fname, const SaveOption &option = SaveOption::PNG) {
  240. std::string type_name = "std::vector<" + fv::util::get_type_name(typeid(V)) + ">";
  241. fv_root_util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
  242. }
  243. };
  244. /**
  245. * A Counter that keeps a mapping of the number of occurances of each input
  246. * value.
  247. */
  248. template<typename V>
  249. class Counter : public Container<std::map<V, size_t>, V> {
  250. public:
  251. Counter(const std::string &name)
  252. : Container<std::map<V, size_t>, V>(name) {
  253. this->container = new std::map<V, size_t>;
  254. }
  255. void save_as(const std::string &fname, const SaveOption &option = SaveOption::PNG) {
  256. std::string type_name = "std::map<" + fv::util::get_type_name(typeid(V)) + ",int>";
  257. fv_root_util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
  258. }
  259. void fill(const V& v) {
  260. (*this->container)[v]++;
  261. }
  262. GenContainer *clone_as(const std::string &new_name) {
  263. return new Counter<V>(new_name);
  264. }
  265. };
  266. template<typename T>
  267. class EfficiencyContainer : public Container<TH1F, std::vector<T>> {
  268. private:
  269. std::function<bool(T)> *selector; // Selects whether object is up for consideration
  270. std::function<bool(T)> *predicate; // Says whether the object passes the efficiency criteria
  271. std::function<float(T)> *get_val; // Returns a floating point value from the object that is actually
  272. // used in the histogram
  273. bool write_num_den;
  274. TH1F num;
  275. TH1F den;
  276. TH1F eff;
  277. TH1Params params;
  278. void _fill() {
  279. for (auto &obj : this->value->get_value()) {
  280. if (selector == nullptr or (*selector)(obj)) {
  281. float val = (*get_val)(obj);
  282. den.Fill(val);
  283. if ((*predicate)(obj)) {
  284. num.Fill(val);
  285. }
  286. }
  287. }
  288. }
  289. public:
  290. EfficiencyContainer(const std::string &name, Value<std::vector<T>> *value, TH1Params params,
  291. std::function<bool(T)> *selector, std::function<bool(T)> *predicate,
  292. std::function<float(T)> *get_val, bool write_num_den = false)
  293. : Container<TH1F, std::vector<T>>(name, value),
  294. num{(name + "_num").c_str(), (name + "_num").c_str(), params.nbins, params.low, params.high},
  295. den{(name + "_den").c_str(), (name + "_den").c_str(), params.nbins, params.low, params.high},
  296. eff{name.c_str(), name.c_str(), params.nbins, params.low, params.high},
  297. selector(selector), predicate(predicate), get_val(get_val), params(params),
  298. write_num_den(write_num_den) {
  299. num.SetXTitle(params.label_x.c_str());
  300. num.SetYTitle(params.label_y.c_str());
  301. den.SetXTitle(params.label_x.c_str());
  302. den.SetYTitle(params.label_y.c_str());
  303. eff.SetXTitle(params.label_x.c_str());
  304. eff.SetYTitle(params.label_y.c_str());
  305. this->container = &eff;
  306. }
  307. TH1F *get_container() {
  308. eff.Sumw2();
  309. eff.Divide(&num, &den, 1, 1, "B");
  310. return this->container;
  311. }
  312. GenContainer *clone_as(const std::string &new_name) {
  313. return new EfficiencyContainer<T>(new_name, this->value, this->params, selector, predicate, get_val);
  314. }
  315. void save_as(const std::string &fname, const SaveOption &option = SaveOption::PNG) {
  316. fv_root_util::save_as(this->get_container(), fname, option);
  317. if (write_num_den) {
  318. fv_root_util::save_as(&num, fname, option);
  319. fv_root_util::save_as(&den, fname, option);
  320. }
  321. }
  322. };
  323. }
  324. #endif // root_container_hpp