Multiple output root files production in single run of code

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.

Thanks,
Best Regards,
Susil

Hi,
I think something like this should work.

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.

Cheers,
Stefano

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()
    }
}

Alternatively see TTree::ChangeFile

Cheers,
Philippe.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.