Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /**
0002   \class    pat::PATPhotonSlimmer PATPhotonSlimmer.h "PhysicsTools/PatAlgos/interface/PATPhotonSlimmer.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 
0011 #include "DataFormats/Common/interface/Association.h"
0012 #include "DataFormats/Common/interface/RefToPtr.h"
0013 
0014 #include "DataFormats/PatCandidates/interface/Photon.h"
0015 
0016 #include "PhysicsTools/PatAlgos/interface/ObjectModifier.h"
0017 
0018 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
0019 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
0020 #include "DataFormats/PatCandidates/interface/PackedCandidate.h"
0021 #include "CommonTools/UtilAlgos/interface/StringCutObjectSelector.h"
0022 #include "RecoEcal/EgammaCoreTools/interface/EcalClusterLazyTools.h"
0023 #include "FWCore/Utilities/interface/isFinite.h"
0024 #include "DataFormats/Common/interface/ValueMap.h"
0025 
0026 namespace pat {
0027 
0028   class PATPhotonSlimmer : public edm::stream::EDProducer<> {
0029   public:
0030     explicit PATPhotonSlimmer(const edm::ParameterSet& iConfig);
0031     ~PATPhotonSlimmer() override {}
0032 
0033     void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0034     void beginLuminosityBlock(const edm::LuminosityBlock&, const edm::EventSetup&) final;
0035 
0036   private:
0037     const edm::EDGetTokenT<edm::View<pat::Photon>> src_;
0038 
0039     const StringCutObjectSelector<pat::Photon> dropSuperClusters_, dropBasicClusters_, dropPreshowerClusters_,
0040         dropSeedCluster_, dropRecHits_, dropSaturation_, dropRegressionData_;
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::Photon> saveNonZSClusterShapes_;
0047     const edm::EDGetTokenT<EcalRecHitCollection> reducedBarrelRecHitCollectionToken_,
0048         reducedEndcapRecHitCollectionToken_;
0049     const bool modifyPhoton_;
0050     std::unique_ptr<pat::ObjectModifier<pat::Photon>> photonModifier_;
0051     const EcalClusterLazyTools::ESGetTokens ecalClusterToolsESGetTokens_;
0052   };
0053 
0054 }  // namespace pat
0055 
0056 pat::PATPhotonSlimmer::PATPhotonSlimmer(const edm::ParameterSet& iConfig)
0057     : src_(consumes<edm::View<pat::Photon>>(iConfig.getParameter<edm::InputTag>("src"))),
0058       dropSuperClusters_(iConfig.getParameter<std::string>("dropSuperCluster")),
0059       dropBasicClusters_(iConfig.getParameter<std::string>("dropBasicClusters")),
0060       dropPreshowerClusters_(iConfig.getParameter<std::string>("dropPreshowerClusters")),
0061       dropSeedCluster_(iConfig.getParameter<std::string>("dropSeedCluster")),
0062       dropRecHits_(iConfig.getParameter<std::string>("dropRecHits")),
0063       dropSaturation_(iConfig.getParameter<std::string>("dropSaturation")),
0064       dropRegressionData_(iConfig.getParameter<std::string>("dropRegressionData")),
0065       reco2pf_(mayConsume<edm::ValueMap<std::vector<reco::PFCandidateRef>>>(
0066           iConfig.getParameter<edm::InputTag>("recoToPFMap"))),
0067       pf2pc_(mayConsume<edm::Association<pat::PackedCandidateCollection>>(
0068           iConfig.getParameter<edm::InputTag>("packedPFCandidates"))),
0069       pc_(mayConsume<pat::PackedCandidateCollection>(iConfig.getParameter<edm::InputTag>("packedPFCandidates"))),
0070       linkToPackedPF_(iConfig.getParameter<bool>("linkToPackedPFCandidates")),
0071       saveNonZSClusterShapes_(iConfig.getParameter<std::string>("saveNonZSClusterShapes")),
0072       reducedBarrelRecHitCollectionToken_(
0073           consumes<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>("reducedBarrelRecHitCollection"))),
0074       reducedEndcapRecHitCollectionToken_(
0075           consumes<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>("reducedEndcapRecHitCollection"))),
0076       modifyPhoton_(iConfig.getParameter<bool>("modifyPhotons")),
0077       ecalClusterToolsESGetTokens_{consumesCollector()} {
0078   if (modifyPhoton_) {
0079     const edm::ParameterSet& mod_config = iConfig.getParameter<edm::ParameterSet>("modifierConfig");
0080     photonModifier_ = std::make_unique<pat::ObjectModifier<pat::Photon>>(mod_config, consumesCollector());
0081   }
0082 
0083   mayConsume<EcalRecHitCollection>(edm::InputTag("reducedEcalRecHitsEB"));
0084   mayConsume<EcalRecHitCollection>(edm::InputTag("reducedEcalRecHitsEE"));
0085 
0086   produces<std::vector<pat::Photon>>();
0087 }
0088 
0089 void pat::PATPhotonSlimmer::beginLuminosityBlock(const edm::LuminosityBlock&, const edm::EventSetup& iSetup) {}
0090 
0091 void pat::PATPhotonSlimmer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0092   using namespace edm;
0093   using namespace std;
0094 
0095   Handle<View<pat::Photon>> src;
0096   iEvent.getByToken(src_, src);
0097 
0098   Handle<edm::ValueMap<std::vector<reco::PFCandidateRef>>> reco2pf;
0099   Handle<edm::Association<pat::PackedCandidateCollection>> pf2pc;
0100   Handle<pat::PackedCandidateCollection> pc;
0101   if (linkToPackedPF_) {
0102     iEvent.getByToken(reco2pf_, reco2pf);
0103     iEvent.getByToken(pf2pc_, pf2pc);
0104     iEvent.getByToken(pc_, pc);
0105   }
0106   noZS::EcalClusterLazyTools lazyToolsNoZS(iEvent,
0107                                            ecalClusterToolsESGetTokens_.get(iSetup),
0108                                            reducedBarrelRecHitCollectionToken_,
0109                                            reducedEndcapRecHitCollectionToken_);
0110 
0111   auto out = std::make_unique<std::vector<pat::Photon>>();
0112   out->reserve(src->size());
0113 
0114   if (modifyPhoton_) {
0115     photonModifier_->setEvent(iEvent);
0116   }
0117   if (modifyPhoton_)
0118     photonModifier_->setEventContent(iSetup);
0119 
0120   std::vector<unsigned int> keys;
0121   for (View<pat::Photon>::const_iterator it = src->begin(), ed = src->end(); it != ed; ++it) {
0122     out->push_back(*it);
0123     pat::Photon& photon = out->back();
0124 
0125     if (modifyPhoton_) {
0126       photonModifier_->modify(photon);
0127     }
0128 
0129     if (dropSuperClusters_(photon)) {
0130       photon.superCluster_.clear();
0131       photon.embeddedSuperCluster_ = false;
0132     }
0133     if (dropBasicClusters_(photon)) {
0134       photon.basicClusters_.clear();
0135     }
0136     if (dropPreshowerClusters_(photon)) {
0137       photon.preshowerClusters_.clear();
0138     }
0139     if (dropSeedCluster_(photon)) {
0140       photon.seedCluster_.clear();
0141       photon.embeddedSeedCluster_ = false;
0142     }
0143     if (dropRecHits_(photon)) {
0144       photon.recHits_ = EcalRecHitCollection();
0145       photon.embeddedRecHits_ = false;
0146     }
0147     if (dropSaturation_(photon)) {
0148       photon.setSaturationInfo(reco::Photon::SaturationInfo());
0149     }
0150     if (dropRegressionData_(photon)) {
0151       photon.setEMax(0);
0152       photon.setE2nd(0);
0153       photon.setE3x3(0);
0154       photon.setETop(0);
0155       photon.setEBottom(0);
0156       photon.setELeft(0);
0157       photon.setERight(0);
0158       photon.setSee(0);
0159       photon.setSep(0);
0160       photon.setSpp(0);
0161       photon.setMaxDR(0);
0162       photon.setMaxDRDPhi(0);
0163       photon.setMaxDRDEta(0);
0164       photon.setMaxDRRawEnergy(0);
0165       photon.setSubClusRawE1(0);
0166       photon.setSubClusRawE2(0);
0167       photon.setSubClusRawE3(0);
0168       photon.setSubClusDPhi1(0);
0169       photon.setSubClusDPhi2(0);
0170       photon.setSubClusDPhi3(0);
0171       photon.setSubClusDEta1(0);
0172       photon.setSubClusDEta2(0);
0173       photon.setSubClusDEta3(0);
0174       photon.setCryPhi(0);
0175       photon.setCryEta(0);
0176       photon.setIEta(0);
0177       photon.setIPhi(0);
0178     }
0179 
0180     if (linkToPackedPF_) {
0181       //std::cout << " PAT  photon in  " << src.id() << " comes from " << photon.refToOrig_.id() << ", " << photon.refToOrig_.key() << std::endl;
0182       keys.clear();
0183       for (auto const& pf : (*reco2pf)[photon.refToOrig_]) {
0184         if (pf2pc->contains(pf.id())) {
0185           keys.push_back((*pf2pc)[pf].key());
0186         }
0187       }
0188       photon.setAssociatedPackedPFCandidates(
0189           edm::RefProd<pat::PackedCandidateCollection>(pc), keys.begin(), keys.end());
0190       //std::cout << "Photon with pt " << photon.pt() << " associated to " << photon.associatedPackedFCandidateIndices_.size() << " PF Candidates\n";
0191       //if there's just one PF Cand then it's me, otherwise I have no univoque parent so my ref will be null
0192       if (keys.size() == 1) {
0193         photon.refToOrig_ = photon.sourceCandidatePtr(0);
0194       } else {
0195         photon.refToOrig_ = reco::CandidatePtr(pc.id());
0196       }
0197     }
0198     if (saveNonZSClusterShapes_(photon)) {
0199       const auto& vCov = lazyToolsNoZS.localCovariances(*(photon.superCluster()->seed()));
0200       float r9 = lazyToolsNoZS.e3x3(*(photon.superCluster()->seed())) / photon.superCluster()->rawEnergy();
0201       float sigmaIetaIeta = (!edm::isNotFinite(vCov[0])) ? sqrt(vCov[0]) : 0;
0202       float sigmaIetaIphi = vCov[1];
0203       float sigmaIphiIphi = (!edm::isNotFinite(vCov[2])) ? sqrt(vCov[2]) : 0;
0204       float e15o55 =
0205           lazyToolsNoZS.e1x5(*(photon.superCluster()->seed())) / lazyToolsNoZS.e5x5(*(photon.superCluster()->seed()));
0206       photon.addUserFloat("sigmaIetaIeta_NoZS", sigmaIetaIeta);
0207       photon.addUserFloat("sigmaIetaIphi_NoZS", sigmaIetaIphi);
0208       photon.addUserFloat("sigmaIphiIphi_NoZS", sigmaIphiIphi);
0209       photon.addUserFloat("r9_NoZS", r9);
0210       photon.addUserFloat("e1x5_over_e5x5_NoZS", e15o55);
0211     }
0212   }
0213 
0214   iEvent.put(std::move(out));
0215 }
0216 
0217 #include "FWCore/Framework/interface/MakerMacros.h"
0218 using namespace pat;
0219 DEFINE_FWK_MODULE(PATPhotonSlimmer);