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