Opening a TFile after initialization

I need to open a TFile after it is already initialized.

I thought I could just do:
TFile f_file;

However this doesn’t work, testing

TFile f_file(fileName.c_str());
std::cout << f_file.IsOpen() << std::endl;

returns 1


TFile f_file;
std::cout << f_file.IsOpen() << std::endl;

returns 0.

I would expect these to return the exact same thing. What is wrong with this, and how do I actually open a TFile after initialization?

The best is to use

std::unique_ptr<TFile> f_file;

Note TFile::Open is static function that allocate a new TFile based on the URL passed as argument. The actually type of the object will a class derived from TFile in the cases where the file not local (reading it from an xrootd server for example).

Using a TFile object instead of TFile::Open restrict to only reading local files.

You can not change the filename of a TFile after the original constructor.