Skip to content

Commit cc7e9ac

Browse files
fix leaks in FairTutorialDet4StraightLineFitter
Replace multiple heap allocated objects by either value members of the class or just local stack variables. Introduce more `override`. And drop empty ReInit and Finish methods that override the original methods that are also doing the same.
1 parent 0f29938 commit cc7e9ac

2 files changed

Lines changed: 28 additions & 64 deletions

File tree

examples/simulation/Tutorial4/src/reco/FairTutorialDet4StraightLineFitter.cxx

Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,24 @@
77
********************************************************************************/
88
#include "FairTutorialDet4StraightLineFitter.h"
99

10-
#include "FairLogger.h" // for FairLogger, etc
1110
#include "FairRootManager.h" // for FairRootManager
1211
#include "FairTrackParam.h" // for FairTrackParam
1312
#include "FairTutorialDet4Hit.h" // for FairTutorialDet4Hit
1413

15-
#include <TClonesArray.h> // for TClonesArray
1614
#include <TF1.h> // for TF1
1715
#include <TGraphErrors.h> // for TGraphErrors
1816
#include <TVector3.h> // for TVector3
19-
#include <set> // for set, set<>::iterator, etc
17+
#include <fairlogger/Logger.h>
18+
#include <set>
2019

2120
FairTutorialDet4StraightLineFitter::FairTutorialDet4StraightLineFitter()
2221
: FairTask("FairTutorialDet4StraightLineFitter")
23-
, fHits(nullptr)
24-
, fTracks(nullptr)
22+
, fTracks(FairTrackParam::Class(), 100)
2523
, fVersion(2)
2624
{
2725
LOG(debug) << "Default Constructor of FairTutorialDet4StraightLineFitter";
2826
}
2927

30-
FairTutorialDet4StraightLineFitter::~FairTutorialDet4StraightLineFitter()
31-
{
32-
LOG(debug) << "Destructor of FairTutorialDet4StraightLineFitter";
33-
if (fTracks) {
34-
fTracks->Delete();
35-
delete fTracks;
36-
}
37-
}
38-
3928
InitStatus FairTutorialDet4StraightLineFitter::Init()
4029
{
4130
LOG(debug) << "Initilization of FairTutorialDet4StraightLineFitter";
@@ -45,7 +34,7 @@ InitStatus FairTutorialDet4StraightLineFitter::Init()
4534

4635
// Get a pointer to the previous already existing data level
4736

48-
fHits = static_cast<TClonesArray*>(ioman->GetObject("TutorialDetHit"));
37+
fHits = dynamic_cast<TClonesArray const*>(ioman->GetObject("TutorialDetHit"));
4938
if (!fHits) {
5039
LOG(error) << "No InputDataLevelName array!\n"
5140
<< "FairTutorialDet4StraightLineFitter will be inactive";
@@ -54,8 +43,7 @@ InitStatus FairTutorialDet4StraightLineFitter::Init()
5443

5544
// Create the TClonesArray for the output data and register
5645
// it in the IO manager
57-
fTracks = new TClonesArray("FairTrackParam", 100);
58-
ioman->Register("TutorialDetTrack", "TutorialDet", fTracks, kTRUE);
46+
ioman->Register("TutorialDetTrack", "TutorialDet", &fTracks, kTRUE);
5947

6048
// Do whatever else is needed at the initilization stage
6149
// Create histograms to be filled
@@ -64,12 +52,6 @@ InitStatus FairTutorialDet4StraightLineFitter::Init()
6452
return kSUCCESS;
6553
}
6654

67-
InitStatus FairTutorialDet4StraightLineFitter::ReInit()
68-
{
69-
LOG(debug) << "Initilization of FairTutorialDet4StraightLineFitter";
70-
return kSUCCESS;
71-
}
72-
7355
void FairTutorialDet4StraightLineFitter::Exec(Option_t* /*option*/)
7456
{
7557
LOG(debug) << "Exec of FairTutorialDet4StraightLineFitter";
@@ -79,7 +61,6 @@ void FairTutorialDet4StraightLineFitter::Exec(Option_t* /*option*/)
7961
}
8062

8163
// Declare some variables
82-
FairTutorialDet4Hit* hit = nullptr;
8364
/*
8465
Int_t detID = 0; // Detector ID
8566
Int_t trackID = 0; // Track index
@@ -98,7 +79,7 @@ void FairTutorialDet4StraightLineFitter::Exec(Option_t* /*option*/)
9879
Float_t* YPosErr = new Float_t[nHits];
9980

