Skip to content

Commit

Permalink
fix: Do not leak TFile in Fair{File,Mixed}Source
Browse files Browse the repository at this point in the history
And refactor into FairFileSourceBase.
  • Loading branch information
ChristianTackeGSI committed Mar 4, 2024
1 parent a3dbb00 commit fb5748b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
18 changes: 9 additions & 9 deletions fairroot/base/source/FairFileSource.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@
#include <typeinfo>
#include <vector>

using fairroot::detail::maybe_owning_ptr;
using fairroot::detail::non_owning;

FairFileSource::FairFileSource(TFile* f, const char* Title, UInt_t)
: FairFileSourceBase()
, fInputTitle(Title)
, fRootFile(f)
, fFriendFileList()
, fInputChainList()
, fFriendTypeList()
Expand Down Expand Up @@ -73,6 +75,7 @@ FairFileSource::FairFileSource(TFile* f, const char* Title, UInt_t)
, fEventMeanTime(0.)
, fCheckFileLayout(kTRUE)
{
fRootFile = maybe_owning_ptr<TFile>{f, non_owning};
if ((!fRootFile) || fRootFile->IsZombie()) {
LOG(fatal) << "Error opening the Input file";
}
Expand All @@ -82,7 +85,6 @@ FairFileSource::FairFileSource(TFile* f, const char* Title, UInt_t)
FairFileSource::FairFileSource(const TString* RootFileName, const char* Title, UInt_t)
: FairFileSourceBase()
, fInputTitle(Title)
, fRootFile(0)
, fFriendFileList()
, fInputChainList()
, fFriendTypeList()
Expand Down Expand Up @@ -110,7 +112,7 @@ FairFileSource::FairFileSource(const TString* RootFileName, const char* Title, U
, fEventMeanTime(0.)
, fCheckFileLayout(kTRUE)
{
fRootFile = TFile::Open(RootFileName->Data());
fRootFile = std::unique_ptr<TFile>{TFile::Open(RootFileName->Data())};
if ((!fRootFile) || fRootFile->IsZombie()) {
LOG(fatal) << "Error opening the Input file";
}
Expand All @@ -120,7 +122,6 @@ FairFileSource::FairFileSource(const TString* RootFileName, const char* Title, U
FairFileSource::FairFileSource(const TString RootFileName, const char* Title, UInt_t)
: FairFileSourceBase()
, fInputTitle(Title)
, fRootFile(0)
, fFriendFileList()
, fInputChainList()
, fFriendTypeList()
Expand Down Expand Up @@ -148,7 +149,7 @@ FairFileSource::FairFileSource(const TString RootFileName, const char* Title, UI
, fEventMeanTime(0.)
, fCheckFileLayout(kTRUE)
{
fRootFile = TFile::Open(RootFileName.Data());
fRootFile = std::unique_ptr<TFile>{TFile::Open(RootFileName.Data())};
if ((!fRootFile) || fRootFile->IsZombie()) {
LOG(fatal) << "Error opening the Input file";
}
Expand Down Expand Up @@ -294,9 +295,8 @@ Bool_t FairFileSource::Init()

void FairFileSource::SetInTree(TTree* tempTree)
{
fInTree = nullptr;
fInTree = tempTree;
fRootFile = tempTree->GetCurrentFile();
fRootFile = maybe_owning_ptr<TFile>{tempTree->GetCurrentFile(), non_owning};
fInChain->Reset();
IsInitialized = kFALSE;
Init();
Expand Down Expand Up @@ -581,8 +581,8 @@ Bool_t FairFileSource::ActivateObjectAny(void** obj, const std::type_info& info,

void FairFileSource::SetInputFile(TString name)
{
fRootFile = TFile::Open(name.Data());
if (fRootFile->IsZombie()) {
fRootFile = std::unique_ptr<TFile>{TFile::Open(name.Data())};
if ((!fRootFile) || fRootFile->IsZombie()) {
LOG(fatal) << "Error opening the Input file";
}
LOG(info) << "FairFileSource set------------";
Expand Down
7 changes: 2 additions & 5 deletions fairroot/base/source/FairFileSource.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* Copyright (C) 2014-2024 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
Expand Down Expand Up @@ -52,9 +52,9 @@ class FairFileSource : public FairFileSourceBase
void ReadBranchEvent(const char* BrName) override;
/**Read specific tree entry on one branch**/
void ReadBranchEvent(const char* BrName, Int_t Entry) override;

void FillEventHeader(FairEventHeader* feh) override;

const TFile* GetRootFile() { return fRootFile; }
/** Add a friend file (input) by name)*/
void AddFriend(TString FileName);
/**Add ROOT file to input, the file will be chained to already added files*/
Expand All @@ -65,7 +65,6 @@ class FairFileSource : public FairFileSourceBase
void CreateNewFriendChain(TString inputFile, TString inputLevel);
TTree* GetInTree() { return fInChain->GetTree(); }
TChain* GetInChain() { return fInChain; }
TFile* GetInFile() { return fRootFile; }
void CloseInFile()
{
if (fRootFile) {
Expand Down Expand Up @@ -112,8 +111,6 @@ class FairFileSource : public FairFileSourceBase
private:
/** Title of input source, could be input, background or signal*/
TString fInputTitle;
/**ROOT file*/
TFile* fRootFile;
/** List of all files added with AddFriend */
std::list<TString> fFriendFileList; //!
std::list<TString> fInputChainList; //!
Expand Down
11 changes: 8 additions & 3 deletions fairroot/base/source/FairFileSourceBase.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (C) 2022-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* Copyright (C) 2022-2024 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
Expand All @@ -9,6 +9,7 @@
#ifndef __FAIRROOT__FairFileSourceBase__
#define __FAIRROOT__FairFileSourceBase__

#include "FairMemory.h"
#include "FairSource.h"

#include <TFile.h>
Expand All @@ -33,18 +34,22 @@ class FairFileSourceBase : public FairSource
Bool_t InitUnpackers() override { return kTRUE; }
Bool_t ReInitUnpackers() override { return kTRUE; }

TFile* GetInFile() { return fRootFile.get(); }
const TFile* GetRootFile() { return fRootFile.get(); }

TObjArray* GetListOfFolders() { return &fListFolder; }
Bool_t CompareBranchList(TFile* fileHandle, TString inputLevel);

protected:
FairFileSourceBase()
: FairSource(){};
FairFileSourceBase() = default;

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

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

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

Expand Down
17 changes: 9 additions & 8 deletions fairroot/base/source/FairMixedSource.cxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* Copyright (C) 2014-2024 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
Expand Down Expand Up @@ -31,11 +31,13 @@
#include <cmath> // floor, fmod
#include <fairlogger/Logger.h>

using fairroot::detail::maybe_owning_ptr;
using fairroot::detail::non_owning;

FairMixedSource::FairMixedSource(TFile* f, const char* Title, UInt_t)
: FairFileSourceBase()
, fRootManager(0)
, fInputTitle(Title)
, fRootFile(f)
, fFriendFileList()
, fInputChainList()
, fFriendTypeList()
Expand Down Expand Up @@ -74,6 +76,7 @@ FairMixedSource::FairMixedSource(TFile* f, const char* Title, UInt_t)
, fRunIdFromSG(kFALSE)
, fRunIdFromSG_identifier(0)
{
fRootFile = maybe_owning_ptr<TFile>{f, non_owning};
if ((!fRootFile) || fRootFile->IsZombie()) {
LOG(fatal) << "Error opening the Input file";
}
Expand All @@ -86,7 +89,6 @@ FairMixedSource::FairMixedSource(const TString* RootFileName, const char* Title,
: FairFileSourceBase()
, fRootManager(0)
, fInputTitle(Title)
, fRootFile(0)
, fFriendFileList()
, fInputChainList()
, fFriendTypeList()
Expand Down Expand Up @@ -125,7 +127,7 @@ FairMixedSource::FairMixedSource(const TString* RootFileName, const char* Title,
, fRunIdFromSG(kFALSE)
, fRunIdFromSG_identifier(0)
{
fRootFile = TFile::Open(RootFileName->Data());
fRootFile = std::unique_ptr<TFile>{TFile::Open(RootFileName->Data())};
if ((!fRootFile) || fRootFile->IsZombie()) {
LOG(fatal) << "Error opening the Input file";
}
Expand All @@ -137,7 +139,6 @@ FairMixedSource::FairMixedSource(const TString RootFileName, const Int_t signalI
: FairFileSourceBase()
, fRootManager(0)
, fInputTitle(Title)
, fRootFile(0)
, fFriendFileList()
, fInputChainList()
, fFriendTypeList()
Expand Down Expand Up @@ -176,7 +177,7 @@ FairMixedSource::FairMixedSource(const TString RootFileName, const Int_t signalI
, fRunIdFromSG(kFALSE)
, fRunIdFromSG_identifier(0)
{
fRootFile = TFile::Open(RootFileName.Data());
fRootFile = std::unique_ptr<TFile>{TFile::Open(RootFileName.Data())};
if ((!fRootFile) || fRootFile->IsZombie()) {
LOG(fatal) << "Error opening the Input file";
}
Expand Down Expand Up @@ -456,8 +457,8 @@ void FairMixedSource::SetBackgroundFile(TString name)
if (name.IsNull()) {
LOG(info) << "No background file defined.";
}
fRootFile = TFile::Open(name);
if (fRootFile->IsZombie()) {
fRootFile = std::unique_ptr<TFile>{TFile::Open(name)};
if ((!fRootFile) || fRootFile->IsZombie()) {
LOG(fatal) << "Error opening the Background file " << name.Data();
}
}
Expand Down
5 changes: 1 addition & 4 deletions fairroot/base/source/FairMixedSource.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* Copyright (C) 2014-2024 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
Expand Down Expand Up @@ -58,7 +58,6 @@ class FairMixedSource : public FairFileSourceBase

void FillEventHeader(FairEventHeader* feh) override;

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

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

/** Title of input source, could be input, background or signal*/
TString fInputTitle;
/**ROOT file*/
TFile* fRootFile;
/** List of all files added with AddFriend */
std::list<TString> fFriendFileList; //!
std::list<TString> fInputChainList; //!
Expand Down

0 comments on commit fb5748b

Please sign in to comment.