Logo ROOT   6.10/09
Reference Guide
tdf101_h1Analysis.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tdataframe
3 /// \notebook
4 /// This tutorial illustrates how to express the H1 analysis with a TDataFrame.
5 ///
6 /// \macro_code
7 ///
8 /// \date December 2016
9 /// \author Axel Naumann
10 
11 #include "ROOT/TDataFrame.hxx"
12 #include "TCanvas.h"
13 #include "TChain.h"
14 #include "TF1.h"
15 #include "TH2.h"
16 #include "TLine.h"
17 #include "TPaveStats.h"
18 #include "TStyle.h"
19 
20 auto Select = [](ROOT::Experimental::TDataFrame &dataFrame) {
21  auto ret =
22  dataFrame.Filter([](float md0_d) { return TMath::Abs(md0_d - 1.8646) < 0.04; }, {"md0_d"})
23  .Filter([](float ptds_d) { return ptds_d > 2.5; }, {"ptds_d"})
24  .Filter([](float etads_d) { return TMath::Abs(etads_d) < 1.5; }, {"etads_d"})
25  .Filter([](int ik, int ipi, std::array_view<int> nhitrp) { return nhitrp[ik - 1] * nhitrp[ipi - 1] > 1; },
26  {"ik", "ipi", "nhitrp"})
27  .Filter([](int ik, std::array_view<float> rstart,
28  std::array_view<float> rend) { return rend[ik - 1] - rstart[ik - 1] > 22; },
29  {"ik", "rstart", "rend"})
30  .Filter([](int ipi, std::array_view<float> rstart,
31  std::array_view<float> rend) { return rend[ipi - 1] - rstart[ipi - 1] > 22; },
32  {"ipi", "rstart", "rend"})
33  .Filter([](int ik, std::array_view<float> nlhk) { return nlhk[ik - 1] > 0.1; }, {"ik", "nlhk"})
34  .Filter([](int ipi, std::array_view<float> nlhpi) { return nlhpi[ipi - 1] > 0.1; }, {"ipi", "nlhpi"})
35  .Filter([](int ipis, std::array_view<float> nlhpi) { return nlhpi[ipis - 1] > 0.1; }, {"ipis", "nlhpi"})
36  .Filter([](int njets) { return njets >= 1; }, {"njets"});
37 
38  return ret;
39 };
40 
41 const Double_t dxbin = (0.17 - 0.13) / 40; // Bin-width
42 
44 {
45  Double_t x = xx[0];
46  if (x <= 0.13957) return 0;
47  Double_t xp3 = (x - par[3]) * (x - par[3]);
48  Double_t res =
49  dxbin * (par[0] * pow(x - 0.13957, par[1]) + par[2] / 2.5066 / par[4] * exp(-xp3 / 2 / par[4] / par[4]));
50  return res;
51 }
52 
53 Double_t fdm2(Double_t *xx, Double_t *par)
54 {
55  static const Double_t sigma = 0.0012;
56  Double_t x = xx[0];
57  if (x <= 0.13957) return 0;
58  Double_t xp3 = (x - 0.1454) * (x - 0.1454);
59  Double_t res = dxbin * (par[0] * pow(x - 0.13957, 0.25) + par[1] / 2.5066 / sigma * exp(-xp3 / 2 / sigma / sigma));
60  return res;
61 }
62 
63 void FitAndPlotHdmd(TH1 &hdmd)
64 {
65  // create the canvas for the h1analysis fit
66  gStyle->SetOptFit();
67  TCanvas *c1 = new TCanvas("c1", "h1analysis analysis", 10, 10, 800, 600);
68  c1->SetBottomMargin(0.15);
69  hdmd.GetXaxis()->SetTitle("m_{K#pi#pi} - m_{K#pi}[GeV/c^{2}]");
70  hdmd.GetXaxis()->SetTitleOffset(1.4);
71 
72  // fit histogram hdmd with function f5 using the loglikelihood option
73  if (gROOT->GetListOfFunctions()->FindObject("f5")) delete gROOT->GetFunction("f5");
74  TF1 *f5 = new TF1("f5", fdm5, 0.139, 0.17, 5);
75  f5->SetParameters(1000000, .25, 2000, .1454, .001);
76  hdmd.Fit("f5", "lr");
77 
78  // Have the number of entries on the first histogram (to cross check when running
79  // with entry lists)
80  TPaveStats *psdmd = (TPaveStats *)hdmd.GetListOfFunctions()->FindObject("stats");
81  if (psdmd) psdmd->SetOptStat(1110);
82  c1->Modified();
83 }
84 
85 void FitAndPlotH2(TH2 &h2)
86 {
87  // create the canvas for tau d0
88  gStyle->SetOptFit(0);
89  gStyle->SetOptStat(1100);
90  TCanvas *c2 = new TCanvas("c2", "tauD0", 100, 100, 800, 600);
91  c2->SetGrid();
92  c2->SetBottomMargin(0.15);
93 
94  // Project slices of 2-d histogram h2 along X , then fit each slice
95  // with function f2 and make a histogram for each fit parameter
96  // Note that the generated histograms are added to the list of objects
97  // in the current directory.
98  if (gROOT->GetListOfFunctions()->FindObject("f2")) delete gROOT->GetFunction("f2");
99  TF1 *f2 = new TF1("f2", fdm2, 0.139, 0.17, 2);
100  f2->SetParameters(10000, 10);
101  h2.FitSlicesX(f2, 0, -1, 1, "qln");
102 
103  TH1D *h2_1 = (TH1D *)gDirectory->Get("h2_1");
104  h2_1->GetXaxis()->SetTitle("#tau[ps]");
105  h2_1->SetMarkerStyle(21);
106  h2_1->Draw();
107  c2->Update();
108  TLine *line = new TLine(0, 0, 0, c2->GetUymax());
109  line->Draw();
110 }
111 
112 void tdf101_h1Analysis()
113 {
114  TChain chain("h42");
115  chain.Add("http://root.cern.ch/files/h1/dstarmb.root");
116  chain.Add("http://root.cern.ch/files/h1/dstarp1a.root");
117  chain.Add("http://root.cern.ch/files/h1/dstarp1b.root");
118  chain.Add("http://root.cern.ch/files/h1/dstarp2.root");
119 
120  ROOT::Experimental::TDataFrame dataFrame(chain);
121  auto selected = Select(dataFrame);
122 
123  auto hdmdARP = selected.Histo1D(TH1F("hdmd", "Dm_d", 40, 0.13, 0.17), "dm_d");
124  auto selectedAddedBranch = selected.Define(
125  "h2_y", [](float rpd0_t, float ptd0_d) { return rpd0_t / 0.029979f * 1.8646f / ptd0_d; }, {"rpd0_t", "ptd0_d"});
126  auto h2ARP = selectedAddedBranch.Histo2D<float, float>(TH2F("h2", "ptD0 vs Dm_d", 30, 0.135, 0.165, 30, -3, 6),
127  "dm_d", "h2_y");
128 
129  FitAndPlotHdmd(*hdmdARP);
130  FitAndPlotH2(*h2ARP);
131 }
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title Offset is a correction factor with respect to the "s...
Definition: TAttAxis.cxx:262
double par[1]
Definition: unuranDistr.cxx:38
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:588
TLine * line
return c1
Definition: legend1.C:41
R__EXTERN TStyle * gStyle
Definition: TStyle.h:402
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:311
Double_t fdm2(Double_t *xx, Double_t *par)
#define gROOT
Definition: TROOT.h:375
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:202
The histogram statistics painter class.
Definition: TPaveStats.h:18
Short_t Abs(Short_t d)
Definition: TMathBase.h:108
virtual TObject * FindObject(const char *name) const
Find an object in this list using its name.
Definition: TList.cxx:501
Double_t x[n]
Definition: legend1.C:17
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)
Definition: TPad.h:323
double pow(double, double)
const Double_t sigma
virtual void SetBottomMargin(Float_t bottommargin)
Set Pad bottom margin in fraction of the pad height.
Definition: TAttPad.cxx:100
Service class for 2-Dim histogram classes.
Definition: TH2.h:30
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
virtual void FitSlicesX(TF1 *f1=0, Int_t firstybin=0, Int_t lastybin=-1, Int_t cut=0, Option_t *option="QNR", TObjArray *arr=0)
Project slices along X in case of a 2-D histogram, then fit each slice with function f1 and make a hi...
Definition: TH2.cxx:896
void SetOptFit(Int_t fit=1)
The type of information about fit parameters printed in the histogram statistics box can be selected ...
Definition: TStyle.cxx:1219
const Double_t dxbin
A simple line.
Definition: TLine.h:23
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition: TAttMarker.h:40
tomato 1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:594
The Canvas class.
Definition: TCanvas.h:31
return c2
Definition: legend2.C:14
double Double_t
Definition: RtypesCore.h:55
The TH1 histogram class.
Definition: TH1.h:56
Double_t fdm5(Double_t *xx, Double_t *par)
Double_t GetUymax() const
Definition: TPad.h:225
double f2(const double *x)
ROOT&#39;s TDataFrame offers a high level interface for analyses of data stored in TTrees.
Definition: TDataFrame.hxx:36
1-Dim function class
Definition: TF1.h:150
A chain is a collection of files containing TTree objects.
Definition: TChain.h:33
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition: TStyle.cxx:1267
#define gDirectory
Definition: TDirectory.h:211
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2208
double exp(double)
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:155
TList * GetListOfFunctions() const
Definition: TH1.h:224
THist< 2, float, THistStatContent, THistStatUncertainty > TH2F
Definition: THist.hxx:317
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Double_t xmin=0, Double_t xmax=0)
Fit histogram with function fname.
Definition: TH1.cxx:3564
void Modified(Bool_t flag=1)
Definition: TPad.h:410
TAxis * GetXaxis()
Definition: TH1.h:300
void SetOptStat(Int_t stat=1)
Set the stat option.
Definition: TPaveStats.cxx:303