Logo ROOT   6.10/09
Reference Guide
tdf003_profiles.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tdataframe
3 /// \notebook -nodraw
4 /// This tutorial illustrates how to use TProfiles in combination with the
5 /// TDataFrame. See the documentation of TProfile and TProfile2D to better
6 /// understand the analogy of this code with the example one.
7 ///
8 /// \macro_code
9 ///
10 /// \date February 2017
11 /// \author Danilo Piparo
12 
13 #include "TFile.h"
14 #include "TH1F.h"
15 #include "TRandom.h"
16 #include "TTree.h"
17 
18 #include "ROOT/TDataFrame.hxx"
19 
20 // A simple helper function to fill a test tree: this makes the example
21 // stand-alone.
22 void fill_tree(const char *filename, const char *treeName)
23 {
24  TFile f(filename, "RECREATE");
25  TTree t(treeName, treeName);
26  float px, py, pz;
27  t.Branch("px", &px);
28  t.Branch("py", &py);
29  t.Branch("pz", &pz);
30  for (int i = 0; i < 25000; i++) {
31  gRandom->Rannor(px, py);
32  pz = px * px + py * py;
33  t.Fill();
34  }
35  t.Write();
36  f.Close();
37  return;
38 }
39 
40 void tdf003_profiles()
41 {
42  // We prepare an input tree to run on
43  auto fileName = "tdf003_profiles.root";
44  auto treeName = "myTree";
45  fill_tree(fileName, treeName);
46 
47  // We read the tree from the file and create a TDataFrame.
48  ROOT::Experimental::TDataFrame d(treeName, fileName, {"px", "py", "pz"});
49 
50  // Create the profiles
51  auto hprof1d = d.Profile1D(TProfile("hprof1d", "Profile of pz versus px", 64, -4, 4));
52  auto hprof2d = d.Profile2D(TProfile2D("hprof2d", "Profile of pz versus px and py", 40, -4, 4, 40, -4, 4, 0, 20));
53 
54  // And Draw
55  auto c1 = new TCanvas("c1", "Profile histogram example", 200, 10, 700, 500);
56  hprof1d->DrawClone();
57  auto c2 = new TCanvas("c2", "Profile2D histogram example", 200, 10, 700, 500);
58  c2->cd();
59  hprof2d->DrawClone();
60 }
virtual void Rannor(Float_t &a, Float_t &b)
Return 2 numbers distributed following a gaussian with mean=0 and sigma=1.
Definition: TRandom.cxx:460
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
Profile Histogram.
Definition: TProfile.h:32
TResultProxy<::TProfile > Profile1D(::TProfile &&model, std::string_view v1Name="", std::string_view v2Name="")
Fill and return a one-dimensional profile (lazy action)
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
The Canvas class.
Definition: TCanvas.h:31
return c2
Definition: legend2.C:14
double f(double x)
Profile2D histograms are used to display the mean value of Z and its RMS for each cell in X...
Definition: TProfile2D.h:27
ROOT&#39;s TDataFrame offers a high level interface for analyses of data stored in TTrees.
Definition: TDataFrame.hxx:36
A TTree object has a header with a name and a title.
Definition: TTree.h:78