Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-04-01 02:23:24

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