Copy some branches into different trees and manipulate some other and then write into different trees

Hello guys,
I have a problem that probably you could solve easily.

I have an existing tree with a certain number of branches.
I would like to get some of these branches and dump them, with a DIFFERENT name, into another tree (which will be saved into another file).

First question: is there a faster way than looping over the entries of the tree? I mean also a function that avoids somehow to loop…

In addition, some other branches I would like to manipulate them and then dump , with a DIFFERENT name, into another tree.

-----------Example of My code--------------
TChain *ch = new TChain(“oldtree”);
ch->Add(input_file);

ch->SetBranchStatus("*",0);
ch->SetBranchStatus(“run”,1); // branch that I would like to copy with a new name into the new file
ch->SetBranchAddress(“run”,&run);

//-------------------opening output file-----------------------
TFile *output_file = new TFile(“newfile.oot”, “RECREATE”);

//-----------------------------------------new Tree definition
TTree* newtree = new TTree(“newTree”, “New Output Tree”);

newtree = oldtree->CloneTree();

newtree->GetBranch(“run”)->SetTitle(“bs_Run”);
newtree->GetBranch(“run”)->SetName(“bs_Run”);

ch->SetBranchStatus(“event”,1); // branch that I want to manipulate first
ch->SetBranchAddress(“event”,&event);

newtree->Branch(“bs_Event”, &bs_Event, “bs_Event/I”);

for (Int_t iEntry = 0; iEntry < nentries; ++iEntry){

ch->GetEntry(iEntry);

bs_Event = event*1000;//dummy manipulation

newtree->Fill();

}//end of loop over the entries

newtree->Write(“newtree”);


Second question: how do I avoid to write also the old tree into the new file?
In the way reported above I get also “oldtree” into my newfile.

Thank you in advance
Matteo

Hi Matteo,

you could try TDataFrame, see for example https://root.cern/doc/master/tdf007__snapshot_8C.html.
So, borrowing some of your variable names:

ROOT::Experimental::TDataFrame t("oldtree",input_file);
t.Snapshot("newTree", "newfile.root", {"run","bs_Event"});

Cheers,
D

Hi,
thanks for the suggestion.

Is this TDataFrame something present only in root 6? Cause I’m using root 5.34 to do what I’m doing…

Cheers
Matteo

Hi Matteo,

TDF is part of ROOT starting from 6.10.

Cheers,
Danilo

Probably not due the change in name and the needed data update.

Rather than

TTree* newtree = new TTree(“newTree”, “New Output Tree”);
newtree = oldtree->CloneTree(); // Lose and leak the TTree allocated above.

you probably meant:

TTree* newtree = oldtree->CloneTree();
newTree->SetName(“newTree”);
newTree->SetTitle("New Output Tree”);

and you likely meant

TTree* newtree = oldtree->CloneTree(0); // Copy just the structure and none of the data.

Cheers,
Philippe.

Thanks Philippe.

Ah ok, so basically I can do a loop to dump the old variables into the new ones without the need of cloning the old tree.
Too bad…

Actually I’ve done a few small tests and it seems that it’s faster (at least for a limited subset of variables) to do the loop rather than do CloneTree().

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