CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/PhysicsTools/IsolationAlgos/plugins/CandIsoDepositProducer.cc

Go to the documentation of this file.
00001 #include "PhysicsTools/IsolationAlgos/plugins/CandIsoDepositProducer.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 #include "FWCore/Framework/interface/MakerMacros.h"
00009 
00010 #include "FWCore/Framework/interface/ESHandle.h"
00011 
00012 #include "DataFormats/RecoCandidate/interface/IsoDeposit.h"
00013 #include "DataFormats/TrackReco/interface/Track.h"
00014 #include "DataFormats/MuonReco/interface/Muon.h"
00015 
00016 
00017 #include "RecoMuon/MuonIsolation/interface/Range.h"
00018 #include "DataFormats/RecoCandidate/interface/IsoDepositDirection.h"
00019 #include "DataFormats/GsfTrackReco/interface/GsfTrack.h"
00020 
00021 #include "PhysicsTools/IsolationAlgos/interface/IsoDepositExtractor.h"
00022 #include "PhysicsTools/IsolationAlgos/interface/IsoDepositExtractorFactory.h"
00023 
00024 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00025 #include <string>
00026 
00027 using namespace edm;
00028 using namespace reco;
00029 using namespace muonisolation;
00030 
00031 
00033 CandIsoDepositProducer::CandIsoDepositProducer(const ParameterSet& par) :
00034   theConfig(par),
00035   theCandCollectionTag(par.getParameter<edm::InputTag>("src")),
00036   theDepositNames(std::vector<std::string>(1,"")),
00037   theMultipleDepositsFlag(par.getParameter<bool>("MultipleDepositsFlag")),
00038   theExtractor(0)
00039   {
00040   LogDebug("PhysicsTools|MuonIsolation")<<" CandIsoDepositProducer CTOR";
00041 
00042   edm::ParameterSet extractorPSet = theConfig.getParameter<edm::ParameterSet>("ExtractorPSet");
00043   std::string extractorName = extractorPSet.getParameter<std::string>("ComponentName");
00044   theExtractor = IsoDepositExtractorFactory::get()->create( extractorName, extractorPSet);
00045 
00046   if (! theMultipleDepositsFlag) produces<reco::IsoDepositMap>();
00047   else {
00048     theDepositNames = extractorPSet.getParameter<std::vector<std::string> >("DepositInstanceLabels");
00049     if (theDepositNames.size() > 10) throw cms::Exception("Configuration Error") << "This module supports only up to 10 deposits"; 
00050     for (unsigned int iDep=0; iDep<theDepositNames.size(); ++iDep){
00051       produces<reco::IsoDepositMap>(theDepositNames[iDep]);
00052     }
00053   }
00054 
00055   std::string trackType = par.getParameter<std::string>("trackType");
00056   if      (trackType == "fake") theTrackType = FakeT;
00057   else if (trackType == "best") theTrackType = BestT;
00058   else if (trackType == "standAloneMuon") theTrackType = StandAloneMuonT;
00059   else if (trackType == "combinedMuon")   theTrackType = CombinedMuonT;
00060   else if (trackType == "trackerMuon")    theTrackType = TrackT;
00061   else if (trackType == "track") theTrackType = TrackT;
00062   else if (trackType == "gsf") theTrackType = GsfT;
00063   else if (trackType == "candidate") theTrackType = CandidateT;
00064   else throw cms::Exception("Error")  << "Track type " << trackType << " not valid.";
00065 }
00066 
00068 CandIsoDepositProducer::~CandIsoDepositProducer(){
00069   LogDebug("PhysicsTools/CandIsoDepositProducer")<<" CandIsoDepositProducer DTOR";
00070   delete theExtractor;
00071 }
00072 
00073 inline const reco::Track * CandIsoDepositProducer::extractTrack(const reco::Candidate &c, reco::Track *dummy) const {
00074     if (theTrackType == CandidateT) {
00075         return 0;
00076     } else if (theTrackType == FakeT) {
00077         *dummy = Track(10,10,c.vertex(),c.momentum(),c.charge(), reco::Track::CovarianceMatrix());
00078         return dummy;
00079     } else {
00080         const RecoCandidate *rc = dynamic_cast<const RecoCandidate *>(&c);
00081         if (rc == 0) throw cms::Exception("Error") << " Candidate is not RecoCandidate: can't get a real track from it!";
00082         switch (theTrackType) {
00083             case FakeT: break; // to avoid warning
00084             case CandidateT: break; // to avoid warning
00085             case BestT: return rc->bestTrack(); break;
00086             case StandAloneMuonT: return &*rc->standAloneMuon(); break;
00087             case CombinedMuonT:   return &*rc->combinedMuon(); break;
00088             case TrackT: return &*rc->track(); break;
00089             case GsfT: return static_cast<const Track*>(rc->gsfTrack().get()); break;
00090         }
00091         return 0;
00092     }
00093 }
00094 
00096 void CandIsoDepositProducer::produce(Event& event, const EventSetup& eventSetup){
00097   static const std::string metname = "CandIsoDepositProducer";
00098 
00099   edm::Handle< edm::View<reco::Candidate> > hCands;
00100   event.getByLabel(theCandCollectionTag, hCands);
00101     
00102   unsigned int nDeps = theMultipleDepositsFlag ? theDepositNames.size() : 1;
00103 
00104   static const unsigned int MAX_DEPS=10; 
00105   std::auto_ptr<reco::IsoDepositMap> depMaps[MAX_DEPS];
00106   
00107   if (nDeps >10 ) LogError(metname)<<"Unable to handle more than 10 input deposits"; 
00108   for (unsigned int i =0;i<nDeps; ++i){ // check if nDeps > 10??
00109     depMaps[i] =  std::auto_ptr<reco::IsoDepositMap>(new reco::IsoDepositMap()); 
00110   } 
00111    
00115   size_t nMuons = hCands->size();
00116   if (nMuons > 0){ 
00117     std::vector<std::vector<IsoDeposit> > deps2D(nDeps, std::vector<IsoDeposit>(nMuons)); 
00118 
00119 
00120     Track dummy;
00121     for (size_t i=0; i<  nMuons; ++i) {
00122       const Candidate &c = (*hCands)[i];
00123       const Track *track = extractTrack(c, &dummy);
00124       if ((theTrackType != CandidateT) && (!track)) {
00125         edm::LogWarning("CandIsoDepositProducer") << "Candidate #"<<i<<" has no bestTrack(), it will produce no deposit";
00126         reco::IsoDeposit emptyDep;
00127         for (size_t iDep=0;iDep<nDeps;++iDep) {
00128           deps2D[iDep][i] = emptyDep; 
00129         }
00130         continue;
00131       }
00132       if (!theMultipleDepositsFlag){
00133         IsoDeposit dep = ( ( theTrackType == CandidateT )
00134                              ? theExtractor->deposit(event, eventSetup, c)
00135                              : theExtractor->deposit(event, eventSetup, *track) );
00136         deps2D[0][i] = dep;
00137       } else {
00138         std::vector<IsoDeposit> deps = ( ( theTrackType == CandidateT )
00139                                            ? theExtractor->deposits(event, eventSetup, c)
00140                                            : theExtractor->deposits(event, eventSetup, *track) );
00141         for (unsigned int iDep=0; iDep < nDeps; ++iDep){        deps2D[iDep][i] =  deps[iDep];  }
00142       }
00143     }
00144     
00145 
00147     for (unsigned int iDep=0; iDep < nDeps; ++iDep){ 
00149       for (unsigned int iMu = 0; iMu< nMuons; ++iMu){ 
00150         LogTrace(metname)<<"Contents of "<<theDepositNames[iDep] 
00151                          <<" for a muon at index "<<iMu; 
00152         LogTrace(metname)<<deps2D[iDep][iMu].print(); 
00153       } 
00154  
00156       reco::IsoDepositMap::Filler filler(*depMaps[iDep]);      
00157       filler.insert(hCands, deps2D[iDep].begin(), deps2D[iDep].end()); 
00158       filler.fill();
00159     }
00160   }
00161 
00162   for (unsigned int iMap = 0; iMap < nDeps; ++iMap) event.put(depMaps[iMap], theDepositNames[iMap]);
00163 }
00164 
00165 DEFINE_FWK_MODULE( CandIsoDepositProducer );