Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
CorrFitSmear1DCF.cxx
1/*
2 * CorrFitSmear1DCF.cxx
3 *
4 * Created on: 4 sty 2019
5 * Author: Daniel Wielanek
6 * E-mail: daniel.wielanek@gmail.com
7 * Warsaw University of Technology, Faculty of Physics
8 */
9
10#include "CorrFitSmear1DCF.h"
11#include <iostream>
12
13#include "CorrFitMapKstarRstar.h"
14#include "Cout.h"
15#include "Femto1DCF.h"
16#include "Splines.h"
17#include "StdHist.h"
18#include "StdMath.h"
19
20#include <TAxis.h>
21#include <TF1.h>
22#include <TH1.h>
23#include <TH2.h>
24#include <TMath.h>
25
26
27namespace Hal {
29 fFunc =
30 new TF1(Hal::Std::GetUniqueName("func_draw"), this, &CorrFitSmear1DCF::GaussNS, -10, 10, 2, this->ClassName(), "GaussNS");
31 }
32
33 CorrFitSmear1DCF::~CorrFitSmear1DCF() {
34 if (fCF) delete fCF;
35 if (fFunc) delete fFunc;
36 if (fSpline) delete fSpline;
37 if (fSplot) delete fSplot;
38 }
39
41 if (fCF) {
42 delete fCF;
43 fCF = nullptr;
44 }
45 fCF = (Hal::Femto1DCF*) cf.Clone();
46 }
47
49 if (!fCF) {
50 Hal::Cout::PrintInfo("Cannot recalculate smear matrix", EInfo::kWarning);
51 return TH2D();
52 }
53 int binsX;
54 double minX, maxX;
55
56 Hal::Std::GetAxisPar(*fCF->GetNum(), binsX, minX, maxX, "x");
57 TH2D newMatrix("newMatrix", "newMatrix", binsX, minX, maxX, binsX, minX, maxX);
58 Hal::Spline2D spline(&matrix, "const");
59 for (int i = 1; i <= binsX; i++) {
60 double x = matrix.GetXaxis()->GetBinCenter(i);
61 for (int j = 1; j <= binsX; j++) {
62 double y = matrix.GetYaxis()->GetBinCenter(j);
63 double val = spline.Eval(x, y);
64 newMatrix.GetBinContent(i, j, val);
65 }
66 }
67 return newMatrix;
68 }
69
71 CorrFitMapKstarRstar* newMap = new CorrFitMapKstarRstar(*fMap);
72 auto histo = newMap->GetHisto();
73 histo->Reset();
74 Int_t sizeQ = newMap->GetHisto()->GetNbinsX();
75 auto smearMT = Std::GetMatrix(smearMatrix, kFALSE);
76 NormalizeMatrix(smearMT);
77 std::vector<double> correction;
78 if (fAutoFill) { correction = GetAutoFill(smearMT, (smearMatrix.GetXaxis()->GetBinCenter(1) < 0)); }
79 auto print = [](const TMatrixD x, TString flag) {
80#ifdef _DEBUG__CORRFITSMEAR1DCF_
81 std::cout << flag << std::endl;
82 for (int i = 0; i < x.GetNrows(); i++) {
83 for (int j = 0; j < x.GetNcols(); j++) {
84 std::cout << Form(" %4.4f ", x[i][j]);
85 }
86 std::cout << std::endl;
87 }
88#endif
89 };
90
91
92 if (fSeparateSmear) {
93 auto den = (TH1D*) fCF->GetDen();
94 auto maph = fMap->GetHisto();
95 auto denVec = GetVec(*den);
96 auto denVecSmeared = smearMT * denVec;
97 for (int i = 1; i <= maph->GetNbinsY(); i++) {
98 auto sim = GetVec(*maph, i);
99 auto numVec = Multiply(sim, denVec);
100 auto numVecSmeared = smearMT * numVec;
101 auto reco = smearMT * sim;
102 for (int j = 1; j <= sizeQ; j++) {
103 if (fAutoFill) {
104 histo->SetBinContent(j, i, numVecSmeared[j - 1][0] / denVecSmeared[j - 1][0] + correction[j - 1]);
105 } else {
106 histo->SetBinContent(j, i, numVecSmeared[j - 1][0] / denVecSmeared[j - 1][0]);
107 }
108 }
109 }
110 } else {
111 auto maph = fMap->GetHisto();
112 for (int i = 1; i <= maph->GetNbinsY(); i++) {
113 auto sim = GetVec(*maph, i);
114
115 auto reco = smearMT * sim;
116
117 if (i == 1) {
118 print(sim, "SIM MOM");
119 print(smearMT, "MATRIX");
120 print(reco, "RECO");
121 }
122 for (int j = 1; j <= sizeQ; j++) {
123 if (fAutoFill) {
124 histo->SetBinContent(j, i, reco[j - 1][0] + correction[j - 1]);
125 } else {
126 histo->SetBinContent(j, i, reco[j - 1][0]);
127 }
128 }
129 }
130 }
131
132 return newMap;
133 }
134
136 CorrFitMapKstarRstar* newMap = new CorrFitMapKstarRstar(*fMap);
137 auto histo = newMap->GetHisto();
138 histo->Reset();
139 std::vector<double> norms = GetGausNorms(func, histo->GetXaxis());
140 TAxis* y = histo->GetYaxis();
141 TAxis* x = histo->GetXaxis();
142
143 if (fSpline) {
144 delete fSpline;
145 fSpline = nullptr;
146 }
147 auto h = Hal::Std::GetProjection1D(histo, 1, 1, "x+bins");
148 fSpline = new Hal::Spline1D(h);
149 delete h;
150 if (!fSplot) {
151 fSplot =
152 new TF1(Hal::Std::GetUniqueName("func_splot"), this, &CorrFitSmear1DCF::Splot, -10, 10, 1, this->ClassName(), "Splot");
153 }
154 if (!fSeparateSmear) {
155 for (int iR = 1; iR <= y->GetNbins(); iR++) {
156 h = Hal::Std::GetProjection1D(fMap->GetHisto(), iR, iR, "x+bins+randname");
157 fSpline->FastOverwrite(h, h->GetBinContent(1), h->GetBinContent(x->GetNbins()));
158 fSpline->Refit();
159 delete h;
160 for (int jQ = 1; jQ <= x->GetNbins(); jQ++) {
161 Double_t q = x->GetBinCenter(jQ);
162 Double_t sigma = func->Eval(q);
163 fFunc->FixParameter(1, norms[jQ - 1]); // normalize gauss
164 fFunc->FixParameter(0, sigma); // set sigma
165 fSplot->FixParameter(0, q);
166 Double_t integral = fSplot->Integral(-4.0 * sigma, 4.0 * sigma, 1e-3);
167 histo->SetBinContent(jQ, iR, integral);
168 }
169 }
170 } else { // TODO make this
171 for (int iR = 1; iR <= y->GetNbins(); iR++) {
172 h = Hal::Std::GetProjection1D(fMap->GetHisto(), iR, iR, "x+bins+randname");
173 fSpline->FastOverwrite(h, h->GetBinContent(1), h->GetBinContent(x->GetNbins()));
174 fSpline->Refit();
175 delete h;
176 for (int jQ = 1; jQ <= x->GetNbins(); jQ++) {
177 Double_t q = x->GetBinCenter(jQ);
178 Double_t sigma = func->Eval(q);
179 fFunc->FixParameter(1, norms[jQ - 1]); // normalize gauss
180 fFunc->FixParameter(0, sigma); // set sigma
181 fSplot->FixParameter(0, q);
182 Double_t integral = fSplot->Integral(-4.0 * sigma, 4.0 * sigma, 1e-3);
183 histo->SetBinContent(jQ, iR, integral);
184 }
185 }
186 }
187 newMap->Recalc();
188 return newMap;
189 }
190
191 TMatrixD CorrFitSmear1DCF::GetVec(const TH2D& vec, Int_t bin) const {
192 TMatrixD vect(vec.GetNbinsX(), 1);
193 for (int i = 1; i <= vec.GetNbinsX(); i++) {
194 vect[i - 1][0] = vec.GetBinContent(i, bin);
195 }
196 return vect;
197 }
198
199 TMatrixD CorrFitSmear1DCF::GetVec(const TH1D& vec) const {
200 TMatrixD vect(vec.GetNbinsX(), 1);
201 for (int i = 1; i <= vec.GetNbinsX(); i++) {
202 vect[i - 1][0] = vec.GetBinContent(i);
203 }
204 return vect;
205 }
206
207 void CorrFitSmear1DCF::NormalizeMatrix(TMatrixD& matrix) const {
208 int rowcol = matrix.GetNrows();
209 for (int i = 0; i < rowcol; i++) {
210 double sum = 0;
211 for (int j = 0; j < rowcol; j++) {
212 sum += matrix[j][i];
213 }
214 if (sum != 0)
215 sum = 1.0 / sum;
216 else {
217 sum = 1;
218 matrix[i][i] = 1; // y,x
219 }
220 for (int j = 0; j < rowcol; j++) {
221 matrix[j][i] = matrix[j][i] * sum;
222 }
223 }
224 }
225
226 TMatrixD CorrFitSmear1DCF::Multiply(TMatrixD A, TMatrixD B) const {
227 TMatrixD res(A.GetNrows(), 1);
228 for (int i = 0; i < A.GetNrows(); i++) {
229 res[i][0] = A[i][0] * B[i][0];
230 }
231 return res;
232 }
233
235 TH2D h(matrix);
236 for (int i = 1; i <= matrix.GetNbinsX(); i++) {
237 for (int j = 1; j <= matrix.GetNbinsX(); j++) {
238 h.SetBinContent(i, j, matrix.GetBinContent(j, i));
239 }
240 }
241 return h;
242 }
243
244 Double_t CorrFitSmear1DCF::GaussNS(Double_t* x, Double_t* params) const {
245 return params[1] * TMath::Exp(-(x[0] * x[0] / (2.0 * params[0] * params[0])));
246 }
247
248 Double_t CorrFitSmear1DCF::CorrFunc(Double_t* x, Double_t* /*params*/) const { return fSpline->Eval(x[0]); }
249
250 Double_t CorrFitSmear1DCF::Splot(Double_t* x, Double_t* params) const {
251 Double_t kstar = x[0] + params[0];
252 if (kstar < 0) kstar = -kstar;
253 return fSpline->Eval(kstar) * fFunc->Eval(x[0]);
254 }
255
256 std::vector<double> CorrFitSmear1DCF::GetAutoFill(const TMatrixD& smearMT, Bool_t lower) const {
257 std::vector<double> correction;
258 int size = smearMT.GetNrows();
259 if (lower) {
260 int half = size / 2;
261 // lower part
262 for (int i = 0; i < half; i++) {
263 double sum = 0;
264 for (int j = 0; j < i; j++) {
265 sum -= smearMT[j][i];
266 }
267 for (int j = i + 1; j < size; j++) {
268 sum += smearMT[j][i];
269 }
270 if (sum < 0) sum = 0;
271 correction.push_back(sum);
272 }
273
274 // upper part
275 for (int i = half; i < size; i++) {
276 double sum = 0;
277 for (int j = 0; j < i; j++) {
278 sum += smearMT[j][i];
279 }
280 for (int j = i + 1; j < size; j++) {
281 sum -= smearMT[j][i];
282 }
283 if (sum < 0) sum = 0;
284 correction.push_back(sum);
285 }
286 } else {
287 for (int i = 0; i < size; i++) {
288 double sum = 0;
289 for (int j = 0; j < i; j++) {
290 sum += smearMT[j][i];
291 }
292 for (int j = i + 1; j < size; j++) {
293 sum -= smearMT[j][i];
294 }
295 if (sum < 0) sum = 0;
296 correction.push_back(sum);
297 }
298 }
299 return correction;
300 }
301
302 std::vector<double> CorrFitSmear1DCF::GetGausNorms(TF1* f, TAxis* x) const {
303 std::vector<double> norms;
304 for (int i = 1; i <= x->GetNbins(); i++) {
305 Double_t kstar = x->GetBinCenter(i);
306 Double_t sigma = f->Eval(kstar);
307 fFunc->FixParameter(0, sigma);
308 fFunc->FixParameter(1, 1);
309 Double_t lowerBoundary = -4.0 * sigma;
310 Double_t upperBoundary = 4.0 * sigma;
311 Double_t integral = fFunc->Integral(lowerBoundary, upperBoundary);
312 if (integral != 0) {
313 norms.push_back(1.0 / integral);
314 } else {
315 norms.push_back(1.0);
316 }
317 }
318 return norms;
319 }
320
321 TMatrixD CorrFitSmear1DCF::ReverseDenominator(TF1* smearfunc, TH2D* smearMatrix) const {
322 Int_t nbins = fCF->GetNum()->GetNbinsX();
323 TMatrixD reversed(nbins + 2, 1);
324 for (int i = 1; i <= nbins; i++) {
325 reversed[i][0] = fCF->GetDen()->GetBinContent(i);
326 }
327 if (fCF->GetNum()->GetXaxis()->GetBinCenter(1) < 0) { // sym
328 reversed[0][0] = fCF->GetDen()->GetBinContent(1);
329 } else {
330 reversed[0][0] = 0;
331 }
332 reversed[nbins + 1][0] = fCF->GetDen()->GetBinContent(nbins);
333 TMatrixD smearMT(nbins + 2, nbins + 2);
334 // row col
335 smearMT[0][0] = smearMT[nbins + 1][nbins + 1] = 1;
336 TAxis* x = fCF->GetNum()->GetXaxis();
337 if (smearfunc) {
338 fFunc->FixParameter(0, 1); // does'nt matter because matrix will be normalized
339 auto norms = GetGausNorms(smearfunc, fCF->GetNum()->GetXaxis());
340 for (int iSim = 1; iSim <= nbins; iSim++) {
341 Double_t qsim = x->GetBinCenter(iSim);
342 Double_t sigma = smearfunc->Eval(qsim);
343 for (int jReco = 1; jReco <= nbins; jReco++) {
344 // Double_t qreco = x->GetBinCenter(jReco);
345 Double_t density = fFunc->Eval(sigma);
346 smearMT[jReco][iSim] = density;
347 }
348 }
349 } else {
350 for (int iSim = 1; iSim <= nbins; iSim++) {
351 for (int jReco = 1; jReco <= nbins; jReco++) {
352 smearMT[jReco][iSim] = smearMatrix->GetBinContent(iSim, jReco);
353 }
354 }
355 }
356 NormalizeMatrix(smearMT);
357 auto rev = smearMT.Invert();
358 auto newDen = reversed * rev;
359 TMatrixD reversedDen(nbins, 1);
360 for (int i = 0; i < nbins; i++) {
361 reversedDen[i][0] = newDen[i + 1][0];
362 }
363 return reversedDen;
364 }
365
366} // namespace Hal
CorrFitMapKstarRstar * SmearFunction(TF1 *func)
CorrFitMapKstarRstar * SmearMatrix(const TH2D &smearMatrix)
TH2D RecalcSmearMatrix(TH2D &matrix)
static TH2D TransposeHistogram(TH2D &matrix)
void SetCF(Hal::Femto1DCF &cf)
static void PrintInfo(TString text, Hal::EInfo status)
Definition Cout.cxx:370
Double_t Eval(Double_t x) const
Definition Splines.cxx:55
void FastOverwrite(TH1D *h, Double_t begval=0, Double_t endval=0)
Definition Splines.cxx:489
Double_t Eval(Double_t x, Double_t y) const
Definition Splines.cxx:327