00001
00002
00003
00004
00005 #include "PhysicsTools/PatAlgos/plugins/PATMuonProducer.h"
00006
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 #include "FWCore/ParameterSet/interface/FileInPath.h"
00009 #include "FWCore/Utilities/interface/Exception.h"
00010
00011 #include "DataFormats/MuonReco/interface/Muon.h"
00012 #include "DataFormats/MuonReco/interface/MuonFwd.h"
00013
00014 #include "DataFormats/TrackReco/interface/TrackToTrackMap.h"
00015
00016 #include "DataFormats/ParticleFlowCandidate/interface/IsolatedPFCandidateFwd.h"
00017 #include "DataFormats/ParticleFlowCandidate/interface/IsolatedPFCandidate.h"
00018
00019 #include "DataFormats/HepMCCandidate/interface/GenParticleFwd.h"
00020 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
00021
00022 #include "DataFormats/Common/interface/Association.h"
00023
00024 #include "TMath.h"
00025
00026 #include <vector>
00027 #include <memory>
00028
00029
00030 using namespace pat;
00031 using namespace std;
00032
00033
00034 PATMuonProducer::PATMuonProducer(const edm::ParameterSet & iConfig) :
00035 isolator_(iConfig.exists("isolation") ? iConfig.getParameter<edm::ParameterSet>("isolation") : edm::ParameterSet(), false),
00036 userDataHelper_ ( iConfig.getParameter<edm::ParameterSet>("userData") )
00037
00038 {
00039
00040
00041
00042 muonSrc_ = iConfig.getParameter<edm::InputTag>( "muonSource" );
00043
00044
00045
00046 embedTrack_ = iConfig.getParameter<bool> ( "embedTrack" );
00047 embedStandAloneMuon_ = iConfig.getParameter<bool> ( "embedStandAloneMuon" );
00048 embedCombinedMuon_ = iConfig.getParameter<bool> ( "embedCombinedMuon" );
00049
00050 embedPickyMuon_ = iConfig.getParameter<bool> ( "embedPickyMuon" );
00051 embedTpfmsMuon_ = iConfig.getParameter<bool> ( "embedTpfmsMuon" );
00052
00053
00054
00055
00056
00057
00058
00059 pfMuonSrc_ = iConfig.getParameter<edm::InputTag>( "pfMuonSource" );
00060 useParticleFlow_ = iConfig.getParameter<bool>( "useParticleFlow" );
00061
00062 embedPFCandidate_ = iConfig.getParameter<bool>( "embedPFCandidate" );
00063
00064
00065
00066 addTeVRefits_ = iConfig.getParameter<bool>("addTeVRefits");
00067 if (addTeVRefits_) {
00068 pickySrc_ = iConfig.getParameter<edm::InputTag>("pickySrc");
00069 tpfmsSrc_ = iConfig.getParameter<edm::InputTag>("tpfmsSrc");
00070 }
00071
00072
00073
00074
00075
00076 addGenMatch_ = iConfig.getParameter<bool> ( "addGenMatch" );
00077 if (addGenMatch_) {
00078 embedGenMatch_ = iConfig.getParameter<bool> ( "embedGenMatch" );
00079 if (iConfig.existsAs<edm::InputTag>("genParticleMatch")) {
00080 genMatchSrc_.push_back(iConfig.getParameter<edm::InputTag>( "genParticleMatch" ));
00081 } else {
00082 genMatchSrc_ = iConfig.getParameter<std::vector<edm::InputTag> >( "genParticleMatch" );
00083 }
00084 }
00085
00086
00087 addTrigMatch_ = iConfig.getParameter<bool> ( "addTrigMatch" );
00088 trigMatchSrc_ = iConfig.getParameter<std::vector<edm::InputTag> >( "trigPrimMatch" );
00089
00090
00091 addResolutions_= iConfig.getParameter<bool> ( "addResolutions" );
00092
00093
00094 addEfficiencies_ = iConfig.getParameter<bool>("addEfficiencies");
00095 if (addEfficiencies_) {
00096 efficiencyLoader_ = pat::helper::EfficiencyLoader(iConfig.getParameter<edm::ParameterSet>("efficiencies"));
00097 }
00098
00099 if (iConfig.exists("isoDeposits")) {
00100 edm::ParameterSet depconf = iConfig.getParameter<edm::ParameterSet>("isoDeposits");
00101 if (depconf.exists("tracker")) isoDepositLabels_.push_back(std::make_pair(TrackerIso, depconf.getParameter<edm::InputTag>("tracker")));
00102 if (depconf.exists("ecal")) isoDepositLabels_.push_back(std::make_pair(ECalIso, depconf.getParameter<edm::InputTag>("ecal")));
00103 if (depconf.exists("hcal")) isoDepositLabels_.push_back(std::make_pair(HCalIso, depconf.getParameter<edm::InputTag>("hcal")));
00104
00105 if (depconf.exists("particle")) isoDepositLabels_.push_back(std::make_pair(ParticleIso, depconf.getParameter<edm::InputTag>("particle")));
00106 if (depconf.exists("chargedparticle")) isoDepositLabels_.push_back(std::make_pair(ChargedParticleIso, depconf.getParameter<edm::InputTag>("chargedparticle")));
00107 if (depconf.exists("neutralparticle")) isoDepositLabels_.push_back(std::make_pair(NeutralParticleIso,depconf.getParameter<edm::InputTag>("neutralparticle")));
00108 if (depconf.exists("gammaparticle")) isoDepositLabels_.push_back(std::make_pair(GammaParticleIso, depconf.getParameter<edm::InputTag>("gammaparticle")));
00109
00110
00111 if (depconf.exists("user")) {
00112 std::vector<edm::InputTag> userdeps = depconf.getParameter<std::vector<edm::InputTag> >("user");
00113 std::vector<edm::InputTag>::const_iterator it = userdeps.begin(), ed = userdeps.end();
00114 int key = UserBaseIso;
00115 for ( ; it != ed; ++it, ++key) {
00116 isoDepositLabels_.push_back(std::make_pair(IsolationKeys(key), *it));
00117 }
00118 }
00119 }
00120
00121
00122 useUserData_ = false;
00123 if ( iConfig.exists("userData") ) {
00124 useUserData_ = true;
00125 }
00126
00127
00128 produces<std::vector<Muon> >();
00129
00130 }
00131
00132
00133 PATMuonProducer::~PATMuonProducer() {
00134 }
00135
00136 void PATMuonProducer::produce(edm::Event & iEvent, const edm::EventSetup & iSetup) {
00137
00138 edm::Handle<edm::View<MuonType> > muons;
00139 iEvent.getByLabel(muonSrc_, muons);
00140
00141 if (isolator_.enabled()) isolator_.beginEvent(iEvent,iSetup);
00142
00143 if (efficiencyLoader_.enabled()) efficiencyLoader_.newEvent(iEvent);
00144
00145 std::vector<edm::Handle<edm::ValueMap<IsoDeposit> > > deposits(isoDepositLabels_.size());
00146 for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
00147 iEvent.getByLabel(isoDepositLabels_[j].second, deposits[j]);
00148 }
00149
00150
00151 GenAssociations genMatches(genMatchSrc_.size());
00152 if (addGenMatch_) {
00153 for (size_t j = 0, nd = genMatchSrc_.size(); j < nd; ++j) {
00154 iEvent.getByLabel(genMatchSrc_[j], genMatches[j]);
00155 }
00156 }
00157
00158
00159 TrigAssociations trigMatches(trigMatchSrc_.size());
00160 if ( addTrigMatch_ ) {
00161 for ( size_t i = 0; i < trigMatchSrc_.size(); ++i ) {
00162 iEvent.getByLabel(trigMatchSrc_[i], trigMatches[i]);
00163 }
00164 }
00165
00166
00167 std::vector<Muon> * patMuons = new std::vector<Muon>();
00168
00169
00170
00171
00172
00173 if( useParticleFlow_ ) {
00174 edm::Handle< reco::PFCandidateCollection > pfMuons;
00175 iEvent.getByLabel(pfMuonSrc_, pfMuons);
00176 unsigned index=0;
00177 for( reco::PFCandidateConstIterator i = pfMuons->begin();
00178 i != pfMuons->end(); ++i, ++index) {
00179
00180
00181 const reco::PFCandidate& pfmu = *i;
00182
00183
00184
00185
00186 const reco::MuonRef& muonRef = pfmu.muonRef();
00187 assert( muonRef.isNonnull() );
00188
00189 MuonBaseRef muonBaseRef(muonRef);
00190 Muon aMuon(muonBaseRef);
00191
00192 reco::PFCandidateRef pfRef(pfMuons,index);
00193
00194 reco::CandidateBaseRef pfBaseRef( pfRef );
00195
00196
00197
00198 fillMuon( aMuon, muonBaseRef, pfBaseRef, genMatches, trigMatches);
00199
00200 aMuon.setPFCandidateRef( pfRef );
00201 if( embedPFCandidate_ ) aMuon.embedPFCandidate();
00202
00203 patMuons->push_back(aMuon);
00204
00205 }
00206 }
00207 else {
00208 edm::Handle<edm::View<MuonType> > muons;
00209 iEvent.getByLabel(muonSrc_, muons);
00210
00211
00212 edm::Handle<reco::TrackToTrackMap> pickyMap, tpfmsMap;
00213 if (addTeVRefits_) {
00214 iEvent.getByLabel(pickySrc_, pickyMap);
00215 iEvent.getByLabel(tpfmsSrc_, tpfmsMap);
00216 }
00217
00218 for (edm::View<MuonType>::const_iterator itMuon = muons->begin(); itMuon != muons->end(); ++itMuon) {
00219
00220
00221
00222 unsigned int idx = itMuon - muons->begin();
00223 MuonBaseRef muonRef = muons->refAt(idx);
00224 reco::CandidateBaseRef muonBaseRef( muonRef );
00225
00226 Muon aMuon(muonRef);
00227
00228 fillMuon( aMuon, muonRef, muonBaseRef, genMatches, trigMatches );
00229
00230
00231 if (addTeVRefits_ && itMuon->isGlobalMuon()) {
00232 reco::TrackToTrackMap::const_iterator it;
00233 const reco::TrackRef& globalTrack = itMuon->globalTrack();
00234
00235
00236
00237
00238 if (!pickyMap.failedToGet()) {
00239 it = pickyMap->find(globalTrack);
00240 if (it != pickyMap->end()) aMuon.setPickyMuon(it->val);
00241 if (embedPickyMuon_) aMuon.embedPickyMuon();
00242 }
00243
00244 if (!tpfmsMap.failedToGet()) {
00245 it = tpfmsMap->find(globalTrack);
00246 if (it != tpfmsMap->end()) aMuon.setTpfmsMuon(it->val);
00247 if (embedTpfmsMuon_) aMuon.embedTpfmsMuon();
00248 }
00249 }
00250
00251
00252 if (isolator_.enabled()) {
00253
00254 isolator_.fill(*muons, idx, isolatorTmpStorage_);
00255 typedef pat::helper::MultiIsolator::IsolationValuePairs IsolationValuePairs;
00256
00257 for (IsolationValuePairs::const_reverse_iterator it = isolatorTmpStorage_.rbegin(), ed = isolatorTmpStorage_.rend(); it != ed; ++it) {
00258 aMuon.setIsolation(it->first, it->second);
00259 }
00260 }
00261
00262 for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
00263 aMuon.setIsoDeposit(isoDepositLabels_[j].first,
00264 (*deposits[j])[muonRef]);
00265 }
00266
00267
00268 edm::Ptr<MuonType> muonsPtr = muons->ptrAt(idx);
00269 if ( useUserData_ ) {
00270 userDataHelper_.add( aMuon, iEvent, iSetup );
00271 }
00272
00273 patMuons->push_back(aMuon);
00274 }
00275
00276 }
00277
00278
00279 std::sort(patMuons->begin(), patMuons->end(), pTComparator_);
00280
00281
00282 std::auto_ptr<std::vector<Muon> > ptr(patMuons);
00283 iEvent.put(ptr);
00284
00285 if (isolator_.enabled()) isolator_.endEvent();
00286 }
00287
00288 void PATMuonProducer::fillMuon( Muon& aMuon,
00289 const MuonBaseRef& muonRef,
00290 const reco::CandidateBaseRef& baseRef,
00291 const GenAssociations& genMatches,
00292 const TrigAssociations& trigMatches ) const {
00293
00294
00295 if (embedTrack_) aMuon.embedTrack();
00296 if (embedStandAloneMuon_) aMuon.embedStandAloneMuon();
00297 if (embedCombinedMuon_) aMuon.embedCombinedMuon();
00298
00299
00300 if (addGenMatch_) {
00301 for(size_t i = 0, n = genMatches.size(); i < n; ++i) {
00302 reco::GenParticleRef genMuon = (*genMatches[i])[baseRef];
00303 aMuon.addGenParticleRef(genMuon);
00304 }
00305 if (embedGenMatch_) aMuon.embedGenParticle();
00306 }
00307
00308
00309 if ( addTrigMatch_ ) {
00310 for ( size_t i = 0; i < trigMatches.size(); ++i ) {
00311 TriggerPrimitiveRef trigPrim = (*trigMatches[i])[baseRef];
00312 if ( trigPrim.isNonnull() && trigPrim.isAvailable() ) {
00313 aMuon.addTriggerMatch(*trigPrim);
00314 }
00315 }
00316 }
00317
00318 if (efficiencyLoader_.enabled()) {
00319 efficiencyLoader_.setEfficiencies( aMuon, muonRef );
00320 }
00321
00322
00323 }
00324
00325 #include "FWCore/Framework/interface/MakerMacros.h"
00326
00327 DEFINE_FWK_MODULE(PATMuonProducer);