10#include "CorrFitSmear1DCF.h"
13#include "CorrFitMapKstarRstar.h"
30 new TF1(Hal::Std::GetUniqueName(
"func_draw"),
this, &CorrFitSmear1DCF::GaussNS, -10, 10, 2, this->ClassName(),
"GaussNS");
33 CorrFitSmear1DCF::~CorrFitSmear1DCF() {
35 if (fFunc)
delete fFunc;
36 if (fSpline)
delete fSpline;
37 if (fSplot)
delete fSplot;
56 Hal::Std::GetAxisPar(*fCF->
GetNum(), binsX, minX, maxX,
"x");
57 TH2D newMatrix(
"newMatrix",
"newMatrix", binsX, minX, maxX, binsX, minX, maxX);
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);
72 auto histo = newMap->GetHisto();
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]);
86 std::cout << std::endl;
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++) {
104 histo->SetBinContent(j, i, numVecSmeared[j - 1][0] / denVecSmeared[j - 1][0] + correction[j - 1]);
106 histo->SetBinContent(j, i, numVecSmeared[j - 1][0] / denVecSmeared[j - 1][0]);
111 auto maph = fMap->GetHisto();
112 for (
int i = 1; i <= maph->GetNbinsY(); i++) {
113 auto sim = GetVec(*maph, i);
115 auto reco = smearMT * sim;
118 print(sim,
"SIM MOM");
119 print(smearMT,
"MATRIX");
122 for (
int j = 1; j <= sizeQ; j++) {
124 histo->SetBinContent(j, i, reco[j - 1][0] + correction[j - 1]);
126 histo->SetBinContent(j, i, reco[j - 1][0]);
137 auto histo = newMap->GetHisto();
139 std::vector<double> norms = GetGausNorms(func, histo->GetXaxis());
140 TAxis* y = histo->GetYaxis();
141 TAxis* x = histo->GetXaxis();
147 auto h = Hal::Std::GetProjection1D(histo, 1, 1,
"x+bins");
152 new TF1(Hal::Std::GetUniqueName(
"func_splot"),
this, &CorrFitSmear1DCF::Splot, -10, 10, 1, this->ClassName(),
"Splot");
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()));
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]);
164 fFunc->FixParameter(0, 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);
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()));
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]);
180 fFunc->FixParameter(0, 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);
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);
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);
207 void CorrFitSmear1DCF::NormalizeMatrix(TMatrixD& matrix)
const {
208 int rowcol = matrix.GetNrows();
209 for (
int i = 0; i < rowcol; i++) {
211 for (
int j = 0; j < rowcol; j++) {
220 for (
int j = 0; j < rowcol; j++) {
221 matrix[j][i] = matrix[j][i] * sum;
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];
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));
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])));
248 Double_t CorrFitSmear1DCF::CorrFunc(Double_t* x, Double_t* )
const {
return fSpline->
Eval(x[0]); }
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]);
256 std::vector<double> CorrFitSmear1DCF::GetAutoFill(
const TMatrixD& smearMT, Bool_t lower)
const {
257 std::vector<double> correction;
258 int size = smearMT.GetNrows();
262 for (
int i = 0; i < half; i++) {
264 for (
int j = 0; j < i; j++) {
265 sum -= smearMT[j][i];
267 for (
int j = i + 1; j < size; j++) {
268 sum += smearMT[j][i];
270 if (sum < 0) sum = 0;
271 correction.push_back(sum);
275 for (
int i = half; i < size; i++) {
277 for (
int j = 0; j < i; j++) {
278 sum += smearMT[j][i];
280 for (
int j = i + 1; j < size; j++) {
281 sum -= smearMT[j][i];
283 if (sum < 0) sum = 0;
284 correction.push_back(sum);
287 for (
int i = 0; i < size; i++) {
289 for (
int j = 0; j < i; j++) {
290 sum += smearMT[j][i];
292 for (
int j = i + 1; j < size; j++) {
293 sum -= smearMT[j][i];
295 if (sum < 0) sum = 0;
296 correction.push_back(sum);
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);
313 norms.push_back(1.0 / integral);
315 norms.push_back(1.0);
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);
327 if (fCF->
GetNum()->GetXaxis()->GetBinCenter(1) < 0) {
328 reversed[0][0] = fCF->
GetDen()->GetBinContent(1);
332 reversed[nbins + 1][0] = fCF->
GetDen()->GetBinContent(nbins);
333 TMatrixD smearMT(nbins + 2, nbins + 2);
335 smearMT[0][0] = smearMT[nbins + 1][nbins + 1] = 1;
336 TAxis* x = fCF->
GetNum()->GetXaxis();
338 fFunc->FixParameter(0, 1);
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++) {
345 Double_t density = fFunc->Eval(sigma);
346 smearMT[jReco][iSim] = density;
350 for (
int iSim = 1; iSim <= nbins; iSim++) {
351 for (
int jReco = 1; jReco <= nbins; jReco++) {
352 smearMT[jReco][iSim] = smearMatrix->GetBinContent(iSim, jReco);
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];
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)
Double_t Eval(Double_t x) const
void FastOverwrite(TH1D *h, Double_t begval=0, Double_t endval=0)
Double_t Eval(Double_t x, Double_t y) const