Skip to content

Commit

Permalink
Refactoring the reference handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cerminar committed Dec 10, 2024
1 parent ef6ff0e commit 1a752d2
Showing 1 changed file with 125 additions and 77 deletions.
202 changes: 125 additions & 77 deletions L1Trigger/Phase2L1ParticleFlow/plugins/L1TCorrelatorLayer1Producer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ class L1TCorrelatorLayer1Producer : public edm::stream::EDProducer<> {
l1ct::DetectorSector<l1ct::EmCaloObjEmu> &sec);
// FIXME: could be templated? maybe not after adding specific variables for Gct and HGCal clusters
void getEmPFCluster(const l1ct::EmCaloObjEmu &decCalo,
const l1ct::DetectorSector<l1ct::EmCaloObjEmu> &sec,
l1t::PFClusterCollection &pfClusters) const;
const l1ct::PFRegionEmu &region,
std::unique_ptr<l1t::PFClusterCollection> &pfClusters) const;

void addDecodedHadCalo(l1ct::HadCaloObjEmu &decCalo,
const edm::Handle<edm::View<l1t::L1Candidate>> &caloHandle,
Expand All @@ -140,8 +140,8 @@ class L1TCorrelatorLayer1Producer : public edm::stream::EDProducer<> {

// FIXME: could be templated? maybe not after adding specific variables for Gct and HGCal clusters
void getHadPFCluster(const l1ct::HadCaloObjEmu &decCalo,
const l1ct::DetectorSector<l1ct::HadCaloObjEmu> &sec,
l1t::PFClusterCollection &pfClusters) const;
const l1ct::PFRegionEmu &region,
std::unique_ptr<l1t::PFClusterCollection> &pfClusters) const;

void addUInt(unsigned int value, std::string iLabel, edm::Event &iEvent);

Expand All @@ -162,7 +162,9 @@ class L1TCorrelatorLayer1Producer : public edm::stream::EDProducer<> {
std::unique_ptr<l1t::PFCandidateCollection> fetchEmCalo() const;
std::unique_ptr<l1t::PFCandidateCollection> fetchTracks() const;
std::unique_ptr<l1t::PFCandidateCollection> fetchPF() const;
std::unique_ptr<std::vector<l1t::PFTrack>> fetchDecodedTracks() const;
std::unique_ptr<l1t::PFClusterCollection> fetchDecodedHadCalo() const;
std::unique_ptr<l1t::PFClusterCollection> fetchDecodedEmCalo() const;
std::unique_ptr<l1t::PFTrackCollection> fetchDecodedTracks() const;
void putPuppi(edm::Event &iEvent) const;

void putEgStaObjects(edm::Event &iEvent, const std::string &egLablel) const;
Expand All @@ -175,6 +177,19 @@ class L1TCorrelatorLayer1Producer : public edm::stream::EDProducer<> {

template <typename T>
void setRefs_(l1t::PFCandidate &pf, const T &p) const;
template <typename T>
void setRefs_(l1t::PFCluster &pf, const T &p) const;
template<typename Tm, typename Tk, typename To>
auto findRef_(const Tm& map, const Tk* key, const To& obj) const {
auto match = map.find(key);
if (match == map.end()) {
throw cms::Exception("CorruptData") << refExcepMsg_(obj);
}
return match->second;
}
template<typename T>
std::string refExcepMsg_(const T &key) const;


void doVertexings(std::vector<float> &pvdz) const;
// for multiplicities
Expand Down Expand Up @@ -232,9 +247,9 @@ L1TCorrelatorLayer1Producer::L1TCorrelatorLayer1Producer(const edm::ParameterSet
#if 0 // LATER
produces<l1t::PFCandidateCollection>("TKVtx");
#endif
produces<std::vector<l1t::PFTrack>>("DecodedTK");
produces<l1t::PFClusterCollection>("DecodedEmPFClusters");
produces<l1t::PFClusterCollection>("DecodedHadPFClusters");
produces<l1t::PFTrackCollection>("DecodedTK");
produces<l1t::PFClusterCollection>("DecodedEmClusters");
produces<l1t::PFClusterCollection>("DecodedHadClusters");

if (hasTracks_) {
const std::string &tkInAlgo = iConfig.getParameter<std::string>("trackInputConversionAlgo");
Expand Down Expand Up @@ -565,15 +580,14 @@ void L1TCorrelatorLayer1Producer::produce(edm::Event &iEvent, const edm::EventSe
regionizer_->run(event_.decoded, event_.pfinputs);

// First, get a copy of the discretized and corrected inputs, and write them out
iEvent.put(fetchEmCalo(), "EmCalo"); // FIXME: still needed? now replaced by PFClusters
iEvent.put(fetchHadCalo(), "Calo"); // FIXME: still needed? now replaced by PFClusters
// FIXME: steer via config flag
iEvent.put(fetchEmCalo(), "EmCalo");
iEvent.put(fetchHadCalo(), "Calo");
iEvent.put(fetchTracks(), "TK");
iEvent.put(fetchDecodedTracks(), "DecodedTK");

// FIXME: add a flag: writing these out should be optional
// FIXME: add fetch method and solve duplications before writing (can use the index of the ptr to the consituents)
// iEvent.put(std::move(hadDecodedClusters), "DecodedHadPFClusters"); //FIXME: convert to std::unique_ptr<l1t::PFClusterContainer>
// iEvent.put(std::move(emDecodedClusters), "DecodedEmPFClusters"); //FIXME: convert to std::unique_ptr<l1t::PFClusterContainer>
iEvent.put(fetchDecodedHadCalo(), "DecodedHadClusters");
iEvent.put(fetchDecodedEmCalo(), "DecodedEmClusters");
iEvent.put(fetchDecodedTracks(), "DecodedTK");

// Then do the vertexing, and save it out
std::vector<float> z0s;
Expand Down Expand Up @@ -765,27 +779,21 @@ void L1TCorrelatorLayer1Producer::encodeAndAddHgcalCluster(ap_uint<256> &word,
}

void L1TCorrelatorLayer1Producer::getEmPFCluster(const l1ct::EmCaloObjEmu &decCalo,
const l1ct::DetectorSector<l1ct::EmCaloObjEmu> &sec,
l1t::PFClusterCollection &pfClusters) const {
const l1ct::PFRegionEmu &region,
std::unique_ptr<l1t::PFClusterCollection> &pfClusters) const {
// Crete the PFCluster and add the original object as Consitutent
l1t::PFCluster pfCl(decCalo.floatPt(),
sec.region.floatGlbEta(decCalo.hwEta),
sec.region.floatGlbPhi(decCalo.hwPhi),
region.floatGlbEta(decCalo.hwEta),
region.floatGlbPhi(decCalo.hwPhi),
decCalo.floatHoe(),
true); // FIXME: is this used?
// Add additional variables specialized for GCT and HGCal clusters
pfCl.setSigmaRR(decCalo.floatSrrTot());
pfCl.setAbsZBarycenter(decCalo.floatMeanZ());
// FIXME: get it from clusterRefMap_
// pfCl.addConstituent(edm::Ptr<l1t::L1Candidate>(caloHandle, ic));

pfClusters.push_back(pfCl);
setRefs_(pfCl, decCalo);
pfClusters->push_back(pfCl);
}

// FIXME: besides the specific "cluster shape variables" this function and the had one could be merged via templates
// we will see after the new implementation is complete: it all depends on the decision about PFClusters.
// do we need to specialize them for barrel& endcap or we can just add all variables to the same class?
// FIXME: remove after extracting the PFCluster creation
void L1TCorrelatorLayer1Producer::addDecodedEmCalo(l1ct::EmCaloObjEmu &decCalo,
const edm::Handle<l1tp2::CaloCrystalClusterCollection> &caloHandle,
unsigned int ic,
Expand All @@ -796,22 +804,23 @@ void L1TCorrelatorLayer1Producer::addDecodedEmCalo(l1ct::EmCaloObjEmu &decCalo,
sec.obj.push_back(decCalo);
}

// FIXME: besides the specific "cluster shape variables" this function and the EM one could be merged via templates
// we will see after the new implementation is complete: it all depends on the decision about PFClusters.
// do we need to specialize them for barrel& endcap or we can just add all variables to the same class?
void L1TCorrelatorLayer1Producer::getHadPFCluster(const l1ct::HadCaloObjEmu &decCalo,
const l1ct::DetectorSector<l1ct::HadCaloObjEmu> &sec,
l1t::PFClusterCollection &pfClusters) const {
const l1ct::PFRegionEmu &region,
std::unique_ptr<l1t::PFClusterCollection> &pfClusters) const {
// Crete the PFCluster and add the original object as Consitutent
l1t::PFCluster pfCl(decCalo.floatPt(),
sec.region.floatGlbEta(decCalo.hwEta),
sec.region.floatGlbPhi(decCalo.hwPhi),
region.floatGlbEta(decCalo.hwEta),
region.floatGlbPhi(decCalo.hwPhi),
decCalo.floatHoe(),
decCalo.hwIsEM());
// Add additional variables specialized for GCT and HGCal clusters
pfCl.setSigmaRR(decCalo.floatSrrTot());
pfCl.setAbsZBarycenter(decCalo.floatMeanZ());
// FIXME: get it from clusterRefMap_
// pfCl.addConstituent(edm::Ptr<l1t::L1Candidate>(caloHandle, ic));

pfClusters.push_back(pfCl);
setRefs_(pfCl, decCalo);
pfClusters->push_back(pfCl);
}

void L1TCorrelatorLayer1Producer::addDecodedHadCalo(l1ct::HadCaloObjEmu &decCalo,
Expand Down Expand Up @@ -1051,56 +1060,80 @@ void L1TCorrelatorLayer1Producer::setRefs_(l1t::PFCandidate &pf, const T &p) con
}
}

template<>
std::string L1TCorrelatorLayer1Producer::refExcepMsg_<l1ct::PFNeutralObjEmu>(const l1ct::PFNeutralObjEmu &key) const {
return "Invalid pointer in Neutral PF candidate id " + std::to_string(key.intId()) + " pt " + std::to_string(key.floatPt()) +
" eta " + std::to_string(key.floatEta()) + " phi " + std::to_string(key.floatPhi());
}

template<>
std::string L1TCorrelatorLayer1Producer::refExcepMsg_<l1ct::HadCaloObjEmu>(const l1ct::HadCaloObjEmu &key) const {
return "Invalid pointer in hadcalo obj, pt " + std::to_string(key.floatPt()) +
" eta " + std::to_string(key.floatEta()) + " phi " + std::to_string(key.floatPhi());
}

template<>
std::string L1TCorrelatorLayer1Producer::refExcepMsg_<l1ct::EmCaloObjEmu>(const l1ct::EmCaloObjEmu &key) const {
return "Invalid pointer in emcalo obj, pt " + std::to_string(key.floatPt()) +
" eta " + std::to_string(key.floatEta()) + " phi " + std::to_string(key.floatPhi());
}

template<>
std::string L1TCorrelatorLayer1Producer::refExcepMsg_<l1ct::TkObjEmu>(const l1ct::TkObjEmu &key) const {
return "Invalid track pointer in track obj, pt " + std::to_string(key.floatPt()) +
" eta " + std::to_string(key.floatEta()) + " phi " + std::to_string(key.floatPhi());
}

template<>
std::string L1TCorrelatorLayer1Producer::refExcepMsg_<l1ct::EGIsoObjEmu>(const l1ct::EGIsoObjEmu &key) const {
return "Invalid cluster pointer in EGIso candidate, pt " + std::to_string(key.floatPt()) +
" eta " + std::to_string(key.floatEta()) + " phi " + std::to_string(key.floatPhi());
}

template<>
std::string L1TCorrelatorLayer1Producer::refExcepMsg_<l1ct::EGIsoEleObjEmu>(const l1ct::EGIsoEleObjEmu &key) const {
return "Invalid cluster pointer in EGEleIso candidate, pt " + std::to_string(key.floatPt()) +
" eta " + std::to_string(key.floatEta()) + " phi " + std::to_string(key.floatPhi());
}




template <>
void L1TCorrelatorLayer1Producer::setRefs_<l1ct::PFNeutralObjEmu>(l1t::PFCandidate &pf,
const l1ct::PFNeutralObjEmu &p) const {
if (p.srcCluster) {
auto match = clusterRefMap_.find(p.srcCluster);
if (match == clusterRefMap_.end()) {
throw cms::Exception("CorruptData") << "Invalid cluster pointer in PF candidate id " << p.intId() << " pt "
<< p.floatPt() << " eta " << p.floatEta() << " phi " << p.floatPhi();
}
pf.setCaloPtr(match->second);
pf.setCaloPtr(findRef_(clusterRefMap_, p.srcCluster, p));
}
}

// FIXME: this method is not needed anymore
template <>
void L1TCorrelatorLayer1Producer::setRefs_<l1ct::HadCaloObjEmu>(l1t::PFCandidate &pf,
const l1ct::HadCaloObjEmu &p) const {
if (p.src) {
auto match = clusterRefMap_.find(p.src);
if (match == clusterRefMap_.end()) {
throw cms::Exception("CorruptData") << "Invalid cluster pointer in hadcalo candidate pt " << p.floatPt()
<< " eta " << p.floatEta() << " phi " << p.floatPhi();
}
pf.setCaloPtr(match->second);
pf.setCaloPtr(findRef_(clusterRefMap_, p.src, p));
}
}

// FIXME: this method is not needed anymore
template <>
void L1TCorrelatorLayer1Producer::setRefs_<l1ct::EmCaloObjEmu>(l1t::PFCandidate &pf,
const l1ct::EmCaloObjEmu &p) const {
if (p.src) {
auto match = clusterRefMap_.find(p.src);
if (match == clusterRefMap_.end()) {
throw cms::Exception("CorruptData") << "Invalid cluster pointer in emcalo candidate pt " << p.floatPt()
<< " eta " << p.floatEta() << " phi " << p.floatPhi();
}
pf.setCaloPtr(match->second);
pf.setCaloPtr(findRef_(clusterRefMap_, p.src, p));
}
}

template <>
void L1TCorrelatorLayer1Producer::setRefs_<l1ct::TkObjEmu>(l1t::PFCandidate &pf, const l1ct::TkObjEmu &p) const {
if (p.src) {
auto match = trackRefMap_.find(p.src);
if (match == trackRefMap_.end()) {
throw cms::Exception("CorruptData") << "Invalid track pointer in track candidate pt " << p.floatPt() << " eta "
<< p.floatEta() << " phi " << p.floatPhi();
}
pf.setPFTrack(match->second);
pf.setPFTrack(findRef_(trackRefMap_, p.src, p));
}
}

template <typename T>
void L1TCorrelatorLayer1Producer::setRefs_(l1t::PFCluster &pf, const T &p) const {
if (p.src) {
pf.addConstituent(findRef_(clusterRefMap_, p.src, p));
}
}

Expand All @@ -1120,6 +1153,33 @@ std::unique_ptr<l1t::PFCandidateCollection> L1TCorrelatorLayer1Producer::fetchHa
}
return ret;
}

std::unique_ptr<l1t::PFClusterCollection> L1TCorrelatorLayer1Producer::fetchDecodedHadCalo() const {
auto ret = std::make_unique<l1t::PFClusterCollection>();
for (const auto &r : event_.pfinputs) {
const auto &reg = r.region;
for (const auto &p : r.hadcalo) {
if (p.hwPt == 0 || !reg.isFiducial(p))
continue;
getHadPFCluster(p, reg, ret);
}
}
return ret;
}

std::unique_ptr<l1t::PFClusterCollection> L1TCorrelatorLayer1Producer::fetchDecodedEmCalo() const {
auto ret = std::make_unique<l1t::PFClusterCollection>();
for (const auto &r : event_.pfinputs) {
const auto &reg = r.region;
for (const auto &p : r.emcalo) {
if (p.hwPt == 0 || !reg.isFiducial(p))
continue;
getEmPFCluster(p, reg, ret);
}
}
return ret;
}

std::unique_ptr<l1t::PFCandidateCollection> L1TCorrelatorLayer1Producer::fetchEmCalo() const {
auto ret = std::make_unique<l1t::PFCandidateCollection>();
for (const auto &r : event_.pfinputs) {
Expand Down Expand Up @@ -1151,8 +1211,8 @@ std::unique_ptr<l1t::PFCandidateCollection> L1TCorrelatorLayer1Producer::fetchTr
return ret;
}

std::unique_ptr<std::vector<l1t::PFTrack>> L1TCorrelatorLayer1Producer::fetchDecodedTracks() const {
auto ret = std::make_unique<std::vector<l1t::PFTrack>>();
std::unique_ptr<l1t::PFTrackCollection> L1TCorrelatorLayer1Producer::fetchDecodedTracks() const {
auto ret = std::make_unique<l1t::PFTrackCollection>();
for (const auto &r : event_.decoded.track) {
const auto &reg = r.region;
for (const auto &p : r.obj) {
Expand Down Expand Up @@ -1319,14 +1379,8 @@ void L1TCorrelatorLayer1Producer::putEgObjects(edm::Event &iEvent,

reco::Candidate::PolarLorentzVector mom(egiso.floatPt(), egiso.floatEta(), egiso.floatPhi(), 0.);

auto match = clusterRefMap_.find(egiso.srcCluster);
if (match == clusterRefMap_.end()) {
throw cms::Exception("CorruptData") << "Invalid cluster pointer in EGIso candidate pt " << egiso.floatPt()
<< " eta " << egiso.floatEta() << " phi " << egiso.floatPhi();
}

l1t::TkEm tkem(reco::Candidate::LorentzVector(mom),
match->second,
findRef_(clusterRefMap_, egiso.srcCluster, egiso),
egiso.floatRelIso(l1ct::EGIsoObjEmu::IsoType::TkIso),
egiso.floatRelIso(l1ct::EGIsoObjEmu::IsoType::TkIsoPV));
tkem.setHwQual(egiso.hwQual);
Expand All @@ -1345,14 +1399,8 @@ void L1TCorrelatorLayer1Producer::putEgObjects(edm::Event &iEvent,

reco::Candidate::PolarLorentzVector mom(egele.floatPt(), egele.floatVtxEta(), egele.floatVtxPhi(), 0.);

auto match = clusterRefMap_.find(egele.srcCluster);
if (match == clusterRefMap_.end()) {
throw cms::Exception("CorruptData") << "Invalid cluster pointer in EGEleIso candidate pt " << egele.floatPt()
<< " eta " << egele.floatEta() << " phi " << egele.floatPhi();
}

l1t::TkElectron tkele(reco::Candidate::LorentzVector(mom),
match->second,
findRef_(clusterRefMap_, egele.srcCluster, egele),
edm::refToPtr(egele.srcTrack->track()),
egele.floatRelIso(l1ct::EGIsoEleObjEmu::IsoType::TkIso));
tkele.setHwQual(egele.hwQual);
Expand Down

0 comments on commit 1a752d2

Please sign in to comment.