44 #include <unordered_set>
56 std::vector<edm::EDPutTokenT<T>>
vproduces(std::vector<std::string>
const&
labels) {
57 std::vector<edm::EDPutTokenT<T>> putTokens{};
58 putTokens.reserve(
labels.size());
60 putTokens.push_back(produces<T>(
label));
65 template <
typename T,
typename U>
69 std::map<reco::SuperClusterRef, unsigned int>& superClusterMap,
72 std::unordered_set<unsigned int>& superClusterFullRelinkMap);
76 std::map<reco::ConversionRef, unsigned int>& conversionMap);
81 std::map<reco::ConversionRef, unsigned int>& conversionMap);
86 std::map<reco::ConversionRef, unsigned int>& conversionMap);
90 std::map<reco::ConversionRef, unsigned int>& conversionMap);
94 std::map<reco::CaloClusterPtr, unsigned int>& caloClusterMap);
98 std::map<reco::CaloClusterPtr, unsigned int>& ebeeClusterMap,
99 std::unordered_set<DetId>& rechitMap,
104 std::map<reco::CaloClusterPtr, unsigned int>& esClusterMap);
108 std::unordered_set<DetId>& hcalDetIds);
111 const std::map<reco::CaloClusterPtr, unsigned int>& ebeeClusterMap,
112 const std::map<reco::CaloClusterPtr, unsigned int>& esClusterMap,
116 template <
typename T>
118 const std::map<reco::SuperClusterRef, unsigned int>& superClusterMap,
122 const std::map<reco::GsfTrackRef, unsigned int>& gsfTrackMap,
127 const std::map<reco::ConversionRef, unsigned int>& conversionMap,
131 const std::map<reco::PhotonCoreRef, unsigned int>& photonCoreMap,
135 const std::map<reco::GsfElectronCoreRef, unsigned int>& gsfElectronCoreMap,
150 template <
typename T>
241 std::vector<edm::Handle<T>>
handles(tokens.size());
243 for (
const auto&
token : tokens) {
249 template <
class Handle,
class T>
251 std::vector<T>
const&
values,
257 typename MapType::Filler
filler(oMap);
269 singleConversionT_(consumes(
config.getParameter<
edm::
InputTag>(
"singleConversions"))),
273 preshowerEcalHits_(doPreshowerEcalHits_
277 photonPfCandMapT_(consumes(
config.getParameter<
edm::
InputTag>(
"photonsPFValMap"))),
278 gsfElectronPfCandMapT_(consumes(
config.getParameter<
edm::
InputTag>(
"gsfElectronsPFValMap"))),
279 recoHIPhotonIsolationMapInputToken_{
281 ? consumes<reco::HIPhotonIsolationMap>(
config.getParameter<
edm::InputTag>(
"hiPhotonIsolationMapInput"))
284 applyPhotonCalibOnData_(
config.getParameter<
bool>(
"applyPhotonCalibOnData")),
285 applyPhotonCalibOnMC_(
config.getParameter<
bool>(
"applyPhotonCalibOnMC")),
286 applyGsfElectronCalibOnData_(
config.getParameter<
bool>(
"applyGsfElectronCalibOnData")),
287 applyGsfElectronCalibOnMC_(
config.getParameter<
bool>(
"applyGsfElectronCalibOnMC")),
289 outPhotons_{produces<reco::PhotonCollection>(
"reducedGedPhotons")},
290 outPhotonCores_{produces<reco::PhotonCoreCollection>(
"reducedGedPhotonCores")},
291 outGsfElectrons_{produces<reco::GsfElectronCollection>(
"reducedGedGsfElectrons")},
292 outGsfElectronCores_{produces<reco::GsfElectronCoreCollection>(
"reducedGedGsfElectronCores")},
293 outGsfTracks_{produces<reco::GsfTrackCollection>(
"reducedGsfTracks")},
294 outConversions_{produces<reco::ConversionCollection>(
"reducedConversions")},
295 outSingleConversions_{produces<reco::ConversionCollection>(
"reducedSingleLegConversions")},
296 outSuperClusters_{produces<reco::SuperClusterCollection>(
"reducedSuperClusters")},
297 outEBEEClusters_{produces<reco::CaloClusterCollection>(
"reducedEBEEClusters")},
298 outESClusters_{produces<reco::CaloClusterCollection>(
"reducedESClusters")},
299 outEBRecHits_{produces<EcalRecHitCollection>(
"reducedEBRecHits")},
300 outEERecHits_{produces<EcalRecHitCollection>(
"reducedEERecHits")},
301 outHBHERecHits_{produces<HBHERecHitCollection>(
"reducedHBHEHits")},
302 outPhotonPfCandMap_{produces<edm::ValueMap<std::vector<reco::PFCandidateRef>>>(
"reducedPhotonPfCandMap")},
303 outGsfElectronPfCandMap_{
304 produces<edm::ValueMap<std::vector<reco::PFCandidateRef>>>(
"reducedGsfElectronPfCandMap")},
305 outPhotonIds_{vproduces<edm::ValueMap<bool>>(
config.getParameter<std::vector<std::string>>(
"photonIDOutput"))},
307 vproduces<edm::ValueMap<float>>(
config.getParameter<std::vector<std::string>>(
"gsfElectronIDOutput"))},
308 outPhotonFloatValueMaps_{
309 vproduces<edm::ValueMap<float>>(
config.getParameter<std::vector<std::string>>(
"photonFloatValueMapOutput"))},
310 outGsfElectronFloatValueMaps_{vproduces<edm::ValueMap<float>>(
311 config.getParameter<std::vector<std::string>>(
"gsfElectronFloatValueMapOutput"))},
316 slimRelinkOOTPhotonSel_(
config.getParameter<
std::string>(
"slimRelinkOOTPhotons")),
319 slimRelinkGsfElectronSel_(
config.getParameter<
std::string>(
"slimRelinkGsfElectrons")),
324 if (not aTag.label().empty())
325 ootPhotonT_ = consumes<reco::PhotonCollection>(aTag);
331 for (
const edm::InputTag&
tag :
config.getParameter<std::vector<edm::InputTag>>(
"gsfElectronIDSources")) {
335 for (
const edm::InputTag&
tag :
config.getParameter<std::vector<edm::InputTag>>(
"photonFloatValueMapSources")) {
339 for (
const edm::InputTag&
tag :
config.getParameter<std::vector<edm::InputTag>>(
"ootPhotonFloatValueMapSources")) {
343 for (
const edm::InputTag&
tag :
config.getParameter<std::vector<edm::InputTag>>(
"gsfElectronFloatValueMapSources")) {
347 if (applyPhotonCalibOnData_ || applyPhotonCalibOnMC_) {
348 setToken(photonCalibEnergyT_,
config,
"photonCalibEnergySource");
349 setToken(photonCalibEnergyErrT_,
config,
"photonCalibEnergyErrSource");
351 if (applyGsfElectronCalibOnData_ || applyGsfElectronCalibOnMC_) {
352 setToken(gsfElectronCalibEnergyT_,
config,
"gsfElectronCalibEnergySource");
353 setToken(gsfElectronCalibEnergyErrT_,
config,
"gsfElectronCalibEnergyErrSource");
354 setToken(gsfElectronCalibEcalEnergyT_,
config,
"gsfElectronCalibEcalEnergySource");
355 setToken(gsfElectronCalibEcalEnergyErrT_,
config,
"gsfElectronCalibEcalEnergyErrSource");
358 if (!ootPhotonT_.isUninitialized()) {
359 outOOTPhotons_ = produces<reco::PhotonCollection>(
"reducedOOTPhotons");
360 outOOTPhotonCores_ = produces<reco::PhotonCoreCollection>(
"reducedOOTPhotonCores");
361 outOOTSuperClusters_ = produces<reco::SuperClusterCollection>(
"reducedOOTSuperClusters");
362 outOOTEBEEClusters_ = produces<reco::CaloClusterCollection>(
"reducedOOTEBEEClusters");
363 outOOTESClusters_ = produces<reco::CaloClusterCollection>(
"reducedOOTESClusters");
365 if (doPreshowerEcalHits_) {
366 outESRecHits_ = produces<EcalRecHitCollection>(
"reducedESRecHits");
368 if (!ootPhotonT_.isUninitialized()) {
369 outOOTPhotonFloatValueMaps_ =
370 vproduces<edm::ValueMap<float>>(
config.getParameter<std::vector<std::string>>(
"ootPhotonFloatValueMapOutput"));
372 if (!recoHIPhotonIsolationMapInputToken_.isUninitialized()) {
373 recoHIPhotonIsolationMapOutputName_ =
374 produces<reco::HIPhotonIsolationMap>(
config.getParameter<
std::string>(
"hiPhotonIsolationMapOutput"));
383 auto photonHandle =
event.getHandle(
photonT_);
385 auto ootPhotonHandle =
394 auto preshowerHitHandle =
397 auto hbheHitHandle =
event.getHandle(
hbheHits_);
407 : std::vector<edm::Handle<edm::ValueMap<float>>>{};
454 std::map<reco::PhotonCoreRef, unsigned int> photonCoreMap;
455 std::map<reco::PhotonCoreRef, unsigned int> ootPhotonCoreMap;
456 std::map<reco::GsfElectronCoreRef, unsigned int> gsfElectronCoreMap;
457 std::map<reco::GsfTrackRef, unsigned int> gsfTrackMap;
458 std::map<reco::ConversionRef, unsigned int> conversionMap;
459 std::map<reco::ConversionRef, unsigned int> singleConversionMap;
460 std::map<reco::SuperClusterRef, unsigned int> superClusterMap;
461 std::map<reco::CaloClusterPtr, unsigned int> ebeeClusterMap;
462 std::map<reco::CaloClusterPtr, unsigned int> esClusterMap;
463 std::map<reco::SuperClusterRef, unsigned int> ootSuperClusterMap;
464 std::map<reco::CaloClusterPtr, unsigned int> ootEbeeClusterMap;
465 std::map<reco::CaloClusterPtr, unsigned int> ootEsClusterMap;
466 std::unordered_set<DetId> rechitMap;
467 std::unordered_set<DetId> hcalRechitMap;
469 std::unordered_set<unsigned int> superClusterFullRelinkMap;
470 std::unordered_set<unsigned int> ootSuperClusterFullRelinkMap;
473 std::vector<std::vector<reco::PFCandidateRef>> pfCandIsoPairVecPho;
474 std::vector<std::vector<reco::PFCandidateRef>> pfCandIsoPairVecEle;
477 std::vector<std::vector<bool>> photonIdVals(photonIdHandles.size());
478 std::vector<std::vector<float>> gsfElectronIdVals(gsfElectronIdHandles.size());
479 std::vector<std::vector<float>> photonFloatValueMapVals(photonFloatValueMapHandles.size());
480 std::vector<std::vector<float>> ootPhotonFloatValueMapVals(ootPhotonFloatValueMapHandles.size());
481 std::vector<std::vector<float>> gsfElectronFloatValueMapVals(gsfElectronFloatValueMapHandles.size());
487 std::vector<reco::HIPhotonIsolation> recoHIPhotonIsolationMapInputVals;
491 for (
const auto&
photon : *photonHandle) {
496 auto& newPhoton =
photons.back();
499 calibratePhoton(newPhoton, photonref, *photonCalibEnergyHandle, *photonCalibEnergyErrHandle);
510 pfCandIsoPairVecPho.push_back((*photonPfCandMapHandle)[photonref]);
514 for (
const auto& photonIdHandle : photonIdHandles) {
515 photonIdVals[subindex++].push_back((*photonIdHandle)[photonref]);
519 for (
const auto& photonFloatValueMapHandle : photonFloatValueMapHandles) {
520 photonFloatValueMapVals[subindex++].push_back((*photonFloatValueMapHandle)[photonref]);
525 recoHIPhotonIsolationMapInputVals.push_back((*recoHIPhotonIsolationMapInputValueMap)[photonref]);
564 for (
const auto& ootPhoton : *ootPhotonHandle) {
577 for (
const auto& ootPhotonFloatValueMapHandle : ootPhotonFloatValueMapHandles) {
578 ootPhotonFloatValueMapVals[subindex++].push_back((*ootPhotonFloatValueMapHandle)[ootPhotonref]);
593 linkSuperCluster(ootSuperCluster, ootSuperClusterMap, ootSuperClusters, relink, ootSuperClusterFullRelinkMap);
595 linkHcalHits(*ootPhoton.superCluster(), *hbheHitHandle, hcalRechitMap);
601 for (
const auto& gsfElectron : *gsfElectronHandle) {
610 *gsfElectronCalibEnergyHandle,
611 *gsfElectronCalibEnergyErrHandle,
612 *gsfElectronCalibEcalEnergyHandle,
613 *gsfElectronCalibEcalEnergyErrHandle);
622 pfCandIsoPairVecEle.push_back((*gsfElectronPfCandMapHandle)[gsfElectronref]);
626 for (
const auto& gsfElectronIdHandle : gsfElectronIdHandles) {
627 gsfElectronIdVals[subindex++].push_back((*gsfElectronIdHandle)[gsfElectronref]);
631 for (
const auto& gsfElectronFloatValueMapHandle : gsfElectronFloatValueMapHandles) {
632 gsfElectronFloatValueMapVals[subindex++].push_back((*gsfElectronFloatValueMapHandle)[gsfElectronref]);
636 linkCore(gsfElectronCore, gsfElectronCores, gsfElectronCoreMap);
641 if (!gsfTrackMap.count(gsfTrack)) {
643 gsfTrackMap[gsfTrack] =
gsfTracks.size() - 1;
647 for (
auto const& ambigGsfTrack : gsfElectron.ambiguousGsfTracks()) {
648 if (!gsfTrackMap.count(ambigGsfTrack)) {
650 gsfTrackMap[ambigGsfTrack] =
gsfTracks.size() - 1;
682 linkHcalHits(*gsfElectron.superCluster(), *hbheHitHandle, hcalRechitMap);
693 bool fullrelink = superClusterFullRelinkMap.count(
index++);
696 superCluster.clearHitsAndFractions();
721 for (
auto& ootSuperCluster : ootSuperClusters) {
727 bool fullrelink = ootSuperClusterFullRelinkMap.count(
index++);
730 ootSuperCluster.clearHitsAndFractions();
749 for (
const EcalRecHit& rechit : *barrelHitHandle) {
750 if (rechitMap.count(rechit.detid())) {
755 for (
const EcalRecHit& rechit : *endcapHitHandle) {
756 if (rechitMap.count(rechit.detid())) {
765 for (
const EcalRecHit& rechit : *preshowerHitHandle) {
766 if (rechitMap.count(rechit.detid())) {
773 for (
const HBHERecHit& rechit : *hbheHitHandle) {
774 if (hcalRechitMap.count(rechit.detid())) {
788 relinkCaloClusters(superCluster, ebeeClusterMap, esClusterMap, outEBEEClusterHandle, outESClusterHandle);
801 ootSuperCluster, ootEbeeClusterMap, ootEsClusterMap, outOOTEBEEClusterHandle, outOOTESClusterHandle);
863 std::vector<reco::GsfTrackRef> ambigTracksInThisElectron;
865 for (
auto const& igsf : gsfElectron.ambiguousGsfTracks()) {
866 ambigTracksInThisElectron.push_back(igsf);
870 gsfElectron.clearAmbiguousGsfTracks();
873 for (
const auto& it : ambigTracksInThisElectron) {
874 const auto& gsftkmapped = gsfTrackMap.find(it);
876 if (gsftkmapped != gsfTrackMap.end()) {
878 gsfElectron.addAmbiguousGsfTrack(gsftkref);
880 throw cms::Exception(
"There must be a problem with linking and mapping of ambiguous gsf tracks...");
883 if (gsfElectron.ambiguousGsfTracksSize() > 0)
884 gsfElectron.setAmbiguous(
true);
886 ambigTracksInThisElectron.clear();
900 fillerPhotons.insert(outPhotonHandle, pfCandIsoPairVecPho.begin(), pfCandIsoPairVecPho.end());
901 fillerPhotons.fill();
905 fillerGsfElectrons.insert(outGsfElectronHandle, pfCandIsoPairVecEle.begin(), pfCandIsoPairVecEle.end());
906 fillerGsfElectrons.fill();
913 for (
auto const& vals : photonIdVals) {
919 for (
auto const& vals : gsfElectronIdVals) {
925 for (
auto const& vals : photonFloatValueMapVals) {
932 for (
auto const& vals : ootPhotonFloatValueMapVals) {
944 for (
auto const& vals : gsfElectronFloatValueMapVals) {
949 template <
typename T,
typename U>
951 if (!coreMap.count(
core)) {
958 std::map<reco::SuperClusterRef, unsigned int>& superClusterMap,
961 std::unordered_set<unsigned int>& superClusterFullRelinkMap) {
962 const auto& mappedsc = superClusterMap.find(superCluster);
965 unsigned int mappedscidx = 0;
966 if (mappedsc == superClusterMap.end()) {
969 superClusterMap[superCluster] = mappedscidx;
971 mappedscidx = mappedsc->second;
976 superClusterFullRelinkMap.insert(mappedscidx);
981 std::map<reco::ConversionRef, unsigned int>& conversionMap) {
982 for (
const auto& convref : convrefs) {
990 std::map<reco::ConversionRef, unsigned int>& conversionMap) {
1006 std::map<reco::ConversionRef, unsigned int>& conversionMap) {
1021 std::map<reco::ConversionRef, unsigned int>& conversionMap) {
1022 if (!conversionMap.count(convref)) {
1030 std::map<reco::CaloClusterPtr, unsigned int>& caloClusterMap) {
1031 if (!caloClusterMap.count(caloCluster)) {
1032 caloClusters.push_back(*caloCluster);
1033 caloClusterMap[caloCluster] = caloClusters.size() - 1;
1039 std::map<reco::CaloClusterPtr, unsigned int>& ebeeClusterMap,
1040 std::unordered_set<DetId>& rechitMap,
1045 std::map<reco::CaloClusterPtr, unsigned int>& esClusterMap) {
1046 for (
const auto& cluster : superCluster.
clusters()) {
1049 for (
const auto& hitfrac : cluster->hitsAndFractions()) {
1050 rechitMap.insert(hitfrac.first);
1053 bool barrel = cluster->hitsAndFractions().front().first.subdetId() ==
EcalBarrel;
1055 DetId seed = EcalClusterTools::getMaximum(*cluster, rhcol).first;
1057 std::vector<DetId> dets5x5 =
1059 for (
const auto& detid : dets5x5) {
1060 rechitMap.insert(detid);
1066 for (
const auto& hitfrac : cluster->hitsAndFractions()) {
1067 rechitMap.insert(hitfrac.first);
1074 std::unordered_set<DetId>& hcalDetIds) {
1079 const std::map<reco::CaloClusterPtr, unsigned int>& ebeeClusterMap,
1080 const std::map<reco::CaloClusterPtr, unsigned int>& esClusterMap,
1084 const auto& seedmapped = ebeeClusterMap.find(superCluster.
seed());
1085 if (seedmapped != ebeeClusterMap.end()) {
1088 superCluster.
setSeed(clusptr);
1093 for (
const auto& cluster : superCluster.
clusters()) {
1094 const auto& clustermapped = ebeeClusterMap.find(cluster);
1095 if (clustermapped != ebeeClusterMap.end()) {
1112 const auto& clustermapped = esClusterMap.find(cluster);
1113 if (clustermapped != esClusterMap.end()) {
1123 if (!esclusters.
empty()) {
1128 template <
typename T>
1131 const std::map<reco::SuperClusterRef, unsigned int>& superClusterMap,
1133 const auto& scmapped = superClusterMap.find(
core.superCluster());
1134 if (scmapped != superClusterMap.end()) {
1137 core.setSuperCluster(scref);
1142 const std::map<reco::GsfTrackRef, unsigned int>& gsfTrackMap,
1144 const auto& gsftkmapped = gsfTrackMap.find(gsfElectronCore.
gsfTrack());
1145 if (gsftkmapped != gsfTrackMap.end()) {
1153 const std::map<reco::ConversionRef, unsigned int>& conversionMap,
1156 for (
const auto& convref : convrefs) {
1157 const auto& convmapped = conversionMap.find(convref);
1158 if (convmapped != conversionMap.end()) {
1163 outconvrefs.
clear();
1167 if (!outconvrefs.
empty()) {
1173 const std::map<reco::PhotonCoreRef, unsigned int>& photonCoreMap,
1175 const auto& coremapped = photonCoreMap.find(
photon.photonCore());
1176 if (coremapped != photonCoreMap.end()) {
1179 photon.setPhotonCore(coreref);
1185 const std::map<reco::GsfElectronCoreRef, unsigned int>& gsfElectronCoreMap,
1187 const auto& coremapped = gsfElectronCoreMap.find(gsfElectron.
core());
1188 if (coremapped != gsfElectronCoreMap.end()) {
1199 float newEnergy = energyMap[oldPhoRef];
1200 float newEnergyErr = energyErrMap[oldPhoRef];
1201 photon.setCorrectedEnergy(reco::Photon::P4type::regression2, newEnergy, newEnergyErr,
true);
1210 const float newEnergy = energyMap[oldEleRef];
1211 const float newEnergyErr = energyErrMap[oldEleRef];
1212 const float newEcalEnergy = ecalEnergyMap[oldEleRef];
1213 const float newEcalEnergyErr = ecalEnergyErrMap[oldEleRef];
1217 const float corr = newEnergy / oldP4.E();
1219 electron.setCorrectedEcalEnergy(newEcalEnergy);
1220 electron.setCorrectedEcalEnergyError(newEcalEnergyErr);
1223 electron.correctMomentum(newP4,
electron.trackMomentumError(), newEnergyErr);