CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_9_patch3/src/PhysicsTools/MVAComputer/interface/TreeReader.h

Go to the documentation of this file.
00001 #ifndef PhysicsTools_MVAComputer_TreeReader_h
00002 #define PhysicsTools_MVAComputer_TreeReader_h
00003 
00004 #include <stdint.h>
00005 #include <utility>
00006 #include <string>
00007 #include <vector>
00008 #include <map>
00009 
00010 #include <TTree.h>
00011 #include <TBranch.h>
00012 
00013 #include "PhysicsTools/MVAComputer/interface/AtomicId.h"
00014 #include "PhysicsTools/MVAComputer/interface/MVAComputer.h"
00015 #include "PhysicsTools/MVAComputer/interface/Variable.h"
00016 
00017 namespace PhysicsTools {
00018 
00019 class TreeReader {
00020     public:
00021         TreeReader();
00022         TreeReader(const TreeReader &orig);
00023         TreeReader(TTree *tree, bool skipTarget = false,
00024                    bool skipWeight = false);
00025         virtual ~TreeReader();
00026 
00027         TreeReader &operator = (const TreeReader &orig);
00028 
00029         void setTree(TTree *tree);
00030 
00031         void addBranch(const std::string &expression,
00032                        AtomicId name = AtomicId(), bool opt = true);
00033         void addBranch(TBranch *branch,
00034                        AtomicId name = AtomicId(), bool opt = true);
00035         template<typename T>
00036         void addSingle(AtomicId name, const T *value, bool opt = false);
00037         template<typename T>
00038         void addMulti(AtomicId name, const std::vector<T> *value);
00039         void setOptional(AtomicId name, bool opt, double optVal = kOptVal);
00040 
00041         void addTypeSingle(AtomicId name, const void *value, char type, bool opt);
00042         void addTypeMulti(AtomicId name, const void *value, char type);
00043 
00044         void automaticAdd(bool skipTarget = false, bool skipWeight = false);
00045 
00046         void reset();
00047         void update();
00048 
00049         uint64_t loop(const MVAComputer *mva);
00050 
00051         double fill(const MVAComputer *mva);
00052 
00053         Variable::ValueList fill();
00054 
00055         std::vector<AtomicId> variables() const;
00056 
00057         static const double     kOptVal;
00058 
00059     private:
00060         TTree                           *tree;
00061 
00062         struct Bool {
00063                 inline Bool() : value(0) {}
00064                 inline operator Bool_t() const { return value; }
00065                 Bool_t  value;
00066         };
00067 
00068         std::vector<std::pair<void*, std::vector<Double_t> > >  multiDouble;
00069         std::vector<std::pair<void*, std::vector<Float_t> > >   multiFloat;
00070         std::vector<std::pair<void*, std::vector<Int_t> > >     multiInt;
00071         std::vector<std::pair<void*, std::vector<Bool_t> > >    multiBool;
00072 
00073         std::vector<Double_t>           singleDouble;
00074         std::vector<Float_t>            singleFloat;
00075         std::vector<Int_t>              singleInt;
00076         std::vector<Bool>               singleBool;
00077 
00078         class Value {
00079             public:
00080                 Value() {}
00081                 Value(int index, bool multiple, bool optional, char type) :
00082                         index(index), optional(optional), multiple(multiple),
00083                         optVal(TreeReader::kOptVal), type(type), ptr(0) {}
00084                 ~Value() {}
00085 
00086                 void setOpt(bool opt, double optVal)
00087                 { this->optional = opt, this->optVal = optVal; }
00088                 void setBranchName(const TString &name)
00089                 { this->name = name; }
00090                 void setPtr(const void *ptr)
00091                 { this->ptr = ptr; }
00092 
00093                 void update(TreeReader *reader) const;
00094                 void fill(AtomicId name, TreeReader *reader) const;
00095 
00096             private:
00097                 TString         name;
00098                 int             index;
00099                 bool            optional;
00100                 bool            multiple;
00101                 double          optVal;
00102                 char            type;
00103                 const void      *ptr;
00104         };
00105 
00106         friend class Value;
00107 
00108         std::map<AtomicId, Value>       valueMap;
00109         Variable::ValueList             values;
00110         bool                            upToDate;
00111 };
00112 
00113 #define TREEREADER_ADD_IMPL(T) \
00114 template<> \
00115 void TreeReader::addSingle<T>(AtomicId name, const T *value, bool opt); \
00116 \
00117 template<> \
00118 void TreeReader::addMulti(AtomicId name, const std::vector<T> *value);
00119 
00120 TREEREADER_ADD_IMPL(Double_t)
00121 TREEREADER_ADD_IMPL(Float_t)
00122 TREEREADER_ADD_IMPL(Int_t)
00123 TREEREADER_ADD_IMPL(Bool_t)
00124 
00125 #undef TREEREADER_ADD_IMPL
00126 
00127 } // namespace PhysicsTools
00128 
00129 #endif // PhysicsTools_MVAComputer_TreeReader_h