10081
for (Int_t iHit = 0; iHit < nHits; iHit++) {
101-
hit = static_cast<FairTutorialDet4Hit*>(fHits->At(iHit));
82+
auto hit = static_cast<FairTutorialDet4Hit const*>(fHits->At(iHit));
10283
if (!hit) {
10384
continue;
10485
}
@@ -111,24 +92,22 @@ void FairTutorialDet4StraightLineFitter::Exec(Option_t* /*option*/)
11192
YPosErr[iHit] = hit->GetDy();
11293
}
11394

114-
TF1* f1 = new TF1("f1", "[0]*x + [1]");
115-
TGraphErrors* LineGraph;
116-
117-
LineGraph = new TGraphErrors(nHits, ZPos, XPos, 0, XPosErr);
118-
LineGraph->Fit("f1", "Q");
119-
Double_t SlopeX = f1->GetParameter(0);
120-
Double_t OffX = f1->GetParameter(1);
121-
Double_t Chi2X = f1->GetChisquare();
95+
auto f1 = TF1("f1", "[0]*x + [1]");
96+
auto linegraphX = TGraphErrors(nHits, ZPos, XPos, nullptr, XPosErr);
97+
linegraphX.Fit(&f1, "Q");
98+
Double_t SlopeX = f1.GetParameter(0);
99+
Double_t OffX = f1.GetParameter(1);
100+
Double_t Chi2X = f1.GetChisquare();
122101
Double_t SlopeY = 0.;
123102
Double_t OffY = 0.;
124103
Double_t Chi2Y;
125104

126105
if (2 == fVersion) {
127-
LineGraph = new TGraphErrors(nHits, ZPos, YPos, 0, YPosErr);
128-
LineGraph->Fit("f1", "Q");
129-
SlopeY = f1->GetParameter(0);
130-
OffY = f1->GetParameter(1);
131-
Chi2Y = f1->GetChisquare();
106+
auto linegraphY = TGraphErrors(nHits, ZPos, YPos, nullptr, YPosErr);
107+
linegraphY.Fit(&f1, "Q");
108+
SlopeY = f1.GetParameter(0);
109+
OffY = f1.GetParameter(1);
110+
Chi2Y = f1.GetChisquare();
132111

133112
LOG(debug) << XPos[0] << "," << XPos[nHits - 1] << "," << YPos[0] << "," << YPos[nHits - 1] << "," << ZPos[0]
134113
<< "," << ZPos[nHits - 1];
@@ -141,15 +120,14 @@ void FairTutorialDet4StraightLineFitter::Exec(Option_t* /*option*/)
141120
LOG(debug) << "Chi2(x,y): " << Chi2X << " ," << Chi2Y;
142121
}
143122

