14#include "FairRunAna.h"
17#include "Math/IFunction.h"
18#include "Minuit2/Minuit2Minimizer.h"
45 double Calculate(
double x,
double y,
double c[])
const {
return c[0]; }
74 double Calculate(
double x,
double y,
double c[])
const {
return c[0] + c[1] *
x + c[2] *
y; }
105 return c[0] + c[1] *
x + c[2] *
y + c[3] *
x *
x + c[4] *
x *
y + c[5] *
y *
y;
144 return c[0] + c[1] *
x + c[2] *
y + c[3] * x2 + c[4] *
x *
y + c[5] * y2 + c[6] * x3 + c[7] * x2y + c[8] * xy2
190 double x2y2 = x2 * y2;
193 return c[0] + c[1] *
x + c[2] *
y + c[3] * x2 + c[4] * xy + c[5] * y2 + c[6] * x3 + c[7] * x2y + c[8] * xy2
194 + c[9] * y3 + c[10] * x4 + c[11] * x3y + c[12] * x2y2 + c[13] * xy3 + c[14] * y4;
239 double x2y2 = x2 * y2;
245 double x2y3 = x2 * y3;
246 double x3y2 = x3 * y2;
249 return c[0] + c[1] *
x + c[2] *
y + c[3] * x2 + c[4] * xy + c[5] * y2 + c[6] * x3 + c[7] * x2y + c[8] * xy2
250 + c[9] * y3 + c[10] * x4 + c[11] * x3y + c[12] * x2y2 + c[13] * xy3 + c[14] * y4 + c[15] * x5 + c[16] * x4y
251 + c[17] * x3y2 + c[18] * x2y3 + c[19] * xy4 + c[20] * y5;
296 double x2y2 = x2 * y2;
302 double x2y3 = x2 * y3;
303 double x3y2 = x3 * y2;
309 double x2y4 = x2 * y4;
310 double x3y3 = x3 * y3;
311 double x4y2 = x4 * y2;
314 return c[0] + c[1] *
x + c[2] *
y + c[3] * x2 + c[4] * xy + c[5] * y2 + c[6] * x3 + c[7] * x2y + c[8] * xy2
315 + c[9] * y3 + c[10] * x4 + c[11] * x3y + c[12] * x2y2 + c[13] * xy3 + c[14] * y4 + c[15] * x5 + c[16] * x4y
316 + c[17] * x3y2 + c[18] * x2y3 + c[19] * xy4 + c[20] * y5 + c[21] * x6 + c[22] * x5y + c[23] * x4y2
317 + c[24] * x3y3 + c[25] * x2y4 + c[26] * xy5 + c[27] * y6;
362 double x2y2 = x2 * y2;
368 double x2y3 = x2 * y3;
369 double x3y2 = x3 * y2;
375 double x2y4 = x2 * y4;
376 double x3y3 = x3 * y3;
377 double x4y2 = x4 * y2;
383 double x2y5 = x2 * y5;
384 double x3y4 = x3 * y4;
385 double x4y3 = x4 * y3;
386 double x5y2 = x5 * y2;
389 return c[0] + c[1] *
x + c[2] *
y + c[3] * x2 + c[4] * xy + c[5] * y2 + c[6] * x3 + c[7] * x2y + c[8] * xy2
390 + c[9] * y3 + c[10] * x4 + c[11] * x3y + c[12] * x2y2 + c[13] * xy3 + c[14] * y4 + c[15] * x5 + c[16] * x4y
391 + c[17] * x3y2 + c[18] * x2y3 + c[19] * xy4 + c[20] * y5 + c[21] * x6 + c[22] * x5y + c[23] * x4y2
392 + c[24] * x3y3 + c[25] * x2y4 + c[26] * xy5 + c[27] * y6 + c[28] * x7 + c[29] * x6y + c[30] * x5y2
393 + c[31] * x4y3 + c[32] * x3y4 + c[33] * x2y5 + c[34] * xy6 + c[35] * y7;
438 double x2y2 = x2 * y2;
444 double x2y3 = x2 * y3;
445 double x3y2 = x3 * y2;
451 double x2y4 = x2 * y4;
452 double x3y3 = x3 * y3;
453 double x4y2 = x4 * y2;
459 double x2y5 = x2 * y5;
460 double x3y4 = x3 * y4;
461 double x4y3 = x4 * y3;
462 double x5y2 = x5 * y2;
468 double x2y6 = x2 * y6;
469 double x3y5 = x3 * y5;
470 double x4y4 = x4 * y4;
471 double x5y3 = x5 * y3;
472 double x6y2 = x6 * y2;
475 return c[0] + c[1] *
x + c[2] *
y + c[3] * x2 + c[4] * xy + c[5] * y2 + c[6] * x3 + c[7] * x2y + c[8] * xy2
476 + c[9] * y3 + c[10] * x4 + c[11] * x3y + c[12] * x2y2 + c[13] * xy3 + c[14] * y4 + c[15] * x5 + c[16] * x4y
477 + c[17] * x3y2 + c[18] * x2y3 + c[19] * xy4 + c[20] * y5 + c[21] * x6 + c[22] * x5y + c[23] * x4y2
478 + c[24] * x3y3 + c[25] * x2y4 + c[26] * xy5 + c[27] * y6 + c[28] * x7 + c[29] * x6y + c[30] * x5y2
479 + c[31] * x4y3 + c[32] * x3y4 + c[33] * x2y5 + c[34] * xy6 + c[35] * y7 + c[36] * x8 + c[37] * x7y
480 + c[38] * x6y2 + c[39] * x5y3 + c[40] * x4y4 + c[41] * x3y5 + c[42] * x2y6 + c[43] * xy7 + c[44] * y8;
525 double x2y2 = x2 * y2;
531 double x2y3 = x2 * y3;
532 double x3y2 = x3 * y2;
538 double x2y4 = x2 * y4;
539 double x3y3 = x3 * y3;
540 double x4y2 = x4 * y2;
546 double x2y5 = x2 * y5;
547 double x3y4 = x3 * y4;
548 double x4y3 = x4 * y3;
549 double x5y2 = x5 * y2;
555 double x2y6 = x2 * y6;
556 double x3y5 = x3 * y5;
557 double x4y4 = x4 * y4;
558 double x5y3 = x5 * y3;
559 double x6y2 = x6 * y2;
565 double x2y7 = x2 * y7;
566 double x3y6 = x3 * y6;
567 double x4y5 = x4 * y5;
568 double x5y4 = x5 * y4;
569 double x6y3 = x6 * y3;
570 double x7y2 = x7 * y2;
573 return c[0] + c[1] *
x + c[2] *
y + c[3] * x2 + c[4] * xy + c[5] * y2 + c[6] * x3 + c[7] * x2y + c[8] * xy2
574 + c[9] * y3 + c[10] * x4 + c[11] * x3y + c[12] * x2y2 + c[13] * xy3 + c[14] * y4 + c[15] * x5 + c[16] * x4y
575 + c[17] * x3y2 + c[18] * x2y3 + c[19] * xy4 + c[20] * y5 + c[21] * x6 + c[22] * x5y + c[23] * x4y2
576 + c[24] * x3y3 + c[25] * x2y4 + c[26] * xy5 + c[27] * y6 + c[28] * x7 + c[29] * x6y + c[30] * x5y2
577 + c[31] * x4y3 + c[32] * x3y4 + c[33] * x2y5 + c[34] * xy6 + c[35] * y7 + c[36] * x8 + c[37] * x7y
578 + c[38] * x6y2 + c[39] * x5y3 + c[40] * x4y4 + c[41] * x3y5 + c[42] * x2y6 + c[43] * xy7 + c[44] * y8
579 + c[45] * x9 + c[46] * x8y + c[47] * x7y2 + c[48] * x6y3 + c[49] * x5y4 + c[50] * x4y5 + c[51] * x3y6
580 + c[52] * x2y7 + c[53] * xy8 + c[54] * y9;
624 double x2y2 = x2 * y2;
630 double x2y3 = x2 * y3;
631 double x3y2 = x3 * y2;
637 double x2y4 = x2 * y4;
638 double x3y3 = x3 * y3;
639 double x4y2 = x4 * y2;
645 double x2y5 = x2 * y5;
646 double x3y4 = x3 * y4;
647 double x4y3 = x4 * y3;
648 double x5y2 = x5 * y2;
654 double x2y6 = x2 * y6;
655 double x3y5 = x3 * y5;
656 double x4y4 = x4 * y4;
657 double x5y3 = x5 * y3;
658 double x6y2 = x6 * y2;
664 double x2y7 = x2 * y7;
665 double x3y6 = x3 * y6;
666 double x4y5 = x4 * y5;
667 double x5y4 = x5 * y4;
668 double x6y3 = x6 * y3;
669 double x7y2 = x7 * y2;
675 double x2y8 = x2 * y8;
676 double x3y7 = x3 * y7;
677 double x4y6 = x4 * y6;
678 double x5y5 = x5 * y5;
679 double x6y4 = x6 * y4;
680 double x7y3 = x7 * y3;
681 double x8y2 = x8 * y2;
684 return c[0] + c[1] *
x + c[2] *
y + c[3] * x2 + c[4] * xy + c[5] * y2 + c[6] * x3 + c[7] * x2y + c[8] * xy2
685 + c[9] * y3 + c[10] * x4 + c[11] * x3y + c[12] * x2y2 + c[13] * xy3 + c[14] * y4 + c[15] * x5 + c[16] * x4y
686 + c[17] * x3y2 + c[18] * x2y3 + c[19] * xy4 + c[20] * y5 + c[21] * x6 + c[22] * x5y + c[23] * x4y2
687 + c[24] * x3y3 + c[25] * x2y4 + c[26] * xy5 + c[27] * y6 + c[28] * x7 + c[29] * x6y + c[30] * x5y2
688 + c[31] * x4y3 + c[32] * x3y4 + c[33] * x2y5 + c[34] * xy6 + c[35] * y7 + c[36] * x8 + c[37] * x7y
689 + c[38] * x6y2 + c[39] * x5y3 + c[40] * x4y4 + c[41] * x3y5 + c[42] * x2y6 + c[43] * xy7 + c[44] * y8
690 + c[45] * x9 + c[46] * x8y + c[47] * x7y2 + c[48] * x6y3 + c[49] * x5y4 + c[50] * x4y5 + c[51] * x3y6
691 + c[52] * x2y7 + c[53] * xy8 + c[54] * y9 + c[55] * x10 + c[56] * x9y + c[57] * x8y2 + c[58] * x7y3
692 + c[59] * x6y4 + c[60] * x5y5 + c[61] * x4y6 + c[62] * x3y7 + c[63] * x2y8 + c[64] * xy9 + c[65] * y10;
713 FCNPolynom(
const std::vector<double>&
x,
const std::vector<double>&
y,
const std::vector<double>& z,
730 virtual double Up()
const {
return 1.; }
741 double* p =
const_cast<double*
>(
x);
743 for (
unsigned int i = 0; i <
fX.size(); i++) {
768 std::vector<double>
fX;
769 std::vector<double>
fY;
770 std::vector<double>
fZ;
780 , fUseEllipseAcc(true)
781 , fPolynomDegree(polynomDegree)
783 fField = FairRunAna::Instance()->GetField();
785 switch (polynomDegree) {
807 std::vector<double>& parBz)
809 double tanXangle = std::tan(
fXangle * 3.14159265 / 180.);
810 double tanYangle = std::tan(
fYangle * 3.14159265 / 180.);
812 double Xmax = Z * tanXangle;
813 double Ymax = Z * tanYangle;
815 std::cout <<
"Fit slice. Xmax=" << Xmax <<
" Ymax=" << Ymax <<
" Z=" << Z <<
" Xanlge=" <<
fXangle
816 <<
" Yangle=" <<
fYangle << std::endl;
820 std::vector<double>
x;
821 std::vector<double>
y;
822 std::vector<double> Bx;
823 std::vector<double> By;
824 std::vector<double> Bz;
826 double X = -Xmax + j * HX;
828 double Y = -Ymax + k * HY;
832 double el = (X * X) / (Xmax * Xmax) + (Y * Y) / (Ymax * Ymax);
839 double pos[3] = {X, Y, Z};
856 const std::vector<double>& z, std::vector<double>& par)
861 ROOT::Minuit2::Minuit2Minimizer theMinuit;
863 theMinuit.SetPrintLevel(-1);
864 theMinuit.SetFunction(*theFCN);
867 for (
int i = 0; i < nofParams; i++) {
869 theMinuit.SetVariable(i, ss.c_str(), 0., 0.1);
870 theMinuit.SetVariableLimits(i, 1., -1.);
873 theMinuit.Minimize();
875 const double* fitResults = theMinuit.X();
878 for (
int i = 0; i < nofParams; i++) {
879 par.push_back(fitResults[i]);
886 std::vector<double>& parBz)
889 const int N = (M + 1) * (M + 2) / 2;
891 double tanXangle = std::tan(
fXangle * 3.14159265 / 180);
892 double tanYangle = std::tan(
fYangle * 3.14159265 / 180);
894 double Xmax = Z * tanXangle;
895 double Ymax = Z * tanYangle;
900 if (dx > Xmax / N / 2) {
903 if (dy > Ymax / N / 2) {
916 TVectorD b0(N), b1(N), b2(N);
920 for (
int i = 0; i < N; i++) {
921 for (
int j = 0; j < N; j++) {
924 b0(i) = b1(i) = b2(i) = 0.;
926 for (
double x = -Xmax;
x <= Xmax;
x += dx) {
927 for (
double y = -Ymax;
y <= Ymax;
y += dy) {
928 double r = std::sqrt(std::fabs(
x *
x / Xmax / Xmax +
y / Ymax *
y / Ymax));
932 Double_t w = 1. / (r * r + 1);
933 Double_t p[3] = {
x,
y, Z};
934 Double_t B[3] = {0., 0., 0.};
935 FairRunAna::Instance()->GetField()->GetFieldValue(p, B);
938 for (
int i = 1; i <= M; i++) {
939 int k = (i - 1) * (i) / 2;
940 int l = i * (i + 1) / 2;
941 for (
int j = 0; j < i; j++) {
942 m(l + j) =
x * m(k + j);
944 m(l + i) =
y * m(k + i - 1);
948 for (
int i = 0; i < N; i++) {
949 for (
int j = 0; j < N; j++) {
950 A(i, j) += w * m(i) * m(j);
952 b0(i) += w * B[0] * m(i);
953 b1(i) += w * B[1] * m(i);
954 b2(i) += w * B[2] * m(i);
960 TVectorD c0 = A * b0, c1 = A * b1, c2 = A * b2;
966 for (
int i = 0; i < N; i++) {
967 parBx.push_back(c0(i));
968 parBy.push_back(c1(i));
969 parBz.push_back(c2(i));
Implementation of the polynomial field approximation.
void FitSlice(float Z, lit::parallel::LitFieldSlice< T > &slice)
Fits (X, Y) slice of the magnetic field at Z position.
void FitSliceVec(float Z, lit::parallel::LitFieldSlice< fvec > &slice)
FitSlice implementation using fvec data type.
void FitSliceMy(double Z, std::vector< double > &parBx, std::vector< double > &parBy, std::vector< double > &parBz)
Fit (X, Y) slice of the magnetic field at Z position.
virtual ~CbmLitFieldFitter()
Destructor.
void FitSliceScal(float Z, lit::parallel::LitFieldSlice< fscal > &slice)
FitSlice implementation using fscal data type.
CbmLitFieldFitter(unsigned int polynomDegree)
Constructor.
unsigned int fPolynomDegree
0 degree polynomial with 1 coefficient.
virtual ~CbmLitPolynom0()
Destructor.
unsigned int GetNofCoefficients() const
Inherited from CbmLitPolynom.
double Calculate(double x, double y, double c[]) const
Inherited from CbmLitPolynom.
CbmLitPolynom0()
Constructor.
10th degree polynomial with 66 coefficients.
unsigned int GetNofCoefficients() const
Inherited from CbmLitPolynom.
CbmLitPolynom10()
Constructor.
virtual ~CbmLitPolynom10()
Destructor.
double Calculate(double x, double y, double c[]) const
Inherited from CbmLitPolynom.
1st degree polynomial with 3 coefficients.
unsigned int GetNofCoefficients() const
Inherited from CbmLitPolynom.
virtual ~CbmLitPolynom1()
Destructor.
double Calculate(double x, double y, double c[]) const
Inherited from CbmLitPolynom.
CbmLitPolynom1()
Constructor.
2nd degree polynomial with 6 coefficients.
unsigned int GetNofCoefficients() const
Inherited from CbmLitPolynom.
double Calculate(double x, double y, double c[]) const
Inherited from CbmLitPolynom.
virtual ~CbmLitPolynom2()
Destructor.
CbmLitPolynom2()
Constructor.
3rd degree polynomial with 10 coefficients.
unsigned int GetNofCoefficients() const
Inherited from CbmLitPolynom.
virtual ~CbmLitPolynom3()
Destructor.
CbmLitPolynom3()
Constructor.
double Calculate(double x, double y, double c[]) const
Inherited from CbmLitPolynom.
4th degree polynomial with 15 coefficients.
double Calculate(double x, double y, double c[]) const
Inherited from CbmLitPolynom.
CbmLitPolynom4()
Constructor.
unsigned int GetNofCoefficients() const
Inherited from CbmLitPolynom.
virtual ~CbmLitPolynom4()
Destructor.
5th degree polynomial with 21 coefficients.
CbmLitPolynom5()
Constructor.
virtual ~CbmLitPolynom5()
Destructor.
double Calculate(double x, double y, double c[]) const
Inherited from CbmLitPolynom.
unsigned int GetNofCoefficients() const
Inherited from CbmLitPolynom.
6th degree polynomial with 28 coefficients.
unsigned int GetNofCoefficients() const
Inherited from CbmLitPolynom.
CbmLitPolynom6()
Constructor.
virtual ~CbmLitPolynom6()
Destructor.
double Calculate(double x, double y, double c[]) const
Inherited from CbmLitPolynom.
7th degree polynomial with 36 coefficients.
virtual ~CbmLitPolynom7()
Destructor.
double Calculate(double x, double y, double c[]) const
Inherited from CbmLitPolynom.
CbmLitPolynom7()
Constructor.
unsigned int GetNofCoefficients() const
Inherited from CbmLitPolynom.
8th degree polynomial with 45 coefficients.
CbmLitPolynom8()
Constructor.
double Calculate(double x, double y, double c[]) const
Inherited from CbmLitPolynom.
virtual ~CbmLitPolynom8()
Destructor.
unsigned int GetNofCoefficients() const
Inherited from CbmLitPolynom.
9th degree polynomial with 55 coefficients.
double Calculate(double x, double y, double c[]) const
Inherited from CbmLitPolynom.
unsigned int GetNofCoefficients() const
Inherited from CbmLitPolynom.
virtual ~CbmLitPolynom9()
Destructor.
CbmLitPolynom9()
Constructor.
Abstract class for polynomial function.
virtual unsigned int GetNofCoefficients() const =0
Return number of coefficients for this polynomial function.
virtual double Calculate(double x, double y, double c[]) const =0
Returns calculated value.
Implements FCNBase which is used for MINUIT minimization.
unsigned int NDim() const
const CbmLitPolynom * GetPolynom() const
Return polynomial which is used for minimization.
Double_t DoEval(const Double_t *x) const
Inherited from FCNBase.
FCNPolynom(const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > &z, CbmLitPolynom *polynom)
Constructor.
ROOT::Math::IBaseFunctionMultiDim * Clone() const
virtual double Up() const
Inherited from FCNBase.
Approximated magnetic field slice in XY plane perpendicular to Z.
std::string ToString(const T &value)