Copy containt of tree

Hi everyone
I try to read file1.root and copy the content of branches and change theirs names to anothers .and save the new one in fille2.root
thanks.

void test(){

TFile *input = new TFile("old_file.root","read");/*     (input)  read file.root  */

TTree *tree_i =(TTree*)input->Get("tree_name");/*the old tree froum input file*/
 double x,y;
tree_i->SetBranchAddress("x",&x);/*the old branch (1)*/
tree_i->SetBranchAddress("y",&y);/*the old branch (2)*/

TFile *output = new TFile("new_file.root","RECREATE");  /* (output) create file.root     */

TH1F *histo = new TH1F("name","title",100,0,10); 

TTree *tree_o = new TTree("tree_name_out","tree_title_out");/*the new tree*/
 
 double &a=x;
 double &b=y;
 
tree_o->Branch("a",&a,"a/D");/*the new branch (1)*/
tree_o->Branch("b",&b,"b/D");/*the new branch (2)*/


int entries = tree_o->GetEntries();
cout<<entries<<endl;

tree_o->Fill();
histo->Fill(a);

histo->Draw();
tree_o->Write();
}

Hi @eddymaoui,
you are missing a loop on all entries for (Long64_t n = 0; n < entries; ++n) in which you need to call tree_i->GetEntry(n) to load that entry number as well as tree_o->Fill() to fill it with the values for that entry.

Note however that there are simpler ways to achieve what you want, see e.g. CopyTree and the related tutorials and also RDataFrame, which is a generic “swiss-army knife” interface for data manipulation. With the latter, for instance, I think you could solve your problem in one line:

ROOT::RDataFrame("tree_name", "old_file.root")
   .Alias("a", "x")
   .Alias("b", "y")
   .Snapshot("tree_name_out", "new_file.root", {"a", "b"});

For simple cases you could also use ROOT’s command line tool rooteventselector for the same purpose, see rooteventselector --help.

Cheers,
Enrico

Hi @eguiraud
even if i add the part of your cammandes ,the code still return the same values,
it create the branches and named with a and b but not yet can stored the data of x and y in a and b.
let me show you the new verion of code :

void forum() {

TFile *input = new TFile("old_file.root","read");/*     (input)  read file.root  */

TTree *tree_i =(TTree*)input->Get("tree_name");

double x,y;
tree_i->SetBranchAddress("x",&x);
tree_i->SetBranchAddress("y",&y);

TFile *output = new TFile("new_file.root","RECREATE");  /* (output) create file.root     */
TH1F *histo = new TH1F("name","title",100,0,10); 
TTree *tree_o = new TTree("tree_name_out","tree_titre_out");
 

double a,b; 
tree_o->Branch("a",&a,"a/D");
tree_o->Branch("b",&b,"b/D");
int entries = tree_o->GetEntries();

for (int i = 0; i < entries; i++){                     /******the begining of part 1********/
tree_i->GetEntry(i);

ROOT::RDataFrame("tree_name", "old_file.root")
   .Alias("a", "x")
   .Alias("b", "y")
   .Snapshot("tree_name_out", "new_file.root", {"a", "b"});
}                                                      /******the end of part 1********/ 

tree_o->Fill();
histo->Fill(a);

histo->Draw();
tree_o->Write();
}

as resulte:

************************************
*    Row   *         a *         b *
************************************
*        0 * 3.35e-316 * 6.94e-310 *
************************************

Hi @eddymaoui,
you have to either use RDataFrame (and those lines are pretty much all you need) or manually loop over the TTrees (in which case tree_o->Fill() and histo->Fill(a) go inside the loop over entries.

Make sure you understand what each part of your code does. The TTree tutorials might help.

Cheers,
Enrico

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