CMS 3D CMS Logo

BaseTreeFiller.cc
Go to the documentation of this file.
4 
5 
7 
8 #include <TList.h>
9 #include <TObjString.h>
10 
11 #include <iostream>
12 using namespace std;
13 
15 
17 
19  if (external_) {
21  iEvent.getByToken(srcToken_, view);
22  passingProbes_.clear();
23  for (size_t i = 0, n = view->size(); i < n; ++i) passingProbes_.push_back(view->refAt(i));
24  }
25 
26 }
27 
28 void tnp::ProbeFlag::fill(const reco::CandidateBaseRef &probe) const {
29  if (external_) {
30  value_ = (std::find(passingProbes_.begin(), passingProbes_.end(), probe) != passingProbes_.end());
31  } else {
32  value_ = bool(cut_(*probe));
33  }
34 }
35 
37  // make trees as requested
39  tree_ = fs->make<TTree>(name,name);
40 
41  // add the branches
42  addBranches_(tree_, iConfig, iC, "");
43 
44  // set up weights, if needed
45  if (iConfig.existsAs<double>("eventWeight")) {
46  weightMode_ = Fixed;
47  weight_ = iConfig.getParameter<double>("eventWeight");
48  } else if (iConfig.existsAs<edm::InputTag>("eventWeight")) {
49  weightMode_ = External;
50  weightSrcToken_ = iC.consumes<GenEventInfoProduct>(iConfig.getParameter<edm::InputTag>("eventWeight"));
51  } else {
52  weightMode_ = None;
53  }
54  if (weightMode_ != None) {
55  tree_->Branch("weight", &weight_, "weight/F");
56  tree_->Branch("totWeight", &totWeight_, "totWeight/F");
57  }
58 
59  storePUweight_ = iConfig.existsAs<edm::InputTag>("PUWeightSrc") ? true: false;
60  if(storePUweight_) {
61  PUweightSrcToken_ = iC.consumes<double>(iConfig.getParameter<edm::InputTag>("PUWeightSrc"));
62  tree_->Branch("PUweight", &PUweight_, "PUweight/F");
63  }
64 
65  if( iConfig.existsAs<edm::InputTag>("pileupInfoTag") )
66  pileupInfoToken_= iC.consumes<std::vector<PileupSummaryInfo> >(iConfig.getParameter<edm::InputTag>("pileupInfoTag"));
67 
68  addRunLumiInfo_ = iConfig.existsAs<bool>("addRunLumiInfo") ? iConfig.getParameter<bool>("addRunLumiInfo") : false;
69  if (addRunLumiInfo_) {
70  tree_->Branch("run", &run_, "run/i");
71  tree_->Branch("lumi", &lumi_, "lumi/i");
72  tree_->Branch("event", &event_, "event/l");
73  tree_->Branch("truePU", &truePU_, "truePU/I");
74  }
75  addEventVariablesInfo_ = iConfig.existsAs<bool>("addEventVariablesInfo") ? iConfig.getParameter<bool>("addEventVariablesInfo") : false;
76  if (addEventVariablesInfo_) {
78  edm::InputTag bsIT = iConfig.existsAs<edm::InputTag>("beamSpot") ? iConfig.getParameter<edm::InputTag>("beamSpot") : edm::InputTag("offlineBeamSpot");
79  edm::InputTag vtxIT = iConfig.existsAs<edm::InputTag>("vertexCollection") ? iConfig.getParameter<edm::InputTag>("vertexCollection") : edm::InputTag("offlinePrimaryVertices");
80  edm::InputTag pfMetIT = iConfig.existsAs<edm::InputTag>("pfMet") ? iConfig.getParameter<edm::InputTag>("pfMet") : edm::InputTag("pfMet");
81  edm::InputTag tcMetIT = iConfig.existsAs<edm::InputTag>("tcMet") ? iConfig.getParameter<edm::InputTag>("tcMet") : edm::InputTag("tcMet");
82  edm::InputTag clMetIT = iConfig.existsAs<edm::InputTag>("clMet") ? iConfig.getParameter<edm::InputTag>("clMet") : edm::InputTag("met");
83 
84  recVtxsToken_ = iC.consumes<reco::VertexCollection>(vtxIT);
85  beamSpotToken_ = iC.consumes<reco::BeamSpot>(bsIT);
86  pfmetToken_ = iC.mayConsume<reco::PFMETCollection>(pfMetIT);
87  pfmetTokenMiniAOD_ = iC.mayConsume<pat::METCollection>(pfMetIT);
88  addCaloMet_ = iConfig.existsAs<bool>("addCaloMet") ? iConfig.getParameter<bool>("addCaloMet") : true;
89  tree_->Branch("event_nPV" ,&mNPV_ ,"mNPV/I");
90  if( addCaloMet_ ) {
91  metToken_ = iC.mayConsume<reco::CaloMETCollection>(clMetIT);
92  tcmetToken_ = iC.mayConsume<reco::METCollection>( tcMetIT);
93  tree_->Branch("event_met_calomet" ,&mMET_ ,"mMET/F");
94  tree_->Branch("event_met_calosumet" ,&mSumET_ ,"mSumET/F");
95  tree_->Branch("event_met_calometsignificance",&mMETSign_ ,"mMETSign/F");
96  tree_->Branch("event_met_tcmet" ,&mtcMET_ ,"mtcMET/F");
97  tree_->Branch("event_met_tcsumet" ,&mtcSumET_ ,"mtcSumET/F");
98  tree_->Branch("event_met_tcmetsignificance",&mtcMETSign_ ,"mtcMETSign/F");
99  }
100  tree_->Branch("event_met_pfmet" ,&mpfMET_ ,"mpfMET/F");
101  tree_->Branch("event_met_pfphi" ,&mpfPhi_ ,"mpfPhi/F");
102  tree_->Branch("event_met_pfsumet" ,&mpfSumET_ ,"mpfSumET/F");
103 
104  tree_->Branch("event_met_pfmetsignificance",&mpfMETSign_ ,"mpfMETSign/F");
105  tree_->Branch("event_PrimaryVertex_x" ,&mPVx_ ,"mPVx/F");
106  tree_->Branch("event_PrimaryVertex_y" ,&mPVy_ ,"mPVy/F");
107  tree_->Branch("event_PrimaryVertex_z" ,&mPVz_ ,"mPVz/F");
108  tree_->Branch("event_BeamSpot_x" ,&mBSx_ ,"mBSx/F");
109  tree_->Branch("event_BeamSpot_y" ,&mBSy_ ,"mBSy/F");
110  tree_->Branch("event_BeamSpot_z" ,&mBSz_ ,"mBSz/F");
111  }
112 
113  addRho_ = iConfig.existsAs<edm::InputTag>("rho") ? true : false;
114  if (addRho_) {
115  rhoToken_ = iC.consumes<double>(iConfig.getParameter<edm::InputTag>("rho"));
116  tree_->Branch("event_rho" ,&rho_ ,"rho/F");
117  }
118 }
119 
121  addRunLumiInfo_(false),
122  addEventVariablesInfo_(false),
123  tree_(nullptr)
124 {
127  addBranches_(main.tree_, iConfig, iC, branchNamePrefix);
128 }
129 
130 void
131 tnp::BaseTreeFiller::addBranches_(TTree *tree, const edm::ParameterSet &iConfig, edm::ConsumesCollector & iC, const std::string &branchNamePrefix) {
132  // set up variables
134  //.. the ones that are strings
135  std::vector<std::string> stringVars = variables.getParameterNamesForType<std::string>();
136  for (std::vector<std::string>::const_iterator it = stringVars.begin(), ed = stringVars.end(); it != ed; ++it) {
137  vars_.push_back(tnp::ProbeVariable(branchNamePrefix + *it, variables.getParameter<std::string>(*it)));
138  }
139  //.. the ones that are InputTags
140  std::vector<std::string> inputTagVars = variables.getParameterNamesForType<edm::InputTag>();
141  for (std::vector<std::string>::const_iterator it = inputTagVars.begin(), ed = inputTagVars.end(); it != ed; ++it) {
142  vars_.push_back(tnp::ProbeVariable(branchNamePrefix + *it, iC.consumes<edm::ValueMap<float> >(variables.getParameter<edm::InputTag>(*it))));
143  }
144  // set up flags
146  //.. the ones that are strings
147  std::vector<std::string> stringFlags = flags.getParameterNamesForType<std::string>();
148  for (std::vector<std::string>::const_iterator it = stringFlags.begin(), ed = stringFlags.end(); it != ed; ++it) {
149  flags_.push_back(tnp::ProbeFlag(branchNamePrefix + *it, flags.getParameter<std::string>(*it)));
150  }
151  //.. the ones that are InputTags
152  std::vector<std::string> inputTagFlags = flags.getParameterNamesForType<edm::InputTag>();
153  for (std::vector<std::string>::const_iterator it = inputTagFlags.begin(), ed = inputTagFlags.end(); it != ed; ++it) {
154  flags_.push_back(tnp::ProbeFlag(branchNamePrefix + *it, iC.consumes<edm::View<reco::Candidate> >(flags.getParameter<edm::InputTag>(*it))));
155  }
156 
157  // then make all the variables in the trees
158  for (std::vector<tnp::ProbeVariable>::iterator it = vars_.begin(), ed = vars_.end(); it != ed; ++it) {
159  tree->Branch(it->name().c_str(), it->address(), (it->name()+"/F").c_str());
160  }
161 
162  for (std::vector<tnp::ProbeFlag>::iterator it = flags_.begin(), ed = flags_.end(); it != ed; ++it) {
163  tree->Branch(it->name().c_str(), it->address(), (it->name()+"/I").c_str());
164  }
165 
166 }
167 
169 
171  run_ = iEvent.id().run();
172  lumi_ = iEvent.id().luminosityBlock();
173  event_ = iEvent.id().event();
174 
175  truePU_ = 0;
176  if (!iEvent.isRealData() and !pileupInfoToken_.isUninitialized()) {
178  iEvent.getByToken(pileupInfoToken_, PupInfo);
179  truePU_ = PupInfo->begin()->getTrueNumInteractions();
180  }
181 
182  totWeight_ = 1.;
183  for (std::vector<tnp::ProbeVariable>::const_iterator it = vars_.begin(), ed = vars_.end(); it != ed; ++it) {
184  it->init(iEvent);
185  }
186  for (std::vector<tnp::ProbeFlag>::const_iterator it = flags_.begin(), ed = flags_.end(); it != ed; ++it) {
187  it->init(iEvent);
188  }
189  if (weightMode_ == External) {
190  // edm::Handle<double> weight;
191  // iEvent.getByToken(weightSrcToken_, weight);
192  // weight_ = *weight;
194  iEvent.getByToken(weightSrcToken_, weight);
195  weight_ = weight->weight();
196  totWeight_ *= weight_;
197  }
198 
200  PUweight_ = 1;
202  edm::Handle<double> weightPU;
203  bool isPresent = iEvent.getByToken(PUweightSrcToken_, weightPU);
204  if(isPresent) PUweight_ = float(*weightPU);
206  }
207 
212  iEvent.getByToken(recVtxsToken_,recVtxs);
213  mNPV_ = 0;
214  mPVx_ = 100.0;
215  mPVy_ = 100.0;
216  mPVz_ = 100.0;
217 
218  for(unsigned int ind=0;ind<recVtxs->size();ind++) {
219  if (!((*recVtxs)[ind].isFake()) && ((*recVtxs)[ind].ndof()>4)
220  && (fabs((*recVtxs)[ind].z())<=24.0) &&
221  ((*recVtxs)[ind].position().Rho()<=2.0) ) {
222  mNPV_++;
223  if(mNPV_==1) { // store the first good primary vertex
224  mPVx_ = (*recVtxs)[ind].x();
225  mPVy_ = (*recVtxs)[ind].y();
226  mPVz_ = (*recVtxs)[ind].z();
227  }
228  }
229  }
230 
233  iEvent.getByToken(beamSpotToken_, beamSpot);
234  mBSx_ = beamSpot->position().X();
235  mBSy_ = beamSpot->position().Y();
236  mBSz_ = beamSpot->position().Z();
237 
238  if( addCaloMet_) {
241  iEvent.getByToken(metToken_,met);
242  if (met->empty()) {
243  mMET_ = -1;
244  mSumET_ = -1;
245  mMETSign_ = -1;
246  }
247  else {
248  mMET_ = (*met)[0].et();
249  mSumET_ = (*met)[0].sumEt();
250  mMETSign_ = (*met)[0].significance();
251  }
252 
255  iEvent.getByToken(tcmetToken_, tcmet);
256  if (tcmet->empty()) {
257  mtcMET_ = -1;
258  mtcSumET_ = -1;
259  mtcMETSign_ = -1;
260  }
261  else {
262  mtcMET_ = (*tcmet)[0].et();
263  mtcSumET_ = (*tcmet)[0].sumEt();
264  mtcMETSign_ = (*tcmet)[0].significance();
265  }
266  }
267 
270  iEvent.getByToken(pfmetToken_, pfmet);
271  if( pfmet.isValid() ) {
272  if (pfmet->empty()) {
273  mpfMET_ = -1;
274  mpfSumET_ = -1;
275  mpfMETSign_ = -1;
276  }
277  else {
278  mpfMET_ = (*pfmet)[0].et();
279  mpfPhi_ = (*pfmet)[0].phi();
280  mpfSumET_ = (*pfmet)[0].sumEt();
281  mpfMETSign_ = (*pfmet)[0].significance();
282  }
283  } else {
285  iEvent.getByToken(pfmetTokenMiniAOD_, pfmet2);
286  const pat::MET &met = pfmet2->front();
287  mpfMET_ = met.pt();
288  mpfPhi_ = met.phi();
289  mpfSumET_ = met.sumEt();
290  mpfMETSign_ = met.significance();
291  }
292 
293  if (addRho_) {
294  edm::Handle<double> rhos;
295  iEvent.getByToken(rhoToken_, rhos);
296  rho_ = (float) *rhos;
297  }
298  }
299 
300 
301 }
302 
304 
305  for(auto const& var : vars_) var.fill(probe);
306  for(auto const& flag : flags_) flag.fill(probe);
307 
308  if (tree_) tree_->Fill();
309 }
311  TList *list = tree_->GetUserInfo();
312  list->Add(new TObjString(pset.dump().c_str()));
313 }
RunNumber_t run() const
Definition: EventID.h:39
bool addEventVariablesInfo_
Add branches with event variables: met, sum ET, .. etc.
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Analysis-level MET class.
Definition: MET.h:40
T getParameter(std::string const &) const
EventNumber_t event() const
Definition: EventID.h:41
edm::EDGetTokenT< reco::PFMETCollection > pfmetToken_
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:161
void init(const edm::Event &iEvent) const
To be called at the beginning of the event (will fetch Candidate View if needed)
edm::EDGetTokenT< GenEventInfoProduct > weightSrcToken_
EDGetTokenT< ProductType > mayConsume(edm::InputTag const &tag)
bool addRunLumiInfo_
Add branches with run and lumisection number.
void writeProvenance(const edm::ParameterSet &pset) const
std::string dump(unsigned int indent=0) const
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
std::vector< MET > METCollection
Definition: MET.h:31
edm::EDGetTokenT< double > PUweightSrcToken_
#define nullptr
std::vector< ProbeVariable > vars_
void fill(const reco::CandidateBaseRef &probe) const
To be called for each item.
std::vector< Variable::Flags > flags
Definition: MVATrainer.cc:135
~ProbeFlag()
Destructor (does nothing)
double pt() const final
transverse momentum
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
double weight() const
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
LuminosityBlockNumber_t luminosityBlock() const
Definition: EventID.h:40
std::vector< reco::MET > METCollection
collection of MET objects
Definition: METCollection.h:23
bool isRealData() const
Definition: EventBase.h:62
void fill(const reco::CandidateBaseRef &probe) const
To be called once per probe, to fill the values for this probe.
A variable for the probe: can be a string expression or an external ValueMap<float> ...
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:169
std::vector< ProbeFlag > flags_
~BaseTreeFiller()
Destructor, does nothing but it&#39;s out-of-line as we have complex data members.
int iEvent
Definition: GenABIO.cc:224
double sumEt() const
Definition: MET.h:56
edm::EDGetTokenT< reco::CaloMETCollection > metToken_
edm::EDGetTokenT< pat::METCollection > pfmetTokenMiniAOD_
edm::EDGetTokenT< reco::METCollection > tcmetToken_
void init(const edm::Event &iEvent) const
To be called once per event, to load possible external variables.
bool isValid() const
Definition: HandleBase.h:74
edm::EDGetTokenT< double > rhoToken_
~ProbeVariable()
Destructor (does nothing)
void addBranches_(TTree *tree, const edm::ParameterSet &iConfig, edm::ConsumesCollector &iC, const std::string &branchNamePrefix="")
std::vector< reco::CaloMET > CaloMETCollection
collection of CaloMET objects
met
===> hadronic RAZOR
edm::EDGetTokenT< reco::VertexCollection > recVtxsToken_
edm::EventID id() const
Definition: EventBase.h:59
edm::EDGetTokenT< reco::BeamSpot > beamSpotToken_
std::vector< reco::PFMET > PFMETCollection
collection of PFMET objects
void event_()
Definition: main.py:1
bool isUninitialized() const
Definition: EDGetToken.h:70
const Point & position() const
position
Definition: BeamSpot.h:62
edm::EDGetTokenT< std::vector< PileupSummaryInfo > > pileupInfoToken_
Definition: tree.py:1
double significance() const
Definition: MET.cc:78
double phi() const final
momentum azimuthal angle
bool storePUweight_
Store Pileup weight when running over Monte Carlo.
BaseTreeFiller(const char *name, const edm::ParameterSet &config, edm::ConsumesCollector &&iC)
specify the name of the TTree, and the configuration for it
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger list("!*","!HLTx*"if it matches 2 triggers or more) will accept the event if all the matching triggers are FAIL.It will reject the event if any of the triggers are PASS or EXCEPTION(this matches the behavior of"!*"before the partial wildcard feature was incorporated).Triggers which are in the READY state are completely ignored.(READY should never be returned since the trigger paths have been run