Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:55:56

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