container.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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/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, const std::string& fname){
  20. INFO("Saving object: " << container->GetName() << " into file " << fname);
  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, fname+".root"); 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. init_TH1();
  45. }
  46. _do_fill(value->get_value());
  47. }
  48. protected:
  49. std::string title;
  50. int nbins;
  51. D low;
  52. D high;
  53. Value<V> *value;
  54. virtual void init_TH1() = 0;
  55. virtual void _do_fill(V& val) = 0;
  56. public:
  57. explicit ContainerTH1(const std::string &name, const std::string& title, GenValue *value,
  58. int nbins, D low, D high)
  59. :Container<TH1>(name, nullptr),
  60. title(title), nbins(nbins), low(low), high(high),
  61. value(dynamic_cast<Value<V>*>(value)) { }
  62. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  63. util::_save_as(get_container(), fname, option);
  64. }
  65. };
  66. template <typename V>
  67. class _ContainerTH1D : public ContainerTH1<V, double>{
  68. using ContainerTH1<V, double>::ContainerTH1;
  69. void init_TH1(){
  70. this->container = new TH1D(this->get_name().c_str(), this->title.c_str(),
  71. this->nbins, this->low, this->high);
  72. }
  73. };
  74. class ContainerTH1D : public _ContainerTH1D<double>{
  75. using _ContainerTH1D<double>::_ContainerTH1D;
  76. void _do_fill(double& val){
  77. this->container->Fill(val);
  78. }
  79. };
  80. class ContainerTH1DMany : public _ContainerTH1D<std::vector<double>>{
  81. using _ContainerTH1D<std::vector<double>>::_ContainerTH1D;
  82. void _do_fill(std::vector<double>& val){
  83. for(double x : val)
  84. this->container->Fill(x);
  85. }
  86. };
  87. template <typename V>
  88. class _ContainerTH1F : public ContainerTH1<V, float>{
  89. using ContainerTH1<V,float>::ContainerTH1;
  90. void init_TH1(){
  91. this->container = new TH1F(this->get_name().c_str(), this->title.c_str(),
  92. this->nbins, this->low, this->high);
  93. }
  94. };
  95. class ContainerTH1F : public _ContainerTH1F<float>{
  96. using _ContainerTH1F<float>::_ContainerTH1F;
  97. void _do_fill(float& val){
  98. this->container->Fill(val);
  99. }
  100. };
  101. class ContainerTH1FMany : public _ContainerTH1F<std::vector<float>>{
  102. using _ContainerTH1F<std::vector<float>>::_ContainerTH1F;
  103. void _do_fill(std::vector<float>& val){
  104. for(float x : val)
  105. this->container->Fill(x);
  106. }
  107. };
  108. template <typename V>
  109. class _ContainerTH1I : public ContainerTH1<V, int>{
  110. using ContainerTH1<V,int>::ContainerTH1;
  111. void init_TH1(){
  112. this->container = new TH1I(this->get_name().c_str(), this->title.c_str(),
  113. this->nbins, this->low, this->high);
  114. }
  115. };
  116. class ContainerTH1I : public _ContainerTH1I<int>{
  117. using _ContainerTH1I<int>::_ContainerTH1I;
  118. void _do_fill(int& val){
  119. this->container->Fill(val);
  120. }
  121. };
  122. class ContainerTH1IMany : public _ContainerTH1I<std::vector<int>>{
  123. using _ContainerTH1I<std::vector<int>>::_ContainerTH1I;
  124. void _do_fill(std::vector<int>& val){
  125. for(int x : val)
  126. this->container->Fill(x);
  127. }
  128. };
  129. /* template <typename T> */
  130. /* class ContainerTH2 : public Container<TH2>{ */
  131. /* private: */
  132. /* void _fill(){ */
  133. /* if (container == nullptr){ */
  134. /* init_TH2(); */
  135. /* } */
  136. /* std::pair<T, T> val = value->get_value(); */
  137. /* container->Fill(val.first, val.second); */
  138. /* } */
  139. /* protected: */
  140. /* std::string title; */
  141. /* int nbins_x; */
  142. /* int nbins_y; */
  143. /* T low_x; */
  144. /* T low_y; */
  145. /* T high_x; */
  146. /* T high_y; */
  147. /* Value<std::pair<T, T> > *value; */
  148. /* virtual void init_TH2() = 0; */
  149. /* public: */
  150. /* explicit ContainerTH2(const std::string& name, const std::string& title, */
  151. /* int nbins_x, double low_x, double high_x, */
  152. /* int nbins_y, double low_y, double high_y, */
  153. /* GenValue* value) */
  154. /* :Container<TH2>(name, nullptr), */
  155. /* nbins_x(nbins_x), low_x(low_x), high_x(high_x), */
  156. /* nbins_y(nbins_y), low_y(low_y), high_y(high_y), */
  157. /* value(dynamic_cast<Value<T>*>(value)) { } */
  158. /* void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) { */
  159. /* util::_save_as(get_container(), fname, option); */
  160. /* } */
  161. /* void save(const SaveOption& option = SaveOption::PNG) { */
  162. /* save_as(this->get_name(), option); */
  163. /* } */
  164. /* }; */
  165. /* class ContainerTH2D : public ContainerTH2<double>{ */
  166. /* using ContainerTH2::ContainerTH2; */
  167. /* void init_TH2(){ */
  168. /* this->container = new TH2D(this->get_name().c_str(), title.c_str(), nbins_x, low_x, high_x, nbins_y, low_y, high_y); */
  169. /* } */
  170. /* }; */
  171. /* class ContainerTH2F : public ContainerTH2<float>{ */
  172. /* using ContainerTH2::ContainerTH2; */
  173. /* void init_TH2(){ */
  174. /* this->container = new TH2F(this->get_name().c_str(), title.c_str(), nbins_x, low_x, high_x, nbins_y, low_y, high_y); */
  175. /* } */
  176. /* }; */
  177. /* class ContainerTH2I : public ContainerTH2<int>{ */
  178. /* using ContainerTH2::ContainerTH2; */
  179. /* void init_TH2(){ */
  180. /* this->container = new TH2I(this->get_name().c_str(), title.c_str(), nbins_x, low_x, high_x, nbins_y, low_y, high_y); */
  181. /* } */
  182. /* }; */
  183. class ContainerTGraph : public Container<TGraph>{
  184. private:
  185. Value<std::pair<int, int> > *value;
  186. std::vector<int> x_data;
  187. std::vector<int> y_data;
  188. bool data_modified;
  189. void _fill(){
  190. auto val = value->get_value();
  191. x_data.push_back(val.first);
  192. y_data.push_back(val.second);
  193. data_modified = true;
  194. }
  195. public:
  196. ContainerTGraph(const std::string &name, GenValue* value)
  197. :Container<TGraph>(name, new TGraph()),
  198. value(dynamic_cast<Value<std::pair<int, int> >*>(value)),
  199. data_modified(false){ }
  200. TGraph* get_container(){
  201. if (data_modified){
  202. delete container;
  203. container = new TGraph(x_data.size(), x_data.data(), y_data.data());
  204. container->SetName(get_name().c_str());
  205. std::cout << "name: " << container->GetName() << std::endl;
  206. data_modified = false;
  207. }
  208. return container;
  209. }
  210. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  211. util::_save_as(get_container(), fname, option);
  212. }
  213. };
  214. }
  215. #endif // root_container_hpp