CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/RecoMuon/MuonIsolationProducers/plugins/MuIsoDepositProducer.cc

Go to the documentation of this file.
00001 #include "RecoMuon/MuonIsolationProducers/plugins/MuIsoDepositProducer.h"
00002 
00003 // Framework
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 (unsigned int 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   unsigned int nDeps = theMultipleDepositsFlag ? theDepositNames.size() : 1;
00086 
00087 
00088 
00089   // Take the muon container
00090   LogTrace(metname)<<" Taking the muons: "<<theMuonCollectionTag;
00091   Handle<View<Track> > tracks;
00093   Handle<View<RecoCandidate> > muons;
00094   Handle<View<Candidate> > cands;
00095 
00096   unsigned int 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     unsigned int 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         nMuons = nCands;
00121     LogDebug(metname)<< "Got candidate view with size "<<nMuons;
00122   }
00123 
00124   static const unsigned int MAX_DEPS=10;
00125   std::auto_ptr<reco::IsoDepositMap> depMaps[MAX_DEPS];
00126 
00127   if (nDeps >10 ) LogError(metname)<<"Unable to handle more than 10 input deposits";
00128   for (unsigned int i =0;i<nDeps; ++i){
00129     depMaps[i] =  std::auto_ptr<reco::IsoDepositMap>(new reco::IsoDepositMap());
00130   }
00131   
00135   if (nMuons > 0){
00136     
00137     std::vector<std::vector<IsoDeposit> > deps2D(nDeps, std::vector<IsoDeposit>(nMuons));
00138     
00139     for (unsigned int i=0; i<  nMuons; ++i) {
00140       TrackBaseRef muRef;
00141       if (readFromRecoMuon){
00142         if (theMuonTrackRefType == "track"){
00143           muRef = TrackBaseRef((*muons)[i].track());
00144         } else if (theMuonTrackRefType == "standAloneMuon"){
00145           muRef = TrackBaseRef((*muons)[i].standAloneMuon());
00146         } else if (theMuonTrackRefType == "combinedMuon"){
00147           muRef = TrackBaseRef((*muons)[i].combinedMuon());
00148         } else if (theMuonTrackRefType == "bestGlbTrkSta"){
00149           if (!(*muons)[i].combinedMuon().isNull()){
00150             muRef = TrackBaseRef((*muons)[i].combinedMuon());
00151           } else if (!(*muons)[i].track().isNull()){
00152             muRef = TrackBaseRef((*muons)[i].track());
00153           } else {
00154             muRef = TrackBaseRef((*muons)[i].standAloneMuon());
00155           }
00156         } else if (theMuonTrackRefType == "bestTrkSta"){
00157           if (!(*muons)[i].track().isNull()){
00158             muRef = TrackBaseRef((*muons)[i].track());
00159           } else {
00160             muRef = TrackBaseRef((*muons)[i].standAloneMuon());
00161           }
00162         }else {
00163           edm::LogWarning(metname)<<"Wrong track type is supplied: breaking";
00164           break;
00165         }
00166       } else if (readFromRecoTrack){
00167         muRef = TrackBaseRef(tracks, i);
00168       }
00169 
00170       if (! theMultipleDepositsFlag){
00171                 if (readFromCandidateView || theExtractForCandidate) deps2D[0][i] = theExtractor->deposit(event, eventSetup, (*cands)[i]);
00172         else deps2D[0][i] = theExtractor->deposit(event, eventSetup, muRef);
00173         
00174       } else {
00175         std::vector<IsoDeposit> deps(nDeps);
00176                 if (readFromCandidateView || theExtractForCandidate) deps = theExtractor->deposits(event, eventSetup, (*cands)[i]);
00177         else deps = theExtractor->deposits(event, eventSetup, muRef);
00178         for (unsigned int iDep =0; iDep<nDeps; ++iDep) {
00179           deps2D[iDep][i] = deps[iDep];
00180         }
00181       }
00182     }
00183     
00185     for (unsigned int iDep=0; iDep < nDeps; ++iDep){
00187       for (unsigned int iMu = 0; iMu< nMuons; ++iMu){
00188         LogTrace(metname)<<"Contents of "<<theDepositNames[iDep]
00189                          <<" for a muon at index "<<iMu;
00190         LogTrace(metname)<<deps2D[iDep][iMu].print();
00191       }
00192 
00194       reco::IsoDepositMap::Filler filler(*depMaps[iDep]);     
00195 
00197       if (readFromRecoMuon){
00198         filler.insert(muons, deps2D[iDep].begin(), deps2D[iDep].end());
00199       } else if (readFromRecoTrack){
00200         filler.insert(tracks, deps2D[iDep].begin(), deps2D[iDep].end());
00201       } else if (readFromCandidateView){
00202         filler.insert(cands, deps2D[iDep].begin(), deps2D[iDep].end());
00203       } else {
00204         edm::LogError(metname)<<"Inconsistent configuration: unknown type requested";
00205       }
00206 
00208       filler.fill();
00209     }
00210   }
00211 
00212 
00213   for (unsigned int iMap = 0; iMap < nDeps; ++iMap){
00214     LogTrace(metname)<<"About to put a deposit named "<<theDepositNames[iMap]
00215                      <<" of size "<<depMaps[iMap]->size()
00216                      <<" into edm::Event";
00217     event.put(depMaps[iMap], theDepositNames[iMap]);
00218   }
00219 
00220   LogTrace(metname) <<" END OF EVENT " <<"================================";
00221 }
00222 
00223 #include "FWCore/PluginManager/interface/ModuleDef.h"
00224 #include "FWCore/Framework/interface/MakerMacros.h"
00225 DEFINE_FWK_MODULE(MuIsoDepositProducer);