Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 10:01:22

0001 #include "RecoMuon/MuonIsolationProducers/plugins/MuIsoDepositProducer.h"
0002 
0003 // Framework
0004 #include "FWCore/Framework/interface/Event.h"
0005 #include "FWCore/Framework/interface/EventSetup.h"
0006 #include "FWCore/Framework/interface/ConsumesCollector.h"
0007 #include "DataFormats/Common/interface/Handle.h"
0008 
0009 #include "DataFormats/RecoCandidate/interface/IsoDeposit.h"
0010 #include "DataFormats/RecoCandidate/interface/IsoDepositFwd.h"
0011 #include "DataFormats/Common/interface/ValueMap.h"
0012 #include "DataFormats/TrackReco/interface/Track.h"
0013 #include "DataFormats/MuonReco/interface/Muon.h"
0014 #include "DataFormats/MuonReco/interface/MuonFwd.h"
0015 
0016 #include "RecoMuon/MuonIsolation/interface/Range.h"
0017 #include "DataFormats/RecoCandidate/interface/IsoDepositDirection.h"
0018 
0019 #include "PhysicsTools/IsolationAlgos/interface/IsoDepositExtractor.h"
0020 #include "PhysicsTools/IsolationAlgos/interface/IsoDepositExtractorFactory.h"
0021 
0022 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0023 #include <string>
0024 
0025 using namespace edm;
0026 using namespace std;
0027 using namespace reco;
0028 using namespace muonisolation;
0029 
0030 //! constructor with config
0031 MuIsoDepositProducer::MuIsoDepositProducer(const ParameterSet& par)
0032     : theDepositNames(std::vector<std::string>(1, std::string())) {
0033   static const std::string metname = "RecoMuon|MuonIsolationProducers|MuIsoDepositProducer";
0034   LogDebug(metname) << " MuIsoDepositProducer CTOR";
0035 
0036   edm::ParameterSet ioPSet = par.getParameter<edm::ParameterSet>("IOPSet");
0037 
0038   theInputType = ioPSet.getParameter<std::string>("InputType");
0039   theExtractForCandidate = ioPSet.getParameter<bool>("ExtractForCandidate");
0040   theMuonTrackRefType = ioPSet.getParameter<std::string>("MuonTrackRefType");
0041 
0042   bool readFromRecoTrack = theInputType == "TrackCollection";
0043   bool readFromRecoMuon = theInputType == "MuonCollection";
0044   bool readFromCandidateView = theInputType == "CandidateView";
0045   if (readFromRecoTrack) {
0046     theMuonCollectionTag = consumes<View<Track>>(ioPSet.getParameter<edm::InputTag>("inputMuonCollection"));
0047   } else if (readFromRecoMuon) {
0048     theMuonCollectionTag = consumes<View<RecoCandidate>>(ioPSet.getParameter<edm::InputTag>("inputMuonCollection"));
0049   } else if (readFromCandidateView) {
0050     theMuonCollectionTag = consumes<View<Candidate>>(ioPSet.getParameter<edm::InputTag>("inputMuonCollection"));
0051   } else {
0052     throw cms::Exception("Configuration") << "Inconsistent configuration or failure to read Candidate-muon view";
0053   }
0054 
0055   theMultipleDepositsFlag = ioPSet.getParameter<bool>("MultipleDepositsFlag");
0056 
0057   if (theMultipleDepositsFlag) {
0058     theDepositNames = par.getParameter<edm::ParameterSet>("ExtractorPSet")
0059                           .getParameter<std::vector<std::string>>("DepositInstanceLabels");
0060   }
0061 
0062   for (unsigned int i = 0; i < theDepositNames.size(); ++i) {
0063     std::string alias = par.getParameter<std::string>("@module_label");
0064     if (!theDepositNames[i].empty())
0065       alias += "_" + theDepositNames[i];
0066     produces<reco::IsoDepositMap>(theDepositNames[i]).setBranchAlias(alias);
0067   }
0068 
0069   edm::ParameterSet extractorPSet = par.getParameter<edm::ParameterSet>("ExtractorPSet");
0070   std::string extractorName = extractorPSet.getParameter<std::string>("ComponentName");
0071   theExtractor = IsoDepositExtractorFactory::get()->create(extractorName, extractorPSet, consumesCollector());
0072   LogDebug(metname) << " Load extractor..." << extractorName;
0073 }
0074 
0075 //! destructor
0076 MuIsoDepositProducer::~MuIsoDepositProducer() {
0077   LogDebug("RecoMuon/MuIsoDepositProducer") << " MuIsoDepositProducer DTOR";
0078 }
0079 
0080 //! build deposits
0081 void MuIsoDepositProducer::produce(Event& event, const EventSetup& eventSetup) {
0082   static const std::string metname = "RecoMuon|MuonIsolationProducers|MuIsoDepositProducer";
0083 
0084   LogDebug(metname) << " Muon Deposit producing..."
0085                     << " BEGINING OF EVENT "
0086                     << "================================";
0087 
0088   unsigned int nDeps = theMultipleDepositsFlag ? theDepositNames.size() : 1;
0089 
0090   // Take the muon container
0091   LogTrace(metname) << " Taking the muons: "
0092                     << theMuonCollectionTag.index();  //a more friendly print would use "inputMuonCollection"
0093   Handle<View<Track>> tracks;
0094   //! read them as RecoCandidates: need to have track() standAloneMuon() etc in the interface
0095   Handle<View<RecoCandidate>> muons;  //! get rid of this at some point and use the cands
0096   Handle<View<Candidate>> cands;
0097 
0098   unsigned int nMuons = 0;
0099 
0100   bool readFromRecoTrack = theInputType == "TrackCollection";
0101   bool readFromRecoMuon = theInputType == "MuonCollection";
0102   bool readFromCandidateView = theInputType == "CandidateView";
0103 
0104   if (readFromRecoMuon) {
0105     event.getByToken(theMuonCollectionTag, muons);
0106     nMuons = muons->size();
0107     LogDebug(metname) << "Got Muons of size " << nMuons;
0108   }
0109   if (readFromRecoTrack) {
0110     event.getByToken(theMuonCollectionTag, tracks);
0111     nMuons = tracks->size();
0112     LogDebug(metname) << "Got MuonTracks of size " << nMuons;
0113   }
0114   if (readFromCandidateView || theExtractForCandidate) {
0115     event.getByToken(theMuonCollectionTag, cands);
0116     unsigned int nCands = cands->size();
0117     if (readFromRecoMuon && theExtractForCandidate) {
0118       //! expect nMuons set already
0119       if (nMuons != nCands)
0120         edm::LogError(metname) << "Inconsistent configuration or failure to read Candidate-muon view";
0121     }
0122     nMuons = nCands;
0123     LogDebug(metname) << "Got candidate view with size " << nMuons;
0124   }
0125 
0126   static const unsigned int MAX_DEPS = 10;
0127   std::unique_ptr<reco::IsoDepositMap> depMaps[MAX_DEPS];
0128 
0129   if (nDeps > 10)
0130     LogError(metname) << "Unable to handle more than 10 input deposits";
0131   for (unsigned int i = 0; i < nDeps; ++i) {
0132     depMaps[i] = std::make_unique<reco::IsoDepositMap>();
0133   }
0134 
0135   //! OK, now we know how many deps for how many muons each we will create
0136   //! might linearize this at some point (lazy)
0137   //! do it in case some muons are there only
0138   if (nMuons > 0) {
0139     std::vector<std::vector<IsoDeposit>> deps2D(nDeps, std::vector<IsoDeposit>(nMuons));
0140 
0141     for (unsigned int i = 0; i < nMuons; ++i) {
0142       TrackBaseRef muRef;
0143       if (readFromRecoMuon) {
0144         if (theMuonTrackRefType == "track") {
0145           muRef = TrackBaseRef((*muons)[i].track());
0146         } else if (theMuonTrackRefType == "standAloneMuon") {
0147           muRef = TrackBaseRef((*muons)[i].standAloneMuon());
0148         } else if (theMuonTrackRefType == "combinedMuon") {
0149           muRef = TrackBaseRef((*muons)[i].combinedMuon());
0150         } else if (theMuonTrackRefType == "bestGlbTrkSta") {
0151           if (!(*muons)[i].combinedMuon().isNull()) {
0152             muRef = TrackBaseRef((*muons)[i].combinedMuon());
0153           } else if (!(*muons)[i].track().isNull()) {
0154             muRef = TrackBaseRef((*muons)[i].track());
0155           } else {
0156             muRef = TrackBaseRef((*muons)[i].standAloneMuon());
0157           }
0158         } else if (theMuonTrackRefType == "bestTrkSta") {
0159           if (!(*muons)[i].track().isNull()) {
0160             muRef = TrackBaseRef((*muons)[i].track());
0161           } else {
0162             muRef = TrackBaseRef((*muons)[i].standAloneMuon());
0163           }
0164         } else {
0165           edm::LogWarning(metname) << "Wrong track type is supplied: breaking";
0166           break;
0167         }
0168       } else if (readFromRecoTrack) {
0169         muRef = TrackBaseRef(tracks, i);
0170       }
0171 
0172       if (!theMultipleDepositsFlag) {
0173         if (readFromCandidateView || theExtractForCandidate)
0174           deps2D[0][i] = theExtractor->deposit(event, eventSetup, (*cands)[i]);
0175         else
0176           deps2D[0][i] = theExtractor->deposit(event, eventSetup, muRef);
0177 
0178       } else {
0179         std::vector<IsoDeposit> deps(nDeps);
0180         if (readFromCandidateView || theExtractForCandidate)
0181           deps = theExtractor->deposits(event, eventSetup, (*cands)[i]);
0182         else
0183           deps = theExtractor->deposits(event, eventSetup, muRef);
0184         for (unsigned int iDep = 0; iDep < nDeps; ++iDep) {
0185           deps2D[iDep][i] = deps[iDep];
0186         }
0187       }
0188     }  //! end for (nMuons)
0189 
0190     //! now fill in selectively
0191     for (unsigned int iDep = 0; iDep < nDeps; ++iDep) {
0192       //!some debugging stuff
0193       for (unsigned int iMu = 0; iMu < nMuons; ++iMu) {
0194         LogTrace(metname) << "Contents of " << theDepositNames[iDep] << " for a muon at index " << iMu;
0195         LogTrace(metname) << deps2D[iDep][iMu].print();
0196       }
0197 
0198       //! fill the maps here
0199       reco::IsoDepositMap::Filler filler(*depMaps[iDep]);
0200 
0201       //!now figure out the source handle (see getByToken above)
0202       if (readFromRecoMuon) {
0203         filler.insert(muons, deps2D[iDep].begin(), deps2D[iDep].end());
0204       } else if (readFromRecoTrack) {
0205         filler.insert(tracks, deps2D[iDep].begin(), deps2D[iDep].end());
0206       } else if (readFromCandidateView) {
0207         filler.insert(cands, deps2D[iDep].begin(), deps2D[iDep].end());
0208       } else {
0209         edm::LogError(metname) << "Inconsistent configuration: unknown type requested";
0210       }
0211 
0212       //! now actually fill
0213       filler.fill();
0214     }
0215   }  //! end if (nMuons>0)
0216 
0217   for (unsigned int iMap = 0; iMap < nDeps; ++iMap) {
0218     LogTrace(metname) << "About to put a deposit named " << theDepositNames[iMap] << " of size "
0219                       << depMaps[iMap]->size() << " into edm::Event";
0220     event.put(std::move(depMaps[iMap]), theDepositNames[iMap]);
0221   }
0222 
0223   LogTrace(metname) << " END OF EVENT "
0224                     << "================================";
0225 }
0226 
0227 #include "FWCore/PluginManager/interface/ModuleDef.h"
0228 #include "FWCore/Framework/interface/MakerMacros.h"
0229 DEFINE_FWK_MODULE(MuIsoDepositProducer);