From 237f52b74fa7dbd864098b044b912e7ca7f681b3 Mon Sep 17 00:00:00 2001
From: Florian Uhlig <f.uhlig@gsi.de>
Date: Thu, 11 Feb 2021 17:54:38 +0100
Subject: [PATCH] L1: Add gFile + gDirectory protection

---
 reco/L1/CbmL1.cxx                            | 48 ++++++++++++++++----
 reco/L1/OffLineInterface/CbmL1RichRingQa.cxx | 12 +++--
 2 files changed, 46 insertions(+), 14 deletions(-)

diff --git a/reco/L1/CbmL1.cxx b/reco/L1/CbmL1.cxx
index 2a2d4292d5..bce8567cdc 100644
--- a/reco/L1/CbmL1.cxx
+++ b/reco/L1/CbmL1.cxx
@@ -561,6 +561,10 @@ InitStatus CbmL1::Init()
   CbmMuchGeoScheme* fGeoScheme = CbmMuchGeoScheme::Instance();
 
   if (fUseMUCH) {
+    /// Save old global file and folder pointer to avoid messing with FairRoot
+    TFile* oldFile     = gFile;
+    TDirectory* oldDir = gDirectory;
+
     TFile* file         = new TFile(fDigiFile);
     TObjArray* stations = (TObjArray*) file->Get("stations");
     fGeoScheme->Init(stations, 0);
@@ -569,6 +573,10 @@ InitStatus CbmL1::Init()
       int nLayers                   = station->GetNLayers();
       NMuchStations += nLayers;
     }
+
+    /// Restore old global file and folder pointer to avoid messing with FairRoot
+    gFile      = oldFile;
+    gDirectory = oldDir;
   }
 
   // count TRD stations
