Logo ROOT   6.10/09
Reference Guide
tdf005_fillAnyObject.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tdataframe
3 /// \notebook -nodraw
4 /// This tutorial shows how to fill any object the class of which exposes a
5 /// `Fill` method.
6 /// \macro_code
7 ///
8 /// \date March 2017
9 /// \author Danilo Piparo
10 
11 #include "TCanvas.h"
12 #include "TFile.h"
13 #include "TH1.h"
14 #include "TTree.h"
15 
16 #include "ROOT/TDataFrame.hxx"
17 
18 // A simple helper function to fill a test tree: this makes the example
19 // stand-alone.
20 void fill_tree(const char *filename, const char *treeName)
21 {
22  TFile f(filename, "RECREATE");
23  TTree t(treeName, treeName);
24  double b1;
25  float b2;
26  t.Branch("b1", &b1);
27  t.Branch("b2", &b2);
28  for (int i = 0; i < 100; ++i) {
29  b1 = i;
30  b2 = i * i;
31  t.Fill();
32  }
33  t.Write();
34  f.Close();
35  return;
36 }
37 
38 int tdf005_fillAnyObject()
39 {
40 
41  // We prepare an input tree to run on
42  auto fileName = "tdf005_fillAnyObject.root";
43  auto treeName = "myTree";
44  fill_tree(fileName, treeName);
45 
46  // We read the tree from the file and create a TDataFrame.
47  ROOT::Experimental::TDataFrame d(treeName, fileName);
48 
49  // ## Filling any object
50  // We now fill some objects which are instances of classes which expose a
51  // `Fill` method with some input arguments.
52  auto th1d = d.Fill<double>(TH1D("th1d", "th1d", 64, 0, 128), {"b1"});
53  auto th1i = d.Fill<float>(TH1I("th1i", "th1i", 64, 0, 128), {"b2"});
54  auto th2d = d.Fill<double, float>(TH2D("th2d", "th2d", 64, 0, 128, 64, 0, 1024), {"b1", "b2"});
55 
56  auto c1 = new TCanvas();
57  th1d->DrawClone();
58 
59  auto c2 = new TCanvas();
60  th1i->DrawClone();
61 
62  auto c3 = new TCanvas();
63  th2d->DrawClone("COLZ");
64 
65  return 0;
66 }
THist< 1, int, THistStatContent > TH1I
Definition: THist.hxx:313
return c1
Definition: legend1.C:41
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
The Canvas class.
Definition: TCanvas.h:31
return c2
Definition: legend2.C:14
double f(double x)
THist< 2, double, THistStatContent, THistStatUncertainty > TH2D
Definition: THist.hxx:316
ROOT&#39;s TDataFrame offers a high level interface for analyses of data stored in TTrees.
Definition: TDataFrame.hxx:36
THist< 1, double, THistStatContent, THistStatUncertainty > TH1D
Definition: THist.hxx:310
A TTree object has a header with a name and a title.
Definition: TTree.h:78
return c3
Definition: legend3.C:15