diff --git a/core/detectors/trd/CbmTrdParSetGas.cxx b/core/detectors/trd/CbmTrdParSetGas.cxx
index 0176e21f9e981da0460529633add2b5550271e16..bd58b60e5a421baada34822a4c616605abff7671 100644
--- a/core/detectors/trd/CbmTrdParSetGas.cxx
+++ b/core/detectors/trd/CbmTrdParSetGas.cxx
@@ -34,11 +34,19 @@ Bool_t CbmTrdParSetGas::getParams(FairParamList* l) {
   if (!l->fill("RepoDrift", repo, 100)) return kFALSE;
   if (!l->fill("RepoPid", pid, 100)) return kFALSE;
 
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   TDirectory* cwd(gDirectory);
-  if (!TFile::Open(Form("%s/%s", gSystem->Getenv("VMCWORKDIR"), repo))) {
+
+  TFile* infile = TFile::Open(Form("%s/%s", gSystem->Getenv("VMCWORKDIR"), repo));
+  if (!infile->IsOpen()) {
     LOG(error) << "Missing TRD DriftMap Repository : " << repo;
+    gFile      = oldFile;
+    gDirectory = oldDir;
     return kFALSE;
-  } else {
+  }
+  else {
     LOG(debug) << "TRD DriftMap Repository : " << gFile->GetName();
     LOG(debug) << "TRD PID Repository : " << pid;
   }
@@ -72,7 +80,12 @@ Bool_t CbmTrdParSetGas::getParams(FairParamList* l) {
   }
   so->Delete();
   delete so;
-  gFile->Close();
+
+  infile->Close();
+  delete infile;
+
+  gFile      = oldFile;
+  gDirectory = oldDir;
 
   return kTRUE;
 }
diff --git a/core/detectors/trd/CbmTrdRadiator.cxx b/core/detectors/trd/CbmTrdRadiator.cxx
index c8a59bd1d686f011f8e7e69518df8a792c4a6c34..12df2100269f8a24afb5de56bb09d56dfe4348da 100644
--- a/core/detectors/trd/CbmTrdRadiator.cxx
+++ b/core/detectors/trd/CbmTrdRadiator.cxx
@@ -9,6 +9,7 @@
 
 #include <FairLogger.h>  // for Logger, LOG
 
+#include <TDirectory.h>  // for TDirectory
 #include <TFile.h>        // for TFile, gFile
 #include <TGeoManager.h>  // for TGeoManager, gGeoManager
 #include <TH1.h>          // for TH1D
@@ -317,6 +318,9 @@ void CbmTrdRadiator::Init()
 
     ProduceSpectra();
 
+    TFile* oldFile     = gFile;
+    TDirectory* oldDir = gDirectory;
+
     TFile* f1 = new TFile("TRhistos.root", "recreate");
 
     for (Int_t i = 0; i < fNMom; i++) {
@@ -324,6 +328,9 @@ void CbmTrdRadiator::Init()
     }
     f1->Close();
     f1->Delete();
+
+    gFile      = oldFile;
+    gDirectory = oldDir;
   }
 }
 
diff --git a/reco/detectors/trd/pid/CbmTrdElectronsTrainAnn.cxx b/reco/detectors/trd/pid/CbmTrdElectronsTrainAnn.cxx
index 1d8f9c39e6ef4f81b959ed06edde2d28ac17d11f..afdfda65d0d71514803040221d0719de1a24a536 100644
--- a/reco/detectors/trd/pid/CbmTrdElectronsTrainAnn.cxx
+++ b/reco/detectors/trd/pid/CbmTrdElectronsTrainAnn.cxx
@@ -196,11 +196,21 @@ void CbmTrdElectronsTrainAnn::Finish() {
   Draw();
 
   if (fOutputDir != "") { gSystem->mkdir(fOutputDir.c_str(), true); }
+
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   TFile* f =
     new TFile(string(fOutputDir + "/trd_elid_hist.root").c_str(), "RECREATE");
   for (unsigned int i = 0; i < fHists.size(); i++) {
     fHists[i]->Write();
   }
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
+
   f->Close();
 }
 
@@ -221,6 +231,10 @@ void CbmTrdElectronsTrainAnn::FillElossVectorReal() {
           "Set input file for beam data and histogram names!");
   }
 
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   TFile* file     = new TFile(fBeamDataFile.c_str(), "READ");
   TH1F* hPion     = (TH1F*) file->Get(fBeamDataPiHist.c_str())->Clone();
   TH1F* hElectron = (TH1F*) file->Get(fBeamDataElHist.c_str())->Clone();
@@ -244,6 +258,10 @@ void CbmTrdElectronsTrainAnn::FillElossVectorReal() {
       }
     }
   }
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 }
 
 void CbmTrdElectronsTrainAnn::FillElossVectorSim() {
diff --git a/reco/detectors/trd/pid/CbmTrdSetTracksPidLike.cxx b/reco/detectors/trd/pid/CbmTrdSetTracksPidLike.cxx
index 3b4bfe1bbdb1202fbd9243182e7a55f80eac1701..8f82ab1a037a1357c9151b21830072a6e0417de0 100644
--- a/reco/detectors/trd/pid/CbmTrdSetTracksPidLike.cxx
+++ b/reco/detectors/trd/pid/CbmTrdSetTracksPidLike.cxx
@@ -64,6 +64,10 @@ Bool_t CbmTrdSetTracksPidLike::ReadData()
     fFileName = fTrdGas->GetFileName("Like");
   }
 
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   // Open ROOT file with the histograms
   TFile* histFile = new TFile(fFileName, "READ");
   if (!histFile || !histFile->IsOpen()) {
@@ -204,6 +208,10 @@ Bool_t CbmTrdSetTracksPidLike::ReadData()
     fHistdEdx->AddAt(hist, particle);
   }
 
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
+
   /// clean up
   histFile->Close();
   delete histFile;
diff --git a/reco/detectors/trd/qa/CbmTrdHitDensityQa.cxx b/reco/detectors/trd/qa/CbmTrdHitDensityQa.cxx
index b17cdcb96eae7d16442d8b168c7bef6d96b7b659..654a174aaead3df4b9153501d6171643f233e2fb 100644
--- a/reco/detectors/trd/qa/CbmTrdHitDensityQa.cxx
+++ b/reco/detectors/trd/qa/CbmTrdHitDensityQa.cxx
@@ -138,7 +138,7 @@ InitStatus CbmTrdHitDensityQa::Init() {
     cout << "                             Task will be inactive" << endl;
     //return kERROR;
   }
