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