CMS 3D CMS Logo

TreeReader.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_MVAComputer_TreeReader_h
2 #define PhysicsTools_MVAComputer_TreeReader_h
3 
4 #include <cstdint>
5 #include <utility>
6 #include <string>
7 #include <vector>
8 #include <map>
9 
10 #include <TTree.h>
11 #include <TBranch.h>
12 
16 
17 namespace PhysicsTools {
18 
19  class TreeReader {
20  public:
21  TreeReader();
22  TreeReader(const TreeReader &orig);
23  TreeReader(TTree *tree, bool skipTarget = false, bool skipWeight = false);
24  virtual ~TreeReader();
25 
26  TreeReader &operator=(const TreeReader &orig);
27 
28  void setTree(TTree *tree);
29 
30  void addBranch(const std::string &expression, AtomicId name = AtomicId(), bool opt = true);
31  void addBranch(TBranch *branch, AtomicId name = AtomicId(), bool opt = true);
32  template <typename T>
33  void addSingle(AtomicId name, const T *value, bool opt = false);
34  template <typename T>
35  void addMulti(AtomicId name, const std::vector<T> *value);
36  void setOptional(AtomicId name, bool opt, double optVal = kOptVal);
37 
38  void addTypeSingle(AtomicId name, const void *value, char type, bool opt);
39  void addTypeMulti(AtomicId name, const void *value, char type);
40 
41  void automaticAdd(bool skipTarget = false, bool skipWeight = false);
42 
43  void reset();
44  void update();
45 
46  uint64_t loop(const MVAComputer *mva);
47 
48  double fill(const MVAComputer *mva);
49 
51 
52  std::vector<AtomicId> variables() const;
53 
54  static const double kOptVal;
55 
56  struct Bool {
57  inline Bool() : value(false) {}
58  inline operator Bool_t() const { return value; }
59  Bool_t value;
60  };
61 
62  class Value {
63  public:
64  Value() {}
65  Value(int index, bool multiple, bool optional, char type)
66  : index(index),
70  type(type),
71  ptr(nullptr) {}
72  ~Value() {}
73 
74  void setOpt(bool opt, double optVal) { this->optional = opt, this->optVal = optVal; }
75  void setBranchName(const TString &name) { this->name = name; }
76  void setPtr(const void *ptr) { this->ptr = ptr; }
77 
78  void update(TreeReader *reader) const;
79  void fill(AtomicId name, TreeReader *reader) const;
80 
81  private:
82  TString name;
83  int index;
84  bool optional;
85  bool multiple;
86  double optVal;
87  char type;
88  const void *ptr;
89  };
90 
91  friend class Value;
92 
93  private:
94  TTree *tree;
95 
96  std::vector<std::pair<void *, std::vector<Double_t> > > multiDouble;
97  std::vector<std::pair<void *, std::vector<Float_t> > > multiFloat;
98  std::vector<std::pair<void *, std::vector<Int_t> > > multiInt;
99  std::vector<std::pair<void *, std::vector<Bool_t> > > multiBool;
100 
101  std::vector<Double_t> singleDouble;
102  std::vector<Float_t> singleFloat;
103  std::vector<Int_t> singleInt;
104  std::vector<Bool> singleBool;
105 
106  std::map<AtomicId, Value> valueMap;
108  bool upToDate;
109  };
110 
111 #define TREEREADER_ADD_IMPL(T) \
112  template <> \
113  void TreeReader::addSingle<T>(AtomicId name, const T *value, bool opt); \
114  \
115  template <> \
116  void TreeReader::addMulti(AtomicId name, const std::vector<T> *value);
117 
118  TREEREADER_ADD_IMPL(Double_t)
119  TREEREADER_ADD_IMPL(Float_t)
120  TREEREADER_ADD_IMPL(Int_t)
121  TREEREADER_ADD_IMPL(Bool_t)
122 
123 #undef TREEREADER_ADD_IMPL
124 
125 } // namespace PhysicsTools
126 
127 #endif // PhysicsTools_MVAComputer_TreeReader_h
void setBranchName(const TString &name)
Definition: TreeReader.h:75
void addMulti(AtomicId name, const std::vector< T > *value)
Variable::ValueList fill()
Definition: TreeReader.cc:286
#define TREEREADER_ADD_IMPL(T)
Definition: TreeReader.h:111
Variable::ValueList values
Definition: TreeReader.h:107
reader
Definition: DQM.py:105
void update(TreeReader *reader) const
Definition: TreeReader.cc:304
void setTree(TTree *tree)
Definition: TreeReader.cc:55
Cheap generic unique keyword identifier class.
Definition: AtomicId.h:31
Value(int index, bool multiple, bool optional, char type)
Definition: TreeReader.h:65
void setPtr(const void *ptr)
Definition: TreeReader.h:76
void addSingle(AtomicId name, const T *value, bool opt=false)
std::vector< std::pair< void *, std::vector< Float_t > > > multiFloat
Definition: TreeReader.h:97
std::vector< Bool > singleBool
Definition: TreeReader.h:104
Main interface class to the generic discriminator computer framework.
Definition: MVAComputer.h:39
std::vector< std::pair< void *, std::vector< Int_t > > > multiInt
Definition: TreeReader.h:98
std::vector< std::pair< void *, std::vector< Double_t > > > multiDouble
Definition: TreeReader.h:96
void automaticAdd(bool skipTarget=false, bool skipWeight=false)
Definition: TreeReader.cc:213
Definition: value.py:1
Helper class that can contain an list of identifier-value pairs.
Definition: Variable.h:77
void setOptional(AtomicId name, bool opt, double optVal=kOptVal)
Definition: TreeReader.cc:117
void addTypeSingle(AtomicId name, const void *value, char type, bool opt)
Definition: TreeReader.cc:128
void fill(AtomicId name, TreeReader *reader) const
Definition: TreeReader.cc:348
uint64_t loop(const MVAComputer *mva)
Definition: TreeReader.cc:260
static const double kOptVal
Definition: TreeReader.h:54
unsigned long long uint64_t
Definition: Time.h:13
void addBranch(const std::string &expression, AtomicId name=AtomicId(), bool opt=true)
Definition: TreeReader.cc:60
std::map< AtomicId, Value > valueMap
Definition: TreeReader.h:106
std::vector< std::pair< void *, std::vector< Bool_t > > > multiBool
Definition: TreeReader.h:99
std::vector< Double_t > singleDouble
Definition: TreeReader.h:101
std::vector< Float_t > singleFloat
Definition: TreeReader.h:102
Definition: tree.py:1
TreeReader & operator=(const TreeReader &orig)
Definition: TreeReader.cc:35
void addTypeMulti(AtomicId name, const void *value, char type)
Definition: TreeReader.cc:173
void setOpt(bool opt, double optVal)
Definition: TreeReader.h:74
long double T
std::vector< Int_t > singleInt
Definition: TreeReader.h:103
std::vector< AtomicId > variables() const
Definition: TreeReader.cc:296