KaliVeda
Toolkit for HIC analysis
KVTreeVectorBranchHandler.h
1 #pragma once
2 
10 #include <vector>
11 #include <unordered_map>
12 #include "TTree.h"
13 
15 {
16  template<typename U>
17  class my_vector
18  {
19  std::vector<U> _data;
20  public:
21  my_vector& operator=(const my_vector&) = delete;
22  my_vector& operator=(my_vector&&) = delete;
23  void operator=(U u)
24  {
25  _data.push_back(u);
26  }
27  std::vector<U>& get_vec_ref()
28  {
29  return _data;
30  }
31  };
32  template<typename T>
33  using particle_vector = std::unordered_map<std::string,my_vector<T>>;
34  template<typename T>
35  std::reference_wrapper<particle_vector<T>> get_vectors()
36  {
37  return std::ref(particle_vector<T>{});
38  }
39  template<typename T>
40  void clear_all_vectors()
41  {
42  for(auto &v : get_vectors<T>().get())
43  {
44  v.second.get_vec_ref().clear();
45  }
46  }
47  particle_vector<int> int_vectors;
48  particle_vector<float> float_vectors;
49  particle_vector<double> double_vectors;
50 
51  public:
52 
66  template<typename VarType>
67  void AddVectorBranch(TTree* t, const std::string& var)
68  {
69  t->Branch(var.c_str(), &get_vectors<VarType>().get()[var].get_vec_ref());
70  }
71 
76  {
77  clear_all_vectors<int>();
78  clear_all_vectors<double>();
79  clear_all_vectors<float>();
80  }
95  template<typename VarType>
96  void FillVectorBranch(const std::string& var, VarType val)
97  {
98  get_vectors<VarType>().get()[var] = val;
99  }
100 };
101 
102 template<>
103 std::reference_wrapper<KVTreeVectorBranchHandler::particle_vector<int>>
104 inline KVTreeVectorBranchHandler::get_vectors<int>()
105 {
106  return int_vectors;
107 }
108 template<>
109 std::reference_wrapper<KVTreeVectorBranchHandler::particle_vector<double>>
110 inline KVTreeVectorBranchHandler::get_vectors<double>()
111 {
112  return double_vectors;
113 }
114 template<>
115 std::reference_wrapper<KVTreeVectorBranchHandler::particle_vector<float>>
116 inline KVTreeVectorBranchHandler::get_vectors<float>()
117 {
118  return float_vectors;
119 }
Binding & operator=(OUT(*fun)(U0 u0))
Facilitate use of std::vector branches in data analysis.
void FillVectorBranch(const std::string &var, VarType val)
void AddVectorBranch(TTree *t, const std::string &var)
virtual Int_t Branch(const char *folder, Int_t bufsize=32000, Int_t splitlevel=99)
v