container.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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());
  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 T>
  40. class ContainerTH1 : public Container<TH1>{
  41. private:
  42. void _fill(){
  43. if (container == nullptr){
  44. init_TH1();
  45. }
  46. container->Fill(value->get_value());
  47. }
  48. protected:
  49. std::string title;
  50. int nbins;
  51. T low;
  52. T high;
  53. Value<T> *value;
  54. virtual void init_TH1() = 0;
  55. public:
  56. explicit ContainerTH1(const std::string &name, const std::string& title, GenValue *value,
  57. int nbins, T low, T high)
  58. :Container<TH1>(name, nullptr),
  59. title(title), nbins(nbins), low(low), high(high),
  60. value(dynamic_cast<Value<T>*>(value)) { }
  61. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  62. util::_save_as(container, fname, option);
  63. }
  64. void save(const SaveOption& option = SaveOption::PNG) {
  65. save_as(this->get_name(), option);
  66. }
  67. };
  68. class ContainerTH1D : public ContainerTH1<double>{
  69. using ContainerTH1::ContainerTH1;
  70. void init_TH1(){
  71. this->container = new TH1D(this->get_name().c_str(), title.c_str(), nbins, low, high);
  72. }
  73. };
  74. class ContainerTH1F : public ContainerTH1<float>{
  75. using ContainerTH1::ContainerTH1;
  76. void init_TH1(){
  77. this->container = new TH1F(this->get_name().c_str(), title.c_str(), nbins, low, high);
  78. }
  79. };
  80. class ContainerTH1I : public ContainerTH1<int>{
  81. using ContainerTH1::ContainerTH1;
  82. void init_TH1(){
  83. this->container = new TH1I(this->get_name().c_str(), title.c_str(), nbins, low, high);
  84. }
  85. };
  86. template <typename T>
  87. class ContainerTH2 : public Container<TH2>{
  88. private:
  89. void _fill(){
  90. if (container == nullptr){
  91. init_TH2();
  92. }
  93. std::pair<T, T> val = value->get_value();
  94. container->Fill(val.first, val.second);
  95. }
  96. protected:
  97. std::string title;
  98. int nbins_x;
  99. int nbins_y;
  100. T low_x;
  101. T low_y;
  102. T high_x;
  103. T high_y;
  104. Value<std::pair<T, T> > *value;
  105. virtual void init_TH2() = 0;
  106. public:
  107. explicit ContainerTH2(const std::string& name, const std::string& title,
  108. int nbins_x, double low_x, double high_x,
  109. int nbins_y, double low_y, double high_y,
  110. GenValue* value)
  111. :Container<TH2>(name, nullptr),
  112. nbins_x(nbins_x), low_x(low_x), high_x(high_x),
  113. nbins_y(nbins_y), low_y(low_y), high_y(high_y),
  114. value(dynamic_cast<Value<T>*>(value)) { }
  115. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  116. util::_save_as(container, fname, option);
  117. }
  118. void save(const SaveOption& option = SaveOption::PNG) {
  119. save_as(this->get_name(), option);
  120. }
  121. };
  122. class ContainerTH2D : public ContainerTH2<double>{
  123. using ContainerTH2::ContainerTH2;
  124. void init_TH2(){
  125. this->container = new TH2D(this->get_name().c_str(), title.c_str(), nbins_x, low_x, high_x, nbins_y, low_y, high_y);
  126. }
  127. };
  128. class ContainerTH2F : public ContainerTH2<float>{
  129. using ContainerTH2::ContainerTH2;
  130. void init_TH2(){
  131. this->container = new TH2F(this->get_name().c_str(), title.c_str(), nbins_x, low_x, high_x, nbins_y, low_y, high_y);
  132. }
  133. };
  134. class ContainerTH2I : public ContainerTH2<int>{
  135. using ContainerTH2::ContainerTH2;
  136. void init_TH2(){
  137. this->container = new TH2I(this->get_name().c_str(), title.c_str(), nbins_x, low_x, high_x, nbins_y, low_y, high_y);
  138. }
  139. };
  140. class ContainerTGraph : public Container<TGraph>{
  141. private:
  142. Value<std::pair<int, int> > *value;
  143. std::vector<int> x_data;
  144. std::vector<int> y_data;
  145. bool data_modified;
  146. void _fill(){
  147. auto val = value->get_value();
  148. x_data.push_back(val.first);
  149. y_data.push_back(val.second);
  150. data_modified = true;
  151. }
  152. public:
  153. ContainerTGraph(const std::string &name, GenValue* value)
  154. :Container<TGraph>(name, new TGraph()),
  155. value(dynamic_cast<Value<std::pair<int, int> >*>(value)),
  156. data_modified(false){ }
  157. TGraph* get_container(){
  158. if (data_modified){
  159. delete container;
  160. container = new TGraph(x_data.size(), x_data.data(), y_data.data());
  161. data_modified = false;
  162. }
  163. return container;
  164. }
  165. void save_as(const std::string& fname, const SaveOption& option = SaveOption::PNG) {
  166. util::_save_as(container, fname, option);
  167. }
  168. void save(const SaveOption& option = SaveOption::PNG) {
  169. save_as(this->get_name(), option);
  170. }
  171. };
  172. }
  173. #endif // root_container_hpp