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 
41 namespace tnp {
42 
44  class ProbeVariable {
45  public:
47  ProbeVariable(const std::string &name, const std::string &expression)
48  : name_(name), external_(false), function_(expression) {}
49 
52  : name_(name), external_(true), function_("-1"), srcToken_(srcToken) {}
53 
56 
58  float *address() const { return &value_; }
59 
61  const std::string &name() const { return name_; }
62 
64  void init(const edm::Event &iEvent) const {
65  if (external_)
66  iEvent.getByToken(srcToken_, handle_);
67  }
68 
70  void fill(const reco::CandidateBaseRef &probe) const { value_ = external_ ? (*handle_)[probe] : function_(*probe); }
71 
72  private:
76  mutable float value_;
77 
79  bool external_;
80  // ---- this below is used if 'external_' is false
83  // In releases older than 3.4.X, it can be parially emulated by PATStringObjectFunction (PhysicsTools/PatUtils/interface/StringParserTools.h)
84  // Or you can use StringObjectFunction<reco::Candidate> and get only reco::Candidate methods
85 
86  // ---- this below is used if 'external_' is true
91  };
92 
93  class ProbeFlag {
94  public:
96  ProbeFlag(const std::string &name, const std::string &cut) : name_(name), external_(false), cut_(cut) {}
97 
100  : name_(name), external_(true), cut_(""), srcToken_(srcToken) {}
101 
103  ~ProbeFlag();
104 
106  int32_t *address() const { return &value_; }
107 
109  const std::string &name() const { return name_; }
110 
112  void init(const edm::Event &iEvent) const;
113 
115  void fill(const reco::CandidateBaseRef &probe) const;
116 
117  private:
121  mutable int32_t value_;
122 
124  bool external_;
125  // ---- this below is used if 'external_' is false
128 
129  // ---- this below is used if 'external_' is true
133  mutable std::vector<reco::CandidateBaseRef> passingProbes_;
134  };
135 
136  // This class inherits from boost::noncopyable, as copying it would break the addresses in the TTree
137  class BaseTreeFiller : boost::noncopyable {
138  public:
141  : BaseTreeFiller(name, config, iC){};
143 
146  const edm::ParameterSet &iConfig,
148  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  std::vector<ProbeVariable> vars_;
171  std::vector<ProbeFlag> flags_;
172 
174  enum WeightMode { None, Fixed, External };
186 
189 
192 
195  bool addRho_;
197 
198  void addBranches_(TTree *tree,
199  const edm::ParameterSet &iConfig,
201  const std::string &branchNamePrefix = "");
202 
203  //implementation notice: these two are 'mutable' because we will fill them from a 'const' method
204  mutable TTree *tree_;
205  mutable float weight_, PUweight_, totWeight_;
206  mutable uint32_t run_, lumi_, mNPV_;
207  mutable uint64_t event_;
208  mutable int truePU_;
209 
210  mutable float mPVx_, mPVy_, mPVz_, mBSx_, mBSy_, mBSz_;
211  mutable float rho_;
212  mutable float mMET_, mSumET_, mMETSign_, mtcMET_, mtcSumET_, mtcMETSign_, mpfMET_, mpfSumET_, mpfMETSign_, mpfPhi_;
213  };
214 
215 } // namespace tnp
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:525
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
StringObjectFunction< reco::Candidate, true > function_
a lazy-parsed StringObjectFunction<reco::Candidate> that gets all the methods of daughter classes too...
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:13
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_
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