Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-05-10 05:44:10

0001 //
0002 //
0003 
0004 /**
0005   \class    NearbyCandCountComputer NearbyCandCountComputer.h "PhysicsTools/TagAndProbe/interface/NearbyCandCountComputer.h"
0006   \brief    Count candidates near to another candidate, write result in ValueMap
0007 
0008             Implementation notice: not templated, because we want to allow cuts on the pair through PATDiObjectProxy
0009 
0010   \author   Giovanni Petrucciani
0011   \version  $Id: NearbyCandCountComputer.cc,v 1.2 2010/07/09 14:03:51 gpetrucc Exp $
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_;  // lazy parsing, to allow cutting on variables not in reco::Candidate class
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   // read input
0063   Handle<View<reco::Candidate>> probes, objects;
0064   iEvent.getByToken(probesToken_, probes);
0065   iEvent.getByToken(objectsToken_, objects);
0066 
0067   // prepare vector for output
0068   std::vector<float> values;
0069 
0070   // fill
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   // convert into ValueMap and store
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);