TObjArray to TTree

I am writing a chain with a TCollection/TObjArray as below

  TTree *bush=(TTree*)fIN->Get("bush");
  TObjArray *branchList;
  branchList  = bush->GetListOfBranches();
  int nBranch = bush->GetNbranches();
  
  for(int i=0;i<nBranch;i++){
     cout << "\n branch i is present: " << branchList->At(i)->GetName();
  }
  
  TChain *fChain = new TChain();
  fChain->SetMakeClass(1);
  fChain->Branch(branchList);
    cout << "Made branches.\n";

This results in a segfault after the cout list. So I know that the branchlist is good, because I can list it. But The chain does not get its branches.

Can anyone help with this?

Many Thanks!

Hi,

what do you mean with “cout list” ? Where exactly is this crashing?
I’ll be able to help you if you share the root file.

Danilo

Hi,

[quote]fChain->Branch(branchList);[/quote]is very unlikely to do what you were looking for … this add new empty branches to a TChain (somewhat illegal already) and those branch contain … TBranch objects in your case (rather than your data).

I am not sure what you are trying to do here.

Are you trying to analyze the content of the TTree named ‘Bush’? Are you trying to copy it (in which case you ought to be using TTree::CloneTree)? For reference TChain are designed to look at the content of existing TTree in existing files and are usually build by giving the instance a series of file names.

Cheers,
Philippe.

I had been trying to make a copy of the branch structure in bush into the chain. But I have stopped using chains anyway (they were a bit slow). Many thanks for the help!

Hi,

[quote]I had been trying to make a copy of the branch structure in bush into the chain[/quote]This is not semantically possible (TChain are per se read-only).

[quote] But I have stopped using chains anyway (they were a bit slow)[/quote]TChains are collections of files/trees and should be as fast as TTree. In what use case did you find them too slow?

Cheers,
Philippe.