Adding Branch with an array structure to a existing tree

Hello

I’m trying to add a branch with a array structure to a existing tree.
Basically I can follow the example from root.cern.ch/root/htmldoc/guide … sting-tree , to add a branch. It works.

However when I try to add a Branch with arrray structure, it failed, the root file shows all value are zero.
The following is part of the code:


[code] TFile* inf = TFile::Open(ifname);
TTree* ntGen = (TTree*)inf->Get(“ntGen”);
Int_t Gsize; ntGen->SetBranchAddress(“Gsize”,&Gsize);
Float_t Gpt[MAX_GEN]; ntGen->SetBranchAddress(“Gpt”,Gpt);

TFile* otf = TFile::Open(ofname,“update”);
TTree* ntGennew = (TTree*)otf->Get(“ntGen”);
Float_t FonllGptWeight[MAX_GEN];
TBranch* newBr_FonllGptWeight=ntGennew->Branch(“FonllGptWeight”,FonllGptWeight,“FonllGptWeight[Gsize]/F”);

Int_t nentries = ntGen->GetEntries();
for(Int_t i=0;i<nentries;i++)
{
ntGen->GetEntry(i);
for(Int_t iGsize=0;iGsize<Gsize;iGsize++)
{
FonllGptWeight[iGsize] = something.
cout<<FonllGptWeight[iGsize]<<endl;
}
newBr_FonllGptWeight->Fill();
}
ntGennew->Write("",TObject::kOverwrite);[/code]


I check the FonllGptWeight[iGsize] has nonzero value,
but in the output root, the branch is all zero.

Hi,

Assuming you already are guaranteed that Gsize is the same in both the old and new file for each entry, you need to make sure that the value the new tree is looking at (when processing the leaf FonllGptWeight[Gsize]/F) is loaded. For example:

TBranch *newBranchIndex = ntGennew->GetBranch("Gsize"); ... for(Int_t i=0;i<nentries;i++) newBranchIndex->GetEntry(i); // or ntGennew->GetEntry(i); ...

Cheers,
Philippe.