Selecting best element in TTree branch

Hi all,

In a TTree branch containing a vector, drawing the vector results in drawing all N elements for each event. Is there a way to select the best element per event given a criterion. I’m most interested in things like Min/Max element.



In the trunk you can use the TTreeFormula special function Min$,Max$,MinIf$,MaxIf$.

For older version you will to write some customized code and use either MakeSelector, MakeProxy or write a small TTree::Draw script (and use with tree->Draw(“myscript.C”); );




How do I use Max$ ? Where is it documented?

I thought to find the maximum likelihood in a array float[6] to use something like this event for event:


But It throws me an exeption:

Error in TTreeFormula::Compile: Bad numerical expression : “Max$(RICH_Like[0],Max$(RICH_Like[1],Max$(RICH_Like[2],Max$(RICH_Like[3],Max$(RICH_Like[4],RICH_Like[5])))))”

for root 5.22/00 17 December 2008.

Any suggestions?


Max$ is documented in the reference guide under TTree::Draw.

Max$ will implicitly do the loop for you so that all you need to use is: "Max$(RICH_Like)"Cheers,

Thank you for this fast response. It still does not work. I know I’m a moron. Please tell me what I’m doing in this sandbox script wrong:

float array[6];
TTree* tree = new TTree("tree", "tree");
tree->Branch("array", array, "array[6]/F");
for (int i=0; i < 1000; i++){
	for (int j=0; j < 6; j++){
		array[j]=gRandom->Gaus(100, 10);
TCanvas* canvas = new TCanvas("canvas", "canvas", 400, 400);
tree->Draw("array[0]");  // works
tree->Draw("Max$(array)"); // gives an exeption: Bad numerical expression : "Max$(array)"

also array[] does not work. Meanwhile I will have a look into the documentation…

I had a look and only Alt$ is documented ( Is it already a released functionality?


Indeed, this problem (Max$ was not working on simple arrays) has been fixed in v5.26.


Yes, vers. 26 accepts it. It seems to work. Thank you very much!

Does this work in ROOT 6.08? I can not figure out how to use the Max$ and Min$ functionality for a TNtupleD although Draw is inherited from TTree. Here’s a working example of my problem:

TNtupleD* tup = new TNtupleD("tup","tup","x:y") tup->Fill(0.1,3) tup->Fill(0.2,2) tup->Fill(0.3,1) tup->Fill(0.4,0) int n = tup->Draw("x","y==Min$(y)","goff") double xAtMin = tup->GetV1()[0]

I expect xAtMin==0.4 corresponding to the minimum of y, but instead I find xAtMin==0.1. In fact, the number of entries drawn is the full n=4, so the Min$() does not seem to do anything in this case.

I apologize in advance if this is a silly mistake, but I’ve spent too much time looking for an existing answer.

These “$” functions work on the current entry.
So, you would need to have a leaf with an array “y[…]” and then “Min$(y)” would, for each entry separately, find its minimal value (for an ordinary scalar variable “y==Min$(y)==Max$(y)” for each entry, of course).