Hi,
I have a standalone simple code to produce some TTree and store it in my events in a output root file. Now I want to store only 1- 1000 (say) events in this output file and would like to store 1001-2000 events in next output file …and so on…in the SAME RUN of the code. Is it possible?
May be it simple but I never did this so a simplest possible snipest of the code would be very helpful.
TTree *tree = new TTre("tree","events");
int i=0;
for(i=0;i<=10000;i++)
...
//fill the tree just to 1000 events
...
if(i!=1000 && i!=0)
{
TFile *f= new TFile(Form("Events_up_to_%d.root",i),"recreate");
tree->Write();
f->Closed();
f->Delete();
tree->Reset()
}
In this way after you have declared the tree, you can use it the same tree just resetting after wrote it on files.
The files are saved with different names. I hope it works fine.
an improvement over the previous proposal which avoid the TTree from holding all the data in memory before writing (and instead write it chunk by chunk):
TTree *tree = new TTre("tree","events");
TFile *f= new TFile(Form("Events_up_to_%d.root",1000),"recreate");
tree->SetDirectory(f);
int i=0;
for(i=0;i<=10000;i++)
...
if(i!=1000 && i!=0)
{
f->Write();
tree->SetDirectory(nullptr);
delete f;
f= new TFile(Form("Events_up_to_%d.root",i),"recreate");
tree->SetDirectory(f);
tree->Reset()
}
}