CMS 3D CMS Logo

PFTauElecRejectionBenchmark.cc
Go to the documentation of this file.
2 
3 // preprocessor macro for booking 1d histos with DQMStore -or- bare Root
4 #define BOOK1D(name,title,nbinsx,lowx,highx) \
5  h##name = db_ ? db_->book1D(#name,title,nbinsx,lowx,highx)->getTH1F() \
6  : new TH1F(#name,title,nbinsx,lowx,highx)
7 
8 // preprocessor macro for booking 2d histos with DQMStore -or- bare Root
9 #define BOOK2D(name,title,nbinsx,lowx,highx,nbinsy,lowy,highy) \
10  h##name = db_ ? db_->book2D(#name,title,nbinsx,lowx,highx,nbinsy,lowy,highy)->getTH2F() \
11  : new TH2F(#name,title,nbinsx,lowx,highx,nbinsy,lowy,highy)
12 
13 // all versions OK
14 // preprocesor macro for setting axis titles
15 #define SETAXES(name,xtitle,ytitle) \
16  h##name->GetXaxis()->SetTitle(xtitle); h##name->GetYaxis()->SetTitle(ytitle)
17 
18 
19 using namespace reco;
20 using namespace std;
21 
22 class MonitorElement;
23 
25 
27  if(file_) file_->Close();
28 }
29 
31  // Store the DAQ Histograms
32  if (!outputFile_.empty()) {
33  if (db_)
34  db_->save(outputFile_);
35  // use bare Root if no DQM (FWLite applications)
36  else if (file_) {
37  file_->Write(outputFile_.c_str());
38  cout << "Benchmark output written to file " << outputFile_.c_str() << endl;
39  file_->Close();
40  }
41  }
42  else
43  cout << "No output file specified ("<<outputFile_<<"). Results will not be saved!" << endl;
44 
45 }
46 
47 
49  string Filename,
50  string benchmarkLabel,
51  double maxDeltaR,
52  double minRecoPt,
53  double maxRecoAbsEta,
54  double minMCPt,
55  double maxMCAbsEta,
56  string sGenMatchObjectLabel,
57  bool applyEcalCrackCut,
58  DQMStore * db_store) {
60  benchmarkLabel_ = benchmarkLabel;
61  outputFile_=Filename;
64  minMCPt_ = minMCPt;
66  sGenMatchObjectLabel_ = sGenMatchObjectLabel;
67  applyEcalCrackCut_= applyEcalCrackCut;
68 
69  file_ = nullptr;
70  db_ = db_store;
71 
72  // print parameters
73  cout<< "PFTauElecRejectionBenchmark Setup parameters =============================================="<<endl;
74  cout << "Filename to write histograms " << Filename<<endl;
75  cout << "Benchmark label name " << benchmarkLabel_<<endl;
76  cout << "maxDeltaRMax " << maxDeltaR << endl;
77  cout << "minRecoPt " << minRecoPt_ << endl;
78  cout << "maxRecoAbsEta " << maxRecoAbsEta_ << endl;
79  cout << "minMCPt " << minMCPt_ << endl;
80  cout << "maxMCAbsEta " << maxMCAbsEta_ << endl;
81  cout << "applyEcalCrackCut " << applyEcalCrackCut_ << endl;
82  cout << "sGenMatchObjectLabel " << sGenMatchObjectLabel_ << endl;
83 
84  // Book histogram
85 
86  // Establish DQM Store
87  string path = "PFTask/Benchmarks/"+ benchmarkLabel_ + "/";
88  path += "Gen";
89  if (db_) {
90  db_->setCurrentFolder(path);
91  }
92  else {
93  file_ = new TFile(outputFile_.c_str(), "recreate");
94  cout << "Info: DQM is not available to provide data storage service. Using TFile to save histograms. "<<endl;
95  }
96 
97  // E/p
98  BOOK1D(EoverP,"E/p",100, 0., 4.);
99  SETAXES(EoverP, "E/p", "Entries");
100 
101  BOOK1D(EoverP_barrel,"E/p barrel",100, 0., 4.);
102  SETAXES(EoverP_barrel, "E/p barrel", "Entries");
103 
104  BOOK1D(EoverP_endcap,"E/p endcap",100, 0., 4.);
105  SETAXES(EoverP_endcap, "E/p endcap", "Entries");
106 
107  BOOK1D(EoverP_preid0,"E/p (preid=0)",100, 0., 4.);
108  SETAXES(EoverP_preid0, "E/p", "Entries");
109 
110  BOOK1D(EoverP_preid1,"E/p (preid=1)",100, 0., 4.);
111  SETAXES(EoverP_preid1, "E/p", "Entries");
112 
113  // H/p
114  BOOK1D(HoverP,"H/p",100, 0., 2.);
115  SETAXES(HoverP, "H/p", "Entries");
116 
117  BOOK1D(HoverP_barrel,"H/p barrel",100, 0., 2.);
118  SETAXES(HoverP_barrel, "H/p barrel", "Entries");
119 
120  BOOK1D(HoverP_endcap,"H/p endcap",100, 0., 2.);
121  SETAXES(HoverP_endcap, "H/p endcap", "Entries");
122 
123  BOOK1D(HoverP_preid0,"H/p (preid=0)",100, 0., 2.);
124  SETAXES(HoverP_preid0, "H/p", "Entries");
125 
126  BOOK1D(HoverP_preid1,"H/p (preid=1)",100, 0., 2.);
127  SETAXES(HoverP_preid1, "H/p", "Entries");
128 
129  // emfrac
130  BOOK1D(Emfrac,"EM fraction",100, 0., 1.01);
131  SETAXES(Emfrac, "em fraction", "Entries");
132 
133  BOOK1D(Emfrac_barrel,"EM fraction barrel",100, 0., 1.01);
134  SETAXES(Emfrac_barrel, "em fraction barrel", "Entries");
135 
136  BOOK1D(Emfrac_endcap,"EM fraction endcap",100, 0., 1.01);
137  SETAXES(Emfrac_endcap, "em fraction endcap", "Entries");
138 
139  BOOK1D(Emfrac_preid0,"EM fraction (preid=0)",100, 0., 1.01);
140  SETAXES(Emfrac_preid0, "em fraction", "Entries");
141 
142  BOOK1D(Emfrac_preid1,"EM fraction (preid=1)",100, 0., 1.01);
143  SETAXES(Emfrac_preid1, "em fraction", "Entries");
144 
145  // PreID
146  BOOK1D(ElecPreID,"PFElectron PreID decision",6, 0., 1.01);
147  SETAXES(ElecPreID, "PFElectron PreID decision", "Entries");
148 
149  // MVA
150  BOOK1D(ElecMVA,"PFElectron MVA",100, -1.01, 1.01);
151  SETAXES(ElecMVA, "PFElectron MVA", "Entries");
152 
153  // Discriminant
154  BOOK1D(TauElecDiscriminant,"PFTau-Electron Discriminant",6, 0., 1.01);
155  SETAXES(TauElecDiscriminant, "PFTau-Electron Discriminant", "Entries");
156 
157 
158  // PFCand clusters
159  BOOK1D(pfcand_deltaEta,"PFCand cluster dEta",100, 0., 0.8);
160  SETAXES(pfcand_deltaEta, "PFCand cluster #Delta(#eta)", "Entries");
161 
162  BOOK1D(pfcand_deltaEta_weightE,"PFCand cluster dEta, energy weighted",100, 0., 0.8);
163  SETAXES(pfcand_deltaEta_weightE, "PFCand cluster #Delta(#eta)", "Entries");
164 
165  BOOK1D(pfcand_deltaPhiOverQ,"PFCand cluster dPhi/q",100, -0.8, 0.8);
166  SETAXES(pfcand_deltaPhiOverQ, "PFCand cluster #Delta(#phi)/q", "Entries");
167 
168  BOOK1D(pfcand_deltaPhiOverQ_weightE,"PFCand cluster dEta/q, energy weighted",100, -0.8, 0.8);
169  SETAXES(pfcand_deltaPhiOverQ_weightE, "PFCand cluster #Delta(#phi)/q", "Entries");
170 
171 
172  // Leading KF track
173  BOOK1D(leadTk_pt,"leading KF track pt",100, 0., 80.);
174  SETAXES(leadTk_pt, "leading KF track p_{T} (GeV)", "Entries");
175 
176  BOOK1D(leadTk_eta,"leading KF track eta",100, -4., 4.);
177  SETAXES(leadTk_eta, "leading KF track #eta", "Entries");
178 
179  BOOK1D(leadTk_phi,"leading KF track phi",100, -3.2, 3.2);
180  SETAXES(leadTk_phi, "leading KF track #phi", "Entries");
181 
182  // Leading Gsf track
183  BOOK1D(leadGsfTk_pt,"leading Gsf track pt",100, 0., 80.);
184  SETAXES(leadGsfTk_pt, "leading Gsf track p_{T} (GeV)", "Entries");
185 
186  BOOK1D(leadGsfTk_eta,"leading Gsf track eta",100, -4., 4.);
187  SETAXES(leadGsfTk_eta, "leading Gsf track #eta", "Entries");
188 
189  BOOK1D(leadGsfTk_phi,"leading Gsf track phi",100, -3.2, 3.2);
190  SETAXES(leadGsfTk_phi, "leading Gsf track #phi", "Entries");
191 
192 
193  // H/p vs E/p
194  BOOK2D(HoPvsEoP,"H/p vs. E/p",100, 0., 2., 100, 0., 2.);
195  SETAXES(HoPvsEoP, "E/p", "H/p");
196 
197  BOOK2D(HoPvsEoP_preid0,"H/p vs. E/p (preid=0)",100, 0., 2., 100, 0., 2.);
198  SETAXES(HoPvsEoP_preid0, "E/p", "H/p");
199 
200  BOOK2D(HoPvsEoP_preid1,"H/p vs. E/p (preid=0)",100, 0., 2., 100, 0., 2.);
201  SETAXES(HoPvsEoP_preid1, "E/p", "H/p");
202 
203  // em fraction vs E/p
204  BOOK2D(EmfracvsEoP,"emfrac vs. E/p",100, 0., 2., 100, 0., 1.01);
205  SETAXES(EmfracvsEoP, "E/p", "em fraction");
206 
207  BOOK2D(EmfracvsEoP_preid0,"emfrac vs. E/p (preid=0)",100, 0., 2., 100, 0., 1.01);
208  SETAXES(EmfracvsEoP_preid0, "E/p", "em fraction");
209 
210  BOOK2D(EmfracvsEoP_preid1,"emfrac vs. E/p (preid=0)",100, 0., 2., 100, 0., 1.01);
211  SETAXES(EmfracvsEoP_preid1, "E/p", "em fraction");
212 
213 
214 }
215 
216 
219  edm::Handle<reco::PFTauDiscriminator> pfTauElecDiscr) {
220 
221 
222  // Find Gen Objects to be matched with
223  HepMC::GenEvent * generated_event = new HepMC::GenEvent(*(mcevt->GetEvent()));
224  _GenObjects.clear();
225 
226  TLorentzVector taunet;
227  HepMC::GenEvent::particle_iterator p;
228  for (p = generated_event->particles_begin(); p != generated_event->particles_end(); p++) {
229  if(std::abs((*p)->pdg_id()) == 15&&(*p)->status()==2) {
230  bool lept_decay = false;
231  TLorentzVector tau((*p)->momentum().px(),(*p)->momentum().py(),(*p)->momentum().pz(),(*p)->momentum().e());
232  HepMC::GenVertex::particle_iterator z = (*p)->end_vertex()->particles_begin(HepMC::descendants);
233  for(; z != (*p)->end_vertex()->particles_end(HepMC::descendants); z++) {
234  if(std::abs((*z)->pdg_id()) == 11 || std::abs((*z)->pdg_id()) == 13) lept_decay=true;
235  if(std::abs((*z)->pdg_id()) == 16)
236  taunet.SetPxPyPzE((*z)->momentum().px(),(*z)->momentum().py(),(*z)->momentum().pz(),(*z)->momentum().e());
237 
238  }
239  if(lept_decay==false) {
240  TLorentzVector jetMom=tau-taunet;
241  if (sGenMatchObjectLabel_=="tau") _GenObjects.push_back(jetMom);
242  }
243  } else if(std::abs((*p)->pdg_id()) == 11&&(*p)->status()==1) {
244  TLorentzVector elec((*p)->momentum().px(),(*p)->momentum().py(),(*p)->momentum().pz(),(*p)->momentum().e());
245  if (sGenMatchObjectLabel_=="e") _GenObjects.push_back(elec);
246  }
247  }
248 
250 
251 
252  // Loop over all PFTaus
253  math::XYZPointF myleadTkEcalPos;
254  for (PFTauCollection::size_type iPFTau=0;iPFTau<pfTaus->size();iPFTau++) {
255  PFTauRef thePFTau(pfTaus,iPFTau);
256  if ((*pfTauIsoDiscr)[thePFTau] == 1) {
257  if ((*thePFTau).et() > minRecoPt_ && std::abs((*thePFTau).eta()) < maxRecoAbsEta_) {
258 
259  // Check if track goes to Ecal crack
260  reco::TrackRef myleadTk;
261  const reco::PFCandidatePtr& pflch = thePFTau->leadPFChargedHadrCand();
262  if(pflch.isNonnull()){
263  myleadTk=pflch->trackRef();
264  myleadTkEcalPos = pflch->positionAtECALEntrance();
265 
266  if(myleadTk.isNonnull()){
267  if (applyEcalCrackCut_ && isInEcalCrack(std::abs((double)myleadTkEcalPos.eta()))) {
268  continue; // do nothing
269  } else {
270 
271  // Match with gen object
272  for (unsigned int i = 0; i<_GenObjects.size();i++) {
273  if (_GenObjects[i].Et() >= minMCPt_ && std::abs(_GenObjects[i].Eta()) < maxMCAbsEta_ ) {
274  TLorentzVector pftau((*thePFTau).px(),(*thePFTau).py(),(*thePFTau).pz(),(*thePFTau).energy());
275  double GenDeltaR = pftau.DeltaR(_GenObjects[i]);
276  if (GenDeltaR<maxDeltaR_) {
277 
278  hleadTk_pt->Fill((float)myleadTk->pt());
279  hleadTk_eta->Fill((float)myleadTk->eta());
280  hleadTk_phi->Fill((float)myleadTk->phi());
281 
282  hEoverP->Fill((*thePFTau).ecalStripSumEOverPLead());
283  hHoverP->Fill((*thePFTau).hcal3x3OverPLead());
284  hEmfrac->Fill((*thePFTau).emFraction());
285 
286  if (std::abs(myleadTk->eta())<1.5) {
287  hEoverP_barrel->Fill((*thePFTau).ecalStripSumEOverPLead());
288  hHoverP_barrel->Fill((*thePFTau).hcal3x3OverPLead());
289  hEmfrac_barrel->Fill((*thePFTau).emFraction());
290  } else if (std::abs(myleadTk->eta())>1.5 && std::abs(myleadTk->eta())<2.5) {
291  hEoverP_endcap->Fill((*thePFTau).ecalStripSumEOverPLead());
292  hHoverP_endcap->Fill((*thePFTau).hcal3x3OverPLead());
293  hEmfrac_endcap->Fill((*thePFTau).emFraction());
294  }
295 
296  // if -999 fill in -1 bin!
297  if ((*thePFTau).electronPreIDOutput()<-1)
298  hElecMVA->Fill(-1);
299  else
300  hElecMVA->Fill((*thePFTau).electronPreIDOutput());
301 
302  hTauElecDiscriminant->Fill((*pfTauElecDiscr)[thePFTau]);
303 
304  hHoPvsEoP->Fill((*thePFTau).ecalStripSumEOverPLead(),(*thePFTau).hcal3x3OverPLead());
305  hEmfracvsEoP->Fill((*thePFTau).emFraction(),(*thePFTau).hcal3x3OverPLead());
306 
307  if ((*thePFTau).electronPreIDDecision()==1) {
308  hEoverP_preid1->Fill((*thePFTau).ecalStripSumEOverPLead());
309  hHoverP_preid1->Fill((*thePFTau).hcal3x3OverPLead());
310  hEmfrac_preid1->Fill((*thePFTau).emFraction());
311  hHoPvsEoP_preid1->Fill((*thePFTau).ecalStripSumEOverPLead(),(*thePFTau).hcal3x3OverPLead());
312  hEmfracvsEoP_preid1->Fill((*thePFTau).emFraction(),(*thePFTau).hcal3x3OverPLead());
313  } else {
314  hEoverP_preid0->Fill((*thePFTau).ecalStripSumEOverPLead());
315  hHoverP_preid0->Fill((*thePFTau).hcal3x3OverPLead());
316  hEmfrac_preid0->Fill((*thePFTau).emFraction());
317  hHoPvsEoP_preid0->Fill((*thePFTau).ecalStripSumEOverPLead(),(*thePFTau).hcal3x3OverPLead());
318  hEmfracvsEoP_preid0->Fill((*thePFTau).emFraction(),(*thePFTau).hcal3x3OverPLead());
319  }
320 
321 
322  }
323  }
324 
325  // Loop over all PFCands for cluster plots
326  std::vector<CandidatePtr> myPFCands=(*thePFTau).pfTauTagInfoRef()->PFCands();
327  for(int i=0;i<(int)myPFCands.size();i++){
328  const reco::PFCandidate* pfCand = dynamic_cast<const reco::PFCandidate*>(myPFCands[i].get());
329  if (pfCand == nullptr)
330  continue;
331 
332  math::XYZPointF candPos;
333  if (std::abs(pfCand->pdgId()) == 211 || std::abs(pfCand->pdgId()) == 11) // if charged hadron or electron
334  candPos = pfCand->positionAtECALEntrance();
335  else
336  candPos = math::XYZPointF(pfCand->px(),pfCand->py(),pfCand->pz());
337 
338  //double deltaR = ROOT::Math::VectorUtil::DeltaR(myleadTkEcalPos,candPos);
339  double deltaPhi = ROOT::Math::VectorUtil::DeltaPhi(myleadTkEcalPos,candPos);
340  double deltaEta = std::abs(myleadTkEcalPos.eta()-myPFCands[i]->eta());
341  double deltaPhiOverQ = deltaPhi/(double)myleadTk->charge();
342 
343  hpfcand_deltaEta->Fill(deltaEta);
344  hpfcand_deltaEta_weightE->Fill(deltaEta*pfCand->ecalEnergy());
345  hpfcand_deltaPhiOverQ->Fill(deltaPhiOverQ);
346  hpfcand_deltaPhiOverQ_weightE->Fill(deltaPhiOverQ*pfCand->ecalEnergy());
347 
348  }
349 
350  }
351 
352  }
353  }
354  }
355 
356  }
357  }
358  }
359 }
360 
361 // Ecal crack map from Egamma POG
363  return (eta < 0.018 ||
364  (eta>0.423 && eta<0.461) ||
365  (eta>0.770 && eta<0.806) ||
366  (eta>1.127 && eta<1.163) ||
367  (eta>1.460 && eta<1.558));
368 }
double ecalEnergy() const
return corrected Ecal energy
Definition: PFCandidate.h:222
int pdgId() const final
PDG identifier.
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:251
double px() const final
x coordinate of momentum vector
#define nullptr
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< float > > XYZPointF
point in space with cartesian internal representation
Definition: Point3D.h:10
uint16_t size_type
#define BOOK1D(name, title, nbinsx, lowx, highx)
const math::XYZPointF & positionAtECALEntrance() const
Definition: PFCandidate.h:368
static const double deltaEta
Definition: CaloConstants.h:8
reco::TrackRef trackRef() const
Definition: PFCandidate.cc:442
std::vector< TLorentzVector > _GenObjects
double pz() const final
z coordinate of momentum vector
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool isNonnull() const
Checks for non-null.
Definition: Ptr.h:168
#define BOOK2D(name, title, nbinsx, lowx, highx, nbinsy, lowy, highy)
#define SETAXES(name, xtitle, ytitle)
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:38
double py() const final
y coordinate of momentum vector
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
fixed size matrix
void process(edm::Handle< edm::HepMCProduct > mcevt, edm::Handle< reco::PFTauCollection > pfTaus, edm::Handle< reco::PFTauDiscriminator > pfTauIsoDiscr, edm::Handle< reco::PFTauDiscriminator > pfTauElecDiscr)
void setup(std::string Filename, std::string benchmarkLabel, double maxDeltaR, double minRecoPt, double maxRecoAbsEta, double minMCPt, double maxMCAbsEta, std::string sGenMatchObjectLabel, bool applyEcalCrackCut, DQMStore *db_store)