File indexing completed on 2023-03-17 11:16:43
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include "FWCore/Framework/interface/stream/EDProducer.h"
0015 #include "FWCore/Framework/interface/Event.h"
0016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0017 #include "FWCore/Utilities/interface/InputTag.h"
0018
0019 #include "DataFormats/Math/interface/deltaR.h"
0020 #include "DataFormats/Common/interface/ValueMap.h"
0021 #include "DataFormats/Common/interface/View.h"
0022
0023 #include "DataFormats/Candidate/interface/CandidateFwd.h"
0024 #include "DataFormats/Candidate/interface/Candidate.h"
0025 #include "PhysicsTools/PatUtils/interface/PATDiObjectProxy.h"
0026
0027 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0028
0029 class NearbyCandCountComputer : public edm::stream::EDProducer<> {
0030 public:
0031 explicit NearbyCandCountComputer(const edm::ParameterSet& iConfig);
0032 ~NearbyCandCountComputer() override;
0033
0034 void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0035
0036 private:
0037 edm::EDGetTokenT<edm::View<reco::Candidate>> probesToken_;
0038 edm::EDGetTokenT<edm::View<reco::Candidate>> objectsToken_;
0039 double deltaR2_;
0040 StringCutObjectSelector<reco::Candidate, true>
0041 objCut_;
0042 StringCutObjectSelector<pat::DiObjectProxy, true> pairCut_;
0043 };
0044
0045 NearbyCandCountComputer::NearbyCandCountComputer(const edm::ParameterSet& iConfig)
0046 : probesToken_(consumes<edm::View<reco::Candidate>>(iConfig.getParameter<edm::InputTag>("probes"))),
0047 objectsToken_(consumes<edm::View<reco::Candidate>>(iConfig.getParameter<edm::InputTag>("objects"))),
0048 deltaR2_(std::pow(iConfig.getParameter<double>("deltaR"), 2)),
0049 objCut_(
0050 iConfig.existsAs<std::string>("objectSelection") ? iConfig.getParameter<std::string>("objectSelection") : "",
0051 true),
0052 pairCut_(iConfig.existsAs<std::string>("pairSelection") ? iConfig.getParameter<std::string>("pairSelection") : "",
0053 true) {
0054 produces<edm::ValueMap<float>>();
0055 }
0056
0057 NearbyCandCountComputer::~NearbyCandCountComputer() {}
0058
0059 void NearbyCandCountComputer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0060 using namespace edm;
0061
0062
0063 Handle<View<reco::Candidate>> probes, objects;
0064 iEvent.getByToken(probesToken_, probes);
0065 iEvent.getByToken(objectsToken_, objects);
0066
0067
0068 std::vector<float> values;
0069
0070
0071 View<reco::Candidate>::const_iterator probe, endprobes = probes->end();
0072 View<reco::Candidate>::const_iterator object, beginobjects = objects->begin(), endobjects = objects->end();
0073 for (probe = probes->begin(); probe != endprobes; ++probe) {
0074 float count = 0;
0075 for (object = beginobjects; object != endobjects; ++object) {
0076 if ((deltaR2(*probe, *object) < deltaR2_) && objCut_(*object) && pairCut_(pat::DiObjectProxy(*probe, *object))) {
0077 count++;
0078 }
0079 }
0080 values.push_back(count);
0081 }
0082
0083
0084 auto valMap = std::make_unique<ValueMap<float>>();
0085 ValueMap<float>::Filler filler(*valMap);
0086 filler.insert(probes, values.begin(), values.end());
0087 filler.fill();
0088 iEvent.put(std::move(valMap));
0089 }
0090
0091 #include "FWCore/Framework/interface/MakerMacros.h"
0092 DEFINE_FWK_MODULE(NearbyCandCountComputer);