Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "FWCore/Framework/interface/Event.h"
0002 #include "FWCore/Framework/interface/Frameworkfwd.h"
0003 #include "FWCore/Framework/interface/MakerMacros.h"
0004 #include "FWCore/Framework/interface/stream/EDProducer.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "FWCore/Utilities/interface/RunningAverage.h"
0007 #include "RecoParticleFlow/PFClusterProducer/interface/PFRecHitCreatorBase.h"
0008 #include "RecoParticleFlow/PFClusterProducer/interface/PFRecHitNavigatorBase.h"
0009 
0010 #include <memory>
0011 
0012 //
0013 // class declaration
0014 //
0015 
0016 class PFRecHitProducer final : public edm::stream::EDProducer<> {
0017 public:
0018   explicit PFRecHitProducer(const edm::ParameterSet& iConfig);
0019   ~PFRecHitProducer() override;
0020 
0021   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0022 
0023 private:
0024   void produce(edm::Event&, const edm::EventSetup&) override;
0025   void beginLuminosityBlock(edm::LuminosityBlock const&, const edm::EventSetup&) override;
0026   void endLuminosityBlock(edm::LuminosityBlock const&, const edm::EventSetup&) override;
0027   std::vector<std::unique_ptr<PFRecHitCreatorBase> > creators_;
0028   std::unique_ptr<PFRecHitNavigatorBase> navigator_;
0029   bool init_;
0030 };
0031 
0032 #include "FWCore/Framework/interface/MakerMacros.h"
0033 DEFINE_FWK_MODULE(PFRecHitProducer);
0034 
0035 namespace {
0036   bool sortByDetId(const reco::PFRecHit& a, const reco::PFRecHit& b) { return a.detId() < b.detId(); }
0037 
0038   edm::RunningAverage localRA1;
0039   edm::RunningAverage localRA2;
0040 }  // namespace
0041 
0042 PFRecHitProducer::PFRecHitProducer(const edm::ParameterSet& iConfig) {
0043   produces<reco::PFRecHitCollection>();
0044   produces<reco::PFRecHitCollection>("Cleaned");
0045 
0046   edm::ConsumesCollector cc = consumesCollector();
0047 
0048   std::vector<edm::ParameterSet> creators = iConfig.getParameter<std::vector<edm::ParameterSet> >("producers");
0049   for (auto& creator : creators) {
0050     std::string name = creator.getParameter<std::string>("name");
0051     creators_.emplace_back(PFRecHitFactory::get()->create(name, creator, cc));
0052   }
0053 
0054   edm::ParameterSet navSet = iConfig.getParameter<edm::ParameterSet>("navigator");
0055   navigator_ = PFRecHitNavigationFactory::get()->create(navSet.getParameter<std::string>("name"), navSet, cc);
0056 }
0057 
0058 PFRecHitProducer::~PFRecHitProducer() = default;
0059 
0060 //
0061 // member functions
0062 //
0063 
0064 void PFRecHitProducer::beginLuminosityBlock(edm::LuminosityBlock const& iLumi, const edm::EventSetup& iSetup) {
0065   for (const auto& creator : creators_) {
0066     creator->init(iSetup);
0067   }
0068   navigator_->init(iSetup);
0069 }
0070 
0071 void PFRecHitProducer::endLuminosityBlock(edm::LuminosityBlock const& iLumi, const edm::EventSetup&) {}
0072 
0073 // ------------ method called to produce the data  ------------
0074 void PFRecHitProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0075   using namespace edm;
0076   auto out = std::make_unique<reco::PFRecHitCollection>();
0077   auto cleaned = std::make_unique<reco::PFRecHitCollection>();
0078 
0079   out->reserve(localRA1.upper());
0080   cleaned->reserve(localRA2.upper());
0081 
0082   for (const auto& creator : creators_) {
0083     creator->importRecHits(out, cleaned, iEvent, iSetup);
0084   }
0085 
0086   if (out->capacity() > 2 * out->size())
0087     out->shrink_to_fit();
0088   if (cleaned->capacity() > 2 * cleaned->size())
0089     cleaned->shrink_to_fit();
0090   localRA1.update(out->size());
0091   localRA2.update(cleaned->size());
0092   std::sort(out->begin(), out->end(), sortByDetId);
0093 
0094   //create a refprod here
0095   edm::RefProd<reco::PFRecHitCollection> refProd = iEvent.getRefBeforePut<reco::PFRecHitCollection>();
0096 
0097   for (auto& pfrechit : *out) {
0098     navigator_->associateNeighbours(pfrechit, out, refProd);
0099   }
0100 
0101   iEvent.put(std::move(out), "");
0102   iEvent.put(std::move(cleaned), "Cleaned");
0103 }
0104 
0105 void PFRecHitProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0106   //The following says we do not know what parameters are allowed so do no validation
0107   // Please change this to state exactly what you do use, even if it is no parameters
0108   edm::ParameterSetDescription desc;
0109   desc.setUnknown();
0110   descriptions.addDefault(desc);
0111 }