diff --git a/reco/detectors/rich/CbmRichRadiusCorrection.h b/reco/detectors/rich/CbmRichRadiusCorrection.h
index 71f0b7f1a53593f1e0ea3fd19a31e17e93c0e96f..72f8304a0ae451632cb8447634897a306fc369be 100644
--- a/reco/detectors/rich/CbmRichRadiusCorrection.h
+++ b/reco/detectors/rich/CbmRichRadiusCorrection.h
@@ -65,7 +65,10 @@ private:
     string fileName = gSystem->Getenv("VMCWORKDIR");
     fileName += "/parameters/rich/radius_correction_map_compact.root";
 
-    TDirectory* current = gDirectory;
+    /// Save old global file and folder pointer to avoid messing with FairRoot
+    TFile* oldFile     = gFile;
+    TDirectory* oldDir = gDirectory;
+
     TFile* file         = new TFile(fileName.c_str(), "READ");
 
     if (NULL == file || !file->IsOpen()) {
@@ -83,7 +86,10 @@ private:
 
     file->Close();
     delete file;
-    current->cd();
+
+    /// Restore old global file and folder pointer to avoid messing with FairRoot
+    gFile      = oldFile;
+    gDirectory = oldDir;
   }
 
   static TH2D* fhMapAaxisXY;
