CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
BaseTnPEfficiencyTask.cc
Go to the documentation of this file.
1 /*
2  * \file BaseTnPEfficiencyTask.cc
3  *
4  * \author L. Lunerti - INFN Bologna
5  *
6  */
7 
9 
10 // Framework
12 
13 // Geometry
15 
16 //Math
18 
19 //Root
20 #include "TRegexp.h"
21 
22 #include <tuple>
23 
25  : m_nEvents(0),
26  m_muToken(consumes<reco::MuonCollection>(config.getUntrackedParameter<edm::InputTag>("inputTagMuons"))),
27  m_borderCut(config.getUntrackedParameter<double>("borderCut")),
28  m_dxCut(config.getUntrackedParameter<double>("dx_cut")),
29  m_detailedAnalysis(config.getUntrackedParameter<bool>("detailedAnalysis")),
30  m_primaryVerticesToken(
31  consumes<std::vector<reco::Vertex>>(config.getUntrackedParameter<edm::InputTag>("inputTagPrimaryVertices"))),
32  m_triggerResultsToken(
33  consumes<edm::TriggerResults>(config.getUntrackedParameter<edm::InputTag>("trigResultsTag"))),
34  m_triggerEventToken(consumes<trigger::TriggerEvent>(config.getUntrackedParameter<edm::InputTag>("trigEventTag"))),
35  m_trigName(config.getUntrackedParameter<std::string>("trigName")),
36  m_probeSelector(config.getUntrackedParameter<std::string>("probeCut")),
37  m_dxyCut(config.getUntrackedParameter<double>("probeDxyCut")),
38  m_dzCut(config.getUntrackedParameter<double>("probeDzCut")),
39  m_tagSelector(config.getUntrackedParameter<std::string>("tagCut")),
40  m_lowPairMassCut(config.getUntrackedParameter<double>("lowPairMassCut")),
41  m_highPairMassCut(config.getUntrackedParameter<double>("highPairMassCut")) {
42  LogTrace("DQMOffline|MuonDPG|BaseTnPEfficiencyTask") << "[BaseTnPEfficiencyTask]: Constructor" << std::endl;
43 }
44 
46  LogTrace("DQMOffline|MuonDPG|BaseTnPEfficiencyTask")
47  << "[BaseTnPEfficiencyTask]: analyzed " << m_nEvents << " events" << std::endl;
48 }
49 
51  bool changed = true;
52  m_hltConfig.init(run, context, "HLT", changed);
53 
54  bool enableWildCard = true;
55 
56  TString tName = TString(m_trigName);
57  TRegexp tNamePattern = TRegexp(tName, enableWildCard);
58 
59  for (unsigned iPath = 0; iPath < m_hltConfig.size(); ++iPath) {
60  TString pathName = TString(m_hltConfig.triggerName(iPath));
61  if (pathName.Contains(tNamePattern)) {
62  m_trigIndices.push_back(static_cast<int>(iPath));
63  }
64  }
65 }
66 
68  edm::Run const& run,
69  edm::EventSetup const& context) {
70  LogTrace("DQMOffline|MuonDPG|BaseTnPEfficiencyTask") << "[BaseTnPEfficiencyTask]: bookHistograms" << std::endl;
71 
72  if (m_detailedAnalysis) {
73  std::string baseDir = topFolder() + "/detailed/";
74  iBooker.setCurrentFolder(baseDir);
75 
76  LogTrace("DQMOffline|MuonDPG|BaseTnPEfficiencyTask")
77  << "[BaseTnPEfficiencyTask]: booking histos in " << baseDir << std::endl;
78 
79  m_histos["probePt"] = iBooker.book1D("probePt", "probePt;probe p_{T} [GeV];Events", 125, 0., 250.);
80  m_histos["probeEta"] = iBooker.book1D("probeEta", "probeEta;probe #eta;Events", 24, -2.4, 2.4);
81  m_histos["probePhi"] = iBooker.book1D("probePhi", "probePhi;probe #phi; Events", 36, -TMath::Pi(), TMath::Pi());
82  m_histos["probeNumberOfMatchedStations"] = iBooker.book1D(
83  "probeNumberOfMatchedStations", "probeNumberOfMatchedStations;Number of matched stations;Events", 5, 0., 5);
84  m_histos["pairMass"] = iBooker.book1D("pairMass", "pairMass", 25, 70., 120.);
85  }
86 }
87 
89  ++m_nEvents;
90 
92  if (!event.getByToken(m_muToken, muons))
93  return;
94 
96  if (!event.getByToken(m_primaryVerticesToken, vtxs))
97  return;
98  const reco::Vertex& vertex = vtxs->at(0);
99 
101  if (!event.getByToken(m_triggerResultsToken, triggerResults))
102  return;
103 
105  if (!event.getByToken(m_triggerEventToken, triggerEvent))
106  return;
107 
108  //common tnp variables
109  std::vector<unsigned> preSel_tag_indices;
110  std::vector<unsigned> tag_indices;
111  std::vector<unsigned> preSel_probe_indices;
112  std::vector<unsigned> probe_indices;
113 
114  if (muons.isValid() && vtxs.isValid()) {
115  //Is there a better way to initialize two different type variables?
116  for (auto [muon, muonColl_index] = std::tuple{std::vector<reco::Muon>::const_iterator{(*muons).begin()}, 0};
117  muon != (*muons).end();
118  ++muon, ++muonColl_index) {
119  bool trigMatch = false;
120 
121  //Getting trigger infos for tag selection
122  if (triggerResults.isValid() && triggerEvent.isValid()) {
123  const trigger::TriggerObjectCollection trigObjColl = triggerEvent->getObjects();
124  trigMatch = hasTrigger(m_trigIndices, trigObjColl, triggerEvent, *muon);
125  }
126 
127  //Probe selection
128  if (m_probeSelector(*muon) && (std::abs(muon->muonBestTrack()->dxy(vertex.position())) < m_dxyCut) &&
129  (std::abs(muon->muonBestTrack()->dz(vertex.position())) < m_dzCut)) {
130  preSel_probe_indices.push_back(muonColl_index);
131  }
132  //Tag selection
133  if (m_tagSelector(*muon) && trigMatch) {
134  preSel_tag_indices.push_back(muonColl_index);
135  }
136 
137  } //loop over muons
138  }
139 
140  //Probe selection
141  for (const auto i_tag : preSel_tag_indices) {
142  reco::Muon tag = (*muons).at(i_tag);
143  float pt_max = 0.;
144  int max_pt_idx;
145  bool pair_found = false;
146 
147  for (const auto i_probe : preSel_probe_indices) {
148  //Prevent tag and probe to be the same object
149  if (i_probe == i_tag)
150  continue;
151 
152  reco::Muon preSel_probe = (*muons).at(i_probe);
153 
154  int pair_charge_product = tag.charge() * preSel_probe.charge();
155 
156  //check if tag+probe pair is compatible with Z decay
157  if (pair_charge_product > 0)
158  continue;
159 
160  float pair_mass = (tag.polarP4() + preSel_probe.polarP4()).M();
161  m_histos.find("pairMass")->second->Fill(pair_mass);
162 
163  if (pair_mass < m_lowPairMassCut || pair_mass > m_highPairMassCut)
164  continue;
165 
166  float pair_pt = (tag.polarP4() + preSel_probe.polarP4()).Pt();
167  if (pair_pt > pt_max) {
168  pair_found = true;
169  pt_max = pair_pt;
170  max_pt_idx = i_probe;
171  }
172  }
173  if (pair_found) {
174  probe_indices.push_back(max_pt_idx);
175  tag_indices.push_back(i_tag);
176  }
177  }
178 
179  m_probeIndices.push_back(probe_indices);
180  m_tagIndices.push_back(tag_indices);
181 }
182 
183 bool BaseTnPEfficiencyTask::hasTrigger(std::vector<int>& trigIndices,
186  const reco::Muon& muon) {
187  float dR2match = 999.;
188  for (int trigIdx : trigIndices) {
189  const std::vector<std::string> trigModuleLabels = m_hltConfig.moduleLabels(trigIdx);
190  const unsigned trigModuleIndex = trigModuleLabels.size() - 2;
191  const unsigned hltFilterIndex = trigEvent->filterIndex(edm::InputTag(trigModuleLabels[trigModuleIndex], "", "HLT"));
192  if (hltFilterIndex < trigEvent->sizeFilters()) {
193  const trigger::Keys keys = trigEvent->filterKeys(hltFilterIndex);
194  const trigger::Vids vids = trigEvent->filterIds(hltFilterIndex);
195  const unsigned nTriggers = vids.size();
196 
197  for (unsigned iTrig = 0; iTrig < nTriggers; ++iTrig) {
198  trigger::TriggerObject trigObj = trigObjs[keys[iTrig]];
199  float dR2 = deltaR2(muon, trigObj);
200  if (dR2 < dR2match)
201  dR2match = dR2;
202  }
203  }
204  }
205  return dR2match < 0.01;
206 }
unsigned int size() const
number of trigger paths in trigger table
const double Pi
const std::string m_trigName
virtual std::string topFolder() const =0
Return the top folder.
~BaseTnPEfficiencyTask() override
Destructor.
const std::string & triggerName(unsigned int triggerIndex) const
bool hasTrigger(std::vector< int > &trigIndices, const trigger::TriggerObjectCollection &trigObjs, edm::Handle< trigger::TriggerEvent > &trigEvent, const reco::Muon &muon)
virtual void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:32
std::vector< std::vector< unsigned > > m_tagIndices
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
const edm::EDGetTokenT< edm::TriggerResults > m_triggerResultsToken
const edm::EDGetTokenT< reco::MuonCollection > m_muToken
const Point & position() const
position
Definition: Vertex.h:127
const edm::EDGetTokenT< trigger::TriggerEvent > m_triggerEventToken
std::vector< Muon > MuonCollection
collection of Muon objects
Definition: MuonFwd.h:9
#define LogTrace(id)
const edm::EDGetTokenT< std::vector< reco::Vertex > > m_primaryVerticesToken
Single trigger physics object (e.g., an isolated muon)
Definition: TriggerObject.h:21
std::vector< std::vector< unsigned > > m_probeIndices
const StringCutObjectSelector< reco::Candidate, true > m_probeSelector
std::vector< int > m_trigIndices
const PolarLorentzVector & polarP4() const final
four-momentum Lorentz vector
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
std::map< std::string, MonitorElement * > m_histos
static std::string const triggerResults
Definition: EdmProvDump.cc:44
void analyze(const edm::Event &event, const edm::EventSetup &context) override
Analyze.
bool isValid() const
Definition: HandleBase.h:70
const StringCutObjectSelector< reco::Muon, true > m_tagSelector
const std::vector< std::string > & moduleLabels(unsigned int trigger) const
label(s) of module(s) on a trigger path
std::vector< TriggerObject > TriggerObjectCollection
collection of trigger physics objects (e.g., all isolated muons)
Definition: TriggerObject.h:75
void bookHistograms(DQMStore::IBooker &iBooker, edm::Run const &run, edm::EventSetup const &context) override
constexpr auto deltaR2(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:16
std::vector< size_type > Keys
bool init(const edm::Run &iRun, const edm::EventSetup &iSetup, const std::string &processName, bool &changed)
d&#39;tor
void dqmBeginRun(const edm::Run &run, const edm::EventSetup &context) override
BeginRun.
tuple config
parse the configuration file
tuple muons
Definition: patZpeak.py:41
HLTConfigProvider m_hltConfig
MonitorElement * book1D(TString const &name, TString const &title, int const nchX, double const lowX, double const highX, FUNC onbooking=NOOP())
Definition: DQMStore.h:98
BaseTnPEfficiencyTask(const edm::ParameterSet &config)
Constructor.
std::vector< int > Vids
Definition: Run.h:45
int charge() const final
electric charge