@@ -941,7 +949,9 @@ InitStatus CbmL1::Init()
   TString stationName = "Radiation Thickness [%], Station";
   if (fUseMVD) {
     if (fMvdMatBudgetFileName != "") {
-      TFile* oldfile = gFile;
+      /// Save old global file and folder pointer to avoid messing with FairRoot
+      TFile* oldFile     = gFile;
+      TDirectory* oldDir = gDirectory;
       TFile* rlFile  = new TFile(fMvdMatBudgetFileName);
       cout << "MVD Material budget file is " << fMvdMatBudgetFileName << ".\n";
       for (int j = 0, iSta = 0; iSta < algo->NMvdStations; iSta++, j++) {
@@ -974,7 +984,9 @@ InitStatus CbmL1::Init()
       }
       rlFile->Close();
       rlFile->Delete();
-      gFile = oldfile;
+      /// Restore old global file and folder pointer to avoid messing with FairRoot
+      gFile      = oldFile;
+      gDirectory = oldDir;
     }
     else {
       LOG(warn) << "No MVD material budget file is found. Homogenious budget "
@@ -990,7 +1002,9 @@ InitStatus CbmL1::Init()
     }
   }
   if (fStsMatBudgetFileName != "") {
-    TFile* oldfile = gFile;
+    /// Save old global file and folder pointer to avoid messing with FairRoot
+    TFile* oldFile     = gFile;
+    TDirectory* oldDir = gDirectory;
     TFile* rlFile  = new TFile(fStsMatBudgetFileName);
     cout << "STS Material budget file is " << fStsMatBudgetFileName << ".\n";
     for (int j = 1, iSta = algo->NMvdStations; iSta < (algo->NMvdStations + NStsStations); iSta++, j++) {
@@ -1018,7 +1032,9 @@ InitStatus CbmL1::Init()
     }
     rlFile->Close();
     rlFile->Delete();
-    gFile = oldfile;
+    /// Restore old global file and folder pointer to avoid messing with FairRoot
+    gFile      = oldFile;
+    gDirectory = oldDir;
   }
   else {
     LOG(warn) << "No STS material budget file is found. Homogenious budget "
@@ -1033,7 +1049,9 @@ InitStatus CbmL1::Init()
 
   if (fUseMUCH)
     if (fMuchMatBudgetFileName != "") {
-      TFile* oldfile = gFile;
+      /// Save old global file and folder pointer to avoid messing with FairRoot
+      TFile* oldFile     = gFile;
+      TDirectory* oldDir = gDirectory;
       TFile* rlFile  = new TFile(fMuchMatBudgetFileName);
       cout << "Much Material budget file is " << fMuchMatBudgetFileName << ".\n";
       for (int j = 1, iSta = (NStsStations + NMvdStations); iSta < (NStsStations + NMvdStations + NMuchStations);
@@ -1073,7 +1091,9 @@ InitStatus CbmL1::Init()
       }
       rlFile->Close();
       rlFile->Delete();
-      gFile = oldfile;
+      /// Restore old global file and folder pointer to avoid messing with FairRoot
+      gFile      = oldFile;
+      gDirectory = oldDir;
     }
     else {
       LOG(warn) << "No Much material budget file is found. Homogenious budget "
@@ -1088,7 +1108,9 @@ InitStatus CbmL1::Init()
 
   if (fUseTRD)
     if (fTrdMatBudgetFileName != "") {
-      TFile* oldfile = gFile;
+      /// Save old global file and folder pointer to avoid messing with FairRoot
+      TFile* oldFile     = gFile;
+      TDirectory* oldDir = gDirectory;
       TFile* rlFile  = new TFile(fTrdMatBudgetFileName);
       cout << "TRD Material budget file is " << fTrdMatBudgetFileName << ".\n";
       for (int j = 1, iSta = (NStsStations + NMvdStations + NMuchStations);
@@ -1128,7 +1150,9 @@ InitStatus CbmL1::Init()
       }
       rlFile->Close();
       rlFile->Delete();
-      gFile = oldfile;
+      /// Restore old global file and folder pointer to avoid messing with FairRoot
+      gFile      = oldFile;
+      gDirectory = oldDir;
     }
     else {
       LOG(warn) << "No TRD material budget file is found. Homogenious budget "
@@ -1144,7 +1168,9 @@ InitStatus CbmL1::Init()
 
   if (fUseTOF)
     if (fTofMatBudgetFileName != "") {
-      TFile* oldfile = gFile;
+      /// Save old global file and folder pointer to avoid messing with FairRoot
+      TFile* oldFile     = gFile;
+      TDirectory* oldDir = gDirectory;
       TFile* rlFile  = new TFile(fTofMatBudgetFileName);
       cout << "TOF Material budget file is " << fTofMatBudgetFileName << ".\n";
       for (int j = 1, iSta = (NStsStations + NMvdStations + NMuchStations + NTrdStations);
@@ -1184,7 +1210,9 @@ InitStatus CbmL1::Init()
       }
       rlFile->Close();
       rlFile->Delete();
-      gFile = oldfile;
+      /// Restore old global file and folder pointer to avoid messing with FairRoot
+      gFile      = oldFile;
+      gDirectory = oldDir;
     }
     else {
       LOG(warn) << "No TOF material budget file is found. Homogenious budget "
diff --git a/reco/L1/OffLineInterface/CbmL1RichRingQa.cxx b/reco/L1/OffLineInterface/CbmL1RichRingQa.cxx
index 0c6031e5e0..3fa5d26113 100644
--- a/reco/L1/OffLineInterface/CbmL1RichRingQa.cxx
+++ b/reco/L1/OffLineInterface/CbmL1RichRingQa.cxx
@@ -392,7 +392,7 @@ void CbmL1RichRingQa::Exec(Option_t* /*option*/) {
     AllRingsDown->Draw("*");
     // Draw N Hits for rings
     /*
-    TText* text1 = new TText( ArrRingX[i] - ArrRingR[i]/5 , ArrRingY[i] + ArrRingR[i]/5  , st.Data() ); 
+    TText* text1 = new TText( ArrRingX[i] - ArrRingR[i]/5 , ArrRingY[i] + ArrRingR[i]/5  , st.Data() );
     text1->SetTextAlign(12);
     text1->SetTextSize(0.01);
     text1->Draw();
@@ -663,7 +663,7 @@ void CbmL1RichRingQa::Exec(Option_t* /*option*/) {
        st1 += ring.NHits;
        st1 += "  P = ";
        st1 += ring.P;
-       TText* textMc1 = new TText( ring.x + ring.r/5 , -ring.y - ring.r/5 , st1.Data() ); 
+       TText* textMc1 = new TText( ring.x + ring.r/5 , -ring.y - ring.r/5 , st1.Data() );
        textMc1->SetTextAlign(12);
        textMc1->SetTextSize(0.01);
        textMc1->SetTextColor(1);
@@ -1132,14 +1132,18 @@ void CbmL1RichRingQa::Exec(Option_t* /*option*/) {
   }
 
   // Open output file and write histograms
-  TDirectory* curr = gDirectory;
+  /// Save old global file and folder pointer to avoid messing with FairRoot
+  TFile* oldFile     = gFile;
+  TDirectory* oldDir = gDirectory;
   TFile* outfile   = new TFile("L1RingQaHisto.root", "RECREATE");
   outfile->cd();
   TIter hiter(listHisto);
   while (TObject* obj = hiter())
     obj->Write();
   outfile->Close();
-  curr->cd();
+  /// Restore old global file and folder pointer to avoid messing with FairRoot
+  gFile      = oldFile;
+  gDirectory = oldDir;
 }
 
 void CbmL1RichRingQa::Finish() {
-- 
GitLab