Adding new branches on existing trees

Hi,

I am trying to add a new branch on my tree, then I stumbled upon the AddFriend function and I made a code below to add a branch to an existing tree.

Summary of the code:
-I want to add a new branch in “2.root” file via the AddFriend function.
-I created a new tree called “t” with a branch named “new_jets_n”.
-To check if my code is working properly, I copied the values of “jets_n”(number of jets) from the original tree file to the “new_jets_n”.
-In the end, I would expect “jets_n” and “new_jets_n” to have the same distribution. Unfortunately they’re not the same.

Their values are the same within the for loop of the code, but the distributions that I get in the end are not the same.
Notable differences are the values of the x-axis are no longer whole numbers, and they’re at the order of 10^-45.

I would like some assistance to the issue here.

Thank you for your time. :slight_smile:

-Chang

[code]void AddLeaf()
{
int jets_n;
int new_jets_n;
int nentries;
TBranch* b_jets_n;

TFile* file=new TFile("2.root","UPDATE");
TTree* old_tree=(TTree*)file->Get("nominal;1");
old_tree->SetBranchAddress("jets_n",&jets_n,&b_jets_n);

TTree* t=new TTree("tree","tree");
TBranch* b=t->Branch("new_jets_n",&new_jets_n,"new_jets_n");

nentries=old_tree->GetEntries();

for(unsigned int i=0;i<nentries;i++)
{
    old_tree->GetEntry(i);
    new_jets_n=jets_n;
    std::cout<<i<<std::endl;
    std::cout<<"Old="<<jets_n<<std::endl;
    std::cout<<"New="<<new_jets_n<<std::endl<<std::endl;
    t->Fill();
}
t->Write();
std::cout<<"End Loop"<<std::endl;
old_tree->AddFriend(t);
old_tree->Write();
std::cout<<"Tree Written"<<std::endl;

}[/code]

See Adding a Branch to an Existing Tree in the TTree documentation.

Hi,

One of my colleagues saw the problem in the code.
I am leaving this here for anyone who wants to do the same thing.

Instead of:
TBranch* b=t->Branch(“new_jets_n”,&new_jets_n,“new_jets_n”);

it should be:
TBranch* b=t->Branch(“new_jets_n”,&new_jets_n,“new_jets_n/I”);

Hi,

Did you tryTBranch* b=t->Branch("new_jets_n",&new_jets_n);In newer version of ROOT, Branch should be able to determine the type of the branch (if the leaf list contains only one element).

Cheers,
Philippe.

1 Like