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
PhysicsTools::TreeReader::addBranch
void addBranch(const std::string &expression, AtomicId name=AtomicId(), bool opt=true)
Definition: TreeReader.cc:60
TREEREADER_ADD_IMPL
#define TREEREADER_ADD_IMPL(T)
Definition: TreeReader.h:111
PhysicsTools::AtomicId
Cheap generic unique keyword identifier class.
Definition: AtomicId.h:31
PhysicsTools::TreeReader::addMulti
void addMulti(AtomicId name, const std::vector< T > *value)
PhysicsTools::TreeReader::reset
void reset()
Definition: TreeReader.cc:234
PhysicsTools::TreeReader::TreeReader
TreeReader()
Definition: TreeReader.cc:25
funct::false
false
Definition: Factorize.h:34
PhysicsTools::TreeReader::singleFloat
std::vector< Float_t > singleFloat
Definition: TreeReader.h:102
PhysicsTools::TreeReader::~TreeReader
virtual ~TreeReader()
Definition: TreeReader.cc:33
PhysicsTools::TreeReader::Bool::Bool
Bool()
Definition: TreeReader.h:57
PhysicsTools::TreeReader::multiDouble
std::vector< std::pair< void *, std::vector< Double_t > > > multiDouble
Definition: TreeReader.h:96
PhysicsTools::TreeReader::Value::~Value
~Value()
Definition: TreeReader.h:72
MicroEventContent_cff.branch
branch
Definition: MicroEventContent_cff.py:152
tree
Definition: tree.py:1
PhysicsTools::TreeReader::fill
Variable::ValueList fill()
Definition: TreeReader.cc:286
PhysicsTools::TreeReader::variables
std::vector< AtomicId > variables() const
Definition: TreeReader.cc:296
PhysicsTools::TreeReader::Value::multiple
bool multiple
Definition: TreeReader.h:85
PhysicsTools::TreeReader::addSingle
void addSingle(AtomicId name, const T *value, bool opt=false)
beam_dqm_sourceclient-live_cfg.mva
mva
Definition: beam_dqm_sourceclient-live_cfg.py:119
PhysicsTools::TreeReader::Value::index
int index
Definition: TreeReader.h:83
runTheMatrix.opt
opt
Definition: runTheMatrix.py:280
PhysicsTools::MVAComputer
Main interface class to the generic discriminator computer framework.
Definition: MVAComputer.h:39
PhysicsTools::Variable::ValueList
Helper class that can contain an list of identifier-value pairs.
Definition: Variable.h:77
PhysicsTools::TreeReader::Value::Value
Value(int index, bool multiple, bool optional, char type)
Definition: TreeReader.h:65
PhysicsTools::TreeReader::values
Variable::ValueList values
Definition: TreeReader.h:107
PhysicsTools::TreeReader::multiFloat
std::vector< std::pair< void *, std::vector< Float_t > > > multiFloat
Definition: TreeReader.h:97
PhysicsTools::TreeReader::Bool
Definition: TreeReader.h:56
PhysicsTools::TreeReader::Value::optional
bool optional
Definition: TreeReader.h:84
PhysicsTools::TreeReader::Bool::value
Bool_t value
Definition: TreeReader.h:59
DQM.reader
reader
Definition: DQM.py:105
PhysicsTools::TreeReader::Value::update
void update(TreeReader *reader) const
Definition: TreeReader.cc:304
AtomicId.h
PhysicsTools::TreeReader::Value::setPtr
void setPtr(const void *ptr)
Definition: TreeReader.h:76
PhysicsTools::TreeReader::multiInt
std::vector< std::pair< void *, std::vector< Int_t > > > multiInt
Definition: TreeReader.h:98
MVAComputer.h
PhysicsTools::TreeReader::Value::fill
void fill(AtomicId name, TreeReader *reader) const
Definition: TreeReader.cc:348
PhysicsTools::TreeReader::upToDate
bool upToDate
Definition: TreeReader.h:108
PhysicsTools::TreeReader::singleBool
std::vector< Bool > singleBool
Definition: TreeReader.h:104
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
PhysicsTools
Definition: Histogram.h:13
PhysicsTools::TreeReader::Value::Value
Value()
Definition: TreeReader.h:64
PhysicsTools::TreeReader::addTypeSingle
void addTypeSingle(AtomicId name, const void *value, char type, bool opt)
Definition: TreeReader.cc:128
PhysicsTools::TreeReader::Value
Definition: TreeReader.h:62
value
Definition: value.py:1
PhysicsTools::TreeReader::update
void update()
Definition: TreeReader.cc:250
PhysicsTools::TreeReader::addTypeMulti
void addTypeMulti(AtomicId name, const void *value, char type)
Definition: TreeReader.cc:173
PhysicsTools::TreeReader::Value::type
char type
Definition: TreeReader.h:87
PhysicsTools::TreeReader::Value::name
TString name
Definition: TreeReader.h:82
PhysicsTools::TreeReader::setOptional
void setOptional(AtomicId name, bool opt, double optVal=kOptVal)
Definition: TreeReader.cc:117
type
type
Definition: HCALResponse.h:21
PhysicsTools::TreeReader::automaticAdd
void automaticAdd(bool skipTarget=false, bool skipWeight=false)
Definition: TreeReader.cc:213
PhysicsTools::TreeReader::Value::setOpt
void setOpt(bool opt, double optVal)
Definition: TreeReader.h:74
PhysicsTools::TreeReader::Value::optVal
double optVal
Definition: TreeReader.h:86
T
long double T
Definition: Basic3DVectorLD.h:48
PhysicsTools::TreeReader::singleInt
std::vector< Int_t > singleInt
Definition: TreeReader.h:103
PhysicsTools::TreeReader::operator=
TreeReader & operator=(const TreeReader &orig)
Definition: TreeReader.cc:35
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
PhysicsTools::TreeReader::multiBool
std::vector< std::pair< void *, std::vector< Bool_t > > > multiBool
Definition: TreeReader.h:99
cond::uint64_t
unsigned long long uint64_t
Definition: Time.h:13
PhysicsTools::TreeReader::kOptVal
static const double kOptVal
Definition: TreeReader.h:54
PhysicsTools::TreeReader
Definition: TreeReader.h:19
Variable.h
PhysicsTools::TreeReader::setTree
void setTree(TTree *tree)
Definition: TreeReader.cc:55
PhysicsTools::TreeReader::valueMap
std::map< AtomicId, Value > valueMap
Definition: TreeReader.h:106
PhysicsTools::TreeReader::Value::setBranchName
void setBranchName(const TString &name)
Definition: TreeReader.h:75
PhysicsTools::TreeReader::Value::ptr
const void * ptr
Definition: TreeReader.h:88
PhysicsTools::TreeReader::tree
TTree * tree
Definition: TreeReader.h:94
PhysicsTools::TreeReader::loop
uint64_t loop(const MVAComputer *mva)
Definition: TreeReader.cc:260
PhysicsTools::TreeReader::singleDouble
std::vector< Double_t > singleDouble
Definition: TreeReader.h:101