CMS 3D CMS Logo

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         static const double     kOptVal;
00054 
00055     private:
00056         TTree                           *tree;
00057 
00058         struct Bool {
00059                 inline Bool() : value(0) {}
00060                 inline operator Bool_t() const { return value; }
00061                 Bool_t  value;
00062         };
00063 
00064         std::vector<std::pair<void*, std::vector<Double_t> > >  multiDouble;
00065         std::vector<std::pair<void*, std::vector<Float_t> > >   multiFloat;
00066         std::vector<std::pair<void*, std::vector<Int_t> > >     multiInt;
00067         std::vector<std::pair<void*, std::vector<Bool_t> > >    multiBool;
00068 
00069         std::vector<Double_t>           singleDouble;
00070         std::vector<Float_t>            singleFloat;
00071         std::vector<Int_t>              singleInt;
00072         std::vector<Bool>               singleBool;
00073 
00074         class Value {
00075             public:
00076                 Value() {}
00077                 Value(int index, bool multiple, bool optional, char type) :
00078                         index(index), optional(optional), multiple(multiple),
00079                         optVal(TreeReader::kOptVal), type(type), ptr(0) {}
00080                 ~Value() {}
00081 
00082                 void setOpt(bool opt, double optVal)
00083                 { this->optional = opt, this->optVal = optVal; }
00084                 void setBranchName(const TString &name)
00085                 { this->name = name; }
00086                 void setPtr(const void *ptr)
00087                 { this->ptr = ptr; }
00088 
00089                 void update(TreeReader *reader) const;
00090                 void fill(AtomicId name, TreeReader *reader) const;
00091 
00092             private:
00093                 TString         name;
00094                 int             index;
00095                 bool            optional;
00096                 bool            multiple;
00097                 double          optVal;
00098                 char            type;
00099                 const void      *ptr;
00100         };
00101 
00102         friend class Value;
00103 
00104         std::map<AtomicId, Value>       valueMap;
00105         Variable::ValueList             values;
00106         bool                            upToDate;
00107 };
00108 
00109 #define TREEREADER_ADD_IMPL(T) \
00110 template<> \
00111 void TreeReader::addSingle<T>(AtomicId name, const T *value, bool opt); \
00112 \
00113 template<> \
00114 void TreeReader::addMulti(AtomicId name, const std::vector<T> *value);
00115 
00116 TREEREADER_ADD_IMPL(Double_t)
00117 TREEREADER_ADD_IMPL(Float_t)
00118 TREEREADER_ADD_IMPL(Int_t)
00119 TREEREADER_ADD_IMPL(Bool_t)
00120 
00121 #undef TREEREADER_ADD_IMPL
00122 
00123 } // namespace PhysicsTools
00124 
00125 #endif // PhysicsTools_MVAComputer_TreeReader_h

Generated on Tue Jun 9 17:41:20 2009 for CMSSW by  doxygen 1.5.4