container.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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,V>{
  74. private:
  75. void _fill(){
  76. if (this->container == nullptr){
  77. if (this->value == nullptr){
  78. CRITICAL("Container: \"" << this->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. virtual void _do_fill() = 0;
  96. public:
  97. explicit _ContainerTH1(const std::string &name, const std::string& title, Value<V>* value,
  98. int nbins, double low, double high,
  99. const std::string& label_x = "",
  100. const std::string& label_y = "")
  101. :Container<TH1,V>(name, value),
  102. title(title), nbins(nbins), low(low), high(high),
  103. label_x(label_x), label_y(label_y) { }
  104. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  105. util::save_as(this->get_container(), fname, option);
  106. }
  107. };
  108. template <typename V>
  109. class ContainerTH1 : public _ContainerTH1<V>{
  110. using _ContainerTH1<V>::_ContainerTH1;
  111. void _do_fill(){
  112. this->container->Fill(this->value->get_value());
  113. }
  114. };
  115. template <typename V>
  116. class ContainerTH1Many : public _ContainerTH1<std::vector<V>>{
  117. using _ContainerTH1<std::vector<V>>::_ContainerTH1;
  118. void _do_fill(){
  119. for(V x : this->value->get_value())
  120. this->container->Fill(x);
  121. }
  122. };
  123. template <typename V>
  124. class _ContainerTH2 : public Container<TH2,std::pair<V,V>>{
  125. private:
  126. void _fill(){
  127. if (this->container == nullptr){
  128. if (this->value == nullptr){
  129. CRITICAL("Container: \"" << this->get_name() << "\" has a null Value object. "
  130. << "Probably built with imcompatible type",-1);
  131. }
  132. this->container = new TH2D(this->get_name().c_str(), this->title.c_str(),
  133. this->nbins_x, this->low_x, this->high_x,
  134. this->nbins_y, this->low_y, this->high_y);
  135. this->container->SetXTitle(label_x.c_str());
  136. this->container->SetYTitle(label_y.c_str());
  137. }
  138. _do_fill(this->value->get_value());
  139. }
  140. protected:
  141. std::string title;
  142. std::string label_x;
  143. std::string label_y;
  144. int nbins_x;
  145. int nbins_y;
  146. double low_x;
  147. double low_y;
  148. double high_x;
  149. double high_y;
  150. virtual void _do_fill(std::pair<V,V>& val) = 0;
  151. public:
  152. explicit _ContainerTH2(const std::string& name, const std::string& title,
  153. Value<std::pair<V, V>>* value,
  154. int nbins_x, double low_x, double high_x,
  155. int nbins_y, double low_y, double high_y,
  156. const std::string& label_x = "",
  157. const std::string& label_y = "")
  158. :Container<TH2,std::pair<V,V>>(name, value),
  159. title(title),
  160. nbins_x(nbins_x), low_x(low_x), high_x(high_x),
  161. nbins_y(nbins_y), low_y(low_y), high_y(high_y),
  162. label_x(label_x), label_y(label_y) { }
  163. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  164. util::save_as(this->get_container(), fname, option);
  165. }
  166. };
  167. template <typename V>
  168. class ContainerTH2 : public _ContainerTH2<V>{
  169. using _ContainerTH2<V>::_ContainerTH2;
  170. void _do_fill(std::pair<V,V>& val){
  171. this->container->Fill(val.first,val.second);
  172. }
  173. };
  174. template <typename V>
  175. class ContainerTH2Many : public _ContainerTH2<std::vector<V>>{
  176. using _ContainerTH2<std::vector<V>>::_ContainerTH2;
  177. void _do_fill(std::pair<std::vector<V>,std::vector<V>>& val){
  178. int min_size = std::min(val.first.size(), val.second.size());
  179. for(int i=0; i<min_size; i++)
  180. this->container->Fill(val.first[i],val.second[i]);
  181. }
  182. };
  183. template <typename V>
  184. class ContainerTGraph : public Container<TGraph,std::pair<V,V>>{
  185. private:
  186. std::vector<V> x_data;
  187. std::vector<V> y_data;
  188. std::string title;
  189. bool data_modified;
  190. void _fill(){
  191. auto val = this->value->get_value();
  192. x_data.push_back(val.first);
  193. y_data.push_back(val.second);
  194. data_modified = true;
  195. }
  196. public:
  197. ContainerTGraph(const std::string& name, const std::string& title, Value<std::pair<V, V>>* value)
  198. :Container<TGraph,std::pair<V,V>>(name, value),
  199. data_modified(false){
  200. this->container = new TGraph();
  201. }
  202. TGraph* get_container(){
  203. if (data_modified){
  204. delete this->container;
  205. this->container = new TGraph(x_data.size(), x_data.data(), y_data.data());
  206. this->container->SetName(this->get_name().c_str());
  207. this->container->SetTitle(title.c_str());
  208. data_modified = false;
  209. }
  210. return this->container;
  211. }
  212. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  213. util::save_as(get_container(), fname, option);
  214. }
  215. };
  216. template <typename T>
  217. class Vector : public Container<std::vector<T>,T>{
  218. private:
  219. void _fill(){
  220. this->container->push_back(this->value->get_value());
  221. }
  222. public:
  223. Vector(const std::string& name, Value<T>* value)
  224. :Container<std::vector<T>,T>(name, value){
  225. this->container = new std::vector<T>;
  226. }
  227. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  228. std::string type_name = "std::vector<"+fv::util::get_type_name(typeid(T))+">";
  229. util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
  230. }
  231. };
  232. template <typename V, typename D>
  233. class _Counter : public Container<std::map<D,int>,V>{
  234. public:
  235. explicit _Counter(const std::string& name, Value<V>* value)
  236. :Container<std::map<D,int>,V>(name, value) {
  237. this->container = new std::map<D,int>;
  238. }
  239. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  240. std::string type_name = "std::map<"+fv::util::get_type_name(typeid(D))+",int>";
  241. 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 _Counter<V,V>{
  250. using _Counter<V,V>::_Counter;
  251. void _fill(){
  252. (*this->container)[this->value->get_value()]++;
  253. }
  254. };
  255. /**
  256. * Same as Counter but accepts multiple values per fill.
  257. */
  258. template <typename V>
  259. class CounterMany : public _Counter<std::vector<V>,V>{
  260. using _Counter<std::vector<V>,V>::_Counter;
  261. void _fill(){
  262. for(V& val : this->value->get_value())
  263. (*this->container)[val]++;
  264. }
  265. };
  266. }
  267. #endif // root_container_hpp