Reading a branch within a branch

Hi experts,

I’m looking to access some leaf variables saved in a nested set of branches.
The file can be found here: /afs/cern.ch/work/a/afray/public/pool.root

Essentially, I want to access the variables found within CollectionTree->McEventCollection_p5_GEN_EVENT->m_genParticles->

I’m not quite sure how to go about accessing the variables m_px, m_py etc.

I’ve tried the standard routine of:

TFile* inputFile = new TFile("/afs/cern.ch/user/a/afray/public/PhD/SpuriousSignalSyst/JIRA_v2_copy/pool.root", "READ");

TTree* inputTree = (TTree*)inputFile->Get("CollectionTree");
        
Float_t px(0), py(0), pz(0), m(0);
Int_t pdfID(0);
inputTree->SetBranchAddress("McEventCollection_p5_GEN_EVENT/m_genParticles/m_px", &px);

but I receive the error:
‘Error in TTree::SetBranchAddress: unknown branch -> McEventCollection_p5_GEN_EVENT/m_genParticles/m_px’.

I’m aware the SetBranchAddress line is incorrect in formalism (I don’t think you can / separate the branches) but I’m not sure what the correct form should be. Is there a simple way around this?

Cheers,
Antony

Hi Anthony,

[quote]I’m aware the SetBranchAddress line is incorrect in formalism (I don’t think you can / separate the branches)[/quote]This is not accurate. Please review the User’s Guide chapter on TTree and reading data out of it or check the result of TTree::MakeClass, MakeSelector and MakeProxy.

"McEventCollection_p5_GEN_EVENT/m_genParticles/m_px"This is unlikely to be the correct branch name. How did you derive this name?

Since there is a trailing ‘s’ in the name of the data member I am guessing this is actually a collection. You will get more than one float per entry. I.e. you need to pass an array rather than a single variable.

Also how did you derive that m_px was of type ‘float’, this is unusual (but possible). We most often see ‘double’.

I am guessing something like this might work for you …

inputTree->SetMakeClass(true); const int kMaxNumberOfGenParticles = .....; Double_t px[kMaxNumberOfGenParticles] inputTree->SetBranchAddress("McEventCollection_p5_GEN_EVENT.m_genParticles.m_px", &(px[0]));

Or you may want to investigate using the TTreeReader instead.

Cheers,
Philippe.

Hi Philippe,

Thanks for the suggestions. I’ve tried the following code:

TFile* inputFile = new TFile("/afs/cern.ch/user/a/afray/public/PhD/SpuriousSignalSyst/JIRA_v2_copy/pool.root", "READ");
TTree* inputTree = (TTree*)inputFile->Get("CollectionTree");
inputTree->SetMakeClass(true);
const int kMaxNumberOfGenParticles = 1000;
Double_t px[kMaxNumberOfGenParticles];
inputTree->SetBranchAddress("McEventCollection_p5_GEN_EVENT.m_genParticle.m_px", &(px[0]));

but I receive the error

Error in TTree::SetBranchAddress: unknown branch -> McEventCollection_p5_GEN_EVENT.m_genParticles.m_px

I’m not sure why the branches are named as such. It’s an output file from running a command needed to validate a job option file for an MC production request via JIRA.

But to clarify, the file is structured such that:
CollectionTree (Tree) -> McEventCollection_p5_GEN_EVENT (Branch) -> m_genParticles (SubBranch of previous) -> m_px (Leaf variable of interest).

m_px being a float was purely a guess, using a double in retrospect is a better choice.

I will read up on TTreeReader and look at the User’s Guide TTree section, unless you have any other ideas.

Cheers,
Antony

[quote]but I receive the error

Error in TTree::SetBranchAddress: unknown branch -> McEventCollection_p5_GEN_EVENT.m_genParticles.m_px[/quote]The name I used was just a guess. See the result of TTree::Print or TTree::MakeSelector to have a better idea of the actual branch names. (and if m_genParticles is not split, you will need to either use the original shared library or the result of MakeProxy (or the TTreeReader might also work).

Cheers,
Philippe.

Did this ever get resolved…? I have the same issue:

a file containing a tree, containing a branch, containing a branch, containing a variable I need.
None of the above seems to have worked…

How does one do this?

Please follow up at Accessing a leaf within a branch in a branch