Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:25:03

0001 #include "CommonTools/CandAlgos/interface/ModifyObjectValueBase.h"
0002 #include "FWCore/Utilities/interface/InputTag.h"
0003 #include "FWCore/Utilities/interface/EDGetToken.h"
0004 #include "FWCore/Framework/interface/ESHandle.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "DataFormats/Common/interface/Handle.h"
0007 #include "DataFormats/Common/interface/ValueMap.h"
0008 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
0009 #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
0010 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
0011 #include "DataFormats/EgammaCandidates/interface/Photon.h"
0012 #include "DataFormats/EgammaCandidates/interface/Conversion.h"
0013 #include "RecoEcal/EgammaCoreTools/interface/EcalClusterTools.h"
0014 #include "CommonTools/Egamma/interface/ConversionTools.h"
0015 
0016 #include <vdt/vdtMath.h>
0017 
0018 //this modifier fills variables where not present in CMSSW_92X to CMSSW_105X
0019 //use case is when reading older new samples in newer releases, aka legacy
0020 //note we suffer from the problem of needing to use the electrons/photons the valuemaps
0021 //are keyed to (something that is thankfully going away in 11X!) so we have to take
0022 //those collections in and figure out which ele/pho matches to them
0023 class EG9X105XObjectUpdateModifier : public ModifyObjectValueBase {
0024 public:
0025   template <typename T>
0026   class TokenHandlePair {
0027   public:
0028     TokenHandlePair(const edm::ParameterSet& conf, const std::string& name, edm::ConsumesCollector& cc)
0029         : token_(cc.consumes(conf.getParameter<edm::InputTag>(name))) {}
0030     void setHandle(const edm::Event& iEvent) { handle_ = iEvent.getHandle(token_); }
0031     const edm::Handle<T>& handle() const { return handle_; }
0032 
0033   private:
0034     edm::EDGetTokenT<T> token_;
0035     edm::Handle<T> handle_;
0036   };
0037 
0038   EG9X105XObjectUpdateModifier(const edm::ParameterSet& conf, edm::ConsumesCollector& cc);
0039   ~EG9X105XObjectUpdateModifier() override {}
0040 
0041   void setEvent(const edm::Event&) final;
0042 
0043   void modifyObject(reco::GsfElectron& ele) const final;
0044   void modifyObject(reco::Photon& pho) const final;
0045 
0046   void modifyObject(pat::Electron& ele) const final { return modifyObject(static_cast<reco::GsfElectron&>(ele)); }
0047   void modifyObject(pat::Photon& pho) const final { return modifyObject(static_cast<reco::Photon&>(pho)); }
0048 
0049 private:
0050   template <typename ObjType>
0051   static edm::Ptr<ObjType> getPtrForValueMap(const ObjType& obj,
0052                                              const edm::Handle<edm::View<ObjType> >& objsVMIsKeyedTo);
0053 
0054   TokenHandlePair<edm::View<reco::GsfElectron> > eleCollVMsAreKeyedTo_;
0055   TokenHandlePair<edm::View<reco::Photon> > phoCollVMsAreKeyedTo_;
0056 
0057   TokenHandlePair<reco::ConversionCollection> conversions_;
0058   TokenHandlePair<reco::BeamSpot> beamspot_;
0059   TokenHandlePair<EcalRecHitCollection> ecalRecHitsEB_;
0060   TokenHandlePair<EcalRecHitCollection> ecalRecHitsEE_;
0061 
0062   TokenHandlePair<edm::ValueMap<float> > eleTrkIso_;
0063   TokenHandlePair<edm::ValueMap<float> > eleTrkIso04_;
0064   TokenHandlePair<edm::ValueMap<float> > phoPhotonIso_;
0065   TokenHandlePair<edm::ValueMap<float> > phoNeutralHadIso_;
0066   TokenHandlePair<edm::ValueMap<float> > phoChargedHadIso_;
0067   TokenHandlePair<edm::ValueMap<float> > phoChargedHadWorstVtxIso_;
0068   TokenHandlePair<edm::ValueMap<float> > phoChargedHadWorstVtxConeVetoIso_;
0069   TokenHandlePair<edm::ValueMap<float> > phoChargedHadPFPVIso_;
0070   //there is a bug which GsfTracks are now allowed to be a match for conversions
0071   //due to improper linking of references in the miniAOD since 94X
0072   //this allows us to emulate it or not
0073   //note: even if this enabled, it will do nothing on miniAOD produced with 94X, 102X
0074   //till upto whenever this is fixed (11X?) as the GsfTrack references point to a different
0075   //collection to the conversion track references
0076   bool allowGsfTrkMatchForConvs_;
0077   //this allows us to update the charged hadron PF PV isolation
0078   //chargedHadPFPVIso is filled in iorules but when running on miniAOD, the value used in IDs
0079   //is remade on the miniAOD packedcandidates which differs due to rounding
0080   //its still the same variable but can have differences hence inorder to allow IDs calculated on miniAOD
0081   //on the same file to be exactly reproduced, this option is set true
0082   bool updateChargedHadPFPVIso_;
0083 };
0084 
0085 EG9X105XObjectUpdateModifier::EG9X105XObjectUpdateModifier(const edm::ParameterSet& conf, edm::ConsumesCollector& cc)
0086     : ModifyObjectValueBase(conf),
0087       eleCollVMsAreKeyedTo_(conf, "eleCollVMsAreKeyedTo", cc),
0088       phoCollVMsAreKeyedTo_(conf, "phoCollVMsAreKeyedTo", cc),
0089       conversions_(conf, "conversions", cc),
0090       beamspot_(conf, "beamspot", cc),
0091       ecalRecHitsEB_(conf, "ecalRecHitsEB", cc),
0092       ecalRecHitsEE_(conf, "ecalRecHitsEE", cc),
0093       eleTrkIso_(conf, "eleTrkIso", cc),
0094       eleTrkIso04_(conf, "eleTrkIso04", cc),
0095       phoPhotonIso_(conf, "phoPhotonIso", cc),
0096       phoNeutralHadIso_(conf, "phoNeutralHadIso", cc),
0097       phoChargedHadIso_(conf, "phoChargedHadIso", cc),
0098       phoChargedHadWorstVtxIso_(conf, "phoChargedHadWorstVtxIso", cc),
0099       phoChargedHadWorstVtxConeVetoIso_(conf, "phoChargedHadWorstVtxConeVetoIso", cc),
0100       phoChargedHadPFPVIso_(conf, "phoChargedHadPFPVIso", cc),
0101       allowGsfTrkMatchForConvs_(conf.getParameter<bool>("allowGsfTrackForConvs")),
0102       updateChargedHadPFPVIso_(conf.getParameter<bool>("updateChargedHadPFPVIso")) {}
0103 
0104 void EG9X105XObjectUpdateModifier::setEvent(const edm::Event& iEvent) {
0105   eleCollVMsAreKeyedTo_.setHandle(iEvent);
0106   phoCollVMsAreKeyedTo_.setHandle(iEvent);
0107   conversions_.setHandle(iEvent);
0108   beamspot_.setHandle(iEvent);
0109   ecalRecHitsEB_.setHandle(iEvent);
0110   ecalRecHitsEE_.setHandle(iEvent);
0111   eleTrkIso_.setHandle(iEvent);
0112   eleTrkIso04_.setHandle(iEvent);
0113   phoPhotonIso_.setHandle(iEvent);
0114   phoNeutralHadIso_.setHandle(iEvent);
0115   phoChargedHadIso_.setHandle(iEvent);
0116   phoChargedHadWorstVtxIso_.setHandle(iEvent);
0117   phoChargedHadWorstVtxConeVetoIso_.setHandle(iEvent);
0118   if (updateChargedHadPFPVIso_)
0119     phoChargedHadPFPVIso_.setHandle(iEvent);
0120 }
0121 
0122 void EG9X105XObjectUpdateModifier::modifyObject(reco::GsfElectron& ele) const {
0123   edm::Ptr<reco::GsfElectron> ptrForVM = getPtrForValueMap(ele, eleCollVMsAreKeyedTo_.handle());
0124   if (ptrForVM.isNull()) {
0125     throw cms::Exception("LogicError")
0126         << " in EG9X105ObjectUpdateModifier, line " << __LINE__ << " electron " << ele.et() << " " << ele.eta() << " "
0127         << ele.superCluster()->seed()->seed().rawId()
0128         << " failed to match to the electrons the key map was keyed to, check the map collection is correct";
0129   }
0130   reco::GsfElectron::ConversionRejection convRejVars = ele.conversionRejectionVariables();
0131   if (allowGsfTrkMatchForConvs_) {
0132     convRejVars.vtxFitProb = ConversionTools::getVtxFitProb(
0133         ConversionTools::matchedConversion(ele.core(), *conversions_.handle(), beamspot_.handle()->position()));
0134   } else {
0135     //its rather important to use the core function here to get the org trk ref
0136     convRejVars.vtxFitProb = ConversionTools::getVtxFitProb(ConversionTools::matchedConversion(
0137         ele.core()->ctfTrack(), *conversions_.handle(), beamspot_.handle()->position(), 2.0, 1e-6, 0));
0138   }
0139   ele.setConversionRejectionVariables(convRejVars);
0140 
0141   reco::GsfElectron::IsolationVariables isolVars03 = ele.dr03IsolationVariables();
0142   isolVars03.tkSumPtHEEP = (*eleTrkIso_.handle())[ptrForVM];
0143   ele.setDr03Isolation(isolVars03);
0144   reco::GsfElectron::IsolationVariables isolVars04 = ele.dr04IsolationVariables();
0145   isolVars04.tkSumPtHEEP = (*eleTrkIso04_.handle())[ptrForVM];
0146   ele.setDr04Isolation(isolVars04);
0147 }
0148 
0149 void EG9X105XObjectUpdateModifier::modifyObject(reco::Photon& pho) const {
0150   edm::Ptr<reco::Photon> ptrForVM = getPtrForValueMap(pho, phoCollVMsAreKeyedTo_.handle());
0151   if (ptrForVM.isNull()) {
0152     throw cms::Exception("LogicError")
0153         << " in EG9X105ObjectUpdateModifier, line " << __LINE__ << " photon " << pho.et() << " " << pho.eta() << " "
0154         << pho.superCluster()->seed()->seed().rawId()
0155         << " failed to match to the photons the key map was keyed to, check the map collection is correct";
0156   }
0157 
0158   reco::Photon::PflowIsolationVariables pfIso = pho.getPflowIsolationVariables();
0159   pfIso.photonIso = (*phoPhotonIso_.handle())[ptrForVM];
0160   pfIso.neutralHadronIso = (*phoNeutralHadIso_.handle())[ptrForVM];
0161   pfIso.chargedHadronIso = (*phoChargedHadIso_.handle())[ptrForVM];
0162   pfIso.chargedHadronWorstVtxIso = (*phoChargedHadWorstVtxIso_.handle())[ptrForVM];
0163   pfIso.chargedHadronWorstVtxGeomVetoIso = (*phoChargedHadWorstVtxConeVetoIso_.handle())[ptrForVM];
0164   if (updateChargedHadPFPVIso_) {
0165     pfIso.chargedHadronPFPVIso = (*phoChargedHadPFPVIso_.handle())[ptrForVM];
0166   }
0167   pho.setPflowIsolationVariables(pfIso);
0168 
0169   reco::Photon::ShowerShape fracSS = pho.showerShapeVariables();
0170   reco::Photon::ShowerShape fullSS = pho.full5x5_showerShapeVariables();
0171 
0172   const reco::CaloClusterPtr seedClus = pho.superCluster()->seed();
0173   const bool isEB = seedClus->seed().subdetId() == EcalBarrel;
0174   const auto& recHits = isEB ? *ecalRecHitsEB_.handle() : *ecalRecHitsEE_.handle();
0175   Cluster2ndMoments clus2ndMomFrac = EcalClusterTools::cluster2ndMoments(*seedClus, recHits);
0176   Cluster2ndMoments clus2ndMomFull = noZS::EcalClusterTools::cluster2ndMoments(*seedClus, recHits);
0177   fracSS.smMajor = clus2ndMomFrac.sMaj;
0178   fracSS.smMinor = clus2ndMomFrac.sMin;
0179   fracSS.smAlpha = clus2ndMomFrac.alpha;
0180   fullSS.smMajor = clus2ndMomFull.sMaj;
0181   fullSS.smMinor = clus2ndMomFull.sMin;
0182   fullSS.smAlpha = clus2ndMomFull.alpha;
0183   pho.setShowerShapeVariables(fracSS);
0184   pho.full5x5_setShowerShapeVariables(fullSS);
0185 }
0186 
0187 template <typename ObjType>
0188 edm::Ptr<ObjType> EG9X105XObjectUpdateModifier::getPtrForValueMap(
0189     const ObjType& obj, const edm::Handle<edm::View<ObjType> >& objsVMIsKeyedTo) {
0190   for (auto& objVMPtr : objsVMIsKeyedTo->ptrs()) {
0191     if (obj.superCluster()->seed()->seed() == objVMPtr->superCluster()->seed()->seed())
0192       return objVMPtr;
0193   }
0194   return edm::Ptr<ObjType>(objsVMIsKeyedTo.id());  //return null ptr if not found
0195 }
0196 
0197 DEFINE_EDM_PLUGIN(ModifyObjectValueFactory, EG9X105XObjectUpdateModifier, "EG9X105XObjectUpdateModifier");