Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
df004_cutFlowReport.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_dataframe
3 /// \notebook
4 /// This tutorial shows how to get information about the efficiency of the filters
5 /// applied
6 ///
7 /// \macro_code
8 /// \macro_output
9 ///
10 /// \date December 2016
11 /// \author Danilo Piparo
12 
13 using FourVector = ROOT::Math::XYZTVector;
14 using FourVectors = std::vector<FourVector>;
15 using CylFourVector = ROOT::Math::RhoEtaPhiVector;
16 
17 // A simple helper function to fill a test tree: this makes the example
18 // stand-alone.
19 void fill_tree(const char *treeName, const char *fileName)
20 {
21  ROOT::RDataFrame d(50);
22  int i(0);
23  d.Define("b1", [&i]() { return (double)i; })
24  .Define("b2",
25  [&i]() {
26  auto j = i * i;
27  ++i;
28  return j;
29  })
30  .Snapshot(treeName, fileName);
31 }
32 
33 void df004_cutFlowReport()
34 {
35 
36  // We prepare an input tree to run on
37  auto fileName = "df004_cutFlowReport.root";
38  auto treeName = "myTree";
39  fill_tree(treeName, fileName);
40 
41  // We read the tree from the file and create a RDataFrame
42  ROOT::RDataFrame d(treeName, fileName, {"b1", "b2"});
43 
44  // ## Define cuts and create the report
45  // Here we define two simple cuts
46  auto cut1 = [](double b1) { return b1 > 25.; };
47  auto cut2 = [](int b2) { return 0 == b2 % 2; };
48 
49  // An optional string parameter name can be passed to the Filter method to create a named filter.
50  // Named filters work as usual, but also keep track of how many entries they accept and reject.
51  auto filtered1 = d.Filter(cut1, {"b1"}, "Cut1");
52  auto filtered2 = d.Filter(cut2, {"b2"}, "Cut2");
53 
54  auto augmented1 = filtered2.Define("b3", [](double b1, int b2) { return b1 / b2; });
55  auto cut3 = [](double x) { return x < .5; };
56  auto filtered3 = augmented1.Filter(cut3, {"b3"}, "Cut3");
57 
58  // Statistics are retrieved through a call to the Report method:
59  // when Report is called on the main RDataFrame object, it retrieves stats
60  // for all named filters declared up to that point.
61  // When called on a stored chain state (i.e. a chain/graph node), it
62  // retrieves stats for all named filters in the section of the chain between
63  // the main RDataFrame and that node (included).
64  // Stats are printed in the same order as named filters have been added to
65  // the graph, and refer to the latest event-loop that has been run using the
66  // relevant RDataFrame.
67  std::cout << "Cut3 stats:" << std::endl;
68  filtered3.Report()->Print();
69 
70  // It is not only possible to print the information about cuts, but also to
71  // retrieve it to then use it programmatically.
72  std::cout << "All stats:" << std::endl;
73  auto allCutsReport = d.Report();
74  allCutsReport->Print();
75 
76  // We can now loop on the cuts
77  std::cout << "Name\tAll\tPass\tEfficiency" << std::endl;
78  for (auto &&cutInfo : allCutsReport) {
79  std::cout << cutInfo.GetName() << "\t" << cutInfo.GetAll() << "\t" << cutInfo.GetPass() << "\t"
80  << cutInfo.GetEff() << " %" << std::endl;
81  }
82 
83  // Or get information about them individually
84  auto cutName = "Cut1";
85  auto cut = allCutsReport->At("Cut1");
86  std::cout << cutName << " efficiency is " << cut.GetEff() << " %" << std::endl;
87 }