Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-25 02:14:08

0001 /** \class MuonProducer
0002  *  See header file.
0003  *
0004  *  \author R. Bellan - UCSB <riccardo.bellan@cern.ch>
0005  */
0006 
0007 #include "RecoMuon/MuonIdentification/plugins/MuonProducer.h"
0008 
0009 #include "RecoMuon/MuonIsolation/interface/MuPFIsoHelper.h"
0010 #include "DataFormats/VertexReco/interface/VertexFwd.h"
0011 #include "DataFormats/VertexReco/interface/Vertex.h"
0012 #include "DataFormats/MuonReco/interface/MuonSelectors.h"
0013 
0014 #ifndef dout
0015 #define dout  \
0016   if (debug_) \
0017   std::cout
0018 #endif
0019 
0020 using std::endl;
0021 
0022 typedef std::map<reco::MuonRef, unsigned int> MuToPFMap;
0023 
0024 namespace reco {
0025   typedef edm::ValueMap<reco::MuonShower> MuonShowerMap;
0026 }
0027 
0028 /// Constructor
0029 MuonProducer::MuonProducer(const edm::ParameterSet& pSet)
0030     : debug_(pSet.getUntrackedParameter<bool>("ActivateDebug", false)) {
0031   setAlias(pSet.getParameter<std::string>("@module_label"));
0032 
0033   fastLabelling_ = pSet.getUntrackedParameter<bool>("FastLabelling", true);
0034 
0035   theMuonsCollectionLabel = pSet.getParameter<edm::InputTag>("InputMuons");
0036   theMuonsCollectionToken_ = consumes<reco::MuonCollection>(theMuonsCollectionLabel);
0037 
0038   thePFCandLabel = pSet.getParameter<edm::InputTag>("PFCandidates");
0039   thePFCandToken_ = consumes<reco::PFCandidateCollection>(thePFCandLabel);
0040 
0041   // Variables to switch on-off the differnt parts
0042   fillSelectors_ = pSet.getParameter<bool>("FillSelectorMaps");
0043   fillCosmicsIdMap_ = pSet.getParameter<bool>("FillCosmicsIdMap");
0044   fillPFMomentum_ = pSet.getParameter<bool>("FillPFMomentumAndAssociation");
0045   fillPFIsolation_ = pSet.getParameter<bool>("FillPFIsolation");
0046   fillDetectorBasedIsolation_ = pSet.getParameter<bool>("FillDetectorBasedIsolation");
0047   fillShoweringInfo_ = pSet.getParameter<bool>("FillShoweringInfo");
0048   fillTimingInfo_ = pSet.getParameter<bool>("FillTimingInfo");
0049   computeStandardSelectors_ = pSet.getParameter<bool>("ComputeStandardSelectors");
0050 
0051   produces<reco::MuonCollection>();
0052 
0053   if (fillTimingInfo_) {
0054     timeMapCmbToken_ = consumes<reco::MuonTimeExtraMap>(edm::InputTag(theMuonsCollectionLabel.label(), "combined"));
0055     timeMapDTToken_ = consumes<reco::MuonTimeExtraMap>(edm::InputTag(theMuonsCollectionLabel.label(), "dt"));
0056     timeMapCSCToken_ = consumes<reco::MuonTimeExtraMap>(edm::InputTag(theMuonsCollectionLabel.label(), "csc"));
0057 
0058     produces<reco::MuonTimeExtraMap>("combined");
0059     produces<reco::MuonTimeExtraMap>("dt");
0060     produces<reco::MuonTimeExtraMap>("csc");
0061   }
0062 
0063   if (fillDetectorBasedIsolation_) {
0064     theTrackDepositName = pSet.getParameter<edm::InputTag>("TrackIsoDeposits");
0065     theTrackDepositToken_ = consumes<reco::IsoDepositMap>(theTrackDepositName);
0066     produces<reco::IsoDepositMap>(labelOrInstance(theTrackDepositName));
0067 
0068     theJetDepositName = pSet.getParameter<edm::InputTag>("JetIsoDeposits");
0069     theJetDepositToken_ = consumes<reco::IsoDepositMap>(theJetDepositName);
0070     produces<reco::IsoDepositMap>(labelOrInstance(theJetDepositName));
0071 
0072     theEcalDepositName = pSet.getParameter<edm::InputTag>("EcalIsoDeposits");
0073     theEcalDepositToken_ = consumes<reco::IsoDepositMap>(theEcalDepositName);
0074     produces<reco::IsoDepositMap>(theEcalDepositName.instance());
0075 
0076     theHcalDepositName = pSet.getParameter<edm::InputTag>("HcalIsoDeposits");
0077     theHcalDepositToken_ = consumes<reco::IsoDepositMap>(theHcalDepositName);
0078     produces<reco::IsoDepositMap>(theHcalDepositName.instance());
0079 
0080     theHoDepositName = pSet.getParameter<edm::InputTag>("HoIsoDeposits");
0081     theHoDepositToken_ = consumes<reco::IsoDepositMap>(theHoDepositName);
0082 
0083     produces<reco::IsoDepositMap>(theHoDepositName.instance());
0084   }
0085 
0086   if (fillSelectors_) {
0087     theSelectorMapNames = pSet.getParameter<InputTags>("SelectorMaps");
0088 
0089     for (InputTags::const_iterator tag = theSelectorMapNames.begin(); tag != theSelectorMapNames.end(); ++tag) {
0090       theSelectorMapTokens_.push_back(consumes<edm::ValueMap<bool>>(*tag));
0091       produces<edm::ValueMap<bool>>(labelOrInstance(*tag));
0092     }
0093   }
0094 
0095   if (fillShoweringInfo_) {
0096     theShowerMapName = pSet.getParameter<edm::InputTag>("ShowerInfoMap");
0097     theShowerMapToken_ = consumes<reco::MuonShowerMap>(theShowerMapName);
0098     produces<edm::ValueMap<reco::MuonShower>>(labelOrInstance(theShowerMapName));
0099   }
0100 
0101   if (fillCosmicsIdMap_) {
0102     theCosmicCompMapName = pSet.getParameter<edm::InputTag>("CosmicIdMap");
0103     theCosmicIdMapToken_ = consumes<edm::ValueMap<unsigned int>>(theCosmicCompMapName);
0104     theCosmicCompMapToken_ = consumes<edm::ValueMap<reco::MuonCosmicCompatibility>>(theCosmicCompMapName);
0105 
0106     produces<edm::ValueMap<reco::MuonCosmicCompatibility>>(labelOrInstance(theCosmicCompMapName));
0107     produces<edm::ValueMap<unsigned int>>(labelOrInstance(theCosmicCompMapName));
0108   }
0109 
0110   theMuToMuMapName = theMuonsCollectionLabel.label() + "2" + theAlias + "sMap";
0111   produces<edm::ValueMap<reco::MuonRef>>(theMuToMuMapName);
0112 
0113   if (fillPFIsolation_) {
0114     edm::ParameterSet pfIsoPSet = pSet.getParameter<edm::ParameterSet>("PFIsolation");
0115 
0116     //Define a map between the isolation and the PSet for the PFHelper
0117     std::map<std::string, edm::ParameterSet> psetMap;
0118 
0119     //First declare what isolation you are going to read
0120     std::vector<std::string> isolationLabels;
0121     isolationLabels.push_back("pfIsolationR03");
0122     isolationLabels.push_back("pfIsoMeanDRProfileR03");
0123     isolationLabels.push_back("pfIsoSumDRProfileR03");
0124     isolationLabels.push_back("pfIsolationR04");
0125     isolationLabels.push_back("pfIsoMeanDRProfileR04");
0126     isolationLabels.push_back("pfIsoSumDRProfileR04");
0127 
0128     //Fill the label,pet map and initialize MuPFIsoHelper
0129     for (std::vector<std::string>::const_iterator label = isolationLabels.begin(); label != isolationLabels.end();
0130          ++label)
0131       psetMap[*label] = pfIsoPSet.getParameter<edm::ParameterSet>(*label);
0132 
0133     thePFIsoHelper = new MuPFIsoHelper(psetMap, consumesCollector());
0134 
0135     //Now loop on the mass read for each PSet the parameters and save them to the mapNames for later
0136 
0137     for (std::map<std::string, edm::ParameterSet>::const_iterator map = psetMap.begin(); map != psetMap.end(); ++map) {
0138       std::map<std::string, edm::InputTag> isoMap;
0139       isoMap["chargedParticle"] = map->second.getParameter<edm::InputTag>("chargedParticle");
0140       isoMap["chargedHadron"] = map->second.getParameter<edm::InputTag>("chargedHadron");
0141       isoMap["neutralHadron"] = map->second.getParameter<edm::InputTag>("neutralHadron");
0142       isoMap["neutralHadronHighThreshold"] = map->second.getParameter<edm::InputTag>("neutralHadronHighThreshold");
0143       isoMap["photon"] = map->second.getParameter<edm::InputTag>("photon");
0144       isoMap["photonHighThreshold"] = map->second.getParameter<edm::InputTag>("photonHighThreshold");
0145       isoMap["pu"] = map->second.getParameter<edm::InputTag>("pu");
0146 
0147       std::map<std::string, edm::EDGetTokenT<edm::ValueMap<double>>> isoMapToken;
0148       isoMapToken["chargedParticle"] = consumes<edm::ValueMap<double>>(isoMap["chargedParticle"]);
0149       isoMapToken["chargedHadron"] = consumes<edm::ValueMap<double>>(isoMap["chargedHadron"]);
0150       isoMapToken["neutralHadron"] = consumes<edm::ValueMap<double>>(isoMap["neutralHadron"]);
0151       isoMapToken["neutralHadronHighThreshold"] = consumes<edm::ValueMap<double>>(isoMap["neutralHadronHighThreshold"]);
0152       isoMapToken["photon"] = consumes<edm::ValueMap<double>>(isoMap["photon"]);
0153       isoMapToken["photonHighThreshold"] = consumes<edm::ValueMap<double>>(isoMap["photonHighThreshold"]);
0154       isoMapToken["pu"] = consumes<edm::ValueMap<double>>(isoMap["pu"]);
0155 
0156       pfIsoMapNames.push_back(isoMap);
0157       pfIsoMapTokens_.push_back(isoMapToken);
0158     }
0159 
0160     for (unsigned int j = 0; j < pfIsoMapNames.size(); ++j) {
0161       for (std::map<std::string, edm::InputTag>::const_iterator map = pfIsoMapNames.at(j).begin();
0162            map != pfIsoMapNames.at(j).end();
0163            ++map)
0164         produces<edm::ValueMap<double>>(labelOrInstance(map->second));
0165     }
0166   }
0167 
0168   if (computeStandardSelectors_) {
0169     vertexes_ = consumes<reco::VertexCollection>(pSet.getParameter<edm::InputTag>("vertices"));
0170   }
0171 }
0172 
0173 /// Destructor
0174 MuonProducer::~MuonProducer() {
0175   if (fillPFIsolation_ && thePFIsoHelper)
0176     delete thePFIsoHelper;
0177 }
0178 
0179 /// reconstruct muons
0180 void MuonProducer::produce(edm::Event& event, const edm::EventSetup& eventSetup) {
0181   const std::string metname = "Muon|RecoMuon|MuonIdentification|MuonProducer";
0182 
0183   // the muon collection, it will be loaded in the event
0184   auto outputMuons = std::make_unique<reco::MuonCollection>();
0185   reco::MuonRefProd outputMuonsRefProd = event.getRefBeforePut<reco::MuonCollection>();
0186 
0187   edm::Handle<reco::MuonCollection> inputMuons;
0188   event.getByToken(theMuonsCollectionToken_, inputMuons);
0189   edm::OrphanHandle<reco::MuonCollection> inputMuonsOH(inputMuons.product(), inputMuons.id());
0190 
0191   edm::Handle<reco::PFCandidateCollection> pfCandidates;
0192   event.getByToken(thePFCandToken_, pfCandidates);
0193 
0194   edm::Handle<reco::VertexCollection> primaryVertices;
0195   const reco::Vertex* vertex(nullptr);
0196   if (computeStandardSelectors_) {
0197     event.getByToken(vertexes_, primaryVertices);
0198     if (!primaryVertices->empty())
0199       vertex = &(primaryVertices->front());
0200   }
0201 
0202   // fetch collections for PFIso
0203   if (fillPFIsolation_)
0204     thePFIsoHelper->beginEvent(event);
0205 
0206   // Fill timing information
0207   edm::Handle<reco::MuonTimeExtraMap> timeMapCmb;
0208   edm::Handle<reco::MuonTimeExtraMap> timeMapDT;
0209   edm::Handle<reco::MuonTimeExtraMap> timeMapCSC;
0210 
0211   int nMuons = inputMuons->size();
0212 
0213   std::vector<reco::MuonTimeExtra> dtTimeColl(nMuons);
0214   std::vector<reco::MuonTimeExtra> cscTimeColl(nMuons);
0215   std::vector<reco::MuonTimeExtra> combinedTimeColl(nMuons);
0216 
0217   if (fillTimingInfo_) {
0218     event.getByToken(timeMapCmbToken_, timeMapCmb);
0219     event.getByToken(timeMapDTToken_, timeMapDT);
0220     event.getByToken(timeMapCSCToken_, timeMapCSC);
0221   }
0222 
0223   std::vector<reco::IsoDeposit> trackDepColl(nMuons);
0224   std::vector<reco::IsoDeposit> ecalDepColl(nMuons);
0225   std::vector<reco::IsoDeposit> hcalDepColl(nMuons);
0226   std::vector<reco::IsoDeposit> hoDepColl(nMuons);
0227   std::vector<reco::IsoDeposit> jetDepColl(nMuons);
0228 
0229   edm::Handle<reco::IsoDepositMap> trackIsoDepMap;
0230   edm::Handle<reco::IsoDepositMap> ecalIsoDepMap;
0231   edm::Handle<reco::IsoDepositMap> hcalIsoDepMap;
0232   edm::Handle<reco::IsoDepositMap> hoIsoDepMap;
0233   edm::Handle<reco::IsoDepositMap> jetIsoDepMap;
0234 
0235   if (fillDetectorBasedIsolation_) {
0236     event.getByToken(theTrackDepositToken_, trackIsoDepMap);
0237     event.getByToken(theEcalDepositToken_, ecalIsoDepMap);
0238     event.getByToken(theHcalDepositToken_, hcalIsoDepMap);
0239     event.getByToken(theHoDepositToken_, hoIsoDepMap);
0240     event.getByToken(theJetDepositToken_, jetIsoDepMap);
0241   }
0242 
0243   std::vector<std::map<std::string, edm::Handle<edm::ValueMap<double>>>> pfIsoMaps;
0244   std::vector<std::map<std::string, std::vector<double>>> pfIsoMapVals;
0245 
0246   if (fillPFIsolation_) {
0247     for (unsigned int j = 0; j < pfIsoMapNames.size(); ++j) {
0248       std::map<std::string, std::vector<double>> mapVals;
0249       std::map<std::string, edm::Handle<edm::ValueMap<double>>> maps;
0250       for (std::map<std::string, edm::InputTag>::const_iterator map = pfIsoMapNames.at(j).begin();
0251            map != pfIsoMapNames.at(j).end();
0252            ++map) {
0253         edm::Handle<edm::ValueMap<double>> handleTmp;
0254         event.getByToken(pfIsoMapTokens_.at(j)[map->first], handleTmp);
0255         maps[map->first] = handleTmp;
0256         mapVals[map->first].resize(nMuons);
0257       }
0258       pfIsoMapVals.push_back(mapVals);
0259       pfIsoMaps.push_back(maps);
0260     }
0261   }
0262 
0263   std::vector<edm::Handle<edm::ValueMap<bool>>> selectorMaps(fillSelectors_ ? theSelectorMapNames.size() : 0);
0264   std::vector<std::vector<bool>> selectorMapResults(fillSelectors_ ? theSelectorMapNames.size() : 0);
0265   if (fillSelectors_) {
0266     unsigned int s = 0;
0267     for (InputTags::const_iterator tag = theSelectorMapNames.begin(); tag != theSelectorMapNames.end(); ++tag, ++s) {
0268       event.getByToken(theSelectorMapTokens_.at(s), selectorMaps[s]);
0269       selectorMapResults[s].resize(nMuons);
0270     }
0271   }
0272 
0273   edm::Handle<reco::MuonShowerMap> showerInfoMap;
0274   if (fillShoweringInfo_)
0275     event.getByToken(theShowerMapToken_, showerInfoMap);
0276 
0277   std::vector<reco::MuonShower> showerInfoColl(nMuons);
0278 
0279   edm::Handle<edm::ValueMap<unsigned int>> cosmicIdMap;
0280   if (fillCosmicsIdMap_)
0281     event.getByToken(theCosmicIdMapToken_, cosmicIdMap);
0282   std::vector<unsigned int> cosmicIdColl(fillCosmicsIdMap_ ? nMuons : 0);
0283 
0284   edm::Handle<edm::ValueMap<reco::MuonCosmicCompatibility>> cosmicCompMap;
0285   if (fillCosmicsIdMap_)
0286     event.getByToken(theCosmicCompMapToken_, cosmicCompMap);
0287   std::vector<reco::MuonCosmicCompatibility> cosmicCompColl(fillCosmicsIdMap_ ? nMuons : 0);
0288 
0289   std::vector<reco::MuonRef> muonRefColl(nMuons);
0290 
0291   if (inputMuons->empty()) {
0292     edm::OrphanHandle<reco::MuonCollection> muonHandle = event.put(std::move(outputMuons));
0293 
0294     if (fillTimingInfo_) {
0295       fillMuonMap<reco::MuonTimeExtra>(event, muonHandle, combinedTimeColl, "combined");
0296       fillMuonMap<reco::MuonTimeExtra>(event, muonHandle, dtTimeColl, "dt");
0297       fillMuonMap<reco::MuonTimeExtra>(event, muonHandle, cscTimeColl, "csc");
0298     }
0299 
0300     if (fillDetectorBasedIsolation_) {
0301       fillMuonMap<reco::IsoDeposit>(event, muonHandle, trackDepColl, labelOrInstance(theTrackDepositName));
0302       fillMuonMap<reco::IsoDeposit>(event, muonHandle, jetDepColl, labelOrInstance(theJetDepositName));
0303       fillMuonMap<reco::IsoDeposit>(event, muonHandle, ecalDepColl, theEcalDepositName.instance());
0304       fillMuonMap<reco::IsoDeposit>(event, muonHandle, hcalDepColl, theHcalDepositName.instance());
0305       fillMuonMap<reco::IsoDeposit>(event, muonHandle, hoDepColl, theHoDepositName.instance());
0306     }
0307 
0308     if (fillSelectors_) {
0309       unsigned int s = 0;
0310       for (InputTags::const_iterator tag = theSelectorMapNames.begin(); tag != theSelectorMapNames.end(); ++tag, ++s) {
0311         fillMuonMap<bool>(event, muonHandle, selectorMapResults[s], labelOrInstance(*tag));
0312       }
0313     }
0314 
0315     if (fillShoweringInfo_)
0316       fillMuonMap<reco::MuonShower>(event, muonHandle, showerInfoColl, labelOrInstance(theShowerMapName));
0317 
0318     if (fillCosmicsIdMap_) {
0319       fillMuonMap<unsigned int>(event, muonHandle, cosmicIdColl, labelOrInstance(theCosmicCompMapName));
0320       fillMuonMap<reco::MuonCosmicCompatibility>(
0321           event, muonHandle, cosmicCompColl, labelOrInstance(theCosmicCompMapName));
0322     }
0323 
0324     fillMuonMap<reco::MuonRef>(event, inputMuonsOH, muonRefColl, theMuToMuMapName);
0325 
0326     if (fillPFIsolation_) {
0327       for (unsigned int j = 0; j < pfIsoMapNames.size(); ++j)
0328         for (std::map<std::string, edm::InputTag>::const_iterator map = pfIsoMapNames.at(j).begin();
0329              map != pfIsoMapNames.at(j).end();
0330              ++map) {
0331           fillMuonMap<double>(event, muonHandle, pfIsoMapVals.at(j)[map->first], labelOrInstance(map->second));
0332         }
0333     }
0334     return;
0335   }
0336 
0337   // FIXME: add the option to swith off the Muon-PF "info association".
0338 
0339   MuToPFMap muToPFMap;
0340 
0341   if (fillPFMomentum_) {
0342     dout << "Number of PFCandidates: " << pfCandidates->size() << endl;
0343     for (unsigned int i = 0; i < pfCandidates->size(); ++i)
0344       if (abs(pfCandidates->at(i).pdgId()) == 13) {
0345         muToPFMap[pfCandidates->at(i).muonRef()] = i;
0346         dout << "MuonRef: " << pfCandidates->at(i).muonRef().id() << " " << pfCandidates->at(i).muonRef().key()
0347              << " PF p4: " << pfCandidates->at(i).p4() << endl;
0348       }
0349     dout << "Number of PFMuons: " << muToPFMap.size() << endl;
0350     dout << "Number of Muons in the original collection: " << inputMuons->size() << endl;
0351   }
0352 
0353   reco::MuonRef::key_type muIndex = 0;
0354   unsigned int i = 0;
0355   for (auto const& inMuon : *inputMuons) {
0356     reco::MuonRef muRef(inputMuons, muIndex);
0357     muonRefColl[i] = reco::MuonRef(outputMuonsRefProd, muIndex++);
0358 
0359     // Copy the muon
0360     reco::Muon outMuon = inMuon;
0361 
0362     if (fillPFMomentum_) {
0363       // search for the corresponding pf candidate
0364       MuToPFMap::iterator iter = muToPFMap.find(muRef);
0365       if (iter != muToPFMap.end()) {
0366         const auto& pfMu = pfCandidates->at(iter->second);
0367         outMuon.setPFP4(pfMu.p4());
0368         outMuon.setP4(pfMu.p4());          //PF is the default
0369         outMuon.setCharge(pfMu.charge());  //PF is the default
0370         outMuon.setPdgId(-13 * pfMu.charge());
0371         outMuon.setBestTrack(pfMu.bestMuonTrackType());
0372         muToPFMap.erase(iter);
0373         dout << "MuonRef: " << muRef.id() << " " << muRef.key() << " Is it PF? " << outMuon.isPFMuon()
0374              << " PF p4: " << outMuon.pfP4() << endl;
0375       }
0376 
0377       dout << "MuonRef: " << muRef.id() << " " << muRef.key() << " Is it PF? " << outMuon.isPFMuon() << endl;
0378 
0379       dout << "GLB " << outMuon.isGlobalMuon() << " TM " << outMuon.isTrackerMuon() << " STA "
0380            << outMuon.isStandAloneMuon() << " p4 " << outMuon.p4() << endl;
0381     }
0382 
0383     // Add PF isolation info
0384     if (fillPFIsolation_) {
0385       thePFIsoHelper->embedPFIsolation(outMuon, muRef);
0386 
0387       for (unsigned int j = 0; j < pfIsoMapNames.size(); ++j) {
0388         for (std::map<std::string, edm::InputTag>::const_iterator map = pfIsoMapNames[j].begin();
0389              map != pfIsoMapNames[j].end();
0390              ++map) {
0391           (pfIsoMapVals[j])[map->first][i] = (*pfIsoMaps[j][map->first])[muRef];
0392         }
0393       }
0394     }
0395 
0396     // Fill timing information
0397     if (fillTimingInfo_) {
0398       combinedTimeColl[i] = (*timeMapCmb)[muRef];
0399       dtTimeColl[i] = (*timeMapDT)[muRef];
0400       cscTimeColl[i] = (*timeMapCSC)[muRef];
0401     }
0402 
0403     if (fillDetectorBasedIsolation_) {
0404       trackDepColl[i] = (*trackIsoDepMap)[muRef];
0405       ecalDepColl[i] = (*ecalIsoDepMap)[muRef];
0406       hcalDepColl[i] = (*hcalIsoDepMap)[muRef];
0407       hoDepColl[i] = (*hoIsoDepMap)[muRef];
0408       jetDepColl[i] = (*jetIsoDepMap)[muRef];
0409       ;
0410     }
0411 
0412     if (fillSelectors_) {
0413       unsigned int s = 0;
0414       for (InputTags::const_iterator tag = theSelectorMapNames.begin(); tag != theSelectorMapNames.end(); ++tag, ++s)
0415         selectorMapResults[s][i] = (*selectorMaps[s])[muRef];
0416     }
0417 
0418     // Fill the Showering Info
0419     if (fillShoweringInfo_)
0420       showerInfoColl[i] = (*showerInfoMap)[muRef];
0421 
0422     if (fillCosmicsIdMap_) {
0423       cosmicIdColl[i] = (*cosmicIdMap)[muRef];
0424       cosmicCompColl[i] = (*cosmicCompMap)[muRef];
0425     }
0426 
0427     // Standard Selectors - keep it at the end so that all inputs are available
0428     if (computeStandardSelectors_) {
0429       outMuon.setSelectors(0);  // reset flags
0430       bool isRun2016BCDEF = (272728 <= event.run() && event.run() <= 278808);
0431       outMuon.setSelectors(muon::makeSelectorBitset(outMuon, vertex, isRun2016BCDEF));
0432     }
0433 
0434     outputMuons->push_back(outMuon);
0435     ++i;
0436   }
0437 
0438   dout << "Number of Muons in the new muon collection: " << outputMuons->size() << endl;
0439   edm::OrphanHandle<reco::MuonCollection> muonHandle = event.put(std::move(outputMuons));
0440 
0441   if (fillTimingInfo_) {
0442     fillMuonMap<reco::MuonTimeExtra>(event, muonHandle, combinedTimeColl, "combined");
0443     fillMuonMap<reco::MuonTimeExtra>(event, muonHandle, dtTimeColl, "dt");
0444     fillMuonMap<reco::MuonTimeExtra>(event, muonHandle, cscTimeColl, "csc");
0445   }
0446 
0447   if (fillDetectorBasedIsolation_) {
0448     fillMuonMap<reco::IsoDeposit>(event, muonHandle, trackDepColl, labelOrInstance(theTrackDepositName));
0449     fillMuonMap<reco::IsoDeposit>(event, muonHandle, jetDepColl, labelOrInstance(theJetDepositName));
0450     fillMuonMap<reco::IsoDeposit>(event, muonHandle, ecalDepColl, theEcalDepositName.instance());
0451     fillMuonMap<reco::IsoDeposit>(event, muonHandle, hcalDepColl, theHcalDepositName.instance());
0452     fillMuonMap<reco::IsoDeposit>(event, muonHandle, hoDepColl, theHoDepositName.instance());
0453   }
0454 
0455   if (fillPFIsolation_) {
0456     for (unsigned int j = 0; j < pfIsoMapNames.size(); ++j) {
0457       for (std::map<std::string, edm::InputTag>::const_iterator map = pfIsoMapNames[j].begin();
0458            map != pfIsoMapNames[j].end();
0459            ++map)
0460         fillMuonMap<double>(event, muonHandle, pfIsoMapVals[j][map->first], labelOrInstance(map->second));
0461     }
0462   }
0463 
0464   if (fillSelectors_) {
0465     unsigned int s = 0;
0466     for (InputTags::const_iterator tag = theSelectorMapNames.begin(); tag != theSelectorMapNames.end(); ++tag, ++s)
0467       fillMuonMap<bool>(event, muonHandle, selectorMapResults[s], labelOrInstance(*tag));
0468   }
0469 
0470   if (fillShoweringInfo_)
0471     fillMuonMap<reco::MuonShower>(event, muonHandle, showerInfoColl, labelOrInstance(theShowerMapName));
0472 
0473   if (fillCosmicsIdMap_) {
0474     fillMuonMap<unsigned int>(event, muonHandle, cosmicIdColl, labelOrInstance(theCosmicCompMapName));
0475     fillMuonMap<reco::MuonCosmicCompatibility>(
0476         event, muonHandle, cosmicCompColl, labelOrInstance(theCosmicCompMapName));
0477   }
0478 
0479   fillMuonMap<reco::MuonRef>(event, inputMuonsOH, muonRefColl, theMuToMuMapName);
0480 }
0481 
0482 template <typename TYPE>
0483 void MuonProducer::fillMuonMap(edm::Event& event,
0484                                const edm::OrphanHandle<reco::MuonCollection>& muonHandle,
0485                                const std::vector<TYPE>& muonExtra,
0486                                const std::string& label) {
0487   typedef typename edm::ValueMap<TYPE>::Filler FILLER;
0488 
0489   auto muonMap = std::make_unique<edm::ValueMap<TYPE>>();
0490   if (!muonExtra.empty()) {
0491     FILLER filler(*muonMap);
0492     filler.insert(muonHandle, muonExtra.begin(), muonExtra.end());
0493     filler.fill();
0494   }
0495   event.put(std::move(muonMap), label);
0496 }
0497 
0498 std::string MuonProducer::labelOrInstance(const edm::InputTag& input) const {
0499   if (fastLabelling_)
0500     return input.label();
0501 
0502   return input.label() != theMuonsCollectionLabel.label() ? input.label() : input.instance();
0503 }