Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-04-29 23:10:54

0001 #include "DataFormats/Common/interface/AssociationVector.h"
0002 #include "DataFormats/Common/interface/Handle.h"
0003 #include "DataFormats/Common/interface/RefToBaseProd.h"
0004 #include "DataFormats/GsfTrackReco/interface/GsfTrack.h"
0005 #include "DataFormats/MuonReco/interface/Muon.h"
0006 #include "DataFormats/RecoCandidate/interface/IsoDeposit.h"
0007 #include "DataFormats/RecoCandidate/interface/IsoDepositDirection.h"
0008 #include "DataFormats/RecoCandidate/interface/IsoDepositFwd.h"
0009 #include "DataFormats/RecoCandidate/interface/RecoCandidate.h"
0010 #include "DataFormats/TrackReco/interface/Track.h"
0011 #include "FWCore/Framework/interface/ConsumesCollector.h"
0012 #include "FWCore/Framework/interface/Event.h"
0013 #include "FWCore/Framework/interface/EventSetup.h"
0014 #include "FWCore/Framework/interface/stream/EDProducer.h"
0015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0017 #include "PhysicsTools/IsolationAlgos/interface/IsoDepositExtractor.h"
0018 #include "PhysicsTools/IsolationAlgos/interface/IsoDepositExtractorFactory.h"
0019 
0020 #include <string>
0021 
0022 class CandIsoDepositProducer : public edm::stream::EDProducer<> {
0023 public:
0024   CandIsoDepositProducer(const edm::ParameterSet &);
0025 
0026   ~CandIsoDepositProducer() override;
0027 
0028   void produce(edm::Event &, const edm::EventSetup &) override;
0029 
0030 private:
0031   inline const reco::Track *extractTrack(const reco::Candidate &cand, reco::Track *dummyStorage) const;
0032   enum TrackType { FakeT, BestT, StandAloneMuonT, CombinedMuonT, TrackT, GsfT, CandidateT };
0033   edm::ParameterSet theConfig;
0034   edm::EDGetTokenT<edm::View<reco::Candidate> > theCandCollectionToken;
0035   TrackType theTrackType;
0036   std::vector<std::string> theDepositNames;
0037   bool theMultipleDepositsFlag;
0038   std::unique_ptr<reco::isodeposit::IsoDepositExtractor> theExtractor;
0039 };
0040 
0041 using namespace edm;
0042 using namespace reco;
0043 
0044 /// constructor with config
0045 CandIsoDepositProducer::CandIsoDepositProducer(const ParameterSet &par)
0046     : theConfig(par),
0047       theCandCollectionToken(consumes<edm::View<reco::Candidate> >(par.getParameter<edm::InputTag>("src"))),
0048       theDepositNames(std::vector<std::string>(1, "")),
0049       theMultipleDepositsFlag(par.getParameter<bool>("MultipleDepositsFlag")) {
0050   LogDebug("PhysicsTools|MuonIsolation") << " CandIsoDepositProducer CTOR";
0051 
0052   edm::ParameterSet extractorPSet = theConfig.getParameter<edm::ParameterSet>("ExtractorPSet");
0053   std::string extractorName = extractorPSet.getParameter<std::string>("ComponentName");
0054   theExtractor = IsoDepositExtractorFactory::get()->create(extractorName, extractorPSet, consumesCollector());
0055 
0056   if (!theMultipleDepositsFlag)
0057     produces<reco::IsoDepositMap>();
0058   else {
0059     theDepositNames = extractorPSet.getParameter<std::vector<std::string> >("DepositInstanceLabels");
0060     if (theDepositNames.size() > 10)
0061       throw cms::Exception("Configuration Error") << "This module supports only up to 10 deposits";
0062     for (unsigned int iDep = 0; iDep < theDepositNames.size(); ++iDep) {
0063       produces<reco::IsoDepositMap>(theDepositNames[iDep]);
0064     }
0065   }
0066 
0067   std::string trackType = par.getParameter<std::string>("trackType");
0068   if (trackType == "fake")
0069     theTrackType = FakeT;
0070   else if (trackType == "best")
0071     theTrackType = BestT;
0072   else if (trackType == "standAloneMuon")
0073     theTrackType = StandAloneMuonT;
0074   else if (trackType == "combinedMuon")
0075     theTrackType = CombinedMuonT;
0076   else if (trackType == "trackerMuon")
0077     theTrackType = TrackT;
0078   else if (trackType == "track")
0079     theTrackType = TrackT;
0080   else if (trackType == "gsf")
0081     theTrackType = GsfT;
0082   else if (trackType == "candidate")
0083     theTrackType = CandidateT;
0084   else
0085     throw cms::Exception("Error") << "Track type " << trackType << " not valid.";
0086 }
0087 
0088 /// destructor
0089 CandIsoDepositProducer::~CandIsoDepositProducer() {
0090   LogDebug("PhysicsTools/CandIsoDepositProducer") << " CandIsoDepositProducer DTOR";
0091 }
0092 
0093 inline const reco::Track *CandIsoDepositProducer::extractTrack(const reco::Candidate &c, reco::Track *dummy) const {
0094   if (theTrackType == CandidateT) {
0095     return nullptr;
0096   } else if (theTrackType == FakeT) {
0097     *dummy = Track(10, 10, c.vertex(), c.momentum(), c.charge(), reco::Track::CovarianceMatrix());
0098     return dummy;
0099   } else {
0100     const RecoCandidate *rc = dynamic_cast<const RecoCandidate *>(&c);
0101     if (rc == nullptr)
0102       throw cms::Exception("Error") << " Candidate is not RecoCandidate: can't get a real track from it!";
0103     switch (theTrackType) {
0104       case FakeT:
0105         break;  // to avoid warning
0106       case CandidateT:
0107         break;  // to avoid warning
0108       case BestT:
0109         return rc->bestTrack();
0110         break;
0111       case StandAloneMuonT:
0112         return &*rc->standAloneMuon();
0113         break;
0114       case CombinedMuonT:
0115         return &*rc->combinedMuon();
0116         break;
0117       case TrackT:
0118         return &*rc->track();
0119         break;
0120       case GsfT:
0121         return static_cast<const Track *>(rc->gsfTrack().get());
0122         break;
0123     }
0124     return nullptr;
0125   }
0126 }
0127 
0128 /// build deposits
0129 void CandIsoDepositProducer::produce(Event &event, const EventSetup &eventSetup) {
0130   static const std::string metname = "CandIsoDepositProducer";
0131 
0132   edm::Handle<edm::View<reco::Candidate> > hCands;
0133   event.getByToken(theCandCollectionToken, hCands);
0134 
0135   unsigned int nDeps = theMultipleDepositsFlag ? theDepositNames.size() : 1;
0136 
0137   static const unsigned int MAX_DEPS = 10;
0138 
0139   if (nDeps > MAX_DEPS)
0140     LogError(metname) << "Unable to handle more than 10 input deposits";
0141 
0142   //! OK, now we know how many deps for how many muons each we will create
0143   //! might linearize this at some point (lazy)
0144   //! do it in case some muons are there only
0145   size_t nMuons = hCands->size();
0146   std::vector<std::vector<IsoDeposit> > deps2D(nDeps, std::vector<IsoDeposit>(nMuons));
0147 
0148   if (nMuons > 0) {
0149     theExtractor->initEvent(event, eventSetup);
0150 
0151     Track dummy;
0152     for (size_t i = 0; i < nMuons; ++i) {
0153       const Candidate &c = (*hCands)[i];
0154       const Track *track = extractTrack(c, &dummy);
0155       if ((theTrackType != CandidateT) && (!track)) {
0156         edm::LogWarning("CandIsoDepositProducer")
0157             << "Candidate #" << i << " has no bestTrack(), it will produce no deposit";
0158         reco::IsoDeposit emptyDep;
0159         for (size_t iDep = 0; iDep < nDeps; ++iDep) {
0160           deps2D[iDep][i] = emptyDep;  //! well, it is empty already by construction, but still
0161         }
0162         continue;
0163       }
0164       if (!theMultipleDepositsFlag) {
0165         deps2D[0][i] = ((theTrackType == CandidateT) ? theExtractor->deposit(event, eventSetup, c)
0166                                                      : theExtractor->deposit(event, eventSetup, *track));
0167       } else {
0168         std::vector<IsoDeposit> deps =
0169             ((theTrackType == CandidateT) ? theExtractor->deposits(event, eventSetup, c)
0170                                           : theExtractor->deposits(event, eventSetup, *track));
0171         for (unsigned int iDep = 0; iDep < nDeps; ++iDep) {
0172           deps2D[iDep][i] = deps[iDep];
0173         }
0174       }
0175     }  //! for(i<nMuons)
0176   }    //if (nMuons>0)
0177 
0178   //! now fill in selectively
0179   for (unsigned int iDep = 0; iDep < nDeps; ++iDep) {
0180     //!some debugging stuff
0181     for (unsigned int iMu = 0; iMu < nMuons; ++iMu) {
0182       LogTrace(metname) << "Contents of " << theDepositNames[iDep] << " for a muon at index " << iMu;
0183       LogTrace(metname) << deps2D[iDep][iMu].print();
0184     }
0185 
0186     //! fill the maps here
0187     auto depMap = std::make_unique<reco::IsoDepositMap>();
0188     reco::IsoDepositMap::Filler filler(*depMap);
0189     filler.insert(hCands, deps2D[iDep].begin(), deps2D[iDep].end());
0190     deps2D[iDep].clear();
0191     filler.fill();
0192     event.put(std::move(depMap), theDepositNames[iDep]);
0193   }  //! for(iDep<nDeps)
0194 }
0195 
0196 #include "FWCore/Framework/interface/MakerMacros.h"
0197 DEFINE_FWK_MODULE(CandIsoDepositProducer);