Reading from TTree iwth different Branches

I am trying to read data from ROOT Tree with the name dk2nuTree. I am trying to print data from nuray.E leaf. I have posted a picture of the ROOT Object Browser view in the comment section. Thank you.

void nuray()
   {
   
           TFile *filename = new TFile("g4lbne_v3r5p6_QGSP_BERT_OptEngNov2017_150cmTargetCone_NoMod2_neutrino_00001.dk2nu.root", "READ");
           TTree *dk2nuTree = (TTree*)filename->Get("dk2nuTree");
    
           Float_t Energy;
    
           dk2nuTree->SetBranchAddress("nuray.E", &Energy);
          
          Long64_t nentries = dk2nuTree->GetEntries();
          for (Long64_t i = 0; i < nentries; i++) {
                  dk2nuTree->GetEntry(i);
                  cout << Energy << endl;
          }       
         
         filename->Close();
  } 




I went through this and some other documentation yesterday but could not understand how to navigate through the given file. If you could provide me with a code on how to access data from Branch within Branch’s leaf that would be helpful. Thank you.

void nuray()
{

        TFile *filename = new TFile("g4lbne_v3r5p6_QGSP_BERT_OptEngNov2017_150cmTargetCone_NoMod2_neutrino_00001.dk2nu.root", "READ");
        TTree *dk2nuTree = (TTree*)filename->Get("dk2nuTree");
        TBranch *dk2nu = dk2nuTree->GetBranch("dk2nu");
        TBranch *nuray = dk2nu->GetBranch("nuray");
        Float_t Energy;

        nuray->SetBranchAddress("nuray.E", &Energy);

        Long64_t nentries = dk2nuTree->GetEntries();
        for (Long64_t i = 0; i < nentries; i++) {
                dk2nuTree->GetEntry(i);
                cout << Energy << endl;
        }

        filename->Close();
}

Hi @TheScientist ,
this might be simpler with RDataFrame, this should be all the code you need assuming nuray.E is a single float per entry:

ROOT::RDataFrame df("dk2nuTree", "g4lbne_v3r5p6_QGSP_BERT_OptEngNov2017_150cmTargetCone_NoMod2_neutrino_00001.dk2nu.root");
df.Foreach([](float e) { std::cout << e << std::endl; }, {"nuray.E"});

Thank you for your quick reply.

Is there a way to access the leaf (nuray.E) by adding a few lines of code in my code? I am trying to understand TTree intuitively so I do not want to skip any steps. Thank you.

The only weird thing in the code you posted is that you call SetBranchAddress on nuray rather than dk2nuTree.

But that code is not the fastest way to read data from a TTree. You should at least call SetBranchStatus("*", 0) and then SetBranchStatus("nuray", 1) on the tree (otherwise each GetEntry call loads all branch values), and actually the fastest way to read data from TTrees is to call GetEntry on each branch you want to read rather than on the TTree. For these reasons as well as several others (difficulty of parallelization being another important one) we don’t recommend that users deal with TTree directly unless there is a compelling reason. TTreeReader or RDataFrame offer better-behaved interfaces to ROOT data.

Cheers,
Enrico

1 Like