Open and Close multiple TFile simutaneously


I am working with more than one file simutaneously. For example, I want to read the data from one file out, after some modification, I write the data in other file. But recently I got a problem with the order to open and close the TFile objects. Only the current active TFile stores the TTree objects that is resident in the memory. Here is some example code, that helps to understand my problem.

#include <TTree.h>
#include <TFile.h>

using namespace std;

int main()

long value1 = 0;
long value2 = 0;

TFile f(“thefile1.root”,“recreate”);

TFile g(“thefile2.root”,“recreate”); // open and close without doing anything

//f.ReOpen(“update”); // without calling of reopen, none of the files will be written correctly

TTree tr1 = new TTree(“T1”,“thefirsttree”);
tr2 = new TTree(“T2”,“test”);

value1 = 1111; // some number to test the TTree
value2 = 2222;

// g.Write();

return 0;

In this Code, if I don’t call the ReOpen() function, the files won’t get saved in the right way. But this is not a solution for complicated cases. Is there better, more elegant way to switch the files? something like setCurrentFile()?

Thanks very much!

Simon Lu

simply call; before creating your TTrees


It works, thank you very much :stuck_out_tongue: