Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:48

0001 /**
0002   \class    pat::PATElectronSlimmer PATElectronSlimmer.h "PhysicsTools/PatAlgos/interface/PATElectronSlimmer.h"
0003   \brief    Slimmer of PAT Electrons 
0004 */
0005 
0006 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
0007 #include "DataFormats/PatCandidates/interface/Electron.h"
0008 
0009 #include "PhysicsTools/PatAlgos/interface/ObjectModifier.h"
0010 
0011 #include "FWCore/Framework/interface/Frameworkfwd.h"
0012 #include "FWCore/Framework/interface/stream/EDProducer.h"
0013 #include "FWCore/Framework/interface/Event.h"
0014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0015 #include "DataFormats/Common/interface/RefToPtr.h"
0016 #include "DataFormats/Common/interface/Association.h"
0017 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
0018 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
0019 #include "DataFormats/PatCandidates/interface/PackedCandidate.h"
0020 #include "CommonTools/UtilAlgos/interface/StringCutObjectSelector.h"
0021 #include "RecoEcal/EgammaCoreTools/interface/EcalClusterLazyTools.h"
0022 #include "FWCore/Utilities/interface/isFinite.h"
0023 
0024 namespace pat {
0025 
0026   class PATElectronSlimmer : public edm::stream::EDProducer<> {
0027   public:
0028     explicit PATElectronSlimmer(const edm::ParameterSet& iConfig);
0029 
0030     void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) final;
0031 
0032   private:
0033     const edm::EDGetTokenT<edm::View<pat::Electron>> src_;
0034 
0035     const StringCutObjectSelector<pat::Electron> dropSuperClusters_, dropBasicClusters_, dropPFlowClusters_,
0036         dropPreshowerClusters_, dropSeedCluster_, dropRecHits_;
0037     const StringCutObjectSelector<pat::Electron> dropCorrections_, dropIsolations_, dropShapes_, dropSaturation_,
0038         dropExtrapolations_, dropClassifications_;
0039 
0040     const edm::EDGetTokenT<edm::ValueMap<std::vector<reco::PFCandidateRef>>> reco2pf_;
0041     const edm::EDGetTokenT<edm::Association<pat::PackedCandidateCollection>> pf2pc_;
0042     const edm::EDGetTokenT<pat::PackedCandidateCollection> pc_;
0043     const bool linkToPackedPF_;
0044     const StringCutObjectSelector<pat::Electron> saveNonZSClusterShapes_;
0045     const edm::EDGetTokenT<EcalRecHitCollection> reducedBarrelRecHitCollectionToken_,
0046         reducedEndcapRecHitCollectionToken_;
0047     const EcalClusterLazyTools::ESGetTokens ecalClusterToolsESGetTokens_;
0048     const bool modifyElectron_;
0049     std::unique_ptr<pat::ObjectModifier<pat::Electron>> electronModifier_;
0050   };
0051 
0052 }  // namespace pat
0053 
0054 pat::PATElectronSlimmer::PATElectronSlimmer(const edm::ParameterSet& iConfig)
0055     : src_(consumes<edm::View<pat::Electron>>(iConfig.getParameter<edm::InputTag>("src"))),
0056       dropSuperClusters_(iConfig.getParameter<std::string>("dropSuperCluster")),
0057       dropBasicClusters_(iConfig.getParameter<std::string>("dropBasicClusters")),
0058       dropPFlowClusters_(iConfig.getParameter<std::string>("dropPFlowClusters")),
0059       dropPreshowerClusters_(iConfig.getParameter<std::string>("dropPreshowerClusters")),
0060       dropSeedCluster_(iConfig.getParameter<std::string>("dropSeedCluster")),
0061       dropRecHits_(iConfig.getParameter<std::string>("dropRecHits")),
0062       dropCorrections_(iConfig.getParameter<std::string>("dropCorrections")),
0063       dropIsolations_(iConfig.getParameter<std::string>("dropIsolations")),
0064       dropShapes_(iConfig.getParameter<std::string>("dropShapes")),
0065       dropSaturation_(iConfig.getParameter<std::string>("dropSaturation")),
0066       dropExtrapolations_(iConfig.getParameter<std::string>("dropExtrapolations")),
0067       dropClassifications_(iConfig.getParameter<std::string>("dropClassifications")),
0068       reco2pf_(mayConsume<edm::ValueMap<std::vector<reco::PFCandidateRef>>>(
0069           iConfig.getParameter<edm::InputTag>("recoToPFMap"))),
0070       pf2pc_(mayConsume<edm::Association<pat::PackedCandidateCollection>>(
0071           iConfig.getParameter<edm::InputTag>("packedPFCandidates"))),
0072       pc_(mayConsume<pat::PackedCandidateCollection>(iConfig.getParameter<edm::InputTag>("packedPFCandidates"))),
0073       linkToPackedPF_(iConfig.getParameter<bool>("linkToPackedPFCandidates")),
0074       saveNonZSClusterShapes_(iConfig.getParameter<std::string>("saveNonZSClusterShapes")),
0075       reducedBarrelRecHitCollectionToken_(
0076           consumes(iConfig.getParameter<edm::InputTag>("reducedBarrelRecHitCollection"))),
0077       reducedEndcapRecHitCollectionToken_(
0078           consumes(iConfig.getParameter<edm::InputTag>("reducedEndcapRecHitCollection"))),
0079       ecalClusterToolsESGetTokens_{consumesCollector()},
0080       modifyElectron_(iConfig.getParameter<bool>("modifyElectrons")) {
0081   if (modifyElectron_) {
0082     const edm::ParameterSet& mod_config = iConfig.getParameter<edm::ParameterSet>("modifierConfig");
0083     electronModifier_ = std::make_unique<pat::ObjectModifier<pat::Electron>>(mod_config, consumesCollector());
0084   }
0085 
0086   mayConsume<EcalRecHitCollection>(edm::InputTag("reducedEcalRecHitsEB"));
0087   mayConsume<EcalRecHitCollection>(edm::InputTag("reducedEcalRecHitsEE"));
0088 
0089   produces<std::vector<pat::Electron>>();
0090 }
0091 
0092 void pat::PATElectronSlimmer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0093   using namespace edm;
0094   using namespace std;
0095 
0096   Handle<View<pat::Electron>> src;
0097   iEvent.getByToken(src_, src);
0098 
0099   Handle<edm::ValueMap<std::vector<reco::PFCandidateRef>>> reco2pf;
0100   Handle<edm::Association<pat::PackedCandidateCollection>> pf2pc;
0101   Handle<pat::PackedCandidateCollection> pc;
0102   if (linkToPackedPF_) {
0103     iEvent.getByToken(reco2pf_, reco2pf);
0104     iEvent.getByToken(pf2pc_, pf2pc);
0105     iEvent.getByToken(pc_, pc);
0106   }
0107   noZS::EcalClusterLazyTools lazyToolsNoZS(iEvent,
0108                                            ecalClusterToolsESGetTokens_.get(iSetup),
0109                                            reducedBarrelRecHitCollectionToken_,
0110                                            reducedEndcapRecHitCollectionToken_);
0111 
0112   auto out = std::make_unique<std::vector<pat::Electron>>();
0113   out->reserve(src->size());
0114 
0115   if (modifyElectron_) {
0116     electronModifier_->setEvent(iEvent);
0117   }
0118   if (modifyElectron_)
0119     electronModifier_->setEventContent(iSetup);
0120 
0121   std::vector<unsigned int> keys;
0122   for (View<pat::Electron>::const_iterator it = src->begin(), ed = src->end(); it != ed; ++it) {
0123     out->push_back(*it);
0124     pat::Electron& electron = out->back();
0125 
0126     if (modifyElectron_) {
0127       electronModifier_->modify(electron);
0128     }
0129 
0130     if (dropSuperClusters_(electron)) {
0131       electron.superCluster_.clear();
0132       electron.embeddedSuperCluster_ = false;
0133     }
0134     if (dropBasicClusters_(electron)) {
0135       electron.basicClusters_.clear();
0136     }
0137     if (dropSuperClusters_(electron) || dropPFlowClusters_(electron)) {
0138       electron.pflowSuperCluster_.clear();
0139       electron.embeddedPflowSuperCluster_ = false;
0140     }
0141     if (dropBasicClusters_(electron) || dropPFlowClusters_(electron)) {
0142       electron.pflowBasicClusters_.clear();
0143     }
0144     if (dropPreshowerClusters_(electron)) {
0145       electron.preshowerClusters_.clear();
0146     }
0147     if (dropPreshowerClusters_(electron) || dropPFlowClusters_(electron)) {
0148       electron.pflowPreshowerClusters_.clear();
0149     }
0150     if (dropSeedCluster_(electron)) {
0151       electron.seedCluster_.clear();
0152       electron.embeddedSeedCluster_ = false;
0153     }
0154     if (dropRecHits_(electron)) {
0155       electron.recHits_ = EcalRecHitCollection();
0156       electron.embeddedRecHits_ = false;
0157     }
0158     if (dropCorrections_(electron)) {
0159       electron.setCorrections(reco::GsfElectron::Corrections());
0160     }
0161     if (dropIsolations_(electron)) {
0162       electron.setDr03Isolation(reco::GsfElectron::IsolationVariables());
0163       electron.setDr04Isolation(reco::GsfElectron::IsolationVariables());
0164       electron.setPfIsolationVariables(reco::GsfElectron::PflowIsolationVariables());
0165     }
0166     if (dropShapes_(electron)) {
0167       electron.setShowerShape(reco::GsfElectron::ShowerShape());
0168     }
0169     if (dropSaturation_(electron)) {
0170       electron.setSaturationInfo(reco::GsfElectron::SaturationInfo());
0171     }
0172     if (dropExtrapolations_(electron)) {
0173       electron.setTrackExtrapolations(reco::GsfElectron::TrackExtrapolations());
0174     }
0175     if (dropClassifications_(electron)) {
0176       electron.setClassificationVariables(reco::GsfElectron::ClassificationVariables());
0177       electron.setClassification(reco::GsfElectron::Classification());
0178     }
0179     if (linkToPackedPF_) {
0180       //std::cout << " PAT  electron in  " << src.id() << " comes from " << electron.refToOrig_.id() << ", " << electron.refToOrig_.key() << std::endl;
0181       keys.clear();
0182       for (auto const& pf : (*reco2pf)[electron.refToOrig_]) {
0183         if (pf2pc->contains(pf.id())) {
0184           keys.push_back((*pf2pc)[pf].key());
0185         }
0186       }
0187       electron.setAssociatedPackedPFCandidates(
0188           edm::RefProd<pat::PackedCandidateCollection>(pc), keys.begin(), keys.end());
0189       //std::cout << "Electron with pt " << electron.pt() << " associated to " << electron.associatedPackedFCandidateIndices_.size() << " PF Candidates\n";
0190       //if there's just one PF Cand then it's me, otherwise I have no univoque parent so my ref will be null
0191       if (keys.size() == 1) {
0192         electron.refToOrig_ = electron.sourceCandidatePtr(0);
0193       } else {
0194         electron.refToOrig_ = reco::CandidatePtr(pc.id());
0195       }
0196     }
0197     if (saveNonZSClusterShapes_(electron)) {
0198       const auto& vCov = lazyToolsNoZS.localCovariances(*(electron.superCluster()->seed()));
0199       electron.full5x5_setSigmaIetaIphi(vCov[1]);
0200     }
0201   }
0202 
0203   iEvent.put(std::move(out));
0204 }
0205 
0206 #include "FWCore/Framework/interface/MakerMacros.h"
0207 using namespace pat;
0208 DEFINE_FWK_MODULE(PATElectronSlimmer);