TTree::GetV1 Seg Fault (Stack Overflow?)


I’ve recently started to handle larger TTrees and discovered what I believe is a stack overflow. My goal was to get the maximum and minimum values for a given expression, but I’m now concerned that code using GetV1 in other places will fail for larger trees.
Here is an example:

#include "TFile.h"
#include "TTree.h"
#include "TRandom.h"

#include <iostream>

void testTree() {
   TFile *file = new TFile("test.root", "RECREATE");
   TTree *t = new TTree("t", "Tree");

   double value;
   t->Branch("value", &value);

   for (int i=0;i<5000001;i++) {
      value = gRandom->Uniform();


   //This method works, but only for leafs, no expressions.
   std::cout << "Min value: " << t->GetMinimum("value") << ", max value: " << t->GetMaximum("value") << "\n";

   //This works for expressions, but is susceptible to stack overflows for large trees.
   auto entries = std::minmax_element(t->GetV1(), t->GetV1() + t->GetSelectedRows());
   std::cout << "Min value: " << *entries.first << ", max value: " << *entries.second << "\n";

I see two possible solutions:

  1. Change GetV1() to assign memory on the heap instead of the stack. (Using new or a std::array?)
  2. For my immediate issue modify TTree::GetMinimum and TTree::GetMaximum to accept expression like Draw or Scan. This doesn’t solve the underlying issue with GetV1, but makes Get[Min|Max]imum more useful.

Try: auto entries = std::minmax_element(t->GetV1(), t->GetV1() + (t->GetSelectedRows() % t->GetEstimate()));

What is TTree::GetEstimate()? The documentation is severely lacking.

If I understand correctly, GetEstimate provides the number of events used to estimate histogram limits when using Draw.
Would this be a better solution?

auto entries = std::minmax_element(t->GetV1(), t->GetV1() + std::min(t->GetSelectedRows(), t->GetEstimate()));

This doesn’t address the stack overflow issue, but simply reduces the number of entries to use for computing the min and max.


  1. “How to obtain more info from TTree::Draw” in the TTree::Draw method description,
  2. “Note GetSelectedRows currently returns the actual number of values plotted and thus if the formula contains arrays, this number might be greater than the number of entries in the trees.” in the TSelectorDraw::GetVal method description,
  3. an old forum thread TTree::Draw GetW, GetV1, GetV2, GetV3, GetV4, GetVal (about which you could ask @pcanal)
1 Like

Thanks. The TTree:Draw documentation always seems to have another surprise in it.

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