144-
FairTrackParam* track = new FairTrackParam();
123+
auto track = static_cast<FairTrackParam*>(fTracks.ConstructedAt(0));
145124
track->SetX(OffX);
146125
track->SetTx(SlopeX);
147126
track->SetZ(0.);
148127
if (2 == fVersion) {
149128
track->SetY(OffY);
150129
track->SetTy(SlopeY);
151130
}
152-
new ((*fTracks)[0]) FairTrackParam(*track);
153131
// const TMatrixFSym matrix;
154132
// Double_t Z = 0.;
155133
// new ((*fTracks)[0]) FairTrackParam(OffX, OffY, Z, SlopeX, SlopeY, matrix);
@@ -168,15 +146,13 @@ Bool_t FairTutorialDet4StraightLineFitter::IsGoodEvent()
168146
// event, so we have to check for this.
169147
// In the end the algorithm should be able to work also with
170148
// missing hits in some stations
171-
FairTutorialDet4Hit* hit;
172149
std::set<Int_t> detIdSet;
173-
std::set<Int_t>::iterator it;
174150

175151
Int_t nHits = fHits->GetEntriesFast();
176152
for (Int_t iHit = 0; iHit < nHits; ++iHit) {
177-
hit = static_cast<FairTutorialDet4Hit*>(fHits->At(iHit));
153+
auto hit = static_cast<FairTutorialDet4Hit const*>(fHits->At(iHit));
178154
Int_t detId = hit->GetDetectorID();
179-
it = detIdSet.find(detId);
155+
auto it = detIdSet.find(detId);
180156
if (it == detIdSet.end()) {
181157
detIdSet.insert(detId);
182158
} else {
@@ -187,8 +163,3 @@ Bool_t FairTutorialDet4StraightLineFitter::IsGoodEvent()
187163
}
188164
return kTRUE;
189165
}
190-
191-
void FairTutorialDet4StraightLineFitter::Finish()
192-
{
193-
LOG(debug) << "Finish of FairTutorialDet4StraightLineFitter";
194-
}
Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/********************************************************************************
2-
* Copyright (C) 2014-2022 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
2+
* Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
33
* *
44
* This software is distributed under the terms of the *
55
* GNU Lesser General Public Licence (LGPL) version 3, *
@@ -11,8 +11,7 @@
1111
#include "FairTask.h" // for InitStatus, FairTask
1212

1313
#include <Rtypes.h> // for ClassDef
14-
15-
class TClonesArray;
14+
#include <TClonesArray.h>
1615

1716
class FairTutorialDet4StraightLineFitter : public FairTask
1817
{
@@ -24,28 +23,22 @@ class FairTutorialDet4StraightLineFitter : public FairTask
2423
// FairTutorialDet4StraightLineFitter(Int_t verbose);
2524

2625
/** Destructor **/
27-
~FairTutorialDet4StraightLineFitter();
26+
~FairTutorialDet4StraightLineFitter() override = default;
2827

2928
/** Initiliazation of task at the beginning of a run **/
30-
virtual InitStatus Init();
31-
32-
/** ReInitiliazation of task when the runID changes **/
33-
virtual InitStatus ReInit();
29+
InitStatus Init() override;
3430

3531
/** Executed for each event. **/
36-
virtual void Exec(Option_t* opt);
37-
38-
/** Finish task called at the end of the run **/
39-
virtual void Finish();
32+
void Exec(Option_t* opt) override;
4033

4134
void SetVersion(Int_t val) { fVersion = val; }
4235

4336
private:
4437
/** Input array from previous already existing data level **/
45-
TClonesArray* fHits;
38+
TClonesArray const* fHits{nullptr};
4639

4740
/** Output array to new data level**/
48-
TClonesArray* fTracks;
41+
TClonesArray fTracks;
4942

5043
Int_t fVersion;
5144

@@ -54,7 +47,7 @@ class FairTutorialDet4StraightLineFitter : public FairTask
5447
FairTutorialDet4StraightLineFitter(const FairTutorialDet4StraightLineFitter&);
5548
FairTutorialDet4StraightLineFitter operator=(const FairTutorialDet4StraightLineFitter&);
5649

57-
ClassDef(FairTutorialDet4StraightLineFitter, 1);
50+
ClassDefOverride(FairTutorialDet4StraightLineFitter, 1);
5851
};
5952

6053
#endif

0 commit comments

Comments
 (0)