Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-05-18 03:27:25

0001 /**
0002   \class    pat::PATTauSlimmer PATTauSlimmer.h "PhysicsTools/PatAlgos/interface/PATTauSlimmer.h"
0003   \brief    Slimmer of PAT Taus 
0004 */
0005 
0006 #include "FWCore/Framework/interface/Frameworkfwd.h"
0007 #include "FWCore/Framework/interface/stream/EDProducer.h"
0008 #include "FWCore/Framework/interface/Event.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0010 #include "DataFormats/Common/interface/RefToPtr.h"
0011 #include "DataFormats/Common/interface/Association.h"
0012 #include "DataFormats/PatCandidates/interface/PackedCandidate.h"
0013 
0014 #include "DataFormats/PatCandidates/interface/Tau.h"
0015 #include "PhysicsTools/PatAlgos/interface/ObjectModifier.h"
0016 
0017 namespace pat {
0018 
0019   class PATTauSlimmer : public edm::stream::EDProducer<> {
0020   public:
0021     explicit PATTauSlimmer(const edm::ParameterSet &iConfig);
0022     ~PATTauSlimmer() override {}
0023 
0024     void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override;
0025 
0026   private:
0027     const edm::EDGetTokenT<edm::View<pat::Tau>> src_;
0028     const bool linkToPackedPF_;
0029     const edm::EDGetTokenT<edm::Association<pat::PackedCandidateCollection>> pf2pc_;
0030     const bool linkToLostTracks_;
0031     const edm::EDGetTokenT<edm::Association<pat::PackedCandidateCollection>> trk2lost_;
0032     const bool dropPiZeroRefs_;
0033     const bool dropTauChargedHadronRefs_;
0034     const bool dropPFSpecific_;
0035     const bool modifyTau_;
0036     std::unique_ptr<pat::ObjectModifier<pat::Tau>> tauModifier_;
0037   };
0038 
0039 }  // namespace pat
0040 
0041 pat::PATTauSlimmer::PATTauSlimmer(const edm::ParameterSet &iConfig)
0042     : src_(consumes<edm::View<pat::Tau>>(iConfig.getParameter<edm::InputTag>("src"))),
0043       linkToPackedPF_(iConfig.getParameter<bool>("linkToPackedPFCandidates")),
0044       pf2pc_(mayConsume<edm::Association<pat::PackedCandidateCollection>>(
0045           iConfig.getParameter<edm::InputTag>("packedPFCandidates"))),
0046       linkToLostTracks_(iConfig.getParameter<bool>("linkToLostTracks")),
0047       trk2lost_(mayConsume<edm::Association<pat::PackedCandidateCollection>>(
0048           iConfig.getParameter<edm::InputTag>("lostTracks"))),
0049       dropPiZeroRefs_(iConfig.exists("dropPiZeroRefs") ? iConfig.getParameter<bool>("dropPiZeroRefs") : true),
0050       dropTauChargedHadronRefs_(
0051           iConfig.exists("dropTauChargedHadronRefs") ? iConfig.getParameter<bool>("dropTauChargedHadronRefs") : true),
0052       dropPFSpecific_(iConfig.exists("dropPFSpecific") ? iConfig.getParameter<bool>("dropPFSpecific") : true),
0053       modifyTau_(iConfig.getParameter<bool>("modifyTaus")) {
0054   if (modifyTau_) {
0055     const edm::ParameterSet &mod_config = iConfig.getParameter<edm::ParameterSet>("modifierConfig");
0056     tauModifier_ = std::make_unique<pat::ObjectModifier<pat::Tau>>(mod_config, consumesCollector());
0057   }
0058   produces<std::vector<pat::Tau>>();
0059 }
0060 
0061 void pat::PATTauSlimmer::produce(edm::Event &iEvent, const edm::EventSetup &iSetup) {
0062   using namespace edm;
0063   using namespace std;
0064 
0065   if (modifyTau_)
0066     tauModifier_->setEventContent(iSetup);
0067 
0068   Handle<View<pat::Tau>> src;
0069   iEvent.getByToken(src_, src);
0070 
0071   Handle<edm::Association<pat::PackedCandidateCollection>> pf2pc;
0072   if (linkToPackedPF_)
0073     iEvent.getByToken(pf2pc_, pf2pc);
0074 
0075   Handle<edm::Association<pat::PackedCandidateCollection>> trk2lost;
0076   if (linkToLostTracks_)
0077     iEvent.getByToken(trk2lost_, trk2lost);
0078 
0079   auto out = std::make_unique<std::vector<pat::Tau>>();
0080   out->reserve(src->size());
0081 
0082   if (modifyTau_) {
0083     tauModifier_->setEvent(iEvent);
0084   }
0085 
0086   for (View<pat::Tau>::const_iterator it = src->begin(), ed = src->end(); it != ed; ++it) {
0087     out->push_back(*it);
0088     pat::Tau &tau = out->back();
0089 
0090     if (modifyTau_) {
0091       tauModifier_->modify(tau);
0092     }
0093 
0094     // clearing the pat isolation which is not used by taus
0095     tau.isolations_.clear();
0096     tau.isoDeposits_.clear();
0097 
0098     if (linkToPackedPF_) {
0099       reco::CandidatePtrVector signalChHPtrs, signalNHPtrs, signalGammaPtrs, isolationChHPtrs, isolationNHPtrs,
0100           isolationGammaPtrs;
0101 
0102       for (const reco::PFCandidatePtr &p : tau.signalPFChargedHadrCands()) {
0103         signalChHPtrs.push_back(edm::refToPtr((*pf2pc)[p]));
0104       }
0105       tau.setSignalChargedHadrCands(signalChHPtrs);
0106 
0107       for (const reco::PFCandidatePtr &p : tau.signalPFNeutrHadrCands()) {
0108         signalNHPtrs.push_back(edm::refToPtr((*pf2pc)[p]));
0109       }
0110       tau.setSignalNeutralHadrCands(signalNHPtrs);
0111 
0112       for (const reco::PFCandidatePtr &p : tau.signalPFGammaCands()) {
0113         signalGammaPtrs.push_back(edm::refToPtr((*pf2pc)[p]));
0114       }
0115       tau.setSignalGammaCands(signalGammaPtrs);
0116 
0117       for (const reco::PFCandidatePtr &p : tau.isolationPFChargedHadrCands()) {
0118         isolationChHPtrs.push_back(edm::refToPtr((*pf2pc)[p]));
0119       }
0120       tau.setIsolationChargedHadrCands(isolationChHPtrs);
0121 
0122       for (const reco::PFCandidatePtr &p : tau.isolationPFNeutrHadrCands()) {
0123         isolationNHPtrs.push_back(edm::refToPtr((*pf2pc)[p]));
0124       }
0125       tau.setIsolationNeutralHadrCands(isolationNHPtrs);
0126 
0127       for (const reco::PFCandidatePtr &p : tau.isolationPFGammaCands()) {
0128         isolationGammaPtrs.push_back(edm::refToPtr((*pf2pc)[p]));
0129       }
0130       tau.setIsolationGammaCands(isolationGammaPtrs);
0131     }
0132     if (linkToLostTracks_ && !tau.signalTracks().empty()) {
0133       std::vector<reco::CandidatePtr> signalLostTracks;
0134       for (const auto &trkRef : tau.signalTracks()) {
0135         const auto &lostCandRef = (*trk2lost)[edm::refToPtr(trkRef)];
0136         if (lostCandRef.isNonnull())
0137           signalLostTracks.push_back(edm::refToPtr(lostCandRef));
0138       }
0139       tau.setSignalLostTracks(signalLostTracks);
0140     }
0141 
0142     if (dropPiZeroRefs_) {
0143       tau.pfSpecific_[0].signalPiZeroCandidates_.clear();
0144       tau.pfSpecific_[0].isolationPiZeroCandidates_.clear();
0145     }
0146     if (dropTauChargedHadronRefs_) {
0147       tau.pfSpecific_[0].signalTauChargedHadronCandidates_.clear();
0148       tau.pfSpecific_[0].isolationTauChargedHadronCandidates_.clear();
0149     }
0150     if (dropPFSpecific_) {
0151       tau.pfSpecific_.clear();
0152     }
0153   }
0154 
0155   iEvent.put(std::move(out));
0156 }
0157 
0158 #include "FWCore/Framework/interface/MakerMacros.h"
0159 using namespace pat;
0160 DEFINE_FWK_MODULE(PATTauSlimmer);