CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TreeReader.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_MVAComputer_TreeReader_h
2 #define PhysicsTools_MVAComputer_TreeReader_h
3 
4 #include <stdint.h>
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  private:
60  TTree *tree;
61 
62  struct Bool {
63  inline Bool() : value(0) {}
64  inline operator Bool_t() const { return value; }
65  Bool_t value;
66  };
67 
68  std::vector<std::pair<void*, std::vector<Double_t> > > multiDouble;
69  std::vector<std::pair<void*, std::vector<Float_t> > > multiFloat;
70  std::vector<std::pair<void*, std::vector<Int_t> > > multiInt;
71  std::vector<std::pair<void*, std::vector<Bool_t> > > multiBool;
72 
73  std::vector<Double_t> singleDouble;
74  std::vector<Float_t> singleFloat;
75  std::vector<Int_t> singleInt;
76  std::vector<Bool> singleBool;
77 
78  class Value {
79  public:
80  Value() {}
81  Value(int index, bool multiple, bool optional, char type) :
82  index(index), optional(optional), multiple(multiple),
83  optVal(TreeReader::kOptVal), type(type), ptr(0) {}
84  ~Value() {}
85 
86  void setOpt(bool opt, double optVal)
87  { this->optional = opt, this->optVal = optVal; }
88  void setBranchName(const TString &name)
89  { this->name = name; }
90  void setPtr(const void *ptr)
91  { this->ptr = ptr; }
92 
93  void update(TreeReader *reader) const;
94  void fill(AtomicId name, TreeReader *reader) const;
95 
96  private:
97  TString name;
98  int index;
99  bool optional;
100  bool multiple;
101  double optVal;
102  char type;
103  const void *ptr;
104  };
105 
106  friend class Value;
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:22
void setBranchName(const TString &name)
Definition: TreeReader.h:88
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
Cheap generic unique keyword identifier class.
Definition: AtomicId.h:32
Value(int index, bool multiple, bool optional, char type)
Definition: TreeReader.h:81
void setPtr(const void *ptr)
Definition: TreeReader.h:90
void addSingle(AtomicId name, const T *value, bool opt=false)
std::vector< Bool > singleBool
Definition: TreeReader.h:76
Main interface class to the generic discriminator computer framework.
Definition: MVAComputer.h:40
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:71
Helper class that can contain an list of identifier-value pairs.
Definition: Variable.h:82
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
void update(TreeReader *reader) const
Definition: TreeReader.cc:352
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:70
std::vector< Double_t > singleDouble
Definition: TreeReader.h:73
void fill(AtomicId name, TreeReader *reader) const
Definition: TreeReader.cc:401
std::vector< Float_t > singleFloat
Definition: TreeReader.h:74
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:86
std::vector< std::pair< void *, std::vector< Double_t > > > multiDouble
Definition: TreeReader.h:68
std::vector< Int_t > singleInt
Definition: TreeReader.h:75
std::vector< std::pair< void *, std::vector< Float_t > > > multiFloat
Definition: TreeReader.h:69