4 #include <unordered_set>
51 preshowerEcalHits_(doPreshowerEcalHits_
59 recoHIPhotonIsolationMapInputToken_{
61 ? consumes<reco::HIPhotonIsolationMap>(
config.getParameter<
edm::InputTag>(
"hiPhotonIsolationMapInput"))
63 recoHIPhotonIsolationMapOutputName_{!recoHIPhotonIsolationMapInputToken_.
isUninitialized()
67 applyPhotonCalibOnData_(
config.getParameter<
bool>(
"applyPhotonCalibOnData")),
68 applyPhotonCalibOnMC_(
config.getParameter<
bool>(
"applyPhotonCalibOnMC")),
69 applyGsfElectronCalibOnData_(
config.getParameter<
bool>(
"applyGsfElectronCalibOnData")),
70 applyGsfElectronCalibOnMC_(
config.getParameter<
bool>(
"applyGsfElectronCalibOnMC")),
72 outPhotons_(
"reducedGedPhotons"),
73 outPhotonCores_(
"reducedGedPhotonCores"),
74 outOOTPhotons_(
"reducedOOTPhotons"),
75 outOOTPhotonCores_(
"reducedOOTPhotonCores"),
76 outGsfElectrons_(
"reducedGedGsfElectrons"),
77 outGsfElectronCores_(
"reducedGedGsfElectronCores"),
78 outGsfTracks_(
"reducedGsfTracks"),
79 outConversions_(
"reducedConversions"),
80 outSingleConversions_(
"reducedSingleLegConversions"),
81 outSuperClusters_(
"reducedSuperClusters"),
82 outEBEEClusters_(
"reducedEBEEClusters"),
83 outESClusters_(
"reducedESClusters"),
84 outOOTSuperClusters_(
"reducedOOTSuperClusters"),
85 outOOTEBEEClusters_(
"reducedOOTEBEEClusters"),
86 outOOTESClusters_(
"reducedOOTESClusters"),
87 outEBRecHits_(
"reducedEBRecHits"),
88 outEERecHits_(
"reducedEERecHits"),
89 outESRecHits_(
"reducedESRecHits"),
90 outHBHERecHits_(
"reducedHBHEHits"),
91 outPhotonPfCandMap_(
"reducedPhotonPfCandMap"),
92 outGsfElectronPfCandMap_(
"reducedGsfElectronPfCandMap"),
93 outPhotonIds_(
config.getParameter<std::vector<std::string>>(
"photonIDOutput")),
94 outGsfElectronIds_(
config.getParameter<std::vector<std::string>>(
"gsfElectronIDOutput")),
95 outPhotonFloatValueMaps_(
config.getParameter<std::vector<std::string>>(
"photonFloatValueMapOutput")),
96 outOOTPhotonFloatValueMaps_(
config.getParameter<std::vector<std::string>>(
"ootPhotonFloatValueMapOutput")),
97 outGsfElectronFloatValueMaps_(
config.getParameter<std::vector<std::string>>(
"gsfElectronFloatValueMapOutput")),
102 slimRelinkOOTPhotonSel_(
config.getParameter<
std::string>(
"slimRelinkOOTPhotons")),
105 slimRelinkGsfElectronSel_(
config.getParameter<
std::string>(
"slimRelinkGsfElectrons")),
109 caloTopology_ = esConsumes();
110 if (not aTag.
label().empty())
111 ootPhotonT_ = consumes<reco::PhotonCollection>(aTag);
117 for (
const edm::InputTag&
tag :
config.getParameter<std::vector<edm::InputTag>>(
"gsfElectronIDSources")) {
121 for (
const edm::InputTag&
tag :
config.getParameter<std::vector<edm::InputTag>>(
"photonFloatValueMapSources")) {
125 for (
const edm::InputTag&
tag :
config.getParameter<std::vector<edm::InputTag>>(
"ootPhotonFloatValueMapSources")) {
129 for (
const edm::InputTag&
tag :
config.getParameter<std::vector<edm::InputTag>>(
"gsfElectronFloatValueMapSources")) {
133 if (applyPhotonCalibOnData_ || applyPhotonCalibOnMC_) {
134 setToken(photonCalibEnergyT_,
config,
"photonCalibEnergySource");
135 setToken(photonCalibEnergyErrT_,
config,
"photonCalibEnergyErrSource");
137 if (applyGsfElectronCalibOnData_ || applyGsfElectronCalibOnMC_) {
138 setToken(gsfElectronCalibEnergyT_,
config,
"gsfElectronCalibEnergySource");
139 setToken(gsfElectronCalibEnergyErrT_,
config,
"gsfElectronCalibEnergyErrSource");
140 setToken(gsfElectronCalibEcalEnergyT_,
config,
"gsfElectronCalibEcalEnergySource");
141 setToken(gsfElectronCalibEcalEnergyErrT_,
config,
"gsfElectronCalibEcalEnergyErrSource");
144 produces<reco::PhotonCollection>(outPhotons_);
145 produces<reco::PhotonCoreCollection>(outPhotonCores_);
146 if (!ootPhotonT_.isUninitialized()) {
147 produces<reco::PhotonCollection>(outOOTPhotons_);
148 produces<reco::PhotonCoreCollection>(outOOTPhotonCores_);
150 produces<reco::GsfElectronCollection>(outGsfElectrons_);
151 produces<reco::GsfElectronCoreCollection>(outGsfElectronCores_);
152 produces<reco::GsfTrackCollection>(outGsfTracks_);
153 produces<reco::ConversionCollection>(outConversions_);
154 produces<reco::ConversionCollection>(outSingleConversions_);
155 produces<reco::SuperClusterCollection>(outSuperClusters_);
156 produces<reco::CaloClusterCollection>(outEBEEClusters_);
157 produces<reco::CaloClusterCollection>(outESClusters_);
158 if (!ootPhotonT_.isUninitialized()) {
159 produces<reco::SuperClusterCollection>(outOOTSuperClusters_);
160 produces<reco::CaloClusterCollection>(outOOTEBEEClusters_);
161 produces<reco::CaloClusterCollection>(outOOTESClusters_);
163 produces<EcalRecHitCollection>(outEBRecHits_);
164 produces<EcalRecHitCollection>(outEERecHits_);
165 if (doPreshowerEcalHits_)
166 produces<EcalRecHitCollection>(outESRecHits_);
167 produces<HBHERecHitCollection>(outHBHERecHits_);
168 produces<edm::ValueMap<std::vector<reco::PFCandidateRef>>>(outPhotonPfCandMap_);
169 produces<edm::ValueMap<std::vector<reco::PFCandidateRef>>>(outGsfElectronPfCandMap_);
171 produces<edm::ValueMap<bool>>(outid);
173 for (
const std::string& outid : outGsfElectronIds_) {
174 produces<edm::ValueMap<float>>(outid);
176 for (
const std::string& outid : outPhotonFloatValueMaps_) {
177 produces<edm::ValueMap<float>>(outid);
179 if (!ootPhotonT_.isUninitialized()) {
180 for (
const std::string& outid : outOOTPhotonFloatValueMaps_) {
181 produces<edm::ValueMap<float>>(outid);
184 for (
const std::string& outid : outGsfElectronFloatValueMaps_) {
185 produces<edm::ValueMap<float>>(outid);
187 if (!recoHIPhotonIsolationMapInputToken_.isUninitialized()) {
188 produces<reco::HIPhotonIsolationMap>(recoHIPhotonIsolationMapOutputName_);
235 std::vector<edm::Handle<edm::ValueMap<bool>>> photonIdHandles(
photonIdTs_.size());
241 std::vector<edm::Handle<edm::ValueMap<float>>> gsfElectronIdHandles(
gsfElectronIdTs_.size());
250 theEvent.
getByToken(photonFloatValueMapT, photonFloatValueMapHandles[
index++]);
257 theEvent.
getByToken(ootPhotonFloatValueMapT, ootPhotonFloatValueMapHandles[
index++]);
264 theEvent.
getByToken(gsfElectronFloatValueMapT, gsfElectronFloatValueMapHandles[
index++]);
288 auto photons = std::make_unique<reco::PhotonCollection>();
289 auto photonCores = std::make_unique<reco::PhotonCoreCollection>();
290 auto ootPhotons = std::make_unique<reco::PhotonCollection>();
291 auto ootPhotonCores = std::make_unique<reco::PhotonCoreCollection>();
292 auto gsfElectrons = std::make_unique<reco::GsfElectronCollection>();
293 auto gsfElectronCores = std::make_unique<reco::GsfElectronCoreCollection>();
294 auto gsfTracks = std::make_unique<reco::GsfTrackCollection>();
295 auto conversions = std::make_unique<reco::ConversionCollection>();
297 auto superClusters = std::make_unique<reco::SuperClusterCollection>();
298 auto ebeeClusters = std::make_unique<reco::CaloClusterCollection>();
299 auto esClusters = std::make_unique<reco::CaloClusterCollection>();
300 auto ootSuperClusters = std::make_unique<reco::SuperClusterCollection>();
301 auto ootEbeeClusters = std::make_unique<reco::CaloClusterCollection>();
302 auto ootEsClusters = std::make_unique<reco::CaloClusterCollection>();
303 auto ebRecHits = std::make_unique<EcalRecHitCollection>();
304 auto eeRecHits = std::make_unique<EcalRecHitCollection>();
305 auto esRecHits = std::make_unique<EcalRecHitCollection>();
306 auto hbheRecHits = std::make_unique<HBHERecHitCollection>();
307 auto photonPfCandMap = std::make_unique<edm::ValueMap<std::vector<reco::PFCandidateRef>>>();
308 auto gsfElectronPfCandMap = std::make_unique<edm::ValueMap<std::vector<reco::PFCandidateRef>>>();
311 std::map<reco::PhotonCoreRef, unsigned int> photonCoreMap;
312 std::map<reco::PhotonCoreRef, unsigned int> ootPhotonCoreMap;
313 std::map<reco::GsfElectronCoreRef, unsigned int> gsfElectronCoreMap;
314 std::map<reco::GsfTrackRef, unsigned int> gsfTrackMap;
315 std::map<reco::ConversionRef, unsigned int> conversionMap;
316 std::map<reco::ConversionRef, unsigned int> singleConversionMap;
317 std::map<reco::SuperClusterRef, unsigned int> superClusterMap;
318 std::map<reco::CaloClusterPtr, unsigned int> ebeeClusterMap;
319 std::map<reco::CaloClusterPtr, unsigned int> esClusterMap;
320 std::map<reco::SuperClusterRef, unsigned int> ootSuperClusterMap;
321 std::map<reco::CaloClusterPtr, unsigned int> ootEbeeClusterMap;
322 std::map<reco::CaloClusterPtr, unsigned int> ootEsClusterMap;
323 std::unordered_set<DetId> rechitMap;
324 std::unordered_set<DetId> hcalRechitMap;
326 std::unordered_set<unsigned int> superClusterFullRelinkMap;
327 std::unordered_set<unsigned int> ootSuperClusterFullRelinkMap;
330 std::vector<std::vector<reco::PFCandidateRef>> pfCandIsoPairVecPho;
331 std::vector<std::vector<reco::PFCandidateRef>> pfCandIsoPairVecEle;
334 std::vector<std::vector<bool>> photonIdVals(photonIdHandles.size());
335 std::vector<std::vector<float>> gsfElectronIdVals(gsfElectronIdHandles.size());
336 std::vector<std::vector<float>> photonFloatValueMapVals(photonFloatValueMapHandles.size());
337 std::vector<std::vector<float>> ootPhotonFloatValueMapVals(ootPhotonFloatValueMapHandles.size());
338 std::vector<std::vector<float>> gsfElectronFloatValueMapVals(gsfElectronFloatValueMapHandles.size());
344 std::vector<reco::HIPhotonIsolation> recoHIPhotonIsolationMapInputVals;
348 for (
const auto&
photon : *photonHandle) {
353 auto& newPhoton =
photons->back();
356 calibratePhoton(newPhoton, photonref, *photonCalibEnergyHandle, *photonCalibEnergyErrHandle);
367 pfCandIsoPairVecPho.push_back((*photonPfCandMapHandle)[photonref]);
371 for (
const auto& photonIdHandle : photonIdHandles) {
372 photonIdVals[subindex++].push_back((*photonIdHandle)[photonref]);
376 for (
const auto& photonFloatValueMapHandle : photonFloatValueMapHandles) {
377 photonFloatValueMapVals[subindex++].push_back((*photonFloatValueMapHandle)[photonref]);
382 recoHIPhotonIsolationMapInputVals.push_back((*recoHIPhotonIsolationMapInputValueMap)[photonref]);
421 for (
const auto& ootPhoton : *ootPhotonHandle) {
434 for (
const auto& ootPhotonFloatValueMapHandle : ootPhotonFloatValueMapHandles) {
435 ootPhotonFloatValueMapVals[subindex++].push_back((*ootPhotonFloatValueMapHandle)[ootPhotonref]);
450 linkSuperCluster(ootSuperCluster, ootSuperClusterMap, *ootSuperClusters, relink, ootSuperClusterFullRelinkMap);
452 linkHcalHits(*ootPhoton.superCluster(), *hbheHitHandle, hcalRechitMap);
458 for (
const auto& gsfElectron : *gsfElectronHandle) {
468 *gsfElectronCalibEnergyHandle,
469 *gsfElectronCalibEnergyErrHandle,
470 *gsfElectronCalibEcalEnergyHandle,
471 *gsfElectronCalibEcalEnergyErrHandle);
480 pfCandIsoPairVecEle.push_back((*gsfElectronPfCandMapHandle)[gsfElectronref]);
484 for (
const auto& gsfElectronIdHandle : gsfElectronIdHandles) {
485 gsfElectronIdVals[subindex++].push_back((*gsfElectronIdHandle)[gsfElectronref]);
489 for (
const auto& gsfElectronFloatValueMapHandle : gsfElectronFloatValueMapHandles) {
490 gsfElectronFloatValueMapVals[subindex++].push_back((*gsfElectronFloatValueMapHandle)[gsfElectronref]);
494 linkCore(gsfElectronCore, *gsfElectronCores, gsfElectronCoreMap);
499 if (!gsfTrackMap.count(gsfTrack)) {
501 gsfTrackMap[gsfTrack] =
gsfTracks->size() - 1;
505 for (
auto igsf = gsfElectron.ambiguousGsfTracksBegin(); igsf != gsfElectron.ambiguousGsfTracksEnd(); ++igsf) {
507 if (!gsfTrackMap.count(ambigGsfTrack)) {
509 gsfTrackMap[ambigGsfTrack] =
gsfTracks->size() - 1;
541 linkHcalHits(*gsfElectron.superCluster(), *hbheHitHandle, hcalRechitMap);
552 bool fullrelink = superClusterFullRelinkMap.count(
index++);
555 superCluster.clearHitsAndFractions();
580 for (
auto& ootSuperCluster : *ootSuperClusters) {
586 bool fullrelink = ootSuperClusterFullRelinkMap.count(
index++);
589 ootSuperCluster.clearHitsAndFractions();
608 for (
const EcalRecHit& rechit : *barrelHitHandle) {
609 if (rechitMap.count(rechit.detid())) {
610 ebRecHits->push_back(rechit);
614 for (
const EcalRecHit& rechit : *endcapHitHandle) {
615 if (rechitMap.count(rechit.detid())) {
616 eeRecHits->push_back(rechit);
624 for (
const EcalRecHit& rechit : *preshowerHitHandle) {
625 if (rechitMap.count(rechit.detid())) {
626 esRecHits->push_back(rechit);
632 for (
const HBHERecHit& rechit : *hbheHitHandle) {
633 if (hcalRechitMap.count(rechit.detid())) {
634 hbheRecHits->push_back(rechit);
647 relinkCaloClusters(superCluster, ebeeClusterMap, esClusterMap, outEBEEClusterHandle, outESClusterHandle);
660 ootSuperCluster, ootEbeeClusterMap, ootEsClusterMap, outOOTEBEEClusterHandle, outOOTESClusterHandle);
722 std::vector<reco::GsfTrackRef> ambigTracksInThisElectron;
724 for (
auto igsf = gsfElectron.ambiguousGsfTracksBegin(); igsf != gsfElectron.ambiguousGsfTracksEnd(); ++igsf) {
725 ambigTracksInThisElectron.push_back(*igsf);
729 gsfElectron.clearAmbiguousGsfTracks();
732 for (
const auto& it : ambigTracksInThisElectron) {
733 const auto& gsftkmapped = gsfTrackMap.find(it);
735 if (gsftkmapped != gsfTrackMap.end()) {
737 gsfElectron.addAmbiguousGsfTrack(gsftkref);
739 throw cms::Exception(
"There must be a problem with linking and mapping of ambiguous gsf tracks...");
742 if (gsfElectron.ambiguousGsfTracksSize() > 0)
743 gsfElectron.setAmbiguous(
true);
745 ambigTracksInThisElectron.clear();
759 fillerPhotons.insert(outPhotonHandle, pfCandIsoPairVecPho.begin(), pfCandIsoPairVecPho.end());
760 fillerPhotons.fill();
764 fillerGsfElectrons.insert(outGsfElectronHandle, pfCandIsoPairVecEle.begin(), pfCandIsoPairVecEle.end());
765 fillerGsfElectrons.fill();
772 auto oMap = std::make_unique<MapType>();
774 typename MapType::Filler
filler(*oMap);
775 filler.insert(refH, vec.begin(), vec.end());
783 for (
auto const& vals : photonIdVals) {
789 for (
auto const& vals : gsfElectronIdVals) {
795 for (
auto const& vals : photonFloatValueMapVals) {
802 for (
auto const& vals : ootPhotonFloatValueMapVals) {
814 for (
auto const& vals : gsfElectronFloatValueMapVals) {
819 template <
typename T,
typename U>
821 if (!coreMap.count(
core)) {
828 std::map<reco::SuperClusterRef, unsigned int>& superClusterMap,
831 std::unordered_set<unsigned int>& superClusterFullRelinkMap) {
832 const auto& mappedsc = superClusterMap.find(superCluster);
835 unsigned int mappedscidx = 0;
836 if (mappedsc == superClusterMap.end()) {
839 superClusterMap[superCluster] = mappedscidx;
841 mappedscidx = mappedsc->second;
846 superClusterFullRelinkMap.insert(mappedscidx);
851 std::map<reco::ConversionRef, unsigned int>& conversionMap) {
852 for (
const auto& convref : convrefs) {
860 std::map<reco::ConversionRef, unsigned int>& conversionMap) {
876 std::map<reco::ConversionRef, unsigned int>& conversionMap) {
891 std::map<reco::ConversionRef, unsigned int>& conversionMap) {
892 if (!conversionMap.count(convref)) {
900 std::map<reco::CaloClusterPtr, unsigned int>& caloClusterMap) {
901 if (!caloClusterMap.count(caloCluster)) {
902 caloClusters.push_back(*caloCluster);
903 caloClusterMap[caloCluster] = caloClusters.size() - 1;
909 std::map<reco::CaloClusterPtr, unsigned int>& ebeeClusterMap,
910 std::unordered_set<DetId>& rechitMap,
915 std::map<reco::CaloClusterPtr, unsigned int>& esClusterMap) {
916 for (
const auto& cluster : superCluster.
clusters()) {
919 for (
const auto& hitfrac : cluster->hitsAndFractions()) {
920 rechitMap.insert(hitfrac.first);
923 bool barrel = cluster->hitsAndFractions().front().first.subdetId() ==
EcalBarrel;
925 DetId seed = EcalClusterTools::getMaximum(*cluster, rhcol).first;
927 std::vector<DetId> dets5x5 =
929 for (
const auto& detid : dets5x5) {
930 rechitMap.insert(detid);
936 for (
const auto& hitfrac : cluster->hitsAndFractions()) {
937 rechitMap.insert(hitfrac.first);
944 std::unordered_set<DetId>& hcalDetIds) {
949 const std::map<reco::CaloClusterPtr, unsigned int>& ebeeClusterMap,
950 const std::map<reco::CaloClusterPtr, unsigned int>& esClusterMap,
954 const auto& seedmapped = ebeeClusterMap.find(superCluster.
seed());
955 if (seedmapped != ebeeClusterMap.end()) {
963 for (
const auto& cluster : superCluster.
clusters()) {
964 const auto& clustermapped = ebeeClusterMap.find(cluster);
965 if (clustermapped != ebeeClusterMap.end()) {
982 const auto& clustermapped = esClusterMap.find(cluster);
983 if (clustermapped != esClusterMap.end()) {
993 if (!esclusters.
empty()) {
998 template <
typename T>
1001 const std::map<reco::SuperClusterRef, unsigned int>& superClusterMap,
1003 const auto& scmapped = superClusterMap.find(
core.superCluster());
1004 if (scmapped != superClusterMap.end()) {
1007 core.setSuperCluster(scref);
1012 const std::map<reco::GsfTrackRef, unsigned int>& gsfTrackMap,
1014 const auto& gsftkmapped = gsfTrackMap.find(gsfElectronCore.
gsfTrack());
1015 if (gsftkmapped != gsfTrackMap.end()) {
1023 const std::map<reco::ConversionRef, unsigned int>& conversionMap,
1026 for (
const auto& convref : convrefs) {
1027 const auto& convmapped = conversionMap.find(convref);
1028 if (convmapped != conversionMap.end()) {
1033 outconvrefs.
clear();
1037 if (!outconvrefs.
empty()) {
1043 const std::map<reco::PhotonCoreRef, unsigned int>& photonCoreMap,
1045 const auto& coremapped = photonCoreMap.find(
photon.photonCore());
1046 if (coremapped != photonCoreMap.end()) {
1049 photon.setPhotonCore(coreref);
1055 const std::map<reco::GsfElectronCoreRef, unsigned int>& gsfElectronCoreMap,
1057 const auto& coremapped = gsfElectronCoreMap.find(gsfElectron.
core());
1058 if (coremapped != gsfElectronCoreMap.end()) {
1069 float newEnergy = energyMap[oldPhoRef];
1070 float newEnergyErr = energyErrMap[oldPhoRef];
1071 photon.setCorrectedEnergy(reco::Photon::P4type::regression2, newEnergy, newEnergyErr,
true);
1080 const float newEnergy = energyMap[oldEleRef];
1081 const float newEnergyErr = energyErrMap[oldEleRef];
1082 const float newEcalEnergy = ecalEnergyMap[oldEleRef];
1083 const float newEcalEnergyErr = ecalEnergyErrMap[oldEleRef];
1087 const float corr = newEnergy / oldP4.E();
1089 electron.setCorrectedEcalEnergy(newEcalEnergy);
1090 electron.setCorrectedEcalEnergyError(newEcalEnergyErr);
1093 electron.correctMomentum(newP4,
electron.trackMomentumError(), newEnergyErr);