Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
CorrFitGUI.cxx
1/*
2 * CorrFitGUI.cxx
3 *
4 * Created on: 11 sty 2021
5 * Author: Daniel Wielanek
6 * E-mail: daniel.wielanek@gmail.com
7 * Warsaw University of Technology, Faculty of Physics
8 */
9#include "CorrFitGUI.h"
10#include "ChiSqMap2D.h"
11#include "CorrFitFunc.h"
12#include "CorrFitPainter.h"
13#include "Std.h"
14
15#include <TCanvas.h>
16#include <TColor.h>
17#include <TF1.h>
18
19#include <TROOT.h>
20#include <TSystem.h>
21#include <TVirtualPad.h>
22
23#include "Cout.h" //KURWA
24
25#include "../painters/FemtoCFPainter.h"
26#include <utility>
27
28namespace Hal {
29 CorrFitGUI::CorrFitGUI(CorrFit* f, Int_t prec) :
30 TGMainFrame(gClient->GetRoot(), 350, 300 + 440), fFunc(f), fNormIndex(fFunc->GetParameterIndex("N")) {
31 const Int_t width = 350;
32 Int_t nparams = fFunc->GetParametersNo();
33 fInitalNorm = fFunc->GetParameter(fNormIndex);
34 fSliders = new CorrFitParButton*[nparams];
35 Int_t maxL = 0;
36 Color_t col = f->GetLineColor();
37 TGHorizontalFrame* descFrame = new TGHorizontalFrame(this, width, 40);
38 TGLabel* lab11 = new TGLabel(descFrame, f->ClassName());
39 TColor* color = gROOT->GetColor(col);
40 lab11->SetTextColor(color);
41 TGLabel* lab12 = new TGLabel(descFrame, f->GetName());
42 descFrame->AddFrame(lab11, new TGLayoutHints(kLHintsCenterX | kLHintsExpandX, 5, 5, 3, 4));
43 descFrame->AddFrame(lab12, new TGLayoutHints(kLHintsCenterX | kLHintsExpandX, 5, 5, 3, 4));
44 AddFrame(descFrame, new TGLayoutHints(kLHintsCenterX | kLHintsExpandX, 5, 5, 5, 5));
45 for (int i = 0; i < nparams; i++) {
46 maxL = fFunc->GetParameterName(i).Length();
47 }
48
49 for (int i = 0; i < nparams; i++) {
50 TString name = fFunc->GetParameterName(i);
51 if (name.Length() < maxL) {
52 for (int j = name.Length(); j < maxL; j++) {
53 name = name + " ";
54 }
55 }
56 fSliders[i] = new CorrFitParButton(this, 200, 40);
57 fSliders[i]->Init(this, name, fFunc->GetParamConf(i));
58 fSliders[i]->SetPrecission(prec);
59 }
60
61 TGHorizontalFrame* autonormframe = new TGHorizontalFrame(this, width, 40);
62 fAutoNorm = new TGCheckButton(autonormframe, "Autonorm");
63 fAutoNorm->SetOn(kTRUE);
64 fAutoNorm->Connect("Clicked()", this->ClassName(), this, "ApplyParams()");
65
66 TGTextButton* round1 = new TGTextButton(autonormframe, "&Round 1");
67 autonormframe->AddFrame(round1, new TGLayoutHints(kLHintsLeft, 5, 5, 3, 4));
68 round1->Connect("Clicked()", this->ClassName(), this, "Round(=1)");
69
70 TGTextButton* round2 = new TGTextButton(autonormframe, "&Round 2");
71 autonormframe->AddFrame(round2, new TGLayoutHints(kLHintsLeft, 5, 5, 3, 4));
72 round2->Connect("Clicked()", this->ClassName(), this, "Round(=2)");
73
74 TGTextButton* round3 = new TGTextButton(autonormframe, "&Round 3");
75 autonormframe->AddFrame(round3, new TGLayoutHints(kLHintsLeft, 5, 5, 3, 4));
76 round3->Connect("Clicked()", this->ClassName(), this, "Round(=3)");
77
78
79 autonormframe->AddFrame(fAutoNorm, new TGLayoutHints(kLHintsTop | kLHintsRight, 5, 5, 5, 5));
80 AddFrame(autonormframe, new TGLayoutHints(kLHintsCenterX | kLHintsExpandX, 5, 5, 5, 5));
81
82 TGHorizontalFrame* legendFrame = new TGHorizontalFrame(this, width, 40);
83 TGLabel* lab1 = new TGLabel(legendFrame, "Par. Name");
84 TGLabel* lab2 = new TGLabel(legendFrame, "Min");
85 TGLabel* lab3 = new TGLabel(legendFrame, "Max");
86 TGLabel* lab4 = new TGLabel(legendFrame, "Steps");
87 legendFrame->AddFrame(lab1, new TGLayoutHints(kLHintsCenterX | kLHintsExpandX, 5, 5, 3, 4));
88 legendFrame->AddFrame(lab2, new TGLayoutHints(kLHintsCenterX | kLHintsExpandX, 5, 5, 3, 4));
89 legendFrame->AddFrame(lab3, new TGLayoutHints(kLHintsCenterX | kLHintsExpandX, 5, 5, 3, 4));
90 legendFrame->AddFrame(lab4, new TGLayoutHints(kLHintsCenterX | kLHintsExpandX, 5, 5, 3, 4));
91
92 AddFrame(legendFrame, new TGLayoutHints(kLHintsCenterX | kLHintsExpandX, 5, 5, 5, 5));
93
94
95 fPairChi1 = new CorrFitChiSelector(this, width, 100);
96 fPairChi2 = new CorrFitChiSelector(this, width, 100);
97 fPairChi1->Init(this, fFunc);
98 fPairChi2->Init(this, fFunc);
99
100 TGHorizontalFrame* chiFrame = new TGHorizontalFrame(this, width, 40);
101 fChiMin = new TGCheckButton(chiFrame, "Draw Chi min");
102 fChiMin->SetOn(kFALSE);
103 fChiLogz = new TGCheckButton(chiFrame, "Draw Chi logZ");
104 fChiLogz->SetOn(kFALSE);
105 fChiFit = new TGCheckButton(chiFrame, "Draw Fit par");
106 fChiFit->SetOn(kTRUE);
107 chiFrame->AddFrame(fChiMin, new TGLayoutHints(kLHintsTop | kLHintsRight, 5, 5, 5, 5));
108 chiFrame->AddFrame(fChiLogz, new TGLayoutHints(kLHintsTop | kLHintsRight, 5, 5, 5, 5));
109 chiFrame->AddFrame(fChiFit, new TGLayoutHints(kLHintsTop | kLHintsRight, 5, 5, 5, 5));
110 AddFrame(chiFrame, new TGLayoutHints(kLHintsCenterX | kLHintsExpandX, 5, 5, 5, 5));
111
112 TGHorizontalFrame* applyFrame = new TGHorizontalFrame(this, width, 40);
113 TGTextButton* draw = new TGTextButton(applyFrame, "&Draw chi");
114 applyFrame->AddFrame(draw, new TGLayoutHints(kLHintsCenterX, 5, 5, 3, 4));
115 draw->Connect("Clicked()", this->ClassName(), this, "DrawChi2()");
116 TGTextButton* exit = new TGTextButton(applyFrame, "&Exit", "gApplication->Terminate(0)");
117 applyFrame->AddFrame(exit, new TGLayoutHints(kLHintsCenterX, 5, 5, 3, 4));
118 AddFrame(applyFrame, new TGLayoutHints(kLHintsCenterX | kLHintsExpandX, 5, 5, 5, 5));
119 SetCleanup(kDeepCleanup);
120 SetWindowName("CorrFit GUI");
121 MapSubwindows();
122 Resize(width + 1, nparams * 40 + 240);
123 MapWindow();
124 }
125
126 void CorrFitGUI::ApplyParams() {
127 for (int i = 0; i < fFunc->GetParametersNo(); i++) {
128 fFunc->OverwriteParam(i, fSliders[i]->GetValue());
129 fFunc->fTempParamsEval[i] = fSliders[i]->GetValue();
130 }
131 // fFunc->ParametersChanged(); called by painter
132 // fFunc->SetErrorsNegative();
133 auto func = dynamic_cast<CorrFitFunc*>(fFunc);
134 Double_t normScale = fFunc->fTempParamsEval[fNormIndex];
135 if (normScale == 0) { normScale = 1; }
136 if (func) {
137 if (func->GetPainter()) {
138 auto cf_painter = (Hal::FemtoCFPainter*) func->GetPainter()->GetParent();
139 if (fAutoNorm->IsOn()) {
140 func->GetPainter()->SetOption("norm+keep");
141 cf_painter->Rescale(1.0 / normScale);
142 } else {
143 func->GetPainter()->SetOption("!norm+keep");
144 cf_painter->Rescale(1.0);
145 }
146 }
147 func->Repaint();
148 }
149 }
150
151 CorrFitGUI::~CorrFitGUI() { Cleanup(); }
152
153 CorrFitParButton::CorrFitParButton(const TGWindow* p, UInt_t w, UInt_t h, UInt_t options, Pixel_t back) :
154 TGHorizontalFrame(p, w, h, options, back), fNumberEntry(nullptr), fComboBox(nullptr), fDiscrete(kFALSE) {}
155
156 void CorrFitParButton::Init(CorrFitGUI* gui, TString parName, FitParam parConf) {
157 fDiscrete = parConf.IsDiscrete();
158 SetLayoutManager(new TGHorizontalLayout(this));
159
160 TGHorizontalFrame* rightLabel = new TGHorizontalFrame(this);
161
162 TGHorizontalFrame* leftLabel = new TGHorizontalFrame(this);
163
164 TGLabel* label = new TGLabel(leftLabel, parName);
165 if (parConf.IsFixed()) {
166 label->SetTextColor(new TColor(255, 0, 0));
167 } else {
168 label->SetTextColor(new TColor(0, 0, 0));
169 }
170 label->SetMinWidth(100);
171 leftLabel->AddFrame(label, new TGLayoutHints(kLHintsLeft, 5, 5, 5, 5));
172
173 if (fDiscrete) {
174 Int_t par_sec = 0;
175 Double_t val =
176 Hal::Std::Discretize(parConf.GetNPoints(), parConf.GetMapMin(), parConf.GetMapMax(), parConf.GetFittedValue(), '=');
177
178 Int_t entry = 0;
179 for (Double_t a = parConf.GetMapMin(); a <= parConf.GetMapMax(); a += parConf.GetDParam()) {
180 fSteps.push_back(a);
181 if (a == val) par_sec = entry;
182 entry++;
183 }
184
185 fComboBox = new TGComboBox(rightLabel);
186 for (unsigned int i = 0; i < fSteps.size(); i++) {
187 fComboBox->AddEntry(Form("%4.3f", fSteps[i]), i);
188 }
189 fComboBox->Resize(70, 20);
190 fComboBox->Select(par_sec);
191
192 fComboBox->Connect("Selected(Int_t)", gui->ClassName(), gui, "ApplyParams()");
193 rightLabel->AddFrame(fComboBox, new TGLayoutHints(kLHintsTop | kLHintsRight, 5, 5, 5, 5));
194 this->AddFrame(leftLabel, new TGLayoutHints(kLHintsLeft | kLHintsExpandX | kLHintsExpandY, 5, 5, 5, 5));
195 this->AddFrame(rightLabel, new TGLayoutHints(kLHintsRight | kLHintsExpandX | kLHintsExpandY, 5, 5, 5, 5));
196 gui->AddFrame(this, new TGLayoutHints(kLHintsTop | kLHintsExpandX, 5, 5, 5, 5));
197 } else {
198 fNumberEntry = new TGNumberEntry(rightLabel,
199 parConf.GetFittedValue(),
200 5,
201 -1,
202 TGNumberFormat::EStyle::kNESReal,
203 TGNumberFormat::EAttribute::kNEAAnyNumber,
204 TGNumberFormat::kNELLimitMinMax,
205 parConf.GetMapMin(),
206 parConf.GetMapMax());
207 fNumberEntry->Connect("ValueSet(Long_t)", gui->ClassName(), gui, "ApplyParams()");
208 rightLabel->AddFrame(fNumberEntry, new TGLayoutHints(kLHintsTop | kLHintsRight, 5, 5, 5, 5));
209 this->AddFrame(leftLabel, new TGLayoutHints(kLHintsLeft | kLHintsExpandX | kLHintsExpandY, 5, 5, 5, 5));
210 this->AddFrame(rightLabel, new TGLayoutHints(kLHintsRight | kLHintsExpandX | kLHintsExpandY, 5, 5, 5, 5));
211 gui->AddFrame(this, new TGLayoutHints(kLHintsTop | kLHintsExpandX, 5, 5, 5, 5));
212 }
213 }
214
215 Double_t CorrFitParButton::GetValue() {
216 if (fDiscrete) {
217 Int_t pos = fComboBox->GetSelected();
218 return fSteps[pos];
219 } else {
220 return fNumberEntry->GetNumber();
221 }
222 }
223
224 void CorrFitParButton::SetValue(Double_t val) {
225 if (!fDiscrete) { fNumberEntry->SetNumber(val, kFALSE); }
226 }
227 void CorrFitParButton::SetPrecission(Int_t prec) {
228 TGNumberFormat::EStyle style = TGNumberFormat::EStyle::kNESReal;
229 if (prec == 1) style = TGNumberFormat::EStyle::kNESRealOne;
230 if (prec == 2) style = TGNumberFormat::EStyle::kNESRealTwo;
231 if (prec == 3) style = TGNumberFormat::EStyle::kNESRealThree;
232
233 if (!fDiscrete) { return fNumberEntry->SetFormat(style, TGNumberFormat::EAttribute::kNEAAnyNumber); }
234 }
235
236 CorrFitChiSelector::CorrFitChiSelector(const TGWindow* p, UInt_t w, UInt_t h, UInt_t options, Pixel_t back) :
237 TGHorizontalFrame(p, w, h, options, back), fParName(nullptr), fMin(nullptr), fMax(nullptr), fSteps(nullptr) {}
238
239 void CorrFitChiSelector::Init(CorrFitGUI* gui, CorrFit* f) {
240 fParName = new TGComboBox(this);
241 for (int i = 0; i < f->GetParametersNo(); i++) {
242 fParName->AddEntry(f->GetParameterName(i), i);
243 }
244 fParName->Selected(0);
245 AddFrame(fParName, new TGLayoutHints(kLHintsLeft | kLHintsCenterY | kLHintsExpandX, 5, 5, 5, 5));
246 fMin = new TGNumberEntry(this,
247 0,
248 5,
249 -1,
250 TGNumberFormat::EStyle::kNESReal,
251 TGNumberFormat::EAttribute::kNEAAnyNumber,
252 TGNumberFormat::kNELLimitMinMax,
253 -1E+6,
254 1E+6);
255 this->AddFrame(fMin, new TGLayoutHints(kLHintsTop | kLHintsCenterY | kLHintsExpandX, 5, 5, 5, 5));
256 fMax = new TGNumberEntry(this,
257 0,
258 5,
259 -1,
260 TGNumberFormat::EStyle::kNESReal,
261 TGNumberFormat::EAttribute::kNEAAnyNumber,
262 TGNumberFormat::kNELLimitMinMax,
263 -1E+6,
264 1E+6);
265 this->AddFrame(fMax, new TGLayoutHints(kLHintsTop | kLHintsCenterY | kLHintsExpandX, 5, 5, 5, 5));
266 fSteps = new TGNumberEntry(this,
267 0,
268 5,
269 -1,
270 TGNumberFormat::EStyle::kNESInteger,
271 TGNumberFormat::EAttribute::kNEAPositive,
272 TGNumberFormat::kNELLimitMinMax,
273 1,
274 1E+6);
275 this->AddFrame(fSteps, new TGLayoutHints(kLHintsTop | kLHintsCenterY | kLHintsExpandX, 5, 5, 5, 5));
276 fParName->Resize(70, 20);
277 gui->AddFrame(this, new TGLayoutHints(kLHintsCenterX, 5, 5, 5, 5));
278 }
279
280 void CorrFitChiSelector::GetValue(Int_t& par, Int_t& steps, Double_t& min, Double_t& max) {
281 par = fParName->GetSelected();
282 steps = fSteps->GetIntNumber();
283 min = fMin->GetNumber();
284 max = fMax->GetNumber();
285 }
286
287 void CorrFitGUI::DrawChi2() {
288 SetParams();
289 Int_t par1, par2, steps1, steps2;
290 Double_t min1, min2, max1, max2;
291 fPairChi1->GetValue(par1, steps1, min1, max1);
292 fPairChi2->GetValue(par2, steps2, min2, max2);
293 if (min1 > max1) return;
294 if (min2 > max2) return;
295 if (steps1 < 1 || steps2 < 1) return;
296 ChiSqMap2D* chi = static_cast<CorrFitFunc*>(fFunc)->GetChiSquareMap(par1, steps1, min1, max1, par2, steps2, min2, max2);
297 ApplyParams();
298 TVirtualPad* pad = gPad;
299 TCanvas* c = new TCanvas();
300 c->cd();
301 TString opt = "";
302 if (fChiMin->IsOn()) opt = "min";
303 if (!fChiFit->IsOn()) {
304 if (opt.Length() == 0)
305 opt = "nolin";
306 else
307 opt = opt + "+nolin";
308 }
309 chi->Draw(opt);
310 c->Modified();
311 c->Update();
312
313 if (fChiLogz->IsOn()) { gPad->SetLogz(); }
314#ifdef __APPLE__
315 for (int ispe = 0; ispe < 2; ispe++) {
316 if (gSystem->ProcessEvents()) break;
317 }
318#endif
319 gPad = pad;
320 }
321
322 void CorrFitGUI::SetParams() {
323 for (int i = 0; i < fFunc->GetParametersNo(); i++) {
324 fFunc->OverwriteParam(i, fSliders[i]->GetValue());
325 }
326 }
327
328 void CorrFitGUI::Round(Int_t prec) {
329 Double_t x = TMath::Power(10, prec);
330 for (int i = 0; i < fFunc->GetParametersNo(); i++) {
331 Double_t val = fSliders[i]->GetValue();
332 val = std::round(val * x) / x;
333
334 fSliders[i]->SetValue(val);
335 }
336 ApplyParams();
337 }
338
339
340} // namespace Hal