Getting data from vector<double> branches

I have a TTree with different branches like

 *Br    7 :branch_x       : vector<double>                                         *
 *Entries : 19996000 : Total  Size=  282525118 bytes  File Size  =   36126745 *
 *Baskets :    11387 : Basket Size=      32000 bytes  Compression=   7.81

but when I open it in the TBrowser, the histograms have ~150000 entries. In fact, that is the number of entries I am expecting.

First, I would like to understand why when typing tree->Print() I see more entries than I can see in the histogram in TBrowser.

Also, I have been having a lot of problems getting the value of each entry. I followed some tutorials and other questions in this forum but nothing worked. What would be the best way to access each entry?
So far I have been working with double objects, and I would just loop over the entries of the TTree to get the variable. Is there a similar way to work with vector<double> objects?

Thank you!

See for example the hvector.C tutorial. And using TTreeReader or RDataFrame could also do it (maybe @eguiraud can give you more details)

I don’t know why the number of entry is different in TTree::Print (may be a bug that you can report on jira).

To read a vector<double>:

TTreeReader r(treePtr);
TTreeReaderArray<double> arr(r, "branch_x");
while (r.Next()) {
  for (auto e : arr)
    std::cout << e << std::endl;
}

Hope this helps!
Cheers,
Enrico

1 Like

TTree::Print shows as “Entries” the number of vectors (one for each “Fill” call) stored in the branch, while the histogram in TBrowser contains all the entries of all the vectors in the branch.

1 Like

Thank you! What threw me off is that Entries printed by TTree::Print is higher than the reported number of entries according to the histogram – maybe a lot of the vectors are empty…?

1 Like

How can I access to an specific entry with this idea? The while (r.Next()) loop will go over every entry. How can I make it to just go from entry 500 to entry 1000, for example?

Hi,
please check the TTreeReader documentation, it has a couple different ways to loop on a custom entry range (SetEntry, SetEntriesRange).

Cheers,
Enrico

Hello,
I have a similar situation.
#Entries in TBrowser is less then #Entries in tree->Print().
And it is exactly what you mentioned, many vectors are empty.

I need to get #Entries in TBrowser.
Do you have any hints which Root documents that I should look at to get this value?

Hi @nvhviet ,
note that this is a very old thread. The histogram displayed by the TBrowser shows the number of values the histogram was filled with. Empty vectors count as zero, vectors with many elements count as N. TTree::Print instead lists the number of vectors, i.e. the number of entries in the TTree. These are two different numbers.

If this does not help please open a new topic describing your problem, I am not sure I understand what value you want to see in the TBrowser (try right-clicking on the TTree in the browser, that allows you calling some of its methods).

Cheers,
Enrico

Hello Enrico,
Thank you for your response.
I follow this example:
ROOT: tutorials/tree/hvector.C File Reference (cern.ch)

I set the vector-type branch address and fill it into a TH1 histogram.
Then I use the GetEntries() of the histogram to get the Entries displayed by TBrowser.

My problem is solved.
Thank you.
Viet