Making cuts on ntuple data

Hello Fellow Rooters,
When plotting histograms, it’s easy to make cuts on ntuple data. However, extracting a subset of ntuple data is surprisingly more complex and one basically has to loop over all the data.
The workman-like code attached extracts a 2-dimensional plot from a 3-D plot along the plane (X==3 && Y==3). Two questions occurred to me while working on it:

  1. Is there a way to improve the code and achieve the same goal more efficiently/elegantly?

  2. If I wish to extract the 'i’th X-value from my nTuple “test_bench”, the code goes something like:

      Int_t X_value;
      TLeaf *l_Xvalue =  test_bench->GetLeaf("X");
      l_Xvalue->GetBranch()->GetEntry(i); 
      X_value = l_Xvalue->GetValue();
    

This seems somewhat odd to me. I first point to the ‘leaf’ where the data is stored, then I point to the ‘branch’, then I point to the ‘entry’. I would have expected that the proper order would have been: ‘Branch’->‘Leaf’->‘Entry’. Can someone kindly explain?

Regards,
Ricardotest_code.zip (166.1 KB)

Hi,

There are several ways to extract data from an ntuple. The new recommended way is to use the RData frame, which is the most efficient since it provides code optimised for your machine, using all your available cores and it provides for you the event loop.
See https://root.cern/doc/master/classROOT_1_1RDataFrame.html

Concerning your questions about TLeaf and TBranch, maybe @pcanal can answer for you

Best Regards

Lorenzo

In you case, you can simplify the code with either RDataFrame or

Z_D->Draw("D:Z","X==3 && Y==3");

or

TBranch *X_branch = nullptr;  
TBranch *Y_branch = nullptr;  
TBranch *D_branch = nullptr;  
TBranch *Z_branch = nullptr;  

test_bench->SetBranchAddress("X", &X_value, &X_branch);
test_bench->SetBranchAddress("Y", &Y_value, &Y_branch);
test_bench->SetBranchAddress("Z", &Z_value, &Z_branch);
test_bench->SetBranchAddress("D", &D_value, &D_branch);

and since you are reading all the branches

test_bench->GetEntry(i); // it is also fine to call each branch's GetEntry

I would have expected that the proper order would have been: ‘Branch’->‘Leaf’->‘Entry’

You can also do the search that way. Using the other way around is practical when you are not sure of the branchname.

Cheers,
Philippe.

Thanks @pcanal and @moneta. Your informative replies demonstrate why this subsection of the forum is so useful for us newbies.

Regards,
RL