1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
16 #include "AliMagWrapCheb.h"
21 ClassImp(AliMagWrapCheb)
23 //__________________________________________________________________________________________
24 AliMagWrapCheb::AliMagWrapCheb() :
25 fNParamsSol(0),fNZSegSol(0),fNPSegSol(0),fNRSegSol(0),
26 fSegZSol(0),fSegPSol(0),fSegRSol(0),
27 fBegSegPSol(0),fNSegPSol(0),fBegSegRSol(0),fNSegRSol(0),fSegIDSol(0),fMinZSol(1.e6),fMaxZSol(-1.e6),fParamsSol(0),fMaxRSol(0),
29 fNParamsTPC(0),fNZSegTPC(0),fNPSegTPC(0),fNRSegTPC(0),
30 fSegZTPC(0),fSegPTPC(0),fSegRTPC(0),
31 fBegSegPTPC(0),fNSegPTPC(0),fBegSegRTPC(0),fNSegRTPC(0),fSegIDTPC(0),fMinZTPC(1.e6),fMaxZTPC(-1.e6),fParamsTPC(0),fMaxRTPC(0),
33 fNParamsDip(0),fNZSegDip(0),fNYSegDip(0),fNXSegDip(0),
34 fSegZDip(0),fSegYDip(0),fSegXDip(0),
35 fBegSegYDip(0),fNSegYDip(0),fBegSegXDip(0),fNSegXDip(0),fSegIDDip(0),fMinZDip(1.e6),fMaxZDip(-1.e6),fParamsDip(0)
38 // default constructor
41 //__________________________________________________________________________________________
42 AliMagWrapCheb::AliMagWrapCheb(const AliMagWrapCheb& src) :
44 fNParamsSol(0),fNZSegSol(0),fNPSegSol(0),fNRSegSol(0),
45 fSegZSol(0),fSegPSol(0),fSegRSol(0),
46 fBegSegPSol(0),fNSegPSol(0),fBegSegRSol(0),fNSegRSol(0),fSegIDSol(0),fMinZSol(1.e6),fMaxZSol(-1.e6),fParamsSol(0),fMaxRSol(0),
48 fNParamsTPC(0),fNZSegTPC(0),fNPSegTPC(0),fNRSegTPC(0),
49 fSegZTPC(0),fSegPTPC(0),fSegRTPC(0),
50 fBegSegPTPC(0),fNSegPTPC(0),fBegSegRTPC(0),fNSegRTPC(0),fSegIDTPC(0),fMinZTPC(1.e6),fMaxZTPC(-1.e6),fParamsTPC(0),fMaxRTPC(0),
52 fNParamsDip(0),fNZSegDip(0),fNYSegDip(0),fNXSegDip(0),
53 fSegZDip(0),fSegYDip(0),fSegXDip(0),
54 fBegSegYDip(0),fNSegYDip(0),fBegSegXDip(0),fNSegXDip(0),fSegIDDip(0),fMinZDip(1.e6),fMaxZDip(-1.e6),fParamsDip(0)
60 //__________________________________________________________________________________________
61 void AliMagWrapCheb::CopyFrom(const AliMagWrapCheb& src)
65 SetName(src.GetName());
66 SetTitle(src.GetTitle());
68 fNParamsSol = src.fNParamsSol;
69 fNZSegSol = src.fNZSegSol;
70 fNPSegSol = src.fNPSegSol;
71 fNRSegSol = src.fNRSegSol;
72 fMinZSol = src.fMinZSol;
73 fMaxZSol = src.fMaxZSol;
74 fMaxRSol = src.fMaxRSol;
75 if (src.fNParamsSol) {
76 memcpy(fSegZSol = new Float_t[fNZSegSol], src.fSegZSol, sizeof(Float_t)*fNZSegSol);
77 memcpy(fSegPSol = new Float_t[fNPSegSol], src.fSegPSol, sizeof(Float_t)*fNPSegSol);
78 memcpy(fSegRSol = new Float_t[fNRSegSol], src.fSegRSol, sizeof(Float_t)*fNRSegSol);
79 memcpy(fBegSegPSol= new Int_t[fNZSegSol], src.fBegSegPSol, sizeof(Int_t)*fNZSegSol);
80 memcpy(fNSegPSol = new Int_t[fNZSegSol], src.fNSegPSol, sizeof(Int_t)*fNZSegSol);
81 memcpy(fBegSegRSol= new Int_t[fNPSegSol], src.fBegSegRSol, sizeof(Int_t)*fNPSegSol);
82 memcpy(fNSegRSol = new Int_t[fNPSegSol], src.fNSegRSol, sizeof(Int_t)*fNPSegSol);
83 memcpy(fSegIDSol = new Int_t[fNRSegSol], src.fSegIDSol, sizeof(Int_t)*fNRSegSol);
84 fParamsSol = new TObjArray(fNParamsSol);
85 for (int i=0;i<fNParamsSol;i++) fParamsSol->AddAtAndExpand(new AliCheb3D(*src.GetParamSol(i)),i);
88 fNParamsTPC = src.fNParamsTPC;
89 fNZSegTPC = src.fNZSegTPC;
90 fNPSegTPC = src.fNPSegTPC;
91 fNRSegTPC = src.fNRSegTPC;
92 fMinZTPC = src.fMinZTPC;
93 fMaxZTPC = src.fMaxZTPC;
94 fMaxRTPC = src.fMaxRTPC;
95 if (src.fNParamsTPC) {
96 memcpy(fSegZTPC = new Float_t[fNZSegTPC], src.fSegZTPC, sizeof(Float_t)*fNZSegTPC);
97 memcpy(fSegPTPC = new Float_t[fNPSegTPC], src.fSegPTPC, sizeof(Float_t)*fNPSegTPC);
98 memcpy(fSegRTPC = new Float_t[fNRSegTPC], src.fSegRTPC, sizeof(Float_t)*fNRSegTPC);
99 memcpy(fBegSegPTPC= new Int_t[fNZSegTPC], src.fBegSegPTPC, sizeof(Int_t)*fNZSegTPC);
100 memcpy(fNSegPTPC = new Int_t[fNZSegTPC], src.fNSegPTPC, sizeof(Int_t)*fNZSegTPC);
101 memcpy(fBegSegRTPC= new Int_t[fNPSegTPC], src.fBegSegRTPC, sizeof(Int_t)*fNPSegTPC);
102 memcpy(fNSegRTPC = new Int_t[fNPSegTPC], src.fNSegRTPC, sizeof(Int_t)*fNPSegTPC);
103 memcpy(fSegIDTPC = new Int_t[fNRSegTPC], src.fSegIDTPC, sizeof(Int_t)*fNRSegTPC);
104 fParamsTPC = new TObjArray(fNParamsTPC);
105 for (int i=0;i<fNParamsTPC;i++) fParamsTPC->AddAtAndExpand(new AliCheb3D(*src.GetParamTPCInt(i)),i);
108 fNParamsDip = src.fNParamsDip;
109 fNZSegDip = src.fNZSegDip;
110 fNYSegDip = src.fNYSegDip;
111 fNXSegDip = src.fNXSegDip;
112 fMinZDip = src.fMinZDip;
113 fMaxZDip = src.fMaxZDip;
114 if (src.fNParamsDip) {
115 memcpy(fSegZDip = new Float_t[fNZSegDip], src.fSegZDip, sizeof(Float_t)*fNZSegDip);
116 memcpy(fSegYDip = new Float_t[fNYSegDip], src.fSegYDip, sizeof(Float_t)*fNYSegDip);
117 memcpy(fSegXDip = new Float_t[fNXSegDip], src.fSegXDip, sizeof(Float_t)*fNXSegDip);
118 memcpy(fBegSegYDip= new Int_t[fNZSegDip], src.fBegSegYDip, sizeof(Int_t)*fNZSegDip);
119 memcpy(fNSegYDip = new Int_t[fNZSegDip], src.fNSegYDip, sizeof(Int_t)*fNZSegDip);
120 memcpy(fBegSegXDip= new Int_t[fNYSegDip], src.fBegSegXDip, sizeof(Int_t)*fNYSegDip);
121 memcpy(fNSegXDip = new Int_t[fNYSegDip], src.fNSegXDip, sizeof(Int_t)*fNYSegDip);
122 memcpy(fSegIDDip = new Int_t[fNXSegDip], src.fSegIDDip, sizeof(Int_t)*fNXSegDip);
123 fParamsDip = new TObjArray(fNParamsDip);
124 for (int i=0;i<fNParamsDip;i++) fParamsDip->AddAtAndExpand(new AliCheb3D(*src.GetParamDip(i)),i);
129 //__________________________________________________________________________________________
130 AliMagWrapCheb& AliMagWrapCheb::operator=(const AliMagWrapCheb& rhs)
141 //__________________________________________________________________________________________
142 void AliMagWrapCheb::Clear(const Option_t *)
144 // clear all dynamic parts
146 delete fParamsSol; fParamsSol = 0;
147 delete[] fSegZSol; fSegZSol = 0;
148 delete[] fSegPSol; fSegPSol = 0;
149 delete[] fSegRSol; fSegRSol = 0;
150 delete[] fBegSegPSol; fBegSegPSol = 0;
151 delete[] fNSegPSol; fNSegPSol = 0;
152 delete[] fBegSegRSol; fBegSegRSol = 0;
153 delete[] fNSegRSol; fNSegRSol = 0;
154 delete[] fSegIDSol; fSegIDSol = 0;
156 fNParamsSol = fNZSegSol = fNPSegSol = fNRSegSol = 0;
162 delete fParamsTPC; fParamsTPC = 0;
163 delete[] fSegZTPC; fSegZTPC = 0;
164 delete[] fSegPTPC; fSegPTPC = 0;
165 delete[] fSegRTPC; fSegRTPC = 0;
166 delete[] fBegSegPTPC; fBegSegPTPC = 0;
167 delete[] fNSegPTPC; fNSegPTPC = 0;
168 delete[] fBegSegRTPC; fBegSegRTPC = 0;
169 delete[] fNSegRTPC; fNSegRTPC = 0;
170 delete[] fSegIDTPC; fSegIDTPC = 0;
172 fNParamsTPC = fNZSegTPC = fNPSegTPC = fNRSegTPC = 0;
178 delete fParamsDip; fParamsDip = 0;
179 delete[] fSegZDip; fSegZDip = 0;
180 delete[] fSegYDip; fSegYDip = 0;
181 delete[] fSegXDip; fSegXDip = 0;
182 delete[] fBegSegYDip; fBegSegYDip = 0;
183 delete[] fNSegYDip; fNSegYDip = 0;
184 delete[] fBegSegXDip; fBegSegXDip = 0;
185 delete[] fNSegXDip; fNSegXDip = 0;
186 delete[] fSegIDDip; fSegIDDip = 0;
188 fNParamsDip = fNZSegDip = fNYSegDip = fNXSegDip = 0;
194 //__________________________________________________________________________________________
195 void AliMagWrapCheb::Field(const Double_t *xyz, Double_t *b) const
197 // compute field in cartesian coordinates. If point is outside of the parameterized region
198 // get it at closest valid point
201 #ifndef _BRING_TO_BOUNDARY_ // exact matching to fitted volume is requested
202 b[0] = b[1] = b[2] = 0;
205 if (xyz[2]>fMinZSol) {
206 CartToCyl(xyz,rphiz);
207 FieldCylSol(rphiz,b);
208 // convert field to cartesian system
209 CylToCartCylB(rphiz, b,b);
213 int iddip = FindDipSegment(xyz);
215 AliCheb3D* par = GetParamDip(iddip);
216 #ifndef _BRING_TO_BOUNDARY_
217 if (!par->IsInside(xyz)) return;
223 //__________________________________________________________________________________________
224 Double_t AliMagWrapCheb::GetBz(const Double_t *xyz) const
226 // compute Bz for the point in cartesian coordinates. If point is outside of the parameterized region
227 // get it at closest valid point
230 if (xyz[2]>fMinZSol) {
231 CartToCyl(xyz,rphiz);
232 return FieldCylSolBz(rphiz);
235 int iddip = FindDipSegment(xyz);
236 if (iddip<0) return 0.;
237 AliCheb3D* par = GetParamDip(iddip);
238 #ifndef _BRING_TO_BOUNDARY_
239 if (!par->IsInside(xyz)) return 0.;
241 return par->Eval(xyz,2);
245 //__________________________________________________________________________________________
246 void AliMagWrapCheb::Print(Option_t *) const
249 printf("Alice magnetic field parameterized by Chebyshev polynomials\n");
250 printf("Segmentation for Solenoid (%+.2f<Z<%+.2f cm | R<%.2f cm)\n",fMinZSol,fMaxZSol,fMaxRSol);
253 for (int i=0;i<fNParamsSol;i++) {
255 GetParamSol(i)->Print();
259 printf("Segmentation for TPC field integral (%+.2f<Z<%+.2f cm | R<%.2f cm)\n",fMinZTPC,fMaxZTPC,fMaxRTPC);
262 for (int i=0;i<fNParamsTPC;i++) {
264 GetParamTPCInt(i)->Print();
268 printf("Segmentation for Dipole (%+.2f<Z<%+.2f cm)\n",fMinZDip,fMaxZDip);
270 for (int i=0;i<fNParamsDip;i++) {
272 GetParamDip(i)->Print();
278 //__________________________________________________________________________________________________
279 Int_t AliMagWrapCheb::FindDipSegment(const Double_t *xyz) const
281 // find the segment containing point xyz. If it is outside find the closest segment
282 if (!fNParamsDip) return -1;
283 int xid,yid,zid = TMath::BinarySearch(fNZSegDip,fSegZDip,(Float_t)xyz[2]); // find zsegment
285 Bool_t reCheck = kFALSE;
287 int ysegBeg = fBegSegYDip[zid];
289 for (yid=0;yid<fNSegYDip[zid];yid++) if (xyz[1]<fSegYDip[ysegBeg+yid]) break;
290 if ( --yid < 0 ) yid = 0;
293 int xsegBeg = fBegSegXDip[yid];
294 for (xid=0;xid<fNSegXDip[yid];xid++) if (xyz[0]<fSegXDip[xsegBeg+xid]) break;
296 if ( --xid < 0) xid = 0;
299 // to make sure that due to the precision problems we did not pick the next Zbin
300 if (!reCheck && (xyz[2] - fSegZDip[zid] < 3.e-5) && zid &&
301 !GetParamDip(fSegIDDip[xid])->IsInside(xyz)) { // check the previous Z bin
308 return fSegIDDip[xid];
311 //__________________________________________________________________________________________________
312 Int_t AliMagWrapCheb::FindSolSegment(const Double_t *rpz) const
314 // find the segment containing point xyz. If it is outside find the closest segment
315 if (!fNParamsSol) return -1;
316 int rid,pid,zid = TMath::BinarySearch(fNZSegSol,fSegZSol,(Float_t)rpz[2]); // find zsegment
318 Bool_t reCheck = kFALSE;
320 int psegBeg = fBegSegPSol[zid];
321 for (pid=0;pid<fNSegPSol[zid];pid++) if (rpz[1]<fSegPSol[psegBeg+pid]) break;
322 if ( --pid < 0 ) pid = 0;
325 int rsegBeg = fBegSegRSol[pid];
326 for (rid=0;rid<fNSegRSol[pid];rid++) if (rpz[0]<fSegRSol[rsegBeg+rid]) break;
327 if ( --rid < 0) rid = 0;
330 // to make sure that due to the precision problems we did not pick the next Zbin
331 if (!reCheck && (rpz[2] - fSegZSol[zid] < 3.e-5) && zid &&
332 !GetParamSol(fSegIDSol[rid])->IsInside(rpz)) { // check the previous Z bin
339 return fSegIDSol[rid];
342 //__________________________________________________________________________________________________
343 Int_t AliMagWrapCheb::FindTPCSegment(const Double_t *rpz) const
345 // find the segment containing point xyz. If it is outside find the closest segment
346 if (!fNParamsTPC) return -1;
347 int rid,pid,zid = TMath::BinarySearch(fNZSegTPC,fSegZTPC,(Float_t)rpz[2]); // find zsegment
349 Bool_t reCheck = kFALSE;
351 int psegBeg = fBegSegPTPC[zid];
353 for (pid=0;pid<fNSegPTPC[zid];pid++) if (rpz[1]<fSegPTPC[psegBeg+pid]) break;
354 if ( --pid < 0 ) pid = 0;
357 int rsegBeg = fBegSegRTPC[pid];
358 for (rid=0;rid<fNSegRTPC[pid];rid++) if (rpz[0]<fSegRTPC[rsegBeg+rid]) break;
359 if ( --rid < 0) rid = 0;
362 // to make sure that due to the precision problems we did not pick the next Zbin
363 if (!reCheck && (rpz[2] - fSegZSol[zid] < 3.e-5) && zid &&
364 !GetParamSol(fSegIDSol[rid])->IsInside(rpz)) { // check the previous Z bin
371 return fSegIDTPC[rid];
375 //__________________________________________________________________________________________
376 void AliMagWrapCheb::GetTPCInt(const Double_t *xyz, Double_t *b) const
378 // compute TPC region field integral in cartesian coordinates.
379 // If point is outside of the parameterized region get it at closeset valid point
380 static Double_t rphiz[3];
383 // convert coordinates to cyl system
384 CartToCyl(xyz,rphiz);
385 #ifndef _BRING_TO_BOUNDARY_
386 if ( (rphiz[2]>GetMaxZTPCInt()||rphiz[2]<GetMinZTPCInt()) ||
387 rphiz[0]>GetMaxRTPCInt()) {for (int i=3;i--;) b[i]=0; return;}
390 GetTPCIntCyl(rphiz,b);
392 // convert field to cartesian system
393 CylToCartCylB(rphiz, b,b);
397 //__________________________________________________________________________________________
398 void AliMagWrapCheb::FieldCylSol(const Double_t *rphiz, Double_t *b) const
400 // compute Solenoid field in Cylindircal coordinates
401 // note: if the point is outside the volume get the field in closest parameterized point
402 int id = FindSolSegment(rphiz);
404 AliCheb3D* par = GetParamSol(id);
405 #ifndef _BRING_TO_BOUNDARY_ // exact matching to fitted volume is requested
406 if (!par->IsInside(rphiz)) return;
413 //__________________________________________________________________________________________
414 Double_t AliMagWrapCheb::FieldCylSolBz(const Double_t *rphiz) const
416 // compute Solenoid field in Cylindircal coordinates
417 // note: if the point is outside the volume get the field in closest parameterized point
418 int id = FindSolSegment(rphiz);
420 AliCheb3D* par = GetParamSol(id);
421 #ifndef _BRING_TO_BOUNDARY_
422 return par->IsInside(rphiz) ? par->Eval(rphiz,2) : 0;
424 return par->Eval(rphiz,2);
428 //__________________________________________________________________________________________
429 void AliMagWrapCheb::GetTPCIntCyl(const Double_t *rphiz, Double_t *b) const
431 // compute field integral in TPC region in Cylindircal coordinates
432 // note: the check for the point being inside the parameterized region is done outside
433 int id = FindTPCSegment(rphiz);
435 b[0] = b[1] = b[2] = 0;
438 AliCheb3D* par = GetParamTPCInt(id);
439 if (par->IsInside(rphiz)) {par->Eval(rphiz,b); return;}
440 b[0] = b[1] = b[2] = 0;
446 #ifdef _INC_CREATION_ALICHEB3D_
447 //_______________________________________________
448 void AliMagWrapCheb::LoadData(const char* inpfile)
450 // read coefficients data from the text file
452 TString strf = inpfile;
453 gSystem->ExpandPathName(strf);
454 FILE* stream = fopen(strf,"r");
456 printf("Did not find input file %s\n",strf.Data());
461 AliCheb3DCalc::ReadLine(buffs,stream);
462 if (!buffs.BeginsWith("START")) {
463 Error("LoadData","Expected: \"START <name>\", found \"%s\"\nStop\n",buffs.Data());
466 if (buffs.First(' ')>0) SetName(buffs.Data()+buffs.First(' ')+1);
468 // Solenoid part -----------------------------------------------------------
469 AliCheb3DCalc::ReadLine(buffs,stream);
470 if (!buffs.BeginsWith("START SOLENOID")) {
471 Error("LoadData","Expected: \"START SOLENOID\", found \"%s\"\nStop\n",buffs.Data());
474 AliCheb3DCalc::ReadLine(buffs,stream); // nparam
475 int nparSol = buffs.Atoi();
477 for (int ip=0;ip<nparSol;ip++) {
478 AliCheb3D* cheb = new AliCheb3D();
479 cheb->LoadData(stream);
483 AliCheb3DCalc::ReadLine(buffs,stream);
484 if (!buffs.BeginsWith("END SOLENOID")) {
485 Error("LoadData","Expected \"END SOLENOID\", found \"%s\"\nStop\n",buffs.Data());
489 // TPCInt part -----------------------------------------------------------
490 AliCheb3DCalc::ReadLine(buffs,stream);
491 if (!buffs.BeginsWith("START TPCINT")) {
492 Error("LoadData","Expected: \"START TPCINT\", found \"%s\"\nStop\n",buffs.Data());
495 AliCheb3DCalc::ReadLine(buffs,stream); // nparam
496 int nparTPCInt = buffs.Atoi();
498 for (int ip=0;ip<nparTPCInt;ip++) {
499 AliCheb3D* cheb = new AliCheb3D();
500 cheb->LoadData(stream);
501 AddParamTPCInt(cheb);
504 AliCheb3DCalc::ReadLine(buffs,stream);
505 if (!buffs.BeginsWith("END TPCINT")) {
506 Error("LoadData","Expected \"END TPCINT\", found \"%s\"\nStop\n",buffs.Data());
510 // Dipole part -----------------------------------------------------------
511 AliCheb3DCalc::ReadLine(buffs,stream);
512 if (!buffs.BeginsWith("START DIPOLE")) {
513 Error("LoadData","Expected: \"START DIPOLE\", found \"%s\"\nStop\n",buffs.Data());
516 AliCheb3DCalc::ReadLine(buffs,stream); // nparam
517 int nparDip = buffs.Atoi();
519 for (int ip=0;ip<nparDip;ip++) {
520 AliCheb3D* cheb = new AliCheb3D();
521 cheb->LoadData(stream);
525 AliCheb3DCalc::ReadLine(buffs,stream);
526 if (!buffs.BeginsWith("END DIPOLE")) {
527 Error("LoadData","Expected \"END DIPOLE\", found \"%s\"\nStop\n",GetName(),buffs.Data());
531 AliCheb3DCalc::ReadLine(buffs,stream);
532 if (!buffs.BeginsWith("END") || !buffs.Contains(GetName())) {
533 Error("LoadData","Expected: \"END %s\", found \"%s\"\nStop\n",GetName(),buffs.Data());
537 // ---------------------------------------------------------------------------
543 printf("Loaded magnetic field \"%s\" from %s\n",GetName(),strf.Data());
547 //__________________________________________________________________________________________
548 void AliMagWrapCheb::BuildTableSol()
550 BuildTable(fNParamsSol,fParamsSol,
551 fNZSegSol,fNPSegSol,fNRSegSol,
553 &fSegZSol,&fSegPSol,&fSegRSol,
554 &fBegSegPSol,&fNSegPSol,
555 &fBegSegRSol,&fNSegRSol,
559 //__________________________________________________________________________________________
560 void AliMagWrapCheb::BuildTableDip()
562 BuildTable(fNParamsDip,fParamsDip,
563 fNZSegDip,fNYSegDip,fNXSegDip,
565 &fSegZDip,&fSegYDip,&fSegXDip,
566 &fBegSegYDip,&fNSegYDip,
567 &fBegSegXDip,&fNSegXDip,
571 //__________________________________________________________________________________________
572 void AliMagWrapCheb::BuildTableTPCInt()
574 BuildTable(fNParamsTPC,fParamsTPC,
575 fNZSegTPC,fNPSegTPC,fNRSegTPC,
577 &fSegZTPC,&fSegPTPC,&fSegRTPC,
578 &fBegSegPTPC,&fNSegPTPC,
579 &fBegSegRTPC,&fNSegRTPC,
585 //_______________________________________________
586 #ifdef _INC_CREATION_ALICHEB3D_
588 //__________________________________________________________________________________________
589 AliMagWrapCheb::AliMagWrapCheb(const char* inputFile) :
590 fNParamsSol(0),fNZSegSol(0),fNPSegSol(0),fNRSegSol(0),
591 fSegZSol(0),fSegPSol(0),fSegRSol(0),
592 fBegSegPSol(0),fNSegPSol(0),fBegSegRSol(0),fNSegRSol(0),fSegIDSol(0),fMinZSol(1.e6),fMaxZSol(-1.e6),fParamsSol(0),fMaxRSol(0),
594 fNParamsTPC(0),fNZSegTPC(0),fNPSegTPC(0),fNRSegTPC(0),
595 fSegZTPC(0),fSegPTPC(0),fSegRTPC(0),
596 fBegSegPTPC(0),fNSegPTPC(0),fBegSegRTPC(0),fNSegRTPC(0),fSegIDTPC(0),fMinZTPC(1.e6),fMaxZTPC(-1.e6),fParamsTPC(0),fMaxRTPC(0),
598 fNParamsDip(0),fNZSegDip(0),fNYSegDip(0),fNXSegDip(0),
599 fSegZDip(0),fSegYDip(0),fSegXDip(0),
600 fBegSegYDip(0),fNSegYDip(0),fBegSegXDip(0),fNSegXDip(0),fSegIDDip(0),fMinZDip(1.e6),fMaxZDip(-1.e6),fParamsDip(0)
603 // construct from coeffs from the text file
607 //__________________________________________________________________________________________
608 void AliMagWrapCheb::AddParamSol(const AliCheb3D* param)
610 // adds new parameterization piece for Sol
611 // NOTE: pieces must be added strictly in increasing R then increasing Z order
613 if (!fParamsSol) fParamsSol = new TObjArray();
614 fParamsSol->Add( (AliCheb3D*)param );
616 if (fMaxRSol<param->GetBoundMax(0)) fMaxRSol = param->GetBoundMax(0);
620 //__________________________________________________________________________________________
621 void AliMagWrapCheb::AddParamTPCInt(const AliCheb3D* param)
623 // adds new parameterization piece for TPCInt
624 // NOTE: pieces must be added strictly in increasing R then increasing Z order
626 if (!fParamsTPC) fParamsTPC = new TObjArray();
627 fParamsTPC->Add( (AliCheb3D*)param);
629 if (fMaxRTPC<param->GetBoundMax(0)) fMaxRTPC = param->GetBoundMax(0);
633 //__________________________________________________________________________________________
634 void AliMagWrapCheb::AddParamDip(const AliCheb3D* param)
636 // adds new parameterization piece for Dipole
638 if (!fParamsDip) fParamsDip = new TObjArray();
639 fParamsDip->Add( (AliCheb3D*)param);
644 //__________________________________________________________________________________________
645 void AliMagWrapCheb::ResetTPCInt()
647 // clean TPC field integral (used for update)
649 delete fParamsTPC; fParamsTPC = 0;
650 delete[] fSegZTPC; fSegZTPC = 0;
651 delete[] fSegPTPC; fSegPTPC = 0;
652 delete[] fSegRTPC; fSegRTPC = 0;
653 delete[] fBegSegPTPC; fBegSegPTPC = 0;
654 delete[] fNSegPTPC; fNSegPTPC = 0;
655 delete[] fBegSegRTPC; fBegSegRTPC = 0;
656 delete[] fNSegRTPC; fNSegRTPC = 0;
657 delete[] fSegIDTPC; fSegIDTPC = 0;
659 fNParamsTPC = fNZSegTPC = fNPSegTPC = fNRSegTPC = 0;
667 //__________________________________________________
668 void AliMagWrapCheb::BuildTable(Int_t npar,TObjArray *parArr, Int_t &nZSeg, Int_t &nYSeg, Int_t &nXSeg,
669 Float_t &minZ,Float_t &maxZ,
670 Float_t **segZ,Float_t **segY,Float_t **segX,
671 Int_t **begSegY,Int_t **nSegY,
672 Int_t **begSegX,Int_t **nSegX,
675 // build lookup table for dipole
678 TArrayF segYArr,segXArr;
679 TArrayI begSegYDipArr,begSegXDipArr;
680 TArrayI nSegYDipArr,nSegXDipArr;
682 float *tmpSegZ,*tmpSegY,*tmpSegX;
684 // create segmentation in Z
685 nZSeg = SegmentDimension(&tmpSegZ, parArr, npar, 2, 1,-1, 1,-1, 1,-1) - 1;
689 // for each Z slice create segmentation in Y
690 begSegYDipArr.Set(nZSeg);
691 nSegYDipArr.Set(nZSeg);
693 for (int iz=0;iz<nZSeg;iz++) {
694 printf("\nZSegment#%d %+e : %+e\n",iz,tmpSegZ[iz],tmpSegZ[iz+1]);
695 int ny = SegmentDimension(&tmpSegY, parArr, npar, 1,
696 1,-1, 1,-1, tmpSegZ[iz],tmpSegZ[iz+1]) - 1;
697 segYArr.Set(ny + nYSeg);
698 for (int iy=0;iy<ny;iy++) segYArr[nYSeg+iy] = tmpSegY[iy];
699 begSegYDipArr[iz] = nYSeg;
700 nSegYDipArr[iz] = ny;
701 printf(" Found %d YSegments, to start from %d\n",ny, begSegYDipArr[iz]);
703 // for each slice in Z and Y create segmentation in X
704 begSegXDipArr.Set(nYSeg+ny);
705 nSegXDipArr.Set(nYSeg+ny);
706 xyz[2] = (tmpSegZ[iz]+tmpSegZ[iz+1])/2.; // mean Z of this segment
708 for (int iy=0;iy<ny;iy++) {
710 printf("\n YSegment#%d %+e : %+e\n",iy, tmpSegY[iy],tmpSegY[iy+1]);
711 int nx = SegmentDimension(&tmpSegX, parArr, npar, 0,
712 1,-1, tmpSegY[iy],tmpSegY[iy+1], tmpSegZ[iz],tmpSegZ[iz+1]) - 1;
714 segXArr.Set(nx + nXSeg);
715 for (int ix=0;ix<nx;ix++) segXArr[nXSeg+ix] = tmpSegX[ix];
716 begSegXDipArr[isg] = nXSeg;
717 nSegXDipArr[isg] = nx;
718 printf(" Found %d XSegments, to start from %d\n",nx, begSegXDipArr[isg]);
720 segIDArr.Set(nXSeg+nx);
722 // find corresponding params
723 xyz[1] = (tmpSegY[iy]+tmpSegY[iy+1])/2.; // mean Y of this segment
725 for (int ix=0;ix<nx;ix++) {
726 xyz[0] = (tmpSegX[ix]+tmpSegX[ix+1])/2.; // mean X of this segment
727 for (int ipar=0;ipar<npar;ipar++) {
728 AliCheb3D* cheb = (AliCheb3D*) parArr->At(ipar);
729 if (!cheb->IsInside(xyz)) continue;
730 segIDArr[nXSeg+ix] = ipar;
743 maxZ = tmpSegZ[nZSeg];
744 (*segZ) = new Float_t[nZSeg];
745 for (int i=nZSeg;i--;) (*segZ)[i] = tmpSegZ[i];
748 (*segY) = new Float_t[nYSeg];
749 (*segX) = new Float_t[nXSeg];
750 (*begSegY) = new Int_t[nZSeg];
751 (*nSegY) = new Int_t[nZSeg];
752 (*begSegX) = new Int_t[nYSeg];
753 (*nSegX) = new Int_t[nYSeg];
754 (*segID) = new Int_t[nXSeg];
756 for (int i=nYSeg;i--;) (*segY)[i] = segYArr[i];
757 for (int i=nXSeg;i--;) (*segX)[i] = segXArr[i];
758 for (int i=nZSeg;i--;) {(*begSegY)[i] = begSegYDipArr[i]; (*nSegY)[i] = nSegYDipArr[i];}
759 for (int i=nYSeg;i--;) {(*begSegX)[i] = begSegXDipArr[i]; (*nSegX)[i] = nSegXDipArr[i];}
760 for (int i=nXSeg;i--;) {(*segID)[i] = segIDArr[i];}
765 //__________________________________________________
766 void AliMagWrapCheb::BuildTableDip()
768 // build lookup table for dipole
770 if (fNParamsDip<1) return;
772 TArrayI begSegYDip,begSegXDip;
773 TArrayI nsegYDip,nsegXDip;
775 float *tmpSegZ,*tmpSegY,*tmpSegX;
777 // create segmentation in Z
778 fNZSegDip = SegmentDimension(&tmpSegZ, fParamsDip, fNParamsDip, 2, 1,-1, 1,-1, 1,-1) - 1;
782 // for each Z slice create segmentation in Y
783 begSegYDip.Set(fNZSegDip);
784 nsegYDip.Set(fNZSegDip);
786 for (int iz=0;iz<fNZSegDip;iz++) {
787 printf("\nZSegment#%d %+e : %+e\n",iz,tmpSegZ[iz],tmpSegZ[iz+1]);
788 int ny = SegmentDimension(&tmpSegY, fParamsDip, fNParamsDip, 1,
789 1,-1, 1,-1, tmpSegZ[iz],tmpSegZ[iz+1]) - 1;
790 segY.Set(ny + fNYSegDip);
791 for (int iy=0;iy<ny;iy++) segY[fNYSegDip+iy] = tmpSegY[iy];
792 begSegYDip[iz] = fNYSegDip;
794 printf(" Found %d YSegments, to start from %d\n",ny, begSegYDip[iz]);
796 // for each slice in Z and Y create segmentation in X
797 begSegXDip.Set(fNYSegDip+ny);
798 nsegXDip.Set(fNYSegDip+ny);
799 xyz[2] = (tmpSegZ[iz]+tmpSegZ[iz+1])/2.; // mean Z of this segment
801 for (int iy=0;iy<ny;iy++) {
802 int isg = fNYSegDip+iy;
803 printf("\n YSegment#%d %+e : %+e\n",iy, tmpSegY[iy],tmpSegY[iy+1]);
804 int nx = SegmentDimension(&tmpSegX, fParamsDip, fNParamsDip, 0,
805 1,-1, tmpSegY[iy],tmpSegY[iy+1], tmpSegZ[iz],tmpSegZ[iz+1]) - 1;
807 segX.Set(nx + fNXSegDip);
808 for (int ix=0;ix<nx;ix++) segX[fNXSegDip+ix] = tmpSegX[ix];
809 begSegXDip[isg] = fNXSegDip;
811 printf(" Found %d XSegments, to start from %d\n",nx, begSegXDip[isg]);
813 segID.Set(fNXSegDip+nx);
815 // find corresponding params
816 xyz[1] = (tmpSegY[iy]+tmpSegY[iy+1])/2.; // mean Y of this segment
818 for (int ix=0;ix<nx;ix++) {
819 xyz[0] = (tmpSegX[ix]+tmpSegX[ix+1])/2.; // mean X of this segment
820 for (int ipar=0;ipar<fNParamsDip;ipar++) {
821 AliCheb3D* cheb = (AliCheb3D*) fParamsDip->At(ipar);
822 if (!cheb->IsInside(xyz)) continue;
823 segID[fNXSegDip+ix] = ipar;
835 fMinZDip = tmpSegZ[0];
836 fMaxZDip = tmpSegZ[fNZSegDip];
837 fSegZDip = new Float_t[fNZSegDip];
838 for (int i=fNZSegDip;i--;) fSegZDip[i] = tmpSegZ[i];
841 fSegYDip = new Float_t[fNYSegDip];
842 fSegXDip = new Float_t[fNXSegDip];
843 fBegSegYDip = new Int_t[fNZSegDip];
844 fNSegYDip = new Int_t[fNZSegDip];
845 fBegSegXDip = new Int_t[fNYSegDip];
846 fNSegXDip = new Int_t[fNYSegDip];
847 fSegIDDip = new Int_t[fNXSegDip];
849 for (int i=fNYSegDip;i--;) fSegYDip[i] = segY[i];
850 for (int i=fNXSegDip;i--;) fSegXDip[i] = segX[i];
851 for (int i=fNZSegDip;i--;) {fBegSegYDip[i] = begSegYDip[i]; fNSegYDip[i] = nsegYDip[i];}
852 for (int i=fNYSegDip;i--;) {fBegSegXDip[i] = begSegXDip[i]; fNSegXDip[i] = nsegXDip[i];}
853 for (int i=fNXSegDip;i--;) {fSegIDDip[i] = segID[i];}
858 //________________________________________________________________
859 void AliMagWrapCheb::SaveData(const char* outfile) const
861 // writes coefficients data to output text file
862 TString strf = outfile;
863 gSystem->ExpandPathName(strf);
864 FILE* stream = fopen(strf,"w+");
866 // Sol part ---------------------------------------------------------
867 fprintf(stream,"# Set of Chebyshev parameterizations for ALICE magnetic field\nSTART %s\n",GetName());
868 fprintf(stream,"START SOLENOID\n#Number of pieces\n%d\n",fNParamsSol);
869 for (int ip=0;ip<fNParamsSol;ip++) GetParamSol(ip)->SaveData(stream);
870 fprintf(stream,"#\nEND SOLENOID\n");
872 // TPCInt part ---------------------------------------------------------
873 fprintf(stream,"# Set of Chebyshev parameterizations for ALICE magnetic field\nSTART %s\n",GetName());
874 fprintf(stream,"START TPCINT\n#Number of pieces\n%d\n",fNParamsTPC);
875 for (int ip=0;ip<fNParamsTPC;ip++) GetParamTPCInt(ip)->SaveData(stream);
876 fprintf(stream,"#\nEND TPCINT\n");
878 // Dip part ---------------------------------------------------------
879 fprintf(stream,"START DIPOLE\n#Number of pieces\n%d\n",fNParamsDip);
880 for (int ip=0;ip<fNParamsDip;ip++) GetParamDip(ip)->SaveData(stream);
881 fprintf(stream,"#\nEND DIPOLE\n");
883 fprintf(stream,"#\nEND %s\n",GetName());
889 Int_t AliMagWrapCheb::SegmentDimension(float** seg,const TObjArray* par,int npar, int dim,
890 float xmn,float xmx,float ymn,float ymx,float zmn,float zmx)
892 // find all boundaries in deimension dim for boxes in given region.
893 // if mn>mx for given projection the check is not done for it.
894 float *tmpC = new float[2*npar];
895 int *tmpInd = new int[2*npar];
897 for (int ip=0;ip<npar;ip++) {
898 AliCheb3D* cheb = (AliCheb3D*) par->At(ip);
899 if (xmn<xmx && (cheb->GetBoundMin(0)>(xmx+xmn)/2 || cheb->GetBoundMax(0)<(xmn+xmx)/2)) continue;
900 if (ymn<ymx && (cheb->GetBoundMin(1)>(ymx+ymn)/2 || cheb->GetBoundMax(1)<(ymn+ymx)/2)) continue;
901 if (zmn<zmx && (cheb->GetBoundMin(2)>(zmx+zmn)/2 || cheb->GetBoundMax(2)<(zmn+zmx)/2)) continue;
903 tmpC[nseg0++] = cheb->GetBoundMin(dim);
904 tmpC[nseg0++] = cheb->GetBoundMax(dim);
906 // range Dim's boundaries in increasing order
907 TMath::Sort(nseg0,tmpC,tmpInd,kFALSE);
908 // count number of really different Z's
911 for (int ip=0;ip<nseg0;ip++) {
912 if (TMath::Abs(cprev-tmpC[ tmpInd[ip] ])>1e-4) {
913 cprev = tmpC[ tmpInd[ip] ];
916 else tmpInd[ip] = -1; // supress redundant Z
919 *seg = new float[nseg]; // create final Z segmenations
921 for (int ip=0;ip<nseg0;ip++) if (tmpInd[ip]>=0) (*seg)[nseg++] = tmpC[ tmpInd[ip] ];