CbmRoot
Loading...
Searching...
No Matches
CbmQaUtil.cxx
Go to the documentation of this file.
1/* Copyright (C) 2023 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Sergey Gorbunov [committer] */
4
9
10
11#include "CbmQaUtil.h"
12
13#include "CbmQaCanvas.h"
14#include "TAxis.h"
15#include "TCanvas.h"
16#include "TF1.h"
17#include "TH1.h"
18#include "TPaletteAxis.h"
19#include "TPaveStats.h"
20#include "TStyle.h"
21#include "TVirtualPad.h"
22
24{
25
26 // ---------------------------------------------------------------------------------------------------------------------
27 //
28 TPaveStats* GetHistStats(TH1* pHist)
29 {
30 //
31 // Returns histogram stats window. Creates is if it doesn't exists.
32 //
33
34 TPaveStats* stats = (TPaveStats*) pHist->FindObject("stats");
35 if (stats) {
36 return stats;
37 }
38
39 TVirtualPad* padsav = gPad;
40 auto styleSave = gStyle->GetOptStat();
41
43 //pHist->SetStats(false); // remove the old stat window
44 pHist->SetStats(true); // set the flag to create the stat window during Draw()
45 if (styleSave == 0) {
46 gStyle->SetOptStat(1);
47 } // set some stat option
48
49 // protection of a warning for automatic axis binning
50 TAxis* x = pHist->GetXaxis();
51 TAxis* y = pHist->GetYaxis();
52 TAxis* z = pHist->GetZaxis();
53 assert(x);
54 assert(y);
55 assert(z);
56 double rx[2]{x->GetXmin(), x->GetXmax()};
57 double ry[2]{y->GetXmin(), y->GetXmax()};
58 double rz[2]{z->GetXmin(), z->GetXmax()};
59
60 x->SetLimits(0., 1.);
61 y->SetLimits(0., 1.);
62 z->SetLimits(0., 1.);
63
64 pHist->Draw("");
67
68 x->SetLimits(rx[0], rx[1]);
69 y->SetLimits(ry[0], ry[1]);
70 z->SetLimits(rz[0], rz[1]);
71
72 if (styleSave == 0) {
73 gStyle->SetOptStat(styleSave);
74 }
75 if (padsav) padsav->cd();
76
77 stats = (TPaveStats*) pHist->FindObject("stats");
78
79 // at this point the statistics window should exist under all circumstances
80 assert(stats);
81
82 return stats;
83 }
84
85
86 // ---------------------------------------------------------------------------------------------------------------------
87 //
88 std::tuple<double, double> FitKaniadakisGaussian(TH1* pHist)
89 {
90 //
91 // Fit function - Kaniadakis Gaussian Distribution:
92 // https://en.wikipedia.org/wiki/Kaniadakis_Gaussian_distribution
93 //
94 // where exp_k(x) is calculated via arcsinh():
95 // https://en.wikipedia.org/wiki/Kaniadakis_statistics
96 //
97 // parameters:
98 //
99 // [0] - mean
100 // [1] - Std. Dev ( calculated after the fit )
101 // [2] - peak
102 // [3] - hwhm - half width at half maximum, scaled. ( == standard deviation for a gaussian )
103 // [4] - k - Kaniadakis parameter, k in [0.,1.]. ( == 0.0 for a gaussian. The formula below breaks when k is exactly 0.)
104 //
105 // returns mean and std.dev
106 //
107
108 auto retValue = std::tuple(0., -1.);
109
110 double xMin = pHist->GetXaxis()->GetXmin();
111 double xMax = pHist->GetXaxis()->GetXmax();
112
113 TF1 fit("FitKaniadakisGaussian", "[2] * TMath::Exp(TMath::ASinH(-0.5*[4]*((x-[0])/[3])**2)/[4]) + 0.0*[1]", xMin,
114 xMax, "NL");
115
116 fit.SetParName(0, "fit_Mean");
117 fit.SetParName(1, "fit_StdDev");
118 fit.SetParName(2, "fit_Peak");
119 fit.SetParName(3, "fit_Hwhm");
120 fit.SetParName(4, "fit_k");
121
122 // set reasonable initial values
123
124 double mean = pHist->GetMean();
125 double peak = pHist->GetBinContent(pHist->GetMaximumBin());
126 double hwhm = pHist->GetStdDev();
127
128 fit.SetParameters(mean, hwhm, peak, hwhm, .001);
129
130 // limit parameter k
131
132 fit.SetParLimits(4, 0.00001, 5.);
133
134 // check if the histogram has enough entries excluding underflow and overflow bins
135 double entries = 0.;
136 for (int i = 1; i <= pHist->GetNbinsX(); ++i) {
137 if (fabs(pHist->GetBinContent(i)) > 0.) {
138 entries++;
139 }
140 }
141
142 // fit the histogram in quite mode
143 if (entries > 0) {
144 pHist->Fit(&fit, "Q");
145 TF1* f = pHist->GetFunction("FitKaniadakisGaussian");
146 assert(f);
147
148 // calculate the Std Dev and put it to parameter [1]
149
150 if (entries > 1) {
151 f->SetParameter(1, sqrt(f->CentralMoment(2, xMin, xMax)));
152 }
153 else {
154 f->SetParameter(1, f->GetParameter(3));
155 }
156 f->SetParError(1, f->GetParError(3));
157
158 // fix some parameters to prevent them from showing up in the stat window
159
160 f->FixParameter(2, f->GetParameter(2));
161 f->FixParameter(3, f->GetParameter(3));
162 f->FixParameter(4, f->GetParameter(4));
163 retValue = std::tuple(f->GetParameter(0), f->GetParameter(1));
164 }
165
166 TPaveStats* stats = GetHistStats(pHist);
167 assert(stats);
168 stats->SetX1NDC(0.7);
169 stats->SetY1NDC(0.5);
170 stats->SetOptStat(111110);
171 stats->SetOptFit(100001);
172
173 return retValue;
174 }
175
176
177 // ---------------------------------------------------------------------------------------------------------------------
178 //
179 void SetLargeStats(TH1* pHist)
180 {
181 //
182 // Set large stat. window
183 //
184
185 TPaveStats* stats = GetHistStats(pHist);
186 assert(stats);
187 stats->SetX1NDC(0.6);
188 stats->SetY1NDC(0.5);
189 stats->SetOptStat(111110);
190 stats->SetOptFit(100001);
191 }
192
193
194} // namespace cbm::qa::util
Definition of the CbmQaCanvas class.
Useful utilities for CBM QA tasks.
friend fvec sqrt(const fvec &a)
static CbmQaCanvas & GetDummyCanvas()
a static canvas for temporary drawing
Definition CbmQaCanvas.h:51
namespace cbm::qa::util contains useful utilities for CBM QA tasks
Definition CbmQaUtil.cxx:24
TPaveStats * GetHistStats(TH1 *pHist)
Finds/Creates stats window for a histogram.
Definition CbmQaUtil.cxx:28
std::tuple< double, double > FitKaniadakisGaussian(TH1 *pHist)
Fit a histogram with Kaniadakis Gaussian Distribution.
Definition CbmQaUtil.cxx:88
void SetLargeStats(TH1 *pHist)
Set large stat. window.