00001
00009 #include "RecoMuon/MuonIdentification/plugins/MuonProducer.h"
00010
00011 #include "RecoMuon/MuonIsolation/interface/MuPFIsoHelper.h"
00012
00013 #include "FWCore/Framework/interface/Event.h"
00014 #include "FWCore/Framework/interface/EventSetup.h"
00015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00016 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00017
00018 #include "DataFormats/MuonReco/interface/Muon.h"
00019 #include "DataFormats/MuonReco/interface/MuonTrackLinks.h"
00020 #include "DataFormats/MuonReco/interface/MuonTimeExtraMap.h"
00021 #include "DataFormats/MuonReco/interface/MuonShower.h"
00022 #include "DataFormats/MuonReco/interface/MuonCosmicCompatibility.h"
00023 #include "DataFormats/MuonReco/interface/MuonToMuonMap.h"
00024
00025 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
00026 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
00027
00028 #include "DataFormats/Common/interface/ValueMap.h"
00029 #include "DataFormats/RecoCandidate/interface/IsoDepositFwd.h"
00030 #include "DataFormats/RecoCandidate/interface/IsoDeposit.h"
00031
00032 #include <boost/foreach.hpp>
00033 #define foreach BOOST_FOREACH
00034
00035 #ifndef dout
00036 #define dout if(debug_) std::cout
00037 #endif
00038
00039 using std::endl;
00040
00041 typedef std::map<reco::MuonRef, reco::Candidate::LorentzVector> MuToPFMap;
00042
00043 namespace reco {
00044 typedef edm::ValueMap<reco::MuonShower> MuonShowerMap;
00045 }
00046
00047
00049 MuonProducer::MuonProducer(const edm::ParameterSet& pSet):debug_(pSet.getUntrackedParameter<bool>("ActivateDebug",false)){
00050
00051 setAlias(pSet.getParameter<std::string>("@module_label"));
00052
00053 fastLabelling_ = pSet.getUntrackedParameter<bool>("FastLabelling",true);
00054
00055
00056 theMuonsCollectionLabel = pSet.getParameter<edm::InputTag>("InputMuons");
00057 thePFCandLabel = pSet.getParameter<edm::InputTag>("PFCandidates");
00058
00059
00060
00061 fillSelectors_ = pSet.getParameter<bool>("FillSelectorMaps");
00062 fillCosmicsIdMap_ = pSet.getParameter<bool>("FillCosmicsIdMap");
00063 fillPFMomentum_ = pSet.getParameter<bool>("FillPFMomentumAndAssociation");
00064 fillPFIsolation_ = pSet.getParameter<bool>("FillPFIsolation");
00065
00066 produces<reco::MuonCollection>();
00067 produces<reco::MuonTimeExtraMap>("combined");
00068 produces<reco::MuonTimeExtraMap>("dt");
00069 produces<reco::MuonTimeExtraMap>("csc");
00070
00071
00072 theTrackDepositName = pSet.getParameter<edm::InputTag>("TrackIsoDeposits");
00073 produces<reco::IsoDepositMap>(labelOrInstance(theTrackDepositName));
00074 theJetDepositName = pSet.getParameter<edm::InputTag>("JetIsoDeposits");
00075 produces<reco::IsoDepositMap>(labelOrInstance(theJetDepositName));
00076 theEcalDepositName = pSet.getParameter<edm::InputTag>("EcalIsoDeposits");
00077 produces<reco::IsoDepositMap>(theEcalDepositName.instance());
00078 theHcalDepositName = pSet.getParameter<edm::InputTag>("HcalIsoDeposits");
00079 produces<reco::IsoDepositMap>(theHcalDepositName.instance());
00080 theHoDepositName = pSet.getParameter<edm::InputTag>("HoIsoDeposits");
00081 produces<reco::IsoDepositMap>(theHoDepositName.instance());
00082
00083
00084 if(fillSelectors_){
00085 theSelectorMapNames = pSet.getParameter<InputTags>("SelectorMaps");
00086
00087 for(InputTags::const_iterator tag = theSelectorMapNames.begin(); tag != theSelectorMapNames.end(); ++tag)
00088 produces<edm::ValueMap<bool> >(labelOrInstance(*tag));
00089 }
00090
00091 theShowerMapName = pSet.getParameter<edm::InputTag>("ShowerInfoMap");
00092 produces<edm::ValueMap<reco::MuonShower> >(labelOrInstance(theShowerMapName));
00093
00094 if(fillCosmicsIdMap_){
00095 theCosmicCompMapName = pSet.getParameter<edm::InputTag>("CosmicIdMap");
00096 produces<edm::ValueMap<reco::MuonCosmicCompatibility> >(labelOrInstance(theCosmicCompMapName));
00097 produces<edm::ValueMap<unsigned int> >(labelOrInstance(theCosmicCompMapName));
00098 }
00099
00100 theMuToMuMapName = theMuonsCollectionLabel.label()+"2"+theAlias+"sMap";
00101 produces<edm::ValueMap<reco::MuonRef> >(theMuToMuMapName);
00102
00103 if(fillPFIsolation_){
00104 edm::ParameterSet pfIsoPSet = pSet.getParameter<edm::ParameterSet>("PFIsolation");
00105 thePFIsoHelper = new MuPFIsoHelper(pfIsoPSet);
00106 edm::ParameterSet isoCfg03 = pfIsoPSet.getParameter<edm::ParameterSet>("isolationR03");
00107 edm::ParameterSet isoCfg04 = pfIsoPSet.getParameter<edm::ParameterSet>("isolationR04");
00108
00109 theIsoPF03MapNames["chargedParticle"] = isoCfg03.getParameter<edm::InputTag>("chargedParticle");
00110 theIsoPF03MapNames["chargedHadron"] = isoCfg03.getParameter<edm::InputTag>("chargedHadron");
00111 theIsoPF03MapNames["neutralHadron"] = isoCfg03.getParameter<edm::InputTag>("neutralHadron");
00112 theIsoPF03MapNames["photon"] = isoCfg03.getParameter<edm::InputTag>("photon");
00113 theIsoPF03MapNames["pu"] = isoCfg03.getParameter<edm::InputTag>("pu");
00114
00115 theIsoPF04MapNames["chargedParticle"] = isoCfg04.getParameter<edm::InputTag>("chargedParticle");
00116 theIsoPF04MapNames["chargedHadron"] = isoCfg04.getParameter<edm::InputTag>("chargedHadron");
00117 theIsoPF04MapNames["neutralHadron"] = isoCfg04.getParameter<edm::InputTag>("neutralHadron");
00118 theIsoPF04MapNames["photon"] = isoCfg04.getParameter<edm::InputTag>("photon");
00119 theIsoPF04MapNames["pu"] = isoCfg04.getParameter<edm::InputTag>("pu");
00120
00121 for(std::map<std::string,edm::InputTag>::const_iterator map = theIsoPF03MapNames.begin(); map != theIsoPF03MapNames.end(); ++map)
00122 produces<edm::ValueMap<double> >(labelOrInstance(map->second));
00123
00124 for(std::map<std::string,edm::InputTag>::const_iterator map = theIsoPF04MapNames.begin(); map != theIsoPF04MapNames.end(); ++map)
00125 produces<edm::ValueMap<double> >(labelOrInstance(map->second));
00126 }
00127 }
00128
00130 MuonProducer::~MuonProducer(){
00131 if (thePFIsoHelper) delete thePFIsoHelper;
00132 }
00133
00134
00136 void MuonProducer::produce(edm::Event& event, const edm::EventSetup& eventSetup){
00137
00138 const std::string metname = "Muon|RecoMuon|MuonIdentification|MuonProducer";
00139
00140
00141 std::auto_ptr<reco::MuonCollection> outputMuons(new reco::MuonCollection());
00142 reco::MuonRefProd outputMuonsRefProd = event.getRefBeforePut<reco::MuonCollection>();
00143
00144 edm::Handle<reco::MuonCollection> inputMuons;
00145 event.getByLabel(theMuonsCollectionLabel, inputMuons);
00146 edm::OrphanHandle<reco::MuonCollection> inputMuonsOH(inputMuons.product(), inputMuons.id());
00147
00148 edm::Handle<reco::PFCandidateCollection> pfCandidates;
00149 event.getByLabel(thePFCandLabel, pfCandidates);
00150
00151
00152
00153 if(fillPFIsolation_) thePFIsoHelper->beginEvent(event);
00154
00155
00156
00157 edm::Handle<reco::MuonTimeExtraMap> timeMapCmb;
00158 edm::Handle<reco::MuonTimeExtraMap> timeMapDT;
00159 edm::Handle<reco::MuonTimeExtraMap> timeMapCSC;
00160
00161 int nMuons=inputMuons->size();
00162
00163 std::vector<reco::MuonTimeExtra> dtTimeColl(nMuons);
00164 std::vector<reco::MuonTimeExtra> cscTimeColl(nMuons);
00165 std::vector<reco::MuonTimeExtra> combinedTimeColl(nMuons);
00166
00167
00168 event.getByLabel(theMuonsCollectionLabel.label(),"combined",timeMapCmb);
00169 event.getByLabel(theMuonsCollectionLabel.label(),"dt",timeMapDT);
00170 event.getByLabel(theMuonsCollectionLabel.label(),"csc",timeMapCSC);
00171
00172
00173 std::vector<reco::IsoDeposit> trackDepColl(nMuons);
00174 std::vector<reco::IsoDeposit> ecalDepColl(nMuons);
00175 std::vector<reco::IsoDeposit> hcalDepColl(nMuons);
00176 std::vector<reco::IsoDeposit> hoDepColl(nMuons);
00177 std::vector<reco::IsoDeposit> jetDepColl(nMuons);
00178
00179
00180 edm::Handle<reco::IsoDepositMap> trackIsoDepMap;
00181 edm::Handle<reco::IsoDepositMap> ecalIsoDepMap;
00182 edm::Handle<reco::IsoDepositMap> hcalIsoDepMap;
00183 edm::Handle<reco::IsoDepositMap> hoIsoDepMap;
00184 edm::Handle<reco::IsoDepositMap> jetIsoDepMap;
00185
00186
00187 event.getByLabel(theTrackDepositName,trackIsoDepMap);
00188 event.getByLabel(theEcalDepositName,ecalIsoDepMap);
00189 event.getByLabel(theHcalDepositName,hcalIsoDepMap);
00190 event.getByLabel(theHoDepositName,hoIsoDepMap);
00191 event.getByLabel(theJetDepositName,jetIsoDepMap);
00192
00193
00194 std::map<std::string,edm::Handle<edm::ValueMap<double> > > pfIso03Maps;
00195 std::map<std::string,std::vector<double> > pfIso03MapVals;
00196 std::map<std::string,edm::Handle<edm::ValueMap<double> > > pfIso04Maps;
00197 std::map<std::string,std::vector<double> > pfIso04MapVals;
00198
00199 if(fillPFIsolation_){
00200 for(std::map<std::string,edm::InputTag>::const_iterator map = theIsoPF03MapNames.begin(); map != theIsoPF03MapNames.end(); ++map){
00201 event.getByLabel(map->second,pfIso03Maps[map->first]);
00202 pfIso03MapVals[map->first].resize(nMuons);
00203 }
00204 for(std::map<std::string,edm::InputTag>::const_iterator map = theIsoPF04MapNames.begin(); map != theIsoPF04MapNames.end(); ++map){
00205 event.getByLabel(map->second,pfIso04Maps[map->first]);
00206 pfIso04MapVals[map->first].resize(nMuons);
00207 }
00208 }
00209
00210 std::vector<edm::Handle<edm::ValueMap<bool> > > selectorMaps(fillSelectors_ ? theSelectorMapNames.size() : 0);
00211 std::vector<std::vector<bool> > selectorMapResults(fillSelectors_ ? theSelectorMapNames.size() : 0);
00212 if(fillSelectors_){
00213 unsigned int s=0;
00214 for(InputTags::const_iterator tag = theSelectorMapNames.begin(); tag != theSelectorMapNames.end(); ++tag, ++s){
00215 event.getByLabel(*tag,selectorMaps[s]);
00216 selectorMapResults[s].resize(nMuons);
00217 }
00218 }
00219
00220 edm::Handle<reco::MuonShowerMap> showerInfoMap;
00221 event.getByLabel(theShowerMapName,showerInfoMap);
00222
00223 std::vector<reco::MuonShower> showerInfoColl(nMuons);
00224
00225 edm::Handle<edm::ValueMap<unsigned int> > cosmicIdMap;
00226 if(fillCosmicsIdMap_) event.getByLabel(theCosmicCompMapName,cosmicIdMap);
00227 std::vector<unsigned int> cosmicIdColl(fillCosmicsIdMap_ ? nMuons : 0);
00228
00229 edm::Handle<edm::ValueMap<reco::MuonCosmicCompatibility> > cosmicCompMap;
00230 if(fillCosmicsIdMap_) event.getByLabel(theCosmicCompMapName,cosmicCompMap);
00231 std::vector<reco::MuonCosmicCompatibility> cosmicCompColl(fillCosmicsIdMap_ ? nMuons : 0);
00232
00233
00234 std::vector<reco::MuonRef> muonRefColl(nMuons);
00235
00236
00237
00238 if(inputMuons->empty()) {
00239 edm::OrphanHandle<reco::MuonCollection> muonHandle = event.put(outputMuons);
00240
00241 fillMuonMap<reco::MuonTimeExtra>(event, muonHandle, combinedTimeColl,"combined");
00242 fillMuonMap<reco::MuonTimeExtra>(event, muonHandle, dtTimeColl,"dt");
00243 fillMuonMap<reco::MuonTimeExtra>(event, muonHandle, cscTimeColl,"csc");
00244
00245 fillMuonMap<reco::IsoDeposit>(event, muonHandle, trackDepColl, labelOrInstance(theTrackDepositName));
00246 fillMuonMap<reco::IsoDeposit>(event, muonHandle, jetDepColl, labelOrInstance(theJetDepositName));
00247 fillMuonMap<reco::IsoDeposit>(event, muonHandle, ecalDepColl, theEcalDepositName.instance());
00248 fillMuonMap<reco::IsoDeposit>(event, muonHandle, hcalDepColl, theHcalDepositName.instance());
00249 fillMuonMap<reco::IsoDeposit>(event, muonHandle, hoDepColl, theHoDepositName.instance());
00250
00251
00252 if(fillSelectors_){
00253 unsigned int s = 0;
00254 for(InputTags::const_iterator tag = theSelectorMapNames.begin();
00255 tag != theSelectorMapNames.end(); ++tag, ++s){
00256 fillMuonMap<bool>(event, muonHandle, selectorMapResults[s], labelOrInstance(*tag));
00257 }
00258 }
00259
00260 fillMuonMap<reco::MuonShower>(event, muonHandle, showerInfoColl, labelOrInstance(theShowerMapName));
00261
00262 if(fillCosmicsIdMap_){
00263 fillMuonMap<unsigned int>(event, muonHandle, cosmicIdColl, labelOrInstance(theCosmicCompMapName));
00264 fillMuonMap<reco::MuonCosmicCompatibility>(event, muonHandle, cosmicCompColl, labelOrInstance(theCosmicCompMapName));
00265 }
00266
00267 fillMuonMap<reco::MuonRef>(event,inputMuonsOH, muonRefColl, theMuToMuMapName);
00268
00269 if(fillPFIsolation_){
00270 for(std::map<std::string,edm::InputTag>::const_iterator map = theIsoPF03MapNames.begin(); map != theIsoPF03MapNames.end(); ++map)
00271 fillMuonMap<double>(event, muonHandle, pfIso03MapVals[map->first], labelOrInstance(map->second));
00272 for(std::map<std::string,edm::InputTag>::const_iterator map = theIsoPF04MapNames.begin(); map != theIsoPF04MapNames.end(); ++map)
00273 fillMuonMap<double>(event, muonHandle, pfIso04MapVals[map->first], labelOrInstance(map->second));
00274 }
00275
00276 return;
00277 }
00278
00279
00280
00281
00282 MuToPFMap muToPFMap;
00283
00284 if(fillPFMomentum_){
00285 dout << "Number of PFCandidates: " << pfCandidates->size() << endl;
00286 foreach(const reco::PFCandidate &pfCand, *pfCandidates)
00287 if(abs(pfCand.pdgId()) == 13){
00288 muToPFMap[pfCand.muonRef()] = pfCand.p4();
00289 dout << "MuonRef: " << pfCand.muonRef().id() << " " << pfCand.muonRef().key() << " PF p4: " << pfCand.p4() << endl;
00290 }
00291 dout << "Number of PFMuons: " << muToPFMap.size() << endl;
00292 dout << "Number of Muons in the original collection: " << inputMuons->size() << endl;
00293 }
00294
00295 reco::MuonRef::key_type muIndex = 0;
00296 unsigned int i = 0;
00297 foreach(const reco::Muon &inMuon, *inputMuons){
00298
00299 reco::MuonRef muRef(inputMuons, muIndex);
00300 muonRefColl[i] = reco::MuonRef(outputMuonsRefProd, muIndex++);
00301
00302
00303 reco::Muon outMuon = inMuon;
00304
00305 if(fillPFMomentum_){
00306
00307 MuToPFMap::iterator iter = muToPFMap.find(muRef);
00308 if(iter != muToPFMap.end()){
00309 outMuon.setPFP4(iter->second);
00310 muToPFMap.erase(iter);
00311 dout << "MuonRef: " << muRef.id() << " " << muRef.key()
00312 << " Is it PF? " << outMuon.isPFMuon()
00313 << " PF p4: " << outMuon.pfP4() << endl;
00314 }
00315
00316
00317 dout << "MuonRef: " << muRef.id() << " " << muRef.key()
00318 << " Is it PF? " << outMuon.isPFMuon() << endl;
00319
00320 dout << "GLB " << outMuon.isGlobalMuon()
00321 << " TM " << outMuon.isTrackerMuon()
00322 << " STA " << outMuon.isStandAloneMuon()
00323 << " p4 " << outMuon.p4() << endl;
00324 }
00325
00326
00327 if(fillPFIsolation_){
00328 thePFIsoHelper->embedPFIsolation(outMuon,muRef);
00329 for(std::map<std::string,edm::InputTag>::const_iterator map = theIsoPF03MapNames.begin(); map != theIsoPF03MapNames.end(); ++map)
00330 pfIso03MapVals[map->first][i] = (*pfIso03Maps[map->first])[muRef];
00331 for(std::map<std::string,edm::InputTag>::const_iterator map = theIsoPF04MapNames.begin(); map != theIsoPF04MapNames.end(); ++map)
00332 pfIso04MapVals[map->first][i] = (*pfIso04Maps[map->first])[muRef];
00333 }
00334
00335
00336
00337
00338 combinedTimeColl[i] = (*timeMapCmb)[muRef];
00339 dtTimeColl[i] = (*timeMapDT)[muRef];
00340 cscTimeColl[i] = (*timeMapCSC)[muRef];
00341
00342
00343 trackDepColl[i] = (*trackIsoDepMap)[muRef];
00344 ecalDepColl[i] = (*ecalIsoDepMap)[muRef];
00345 hcalDepColl[i] = (*hcalIsoDepMap)[muRef];
00346 hoDepColl[i] = (*hoIsoDepMap)[muRef];
00347 jetDepColl[i] = (*jetIsoDepMap)[muRef];;
00348
00349 if(fillSelectors_){
00350 unsigned int s = 0;
00351 for(InputTags::const_iterator tag = theSelectorMapNames.begin();
00352 tag != theSelectorMapNames.end(); ++tag, ++s)
00353 selectorMapResults[s][i] = (*selectorMaps[s])[muRef];
00354 }
00355
00356
00357 showerInfoColl[i] = (*showerInfoMap)[muRef];
00358
00359 if(fillCosmicsIdMap_){
00360 cosmicIdColl[i] = (*cosmicIdMap)[muRef];
00361 cosmicCompColl[i] = (*cosmicCompMap)[muRef];
00362 }
00363
00364 outputMuons->push_back(outMuon);
00365 ++i;
00366 }
00367
00368 dout << "Number of Muons in the new muon collection: " << outputMuons->size() << endl;
00369 edm::OrphanHandle<reco::MuonCollection> muonHandle = event.put(outputMuons);
00370
00371 fillMuonMap<reco::MuonTimeExtra>(event, muonHandle, combinedTimeColl,"combined");
00372 fillMuonMap<reco::MuonTimeExtra>(event, muonHandle, dtTimeColl,"dt");
00373 fillMuonMap<reco::MuonTimeExtra>(event, muonHandle, cscTimeColl,"csc");
00374
00375
00376 fillMuonMap<reco::IsoDeposit>(event, muonHandle, trackDepColl, labelOrInstance(theTrackDepositName));
00377 fillMuonMap<reco::IsoDeposit>(event, muonHandle, jetDepColl, labelOrInstance(theJetDepositName));
00378 fillMuonMap<reco::IsoDeposit>(event, muonHandle, ecalDepColl, theEcalDepositName.instance());
00379 fillMuonMap<reco::IsoDeposit>(event, muonHandle, hcalDepColl, theHcalDepositName.instance());
00380 fillMuonMap<reco::IsoDeposit>(event, muonHandle, hoDepColl, theHoDepositName.instance());
00381
00382 if(fillPFIsolation_){
00383 for(std::map<std::string,edm::InputTag>::const_iterator map = theIsoPF03MapNames.begin(); map != theIsoPF03MapNames.end(); ++map)
00384 fillMuonMap<double>(event, muonHandle, pfIso03MapVals[map->first], labelOrInstance(map->second));
00385 for(std::map<std::string,edm::InputTag>::const_iterator map = theIsoPF04MapNames.begin(); map != theIsoPF04MapNames.end(); ++map)
00386 fillMuonMap<double>(event, muonHandle, pfIso04MapVals[map->first], labelOrInstance(map->second));
00387 }
00388
00389 if(fillSelectors_){
00390 unsigned int s = 0;
00391 for(InputTags::const_iterator tag = theSelectorMapNames.begin();
00392 tag != theSelectorMapNames.end(); ++tag, ++s)
00393 fillMuonMap<bool>(event, muonHandle, selectorMapResults[s], labelOrInstance(*tag));
00394 }
00395
00396 fillMuonMap<reco::MuonShower>(event, muonHandle, showerInfoColl, labelOrInstance(theShowerMapName));
00397
00398 if(fillCosmicsIdMap_){
00399 fillMuonMap<unsigned int>(event, muonHandle, cosmicIdColl, labelOrInstance(theCosmicCompMapName));
00400 fillMuonMap<reco::MuonCosmicCompatibility>(event, muonHandle, cosmicCompColl, labelOrInstance(theCosmicCompMapName));
00401 }
00402
00403 fillMuonMap<reco::MuonRef>(event,inputMuonsOH, muonRefColl, theMuToMuMapName);
00404
00405
00406
00407 }
00408
00409 template<typename TYPE>
00410 void MuonProducer::fillMuonMap(edm::Event& event,
00411 const edm::OrphanHandle<reco::MuonCollection>& muonHandle,
00412 const std::vector<TYPE>& muonExtra,
00413 const std::string& label){
00414
00415 typedef typename edm::ValueMap<TYPE>::Filler FILLER;
00416
00417 std::auto_ptr<edm::ValueMap<TYPE> > muonMap(new edm::ValueMap<TYPE>());
00418 if(!muonExtra.empty()){
00419 FILLER filler(*muonMap);
00420 filler.insert(muonHandle, muonExtra.begin(), muonExtra.end());
00421 filler.fill();
00422 }
00423 event.put(muonMap,label);
00424 }
00425
00426
00427 std::string MuonProducer::labelOrInstance(const edm::InputTag &input) const{
00428 if(fastLabelling_) return input.label();
00429
00430 return input.label() != theMuonsCollectionLabel.label() ? input.label() : input.instance();
00431 }