Logo ROOT   6.10/09
Reference Guide
tdf004_cutFlowReport.py
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 May 2017
9 ## \author Danilo Piparo
10 
11 import ROOT
12 
13 fill_tree_code ='''
14 using FourVector = ROOT::Math::XYZTVector;
15 using FourVectors = std::vector<FourVector>;
16 using CylFourVector = ROOT::Math::RhoEtaPhiVector;
17 void fill_tree(const char *filename, const char *treeName)
18 {
19  TFile f(filename, "RECREATE");
20  TTree t(treeName, treeName);
21  double b1;
22  int b2;
23  t.Branch("b1", &b1);
24  t.Branch("b2", &b2);
25  for (int i = 0; i < 50; ++i) {
26  b1 = i;
27  b2 = i * i;
28  t.Fill();
29  }
30  t.Write();
31  f.Close();
32  return;
33 }
34 '''
35 
36 # We prepare an input tree to run on
37 fileName = 'tdf004_cutFlowReport_py.root'
38 treeName = 'myTree'
39 ROOT.gInterpreter.Declare(fill_tree_code)
40 ROOT.fill_tree(fileName, treeName)
41 
42 # We read the tree from the file and create a TDataFrame, a class that
43 # allows us to interact with the data contained in the tree.
44 TDF = ROOT.ROOT.Experimental.TDataFrame
45 d = TDF(treeName, fileName)
46 
47 # ## Define cuts and create the report
48 # An optional string parameter name can be passed to the Filter method to create a named filter.
49 # Named filters work as usual, but also keep track of how many entries they accept and reject.
50 filtered1 = d.Filter('b1 > 25', 'Cut1')
51 filtered2 = d.Filter('0 == b2 % 2', 'Cut2')
52 
53 augmented1 = filtered2.Define('b3', 'b1 / b2')
54 filtered3 = augmented1.Filter('b3 < .5','Cut3')
55 
56 # Statistics are retrieved through a call to the Report method:
57 # when Report is called on the main TDataFrame object, it prints stats for all named filters declared up to that
58 # point when called on a stored chain state (i.e. a chain/graph node), it prints stats for all named filters in the
59 # section of the chain between the main TDataFrame and that node (included).
60 # Stats are printed in the same order as named filters have been added to the graph, and refer to the latest
61 # event-loop that has been run using the relevant TDataFrame.
62 print('Cut3 stats:')
63 filtered3.Report()
64 print('All stats:')
65 d.Report()