CMS 3D CMS Logo

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