# TTree::GetV1 Seg Fault (Stack Overflow?)

**URL:** <https://root-forum.cern.ch/t/ttree-getv1-seg-fault-stack-overflow/24579>\
**Category:** ROOT\
**Tags:** tree\
**Created:** [April 27, 2017, 3:19pm UTC](https://root-forum.cern.ch/t/ttree-getv1-seg-fault-stack-overflow/24579 "2017-04-27T15:19:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [April 27, 2017, 3:19pm UTC](https://root-forum.cern.ch/t/ttree-getv1-seg-fault-stack-overflow/24579/1 "2017-04-27T15:19:53Z")

</div>

Hi,

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:

```auto
#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();
      t->Fill();
   }

   t->ResetBranchAddresses();

   //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.
   t->Draw("value","","GOFF");
   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.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 27, 2017, 3:36pm UTC](https://root-forum.cern.ch/t/ttree-getv1-seg-fault-stack-overflow/24579/2 "2017-04-27T15:36:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [April 27, 2017, 3:39pm UTC](https://root-forum.cern.ch/t/ttree-getv1-seg-fault-stack-overflow/24579/3 "2017-04-27T15:39:00Z")

</div>

What is [`TTree::GetEstimate()`](https://root.cern.ch/doc/master/classTTree.html#a7b4da498bffcc046f236d3ba35e4aaa5)? The documentation is severely lacking.

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [April 27, 2017, 3:43pm UTC](https://root-forum.cern.ch/t/ttree-getv1-seg-fault-stack-overflow/24579/4 "2017-04-27T15:43:44Z")

</div>

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
t->Draw("value","","GOFF");
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.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 27, 2017, 3:50pm UTC](https://root-forum.cern.ch/t/ttree-getv1-seg-fault-stack-overflow/24579/5 "2017-04-27T15:50:17Z")

</div>

See:

1. “How to obtain more info from [TTree::Draw](https://root.cern.ch/doc/master/classTTree.html#a73450649dc6e54b5b94516c468523e45)” in the [TTree::Draw](https://root.cern.ch/doc/master/classTTree.html#a73450649dc6e54b5b94516c468523e45) 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](https://root.cern.ch/doc/master/classTSelectorDraw.html#aad8a7a873e074b802d53b92a30b38634) method description,
3. an old forum thread [TTree::Draw GetW, GetV1, GetV2, GetV3, GetV4, GetVal](https://root-forum.cern.ch/t/ttree-draw-getw-getv1-getv2-getv3-getv4-getval/19013) (about which you could ask @pcanal)

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [April 27, 2017, 3:54pm UTC](https://root-forum.cern.ch/t/ttree-getv1-seg-fault-stack-overflow/24579/6 "2017-04-27T15:54:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [May 11, 2017, 3:54pm UTC](https://root-forum.cern.ch/t/ttree-getv1-seg-fault-stack-overflow/24579/7 "2017-05-11T15:54:52Z")

</div>

Closed on May 11, 2017, 3:54pm UTC

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