Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
df005_fillAnyObject.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_dataframe
3 /// \notebook -draw
4 /// This tutorial shows how to fill any object the class of which exposes a
5 /// `Fill` method.
6 ///
7 /// \macro_code
8 /// \macro_image
9 ///
10 /// \date March 2017
11 /// \author Danilo Piparo
12 
13 // A simple helper function to fill a test tree: this makes the example
14 // stand-alone.
15 void fill_tree(const char *treeName, const char *fileName)
16 {
17  ROOT::RDataFrame d(100);
18  auto i = 0.;
19  d.Define("b1", [&i]() { return i; })
20  .Define("b2",
21  [&i]() {
22  float j = i * i;
23  ++i;
24  return j;
25  })
26  .Snapshot(treeName, fileName);
27 }
28 
29 int df005_fillAnyObject()
30 {
31 
32  // We prepare an input tree to run on
33  auto fileName = "df005_fillAnyObject.root";
34  auto treeName = "myTree";
35  fill_tree(treeName, fileName);
36 
37  // We read the tree from the file and create a RDataFrame.
38  ROOT::RDataFrame d(treeName, fileName);
39 
40  // ## Filling any object
41  // We now fill some objects which are instances of classes which expose a
42  // `Fill` method with some input arguments.
43  auto th1d = d.Fill<double>(TH1D("th1d", "th1d", 64, 0, 128), {"b1"});
44  auto th1i = d.Fill<float>(TH1I("th1i", "th1i", 64, 0, 128), {"b2"});
45  auto th2d = d.Fill<double, float>(TH2D("th2d", "th2d", 64, 0, 128, 64, 0, 1024), {"b1", "b2"});
46 
47  auto c1 = new TCanvas();
48  th1d->DrawClone();
49 
50  auto c2 = new TCanvas();
51  th1i->DrawClone();
52 
53  auto c3 = new TCanvas();
54  th2d->DrawClone("COLZ");
55 
56  return 0;
57 }