diff --git a/reco/detectors/rich/alignment/CbmHistManager.cxx b/reco/detectors/rich/alignment/CbmHistManager.cxx
index 8085f37c0b3af82663e98f118e7ea29da8e104e4..8bbf29dcdedc0ede133eccfa5c1e0e5bd87c30a6 100644
--- a/reco/detectors/rich/alignment/CbmHistManager.cxx
+++ b/reco/detectors/rich/alignment/CbmHistManager.cxx
@@ -105,7 +105,11 @@ void CbmHistManager::WriteToFile() {
 void CbmHistManager::WriteToFileNew(const string& str) {
   std::string histoName = "";
   map<string, TNamed*>::iterator it;
-  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(str.c_str(), "RECREATE");
   fHist->cd();
 
@@ -134,7 +138,10 @@ void CbmHistManager::WriteToFileNew(const string& str) {
 
   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/reco/detectors/rich/alignment/CbmRichAlignment.cxx b/reco/detectors/rich/alignment/CbmRichAlignment.cxx
index 7dfa4de080d7d4c71b7b793a6236d587bc6f79ea..81da33d0a43c5ec2badb1aed16997e1134b2bedd 100644
--- a/reco/detectors/rich/alignment/CbmRichAlignment.cxx
+++ b/reco/detectors/rich/alignment/CbmRichAlignment.cxx
@@ -535,10 +535,18 @@ void CbmRichAlignment::DrawFit(vector<Double_t>& outputFit, Int_t thresh) {
 }
 
 void CbmRichAlignment::DrawHistFromFile(TString fileName) {
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   fHM         = new CbmHistManager();
   TFile* file = new TFile(fileName, "READ");
   fHM->ReadFromFile(file);
   DrawHistAlignment();
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 }
 
 void CbmRichAlignment::Finish() {
diff --git a/reco/detectors/rich/alignment/CbmRichCorrection.cxx b/reco/detectors/rich/alignment/CbmRichCorrection.cxx
index 56246b254cdd1d1f1c0fd03b24420a7d0f9e3a48..a730965f06f3589198d76a8c10a693eb9bd28c7f 100644
--- a/reco/detectors/rich/alignment/CbmRichCorrection.cxx
+++ b/reco/detectors/rich/alignment/CbmRichCorrection.cxx
@@ -1541,10 +1541,18 @@ void CbmRichCorrection::DrawHistProjection() {
 }
 
 void CbmRichCorrection::DrawHistFromFile(TString fileName) {
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   fHM         = new CbmHistManager();
   TFile* file = new TFile(fileName, "READ");
   fHM->ReadFromFile(file);
   DrawHistProjection();
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 }
 
 void CbmRichCorrection::Finish() {
diff --git a/reco/detectors/rich/alignment/CbmRichCorrectionVector.cxx b/reco/detectors/rich/alignment/CbmRichCorrectionVector.cxx
index a410c2ced29c0a045338deedbbbdc4b1d280676e..9c791bfe903797377fc2c4ed5dda394a7445c01e 100644
--- a/reco/detectors/rich/alignment/CbmRichCorrectionVector.cxx
+++ b/reco/detectors/rich/alignment/CbmRichCorrectionVector.cxx
@@ -1566,10 +1566,18 @@ void CbmRichCorrectionVector::DrawHistProjection() {
 }
 
 void CbmRichCorrectionVector::DrawHistFromFile(TString fileName) {
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   fHM         = new CbmHistManager();
   TFile* file = new TFile(fileName, "READ");
   fHM->ReadFromFile(file);
   DrawHistMapping();
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 }
 
 void CbmRichCorrectionVector::Finish() {
diff --git a/reco/detectors/rich/alignment/CbmRichPMTMapping.cxx b/reco/detectors/rich/alignment/CbmRichPMTMapping.cxx
index bf98f8925294b2936dec7780d901dae94d9055b5..1f8901700d1d679bdc5dd2310ea75753aa5a77c0 100644
--- a/reco/detectors/rich/alignment/CbmRichPMTMapping.cxx
+++ b/reco/detectors/rich/alignment/CbmRichPMTMapping.cxx
@@ -1473,10 +1473,18 @@ void CbmRichPMTMapping::DrawHist() {
 }
 
 void CbmRichPMTMapping::DrawHistFromFile(TString fileName) {
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   fHM         = new CbmHistManager();
   TFile* file = new TFile(fileName, "READ");
   fHM->ReadFromFile(file);
   DrawHist();
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 }
 
 void CbmRichPMTMapping::Finish() {
diff --git a/reco/detectors/rich/alignment/CbmRichRecoQa.cxx b/reco/detectors/rich/alignment/CbmRichRecoQa.cxx
index dc9d3e4c5460282e5b5a78fac0377ea5bbdbe13b..37a83a4261ebddc5ad7b8e60ba9faa279cfcd9f3 100644
--- a/reco/detectors/rich/alignment/CbmRichRecoQa.cxx
+++ b/reco/detectors/rich/alignment/CbmRichRecoQa.cxx
@@ -1223,6 +1223,10 @@ void CbmRichRecoQa::DrawFromFile(const string& fileName,
                                  const string& outputDir) {
   fOutputDir = outputDir;
 
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   if (fHM != nullptr) delete fHM;
 
   fHM         = new CbmHistManager();
@@ -1232,6 +1236,10 @@ void CbmRichRecoQa::DrawFromFile(const string& fileName,
   DrawHist();
 
   fHM->SaveCanvasToImage(fOutputDir);
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 }
 
 ClassImp(CbmRichRecoQa)
diff --git a/reco/detectors/rich/mcbm/CbmRichMCbmAerogelAna.cxx b/reco/detectors/rich/mcbm/CbmRichMCbmAerogelAna.cxx
index 3fae18de7b729012ea446a9b76823f843b655cec..4d8d7d85068a03cbb183e2d6550a3ed681ccc98e 100644
--- a/reco/detectors/rich/mcbm/CbmRichMCbmAerogelAna.cxx
+++ b/reco/detectors/rich/mcbm/CbmRichMCbmAerogelAna.cxx
@@ -544,7 +544,10 @@ void CbmRichMCbmAerogelAna::Finish() {
   }
 
   if (this->fDoWriteHistToFile) {
+    /// Save old global file and folder pointer to avoid messing with FairRoot
+    TFile* oldFile    = gFile;
     TDirectory* oldir = gDirectory;
+
     std::string s     = fOutputDir + "/RecoHists.root";
     TFile* outFile    = new TFile(s.c_str(), "RECREATE");
     if (outFile->IsOpen()) {
@@ -553,6 +556,8 @@ void CbmRichMCbmAerogelAna::Finish() {
       outFile->Close();
       std::cout << "Done!" << std::endl;
     }
+    /// Restore old global file and folder pointer to avoid messing with FairRoot
+    gFile = oldFile;
     gDirectory->cd(oldir->GetPath());
   }
 }
@@ -562,6 +567,10 @@ void CbmRichMCbmAerogelAna::DrawFromFile(const string& fileName,
                                          const string& outputDir) {
   fOutputDir = outputDir;
 
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   if (fHM != nullptr) delete fHM;
 
   fHM         = new CbmHistManager();
@@ -570,6 +579,10 @@ void CbmRichMCbmAerogelAna::DrawFromFile(const string& fileName,
   DrawHist();
 
   fHM->SaveCanvasToImage(fOutputDir);
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 }
 
 
diff --git a/reco/detectors/rich/mcbm/CbmRichMCbmQa.cxx b/reco/detectors/rich/mcbm/CbmRichMCbmQa.cxx
index 1dc5c49d5e2d13005340d50999c0af523912b7a1..e653fd2bec8741e0b6aced5c1032a149cf76c279 100644
--- a/reco/detectors/rich/mcbm/CbmRichMCbmQa.cxx
+++ b/reco/detectors/rich/mcbm/CbmRichMCbmQa.cxx
@@ -394,17 +394,17 @@ void CbmRichMCbmQa::Exec(Option_t* /*option*/) {
         if (point == NULL) continue;
 
         Int_t mcTrackIdPoint = point->GetTrackID();
-      
+
         if (mcTrackIdPoint == NULL) continue;
         CbmMCTrack* mcTrack = (CbmMCTrack*)fMCTracks->At(mcTrackIdPoint);
         if (mcTrack == NULL) continue;
-       
-        
+
+
         Int_t pdg = TMath::Abs(mcTrack->GetPdgCode());
        // cout << "pdg: " << pdg << endl;
-        fHM->H2("fh_rich_points_xy")->Fill(point->GetX(), point->GetY());   
-        
-        
+        fHM->H2("fh_rich_points_xy")->Fill(point->GetX(), point->GetY());
+
+
         for(int iT = 0; iT < nofTofHits; iT++){
             CbmTofHit* tofHit = static_cast<CbmTofHit*>(fTofHits->At(iT));
             if(NULL == tofHit) continue;
@@ -418,18 +418,18 @@ void CbmRichMCbmQa::Exec(Option_t* /*option*/) {
             CbmMCTrack* mcTrackTof = (CbmMCTrack*)fMCTracks->At(mcTrackIdTofHit);
             if (NULL == mcTrackTof) continue;
 
-           
+
             Double_t time = tofHit->GetTime();
             Double_t timect = 0.2998*time;          //time in ns, timect in m
             Double_t trackLength = tofHit->GetR()/100;
             Double_t beta = trackLength/timect;
-            
-            
+
+
             if(mcTrackIdPoint == mcTrackIdTofHit){
                 fHM->H1("fh_beta_dis_all")->Fill(beta);
             }
         }
-     
+
         for(int iRing = 0; iRing < nofRichRings; iRing++){
             CbmRichRing* ring = static_cast<CbmRichRing*>(fRichRings->At(iRing));
             if (NULL == ring) continue;
@@ -437,11 +437,11 @@ void CbmRichMCbmQa::Exec(Option_t* /*option*/) {
             if (NULL == ringMatch) continue;
             Int_t mcTrackIdRing = ringMatch->GetMatchedLink().GetIndex();
             if (mcTrackIdRing < 0) continue;
-        
-        
+
+
             CbmMCTrack* mcTrackRing = (CbmMCTrack*)fMCTracks->At(mcTrackIdRing);
             if(mcTrackRing == NULL) continue;
-            
+
             if(mcTrackIdTofHit == mcTrackIdRing){
                // cout << "mcTrackIdTofHit: " << mcTrackIdTofHit << endl;
                // cout << "mcTrackIdRing: " << mcTrackIdRing << endl;
@@ -451,11 +451,11 @@ void CbmRichMCbmQa::Exec(Option_t* /*option*/) {
                     fHM->H2("fh_radius_beta")->Fill(beta, radius);
                     fHM->H1("fh_beta_dis_ring")->Fill(beta);
                     fHM->H2("fh_radius_momentum")->Fill(momTotal, radius);
-                    
+
                 }
             }
-        }              
-    }           
+        }
+    }
 
 */
 
@@ -469,17 +469,17 @@ void CbmRichMCbmQa::Exec(Option_t* /*option*/) {
         if (NULL == ringMatch) continue;
         Int_t mcTrackIdRing = ringMatch->GetMatchedLink().GetIndex();
         if (mcTrackIdRing < 0) continue;
-        
-        
+
+
         CbmMCTrack* mcTrackRing = (CbmMCTrack*)fMCTracks->At(mcTrackIdRing);
         if(mcTrackRing == NULL) continue;
         Double_t radius = ring->GetRadius();
-        
+
         Int_t motherId = mcTrackRing->GetMotherId();
        // Int_t pdg = TMath::Abs(mcTrackRing->GetPdgCode());
        // cout << "pdg" << pdg << endl;
-        
-      
+
+
         int nofHits = ring->GetNofHits();
         TVector3 vec1;
         mcTrackRing->GetMomentum(vec1);
@@ -492,7 +492,7 @@ void CbmRichMCbmQa::Exec(Option_t* /*option*/) {
         fHM->H1("fh_rich_ring_radius")->Fill(radius);
         //fHM->H2("fh_radius_momentum")->Fill(momTotal1, radius);
         cout << "richRingId: " << mcTrackIdRing << endl;
-        
+
         for (int iH = 0; iH < nofHits; iH++) {
             Int_t hitInd = ring->GetHit(iH);
             CbmRichHit* hit = (CbmRichHit*) fRichHits->At(hitInd);
@@ -503,10 +503,10 @@ void CbmRichMCbmQa::Exec(Option_t* /*option*/) {
             Double_t dR = radius - TMath::Sqrt( (cX - hitX)*( cX - hitX) + (cY - hitY)*(cY - hitY) );
             fHM->H1("fh_dR")->Fill(dR);
         }
-     
+
 
         for(int iT = 0; iT < nofTofHits; iT++){
-            
+
             CbmTofHit* tofHit = static_cast<CbmTofHit*>(fTofHits->At(iT));
             if(NULL == tofHit) continue;
             CbmTrackMatchNew* tofHitMatch = (CbmTrackMatchNew*) fTofHitMatches->At(iT);
@@ -526,10 +526,10 @@ void CbmRichMCbmQa::Exec(Option_t* /*option*/) {
             Double_t timect = 0.2998*time;          //time in ns, timect in m
             Double_t trackLength = tofHit->GetR()/100;
             Double_t beta = trackLength/timect;
-           
+
             Double_t mass2 = TMath::Power(momTotal, 2.) * (TMath::Power(1/beta, 2) - 1);     //m² = p²*((1/beta)²-1)
 
-            
+
             if(mcTrackIdTofHit == mcTrackIdRing){
                 //cout << "mcTrackIdTofHit: " << mcTrackIdTofHit << endl;
                 //cout << "mcTrackIdRing: " << mcTrackIdRing << endl;
@@ -539,10 +539,10 @@ void CbmRichMCbmQa::Exec(Option_t* /*option*/) {
                   //  fHM->H2("fh_radius_beta")->Fill(beta, radius);
                   //  fHM->H1("fh_beta_dis_ring")->Fill(beta);
                   //  fHM->H2("fh_radius_momentum")->Fill(momTotal, radius);
-                    
+
                 }
             }
-        }    
+        }
     }
     */
 }
@@ -746,6 +746,10 @@ void CbmRichMCbmQa::DrawFromFile(const string& fileName,
                                  const string& outputDir) {
   fOutputDir = outputDir;
 
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   if (fHM != nullptr) delete fHM;
 
   fHM         = new CbmHistManager();
@@ -755,6 +759,10 @@ void CbmRichMCbmQa::DrawFromFile(const string& fileName,
   DrawHist();
 
   fHM->SaveCanvasToImage(fOutputDir);
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 }
 
 
diff --git a/reco/detectors/rich/mcbm/CbmRichMCbmQaReal.cxx b/reco/detectors/rich/mcbm/CbmRichMCbmQaReal.cxx
index 338c8eb515c44c8661a188a65dfa241e98668055..302c048974239bb898d83b819d842ffe94a674da 100644
--- a/reco/detectors/rich/mcbm/CbmRichMCbmQaReal.cxx
+++ b/reco/detectors/rich/mcbm/CbmRichMCbmQaReal.cxx
@@ -2949,7 +2949,10 @@ void CbmRichMCbmQaReal::Finish() {
   }
 
   if (this->fDoWriteHistToFile) {
+    /// Save old global file and folder pointer to avoid messing with FairRoot
+    TFile* oldFile    = gFile;
     TDirectory* oldir = gDirectory;
+
     std::string s     = fOutputDir + "/RecoHists.root";
     TFile* outFile    = new TFile(s.c_str(), "RECREATE");
     if (outFile->IsOpen()) {
@@ -2958,6 +2961,8 @@ void CbmRichMCbmQaReal::Finish() {
       outFile->Close();
       std::cout << "Done!" << std::endl;
     }
+    /// Restore old global file and folder pointer to avoid messing with FairRoot
+    gFile = oldFile;
     gDirectory->cd(oldir->GetPath());
   }
 
@@ -3001,6 +3006,10 @@ void CbmRichMCbmQaReal::DrawFromFile(const string& fileName,
                                      const string& outputDir) {
   fOutputDir = outputDir;
 
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   if (fHM != nullptr) delete fHM;
 
   fHM         = new CbmHistManager();
@@ -3009,6 +3018,10 @@ void CbmRichMCbmQaReal::DrawFromFile(const string& fileName,
   DrawHist();
 
   fHM->SaveCanvasToImage(fOutputDir);
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 }
 
 bool CbmRichMCbmQaReal::isAccmRICH(CbmTofTracklet* track) {
diff --git a/reco/detectors/rich/mcbm/CbmRichMCbmQaRichOnly.cxx b/reco/detectors/rich/mcbm/CbmRichMCbmQaRichOnly.cxx
index 68c6a3eda6712125d576c0f36465f318664e2047..178faf1011f62387ef2fbe33fdc00552191f174a 100644
--- a/reco/detectors/rich/mcbm/CbmRichMCbmQaRichOnly.cxx
+++ b/reco/detectors/rich/mcbm/CbmRichMCbmQaRichOnly.cxx
@@ -716,7 +716,10 @@ void CbmRichMCbmQaRichOnly::Finish() {
   }
 
   if (this->fDoWriteHistToFile) {
+    /// Save old global file and folder pointer to avoid messing with FairRoot
+    TFile* oldFile    = gFile;
     TDirectory* oldir = gDirectory;
+
     std::string s     = fOutputDir + "/RecoHists.root";
     TFile* outFile    = new TFile(s.c_str(), "RECREATE");
     if (outFile->IsOpen()) {
@@ -725,6 +728,8 @@ void CbmRichMCbmQaRichOnly::Finish() {
       outFile->Close();
       std::cout << "Done!" << std::endl;
     }
+    /// Restore old global file and folder pointer to avoid messing with FairRoot
+    gFile = oldFile;
     gDirectory->cd(oldir->GetPath());
   }
 }
@@ -734,6 +739,10 @@ void CbmRichMCbmQaRichOnly::DrawFromFile(const string& fileName,
                                          const string& outputDir) {
   fOutputDir = outputDir;
 
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   if (fHM != nullptr) delete fHM;
 
   fHM         = new CbmHistManager();
@@ -742,6 +751,10 @@ void CbmRichMCbmQaRichOnly::DrawFromFile(const string& fileName,
   DrawHist();
 
   fHM->SaveCanvasToImage(fOutputDir);
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 }
 
 bool CbmRichMCbmQaRichOnly::doToT(CbmRichHit* hit) {
diff --git a/reco/detectors/rich/qa/CbmRichGeoTest.cxx b/reco/detectors/rich/qa/CbmRichGeoTest.cxx
index 91f216c502a0e00eeeab4737813b61a91f9eb8da..74870cf0e547eca36585bdaa73cefdd1631052aa 100644
--- a/reco/detectors/rich/qa/CbmRichGeoTest.cxx
+++ b/reco/detectors/rich/qa/CbmRichGeoTest.cxx
@@ -1852,6 +1852,10 @@ void CbmRichGeoTest::DrawFromFile(const string& fileName,
                                   const string& outputDir) {
   fOutputDir = outputDir;
 
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   if (fHM != NULL) delete fHM;
 
   fHM         = new CbmHistManager();
@@ -1861,6 +1865,10 @@ void CbmRichGeoTest::DrawFromFile(const string& fileName,
   DrawHist();
 
   fHM->SaveCanvasToImage(fOutputDir, "png,eps");
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 }
 
 ClassImp(CbmRichGeoTest)
diff --git a/reco/detectors/rich/qa/CbmRichGeoTestOpt.cxx b/reco/detectors/rich/qa/CbmRichGeoTestOpt.cxx
index 2d075053526bcd7fdf1b73024bf566f522222081..edb4be04de7f977bcb5b4ab6405b54d59fbc8ed3 100644
--- a/reco/detectors/rich/qa/CbmRichGeoTestOpt.cxx
+++ b/reco/detectors/rich/qa/CbmRichGeoTestOpt.cxx
@@ -70,12 +70,22 @@ CbmRichGeoTestOpt::H1MeanRms(CbmRichGeoTestOptFileEnum fileEnum,
                              const string& histName) {
   string path = GetFilePath(fileEnum, iFile);
   if (path == "") return make_pair(0., 0.);
+
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   TFile* file = new TFile(path.c_str(), "READ");
   if (file == nullptr) return make_pair(0., 0.);
   TH1D* hist = (TH1D*) file->Get(histName.c_str());
   if (hist == nullptr) return make_pair(0., 0.);
   double mean = hist->GetMean();
   double rms  = hist->GetRMS();
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
+
   file->Close();
   delete file;
   return make_pair(mean, rms);
@@ -87,6 +97,11 @@ CbmRichGeoTestOpt::H2ProjYMeanRms(CbmRichGeoTestOptFileEnum fileEnum,
                                   const string& histName) {
   string path = GetFilePath(fileEnum, iFile);
   if (path == "") return make_pair(0., 0.);
+
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   TFile* file = new TFile(path.c_str(), "READ");
   if (file == nullptr) return make_pair(0., 0.);
   TH2D* hist = (TH2D*) file->Get(histName.c_str());
@@ -94,6 +109,11 @@ CbmRichGeoTestOpt::H2ProjYMeanRms(CbmRichGeoTestOptFileEnum fileEnum,
   TH1D* py = hist->ProjectionY((histName + to_string(iFile) + "_py").c_str());
   double mean = py->GetMean();
   double rms  = py->GetRMS();
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
+
   file->Close();
   delete file;
   return make_pair(mean, rms);
@@ -104,11 +124,21 @@ double CbmRichGeoTestOpt::HEntries(CbmRichGeoTestOptFileEnum fileEnum,
                                    const string& histName) {
   string path = GetFilePath(fileEnum, iFile);
   if (path == "") return 0.;
+
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   TFile* file = new TFile(path.c_str(), "READ");
   if (file == nullptr) return 0.;
   TH1* hist = (TH1*) file->Get(histName.c_str());
   if (hist == nullptr) return 0.;
   double entries = hist->GetEntries();  //hist->Integral();
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
+
   file->Close();
   delete file;
   return entries;
diff --git a/reco/detectors/rich/qa/CbmRichRecoQa.cxx b/reco/detectors/rich/qa/CbmRichRecoQa.cxx
index 7bcf88e901f13f0042f1ca847abc82c77a72f548..a7aaf87c033f2f5be05e54eb1e51c01e6378d5bc 100644
--- a/reco/detectors/rich/qa/CbmRichRecoQa.cxx
+++ b/reco/detectors/rich/qa/CbmRichRecoQa.cxx
@@ -843,6 +843,10 @@ void CbmRichRecoQa::DrawFromFile(const string& fileName,
                                  const string& outputDir) {
   fOutputDir = outputDir;
 
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
+
   if (fHM != nullptr) delete fHM;
 
   fHM         = new CbmHistManager();
@@ -852,6 +856,10 @@ void CbmRichRecoQa::DrawFromFile(const string& fileName,
   DrawHist();
 
   fHM->SaveCanvasToImage(fOutputDir);
+
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 }
 
 ClassImp(CbmRichRecoQa)