# Get min value and max value for a selection in a tree

Hello,

stupid question: How do I obtain the minimum value and the maximum value in one branch of a tree using a selection (cut)? I tried drawing a histogram, but the range of the histogram is somewhat wider than the range of the entries.

Cheers Promme

I know of no way to do this using only ROOT functionality, but since you are
probably already using C++ you could use std::min_element or
std::max_element from the standard library which work on iterators. The
downside is that you need to have all values available in memory for that.

``````#include <algorithm>

void f() {
// create some dummy data
TNtuple nt("nt", "", "x");
for (int i=0; i<1000; ++i) {
nt.Fill(gRandom->Gaus());
}

gROOT->SetBatch(); // prevent actually showing the plots here

// must draw something first to fill data structures
const unsigned nentries = nt.Draw("x", "x>0");

if (nentries) {
// getting some minimum or maximum value only makes sense if the cut
// selected anything
double* x = nt.GetV1();
std::cout << "min :" << *(std::min_element(x, x+nentries)) << std::endl;
std::cout << "max :" << *(std::max_element(x, x+nentries)) << std::endl;
}
}``````

HTH,

Benjamin

Ok, thanks for the suggestion. Goes into the direction of my solution, where I scanned calling GetBinEntry of TAxis to be larger than 1 and to break then. The Drawback is, that precision is limited by the binsize of the histogram. I was hoping for an elegant solution. So I will have to filter it by hand.
Thank you anyway.

Cheers

[quote=“Promme”]Goes into the direction of my solution, where I scanned
calling GetBinEntry of TAxis to be larger than 1 and to break then. The
Drawback is, that precision is limited by the binsize of the histogram. I was
hoping for an elegant solution. So I will have to filter it by hand.
[/quote]
Did you actually try what I suggested? TTree::GetV1 after a draw will give you
access to the raw values in the tree (just like TTree::Scan would print them).
There is no binning involved.

Ah, ok. That was not clear to me and might be indeed what I searched: full access to unbinned data. I will try this out.

Thank you

works, thank you!