I want to draw differences in a branch like “val[i] - val[i-1]” using TTree.
I know that accessing with TTree::SetBranchAddress is OK for that, but I want to do easiler
Is there any special expression in TTree::Draw or any nice way?
See the paragraph about arrays in TTree::Draw and also the “Special functions and variables” section in that help. Entry$ might be of some help. @pcanal might have some other ideas too.
`
Hi,
for each TTree entry, is val an array and i an element of the array, or is val a scalar and val[i] is its value at entry i and val[i-1] its value at entry i-1?
Hi Genki,
I don’t know about TTree::Draw, but with RDataFrame you could do the following (this is all the code needed, it’s untested but it should give you an idea):
#include <ROOT/RDataFrame.hxx>
int main() {
float last, beforelast;
TH1D diff_histo;
// a lambda function that fills diff_histo with `last - beforelast`
auto fill_with_diff = [&](float val) {
beforelast = last;
last = val;
diff_histo.Fill(last - beforelast);
};
ROOT::RDataFrame df("treename", "filename.root");
// for each entry, call `fill_with_diff` on branch "val"
df.Foreach(fill_with_diff, {"val"});
return 0;
}
(That code is not safe to execute on multiple threads, in case you see from the RDataFrame documentation that you can run things on multiple threads by calling `ROOT::EnableImplicitMT)
TTree::Draw does not have support for accross entry manipulation, however via the support for passing a function or a script to TTree::Draw the same technique as the one described by Enrico can work (i.e. having the function do the caching).
Thanks for your quick suggestion! Since I haven’t used RDataFrame, it’s good chance for me to try both of the class and your snippet! I’ll try right now