container.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. container->Fill(value->get_value());
  15. }
  16. protected:
  17. Value<T> *value;
  18. ContainerTH1(TH1* container, const std::string &name, Value<T> *value)
  19. :Container<TH1>(container, name),
  20. value(value){ }
  21. };
  22. class ContainerTH1D : public ContainerTH1<double>{
  23. public:
  24. ContainerTH1D(const std::string& name, const std::string& title,
  25. int nbins, double low, double high, GenValue* value)
  26. :ContainerTH1<double>(NULL, name, dynamic_cast<Value<double>*>(value)){
  27. this->container = new TH1D(name.c_str(), title.c_str(), nbins, low, high);
  28. }
  29. };
  30. class ContainerTH1I : public ContainerTH1<int>{
  31. public:
  32. ContainerTH1I(const std::string& name, const std::string& title,
  33. int nbins, int low, int high, GenValue* value)
  34. :ContainerTH1<int>(NULL, name, dynamic_cast<Value<int>*>(value)){
  35. this->container = new TH1I(name.c_str(), title.c_str(), nbins, low, high);
  36. }
  37. };
  38. template <typename T>
  39. class ContainerTH2 : public Container<TH2>{
  40. private:
  41. void _fill(){
  42. std::pair<T, T> val = value->get_value();
  43. container->Fill(val.first, val.second);
  44. }
  45. protected:
  46. Value<std::pair<T, T> > *value;
  47. ContainerTH2(TH2* container, const std::string &name, Value<std::pair<T, T> > *value)
  48. :Container<TH2>(container, name),
  49. value(value){ }
  50. };
  51. class ContainerTH2D : public ContainerTH2<double>{
  52. public:
  53. ContainerTH2D(const std::string& name, const std::string& title,
  54. int nbins_x, double low_x, double high_x,
  55. int nbins_y, double low_y, double high_y,
  56. GenValue* value)
  57. :ContainerTH2<double>(NULL, name, dynamic_cast<Value<std::pair<double, double> >*>(value)){
  58. this->container = new TH2D(name.c_str(), title.c_str(), nbins_x, low_x, high_x, nbins_y, low_y, high_y);
  59. }
  60. };
  61. class ContainerTH2I : public ContainerTH2<int>{
  62. public:
  63. ContainerTH2I(const std::string& name, const std::string& title,
  64. int nbins_x, int low_x, int high_x,
  65. int nbins_y, int low_y, int high_y,
  66. GenValue* value)
  67. :ContainerTH2<int>(NULL, name, dynamic_cast<Value<std::pair<int, int> >*>(value)){
  68. this->container = new TH2I(name.c_str(), title.c_str(), nbins_x, low_x, high_x, nbins_y, low_y, high_y);
  69. }
  70. };
  71. class ContainerTGraph : public Container<TGraph>{
  72. private:
  73. Value<std::pair<int, int> > *value;
  74. std::vector<int> x_data;
  75. std::vector<int> y_data;
  76. bool data_modified;
  77. void _fill(){
  78. auto val = value->get_value();
  79. x_data.push_back(val.first);
  80. y_data.push_back(val.second);
  81. /* std::cout << x_data.size() << std::endl; */
  82. data_modified = true;
  83. }
  84. public:
  85. ContainerTGraph(const std::string &name, GenValue* value)
  86. :Container<TGraph>(new TGraph(), name),
  87. value(dynamic_cast<Value<std::pair<int, int> >*>(value)),
  88. data_modified(false){ }
  89. TGraph* get_container(){
  90. if (data_modified){
  91. delete container;
  92. container = new TGraph(x_data.size(), x_data.data(), y_data.data());
  93. data_modified = false;
  94. }
  95. return container;
  96. }
  97. };
  98. }
  99. #endif // root_container_hpp