GetEntries output when multiple branches exist

Hi everyone,

just a quick question about the GetEntries object. In the root file I attached, I have two trees. The tree “sim” has multiple branches, including one called “particles.”. I’d like to get the number of entries from that branch (all leaves withing are histograms with the same number of events). The algorithm I have so far is:

TFile *file = new TFile("DAT000001.root", "READ");
    TTree *tree =(TTree*) file->Get("sim");

    int id;
    tree->SetBranchAddress("particle..ParticleID",&id);
    int NEntries = tree->GetEntries();

When printing NEntries, I get “1” instead of the expected number of events contained in these histograms. I suspect that the fact that the “sim” tree has multiple branches, it read the number of events in the first one “shower” and returns its number of entries, which is indeed 1. So my question is, in this particular case, how do I get the number of trees in the second branch?

Thanks for your help.

Cheers,
K.

DAT000001.root (383.6 KB)


Please read tips for efficient and successful posting and posting code

_ROOT Version:6.18
_Platform:Ubuntu
_Compiler: gcc 5.4.0 / g++ 4.8.5


Hi,
indeed that file contains one entry. In other words, the TTree seen as a table contains one row (and multiple columns/branches).

For that one entry, the value of "particle..ParticleID" is an array of several thousand elements: those are the ones you see in the histogram.

There is something else that’s weird with that file, I am not able to load the contents of particle..ParticleID without getting a crash.

Cheers,
Enrico

EDIT: it looks like the file was written with ROOT v5.34. @pcanal might be able to say why I can’t read the "particle..ParticleID" branch in TTree "sim" with a recent ROOT version.

EDIT 2:
managed to find a way to read that branch that does not crash:

#include <TTreeReader.h>
#include <TTreeReaderArray.h>
#include <TFile.h>
#include <iostream>

int main() {
   TFile f("DAT000001.root");
   TTreeReader r("sim", &f);
   TTreeReaderArray<int> ra(r, "particle..ParticleID");
   r.Next();
   std::cout << ra.GetSize() << std::endl;
   return 0;
}

prints 10547, i.e. the length of the ParticleID array in the one TTree entry.

Thanks a lot Enrico, I didn’t think about using TTreeReader instead, that’s much better indeed. The advantage of using it is also that I directly create an array with the data contained in these each leaf.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.