CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
L1MuonMatcher.cc
Go to the documentation of this file.
1 //
2 //
3 
15 
18 
23 
25 
27 
29 
30 namespace pat {
31 
33  public:
34  explicit L1MuonMatcher(const edm::ParameterSet &iConfig);
35  ~L1MuonMatcher() override {}
36 
37  void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override;
38 
39  private:
43 
45 
50 
53 
56 
59 
61  int firstBX_;
62  int lastBX_;
63 
65  template <typename Hand, typename T>
67  const Hand &handle,
68  const std::vector<T> &values,
69  const std::string &label) const;
70  };
71 
72 } // namespace pat
73 
75  : matcher_(iConfig, consumesCollector()),
76  recoToken_(consumes<edm::View<reco::Candidate> >(iConfig.getParameter<edm::InputTag>("src"))),
77  labelL1_(iConfig.getParameter<std::string>("setL1Label")),
78  labelProp_(iConfig.getParameter<std::string>("setPropLabel")),
79  writeExtraInfo_(iConfig.getParameter<bool>("writeExtraInfo")),
80  useStage2L1_(iConfig.getParameter<bool>("useStage2L1")),
81  firstBX_(iConfig.getParameter<int>("firstBX")),
82  lastBX_(iConfig.getParameter<int>("lastBX")) {
83  if (useStage2L1_) {
84  l1tToken_ = consumes<l1t::MuonBxCollection>(iConfig.getParameter<edm::InputTag>("matched"));
85  } else {
86  l1Token_ = consumes<std::vector<l1extra::L1MuonParticle> >(iConfig.getParameter<edm::InputTag>("matched"));
87  }
88  produces<PATPrimitiveCollection>("l1muons"); // l1 in PAT format
89  produces<PATPrimitiveCollection>("propagatedReco"); // reco to muon station 2
90  produces<PATTriggerAssociation>("propagatedReco"); // asso reco to propagated reco
91  produces<PATTriggerAssociation>(); // asso reco to l1
92  if (writeExtraInfo_) {
93  produces<edm::ValueMap<float> >("deltaR");
94  produces<edm::ValueMap<float> >("deltaPhi");
95  produces<edm::ValueMap<int> >("quality");
96  produces<edm::ValueMap<int> >("bx");
97  if (useStage2L1_) {
98  produces<edm::ValueMap<int> >("iPhi");
99  produces<edm::ValueMap<int> >("tfIndex");
100  }
101  produces<edm::ValueMap<int> >("isolated");
102  produces<edm::ValueMap<reco::CandidatePtr> >();
103  produces<edm::ValueMap<reco::CandidatePtr> >("l1ToReco");
104  }
105 }
106 
108  using namespace edm;
109  using namespace std;
110 
111  matcher_.init(iSetup);
112 
116 
117  std::vector<l1t::Muon> l1ts;
118  std::vector<size_t> bxIdxs;
119 
120  int minBxIdx = 0;
121  size_t l1size = 0;
122 
123  iEvent.getByToken(recoToken_, reco);
124 
125  if (useStage2L1_) {
126  iEvent.getByToken(l1tToken_, l1tBX);
127  l1size = l1tBX->size();
128 
129  int minBX = max(firstBX_, l1tBX->getFirstBX());
130  int maxBX = min(lastBX_, l1tBX->getLastBX());
131 
132  minBxIdx = l1tBX->begin(minBX) - l1tBX->begin();
133  std::copy(l1tBX->begin(minBX), l1tBX->end(maxBX), std::back_inserter(l1ts));
134 
135  for (int ibx = l1tBX->getFirstBX(); ibx <= l1tBX->getLastBX(); ++ibx) {
136  bxIdxs.push_back(l1tBX->end(ibx) - l1tBX->begin());
137  }
138  } else {
139  iEvent.getByToken(l1Token_, l1s);
140  l1size = l1s->size();
141  }
142 
143  unique_ptr<PATPrimitiveCollection> propOut(new PATPrimitiveCollection());
144  unique_ptr<PATPrimitiveCollection> l1Out(new PATPrimitiveCollection());
145  std::vector<edm::Ptr<reco::Candidate> > l1rawMatches(reco->size());
146  vector<int> isSelected(l1size, -1);
147  std::vector<edm::Ptr<reco::Candidate> > whichRecoMatch(l1size);
148  vector<int> propMatches(reco->size(), -1);
149  vector<int> fullMatches(reco->size(), -1);
150  vector<float> deltaRs(reco->size(), 999), deltaPhis(reco->size(), 999);
151  vector<int> quality(reco->size(), 0), bx(reco->size(), -999), isolated(reco->size(), -999);
152  vector<int> iPhi(reco->size(), 0), tfIndex(reco->size(), -999);
153  for (int i = 0, n = reco->size(); i < n; ++i) {
154  TrajectoryStateOnSurface propagated;
155  const reco::Candidate &mu = (*reco)[i];
156  int match = useStage2L1_ ? matcher_.match(mu, l1ts, deltaRs[i], deltaPhis[i], propagated)
157  : matcher_.match(mu, *l1s, deltaRs[i], deltaPhis[i], propagated);
158  if (propagated.isValid()) {
159  GlobalPoint pos = propagated.globalPosition();
160  propMatches[i] = propOut->size();
161  propOut->push_back(PATPrimitive(math::PtEtaPhiMLorentzVector(mu.pt(), pos.eta(), pos.phi(), mu.mass())));
162  propOut->back().addFilterLabel(labelProp_);
163  propOut->back().setCharge(mu.charge());
164  }
165  if (match != -1) {
166  if (useStage2L1_) {
167  match += minBxIdx;
168  }
169 
170  whichRecoMatch[match] = reco->ptrAt(i);
171 
172  int charge = 0;
174 
175  if (useStage2L1_) {
176  const l1t::Muon &l1t = (*l1tBX)[match];
177  charge = l1t.charge();
178  p4 = l1t.polarP4();
179  } else {
180  const l1extra::L1MuonParticle &l1 = (*l1s)[match];
181  charge = l1.charge();
182  p4 = l1.polarP4();
183  }
184 
185  if (isSelected[match] == -1) { // copy to output if needed
186  isSelected[match] = l1Out->size();
187  l1Out->push_back(PATPrimitive(p4));
188  l1Out->back().addFilterLabel(labelL1_);
189  l1Out->back().setCharge(charge);
190  }
191 
192  fullMatches[i] = isSelected[match]; // index in the output collection
193 
194  if (useStage2L1_) {
195  const l1t::Muon &l1t = (*l1tBX)[match];
196  quality[i] = l1t.hwQual();
197  bx[i] = l1tBX->getFirstBX() + (std::upper_bound(bxIdxs.begin(), bxIdxs.end(), match) - bxIdxs.begin());
198  isolated[i] = l1t.hwIso();
199  l1rawMatches[i] = edm::Ptr<reco::Candidate>(l1tBX, size_t(match));
200  iPhi[i] = l1t.hwPhi();
201  tfIndex[i] = l1t.tfMuonIndex();
202  } else {
203  const L1MuGMTCand &gmt = (*l1s)[match].gmtMuonCand();
204  quality[i] = gmt.quality();
205  bx[i] = gmt.bx();
206  isolated[i] = gmt.isol();
207  l1rawMatches[i] = edm::Ptr<reco::Candidate>(l1s, size_t(match));
208  }
209  }
210  }
211 
212  OrphanHandle<PATPrimitiveCollection> l1Done = iEvent.put(std::move(l1Out), "l1muons");
213  OrphanHandle<PATPrimitiveCollection> propDone = iEvent.put(std::move(propOut), "propagatedReco");
214 
215  unique_ptr<PATTriggerAssociation> propAss(new PATTriggerAssociation(propDone));
216  PATTriggerAssociation::Filler propFiller(*propAss);
217  propFiller.insert(reco, propMatches.begin(), propMatches.end());
218  propFiller.fill();
219  iEvent.put(std::move(propAss), "propagatedReco");
220 
221  unique_ptr<PATTriggerAssociation> fullAss(new PATTriggerAssociation(l1Done));
222  PATTriggerAssociation::Filler fullFiller(*fullAss);
223  fullFiller.insert(reco, fullMatches.begin(), fullMatches.end());
224  fullFiller.fill();
225  iEvent.put(std::move(fullAss));
226 
227  if (writeExtraInfo_) {
228  storeExtraInfo(iEvent, reco, deltaRs, "deltaR");
229  storeExtraInfo(iEvent, reco, deltaPhis, "deltaPhi");
230  storeExtraInfo(iEvent, reco, bx, "bx");
231  storeExtraInfo(iEvent, reco, isolated, "isolated");
232  storeExtraInfo(iEvent, reco, quality, "quality");
233  storeExtraInfo(iEvent, reco, l1rawMatches, "");
234  if (useStage2L1_) {
235  storeExtraInfo(iEvent, l1tBX, whichRecoMatch, "l1ToReco");
236  storeExtraInfo(iEvent, reco, tfIndex, "tfIndex");
237  storeExtraInfo(iEvent, reco, iPhi, "iPhi");
238  } else {
239  storeExtraInfo(iEvent, l1s, whichRecoMatch, "l1ToReco");
240  }
241  }
242 }
243 
244 template <typename Hand, typename T>
246  const Hand &handle,
247  const std::vector<T> &values,
248  const std::string &label) const {
249  using namespace edm;
250  using namespace std;
251  unique_ptr<ValueMap<T> > valMap(new ValueMap<T>());
252  typename edm::ValueMap<T>::Filler filler(*valMap);
253  filler.insert(handle, values.begin(), values.end());
254  filler.fill();
255  iEvent.put(std::move(valMap), label);
256 }
257 
259 using namespace pat;
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
std::vector< TriggerObjectStandAlone > TriggerObjectStandAloneCollection
Collection of TriggerObjectStandAlone.
bool writeExtraInfo_
Write out additional info as ValueMaps.
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
virtual double pt() const =0
transverse momentum
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void storeExtraInfo(edm::Event &iEvent, const Hand &handle, const std::vector< T > &values, const std::string &label) const
Store extra information in a ValueMap.
virtual double mass() const =0
mass
Geom::Phi< T > phi() const
Definition: PV3DBase.h:66
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
L1MuonMatcher(const edm::ParameterSet &iConfig)
__host__ __device__ constexpr RandomIt upper_bound(RandomIt first, RandomIt last, const T &value, Compare comp={})
GlobalPoint globalPosition() const
pat::TriggerObjectStandAloneCollection PATPrimitiveCollection
int hwPhi() const
Definition: L1Candidate.h:37
string quality
L1MuonMatcherAlgo matcher_
pat::TriggerObjectStandAlone PATPrimitive
bool isol() const
get isolation
Definition: L1MuGMTCand.h:111
PtEtaPhiMLorentzVectorD PtEtaPhiMLorentzVector
Lorentz vector with cartesian internal representation.
Definition: LorentzVector.h:25
edm::EDGetTokenT< std::vector< l1extra::L1MuonParticle > > l1Token_
int hwIso() const
Definition: L1Candidate.h:39
char const * label
std::string labelL1_
Labels to set as filter names in the output.
int iEvent
Definition: GenABIO.cc:224
int bx() const
get bunch crossing identifier
Definition: L1MuGMTCand.h:117
pat::TriggerObjectStandAloneMatch PATTriggerAssociation
const PolarLorentzVector & polarP4() const final
four-momentum Lorentz vector
def move
Definition: eostools.py:511
edm::EDGetTokenT< l1t::MuonBxCollection > l1tToken_
tuple handle
Definition: patZpeak.py:25
Matcher of reconstructed objects to L1 Muons.
virtual int charge() const =0
electric charge
const int mu
Definition: Constants.h:22
bool useStage2L1_
Allow to run both on legacy or stage2 (2016) L1 Muon trigger output.
int hwQual() const
Definition: L1Candidate.h:38
Definition: Muon.h:21
~L1MuonMatcher() override
unsigned int quality() const
get quality
Definition: L1MuGMTCand.h:90
int tfMuonIndex() const
Definition: Muon.h:98
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
int firstBX_
Skim stage2 BX vector.
bool isSelected(const std::vector< L1HPSPFTauQualityCut > &qualityCuts, const l1t::PFCandidate &pfCand, float_t primaryVertexZ)
edm::EDGetTokenT< edm::View< reco::Candidate > > recoToken_
Tokens for input collections.
T eta() const
Definition: PV3DBase.h:73
std::string labelProp_
Matcher of reconstructed objects to L1 Muons.
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
int charge() const final
electric charge
Analysis-level trigger object class (stand-alone)