CMS 3D CMS Logo

PATMuonProducer.cc
Go to the documentation of this file.
1 //
2 //
3 
5 
9 
13 
15 
19 
22 
24 
27 
31 
36 
37 #include "TMath.h"
38 
40 
45 
50 
53 
54 #include <vector>
55 #include <memory>
56 
57 
58 using namespace pat;
59 using namespace std;
60 
62 
63  if (iConfig.getParameter<bool>("computeMuonMVA")) {
64  edm::FileInPath mvaTrainingFile = iConfig.getParameter<edm::FileInPath>("mvaTrainingFile");
65  edm::FileInPath mvaLowPtTrainingFile = iConfig.getParameter<edm::FileInPath>("lowPtmvaTrainingFile");
66  float mvaDrMax = iConfig.getParameter<double>("mvaDrMax");
67  muonMvaEstimator_ = std::make_unique<MuonMvaEstimator>(mvaTrainingFile, mvaDrMax);
68  muonLowPtMvaEstimator_ = std::make_unique<MuonMvaEstimator>(mvaLowPtTrainingFile, mvaDrMax);
69  }
70 
71  if (iConfig.getParameter<bool>("computeSoftMuonMVA")) {
72  edm::FileInPath softMvaTrainingFile = iConfig.getParameter<edm::FileInPath>("softMvaTrainingFile");
73  softMuonMvaEstimator_ = std::make_unique<SoftMuonMvaEstimator>(softMvaTrainingFile);
74  }
75 }
76 
78  relMiniIsoPUCorrected_(0),
79  useUserData_(iConfig.exists("userData")),
80  computeMuonMVA_(false),
81  computeSoftMuonMVA_(false),
82  recomputeBasicSelectors_(false),
83  mvaUseJec_(false),
84  isolator_(iConfig.exists("userIsolation") ? iConfig.getParameter<edm::ParameterSet>("userIsolation") : edm::ParameterSet(), consumesCollector(), false)
85 {
86  // input source
87  muonToken_ = consumes<edm::View<reco::Muon> >(iConfig.getParameter<edm::InputTag>( "muonSource" ));
88  // embedding of tracks
89  embedBestTrack_ = iConfig.getParameter<bool>( "embedMuonBestTrack" );
90  embedTunePBestTrack_ = iConfig.getParameter<bool>( "embedTunePMuonBestTrack" );
91  forceEmbedBestTrack_ = iConfig.getParameter<bool>( "forceBestTrackEmbedding" );
92  embedTrack_ = iConfig.getParameter<bool>( "embedTrack" );
93  embedCombinedMuon_ = iConfig.getParameter<bool>( "embedCombinedMuon" );
94  embedStandAloneMuon_ = iConfig.getParameter<bool>( "embedStandAloneMuon" );
95  // embedding of muon MET correction information
96  embedCaloMETMuonCorrs_ = iConfig.getParameter<bool>("embedCaloMETMuonCorrs" );
97  embedTcMETMuonCorrs_ = iConfig.getParameter<bool>("embedTcMETMuonCorrs" );
98  caloMETMuonCorrsToken_ = mayConsume<edm::ValueMap<reco::MuonMETCorrectionData> >(iConfig.getParameter<edm::InputTag>("caloMETMuonCorrs" ));
99  tcMETMuonCorrsToken_ = mayConsume<edm::ValueMap<reco::MuonMETCorrectionData> >(iConfig.getParameter<edm::InputTag>("tcMETMuonCorrs" ));
100  // pflow specific configurables
101  useParticleFlow_ = iConfig.getParameter<bool>( "useParticleFlow" );
102  embedPFCandidate_ = iConfig.getParameter<bool>( "embedPFCandidate" );
103  pfMuonToken_ = mayConsume<reco::PFCandidateCollection>(iConfig.getParameter<edm::InputTag>( "pfMuonSource" ));
104  embedPfEcalEnergy_ = iConfig.getParameter<bool>( "embedPfEcalEnergy" );
105  // embedding of tracks from TeV refit
106  embedPickyMuon_ = iConfig.getParameter<bool>( "embedPickyMuon" );
107  embedTpfmsMuon_ = iConfig.getParameter<bool>( "embedTpfmsMuon" );
108  embedDytMuon_ = iConfig.getParameter<bool>( "embedDytMuon" );
109  // Monte Carlo matching
110  addGenMatch_ = iConfig.getParameter<bool>( "addGenMatch" );
111  if (addGenMatch_) {
112  embedGenMatch_ = iConfig.getParameter<bool>( "embedGenMatch" );
113  if (iConfig.existsAs<edm::InputTag>("genParticleMatch")) {
114  genMatchTokens_.push_back(consumes<edm::Association<reco::GenParticleCollection> >(iConfig.getParameter<edm::InputTag>( "genParticleMatch" )));
115  }
116  else {
117  genMatchTokens_ = edm::vector_transform(iConfig.getParameter<std::vector<edm::InputTag> >( "genParticleMatch" ), [this](edm::InputTag const & tag){return consumes<edm::Association<reco::GenParticleCollection> >(tag);});
118  }
119  }
120  // efficiencies
121  addEfficiencies_ = iConfig.getParameter<bool>("addEfficiencies");
122  if(addEfficiencies_){
123  efficiencyLoader_ = pat::helper::EfficiencyLoader(iConfig.getParameter<edm::ParameterSet>("efficiencies"), consumesCollector());
124  }
125  // resolutions
126  addResolutions_ = iConfig.getParameter<bool>("addResolutions");
127  if (addResolutions_) {
129  }
130  // puppi
131  addPuppiIsolation_ = iConfig.getParameter<bool>("addPuppiIsolation");
132  if(addPuppiIsolation_){
133  PUPPIIsolation_charged_hadrons_ = consumes<edm::ValueMap<float> >(iConfig.getParameter<edm::InputTag>("puppiIsolationChargedHadrons"));
134  PUPPIIsolation_neutral_hadrons_ = consumes<edm::ValueMap<float> >(iConfig.getParameter<edm::InputTag>("puppiIsolationNeutralHadrons"));
135  PUPPIIsolation_photons_ = consumes<edm::ValueMap<float> >(iConfig.getParameter<edm::InputTag>("puppiIsolationPhotons"));
136  //puppiNoLeptons
137  PUPPINoLeptonsIsolation_charged_hadrons_ = consumes<edm::ValueMap<float> >(iConfig.getParameter<edm::InputTag>("puppiNoLeptonsIsolationChargedHadrons"));
138  PUPPINoLeptonsIsolation_neutral_hadrons_ = consumes<edm::ValueMap<float> >(iConfig.getParameter<edm::InputTag>("puppiNoLeptonsIsolationNeutralHadrons"));
139  PUPPINoLeptonsIsolation_photons_ = consumes<edm::ValueMap<float> >(iConfig.getParameter<edm::InputTag>("puppiNoLeptonsIsolationPhotons"));
140  }
141  // read isoDeposit labels, for direct embedding
142  readIsolationLabels(iConfig, "isoDeposits", isoDepositLabels_, isoDepositTokens_);
143  // read isolation value labels, for direct embedding
145  // check to see if the user wants to add user data
146  if( useUserData_ ){
147  userDataHelper_ = PATUserDataHelper<Muon>(iConfig.getParameter<edm::ParameterSet>("userData"), consumesCollector());
148  }
149  // embed high level selection variables
150  embedHighLevelSelection_ = iConfig.getParameter<bool>("embedHighLevelSelection");
151  if ( embedHighLevelSelection_ ) {
152  beamLineToken_ = consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>("beamLineSrc"));
153  pvToken_ = consumes<std::vector<reco::Vertex> >(iConfig.getParameter<edm::InputTag>("pvSrc"));
154  }
155 
156  //for mini-isolation calculation
157  computeMiniIso_ = iConfig.getParameter<bool>("computeMiniIso");
158 
159  computePuppiCombinedIso_ = iConfig.getParameter<bool>("computePuppiCombinedIso");
160 
161  effectiveAreaVec_ = iConfig.getParameter<std::vector<double> >("effectiveAreaVec");
162 
163  miniIsoParams_ = iConfig.getParameter<std::vector<double> >("miniIsoParams");
164  if(computeMiniIso_ && miniIsoParams_.size() != 9){
165  throw cms::Exception("ParameterError") << "miniIsoParams must have exactly 9 elements.\n";
166  }
167  if(computeMiniIso_ || computePuppiCombinedIso_)
168  pcToken_ = consumes<pat::PackedCandidateCollection >(iConfig.getParameter<edm::InputTag>("pfCandsForMiniIso"));
169 
170  // standard selectors
171  recomputeBasicSelectors_ = iConfig.getParameter<bool>("recomputeBasicSelectors");
172  computeMuonMVA_ = iConfig.getParameter<bool>("computeMuonMVA");
173  if (computeMuonMVA_ and not computeMiniIso_)
174  throw cms::Exception("ConfigurationError") << "MiniIso is needed for Muon MVA calculation.\n";
175 
176  if (computeMuonMVA_) {
177  // pfCombinedInclusiveSecondaryVertexV2BJetTags
178  mvaBTagCollectionTag_ = consumes<reco::JetTagCollection>(iConfig.getParameter<edm::InputTag>("mvaJetTag"));
179  mvaL1Corrector_ = consumes<reco::JetCorrector>(iConfig.getParameter<edm::InputTag>("mvaL1Corrector"));
180  mvaL1L2L3ResCorrector_ = consumes<reco::JetCorrector>(iConfig.getParameter<edm::InputTag>("mvaL1L2L3ResCorrector"));
181  rho_ = consumes<double>(iConfig.getParameter<edm::InputTag>("rho"));
182  mvaUseJec_ = iConfig.getParameter<bool>("mvaUseJec");
183  }
184 
185  computeSoftMuonMVA_ = iConfig.getParameter<bool>("computeSoftMuonMVA");
186 
187  // MC info
188  simInfo_ = consumes<edm::ValueMap<reco::MuonSimInfo> >(iConfig.getParameter<edm::InputTag>("muonSimInfo"));
189 
190  addTriggerMatching_ = iConfig.getParameter<bool>("addTriggerMatching");
191  if ( addTriggerMatching_ ){
192  triggerObjects_ = consumes<std::vector<pat::TriggerObjectStandAlone>>(iConfig.getParameter<edm::InputTag>("triggerObjects"));
193  triggerResults_ = consumes<edm::TriggerResults>(iConfig.getParameter<edm::InputTag>("triggerResults"));
194  }
195  hltCollectionFilters_ = iConfig.getParameter<std::vector<std::string>>("hltCollectionFilters");
196 
197  // produces vector of muons
198  produces<std::vector<Muon> >();
199 }
200 
201 
203 {
204 }
205 
206 std::optional<GlobalPoint> PATMuonProducer::getMuonDirection(const reco::MuonChamberMatch& chamberMatch,
208  const DetId& chamberId)
209 {
210  const GeomDet* chamberGeometry = geometry->idToDet( chamberId );
211  if (chamberGeometry){
212  LocalPoint localPosition(chamberMatch.x, chamberMatch.y, 0);
213  return std::optional<GlobalPoint>(std::in_place, chamberGeometry->toGlobal(localPosition));
214  }
215  return std::optional<GlobalPoint>();
216 
217 }
218 
219 
221  edm::Handle<std::vector<pat::TriggerObjectStandAlone> >& triggerObjects,
222  const edm::TriggerNames & names,
224 {
225  // L1 trigger object parameters are defined at MB2/ME2. Use the muon
226  // chamber matching information to get the local direction of the
227  // muon trajectory and convert it to a global direction to match the
228  // trigger objects
229 
230  std::optional<GlobalPoint> muonPosition;
231  // Loop over chambers
232  // initialize muonPosition with any available match, just in case
233  // the second station is missing - it's better folling back to
234  // dR matching at IP
235  for ( const auto& chamberMatch: aMuon.matches() ) {
236  if ( chamberMatch.id.subdetId() == MuonSubdetId::DT) {
237  DTChamberId detId(chamberMatch.id.rawId());
238  if (abs(detId.station())>3) continue;
239  muonPosition = getMuonDirection(chamberMatch, geometry, detId);
240  if (abs(detId.station())==2) break;
241  }
242  if ( chamberMatch.id.subdetId() == MuonSubdetId::CSC) {
243  CSCDetId detId(chamberMatch.id.rawId());
244  if (abs(detId.station())>3) continue;
245  muonPosition = getMuonDirection(chamberMatch, geometry, detId);
246  if (abs(detId.station())==2) break;
247  }
248  }
249  if (not muonPosition) return;
250  for (const auto& triggerObject: *triggerObjects){
251  if (triggerObject.hasTriggerObjectType(trigger::TriggerL1Mu)){
252  if (fabs(triggerObject.eta())<0.001){
253  // L1 is defined in X-Y plain
254  if (deltaPhi(triggerObject.phi(),muonPosition->phi())>0.1) continue;
255  } else {
256  // 3D L1
257  if (deltaR(triggerObject.p4(),*muonPosition)>0.15) continue;
258  }
259  pat::TriggerObjectStandAlone obj(triggerObject);
260  obj.unpackPathNames(names);
261  aMuon.addTriggerObjectMatch(obj);
262  }
263  }
264 }
265 
267  edm::Handle<std::vector<pat::TriggerObjectStandAlone> >& triggerObjects,
268  const edm::TriggerNames & names,
269  const std::vector<std::string>& collection_filter_names)
270 {
271  // WARNING: in a case of close-by muons the dR matching may select both muons.
272  // It's better to select the best match for a given collection.
273  for (const auto& triggerObject: *triggerObjects){
274  if (triggerObject.hasTriggerObjectType(trigger::TriggerMuon)){
275  bool keepIt = false;
276  for (const auto& name: collection_filter_names){
277  if (triggerObject.hasCollection(name)){
278  keepIt = true;
279  break;
280  }
281  }
282  if (not keepIt) continue;
283  if ( deltaR(triggerObject.p4(),muon)>0.1 ) continue;
284  pat::TriggerObjectStandAlone obj(triggerObject);
285  obj.unpackPathNames(names);
286  muon.addTriggerObjectMatch(obj);
287  }
288  }
289 }
290 
291 
293 {
294  // get the tracking Geometry
296  iSetup.get<GlobalTrackingGeometryRecord>().get(geometry);
297  if ( ! geometry.isValid() )
298  throw cms::Exception("FatalError") << "Unable to find GlobalTrackingGeometryRecord in event!\n";
299 
300  // switch off embedding (in unschedules mode)
301  if (iEvent.isRealData()){
302  addGenMatch_ = false;
303  embedGenMatch_ = false;
304  }
305 
307  iEvent.getByToken(muonToken_, muons);
308 
309 
312  iEvent.getByToken(pcToken_, pc);
313 
314  // get the ESHandle for the transient track builder,
315  // if needed for high level selection embedding
317 
318  if(isolator_.enabled()) isolator_.beginEvent(iEvent,iSetup);
320  if(resolutionLoader_.enabled()) resolutionLoader_.newEvent(iEvent, iSetup);
321 
323  for (size_t j = 0; j<isoDepositTokens_.size(); ++j) {
324  iEvent.getByToken(isoDepositTokens_[j], deposits[j]);
325  }
326 
328  for (size_t j = 0; j<isolationValueTokens_.size(); ++j) {
330  }
331 
332  //value maps for puppi isolation
333  edm::Handle<edm::ValueMap<float>> PUPPIIsolation_charged_hadrons;
334  edm::Handle<edm::ValueMap<float>> PUPPIIsolation_neutral_hadrons;
335  edm::Handle<edm::ValueMap<float>> PUPPIIsolation_photons;
336  //value maps for puppiNoLeptons isolation
337  edm::Handle<edm::ValueMap<float>> PUPPINoLeptonsIsolation_charged_hadrons;
338  edm::Handle<edm::ValueMap<float>> PUPPINoLeptonsIsolation_neutral_hadrons;
339  edm::Handle<edm::ValueMap<float>> PUPPINoLeptonsIsolation_photons;
340  if(addPuppiIsolation_){
341  //puppi
342  iEvent.getByToken(PUPPIIsolation_charged_hadrons_, PUPPIIsolation_charged_hadrons);
343  iEvent.getByToken(PUPPIIsolation_neutral_hadrons_, PUPPIIsolation_neutral_hadrons);
344  iEvent.getByToken(PUPPIIsolation_photons_, PUPPIIsolation_photons);
345  //puppiNoLeptons
346  iEvent.getByToken(PUPPINoLeptonsIsolation_charged_hadrons_, PUPPINoLeptonsIsolation_charged_hadrons);
347  iEvent.getByToken(PUPPINoLeptonsIsolation_neutral_hadrons_, PUPPINoLeptonsIsolation_neutral_hadrons);
348  iEvent.getByToken(PUPPINoLeptonsIsolation_photons_, PUPPINoLeptonsIsolation_photons);
349  }
350 
351  // inputs for muon mva
352  edm::Handle<reco::JetTagCollection> mvaBTagCollectionTag;
355  if (computeMuonMVA_) {
356  iEvent.getByToken(mvaBTagCollectionTag_,mvaBTagCollectionTag);
357  iEvent.getByToken(mvaL1Corrector_,mvaL1Corrector);
358  iEvent.getByToken(mvaL1L2L3ResCorrector_,mvaL1L2L3ResCorrector);
359  }
360 
361  // prepare the MC genMatchTokens_
362  GenAssociations genMatches(genMatchTokens_.size());
363  if (addGenMatch_) {
364  for (size_t j = 0, nd = genMatchTokens_.size(); j < nd; ++j) {
365  iEvent.getByToken(genMatchTokens_[j], genMatches[j]);
366  }
367  }
368 
369  // prepare the high level selection: needs beamline
370  // OR primary vertex, depending on user selection
373  bool beamSpotIsValid = false;
374  bool primaryVertexIsValid = false;
375  if ( embedHighLevelSelection_ ) {
376  // get the beamspot
377  edm::Handle<reco::BeamSpot> beamSpotHandle;
378  iEvent.getByToken(beamLineToken_, beamSpotHandle);
379 
380  // get the primary vertex
382  iEvent.getByToken( pvToken_, pvHandle );
383 
384  if( beamSpotHandle.isValid() ){
385  beamSpot = *beamSpotHandle;
386  beamSpotIsValid = true;
387  } else{
388  edm::LogError("DataNotAvailable")
389  << "No beam spot available from EventSetup, not adding high level selection \n";
390  }
391  if( pvHandle.isValid() && !pvHandle->empty() ) {
392  primaryVertex = pvHandle->at(0);
393  primaryVertexIsValid = true;
394  } else {
395  edm::LogError("DataNotAvailable")
396  << "No primary vertex available from EventSetup, not adding high level selection \n";
397  }
398  // this is needed by the IPTools methods from the tracking group
399  iSetup.get<TransientTrackRecord>().get("TransientTrackBuilder", trackBuilder);
400  }
401 
402  // MC info
404  bool simInfoIsAvailalbe = iEvent.getByToken(simInfo_,simInfo);
405 
406  // this will be the new object collection
407  std::vector<Muon> * patMuons = new std::vector<Muon>();
408 
410  if( useParticleFlow_ ){
411  // get the PFCandidates of type muons
412  iEvent.getByToken(pfMuonToken_, pfMuons);
413 
414  unsigned index=0;
415  for( reco::PFCandidateConstIterator i = pfMuons->begin(); i != pfMuons->end(); ++i, ++index) {
416  const reco::PFCandidate& pfmu = *i;
417  //const reco::IsolaPFCandidate& pfmu = *i;
418  const reco::MuonRef& muonRef = pfmu.muonRef();
419  assert( muonRef.isNonnull() );
420 
421  MuonBaseRef muonBaseRef(muonRef);
422  Muon aMuon(muonBaseRef);
423 
424  if ( useUserData_ ) {
425  userDataHelper_.add( aMuon, iEvent, iSetup );
426  }
427 
428  // embed high level selection
429  if ( embedHighLevelSelection_ ) {
430  // get the tracks
431  reco::TrackRef innerTrack = muonBaseRef->innerTrack();
432  reco::TrackRef globalTrack= muonBaseRef->globalTrack();
433  reco::TrackRef bestTrack = muonBaseRef->muonBestTrack();
434  reco::TrackRef chosenTrack = innerTrack;
435  // Make sure the collection it points to is there
436  if ( bestTrack.isNonnull() && bestTrack.isAvailable() )
437  chosenTrack = bestTrack;
438 
439  if ( chosenTrack.isNonnull() && chosenTrack.isAvailable() ) {
440  unsigned int nhits = chosenTrack->numberOfValidHits(); // ????
441  aMuon.setNumberOfValidHits( nhits );
442 
443  reco::TransientTrack tt = trackBuilder->build(chosenTrack);
444  embedHighLevel( aMuon,
445  chosenTrack,
446  tt,
447  primaryVertex,
448  primaryVertexIsValid,
449  beamSpot,
450  beamSpotIsValid );
451 
452  }
453 
454  if ( globalTrack.isNonnull() && globalTrack.isAvailable() && !embedCombinedMuon_) {
455  double norm_chi2 = globalTrack->chi2() / globalTrack->ndof();
456  aMuon.setNormChi2( norm_chi2 );
457  }
458  }
459  reco::PFCandidateRef pfRef(pfMuons,index);
460  //reco::PFCandidatePtr ptrToMother(pfMuons,index);
461  reco::CandidateBaseRef pfBaseRef( pfRef );
462 
463  aMuon.setPFCandidateRef( pfRef );
464  if( embedPFCandidate_ ) aMuon.embedPFCandidate();
465  fillMuon( aMuon, muonBaseRef, pfBaseRef, genMatches, deposits, isolationValues );
466 
467  if(computeMiniIso_)
468  setMuonMiniIso(aMuon, pc.product());
469 
470  if (addPuppiIsolation_) {
471  aMuon.setIsolationPUPPI((*PUPPIIsolation_charged_hadrons)[muonBaseRef],
472  (*PUPPIIsolation_neutral_hadrons)[muonBaseRef],
473  (*PUPPIIsolation_photons)[muonBaseRef]);
474 
475  aMuon.setIsolationPUPPINoLeptons((*PUPPINoLeptonsIsolation_charged_hadrons)[muonBaseRef],
476  (*PUPPINoLeptonsIsolation_neutral_hadrons)[muonBaseRef],
477  (*PUPPINoLeptonsIsolation_photons)[muonBaseRef]);
478  }
479  else {
480  aMuon.setIsolationPUPPI(-999., -999.,-999.);
481  aMuon.setIsolationPUPPINoLeptons(-999., -999.,-999.);
482  }
483 
484  if (embedPfEcalEnergy_) {
485  aMuon.setPfEcalEnergy(pfmu.ecalEnergy());
486  }
487 
488  patMuons->push_back(aMuon);
489  }
490  }
491  else {
493  iEvent.getByToken(muonToken_, muons);
494 
495  // embedding of muon MET corrections
497  //edm::ValueMap<reco::MuonMETCorrectionData> caloMETmuCorValueMap;
499  iEvent.getByToken(caloMETMuonCorrsToken_, caloMETMuonCorrs);
500  //caloMETmuCorValueMap = *caloMETmuCorValueMap_h;
501  }
503  //edm::ValueMap<reco::MuonMETCorrectionData> tcMETmuCorValueMap;
505  iEvent.getByToken(tcMETMuonCorrsToken_, tcMETMuonCorrs);
506  //tcMETmuCorValueMap = *tcMETmuCorValueMap_h;
507  }
508 
509  if (embedPfEcalEnergy_) {
510  // get the PFCandidates of type muons
511  iEvent.getByToken(pfMuonToken_, pfMuons);
512  }
513 
514  for (edm::View<reco::Muon>::const_iterator itMuon = muons->begin(); itMuon != muons->end(); ++itMuon) {
515  // construct the Muon from the ref -> save ref to original object
516  unsigned int idx = itMuon - muons->begin();
517  MuonBaseRef muonRef = muons->refAt(idx);
518  reco::CandidateBaseRef muonBaseRef( muonRef );
519 
520  Muon aMuon(muonRef);
521  fillMuon( aMuon, muonRef, muonBaseRef, genMatches, deposits, isolationValues);
522  if(computeMiniIso_)
523  setMuonMiniIso(aMuon, pc.product());
524  if (addPuppiIsolation_) {
525  aMuon.setIsolationPUPPI((*PUPPIIsolation_charged_hadrons)[muonRef], (*PUPPIIsolation_neutral_hadrons)[muonRef], (*PUPPIIsolation_photons)[muonRef]);
526  aMuon.setIsolationPUPPINoLeptons((*PUPPINoLeptonsIsolation_charged_hadrons)[muonRef], (*PUPPINoLeptonsIsolation_neutral_hadrons)[muonRef], (*PUPPINoLeptonsIsolation_photons)[muonRef]);
527  }
528  else {
529  aMuon.setIsolationPUPPI(-999., -999.,-999.);
530  aMuon.setIsolationPUPPINoLeptons(-999., -999.,-999.);
531  }
532 
533  // Isolation
534  if (isolator_.enabled()) {
535  //reco::CandidatePtr mother = ptrToMother->sourceCandidatePtr(0);
536  isolator_.fill(*muons, idx, isolatorTmpStorage_);
537  typedef pat::helper::MultiIsolator::IsolationValuePairs IsolationValuePairs;
538  // better to loop backwards, so the vector is resized less times
539  for (IsolationValuePairs::const_reverse_iterator it = isolatorTmpStorage_.rbegin(), ed = isolatorTmpStorage_.rend(); it != ed; ++it) {
540  aMuon.setIsolation(it->first, it->second);
541  }
542  }
543 
544  // for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
545  // aMuon.setIsoDeposit(isoDepositLabels_[j].first,
546  // (*deposits[j])[muonRef]);
547  // }
548 
549  // add sel to selected
550  edm::Ptr<reco::Muon> muonsPtr = muons->ptrAt(idx);
551  if ( useUserData_ ) {
552  userDataHelper_.add( aMuon, iEvent, iSetup );
553  }
554 
555  // embed high level selection
556  if ( embedHighLevelSelection_ ) {
557  // get the tracks
558  reco::TrackRef innerTrack = itMuon->innerTrack();
559  reco::TrackRef globalTrack= itMuon->globalTrack();
560  reco::TrackRef bestTrack = itMuon->muonBestTrack();
561  reco::TrackRef chosenTrack = innerTrack;
562  // Make sure the collection it points to is there
563  if ( bestTrack.isNonnull() && bestTrack.isAvailable() )
564  chosenTrack = bestTrack;
565  if ( chosenTrack.isNonnull() && chosenTrack.isAvailable() ) {
566  unsigned int nhits = chosenTrack->numberOfValidHits(); // ????
567  aMuon.setNumberOfValidHits( nhits );
568 
569  reco::TransientTrack tt = trackBuilder->build(chosenTrack);
570  embedHighLevel( aMuon,
571  chosenTrack,
572  tt,
573  primaryVertex,
574  primaryVertexIsValid,
575  beamSpot,
576  beamSpotIsValid );
577 
578  }
579 
580  if ( globalTrack.isNonnull() && globalTrack.isAvailable() ) {
581  double norm_chi2 = globalTrack->chi2() / globalTrack->ndof();
582  aMuon.setNormChi2( norm_chi2 );
583  }
584  }
585 
586  // embed MET muon corrections
587  if( embedCaloMETMuonCorrs_ ) aMuon.embedCaloMETMuonCorrs((*caloMETMuonCorrs)[muonRef]);
588  if( embedTcMETMuonCorrs_ ) aMuon.embedTcMETMuonCorrs((*tcMETMuonCorrs )[muonRef]);
589 
590  if (embedPfEcalEnergy_) {
591  aMuon.setPfEcalEnergy(-99.0);
592  for (const reco::PFCandidate &pfmu : *pfMuons) {
593  if (pfmu.muonRef().isNonnull()) {
594  if (pfmu.muonRef().id() != muonRef.id()) throw cms::Exception("Configuration") << "Muon reference within PF candidates does not point to the muon collection." << std::endl;
595  if (pfmu.muonRef().key() == muonRef.key()) {
596  aMuon.setPfEcalEnergy(pfmu.ecalEnergy());
597  }
598  }
599  }
600  }
601  // MC info
602  aMuon.initSimInfo();
603  if (simInfoIsAvailalbe){
604  const auto& msi = (*simInfo)[muonBaseRef];
605  aMuon.setSimType(msi.primaryClass);
606  aMuon.setExtSimType(msi.extendedClass);
607  aMuon.setSimFlavour(msi.flavour);
608  aMuon.setSimHeaviestMotherFlavour(msi.heaviestMotherFlavour);
609  aMuon.setSimPdgId(msi.pdgId);
610  aMuon.setSimMotherPdgId(msi.motherPdgId);
611  aMuon.setSimBX(msi.tpBX);
612  aMuon.setSimTpEvent(msi.tpEvent);
613  aMuon.setSimProdRho(msi.vertex.Rho());
614  aMuon.setSimProdZ(msi.vertex.Z());
615  aMuon.setSimPt(msi.p4.pt());
616  aMuon.setSimEta(msi.p4.eta());
617  aMuon.setSimPhi(msi.p4.phi());
618  aMuon.setSimMatchQuality(msi.tpAssoQuality);
619  }
620  patMuons->push_back(aMuon);
621  }
622  }
623 
624  // sort muons in pt
625  std::sort(patMuons->begin(), patMuons->end(), pTComparator_);
626 
627  // Store standard muon selection decisions and jet related
628  // quantaties.
629  // Need a separate loop over muons to have all inputs properly
630  // computed and stored in the object.
632  if (computeMuonMVA_) iEvent.getByToken(rho_,rho);
633  const reco::Vertex* pv(nullptr);
634  if (primaryVertexIsValid) pv = &primaryVertex;
635 
638  bool triggerObjectsAvailable = false;
639  bool triggerResultsAvailable = false;
640  if (addTriggerMatching_){
641  triggerObjectsAvailable = iEvent.getByToken(triggerObjects_, triggerObjects);
642  triggerResultsAvailable = iEvent.getByToken(triggerResults_, triggerResults);
643  }
644 
645  for(auto& muon: *patMuons){
646  // trigger info
647  if (addTriggerMatching_ and triggerObjectsAvailable and triggerResultsAvailable){
648  const edm::TriggerNames & triggerNames(iEvent.triggerNames( *triggerResults ));
649  fillL1TriggerInfo(muon,triggerObjects,triggerNames,geometry);
650  fillHltTriggerInfo(muon,triggerObjects,triggerNames,hltCollectionFilters_);
651  }
652 
654  muon.setSelectors(0);
655  bool isRun2016BCDEF = (272728 <= iEvent.run() && iEvent.run() <= 278808);
656  muon.setSelectors(muon::makeSelectorBitset(muon, pv, isRun2016BCDEF));
657  }
658  float miniIsoValue = -1;
659  if (computeMiniIso_){
660  // MiniIsolation working points
661 
662  miniIsoValue = getRelMiniIsoPUCorrected(muon,*rho, effectiveAreaVec_);
663 
664  muon.setSelector(reco::Muon::MiniIsoLoose, miniIsoValue<0.40);
665  muon.setSelector(reco::Muon::MiniIsoMedium, miniIsoValue<0.20);
666  muon.setSelector(reco::Muon::MiniIsoTight, miniIsoValue<0.10);
667  muon.setSelector(reco::Muon::MiniIsoVeryTight, miniIsoValue<0.05);
668  }
669 
670  double puppiCombinedIsolationPAT = -1;
672 
673  puppiCombinedIsolationPAT=puppiCombinedIsolation(muon, pc.product());
674  muon.setSelector(reco::Muon::PuppiIsoLoose, puppiCombinedIsolationPAT<0.27);
675  muon.setSelector(reco::Muon::PuppiIsoMedium, puppiCombinedIsolationPAT<0.22);
676  muon.setSelector(reco::Muon::PuppiIsoTight, puppiCombinedIsolationPAT<0.12);
677  }
678 
679  float jetPtRatio = 0.0;
680  float jetPtRel = 0.0;
681  float mva = 0.0;
682  float mva_lowpt = 0.0;
683  if (computeMuonMVA_ && primaryVertexIsValid && computeMiniIso_){
684  if (mvaUseJec_){
685  mva = globalCache()->muonMvaEstimator()->computeMva(muon,
686  primaryVertex,
687  *(mvaBTagCollectionTag.product()),
688  jetPtRatio,
689  jetPtRel,
690  miniIsoValue,
691  &*mvaL1Corrector,
692  &*mvaL1L2L3ResCorrector
693  );
694  mva_lowpt = globalCache()->muonLowPtMvaEstimator()->computeMva(muon,
695  primaryVertex,
696  *(mvaBTagCollectionTag.product()),
697  jetPtRatio,
698  jetPtRel,
699  miniIsoValue,
700  &*mvaL1Corrector,
701  &*mvaL1L2L3ResCorrector
702  );
703 
704  }
705  else{
706  mva = globalCache()->muonMvaEstimator()->computeMva(muon,
707  primaryVertex,
708  *(mvaBTagCollectionTag.product()),
709  jetPtRatio,
710  jetPtRel,
711  miniIsoValue);
712  mva_lowpt = globalCache()->muonLowPtMvaEstimator()->computeMva(muon,
713  primaryVertex,
714  *(mvaBTagCollectionTag.product()),
715  jetPtRatio,
716  jetPtRel,
717  miniIsoValue);
718  }
719 
720  muon.setMvaValue(mva);
721  muon.setLowPtMvaValue(mva_lowpt);
722  muon.setJetPtRatio(jetPtRatio);
723  muon.setJetPtRel(jetPtRel);
724 
725  // multi-isolation
726  if (computeMiniIso_){
727  muon.setSelector(reco::Muon::MultiIsoMedium, miniIsoValue<0.11 && (muon.jetPtRatio() > 0.74 || muon.jetPtRel() > 6.8) );
728  }
729 
730  // MVA working points
731  // https://twiki.cern.ch/twiki/bin/viewauth/CMS/LeptonMVA
732  double dB2D = fabs(muon.dB(pat::Muon::PV2D));
733  double dB3D = fabs(muon.dB(pat::Muon::PV3D));
734  double edB3D = fabs(muon.edB(pat::Muon::PV3D));
735  double sip3D = edB3D>0?dB3D/edB3D:0.0;
736  double dz = fabs(muon.muonBestTrack()->dz(primaryVertex.position()));
737 
738  // muon preselection
739  if (muon.pt()>5 and muon.isLooseMuon() and
740  muon.passed(reco::Muon::MiniIsoLoose) and
741  sip3D<8.0 and dB2D<0.05 and dz<0.1){
742  muon.setSelector(reco::Muon::MvaLoose, muon.mvaValue()>-0.60);
743  muon.setSelector(reco::Muon::MvaMedium, muon.mvaValue()>-0.20);
744  muon.setSelector(reco::Muon::MvaTight, muon.mvaValue()> 0.15);
745  muon.setSelector(reco::Muon::MvaVTight, muon.mvaValue()> 0.45);
746  muon.setSelector(reco::Muon::MvaVVTight, muon.mvaValue()> 0.9);
747  }
748  if (muon.pt()>5 and muon.isLooseMuon() and
749  sip3D<4 and dB2D < 0.5 and dz < 1){
750  muon.setSelector(reco::Muon::LowPtMvaLoose, muon.lowptMvaValue()>-0.60);
751  muon.setSelector(reco::Muon::LowPtMvaMedium, muon.lowptMvaValue()>-0.20);
752  }
753  }
754 
755  //SOFT MVA
756  if (computeSoftMuonMVA_){
757  float mva = globalCache()->softMuonMvaEstimator()->computeMva(muon);
758  muon.setSoftMvaValue(mva);
759  //preselection in SoftMuonMvaEstimator.cc
760  muon.setSelector(reco::Muon::SoftMvaId, muon.softMvaValue() > 0.58 ); //WP choose for bmm4
761 
762  }
763  }
764 
765  // put products in Event
766  std::unique_ptr<std::vector<Muon> > ptr(patMuons);
767  iEvent.put(std::move(ptr));
768 
770 }
771 
772 
773 void PATMuonProducer::fillMuon( Muon& aMuon, const MuonBaseRef& muonRef, const reco::CandidateBaseRef& baseRef, const GenAssociations& genMatches, const IsoDepositMaps& deposits, const IsolationValueMaps& isolationValues ) const
774 {
775  // in the particle flow algorithm,
776  // the muon momentum is recomputed.
777  // the new value is stored as the momentum of the
778  // resulting PFCandidate of type Muon, and choosen
779  // as the pat::Muon momentum
780  if (useParticleFlow_)
781  aMuon.setP4( aMuon.pfCandidateRef()->p4() );
782  if (embedTrack_) aMuon.embedTrack();
785 
786  // embed the TeV refit track refs (only available for globalMuons)
787  if (aMuon.isGlobalMuon()) {
789  aMuon.embedPickyMuon();
791  aMuon.embedTpfmsMuon();
793  aMuon.embedDytMuon();
794  }
795 
796  // embed best tracks (at the end, so unless forceEmbedBestTrack_ is true we can save some space not embedding them twice)
799 
800  // store the match to the generated final state muons
801  if (addGenMatch_) {
802  for(size_t i = 0, n = genMatches.size(); i < n; ++i) {
803  reco::GenParticleRef genMuon = (*genMatches[i])[baseRef];
804  aMuon.addGenParticleRef(genMuon);
805  }
806  if (embedGenMatch_) aMuon.embedGenParticle();
807  }
808  if (efficiencyLoader_.enabled()) {
809  efficiencyLoader_.setEfficiencies( aMuon, muonRef );
810  }
811 
812  for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
813  if(useParticleFlow_) {
814  if (deposits[j]->contains(baseRef.id())) {
815  aMuon.setIsoDeposit(isoDepositLabels_[j].first, (*deposits[j])[baseRef]);
816  } else if (deposits[j]->contains(muonRef.id())){
817  aMuon.setIsoDeposit(isoDepositLabels_[j].first, (*deposits[j])[muonRef]);
818  } else {
819  reco::CandidatePtr source = aMuon.pfCandidateRef()->sourceCandidatePtr(0);
820  aMuon.setIsoDeposit(isoDepositLabels_[j].first, (*deposits[j])[source]);
821  }
822  }
823  else{
824  aMuon.setIsoDeposit(isoDepositLabels_[j].first, (*deposits[j])[muonRef]);
825  }
826  }
827 
828  for (size_t j = 0; j<isolationValues.size(); ++j) {
829  if(useParticleFlow_) {
830  if (isolationValues[j]->contains(baseRef.id())) {
831  aMuon.setIsolation(isolationValueLabels_[j].first, (*isolationValues[j])[baseRef]);
832  } else if (isolationValues[j]->contains(muonRef.id())) {
833  aMuon.setIsolation(isolationValueLabels_[j].first, (*isolationValues[j])[muonRef]);
834  } else {
835  reco::CandidatePtr source = aMuon.pfCandidateRef()->sourceCandidatePtr(0);
836  aMuon.setIsolation(isolationValueLabels_[j].first, (*isolationValues[j])[source]);
837  }
838  }
839  else{
840  aMuon.setIsolation(isolationValueLabels_[j].first, (*isolationValues[j])[muonRef]);
841  }
842  }
843 
844  if (resolutionLoader_.enabled()) {
846  }
847 }
848 
850 {
851  pat::PFIsolation miniiso = pat::getMiniPFIsolation(pc, aMuon.p4(),
852  miniIsoParams_[0], miniIsoParams_[1], miniIsoParams_[2],
853  miniIsoParams_[3], miniIsoParams_[4], miniIsoParams_[5],
854  miniIsoParams_[6], miniIsoParams_[7], miniIsoParams_[8]);
855  aMuon.setMiniPFIsolation(miniiso);
856 }
857 
858 double PATMuonProducer::getRelMiniIsoPUCorrected(const pat::Muon& muon, double rho, const std::vector<double> &area)
859 {
860  double mindr(miniIsoParams_[0]);
861  double maxdr(miniIsoParams_[1]);
862  double kt_scale(miniIsoParams_[2]);
863  double drcut = pat::miniIsoDr(muon.p4(),mindr,maxdr,kt_scale);
864  return pat::muonRelMiniIsoPUCorrected(muon.miniPFIsolation(), muon.p4(), drcut, rho, area);
865 }
866 
867 
869 {
870  double dR_threshold = 0.4;
871  double dR2_threshold = dR_threshold * dR_threshold;
872  double mix_fraction = 0.5;
873  enum particleType{
874  CH = 0,
875  NH = 1,
876  PH = 2,
877  OTHER = 100000
878  };
879  double val_PuppiWithLep = 0.0;
880  double val_PuppiWithoutLep = 0.0;
881 
882  for(const auto & cand : *pc){//pat::pat::PackedCandidate loop start
883 
884  const particleType pType =
885  isChargedHadron( cand.pdgId() ) ? CH :
886  isNeutralHadron( cand.pdgId() ) ? NH :
887  isPhoton( cand.pdgId() ) ? PH : OTHER;
888  if( pType == OTHER ){
889  if( cand.pdgId() != 1 && cand.pdgId() != 2
890  && abs( cand.pdgId() ) != 11
891  && abs( cand.pdgId() ) != 13){
892  LogTrace("PATMuonProducer") <<"candidate with PDGID = " << cand.pdgId() << " is not CH/NH/PH/e/mu or 1/2 (and this is removed from isolation calculation)" << std::endl;
893  }
894  continue;
895  }
896  double d_eta = std::abs( cand.eta() - muon.eta() );
897  if( d_eta > dR_threshold ) continue;
898 
899  double d_phi = std::abs(reco::deltaPhi(cand.phi(),muon.phi()));
900  if( d_phi > dR_threshold ) continue ;
901 
902  double dR2=reco::deltaR2(cand, muon);
903  if( dR2 > dR2_threshold ) continue;
904  if( pType == CH && dR2 < 0.0001*0.0001 ) continue;
905  if( pType == NH && dR2 < 0.01 *0.01 ) continue;
906  if( pType == PH && dR2 < 0.01 *0.01 ) continue;
907  val_PuppiWithLep += cand.pt() * cand.puppiWeight();
908  val_PuppiWithoutLep += cand.pt() * cand.puppiWeightNoLep();
909 
910  }//pat::pat::PackedCandidate loop end
911 
912  double reliso_Puppi_withLep = val_PuppiWithLep/muon.pt();
913  double reliso_Puppi_withoutlep = val_PuppiWithoutLep/muon.pt();
914  double reliso_Puppi_combined = mix_fraction * reliso_Puppi_withLep + ( 1.0 - mix_fraction) * reliso_Puppi_withoutlep;
915  return reliso_Puppi_combined;
916 }
917 
919  return std::abs(pdgid) == 130;
920 }
921 
923  return std::abs(pdgid) == 211;
924 }
925 
927  return pdgid==22;
928 }
929 
930 // ParameterSet description for module
932 {
934  iDesc.setComment("PAT muon producer module");
935 
936  // input source
937  iDesc.add<edm::InputTag>("muonSource", edm::InputTag("no default"))->setComment("input collection");
938 
939  // embedding
940  iDesc.add<bool>("embedMuonBestTrack", true)->setComment("embed muon best track (global pflow)");
941  iDesc.add<bool>("embedTunePMuonBestTrack", true)->setComment("embed muon best track (muon only)");
942  iDesc.add<bool>("forceBestTrackEmbedding", true)->setComment("force embedding separately the best tracks even if they're already embedded e.g. as tracker or global tracks");
943  iDesc.add<bool>("embedTrack", true)->setComment("embed external track");
944  iDesc.add<bool>("embedStandAloneMuon", true)->setComment("embed external stand-alone muon");
945  iDesc.add<bool>("embedCombinedMuon", false)->setComment("embed external combined muon");
946  iDesc.add<bool>("embedPickyMuon", false)->setComment("embed external picky track");
947  iDesc.add<bool>("embedTpfmsMuon", false)->setComment("embed external tpfms track");
948  iDesc.add<bool>("embedDytMuon", false)->setComment("embed external dyt track ");
949 
950  // embedding of MET muon corrections
951  iDesc.add<bool>("embedCaloMETMuonCorrs", true)->setComment("whether to add MET muon correction for caloMET or not");
952  iDesc.add<edm::InputTag>("caloMETMuonCorrs", edm::InputTag("muonMETValueMapProducer" , "muCorrData"))->setComment("source of MET muon corrections for caloMET");
953  iDesc.add<bool>("embedTcMETMuonCorrs", true)->setComment("whether to add MET muon correction for tcMET or not");
954  iDesc.add<edm::InputTag>("tcMETMuonCorrs", edm::InputTag("muonTCMETValueMapProducer" , "muCorrData"))->setComment("source of MET muon corrections for tcMET");
955 
956  // pf specific parameters
957  iDesc.add<edm::InputTag>("pfMuonSource", edm::InputTag("pfMuons"))->setComment("particle flow input collection");
958  iDesc.add<bool>("useParticleFlow", false)->setComment("whether to use particle flow or not");
959  iDesc.add<bool>("embedPFCandidate", false)->setComment("embed external particle flow object");
960  iDesc.add<bool>("embedPfEcalEnergy", true)->setComment("add ecal energy as reconstructed by PF");
961 
962  // MC matching configurables
963  iDesc.add<bool>("addGenMatch", true)->setComment("add MC matching");
964  iDesc.add<bool>("embedGenMatch", false)->setComment("embed MC matched MC information");
965  std::vector<edm::InputTag> emptySourceVector;
966  iDesc.addNode( edm::ParameterDescription<edm::InputTag>("genParticleMatch", edm::InputTag(), true) xor
967  edm::ParameterDescription<std::vector<edm::InputTag> >("genParticleMatch", emptySourceVector, true)
968  )->setComment("input with MC match information");
969 
970  // mini-iso
971  iDesc.add<bool>("computeMiniIso", false)->setComment("whether or not to compute and store electron mini-isolation");
972  iDesc.add<bool>("computePuppiCombinedIso",false)->setComment("whether or not to compute and store puppi combined isolation");
973 
974  iDesc.add<edm::InputTag>("pfCandsForMiniIso", edm::InputTag("packedPFCandidates"))->setComment("collection to use to compute mini-iso");
975  iDesc.add<std::vector<double> >("miniIsoParams", std::vector<double>())->setComment("mini-iso parameters to use for muons");
976 
977  iDesc.add<bool>("addTriggerMatching", false)->setComment("add L1 and HLT matching to offline muon");
978 
980 
981  // IsoDeposit configurables
982  edm::ParameterSetDescription isoDepositsPSet;
983  isoDepositsPSet.addOptional<edm::InputTag>("tracker");
984  isoDepositsPSet.addOptional<edm::InputTag>("ecal");
985  isoDepositsPSet.addOptional<edm::InputTag>("hcal");
986  isoDepositsPSet.addOptional<edm::InputTag>("particle");
987  isoDepositsPSet.addOptional<edm::InputTag>("pfChargedHadrons");
988  isoDepositsPSet.addOptional<edm::InputTag>("pfChargedAll");
989  isoDepositsPSet.addOptional<edm::InputTag>("pfPUChargedHadrons");
990  isoDepositsPSet.addOptional<edm::InputTag>("pfNeutralHadrons");
991  isoDepositsPSet.addOptional<edm::InputTag>("pfPhotons");
992  isoDepositsPSet.addOptional<std::vector<edm::InputTag> >("user");
993  iDesc.addOptional("isoDeposits", isoDepositsPSet);
994 
995  // isolation values configurables
996  edm::ParameterSetDescription isolationValuesPSet;
997  isolationValuesPSet.addOptional<edm::InputTag>("tracker");
998  isolationValuesPSet.addOptional<edm::InputTag>("ecal");
999  isolationValuesPSet.addOptional<edm::InputTag>("hcal");
1000  isolationValuesPSet.addOptional<edm::InputTag>("particle");
1001  isolationValuesPSet.addOptional<edm::InputTag>("pfChargedHadrons");
1002  isolationValuesPSet.addOptional<edm::InputTag>("pfChargedAll");
1003  isolationValuesPSet.addOptional<edm::InputTag>("pfPUChargedHadrons");
1004  isolationValuesPSet.addOptional<edm::InputTag>("pfNeutralHadrons");
1005  isolationValuesPSet.addOptional<edm::InputTag>("pfPhotons");
1006  iDesc.addOptional("isolationValues", isolationValuesPSet);
1007 
1008  iDesc.ifValue(edm::ParameterDescription<bool>("addPuppiIsolation", false, true),
1009  true >> (edm::ParameterDescription<edm::InputTag>("puppiIsolationChargedHadrons", edm::InputTag("muonPUPPIIsolation","h+-DR030-ThresholdVeto000-ConeVeto000"), true) and
1010  edm::ParameterDescription<edm::InputTag>("puppiIsolationNeutralHadrons", edm::InputTag("muonPUPPIIsolation","h0-DR030-ThresholdVeto000-ConeVeto001"), true) and
1011  edm::ParameterDescription<edm::InputTag>("puppiIsolationPhotons", edm::InputTag("muonPUPPIIsolation","gamma-DR030-ThresholdVeto000-ConeVeto001"), true) and
1012  edm::ParameterDescription<edm::InputTag>("puppiNoLeptonsIsolationChargedHadrons", edm::InputTag("muonPUPPINoLeptonsIsolation","h+-DR030-ThresholdVeto000-ConeVeto000"), true) and
1013  edm::ParameterDescription<edm::InputTag>("puppiNoLeptonsIsolationNeutralHadrons", edm::InputTag("muonPUPPINoLeptonsIsolation","h0-DR030-ThresholdVeto000-ConeVeto001"), true) and
1014  edm::ParameterDescription<edm::InputTag>("puppiNoLeptonsIsolationPhotons", edm::InputTag("muonPUPPINoLeptonsIsolation","gamma-DR030-ThresholdVeto000-ConeVeto001"), true)) or
1015  false >> edm::EmptyGroupDescription());
1016 
1017  // Efficiency configurables
1018  edm::ParameterSetDescription efficienciesPSet;
1019  efficienciesPSet.setAllowAnything(); // TODO: the pat helper needs to implement a description.
1020  iDesc.add("efficiencies", efficienciesPSet);
1021  iDesc.add<bool>("addEfficiencies", false);
1022 
1023  // Check to see if the user wants to add user data
1024  edm::ParameterSetDescription userDataPSet;
1026  iDesc.addOptional("userData", userDataPSet);
1027 
1028  edm::ParameterSetDescription isolationPSet;
1029  isolationPSet.setAllowAnything(); // TODO: the pat helper needs to implement a description.
1030  iDesc.add("userIsolation", isolationPSet);
1031 
1032  iDesc.add<bool>("embedHighLevelSelection", true)->setComment("embed high level selection");
1033  edm::ParameterSetDescription highLevelPSet;
1034  highLevelPSet.setAllowAnything();
1035  iDesc.addNode( edm::ParameterDescription<edm::InputTag>("beamLineSrc", edm::InputTag(), true)
1036  )->setComment("input with high level selection");
1038  )->setComment("input with high level selection");
1039 
1040  //descriptions.add("PATMuonProducer", iDesc);
1041 }
1042 
1043 
1044 
1045 // embed various impact parameters with errors
1046 // embed high level selection
1051  bool primaryVertexIsValid,
1053  bool beamspotIsValid
1054  )
1055 {
1056  // Correct to PV
1057 
1058  // PV2D
1059  std::pair<bool,Measurement1D> result =
1061  GlobalVector(track->px(),
1062  track->py(),
1063  track->pz()),
1064  primaryVertex);
1065  double d0_corr = result.second.value();
1066  double d0_err = primaryVertexIsValid ? result.second.error() : -1.0;
1067  aMuon.setDB( d0_corr, d0_err, pat::Muon::PV2D);
1068 
1069 
1070  // PV3D
1071  result =
1073  GlobalVector(track->px(),
1074  track->py(),
1075  track->pz()),
1076  primaryVertex);
1077  d0_corr = result.second.value();
1078  d0_err = primaryVertexIsValid ? result.second.error() : -1.0;
1079  aMuon.setDB( d0_corr, d0_err, pat::Muon::PV3D);
1080 
1081 
1082  // Correct to beam spot
1083  // make a fake vertex out of beam spot
1084  reco::Vertex vBeamspot(beamspot.position(), beamspot.rotatedCovariance3D());
1085 
1086  // BS2D
1087  result =
1089  GlobalVector(track->px(),
1090  track->py(),
1091  track->pz()),
1092  vBeamspot);
1093  d0_corr = result.second.value();
1094  d0_err = beamspotIsValid ? result.second.error() : -1.0;
1095  aMuon.setDB( d0_corr, d0_err, pat::Muon::BS2D);
1096 
1097  // BS3D
1098  result =
1100  GlobalVector(track->px(),
1101  track->py(),
1102  track->pz()),
1103  vBeamspot);
1104  d0_corr = result.second.value();
1105  d0_err = beamspotIsValid ? result.second.error() : -1.0;
1106  aMuon.setDB( d0_corr, d0_err, pat::Muon::BS3D);
1107 
1108 
1109  // PVDZ
1110  aMuon.setDB( track->dz(primaryVertex.position()), std::hypot(track->dzError(), primaryVertex.zError()), pat::Muon::PVDZ );
1111 }
1112 
1114 
bool embedTpfmsMuon_
embed track from tpfms muon fit into the muon
edm::EDGetTokenT< edm::ValueMap< float > > PUPPIIsolation_charged_hadrons_
bool enabled() const
&#39;true&#39; if this there is at least one efficiency configured
bool useUserData_
add user data to the muon (this will be data members of th muon even w/o embedding) ...
constexpr double deltaPhi(double phi1, double phi2)
Definition: deltaPhi.h:22
bool isAvailable() const
Definition: Ref.h:575
edm::EDGetTokenT< edm::ValueMap< float > > PUPPIIsolation_photons_
T getParameter(std::string const &) const
double ecalEnergy() const
return corrected Ecal energy
Definition: PFCandidate.h:222
void setComment(std::string const &value)
Assists in assimilating all pat::UserData into pat objects.
ParameterDescriptionNode * ifValue(ParameterDescription< T > const &switchParameter, std::unique_ptr< ParameterDescriptionCases< T >> cases)
void newEvent(const edm::Event &event)
To be called for each new event, reads in the ValueMaps for efficiencies.
bool addPuppiIsolation_
add puppi isolation
void embedDytMuon()
embed reference to the above dyt Track
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:251
void embedTpfmsMuon()
embed reference to the above tpfms Track
edm::EDGetTokenT< edm::ValueMap< reco::MuonMETCorrectionData > > tcMETMuonCorrsToken_
source of tcMET muon corrections
double eta() const final
momentum pseudorapidity
void setIsolation(IsolationKeys key, float value)
Definition: Lepton.h:99
bool embedTcMETMuonCorrs_
embed muon MET correction info for tcMET into the muon
class definition
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:161
void embedTcMETMuonCorrs(const reco::MuonMETCorrectionData &t)
edm::EDGetTokenT< std::vector< reco::Vertex > > pvToken_
input source of the primary vertex
bool contains(EventRange const &lh, EventID const &rh)
Definition: EventRange.cc:38
static const int OTHER
edm::EDGetTokenT< edm::ValueMap< float > > PUPPINoLeptonsIsolation_photons_
void embedCombinedMuon()
set reference to Track reconstructed in both tracked and muon detector (reimplemented from reco::Muon...
edm::EDGetTokenT< double > rho_
edm::EDGetTokenT< edm::View< reco::Muon > > muonToken_
input source
void embedTunePMuonBestTrack(bool force=false)
bool addEfficiencies_
add efficiencies to the muon (this will be data members of th muon even w/o embedding) ...
edm::EDGetTokenT< edm::ValueMap< reco::MuonMETCorrectionData > > caloMETMuonCorrsToken_
source of caloMET muon corrections
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
enum start value shifted to 81 so as to avoid clashes with PDG codes
void setAllowAnything()
allow any parameter label/value pairs
double zError() const
error on z
Definition: Vertex.h:123
void setSimFlavour(int f)
Definition: Muon.h:310
GlobalPoint toGlobal(const Local2DPoint &lp) const
Conversion to the global R.F. from the R.F. of the GeomDet.
Definition: GeomDet.h:54
bool embedCaloMETMuonCorrs_
embed muon MET correction info for caloMET into the muon
bool isChargedHadron(long pdgid)
std::pair< bool, Measurement1D > signedTransverseImpactParameter(const reco::TransientTrack &track, const GlobalVector &direction, const reco::Vertex &vertex)
Definition: IPTools.cc:50
~PATMuonProducer() override
default destructur
edm::EDGetTokenT< edm::ValueMap< reco::MuonSimInfo > > simInfo_
MC info.
void embedMuonBestTrack(bool force=false)
std::vector< pat::PackedCandidate > PackedCandidateCollection
void setIsolationPUPPINoLeptons(float chargedhadrons, float neutralhadrons, float photons)
sets PUPPINoLeptons isolations
Definition: Muon.h:200
void setMuonMiniIso(pat::Muon &aMuon, const pat::PackedCandidateCollection *pc)
reco::TransientTrack build(const reco::Track *p) const
PATMuonProducer(const edm::ParameterSet &iConfig, PATMuonHeavyObjectCache const *)
default constructir
bool addTriggerMatching_
Trigger.
void setSimBX(int bx)
Definition: Muon.h:314
ParameterDescriptionNode * addNode(ParameterDescriptionNode const &node)
bool embedBestTrack_
embed the track from best muon measurement (global pflow)
edm::EDGetTokenT< reco::JetCorrector > mvaL1Corrector_
std::pair< bool, Measurement1D > signedImpactParameter3D(const reco::TransientTrack &track, const GlobalVector &direction, const reco::Vertex &vertex)
Definition: IPTools.cc:71
double pt() const final
transverse momentum
bool addResolutions_
add resolutions to the muon (this will be data members of th muon even w/o embedding) ...
void unpackPathNames(const edm::TriggerNames &names)
unpack trigger names into indices
edm::EDGetTokenT< edm::ValueMap< float > > PUPPINoLeptonsIsolation_charged_hadrons_
std::vector< double > miniIsoParams_
void setSimPhi(float phi)
Definition: Muon.h:320
PFCandidateCollection::const_iterator PFCandidateConstIterator
iterator
void setResolutions(pat::PATObject< T > &obj) const
Sets the efficiencies for this object, using the reference to the original objects.
void setSimProdRho(float rho)
Definition: Muon.h:316
void setSimHeaviestMotherFlavour(int id)
Definition: Muon.h:311
void setPFCandidateRef(const reco::PFCandidateRef &ref)
add a reference to the source IsolatedPFCandidate
Definition: Muon.h:133
bool isRealData() const
Definition: EventBase.h:62
const Point & position() const
position
Definition: Vertex.h:109
void embedHighLevel(pat::Muon &aMuon, reco::TrackRef track, reco::TransientTrack &tt, reco::Vertex &primaryVertex, bool primaryVertexIsValid, reco::BeamSpot &beamspot, bool beamspotIsValid)
std::vector< double > effectiveAreaVec_
ProductID id() const
Definition: RefToBase.h:242
const std::string names[nVars_]
edm::EDGetTokenT< edm::ValueMap< float > > PUPPINoLeptonsIsolation_neutral_hadrons_
void setSimType(reco::MuonSimType type)
Definition: Muon.h:308
void embedStandAloneMuon()
set reference to Track reconstructed in the muon detector only (reimplemented from reco::Muon) ...
edm::EDGetTokenT< reco::JetTagCollection > mvaBTagCollectionTag_
edm::EDGetTokenT< reco::PFCandidateCollection > pfMuonToken_
input source pfCandidates that will be to be transformed into pat::Muons, when using PF2PAT ...
void setIsolationPUPPI(float chargedhadrons, float neutralhadrons, float photons)
sets PUPPI isolations
Definition: Muon.h:193
pat::helper::MultiIsolator isolator_
helper class to add userdefined isolation values to the muon
std::vector< edm::EDGetTokenT< edm::ValueMap< IsoDeposit > > > isoDepositTokens_
bool enabled() const
&#39;true&#39; if this there is at least one efficiency configured
void setIsoDeposit(IsolationKeys key, const IsoDeposit &dep)
Sets the IsoDeposit associated with some key; if it is already existent, it is overwritten.
Definition: Lepton.h:176
Definition: HeavyIon.h:7
void embedTrack()
set reference to Track reconstructed in the tracker only (reimplemented from reco::Muon) ...
static void fillDescription(edm::ParameterSetDescription &iDesc)
const PFIsolation & miniPFIsolation() const
Definition: Lepton.h:196
edm::EDGetTokenT< reco::BeamSpot > beamLineToken_
input source of the primary vertex/beamspot
bool useParticleFlow_
switch to use particle flow (PF2PAT) or not
pat::PATUserDataHelper< pat::Muon > userDataHelper_
helper class to add userData to the muon
bool enabled() const
True if it has a non null configuration.
Definition: MultiIsolator.h:50
bool embedStandAloneMuon_
embed track from muon system into the muon
void setComment(std::string const &value)
double muonRelMiniIsoPUCorrected(const PFIsolation &iso, const math::XYZTLorentzVector &p4, double dr, double rho, const std::vector< double > &area)
edm::EDGetTokenT< edm::TriggerResults > triggerResults_
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void beginEvent(const edm::Event &event, const edm::EventSetup &eventSetup)
GreaterByPt< Muon > pTComparator_
bool embedTrack_
embed the track from inner tracker into the muon
void setDB(double dB, double edB, IPTYPE type=PV2D)
Definition: Muon.h:244
bool addGenMatch_
add generator match information
PATMuonHeavyObjectCache(const edm::ParameterSet &)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
description of config file parameters
Definition: Muon.py:1
void fillHltTriggerInfo(pat::Muon &muon, edm::Handle< std::vector< pat::TriggerObjectStandAlone > > &triggerObjects, const edm::TriggerNames &names, const std::vector< std::string > &collection_names)
bool isGlobalMuon() const override
Definition: Muon.h:291
bool embedPfEcalEnergy_
add ecal PF energy
void embedGenParticle()
Definition: PATObject.h:694
void newEvent(const edm::Event &event, const edm::EventSetup &setup)
To be called for each new event, reads in the EventSetup object.
void embedPFCandidate()
embed the IsolatedPFCandidate pointed to by pfCandidateRef_
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
everything that needs to be done during the event loop
reco::PFCandidateRef pfCandidateRef() const
RunNumber_t run() const
Definition: Event.h:101
def pv(vc)
Definition: MetAnalyzer.py:7
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
const LorentzVector & p4() const final
four-momentum Lorentz vector
Definition: LeafCandidate.h:99
void fillL1TriggerInfo(pat::Muon &muon, edm::Handle< std::vector< pat::TriggerObjectStandAlone > > &triggerObjects, const edm::TriggerNames &names, const edm::ESHandle< GlobalTrackingGeometry > &geometry)
static std::string const triggerResults
Definition: EdmProvDump.cc:45
ParameterDescriptionBase * add(U const &iLabel, T const &value)
bool isValid() const
Definition: HandleBase.h:74
double puppiCombinedIsolation(const pat::Muon &muon, const pat::PackedCandidateCollection *pc)
#define LogTrace(id)
std::vector< std::string > hltCollectionFilters_
reco::MuonRef muonRef() const
Definition: PFCandidate.cc:459
reco::Muon::Selector makeSelectorBitset(reco::Muon const &muon, reco::Vertex const *vertex=0, bool run2016_hip_mitigation=false)
edm::EDGetTokenT< pat::PackedCandidateCollection > pcToken_
Definition: DetId.h:18
bool isAValidMuonTrack(const MuonTrackType &type) const
void initSimInfo(void)
constexpr auto deltaR2(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:16
void addGenParticleRef(const reco::GenParticleRef &ref)
Definition: PATObject.h:678
auto vector_transform(std::vector< InputType > const &input, Function predicate) -> std::vector< typename std::remove_cv< typename std::remove_reference< decltype(predicate(input.front()))>::type >::type >
Definition: transform.h:11
void setPfEcalEnergy(float pfEcalEnergy)
Definition: Muon.h:267
bool embedTunePBestTrack_
embed the track from best muon measurement (muon only)
T const * product() const
Definition: Handle.h:74
static void fillDescription(edm::ParameterSetDescription &iDesc)
Method for documentation and validation of PSet.
double getRelMiniIsoPUCorrected(const pat::Muon &muon, double rho, const std::vector< double > &area)
void setSimMotherPdgId(int id)
Definition: Muon.h:313
void setEfficiencies(pat::PATObject< T > &obj, const R &originalRef) const
Sets the efficiencies for this object, using the reference to the original objects.
void addTriggerObjectMatch(const TriggerObjectStandAlone &trigObj)
add a trigger match
Definition: PATObject.h:204
void setExtSimType(reco::ExtendedMuonSimType type)
Definition: Muon.h:309
std::vector< MuonChamberMatch > & matches()
get muon matching information
Definition: Muon.h:144
void setMiniPFIsolation(PFIsolation const &iso)
Definition: Lepton.h:197
std::vector< edm::Handle< edm::Association< reco::GenParticleCollection > > > GenAssociations
edm::EDGetTokenT< reco::JetCorrector > mvaL1L2L3ResCorrector_
void fillMuon(Muon &aMuon, const MuonBaseRef &muonRef, const reco::CandidateBaseRef &baseRef, const GenAssociations &genMatches, const IsoDepositMaps &deposits, const IsolationValueMaps &isolationValues) const
common muon filling, for both the standard and PF2PAT case
ESHandle< TrackerGeometry > geometry
std::vector< std::pair< pat::IsolationKeys, float > > IsolationValuePairs
Definition: MultiIsolator.h:16
void setSimProdZ(float z)
Definition: Muon.h:317
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
pat::helper::EfficiencyLoader efficiencyLoader_
helper class to add efficiencies to the muon
HLT enums.
bool embedPickyMuon_
embed track from picky muon fit into the muon
void setNormChi2(double normChi2)
Definition: Muon.h:255
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
bool embedDytMuon_
embed track from DYT muon fit into the muon
void embedCaloMETMuonCorrs(const reco::MuonMETCorrectionData &t)
PFIsolation getMiniPFIsolation(const pat::PackedCandidateCollection *pfcands, const math::XYZTLorentzVector &p4, float mindr=0.05, float maxdr=0.2, float kt_scale=10.0, float ptthresh=0.5, float deadcone_ch=0.0001, float deadcone_pu=0.01, float deadcone_ph=0.01, float deadcone_nh=0.01, float dZ_cut=0.0)
void setSimPdgId(int id)
Definition: Muon.h:312
T get() const
Definition: EventSetup.h:71
void setSimPt(float pt)
Definition: Muon.h:318
const GeomDet * idToDet(DetId) const override
float miniIsoDr(const math::XYZTLorentzVector &p4, float mindr, float maxdr, float kt_scale)
std::vector< edm::Handle< edm::ValueMap< IsoDeposit > > > IsoDepositMaps
bool isPhoton(long pdgid)
pat::helper::MultiIsolator::IsolationValuePairs isolatorTmpStorage_
isolation value pair for temporary storage before being folded into the muon
edm::EDGetTokenT< std::vector< pat::TriggerObjectStandAlone > > triggerObjects_
bool embedGenMatch_
embed the gen match information into the muon
const Point & position() const
position
Definition: BeamSpot.h:62
void readIsolationLabels(const edm::ParameterSet &iConfig, const char *psetName, IsolationLabels &labels, std::vector< edm::EDGetTokenT< edm::ValueMap< T > > > &tokens)
IsolationLabels isoDepositLabels_
input source for isoDeposits
std::vector< edm::EDGetTokenT< edm::Association< reco::GenParticleCollection > > > genMatchTokens_
input tags for generator match information
Covariance3DMatrix rotatedCovariance3D() const
Definition: BeamSpot.cc:78
static constexpr int DT
Definition: MuonSubdetId.h:12
IsolationLabels isolationValueLabels_
input source isolation value maps
bool isValid() const
Definition: ESHandle.h:44
std::vector< edm::Handle< edm::ValueMap< double > > > IsolationValueMaps
void embedPickyMuon()
embed reference to the above picky Track
static constexpr int CSC
Definition: MuonSubdetId.h:13
bool forceEmbedBestTrack_
force separate embed of the best track even if already embedded
bool embedPFCandidate_
embed pfCandidates into the muon
double phi() const final
momentum azimuthal angle
std::vector< edm::EDGetTokenT< edm::ValueMap< double > > > isolationValueTokens_
void setNumberOfValidHits(unsigned int numberOfValidHits)
Definition: Muon.h:250
bool computeMuonMVA_
standard muon selectors
void setSimEta(float eta)
Definition: Muon.h:319
bool embedCombinedMuon_
embed track of the combined fit into the muon
Analysis-level muon class.
Definition: Muon.h:50
pat::helper::KinResolutionsLoader resolutionLoader_
helper class to add resolutions to the muon
static std::string const source
Definition: EdmProvDump.cc:47
void setP4(const LorentzVector &p4) final
set 4-momentum
edm::TriggerNames const & triggerNames(edm::TriggerResults const &triggerResults) const override
Definition: Event.cc:256
edm::EDGetTokenT< edm::ValueMap< float > > PUPPIIsolation_neutral_hadrons_
def move(src, dest)
Definition: eostools.py:511
std::optional< GlobalPoint > getMuonDirection(const reco::MuonChamberMatch &chamberMatch, const edm::ESHandle< GlobalTrackingGeometry > &geometry, const DetId &chamberId)
bool isNeutralHadron(long pdgid)
Global3DVector GlobalVector
Definition: GlobalVector.h:10
Analysis-level trigger object class (stand-alone)
void setSimMatchQuality(float quality)
Definition: Muon.h:321
void fill(const edm::View< T > &coll, int idx, IsolationValuePairs &isolations) const
Definition: MultiIsolator.h:82
void setSimTpEvent(int tpEvent)
Definition: Muon.h:315
bool embedHighLevelSelection_
embed high level selection variables