diff --git a/core/field/CbmBsField.cxx b/core/field/CbmBsField.cxx
index fbe724947ba0326091ab20c755703a3f68d70c2a..c12c858d8ebc0edddd8ce68bf1dc8db1030637e4 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 59c831e7c4ba2cc00163d9853516e7c0da71ad3d..c3a7f90cf6df05e04d7aab913849790d8b377c30 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 112ee0b9ce20660c1c3a1060b859be7701a0fd8e..afd117a1d1809c6b3d2a43d53076365e80d873e3 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 d403b5c0f93701e1d73745cdb6d69a0032fd24ea..f535ff91b94a30e6b139c98b36d392026bfa3f6d 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 c9719e51e5d63d350da4e749b776fe12fc4d29de..56e686a5166f9b40707dbeff6fd4badf385b2197 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;