Skip to content

Commit 9380ace

Browse files
fix(Base): Do not leak TFile in Fair{File,Mixed}Source
And refactor into FairFileSourceBase.
1 parent 135bd40 commit 9380ace

6 files changed

Lines changed: 40 additions & 55 deletions

File tree

fairroot/base/source/FairFileSource.cxx

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@
4242
#include <typeinfo>
4343
#include <vector>
4444

45+
using fairroot::detail::maybe_owning_ptr;
46+
using fairroot::detail::non_owning;
47+
4548
FairFileSource::FairFileSource(TFile* f, const char* Title, UInt_t)
46-
: FairFileSourceBase()
49+
: FairFileSourceBase(maybe_owning_ptr<TFile>{f, non_owning})
4750
, fInputTitle(Title)
48-
, fRootFile(f)
4951
, fFriendFileList()
5052
, fInputChainList()
5153
, fFriendTypeList()
@@ -73,16 +75,12 @@ FairFileSource::FairFileSource(TFile* f, const char* Title, UInt_t)
7375
, fEventMeanTime(0.)
7476
, fCheckFileLayout(kTRUE)
7577
{
76-
if ((!fRootFile) || fRootFile->IsZombie()) {
77-
LOG(fatal) << "Error opening the Input file";
78-
}
7978
LOG(debug) << "FairFileSource created------------";
8079
}
8180

