container.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. void save_as_stl(void* container, const std::string& type_name,
  50. const std::string& obj_name,
  51. const SaveOption& option = SaveOption::PNG) {
  52. switch(option){
  53. case PNG:
  54. INFO("Cannot save STL container " << type_name <<" as png");
  55. break;
  56. case PDF:
  57. INFO("Cannot save STL container " << type_name <<" as pdf");
  58. break;
  59. case ROOT:
  60. gDirectory->WriteObjectAny(container, type_name.c_str(), obj_name.c_str());
  61. break;
  62. default:
  63. break;
  64. }
  65. }
  66. }
  67. namespace fv::root {
  68. template <typename V>
  69. class _ContainerTH1 : public Container<TH1>{
  70. private:
  71. void _fill(){
  72. if (container == nullptr){
  73. if (value == nullptr){
  74. CRITICAL("Container: \"" << get_name() << "\" has a null Value object. "
  75. << "Probably built with imcompatible type",-1);
  76. }
  77. this->container = new TH1D(this->get_name().c_str(), this->title.c_str(),
  78. this->nbins, this->low, this->high);
  79. }
  80. _do_fill();
  81. }
  82. protected:
  83. std::string title;
  84. int nbins;
  85. double low;
  86. double high;
  87. Value<V> *value;
  88. virtual void _do_fill() = 0;
  89. public:
  90. explicit _ContainerTH1(const std::string &name, const std::string& title, Value<V>* value,
  91. int nbins, double low, double high)
  92. :Container<TH1>(name, nullptr),
  93. title(title), nbins(nbins), low(low), high(high),
  94. value(value) { }
  95. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  96. util::save_as(get_container(), fname, option);
  97. }
  98. };
  99. template <typename V>
  100. class ContainerTH1 : public _ContainerTH1<V>{
  101. using _ContainerTH1<V>::_ContainerTH1;
  102. void _do_fill(){
  103. this->container->Fill(this->value->get_value());
  104. }
  105. };
  106. template <typename V>
  107. class ContainerTH1Many : public _ContainerTH1<std::vector<V>>{
  108. using _ContainerTH1<std::vector<V>>::_ContainerTH1;
  109. void _do_fill(){
  110. for(V x : this->value->get_value())
  111. this->container->Fill(x);
  112. }
  113. };
  114. template <typename V>
  115. class _ContainerTH2 : public Container<TH2>{
  116. private:
  117. void _fill(){
  118. if (container == nullptr){
  119. if (value == nullptr){
  120. CRITICAL("Container: \"" << get_name() << "\" has a null Value object. "
  121. << "Probably built with imcompatible type",-1);
  122. }
  123. this->container = new TH2D(this->get_name().c_str(), this->title.c_str(),
  124. this->nbins_x, this->low_x, this->high_x,
  125. this->nbins_y, this->low_y, this->high_y);
  126. }
  127. _do_fill(value->get_value());
  128. }
  129. protected:
  130. std::string title;
  131. int nbins_x;
  132. int nbins_y;
  133. double low_x;
  134. double low_y;
  135. double high_x;
  136. double high_y;
  137. Value<std::pair<V,V>> *value;
  138. virtual void _do_fill(std::pair<V,V>& val) = 0;
  139. public:
  140. explicit _ContainerTH2(const std::string& name, const std::string& title,
  141. Value<std::pair<V, V>>* value,
  142. int nbins_x, double low_x, double high_x,
  143. int nbins_y, double low_y, double high_y)
  144. :Container<TH2>(name, nullptr),
  145. nbins_x(nbins_x), low_x(low_x), high_x(high_x),
  146. nbins_y(nbins_y), low_y(low_y), high_y(high_y),
  147. value(value) { }
  148. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  149. util::save_as(get_container(), fname, option);
  150. }
  151. };
  152. template <typename V>
  153. class ContainerTH2 : public _ContainerTH2<std::vector<V>>{
  154. using _ContainerTH2<std::vector<V>>::_ContainerTH2;
  155. void _do_fill(std::pair<V,V>& val){
  156. this->container->Fill(val.first,val.second);
  157. }
  158. };
  159. template <typename V>
  160. class ContainerTH2Many : public _ContainerTH2<std::vector<V>>{
  161. using _ContainerTH2<std::vector<V>>::_ContainerTH2;
  162. void _do_fill(std::pair<std::vector<V>,std::vector<V>>& val){
  163. int min_size = std::min(val.first.size(), val.second.size());
  164. for(int i=0; i<min_size; i++)
  165. this->container->Fill(val.first[i],val.second[i]);
  166. }
  167. };
  168. class ContainerTGraph : public Container<TGraph>{
  169. private:
  170. Value<std::pair<int, int> > *value;
  171. std::vector<int> x_data;
  172. std::vector<int> y_data;
  173. std::string title;
  174. bool data_modified;
  175. void _fill(){
  176. auto val = value->get_value();
  177. x_data.push_back(val.first);
  178. y_data.push_back(val.second);
  179. data_modified = true;
  180. }
  181. public:
  182. ContainerTGraph(const std::string& name, const std::string& title, Value<std::pair<int, int>>* value)
  183. :Container<TGraph>(name, new TGraph()),
  184. value(value),
  185. data_modified(false){ }
  186. TGraph* get_container(){
  187. if (data_modified){
  188. delete container;
  189. container = new TGraph(x_data.size(), x_data.data(), y_data.data());
  190. container->SetName(get_name().c_str());
  191. container->SetTitle(title.c_str());
  192. data_modified = false;
  193. }
  194. return container;
  195. }
  196. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  197. util::save_as(get_container(), fname, option);
  198. }
  199. };
  200. template <typename V, typename D>
  201. class _Counter : public Container<std::map<D,int>>{
  202. protected:
  203. Value<V>* value;
  204. public:
  205. explicit _Counter(const std::string& name, Value<V>* value)
  206. :Container<std::map<D,int>>(name, new std::map<D,int>()),
  207. value(value) { }
  208. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  209. std::string type_name = "std::map<"+fv::util::get_type_name(typeid(D))+",int>";
  210. util::save_as_stl(this->get_container(), type_name, this->get_name(), option);
  211. }
  212. };
  213. /**
  214. * A Counter that keeps a mapping of the number of occurances of each input
  215. * value.
  216. */
  217. template <typename V>
  218. class Counter : public _Counter<V,V>{
  219. using _Counter<V,V>::_Counter;
  220. void _fill(){
  221. (*this->container)[this->value->get_value()]++;
  222. }
  223. };
  224. /**
  225. * Same as Counter but accepts multiple values per fill.
  226. */
  227. template <typename V>
  228. class CounterMany : public _Counter<std::vector<V>,V>{
  229. using _Counter<std::vector<V>,V>::_Counter;
  230. void _fill(){
  231. for(V& val : this->value->get_value())
  232. (*this->container)[val]++;
  233. }
  234. };
  235. }
  236. #endif // root_container_hpp