Skip to content

Commit 6e2aeb3

Browse files
authored
Merge pull request #46567 from aloeliger/Fix_UCTSummary_Leak_14_1
[14_1] Fix memory leak issues in Calo Layer 1 UCT Infrastructure
2 parents d42f07c + 9b9f206 commit 6e2aeb3

File tree

10 files changed

+94
-89
lines changed

10 files changed

+94
-89
lines changed

L1Trigger/L1TCaloLayer1/plugins/L1TCaloLayer1.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,16 @@ class L1TCaloLayer1 : public edm::stream::EDProducer<> {
7474
edm::EDPutTokenT<L1CaloRegionCollection> regionPutToken;
7575
const L1TCaloLayer1FetchLUTsTokens lutsTokens;
7676

77-
std::vector<std::array<std::array<std::array<uint32_t, nEtBins>, nCalSideBins>, nCalEtaBins> > ecalLUT;
78-
std::vector<std::array<std::array<std::array<uint32_t, nEtBins>, nCalSideBins>, nCalEtaBins> > hcalLUT;
79-
std::vector<std::array<std::array<uint32_t, nEtBins>, nHfEtaBins> > hfLUT;
77+
std::vector<std::array<std::array<std::array<uint32_t, nEtBins>, nCalSideBins>, nCalEtaBins>> ecalLUT;
78+
std::vector<std::array<std::array<std::array<uint32_t, nEtBins>, nCalSideBins>, nCalEtaBins>> hcalLUT;
79+
std::vector<std::array<std::array<uint32_t, nEtBins>, nHfEtaBins>> hfLUT;
8080
std::vector<unsigned long long int> hcalFBLUT;
8181

8282
std::vector<unsigned int> ePhiMap;
8383
std::vector<unsigned int> hPhiMap;
8484
std::vector<unsigned int> hfPhiMap;
8585

86-
std::vector<UCTTower*> twrList;
86+
std::vector<std::shared_ptr<UCTTower>> twrList;
8787

8888
bool useLSB;
8989
bool useCalib;
@@ -140,7 +140,7 @@ L1TCaloLayer1::L1TCaloLayer1(const edm::ParameterSet& iConfig)
140140
for (uint32_t crd = 0; crd < cards.size(); crd++) {
141141
vector<UCTRegion*> regions = cards[crd]->getRegions();
142142
for (uint32_t rgn = 0; rgn < regions.size(); rgn++) {
143-
vector<UCTTower*> towers = regions[rgn]->getTowers();
143+
vector<std::shared_ptr<UCTTower>> towers = regions[rgn]->getTowers();
144144
for (uint32_t twr = 0; twr < towers.size(); twr++) {
145145
twrList.push_back(towers[twr]);
146146
}
@@ -150,7 +150,7 @@ L1TCaloLayer1::L1TCaloLayer1(const edm::ParameterSet& iConfig)
150150

151151
// This sort corresponds to the sort condition on
152152
// the output CaloTowerBxCollection
153-
std::sort(twrList.begin(), twrList.end(), [](UCTTower* a, UCTTower* b) {
153+
std::sort(twrList.begin(), twrList.end(), [](std::shared_ptr<UCTTower> a, std::shared_ptr<UCTTower> b) {
154154
return CaloTools::caloTowerHash(a->caloEta(), a->caloPhi()) < CaloTools::caloTowerHash(b->caloEta(), b->caloPhi());
155155
});
156156
}

L1Trigger/L1TCaloLayer1/plugins/L1TCaloSummary.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ void L1TCaloSummary<INPUT, OUTPUT>::produce(edm::Event& iEvent, const edm::Event
195195
// of size 7*2. Indices are mapped in UCTSummaryCard accordingly.
196196
UCTSummaryCard summaryCard =
197197
UCTSummaryCard(&pumLUT, jetSeed, tauSeed, tauIsolationFactor, eGammaSeed, eGammaIsolationFactor);
198-
std::vector<UCTRegion*> inputRegions;
199-
inputRegions.clear();
198+
std::vector<UCTRegion> inputRegions;
199+
inputRegions.reserve(252); // 252 calorimeter regions. 18 phi * 14 eta
200200
edm::Handle<std::vector<L1CaloRegion>> regionCollection;
201201
if (!iEvent.getByToken(regionToken, regionCollection))
202202
edm::LogError("L1TCaloSummary") << "UCT: Failed to get regions from region collection!";
@@ -222,8 +222,8 @@ void L1TCaloSummary<INPUT, OUTPUT>::produce(edm::Event& iEvent, const edm::Event
222222
uint32_t crate = g.getCrate(t.first, t.second);
223223
uint32_t card = g.getCard(t.first, t.second);
224224
uint32_t region = g.getRegion(absCaloEta, absCaloPhi);
225-
UCTRegion* test = new UCTRegion(crate, card, negativeEta, region, fwVersion);
226-
test->setRegionSummary(i.raw());
225+
UCTRegion test = UCTRegion(crate, card, negativeEta, region, fwVersion);
226+
test.setRegionSummary(i.raw());
227227
inputRegions.push_back(test);
228228
//This *should* fill the tensor in the proper order to be fed to the anomaly model
229229
//We take 4 off of the GCT eta/iEta.
@@ -282,9 +282,10 @@ void L1TCaloSummary<INPUT, OUTPUT>::produce(edm::Event& iEvent, const edm::Event
282282
double phi = -999.;
283283
double mass = 0;
284284

285-
std::list<UCTObject*> boostedJetObjs = summaryCard.getBoostedJetObjs();
286-
for (std::list<UCTObject*>::const_iterator i = boostedJetObjs.begin(); i != boostedJetObjs.end(); i++) {
287-
const UCTObject* object = *i;
285+
std::list<std::shared_ptr<UCTObject>> boostedJetObjs = summaryCard.getBoostedJetObjs();
286+
for (std::list<std::shared_ptr<UCTObject>>::const_iterator i = boostedJetObjs.begin(); i != boostedJetObjs.end();
287+
i++) {
288+
const std::shared_ptr<UCTObject> object = *i;
288289
pt = ((double)object->et()) * caloScaleFactor * boostedJetPtFactor;
289290
eta = g.getUCTTowerEta(object->iEta());
290291
phi = g.getUCTTowerPhi(object->iPhi());

L1Trigger/L1TCaloLayer1/src/UCTLayer1.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const UCTRegion* UCTLayer1::getRegion(int regionEtaIndex, uint32_t regionPhiInde
6262
return region;
6363
}
6464

65-
const UCTTower* UCTLayer1::getTower(int caloEta, int caloPhi) const {
65+
const std::shared_ptr<UCTTower> UCTLayer1::getTower(int caloEta, int caloPhi) const {
6666
if (caloPhi < 0) {
6767
LOG_ERROR << "UCT::getTower - Negative caloPhi is unacceptable -- bailing" << std::endl;
6868
exit(1);
@@ -71,7 +71,7 @@ const UCTTower* UCTLayer1::getTower(int caloEta, int caloPhi) const {
7171
UCTTowerIndex twr = UCTTowerIndex(caloEta, caloPhi);
7272
const UCTRegionIndex rgn = g.getUCTRegionIndex(twr);
7373
const UCTRegion* region = getRegion(rgn);
74-
const UCTTower* tower = region->getTower(twr);
74+
const std::shared_ptr<UCTTower> tower = region->getTower(twr);
7575
return tower;
7676
}
7777

L1Trigger/L1TCaloLayer1/src/UCTLayer1.hh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define UCTLayer1_hh
33

44
#include <vector>
5+
#include <memory>
56

67
class UCTCrate;
78
class UCTRegion;
@@ -36,7 +37,7 @@ public:
3637

3738
std::vector<UCTCrate*>& getCrates() { return crates; }
3839
const UCTRegion* getRegion(UCTRegionIndex r) const { return getRegion(r.first, r.second); }
39-
const UCTTower* getTower(UCTTowerIndex t) const { return getTower(t.first, t.second); }
40+
const std::shared_ptr<UCTTower> getTower(UCTTowerIndex t) const { return getTower(t.first, t.second); }
4041

4142
// To zero out event in case of selective tower filling
4243
bool clearEvent();
@@ -58,7 +59,7 @@ private:
5859
// Helper functions
5960

6061
const UCTRegion* getRegion(int regionEtaIndex, uint32_t regionPhiIndex) const;
61-
const UCTTower* getTower(int caloEtaIndex, int caloPhiIndex) const;
62+
const std::shared_ptr<UCTTower> getTower(int caloEtaIndex, int caloPhiIndex) const;
6263

6364
//Private data
6465

L1Trigger/L1TCaloLayer1/src/UCTRegion.cc

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ uint32_t getHitTowerLocation(uint32_t* et) {
6868
return iAve;
6969
}
7070

71+
UCTRegion::UCTRegion(const UCTRegion& otherRegion)
72+
: crate(otherRegion.crate),
73+
card(otherRegion.card),
74+
region(otherRegion.region),
75+
towers(otherRegion.towers),
76+
regionSummary(otherRegion.regionSummary),
77+
fwVersion(otherRegion.fwVersion) {}
78+
7179
UCTRegion::UCTRegion(uint32_t crt, uint32_t crd, bool ne, uint32_t rgn, int fwv)
7280
: crate(crt), card(crd), region(rgn), negativeEta(ne), regionSummary(0), fwVersion(fwv) {
7381
UCTGeometry g;
@@ -76,24 +84,17 @@ UCTRegion::UCTRegion(uint32_t crt, uint32_t crd, bool ne, uint32_t rgn, int fwv)
7684
towers.clear();
7785
for (uint32_t iEta = 0; iEta < nEta; iEta++) {
7886
for (uint32_t iPhi = 0; iPhi < nPhi; iPhi++) {
79-
towers.push_back(new UCTTower(crate, card, ne, region, iEta, iPhi, fwVersion));
87+
towers.push_back(std::make_shared<UCTTower>(crate, card, ne, region, iEta, iPhi, fwVersion));
8088
}
8189
}
8290
}
8391

84-
UCTRegion::~UCTRegion() {
85-
for (uint32_t i = 0; i < towers.size(); i++) {
86-
if (towers[i] != nullptr)
87-
delete towers[i];
88-
}
89-
}
90-
91-
const UCTTower* UCTRegion::getTower(uint32_t caloEta, uint32_t caloPhi) const {
92+
const std::shared_ptr<UCTTower> UCTRegion::getTower(uint32_t caloEta, uint32_t caloPhi) const {
9293
UCTGeometry g;
9394
uint32_t nPhi = g.getNPhi(region);
9495
uint32_t iEta = g.getiEta(caloEta);
9596
uint32_t iPhi = g.getiPhi(caloPhi);
96-
UCTTower* tower = towers[iEta * nPhi + iPhi];
97+
std::shared_ptr<UCTTower> tower = towers[iEta * nPhi + iPhi];
9798
return tower;
9899
}
99100

@@ -229,7 +230,7 @@ bool UCTRegion::setECALData(UCTTowerIndex t, bool ecalFG, uint32_t ecalET) {
229230
uint32_t absCaloPhi = abs(t.second);
230231
uint32_t iEta = g.getiEta(absCaloEta);
231232
uint32_t iPhi = g.getiPhi(absCaloPhi);
232-
UCTTower* tower = towers[iEta * nPhi + iPhi];
233+
std::shared_ptr<UCTTower> tower = towers[iEta * nPhi + iPhi];
233234
return tower->setECALData(ecalFG, ecalET);
234235
}
235236

@@ -244,7 +245,7 @@ bool UCTRegion::setHCALData(UCTTowerIndex t, uint32_t hcalFB, uint32_t hcalET) {
244245
// Valid data are:
245246
// absCaloEta = 30-39, 1 < absCaloPhi <= 72 (every second value)
246247
for (uint32_t iPhi = iPhiStart; iPhi < iPhiStart + 2; iPhi++) { // For artificial splitting in half
247-
UCTTower* tower = towers[iEta * nPhi + iPhi];
248+
std::shared_ptr<UCTTower> tower = towers[iEta * nPhi + iPhi];
248249
// We divide by 2 in output section, after LUT
249250
if (!tower->setHFData(hcalFB, hcalET))
250251
return false;
@@ -253,14 +254,14 @@ bool UCTRegion::setHCALData(UCTTowerIndex t, uint32_t hcalFB, uint32_t hcalET) {
253254
// Valid data are:
254255
// absCaloEta = 40,41, 1 < absCaloPhi <= 72 (every fourth value)
255256
for (uint32_t iPhi = 0; iPhi < 4; iPhi++) { // For artificial splitting in quarter
256-
UCTTower* tower = towers[iEta * nPhi + iPhi];
257+
std::shared_ptr<UCTTower> tower = towers[iEta * nPhi + iPhi];
257258
// We divide by 4 in output section, after LUT
258259
if (!tower->setHFData(hcalFB, hcalET))
259260
return false;
260261
}
261262
} else {
262263
uint32_t iPhi = g.getiPhi(absCaloPhi);
263-
UCTTower* tower = towers[iEta * nPhi + iPhi];
264+
std::shared_ptr<UCTTower> tower = towers[iEta * nPhi + iPhi];
264265
return tower->setHCALData(hcalFB, hcalET);
265266
}
266267
return true;

L1Trigger/L1TCaloLayer1/src/UCTRegion.hh

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <vector>
55
#include <iostream>
6+
#include <memory>
67

78
#include "UCTTower.hh"
89

@@ -30,19 +31,19 @@ public:
3031

3132
UCTRegion() = delete;
3233

33-
// No copy constructor is needed
34-
35-
UCTRegion(const UCTRegion&) = delete;
34+
//copy constructor helps gettting around some memory leakage
35+
// and ownership problems in the summary card emulation
36+
UCTRegion(const UCTRegion&);
3637

3738
// No equality operator is needed
3839

3940
const UCTRegion& operator=(const UCTRegion&) = delete;
4041

41-
virtual ~UCTRegion();
42+
virtual ~UCTRegion() = default;
4243

4344
// To setData for towers before processing
4445

45-
const std::vector<UCTTower*>& getTowers() { return towers; }
46+
const std::vector<std::shared_ptr<UCTTower>>& getTowers() { return towers; }
4647

4748
// To process event
4849

@@ -92,14 +93,14 @@ public:
9293

9394
const bool isNegativeEta() const { return negativeEta; }
9495

95-
const UCTTower* getTower(UCTTowerIndex t) const { return getTower(t.first, t.second); }
96+
const std::shared_ptr<UCTTower> getTower(UCTTowerIndex t) const { return getTower(t.first, t.second); }
9697

9798
friend std::ostream& operator<<(std::ostream&, const UCTRegion&);
9899

99100
protected:
100101
// Helper functions
101102

102-
const UCTTower* getTower(uint32_t caloEta, uint32_t caloPhi) const;
103+
const std::shared_ptr<UCTTower> getTower(uint32_t caloEta, uint32_t caloPhi) const;
103104

104105
// Region location definition
105106

@@ -110,7 +111,7 @@ protected:
110111

111112
// Owned region level data
112113

113-
std::vector<UCTTower*> towers;
114+
std::vector<std::shared_ptr<UCTTower>> towers;
114115

115116
uint32_t regionSummary;
116117

L1Trigger/L1TCaloLayer1/src/UCTSummaryCard.cc

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,6 @@ UCTSummaryCard::UCTSummaryCard(const std::vector<std::vector<std::vector<uint32_
4040
}
4141
}
4242

43-
UCTSummaryCard::~UCTSummaryCard() {
44-
for (uint32_t i = 0; i < regions.size(); i++) {
45-
if (regions[i] != nullptr)
46-
delete regions[i];
47-
}
48-
}
49-
5043
bool UCTSummaryCard::process() {
5144
clearEvent();
5245
UCTGeometry g;
@@ -126,10 +119,10 @@ bool UCTSummaryCard::process() {
126119
double mhtPhi = (atan2(sumHy, sumHx) * 180. / 3.1415927) + 180.;
127120
int mhtIPhi = (int)(72. * (mhtPhi / 360.));
128121

129-
ET = new UCTObject(UCTObject::ET, etValue, 0, metIPhi, pileup, 0, 0);
130-
HT = new UCTObject(UCTObject::HT, htValue, 0, mhtIPhi, pileup, 0, 0);
131-
MET = new UCTObject(UCTObject::MET, metValue, 0, metIPhi, pileup, 0, 0);
132-
MHT = new UCTObject(UCTObject::MHT, mhtValue, 0, mhtIPhi, pileup, 0, 0);
122+
ET = std::make_shared<UCTObject>(UCTObject::ET, etValue, 0, metIPhi, pileup, 0, 0);
123+
HT = std::make_shared<UCTObject>(UCTObject::HT, htValue, 0, mhtIPhi, pileup, 0, 0);
124+
MET = std::make_shared<UCTObject>(UCTObject::MET, metValue, 0, metIPhi, pileup, 0, 0);
125+
MHT = std::make_shared<UCTObject>(UCTObject::MHT, mhtValue, 0, mhtIPhi, pileup, 0, 0);
133126

134127
// Then sort the candidates for output usage
135128
emObjs.sort();
@@ -365,12 +358,16 @@ bool UCTSummaryCard::processRegion(UCTRegionIndex center) {
365358
if (centralET >= northET && centralET >= nwET && centralET >= westET && centralET >= swET && centralET > southET &&
366359
centralET > seET && centralET > eastET && centralET > neET && centralET > jetSeed) {
367360
if (centralRegion)
368-
centralJetObjs.push_back(new UCTObject(UCTObject::jet, jetET, hitCaloEta, hitCaloPhi, pu3x3, 0, et3x3));
361+
centralJetObjs.push_back(
362+
std::make_shared<UCTObject>(UCTObject::jet, jetET, hitCaloEta, hitCaloPhi, pu3x3, 0, et3x3));
369363
else
370-
forwardJetObjs.push_back(new UCTObject(UCTObject::jet, jetET, hitCaloEta, hitCaloPhi, pu3x3, 0, et3x3));
364+
forwardJetObjs.push_back(
365+
std::make_shared<UCTObject>(UCTObject::jet, jetET, hitCaloEta, hitCaloPhi, pu3x3, 0, et3x3));
371366
}
372367

373-
auto boostedJet = new UCTObject(UCTObject::jet, jetET, hitCaloEta, hitCaloPhi, pu3x3, 0, et3x3);
368+
//auto boostedJet = new UCTObject(UCTObject::jet, jetET, hitCaloEta, hitCaloPhi, pu3x3, 0, et3x3);
369+
std::shared_ptr<UCTObject> boostedJet =
370+
std::make_shared<UCTObject>(UCTObject::jet, jetET, hitCaloEta, hitCaloPhi, pu3x3, 0, et3x3);
374371
boostedJet->setNTaus(nTauLike);
375372
boostedJet->setBoostedJetRegionET(boostedJetRegionET);
376373
boostedJet->setBoostedJetRegionTauVeto(boostedJetRegionTauVeto);
@@ -419,13 +416,15 @@ bool UCTSummaryCard::processRegion(UCTRegionIndex center) {
419416
}
420417
}
421418
if (tauET != 0) {
422-
tauObjs.push_back(new UCTObject(UCTObject::tau, tauET, hitCaloEta, hitCaloPhi, tauPU, 0xDEADBEEF, et3x3));
419+
tauObjs.push_back(
420+
std::make_shared<UCTObject>(UCTObject::tau, tauET, hitCaloEta, hitCaloPhi, tauPU, 0xDEADBEEF, et3x3));
423421
// Subtract footprint
424422
uint32_t isolation = 0;
425423
if (et3x3 > tauET)
426424
isolation = et3x3 - tauET;
427425
if (isolation < ((uint32_t)(tauIsolationFactor * (double)tauET))) {
428-
isoTauObjs.push_back(new UCTObject(UCTObject::isoTau, tauET, hitCaloEta, hitCaloPhi, pu3x3, isolation, et3x3));
426+
isoTauObjs.push_back(
427+
std::make_shared<UCTObject>(UCTObject::isoTau, tauET, hitCaloEta, hitCaloPhi, pu3x3, isolation, et3x3));
429428
}
430429
}
431430
}
@@ -474,13 +473,14 @@ bool UCTSummaryCard::processRegion(UCTRegionIndex center) {
474473
}
475474
}
476475
if (eGammaET != 0) {
477-
emObjs.push_back(new UCTObject(UCTObject::eGamma, eGammaET, hitCaloEta, hitCaloPhi, eGammaPU, 0xDEADBEEF, et3x3));
476+
emObjs.push_back(std::make_shared<UCTObject>(
477+
UCTObject::eGamma, eGammaET, hitCaloEta, hitCaloPhi, eGammaPU, 0xDEADBEEF, et3x3));
478478
uint32_t isolation = 0;
479479
if (et3x3 > eGammaET)
480480
isolation = et3x3 - eGammaET;
481481
if (isolation < ((uint32_t)(eGammaIsolationFactor * (double)eGammaET))) {
482-
isoEMObjs.push_back(
483-
new UCTObject(UCTObject::isoEGamma, eGammaET, hitCaloEta, hitCaloPhi, pu3x3, isolation, et3x3));
482+
isoEMObjs.push_back(std::make_shared<UCTObject>(
483+
UCTObject::isoEGamma, eGammaET, hitCaloEta, hitCaloPhi, pu3x3, isolation, et3x3));
484484
}
485485
}
486486
}
@@ -528,10 +528,10 @@ const UCTRegion* UCTSummaryCard::getRegion(int regionEtaIndex, uint32_t regionPh
528528
edm::LogError("L1TCaloSummary") << "UCTSummaryCard: Incorrect region requested -- bailing" << std::endl;
529529
exit(1);
530530
}
531-
return regions[i];
531+
return &regions[i];
532532
}
533533

534-
bool UCTSummaryCard::setRegionData(std::vector<UCTRegion*> inputRegions) {
534+
bool UCTSummaryCard::setRegionData(std::vector<UCTRegion> inputRegions) {
535535
for (long unsigned int i = 0; i < inputRegions.size(); i++) {
536536
regions.push_back(inputRegions[i]);
537537
}

0 commit comments

Comments
 (0)