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