Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 
0002 /*****************************************************************************
0003  * Project: CMS detector at the CERN
0004  *
0005  * Package: PhysicsTools/TagAndProbe
0006  *
0007  *
0008  * Authors:
0009  *
0010  *   Kalanand Mishra, Fermilab - kalanand@fnal.gov
0011  *
0012  * Description:
0013  *   - Matches a given object with other objects using deltaR-matching.
0014  *   - For example: can match a photon with track within a given deltaR.
0015  *   - Saves collection of the reference vectors of matched objects.
0016  * History:
0017  *
0018  *
0019  *****************************************************************************/
0020 ////////////////////////////////////////////////////////////////////////////////
0021 // Includes
0022 ////////////////////////////////////////////////////////////////////////////////
0023 #include "FWCore/Framework/interface/global/EDProducer.h"
0024 #include "FWCore/Framework/interface/Event.h"
0025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0026 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0027 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0028 #include "FWCore/Utilities/interface/InputTag.h"
0029 #include "FWCore/Utilities/interface/transform.h"
0030 
0031 #include "DataFormats/Common/interface/View.h"
0032 #include "DataFormats/Math/interface/deltaR.h"
0033 
0034 #include "DataFormats/MuonReco/interface/Muon.h"
0035 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
0036 #include "DataFormats/EgammaCandidates/interface/Electron.h"
0037 #include "DataFormats/JetReco/interface/Jet.h"
0038 #include "DataFormats/EgammaCandidates/interface/Photon.h"
0039 #include "DataFormats/TrackReco/interface/Track.h"
0040 
0041 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0042 
0043 #include <memory>
0044 #include <vector>
0045 #include <sstream>
0046 
0047 ////////////////////////////////////////////////////////////////////////////////
0048 // class definition
0049 ////////////////////////////////////////////////////////////////////////////////
0050 
0051 namespace ovm {
0052   template <typename T1, typename T2>
0053   struct StreamCache {
0054     StreamCache(std::string const& cut, std::string const& match) : objCut_(cut, true), objMatchCut_(match, true) {}
0055     StringCutObjectSelector<T1, true>
0056         objCut_;  // lazy parsing, to allow cutting on variables not in reco::Candidate class
0057     StringCutObjectSelector<T2, true>
0058         objMatchCut_;  // lazy parsing, to allow cutting on variables not in reco::Candidate class
0059   };
0060 }  // namespace ovm
0061 
0062 template <typename T1, typename T2>
0063 class ObjectViewMatcher : public edm::global::EDProducer<edm::StreamCache<ovm::StreamCache<T1, T2>>> {
0064 public:
0065   // construction/destruction
0066   ObjectViewMatcher(const edm::ParameterSet& iConfig);
0067   ~ObjectViewMatcher() override;
0068 
0069   // member functions
0070   std::unique_ptr<ovm::StreamCache<T1, T2>> beginStream(edm::StreamID) const override;
0071   void produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const override;
0072   void endJob() override;
0073 
0074   static void fillDescriptions(edm::ConfigurationDescriptions&);
0075 
0076 private:
0077   // member data
0078   edm::EDGetTokenT<edm::View<T1>> srcCandsToken_;
0079   std::vector<edm::EDGetTokenT<edm::View<T2>>> srcObjectsTokens_;
0080   double deltaRMax_;
0081 
0082   std::string moduleLabel_;
0083   std::string cut_;
0084   std::string match_;
0085 
0086   mutable std::atomic<unsigned int> nObjectsTot_;
0087   mutable std::atomic<unsigned int> nObjectsMatch_;
0088 };
0089 
0090 ////////////////////////////////////////////////////////////////////////////////
0091 // construction/destruction
0092 ////////////////////////////////////////////////////////////////////////////////
0093 
0094 //______________________________________________________________________________
0095 template <typename T1, typename T2>
0096 ObjectViewMatcher<T1, T2>::ObjectViewMatcher(const edm::ParameterSet& iConfig)
0097     : srcCandsToken_(this->consumes(iConfig.getParameter<edm::InputTag>("srcObject"))),
0098       srcObjectsTokens_(edm::vector_transform(
0099           iConfig.getParameter<std::vector<edm::InputTag>>("srcObjectsToMatch"),
0100           [this](edm::InputTag const& tag) { return this->template consumes<edm::View<T2>>(tag); })),
0101       deltaRMax_(iConfig.getParameter<double>("deltaRMax")),
0102       moduleLabel_(iConfig.getParameter<std::string>("@module_label")),
0103       cut_(iConfig.getParameter<std::string>("srcObjectSelection")),
0104       match_(iConfig.getParameter<std::string>("srcObjectsToMatchSelection")),
0105       nObjectsTot_(0),
0106       nObjectsMatch_(0) {
0107   this->template produces<std::vector<T1>>();
0108 }
0109 
0110 //______________________________________________________________________________
0111 template <typename T1, typename T2>
0112 ObjectViewMatcher<T1, T2>::~ObjectViewMatcher() {}
0113 
0114 ////////////////////////////////////////////////////////////////////////////////
0115 // implementation of member functions
0116 ////////////////////////////////////////////////////////////////////////////////
0117 
0118 //______________________________________________________________________________
0119 template <typename T1, typename T2>
0120 std::unique_ptr<ovm::StreamCache<T1, T2>> ObjectViewMatcher<T1, T2>::beginStream(edm::StreamID) const {
0121   return std::make_unique<ovm::StreamCache<T1, T2>>(cut_, match_);
0122 }
0123 
0124 //______________________________________________________________________________
0125 template <typename T1, typename T2>
0126 void ObjectViewMatcher<T1, T2>::produce(edm::StreamID iID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
0127   auto cleanObjects = std::make_unique<std::vector<T1>>();
0128 
0129   edm::Handle<edm::View<T1>> candidates;
0130   iEvent.getByToken(srcCandsToken_, candidates);
0131 
0132   bool* isMatch = new bool[candidates->size()];
0133   for (unsigned int iObject = 0; iObject < candidates->size(); iObject++)
0134     isMatch[iObject] = false;
0135 
0136   auto& objCut = this->streamCache(iID)->objCut_;
0137   auto& objMatchCut = this->streamCache(iID)->objMatchCut_;
0138   for (unsigned int iSrc = 0; iSrc < srcObjectsTokens_.size(); iSrc++) {
0139     edm::Handle<edm::View<T2>> objects;
0140     iEvent.getByToken(srcObjectsTokens_[iSrc], objects);
0141 
0142     if (objects->empty())
0143       continue;
0144 
0145     for (unsigned int iObject = 0; iObject < candidates->size(); iObject++) {
0146       const T1& candidate = candidates->at(iObject);
0147       if (!objCut(candidate))
0148         continue;
0149 
0150       for (unsigned int iObj = 0; iObj < objects->size(); iObj++) {
0151         const T2& obj = objects->at(iObj);
0152         if (!objMatchCut(obj))
0153           continue;
0154         double deltaR = reco::deltaR(candidate, obj);
0155         if (deltaR < deltaRMax_)
0156           isMatch[iObject] = true;
0157       }
0158     }
0159   }
0160 
0161   unsigned int counter = 0;
0162   typename edm::View<T1>::const_iterator tIt, endcands = candidates->end();
0163   for (tIt = candidates->begin(); tIt != endcands; ++tIt, ++counter) {
0164     if (isMatch[counter])
0165       cleanObjects->push_back(*tIt);
0166   }
0167 
0168   nObjectsTot_ += candidates->size();
0169   nObjectsMatch_ += cleanObjects->size();
0170 
0171   delete[] isMatch;
0172   iEvent.put(std::move(cleanObjects));
0173 }
0174 
0175 //______________________________________________________________________________
0176 template <typename T1, typename T2>
0177 void ObjectViewMatcher<T1, T2>::endJob() {
0178   std::stringstream ss;
0179   ss << "nObjectsTot=" << nObjectsTot_ << " nObjectsMatched=" << nObjectsMatch_
0180      << " fObjectsMatch=" << 100. * (nObjectsMatch_ / (double)nObjectsTot_) << "%\n";
0181   std::cout << "++++++++++++++++++++++++++++++++++++++++++++++++++"
0182             << "\n"
0183             << moduleLabel_ << "(ObjectViewMatcher) SUMMARY:\n"
0184             << ss.str() << "++++++++++++++++++++++++++++++++++++++++++++++++++" << std::endl;
0185 }
0186 
0187 //______________________________________________________________________________
0188 template <typename T1, typename T2>
0189 void ObjectViewMatcher<T1, T2>::fillDescriptions(edm::ConfigurationDescriptions& desc) {
0190   edm::ParameterSetDescription pset;
0191   pset.add<edm::InputTag>("srcObject");
0192   pset.add<std::vector<edm::InputTag>>("srcObjectsToMatch");
0193   pset.add<double>("deltaRMax");
0194   pset.add<std::string>("srcObjectSelection", "");
0195   pset.add<std::string>("srcObjectsToMatchSelection", "");
0196 
0197   desc.addDefault(pset);
0198 }
0199 
0200 ////////////////////////////////////////////////////////////////////////////////
0201 // plugin definition
0202 ////////////////////////////////////////////////////////////////////////////////
0203 
0204 typedef ObjectViewMatcher<reco::Photon, reco::Track> TrackMatchedPhotonProducer;
0205 typedef ObjectViewMatcher<reco::Jet, reco::Track> TrackMatchedJetProducer;
0206 
0207 #include "FWCore/Framework/interface/MakerMacros.h"
0208 DEFINE_FWK_MODULE(TrackMatchedPhotonProducer);
0209 DEFINE_FWK_MODULE(TrackMatchedJetProducer);