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
284 int ysegBeg = fBegSegYDip[zid];
286 for (yid=0;yid<fNSegYDip[zid];yid++) if (xyz[1]<fSegYDip[ysegBeg+yid]) break;
287 if ( --yid < 0 ) yid = 0;
290 int xsegBeg = fBegSegXDip[yid];
291 for (xid=0;xid<fNSegXDip[yid];xid++) if (xyz[0]<fSegXDip[xsegBeg+xid]) break;
292 if ( --xid < 0) xid = 0;
295 return fSegIDDip[xid];
298 //__________________________________________________________________________________________________
299 Int_t AliMagWrapCheb::FindSolSegment(const Double_t *rpz) const
301 // find the segment containing point xyz. If it is outside find the closest segment
302 if (!fNParamsSol) return -1;
303 int rid,pid,zid = TMath::BinarySearch(fNZSegSol,fSegZSol,(Float_t)rpz[2]); // find zsegment
304 int psegBeg = fBegSegPSol[zid];
306 for (pid=0;pid<fNSegPSol[zid];pid++) if (rpz[1]<fSegPSol[psegBeg+pid]) break;
307 if ( --pid < 0 ) pid = 0;
310 int rsegBeg = fBegSegRSol[pid];
311 for (rid=0;rid<fNSegRSol[pid];rid++) if (rpz[0]<fSegRSol[rsegBeg+rid]) break;
312 if ( --rid < 0) rid = 0;
315 return fSegIDSol[rid];
318 //__________________________________________________________________________________________________
319 Int_t AliMagWrapCheb::FindTPCSegment(const Double_t *rpz) const
321 // find the segment containing point xyz. If it is outside find the closest segment
322 if (!fNParamsTPC) return -1;
323 int rid,pid,zid = TMath::BinarySearch(fNZSegTPC,fSegZTPC,(Float_t)rpz[2]); // find zsegment
324 int psegBeg = fBegSegPTPC[zid];
326 for (pid=0;pid<fNSegPTPC[zid];pid++) if (rpz[1]<fSegPTPC[psegBeg+pid]) break;
327 if ( --pid < 0 ) pid = 0;
330 int rsegBeg = fBegSegRTPC[pid];
331 for (rid=0;rid<fNSegRTPC[pid];rid++) if (rpz[0]<fSegRTPC[rsegBeg+rid]) break;
332 if ( --rid < 0) rid = 0;
335 return fSegIDTPC[rid];
339 //__________________________________________________________________________________________
340 void AliMagWrapCheb::GetTPCInt(const Double_t *xyz, Double_t *b) const
342 // compute TPC region field integral in cartesian coordinates.
343 // If point is outside of the parameterized region get it at closeset valid point
344 static Double_t rphiz[3];
347 // convert coordinates to cyl system
348 CartToCyl(xyz,rphiz);
349 #ifndef _BRING_TO_BOUNDARY_
350 if ( (rphiz[2]>GetMaxZTPCInt()||rphiz[2]<GetMinZTPCInt()) ||
351 rphiz[0]>GetMaxRTPCInt()) {for (int i=3;i--;) b[i]=0; return;}
354 GetTPCIntCyl(rphiz,b);
356 // convert field to cartesian system
357 CylToCartCylB(rphiz, b,b);
361 //__________________________________________________________________________________________
362 void AliMagWrapCheb::FieldCylSol(const Double_t *rphiz, Double_t *b) const
364 // compute Solenoid field in Cylindircal coordinates
365 // note: if the point is outside the volume get the field in closest parameterized point
366 int id = FindSolSegment(rphiz);
368 AliCheb3D* par = GetParamSol(id);
369 #ifndef _BRING_TO_BOUNDARY_ // exact matching to fitted volume is requested
370 if (!par->IsInside(rphiz)) return;
377 //__________________________________________________________________________________________
378 Double_t AliMagWrapCheb::FieldCylSolBz(const Double_t *rphiz) const
380 // compute Solenoid field in Cylindircal coordinates
381 // note: if the point is outside the volume get the field in closest parameterized point
382 int id = FindSolSegment(rphiz);
384 AliCheb3D* par = GetParamSol(id);
385 #ifndef _BRING_TO_BOUNDARY_
386 return par->IsInside(rphiz) ? par->Eval(rphiz,2) : 0;
388 return par->Eval(rphiz,2);
392 //__________________________________________________________________________________________
393 void AliMagWrapCheb::GetTPCIntCyl(const Double_t *rphiz, Double_t *b) const
395 // compute field integral in TPC region in Cylindircal coordinates
396 // note: the check for the point being inside the parameterized region is done outside
397 int id = FindTPCSegment(rphiz);
399 b[0] = b[1] = b[2] = 0;
402 AliCheb3D* par = GetParamTPCInt(id);
403 if (par->IsInside(rphiz)) {par->Eval(rphiz,b); return;}
404 b[0] = b[1] = b[2] = 0;
410 #ifdef _INC_CREATION_ALICHEB3D_
411 //_______________________________________________
412 void AliMagWrapCheb::LoadData(const char* inpfile)
414 // read coefficients data from the text file
416 TString strf = inpfile;
417 gSystem->ExpandPathName(strf);
418 FILE* stream = fopen(strf,"r");
420 printf("Did not find input file %s\n",strf.Data());
425 AliCheb3DCalc::ReadLine(buffs,stream);
426 if (!buffs.BeginsWith("START")) {
427 Error("LoadData","Expected: \"START <name>\", found \"%s\"\nStop\n",buffs.Data());
430 if (buffs.First(' ')>0) SetName(buffs.Data()+buffs.First(' ')+1);
432 // Solenoid part -----------------------------------------------------------
433 AliCheb3DCalc::ReadLine(buffs,stream);
434 if (!buffs.BeginsWith("START SOLENOID")) {
435 Error("LoadData","Expected: \"START SOLENOID\", found \"%s\"\nStop\n",buffs.Data());
438 AliCheb3DCalc::ReadLine(buffs,stream); // nparam
439 int nparSol = buffs.Atoi();
441 for (int ip=0;ip<nparSol;ip++) {
442 AliCheb3D* cheb = new AliCheb3D();
443 cheb->LoadData(stream);
447 AliCheb3DCalc::ReadLine(buffs,stream);
448 if (!buffs.BeginsWith("END SOLENOID")) {
449 Error("LoadData","Expected \"END SOLENOID\", found \"%s\"\nStop\n",buffs.Data());
453 // TPCInt part -----------------------------------------------------------
454 AliCheb3DCalc::ReadLine(buffs,stream);
455 if (!buffs.BeginsWith("START TPCINT")) {
456 Error("LoadData","Expected: \"START TPCINT\", found \"%s\"\nStop\n",buffs.Data());
459 AliCheb3DCalc::ReadLine(buffs,stream); // nparam
460 int nparTPCInt = buffs.Atoi();
462 for (int ip=0;ip<nparTPCInt;ip++) {
463 AliCheb3D* cheb = new AliCheb3D();
464 cheb->LoadData(stream);
465 AddParamTPCInt(cheb);
468 AliCheb3DCalc::ReadLine(buffs,stream);
469 if (!buffs.BeginsWith("END TPCINT")) {
470 Error("LoadData","Expected \"END TPCINT\", found \"%s\"\nStop\n",buffs.Data());
474 // Dipole part -----------------------------------------------------------
475 AliCheb3DCalc::ReadLine(buffs,stream);
476 if (!buffs.BeginsWith("START DIPOLE")) {
477 Error("LoadData","Expected: \"START DIPOLE\", found \"%s\"\nStop\n",buffs.Data());
480 AliCheb3DCalc::ReadLine(buffs,stream); // nparam
481 int nparDip = buffs.Atoi();
483 for (int ip=0;ip<nparDip;ip++) {
484 AliCheb3D* cheb = new AliCheb3D();
485 cheb->LoadData(stream);
489 AliCheb3DCalc::ReadLine(buffs,stream);
490 if (!buffs.BeginsWith("END DIPOLE")) {
491 Error("LoadData","Expected \"END DIPOLE\", found \"%s\"\nStop\n",GetName(),buffs.Data());
495 AliCheb3DCalc::ReadLine(buffs,stream);
496 if (!buffs.BeginsWith("END") || !buffs.Contains(GetName())) {
497 Error("LoadData","Expected: \"END %s\", found \"%s\"\nStop\n",GetName(),buffs.Data());
501 // ---------------------------------------------------------------------------
507 printf("Loaded magnetic field \"%s\" from %s\n",GetName(),strf.Data());
511 //__________________________________________________________________________________________
512 void AliMagWrapCheb::BuildTableSol()
514 BuildTable(fNParamsSol,fParamsSol,
515 fNZSegSol,fNPSegSol,fNRSegSol,
517 &fSegZSol,&fSegPSol,&fSegRSol,
518 &fBegSegPSol,&fNSegPSol,
519 &fBegSegRSol,&fNSegRSol,
523 //__________________________________________________________________________________________
524 void AliMagWrapCheb::BuildTableDip()
526 BuildTable(fNParamsDip,fParamsDip,
527 fNZSegDip,fNYSegDip,fNXSegDip,
529 &fSegZDip,&fSegYDip,&fSegXDip,
530 &fBegSegYDip,&fNSegYDip,
531 &fBegSegXDip,&fNSegXDip,
535 //__________________________________________________________________________________________
536 void AliMagWrapCheb::BuildTableTPCInt()
538 BuildTable(fNParamsTPC,fParamsTPC,
539 fNZSegTPC,fNPSegTPC,fNRSegTPC,
541 &fSegZTPC,&fSegPTPC,&fSegRTPC,
542 &fBegSegPTPC,&fNSegPTPC,
543 &fBegSegRTPC,&fNSegRTPC,
549 //_______________________________________________
550 #ifdef _INC_CREATION_ALICHEB3D_
552 //__________________________________________________________________________________________
553 AliMagWrapCheb::AliMagWrapCheb(const char* inputFile) :
554 fNParamsSol(0),fNZSegSol(0),fNPSegSol(0),fNRSegSol(0),
555 fSegZSol(0),fSegPSol(0),fSegRSol(0),
556 fBegSegPSol(0),fNSegPSol(0),fBegSegRSol(0),fNSegRSol(0),fSegIDSol(0),fMinZSol(1.e6),fMaxZSol(-1.e6),fParamsSol(0),fMaxRSol(0),
558 fNParamsTPC(0),fNZSegTPC(0),fNPSegTPC(0),fNRSegTPC(0),
559 fSegZTPC(0),fSegPTPC(0),fSegRTPC(0),
560 fBegSegPTPC(0),fNSegPTPC(0),fBegSegRTPC(0),fNSegRTPC(0),fSegIDTPC(0),fMinZTPC(1.e6),fMaxZTPC(-1.e6),fParamsTPC(0),fMaxRTPC(0),
562 fNParamsDip(0),fNZSegDip(0),fNYSegDip(0),fNXSegDip(0),
563 fSegZDip(0),fSegYDip(0),fSegXDip(0),
564 fBegSegYDip(0),fNSegYDip(0),fBegSegXDip(0),fNSegXDip(0),fSegIDDip(0),fMinZDip(1.e6),fMaxZDip(-1.e6),fParamsDip(0)
567 // construct from coeffs from the text file
571 //__________________________________________________________________________________________
572 void AliMagWrapCheb::AddParamSol(const AliCheb3D* param)
574 // adds new parameterization piece for Sol
575 // NOTE: pieces must be added strictly in increasing R then increasing Z order
577 if (!fParamsSol) fParamsSol = new TObjArray();
578 fParamsSol->Add( (AliCheb3D*)param );
580 if (fMaxRSol<param->GetBoundMax(0)) fMaxRSol = param->GetBoundMax(0);
584 //__________________________________________________________________________________________
585 void AliMagWrapCheb::AddParamTPCInt(const AliCheb3D* param)
587 // adds new parameterization piece for TPCInt
588 // NOTE: pieces must be added strictly in increasing R then increasing Z order
590 if (!fParamsTPC) fParamsTPC = new TObjArray();
591 fParamsTPC->Add( (AliCheb3D*)param);
593 if (fMaxRTPC<param->GetBoundMax(0)) fMaxRTPC = param->GetBoundMax(0);
597 //__________________________________________________________________________________________
598 void AliMagWrapCheb::AddParamDip(const AliCheb3D* param)
600 // adds new parameterization piece for Dipole
602 if (!fParamsDip) fParamsDip = new TObjArray();
603 fParamsDip->Add( (AliCheb3D*)param);
608 //__________________________________________________________________________________________
609 void AliMagWrapCheb::ResetTPCInt()
611 // clean TPC field integral (used for update)
613 delete fParamsTPC; fParamsTPC = 0;
614 delete[] fSegZTPC; fSegZTPC = 0;
615 delete[] fSegPTPC; fSegPTPC = 0;
616 delete[] fSegRTPC; fSegRTPC = 0;
617 delete[] fBegSegPTPC; fBegSegPTPC = 0;
618 delete[] fNSegPTPC; fNSegPTPC = 0;
619 delete[] fBegSegRTPC; fBegSegRTPC = 0;
620 delete[] fNSegRTPC; fNSegRTPC = 0;
621 delete[] fSegIDTPC; fSegIDTPC = 0;
623 fNParamsTPC = fNZSegTPC = fNPSegTPC = fNRSegTPC = 0;
631 //__________________________________________________
632 void AliMagWrapCheb::BuildTable(Int_t npar,TObjArray *parArr, Int_t &nZSeg, Int_t &nYSeg, Int_t &nXSeg,
633 Float_t &minZ,Float_t &maxZ,
634 Float_t **segZ,Float_t **segY,Float_t **segX,
635 Int_t **begSegY,Int_t **nSegY,
636 Int_t **begSegX,Int_t **nSegX,
639 // build lookup table for dipole
642 TArrayF segYArr,segXArr;
643 TArrayI begSegYDipArr,begSegXDipArr;
644 TArrayI nSegYDipArr,nSegXDipArr;
646 float *tmpSegZ,*tmpSegY,*tmpSegX;
648 // create segmentation in Z
649 nZSeg = SegmentDimension(&tmpSegZ, parArr, npar, 2, 1,-1, 1,-1, 1,-1) - 1;
653 // for each Z slice create segmentation in Y
654 begSegYDipArr.Set(nZSeg);
655 nSegYDipArr.Set(nZSeg);
657 for (int iz=0;iz<nZSeg;iz++) {
658 printf("\nZSegment#%d %+e : %+e\n",iz,tmpSegZ[iz],tmpSegZ[iz+1]);
659 int ny = SegmentDimension(&tmpSegY, parArr, npar, 1,
660 1,-1, 1,-1, tmpSegZ[iz],tmpSegZ[iz+1]) - 1;
661 segYArr.Set(ny + nYSeg);
662 for (int iy=0;iy<ny;iy++) segYArr[nYSeg+iy] = tmpSegY[iy];
663 begSegYDipArr[iz] = nYSeg;
664 nSegYDipArr[iz] = ny;
665 printf(" Found %d YSegments, to start from %d\n",ny, begSegYDipArr[iz]);
667 // for each slice in Z and Y create segmentation in X
668 begSegXDipArr.Set(nYSeg+ny);
669 nSegXDipArr.Set(nYSeg+ny);
670 xyz[2] = (tmpSegZ[iz]+tmpSegZ[iz+1])/2.; // mean Z of this segment
672 for (int iy=0;iy<ny;iy++) {
674 printf("\n YSegment#%d %+e : %+e\n",iy, tmpSegY[iy],tmpSegY[iy+1]);
675 int nx = SegmentDimension(&tmpSegX, parArr, npar, 0,
676 1,-1, tmpSegY[iy],tmpSegY[iy+1], tmpSegZ[iz],tmpSegZ[iz+1]) - 1;
678 segXArr.Set(nx + nXSeg);
679 for (int ix=0;ix<nx;ix++) segXArr[nXSeg+ix] = tmpSegX[ix];
680 begSegXDipArr[isg] = nXSeg;
681 nSegXDipArr[isg] = nx;
682 printf(" Found %d XSegments, to start from %d\n",nx, begSegXDipArr[isg]);
684 segIDArr.Set(nXSeg+nx);
686 // find corresponding params
687 xyz[1] = (tmpSegY[iy]+tmpSegY[iy+1])/2.; // mean Y of this segment
689 for (int ix=0;ix<nx;ix++) {
690 xyz[0] = (tmpSegX[ix]+tmpSegX[ix+1])/2.; // mean X of this segment
691 for (int ipar=0;ipar<npar;ipar++) {
692 AliCheb3D* cheb = (AliCheb3D*) parArr->At(ipar);
693 if (!cheb->IsInside(xyz)) continue;
694 segIDArr[nXSeg+ix] = ipar;
707 maxZ = tmpSegZ[nZSeg];
708 (*segZ) = new Float_t[nZSeg];
709 for (int i=nZSeg;i--;) (*segZ)[i] = tmpSegZ[i];
712 (*segY) = new Float_t[nYSeg];
713 (*segX) = new Float_t[nXSeg];
714 (*begSegY) = new Int_t[nZSeg];
715 (*nSegY) = new Int_t[nZSeg];
716 (*begSegX) = new Int_t[nYSeg];
717 (*nSegX) = new Int_t[nYSeg];
718 (*segID) = new Int_t[nXSeg];
720 for (int i=nYSeg;i--;) (*segY)[i] = segYArr[i];
721 for (int i=nXSeg;i--;) (*segX)[i] = segXArr[i];
722 for (int i=nZSeg;i--;) {(*begSegY)[i] = begSegYDipArr[i]; (*nSegY)[i] = nSegYDipArr[i];}
723 for (int i=nYSeg;i--;) {(*begSegX)[i] = begSegXDipArr[i]; (*nSegX)[i] = nSegXDipArr[i];}
724 for (int i=nXSeg;i--;) {(*segID)[i] = segIDArr[i];}
729 //__________________________________________________
730 void AliMagWrapCheb::BuildTableDip()
732 // build lookup table for dipole
734 if (fNParamsDip<1) return;
736 TArrayI begSegYDip,begSegXDip;
737 TArrayI nsegYDip,nsegXDip;
739 float *tmpSegZ,*tmpSegY,*tmpSegX;
741 // create segmentation in Z
742 fNZSegDip = SegmentDimension(&tmpSegZ, fParamsDip, fNParamsDip, 2, 1,-1, 1,-1, 1,-1) - 1;
746 // for each Z slice create segmentation in Y
747 begSegYDip.Set(fNZSegDip);
748 nsegYDip.Set(fNZSegDip);
750 for (int iz=0;iz<fNZSegDip;iz++) {
751 printf("\nZSegment#%d %+e : %+e\n",iz,tmpSegZ[iz],tmpSegZ[iz+1]);
752 int ny = SegmentDimension(&tmpSegY, fParamsDip, fNParamsDip, 1,
753 1,-1, 1,-1, tmpSegZ[iz],tmpSegZ[iz+1]) - 1;
754 segY.Set(ny + fNYSegDip);
755 for (int iy=0;iy<ny;iy++) segY[fNYSegDip+iy] = tmpSegY[iy];
756 begSegYDip[iz] = fNYSegDip;
758 printf(" Found %d YSegments, to start from %d\n",ny, begSegYDip[iz]);
760 // for each slice in Z and Y create segmentation in X
761 begSegXDip.Set(fNYSegDip+ny);
762 nsegXDip.Set(fNYSegDip+ny);
763 xyz[2] = (tmpSegZ[iz]+tmpSegZ[iz+1])/2.; // mean Z of this segment
765 for (int iy=0;iy<ny;iy++) {
766 int isg = fNYSegDip+iy;
767 printf("\n YSegment#%d %+e : %+e\n",iy, tmpSegY[iy],tmpSegY[iy+1]);
768 int nx = SegmentDimension(&tmpSegX, fParamsDip, fNParamsDip, 0,
769 1,-1, tmpSegY[iy],tmpSegY[iy+1], tmpSegZ[iz],tmpSegZ[iz+1]) - 1;
771 segX.Set(nx + fNXSegDip);
772 for (int ix=0;ix<nx;ix++) segX[fNXSegDip+ix] = tmpSegX[ix];
773 begSegXDip[isg] = fNXSegDip;
775 printf(" Found %d XSegments, to start from %d\n",nx, begSegXDip[isg]);
777 segID.Set(fNXSegDip+nx);
779 // find corresponding params
780 xyz[1] = (tmpSegY[iy]+tmpSegY[iy+1])/2.; // mean Y of this segment
782 for (int ix=0;ix<nx;ix++) {
783 xyz[0] = (tmpSegX[ix]+tmpSegX[ix+1])/2.; // mean X of this segment
784 for (int ipar=0;ipar<fNParamsDip;ipar++) {
785 AliCheb3D* cheb = (AliCheb3D*) fParamsDip->At(ipar);
786 if (!cheb->IsInside(xyz)) continue;
787 segID[fNXSegDip+ix] = ipar;
799 fMinZDip = tmpSegZ[0];
800 fMaxZDip = tmpSegZ[fNZSegDip];
801 fSegZDip = new Float_t[fNZSegDip];
802 for (int i=fNZSegDip;i--;) fSegZDip[i] = tmpSegZ[i];
805 fSegYDip = new Float_t[fNYSegDip];
806 fSegXDip = new Float_t[fNXSegDip];
807 fBegSegYDip = new Int_t[fNZSegDip];
808 fNSegYDip = new Int_t[fNZSegDip];
809 fBegSegXDip = new Int_t[fNYSegDip];
810 fNSegXDip = new Int_t[fNYSegDip];
811 fSegIDDip = new Int_t[fNXSegDip];
813 for (int i=fNYSegDip;i--;) fSegYDip[i] = segY[i];
814 for (int i=fNXSegDip;i--;) fSegXDip[i] = segX[i];
815 for (int i=fNZSegDip;i--;) {fBegSegYDip[i] = begSegYDip[i]; fNSegYDip[i] = nsegYDip[i];}
816 for (int i=fNYSegDip;i--;) {fBegSegXDip[i] = begSegXDip[i]; fNSegXDip[i] = nsegXDip[i];}
817 for (int i=fNXSegDip;i--;) {fSegIDDip[i] = segID[i];}
822 //________________________________________________________________
823 void AliMagWrapCheb::SaveData(const char* outfile) const
825 // writes coefficients data to output text file
826 TString strf = outfile;
827 gSystem->ExpandPathName(strf);
828 FILE* stream = fopen(strf,"w+");
830 // Sol part ---------------------------------------------------------
831 fprintf(stream,"# Set of Chebyshev parameterizations for ALICE magnetic field\nSTART %s\n",GetName());
832 fprintf(stream,"START SOLENOID\n#Number of pieces\n%d\n",fNParamsSol);
833 for (int ip=0;ip<fNParamsSol;ip++) GetParamSol(ip)->SaveData(stream);
834 fprintf(stream,"#\nEND SOLENOID\n");
836 // TPCInt part ---------------------------------------------------------
837 fprintf(stream,"# Set of Chebyshev parameterizations for ALICE magnetic field\nSTART %s\n",GetName());
838 fprintf(stream,"START TPCINT\n#Number of pieces\n%d\n",fNParamsTPC);
839 for (int ip=0;ip<fNParamsTPC;ip++) GetParamTPCInt(ip)->SaveData(stream);
840 fprintf(stream,"#\nEND TPCINT\n");
842 // Dip part ---------------------------------------------------------
843 fprintf(stream,"START DIPOLE\n#Number of pieces\n%d\n",fNParamsDip);
844 for (int ip=0;ip<fNParamsDip;ip++) GetParamDip(ip)->SaveData(stream);
845 fprintf(stream,"#\nEND DIPOLE\n");
847 fprintf(stream,"#\nEND %s\n",GetName());
853 Int_t AliMagWrapCheb::SegmentDimension(float** seg,const TObjArray* par,int npar, int dim,
854 float xmn,float xmx,float ymn,float ymx,float zmn,float zmx)
856 // find all boundaries in deimension dim for boxes in given region.
857 // if mn>mx for given projection the check is not done for it.
858 float *tmpC = new float[2*npar];
859 int *tmpInd = new int[2*npar];
861 for (int ip=0;ip<npar;ip++) {
862 AliCheb3D* cheb = (AliCheb3D*) par->At(ip);
863 if (xmn<xmx && (cheb->GetBoundMin(0)>(xmx+xmn)/2 || cheb->GetBoundMax(0)<(xmn+xmx)/2)) continue;
864 if (ymn<ymx && (cheb->GetBoundMin(1)>(ymx+ymn)/2 || cheb->GetBoundMax(1)<(ymn+ymx)/2)) continue;
865 if (zmn<zmx && (cheb->GetBoundMin(2)>(zmx+zmn)/2 || cheb->GetBoundMax(2)<(zmn+zmx)/2)) continue;
867 tmpC[nseg0++] = cheb->GetBoundMin(dim);
868 tmpC[nseg0++] = cheb->GetBoundMax(dim);
870 // range Dim's boundaries in increasing order
871 TMath::Sort(nseg0,tmpC,tmpInd,kFALSE);
872 // count number of really different Z's
875 for (int ip=0;ip<nseg0;ip++) {
876 if (TMath::Abs(cprev-tmpC[ tmpInd[ip] ])>1e-4) {
877 cprev = tmpC[ tmpInd[ip] ];
880 else tmpInd[ip] = -1; // supress redundant Z
883 *seg = new float[nseg]; // create final Z segmenations
885 for (int ip=0;ip<nseg0;ip++) if (tmpInd[ip]>=0) (*seg)[nseg++] = tmpC[ tmpInd[ip] ];