Commit e391bb43 authored by Goetz Gaycken's avatar Goetz Gaycken
Browse files

Fix memory leak in TRT AliveStraws.

Memory leaks are resolved by replacing manually managed storage by std::arrays.
parent dd08934d
/*
Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
*/
#ifndef ALIVESTRAWS_H
#define ALIVESTRAWS_H
#include "AthenaKernel/CLASS_DEF.h"
#include "AthenaKernel/CondCont.h"
#include <cassert>
#include <array>
namespace TRTCond{
class AliveStraws{
public:
AliveStraws() {
// Create arrays for alive straws
// These are moved to CondStore which takes care of their deletion
m_stw_total = new int[7]();
m_stw_local = new int*[6];
m_stw_wheel = new int*[34];
for (int i=0; i<6 ; ++i) m_stw_local[i] = new int[32]();
for (int i=0; i<34; ++i) m_stw_wheel[i] = new int[32]();
}
static constexpr unsigned int NTOTAL = 7;
static constexpr unsigned int NLOCAL = 6;
static constexpr unsigned int NWHEEL = 34;
static constexpr unsigned int NLOCALPHI = 32;
AliveStraws() {}
virtual ~AliveStraws() {}
int *getStwTotal() const {return m_stw_total; }
int **getStwLocal() const {return m_stw_local; }
int **getStwWheel() const {return m_stw_wheel; }
void update(const int& i, const int& j, const int& phi) {
const std::array<int,NTOTAL> &getStwTotal() const {return m_stw_total; }
const std::array<std::array<int,NLOCALPHI>,NLOCAL> &getStwLocal() const {return m_stw_local; }
const std::array<std::array<int,NLOCALPHI>,NWHEEL> &getStwWheel() const {return m_stw_wheel; }
void update(const unsigned int i, const unsigned int j, const unsigned int phi) {
assert(i>0 && i<m_stw_total.size() && i-1<m_stw_local.size() && j<m_stw_wheel.size() && phi<NLOCALPHI);
m_stw_total[0] +=1;
m_stw_total[i] +=1;
m_stw_local[i-1][phi] +=1;
......@@ -32,10 +32,11 @@ namespace TRTCond{
}
private:
int *m_stw_total;
int **m_stw_local;
int **m_stw_wheel;
std::array<int,NTOTAL> m_stw_total {};
std::array<std::array<int,NLOCALPHI>,NLOCAL> m_stw_local {};
std::array<std::array<int,NLOCALPHI>,NWHEEL> m_stw_wheel {};
};
}
CLASS_DEF(TRTCond::AliveStraws,234870469,1)
CONDCONT_DEF(TRTCond::AliveStraws,110897079);
......
/*
Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
*/
///////////////////////////////////////////////////////////////////
......@@ -78,15 +78,17 @@ namespace InDet
static const int NTOTAL = 7;
//(barrel, ECA, ECB)side C, (barrel, ECA, ECB)side A [6][32]
static const int NLOCAL = 6;
static const int NWHEEL = 34;
static const int NLOCALPHI = 32;
struct OccupancyData {
OccupancyData(const std::array<std::array<int,NLOCALPHI>,NLOCAL> &local)
: m_stw_local(local)
{}
int m_occ_total[NTOTAL] = {0};
int m_hit_total[NTOTAL] = {0};
int m_occ_local[NLOCAL][NLOCALPHI] = {{0}};
int m_hit_local[NLOCAL][NLOCALPHI] = {{0}};
int* m_stw_total = nullptr;
int** m_stw_local = nullptr;
int** m_stw_wheel = nullptr;
const std::array<std::array<int,NLOCALPHI>,NLOCAL> &m_stw_local;
float m_stws_ratio[2][NLOCALPHI] = {{0}};
};
......
/*
Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
*/
///////////////////////////////////////////////////////////////////
......@@ -163,6 +163,7 @@ float TRT_LocalOccupancy::LocalOccupancy(const Trk::Track& track ) const {
float averageocc = 0;
int nhits = 0;
const std::array<std::array<int,NLOCALPHI>,NLOCAL> &stw_local = data->m_stw_local;
for (int i=0; i<6; ++i){
for (int j = 0; j < 32; j++){
......@@ -171,8 +172,8 @@ float TRT_LocalOccupancy::LocalOccupancy(const Trk::Track& track ) const {
if (hits_array<1) continue;
float occ=0;
occ = (data->m_occ_local [i][j])*1.e-2 ;
if (data->m_stw_local[i][j] != 0){
if(occ == 0 && float(hits_array)/data->m_stw_local[i][j] > 0.01){
if (stw_local[i][j] != 0){
if(occ == 0 && float(hits_array)/stw_local[i][j] > 0.01){
ATH_MSG_DEBUG("Occupancy is 0 for : " << i << " " << j << " BUT THERE ARE HITS!!!: " << hits_array);
continue;
}
......@@ -247,15 +248,13 @@ std::map<int, double> TRT_LocalOccupancy::getDetectorOccupancy( const TRT_RDO_C
SG::ReadCondHandle<TRTCond::AliveStraws> strawHandle{m_strawReadKey};
const TRTCond::AliveStraws* strawCounts{*strawHandle};
const std::array<int,TRTCond::AliveStraws::NTOTAL> &straws = strawCounts->getStwTotal();
int* straws = strawCounts->getStwTotal();
occResults[-1] = (double)hitCounter[-1]/(double)straws[1];
occResults[-2] = (double)hitCounter[-2]/(double)(straws[2] + straws[3]);
occResults[1] = (double)hitCounter[1] /(double)straws[4];
occResults[2] = (double)hitCounter[2] /(double)(straws[5] + straws[6]);
return occResults;
}
......@@ -488,10 +487,14 @@ const TRT_LocalOccupancy::OccupancyData* TRT_LocalOccupancy::getData() const
std::unique_ptr<TRT_LocalOccupancy::OccupancyData>
TRT_LocalOccupancy::makeData() const
{
auto data = std::make_unique<OccupancyData>();
SG::ReadHandle<TRT_DriftCircleContainer> driftCircleContainer( m_trt_driftcircles );
// count live straws
SG::ReadCondHandle<TRTCond::AliveStraws> strawHandle{m_strawReadKey};
const TRTCond::AliveStraws* strawCounts{*strawHandle};
auto data = std::make_unique<OccupancyData>(strawCounts->getStwLocal());
// put # hits in vectors
if ( driftCircleContainer.isValid() ) {
ATH_MSG_DEBUG("Found Drift Circles in StoreGate");
......@@ -519,19 +522,14 @@ TRT_LocalOccupancy::makeData() const
ATH_MSG_WARNING("No TRT Drift Circles in StoreGate");
}
const std::array<int,NTOTAL> &stw_total = strawCounts->getStwTotal();
const std::array<std::array<int,NLOCALPHI>,NLOCAL> &stw_local = strawCounts->getStwLocal();
// count live straws
SG::ReadCondHandle<TRTCond::AliveStraws> strawHandle{m_strawReadKey};
const TRTCond::AliveStraws* strawCounts{*strawHandle};
data->m_stw_total = strawCounts->getStwTotal();
data->m_stw_local = strawCounts->getStwLocal();
// Calculate Occs:
for (int i=0; i<NTOTAL; ++i) {
float occ = 0;
int hits = data->m_hit_total[i];
int stws = data->m_stw_total[i];
int stws = stw_total[i];
if (stws>0) occ = float(hits*100)/stws;
data->m_occ_total[i] = int(occ);
}
......@@ -539,13 +537,13 @@ TRT_LocalOccupancy::makeData() const
for (int j=0; j<NLOCALPHI; ++j) {
float occ = 0;
int hits = data->m_hit_local[i][j];
int stws = data->m_stw_local[i][j];
int stws = stw_local[i][j];
if (stws>0) occ = float(hits*100)/stws;
data->m_occ_local[i][j] = int(occ);
}
}
ATH_MSG_DEBUG("Active straws: " << data->m_stw_total[0] << "\t total number of hits: " << data->m_hit_total[0] << "\t occ: " << data->m_occ_total[0] );
ATH_MSG_DEBUG("Active straws: " << stw_total[0] << "\t total number of hits: " << data->m_hit_total[0] << "\t occ: " << data->m_occ_total[0] );
return data;
}
......@@ -553,17 +551,17 @@ TRT_LocalOccupancy::makeData() const
std::unique_ptr<TRT_LocalOccupancy::OccupancyData>
TRT_LocalOccupancy::makeDataTrigger() const
{
auto data = std::make_unique<OccupancyData>();
SG::ReadCondHandle<TRTCond::AliveStraws> strawHandle{m_strawReadKey};
const TRTCond::AliveStraws* strawCounts{*strawHandle};
data->m_stw_local = strawCounts->getStwLocal();
data->m_stw_wheel = strawCounts->getStwWheel();
auto data = std::make_unique<OccupancyData>(strawCounts->getStwLocal());
const std::array<std::array<int,NLOCALPHI>,NLOCAL> &stw_local = strawCounts->getStwLocal();;
const std::array<std::array<int,NLOCALPHI>,NWHEEL> &stw_wheel = strawCounts->getStwWheel();
for (int i=0; i<5; ++i){
for (int j=0; j<NLOCALPHI; ++j){
data->m_stws_ratio[0][j]+=float(data->m_stw_wheel[i+3 ][j])/data->m_stw_local[1][j];
data->m_stws_ratio[1][j]+=float(data->m_stw_wheel[i+20][j])/data->m_stw_local[4][j];
data->m_stws_ratio[0][j]+=float(stw_wheel[i+3 ][j])/stw_local[1][j];
data->m_stws_ratio[1][j]+=float(stw_wheel[i+20][j])/stw_local[4][j];
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment