diff --git a/MQ/eTOF/CbmDeviceEventBuilderEtofStar2019.cxx b/MQ/eTOF/CbmDeviceEventBuilderEtofStar2019.cxx
index b18312752435a33bc67a8cea4bf18071f47d4b6e..7053749a34446baaa247923367e721552b5b4ff4 100644
--- a/MQ/eTOF/CbmDeviceEventBuilderEtofStar2019.cxx
+++ b/MQ/eTOF/CbmDeviceEventBuilderEtofStar2019.cxx
@@ -562,11 +562,13 @@ void CbmDeviceEventBuilderEtofStar2019::Finish() {
     std::vector<std::pair<TNamed*, std::string>> vHistos =
       fEventBuilderAlgo->GetHistoVector();
 
+    /// Save old global file and folder pointer to avoid messing with FairRoot
+    TFile* oldFile     = gFile;
+    TDirectory* oldDir = gDirectory;
+
     /// (Re-)Create ROOT file to store the histos
-    TDirectory* oldDir = NULL;
-    TFile* histoFile   = NULL;
-    // Store current directory position to allow restore later
-    oldDir = gDirectory;
+    TFile* histoFile = nullptr;
+
     // open separate histo file in recreate mode
     histoFile = new TFile("data/eventBuilderMonHist.root", "RECREATE");
     histoFile->cd();
@@ -583,8 +585,10 @@ void CbmDeviceEventBuilderEtofStar2019::Finish() {
       histoFile->cd();
     }  // for( UInt_t uHisto = 0; uHisto < vHistos.size(); ++uHisto )
 
-    // Restore original directory position
-    oldDir->cd();
+    /// Restore old global file and folder pointer to avoid messing with FairRoot
+    gFile      = oldFile;
+    gDirectory = oldDir;
+
     histoFile->Close();
   }  // if( kTRUE == fbMonitorMode )
 }
diff --git a/MQ/hitbuilder/CbmDeviceHitBuilderTof.cxx b/MQ/hitbuilder/CbmDeviceHitBuilderTof.cxx
index 1d73748e43b78d7d406e79ac5b0cd41d3b9ad60e..c85c0c2a146683c2d73b2bdb56edeb51770c7b01 100644
--- a/MQ/hitbuilder/CbmDeviceHitBuilderTof.cxx
+++ b/MQ/hitbuilder/CbmDeviceHitBuilderTof.cxx
@@ -372,11 +372,17 @@ Bool_t CbmDeviceHitBuilderTof::InitRootOutput()
     rootMgr->WriteFolder();
     LOG(info) << "Initialized outTree with rootMgr at " << rootMgr;
     /*
+    /// Save old global file and folder pointer to avoid messing with FairRoot
+    TFile* oldFile     = gFile;
+    TDirectory* oldDir = gDirectory;
     fOutRootFile = new TFile(fOutRootFileName,"recreate");
     fRootEvent   = new TTree("CbmEvent","Cbm Event");
     fRootEvent->Branch("CbmDigi",fTofCalDigisColl);
     LOG(info)<<"Open Root Output file " << fOutRootFileName;
     fRootEvent->Write();
+    /// Restore old global file and folder pointer to avoid messing with FairRoot
+    gFile      = oldFile;
+    gDirectory = oldDir;
     */
   }
   return kTRUE;
@@ -558,22 +564,22 @@ bool CbmDeviceHitBuilderTof::HandleData(FairMQParts& parts, int /*index*/)
   const Int_t iNDigiIn=100;
   std::array<CbmTofDigi,iNDigiIn> *aTofDigi = static_cast<std::array<CbmTofDigi,iNDigiIn>*>(msg->GetData());
   for (int iDigi=0; iDigi<fiNDigiIn; iDigi++) {
-    LOG(info) << "#" << iDigi << " " <<(*aTofDigi)[iDigi].ToString();  
+    LOG(info) << "#" << iDigi << " " <<(*aTofDigi)[iDigi].ToString();
   }
-  
+
 
   pDigiIn=static_cast<CbmTofDigi*> (msg->GetData());
   CbmTofDigi*  pDigi=pDigiIn;
   CbmTofDigi  aTofDigi[fiNDigiIn];
- 
+
 
   for (int iDigi=0; iDigi<fiNDigiIn; iDigi++) {
-  //aTofDigi[iDigi] = *pDigi++; 
-    aTofDigi[iDigi] = *pDigi; 
-    fvDigiIn[iDigi] = *pDigi; 
-    LOG(info) << "#" << iDigi << " at "<<pDigi<< " " <<aTofDigi[iDigi].ToString();  
+  //aTofDigi[iDigi] = *pDigi++;
+    aTofDigi[iDigi] = *pDigi;
+    fvDigiIn[iDigi] = *pDigi;
+    LOG(info) << "#" << iDigi << " at "<<pDigi<< " " <<aTofDigi[iDigi].ToString();
     // LOG(info) << "#" << iDigi << " at "<<pDigi<< " " <<pDigi->ToString();   // does not work ???
-    pDigi++; 
+    pDigi++;
   }
   */
 
@@ -748,10 +754,10 @@ Bool_t CbmDeviceHitBuilderTof::InitCalibParameter()
   }
   LOG(info) << "defaults set";
 
-  TDirectory* oldir = gDirectory;  // <= To prevent histos from being sucked in by the param file of the TRootManager!
-  /*
-  gROOT->cd(); // <= To prevent histos from being sucked in by the param file of the TRootManager !
-  */
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
 
   if (0 < fCalMode) {
     fCalParFileName = fDigiBdfPar->GetCalibFileName();
@@ -780,7 +786,7 @@ Bool_t CbmDeviceHitBuilderTof::InitCalibParameter()
       // copy Histo to memory
       TDirectory* curdir = gDirectory;
       if (NULL != hSvel) {
-        gDirectory->cd(oldir->GetPath());
+        gDirectory->cd(oldDir->GetPath());
         //TProfile *hSvelmem =
         //(TProfile*) hSvel->Clone();
         gDirectory->cd(curdir->GetPath());
@@ -792,7 +798,7 @@ Bool_t CbmDeviceHitBuilderTof::InitCalibParameter()
       for (Int_t iPar = 0; iPar < 4; iPar++) {
         TProfile* hFparcur = (TProfile*) gDirectory->FindObjectAny(Form("cl_SmT%01d_Fpar%1d", iSmType, iPar));
         if (NULL != hFparcur) {
-          gDirectory->cd(oldir->GetPath());
+          gDirectory->cd(oldDir->GetPath());
           //TProfile *hFparmem =
           //(TProfile*) hFparcur->Clone();
           gDirectory->cd(curdir->GetPath());
@@ -890,19 +896,22 @@ Bool_t CbmDeviceHitBuilderTof::InitCalibParameter()
 
             // copy Histo to memory
             // TDirectory * curdir = gDirectory;
-            gDirectory->cd(oldir->GetPath());
+            gDirectory->cd(oldDir->GetPath());
             TH1D* h1DelTof =
               (TH1D*) htmpDelTof->Clone(Form("cl_CorSmT%01d_sm%03d_rpc%03d_Sel%02d_DelTof", iSmType, iSm, iRpc, iSel));
 
-            LOG(debug) << " copy histo " << h1DelTof->GetName() << " to directory " << oldir->GetName();
-
+            LOG(debug) << " copy histo " << h1DelTof->GetName() << " to directory " << oldDir->GetName();
             gDirectory->cd(curdir->GetPath());
           }
         }
     }
   }
   //   fCalParFile->Delete();
-  gDirectory->cd(oldir->GetPath());  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  gFile      = oldFile;
+  gDirectory = oldDir;
+
   LOG(info) << "InitCalibParameter: initialization done";
   return kTRUE;
 }
@@ -2086,8 +2095,8 @@ Bool_t CbmDeviceHitBuilderTof::BuildHits()
                       gGeoManager->LocalToMaster(hitpos_local, hitpos);
                       /*
 		      LOG(debug)<<
-			Form("LocalToMaster for node %p: (%6.1f,%6.1f,%6.1f) ->(%6.1f,%6.1f,%6.1f)", 
-			     cNode, hitpos_local[0], hitpos_local[1], hitpos_local[2], 
+			Form("LocalToMaster for node %p: (%6.1f,%6.1f,%6.1f) ->(%6.1f,%6.1f,%6.1f)",
+			     cNode, hitpos_local[0], hitpos_local[1], hitpos_local[2],
 			     hitpos[0], hitpos[1], hitpos[2]);
 		      */
                       TVector3 hitPos(hitpos[0], hitpos[1], hitpos[2]);
@@ -2098,9 +2107,9 @@ Bool_t CbmDeviceHitBuilderTof::BuildHits()
                       TVector3 hitPosErr(0.5, 0.5, 0.5);  // including positioning uncertainty
                       /*
 			TVector3 hitPosErr( fChannelInfo->GetSizex()/TMath::Sqrt(12.0),   // Single strips approximation
-			0.5, // Use generic value 
+			0.5, // Use generic value
 			1.);
-			
+
 		      */
                       //fDigiBdfPar->GetFeeTimeRes() * fDigiBdfPar->GetSigVel(iSmType,iRpc), // Use the electronics resolution
                       //fDigiBdfPar->GetNbGaps( iSmType, iRpc)*
@@ -2124,7 +2133,7 @@ Bool_t CbmDeviceHitBuilderTof::BuildHits()
 					fiNbHits,iNbChanInHit,iDetId,iChm,iLastChan,iRefId,
 					dWeightedTime,dWeightedPosY)
 				<<", DigiSize: "<<vDigiIndRef.size()
-				<<", DigiInds: ";		      
+				<<", DigiInds: ";
 		      for (UInt_t i=0; i<vDigiIndRef.size();i++){
 			LOG(debug)<<" "<<vDigiIndRef.at(i)<<"(M"<<fviClusterMul[iSmType][iSm][iRpc]<<")";
 		      }
@@ -2173,7 +2182,7 @@ Bool_t CbmDeviceHitBuilderTof::BuildHits()
                       //fviTrkMul[iSmType][iRpc].push_back( vPtsRef.size() );
                       fvdX[iSmType][iRpc].push_back(dWeightedPosX);
                       fvdY[iSmType][iRpc].push_back(dWeightedPosY);
