Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-10 01:53:54

0001 #include "DataFormats/L1TParticleFlow/interface/PFCandidate.h"
0002 
0003 #include "FWCore/Framework/interface/global/EDProducer.h"
0004 #include "FWCore/Framework/interface/Event.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "FWCore/Utilities/interface/transform.h"
0007 #include "FWCore/Utilities/interface/InputTag.h"
0008 #include "DataFormats/Common/interface/CloneTrait.h"
0009 #include <vector>
0010 
0011 class L1TPFCandMultiMerger : public edm::global::EDProducer<> {
0012 public:
0013   explicit L1TPFCandMultiMerger(const edm::ParameterSet&);
0014   ~L1TPFCandMultiMerger() override;
0015 
0016 private:
0017   void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0018 
0019   std::vector<std::string> instances_, regionalInstances_;
0020   std::vector<bool> alsoRegional_;  // aligned with instances_
0021   std::vector<edm::EDGetTokenT<l1t::PFCandidateCollection>> tokens_;
0022   std::vector<edm::EDGetTokenT<l1t::PFCandidateRegionalOutput>> regionalTokens_;
0023 };
0024 
0025 L1TPFCandMultiMerger::L1TPFCandMultiMerger(const edm::ParameterSet& iConfig)
0026     : instances_(iConfig.getParameter<std::vector<std::string>>("labelsToMerge")),
0027       regionalInstances_(iConfig.getParameter<std::vector<std::string>>("regionalLabelsToMerge")) {
0028   const std::vector<edm::InputTag>& pfProducers = iConfig.getParameter<std::vector<edm::InputTag>>("pfProducers");
0029   tokens_.reserve(instances_.size() * pfProducers.size());
0030   for (const std::string& instance : instances_) {
0031     for (const edm::InputTag& tag : pfProducers) {
0032       tokens_.push_back(consumes<l1t::PFCandidateCollection>(edm::InputTag(tag.label(), instance, tag.process())));
0033     }
0034     produces<l1t::PFCandidateCollection>(instance);
0035     // check if regional output is needed too
0036     if (std::find(regionalInstances_.begin(), regionalInstances_.end(), instance) != regionalInstances_.end()) {
0037       alsoRegional_.push_back(true);
0038       for (const edm::InputTag& tag : pfProducers) {
0039         regionalTokens_.push_back(
0040             consumes<l1t::PFCandidateRegionalOutput>(edm::InputTag(tag.label(), instance + "Regional", tag.process())));
0041       }
0042       produces<l1t::PFCandidateRegionalOutput>(instance + "Regional");
0043     } else {
0044       alsoRegional_.push_back(false);
0045     }
0046   }
0047   // check that regional output is not requested without the standard one
0048   for (const std::string& instance : regionalInstances_) {
0049     auto match = std::find(instances_.begin(), instances_.end(), instance);
0050     if (match == instances_.end()) {
0051       throw cms::Exception("Configuration", "The regional label '" + instance + "' is not in labelsToMerge\n");
0052     }
0053   }
0054 }
0055 
0056 L1TPFCandMultiMerger::~L1TPFCandMultiMerger() {}
0057 
0058 void L1TPFCandMultiMerger::produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup&) const {
0059   edm::Handle<l1t::PFCandidateCollection> handle;
0060   edm::Handle<l1t::PFCandidateRegionalOutput> regionalHandle;
0061   unsigned int ninstances = instances_.size(), nproducers = tokens_.size() / ninstances;
0062   std::vector<int> keys;
0063   for (unsigned int ii = 0, it = 0, irt = 0; ii < ninstances; ++ii) {
0064     auto out = std::make_unique<l1t::PFCandidateCollection>();
0065     std::unique_ptr<l1t::PFCandidateRegionalOutput> regout;
0066     if (alsoRegional_[ii]) {
0067       auto refprod = iEvent.getRefBeforePut<l1t::PFCandidateCollection>(instances_[ii]);
0068       regout = std::make_unique<l1t::PFCandidateRegionalOutput>(edm::RefProd<l1t::PFCandidateCollection>(refprod));
0069     }
0070     for (unsigned int ip = 0; ip < nproducers; ++ip, ++it) {
0071       iEvent.getByToken(tokens_[it], handle);
0072       unsigned int offset = out->size();
0073       out->insert(out->end(), handle->begin(), handle->end());
0074       if (alsoRegional_[ii]) {
0075         iEvent.getByToken(regionalTokens_[irt++], regionalHandle);
0076         const auto& src = *regionalHandle;
0077         for (unsigned int ireg = 0, nreg = src.nRegions(); ireg < nreg; ++ireg) {
0078           auto region = src.region(ireg);
0079           keys.clear();
0080           for (auto iter = region.begin(), iend = region.end(); iter != iend; ++iter) {
0081             keys.push_back(iter.idx() + offset);
0082           }
0083           regout->addRegion(keys, src.eta(ireg), src.phi(ireg));
0084         }
0085       }
0086     }
0087     iEvent.put(std::move(out), instances_[ii]);
0088     if (alsoRegional_[ii]) {
0089       iEvent.put(std::move(regout), instances_[ii] + "Regional");
0090     }
0091   }
0092 }
0093 
0094 #include "FWCore/Framework/interface/MakerMacros.h"
0095 DEFINE_FWK_MODULE(L1TPFCandMultiMerger);