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  *   - Cleans a given object collection of other
0014  *     cross-object candidates using deltaR-matching.
0015  *   - For example: can clean a muon collection by
0016  *      removing all jets in the muon collection.
0017  *   - Saves collection of the reference vectors of cleaned objects.
0018  * History:
0019  *   Generalized the existing CandViewCleaner
0020  *
0021  *****************************************************************************/
0022 ////////////////////////////////////////////////////////////////////////////////
0023 // Includes
0024 ////////////////////////////////////////////////////////////////////////////////
0025 #include "FWCore/Framework/interface/global/EDProducer.h"
0026 #include "FWCore/Framework/interface/Event.h"
0027 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0028 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0029 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0030 #include "FWCore/Utilities/interface/InputTag.h"
0031 #include "FWCore/Utilities/interface/transform.h"
0032 
0033 #include "DataFormats/Common/interface/View.h"
0034 #include "DataFormats/Math/interface/deltaR.h"
0035 
0036 #include "DataFormats/Candidate/interface/Candidate.h"
0037 #include "DataFormats/Candidate/interface/CandidateFwd.h"
0038 #include "DataFormats/MuonReco/interface/Muon.h"
0039 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
0040 #include "DataFormats/EgammaCandidates/interface/Electron.h"
0041 #include "DataFormats/JetReco/interface/Jet.h"
0042 #include "DataFormats/EgammaCandidates/interface/Photon.h"
0043 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0044 
0045 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0046 
0047 #include <memory>
0048 #include <vector>
0049 #include <sstream>
0050 
0051 ////////////////////////////////////////////////////////////////////////////////
0052 // class definition
0053 ////////////////////////////////////////////////////////////////////////////////
0054 namespace ovc {
0055   template <typename T>
0056   struct StreamCache {
0057     StreamCache(std::string const& keep, std::string const& remove)
0058         : objKeepCut_(keep, true), objRemoveCut_(remove, true) {}
0059     StringCutObjectSelector<T, true>
0060         objKeepCut_;  // lazy parsing, to allow cutting on variables not in reco::Candidate class
0061     StringCutObjectSelector<reco::Candidate, true> objRemoveCut_;  // lazy parsing, to allow cutting on variables
0062   };
0063 }  // namespace ovc
0064 template <typename T>
0065 class ObjectViewCleaner : public edm::global::EDProducer<edm::StreamCache<ovc::StreamCache<T>>> {
0066 public:
0067   // construction/destruction
0068   ObjectViewCleaner(const edm::ParameterSet& iConfig);
0069   ~ObjectViewCleaner() override;
0070 
0071   // member functions
0072   std::unique_ptr<ovc::StreamCache<T>> beginStream(edm::StreamID) const override;
0073   void produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const override;
0074   void endJob() override;
0075 
0076   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0077 
0078 private:
0079   // member data
0080   const edm::EDGetTokenT<edm::View<T>> srcCandsToken_;
0081   const std::vector<edm::EDGetTokenT<edm::View<reco::Candidate>>> srcObjectsTokens_;
0082   const double deltaRMin_;
0083 
0084   const std::string moduleLabel_;
0085   const std::string keep_;
0086   const std::string remove_;
0087 
0088   mutable std::atomic<unsigned int> nObjectsTot_;
0089   mutable std::atomic<unsigned int> nObjectsClean_;
0090 };
0091 
0092 using namespace std;
0093 
0094 ////////////////////////////////////////////////////////////////////////////////
0095 // construction/destruction
0096 ////////////////////////////////////////////////////////////////////////////////
0097 
0098 //______________________________________________________________________________
0099 template <typename T>
0100 ObjectViewCleaner<T>::ObjectViewCleaner(const edm::ParameterSet& iConfig)
0101     : srcCandsToken_(this->consumes(iConfig.getParameter<edm::InputTag>("srcObject"))),
0102       srcObjectsTokens_(edm::vector_transform(
0103           iConfig.getParameter<vector<edm::InputTag>>("srcObjectsToRemove"),
0104           [this](edm::InputTag const& tag) { return this->template consumes<edm::View<reco::Candidate>>(tag); })),
0105       deltaRMin_(iConfig.getParameter<double>("deltaRMin")),
0106       moduleLabel_(iConfig.getParameter<string>("@module_label")),
0107       keep_(iConfig.getParameter<std::string>("srcObjectSelection")),
0108       remove_(iConfig.getParameter<std::string>("srcObjectsToRemoveSelection")),
0109       nObjectsTot_(0),
0110       nObjectsClean_(0) {
0111   this->template produces<edm::RefToBaseVector<T>>();
0112 }
0113 
0114 //______________________________________________________________________________
0115 template <typename T>
0116 ObjectViewCleaner<T>::~ObjectViewCleaner() {}
0117 
0118 ////////////////////////////////////////////////////////////////////////////////
0119 // implementation of member functions
0120 ////////////////////////////////////////////////////////////////////////////////
0121 
0122 template <typename T>
0123 std::unique_ptr<ovc::StreamCache<T>> ObjectViewCleaner<T>::beginStream(edm::StreamID) const {
0124   return std::make_unique<ovc::StreamCache<T>>(keep_, remove_);
0125 }
0126 //______________________________________________________________________________
0127 template <typename T>
0128 void ObjectViewCleaner<T>::produce(edm::StreamID streamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
0129   auto cleanObjects = std::make_unique<edm::RefToBaseVector<T>>();
0130 
0131   edm::Handle<edm::View<T>> candidates;
0132   iEvent.getByToken(srcCandsToken_, candidates);
0133 
0134   bool* isClean = new bool[candidates->size()];
0135   for (unsigned int iObject = 0; iObject < candidates->size(); iObject++)
0136     isClean[iObject] = true;
0137 
0138   auto& objKeepCut = this->streamCache(streamID)->objKeepCut_;
0139   auto& objRemoveCut = this->streamCache(streamID)->objRemoveCut_;
0140   for (unsigned int iSrc = 0; iSrc < srcObjectsTokens_.size(); iSrc++) {
0141     edm::Handle<edm::View<reco::Candidate>> objects;
0142     iEvent.getByToken(srcObjectsTokens_[iSrc], objects);
0143 
0144     for (unsigned int iObject = 0; iObject < candidates->size(); iObject++) {
0145       const T& candidate = candidates->at(iObject);
0146       if (!objKeepCut(candidate))
0147         isClean[iObject] = false;
0148 
0149       for (unsigned int iObj = 0; iObj < objects->size(); iObj++) {
0150         const reco::Candidate& obj = objects->at(iObj);
0151         if (!objRemoveCut(obj))
0152           continue;
0153 
0154         double deltaR = reco::deltaR(candidate, obj);
0155         if (deltaR < deltaRMin_)
0156           isClean[iObject] = false;
0157       }
0158     }
0159   }
0160 
0161   for (unsigned int iObject = 0; iObject < candidates->size(); iObject++)
0162     if (isClean[iObject])
0163       cleanObjects->push_back(candidates->refAt(iObject));
0164 
0165   nObjectsTot_ += candidates->size();
0166   nObjectsClean_ += cleanObjects->size();
0167 
0168   delete[] isClean;
0169   iEvent.put(std::move(cleanObjects));
0170 }
0171 
0172 //______________________________________________________________________________
0173 template <typename T>
0174 void ObjectViewCleaner<T>::endJob() {
0175   stringstream ss;
0176   ss << "nObjectsTot=" << nObjectsTot_ << " nObjectsClean=" << nObjectsClean_
0177      << " fObjectsClean=" << 100. * (nObjectsClean_ / (double)nObjectsTot_) << "%\n";
0178   edm::LogInfo("ObjectViewCleaner") << "++++++++++++++++++++++++++++++++++++++++++++++++++"
0179                                     << "\n"
0180                                     << moduleLabel_ << "(ObjectViewCleaner) SUMMARY:\n"
0181                                     << ss.str() << "++++++++++++++++++++++++++++++++++++++++++++++++++";
0182 }
0183 
0184 template <typename T>
0185 void ObjectViewCleaner<T>::fillDescriptions(edm::ConfigurationDescriptions& desc) {
0186   edm::ParameterSetDescription pset;
0187   pset.add<edm::InputTag>("srcObject");
0188   pset.add<std::vector<edm::InputTag>>("srcObjectsToRemove");
0189   pset.add<double>("deltaRMin");
0190   pset.add<std::string>("srcObjectSelection", "");
0191   pset.add<std::string>("srcObjectsToRemoveSelection", "");
0192   desc.addDefault(pset);
0193 }
0194 ////////////////////////////////////////////////////////////////////////////////
0195 // plugin definition
0196 ////////////////////////////////////////////////////////////////////////////////
0197 
0198 typedef ObjectViewCleaner<reco::Candidate> CandViewCleaner;
0199 typedef ObjectViewCleaner<reco::Jet> JetViewCleaner;
0200 typedef ObjectViewCleaner<reco::Muon> MuonViewCleaner;
0201 typedef ObjectViewCleaner<reco::GsfElectron> GsfElectronViewCleaner;
0202 typedef ObjectViewCleaner<reco::Electron> ElectronViewCleaner;
0203 typedef ObjectViewCleaner<reco::Photon> PhotonViewCleaner;
0204 
0205 #include "FWCore/Framework/interface/MakerMacros.h"
0206 DEFINE_FWK_MODULE(CandViewCleaner);
0207 DEFINE_FWK_MODULE(JetViewCleaner);
0208 DEFINE_FWK_MODULE(MuonViewCleaner);
0209 DEFINE_FWK_MODULE(GsfElectronViewCleaner);
0210 DEFINE_FWK_MODULE(ElectronViewCleaner);
0211 DEFINE_FWK_MODULE(PhotonViewCleaner);