CMS 3D CMS Logo

BaseTreeFiller.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_TagAndProbe_BaseTreeFiller_h
2 #define PhysicsTools_TagAndProbe_BaseTreeFiller_h
3 
4 #include <vector>
5 #include <string>
6 #include <cstdint>
7 
28 
31 
32 #include <TTree.h>
33 #include <boost/utility.hpp>
34 
35 
42 namespace tnp {
43 
46  public:
48  ProbeVariable(const std::string &name, const std::string &expression) :
49  name_(name), external_(false), function_(expression) {}
50 
53  name_(name), external_(true), function_("-1"), srcToken_(srcToken) {}
54 
56  ~ProbeVariable() ;
57 
59  float * address() const { return &value_; }
60 
62  const std::string & name() const { return name_; }
63 
65  void init(const edm::Event &iEvent) const {
66  if (external_) iEvent.getByToken(srcToken_, handle_);
67  }
68 
70  void fill(const reco::CandidateBaseRef &probe) const {
71  value_ = external_ ? (*handle_)[probe] : function_(*probe);
72  }
73 
74  private:
78  mutable float value_;
79 
81  bool external_;
82  // ---- this below is used if 'external_' is false
85  // In releases older than 3.4.X, it can be parially emulated by PATStringObjectFunction (PhysicsTools/PatUtils/interface/StringParserTools.h)
86  // Or you can use StringObjectFunction<reco::Candidate> and get only reco::Candidate methods
87 
88  // ---- this below is used if 'external_' is true
93 };
94 
95 class ProbeFlag {
96  public:
99  name_(name), external_(false), cut_(cut) {}
100 
103  name_(name), external_(true), cut_(""), srcToken_(srcToken) {}
104 
106  ~ProbeFlag() ;
107 
109  int32_t * address() const { return &value_; }
110 
112  const std::string & name() const { return name_; }
113 
115  void init(const edm::Event &iEvent) const ;
116 
118  void fill(const reco::CandidateBaseRef &probe) const ;
119 
120  private:
124  mutable int32_t value_;
125 
127  bool external_;
128  // ---- this below is used if 'external_' is false
131 
132  // ---- this below is used if 'external_' is true
136  mutable std::vector<reco::CandidateBaseRef> passingProbes_;
137 };
138 
139 
140 // This class inherits from boost::noncopyable, as copying it would break the addresses in the TTree
141 class BaseTreeFiller : boost::noncopyable {
142  public:
144  BaseTreeFiller(const char *name, const edm::ParameterSet& config, edm::ConsumesCollector && iC) : BaseTreeFiller(name, config, iC) {};
146 
148  BaseTreeFiller(BaseTreeFiller &main, const edm::ParameterSet &iConfig, edm::ConsumesCollector && iC, const std::string &branchNamePrefix);
149 
151  ~BaseTreeFiller();
152 
153  //implementation notice: we declare 'const' the methods which don't change the configuration
154  // and that can't mess up the addresses in the TTree.
155 
157  void init(const edm::Event &iEvent) const ;
158 
160  void fill(const reco::CandidateBaseRef &probe) const ;
161 
164  void writeProvenance(const edm::ParameterSet &pset) const ;
165 
166  //get the pileup weight informations
167  bool storePUweight() const {return storePUweight_;};
168 
169  protected:
170 
171  std::vector<ProbeVariable> vars_;
172  std::vector<ProbeFlag> flags_;
173 
175  enum WeightMode { None, Fixed, External };
187 
190 
193 
196  bool addRho_;
198 
199  void addBranches_(TTree *tree, const edm::ParameterSet &iConfig, edm::ConsumesCollector & iC, const std::string &branchNamePrefix="") ;
200 
201  //implementation notice: these two are 'mutable' because we will fill them from a 'const' method
202  mutable TTree * tree_;
203  mutable float weight_, PUweight_, totWeight_;
204  mutable uint32_t run_, lumi_, mNPV_;
205  mutable uint64_t event_;
206  mutable int truePU_;
207 
208  mutable float mPVx_,mPVy_,mPVz_,mBSx_,mBSy_,mBSz_;
209  mutable float rho_;
210  mutable float mMET_,mSumET_,mMETSign_,mtcMET_,mtcSumET_,
211  mtcMETSign_,mpfMET_,mpfSumET_,mpfMETSign_, mpfPhi_;
212 };
213 
214 
215 } //namespace
216 
217 #endif
float * address() const
Addess for ROOT Branch.
bool addEventVariablesInfo_
Add branches with event variables: met, sum ET, .. etc.
ProbeVariable(const std::string &name, const std::string &expression)
Create a ProbeVariable to be evaluated on the fly from a string expression.
edm::EDGetTokenT< reco::PFMETCollection > pfmetToken_
bool external_
true if it&#39;s an external ValueMap, false if it&#39;s a StringParser function
std::string name_
the name of the variable, which becomes the ROOT branch name
edm::EDGetTokenT< GenEventInfoProduct > weightSrcToken_
bool addRunLumiInfo_
Add branches with run and lumisection number.
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
edm::EDGetTokenT< double > PUweightSrcToken_
std::vector< ProbeVariable > vars_
ProbeFlag(const std::string &name, const std::string &cut)
Create a ProbeFlag to be evaluated on the fly from a string cut.
StringCutObjectSelector< reco::Candidate, true > cut_
implementation of the cut using a string selector
std::string name_
the name of the variable, which becomes the ROOT branch name
Definition: config.py:1
void init(const edm::Event &iEvent) const
To be called at the beginning of the event (will fetch ValueMap if needed)
A variable for the probe: can be a string expression or an external ValueMap<float> ...
int32_t * address() const
Addess for ROOT Branch.
const std::string & name() const
name
ProbeFlag(const std::string &name, const edm::EDGetTokenT< edm::View< reco::Candidate > > &srcToken)
Create a ProbeFlag to be read from a ValueMap.
std::vector< ProbeFlag > flags_
int iEvent
Definition: GenABIO.cc:224
edm::Handle< edm::ValueMap< float > > handle_
the handle to keep the ValueMap
edm::EDGetTokenT< reco::CaloMETCollection > metToken_
edm::EDGetTokenT< pat::METCollection > pfmetTokenMiniAOD_
edm::EDGetTokenT< reco::METCollection > tcmetToken_
ProbeVariable(const std::string &name, const edm::EDGetTokenT< edm::ValueMap< float > > &srcToken)
Create a ProbeVariable to be read from a ValueMap.
edm::EDGetTokenT< double > rhoToken_
int32_t value_
the place where we store the value, and that ROOT uses to fill the tree
edm::EDGetTokenT< edm::ValueMap< float > > srcToken_
the external valuemap
~ProbeVariable()
Destructor (does nothing)
unsigned long long uint64_t
Definition: Time.h:15
WeightMode
How event weights are defined: &#39;None&#39; = no weights, &#39;Fixed&#39; = one value specified in cfg file...
edm::EDGetTokenT< reco::VertexCollection > recVtxsToken_
edm::EDGetTokenT< reco::BeamSpot > beamSpotToken_
void fill(const reco::CandidateBaseRef &probe) const
To be called for each item.
Definition: main.py:1
bool external_
true if it&#39;s an external Candidate View, false if it&#39;s a StringParser cut
edm::EDGetTokenT< std::vector< PileupSummaryInfo > > pileupInfoToken_
StringObjectFunction< reco::Candidate, true > function_
a lazy-parsed StringObjectFunction<reco::Candidate> that gets all the methods of daughter classes too...
Definition: tree.py:1
const std::string & name() const
name
bool storePUweight_
Store Pileup weight when running over Monte Carlo.
edm::EDGetTokenT< edm::View< reco::Candidate > > srcToken_
the external collection
std::vector< reco::CandidateBaseRef > passingProbes_
the handle to keep the refs to the passing probes
float value_
the place where we store the value, and that ROOT uses to fill the tree
BaseTreeFiller(const char *name, const edm::ParameterSet &config, edm::ConsumesCollector &&iC)
specify the name of the TTree, and the configuration for it