From 58cec28950569ceb1b756d9a13343da42e8c1d5b Mon Sep 17 00:00:00 2001 From: Florian Uhlig <f.uhlig@gsi.de> Date: Thu, 11 Feb 2021 18:02:28 +0100 Subject: [PATCH] core: Add gFile + gDirectory protection --- core/field/CbmBsField.cxx | 20 +++++++++++++---- core/field/CbmFieldMap.cxx | 22 ++++++++++++++----- core/field/CbmFieldMapDistorted.cxx | 19 +++++++++++----- reco/qa/CbmRecoQa.cxx | 9 ++++++++ .../generators/CbmUnigenGenerator.cxx | 8 +++++++ 5 files changed, 62 insertions(+), 16 deletions(-) diff --git a/core/field/CbmBsField.cxx b/core/field/CbmBsField.cxx index fbe724947b..c12c858d8e 100644 --- a/core/field/CbmBsField.cxx +++ b/core/field/CbmBsField.cxx @@ -182,16 +182,25 @@ void CbmBsField::CalculateMapFromBs(Int_t pNx, Int_t pNy, Int_t pNz) { void CbmBsField::writeBsRootfile(const char* name) // Write Field Splined { - TFile* oldfile = gFile; + /// Save old global file and folder pointer to avoid messing with FairRoot + TFile* oldFile = gFile; + TDirectory* oldDir = gDirectory; + TFile* f = new TFile(name, "RECREATE"); this->Write(); f->Close(); - if (oldfile) oldfile->cd(); + + /// Restore old global file and folder pointer to avoid messing with FairRoot + gFile = oldFile; + gDirectory = oldDir; } void CbmBsField::readBsRootfile(const char* name) // Read Field Splined { - TFile* oldfile = gFile; + /// Save old global file and folder pointer to avoid messing with FairRoot + TFile* oldFile = gFile; + TDirectory* oldDir = gDirectory; + TFile* f = new TFile(name, "READ"); if (f->IsZombie()) { cout << "-E- CbmBsField::readBsRootfile: can not read from file: " << endl; @@ -219,7 +228,10 @@ void CbmBsField::readBsRootfile(const char* name) // Read Field Splined fZ = new TArrayF(*fnew->GetZ()); f->Close(); - if (oldfile) oldfile->cd(); + + /// Restore old global file and folder pointer to avoid messing with FairRoot + gFile = oldFile; + gDirectory = oldDir; UX1 = fX->GetArray(); UX2 = fY->GetArray(); diff --git a/core/field/CbmFieldMap.cxx b/core/field/CbmFieldMap.cxx index 59c831e7c4..c3a7f90cf6 100644 --- a/core/field/CbmFieldMap.cxx +++ b/core/field/CbmFieldMap.cxx @@ -536,11 +536,18 @@ void CbmFieldMap::WriteAsciiFile(const char* fileName) { void CbmFieldMap::WriteRootFile(const char* fileName, const char* mapName) { CbmFieldMapData* data = new CbmFieldMapData(mapName, *this); - TFile* oldFile = gFile; + + /// Save old global file and folder pointer to avoid messing with FairRoot + TFile* oldFile = gFile; + TDirectory* oldDir = gDirectory; + TFile* file = new TFile(fileName, "RECREATE"); data->Write(); file->Close(); - if (oldFile) oldFile->cd(); + + /// Restore old global file and folder pointer to avoid messing with FairRoot + gFile = oldFile; + gDirectory = oldDir; } // ------------------------------------------------------------------------ @@ -696,9 +703,9 @@ void CbmFieldMap::ReadAsciiFile(const char* fileName) { // ------------- Read field map from ROOT file (private) --------------- void CbmFieldMap::ReadRootFile(const char* fileName, const char* mapName) { - - // Store gFile pointer - TFile* oldFile = gFile; + /// Save old global file and folder pointer to avoid messing with FairRoot + TFile* oldFile = gFile; + TDirectory* oldDir = gDirectory; // Open root file LOG(info) << "CbmFieldMap: Reading field map from ROOT file " << fileName; @@ -723,7 +730,10 @@ void CbmFieldMap::ReadRootFile(const char* fileName, const char* mapName) { // Close the root file and delete the data object file->Close(); delete data; - if (oldFile) oldFile->cd(); + + /// Restore old global file and folder pointer to avoid messing with FairRoot + gFile = oldFile; + gDirectory = oldDir; } // ------------------------------------------------------------------------ diff --git a/core/field/CbmFieldMapDistorted.cxx b/core/field/CbmFieldMapDistorted.cxx index 112ee0b9ce..afd117a1d1 100644 --- a/core/field/CbmFieldMapDistorted.cxx +++ b/core/field/CbmFieldMapDistorted.cxx @@ -265,8 +265,10 @@ void CbmFieldMapDistorted::SetFromParent(FairField* field) { // ----------- Read Distortion Formulas from Distortion File ------------------------------------------ void CbmFieldMapDistorted::ReadDistortionInformation(const char* filename) { - TFile* filesave = gFile; - gFile = 0; + /// Save old global file and folder pointer to avoid messing with FairRoot + TFile* oldFile = gFile; + TDirectory* oldDir = gDirectory; + if (filename) { if (strlen(filename)) { fDistortionFilename = filename; } } @@ -299,13 +301,16 @@ void CbmFieldMapDistorted::ReadDistortionInformation(const char* filename) { } } } - gFile = filesave; + /// Restore old global file and folder pointer to avoid messing with FairRoot + gFile = oldFile; + gDirectory = oldDir; } // ----------- Write Distortion Formulas to Distortion File ------------------------------------------ void CbmFieldMapDistorted::WriteDistortionInformation(const char* filename) { - TFile* filesave = gFile; - gFile = 0; + /// Save old global file and folder pointer to avoid messing with FairRoot + TFile* oldFile = gFile; + TDirectory* oldDir = gDirectory; if (filename) { if (strlen(filename)) { fDistortionFilename = filename; } @@ -336,7 +341,9 @@ void CbmFieldMapDistorted::WriteDistortionInformation(const char* filename) { } } } - gFile = filesave; + /// Restore old global file and folder pointer to avoid messing with FairRoot + gFile = oldFile; + gDirectory = oldDir; } // ---------------Getter and Setter for Distortion Formulas------------------------------ diff --git a/reco/qa/CbmRecoQa.cxx b/reco/qa/CbmRecoQa.cxx index d403b5c0f9..f535ff91b9 100644 --- a/reco/qa/CbmRecoQa.cxx +++ b/reco/qa/CbmRecoQa.cxx @@ -181,6 +181,9 @@ void CbmRecoQa::FinishEvent() { // --- Finish Task // Save Data in File void CbmRecoQa::FinishTask() { + /// Save old global file and folder pointer to avoid messing with FairRoot + TFile* oldFile = gFile; + TDirectory* oldDir = gDirectory; std::string filename = outname + ".qa.hists.root"; pullresfile = new TFile(filename.c_str(), "Recreate"); @@ -196,6 +199,12 @@ void CbmRecoQa::FinishTask() { } gDirectory->cd(".."); } + + pullresfile->Close(); + + /// Restore old global file and folder pointer to avoid messing with FairRoot + gFile = oldFile; + gDirectory = oldDir; } // --- Write Data in Historgrams, depending on detectorw diff --git a/sim/transport/generators/CbmUnigenGenerator.cxx b/sim/transport/generators/CbmUnigenGenerator.cxx index c9719e51e5..56e686a516 100644 --- a/sim/transport/generators/CbmUnigenGenerator.cxx +++ b/sim/transport/generators/CbmUnigenGenerator.cxx @@ -158,9 +158,17 @@ Bool_t CbmUnigenGenerator::Init() { } LOG(INFO) << GetName() << ": Mode " << ss.str(); + /// Save old global file and folder pointer to avoid messing with FairRoot + TFile* oldFile = gFile; + TDirectory* oldDir = gDirectory; // --- Try to open file fFile = new TFile(fFileName, "READ"); + + /// Restore old global file and folder pointer to avoid messing with FairRoot + gFile = oldFile; + gDirectory = oldDir; + if (!fFile->IsOpen()) { LOG(error) << GetName() << ": Could not open input file " << fFileName; return kFALSE; -- GitLab