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