Open and Close multiple TFile simutaneously

Hi,

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>
#include

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
g.Close();

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

TTree tr1 = new TTree(“T1”,“thefirsttree”);
tr1->Branch(“value1”,&value1,“value1/L”);
TTree
tr2 = new TTree(“T2”,“test”);
tr2->Branch(“value2”,&value2,“value2/L”);

value1 = 1111; // some number to test the TTree
tr1->Fill();
value2 = 2222;
tr2->Fill();

f.Write();
// 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

f.cd(); before creating your TTrees

Rene

It works, thank you very much :stuck_out_tongue: