Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
CorrFitPainter.cxx
1/*
2 * CorrFitPainter.cxx
3 *
4 * Created on: 23 lip 2024
5 * Author: daniel
6 */
7
8#include "CorrFitPainter.h"
9
10#include <TLegend.h>
11#include <TVirtualPad.h>
12
13#include "CorrFit.h"
14#include "CorrFitFunc.h"
15#include "Cout.h"
16#include "FemtoCFPainter.h"
17#include "Std.h"
18#include "StdString.h"
19
20namespace Hal {
21 const int CorrFitPainter::kAutoNormBit = 8;
22 const int CorrFitPainter::kLegendBit = 9;
23 const int CorrFitPainter::kChi2 = 10;
24 const int CorrFitPainter::kChi2Short = 11;
25 CorrFitPainter::CorrFitPainter(CorrFitFunc* func) : Painter(), fFittedFunc(func) {
26 fNormIndex = fFittedFunc->GetParameterIndex("N");
27 }
28
29 void CorrFitPainter::ScaleFunctions() {
30 Int_t flag = CheckOpt(kAutoNormBit);
31 for (auto x : fFunctions) {
32 for (auto y : x) {
33 if (y) { y->FixParameter(y->GetNpar() - 1, flag); }
34 }
35 }
36 }
37
38 void CorrFitPainter::DeleteFunctions() {
39 for (auto x : fFunctions) {
40 for (auto y : x)
41 if (y) delete y;
42 }
43 fFunctions.clear();
44 }
45
46 void CorrFitPainter::MakeLegend() {
47 if (!CheckOpt(kLegendBit)) return;
48 fLegendPad->cd();
49 fLegend = new TLegend(fLegendPos[0], fLegendPos[1], fLegendPos[2], fLegendPos[3]);
50 fLegend->SetHeader(GetName());
51 if (fFittedFunc) { fLegend->SetHeader(fFittedFunc->GetName()); }
52 auto label = GetLegendLabels();
53 for (auto str : label) {
54 fLegendEntries.push_back(fLegend->AddEntry((TObject*) 0x0, str, ""));
55 }
56 }
57
58 void CorrFitPainter::UpdateLegend() {
59 if (!fLegend) return;
60 auto labels = GetLegendLabels();
61 for (unsigned int i = 0; i < labels.size(); i++) {
62 TLegendEntry* ent = fLegendEntries[i];
63 ent->SetLabel(labels[i]);
64 ent->SetTextColor(kGreen);
65 }
66 fLegendPad->Modified(1);
67 fLegendPad->Update();
68 }
69
70 CorrFitPainter::~CorrFitPainter() { DeleteFunctions(); }
71
72 void CorrFitPainter::InnerPaint() {
73 DeleteFunctions();
74 MakeFunctions();
75 UpdateParameters();
76 ScaleFunctions();
77 ScaleHistograms();
78 DrawFunctions();
79 MakeLegend();
80 if (fLegendPad && CheckOpt(kLegendBit)) {
81 fLegendPad->cd();
82 fLegend->Draw("SAME");
83 }
84 Painter::InnerPaint();
85 OptionsApplied();
86 }
87
88 void CorrFitPainter::InnerRepaint() {
89 UpdateParameters();
90 ScaleFunctions();
91 ScaleHistograms();
92 DrawFunctions();
93 UpdateLegend();
94 Painter::InnerRepaint();
95 OptionsApplied();
96 }
97
98 void CorrFitPainter::DrawFunctions() {
99 LockPad();
100 int count = 1;
101 for (auto padfunc : fFunctions) {
102 GotoPad(count++);
103 for (auto func : padfunc) {
104 if (func) { func->Draw(fDefFuncDrawOpt); }
105 }
106 }
107 UnlockPad();
108 }
109
110 ULong64_t CorrFitPainter::PrepBitTemplate(std::initializer_list<int> temps) const {
111 ULong64_t res = 0;
112 auto vec = Hal::Std::GetVector(temps);
113 for (auto i : vec)
114 SETBIT(res, i);
115 return res;
116 }
117
118 Bool_t CorrFitPainter::AreSimiliar(ULong64_t current, ULong64_t pattern) const { return pattern == current & pattern; }
119
120 ULong64_t CorrFitPainter::SetOptionInternal(TString opt, ULong64_t newFlag) {
121 ContitionalPattern(opt, "norm", newFlag, kAutoNormBit, kTRUE);
122 if (Hal::Std::FindParam(opt, "chi2s", kTRUE)) {
123 CLRBIT(newFlag, kChi2Short);
124 SETBIT(newFlag, kChi2);
125 }
126 if (Hal::Std::FindParam(opt, "chi2", kTRUE)) {
127 SETBIT(newFlag, kChi2);
128 SETBIT(newFlag, kChi2Short);
129 }
130 if (Hal::Std::FindParam(opt, "legend", kTRUE)) {
131 SETBIT(newFlag, kLegendBit);
132 auto bra = Hal::Std::FindBrackets(opt, kTRUE, kTRUE);
133 for (auto pat : bra) {
134 std::vector<Double_t> vals;
135 if (GetPatterns(pat, "leg", vals)) {
136 if (vals.size() == 4) {
137 for (int i = 0; i < 4; i++)
138 fLegendPos[i] = vals[i];
139 }
140 }
141 }
142 }
143 return newFlag;
144 }
145
146 std::vector<TString> CorrFitPainter::GetLegendLabels() const {
147 std::vector<TString> label;
148 TString chi_label = "";
149 if (CheckOpt(kChi2)) {
150 Double_t chi2 = fFittedFunc->GetChiTF(fFittedFunc->fTempParamsEval); // legend present, we have to recalcuate chi2
151 TString chi_s, chindf_s, ndf_s;
152 Double_t chi = fFittedFunc->GetChiSquare();
153 Double_t chindf = fFittedFunc->GetChiNDF();
154 Double_t ndf = fFittedFunc->GetNDF();
155 if (chi <= 1000)
156 chi_s = Form("%4.3f", chi);
157 else
158 chi_s = Hal::Std::RoundToString(chi, 2, "prefix");
159 if (chindf <= 1000) {
160 chindf_s = Form("%4.3f", chindf);
161 } else {
162 chindf_s = Hal::Std::RoundToString(chindf, 2, "prefix");
163 }
164 if (ndf <= 1000) {
165 ndf_s = Form("%i", (int) ndf);
166 } else {
167 ndf_s = Hal::Std::RoundToString(ndf, 2, "prefix");
168 }
169 if (CheckOpt(kChi2Short)) {
170 chi_label = Form("#chi^{2}/NDF %s (%s/%s)", chindf_s.Data(), chi_s.Data(), ndf_s.Data());
171 } else {
172 chi_label = Form("#chi^{2}/NDF %s", chindf_s.Data());
173 }
174 }
175 for (int i = 0; i < fFittedFunc->GetParametersNo(); i++) {
176 if (fFittedFunc->IsParFixed(i)) {
177 label.push_back(Form(
178 "%s %4.3f (fixed)", fFittedFunc->fParameters[i].GetParName().Data(), fFittedFunc->fParameters[i].GetFittedValue()));
179 } else {
180 if (TMath::IsNaN(fFittedFunc->fParameters[i].GetError())) {
181 label.push_back(Form("%s %4.3f#color[2]{#pm%4.3f}",
182 fFittedFunc->fParameters[i].GetParName().Data(),
183 fFittedFunc->fParameters[i].GetFittedValue(),
184 fFittedFunc->fParameters[i].GetError()));
185 } else if (fFittedFunc->fParameters[i].GetError() < 0) {
186 label.push_back(Form("%s %4.3f#color[16]{#pm%4.3f}",
187 fFittedFunc->fParameters[i].GetParName().Data(),
188 fFittedFunc->fParameters[i].GetFittedValue(),
189 fFittedFunc->fParameters[i].GetError()));
190 } else {
191 label.push_back(Form("%s %4.3f#pm%4.3f",
192 fFittedFunc->fParameters[i].GetParName().Data(),
193 fFittedFunc->fParameters[i].GetFittedValue(),
194 fFittedFunc->fParameters[i].GetError()));
195 }
196 }
197 }
198 if (chi_label.Length() > 0) label.push_back(chi_label);
199 return label;
200 }
201
202
203 void CorrFitPainter::ScaleHistograms() {
204 auto func = (CorrFitFunc*) fFittedFunc;
205 Double_t norm = func->GetNorm();
206 if (CheckOpt(kAutoNormBit)) {
207 fCFPainter->Rescale(1.0 / func->GetNorm());
208 fCFPainter->ScaleHistograms();
209 }
210 }
211
212} /* namespace Hal */