So I frequently make plots like this:
chain->Draw(“pt>>myGraph”,“eta < 2.4”);
Now one of my variables is a vector filled with integers. I want to make a plot like this, but require that there be at least 3 (for example) members of this vector with integer > 2 (for example)
Is this possible, or do I have to loop over the ntuple?
Sorry if this is an easy question, I couldn’t find the answer anywhere, and this would be really helpful if it is possible.
See my first reply here: [url]Getting Fill(i, v[i]) behavior from TTree::Draw()
It should point you to the documentation with the info you need.
You might also need the advice from here: [url]TTree::Draw and TVector Branches
Thanks for reply Jean-François. So I tried this command:
tree->Scan("@btagDiscriminant.size()") and got 102, as expected, ie this vector has 102 entries.
Then I counted (by hand) the entries where btagDiscriminant was positive, and counted 11 out of the 102. I tried to reproduce this with this command, based on the documentation you sent:
But it still returned 102. What am I missing?
It sounds like your case is right on the border of what you can and cannot do with TTree::Draw/Scan. Or at least of what I know how to do.
If you are trying to get a histogram of the “greater than zeroness” of the elements in a vector, then you can just do “btagDiscriminant > 0” and your histogram should have two bins, one for those elements that pass, and one for those that fail.
If you want a histogram of the number of elements in the vector which are greater than zero, you could try something like this: “Sum$(btagDiscriminant->fElements > 0)”.
Yes! That does indeed allow me to do what I need to do. The exact syntax is
tree->Scan(“Sum$(btagDiscriminant > 0)”) returns 11, the number with btagDiscriminant > 0.
tree->Scan(“Length$(btagDiscriminant)”) returns 102.
tree->Scan(“Length$(btagDiscriminant > 0)”) returns 102.
tree->Scan(“Sum$(btagDiscriminant)”) returns -255.08438, which really is the sum of all of them
tree->Scan(“Sum$(btagDiscriminant > 0)”) returns 11, the number of entries with btagDiscriminant > 0
I guess this makes sense; the second one isn’t defined and so ignores the inequality, the third one adds all the values, and the fourth one returns 0 if the discriminant is nonpositive and 1 otherwise, then adds up all the zeros and ones to return 11. But then, I wonder if there is a way to, say, add up all the positive entries? Probably not…
But only academic curiosity at this point, I can do what I need to do. Thanks again!
I’m glad I could help. Your academic question reminds me that there is actually a somewhat simple answer. You can use the trinary operator in the formula. In C/C++,
is equivalent to
So you could do
Of course there is also the selection/TCut field for Scan & Draw:
ttree->Scan("Sum$(btagDiscriminant)","btagDiscriminant > 0")
The selection would just ignore entries less than or equal to zero, but then the entry count would be the number of selected entries. In the trinary operator case the count would include the zeros.