From 3a29d356cb651322e1ab2c99785d00ef7aad2fdb Mon Sep 17 00:00:00 2001 From: Stephan Hageboeck Date: Fri, 20 Mar 2026 11:16:17 +0100 Subject: [PATCH] [tmva] Retire TH1::AddDirectory use in TMVA. Replace uses of TH1::AddDirectory by TDirectory::TContext. It is thread-local and uses the RAII idiom, so state changes cannot inadvertently leak out of TMVA functions. --- tmva/tmva/src/MethodBase.cxx | 5 +---- tmva/tmva/src/MethodLikelihood.cxx | 12 +++--------- tmva/tmva/src/PDEFoam.cxx | 5 +---- tmva/tmva/src/VariableGaussTransform.cxx | 4 +--- 4 files changed, 6 insertions(+), 20 deletions(-) diff --git a/tmva/tmva/src/MethodBase.cxx b/tmva/tmva/src/MethodBase.cxx index 32ca641140c54..608194a490557 100644 --- a/tmva/tmva/src/MethodBase.cxx +++ b/tmva/tmva/src/MethodBase.cxx @@ -1458,13 +1458,10 @@ void TMVA::MethodBase::WriteStateToXML( void* parent ) const void TMVA::MethodBase::ReadStateFromStream( TFile& rf ) { - Bool_t addDirStatus = TH1::AddDirectoryStatus(); - TH1::AddDirectory( 0 ); // this avoids the binding of the hists in PDF to the current ROOT file + TDirectory::TContext dirCtx{nullptr}; // Don't register histograms to current directory fMVAPdfS = (TMVA::PDF*)rf.Get( "MVA_PDF_Signal" ); fMVAPdfB = (TMVA::PDF*)rf.Get( "MVA_PDF_Background" ); - TH1::AddDirectory( addDirStatus ); - ReadWeightsFromStream( rf ); SetTestvarName(); diff --git a/tmva/tmva/src/MethodLikelihood.cxx b/tmva/tmva/src/MethodLikelihood.cxx index 5c715c9571c86..f7033a6eb7e13 100644 --- a/tmva/tmva/src/MethodLikelihood.cxx +++ b/tmva/tmva/src/MethodLikelihood.cxx @@ -668,8 +668,7 @@ void TMVA::MethodLikelihood::WriteWeightsToStream( TFile& ) const void TMVA::MethodLikelihood::ReadWeightsFromXML(void* wghtnode) { TString pname = "PDF_"; - Bool_t addDirStatus = TH1::AddDirectoryStatus(); - TH1::AddDirectory(0); // this avoids the binding of the hists in TMVA::PDF to the current ROOT file + TDirectory::TContext dirCtx{nullptr}; // Don't register histograms to current directory UInt_t nvars=0; gTools().ReadAttr(wghtnode, "NVariables",nvars); void* descnode = gTools().GetChild(wghtnode); @@ -688,7 +687,6 @@ void TMVA::MethodLikelihood::ReadWeightsFromXML(void* wghtnode) (*(*fPDFBgd)[ivar]).ReadXML(pdfnode); descnode = gTools().GetNextChild(descnode); } - TH1::AddDirectory(addDirStatus); } //////////////////////////////////////////////////////////////////////////////// @@ -698,8 +696,7 @@ void TMVA::MethodLikelihood::ReadWeightsFromXML(void* wghtnode) void TMVA::MethodLikelihood::ReadWeightsFromStream( std::istream & istr ) { TString pname = "PDF_"; - Bool_t addDirStatus = TH1::AddDirectoryStatus(); - TH1::AddDirectory(0); // this avoids the binding of the hists in TMVA::PDF to the current ROOT file + TDirectory::TContext dirCtx{nullptr}; // Don't register histograms to current directory for (UInt_t ivar=0; ivar> *(*fPDFSig)[ivar]; istr >> *(*fPDFBgd)[ivar]; } - TH1::AddDirectory(addDirStatus); } //////////////////////////////////////////////////////////////////////////////// @@ -720,13 +716,11 @@ void TMVA::MethodLikelihood::ReadWeightsFromStream( std::istream & istr ) void TMVA::MethodLikelihood::ReadWeightsFromStream( TFile& rf ) { TString pname = "PDF_"; - Bool_t addDirStatus = TH1::AddDirectoryStatus(); - TH1::AddDirectory(0); // this avoids the binding of the hists in TMVA::PDF to the current ROOT file + TDirectory::TContext dirCtx{nullptr}; // Don't register histograms to current directory for (UInt_t ivar=0; ivar