Exercise 9: Writing the n-tuple
(10 minutes)
If you’ve made changes to your n-tuple (defining new columns, applying
filters) and you want to write the revised n-tuple to a file, use the
Snapshot()
method.
Here’s an example
// Open a dataframe from an n-tuple within a file.
auto dataframe = ROOT::RDataFrame("tree1","experiment.root");
// Make a change to the n-tuple.
auto pzcut = dataframe.Filter("pz < 145");
// Write the revised n-tuple with the name "pzcut" to file
// "analysis.root".
pzcut.Snapshot("pzcut","analysis.root");
Here’s the Exercise
In the past few pages, we’ve built a collection of new variables
(\(p_{T}\), \(\theta\), \(E_{meas}\), \(E_{loss}\)) and useful analysis cuts
(\(\chi^{2} < 1.5\), \(\theta < 0.15\)). Derive an n-tuple with all these
new columns and cuts, and write it as n-tuple analyzed
to file
analysis.root
.
Tip
If you look up Snapshot()
on the RDataFrame web page
you’ll see that it’s classified as an instant action. This means
that the event loop will be executed when the Snapshot()
is executed.
If, for some reason, you want a lazy evaluation of Snapshot()
, click on the highlighted word
Snapshot()
on that page. It will take you to a different page; search for fLazy
on that new page.

Figure 58: https://xkcd.com/2755/ by Randall Munroe