Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 //
0002 
0003 /**
0004   \class    ProbeMulteplicityProducer"
0005   \brief    Matcher of number of reconstructed objects in the event to probe 
0006             
0007   \author   Kalanand Mishra
0008 */
0009 
0010 #include "FWCore/Framework/interface/stream/EDProducer.h"
0011 #include "FWCore/Framework/interface/Event.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0013 #include "FWCore/Utilities/interface/InputTag.h"
0014 
0015 #include "DataFormats/Math/interface/deltaR.h"
0016 #include "DataFormats/Common/interface/ValueMap.h"
0017 #include "DataFormats/Common/interface/View.h"
0018 
0019 #include "DataFormats/Candidate/interface/CandidateFwd.h"
0020 #include "DataFormats/Candidate/interface/Candidate.h"
0021 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0022 
0023 class ProbeMulteplicityProducer : public edm::stream::EDProducer<> {
0024 public:
0025   explicit ProbeMulteplicityProducer(const edm::ParameterSet& iConfig);
0026   ~ProbeMulteplicityProducer() override;
0027 
0028   void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0029 
0030 private:
0031   edm::EDGetTokenT<reco::CandidateView> pairs_;
0032   StringCutObjectSelector<reco::Candidate, true>
0033       pairCut_;  // lazy parsing, to allow cutting on variables not in reco::Candidate class
0034   StringCutObjectSelector<reco::Candidate, true>
0035       probeCut_;  // lazy parsing, to allow cutting on variables not in reco::Candidate class
0036 };
0037 
0038 ProbeMulteplicityProducer::ProbeMulteplicityProducer(const edm::ParameterSet& iConfig)
0039     : pairs_(consumes<reco::CandidateView>(iConfig.getParameter<edm::InputTag>("pairs"))),
0040       pairCut_(iConfig.existsAs<std::string>("pairSelection") ? iConfig.getParameter<std::string>("pairSelection") : "",
0041                true),
0042       probeCut_(
0043           iConfig.existsAs<std::string>("probeSelection") ? iConfig.getParameter<std::string>("probeSelection") : "",
0044           true) {
0045   produces<edm::ValueMap<float>>();
0046 }
0047 
0048 ProbeMulteplicityProducer::~ProbeMulteplicityProducer() {}
0049 
0050 void ProbeMulteplicityProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0051   using namespace edm;
0052 
0053   // read input
0054   Handle<View<reco::Candidate>> pairs;
0055   iEvent.getByToken(pairs_, pairs);
0056 
0057   // fill
0058   unsigned int i = 0;
0059   std::vector<unsigned int> tagKeys;
0060   std::vector<float> values;
0061   View<reco::Candidate>::const_iterator pair, endpairs = pairs->end();
0062   for (pair = pairs->begin(); pair != endpairs; ++pair, ++i) {
0063     reco::CandidateBaseRef probeRef = pair->daughter(1)->masterClone();
0064     unsigned int tagKey = pair->daughter(0)->masterClone().key();
0065     unsigned int copies = 1;
0066     if (pairCut_(*pair) && probeCut_(*probeRef)) {
0067       for (unsigned int j = 0; j < i; ++j)
0068         if (tagKeys[j] == tagKey)
0069           copies++;
0070       for (unsigned int j = 0; j < i; ++j)
0071         if (tagKeys[j] == tagKey)
0072           values[j] = copies;
0073     } else {
0074       tagKey = std::numeric_limits<unsigned int>::max();
0075       copies = 0;
0076     }
0077     tagKeys.push_back(tagKey);
0078     values.push_back(copies);
0079   }
0080 
0081   // convert into ValueMap and store
0082   auto valMap = std::make_unique<ValueMap<float>>();
0083   ValueMap<float>::Filler filler(*valMap);
0084   filler.insert(pairs, values.begin(), values.end());
0085   filler.fill();
0086   iEvent.put(std::move(valMap));
0087 }
0088 
0089 #include "FWCore/Framework/interface/MakerMacros.h"
0090 DEFINE_FWK_MODULE(ProbeMulteplicityProducer);