container.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #ifndef root_container_hpp
  2. #define root_container_hpp
  3. #include <utility>
  4. #include "../filval/filval.hpp"
  5. #include "TH1.h"
  6. #include "TH2.h"
  7. #include "TGraph.h"
  8. #include <iostream>
  9. namespace filval::root {
  10. template <typename T>
  11. class ContainerTH1 : public Container<TH1>{
  12. private:
  13. void _fill(){
  14. if (container == NULL){
  15. init_TH1();
  16. }
  17. container->Fill(value->get_value());
  18. }
  19. protected:
  20. std::string title;
  21. int nbins;
  22. T low;
  23. T high;
  24. Value<T> *value;
  25. virtual void init_TH1() = 0;
  26. public:
  27. explicit ContainerTH1(const std::string &name, const std::string& title, GenValue *value,
  28. int nbins, T low, T high)
  29. :Container<TH1>(name, NULL),
  30. title(title), nbins(nbins), low(low), high(high),
  31. value(dynamic_cast<Value<T>*>(value)) { }
  32. };
  33. class ContainerTH1D : public ContainerTH1<double>{
  34. using ContainerTH1::ContainerTH1;
  35. void init_TH1(){
  36. this->container = new TH1D(this->get_name().c_str(), title.c_str(), nbins, low, high);
  37. }
  38. };
  39. class ContainerTH1F : public ContainerTH1<float>{
  40. using ContainerTH1::ContainerTH1;
  41. void init_TH1(){
  42. this->container = new TH1F(this->get_name().c_str(), title.c_str(), nbins, low, high);
  43. }
  44. };
  45. class ContainerTH1I : public ContainerTH1<int>{
  46. using ContainerTH1::ContainerTH1;
  47. void init_TH1(){
  48. this->container = new TH1I(this->get_name().c_str(), title.c_str(), nbins, low, high);
  49. }
  50. };
  51. template <typename T>
  52. class ContainerTH2 : public Container<TH2>{
  53. private:
  54. void _fill(){
  55. if (container == NULL){
  56. init_TH2();
  57. }
  58. std::pair<T, T> val = value->get_value();
  59. container->Fill(val.first, val.second);
  60. }
  61. protected:
  62. std::string title;
  63. int nbins_x;
  64. int nbins_y;
  65. T low_x;
  66. T low_y;
  67. T high_x;
  68. T high_y;
  69. Value<std::pair<T, T> > *value;
  70. virtual void init_TH2() = 0;
  71. public:
  72. explicit ContainerTH2(const std::string& name, const std::string& title,
  73. int nbins_x, double low_x, double high_x,
  74. int nbins_y, double low_y, double high_y,
  75. GenValue* value)
  76. :Container<TH2>(name, NULL),
  77. nbins_x(nbins_x), low_x(low_x), high_x(high_x),
  78. nbins_y(nbins_y), low_y(low_y), high_y(high_y),
  79. value(dynamic_cast<Value<T>*>(value)) { }
  80. };
  81. class ContainerTH2D : public ContainerTH2<double>{
  82. using ContainerTH2::ContainerTH2;
  83. void init_TH2(){
  84. this->container = new TH2D(this->get_name().c_str(), title.c_str(), nbins_x, low_x, high_x, nbins_y, low_y, high_y);
  85. }
  86. };
  87. class ContainerTH2F : public ContainerTH2<float>{
  88. using ContainerTH2::ContainerTH2;
  89. void init_TH2(){
  90. this->container = new TH2F(this->get_name().c_str(), title.c_str(), nbins_x, low_x, high_x, nbins_y, low_y, high_y);
  91. }
  92. };
  93. class ContainerTH2I : public ContainerTH2<int>{
  94. using ContainerTH2::ContainerTH2;
  95. void init_TH2(){
  96. this->container = new TH2I(this->get_name().c_str(), title.c_str(), nbins_x, low_x, high_x, nbins_y, low_y, high_y);
  97. }
  98. };
  99. class ContainerTGraph : public Container<TGraph>{
  100. private:
  101. Value<std::pair<int, int> > *value;
  102. std::vector<int> x_data;
  103. std::vector<int> y_data;
  104. bool data_modified;
  105. void _fill(){
  106. auto val = value->get_value();
  107. x_data.push_back(val.first);
  108. y_data.push_back(val.second);
  109. data_modified = true;
  110. }
  111. public:
  112. ContainerTGraph(const std::string &name, GenValue* value)
  113. :Container<TGraph>(name, new TGraph()),
  114. value(dynamic_cast<Value<std::pair<int, int> >*>(value)),
  115. data_modified(false){ }
  116. TGraph* get_container(){
  117. if (data_modified){
  118. delete container;
  119. container = new TGraph(x_data.size(), x_data.data(), y_data.data());
  120. data_modified = false;
  121. }
  122. return container;
  123. }
  124. };
  125. }
  126. #endif // root_container_hpp