7#include "TLorentzVector.h"
17 const Int_t nParts = 2;
18 const Int_t nComp = 3;
20 C =
new TH1F**[nParts];
21 M =
new TH1F**[nParts];
22 S =
new TH1F**[nParts];
24 f =
new TFile(Form(
"fitParMonitor_%s.root", out.Data()),
"recreate");
26 for (Int_t iPart = 0; iPart < nParts; iPart++) {
27 C[iPart] =
new TH1F*[nComp];
28 M[iPart] =
new TH1F*[nComp];
29 S[iPart] =
new TH1F*[nComp];
30 for (Int_t iComp = 0; iComp < nComp; iComp++) {
31 C[iPart][iComp] =
new TH1F(Form(
"Constant_%d_%d", iPart, iComp),
"Constant", 100, 0., 0.);
32 M[iPart][iComp] =
new TH1F(Form(
"Mean_%d_%d", iPart, iComp),
"Mean", 100, 0., 0.);
33 S[iPart][iComp] =
new TH1F(Form(
"Sigma_%d_%d", iPart, iComp),
"Sigma", 100, 0., 0.);
40 for (Int_t iPart = 0; iPart < 2; iPart++) {
41 for (Int_t iComp = 0; iComp < 3; iComp++) {
42 C[iPart][iComp]->Write();
43 M[iPart][iComp]->Write();
44 S[iPart][iComp]->Write();
53 for (Int_t iPart = 0; iPart < 2; iPart++) {
54 for (Int_t iComponent = 0; iComponent < 3; iComponent++) {
55 delete constant[iPart][iComponent];
56 delete mean[iPart][iComponent];
57 delete sigma[iPart][iComponent];
59 delete [] constant[iPart];
60 delete [] mean[iPart];
61 delete [] sigma[iPart];
105 winXstep = (WinXmax - WinXmin) / (dimNhist - 1);
106 winYstep = (WinYmax - WinYmin) / (dimNhist - 1);
107 winZstep = (WinZmax - WinZmin) / (dimNhist - 1);
109 const Int_t nComponents = 3;
110 const Int_t nParts = 2;
112 constant =
new TGraph2D**[nParts];
113 mean =
new TGraph2D**[nParts];
114 sigma =
new TGraph2D**[nParts];
115 for (Int_t iPart = 0; iPart < nParts; iPart++) {
116 constant[iPart] =
new TGraph2D*[nComponents];
117 mean[iPart] =
new TGraph2D*[nComponents];
118 sigma[iPart] =
new TGraph2D*[nComponents];
119 for (Int_t iComponent = 0; iComponent < nComponents; iComponent++) {
120 constant[iPart][iComponent] =
new TGraph2D();
121 mean[iPart][iComponent] =
new TGraph2D();
122 sigma[iPart][iComponent] =
new TGraph2D();
132 if (axis.Contains(
"X")) {
135 }
else if (axis.Contains(
"Z")) {
140 const Int_t nParts = 2;
141 const Int_t nComponents = 3;
143 if (axis.Contains(
"X")) {
144 for (Int_t iPart = 0; iPart < nParts; iPart++)
145 for (Int_t iComp = 0; iComp < nComponents; iComp++)
146 DoExtrapolationAlongX(iPart, iComp);
147 }
else if (axis.Contains(
"Z")) {
148 for (Int_t iPart = 0; iPart < nParts; iPart++)
149 for (Int_t iComp = 0; iComp < nComponents; iComp++)
150 DoExtrapolationAlongZ(iPart, iComp);
152 cout <<
"Not supported direction!!! Exiting ..." << endl;
159void BmnSP41FieldMapCreator::DoExtrapolationAlongX(Int_t iPart, Int_t iComp) {
160 TGraph*** bField =
new TGraph**[dimGrid];
161 TF1*** fit_func =
new TF1**[dimNhist];
162 TString comp = (iComp == 0) ?
"x" : (iComp == 1) ?
"y" : (iComp == 2) ?
"z" :
"";
164 Double_t Ymax = fMap->
GetYmax();
165 Double_t Ymin = fMap->
GetYmin();
167 Double_t Zmax = fMap->
GetZmax();
168 Double_t Zmin = fMap->
GetZmin();
171 Int_t nSlicesY = dimGrid - 1;
172 Int_t nSlicesZ = dimGrid - 1;
175 Double_t dz = (Zmax - Zmin) / nSlicesZ;
176 Double_t dy = (Ymax - Ymin) / nSlicesY;
180 for (Int_t
i = 0;
i < dimGrid;
i++) {
183 cout <<
"Processing node " <<
i << endl;
184 bField[
i] =
new TGraph*[dimGrid];
185 Double_t ySlice = Ymin +
i * dy;
187 for (Int_t j = 0; j < dimGrid; j++) {
188 Float_t zSlice = Zmin + j * dz;
189 bField[
i][j] =
new TGraph();
191 for (Int_t k = 0; k < fMap->
GetNx(); k++) {
193 bField[
i][j]->SetPoint(k, x, (comp ==
"x") ? fMap->
GetBx(x, ySlice, zSlice) :
194 (comp ==
"y") ? fMap->GetBy(x, ySlice, zSlice) :
195 (comp ==
"z") ? fMap->GetBz(x, ySlice, zSlice) : 0.0);
198 vector <Double_t> fit_par;
203 f->SetParameter(2, 50.);
204 fit_par = bField[
i][j]->Fit(
f,
"SQ",
"",
f->GetXmin(),
f->GetXmax())->Parameters();
207 f->SetParameter(2, 100.);
208 fit_par = bField[
i][j]->Fit(
f,
"SQ",
"",
f->GetXmin(),
f->GetXmax())->Parameters();
211 fMonitor->
C[iPart][iComp]->Fill(fit_par[0]);
212 fMonitor->
M[iPart][iComp]->Fill(fit_par[1]);
213 fMonitor->
S[iPart][iComp]->Fill(fit_par[2]);
215 constant[iPart][iComp]->SetPoint(counter, ySlice, zSlice, fit_par[0]);
216 mean[iPart][iComp]->SetPoint(counter, ySlice, zSlice, fit_par[1]);
217 sigma[iPart][iComp]->SetPoint(counter, ySlice, zSlice, fit_par[2]);
226 for (Int_t
i = 0;
i < dimNhist;
i++) {
227 fit_func[
i] =
new TF1*[dimNhist];
228 Double_t y = WinYmin +
i * winYstep;
230 for (Int_t j = 0; j < dimNhist; j++) {
231 Double_t z = WinZmin + j * winZstep;
232 fit_func[
i][j] =
new TF1(
"func",
"[0]*exp(-0.5*((x-[1])/[2])**2)",
233 (iPart == 1) ? fConditions->
tailRangeRight.second : fConditions->
min, (iPart == 1) ? fConditions->
max : fConditions->tailRangeLeft.first);
234 fit_func[
i][j]->SetParameter(0, constant[iPart][iComp]->Interpolate(y, z));
235 fit_func[
i][j]->SetParameter(1, mean[iPart][iComp]->Interpolate(y, z));
236 fit_func[
i][j]->SetParameter(2, sigma[iPart][iComp]->Interpolate(y, z));
240 DrawProfileExtrapX(dimNhist, bField, fit_func,
"B" + comp +
".png", comp, Ymin, Zmin, dy, dz);
242 for (Int_t
i = 0;
i < dimNhist;
i++) {
243 for (Int_t j = 0; j < dimNhist; j++)
244 delete fit_func[
i][j];
245 delete [] fit_func[
i];
249 for (Int_t
i = 0;
i < dimGrid;
i++) {
250 for (Int_t j = 0; j < dimGrid; j++)
257void BmnSP41FieldMapCreator::DoExtrapolationAlongZ(Int_t iPart, Int_t iComp) {
258 TGraph*** bField =
new TGraph**[dimGrid];
259 TF1*** fit_func =
new TF1**[dimNhist];
260 TString comp = (iComp == 0) ?
"x" : (iComp == 1) ?
"y" : (iComp == 2) ?
"z" :
"";
262 Double_t Xmax = fMap->
GetXmax();
263 Double_t Xmin = fMap->
GetXmin();
265 Double_t Ymax = fMap->
GetYmax();
266 Double_t Ymin = fMap->
GetYmin();
269 Int_t nSlicesX = dimGrid - 1;
270 Int_t nSlicesY = dimGrid - 1;
273 Double_t dx = (Xmax - Xmin) / nSlicesX;
274 Double_t dy = (Ymax - Ymin) / nSlicesY;
278 for (Int_t
i = 0;
i < dimGrid;
i++) {
281 cout <<
"Processing node " <<
i << endl;
282 bField[
i] =
new TGraph*[dimGrid];
283 Double_t xSlice = Xmin +
i * dx;
285 for (Int_t j = 0; j < dimGrid; j++) {
286 Float_t ySlice = Ymin + j * dy;
287 bField[
i][j] =
new TGraph();
289 for (Int_t k = 0; k < fMap->
GetNz(); k++) {
291 bField[
i][j]->SetPoint(k, z, (comp ==
"x") ? fMap->
GetBx(xSlice, ySlice, z) :
292 (comp ==
"y") ? fMap->GetBy(xSlice, ySlice, z) :
293 (comp ==
"z") ? fMap->GetBz(xSlice, ySlice, z) : 0.0);
296 vector <Double_t> fit_par;
301 f->SetParameter(2, 100.);
302 fit_par = bField[
i][j]->Fit(
f,
"SQ",
"G",
f->GetXmin(),
f->GetXmax())->Parameters();
305 f->SetParameter(2, 120.);
306 fit_par = bField[
i][j]->Fit(
f,
"SQ",
"G",
f->GetXmin(),
f->GetXmax())->Parameters();
309 fMonitor->
C[iPart][iComp]->Fill(fit_par[0]);
310 fMonitor->
M[iPart][iComp]->Fill(fit_par[1]);
311 fMonitor->
S[iPart][iComp]->Fill(fit_par[2]);
313 constant[iPart][iComp]->SetPoint(counter, xSlice, ySlice, fit_par[0]);
314 mean[iPart][iComp]->SetPoint(counter, xSlice, ySlice, fit_par[1]);
315 sigma[iPart][iComp]->SetPoint(counter, xSlice, ySlice, fit_par[2]);
324 for (Int_t
i = 0;
i < dimNhist;
i++) {
325 fit_func[
i] =
new TF1*[dimNhist];
326 Double_t x = WinXmin +
i * winXstep;
328 for (Int_t j = 0; j < dimNhist; j++) {
329 Double_t y = WinYmin + j * winYstep;
330 fit_func[
i][j] =
new TF1(
"func",
"[0]*exp(-0.5*((x-[1])/[2])**2)", (iPart == 1) ? fConditions->
tailRangeRight.second : fConditions->
min,
331 (iPart == 1) ? fConditions->
max : fConditions->tailRangeLeft.first);
332 fit_func[
i][j]->SetParameter(0, constant[iPart][iComp]->Interpolate(x, y));
333 fit_func[
i][j]->SetParameter(1, mean[iPart][iComp]->Interpolate(x, y));
334 fit_func[
i][j]->SetParameter(2, sigma[iPart][iComp]->Interpolate(x, y));
338 DrawProfileExtrapZ(dimNhist, bField, fit_func,
"B" + comp +
".pdf", comp, Xmin, Ymin, dx, dy);
340 for (Int_t
i = 0;
i < dimNhist;
i++) {
341 for (Int_t j = 0; j < dimNhist; j++)
342 delete fit_func[
i][j];
343 delete [] fit_func[
i];
347 for (Int_t
i = 0;
i < dimGrid;
i++) {
348 for (Int_t j = 0; j < dimGrid; j++)
355void BmnSP41FieldMapCreator::DrawProfileExtrapZ(Int_t
dim, TGraph*** gr, TF1***
f, TString name, TString comp, Double_t Xmin, Double_t Ymin, Double_t dx, Double_t dy) {
356 TCanvas* c =
new TCanvas(
"1",
"1", 1200, 800);
359 for (Int_t
i = 0;
i <
dim;
i++) {
360 Double_t X = WinXmin +
i * winXstep;
361 Double_t sliceX = (X - Xmin) / dx;
362 Int_t shiftI = Int_t(sliceX);
363 for (Int_t j = 0; j <
dim; j++) {
364 Double_t Y = WinYmin + j * winYstep;
365 Double_t sliceY = (Y - Ymin) / dy;
366 Int_t shiftJ = Int_t(sliceY);
368 gr[shiftI][shiftJ]->Draw();
369 gr[shiftI][shiftJ]->GetXaxis()->SetLimits(fConditions->
min, fConditions->
max);
370 gr[shiftI][shiftJ]->SetTitle(
"B" + comp + TString(Form(
", x = %G [cm]", X)) + TString(Form(
", y = %G [cm]", Y)));
371 gr[shiftI][shiftJ]->GetXaxis()->SetTitleSize(0.1);
372 gr[shiftI][shiftJ]->GetXaxis()->SetLabelSize(0.08);
373 gr[shiftI][shiftJ]->GetYaxis()->SetTitleSize(0.1);
374 gr[shiftI][shiftJ]->GetYaxis()->SetLabelSize(0.1);
375 gr[shiftI][shiftJ]->GetXaxis()->SetTitle(
"Z, cm");
376 gr[shiftI][shiftJ]->GetYaxis()->SetTitle(
"B" + comp +
", kG");
378 f[
i][j]->Draw(
"same");
385void BmnSP41FieldMapCreator::DrawProfileExtrapX(Int_t
dim, TGraph*** gr, TF1***
f, TString name, TString comp, Double_t Ymin, Double_t Zmin, Double_t dy, Double_t dz) {
386 gStyle->SetTitleFontSize(0.1);
387 TCanvas* c =
new TCanvas(
"1",
"1", 4000, 4000);
390 for (Int_t
i = 0;
i <
dim;
i++) {
391 Double_t Y = WinYmin +
i * winYstep;
392 Double_t sliceY = (Y - Ymin) / dy;
393 Int_t shiftI = Int_t(sliceY);
394 for (Int_t j = 0; j <
dim; j++) {
395 Double_t Z = WinZmin + j * winZstep;
396 Double_t sliceZ = (Z - Zmin) / dz;
397 Int_t shiftJ = Int_t(sliceZ);
399 gr[shiftI][shiftJ]->Draw();
400 gr[shiftI][shiftJ]->GetXaxis()->SetLimits(fConditions->
min, fConditions->
max);
401 gr[shiftI][shiftJ]->SetLineWidth(2);
402 gr[shiftI][shiftJ]->GetXaxis()->SetNdivisions(5);
403 gr[shiftI][shiftJ]->SetTitle(
"B" + comp + TString(Form(
", y = %G [cm]", Y)) + TString(Form(
", z = %G [cm]", Z)));
404 gr[shiftI][shiftJ]->GetXaxis()->SetTitleSize(0.1);
405 gr[shiftI][shiftJ]->GetXaxis()->SetLabelSize(0.08);
406 gr[shiftI][shiftJ]->GetYaxis()->SetTitleSize(0.1);
407 gr[shiftI][shiftJ]->GetYaxis()->SetLabelSize(0.1);
408 gr[shiftI][shiftJ]->GetXaxis()->SetTitle(
"X, cm");
409 gr[shiftI][shiftJ]->GetYaxis()->SetTitle(
"B" + comp +
", kG");
411 f[
i][j]->Draw(
"same");
412 f[
i][j]->SetLineWidth(2);
421 Double_t Xmin = fMap->
GetXmin();
422 Double_t Ymin = fMap->
GetYmin();
423 Double_t Zmin = fMap->
GetZmin();
425 Double_t Xmax = fMap->
GetXmax();
426 Double_t Ymax = fMap->
GetYmax();
427 Double_t Zmax = fMap->
GetZmax();
433 Int_t Nx = fMap->
GetNx();
434 Int_t Ny = fMap->
GetNy();
435 Int_t Nz = fMap->
GetNz();
440 Double_t x = -1., y = -1., z = -1.;
442 ofstream mapFile(nameOutput);
444 mapFile.precision(6);
445 mapFile << showpoint;
446 mapFile <<
"nosym" << endl;
451 if (axis.Contains(
"X")) {
452 Xmin = fConditions->
min;
453 Xmax = fConditions->
max;
454 Nx = Int_t((Xmax - Xmin) / 2.) + 1;
455 mapFile << Xmin <<
" " << Xmax <<
" " << Nx << endl;
457 mapFile << Xmin <<
" " << Xmax <<
" " << Nx << endl;
459 mapFile << Ymin <<
" " << Ymax <<
" " << Ny << endl;
461 if (axis.Contains(
"Z")) {
462 Zmin = fConditions->
min;
463 Zmax = fConditions->
max;
464 Nz = Int_t((Zmax - Zmin) / 2.) + 1;
465 mapFile << Zmin <<
" " << Zmax <<
" " << Nz << endl;
467 mapFile << Zmin <<
" " << Zmax <<
" " << Nz << endl;
469 for (Int_t ix = 0; ix < Nx; ix++) {
471 for (Int_t iy = 0; iy < Ny; iy++) {
474 cout << x <<
" " << y << endl;
475 for (Int_t iz = 0; iz < Nz; iz++) {
479 Double_t bx = -1., by = -1., bz = -1.;
481 Bool_t isInMap = kTRUE;
483 if (axis.Contains(
"X")) {
484 if (x < fMap->GetXmin()) {
486 bx = FieldExtrapolate(x, y, z, constant[0][0], mean[0][0], sigma[0][0]);
487 by = FieldExtrapolate(x, y, z, constant[0][1], mean[0][1], sigma[0][1]);
488 bz = FieldExtrapolate(x, y, z, constant[0][2], mean[0][2], sigma[0][2]);
489 }
else if (x > fMap->
GetXmax() - 1.) {
491 bx = FieldExtrapolate(x, y, z, constant[1][0], mean[1][0], sigma[1][0]);
492 by = FieldExtrapolate(x, y, z, constant[1][1], mean[1][1], sigma[1][1]);
493 bz = FieldExtrapolate(x, y, z, constant[1][2], mean[1][2], sigma[1][2]);
495 }
else if (axis.Contains(
"Z")) {
496 if (z < fMap->GetZmin()) {
498 bx = FieldExtrapolate(x, y, z, constant[0][0], mean[0][0], sigma[0][0]);
499 by = FieldExtrapolate(x, y, z, constant[0][1], mean[0][1], sigma[0][1]);
500 bz = FieldExtrapolate(x, y, z, constant[0][2], mean[0][2], sigma[0][2]);
501 }
else if (z > fMap->
GetZmax() - 1.) {
503 bx = FieldExtrapolate(x, y, z, constant[1][0], mean[1][0], sigma[1][0]);
504 by = FieldExtrapolate(x, y, z, constant[1][1], mean[1][1], sigma[1][1]);
505 bz = FieldExtrapolate(x, y, z, constant[1][2], mean[1][2], sigma[1][2]);
510 bx = fMap->
GetBx(x, y, z);
511 by = fMap->
GetBy(x, y, z);
512 bz = fMap->
GetBz(x, y, z);
516 mapFile << checkedFieldValue(bx, by, bz).at(0) / factor <<
" " << checkedFieldValue(bx, by, bz).at(1) / factor <<
" " <<
517 checkedFieldValue(bx, by, bz).at(2) / factor << endl;
525 vector <TLorentzVector> vSmoo;
527 for (Int_t iSmooth = 0; iSmooth < sValues->GetEntriesFast(); iSmooth++) {
530 TLorentzVector tmp(sValue->
X, sValue->
Y, sValue->
Z, sValue->
fieldValue);
531 vSmoo.push_back(tmp);
534 Double_t Xmin = fMap->
GetXmin();
535 Double_t Ymin = fMap->
GetYmin();
536 Double_t Zmin = fMap->
GetZmin();
537 Double_t Zmax = fMap->
GetZmax();
543 Int_t Nx = fMap->
GetNx();
544 Int_t Ny = fMap->
GetNy();
545 Int_t Nz = fMap->
GetNz();
549 Double_t x, y, z, bx, by, bz;
552 ofstream mapFile(nameOutput);
554 mapFile.precision(6);
555 mapFile << showpoint;
556 mapFile <<
"nosym" << endl;
557 mapFile << Xmin <<
" " << -Xmin <<
" " << Nx << endl;
558 mapFile << Ymin <<
" " << -Ymin <<
" " << Ny << endl;
559 mapFile << Zmin <<
" " << Zmax <<
" " << Nz << endl;
561 for (Int_t ix = 0; ix < Nx; ix++) {
564 for (Int_t iy = 0; iy < Ny; iy++) {
567 for (Int_t iz = 0; iz < Nz; iz++) {
568 index = ix * Ny * Nz + iy * Nz + iz;
571 by = (fMap->
GetBy(x, y, z) / factor);
572 for (
auto it = vSmoo.begin(); it != vSmoo.end(); it++) {
574 TLorentzVector
v = *it;
576 if (
v.X() != x ||
v.Y() != y ||
v.Z() != z)
585 bx = (Abs(fMap->
GetBx(x, y, z)) / factor > 10e-8) ? (fMap->
GetBx(x, y, z) / factor) : 0.0;
586 bz = (Abs(fMap->
GetBz(x, y, z)) / factor > 10e-8) ? (fMap->
GetBz(x, y, z) / factor) : 0.0;
588 mapFile << bx <<
" " << by <<
" " << bz << endl;
590 if (index % 1000 == 0)
591 cout <<
"nNodes processed# " << index << endl;
598Double_t BmnSP41FieldMapCreator::FieldExtrapolate(Double_t x, Double_t y, Double_t z, TGraph2D* c, TGraph2D*
m, TGraph2D* s) {
599 vector <Double_t> varToBePassed;
601 if (fConditions->
direction.Contains(
"X")) {
602 varToBePassed.push_back(y);
603 varToBePassed.push_back(z);
604 varToBePassed.push_back(x);
605 }
else if (fConditions->
direction.Contains(
"Z")) {
606 varToBePassed.push_back(x);
607 varToBePassed.push_back(y);
608 varToBePassed.push_back(z);
611 Double_t Mean =
m->Interpolate(varToBePassed[0], varToBePassed[1]);
612 Double_t
Sigma =
s->Interpolate(varToBePassed[0], varToBePassed[1]);
614 return c->Interpolate(varToBePassed[0], varToBePassed[1]) * TMath::Exp(-0.5 * (varToBePassed[2] - Mean) * (varToBePassed[2] - Mean) /
Sigma /
Sigma);
617void BmnSP41FieldMapCreator::Print(
BmnFieldMap* magField) {
618 cout <<
"Nx = " << magField->
GetNx();
619 cout <<
"; Ny = " << magField->
GetNy();
620 cout <<
"; Nz = " << magField->
GetNz();
622 Double_t shift = 1e-6;
624 cout <<
"; Xmin = " << magField->
GetXmin() - shift;
625 cout <<
"; dx = " << magField->
GetXstep();
626 cout <<
"; Ymin = " << magField->
GetYmin() - shift;
627 cout <<
"; dy = " << magField->
GetYstep();
628 cout <<
"; Zmin = " << magField->
GetZmin() - shift;
629 cout <<
"; dz = " << magField->
GetZstep() << endl;
Double_t Sigma(vector< Double_t > dist, vector< Double_t > w)
friend F32vec4 min(const F32vec4 &a, const F32vec4 &b)
friend F32vec4 max(const F32vec4 &a, const F32vec4 &b)
virtual Double_t GetBz(Double_t x, Double_t y, Double_t z)=0
Double_t GetYstep() const
Double_t GetZstep() const
Double_t GetXstep() const
virtual Double_t GetBy(Double_t x, Double_t y, Double_t z)=0
void SetScale(Double_t factor)
virtual Double_t GetBx(Double_t x, Double_t y, Double_t z)=0
void SmoothMap(TClonesArray *)
virtual ~BmnSP41FieldMapCreator()
void CreateExtrapolatedMap()