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;
f_file.Open(fileName.c_str());

However this doesn’t work, testing

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

returns 1

and

TFile f_file;
f_file.Open(fileName.c_str());
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;
f_file.reset(TFile::Open(fileName.c_str());

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.