-                      /*  no TofPoint available for data!  
+                      /*  no TofPoint available for data!
 			  fvdDifX[iSmType][iRpc].push_back( vPtsRef[0]->GetX() - dWeightedPosX);
 			  fvdDifY[iSmType][iRpc].push_back( vPtsRef[0]->GetY() - dWeightedPosY);
 			  fvdDifCh[iSmType][iRpc].push_back( fGeoHandler->GetCell( vPtsRef[0]->GetDetectorID() ) -1 -iLastChan );
@@ -2259,7 +2268,7 @@ Bool_t CbmDeviceHitBuilderTof::BuildHits()
           // and save it if it's the case
           if (0 < iNbChanInHit) {
             /*
-	    LOG(debug)<<"Process cluster " 
+	    LOG(debug)<<"Process cluster "
 		      <<iNbChanInHit;
 	    */
             // Check orientation to properly assign errors
@@ -2290,8 +2299,8 @@ Bool_t CbmDeviceHitBuilderTof::BuildHits()
               gGeoManager->LocalToMaster(hitpos_local, hitpos);
               /*
 	      LOG(debug)<<
-		Form(" LocalToMaster for V-node %p: (%6.1f,%6.1f,%6.1f) ->(%6.1f,%6.1f,%6.1f)", 
-		     cNode, hitpos_local[0], hitpos_local[1], hitpos_local[2], 
+		Form(" LocalToMaster for V-node %p: (%6.1f,%6.1f,%6.1f) ->(%6.1f,%6.1f,%6.1f)",
+		     cNode, hitpos_local[0], hitpos_local[1], hitpos_local[2],
 		     hitpos[0], hitpos[1], hitpos[2])
 		;
 	      */
@@ -2302,7 +2311,7 @@ Bool_t CbmDeviceHitBuilderTof::BuildHits()
               TVector3 hitPosErr(0.5, 0.5, 0.5);  // including positioning uncertainty
               /*
 		TVector3 hitPosErr( fChannelInfo->GetSizex()/TMath::Sqrt(12.0),   // Single strips approximation
-		0.5, // Use generic value 
+		0.5, // Use generic value
 		1.);
 	      */
               Int_t iChm = floor(dWeightedPosX / fChannelInfo->GetSizex()) + iNbCh / 2;
@@ -2313,10 +2322,10 @@ Bool_t CbmDeviceHitBuilderTof::BuildHits()
               //if(NULL != fTofPointsColl) iRefId = fTofPointsColl->IndexOf( vPtsRef[0] );
               /*
 	      LOG(debug)<<"Save V-Hit  "
-			<< Form(" %3d %3d 0x%08x %3d 0x%08x", // %3d %3d 
+			<< Form(" %3d %3d 0x%08x %3d 0x%08x", // %3d %3d
 				fiNbHits,iNbChanInHit,iDetId,iLastChan,iRefId) //vPtsRef.size(),vPtsRef[0])
 		//   dWeightedTime,dWeightedPosY)
-			<<", DigiSize: "<<vDigiIndRef.size();	      
+			<<", DigiSize: "<<vDigiIndRef.size();
 	      for (UInt_t i=0; i<vDigiIndRef.size();i++){
 		LOG(debug)<<"DigiIndRef "<<i<<" "<<vDigiIndRef.at(i)<<"(M"<<fviClusterMul[iSmType][iSm][iRpc]<<")";
 	      }
@@ -2622,7 +2631,7 @@ Bool_t CbmDeviceHitBuilderTof::AddNextChan(Int_t iSmType, Int_t iSm, Int_t iRpc,
   fviClusterMul[iSmType][iSm][iRpc]++;
   /*
   LOG(debug)<<"Save A-Hit "
-            << Form("%2d %2d 0x%08x %3d t %f, y %f ", 
+            << Form("%2d %2d 0x%08x %3d t %f, y %f ",
 		    fiNbHits,iNbChanInHit,iDetId,iLastChan,dLastTime,dLastPosY)
             <<", DigiSize: "<<vDigiIndRef.size();
   for (UInt_t i=0; i<vDigiIndRef.size();i++){
@@ -3315,9 +3324,9 @@ Bool_t CbmDeviceHitBuilderTof::FillHistos()
       gGeoManager->GetCurrentNode();
       gGeoManager->MasterToLocal(hitpos, hitpos_local);
       /*
-	LOG(debug1)<< Form(" MasterToLocal for %d, %d%d%d, node %p: (%6.1f,%6.1f,%6.1f) ->(%6.1f,%6.1f,%6.1f)", 
+	LOG(debug1)<< Form(" MasterToLocal for %d, %d%d%d, node %p: (%6.1f,%6.1f,%6.1f) ->(%6.1f,%6.1f,%6.1f)",
 		      iDetIndx, iSmType, iSm, iRpc,
-                 cNode, hitpos[0], hitpos[1], hitpos[2], 
+                 cNode, hitpos[0], hitpos[1], hitpos[2],
                  hitpos_local[0], hitpos_local[1], hitpos_local[2])
               ;
       */
@@ -3398,9 +3407,9 @@ Bool_t CbmDeviceHitBuilderTof::FillHistos()
           }
           /*
 	    LOG(debug1)<<" fhRpcCluTot:  Digi 0 "<<iDigInd0<<": Ch "<<pDig0->GetChannel()<<", Side "<<pDig0->GetSide()
-	               <<", StripSide "<<(Double_t)iCh*2.+pDig0->GetSide() 
+	               <<", StripSide "<<(Double_t)iCh*2.+pDig0->GetSide()
                        <<" Digi 1 "<<iDigInd1<<": Ch "<<pDig1->GetChannel()<<", Side "<<pDig1->GetSide()
-		       <<" , StripSide "<<(Double_t)iCh*2.+pDig1->GetSide() 
+		       <<" , StripSide "<<(Double_t)iCh*2.+pDig1->GetSide()
 		       <<", Tot0 " << pDig0->GetTot() <<", Tot1 "<<pDig1->GetTot();
 	  */
           fhRpcCluTot[iDetIndx]->Fill(pDig0->GetChannel() * 2. + pDig0->GetSide(), pDig0->GetTot());
@@ -3466,9 +3475,9 @@ Bool_t CbmDeviceHitBuilderTof::FillHistos()
           }  // end of Clustersize > 1 condition
           /*
 	    LOG(debug1)<<" fhTRpcCluTot: Digi 0 "<<iDigInd0<<": Ch "<<pDig0->GetChannel()<<", Side "<<pDig0->GetSide()
-		    <<", StripSide "<<(Double_t)iCh*2.+pDig0->GetSide() 
+		    <<", StripSide "<<(Double_t)iCh*2.+pDig0->GetSide()
                     <<" Digi 1 "<<iDigInd1<<": Ch "<<pDig1->GetChannel()<<", Side "<<pDig1->GetSide()
-		    <<", StripSide "<<(Double_t)iCh*2.+pDig1->GetSide() 
+		    <<", StripSide "<<(Double_t)iCh*2.+pDig1->GetSide()
 		    ;
 	  */
           for (Int_t iSel = 0; iSel < iNSel; iSel++)
