Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
df007_snapshot.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_dataframe
3## \notebook -draw
4## Write ROOT data with RDataFrame.
5##
6## This tutorial shows how to write out datasets in ROOT format using RDataFrame.
7##
8## \macro_image
9## \macro_code
10##
11## \date April 2017
12## \author Danilo Piparo (CERN)
13
14import ROOT
15
16# A simple helper function to fill a test tree: this makes the example stand-alone.
17def fill_tree(treeName, fileName):
18 df = ROOT.RDataFrame(10000)
19 df.Define("b1", "(int) rdfentry_")\
20 .Define("b2", "(float) rdfentry_ * rdfentry_").Snapshot(treeName, fileName)
21
22# We prepare an input tree to run on
23fileName = "df007_snapshot_py.root"
24outFileName = "df007_snapshot_output_py.root"
25outFileNameAllColumns = "df007_snapshot_output_allColumns_py.root"
26treeName = "myTree"
27fill_tree(treeName, fileName)
28
29# We read the tree from the file and create a RDataFrame
30d = ROOT.RDataFrame(treeName, fileName)
31
32# ## Select entries
33# We now select some entries in the dataset
34d_cut = d.Filter("b1 % 2 == 0")
35# ## Enrich the dataset
36# Build some temporary columns: we'll write them out
37
38getVector_code ='''
39std::vector<float> getVector (float b2)
40{
41 std::vector<float> v;
42 for (int i = 0; i < 3; i++) v.push_back(b2*i);
43 return v;
44}
45'''
46ROOT.gInterpreter.Declare(getVector_code)
47
48d2 = d_cut.Define("b1_square", "b1 * b1") \
49 .Define("b2_vector", "getVector( b2 )")
50
51# ## Write it to disk in ROOT format
52# We now write to disk a new dataset with one of the variables originally
53# present in the tree and the new variables.
54# The user can explicitly specify the types of the columns as template
55# arguments of the Snapshot method, otherwise they will be automatically
56# inferred.
57d2.Snapshot(treeName, outFileName, \
58 ["b1", "b1_square", "b2_vector"])
59# Open the new file and list the columns of the tree
60f1 = ROOT.TFile(outFileName)
61t = f1[treeName]
62print("These are the columns b1, b1_square and b2_vector:")
63for branch in t.GetListOfBranches():
64 print("Branch: %s" %branch.GetName())
65
66f1.Close()
67
68# We are not forced to write the full set of column names. We can also
69# specify a regular expression for that. In case nothing is specified, all
70# columns are persistified.
71d2.Snapshot(treeName, outFileNameAllColumns)
72
73# Open the new file and list the columns of the tree
74f2 = ROOT.TFile(outFileNameAllColumns)
75t = f2[treeName]
76print("These are all the columns available to this dataframe:")
77for branch in t.GetListOfBranches():
78 print("Branch: %s" %branch.GetName())
79
80f2.Close()
81
82# We can also get a fresh RDataFrame out of the snapshot and restart the
83# analysis chain from it.
84
85snapshot_df = d2.Snapshot(treeName, outFileName, ["b1_square"]);
86h = snapshot_df.Histo1D("b1_square")
87
88c = ROOT.TCanvas()
89h.Draw()
90c.SaveAs("df007_snapshot.png")
91
92print("Saved figure to df007_snapshot.png")
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...