Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-30 02:33:26

0001 #include "FWCore/Framework/interface/Event.h"
0002 #include "FWCore/Framework/interface/stream/EDProducer.h"
0003 #include "RecoParticleFlow/PFClusterProducer/interface/InitialClusteringStepBase.h"
0004 #include "RecoParticleFlow/PFClusterProducer/interface/PFCPositionCalculatorBase.h"
0005 #include "RecoParticleFlow/PFClusterProducer/interface/PFClusterBuilderBase.h"
0006 #include "RecoParticleFlow/PFClusterProducer/interface/PFClusterEnergyCorrectorBase.h"
0007 #include "RecoParticleFlow/PFClusterProducer/interface/RecHitTopologicalCleanerBase.h"
0008 #include "RecoParticleFlow/PFClusterProducer/interface/SeedFinderBase.h"
0009 
0010 #include <memory>
0011 
0012 class PFClusterProducer : public edm::stream::EDProducer<> {
0013   typedef RecHitTopologicalCleanerBase RHCB;
0014   typedef InitialClusteringStepBase ICSB;
0015   typedef PFClusterBuilderBase PFCBB;
0016   typedef PFCPositionCalculatorBase PosCalc;
0017 
0018 public:
0019   PFClusterProducer(const edm::ParameterSet&);
0020   ~PFClusterProducer() override = default;
0021 
0022   void beginLuminosityBlock(const edm::LuminosityBlock&, const edm::EventSetup&) override;
0023   void produce(edm::Event&, const edm::EventSetup&) override;
0024 
0025 private:
0026   // inputs
0027   edm::EDGetTokenT<reco::PFRecHitCollection> _rechitsLabel;
0028   // options
0029   const bool _prodInitClusters;
0030   // the actual algorithm
0031   std::vector<std::unique_ptr<RecHitTopologicalCleanerBase> > _cleaners;
0032   std::vector<std::unique_ptr<RecHitTopologicalCleanerBase> > _seedcleaners;
0033   std::unique_ptr<SeedFinderBase> _seedFinder;
0034   std::unique_ptr<InitialClusteringStepBase> _initialClustering;
0035   std::unique_ptr<PFClusterBuilderBase> _pfClusterBuilder;
0036   std::unique_ptr<PFCPositionCalculatorBase> _positionReCalc;
0037   std::unique_ptr<PFClusterEnergyCorrectorBase> _energyCorrector;
0038 };
0039 
0040 #include "FWCore/Framework/interface/MakerMacros.h"
0041 DEFINE_FWK_MODULE(PFClusterProducer);
0042 
0043 #ifdef PFLOW_DEBUG
0044 #define LOGVERB(x) edm::LogVerbatim(x)
0045 #define LOGWARN(x) edm::LogWarning(x)
0046 #define LOGERR(x) edm::LogError(x)
0047 #define LOGDRESSED(x) edm::LogInfo(x)
0048 #else
0049 #define LOGVERB(x) LogTrace(x)
0050 #define LOGWARN(x) edm::LogWarning(x)
0051 #define LOGERR(x) edm::LogError(x)
0052 #define LOGDRESSED(x) LogDebug(x)
0053 #endif
0054 
0055 PFClusterProducer::PFClusterProducer(const edm::ParameterSet& conf)
0056     : _prodInitClusters(conf.getUntrackedParameter<bool>("prodInitialClusters", false)) {
0057   _rechitsLabel = consumes<reco::PFRecHitCollection>(conf.getParameter<edm::InputTag>("recHitsSource"));
0058   edm::ConsumesCollector cc = consumesCollector();
0059 
0060   //setup rechit cleaners
0061   const edm::VParameterSet& cleanerConfs = conf.getParameterSetVector("recHitCleaners");
0062   for (const auto& conf : cleanerConfs) {
0063     const std::string& cleanerName = conf.getParameter<std::string>("algoName");
0064     _cleaners.emplace_back(RecHitTopologicalCleanerFactory::get()->create(cleanerName, conf, cc));
0065   }
0066 
0067   if (conf.exists("seedCleaners")) {
0068     const edm::VParameterSet& seedcleanerConfs = conf.getParameterSetVector("seedCleaners");
0069 
0070     for (const auto& conf : seedcleanerConfs) {
0071       const std::string& seedcleanerName = conf.getParameter<std::string>("algoName");
0072       _seedcleaners.emplace_back(RecHitTopologicalCleanerFactory::get()->create(seedcleanerName, conf, cc));
0073     }
0074   }
0075 
0076   // setup seed finding
0077   const edm::ParameterSet& sfConf = conf.getParameterSet("seedFinder");
0078   const std::string& sfName = sfConf.getParameter<std::string>("algoName");
0079   _seedFinder = SeedFinderFactory::get()->create(sfName, sfConf);
0080   //setup topo cluster builder
0081   const edm::ParameterSet& initConf = conf.getParameterSet("initialClusteringStep");
0082   const std::string& initName = initConf.getParameter<std::string>("algoName");
0083   _initialClustering = InitialClusteringStepFactory::get()->create(initName, initConf, cc);
0084   //setup pf cluster builder if requested
0085   const edm::ParameterSet& pfcConf = conf.getParameterSet("pfClusterBuilder");
0086   if (!pfcConf.empty()) {
0087     const std::string& pfcName = pfcConf.getParameter<std::string>("algoName");
0088     _pfClusterBuilder = PFClusterBuilderFactory::get()->create(pfcName, pfcConf, cc);
0089   }
0090   //setup (possible) recalcuation of positions
0091   const edm::ParameterSet& pConf = conf.getParameterSet("positionReCalc");
0092   if (!pConf.empty()) {
0093     const std::string& pName = pConf.getParameter<std::string>("algoName");
0094     _positionReCalc = PFCPositionCalculatorFactory::get()->create(pName, pConf, cc);
0095   }
0096   // see if new need to apply corrections, setup if there.
0097   const edm::ParameterSet& cConf = conf.getParameterSet("energyCorrector");
0098   if (!cConf.empty()) {
0099     const std::string& cName = cConf.getParameter<std::string>("algoName");
0100     _energyCorrector = PFClusterEnergyCorrectorFactory::get()->create(cName, cConf);
0101   }
0102 
0103   if (_prodInitClusters) {
0104     produces<reco::PFClusterCollection>("initialClusters");
0105   }
0106   produces<reco::PFClusterCollection>();
0107 }
0108 
0109 void PFClusterProducer::beginLuminosityBlock(const edm::LuminosityBlock& lumi, const edm::EventSetup& es) {
0110   _initialClustering->update(es);
0111   if (_pfClusterBuilder)
0112     _pfClusterBuilder->update(es);
0113   if (_positionReCalc)
0114     _positionReCalc->update(es);
0115   for (const auto& cleaner : _cleaners)
0116     cleaner->update(es);
0117   for (const auto& cleaner : _seedcleaners)
0118     cleaner->update(es);
0119 }
0120 
0121 void PFClusterProducer::produce(edm::Event& e, const edm::EventSetup& es) {
0122   _initialClustering->reset();
0123   if (_pfClusterBuilder)
0124     _pfClusterBuilder->reset();
0125 
0126   edm::Handle<reco::PFRecHitCollection> rechits;
0127   e.getByToken(_rechitsLabel, rechits);
0128 
0129   _initialClustering->updateEvent(e);
0130 
0131   std::vector<bool> mask(rechits->size(), true);
0132   for (const auto& cleaner : _cleaners) {
0133     cleaner->clean(rechits, mask);
0134   }
0135 
0136   // no seeding on these hits
0137   std::vector<bool> seedmask = mask;
0138   for (const auto& cleaner : _seedcleaners) {
0139     cleaner->clean(rechits, seedmask);
0140   }
0141 
0142   std::vector<bool> seedable(rechits->size(), false);
0143   _seedFinder->findSeeds(rechits, seedmask, seedable);
0144 
0145   auto initialClusters = std::make_unique<reco::PFClusterCollection>();
0146   _initialClustering->buildClusters(rechits, mask, seedable, *initialClusters);
0147   LOGVERB("PFClusterProducer::produce()") << *_initialClustering;
0148 
0149   auto pfClusters = std::make_unique<reco::PFClusterCollection>();
0150   pfClusters = std::make_unique<reco::PFClusterCollection>();
0151   if (_pfClusterBuilder) {  // if we've defined a re-clustering step execute it
0152     _pfClusterBuilder->buildClusters(*initialClusters, seedable, *pfClusters);
0153     LOGVERB("PFClusterProducer::produce()") << *_pfClusterBuilder;
0154   } else {
0155     pfClusters->insert(pfClusters->end(), initialClusters->begin(), initialClusters->end());
0156   }
0157 
0158   if (_positionReCalc) {
0159     _positionReCalc->calculateAndSetPositions(*pfClusters);
0160   }
0161 
0162   if (_energyCorrector) {
0163     _energyCorrector->correctEnergies(*pfClusters);
0164   }
0165 
0166   if (_prodInitClusters)
0167     e.put(std::move(initialClusters), "initialClusters");
0168   e.put(std::move(pfClusters));
0169 }