diff --git a/analysis/detectors/tof/CbmTofAnaTestbeam.cxx b/analysis/detectors/tof/CbmTofAnaTestbeam.cxx
index b6a7a7eb46e34aa1c3bdb8d5c8d58f56f54b2989..2b70c1c33195de8b8e60ebc7e7e0c5bdf6ff9eb5 100644
--- a/analysis/detectors/tof/CbmTofAnaTestbeam.cxx
+++ b/analysis/detectors/tof/CbmTofAnaTestbeam.cxx
@@ -1255,6 +1255,10 @@ Bool_t CbmTofAnaTestbeam::InitParameters() {
 Bool_t CbmTofAnaTestbeam::LoadCalParameter() {
   if (fCalParFileName.IsNull()) return kTRUE;
 
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   fCalParFile = new TFile(fCalParFileName, "");
   if (NULL == fCalParFile) {
     LOG(error) << "CbmTofAnaTestBeam::LoadCalParameter: "
@@ -1435,6 +1439,10 @@ Bool_t CbmTofAnaTestbeam::LoadCalParameter() {
     fhSelHitTupleResidualXYT_Width->SetBinContent(3, fdDTWidth);
   }
 
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
+
   fCalParFile->Close();
   //    fhDTD4DT04D4Off->Draw();
 
@@ -7925,8 +7933,11 @@ Bool_t CbmTofAnaTestbeam::WriteHistos() {
 
   LOG(info) << "Write ./tofAnaTestBeam.hst.root, mode = " << fiCorMode;
 
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   // Write histogramms to the file
-  TDirectory* oldir = gDirectory;
   TFile* fHist      = new TFile(fCalOutFileName, "RECREATE");
   fHist->cd();
 
@@ -8251,7 +8262,9 @@ Bool_t CbmTofAnaTestbeam::WriteHistos() {
     fhXY04->Write();
     fhYX04->Write();
   }
-  gDirectory->cd(oldir->GetPath());
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 
   fHist->Close();
 
diff --git a/analysis/detectors/tof/CbmTofAnaTestbeam_Template.cxx b/analysis/detectors/tof/CbmTofAnaTestbeam_Template.cxx
index d5b3c71bd59b8f19a9cf8a4f11f16b8c755b2d61..1096b7a44418b9d4c5e47038566611d1692aad5f 100644
--- a/analysis/detectors/tof/CbmTofAnaTestbeam_Template.cxx
+++ b/analysis/detectors/tof/CbmTofAnaTestbeam_Template.cxx
@@ -304,8 +304,11 @@ Bool_t CbmTofAnaTestbeam::FillHistos() {
 Bool_t CbmTofAnaTestbeam::WriteHistos() {
   // TODO: add sub-folders
 
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   // Write histogramms to the file
-  TDirectory* oldir = gDirectory;
   TFile* fHist      = new TFile("./tofTests.hst.root", "RECREATE");
   fHist->cd();
 
@@ -319,7 +322,9 @@ Bool_t CbmTofAnaTestbeam::WriteHistos() {
    fhHitMapYZ->Write();
    */
 
-  gDirectory->cd(oldir->GetPath());
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 
   fHist->Close();
 
diff --git a/analysis/detectors/tof/CbmTofGeometryQa.cxx b/analysis/detectors/tof/CbmTofGeometryQa.cxx
index 7c73ecd54298863ed7919104911f30575896e2ff..80591216cca9bfd3fff08225af489dec1c2e8f6b 100644
--- a/analysis/detectors/tof/CbmTofGeometryQa.cxx
+++ b/analysis/detectors/tof/CbmTofGeometryQa.cxx
@@ -1517,8 +1517,11 @@ Bool_t CbmTofGeometryQa::SetHistoFileName(TString sFilenameIn) {
 Bool_t CbmTofGeometryQa::WriteHistos() {
   // TODO: add sub-folders ?
 
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   // Write histogramms to the file
-  TDirectory* oldir = gDirectory;
   TFile* fHist      = new TFile(fsHistoOutFilename, "RECREATE");
   fHist->cd();
 
@@ -1625,7 +1628,9 @@ Bool_t CbmTofGeometryQa::WriteHistos() {
 
   fHist->cd();  // make the file root the current directory
 
-  gDirectory->cd(oldir->GetPath());
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 
   fHist->Close();
 
diff --git a/analysis/detectors/tof/CbmTofHitFinderQa.cxx b/analysis/detectors/tof/CbmTofHitFinderQa.cxx
index d96347110f1c443b4674a1d81d5eb04b9ed708ac..89358379ef04e50039b338143778bcbbe317027a 100644
--- a/analysis/detectors/tof/CbmTofHitFinderQa.cxx
+++ b/analysis/detectors/tof/CbmTofHitFinderQa.cxx
@@ -4621,7 +4621,11 @@ Bool_t CbmTofHitFinderQa::NormalizeMapHistos() {
   // Normalization of Cartesian coordinates maps
   if ("" != fsHistoInNormCartFilename) {
     // Open file and set folders properly to avoid creating objects in uncontrolled places
-    TDirectory* oldir = gDirectory;
+
+    /// Save old global file and folder pointer to avoid messing with FairRoot
+    TFile* oldFile     = gFile;
+    TDirectory* oldDir = gDirectory;
+
     TFile* fHistNorm  = new TFile(fsHistoInNormCartFilename, "READ");
     if (NULL == fHistNorm) {
       LOG(error)
@@ -4715,8 +4719,9 @@ Bool_t CbmTofHitFinderQa::NormalizeMapHistos() {
       fhHitMapMultTrkYZ->Divide(fhHitMapNormYZ);
     }  // if( NULL != fhHitMapNormYZ   )
 
-    // Go back to original folder, whereever it is
-    gDirectory->cd(oldir->GetPath());
+    /// Restore old global file and folder pointer to avoid messing with FairRoot
+    gFile      = oldFile;
+    gDirectory = oldDir;
 
     fHistNorm->Close();
   }  // if( "" != fsHistoInNormCartFilename )
@@ -4728,7 +4733,11 @@ Bool_t CbmTofHitFinderQa::NormalizeMapHistos() {
   // Normalization of Angular   coordinates maps
   if ("" != fsHistoInNormAngFilename) {
     // Open file and set folders properly to avoid creating objects in uncontrolled places
-    TDirectory* oldir = gDirectory;
+
+    /// Save old global file and folder pointer to avoid messing with FairRoot
+    TFile* oldFile     = gFile;
+    TDirectory* oldDir = gDirectory;
+
     TFile* fHistNorm  = new TFile(fsHistoInNormAngFilename, "READ");
     if (NULL == fHistNorm) {
       LOG(error)
@@ -4767,8 +4776,9 @@ Bool_t CbmTofHitFinderQa::NormalizeMapHistos() {
       fhHitMapMultTrkAng->Divide(fhHitMapNormAng);
     }
 
-    // Go back to original folder, whereever it is
-    gDirectory->cd(oldir->GetPath());
+    /// Restore old global file and folder pointer to avoid messing with FairRoot
+    gFile      = oldFile;
+    gDirectory = oldDir;
 
     fHistNorm->Close();
   }  // if( "" != fsHistoInNormAngFilename )
@@ -4780,7 +4790,11 @@ Bool_t CbmTofHitFinderQa::NormalizeMapHistos() {
   // Normalization of Spherical coordinates maps
   if ("" != fsHistoInNormSphFilename) {
     // Open file and set folders properly to avoid creating objects in uncontrolled places
-    TDirectory* oldir = gDirectory;
+
+    /// Save old global file and folder pointer to avoid messing with FairRoot
+    TFile* oldFile     = gFile;
+    TDirectory* oldDir = gDirectory;
+
     TFile* fHistNorm  = new TFile(fsHistoInNormSphFilename, "READ");
     if (NULL == fHistNorm) {
       LOG(error)
@@ -4819,8 +4833,9 @@ Bool_t CbmTofHitFinderQa::NormalizeMapHistos() {
       fhHitMapMultTrkSph->Divide(fhHitMapNormSph);
     }
 
-    // Go back to original folder, whereever it is
-    gDirectory->cd(oldir->GetPath());
+    /// Restore old global file and folder pointer to avoid messing with FairRoot
+    gFile      = oldFile;
+    gDirectory = oldDir;
 
     fHistNorm->Close();
   }  // if( "" != fsHistoInNormSphFilename )
@@ -4869,8 +4884,11 @@ Bool_t CbmTofHitFinderQa::SetHistoFileName(TString sFilenameIn) {
 Bool_t CbmTofHitFinderQa::WriteHistos() {
   // TODO: add sub-folders ?
 
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   // Write histogramms to the file
-  TDirectory* oldir = gDirectory;
   TFile* fHist      = new TFile(fsHistoOutFilename, "RECREATE");
   fHist->cd();
 
@@ -5169,7 +5187,9 @@ Bool_t CbmTofHitFinderQa::WriteHistos() {
   fHist->cd();  // make the file root the current directory
   fhPointMatchWeight->Write();
 
-  gDirectory->cd(oldir->GetPath());
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 
   fHist->Close();
 
diff --git a/analysis/detectors/tof/CbmTofTests.cxx b/analysis/detectors/tof/CbmTofTests.cxx
index 9b3ebd15a18d2817e4782065df5f3b408f0d78ad..c225ffa61d6285404c12e18a9c95c870394c825c 100644
--- a/analysis/detectors/tof/CbmTofTests.cxx
+++ b/analysis/detectors/tof/CbmTofTests.cxx
@@ -1074,8 +1074,11 @@ Bool_t CbmTofTests::FillHistos() {
 Bool_t CbmTofTests::WriteHistos() {
   // TODO: add sub-folders
 
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   // Write histogramms to the file
-  TDirectory* oldir = gDirectory;
   TFile* fHist      = new TFile("./tofTests.hst.root", "RECREATE");
   fHist->cd();
 
@@ -1136,7 +1139,9 @@ Bool_t CbmTofTests::WriteHistos() {
   fhTofEff->Write();
   fhTofMixing->Write();
 
-  gDirectory->cd(oldir->GetPath());
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 
   fHist->Close();
 
diff --git a/core/detectors/tof/CbmTofDigiBdfPar.cxx b/core/detectors/tof/CbmTofDigiBdfPar.cxx
index aa4e8858dd0d50cac00b0557de7b083fe3d21ef2..80521e043bbb18656ffa4a364fa5b8bf903033c8 100644
--- a/core/detectors/tof/CbmTofDigiBdfPar.cxx
+++ b/core/detectors/tof/CbmTofDigiBdfPar.cxx
@@ -333,7 +333,9 @@ Bool_t CbmTofDigiBdfPar::getParams(FairParamList* l) {
 }
 
 Bool_t CbmTofDigiBdfPar::LoadBeamtimeHistos() {
-  TDirectory* oldir = gDirectory;
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
 
   TFile* fBeamtimeInput = new TFile(fsBeamInputFile, "READ");
   if (kFALSE == fBeamtimeInput->IsOpen()) {
@@ -352,16 +354,19 @@ Bool_t CbmTofDigiBdfPar::LoadBeamtimeHistos() {
   if (0 == pInputRes) {
     LOG(error) << "CbmTofDigiBdfPar => Could not recover the Time Resolution "
                   "array from the beamtime data file.";
-    gDirectory->cd(oldir->GetPath());
     fBeamtimeInput->Close();
+    gFile      = oldFile;
+    gDirectory = oldDir;
     return kFALSE;
   }  // if( 0 == pInputEff)
   if (0 == pInputEff->GetSize() || 0 == pInputRes->GetSize()
       || pInputEff->GetSize() != pInputRes->GetSize()) {
     LOG(error) << "CbmTofDigiBdfPar => Efficiency or Time Resolution array "
                   "from the beamtime data file have wrong size.";
-    gDirectory->cd(oldir->GetPath());
     fBeamtimeInput->Close();
+    /// Restore old global file and folder pointer to avoid messing with FairRoot
+    gFile      = oldFile;
+    gDirectory = oldDir;
     return kFALSE;
   }  // if wrong array size
 
@@ -403,8 +408,10 @@ Bool_t CbmTofDigiBdfPar::LoadBeamtimeHistos() {
                       "histogram for Sm Type "
                    << iSmType << ", mapped to input type "
                    << fiSmTypeInpMapp[iSmType];
-        gDirectory->cd(oldir->GetPath());
         fBeamtimeInput->Close();
+        /// Restore old global file and folder pointer to avoid messing with FairRoot
+        gFile      = oldFile;
+        gDirectory = oldDir;
         return kFALSE;
       }  // if( 0 == pH1Temp )
       fh1ClusterSize[iSmType] =
@@ -418,8 +425,10 @@ Bool_t CbmTofDigiBdfPar::LoadBeamtimeHistos() {
                       "histogram for Sm Type "
                    << iSmType << ", mapped to input type "
                    << fiSmTypeInpMapp[iSmType];
-        gDirectory->cd(oldir->GetPath());
         fBeamtimeInput->Close();
+        /// Restore old global file and folder pointer to avoid messing with FairRoot
+        gFile      = oldFile;
+        gDirectory = oldDir;
         return kFALSE;
       }  // if( 0 == pH1Temp )
       fh1ClusterTot[iSmType] =
@@ -428,22 +437,28 @@ Bool_t CbmTofDigiBdfPar::LoadBeamtimeHistos() {
     else {
       LOG(error) << "CbmTofDigiBdfPar => Wrong mapping index for Sm Type "
                  << iSmType << ": Out of input boundaries";
-      gDirectory->cd(oldir->GetPath());
       fBeamtimeInput->Close();
+      /// Restore old global file and folder pointer to avoid messing with FairRoot
+      gFile      = oldFile;
+      gDirectory = oldDir;
       return kFALSE;
     }
 
   if (2 == fiClusterRadiusModel) {
     GetLandauParFromBeamDataFit();
   }  // if( 2 == fiClusterRadiusModel )
-  gDirectory->cd(oldir->GetPath());
   fBeamtimeInput->Close();
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 
   return kTRUE;
 }
 /************************************************************************************/
 Bool_t CbmTofDigiBdfPar::GetLandauParFromBeamDataFit() {
-  TDirectory* oldir = gDirectory;
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
 
   TFile* fSimInput =
     new TFile("RadToClustDist_0000_1000_0010_00025_05025_00025.root", "READ");
@@ -481,6 +496,9 @@ Bool_t CbmTofDigiBdfPar::GetLandauParFromBeamDataFit() {
                << " => Use default values from ASCII parameter file! Pointers: "
                << hFitR0All << " " << hFitSigInAll << " " << hFitR0CntAll << " "
                << hFitSigInCntAll << " ";
+    /// Restore old global file and folder pointer to avoid messing with FairRoot
+    gFile      = oldFile;
+    gDirectory = oldDir;
     return kFALSE;
   }  // if( 0 == hFitR0All || 0 == hFitSigInAll || 0 == hFitR0CntAll || 0 == hFitSigInCntAll )
 
@@ -525,7 +543,9 @@ Bool_t CbmTofDigiBdfPar::GetLandauParFromBeamDataFit() {
     }  // else of if( kTRUE == pResult->IsValid() )
   }    // for( Int_t iSmType = 0; iSmType < fiNbSmTypes; iSmType ++)
 
-  gDirectory->cd(oldir->GetPath());
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 
   return kTRUE;
 }
diff --git a/reco/detectors/tof/CbmTofCalibrator.cxx b/reco/detectors/tof/CbmTofCalibrator.cxx
index e113028f0231b038e4f375ef5873ae136864943b..7fd0eab20b04bbc594dcd6e3413cecd016aa9367 100644
--- a/reco/detectors/tof/CbmTofCalibrator.cxx
+++ b/reco/detectors/tof/CbmTofCalibrator.cxx
@@ -1,7 +1,7 @@
 /** @file CbmTofCalibrator.cxx
  ** @author nh
  ** @date 28.02.2020
- ** 
+ **
  **/
 
 // CBMroot classes and includes
@@ -358,7 +358,7 @@ void CbmTofCalibrator::FillCalHist(CbmTofTracklet* pTrk, Int_t iOpt, CbmEvent* t
                             fTrackletTools->GetTexpected(pTrk, iDetId, pHit) ,
                             fTofFindTracks->GetTOff(iDetId),
                             (1.-2.*tDigi0->GetSide())*hlocal_f[1]/fDigiBdfPar->GetSigVel(iSmType,iSm,iRpc),
-                            tDigi0->GetTime()-fTrackletTools->GetTexpected(pTrk, iDetId, pHit) 
+                            tDigi0->GetTime()-fTrackletTools->GetTexpected(pTrk, iDetId, pHit)
                             -(1.-2.*tDigi0->GetSide())*hlocal_f[1]/fDigiBdfPar->GetSigVel(iSmType,iSm,iRpc));
         */
 
@@ -387,6 +387,11 @@ Bool_t CbmTofCalibrator::UpdateCalHist(Int_t iOpt)
   // get current calibration histos
   LOG(info) << "CbmTofCalibrator:: update histos from "
             << "file " << CbmTofEventClusterizer::Instance()->GetCalParFileName() << " with option " << iOpt;
+
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   TFile* fCalParFile = new TFile(CbmTofEventClusterizer::Instance()->GetCalParFileName(), "");
   if (NULL == fCalParFile) {
     LOG(warn) << "Could not open TofClusterizer calibration file, abort Update ";
@@ -536,6 +541,12 @@ Bool_t CbmTofCalibrator::UpdateCalHist(Int_t iOpt)
     WriteHist(fCalParFileNew);
     fCalParFileNew->Close();
   }
+  fCalParFile->Close();
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
+
   return kTRUE;
 }
 
diff --git a/reco/detectors/tof/CbmTofCosmicClusterizer.cxx b/reco/detectors/tof/CbmTofCosmicClusterizer.cxx
index fcdf9e25bd9ea1674802d3d333d5b5728e931562..e3c8bb185ee6a5ab93bbf69dca0e4ed3656146c4 100644
--- a/reco/detectors/tof/CbmTofCosmicClusterizer.cxx
+++ b/reco/detectors/tof/CbmTofCosmicClusterizer.cxx
@@ -1,7 +1,7 @@
-/** @file CbmTofCosmicClusterizer.cxx 
- ** author nh  
+/** @file CbmTofCosmicClusterizer.cxx
+ ** author nh
  ** adopted from
- ** @file CbmTofSimpClusterizer.cxx 
+ ** @file CbmTofSimpClusterizer.cxx
  ** @author Pierre-Alain Loizeau <loizeau@physi.uni-heidelberg.de>
  ** @date 23.08.2013
  **/
@@ -524,11 +524,10 @@ Bool_t CbmTofCosmicClusterizer::InitCalibParameter() {
 
   LOG(info) << "CbmTofCosmicClusterizer::InitCalibParameter: defaults set";
 
-  TDirectory* oldir =
-    gDirectory;  // <= To prevent histos from being sucked in by the param file of the TRootManager!
-  /*
-  gROOT->cd(); // <= To prevent histos from being sucked in by the param file of the TRootManager !
-  */
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
 
   if (0 < fCalMode) {
     LOG(info)
@@ -558,7 +557,7 @@ Bool_t CbmTofCosmicClusterizer::InitCalibParameter() {
       // copy Histo to memory
       TDirectory* curdir = gDirectory;
       if (NULL != hSvel) {
-        gDirectory->cd(oldir->GetPath());
+        gDirectory->cd(oldDir->GetPath());
         // TProfile *hSvelmem = (TProfile *)hSvel->Clone();   (VF) not used
         gDirectory->cd(curdir->GetPath());
       } else {
@@ -569,7 +568,7 @@ Bool_t CbmTofCosmicClusterizer::InitCalibParameter() {
         TProfile* hFparcur = (TProfile*) gDirectory->FindObjectAny(
           Form("cl_SmT%01d_Fpar%1d", iSmType, iPar));
         if (NULL != hFparcur) {
-          gDirectory->cd(oldir->GetPath());
+          gDirectory->cd(oldDir->GetPath());
           // TProfile *hFparmem = (TProfile *)hFparcur->Clone();   (VF) not used
           gDirectory->cd(curdir->GetPath());
         }
@@ -727,7 +726,7 @@ Bool_t CbmTofCosmicClusterizer::InitCalibParameter() {
 
             // copy Histo to memory
             // TDirectory * curdir = gDirectory;
-            gDirectory->cd(oldir->GetPath());
+            gDirectory->cd(oldDir->GetPath());
             TH1D* h1DelTof = (TH1D*) htmpDelTof->Clone(
               Form("cl_CorSmT%01d_sm%03d_rpc%03d_Sel%02d_DelTof",
                    iSmType,
@@ -735,8 +734,7 @@ Bool_t CbmTofCosmicClusterizer::InitCalibParameter() {
                    iRpc,
                    iSel));
 
-            LOG(debug) << " copy histo " << h1DelTof->GetName()
-                       << " to directory " << oldir->GetName();
+            LOG(debug) << " copy histo " << h1DelTof->GetName() << " to directory " << oldDir->GetName();
 
             gDirectory->cd(curdir->GetPath());
           }
@@ -744,9 +742,10 @@ Bool_t CbmTofCosmicClusterizer::InitCalibParameter() {
     }
   }
   //   fCalParFile->Delete();
-  gDirectory->cd(
-    oldir
-      ->GetPath());  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  gFile      = oldFile;
+  gDirectory = oldDir;
   LOG(info)
     << "CbmTofCosmicClusterizer::InitCalibParameter: initialization done";
   return kTRUE;
@@ -904,10 +903,11 @@ Bool_t CbmTofCosmicClusterizer::DeleteGeometry() {
 /************************************************************************************/
 // Histogramming functions
 Bool_t CbmTofCosmicClusterizer::CreateHistos() {
-  TDirectory* oldir =
-    gDirectory;  // <= To prevent histos from being sucked in by the param file of the TRootManager!
-  gROOT
-    ->cd();  // <= To prevent histos from being sucked in by the param file of the TRootManager !
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   fhClustBuildTime =
     new TH1I("TofCosmicClus_ClustBuildTime",
              "Time needed to build clusters in each event; Time [s]",
@@ -1977,9 +1977,10 @@ Bool_t CbmTofCosmicClusterizer::CreateHistos() {
                500);
   }  // if( kTRUE == fDigiBdfPar->ClustUseTrackId() )
 
-  gDirectory->cd(
-    oldir
-      ->GetPath());  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  gFile      = oldFile;
+  gDirectory = oldDir;
 
   return kTRUE;
 }
@@ -3030,7 +3031,10 @@ Bool_t CbmTofCosmicClusterizer::FillHistos() {
 }
 
 Bool_t CbmTofCosmicClusterizer::WriteHistos() {
-  TDirectory* oldir = gDirectory;
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   TFile* fHist;
   fHist = new TFile(fOutHstFileName, "RECREATE");
   fHist->cd();
@@ -3989,7 +3993,7 @@ Bool_t CbmTofCosmicClusterizer::WriteHistos() {
 	  if (fSel2Addr == iSmAddr) {
 	    //if (fSel2Id == iSmType && fSel2Sm == iSm && fSel2Rpc == iRpc) {
 	    // don't shift reference counter on average
-            //TMean-=((TProfile *)hAvTOff_pfx)->GetBinContent(iSm+1); 
+            //TMean-=((TProfile *)hAvTOff_pfx)->GetBinContent(iSm+1);
           }
 	  */
 
@@ -4683,8 +4687,9 @@ Bool_t CbmTofCosmicClusterizer::WriteHistos() {
     }
   }
 
-
-  gDirectory->cd(oldir->GetPath());
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 
   fHist->Close();
 
@@ -5716,13 +5721,13 @@ Bool_t CbmTofCosmicClusterizer::BuildClusters() {
                         0.5, 0.5, 0.5);  // including positioning uncertainty
                                          /*
                                     TVector3 hitPosErr( fChannelInfo->GetSizex()/TMath::Sqrt(12.0),   // Single strips approximation
-                                       0.5, // Use generic value 
+                                       0.5, // Use generic value
                                        1.);
 
                                     */                                       // fDigiBdfPar->GetFeeTimeRes() * fDigiBdfPar->GetSigVel(iSmType,iRpc), // Use the electronics resolution
-                                       //fDigiBdfPar->GetNbGaps( iSmType, iRpc)*
-                                       //fDigiBdfPar->GetGapSize( iSmType, iRpc)/ //10.0 / // Change gap size in cm
-                                       //TMath::Sqrt(12.0) ); // Use full RPC thickness as "Channel" Z size
+                      //fDigiBdfPar->GetNbGaps( iSmType, iRpc)*
+                      //fDigiBdfPar->GetGapSize( iSmType, iRpc)/ //10.0 / // Change gap size in cm
+                      //TMath::Sqrt(12.0) ); // Use full RPC thickness as "Channel" Z size
 
                       // calc mean ch from dPosX=((Double_t)(-iNbCh/2 + iCh)+0.5)*fChannelInfo->GetSizex();
 
@@ -5981,7 +5986,7 @@ Bool_t CbmTofCosmicClusterizer::BuildClusters() {
                 0.5, 0.5, 0.5);  // including positioning uncertainty
                                  /*
                      TVector3 hitPosErr( fChannelInfo->GetSizex()/TMath::Sqrt(12.0),   // Single strips approximation
-                                       0.5, // Use generic value 
+                                       0.5, // Use generic value
                                        1.);
                      */
               //                fDigiBdfPar->GetFeeTimeRes() * fDigiBdfPar->GetSigVel(iSmType,iRpc), // Use the electronics resolution
diff --git a/reco/detectors/tof/CbmTofEventClusterizer.cxx b/reco/detectors/tof/CbmTofEventClusterizer.cxx
index b5069e503a0d535d51814210bc3142fa6b576418..1470ecf86ab62eff44bbb6d38e37581f5520e0c5 100644
--- a/reco/detectors/tof/CbmTofEventClusterizer.cxx
+++ b/reco/detectors/tof/CbmTofEventClusterizer.cxx
@@ -733,10 +733,10 @@ Bool_t CbmTofEventClusterizer::InitCalibParameter()
   }
   LOG(info) << "CbmTofEventClusterizer::InitCalibParameter: defaults set";
 
-  TDirectory* oldir = gDirectory;  // <= To prevent histos from being sucked in by the param file of the TRootManager!
-  /*
-	 gROOT->cd(); // <= To prevent histos from being sucked in by the param file of the TRootManager !
-	 */
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
 
   if (0 < fCalMode) {
     LOG(info) << "CbmTofEventClusterizer::InitCalibParameter: read histos from "
@@ -764,7 +764,7 @@ Bool_t CbmTofEventClusterizer::InitCalibParameter()
       // copy Histo to memory
       TDirectory* curdir = gDirectory;
       if (NULL != hSvel) {
-        gDirectory->cd(oldir->GetPath());
+        gDirectory->cd(oldDir->GetPath());
         // TProfile *hSvelmem = (TProfile *)hSvel->Clone();  (VF) not used
         gDirectory->cd(curdir->GetPath());
       }
@@ -775,7 +775,7 @@ Bool_t CbmTofEventClusterizer::InitCalibParameter()
       for (Int_t iPar = 0; iPar < 4; iPar++) {
         TProfile* hFparcur = (TProfile*) gDirectory->FindObjectAny(Form("cl_SmT%01d_Fpar%1d", iSmType, iPar));
         if (NULL != hFparcur) {
-          gDirectory->cd(oldir->GetPath());
+          gDirectory->cd(oldDir->GetPath());
           // TProfile *hFparmem = (TProfile *)hFparcur->Clone();  (VF) not used
           gDirectory->cd(curdir->GetPath());
         }
@@ -886,19 +886,22 @@ Bool_t CbmTofEventClusterizer::InitCalibParameter()
 
             // copy Histo to memory
             // TDirectory * curdir = gDirectory;
-            gDirectory->cd(oldir->GetPath());
+            gDirectory->cd(oldDir->GetPath());
             TH1D* h1DelTof =
               (TH1D*) htmpDelTof->Clone(Form("cl_CorSmT%01d_sm%03d_rpc%03d_Sel%02d_DelTof", iSmType, iSm, iRpc, iSel));
 
-            LOG(debug) << " copy histo " << h1DelTof->GetName() << " to directory " << oldir->GetName();
-
+            LOG(debug) << " copy histo " << h1DelTof->GetName() << " to directory " << oldDir->GetName();
             gDirectory->cd(curdir->GetPath());
           }
         }
     }
   }
   //   fCalParFile->Delete();
-  gDirectory->cd(oldir->GetPath());  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  gFile      = oldFile;
+  gDirectory = oldDir;
+
   LOG(info) << "CbmTofEventClusterizer::InitCalibParameter: initialization done";
   return kTRUE;
 }
@@ -2626,7 +2629,9 @@ Bool_t CbmTofEventClusterizer::FillHistos()
 Bool_t CbmTofEventClusterizer::WriteHistos()
 {
   if (fDutId < 0) return kTRUE;
-  TDirectory* oldir = gDirectory;
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
   TFile* fHist;
   fHist = new TFile(fOutHstFileName, "RECREATE");
   fHist->cd();
@@ -3944,7 +3949,9 @@ Bool_t CbmTofEventClusterizer::WriteHistos()
     }
   }
 
-  gDirectory->cd(oldir->GetPath());
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 
   fHist->Close();
 
diff --git a/reco/detectors/tof/CbmTofExtendTracks.cxx b/reco/detectors/tof/CbmTofExtendTracks.cxx
index 93743e71f69a32eb507c00f74c7c17e9db3d4dcc..3bce5c8e28df13a3363eb9fe291e1317b674f3eb 100644
--- a/reco/detectors/tof/CbmTofExtendTracks.cxx
+++ b/reco/detectors/tof/CbmTofExtendTracks.cxx
@@ -265,7 +265,10 @@ Bool_t CbmTofExtendTracks::LoadCalParameter()
     fvZoff[i] = 0.;
 
   if (fCalParFileName.IsNull()) return kTRUE;
-  TDirectory* oldir = gDirectory;
+
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
 
   fCalParFile = new TFile(fCalParFileName, "");
   if (NULL == fCalParFile) {
@@ -287,7 +290,9 @@ Bool_t CbmTofExtendTracks::LoadCalParameter()
   TH1D* fhys = (TH1D*) gDirectory->FindObjectAny(Form("hExt_Ysig"));
   TH1D* fhzs = (TH1D*) gDirectory->FindObjectAny(Form("hExt_Zsig"));
 
-  gDirectory->cd(oldir->GetPath());
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 
   if (NULL != fht) {
     fhExt_Toff = (TH1D*) fht->Clone();
@@ -542,8 +547,9 @@ Bool_t CbmTofExtendTracks::WriteHistos()
 {
   if (fiCorMode < 0) return kTRUE;
   // Write calibration histogramms to the file
-  TDirectory* oldir = gDirectory;
-  TFile* fHist      = new TFile(fCalOutFileName, "RECREATE");
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+  TFile* fHist       = new TFile(fCalOutFileName, "RECREATE");
   fHist->cd();
 
   if (NULL != fhExt_Toff) fhExt_Toff->Write();
@@ -551,7 +557,9 @@ Bool_t CbmTofExtendTracks::WriteHistos()
   if (NULL != fhExt_Yoff) fhExt_Yoff->Write();
   if (NULL != fhExt_Zoff) fhExt_Zoff->Write();
 
-  gDirectory->cd(oldir->GetPath());
+  gFile      = oldFile;
+  gDirectory = oldDir;
+
   fHist->Close();
   return kTRUE;
 }
@@ -608,7 +616,7 @@ void CbmTofExtendTracks::ExecExtend(Option_t* /*opt*/, CbmEvent* tEvent)
   /*
   if (NULL != fvAllHitPointer )
   for(UInt_t iSt=0; iSt<fvAllHitPointer.size(); iSt++) {
-	for (UInt_t iHit=0; iHit<fvAllHitPointer[iSt].size(); iHit++) 
+	for (UInt_t iHit=0; iHit<fvAllHitPointer[iSt].size(); iHit++)
 	  delete(fvAllHitPointer[iSt][iHit]);
 	fvAllHitPointer[iSt].clear();
   }
@@ -1308,7 +1316,7 @@ void CbmTofExtendTracks::FillHistograms(CbmEvent* tEvent)
         LOG(info)<<"Got MuchCorI Tr "<<iTr
         		<<", St "<<iTrSt
         		<<", Hit "<< iHit <<", ind "<<fvMuchHitIndex[iSt][iHit]
-				<<", poi "<< tHit												  
+				<<", poi "<< tHit
 				<<", dx " << dDX
 				<<", dy " << dDY;
         */
diff --git a/reco/detectors/tof/CbmTofFindTracks.cxx b/reco/detectors/tof/CbmTofFindTracks.cxx
index 5e02ae5cca4f24191e04456959019af9e028a0f7..f505b0aef78794438cbd9e1395ad7ed77c645e9a 100644
--- a/reco/detectors/tof/CbmTofFindTracks.cxx
+++ b/reco/detectors/tof/CbmTofFindTracks.cxx
@@ -357,6 +357,10 @@ Bool_t CbmTofFindTracks::LoadCalParameter() {
 
   if (fCalParFileName.IsNull()) return kTRUE;
 
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   fCalParFile = new TFile(fCalParFileName, "");
   if (NULL == fCalParFile) {
     LOG(error) << "CbmTofFindTracks::LoadCalParameter: "
@@ -376,7 +380,6 @@ Bool_t CbmTofFindTracks::LoadCalParameter() {
   TH1D* fhtmpWY = (TH1D*) gDirectory->FindObjectAny(Form("hPullY_Smt_Width"));
   TH1D* fhtmpWZ = (TH1D*) gDirectory->FindObjectAny(Form("hPullZ_Smt_Width"));
 
-
   gROOT->cd();
   if (NULL == fhtmp) {
     LOG(info) << Form("CbmTofFindTracks::LoadCalParameter: hPullT_Smt_Off")
@@ -561,6 +564,10 @@ Bool_t CbmTofFindTracks::LoadCalParameter() {
     }
   }
 
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
+
   return kTRUE;
 }
 //-------------------------------------------------------------------------------------------------
@@ -623,7 +630,9 @@ Bool_t CbmTofFindTracks::WriteHistos() {
                     fiCorMode);
 
   // Write histogramms to the file
-  TDirectory* oldir = gDirectory;
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
   TFile* fHist      = new TFile(fCalOutFileName, "RECREATE");
   fHist->cd();
   const Double_t RMSmin = 0.03;  // in ns
@@ -1063,7 +1072,10 @@ Bool_t CbmTofFindTracks::WriteHistos() {
     fhPullY_Smt_Width->Write();
     fhPullZ_Smt_Width->Write();
   }
-  gDirectory->cd(oldir->GetPath());
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
+
   fHist->Close();
 
   return kTRUE;
diff --git a/reco/detectors/tof/CbmTofHitMaker.cxx b/reco/detectors/tof/CbmTofHitMaker.cxx
index fde5b285e00a22cae4730303b71c869f3fd85e6c..88e0a5b333f9549f5bd029b73679532cdab9c7f7 100644
--- a/reco/detectors/tof/CbmTofHitMaker.cxx
+++ b/reco/detectors/tof/CbmTofHitMaker.cxx
@@ -1,10 +1,10 @@
-/** @file CbmTofHitMaker.cxx 
- ** @author nh  
+/** @file CbmTofHitMaker.cxx
+ ** @author nh
  ** @date01.12.2018
  ** adopted from
- ** @file CbmTofEventClusterizer.cxx 
- ** @file CbmTofTestBeamClusterizer.cxx 
- ** @file CbmTofSimpClusterizer.cxx 
+ ** @file CbmTofEventClusterizer.cxx
+ ** @file CbmTofTestBeamClusterizer.cxx
+ ** @file CbmTofSimpClusterizer.cxx
  ** @author Pierre-Alain Loizeau <loizeau@physi.uni-heidelberg.de>
  ** @date 23.08.2013
  **/
@@ -684,11 +684,10 @@ Bool_t CbmTofHitMaker::InitCalibParameter() {
   }
   LOG(info) << "CbmTofHitMaker::InitCalibParameter: defaults set";
 
-  TDirectory* oldir =
-    gDirectory;  // <= To prevent histos from being sucked in by the param file of the TRootManager!
-  /*
-	 gROOT->cd(); // <= To prevent histos from being sucked in by the param file of the TRootManager !
-	 */
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
 
   if (0 < fCalMode) {
     LOG(info) << "CbmTofHitMaker::InitCalibParameter: read histos from "
@@ -717,7 +716,7 @@ Bool_t CbmTofHitMaker::InitCalibParameter() {
       // copy Histo to memory
       TDirectory* curdir = gDirectory;
       if (NULL != hSvel) {
-        gDirectory->cd(oldir->GetPath());
+        gDirectory->cd(oldDir->GetPath());
         // TProfile *hSvelmem = (TProfile *)hSvel->Clone();  (VF) not used
         gDirectory->cd(curdir->GetPath());
       } else {
@@ -728,7 +727,7 @@ Bool_t CbmTofHitMaker::InitCalibParameter() {
         TProfile* hFparcur = (TProfile*) gDirectory->FindObjectAny(
           Form("cl_SmT%01d_Fpar%1d", iSmType, iPar));
         if (NULL != hFparcur) {
-          gDirectory->cd(oldir->GetPath());
+          gDirectory->cd(oldDir->GetPath());
           // TProfile *hFparmem = (TProfile *)hFparcur->Clone();  (VF) not used
           gDirectory->cd(curdir->GetPath());
         }
@@ -892,7 +891,7 @@ Bool_t CbmTofHitMaker::InitCalibParameter() {
 
             // copy Histo to memory
             // TDirectory * curdir = gDirectory;
-            gDirectory->cd(oldir->GetPath());
+            gDirectory->cd(oldDir->GetPath());
             TH1D* h1DelTof = (TH1D*) htmpDelTof->Clone(
               Form("cl_CorSmT%01d_sm%03d_rpc%03d_Sel%02d_DelTof",
                    iSmType,
@@ -900,8 +899,7 @@ Bool_t CbmTofHitMaker::InitCalibParameter() {
                    iRpc,
                    iSel));
 
-            LOG(debug) << " copy histo " << h1DelTof->GetName()
-                       << " to directory " << oldir->GetName();
+            LOG(debug) << " copy histo " << h1DelTof->GetName() << " to directory " << oldDir->GetName();
 
             gDirectory->cd(curdir->GetPath());
           }
@@ -909,9 +907,10 @@ Bool_t CbmTofHitMaker::InitCalibParameter() {
     }
   }
   //   fCalParFile->Delete();
-  gDirectory->cd(
-    oldir
-      ->GetPath());  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  gFile      = oldFile;
+  gDirectory = oldDir;
   LOG(info) << "CbmTofHitMaker::InitCalibParameter: initialization done";
   return kTRUE;
 }
@@ -1109,12 +1108,19 @@ Bool_t CbmTofHitMaker::FillHistos() {
 }
 
 Bool_t CbmTofHitMaker::WriteHistos() {
-  TDirectory* oldir = gDirectory;
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   TFile* fHist;
   fHist = new TFile(fOutHstFileName, "RECREATE");
   fHist->cd();
   fhClustBuildTime->Write();
-  gDirectory->cd(oldir->GetPath());
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
+
   fHist->Close();
 
   return kTRUE;
diff --git a/reco/detectors/tof/CbmTofSimpClusterizer.cxx b/reco/detectors/tof/CbmTofSimpClusterizer.cxx
index d8687b04b2c7dcf99edf4e7723d94282cb49eb5a..26f22f20dc13a9f3b8946ec0055d2e02db1883f4 100644
--- a/reco/detectors/tof/CbmTofSimpClusterizer.cxx
+++ b/reco/detectors/tof/CbmTofSimpClusterizer.cxx
@@ -536,11 +536,10 @@ Bool_t CbmTofSimpClusterizer::InitCalibParameter() {
 
   LOG(info) << "CbmTofSimpClusterizer::InitCalibParameter: defaults set";
 
-  TDirectory* oldir =
-    gDirectory;  // <= To prevent histos from being sucked in by the param file of the TRootManager!
-  /*
-  gROOT->cd(); // <= To prevent histos from being sucked in by the param file of the TRootManager !
-  */
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
 
   if (0 < fCalMode) {
     LOG(info) << "CbmTofSimpClusterizer::InitCalibParameter: read histos from "
@@ -681,7 +680,7 @@ Bool_t CbmTofSimpClusterizer::InitCalibParameter() {
 
             // copy Histo to memory
             TDirectory* curdir = gDirectory;
-            gDirectory->cd(oldir->GetPath());
+            gDirectory->cd(oldDir->GetPath());
             TH1D* h1DelTof = (TH1D*) htmpDelTof->Clone(
               Form("cl_CorSmT%01d_sm%03d_rpc%03d_Trg%02d_DelTof",
                    iSmType,
@@ -689,8 +688,7 @@ Bool_t CbmTofSimpClusterizer::InitCalibParameter() {
                    iRpc,
                    iTrg));
 
-            LOG(info) << " copy histo " << h1DelTof->GetName()
-                      << " to directory " << oldir->GetName();
+            LOG(info) << " copy histo " << h1DelTof->GetName() << " to directory " << oldDir->GetName();
 
             gDirectory->cd(curdir->GetPath());
           }
@@ -698,9 +696,10 @@ Bool_t CbmTofSimpClusterizer::InitCalibParameter() {
     }
   }
   //   fCalParFile->Delete();
-  gDirectory->cd(
-    oldir
-      ->GetPath());  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  gFile      = oldFile;
+  gDirectory = oldDir;
   LOG(info) << "CbmTofSimpClusterizer::InitCalibParameter: initialization done";
   return kTRUE;
 }
@@ -1141,7 +1140,10 @@ Bool_t CbmTofSimpClusterizer::WriteHistos() {
     return kTRUE;
   }  // if( "" == fsHistoOutFilename )
 
-  TDirectory* oldir = gDirectory;
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   TFile* fHist      = new TFile(fsHistoOutFilename, "RECREATE");
 
   fHist->cd();
@@ -1176,7 +1178,9 @@ Bool_t CbmTofSimpClusterizer::WriteHistos() {
   fhNbSameSide->Write();
   fhNbDigiPerChan->Write();
 
-  gDirectory->cd(oldir->GetPath());
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 
   fHist->Close();
 
diff --git a/reco/detectors/tof/CbmTofTBClusterizer.cxx b/reco/detectors/tof/CbmTofTBClusterizer.cxx
index b9cda61f9084fd7cfd00d0ce294007ce2b35a259..721bf61d20c8b1cb8d006add6dcbd6e1e8dc5af5 100644
--- a/reco/detectors/tof/CbmTofTBClusterizer.cxx
+++ b/reco/detectors/tof/CbmTofTBClusterizer.cxx
@@ -149,11 +149,10 @@ Bool_t CbmTofTBClusterizer::InitCalibParameter() {
 
   LOG(info) << "CbmTofSimpClusterizer::InitCalibParameter: defaults set";
 
-  TDirectory* oldir =
-    gDirectory;  // <= To prevent histos from being sucked in by the param file of the TRootManager!
-  /*
-  gROOT->cd(); // <= To prevent histos from being sucked in by the param file of the TRootManager !
-  */
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
 
 #if 0
   if(0<fCalMode){
@@ -185,7 +184,7 @@ Bool_t CbmTofTBClusterizer::InitCalibParameter() {
           TH2F *htempPos_pfx =(TH2F*) gDirectory->FindObjectAny( Form("cl_CorSmT%01d_sm%03d_rpc%03d_Pos_pfx",iSmType,iSm,iRpc));
           TH2F *htempTOff_pfx=(TH2F*) gDirectory->FindObjectAny( Form("cl_CorSmT%01d_sm%03d_rpc%03d_TOff_pfx",iSmType,iSm,iRpc));
           TH2F *htempTot_pfx =(TH2F*) gDirectory->FindObjectAny( Form("cl_CorSmT%01d_sm%03d_rpc%03d_Tot_pfx",iSmType,iSm,iRpc));
-          if(NULL != htempPos_pfx && NULL != htempTOff_pfx && NULL != htempTot_pfx)  
+          if(NULL != htempPos_pfx && NULL != htempTOff_pfx && NULL != htempTot_pfx)
           {
             Int_t iNbCh = fDigiBdfPar->GetNbChan( iSmType, iRpc );
             Int_t iNbinTot = htempTot_pfx->GetNbinsX();
@@ -196,13 +195,13 @@ Bool_t CbmTofTBClusterizer::InitCalibParameter() {
                 Double_t dTYOff=YMean/fvCPSigPropSpeed[iSmType][iRpc] ;
                 fvCPTOff[iSmType][iSm*iNbRpc+iRpc][iCh][0] += -dTYOff + TMean ;
                 fvCPTOff[iSmType][iSm*iNbRpc+iRpc][iCh][1] += +dTYOff + TMean ;
- 
+
                 Double_t TotMean=((TProfile *)htempTot_pfx)->GetBinContent(iCh+1);  //nh +1 empirical(?)
                 if(1<TotMean){
                   fvCPTotGain[iSmType][iSm*iNbRpc+iRpc][iCh][0] *= TTotMean / TotMean;
                   fvCPTotGain[iSmType][iSm*iNbRpc+iRpc][iCh][1] *= TTotMean / TotMean;
                 }
-                LOG(debug1)<<"CbmTofSimpClusterizer::InitCalibParameter:" 
+                LOG(debug1)<<"CbmTofSimpClusterizer::InitCalibParameter:"
                            <<" SmT "<< iSmType<<" Sm "<<iSm<<" Rpc "<<iRpc<<" Ch "<<iCh
                            <<": YMean "<<YMean<<", TMean "<< TMean
                            <<" -> " << fvCPTOff[iSmType][iSm*iNbRpc+iRpc][iCh][0]
@@ -211,15 +210,15 @@ Bool_t CbmTofTBClusterizer::InitCalibParameter() {
                            <<", NbinTot "<< iNbinTot
                            ;
 
-                TH1D *htempWalk0=(TH1D*)gDirectory->FindObjectAny( 
+                TH1D *htempWalk0=(TH1D*)gDirectory->FindObjectAny(
                                  Form("Cor_SmT%01d_sm%03d_rpc%03d_Ch%03d_S0_Walk_px", iSmType, iSm, iRpc, iCh ));
-                TH1D *htempWalk1=(TH1D*)gDirectory->FindObjectAny( 
+                TH1D *htempWalk1=(TH1D*)gDirectory->FindObjectAny(
                                  Form("Cor_SmT%01d_sm%03d_rpc%03d_Ch%03d_S1_Walk_px", iSmType, iSm, iRpc, iCh ));
-                if(NULL != htempWalk0 && NULL != htempWalk1 ) { // reinitialize Walk array 
+                if(NULL != htempWalk0 && NULL != htempWalk1 ) { // reinitialize Walk array
                   LOG(info)<<"Initialize Walk correction for "
                             <<Form(" SmT%01d_sm%03d_rpc%03d_Ch%03d",iSmType, iSm, iRpc, iCh)
                             ;
-                  if(htempWalk0->GetNbinsX() != nbClWalkBinX) 
+                  if(htempWalk0->GetNbinsX() != nbClWalkBinX)
                     LOG(error)<<"CbmTofSimpClusterizer::InitCalibParameter: Inconsistent Walk histograms"
                               ;
                    for( Int_t iBin = 0; iBin < nbClWalkBinX; iBin++ )
@@ -253,13 +252,13 @@ Bool_t CbmTofTBClusterizer::InitCalibParameter() {
 
            // copy Histo to memory
            TDirectory * curdir = gDirectory;
-           gDirectory->cd( oldir->GetPath() );
+           gDirectory->cd( oldDir->GetPath() );
            TH1D *h1DelTof=(TH1D *)htmpDelTof->Clone(Form("cl_CorSmT%01d_sm%03d_rpc%03d_Trg%02d_DelTof",iSmType,iSm,iRpc,iTrg));
 
            LOG(info)<<" copy histo "
                      <<h1DelTof->GetName()
                      <<" to directory "
-                    <<oldir->GetName()
+                    <<oldDir->GetName()
                     ;
 
            gDirectory->cd( curdir->GetPath() );
@@ -269,9 +268,10 @@ Bool_t CbmTofTBClusterizer::InitCalibParameter() {
   }
 #endif  //0
   //   fCalParFile->Delete();
-  gDirectory->cd(
-    oldir
-      ->GetPath());  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  gFile      = oldFile;
+  gDirectory = oldDir;
   LOG(info) << "CbmTofSimpClusterizer::InitCalibParameter: initialization done";
   return kTRUE;
 }
@@ -417,72 +417,72 @@ void CbmTofTBClusterizer::Exec(Option_t* option) {
              << ", event time " << dEventTime << " ns"
              << ", TOF digis: " << iNbTofDigi;
   /*map<pair<Int_t, Int_t>, list<Int_t> > tofPointDigiInds;
-   
+
    Int_t nofTofPoints = fTofPoints->GetEntries();
-   
+
    for(Int_t iDigInd = 0; iDigInd < iNbTofDigi; ++iDigInd)
    {
       CbmTofDigiExp* pDigi = static_cast<CbmTofDigiExp*> (fTofDigis->At(iDigInd));
       const CbmMatch* pMatch = pDigi->GetMatch();
       Int_t nofLinks = pMatch->GetNofLinks();
-      
+
       for (Int_t iLink = 0; iLink < nofLinks; ++iLink)
       {
          const CbmLink& link = pMatch->GetLink(iLink);
          Int_t tpi = link.GetIndex();
          Int_t tpe = link.GetEntry();
-         
+
          //if (tpi < nofTofPoints)
             tofPointDigiInds[pair<Int_t, Int_t> (tpe, tpi)].push_back(iDigInd);
       }
    }
-   
+
    for (map<pair<Int_t, Int_t>, list<Int_t> >::const_iterator i = tofPointDigiInds.begin(); i != tofPointDigiInds.end(); ++i)
    {
       //const CbmTofPoint* pTofPoint = static_cast<const CbmTofPoint*> (fTofPoints->At(i->first));
       const list<Int_t>& pointDigis = i->second;
       set<Int_t> firedChannels;
       map<Int_t, pair<Double_t, Double_t> > channelDigis;
-      
+
       for (list<Int_t>::const_iterator j = i->second.begin(); j != i->second.end(); ++j)
       {
          const CbmTofDigiExp* pTofDigi = static_cast<const CbmTofDigiExp*> (fTofDigis->At(*j));
          firedChannels.insert(pTofDigi->GetChannel());
-         
+
          if (0 == pTofDigi->GetSide())
             channelDigis[pTofDigi->GetChannel()].first = pTofDigi->GetTime();
          else
             channelDigis[pTofDigi->GetChannel()].second = pTofDigi->GetTime();
-         
+
          //Double_t deltaT = pTofDigi->GetTime() - pTofPoint->GetTime();
          //deltaTHisto->Fill(deltaT);
       }
-      
+
       Double_t minDigiTime = std::numeric_limits<Double_t>::max();
       Double_t maxDigiTime = std::numeric_limits<Double_t>::min();
-      
+
       for (map<Int_t, pair<Double_t, Double_t> >::iterator j = channelDigis.begin(); j != channelDigis.end(); ++j)
       {
          if (0 == j->second.first || 0 == j->second.second)
             continue;
-         
+
          Double_t channelDelta = TMath::Abs(j->second.first - j->second.second);
          deltaChannelTHisto->Fill(channelDelta);
          Double_t channelTime = (j->second.first + j->second.second) / 2;
-         
+
          if (channelTime < minDigiTime)
             minDigiTime = channelTime;
-         
+
          if (channelTime > maxDigiTime)
             maxDigiTime = channelTime;
       }
-      
+
       if (maxDigiTime > minDigiTime)
          deltaPointTHisto->Fill(maxDigiTime - minDigiTime);
-      
+
       nofChannelsTHisto->Fill(firedChannels.size());
    }
-   
+
    return;*/
 
   for (Int_t iDigInd = 0; iDigInd < iNbTofDigi; ++iDigInd) {
@@ -530,35 +530,35 @@ void CbmTofTBClusterizer::Exec(Option_t* option) {
     }
 
     // apply calibration vectors
-    /*pDigi->SetTime(pDigi->GetTime() - // calibrate Digi Time 
+    /*pDigi->SetTime(pDigi->GetTime() - // calibrate Digi Time
          fvCPTOff[pDigi->GetType()]
          [pDigi->GetSm() * fDigiBdfPar->GetNbRpc(pDigi->GetType()) + pDigi->GetRpc()]
          [pDigi->GetChannel()]
          [pDigi->GetSide()]);
 
-      pDigi->SetTot(pDigi->GetTot() * // calibrate Digi ToT 
+      pDigi->SetTot(pDigi->GetTot() * // calibrate Digi ToT
          fvCPTotGain[pDigi->GetType()]
          [pDigi->GetSm() * fDigiBdfPar->GetNbRpc(pDigi->GetType()) + pDigi->GetRpc()]
          [pDigi->GetChannel()]
          [pDigi->GetSide()]);
-      
-      // walk correction 
+
+      // walk correction
       Double_t dTotBinSize = (TOTMax - TOTMin) / 2. / nbClWalkBinX;
       Int_t iWx = (Int_t) ((pDigi->GetTot() - TOTMin / 2.) / dTotBinSize);
-      
+
       if (0 > iWx)
          iWx = 0;
-      
+
       if (iWx > nbClWalkBinX)
          iWx = nbClWalkBinX - 1;
-      
+
       Double_t dDTot = (pDigi->GetTot() - TOTMin / 2.) / dTotBinSize - (Double_t) iWx - 0.5;
       Double_t dWT = fvCPWalk[pDigi->GetType()]
          [pDigi->GetSm() * fDigiBdfPar->GetNbRpc(pDigi->GetType()) + pDigi->GetRpc()]
          [pDigi->GetChannel()]
          [pDigi->GetSide()]
          [iWx];
-      
+
       if (dDTot > 0)
       { // linear interpolation to next bin
          dWT += dDTot * (fvCPWalk[pDigi->GetType()]
diff --git a/reco/detectors/tof/CbmTofTestBeamClusterizer.cxx b/reco/detectors/tof/CbmTofTestBeamClusterizer.cxx
index 6cab269cb6f35c732a2c57f63690ad36d1b80fd8..8a8c039a49108d8e0f68ef183fca1fe704ab44da 100644
--- a/reco/detectors/tof/CbmTofTestBeamClusterizer.cxx
+++ b/reco/detectors/tof/CbmTofTestBeamClusterizer.cxx
@@ -1,7 +1,7 @@
-/** @file CbmTofTestBeamClusterizer.cxx 
- ** author nh  
+/** @file CbmTofTestBeamClusterizer.cxx
+ ** author nh
  ** adopted from
- ** @file CbmTofSimpClusterizer.cxx 
+ ** @file CbmTofSimpClusterizer.cxx
  ** @author Pierre-Alain Loizeau <loizeau@physi.uni-heidelberg.de>
  ** @date 23.08.2013
  **/
@@ -581,11 +581,10 @@ Bool_t CbmTofTestBeamClusterizer::InitCalibParameter() {
 
   LOG(info) << "CbmTofTestBeamClusterizer::InitCalibParameter: defaults set";
 
-  TDirectory* oldir =
-    gDirectory;  // <= To prevent histos from being sucked in by the param file of the TRootManager!
-  /*
-  gROOT->cd(); // <= To prevent histos from being sucked in by the param file of the TRootManager !
-  */
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
 
   if (0 < fCalMode) {
     LOG(info)
@@ -615,7 +614,7 @@ Bool_t CbmTofTestBeamClusterizer::InitCalibParameter() {
       // copy Histo to memory
       TDirectory* curdir = gDirectory;
       if (NULL != hSvel) {
-        gDirectory->cd(oldir->GetPath());
+        gDirectory->cd(oldDir->GetPath());
         // TProfile *hSvelmem = (TProfile *)hSvel->Clone();   (VF) not used
         gDirectory->cd(curdir->GetPath());
       } else {
@@ -626,7 +625,7 @@ Bool_t CbmTofTestBeamClusterizer::InitCalibParameter() {
         TProfile* hFparcur = (TProfile*) gDirectory->FindObjectAny(
           Form("cl_SmT%01d_Fpar%1d", iSmType, iPar));
         if (NULL != hFparcur) {
-          gDirectory->cd(oldir->GetPath());
+          gDirectory->cd(oldDir->GetPath());
           // TProfile *hFparmem = (TProfile *)hFparcur->Clone();   (VF) not used
           gDirectory->cd(curdir->GetPath());
         }
@@ -784,7 +783,7 @@ Bool_t CbmTofTestBeamClusterizer::InitCalibParameter() {
 
             // copy Histo to memory
             // TDirectory * curdir = gDirectory;
-            gDirectory->cd(oldir->GetPath());
+            gDirectory->cd(oldDir->GetPath());
             TH1D* h1DelTof = (TH1D*) htmpDelTof->Clone(
               Form("cl_CorSmT%01d_sm%03d_rpc%03d_Sel%02d_DelTof",
                    iSmType,
@@ -792,8 +791,7 @@ Bool_t CbmTofTestBeamClusterizer::InitCalibParameter() {
                    iRpc,
                    iSel));
 
-            LOG(debug) << " copy histo " << h1DelTof->GetName()
-                       << " to directory " << oldir->GetName();
+            LOG(debug) << " copy histo " << h1DelTof->GetName() << " to directory " << oldDir->GetName();
 
             gDirectory->cd(curdir->GetPath());
           }
@@ -801,9 +799,10 @@ Bool_t CbmTofTestBeamClusterizer::InitCalibParameter() {
     }
   }
   //   fCalParFile->Delete();
-  gDirectory->cd(
-    oldir
-      ->GetPath());  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  // <= To prevent histos from being sucked in by the param file of the TRootManager!
+  gFile      = oldFile;
+  gDirectory = oldDir;
   LOG(info)
     << "CbmTofTestBeamClusterizer::InitCalibParameter: initialization done";
   return kTRUE;
@@ -2361,7 +2360,7 @@ Bool_t CbmTofTestBeamClusterizer::FillHistos() {
       /*
     // find the best dTRef
     fTRefHits=0;
-    dTRef=0.;     // invalidate old value 
+    dTRef=0.;     // invalidate old value
     Double_t dRefChi2=dDoubleMax;
     for( Int_t iHitInd = 0; iHitInd < iNbTofHits; iHitInd++)
     {
@@ -2381,7 +2380,7 @@ Bool_t CbmTofTestBeamClusterizer::FillHistos() {
 	   }
 	 }
 	 if( dNT > 0)
-	 if( dDT2/dNT < dRefChi2 )  
+	 if( dDT2/dNT < dRefChi2 )
          {
 	    fTRefHits=1;
 	    dTRef = pHit->GetTime();
@@ -3074,7 +3073,9 @@ Bool_t CbmTofTestBeamClusterizer::FillHistos() {
 }
 
 Bool_t CbmTofTestBeamClusterizer::WriteHistos() {
-  TDirectory* oldir = gDirectory;
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
   TFile* fHist;
   fHist = new TFile(fOutHstFileName, "RECREATE");
   fHist->cd();
@@ -4698,8 +4699,9 @@ Bool_t CbmTofTestBeamClusterizer::WriteHistos() {
     }
   }
 
-
-  gDirectory->cd(oldir->GetPath());
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 
   fHist->Close();
 
@@ -5719,13 +5721,13 @@ Bool_t CbmTofTestBeamClusterizer::BuildClusters() {
                         0.5, 0.5, 0.5);  // including positioning uncertainty
                                          /*
                                     TVector3 hitPosErr( fChannelInfo->GetSizex()/TMath::Sqrt(12.0),   // Single strips approximation
-                                       0.5, // Use generic value 
+                                       0.5, // Use generic value
                                        1.);
 
                                     */                                       // fDigiBdfPar->GetFeeTimeRes() * fDigiBdfPar->GetSigVel(iSmType,iRpc), // Use the electronics resolution
-                                       //fDigiBdfPar->GetNbGaps( iSmType, iRpc)*
-                                       //fDigiBdfPar->GetGapSize( iSmType, iRpc)/ //10.0 / // Change gap size in cm
-                                       //TMath::Sqrt(12.0) ); // Use full RPC thickness as "Channel" Z size
+                      //fDigiBdfPar->GetNbGaps( iSmType, iRpc)*
+                      //fDigiBdfPar->GetGapSize( iSmType, iRpc)/ //10.0 / // Change gap size in cm
+                      //TMath::Sqrt(12.0) ); // Use full RPC thickness as "Channel" Z size
 
                       // Int_t iDetId = vPtsRef[0]->GetDetectorID();// detID = pt->GetDetectorID() <= from TofPoint
                       // calc mean ch from dPosX=((Double_t)(-iNbCh/2 + iCh)+0.5)*fChannelInfo->GetSizex();
@@ -5834,7 +5836,7 @@ Bool_t CbmTofTestBeamClusterizer::BuildClusters() {
                       fviTrkMul[iSmType][iRpc].push_back(vPtsRef.size());
                       fvdX[iSmType][iRpc].push_back(dWeightedPosX);
                       fvdY[iSmType][iRpc].push_back(dWeightedPosY);
-                      /*  no TofPoint available for data!  
+                      /*  no TofPoint available for data!
                                     fvdDifX[iSmType][iRpc].push_back( vPtsRef[0]->GetX() - dWeightedPosX);
                                     fvdDifY[iSmType][iRpc].push_back( vPtsRef[0]->GetY() - dWeightedPosY);
                                     fvdDifCh[iSmType][iRpc].push_back( fGeoHandler->GetCell( vPtsRef[0]->GetDetectorID() ) -1 -iLastChan );
@@ -6021,7 +6023,7 @@ Bool_t CbmTofTestBeamClusterizer::BuildClusters() {
                 0.5, 0.5, 0.5);  // including positioning uncertainty
                                  /*
                      TVector3 hitPosErr( fChannelInfo->GetSizex()/TMath::Sqrt(12.0),   // Single strips approximation
-                                       0.5, // Use generic value 
+                                       0.5, // Use generic value
                                        1.);
                      */
               //                fDigiBdfPar->GetFeeTimeRes() * fDigiBdfPar->GetSigVel(iSmType,iRpc), // Use the electronics resolution
diff --git a/sim/detectors/tof/CbmTofDigitize.cxx b/sim/detectors/tof/CbmTofDigitize.cxx
index f5e9c11efea2514c1e62b39440011abc3fe58166..0888afb6ed7ff1b5b703d7599a244cf2eff63b5b 100644
--- a/sim/detectors/tof/CbmTofDigitize.cxx
+++ b/sim/detectors/tof/CbmTofDigitize.cxx
@@ -1046,8 +1046,10 @@ Bool_t CbmTofDigitize::WriteHistos() {
     return kTRUE;
   }  // if( "" == fsHistoOutFilename )
 
-  TDirectory* oldir = gDirectory;
-  TFile* fHist      = new TFile(fsHistoOutFilename, "RECREATE");
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+  TFile* fHist       = new TFile(fsHistoOutFilename, "RECREATE");
 
   fHist->cd();
 
@@ -1093,7 +1095,9 @@ Bool_t CbmTofDigitize::WriteHistos() {
     fh1ClusterTotProb[iSmType]->Write();
   }
 
-  gDirectory->cd(oldir->GetPath());
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 
   fHist->Close();