container.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 "filval/container.hpp"
  13. namespace fv::root::util{
  14. /**
  15. * Save a TObject. The TObject will typically be a Histogram or Graph object,
  16. * but can really be any TObject. The SaveOption can be used to specify how to
  17. * save the file.
  18. */
  19. void save_as(TObject* container, const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  20. auto save_img = [](TObject* container, const std::string& fname){
  21. TCanvas* c1 = new TCanvas("c1");
  22. container->Draw();
  23. c1->Draw();
  24. c1->SaveAs(fname.c_str());
  25. delete c1;
  26. };
  27. auto save_bin = [](TObject* container){
  28. INFO("Saving object: " << container->GetName() << " into file " << gDirectory->GetName());
  29. container->Write(container->GetName(), TObject::kOverwrite);
  30. };
  31. switch(option){
  32. case PNG:
  33. save_img(container, fname+".png"); break;
  34. case PDF:
  35. save_img(container, fname+".pdf"); break;
  36. case ROOT:
  37. save_bin(container); break;
  38. default:
  39. break;
  40. }
  41. }
  42. /**
  43. * Saves an STL container into a ROOT file. ROOT knows how to serialize STL
  44. * containers, but it needs the *name* of the type of the container, eg.
  45. * std::map<int,int> to be able to do this. In order to generate this name at
  46. * run-time, the fv::util::get_type_name function uses RTTI to get type info
  47. * and use it to look up the proper name.
  48. *
  49. * For nexted containers, it is necessary to generate the CLING dictionaries
  50. * for each type at compile time to enable serialization. To do this, add the
  51. * type definition into the LinkDef.hpp header file.
  52. */
  53. void save_as_stl(void* container, const std::string& type_name,
  54. const std::string& obj_name,
  55. const SaveOption& option = SaveOption::PNG) {
  56. switch(option){
  57. case PNG:
  58. INFO("Cannot save STL container " << type_name <<" as png");
  59. break;
  60. case PDF:
  61. INFO("Cannot save STL container " << type_name <<" as pdf");
  62. break;
  63. case ROOT:
  64. gDirectory->WriteObjectAny(container, type_name.c_str(), obj_name.c_str());
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. }
  71. namespace fv::root {
  72. template <typename V>
  73. class _ContainerTH1 : public Container<TH1>{
  74. private:
  75. void _fill(){
  76. if (container == nullptr){
  77. if (value == nullptr){
  78. CRITICAL("Container: \"" << get_name() << "\" has a null Value object. "
  79. << "Probably built with imcompatible type",-1);
  80. }
  81. this->container = new TH1D(this->get_name().c_str(), this->title.c_str(),
  82. this->nbins, this->low, this->high);
  83. this->container->SetXTitle(label_x.c_str());
  84. this->container->SetYTitle(label_y.c_str());
  85. }
  86. _do_fill();
  87. }
  88. protected:
  89. std::string title;
  90. std::string label_x;
  91. std::string label_y;
  92. int nbins;
  93. double low;
  94. double high;
  95. Value<V> *value;
  96. virtual void _do_fill() = 0;
  97. public:
  98. explicit _ContainerTH1(const std::string &name, const std::string& title, Value<V>* value,
  99. int nbins, double low, double high,
  100. const std::string& label_x = "",
  101. const std::string& label_y = "")
  102. :Container<TH1>(name, nullptr),
  103. title(title), nbins(nbins), low(low), high(high),
  104. label_x(label_x), label_y(label_y),
  105. value(value) { }
  106. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  107. util::save_as(get_container(), fname, option);
  108. }
  109. };
  110. template <typename V>
  111. class ContainerTH1 : public _ContainerTH1<V>{
  112. using _ContainerTH1<V>::_ContainerTH1;
  113. void _do_fill(){
  114. this->container->Fill(this->value->get_value());
  115. }
  116. };
  117. template <typename V>
  118. class ContainerTH1Many : public _ContainerTH1<std::vector<V>>{
  119. using _ContainerTH1<std::vector<V>>::_ContainerTH1;
  120. void _do_fill(){
  121. for(V x : this->value->get_value())
  122. this->container->Fill(x);
  123. }
  124. };
  125. template <typename V>
  126. class _ContainerTH2 : public Container<TH2>{
  127. private:
  128. void _fill(){
  129. if (container == nullptr){
  130. if (value == nullptr){
  131. CRITICAL("Container: \"" << get_name() << "\" has a null Value object. "
  132. << "Probably built with imcompatible type",-1);
  133. }
  134. this->container = new TH2D(this->get_name().c_str(), this->title.c_str(),
  135. this->nbins_x, this->low_x, this->high_x,
  136. this->nbins_y, this->low_y, this->high_y);
  137. this->container->SetXTitle(label_x.c_str());
  138. this->container->SetYTitle(label_y.c_str());
  139. }
  140. _do_fill(value->get_value());
  141. }
  142. protected:
  143. std::string title;
  144. std::string label_x;
  145. std::string label_y;
  146. int nbins_x;
  147. int nbins_y;
  148. double low_x;
  149. double low_y;
  150. double high_x;
  151. double high_y;
  152. Value<std::pair<V,V>> *value;
  153. virtual void _do_fill(std::pair<V,V>& val) = 0;
  154. public:
  155. explicit _ContainerTH2(const std::string& name, const std::string& title,
  156. Value<std::pair<V, V>>* value,
  157. int nbins_x, double low_x, double high_x,
  158. int nbins_y, double low_y, double high_y,
  159. const std::string& label_x = "",
  160. const std::string& label_y = "")
  161. :Container<TH2>(name, nullptr),
  162. title(title),
  163. nbins_x(nbins_x), low_x(low_x), high_x(high_x),
  164. nbins_y(nbins_y), low_y(low_y), high_y(high_y),
  165. label_x(label_x), label_y(label_y),
  166. value(value) { }
  167. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  168. util::save_as(get_container(), fname, option);
  169. }
  170. };
  171. template <typename V>
  172. class ContainerTH2 : public _ContainerTH2<V>{
  173. using _ContainerTH2<V>::_ContainerTH2;
  174. void _do_fill(std::pair<V,V>& val){
  175. this->container->Fill(val.first,val.second);
  176. }
  177. };
  178. template <typename V>
  179. class ContainerTH2Many : public _ContainerTH2<std::vector<V>>{
  180. using _ContainerTH2<std::vector<V>>::_ContainerTH2;
  181. void _do_fill(std::pair<std::vector<V>,std::vector<V>>& val){
  182. int min_size = std::min(val.first.size(), val.second.size());
  183. for(int i=0; i<min_size; i++)
  184. this->container->Fill(val.first[i],val.second[i]);
  185. }
  186. };
  187. class ContainerTGraph : public Container<TGraph>{
  188. private:
  189. Value<std::pair<int, int> > *value;
  190. std::vector<int> x_data;
  191. std::vector<int> y_data;
  192. std::string title;
  193. bool data_modified;
  194. void _fill(){
  195. auto val = value->get_value();
  196. x_data.push_back(val.first);
  197. y_data.push_back(val.second);
  198. data_modified = true;
  199. }
  200. public:
  201. ContainerTGraph(const std::string& name, const std::string& title, Value<std::pair<int, int>>* value)
  202. :Container<TGraph>(name, new TGraph()),
  203. value(value),
  204. data_modified(false){ }
  205. TGraph* get_container(){
  206. if (data_modified){
  207. delete container;
  208. container = new TGraph(x_data.size(), x_data.data(), y_data.data());
  209. container->SetName(get_name().c_str());
  210. container->SetTitle(title.c_str());
  211. data_modified = false;
  212. }
  213. return container;
  214. }
  215. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  216. util::save_as(get_container(), fname, option);
  217. }
  218. };
  219. template <typename T>
  220. class Vector : public Container<std::vector<T> >{
  221. private:
  222. Value<T>* value;
  223. void _fill(){
  224. this->container->push_back(value->get_value());
  225. }
  226. public:
  227. Vector(const std::string& name, std::vector<T>* container, Value<T>* value)
  228. :Container<std::vector<T>>(name, container), value(value){ }
  229. Vector(const std::string& name, Value<T>* value)
  230. :Container<std::vector<T>>(name, nullptr), value(value){
  231. this->container = new std::vector<T>();
  232. }
  233. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  234. std::string type_name = "std::vector<"+fv::util::get_type_name(typeid(T))+">";
  235. util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
  236. }
  237. };
  238. template <typename V, typename D>
  239. class _Counter : public Container<std::map<D,int>>{
  240. protected:
  241. Value<V>* value;
  242. public:
  243. explicit _Counter(const std::string& name, Value<V>* value)
  244. :Container<std::map<D,int>>(name, new std::map<D,int>()),
  245. value(value) { }
  246. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  247. std::string type_name = "std::map<"+fv::util::get_type_name(typeid(D))+",int>";
  248. util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
  249. }
  250. };
  251. /**
  252. * A Counter that keeps a mapping of the number of occurances of each input
  253. * value.
  254. */
  255. template <typename V>
  256. class Counter : public _Counter<V,V>{
  257. using _Counter<V,V>::_Counter;
  258. void _fill(){
  259. (*this->container)[this->value->get_value()]++;
  260. }
  261. };
  262. /**
  263. * Same as Counter but accepts multiple values per fill.
  264. */
  265. template <typename V>
  266. class CounterMany : public _Counter<std::vector<V>,V>{
  267. using _Counter<std::vector<V>,V>::_Counter;
  268. void _fill(){
  269. for(V& val : this->value->get_value())
  270. (*this->container)[val]++;
  271. }
  272. };
  273. }
  274. #endif // root_container_hpp