Getting the number of entries in a Ttree branch satisfying a condition

Dear community,

I am currently struggling to get the number of entries in a Ttree branch satisfying flag = 1.
I found the following conversation Find the number of entries in a TTree satisfying conditions yet it appears that the suggested code is not compatible with ROOT version Root Version 6.22/06. Is it possible to see the number of entries in the TreeViewer interface?

What about the TTree::Scan() method? You can also use it from the Tree Viewer


also consider using the TTree::GetEntries() method as described here.

1 Like

Thank you. I am very sorry to ask but am I doing it correctly?

// Import data
TFile *file = new TFile(“merged_simulated_data.root”);
TTree tree = (TTree) file->Get(“h1”);

// Create data set
RooDataSet data(“data”,“data”,tree,RooArgList(delta_e,m_bc,fl_b02k2,fl_r_b02,id_k1,id_k2)," id_k1 > 0.9 && m_bc > 5.27 ");
tree->GetEntries(“fl_r_b02 == 1”);

I want to know many entries have flag = 1.

Isn’t it the value returned by tree->GetEntries(“fl_r_b02 == 1”) ?

Unfortunately, no. When I run my macro with the fit of my data I cannot find the number of entries from tree->GetEntries(“fl_r_b02 == 1”) in the output.

What Philippe @pcanal probably means is that you have to print out the result of tree->GetEntries("fl_r_b02 == 1");, not just leave it hanging out there. Like

TFile *file = new TFile("merged_simulated_data.root");
TTree *tree = (TTree*)file->Get("h1");
printf("There are %li entries satisfying your condition\n", tree->GetEntries("fl_r_b02 == 1"));

Does it work for you?

Thank you.

Alternative solution with RDataFrame, as a one-liner:

ULong64_t nGoodEntries = ROOT::RDataFrame("h1", "merged_simulator_data.root").Filter("fl_r_b02 == 1").Count().GetValue();