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