Home | News | Documentation | Download

How to draw like "val[i] - val[i-1]" using TTree easily?

Hi all,

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 :grin:
Is there any special expression in TTree::Draw or any nice way?

Thanks in advance,
Genki

@couet could you suggest here some interesting advice? Thanks!

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?

Cheers,
Enrico

Hi Enrico,

The latter is in my mind, so val is just a scalar. Thank you for your interest in my question!

Cheers,
Genki

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)

Cheers,
Enrico

2 Likes

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).

1 Like

Hi Enrico,

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 :laughing:

Cheers,
Genki

Hi @pcanal,

OK, so I really need a technique like Enrico’s for that :yum: Thanks for your general explanation!

Cheers,
Genki

Hi,

Your code works! Thanks a lot!
Cheers, Genki

1 Like

Great! Again, that code is not safe to run on multiple threads, i.e. do not use ROOT::EnableImplicitMT with it :smile:

1 Like

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