8281
FairFileSource::FairFileSource(const TString* RootFileName, const char* Title, UInt_t)
83-
: FairFileSourceBase()
82+
: FairFileSourceBase(std::unique_ptr<TFile>{TFile::Open(RootFileName->Data())})
8483
, fInputTitle(Title)
85-
, fRootFile(0)
8684
, fFriendFileList()
8785
, fInputChainList()
8886
, fFriendTypeList()
@@ -110,17 +108,12 @@ FairFileSource::FairFileSource(const TString* RootFileName, const char* Title, U
110108
, fEventMeanTime(0.)
111109
, fCheckFileLayout(kTRUE)
112110
{
113-
fRootFile = TFile::Open(RootFileName->Data());
114-
if ((!fRootFile) || fRootFile->IsZombie()) {
115-
LOG(fatal) << "Error opening the Input file";
116-
}
117111
LOG(debug) << "FairFileSource created------------";
118112
}
119113

120114
FairFileSource::FairFileSource(const TString RootFileName, const char* Title, UInt_t)
121-
: FairFileSourceBase()
115+
: FairFileSourceBase(std::unique_ptr<TFile>{TFile::Open(RootFileName.Data())})
122116
, fInputTitle(Title)
123-
, fRootFile(0)
124117
, fFriendFileList()
125118
, fInputChainList()
126119
, fFriendTypeList()
@@ -148,10 +141,6 @@ FairFileSource::FairFileSource(const TString RootFileName, const char* Title, UI
148141
, fEventMeanTime(0.)
149142
, fCheckFileLayout(kTRUE)
150143
{
151-
fRootFile = TFile::Open(RootFileName.Data());
152-
if ((!fRootFile) || fRootFile->IsZombie()) {
153-
LOG(fatal) << "Error opening the Input file";
154-
}
155144
LOG(debug) << "FairFileSource created------------";
156145
}
157146

@@ -294,9 +283,8 @@ Bool_t FairFileSource::Init()
294283

295284
void FairFileSource::SetInTree(TTree* tempTree)
296285
{
297-
fInTree = nullptr;
298286
fInTree = tempTree;
299-
fRootFile = tempTree->GetCurrentFile();
287+
fRootFile = maybe_owning_ptr<TFile>{tempTree->GetCurrentFile(), non_owning};
300288
fInChain->Reset();
301289
IsInitialized = kFALSE;
302290
Init();
@@ -581,8 +569,8 @@ Bool_t FairFileSource::ActivateObjectAny(void** obj, const std::type_info& info,
581569

582570
void FairFileSource::SetInputFile(TString name)
583571
{
584-
fRootFile = TFile::Open(name.Data());
585-
if (fRootFile->IsZombie()) {
572+
fRootFile = std::unique_ptr<TFile>{TFile::Open(name.Data())};
573+
if ((!fRootFile) || fRootFile->IsZombie()) {
586574
LOG(fatal) << "Error opening the Input file";
587575
}
588576
LOG(info) << "FairFileSource set------------";

fairroot/base/source/FairFileSource.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/********************************************************************************
2-
* Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
2+
* Copyright (C) 2014-2024 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, *
@@ -52,9 +52,9 @@ class FairFileSource : public FairFileSourceBase
5252
void ReadBranchEvent(const char* BrName) override;
5353
/**Read specific tree entry on one branch**/
5454
void ReadBranchEvent(const char* BrName, Int_t Entry) override;
55+
5556
void FillEventHeader(FairEventHeader* feh) override;
5657

57-
const TFile* GetRootFile() { return fRootFile; }
5858
/** Add a friend file (input) by name)*/
5959
void AddFriend(TString FileName);
6060
/**Add ROOT file to input, the file will be chained to already added files*/
@@ -65,7 +65,6 @@ class FairFileSource : public FairFileSourceBase
6565
void CreateNewFriendChain(TString inputFile, TString inputLevel);
6666
TTree* GetInTree() { return fInChain->GetTree(); }
6767
TChain* GetInChain() { return fInChain; }
68-
TFile* GetInFile() { return fRootFile; }
6968
void CloseInFile()
7069
{
7170
if (fRootFile) {
@@ -112,8 +111,6 @@ class FairFileSource : public FairFileSourceBase
112111
private:
113112
/** Title of input source, could be input, background or signal*/
114113
TString fInputTitle;
115-
/**ROOT file*/
116-
TFile* fRootFile;
117114
/** List of all files added with AddFriend */
118115
std::list<TString> fFriendFileList; //!
119116
std::list<TString> fInputChainList; //!

fairroot/base/source/FairFileSourceBase.cxx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/********************************************************************************
2-
* Copyright (C) 2022-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
2+
* Copyright (C) 2022-2024 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, *
@@ -15,6 +15,15 @@
1515
#include <fairlogger/Logger.h>
1616
#include <memory>
1717
#include <set>
18+
#include <utility> // for std::move
19+
20+
FairFileSourceBase::FairFileSourceBase(fairroot::detail::maybe_owning_ptr<TFile> file)
21+
: fRootFile(std::move(file))
22+
{
23+
if ((!fRootFile) || fRootFile->IsZombie()) {
24+
LOG(fatal) << "Error opening the Input file";
25+
}
26+
}
1827

1928
FairFileSourceBase::~FairFileSourceBase()
2029
{

fairroot/base/source/FairFileSourceBase.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/********************************************************************************
2-
* Copyright (C) 2022-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
2+
* Copyright (C) 2022-2024 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, *
@@ -9,6 +9,7 @@
99
#ifndef __FAIRROOT__FairFileSourceBase__
1010
#define __FAIRROOT__FairFileSourceBase__
1111

12+
#include "FairMemory.h"
1213
#include "FairSource.h"
1314

1415
#include <TFile.h>
@@ -33,18 +34,23 @@ class FairFileSourceBase : public FairSource
3334
Bool_t InitUnpackers() override { return kTRUE; }
3435
Bool_t ReInitUnpackers() override { return kTRUE; }
3536

37+
TFile* GetInFile() { return fRootFile.get(); }
38+
const TFile* GetRootFile() { return fRootFile.get(); }
39+
3640
TObjArray* GetListOfFolders() { return &fListFolder; }
3741
Bool_t CompareBranchList(TFile* fileHandle, TString inputLevel);
3842

3943
protected:
40-
FairFileSourceBase()
41-
: FairSource(){};
44+
FairFileSourceBase() = default;
45+
FairFileSourceBase(fairroot::detail::maybe_owning_ptr<TFile> file);
4246

4347
std::map<TString, std::list<TString>> fCheckInputBranches{}; //!
4448

4549
static bool ActivateObjectAnyImpl(TTree* source, void** obj, const std::type_info& info, const char* brname);
4650

4751
private:
52+
/**ROOT file*/
53+
fairroot::detail::maybe_owning_ptr<TFile> fRootFile; //!
4854
/** list of folders from all input (and friends) files*/
4955
TObjArray fListFolder{16}; //!
5056

fairroot/base/source/FairMixedSource.cxx

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/********************************************************************************
2-
* Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
2+
* Copyright (C) 2014-2024 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, *
@@ -31,11 +31,13 @@
3131
#include <cmath> // floor, fmod
3232
#include <fairlogger/Logger.h>
3333

34+
using fairroot::detail::maybe_owning_ptr;
35+
using fairroot::detail::non_owning;
36+
3437
FairMixedSource::FairMixedSource(TFile* f, const char* Title, UInt_t)
35-
: FairFileSourceBase()
38+
: FairFileSourceBase(maybe_owning_ptr<TFile>{f, non_owning})
3639
, fRootManager(0)
3740
, fInputTitle(Title)
38-
, fRootFile(f)
3941
, fFriendFileList()
4042
, fInputChainList()
4143
, fFriendTypeList()
@@ -74,19 +76,15 @@ FairMixedSource::FairMixedSource(TFile* f, const char* Title, UInt_t)
7476
, fRunIdFromSG(kFALSE)
7577
, fRunIdFromSG_identifier(0)
7678
{
77-
if ((!fRootFile) || fRootFile->IsZombie()) {
78-
LOG(fatal) << "Error opening the Input file";
79-
}
8079
LOG(info) << "FairMixedSource created------------";
8180

8281
fRootManager = FairRootManager::Instance();
8382
}
8483

8584
FairMixedSource::FairMixedSource(const TString* RootFileName, const char* Title, UInt_t)
86-
: FairFileSourceBase()
85+
: FairFileSourceBase(std::unique_ptr<TFile>{TFile::Open(RootFileName->Data())})
8786
, fRootManager(0)
8887
, fInputTitle(Title)
89-
, fRootFile(0)
9088
, fFriendFileList()
9189
, fInputChainList()
9290
, fFriendTypeList()
@@ -125,19 +123,14 @@ FairMixedSource::FairMixedSource(const TString* RootFileName, const char* Title,
125123
, fRunIdFromSG(kFALSE)
126124
, fRunIdFromSG_identifier(0)
127125
{
128-
fRootFile = TFile::Open(RootFileName->Data());
129-
if ((!fRootFile) || fRootFile->IsZombie()) {
130-
LOG(fatal) << "Error opening the Input file";
131-
}
132126
fRootManager = FairRootManager::Instance();
133127
LOG(info) << "FairMixedSource created------------";
134128
}
135129

136130
FairMixedSource::FairMixedSource(const TString RootFileName, const Int_t signalId, const char* Title, UInt_t)
137-
: FairFileSourceBase()
131+
: FairFileSourceBase(std::unique_ptr<TFile>{TFile::Open(RootFileName.Data())})
138132
, fRootManager(0)
139133
, fInputTitle(Title)
140-
, fRootFile(0)
141134
, fFriendFileList()
142135
, fInputChainList()
143136
, fFriendTypeList()
@@ -176,11 +169,6 @@ FairMixedSource::FairMixedSource(const TString RootFileName, const Int_t signalI
176169
, fRunIdFromSG(kFALSE)
177170
, fRunIdFromSG_identifier(0)
178171
{
179-
fRootFile = TFile::Open(RootFileName.Data());
180-
if ((!fRootFile) || fRootFile->IsZombie()) {
181-
LOG(fatal) << "Error opening the Input file";
182-
}
183-
184172
if (signalId == 0) {
185173
SetBackgroundFile(RootFileName);
186174
} else {
@@ -456,8 +444,8 @@ void FairMixedSource::SetBackgroundFile(TString name)
456444
if (name.IsNull()) {
457445
LOG(info) << "No background file defined.";
458446
}
459-
fRootFile = TFile::Open(name);
460-
if (fRootFile->IsZombie()) {
447+
fRootFile = std::unique_ptr<TFile>{TFile::Open(name)};
448+
if ((!fRootFile) || fRootFile->IsZombie()) {
461449
LOG(fatal) << "Error opening the Background file " << name.Data();
462450
}
463451
}

fairroot/base/source/FairMixedSource.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/********************************************************************************
2-
* Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
2+
* Copyright (C) 2014-2024 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, *
@@ -58,7 +58,6 @@ class FairMixedSource : public FairFileSourceBase
5858

5959
void FillEventHeader(FairEventHeader* feh) override;
6060

61-
const TFile* GetRootFile() { return fRootFile; }
6261
/** Add a friend file (input) by name)*/
6362

6463
Bool_t ActivateObject(TObject** obj, const char* BrName) override;
@@ -132,8 +131,6 @@ class FairMixedSource : public FairFileSourceBase
132131

133132
/** Title of input source, could be input, background or signal*/
134133
TString fInputTitle;
135-
/**ROOT file*/
136-
TFile* fRootFile;
137134
/** List of all files added with AddFriend */
138135
std::list<TString> fFriendFileList; //!
139136
std::list<TString> fInputChainList; //!

0 commit comments

Comments
 (0)