From 149f289cbf2882c3abb6aa910b9d5912b4a1fa11 Mon Sep 17 00:00:00 2001 From: Rafael Date: Mon, 23 Jan 2017 20:56:21 +0100 Subject: [PATCH 1/2] save a copy of modified electrons and photons in the miniAOD event --- .../plugins/GsfElectronGSCrysFixer.cc | 11 ++ .../interface/ReducedEGProducer.h | 4 + .../python/reducedEgamma_cfi.py | 1 + .../src/GEDPhotonGSCrysFixer.cc | 10 ++ .../src/ReducedEGProducer.cc | 100 ++++++++++++++++-- .../python/egammaGainSwitchFix_cff.py | 6 +- RecoEgamma/EgammaTools/test/gsFixTest_cfg.py | 7 +- RecoEgamma/EgammaTools/test/miniaod_PAT.py | 4 +- 8 files changed, 126 insertions(+), 17 deletions(-) diff --git a/RecoEgamma/EgammaElectronProducers/plugins/GsfElectronGSCrysFixer.cc b/RecoEgamma/EgammaElectronProducers/plugins/GsfElectronGSCrysFixer.cc index 9ec64675d6be0..e9389d975d39c 100644 --- a/RecoEgamma/EgammaElectronProducers/plugins/GsfElectronGSCrysFixer.cc +++ b/RecoEgamma/EgammaElectronProducers/plugins/GsfElectronGSCrysFixer.cc @@ -74,6 +74,7 @@ namespace { } typedef edm::ValueMap ElectronRefMap; +typedef edm::ValueMap BoolMap; GsfElectronGSCrysFixer::GsfElectronGSCrysFixer( const edm::ParameterSet & pset ) { @@ -97,6 +98,7 @@ GsfElectronGSCrysFixer::GsfElectronGSCrysFixer( const edm::ParameterSet & pset ) produces(); produces(); + produces(); } @@ -133,6 +135,7 @@ void GsfElectronGSCrysFixer::produce( edm::Event & iEvent, const edm::EventSetup auto newCoresHandle = getHandle(iEvent,newCoresToken_); std::vector oldElectrons; + std::vector isUpdated; for(size_t eleNr=0;eleNrsize();eleNr++){ reco::GsfElectronRef eleRef(elesHandle,eleNr); @@ -160,8 +163,10 @@ void GsfElectronGSCrysFixer::produce( edm::Event & iEvent, const edm::EventSetup // std::cout <<"made a new electron "<ecalEnergy()<push_back(newEle); + isUpdated.emplace_back(true); }else{ outEles->push_back(*eleRef); + isUpdated.emplace_back(false); } } @@ -171,6 +176,12 @@ void GsfElectronGSCrysFixer::produce( edm::Event & iEvent, const edm::EventSetup refMapFiller.insert(newElectronsHandle, oldElectrons.begin(), oldElectrons.end()); refMapFiller.fill(); iEvent.put(std::move(pRefMap)); + std::unique_ptr bRefMap(new BoolMap); + BoolMap::Filler boolMapFiller(*bRefMap); + boolMapFiller.insert(newElectronsHandle, isUpdated.begin(), isUpdated.end()); + boolMapFiller.fill(); + iEvent.put(std::move(bRefMap)); + } void GsfElectronGSCrysFixer::beginLuminosityBlock(edm::LuminosityBlock const& lb, diff --git a/RecoEgamma/EgammaPhotonProducers/interface/ReducedEGProducer.h b/RecoEgamma/EgammaPhotonProducers/interface/ReducedEGProducer.h index f04d871083f5d..94fa0a2534da5 100644 --- a/RecoEgamma/EgammaPhotonProducers/interface/ReducedEGProducer.h +++ b/RecoEgamma/EgammaPhotonProducers/interface/ReducedEGProducer.h @@ -58,8 +58,10 @@ class ReducedEGProducer : public edm::stream::EDProducer<> { //tokens for input collections const edm::EDGetTokenT photonT_; const edm::EDGetTokenT > gsFixedPhotonMapT_; + const edm::EDGetTokenT > gsFixedPhotonBoolMapT_; const edm::EDGetTokenT gsfElectronT_; const edm::EDGetTokenT > gsFixedElectronMapT_; + const edm::EDGetTokenT > gsFixedElectronBoolMapT_; const edm::EDGetTokenT conversionT_; const edm::EDGetTokenT singleConversionT_; @@ -102,6 +104,8 @@ class ReducedEGProducer : public edm::stream::EDProducer<> { const StringCutObjectSelector keepGsfElectronSel_; const StringCutObjectSelector slimRelinkGsfElectronSel_; const StringCutObjectSelector relinkGsfElectronSel_; + + bool keepObjectsBeforeGSFix_; }; #endif diff --git a/RecoEgamma/EgammaPhotonProducers/python/reducedEgamma_cfi.py b/RecoEgamma/EgammaPhotonProducers/python/reducedEgamma_cfi.py index 25255b9a3269b..d21f48bab5ad9 100644 --- a/RecoEgamma/EgammaPhotonProducers/python/reducedEgamma_cfi.py +++ b/RecoEgamma/EgammaPhotonProducers/python/reducedEgamma_cfi.py @@ -1,6 +1,7 @@ import FWCore.ParameterSet.Config as cms reducedEgamma = cms.EDProducer("ReducedEGProducer", + keepObjectsBeforeGSFix = cms.bool(True), keepPhotons = cms.string("hadTowOverEm()<0.15 && pt>10 && (pt>14 || chargedHadronIso()<10)"), #keep in output slimRelinkPhotons = cms.string("hadTowOverEm()<0.15 && pt>10 && (pt>14 || chargedHadronIso()<10)"), #keep only slimmed SuperCluster plus seed cluster relinkPhotons = cms.string("(r9()>0.8 || chargedHadronIso()<20 || chargedHadronIso()<0.3*pt())"), #keep all associated clusters/rechits/conversions diff --git a/RecoEgamma/EgammaPhotonProducers/src/GEDPhotonGSCrysFixer.cc b/RecoEgamma/EgammaPhotonProducers/src/GEDPhotonGSCrysFixer.cc index 05ebc181e8bb8..bbdf100772ac3 100644 --- a/RecoEgamma/EgammaPhotonProducers/src/GEDPhotonGSCrysFixer.cc +++ b/RecoEgamma/EgammaPhotonProducers/src/GEDPhotonGSCrysFixer.cc @@ -9,6 +9,7 @@ namespace { } typedef edm::ValueMap PhotonRefMap; +typedef edm::ValueMap BoolMap; GEDPhotonGSCrysFixer::GEDPhotonGSCrysFixer( const edm::ParameterSet & pset ) { @@ -32,6 +33,7 @@ GEDPhotonGSCrysFixer::GEDPhotonGSCrysFixer( const edm::ParameterSet & pset ) produces(); produces(); + produces(); } namespace { @@ -67,6 +69,7 @@ void GEDPhotonGSCrysFixer::produce( edm::Event & iEvent, const edm::EventSetup & auto newCoresHandle = getHandle(iEvent,newCoresToken_); std::vector oldPhotons; + std::vector isUpdated; for(size_t phoNr=0;phoNrsize();phoNr++){ reco::PhotonRef phoRef(phosHandle,phoNr); @@ -95,17 +98,24 @@ void GEDPhotonGSCrysFixer::produce( edm::Event & iEvent, const edm::EventSetup & } outPhos->push_back(newPho); + isUpdated.emplace_back(true); }else{ outPhos->push_back(*phoRef); + isUpdated.emplace_back(false); } } auto&& newPhotonsHandle(iEvent.put(std::move(outPhos))); std::unique_ptr pRefMap(new PhotonRefMap); PhotonRefMap::Filler refMapFiller(*pRefMap); + std::unique_ptr bRefMap(new BoolMap); + BoolMap::Filler boolMapFiller(*bRefMap); refMapFiller.insert(newPhotonsHandle, oldPhotons.begin(), oldPhotons.end()); refMapFiller.fill(); + boolMapFiller.insert(newPhotonsHandle, isUpdated.begin(), isUpdated.end()); + boolMapFiller.fill(); iEvent.put(std::move(pRefMap)); + iEvent.put(std::move(bRefMap)); } void GEDPhotonGSCrysFixer::beginLuminosityBlock(edm::LuminosityBlock const& lb, diff --git a/RecoEgamma/EgammaPhotonProducers/src/ReducedEGProducer.cc b/RecoEgamma/EgammaPhotonProducers/src/ReducedEGProducer.cc index 2d53fd9efc584..25fdae018bb1c 100644 --- a/RecoEgamma/EgammaPhotonProducers/src/ReducedEGProducer.cc +++ b/RecoEgamma/EgammaPhotonProducers/src/ReducedEGProducer.cc @@ -56,8 +56,10 @@ namespace std { ReducedEGProducer::ReducedEGProducer(const edm::ParameterSet& config) : photonT_(consumes(config.getParameter("photons"))), gsFixedPhotonMapT_(consumes >(config.getParameter("gsFixedPhotonMap"))), + gsFixedPhotonBoolMapT_(consumes >(config.getParameter("gsFixedPhotonMap"))), gsfElectronT_(consumes(config.getParameter("gsfElectrons"))), gsFixedElectronMapT_(consumes >(config.getParameter("gsFixedElectronMap"))), + gsFixedElectronBoolMapT_(consumes >(config.getParameter("gsFixedElectronMap"))), conversionT_(consumes(config.getParameter("conversions"))), singleConversionT_(consumes(config.getParameter("singleConversions"))), barrelEcalHits_(consumes(config.getParameter("barrelEcalHits"))), @@ -89,7 +91,8 @@ ReducedEGProducer::ReducedEGProducer(const edm::ParameterSet& config) : relinkPhotonSel_(config.getParameter("relinkPhotons")), keepGsfElectronSel_(config.getParameter("keepGsfElectrons")), slimRelinkGsfElectronSel_(config.getParameter("slimRelinkGsfElectrons")), - relinkGsfElectronSel_(config.getParameter("relinkGsfElectrons")) + relinkGsfElectronSel_(config.getParameter("relinkGsfElectrons")), + keepObjectsBeforeGSFix_(config.getParameter("keepObjectsBeforeGSFix")) { const std::vector& photonidinputs = config.getParameter >("photonIDSources"); @@ -119,6 +122,15 @@ ReducedEGProducer::ReducedEGProducer(const edm::ParameterSet& config) : produces< reco::PhotonCoreCollection >(outPhotonCores_); produces< reco::GsfElectronCollection >(outGsfElectrons_); produces< reco::GsfElectronCoreCollection >(outGsfElectronCores_); + + if (keepObjectsBeforeGSFix_) { + produces< reco::PhotonCollection >(outPhotons_+"BeforeGSFix"); + produces< reco::PhotonCoreCollection >(outPhotonCores_+"BeforeGSFix"); + produces< reco::GsfElectronCollection >(outGsfElectrons_+"BeforeGSFix"); + produces< reco::GsfElectronCoreCollection >(outGsfElectronCores_+"BeforeGSFix"); + } + + // We still have to implement the collections below produces< reco::ConversionCollection >(outConversions_); produces< reco::ConversionCollection >(outSingleConversions_); produces< reco::SuperClusterCollection >(outSuperClusters_); @@ -152,20 +164,22 @@ ReducedEGProducer::~ReducedEGProducer() void ReducedEGProducer::produce(edm::Event& theEvent, const edm::EventSetup& theEventSetup) { - //get input collections - - + //get input collections edm::Handle photonHandle; theEvent.getByToken(photonT_, photonHandle); edm::Handle > gsFixedPhotonMapHandle; + edm::Handle > gsFixedPhotonBoolMapHandle; theEvent.getByToken(gsFixedPhotonMapT_, gsFixedPhotonMapHandle); + theEvent.getByToken(gsFixedPhotonBoolMapT_, gsFixedPhotonBoolMapHandle); edm::Handle gsfElectronHandle; theEvent.getByToken(gsfElectronT_, gsfElectronHandle); edm::Handle > gsFixedElectronMapHandle; + edm::Handle > gsFixedElectronBoolMapHandle; theEvent.getByToken(gsFixedElectronMapT_, gsFixedElectronMapHandle); + theEvent.getByToken(gsFixedElectronBoolMapT_, gsFixedElectronBoolMapHandle); edm::Handle conversionHandle; theEvent.getByToken(conversionT_, conversionHandle); @@ -217,6 +231,12 @@ void ReducedEGProducer::produce(edm::Event& theEvent, const edm::EventSetup& the std::auto_ptr photonCores(new reco::PhotonCoreCollection); std::auto_ptr gsfElectrons(new reco::GsfElectronCollection); std::auto_ptr gsfElectronCores(new reco::GsfElectronCoreCollection); + + std::auto_ptr photons_beforeGSFix(new reco::PhotonCollection); + std::auto_ptr photonCores_beforeGSFix(new reco::PhotonCoreCollection); + std::auto_ptr gsfElectrons_beforeGSFix(new reco::GsfElectronCollection); + std::auto_ptr gsfElectronCores_beforeGSFix(new reco::GsfElectronCoreCollection); + std::auto_ptr conversions(new reco::ConversionCollection); std::auto_ptr singleConversions(new reco::ConversionCollection); std::auto_ptr superClusters(new reco::SuperClusterCollection); @@ -250,7 +270,9 @@ void ReducedEGProducer::produce(edm::Event& theEvent, const edm::EventSetup& the //maps to collection indices of output objects std::map photonCoreMap; + std::map photonCoreMap_beforeGSFix; std::map gsfElectronCoreMap; + std::map gsfElectronCoreMap_beforeGSFix; std::map conversionMap; std::map singleConversionMap; std::map superClusterMap; @@ -284,8 +306,23 @@ void ReducedEGProducer::produce(edm::Event& theEvent, const edm::EventSetup& the // // check if it was fixed // std::cout << gsFixedPhotonMapHandle->contains(gsFixedPhotonRef.id()) << std::endl; // std::cout << (*gsFixedPhotonMapHandle)[gsFixedPhotonRef].isNull() << std::endl; - reco::PhotonRef photonref = (*gsFixedPhotonMapHandle)[gsFixedPhotonRef]; - + reco::PhotonRef photonref; + if (gsFixedPhotonMapHandle.isValid()) { + photonref = (*gsFixedPhotonMapHandle)[gsFixedPhotonRef]; + if (gsFixedPhotonBoolMapHandle.isValid()) { + if (keepObjectsBeforeGSFix_ && (*gsFixedPhotonBoolMapHandle)[gsFixedPhotonRef]) { + photons_beforeGSFix->push_back(*photonref); + const reco::PhotonCoreRef &photonCore_beforeGSFix = photonref->photonCore(); + photonCores_beforeGSFix->push_back(*photonCore_beforeGSFix); + if (!photonCoreMap_beforeGSFix.count(photonCore_beforeGSFix)) { + photonCoreMap_beforeGSFix[photonCore_beforeGSFix] = photonCores_beforeGSFix->size() - 1; + } + } + } + } else { + photonref = gsFixedPhotonRef; + } + //fill pf candidate value map vector pfCandIsoPairVecPho.push_back((*photonPfCandMapHandle)[photonref]); @@ -358,7 +395,24 @@ void ReducedEGProducer::produce(edm::Event& theEvent, const edm::EventSetup& the gsfElectrons->push_back(gsfElectron); reco::GsfElectronRef gsFixedGsfElectronref(gsfElectronHandle,iele); - reco::GsfElectronRef gsfElectronref = (*gsFixedElectronMapHandle)[gsFixedGsfElectronref]; + reco::GsfElectronRef gsfElectronref; + + + if (gsFixedElectronMapHandle.isValid()) { + gsfElectronref = (*gsFixedElectronMapHandle)[gsFixedGsfElectronref]; + if (gsFixedElectronBoolMapHandle.isValid()) { + if (keepObjectsBeforeGSFix_ && (*gsFixedElectronBoolMapHandle)[gsFixedGsfElectronref]) { + gsfElectrons_beforeGSFix->push_back(*gsfElectronref); + const reco::GsfElectronCoreRef &gsfElectronCore_beforeGSFix = gsfElectronref->core(); + gsfElectronCores_beforeGSFix->push_back(*gsfElectronCore_beforeGSFix); + if (!gsfElectronCoreMap_beforeGSFix.count(gsfElectronCore_beforeGSFix)) { + gsfElectronCoreMap_beforeGSFix[gsfElectronCore_beforeGSFix] = gsfElectronCores_beforeGSFix->size() - 1; + } + } + } + } else { + gsfElectronref = gsFixedGsfElectronref; + } pfCandIsoPairVecEle.push_back((*gsfElectronPfCandMapHandle)[gsfElectronref]); @@ -678,7 +732,7 @@ void ReducedEGProducer::produce(edm::Event& theEvent, const edm::EventSetup& the //put photon and gsfelectroncores into the event const edm::OrphanHandle &outPhotonCoreHandle = theEvent.put(photonCores,outPhotonCores_); const edm::OrphanHandle &outgsfElectronCoreHandle = theEvent.put(gsfElectronCores,outGsfElectronCores_); - + //loop over photons and electrons and relink the cores for (reco::Photon &photon : *photons) { const auto &coremapped = photonCoreMap.find(photon.photonCore()); @@ -697,11 +751,39 @@ void ReducedEGProducer::produce(edm::Event& theEvent, const edm::EventSetup& the gsfElectron.setCore(coreref); } } + + if (keepObjectsBeforeGSFix_) { + const edm::OrphanHandle &outPhotonCoreHandle_beforeGSFix = theEvent.put(photonCores_beforeGSFix,outPhotonCores_+"BeforeGSFix"); + const edm::OrphanHandle &outgsfElectronCoreHandle_beforeGSFix = theEvent.put(gsfElectronCores_beforeGSFix,outGsfElectronCores_+"BeforeGSFix"); + + for (reco::Photon &photon : *photons_beforeGSFix) { + const auto &coremapped = photonCoreMap_beforeGSFix.find(photon.photonCore()); + if (coremapped != photonCoreMap_beforeGSFix.end()) { + //make new ref + reco::PhotonCoreRef coreref(outPhotonCoreHandle_beforeGSFix,coremapped->second); + photon.setPhotonCore(coreref); + } + } + + for (reco::GsfElectron &gsfElectron : *gsfElectrons_beforeGSFix) { + const auto &coremapped = gsfElectronCoreMap_beforeGSFix.find(gsfElectron.core()); + if (coremapped != gsfElectronCoreMap_beforeGSFix.end()) { + //make new ref + reco::GsfElectronCoreRef coreref(outgsfElectronCoreHandle_beforeGSFix,coremapped->second); + gsfElectron.setCore(coreref); + } + } + + } + //(finally) store the output photon and electron collections const edm::OrphanHandle &outPhotonHandle = theEvent.put(photons,outPhotons_); const edm::OrphanHandle &outGsfElectronHandle = theEvent.put(gsfElectrons,outGsfElectrons_); - + if (keepObjectsBeforeGSFix_) { + const edm::OrphanHandle &outPhotonsHandle_beforeGSFix = theEvent.put(photons_beforeGSFix,outPhotons_+"BeforeGSFix"); + const edm::OrphanHandle &outgsfElectronsHandle_beforeGSFix = theEvent.put(gsfElectrons_beforeGSFix,outGsfElectrons_+"BeforeGSFix"); + } //still need to output relinked valuemaps //photon pfcand isolation valuemap diff --git a/RecoEgamma/EgammaTools/python/egammaGainSwitchFix_cff.py b/RecoEgamma/EgammaTools/python/egammaGainSwitchFix_cff.py index e85642ffbd45f..0bc4db62c9563 100644 --- a/RecoEgamma/EgammaTools/python/egammaGainSwitchFix_cff.py +++ b/RecoEgamma/EgammaTools/python/egammaGainSwitchFix_cff.py @@ -1,7 +1,5 @@ import FWCore.ParameterSet.Config as cms -bunchSpacingProducerGSFix = cms.EDProducer("BunchSpacingProducer") - #this module re-makes the rec-hits using the weights reco for hits saved in ecal selected digis from RecoEgamma.EgammaTools.ecalWeightRecHitFromSelectedDigis_cff import * #this module makes a new collection of barrel rechits where gain switched multifit crystals are swapped @@ -24,7 +22,7 @@ from RecoEgamma.EgammaPhotonProducers.gsFixedGedPhotons_cfi import * egammaGainSwitchFixSequence = cms.Sequence( - bunchSpacingProducerGSFix* +# bunchSpacingProducer* ecalWeightLocalRecoFromSelectedDigis* ecalMultiAndGSWeightRecHitEB* gsFixedParticleFlowSuperClustering* @@ -39,7 +37,7 @@ from RecoEgamma.EgammaElectronProducers.gsSimpleFixedPhotons_cfi import gsSimpleFixedPhotons egammaGainSwitchSimpleFixSequence = cms.Sequence( - bunchSpacingProducerGSFix* +# bunchSpacingProducer* ecalWeightLocalRecoFromSelectedDigis* ecalMultiAndGSWeightRecHitEB* gsSimpleFixedGsfElectrons* diff --git a/RecoEgamma/EgammaTools/test/gsFixTest_cfg.py b/RecoEgamma/EgammaTools/test/gsFixTest_cfg.py index 27b420d209755..3cb6c10460f20 100644 --- a/RecoEgamma/EgammaTools/test/gsFixTest_cfg.py +++ b/RecoEgamma/EgammaTools/test/gsFixTest_cfg.py @@ -35,12 +35,13 @@ # correction is ( supercluster raw energy - E(gain switched multi fit hits) + E(gain switched weights hits) ) / supercluster raw energy # does not correct photon H/E or R9 +process.load("RecoLuminosity.LumiProducer.bunchSpacingProducer_cfi") +process.bunchSpacingProducerSequence = cms.Sequence(process.bunchSpacingProducer) + process.load("RecoEgamma.EgammaTools.egammaGainSwitchFix_cff") -process.p = cms.Path(process.egammaGainSwitchFixSequence) +process.p = cms.Path(process.bunchSpacingProducerSequence * process.egammaGainSwitchFixSequence) - - #dumps the products made for easier debugging, you wouldnt normally need to do this #edmDumpEventContent outputTest.root shows you all the products produced #will be very slow when this is happening diff --git a/RecoEgamma/EgammaTools/test/miniaod_PAT.py b/RecoEgamma/EgammaTools/test/miniaod_PAT.py index 1ffd187ef55ac..d788edc543287 100644 --- a/RecoEgamma/EgammaTools/test/miniaod_PAT.py +++ b/RecoEgamma/EgammaTools/test/miniaod_PAT.py @@ -55,9 +55,11 @@ fastCloning = cms.untracked.bool(False), fileName = cms.untracked.string('file:miniaod.root'), outputCommands = process.MINIAODEventContent.outputCommands, - overrideInputFileSplitLevels = cms.untracked.bool(True) + overrideInputFileSplitLevels = cms.untracked.bool(True), ) +# process.MINIAODoutput.outputCommands.extend(['keep *_reducedEgamma_*BeforeGSFix_*']) + # Additional output definition # Other statements From 2bdf876728977616ce7c9272a4d841e4622fc1e8 Mon Sep 17 00:00:00 2001 From: Rafael Date: Mon, 23 Jan 2017 21:00:29 +0100 Subject: [PATCH 2/2] keep the modified objects --- RecoEgamma/EgammaTools/test/miniaod_PAT.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RecoEgamma/EgammaTools/test/miniaod_PAT.py b/RecoEgamma/EgammaTools/test/miniaod_PAT.py index d788edc543287..25537837c52c7 100644 --- a/RecoEgamma/EgammaTools/test/miniaod_PAT.py +++ b/RecoEgamma/EgammaTools/test/miniaod_PAT.py @@ -58,7 +58,7 @@ overrideInputFileSplitLevels = cms.untracked.bool(True), ) -# process.MINIAODoutput.outputCommands.extend(['keep *_reducedEgamma_*BeforeGSFix_*']) +process.MINIAODoutput.outputCommands.extend(['keep *_reducedEgamma_*BeforeGSFix_*']) # Additional output definition