Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:33:18

0001 /*****************************************************************************
0002  * Project: CMS detector at the CERN
0003  *
0004  * Package: Validation/RecoTau
0005  *
0006  * Description:
0007  *   - Cleans a given object collection of other
0008  *     cross-object candidates using deltaR-matching.
0009  *   - For example: can clean a muon collection by
0010  *      removing all jets in the muon collection.
0011  *   - Saves collection of the reference vectors of cleaned objects.
0012  *****************************************************************************/
0013 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0014 
0015 #include "DataFormats/Candidate/interface/Candidate.h"
0016 #include "DataFormats/Candidate/interface/CandidateFwd.h"
0017 #include "DataFormats/Common/interface/View.h"
0018 #include "DataFormats/EgammaCandidates/interface/Electron.h"
0019 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
0020 #include "DataFormats/EgammaCandidates/interface/Photon.h"
0021 #include "DataFormats/JetReco/interface/Jet.h"
0022 #include "DataFormats/Math/interface/deltaR.h"
0023 #include "DataFormats/MuonReco/interface/Muon.h"
0024 #include "DataFormats/TrackReco/interface/Track.h"
0025 
0026 #include "FWCore/Framework/interface/stream/EDProducer.h"
0027 #include "FWCore/Framework/interface/Event.h"
0028 #include "FWCore/Framework/interface/MakerMacros.h"
0029 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0030 #include "FWCore/Utilities/interface/EDGetToken.h"
0031 #include "FWCore/Utilities/interface/InputTag.h"
0032 
0033 #include <memory>
0034 #include <sstream>
0035 #include <vector>
0036 
0037 namespace {
0038 
0039   // Job-level data
0040   struct Counters {
0041     explicit Counters(std::string const& label) : moduleLabel{label} {}
0042     std::string const moduleLabel;
0043     mutable std::atomic<std::size_t> nObjectsTot{};
0044     mutable std::atomic<std::size_t> nObjectsClean{};
0045   };
0046 
0047   template <typename T>
0048   class ObjectViewCleaner : public edm::stream::EDProducer<edm::GlobalCache<Counters>> {
0049   public:
0050     explicit ObjectViewCleaner(edm::ParameterSet const&, Counters const*);
0051 
0052     void produce(edm::Event&, edm::EventSetup const&) override;
0053     static auto initializeGlobalCache(edm::ParameterSet const& iConfig) {
0054       return std::make_unique<Counters>(iConfig.getParameter<std::string>("@module_label"));
0055     }
0056     static void globalEndJob(Counters const*);
0057 
0058   private:
0059     // member data
0060     edm::EDGetTokenT<edm::View<T>> srcCands_;
0061     std::vector<edm::EDGetTokenT<edm::View<reco::Candidate>>> srcObjectsToRemove_;
0062     double deltaRMin_;
0063     StringCutObjectSelector<T, true>
0064         objKeepCut_;  // lazy parsing, to allow cutting on variables not in reco::Candidate class
0065     StringCutObjectSelector<reco::Candidate, true> objRemoveCut_;  // lazy parsing, to allow cutting on variables
0066 
0067     auto tagsToTokens(std::vector<edm::InputTag> const&) -> decltype(srcObjectsToRemove_);
0068     bool isIsolated(edm::Event const&, T const&) const;
0069   };
0070 
0071   using namespace std;
0072 
0073   template <typename T>
0074   ObjectViewCleaner<T>::ObjectViewCleaner(edm::ParameterSet const& iConfig, Counters const*)
0075       : srcCands_{consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("srcObject"))},
0076         srcObjectsToRemove_{tagsToTokens(iConfig.getParameter<vector<edm::InputTag>>("srcObjectsToRemove"))},
0077         deltaRMin_{iConfig.getParameter<double>("deltaRMin")},
0078         objKeepCut_{iConfig.existsAs<std::string>("srcObjectSelection")
0079                         ? iConfig.getParameter<std::string>("srcObjectSelection")
0080                         : "",
0081                     true},
0082         objRemoveCut_{iConfig.existsAs<std::string>("srcObjectsToRemoveSelection")
0083                           ? iConfig.getParameter<std::string>("srcObjectsToRemoveSelection")
0084                           : "",
0085                       true} {
0086     produces<edm::RefToBaseVector<T>>();
0087   }
0088 
0089   //______________________________________________________________________________
0090   template <typename T>
0091   void ObjectViewCleaner<T>::produce(edm::Event& iEvent, edm::EventSetup const&) {
0092     edm::Handle<edm::View<T>> candidates;
0093     iEvent.getByToken(srcCands_, candidates);
0094     globalCache()->nObjectsTot += candidates->size();
0095 
0096     auto cleanObjects = std::make_unique<edm::RefToBaseVector<T>>();
0097     for (unsigned int iCand{}; iCand < candidates->size(); ++iCand) {
0098       auto const& candidate = candidates->at(iCand);
0099       if (objKeepCut_(candidate) && isIsolated(iEvent, candidate)) {
0100         cleanObjects->push_back(candidates->refAt(iCand));
0101       }
0102     }
0103     globalCache()->nObjectsClean += cleanObjects->size();
0104 
0105     iEvent.put(std::move(cleanObjects));
0106   }
0107 
0108   //______________________________________________________________________________
0109   template <typename T>
0110   void ObjectViewCleaner<T>::globalEndJob(Counters const* counters) {
0111     ostringstream oss;
0112     oss << "nObjectsTot=" << counters->nObjectsTot << " nObjectsClean=" << counters->nObjectsClean
0113         << " fObjectsClean=" << 100 * (counters->nObjectsClean / static_cast<double>(counters->nObjectsTot)) << "%\n";
0114     edm::LogInfo("ObjectViewCleaner") << "++++++++++++++++++++++++++++++++++++++++++++++++++\n"
0115                                       << counters->moduleLabel << "(ObjectViewCleaner) SUMMARY:\n"
0116                                       << oss.str() << '\n'
0117                                       << "++++++++++++++++++++++++++++++++++++++++++++++++++";
0118   }
0119 
0120   //______________________________________________________________________________
0121   template <typename T>
0122   bool ObjectViewCleaner<T>::isIsolated(edm::Event const& iEvent, T const& candidate) const {
0123     for (auto const& srcObject : srcObjectsToRemove_) {
0124       edm::Handle<edm::View<reco::Candidate>> objects;
0125       iEvent.getByToken(srcObject, objects);
0126 
0127       for (unsigned int iObj{}; iObj < objects->size(); ++iObj) {
0128         auto const& obj = objects->at(iObj);
0129         if (!objRemoveCut_(obj))
0130           continue;
0131 
0132         if (reco::deltaR(candidate, obj) < deltaRMin_) {
0133           return false;
0134         }
0135       }
0136     }
0137     return true;
0138   };
0139 
0140   //______________________________________________________________________________
0141   template <typename T>
0142   auto ObjectViewCleaner<T>::tagsToTokens(std::vector<edm::InputTag> const& tags) -> decltype(srcObjectsToRemove_) {
0143     std::vector<edm::EDGetTokenT<edm::View<reco::Candidate>>> result;
0144     std::transform(std::cbegin(tags), std::cend(tags), std::back_inserter(result), [this](auto const& tag) {
0145       return this->consumes<edm::View<reco::Candidate>>(tag);
0146     });
0147     return result;
0148   }
0149 
0150 }  // anonymous namespace
0151 
0152 ////////////////////////////////////////////////////////////////////////////////
0153 // plugin definitions
0154 ////////////////////////////////////////////////////////////////////////////////
0155 
0156 typedef ObjectViewCleaner<reco::Candidate> TauValCandViewCleaner;
0157 typedef ObjectViewCleaner<reco::Jet> TauValJetViewCleaner;
0158 typedef ObjectViewCleaner<reco::Muon> TauValMuonViewCleaner;
0159 typedef ObjectViewCleaner<reco::GsfElectron> TauValGsfElectronViewCleaner;
0160 typedef ObjectViewCleaner<reco::Electron> TauValElectronViewCleaner;
0161 typedef ObjectViewCleaner<reco::Photon> TauValPhotonViewCleaner;
0162 typedef ObjectViewCleaner<reco::Track> TauValTrackViewCleaner;
0163 
0164 DEFINE_FWK_MODULE(TauValCandViewCleaner);
0165 DEFINE_FWK_MODULE(TauValJetViewCleaner);
0166 DEFINE_FWK_MODULE(TauValMuonViewCleaner);
0167 DEFINE_FWK_MODULE(TauValGsfElectronViewCleaner);
0168 DEFINE_FWK_MODULE(TauValElectronViewCleaner);
0169 DEFINE_FWK_MODULE(TauValPhotonViewCleaner);
0170 DEFINE_FWK_MODULE(TauValTrackViewCleaner);