00001 #include "RecoMuon/MuonIsolationProducers/plugins/MuIsoDepositProducer.h"
00002
00003
00004 #include "FWCore/Framework/interface/EDProducer.h"
00005 #include "FWCore/Framework/interface/Event.h"
00006 #include "FWCore/Framework/interface/EventSetup.h"
00007 #include "DataFormats/Common/interface/Handle.h"
00008
00009 #include "FWCore/Framework/interface/ESHandle.h"
00010
00011 #include "DataFormats/RecoCandidate/interface/IsoDeposit.h"
00012 #include "DataFormats/RecoCandidate/interface/IsoDepositFwd.h"
00013 #include "DataFormats/Common/interface/ValueMap.h"
00014 #include "DataFormats/TrackReco/interface/Track.h"
00015 #include "DataFormats/MuonReco/interface/Muon.h"
00016 #include "DataFormats/MuonReco/interface/MuonFwd.h"
00017
00018
00019 #include "RecoMuon/MuonIsolation/interface/Range.h"
00020 #include "DataFormats/RecoCandidate/interface/IsoDepositDirection.h"
00021
00022 #include "PhysicsTools/IsolationAlgos/interface/IsoDepositExtractor.h"
00023 #include "PhysicsTools/IsolationAlgos/interface/IsoDepositExtractorFactory.h"
00024
00025 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00026 #include <string>
00027
00028 using namespace edm;
00029 using namespace std;
00030 using namespace reco;
00031 using namespace muonisolation;
00032
00033
00035 MuIsoDepositProducer::MuIsoDepositProducer(const ParameterSet& par) :
00036 theConfig(par),
00037 theDepositNames(std::vector<std::string>(1,std::string())),
00038 theExtractor(0)
00039 {
00040 LogDebug("RecoMuon|MuonIsolation")<<" MuIsoDepositProducer CTOR";
00041
00042 edm::ParameterSet ioPSet = par.getParameter<edm::ParameterSet>("IOPSet");
00043
00044 theInputType = ioPSet.getParameter<std::string>("InputType");
00045 theExtractForCandidate = ioPSet.getParameter<bool>("ExtractForCandidate");
00046 theMuonTrackRefType = ioPSet.getParameter<std::string>("MuonTrackRefType");
00047 theMuonCollectionTag = ioPSet.getParameter<edm::InputTag>("inputMuonCollection");
00048 theMultipleDepositsFlag = ioPSet.getParameter<bool>("MultipleDepositsFlag");
00049
00050
00051
00052 if (theMultipleDepositsFlag){
00053 theDepositNames = par.getParameter<edm::ParameterSet>("ExtractorPSet")
00054 .getParameter<std::vector<std::string> >("DepositInstanceLabels");
00055 }
00056
00057 for (uint i = 0; i < theDepositNames.size(); ++i){
00058 std::string alias = theConfig.getParameter<std::string>("@module_label");
00059 if (theDepositNames[i] != "") alias += "_" + theDepositNames[i];
00060 produces<reco::IsoDepositMap>(theDepositNames[i]).setBranchAlias(alias);
00061 }
00062 }
00063
00065 MuIsoDepositProducer::~MuIsoDepositProducer(){
00066 LogDebug("RecoMuon/MuIsoDepositProducer")<<" MuIsoDepositProducer DTOR";
00067 delete theExtractor;
00068 }
00069
00071 void MuIsoDepositProducer::produce(Event& event, const EventSetup& eventSetup){
00072 std::string metname = "RecoMuon|MuonIsolationProducers|MuIsoDepositProducer";
00073
00074 LogDebug(metname)<<" Muon Deposit producing..."
00075 <<" BEGINING OF EVENT " <<"================================";
00076
00077 if (!theExtractor) {
00078 edm::ParameterSet extractorPSet = theConfig.getParameter<edm::ParameterSet>("ExtractorPSet");
00079 std::string extractorName = extractorPSet.getParameter<std::string>("ComponentName");
00080 theExtractor = IsoDepositExtractorFactory::get()->create( extractorName, extractorPSet);
00081 LogDebug(metname)<<" Load extractor..."<<extractorName;
00082 }
00083
00084
00085 uint nDeps = theMultipleDepositsFlag ? theDepositNames.size() : 1;
00086
00087
00088
00089
00090 LogTrace(metname)<<" Taking the muons: "<<theMuonCollectionTag;
00091 Handle<View<Track> > tracks;
00093 Handle<View<RecoCandidate> > muons;
00094 Handle<View<Candidate> > cands;
00095
00096 uint nMuons = 0;
00097
00098 bool readFromRecoTrack = theInputType == "TrackCollection";
00099 bool readFromRecoMuon = theInputType == "MuonCollection";
00100 bool readFromCandidateView = theInputType == "CandidateView";
00101
00102 if (readFromRecoMuon){
00103 event.getByLabel(theMuonCollectionTag,muons);
00104 nMuons = muons->size();
00105 LogDebug(metname) <<"Got Muons of size "<<nMuons;
00106
00107 }
00108 if (readFromRecoTrack){
00109 event.getByLabel(theMuonCollectionTag,tracks);
00110 nMuons = tracks->size();
00111 LogDebug(metname) <<"Got MuonTracks of size "<<nMuons;
00112 }
00113 if (readFromCandidateView || theExtractForCandidate){
00114 event.getByLabel(theMuonCollectionTag,cands);
00115 uint nCands = cands->size();
00116 if (readFromRecoMuon && theExtractForCandidate){
00118 if (nMuons != nCands) edm::LogError(metname)<<"Inconsistent configuration or failure to read Candidate-muon view";
00119 }
00120 LogDebug(metname)<< "Got candidate view with size "<<nMuons;
00121 }
00122
00123 static const uint MAX_DEPS=10;
00124 std::auto_ptr<reco::IsoDepositMap> depMaps[MAX_DEPS];
00125
00126 if (nDeps >10 ) LogError(metname)<<"Unable to handle more than 10 input deposits";
00127 for (uint i =0;i<nDeps; ++i){
00128 depMaps[i] = std::auto_ptr<reco::IsoDepositMap>(new reco::IsoDepositMap());
00129 }
00130
00134 if (nMuons > 0){
00135
00136 std::vector<std::vector<IsoDeposit> > deps2D(nDeps, std::vector<IsoDeposit>(nMuons));
00137
00138 for (uint i=0; i< nMuons; ++i) {
00139 TrackBaseRef muRef;
00140 if (readFromRecoMuon){
00141 if (theMuonTrackRefType == "track"){
00142 muRef = TrackBaseRef((*muons)[i].track());
00143 } else if (theMuonTrackRefType == "standAloneMuon"){
00144 muRef = TrackBaseRef((*muons)[i].standAloneMuon());
00145 } else if (theMuonTrackRefType == "combinedMuon"){
00146 muRef = TrackBaseRef((*muons)[i].combinedMuon());
00147 } else if (theMuonTrackRefType == "bestGlbTrkSta"){
00148 if (!(*muons)[i].combinedMuon().isNull()){
00149 muRef = TrackBaseRef((*muons)[i].combinedMuon());
00150 } else if (!(*muons)[i].track().isNull()){
00151 muRef = TrackBaseRef((*muons)[i].track());
00152 } else {
00153 muRef = TrackBaseRef((*muons)[i].standAloneMuon());
00154 }
00155 } else if (theMuonTrackRefType == "bestTrkSta"){
00156 if (!(*muons)[i].track().isNull()){
00157 muRef = TrackBaseRef((*muons)[i].track());
00158 } else {
00159 muRef = TrackBaseRef((*muons)[i].standAloneMuon());
00160 }
00161 }else {
00162 edm::LogWarning(metname)<<"Wrong track type is supplied: breaking";
00163 break;
00164 }
00165 } else if (readFromRecoTrack){
00166 muRef = TrackBaseRef(tracks, i);
00167 }
00168
00169 if (! theMultipleDepositsFlag){
00170 if (theExtractForCandidate) deps2D[0][i] = theExtractor->deposit(event, eventSetup, (*cands)[i]);
00171 else deps2D[0][i] = theExtractor->deposit(event, eventSetup, muRef);
00172
00173 } else {
00174 std::vector<IsoDeposit> deps(nDeps);
00175 if (theExtractForCandidate) deps = theExtractor->deposits(event, eventSetup, (*cands)[i]);
00176 else deps = theExtractor->deposits(event, eventSetup, muRef);
00177 for (uint iDep =0; iDep<nDeps; ++iDep) {
00178 deps2D[iDep][i] = deps[iDep];
00179 }
00180 }
00181 }
00182
00184 for (uint iDep=0; iDep < nDeps; ++iDep){
00186 for (uint iMu = 0; iMu< nMuons; ++iMu){
00187 LogTrace(metname)<<"Contents of "<<theDepositNames[iDep]
00188 <<" for a muon at index "<<iMu;
00189 LogTrace(metname)<<deps2D[iDep][iMu].print();
00190 }
00191
00193 reco::IsoDepositMap::Filler filler(*depMaps[iDep]);
00194
00196 if (readFromRecoMuon){
00197 filler.insert(muons, deps2D[iDep].begin(), deps2D[iDep].end());
00198 } else if (readFromRecoTrack){
00199 filler.insert(tracks, deps2D[iDep].begin(), deps2D[iDep].end());
00200 } else if (readFromCandidateView){
00201 filler.insert(cands, deps2D[iDep].begin(), deps2D[iDep].end());
00202 } else {
00203 edm::LogError(metname)<<"Inconsistent configuration: unknown type requested";
00204 }
00205
00207 filler.fill();
00208 }
00209 }
00210
00211
00212 for (uint iMap = 0; iMap < nDeps; ++iMap){
00213 LogTrace(metname)<<"About to put a deposit named "<<theDepositNames[iMap]
00214 <<" of size "<<depMaps[iMap]->size()
00215 <<" into edm::Event";
00216 event.put(depMaps[iMap], theDepositNames[iMap]);
00217 }
00218
00219 LogTrace(metname) <<" END OF EVENT " <<"================================";
00220 }
00221
00222 #include "FWCore/PluginManager/interface/ModuleDef.h"
00223 #include "FWCore/Framework/interface/MakerMacros.h"
00224 DEFINE_FWK_MODULE(MuIsoDepositProducer);