Logo ROOT   6.10/09
Reference Guide
tdf004_cutFlowReport.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tdataframe
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 #include "Math/Vector3D.h"
12 #include "Math/Vector4D.h"
13 #include "TCanvas.h"
14 #include "TMath.h"
15 #include "TRandom3.h"
16 #include "TFile.h"
17 #include "TH1F.h"
18 #include "TTree.h"
19 
20 #include "ROOT/TDataFrame.hxx"
21 
22 using FourVector = ROOT::Math::XYZTVector;
23 using FourVectors = std::vector<FourVector>;
24 using CylFourVector = ROOT::Math::RhoEtaPhiVector;
25 
26 // A simple helper function to fill a test tree: this makes the example
27 // stand-alone.
28 void fill_tree(const char *filename, const char *treeName)
29 {
30  TFile f(filename, "RECREATE");
31  TTree t(treeName, treeName);
32  double b1;
33  int b2;
34  t.Branch("b1", &b1);
35  t.Branch("b2", &b2);
36  for (int i = 0; i < 50; ++i) {
37  b1 = i;
38  b2 = i * i;
39  t.Fill();
40  }
41  t.Write();
42  f.Close();
43  return;
44 }
45 
47 {
48 
49  // We prepare an input tree to run on
50  auto fileName = "tdf004_cutFlowReport.root";
51  auto treeName = "myTree";
52  fill_tree(fileName, treeName);
53 
54  // We read the tree from the file and create a TDataFrame
55  ROOT::Experimental::TDataFrame d(treeName, fileName, {"b1", "b2"});
56 
57  // ## Define cuts and create the report
58  // Here we define two simple cuts
59  auto cut1 = [](double b1) { return b1 > 25.; };
60  auto cut2 = [](int b2) { return 0 == b2 % 2; };
61 
62  // An optional string parameter name can be passed to the Filter method to create a named filter.
63  // Named filters work as usual, but also keep track of how many entries they accept and reject.
64  auto filtered1 = d.Filter(cut1, {"b1"}, "Cut1");
65  auto filtered2 = d.Filter(cut2, {"b2"}, "Cut2");
66 
67  auto augmented1 = filtered2.Define("b3", [](double b1, int b2) { return b1 / b2; });
68  auto cut3 = [](double x) { return x < .5; };
69  auto filtered3 = augmented1.Filter(cut3, {"b3"}, "Cut3");
70 
71  // Statistics are retrieved through a call to the Report method:
72  // when Report is called on the main TDataFrame object, it prints stats for all named filters declared up to that
73  // point
74  // when called on a stored chain state (i.e. a chain/graph node), it prints stats for all named filters in the
75  // section
76  // of the chain between the main TDataFrame and that node (included).
77  // Stats are printed in the same order as named filters have been added to the graph, and refer to the latest
78  // event-loop that has been run using the relevant TDataFrame.
79  std::cout << "Cut3 stats:" << std::endl;
80  filtered3.Report();
81  std::cout << "All stats:" << std::endl;
82  d.Report();
83 }
84 
85 int main()
86 {
88  return 0;
89 }
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
DisplacementVector3D< CylindricalEta3D< double >, DefaultCoordinateSystemTag > RhoEtaPhiVector
3D Vector based on the eta based cylindrical coordinates rho, eta, phi in double precision.
Definition: Vector3Dfwd.h:50
TInterface< TFilterBase > Filter(F f, const ColumnNames_t &bn={}, std::string_view name="")
Append a filter to the call graph.
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
double f(double x)
ROOT&#39;s TDataFrame offers a high level interface for analyses of data stored in TTrees.
Definition: TDataFrame.hxx:36
A TTree object has a header with a name and a title.
Definition: TTree.h:78
int main(int argc, char **argv)