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