Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 //
0002 
0003 /**
0004   \class    OtherObjectVariableComputer"
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 "DataFormats/TrackReco/interface/Track.h"
0022 #include "DataFormats/VertexReco/interface/Vertex.h"
0023 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0024 #include "CommonTools/Utils/interface/StringObjectFunction.h"
0025 
0026 template <typename T>
0027 class OtherObjectVariableComputer : public edm::stream::EDProducer<> {
0028 public:
0029   explicit OtherObjectVariableComputer(const edm::ParameterSet& iConfig);
0030   ~OtherObjectVariableComputer() override;
0031 
0032   void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0033 
0034 private:
0035   edm::EDGetTokenT<edm::View<reco::Candidate>> probesToken_;
0036   edm::EDGetTokenT<edm::View<T>> objectsToken_;
0037   StringObjectFunction<T, true> objVar_;
0038   double default_;
0039   StringCutObjectSelector<T, true> objCut_;  // lazy parsing, to allow cutting on variables not in reco::Candidate class
0040   bool doSort_;
0041   StringObjectFunction<T, true> objSort_;
0042 };
0043 
0044 template <typename T>
0045 OtherObjectVariableComputer<T>::OtherObjectVariableComputer(const edm::ParameterSet& iConfig)
0046     : probesToken_(consumes<edm::View<reco::Candidate>>(iConfig.getParameter<edm::InputTag>("probes"))),
0047       objectsToken_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("objects"))),
0048       objVar_(iConfig.getParameter<std::string>("expression")),
0049       default_(iConfig.getParameter<double>("default")),
0050       objCut_(
0051           iConfig.existsAs<std::string>("objectSelection") ? iConfig.getParameter<std::string>("objectSelection") : "",
0052           true),
0053       doSort_(iConfig.existsAs<std::string>("objectSortDescendingBy")),
0054       objSort_(doSort_ ? iConfig.getParameter<std::string>("objectSortDescendingBy") : "1", true) {
0055   produces<edm::ValueMap<float>>();
0056 }
0057 
0058 template <typename T>
0059 OtherObjectVariableComputer<T>::~OtherObjectVariableComputer() {}
0060 
0061 template <typename T>
0062 void OtherObjectVariableComputer<T>::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0063   using namespace edm;
0064 
0065   // read input
0066   Handle<View<reco::Candidate>> probes;
0067   Handle<View<T>> objects;
0068   iEvent.getByToken(probesToken_, probes);
0069   iEvent.getByToken(objectsToken_, objects);
0070 
0071   // fill
0072   std::vector<std::pair<double, double>> selected;
0073   typename View<T>::const_iterator object, endobjects = objects->end();
0074   for (object = objects->begin(); object != endobjects; ++object) {
0075     if (objCut_(*object)) {
0076       selected.push_back(std::pair<double, double>(objSort_(*object), objVar_(*object)));
0077       if (!doSort_)
0078         break;  // if we take just the first one, there's no need of computing the others
0079     }
0080   }
0081   if (doSort_ && selected.size() > 1)
0082     std::sort(selected.begin(), selected.end());  // sorts (ascending)
0083 
0084   // prepare vector for output
0085   std::vector<float> values(probes->size(), (selected.empty() ? default_ : selected.back().second));
0086 
0087   // convert into ValueMap and store
0088   auto valMap = std::make_unique<ValueMap<float>>();
0089   ValueMap<float>::Filler filler(*valMap);
0090   filler.insert(probes, values.begin(), values.end());
0091   filler.fill();
0092   iEvent.put(std::move(valMap));
0093 }
0094 
0095 typedef OtherObjectVariableComputer<reco::Candidate> OtherCandVariableComputer;
0096 //typedef OtherObjectVariableComputer<reco::Track>     OtherTrackVariableComputer;
0097 //typedef OtherObjectVariableComputer<reco::Vertex>    OtherVertexVariableComputer;
0098 
0099 #include "FWCore/Framework/interface/MakerMacros.h"
0100 DEFINE_FWK_MODULE(OtherCandVariableComputer);
0101 //DEFINE_FWK_MODULE(OtherTrackVariableComputer);
0102 //DEFINE_FWK_MODULE(OtherVertexVariableComputer);