-  /* 
+  /*
   TString origpath = gDirectory->GetPath();
   printf ("\n%s\n",origpath.Data());
   TString newpath = origpath;
@@ -327,7 +327,7 @@ void CbmTrdHitDensityQa::Exec(Option_t*) {
 	  fUsedDigiMap[neighbourAddress] = iDigi;
 	  }
 	  }
-	  if (iRow < fModuleDigi->GetNofRows()-1){ // Only cross like neighbour trigger 
+	  if (iRow < fModuleDigi->GetNofRows()-1){ // Only cross like neighbour trigger
 	  if (local_Row+1 > fModuleDigi->GetNofRowsInSector(iSec)-1)
 	  neighbourAddress = CbmTrdAddress::GetAddress(fLayer, CbmTrdAddress::GetModuleId(moduleAddress), iSec+1, 0, iCol);
 	  else
@@ -409,8 +409,12 @@ void CbmTrdHitDensityQa::Finish() {
   TString title, name;
   std::map<Int_t, TCanvas*> LayerMap;
   std::map<Int_t, TCanvas*>::iterator LayerMapIt;
+
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile   = gFile;
   TString origpath = gDirectory->GetPath();
   printf("\n%s\n", origpath.Data());
+
   TString newpath = origpath;
   printf("fPlotResults: %i\n", (Int_t) fPlotResults);
   if (fPlotResults) {
@@ -761,6 +765,9 @@ void CbmTrdHitDensityQa::Finish() {
 
   gDirectory->Cd("..");
   tempFile->Close();
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile = oldFile;
   gDirectory->Cd(origpath);
   gDirectory->pwd();
   delete util;
diff --git a/reco/detectors/trd/qa/CbmTrdOccupancyQa.cxx b/reco/detectors/trd/qa/CbmTrdOccupancyQa.cxx
index d3d46d715a15b0726e6f1c2b3f52a92babcfb8b8..bc9b7d5e6671813733bb58fdcf8d79fd831f56c2 100644
--- a/reco/detectors/trd/qa/CbmTrdOccupancyQa.cxx
+++ b/reco/detectors/trd/qa/CbmTrdOccupancyQa.cxx
@@ -178,7 +178,12 @@ void CbmTrdOccupancyQa::Exec(Option_t*) {
   printf("TriggerThreshold: %.2E\n", fTriggerThreshold);
   printf("NeigbourReadout:%i\n", Int_t(fNeigbourReadout));
   //  Bool_t debug = false;
-  //TFile *outFile = new TFile("data/CbmTrdOccupancyQa.root","UPDATE","output of CbmTrdOccupancyQa");
+
+  // /// Save old global file and folder pointer to avoid messing with FairRoot
+  // TFile* oldFile     = gFile;
+  // TDirectory* oldDir = gDirectory;
+  // TFile *outFile = new TFile("data/CbmTrdOccupancyQa.root","UPDATE","output of CbmTrdOccupancyQa");
+
   TStopwatch timer;
   timer.Start();
   Int_t nEntries = CbmDigiManager::Instance()->GetNofDigis(ECbmModuleId::kTrd);
@@ -423,6 +428,10 @@ void CbmTrdOccupancyQa::CopyEvent2MemoryMap() {
   }
 }
 void CbmTrdOccupancyQa::SwitchToMergedFile() {
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   TString filename = "data/result.root";
   TFile* Target    = new TFile(filename, "READ");
   if (Target) {
@@ -440,11 +449,19 @@ void CbmTrdOccupancyQa::SwitchToMergedFile() {
   } else {
     cout << "No merged data/result.root found" << endl;
   }
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 }
 void CbmTrdOccupancyQa::CreateLayerView() {
   if (fPlotMergedResults) SwitchToMergedFile();
+
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile   = gFile;
   TString origpath = gDirectory->GetPath();
   printf("\n%s\n", origpath.Data());
+
   TString newpath = origpath;
   newpath.ReplaceAll("eds", "oc_qa");
   newpath.ReplaceAll(":/", "");
@@ -644,6 +661,9 @@ void CbmTrdOccupancyQa::CreateLayerView() {
   c->Close();
 
   tempFile->Close();
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile = oldFile;
   gDirectory->Cd(origpath);
   gDirectory->pwd();
 }
@@ -654,8 +674,11 @@ void CbmTrdOccupancyQa::SetTriggerThreshold(Double_t triggerthreshold) {
   fTriggerThreshold = triggerthreshold;
 }
 void CbmTrdOccupancyQa::SaveHistos2File() {
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile   = gFile;
   TString origpath = gDirectory->GetPath();
   printf("\n%s\n", origpath.Data());
+
   TString newpath = origpath;
   newpath.ReplaceAll("eds", "oc_qa");
   newpath.ReplaceAll(":/", "");
@@ -688,7 +711,7 @@ void CbmTrdOccupancyQa::SaveHistos2File() {
   }
   */
   /*
-    if (!gDirectory->Cd("Module2D")) 
+    if (!gDirectory->Cd("Module2D"))
     gDirectory->mkdir("Module2D");
     gDirectory->Cd("Module2D");
     for (fModuleOccupancyMapIt = fModuleOccupancyMap.begin();
@@ -719,6 +742,9 @@ void CbmTrdOccupancyQa::SaveHistos2File() {
   //outFile->Close();
 
   tempFile->Close();
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile = oldFile;
   gDirectory->Cd(origpath);
   gDirectory->pwd();
 }
diff --git a/reco/detectors/trd/qa/CbmTrdQa.cxx b/reco/detectors/trd/qa/CbmTrdQa.cxx
index adcdfc7cd7cfbaf84af5a36e6d52004cfc1fd311..6023e1d3a9fcf52a2569fb63211b44ccde3f6927 100644
--- a/reco/detectors/trd/qa/CbmTrdQa.cxx
+++ b/reco/detectors/trd/qa/CbmTrdQa.cxx
@@ -1125,8 +1125,8 @@ void CbmTrdQa::Exec(Option_t*) {
       Double_t trackLength = GetTrackLength(point);
       /*
 	TMath::Sqrt(
-	(point->GetXOut() - point->GetXIn()) * (point->GetXOut() - point->GetXIn()) + 
-	(point->GetYOut() - point->GetYIn()) * (point->GetYOut() - point->GetYIn()) + 
+	(point->GetXOut() - point->GetXIn()) * (point->GetXOut() - point->GetXIn()) +
+	(point->GetYOut() - point->GetYIn()) * (point->GetYOut() - point->GetYIn()) +
 	(point->GetZOut() - point->GetZIn()) * (point->GetZOut() - point->GetZIn())
 	);
       */
