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