File indexing completed on 2024-04-06 12:18:31
0001 #ifndef HLTrigger_JetMET_plugins_HLTMultiplicityValueProducerFromNestedCollection_h
0002 #define HLTrigger_JetMET_plugins_HLTMultiplicityValueProducerFromNestedCollection_h
0003
0004 #include <memory>
0005
0006 #include "FWCore/Framework/interface/Frameworkfwd.h"
0007 #include "FWCore/Framework/interface/global/EDProducer.h"
0008 #include "FWCore/Framework/interface/Event.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0010 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0011 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0013
0014 template <class INP_TYPE, class OUT_TYPE>
0015 class HLTMultiplicityValueProducerFromNestedCollection : public edm::global::EDProducer<> {
0016 public:
0017 explicit HLTMultiplicityValueProducerFromNestedCollection(edm::ParameterSet const&);
0018 ~HLTMultiplicityValueProducerFromNestedCollection() override {}
0019
0020 static void fillDescriptions(edm::ConfigurationDescriptions&);
0021
0022 protected:
0023 void produce(edm::StreamID, edm::Event&, edm::EventSetup const&) const override;
0024
0025 edm::EDGetTokenT<INP_TYPE> const src_token_;
0026 OUT_TYPE const defaultValue_;
0027 };
0028
0029 template <class INP_TYPE, class OUT_TYPE>
0030 HLTMultiplicityValueProducerFromNestedCollection<INP_TYPE, OUT_TYPE>::HLTMultiplicityValueProducerFromNestedCollection(
0031 edm::ParameterSet const& iConfig)
0032 : src_token_(consumes<INP_TYPE>(iConfig.getParameter<edm::InputTag>("src"))),
0033 defaultValue_(iConfig.getParameter<OUT_TYPE>("defaultValue")) {
0034 produces<OUT_TYPE>();
0035 }
0036
0037 template <class INP_TYPE, class OUT_TYPE>
0038 void HLTMultiplicityValueProducerFromNestedCollection<INP_TYPE, OUT_TYPE>::produce(
0039 edm::StreamID, edm::Event& iEvent, edm::EventSetup const& iSetup) const {
0040 auto const& objHandle(iEvent.getHandle(src_token_));
0041
0042 if (objHandle.isValid()) {
0043 LogDebug("Input") << "size of input collection: " << objHandle->size();
0044
0045 OUT_TYPE objMult(0);
0046 for (auto const& obj : *objHandle) {
0047 objMult += obj.size();
0048 }
0049
0050 LogDebug("Output") << "size of output objects: " << objMult;
0051
0052 iEvent.put(std::make_unique<OUT_TYPE>(objMult));
0053 } else {
0054 iEvent.put(std::make_unique<OUT_TYPE>(defaultValue_));
0055 }
0056 }
0057
0058 template <class INP_TYPE, class OUT_TYPE>
0059 void HLTMultiplicityValueProducerFromNestedCollection<INP_TYPE, OUT_TYPE>::fillDescriptions(
0060 edm::ConfigurationDescriptions& descriptions) {
0061 edm::ParameterSetDescription desc;
0062 desc.add<edm::InputTag>("src", edm::InputTag(""))->setComment("input collection");
0063 desc.add<OUT_TYPE>("defaultValue", 0)->setComment("default output value (used when input collection is unavailable)");
0064 descriptions.addWithDefaultLabel(desc);
0065 }
0066
0067 #endif