CMS 3D CMS Logo

L1TPhase2MuonOffline.cc
Go to the documentation of this file.
1 
9 
10 // To convert from HW to Physical coordinates
12 
13 using namespace reco;
14 using namespace edm;
15 using namespace std;
16 using namespace l1t;
17 
18 //__________RECO-GMT Muon Pair Helper Class____________________________
20  : mu_(muon), gmtmu_(gmtmu) {
21  if (gmtmu) {
22  gmtEta_ = gmtmu_->eta();
23  gmtPhi_ = gmtmu_->phi();
24  } else {
25  gmtEta_ = -5.;
26  gmtPhi_ = -5.;
27  }
28  if (mu_) {
29  muEta_ = mu_->eta();
30  muPhi_ = mu_->phi();
31  } else {
32  muEta_ = 999.;
33  muPhi_ = 999.;
34  }
35 };
36 
38  mu_ = muonGmtPair.mu_;
39  gmtmu_ = muonGmtPair.gmtmu_;
40 
41  gmtEta_ = muonGmtPair.gmtEta_;
42  gmtPhi_ = muonGmtPair.gmtPhi_;
43 
44  muEta_ = muonGmtPair.muEta_;
45  muPhi_ = muonGmtPair.muPhi_;
46 }
47 
49  if (!gmtmu_)
50  return 999.;
51  float dEta = gmtEta_ - muEta_;
53  return dEta * dEta + dPhi * dPhi;
54 }
55 
57  if (std::abs(muEta_) < 0.83)
59  if (std::abs(muEta_) < 1.24)
61  if (std::abs(muEta_) < 2.4)
64 }
65 
68  return (gmtPt() - pt()) / pt();
70  return (pt() - gmtPt()) / gmtPt(); // (1/gmtPt - 1/pt) / (1/pt)
72  return (gmtCharge() * charge() * pt() - gmtPt()) /
73  gmtPt(); // (gmtCharge/gmtPt - charge/pt) / (charge/pt) with gmtCharge/charge = gmtCharge*charge
75  return reco::deltaPhi(gmtPhi(), muPhi_);
77  return gmtEta() - muEta_;
79  return gmtCharge() - charge();
80  return -999.;
81 }
82 
85  return pt();
87  return muPhi_;
89  return muEta_;
90  return -999.;
91 }
92 
93 //__________DQM_base_class_______________________________________________
95  : gmtMuonToken_(consumes<l1t::SAMuonCollection>(ps.getParameter<edm::InputTag>("gmtMuonToken"))),
96  gmtTkMuonToken_(consumes<l1t::TrackerMuonCollection>(ps.getParameter<edm::InputTag>("gmtTkMuonToken"))),
97  genParticleToken_(
98  consumes<std::vector<reco::GenParticle>>(ps.getUntrackedParameter<edm::InputTag>("genParticlesInputTag"))),
99  muonTypes_({kSAMuon, kTkMuon}),
100  effTypes_({kEffPt, kEffPhi, kEffEta}),
101  resTypes_({kResPt, kResQOverPt, kResPhi, kResEta}),
102  etaRegions_({kEtaRegionAll, kEtaRegionBmtf, kEtaRegionOmtf, kEtaRegionEmtf}),
103  qualLevels_({kQualOpen, kQualDouble, kQualSingle}),
104  resNames_({{kResPt, "pt"},
105  {kRes1OverPt, "1overpt"},
106  {kResQOverPt, "qoverpt"},
107  {kResPhi, "phi"},
108  {kResEta, "eta"},
109  {kResCh, "charge"}}),
110  resLabels_({{kResPt, "(p_{T}^{L1} - p_{T}^{reco})/p_{T}^{reco}"},
111  {kRes1OverPt, "(p_{T}^{reco} - p_{T}^{L1})/p_{T}^{L1}"},
112  {kResQOverPt, "(q^{L1}*q^{reco}*p_{T}^{reco} - p_{T}^{L1})/p_{T}^{L1}"},
113  {kResPhi, "#phi_{L1} - #phi_{reco}"},
114  {kResEta, "#eta_{L1} - #eta_{reco}"},
115  {kResCh, "charge^{L1} - charge^{reco}"}}),
116  etaNames_({{kEtaRegionAll, "etaMin0_etaMax2p4"},
117  {kEtaRegionBmtf, "etaMin0_etaMax0p83"},
118  {kEtaRegionOmtf, "etaMin0p83_etaMax1p24"},
119  {kEtaRegionEmtf, "etaMin1p24_etaMax2p4"}}),
120  qualNames_({{kQualOpen, "qualOpen"}, {kQualDouble, "qualDouble"}, {kQualSingle, "qualSingle"}}),
121  muonNames_({{kSAMuon, "SAMuon"}, {kTkMuon, "TkMuon"}}),
122  histFolder_(ps.getUntrackedParameter<string>("histFolder")),
123  cutsVPSet_(ps.getUntrackedParameter<std::vector<edm::ParameterSet>>("cuts")),
124  effVsPtBins_(ps.getUntrackedParameter<std::vector<double>>("efficiencyVsPtBins")),
125  effVsPhiBins_(ps.getUntrackedParameter<std::vector<double>>("efficiencyVsPhiBins")),
126  effVsEtaBins_(ps.getUntrackedParameter<std::vector<double>>("efficiencyVsEtaBins")),
127  maxGmtMuonDR_(ps.getUntrackedParameter<double>("maxDR")) {
128  edm::LogInfo("L1TPhase2MuonOffline") << "L1TPhase2MuonOffline::L1TPhase2MuonOffline()" << endl;
129 
130  for (const auto& c : cutsVPSet_) {
131  const auto qCut = c.getUntrackedParameter<int>("qualCut");
132  QualLevel qLevel = kQualOpen;
133  if (qCut > 11) {
134  qLevel = kQualSingle;
135  } else if (qCut > 7) {
136  qLevel = kQualDouble;
137  } else if (qCut > 3) {
138  qLevel = kQualOpen;
139  }
140  cuts_.emplace_back(std::make_pair(c.getUntrackedParameter<int>("ptCut"), qLevel));
141  }
142 }
143 
144 //----------------------------------------------------------------------
146  edm::LogInfo("L1TPhase2MuonOFfline") << "L1TPhase2MuonOffline::dqmBeginRun" << endl;
147 }
148 
149 //_____________________________________________________________________
151  const edm::Run& run,
152  const edm::EventSetup& iSetup) {
153  edm::LogInfo("L1TPhase2MuonOFfline") << "L1TPhase2MuonOffline::bookHistograms" << endl;
154 
155  //book histos
156  for (const auto mutype : muonTypes_) {
157  bookControlHistos(ibooker, mutype);
158  bookEfficiencyHistos(ibooker, mutype);
159  bookResolutionHistos(ibooker, mutype);
160  }
161 }
162 
163 //_____________________________________________________________________
165  edm::LogInfo("L1TPhase2MuonOffline") << "L1TPhase2MuonOffline::analyze() " << endl;
166 
167  // COLLECT GEN MUONS
169 
170  std::vector<const reco::GenParticle*> genmus;
171  for (const reco::GenParticle& gen : *genparticles_) {
172  if (std::abs(gen.pdgId()) != 13)
173  continue;
174  genmus.push_back(&gen);
175  }
176  edm::LogInfo("L1TPhase2MuonOffline") << "L1TPhase2MuonOffline::analyze() N of genmus: " << genmus.size() << endl;
177 
178  // Collect both muon collection:
179  iEvent.getByToken(gmtMuonToken_, gmtSAMuon_);
180  iEvent.getByToken(gmtTkMuonToken_, gmtTkMuon_);
181 
182  // Fill Control histograms
183  edm::LogInfo("L1TPhase2MuonOffline") << "Fill Control histograms for GMT Muons" << endl;
185 
186  // Match each muon to a gen muon, if possible.
187  if (genmus.empty())
188  return;
189  edm::LogInfo("L1TPhase2MuonOffline") << "L1TPhase2MuonOffline::analyze() calling matchMuonsToGen() " << endl;
190  matchMuonsToGen(genmus);
191 
192  // Fill efficiency and resolution once, matching has been done...
195  edm::LogInfo("L1TPhase2MuonOffline") << "L1TPhase2MuonOffline::analyze() Computation finished" << endl;
196 }
197 
198 //_____________________________________________________________________
200  edm::LogInfo("L1TPhase2MuonOffline") << "L1TPhase2MuonOffline::bookControlHistos()" << endl;
201 
202  ibooker.setCurrentFolder(histFolder_ + "/" + muonNames_[mutype] + "/control_variables");
203 
204  controlHistos_[mutype][kPt] = ibooker.book1D(muonNames_[mutype] + "Pt", "MuonPt; p_{T}", 50, 0., 100.);
205  controlHistos_[mutype][kPhi] = ibooker.book1D(muonNames_[mutype] + "Phi", "MuonPhi; #phi", 66, -3.3, 3.3);
206  controlHistos_[mutype][kEta] = ibooker.book1D(muonNames_[mutype] + "Eta", "MuonEta; #eta", 50, -2.5, 2.5);
207  controlHistos_[mutype][kIso] = ibooker.book1D(muonNames_[mutype] + "Iso", "MuonIso; RelIso", 50, 0, 1.0);
208  controlHistos_[mutype][kQual] = ibooker.book1D(muonNames_[mutype] + "Qual", "MuonQual; Quality", 15, 0.5, 15.5);
209  controlHistos_[mutype][kZ0] = ibooker.book1D(muonNames_[mutype] + "Z0", "MuonZ0; Z_{0}", 50, 0, 50.0);
210  controlHistos_[mutype][kD0] = ibooker.book1D(muonNames_[mutype] + "D0", "MuonD0; D_{0}", 50, 0, 200.);
211 }
212 
214  edm::LogInfo("L1TPhase2MuonOffline") << "L1TPhase2MuonOffline::bookEfficiencyHistos()" << endl;
215 
216  ibooker.setCurrentFolder(histFolder_ + "/" + muonNames_[mutype] + "/nums_and_dens");
217 
218  std::string histoname = "";
219  for (const auto eta : etaRegions_) {
220  for (const auto q : qualLevels_) {
221  histoname = "Eff_" + muonNames_[mutype] + "_" + etaNames_[eta] + "_" + qualNames_[q];
222 
223  auto histBins = getHistBinsEff(kEffPt);
224  efficiencyNum_[mutype][eta][q][kEffPt] =
225  ibooker.book1D(histoname + "_Pt_Num", "MuonPt; p_{T} ;", histBins.size() - 1, &histBins[0]);
226  efficiencyDen_[mutype][eta][q][kEffPt] =
227  ibooker.book1D(histoname + "_Pt_Den", "MuonPt; p_{T} ;", histBins.size() - 1, &histBins[0]);
228 
229  histBins = getHistBinsEff(kEffEta);
230  efficiencyNum_[mutype][eta][q][kEffEta] =
231  ibooker.book1D(histoname + "_Eta_Num", "MuonEta; #eta ;", histBins.size() - 1, &histBins[0]);
232  efficiencyDen_[mutype][eta][q][kEffEta] =
233  ibooker.book1D(histoname + "_Eta_Den", "MuonEta; #eta ;", histBins.size() - 1, &histBins[0]);
234 
235  histBins = getHistBinsEff(kEffPhi);
236  efficiencyNum_[mutype][eta][q][kEffPhi] =
237  ibooker.book1D(histoname + "_Phi_Num", "MuonPhi; #phi ;", histBins.size() - 1, &histBins[0]);
238  efficiencyDen_[mutype][eta][q][kEffPhi] =
239  ibooker.book1D(histoname + "_Phi_Den", "MuonPhi; #phi ;", histBins.size() - 1, &histBins[0]);
240  }
241  }
242 }
243 
245  edm::LogInfo("L1TPhase2MuonOffline") << "L1TPhase2MuonOffline::bookResolutionHistos()" << endl;
246 
247  ibooker.setCurrentFolder(histFolder_ + "/" + muonNames_[mutype] + "/resolution");
248  std::string histoname = "";
249  for (const auto eta : etaRegions_) {
250  for (const auto q : qualLevels_) {
251  for (const auto var : resTypes_) {
252  histoname = "Res_" + muonNames_[mutype] + "_" + etaNames_[eta] + "_" + qualNames_[q] + "_" + resNames_[var];
253  auto nbins = std::get<0>(getHistBinsRes(var));
254  auto xmin = std::get<1>(getHistBinsRes(var));
255  auto xmax = std::get<2>(getHistBinsRes(var));
256  resolutionHistos_[mutype][eta][q][var] =
257  ibooker.book1D(histoname, resNames_[var] + ";" + resLabels_[var], nbins, xmin, xmax);
258  }
259  }
260  }
261 }
262 
263 //____________________________________________________________________
265  for (auto& muIt : *gmtSAMuon_) {
266  controlHistos_[kSAMuon][kPt]->Fill(lsb_pt * muIt.hwPt());
267  controlHistos_[kSAMuon][kPhi]->Fill(lsb_phi * muIt.hwPhi());
268  controlHistos_[kSAMuon][kEta]->Fill(lsb_eta * muIt.hwEta());
269  controlHistos_[kSAMuon][kIso]->Fill(muIt.hwIso());
270  controlHistos_[kSAMuon][kQual]->Fill(muIt.hwQual());
271  controlHistos_[kSAMuon][kZ0]->Fill(lsb_z0 * muIt.hwZ0());
272  controlHistos_[kSAMuon][kD0]->Fill(lsb_d0 * muIt.hwD0());
273  }
274 
275  for (auto& muIt : *gmtTkMuon_) {
276  controlHistos_[kTkMuon][kPt]->Fill(lsb_pt * muIt.hwPt());
277  controlHistos_[kTkMuon][kPhi]->Fill(lsb_phi * muIt.hwPhi());
278  controlHistos_[kTkMuon][kEta]->Fill(lsb_eta * muIt.hwEta());
279  controlHistos_[kTkMuon][kIso]->Fill(muIt.hwIso());
280  controlHistos_[kTkMuon][kQual]->Fill(muIt.hwQual());
281  controlHistos_[kTkMuon][kZ0]->Fill(lsb_z0 * muIt.hwZ0());
282  controlHistos_[kTkMuon][kD0]->Fill(lsb_d0 * muIt.hwD0());
283  }
284 }
285 
287  for (const auto& muIt : gmtSAMuonPairs_) {
288  auto eta = muIt.etaRegion();
289  for (const auto var : effTypes_) {
290  auto varToFill = muIt.getVar(var);
291  for (const auto& cut : cuts_) {
292  const auto q = cut.second;
293  efficiencyDen_[kSAMuon][eta][q][var]->Fill(varToFill);
294  if (muIt.gmtPt() < 0)
295  continue; // there is not an assciated gmt muon
296  if (muIt.gmtQual() < q * 4)
297  continue; //quality requirements
298  const auto gmtPtCut = cut.first;
299  if (var != kEffPt && muIt.gmtPt() < gmtPtCut)
300  continue; // pt requirement
301  efficiencyNum_[kSAMuon][eta][q][var]->Fill(varToFill);
302  }
303  }
304  }
305 
307  for (const auto& muIt : gmtTkMuonPairs_) {
308  auto eta = muIt.etaRegion();
309  for (const auto var : effTypes_) {
310  auto varToFill = muIt.getVar(var);
311  for (const auto& cut : cuts_) {
312  const auto q = cut.second;
313  efficiencyDen_[kTkMuon][eta][q][var]->Fill(varToFill);
314  if (muIt.gmtPt() < 0)
315  continue; // there is not an assciated gmt muon
316  if (muIt.gmtQual() < q * 4)
317  continue; //quality requirements
318  const auto gmtPtCut = cut.first;
319  if (var != kEffPt && muIt.gmtPt() < gmtPtCut)
320  continue; // pt requirement
321  efficiencyNum_[kTkMuon][eta][q][var]->Fill(varToFill);
322  }
323  }
324  }
325 }
326 
328  for (const auto& muIt : gmtSAMuonPairs_) {
329  if (muIt.gmtPt() < 0)
330  continue;
331 
332  auto eta = muIt.etaRegion();
333  for (const auto q : qualLevels_) {
334  if (muIt.gmtQual() < q * 4)
335  continue;
336  for (const auto var : resTypes_) {
337  auto varToFill = muIt.getDeltaVar(var);
338 
339  resolutionHistos_[kSAMuon][eta][q][var]->Fill(varToFill);
340  }
341  }
342  }
343 
344  for (const auto& muIt : gmtTkMuonPairs_) {
345  if (muIt.gmtPt() < 0)
346  continue;
347 
348  auto eta = muIt.etaRegion();
349  for (const auto q : qualLevels_) {
350  if (muIt.gmtQual() < q * 4)
351  continue;
352  for (const auto var : resTypes_) {
353  auto varToFill = muIt.getDeltaVar(var);
354 
355  resolutionHistos_[kTkMuon][eta][q][var]->Fill(varToFill);
356  }
357  }
358  }
359 }
360 
361 //_____________________________________________________________________
362 void L1TPhase2MuonOffline::matchMuonsToGen(std::vector<const reco::GenParticle*> genmus) {
363  gmtSAMuonPairs_.clear();
364  gmtTkMuonPairs_.clear();
365 
366  edm::LogInfo("L1TPhase2MuonOffline") << "L1TPhase2MuonOffline::matchMuonsToGen() " << endl;
367 
368  for (const reco::GenParticle* gen : genmus) {
369  edm::LogInfo("L1TPhase2MuonOffline") << "Looping on genmus: " << gen << endl;
370  GenMuonGMTPair pairBestCand(&(*gen), nullptr);
371  float dr2Best = maxGmtMuonDR_ * maxGmtMuonDR_;
372  for (auto& muIt : *gmtSAMuon_) {
373  GenMuonGMTPair pairTmpCand(&(*gen), &(muIt));
374  float dr2Tmp = pairTmpCand.dR2();
375  if (dr2Tmp < dr2Best) {
376  dr2Best = dr2Tmp;
377  pairBestCand = pairTmpCand;
378  }
379  }
380  gmtSAMuonPairs_.emplace_back(pairBestCand);
381 
382  GenMuonGMTPair pairBestCand2(&(*gen), nullptr);
383  dr2Best = maxGmtMuonDR_ * maxGmtMuonDR_;
384  for (auto& tkmuIt : *gmtTkMuon_) {
385  GenMuonGMTPair pairTmpCand(&(*gen), &(tkmuIt));
386  float dr2Tmp = pairTmpCand.dR2();
387  if (dr2Tmp < dr2Best) {
388  dr2Best = dr2Tmp;
389  pairBestCand2 = pairTmpCand;
390  }
391  }
392  gmtTkMuonPairs_.emplace_back(pairBestCand2);
393  }
394  edm::LogInfo("L1TPhase2MuonOffline") << "L1TPhase2MuonOffline::matchMuonsToGen() gmtSAMuons: "
395  << gmtSAMuonPairs_.size() << endl;
396  edm::LogInfo("L1TPhase2MuonOffline") << "L1TPhase2MuonOffline::matchMuonsToGen() gmtTkMuons: "
397  << gmtTkMuonPairs_.size() << endl;
398  edm::LogInfo("L1TPhase2MuonOffline") << "L1TPhase2MuonOffline::matchMuonsToGen() END " << endl;
399 }
400 
402  if (eff == kEffPt) {
403  std::vector<float> effVsPtBins(effVsPtBins_.begin(), effVsPtBins_.end());
404  return effVsPtBins;
405  }
406  if (eff == kEffPhi) {
407  std::vector<float> effVsPhiBins(effVsPhiBins_.begin(), effVsPhiBins_.end());
408  return effVsPhiBins;
409  }
410  if (eff == kEffEta) {
411  std::vector<float> effVsEtaBins(effVsEtaBins_.begin(), effVsEtaBins_.end());
412  return effVsEtaBins;
413  }
414  return {0., 1.};
415 }
416 
417 std::tuple<int, double, double> L1TPhase2MuonOffline::getHistBinsRes(ResType res) {
418  if (res == kResPt)
419  return {50, -2., 2.};
420  if (res == kRes1OverPt)
421  return {50, -2., 2.};
422  if (res == kResQOverPt)
423  return {50, -2., 2.};
424  if (res == kResPhi)
425  return {96, -0.2, 0.2};
426  if (res == kResEta)
427  return {100, -0.1, 0.1};
428  if (res == kResCh)
429  return {5, -2, 3};
430  return {1, 0, 1};
431 }
432 
433 //define this as a plug-in
const std::vector< double > effVsPtBins_
edm::EDGetTokenT< l1t::SAMuonCollection > gmtMuonToken_
constexpr double deltaPhi(double phi1, double phi2)
Definition: deltaPhi.h:26
std::vector< GenMuonGMTPair > gmtSAMuonPairs_
std::map< MuType, std::string > muonNames_
const std::vector< EtaRegion > etaRegions_
edm::Handle< l1t::SAMuonCollection > gmtSAMuon_
void matchMuonsToGen(std::vector< const reco::GenParticle *> genmus)
virtual void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:36
const std::vector< MuType > muonTypes_
float gmtEta() const
const l1t::L1Candidate * gmtmu_
MonitorElement * efficiencyDen_[kNMuTypes][kNEtaRegions][kNQualLevels][kEffTypes]
const std::vector< QualLevel > qualLevels_
std::map< ResType, std::string > resNames_
delete x;
Definition: CaloConfig.h:22
std::tuple< int, double, double > getHistBinsRes(ResType res)
Definition: Electron.h:6
std::vector< std::pair< int, QualLevel > > cuts_
std::map< ResType, std::string > resLabels_
void Fill(long long x)
const std::vector< double > effVsEtaBins_
int iEvent
Definition: GenABIO.cc:224
MonitorElement * controlHistos_[kNMuTypes][kNVarTypes]
const reco::GenParticle * mu_
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::Handle< std::vector< reco::GenParticle > > genparticles_
L1TPhase2MuonOffline(const edm::ParameterSet &ps)
float gmtPhi() const
std::map< EtaRegion, std::string > etaNames_
void bookControlHistos(DQMStore::IBooker &, MuType type)
MonitorElement * efficiencyNum_[kNMuTypes][kNEtaRegions][kNQualLevels][kEffTypes]
std::vector< float > getHistBinsEff(EffType eff)
Log< level::Info, false > LogInfo
void bookHistograms(DQMStore::IBooker &ibooker, const edm::Run &run, const edm::EventSetup &iSetup) override
std::vector< TrackerMuon > TrackerMuonCollection
Definition: TrackerMuon.h:17
std::vector< SAMuon > SAMuonCollection
Definition: SAMuon.h:15
double getVar(const L1TPhase2MuonOffline::EffType) const
const std::vector< ResType > resTypes_
const std::string histFolder_
void bookResolutionHistos(DQMStore::IBooker &ibooker, MuType type)
MonitorElement * resolutionHistos_[kNMuTypes][kNEtaRegions][kNQualLevels][kNResTypes]
fixed size matrix
HLT enums.
double getDeltaVar(const L1TPhase2MuonOffline::ResType) const
const std::vector< double > effVsPhiBins_
const std::vector< EffType > effTypes_
void analyze(const edm::Event &e, const edm::EventSetup &c) override
std::map< QualLevel, std::string > qualNames_
GenMuonGMTPair(const reco::GenParticle *mu, const l1t::L1Candidate *gmtmu)
L1TPhase2MuonOffline::EtaRegion etaRegion() const
void dqmBeginRun(const edm::Run &run, const edm::EventSetup &iSetup) override
edm::Handle< l1t::TrackerMuonCollection > gmtTkMuon_
MonitorElement * book1D(TString const &name, TString const &title, int const nchX, double const lowX, double const highX, FUNC onbooking=NOOP())
Definition: DQMStore.h:98
edm::EDGetTokenT< std::vector< reco::GenParticle > > genParticleToken_
double phi() const final
momentum azimuthal angle
edm::EDGetTokenT< l1t::TrackerMuonCollection > gmtTkMuonToken_
std::vector< GenMuonGMTPair > gmtTkMuonPairs_
Definition: Run.h:45
void bookEfficiencyHistos(DQMStore::IBooker &ibooker, MuType type)
double eta() const final
momentum pseudorapidity