Skip to content

Commit

Permalink
[tmva] Properly manage TFile objects in tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
vepadulano committed Apr 11, 2024
1 parent 7875571 commit 9692f17
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions tutorials/tmva/TMVAClassification.C
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,13 @@ int TMVAClassification( TString myMethodList = "" )

// Read training and test data
// (it is also possible to use ASCII format as input -> see TMVA Users Guide)
TFile *input(0);
TString fname = "./tmva_class_example.root";
if (!gSystem->AccessPathName( fname )) {
input = TFile::Open( fname ); // check if file in local directory exists
}
else {
TFile::SetCacheFileDir(".");
input = TFile::Open("http://root.cern/files/tmva_class_example.root", "CACHEREAD");
}
if (!input) {
std::cout << "ERROR: could not open data file" << std::endl;
exit(1);
// Set the cache directory for the TFile to the current directory. The input
// data file will be downloaded here if not present yet, then it will be read
// from the cache path directly.
TFile::SetCacheFileDir(".");
std::unique_ptr<TFile> input{TFile::Open("http://root.cern/files/tmva_class_example.root", "CACHEREAD")};
if (!input || input->IsZombie()) {
throw std::runtime_error("ERROR: could not open data file");
}
std::cout << "--- TMVAClassification : Using input file: " << input->GetName() << std::endl;

Expand All @@ -193,8 +188,11 @@ int TMVAClassification( TString myMethodList = "" )
TTree *background = (TTree*)input->Get("TreeB");

// Create a ROOT output file where TMVA will store ntuples, histograms, etc.
TString outfileName( "TMVAC.root" );
TFile* outputFile = TFile::Open( outfileName, "RECREATE" );
TString outfileName("TMVAC.root");
std::unique_ptr<TFile> outputFile{TFile::Open(outfileName, "RECREATE")};
if (!outputFile || outputFile->IsZombie()) {
throw std::runtime_error("ERROR: could not open output file");
}

// Create the factory object. Later you can choose the methods
// whose performance you'd like to investigate. The factory is
Expand All @@ -207,7 +205,7 @@ int TMVAClassification( TString myMethodList = "" )
// All TMVA output can be suppressed by removing the "!" (not) in
// front of the "Silent" argument in the option string
auto factory = std::make_unique<TMVA::Factory>(
"TMVAClassification", outputFile,
"TMVAClassification", outputFile.get(),
"!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification");
auto dataloader_raii = std::make_unique<TMVA::DataLoader>("dataset");
auto *dataloader = dataloader_raii.get();
Expand Down Expand Up @@ -538,7 +536,7 @@ int TMVAClassification( TString myMethodList = "" )
// --------------------------------------------------------------

// Save the output
outputFile->Close();
outputFile->Write();

std::cout << "==> Wrote root file: " << outputFile->GetName() << std::endl;
std::cout << "==> TMVAClassification is done!" << std::endl;
Expand Down

0 comments on commit 9692f17

Please sign in to comment.