@@ -1729,8 +1729,11 @@ void CbmTrdQa::SetTriangularPads(Bool_t triangles) {
 
 
 void CbmTrdQa::SaveHistos() {
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile   = gFile;
   TString origpath = gDirectory->GetPath();
   printf("\n%s\n", origpath.Data());
+
   TString newpath = origpath;
   newpath.ReplaceAll("eds", "trd_qa");
   newpath.ReplaceAll(":/", "");
@@ -2000,6 +2003,9 @@ void CbmTrdQa::SaveHistos() {
   delete l;
   gDirectory->Cd("..");
   tempFile->Close();
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile = oldFile;
   gDirectory->Cd(origpath);
   gDirectory->pwd();
 }
@@ -2106,8 +2112,11 @@ void CbmTrdQa::CreateLayerView(std::map<Int_t, TH1*>& Map,
                                Double_t fmax,
                                Double_t fmin,
                                Bool_t logScale) {
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile   = gFile;
   TString origpath = gDirectory->GetPath();
   printf("\n%s\n", origpath.Data());
+
   TString newpath = origpath;
   newpath.ReplaceAll("eds", "trd_qa");
   newpath.ReplaceAll(":/", "");
@@ -2195,13 +2204,19 @@ void CbmTrdQa::CreateLayerView(std::map<Int_t, TH1*>& Map,
   gDirectory->Cd("..");
 
   tempFile->Close();
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile = oldFile;
   gDirectory->Cd(origpath);
   gDirectory->pwd();
 }
 
 void CbmTrdQa::CreateLayerView() {
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile   = gFile;
   TString origpath = gDirectory->GetPath();
   printf("\n%s\n", origpath.Data());
+
   TString newpath = origpath;
   newpath.ReplaceAll("eds", "trd_qa");
   newpath.ReplaceAll(":/", "");
@@ -3123,6 +3138,9 @@ void CbmTrdQa::CreateLayerView() {
   gDirectory->Cd("..");
 
   tempFile->Close();
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile = oldFile;
   gDirectory->Cd(origpath);
   gDirectory->pwd();
 }
diff --git a/sim/detectors/trd/CbmTrdModuleSimR.cxx b/sim/detectors/trd/CbmTrdModuleSimR.cxx
index 8dcb6c278d85840c25c534f0a044f3e62299b445..4eba11aaffa5f5ad28fe5a1fc34fd89db0cefc3b 100644
--- a/sim/detectors/trd/CbmTrdModuleSimR.cxx
+++ b/sim/detectors/trd/CbmTrdModuleSimR.cxx
@@ -84,7 +84,9 @@ CbmTrdModuleSimR::CbmTrdModuleSimR(Int_t mod, Int_t ly, Int_t rot)
   if (!fPulseSwitch && CbmTrdDigitizer::IsTimeBased()) fCollectTime = 200;
   SetNameTitle(Form("TrdSimR%d", mod), "Simulator for rectangular read-out.");
 
-  TFile* oldFile = gFile;
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
 
   TString dir      = getenv("VMCWORKDIR");
   TString filename = dir + "/parameters/trd/FeatureExtractionLookup.root";
@@ -93,7 +95,10 @@ CbmTrdModuleSimR::CbmTrdModuleSimR(Int_t mod, Int_t ly, Int_t rot)
   fDriftTime->SetDirectory(0);
   f->Close();
 
-  gFile   = oldFile;
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
+
   oldFile = nullptr;
   delete oldFile;
 
diff --git a/sim/detectors/trd/qa/CbmTrdHitRateFastQa.cxx b/sim/detectors/trd/qa/CbmTrdHitRateFastQa.cxx
index 9691237dc49a55579db647e34627a72721b4bb8b..63fe73ab401cb60ed6888a84cb6c721471800359 100644
--- a/sim/detectors/trd/qa/CbmTrdHitRateFastQa.cxx
+++ b/sim/detectors/trd/qa/CbmTrdHitRateFastQa.cxx
@@ -252,9 +252,18 @@ void CbmTrdHitRateFastQa::Exec(Option_t*) {
 
   fStation = 0;
   fLayer   = 0;
+
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   tFile    = new TFile(
     "CbmTrdHitRateFastQa.root", "RECREATE", " ROOT file with histograms");
 
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
+
   Char_t name[50];
   Char_t title[50];
 
@@ -1453,8 +1462,8 @@ Double_t CbmTrdHitRateFastQa::CalcHitRate(HitRateGeoPara2* GeoPara,
       r     = sqrt(pow(x / 1.5, 2) + pow(y, 2));
       alpha = atan(r / z) * 1000.;
       /* //Fit without errors
-	   HitRate += 
-	   exp(4.54156e00 + -8.47377e-03 * alpha) + 
+	   HitRate +=
+	   exp(4.54156e00 + -8.47377e-03 * alpha) +
 	   exp(2.40005e01 + -1.19541e-02 * alpha) /
 	   (z * z)
 	   ;
diff --git a/sim/detectors/trd/qa/CbmTrdHitRateQa.cxx b/sim/detectors/trd/qa/CbmTrdHitRateQa.cxx
index 26c83aa882968a5e013f888329104cff170f2fe0..2f53469391c6a475694bcf5cee67295d2d4a49d7 100644
--- a/sim/detectors/trd/qa/CbmTrdHitRateQa.cxx
+++ b/sim/detectors/trd/qa/CbmTrdHitRateQa.cxx
@@ -222,9 +222,18 @@ void CbmTrdHitRateQa::Exec(Option_t*) {
 
   fStation = 0;
   fLayer   = 0;
+
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   tFile =
     new TFile("CbmTrdHitRateQa.root", "RECREATE", " ROOT file with histograms");
 
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
+
   TH1F* h1HitPad     = NULL;
   TH2F* h2Layer      = NULL;
   TH2F* h2Topview[3] = {NULL, NULL, NULL};
@@ -1038,8 +1047,8 @@ Double_t CbmTrdHitRateQa::CalcHitRate(HitRateGeoPara* GeoPara,
       r     = sqrt(pow(x, 2) + pow(y, 2));
       alpha = atan(r / z) * 1000.;
       /* //Fit without errors
-      HitRate += 
-	exp(4.54156e00 + -8.47377e-03 * alpha) + 
+      HitRate +=
+	exp(4.54156e00 + -8.47377e-03 * alpha) +
 	exp(2.40005e01 + -1.19541e-02 * alpha) /
 	(z * z)
 	;