root_value.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #ifndef root_value_hpp
  2. #define root_value_hpp
  3. #include "filval/value.hpp"
  4. #include "TLorentzVector.h"
  5. namespace fv::root{
  6. class LorentzVectors : public DerivedValue<std::vector<TLorentzVector>>{
  7. protected:
  8. Value<std::vector<float>> *pt_val;
  9. Value<std::vector<float>> *eta_val;
  10. Value<std::vector<float>> *phi_val;
  11. Value<std::vector<float>> *mass_val;
  12. void update_value(){
  13. auto pt = pt_val->get_value();
  14. auto eta = eta_val->get_value();
  15. auto phi = phi_val->get_value();
  16. auto mass = mass_val->get_value();
  17. std::vector<int> sizes = {pt.size(), eta.size(), phi.size(), mass.size()};
  18. int size = *std::min_element(sizes.begin(), sizes.end());
  19. this->value.clear();
  20. TLorentzVector lv;
  21. for (int i =0; i<size; i++){
  22. lv.SetPtEtaPhiM(pt[i], eta[i], phi[i], mass[i]);
  23. this->value.push_back(lv);
  24. }
  25. }
  26. public:
  27. static std::string fmt_name(Value<std::vector<float>>* pt, Value<std::vector<float>>* eta,
  28. Value<std::vector<float>>* phi, Value<std::vector<float>>* mass){
  29. return "lorentz_vectors("+pt->get_name()+"," +eta->get_name()+","+
  30. phi->get_name()+"," +mass->get_name()+")";
  31. }
  32. LorentzVectors(Value<std::vector<float>>* pt,
  33. Value<std::vector<float>>* eta,
  34. Value<std::vector<float>>* phi,
  35. Value<std::vector<float>>* mass,
  36. const std::string& alias)
  37. :DerivedValue<std::vector<TLorentzVector>>(fmt_name(pt,eta,phi,mass), alias),
  38. pt_val(pt), eta_val(eta), phi_val(phi), mass_val(mass) { }
  39. };
  40. class Energies : public DerivedValue<std::vector<float>>{
  41. private:
  42. Value<std::vector<TLorentzVector>> *vectors;
  43. void update_value(){
  44. std::vector<TLorentzVector>& vecs = vectors->get_value();
  45. this->value.clear();
  46. for (auto v : vecs){
  47. this->value.push_back(v.Energy());
  48. }
  49. }
  50. public:
  51. static std::string fmt_name(Value<std::vector<TLorentzVector>> *vectors){
  52. return "energies("+vectors->get_name()+")";
  53. }
  54. Energies(Value<std::vector<TLorentzVector>> *vectors,
  55. const std::string& alias)
  56. :DerivedValue<std::vector<float>>(fmt_name(vectors), alias),
  57. vectors(vectors) { }
  58. };
  59. namespace detail {
  60. template<typename Tuple, std::size_t... Is>
  61. decltype(auto) tuple_get_values_impl(const Tuple& t, std::index_sequence<Is...>){
  62. return std::make_tuple(std::get<1>(std::get<Is>(t))->get_value()...);
  63. }
  64. template<typename Tuple, std::size_t... Is>
  65. decltype(auto) tuple_get_labels_impl(const Tuple& t, std::index_sequence<Is...>){
  66. return std::make_tuple(std::get<0>(std::get<Is>(t))...);
  67. }
  68. }
  69. /**
  70. * Converts a tuple of pairs of label/Value objects to a tuple of the contained values
  71. */
  72. template<typename... ArgTypes>
  73. std::tuple<ArgTypes...> tuple_get_values(const std::tuple<std::pair<std::string,Value<ArgTypes>*>...>& t){
  74. return detail::tuple_get_values_impl<std::tuple<std::pair<std::string,Value<ArgTypes>*>...>>(t, std::index_sequence_for<ArgTypes...>{});
  75. }
  76. /**
  77. * Converts a tuple of pairs of label/Value objects to a tuple of just the
  78. * labels
  79. */
  80. template<typename... ArgTypes>
  81. decltype(auto) tuple_get_labels(const std::tuple<std::pair<std::string,Value<ArgTypes>*>...>& t){
  82. return detail::tuple_get_labels_impl<std::tuple<std::pair<std::string,Value<ArgTypes>*>...>>(t, std::index_sequence_for<ArgTypes...>{});
  83. }
  84. //-----------------------
  85. template <typename T>
  86. char type_lookup(){
  87. return 0;
  88. }
  89. template <>
  90. char type_lookup<int>(){
  91. return 'I';
  92. }
  93. template <>
  94. char type_lookup<float>(){
  95. return 'F';
  96. }
  97. template <>
  98. char type_lookup<double>(){
  99. return 'F';
  100. }
  101. template<typename... DataTypes>
  102. class MVAData : public DerivedValue<std::tuple<bool,bool,double,std::tuple<DataTypes...>>>{
  103. private:
  104. std::tuple<std::pair<std::string,Value<DataTypes>*>...> data;
  105. Value<bool>* is_training;
  106. Value<bool>* is_signal;
  107. Value<double>* weight;
  108. void update_value(){
  109. this->value = std::make_tuple(is_training->get_value(), is_signal->get_value(), weight->get_value(),
  110. tuple_get_values(data));
  111. }
  112. template <typename... Types>
  113. static std::vector<std::pair<std::string, char>> get_label_types_impl(std::pair<std::string,Value<Types>*>... pairs){
  114. return std::vector<std::pair<std::string, char>>({ std::make_pair(pairs.first,type_lookup<Types>())...});
  115. }
  116. public:
  117. typedef std::tuple<bool, bool, double, std::tuple<DataTypes...>> type;
  118. std::vector<std::pair<std::string, char>> get_label_types(){
  119. return call(get_label_types_impl<DataTypes...>, data);
  120. }
  121. static std::string fmt_name(Value<bool>* is_training, Value<bool>* is_signal, Value<double>* weight,
  122. const std::pair<std::string, Value<DataTypes>*>&&... data_vals){
  123. //TODO: add data names
  124. return "mva_data("+is_training->get_name()+","
  125. +is_signal->get_name()+","
  126. +weight->get_name()+")";
  127. }
  128. MVAData(Value<bool>* is_training, Value<bool>* is_signal,
  129. Value<double>* weight, const std::pair<std::string, Value<DataTypes>*>&&... data_vals)
  130. :DerivedValue<type>(fmt_name(is_training, is_signal, weight,
  131. std::forward<const std::pair<std::string,Value<DataTypes>*>>(data_vals)...),""),
  132. data(std::make_tuple(data_vals...)),
  133. is_training(is_training),
  134. is_signal(is_signal),
  135. weight(weight) { }
  136. };
  137. }
  138. #endif // root_value_hpp