Logo ROOT   6.12/06
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Namespaces
tdf007_snapshot.py File Reference

Namespaces

 tdf007_snapshot
 

Detailed Description

This tutorial shows how to write out datasets in ROOT formatusing the TDataFrame.

1 
2 import ROOT
3 
4 # A simple helper function to fill a test tree: this makes the example stand-alone.
5 def fill_tree(treeName, fileName):
6  tdf = ROOT.ROOT.Experimental.TDataFrame(10000)
7  tdf.Define("b1", "(int) tdfentry_")\
8  .Define("b2", "(float) tdfentry_ * tdfentry_").Snapshot(treeName, fileName)
9 
10 # We prepare an input tree to run on
11 fileName = "tdf007_snapshot_py.root"
12 outFileName = "tdf007_snapshot_output_py.root"
13 outFileNameAllColumns = "tdf007_snapshot_output_allColumns_py.root"
14 treeName = "myTree"
15 fill_tree(treeName, fileName)
16 
17 # We read the tree from the file and create a TDataFrame.
18 TDF = ROOT.ROOT.Experimental.TDataFrame
19 d = TDF(treeName, fileName)
20 
21 # ## Select entries
22 # We now select some entries in the dataset
23 d_cut = d.Filter("b1 % 2 == 0")
24 # ## Enrich the dataset
25 # Build some temporary columns: we'll write them out
26 
27 getVector_code ='''
28 std::vector<float> getVector (float b2)
29 {
30  std::vector<float> v;
31  for (int i = 0; i < 3; i++) v.push_back(b2*i);
32  return v;
33 }
34 '''
35 ROOT.gInterpreter.Declare(getVector_code)
36 
37 d2 = d_cut.Define("b1_square", "b1 * b1") \
38  .Define("b2_vector", "getVector( b2 )")
39 
40 # ## Write it to disk in ROOT format
41 # We now write to disk a new dataset with one of the variables originally
42 # present in the tree and the new variables.
43 # The user can explicitly specify the types of the columns as template
44 # arguments of the Snapshot method, otherwise they will be automatically
45 # inferred.
46 branchList = ROOT.vector('string')()
47 for branchName in ["b1", "b1_square", "b2_vector"]:
48  branchList.push_back(branchName)
49 d2.Snapshot(treeName, outFileName, branchList)
50 
51 # Open the new file and list the columns of the tree
52 f1 = ROOT.TFile(outFileName)
53 t = f1.myTree
54 print("These are the columns b1, b1_square and b2_vector:")
55 for branch in t.GetListOfBranches():
56  print("Branch: %s" %branch.GetName())
57 
58 f1.Close()
59 
60 # We are not forced to write the full set of column names. We can also
61 # specify a regular expression for that. In case nothing is specified, all
62 # columns are persistified.
63 d2.Snapshot(treeName, outFileNameAllColumns)
64 
65 # Open the new file and list the columns of the tree
66 f2 = ROOT.TFile(outFileNameAllColumns)
67 t = f2.myTree
68 print("These are all the columns available to this tdf:")
69 for branch in t.GetListOfBranches():
70  print("Branch: %s" %branch.GetName())
71 
72 f2.Close()
73 
74 # We can also get a fresh TDataFrame out of the snapshot and restart the
75 # analysis chain from it.
76 
77 branchList.clear()
78 branchList.push_back("b1_square")
79 snapshot_tdf = d2.Snapshot(treeName, outFileName, branchList);
80 h = snapshot_tdf.Histo1D("b1_square")
81 c = ROOT.TCanvas()
82 h.Draw()
83 
Date
April 2017
Author
Danilo Piparo

Definition in file tdf007_snapshot.py.