Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
CorrFit.cxx
1/*
2 * CorrFit.cxx
3 *
4 * Created on: 24 mar 2018
5 * Author: Daniel Wielanek
6 * E-mail: daniel.wielanek@gmail.com
7 * Warsaw University of Technology, Faculty of Physics
8 */
9#include "CorrFit.h"
10
11#include "CorrFitPainter.h"
12#include "Cout.h"
13#include "Std.h"
14
15#include <TF1.h>
16
17namespace Hal {
18 CorrFit::CorrFit(Int_t parameters_no) :
19 fLineColor(kRed),
20 fLineStyle(1),
21 fLineWidth(2),
22 fParametersNo(parameters_no),
23 fBinCalc(kSimple),
24 fMinFunc(kChi),
25 fMinAlgo(kDefaultAlgo),
26 fNDF(0),
27 fThreshold(0) {
28 fParameters.resize(fParametersNo);
29 for (int i = 0; i < fParametersNo; i++) {
30 fParameters[i].SetMapMin(-9999);
31 fParameters[i].SetMapMax(9999);
32 }
33 fTempParamsEval = new Double_t[fParametersNo];
34 }
35
36 std::vector<TString> CorrFit::AlgoToOptions(EMinAlgo algo) const {
37 TString pattern;
38 std::vector<TString> res;
39 switch (algo) {
40 case kMinuitMigrad: {
41 pattern = "Minuit2:Migrad";
42 } break;
43 case kMinuitSimplex: {
44 pattern = "Minuit2:Simplex";
45 } break;
46 case kMinuitCombined: {
47 pattern = "Minuit2:Combined";
48 } break;
49 case kMinuitScan: {
50 pattern = "Minuit2:Scan";
51 } break;
52 case kMinuitFumili: {
53 pattern = "Minuit2:Scan";
54 } break;
55 case kGLSMultiMinConjungateFR: {
56 pattern = "GSLMultiMin:ConjugateFR";
57 } break;
58 case kGLSMultiMinConjugatePR: {
59 pattern = "GSLMultiMin:ConjugatePR";
60 } break;
61 case kGLSMultiMinBFGS: {
62 pattern = "GSLMultiMin:BFGS";
63 } break;
64 case kGLSMultiMinBFGS2: {
65 pattern = "GSLMultiMin:BFGS2";
66 } break;
67 case kGLSMultiMinSteppestDescent: {
68 pattern = "GSLMultiMin:SteepestDescent";
69 } break;
70 case kGLSMultiFit: {
71 pattern = "GSLMultiFit:";
72 } break;
73 case kGLSSimAn: {
74 pattern = "GSLSimAn:";
75 } break;
76 case kDefaultAlgo: {
77 pattern = "Minuit2:Migrad";
78 } break;
79 case kHalScan: {
80 pattern = "HalMinimizer:Scan";
81 } break;
82 case kHalAnt: {
83 pattern = "HalMinimizer:Ant";
84 } break;
85 case kHalScanMigrad: {
86 pattern = "HalMinimizer:Scan:Minuit2:Migrad";
87 } break;
88 case kHalScanScan: {
89 pattern = "HalMinimizer:Scan:Minuit2:Migrad";
90 } break;
91 }
92 res = Hal::Std::ExplodeString(pattern, ':');
93 return res;
94 }
95
96 void CorrFit::ChangeParametersNo() {
97 fParameters.resize(fParametersNo);
98 delete[] fTempParamsEval;
99 fTempParamsEval = new Double_t[fParametersNo];
100 }
101
102 void CorrFit::SetParLimits(Int_t par, Double_t min, Double_t max) {
103 if (par < fParametersNo && par >= 0) { fParameters[par].SetRange(min, max); }
104 }
105
106 void CorrFit::FixParameter(Int_t par, Double_t val) {
107 if (par < fParametersNo && par >= 0) { fParameters[par].SetRange(val, val); }
108 }
109
110 void CorrFit::SetParameterName(Int_t par, TString name) {
111 if (par >= 0 && par < fParametersNo) { fParameters[par].SetParName(name); }
112 }
113
114 Int_t CorrFit::GetParameterIndex(TString name) const {
115 for (int i = 0; i < fParametersNo; i++) {
116 if (fParameters[i].GetParName().EqualTo(name)) return i;
117 }
118 return -1;
119 }
120
121 Bool_t CorrFit::IsParFixed(Int_t par) const {
122 if (par >= 0 && par < fParametersNo) { return fParameters[par].IsFixed(); }
123 return kFALSE;
124 }
125
126 Bool_t CorrFit::IsParDiscrete(Int_t par) const {
127 if (par >= 0 && par < fParametersNo) { return fParameters[par].IsDiscrete(); }
128 return kFALSE;
129 }
130
131 Double_t CorrFit::GetParMin(Int_t par) const {
132 if (par >= 0 && par < fParametersNo) return fParameters[par].GetMin();
133 return 0;
134 }
135
136 Double_t CorrFit::GetParMax(Int_t par) const {
137 if (par >= 0 && par < fParametersNo) return fParameters[par].GetMax();
138 return 10;
139 }
140
141 Double_t CorrFit::GetParameter(Int_t par) const {
142 if (par >= 0 && par < fParametersNo) {
143 return fParameters[par].GetFittedValue();
144 } else {
145 return 0;
146 }
147 }
148
149 Double_t CorrFit::GetParError(Int_t par) const {
150 if (par >= 0 && par < fParametersNo) {
151 return fParameters[par].GetError();
152 } else {
153 return 0;
154 }
155 }
156
157 void CorrFit::PrintFitResults() const {
158 Cout::Text(Form("%s fit results", ClassName()), "M", kWhite);
159 Cout::Database({"ParName", "Val", "Err", "Min", "Max"});
160 for (int i = 0; i < GetParametersNo(); i++) {
161 if (fParameters[i].IsFixed()) {
162 Cout::Database({GetParameterName(i), Form("%4.2f", GetParameter(i)), "fixed", "-", "-"});
163 break;
164 } else {
165 Cout::Database({GetParameterName(i),
166 Form("%4.2f", GetParameter(i)),
167 Form("%4.2f", GetParError(i)),
168 Form("%4.2f", GetParMin(i)),
169 Form("%4.2f", GetParMax(i))});
170 }
171 }
172 Cout::Stars(kWhite);
173 }
174
175 void CorrFit::CopyParamsToTF1(TF1* f, Bool_t numPar, Bool_t graphPar) const {
176 if (numPar)
177 for (int i = 0; i < GetParametersNo(); i++) {
178 f->FixParameter(i, GetParameter(i));
179 f->SetParName(i, GetParameterName(i));
180 }
181 if (graphPar) {
182 f->SetLineColor(GetLineColor());
183 f->SetLineStyle(GetLineStyle());
184 f->SetLineWidth(GetLineWidth());
185 }
186 }
187
188 CorrFit::~CorrFit() {
189 for (auto f : fDrawFunc) {
190 if (f.first != nullptr) { delete f.first; }
191 }
192 if (fPainter) delete fPainter;
193 /*delete[] fTempParamsEval;*/
194 }
195
196 void CorrFit::SetErrorsNegative() {
197 for (auto& i : fParameters)
198 i.SetError(-1);
199 }
200
201 void CorrFit::SetFitOrder(std::initializer_list<Int_t> order) {
202 auto vec = Hal::Std::GetVector(order);
203 if (int(vec.size()) != fParametersNo) {
204 Hal::Cout::PrintInfo("Cannot change fit order, wrong size of list", EInfo::kWarning);
205 return;
206 }
207 std::vector<bool> vals(fParametersNo, false);
208 for (unsigned int it = 0; it < vec.size(); it++) {
209 vals[vec[it]] = true;
210 }
211
212 for (auto i : vals) {
213 if (i == false) {
214 Hal::Cout::PrintInfo("Cannot change fit order, wrong values", EInfo::kWarning);
215 return;
216 }
217 }
218 fFitOrder = vec;
219 }
220
221 void CorrFit::CheckOrder() {
222 if (fFitOrder.size() == 0) {
223 fFitOrder.resize(fParametersNo);
224 for (int i = 0; i < fParametersNo; i++) {
225 fFitOrder[i] = i;
226 }
227 }
228 }
229
230} // namespace Hal
static void PrintInfo(TString text, Hal::EInfo status)
Definition Cout.cxx:370