container.hpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #ifndef root_container_hpp
  2. #define root_container_hpp
  3. #include <utility>
  4. #include <iostream>
  5. #include "TFile.h"
  6. #include "TH1.h"
  7. #include "TH2.h"
  8. #include "TGraph.h"
  9. #include "TROOT.h"
  10. #include "filval.hpp"
  11. namespace fv::root::util{
  12. void _save_img(TObject* container, const std::string& fname){
  13. TCanvas* c1 = new TCanvas("c1");
  14. container->Draw();
  15. c1->Draw();
  16. c1->SaveAs(fname.c_str());
  17. delete c1;
  18. }
  19. void _save_bin(TObject* container){
  20. INFO("Saving object: " << container->GetName() << " into file " << gDirectory->GetName());
  21. /* TFile* f = TFile::Open(fname.c_str(), "UPDATE"); */
  22. container->Write(container->GetName(), TObject::kOverwrite);
  23. /* f->Close(); */
  24. }
  25. void _save_as(TObject* container, const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  26. switch(option){
  27. case PNG:
  28. _save_img(container, fname+".png"); break;
  29. case PDF:
  30. _save_img(container, fname+".pdf"); break;
  31. case ROOT:
  32. _save_bin(container); break;
  33. default:
  34. break;
  35. }
  36. }
  37. }
  38. namespace fv::root {
  39. template <typename V, typename D>
  40. class ContainerTH1 : public Container<TH1>{
  41. private:
  42. void _fill(){
  43. if (container == nullptr){
  44. if (value == nullptr){
  45. CRITICAL("Container: \"" << get_name() << "\" has a null Value object. "
  46. << "Probably built with imcompatible type",-1);
  47. }
  48. init_TH1();
  49. }
  50. _do_fill(value->get_value());
  51. }
  52. protected:
  53. std::string title;
  54. int nbins;
  55. D low;
  56. D high;
  57. Value<V> *value;
  58. virtual void init_TH1() = 0;
  59. virtual void _do_fill(V& val) = 0;
  60. public:
  61. explicit ContainerTH1(const std::string &name, const std::string& title, GenValue *value,
  62. int nbins, D low, D high)
  63. :Container<TH1>(name, nullptr),
  64. title(title), nbins(nbins), low(low), high(high),
  65. value(dynamic_cast<Value<V>*>(value)) { }
  66. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  67. util::_save_as(get_container(), fname, option);
  68. }
  69. };
  70. template <typename V>
  71. class _ContainerTH1D : public ContainerTH1<V, double>{
  72. using ContainerTH1<V, double>::ContainerTH1;
  73. void init_TH1(){
  74. this->container = new TH1D(this->get_name().c_str(), this->title.c_str(),
  75. this->nbins, this->low, this->high);
  76. }
  77. };
  78. class ContainerTH1D : public _ContainerTH1D<double>{
  79. using _ContainerTH1D<double>::_ContainerTH1D;
  80. void _do_fill(double& val){
  81. this->container->Fill(val);
  82. }
  83. };
  84. class ContainerTH1DMany : public _ContainerTH1D<std::vector<double>>{
  85. using _ContainerTH1D<std::vector<double>>::_ContainerTH1D;
  86. void _do_fill(std::vector<double>& val){
  87. for(double x : val)
  88. this->container->Fill(x);
  89. }
  90. };
  91. template <typename V>
  92. class _ContainerTH1F : public ContainerTH1<V, float>{
  93. using ContainerTH1<V,float>::ContainerTH1;
  94. void init_TH1(){
  95. this->container = new TH1F(this->get_name().c_str(), this->title.c_str(),
  96. this->nbins, this->low, this->high);
  97. }
  98. };
  99. class ContainerTH1F : public _ContainerTH1F<float>{
  100. using _ContainerTH1F<float>::_ContainerTH1F;
  101. void _do_fill(float& val){
  102. this->container->Fill(val);
  103. }
  104. };
  105. class ContainerTH1FMany : public _ContainerTH1F<std::vector<float>>{
  106. using _ContainerTH1F<std::vector<float>>::_ContainerTH1F;
  107. void _do_fill(std::vector<float>& val){
  108. for(float x : val)
  109. this->container->Fill(x);
  110. }
  111. };
  112. template <typename V>
  113. class _ContainerTH1I : public ContainerTH1<V, int>{
  114. using ContainerTH1<V,int>::ContainerTH1;
  115. void init_TH1(){
  116. this->container = new TH1I(this->get_name().c_str(), this->title.c_str(),
  117. this->nbins, this->low, this->high);
  118. }
  119. };
  120. class ContainerTH1I : public _ContainerTH1I<int>{
  121. using _ContainerTH1I<int>::_ContainerTH1I;
  122. void _do_fill(int& val){
  123. this->container->Fill(val);
  124. }
  125. };
  126. class ContainerTH1IMany : public _ContainerTH1I<std::vector<int>>{
  127. using _ContainerTH1I<std::vector<int>>::_ContainerTH1I;
  128. void _do_fill(std::vector<int>& val){
  129. for(int x : val)
  130. this->container->Fill(x);
  131. }
  132. };
  133. template <typename V, typename D>
  134. class ContainerTH2 : public Container<TH2>{
  135. private:
  136. void _fill(){
  137. if (container == nullptr){
  138. if (value == nullptr){
  139. CRITICAL("Container: \"" << get_name() << "\" has a null Value object. "
  140. << "Probably built with imcompatible type",-1);
  141. }
  142. init_TH2();
  143. }
  144. _do_fill(value->get_value());
  145. }
  146. protected:
  147. std::string title;
  148. int nbins_x;
  149. int nbins_y;
  150. D low_x;
  151. D low_y;
  152. D high_x;
  153. D high_y;
  154. Value<std::pair<V,V>> *value;
  155. virtual void init_TH2() = 0;
  156. virtual void _do_fill(std::pair<V,V>& val) = 0;
  157. public:
  158. explicit ContainerTH2(const std::string& name, const std::string& title,
  159. GenValue* value,
  160. int nbins_x, D low_x, D high_x,
  161. int nbins_y, D low_y, D high_y)
  162. :Container<TH2>(name, nullptr),
  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. value(dynamic_cast<Value<std::pair<V, V>>*>(value)) { }
  166. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  167. util::_save_as(get_container(), fname, option);
  168. }
  169. };
  170. template <typename V>
  171. class _ContainerTH2D : public ContainerTH2<V, double>{
  172. using ContainerTH2<V, double>::ContainerTH2;
  173. void init_TH2(){
  174. this->container = new TH2D(this->get_name().c_str(), this->title.c_str(),
  175. this->nbins_x, this->low_x, this->high_x,
  176. this->nbins_y, this->low_y, this->high_y);
  177. }
  178. };
  179. class ContainerTH2D : public _ContainerTH2D<double>{
  180. using _ContainerTH2D<double>::_ContainerTH2D;
  181. void _do_fill(std::pair<double,double>& val){
  182. this->container->Fill(val.first, val.second);
  183. }
  184. };
  185. class ContainerTH2DMany : public _ContainerTH2D<std::vector<double>>{
  186. using _ContainerTH2D<std::vector<double>>::_ContainerTH2D;
  187. void _do_fill(std::pair<std::vector<double>,std::vector<double>>& val){
  188. int min_size = std::min(val.first.size(), val.second.size());
  189. for(int i=0; i<min_size; i++)
  190. this->container->Fill(val.first[i],val.second[i]);
  191. }
  192. };
  193. template <typename V>
  194. class _ContainerTH2F : public ContainerTH2<V, float>{
  195. using ContainerTH2<V, float>::ContainerTH2;
  196. void init_TH2(){
  197. this->container = new TH2F(this->get_name().c_str(), this->title.c_str(),
  198. this->nbins_x, this->low_x, this->high_x,
  199. this->nbins_y, this->low_y, this->high_y);
  200. }
  201. };
  202. class ContainerTH2F : public _ContainerTH2F<float>{
  203. using _ContainerTH2F<float>::_ContainerTH2F;
  204. void _do_fill(std::pair<float,float>& val){
  205. this->container->Fill(val.first, val.second);
  206. }
  207. };
  208. class ContainerTH2FMany : public _ContainerTH2F<std::vector<float>>{
  209. using _ContainerTH2F<std::vector<float>>::_ContainerTH2F;
  210. void _do_fill(std::pair<std::vector<float>,std::vector<float>>& val){
  211. int min_size = std::min(val.first.size(), val.second.size());
  212. for(int i=0; i<min_size; i++)
  213. this->container->Fill(val.first[i],val.second[i]);
  214. }
  215. };
  216. template <typename V>
  217. class _ContainerTH2I : public ContainerTH2<V, int>{
  218. using ContainerTH2<V, int>::ContainerTH2;
  219. void init_TH2(){
  220. this->container = new TH2I(this->get_name().c_str(), this->title.c_str(),
  221. this->nbins_x, this->low_x, this->high_x,
  222. this->nbins_y, this->low_y, this->high_y);
  223. }
  224. };
  225. class ContainerTH2I : public _ContainerTH2I<int>{
  226. using _ContainerTH2I<int>::_ContainerTH2I;
  227. void _do_fill(std::pair<int,int>& val){
  228. this->container->Fill(val.first, val.second);
  229. }
  230. };
  231. class ContainerTH2IMany : public _ContainerTH2I<std::vector<int>>{
  232. using _ContainerTH2I<std::vector<int>>::_ContainerTH2I;
  233. void _do_fill(std::pair<std::vector<int>,std::vector<int>>& val){
  234. int min_size = std::min(val.first.size(), val.second.size());
  235. for(int i=0; i<min_size; i++)
  236. this->container->Fill(val.first[i],val.second[i]);
  237. }
  238. };
  239. class ContainerTGraph : public Container<TGraph>{
  240. private:
  241. Value<std::pair<int, int> > *value;
  242. std::vector<int> x_data;
  243. std::vector<int> y_data;
  244. bool data_modified;
  245. void _fill(){
  246. auto val = value->get_value();
  247. x_data.push_back(val.first);
  248. y_data.push_back(val.second);
  249. data_modified = true;
  250. }
  251. public:
  252. ContainerTGraph(const std::string &name, GenValue* value)
  253. :Container<TGraph>(name, new TGraph()),
  254. value(dynamic_cast<Value<std::pair<int, int> >*>(value)),
  255. data_modified(false){ }
  256. TGraph* get_container(){
  257. if (data_modified){
  258. delete container;
  259. container = new TGraph(x_data.size(), x_data.data(), y_data.data());
  260. container->SetName(get_name().c_str());
  261. data_modified = false;
  262. }
  263. return container;
  264. }
  265. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  266. util::_save_as(get_container(), fname, option);
  267. }
  268. };
  269. }
  270. #endif // root_container_hpp