]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliMagWrapCheb.cxx
additional protection agains call with 0 pointers
[u/mrichter/AliRoot.git] / STEER / AliMagWrapCheb.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
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  **************************************************************************/
15
16 #include "AliMagWrapCheb.h"
17 #include "AliLog.h"
18 #include <TSystem.h>
19 #include <TArrayF.h>
20 #include <TArrayI.h>
21
22 ClassImp(AliMagWrapCheb)
23
24 //__________________________________________________________________________________________
25 AliMagWrapCheb::AliMagWrapCheb() : 
26 fNParamsSol(0),fNZSegSol(0),fNPSegSol(0),fNRSegSol(0),
27   fSegZSol(0),fSegPSol(0),fSegRSol(0),
28   fBegSegPSol(0),fNSegPSol(0),fBegSegRSol(0),fNSegRSol(0),fSegIDSol(0),fMinZSol(1.e6),fMaxZSol(-1.e6),fParamsSol(0),fMaxRSol(0),
29 //
30   fNParamsTPC(0),fNZSegTPC(0),fNPSegTPC(0),fNRSegTPC(0),
31   fSegZTPC(0),fSegPTPC(0),fSegRTPC(0),
32   fBegSegPTPC(0),fNSegPTPC(0),fBegSegRTPC(0),fNSegRTPC(0),fSegIDTPC(0),fMinZTPC(1.e6),fMaxZTPC(-1.e6),fParamsTPC(0),fMaxRTPC(0),
33 //
34   fNParamsTPCRat(0),fNZSegTPCRat(0),fNPSegTPCRat(0),fNRSegTPCRat(0),
35   fSegZTPCRat(0),fSegPTPCRat(0),fSegRTPCRat(0),
36   fBegSegPTPCRat(0),fNSegPTPCRat(0),fBegSegRTPCRat(0),fNSegRTPCRat(0),fSegIDTPCRat(0),fMinZTPCRat(1.e6),fMaxZTPCRat(-1.e6),fParamsTPCRat(0),fMaxRTPCRat(0),
37 //
38   fNParamsDip(0),fNZSegDip(0),fNYSegDip(0),fNXSegDip(0),
39   fSegZDip(0),fSegYDip(0),fSegXDip(0),
40   fBegSegYDip(0),fNSegYDip(0),fBegSegXDip(0),fNSegXDip(0),fSegIDDip(0),fMinZDip(1.e6),fMaxZDip(-1.e6),fParamsDip(0)
41 //
42 {
43   // default constructor
44 }
45
46 //__________________________________________________________________________________________
47 AliMagWrapCheb::AliMagWrapCheb(const AliMagWrapCheb& src) : 
48   TNamed(src),
49   fNParamsSol(0),fNZSegSol(0),fNPSegSol(0),fNRSegSol(0),
50   fSegZSol(0),fSegPSol(0),fSegRSol(0),
51   fBegSegPSol(0),fNSegPSol(0),fBegSegRSol(0),fNSegRSol(0),fSegIDSol(0),fMinZSol(1.e6),fMaxZSol(-1.e6),fParamsSol(0),fMaxRSol(0),
52 //
53   fNParamsTPC(0),fNZSegTPC(0),fNPSegTPC(0),fNRSegTPC(0),
54   fSegZTPC(0),fSegPTPC(0),fSegRTPC(0),
55   fBegSegPTPC(0),fNSegPTPC(0),fBegSegRTPC(0),fNSegRTPC(0),fSegIDTPC(0),fMinZTPC(1.e6),fMaxZTPC(-1.e6),fParamsTPC(0),fMaxRTPC(0),
56 //
57   fNParamsTPCRat(0),fNZSegTPCRat(0),fNPSegTPCRat(0),fNRSegTPCRat(0),
58   fSegZTPCRat(0),fSegPTPCRat(0),fSegRTPCRat(0),
59   fBegSegPTPCRat(0),fNSegPTPCRat(0),fBegSegRTPCRat(0),fNSegRTPCRat(0),fSegIDTPCRat(0),fMinZTPCRat(1.e6),fMaxZTPCRat(-1.e6),fParamsTPCRat(0),fMaxRTPCRat(0),
60 //
61   fNParamsDip(0),fNZSegDip(0),fNYSegDip(0),fNXSegDip(0),
62   fSegZDip(0),fSegYDip(0),fSegXDip(0),
63   fBegSegYDip(0),fNSegYDip(0),fBegSegXDip(0),fNSegXDip(0),fSegIDDip(0),fMinZDip(1.e6),fMaxZDip(-1.e6),fParamsDip(0)
64 {
65   // copy constructor
66   CopyFrom(src);
67 }
68
69 //__________________________________________________________________________________________
70 void AliMagWrapCheb::CopyFrom(const AliMagWrapCheb& src) 
71
72   // copy method
73   Clear();
74   SetName(src.GetName());
75   SetTitle(src.GetTitle());
76   //
77   fNParamsSol    = src.fNParamsSol;
78   fNZSegSol      = src.fNZSegSol;
79   fNPSegSol      = src.fNPSegSol;
80   fNRSegSol      = src.fNRSegSol;  
81   fMinZSol       = src.fMinZSol;
82   fMaxZSol       = src.fMaxZSol;
83   fMaxRSol       = src.fMaxRSol;
84   if (src.fNParamsSol) {
85     memcpy(fSegZSol   = new Float_t[fNZSegSol], src.fSegZSol, sizeof(Float_t)*fNZSegSol);
86     memcpy(fSegPSol   = new Float_t[fNPSegSol], src.fSegPSol, sizeof(Float_t)*fNPSegSol);
87     memcpy(fSegRSol   = new Float_t[fNRSegSol], src.fSegRSol, sizeof(Float_t)*fNRSegSol);
88     memcpy(fBegSegPSol= new Int_t[fNZSegSol], src.fBegSegPSol, sizeof(Int_t)*fNZSegSol);
89     memcpy(fNSegPSol  = new Int_t[fNZSegSol], src.fNSegPSol, sizeof(Int_t)*fNZSegSol);
90     memcpy(fBegSegRSol= new Int_t[fNPSegSol], src.fBegSegRSol, sizeof(Int_t)*fNPSegSol);
91     memcpy(fNSegRSol  = new Int_t[fNPSegSol], src.fNSegRSol, sizeof(Int_t)*fNPSegSol);
92     memcpy(fSegIDSol  = new Int_t[fNRSegSol], src.fSegIDSol, sizeof(Int_t)*fNRSegSol);
93     fParamsSol        = new TObjArray(fNParamsSol);
94     for (int i=0;i<fNParamsSol;i++) fParamsSol->AddAtAndExpand(new AliCheb3D(*src.GetParamSol(i)),i);
95   }
96   //
97   fNParamsTPC    = src.fNParamsTPC;
98   fNZSegTPC      = src.fNZSegTPC;
99   fNPSegTPC      = src.fNPSegTPC;
100   fNRSegTPC      = src.fNRSegTPC;  
101   fMinZTPC       = src.fMinZTPC;
102   fMaxZTPC       = src.fMaxZTPC;
103   fMaxRTPC       = src.fMaxRTPC;
104   if (src.fNParamsTPC) {
105     memcpy(fSegZTPC   = new Float_t[fNZSegTPC], src.fSegZTPC, sizeof(Float_t)*fNZSegTPC);
106     memcpy(fSegPTPC   = new Float_t[fNPSegTPC], src.fSegPTPC, sizeof(Float_t)*fNPSegTPC);
107     memcpy(fSegRTPC   = new Float_t[fNRSegTPC], src.fSegRTPC, sizeof(Float_t)*fNRSegTPC);
108     memcpy(fBegSegPTPC= new Int_t[fNZSegTPC], src.fBegSegPTPC, sizeof(Int_t)*fNZSegTPC);
109     memcpy(fNSegPTPC  = new Int_t[fNZSegTPC], src.fNSegPTPC, sizeof(Int_t)*fNZSegTPC);
110     memcpy(fBegSegRTPC= new Int_t[fNPSegTPC], src.fBegSegRTPC, sizeof(Int_t)*fNPSegTPC);
111     memcpy(fNSegRTPC  = new Int_t[fNPSegTPC], src.fNSegRTPC, sizeof(Int_t)*fNPSegTPC);
112     memcpy(fSegIDTPC  = new Int_t[fNRSegTPC], src.fSegIDTPC, sizeof(Int_t)*fNRSegTPC);
113     fParamsTPC        = new TObjArray(fNParamsTPC);
114     for (int i=0;i<fNParamsTPC;i++) fParamsTPC->AddAtAndExpand(new AliCheb3D(*src.GetParamTPCInt(i)),i);
115   }
116   //
117   fNParamsTPCRat    = src.fNParamsTPCRat;
118   fNZSegTPCRat      = src.fNZSegTPCRat;
119   fNPSegTPCRat      = src.fNPSegTPCRat;
120   fNRSegTPCRat      = src.fNRSegTPCRat;  
121   fMinZTPCRat       = src.fMinZTPCRat;
122   fMaxZTPCRat       = src.fMaxZTPCRat;
123   fMaxRTPCRat       = src.fMaxRTPCRat;
124   if (src.fNParamsTPCRat) {
125     memcpy(fSegZTPCRat   = new Float_t[fNZSegTPCRat], src.fSegZTPCRat, sizeof(Float_t)*fNZSegTPCRat);
126     memcpy(fSegPTPCRat   = new Float_t[fNPSegTPCRat], src.fSegPTPCRat, sizeof(Float_t)*fNPSegTPCRat);
127     memcpy(fSegRTPCRat   = new Float_t[fNRSegTPCRat], src.fSegRTPCRat, sizeof(Float_t)*fNRSegTPCRat);
128     memcpy(fBegSegPTPCRat= new Int_t[fNZSegTPCRat], src.fBegSegPTPCRat, sizeof(Int_t)*fNZSegTPCRat);
129     memcpy(fNSegPTPCRat  = new Int_t[fNZSegTPCRat], src.fNSegPTPCRat, sizeof(Int_t)*fNZSegTPCRat);
130     memcpy(fBegSegRTPCRat= new Int_t[fNPSegTPCRat], src.fBegSegRTPCRat, sizeof(Int_t)*fNPSegTPCRat);
131     memcpy(fNSegRTPCRat  = new Int_t[fNPSegTPCRat], src.fNSegRTPCRat, sizeof(Int_t)*fNPSegTPCRat);
132     memcpy(fSegIDTPCRat  = new Int_t[fNRSegTPCRat], src.fSegIDTPCRat, sizeof(Int_t)*fNRSegTPCRat);
133     fParamsTPCRat        = new TObjArray(fNParamsTPCRat);
134     for (int i=0;i<fNParamsTPCRat;i++) fParamsTPCRat->AddAtAndExpand(new AliCheb3D(*src.GetParamTPCRatInt(i)),i);
135   }
136   //
137   fNParamsDip    = src.fNParamsDip;
138   fNZSegDip      = src.fNZSegDip;
139   fNYSegDip      = src.fNYSegDip;
140   fNXSegDip      = src.fNXSegDip;  
141   fMinZDip       = src.fMinZDip;
142   fMaxZDip       = src.fMaxZDip;
143   if (src.fNParamsDip) {
144     memcpy(fSegZDip   = new Float_t[fNZSegDip], src.fSegZDip, sizeof(Float_t)*fNZSegDip);
145     memcpy(fSegYDip   = new Float_t[fNYSegDip], src.fSegYDip, sizeof(Float_t)*fNYSegDip);
146     memcpy(fSegXDip   = new Float_t[fNXSegDip], src.fSegXDip, sizeof(Float_t)*fNXSegDip);
147     memcpy(fBegSegYDip= new Int_t[fNZSegDip], src.fBegSegYDip, sizeof(Int_t)*fNZSegDip);
148     memcpy(fNSegYDip  = new Int_t[fNZSegDip], src.fNSegYDip, sizeof(Int_t)*fNZSegDip);
149     memcpy(fBegSegXDip= new Int_t[fNYSegDip], src.fBegSegXDip, sizeof(Int_t)*fNYSegDip);
150     memcpy(fNSegXDip  = new Int_t[fNYSegDip], src.fNSegXDip, sizeof(Int_t)*fNYSegDip);
151     memcpy(fSegIDDip  = new Int_t[fNXSegDip], src.fSegIDDip, sizeof(Int_t)*fNXSegDip);
152     fParamsDip        = new TObjArray(fNParamsDip);
153     for (int i=0;i<fNParamsDip;i++) fParamsDip->AddAtAndExpand(new AliCheb3D(*src.GetParamDip(i)),i);
154   }
155   //
156 }
157
158 //__________________________________________________________________________________________
159 AliMagWrapCheb& AliMagWrapCheb::operator=(const AliMagWrapCheb& rhs)
160 {
161   // assignment
162   if (this != &rhs) {  
163     Clear();
164     CopyFrom(rhs);
165   }
166   return *this;  
167   //
168 }
169
170 //__________________________________________________________________________________________
171 void AliMagWrapCheb::Clear(const Option_t *)
172 {
173   // clear all dynamic parts
174   if (fNParamsSol) {
175     delete   fParamsSol;  fParamsSol = 0;
176     delete[] fSegZSol;    fSegZSol   = 0;
177     delete[] fSegPSol;    fSegPSol   = 0;
178     delete[] fSegRSol;    fSegRSol   = 0;
179     delete[] fBegSegPSol; fBegSegPSol = 0;
180     delete[] fNSegPSol;   fNSegPSol   = 0;
181     delete[] fBegSegRSol; fBegSegRSol = 0;
182     delete[] fNSegRSol;   fNSegRSol   = 0;
183     delete[] fSegIDSol;   fSegIDSol   = 0;   
184   }
185   fNParamsSol = fNZSegSol = fNPSegSol = fNRSegSol = 0;
186   fMinZSol = 1e6;
187   fMaxZSol = -1e6;
188   fMaxRSol = 0;
189   //
190   if (fNParamsTPC) {
191     delete   fParamsTPC;  fParamsTPC = 0;
192     delete[] fSegZTPC;    fSegZTPC   = 0;
193     delete[] fSegPTPC;    fSegPTPC   = 0;
194     delete[] fSegRTPC;    fSegRTPC   = 0;
195     delete[] fBegSegPTPC; fBegSegPTPC = 0;
196     delete[] fNSegPTPC;   fNSegPTPC   = 0;
197     delete[] fBegSegRTPC; fBegSegRTPC = 0;
198     delete[] fNSegRTPC;   fNSegRTPC   = 0;
199     delete[] fSegIDTPC;   fSegIDTPC   = 0;   
200   }
201   fNParamsTPC = fNZSegTPC = fNPSegTPC = fNRSegTPC = 0;
202   fMinZTPC = 1e6;
203   fMaxZTPC = -1e6;
204   fMaxRTPC = 0;
205   //
206   if (fNParamsTPCRat) {
207     delete   fParamsTPCRat;  fParamsTPCRat = 0;
208     delete[] fSegZTPCRat;    fSegZTPCRat   = 0;
209     delete[] fSegPTPCRat;    fSegPTPCRat   = 0;
210     delete[] fSegRTPCRat;    fSegRTPCRat   = 0;
211     delete[] fBegSegPTPCRat; fBegSegPTPCRat = 0;
212     delete[] fNSegPTPCRat;   fNSegPTPCRat   = 0;
213     delete[] fBegSegRTPCRat; fBegSegRTPCRat = 0;
214     delete[] fNSegRTPCRat;   fNSegRTPCRat   = 0;
215     delete[] fSegIDTPCRat;   fSegIDTPCRat   = 0;   
216   }
217   fNParamsTPCRat = fNZSegTPCRat = fNPSegTPCRat = fNRSegTPCRat = 0;
218   fMinZTPCRat = 1e6;
219   fMaxZTPCRat = -1e6;
220   fMaxRTPCRat = 0;
221   //
222   if (fNParamsDip) {
223     delete   fParamsDip;  fParamsDip = 0;
224     delete[] fSegZDip;    fSegZDip   = 0;
225     delete[] fSegYDip;    fSegYDip   = 0; 
226     delete[] fSegXDip;    fSegXDip   = 0;
227     delete[] fBegSegYDip; fBegSegYDip = 0;
228     delete[] fNSegYDip;   fNSegYDip   = 0;
229     delete[] fBegSegXDip; fBegSegXDip = 0; 
230     delete[] fNSegXDip;   fNSegXDip   = 0;
231     delete[] fSegIDDip;   fSegIDDip   = 0;
232   }
233   fNParamsDip = fNZSegDip = fNYSegDip = fNXSegDip = 0;
234   fMinZDip = 1e6;
235   fMaxZDip = -1e6;
236   //
237 }
238
239 //__________________________________________________________________________________________
240 void AliMagWrapCheb::Field(const Double_t *xyz, Double_t *b) const
241 {
242   // compute field in cartesian coordinates. If point is outside of the parameterized region
243   // get it at closest valid point
244   Double_t rphiz[3];
245   //
246 #ifndef _BRING_TO_BOUNDARY_  // exact matching to fitted volume is requested
247   b[0] = b[1] = b[2] = 0;
248 #endif
249   //
250   if (xyz[2]>fMinZSol) {
251     CartToCyl(xyz,rphiz);
252     FieldCylSol(rphiz,b);
253     // convert field to cartesian system
254     CylToCartCylB(rphiz, b,b);  
255     return;
256   }
257   //
258   int iddip = FindDipSegment(xyz);
259   if (iddip<0) return;
260   AliCheb3D* par = GetParamDip(iddip);
261 #ifndef _BRING_TO_BOUNDARY_
262   if (!par->IsInside(xyz)) return;
263 #endif
264   par->Eval(xyz,b); 
265   //
266 }
267
268 //__________________________________________________________________________________________
269 Double_t AliMagWrapCheb::GetBz(const Double_t *xyz) const
270 {
271   // compute Bz for the point in cartesian coordinates. If point is outside of the parameterized region
272   // get it at closest valid point
273   Double_t rphiz[3];
274   //
275   if (xyz[2]>fMinZSol) {
276     CartToCyl(xyz,rphiz);
277     return FieldCylSolBz(rphiz);
278   }
279   //
280   int iddip = FindDipSegment(xyz);
281   if (iddip<0) return 0.;
282   AliCheb3D* par = GetParamDip(iddip);
283 #ifndef _BRING_TO_BOUNDARY_
284   if (!par->IsInside(xyz)) return 0.;
285 #endif
286   return par->Eval(xyz,2);
287 }
288
289
290 //__________________________________________________________________________________________
291 void AliMagWrapCheb::Print(Option_t *) const
292 {
293   // print info
294   printf("Alice magnetic field parameterized by Chebyshev polynomials\n");
295   printf("Segmentation for Solenoid (%+.2f<Z<%+.2f cm | R<%.2f cm)\n",fMinZSol,fMaxZSol,fMaxRSol);
296   //
297   if (fParamsSol) {
298     for (int i=0;i<fNParamsSol;i++) {
299       printf("SOL%4d ",i);
300       GetParamSol(i)->Print();
301     }
302   }
303   //
304   printf("Segmentation for TPC field integral (%+.2f<Z<%+.2f cm | R<%.2f cm)\n",fMinZTPC,fMaxZTPC,fMaxRTPC);
305   //
306   if (fParamsTPC) {
307     for (int i=0;i<fNParamsTPC;i++) {
308       printf("TPC%4d ",i);
309       GetParamTPCInt(i)->Print();
310     }
311   }
312   //
313   printf("Segmentation for TPC field ratios integral (%+.2f<Z<%+.2f cm | R<%.2f cm)\n",fMinZTPCRat,fMaxZTPCRat,fMaxRTPCRat);
314   //
315   if (fParamsTPCRat) {
316     for (int i=0;i<fNParamsTPCRat;i++) {
317       printf("TPCRat%4d ",i);
318       GetParamTPCRatInt(i)->Print();
319     }
320   }
321   //
322   printf("Segmentation for Dipole (%+.2f<Z<%+.2f cm)\n",fMinZDip,fMaxZDip);
323   if (fParamsDip) {
324     for (int i=0;i<fNParamsDip;i++) {
325       printf("DIP%4d ",i);
326       GetParamDip(i)->Print();
327     }
328   }
329   //
330 }
331
332 //__________________________________________________________________________________________________
333 Int_t AliMagWrapCheb::FindDipSegment(const Double_t *xyz) const 
334 {
335   // find the segment containing point xyz. If it is outside find the closest segment 
336   if (!fNParamsDip) return -1;
337   int xid,yid,zid = TMath::BinarySearch(fNZSegDip,fSegZDip,(Float_t)xyz[2]); // find zsegment
338   //
339   Bool_t reCheck = kFALSE;
340   while(1) {
341     int ysegBeg = fBegSegYDip[zid];
342     //
343     for (yid=0;yid<fNSegYDip[zid];yid++) if (xyz[1]<fSegYDip[ysegBeg+yid]) break;
344     if ( --yid < 0 ) yid = 0;
345     yid +=  ysegBeg;
346     //
347     int xsegBeg = fBegSegXDip[yid];
348     for (xid=0;xid<fNSegXDip[yid];xid++) if (xyz[0]<fSegXDip[xsegBeg+xid]) break;
349     //
350     if ( --xid < 0) xid = 0;
351     xid +=  xsegBeg;
352     //
353     // to make sure that due to the precision problems we did not pick the next Zbin    
354     if (!reCheck && (xyz[2] - fSegZDip[zid] < 3.e-5) && zid &&
355         !GetParamDip(fSegIDDip[xid])->IsInside(xyz)) {  // check the previous Z bin
356       zid--;
357       reCheck = kTRUE;
358       continue;
359     } 
360     break;
361   }
362   return fSegIDDip[xid];
363 }
364
365 //__________________________________________________________________________________________________
366 Int_t AliMagWrapCheb::FindSolSegment(const Double_t *rpz) const 
367 {
368   // find the segment containing point xyz. If it is outside find the closest segment 
369   if (!fNParamsSol) return -1;
370   int rid,pid,zid = TMath::BinarySearch(fNZSegSol,fSegZSol,(Float_t)rpz[2]); // find zsegment
371   //
372   Bool_t reCheck = kFALSE;
373   while(1) {
374     int psegBeg = fBegSegPSol[zid];
375     for (pid=0;pid<fNSegPSol[zid];pid++) if (rpz[1]<fSegPSol[psegBeg+pid]) break;
376     if ( --pid < 0 ) pid = 0;
377     pid +=  psegBeg;
378     //
379     int rsegBeg = fBegSegRSol[pid];
380     for (rid=0;rid<fNSegRSol[pid];rid++) if (rpz[0]<fSegRSol[rsegBeg+rid]) break;
381     if ( --rid < 0) rid = 0;
382     rid +=  rsegBeg;
383     //
384     // to make sure that due to the precision problems we did not pick the next Zbin    
385     if (!reCheck && (rpz[2] - fSegZSol[zid] < 3.e-5) && zid &&
386         !GetParamSol(fSegIDSol[rid])->IsInside(rpz)) {  // check the previous Z bin
387       zid--;
388       reCheck = kTRUE;
389       continue;
390     } 
391     break;
392   }
393   return fSegIDSol[rid];
394 }
395
396 //__________________________________________________________________________________________________
397 Int_t AliMagWrapCheb::FindTPCSegment(const Double_t *rpz) const 
398 {
399   // find the segment containing point xyz. If it is outside find the closest segment 
400   if (!fNParamsTPC) return -1;
401   int rid,pid,zid = TMath::BinarySearch(fNZSegTPC,fSegZTPC,(Float_t)rpz[2]); // find zsegment
402   //
403   Bool_t reCheck = kFALSE;
404   while(1) {
405     int psegBeg = fBegSegPTPC[zid];
406     //
407     for (pid=0;pid<fNSegPTPC[zid];pid++) if (rpz[1]<fSegPTPC[psegBeg+pid]) break;
408     if ( --pid < 0 ) pid = 0;
409     pid +=  psegBeg;
410     //
411     int rsegBeg = fBegSegRTPC[pid];
412     for (rid=0;rid<fNSegRTPC[pid];rid++) if (rpz[0]<fSegRTPC[rsegBeg+rid]) break;
413     if ( --rid < 0) rid = 0;
414     rid +=  rsegBeg;
415     //
416     // to make sure that due to the precision problems we did not pick the next Zbin    
417     if (!reCheck && (rpz[2] - fSegZSol[zid] < 3.e-5) && zid &&
418         !GetParamSol(fSegIDSol[rid])->IsInside(rpz)) {  // check the previous Z bin
419       zid--;
420       reCheck = kTRUE;
421       continue;
422     } 
423     break;
424   }
425   return fSegIDTPC[rid];
426 }
427
428 //__________________________________________________________________________________________________
429 Int_t AliMagWrapCheb::FindTPCRatSegment(const Double_t *rpz) const 
430 {
431   // find the segment containing point xyz. If it is outside find the closest segment 
432   if (!fNParamsTPCRat) return -1;
433   int rid,pid,zid = TMath::BinarySearch(fNZSegTPCRat,fSegZTPCRat,(Float_t)rpz[2]); // find zsegment
434   //
435   Bool_t reCheck = kFALSE;
436   while(1) {
437     int psegBeg = fBegSegPTPCRat[zid];
438     //
439     for (pid=0;pid<fNSegPTPCRat[zid];pid++) if (rpz[1]<fSegPTPCRat[psegBeg+pid]) break;
440     if ( --pid < 0 ) pid = 0;
441     pid +=  psegBeg;
442     //
443     int rsegBeg = fBegSegRTPCRat[pid];
444     for (rid=0;rid<fNSegRTPCRat[pid];rid++) if (rpz[0]<fSegRTPCRat[rsegBeg+rid]) break;
445     if ( --rid < 0) rid = 0;
446     rid +=  rsegBeg;
447     //
448     // to make sure that due to the precision problems we did not pick the next Zbin    
449     if (!reCheck && (rpz[2] - fSegZSol[zid] < 3.e-5) && zid &&
450         !GetParamSol(fSegIDSol[rid])->IsInside(rpz)) {  // check the previous Z bin
451       zid--;
452       reCheck = kTRUE;
453       continue;
454     } 
455     break;
456   }
457   return fSegIDTPCRat[rid];
458 }
459
460
461 //__________________________________________________________________________________________
462 void AliMagWrapCheb::GetTPCInt(const Double_t *xyz, Double_t *b) const
463 {
464   // compute TPC region field integral in cartesian coordinates.
465   // If point is outside of the parameterized region get it at closeset valid point
466   static Double_t rphiz[3];
467   //
468   if (!xyz || !b) {
469     AliError(Form("Wrong pointers: xyz:%p b:%p",xyz,b));
470     return;
471   }
472   // TPCInt region
473   // convert coordinates to cyl system
474   CartToCyl(xyz,rphiz);
475 #ifndef _BRING_TO_BOUNDARY_
476   if ( (rphiz[2]>GetMaxZTPCInt()||rphiz[2]<GetMinZTPCInt()) ||
477        rphiz[0]>GetMaxRTPCInt()) {for (int i=3;i--;) b[i]=0; return;}
478 #endif
479   //
480   GetTPCIntCyl(rphiz,b);
481   //
482   // convert field to cartesian system
483   CylToCartCylB(rphiz, b,b);
484   //
485 }
486
487 //__________________________________________________________________________________________
488 void AliMagWrapCheb::GetTPCRatInt(const Double_t *xyz, Double_t *b) const
489 {
490   // compute TPCRat region field integral in cartesian coordinates.
491   // If point is outside of the parameterized region get it at closeset valid point
492   static Double_t rphiz[3];
493   //
494   if (!xyz || !b) {
495     AliError(Form("Wrong pointers: xyz:%p b:%p",xyz,b));
496     return;
497   }
498   // TPCRatInt region
499   // convert coordinates to cyl system
500   CartToCyl(xyz,rphiz);
501 #ifndef _BRING_TO_BOUNDARY_
502   if ( (rphiz[2]>GetMaxZTPCRatInt()||rphiz[2]<GetMinZTPCRatInt()) ||
503        rphiz[0]>GetMaxRTPCRatInt()) {for (int i=3;i--;) b[i]=0; return;}
504 #endif
505   //
506   GetTPCRatIntCyl(rphiz,b);
507   //
508   // convert field to cartesian system
509   CylToCartCylB(rphiz, b,b);
510   //
511 }
512
513 //__________________________________________________________________________________________
514 void AliMagWrapCheb::FieldCylSol(const Double_t *rphiz, Double_t *b) const
515 {
516   // compute Solenoid field in Cylindircal coordinates
517   // note: if the point is outside the volume get the field in closest parameterized point
518   int id = FindSolSegment(rphiz);
519   if (id<0) return;
520   AliCheb3D* par = GetParamSol(id);
521 #ifndef _BRING_TO_BOUNDARY_  // exact matching to fitted volume is requested  
522   if (!par->IsInside(rphiz)) return;
523 #endif
524   par->Eval(rphiz,b);
525   return;
526   //
527 }
528
529 //__________________________________________________________________________________________
530 Double_t AliMagWrapCheb::FieldCylSolBz(const Double_t *rphiz) const
531 {
532   // compute Solenoid field in Cylindircal coordinates
533   // note: if the point is outside the volume get the field in closest parameterized point
534   int id = FindSolSegment(rphiz);
535   if (id<0) return 0.;
536   AliCheb3D* par = GetParamSol(id);
537 #ifndef _BRING_TO_BOUNDARY_  
538   return par->IsInside(rphiz) ? par->Eval(rphiz,2) : 0;
539 #endif
540   return par->Eval(rphiz,2);
541   //
542 }
543
544 //__________________________________________________________________________________________
545 void AliMagWrapCheb::GetTPCIntCyl(const Double_t *rphiz, Double_t *b) const
546 {
547   // compute field integral in TPC region in Cylindircal coordinates
548   // note: the check for the point being inside the parameterized region is done outside
549   int id = FindTPCSegment(rphiz);
550   if (id<0) {
551     b[0] = b[1] = b[2] = 0;
552     return;
553   }
554   if (id>=fNParamsTPC) {
555     AliError(Form("Wrong TPCParam segment %d",id));
556     b[0] = b[1] = b[2] = 0;
557     return;
558   }
559   AliCheb3D* par = GetParamTPCInt(id);
560   if (par->IsInside(rphiz)) {
561     par->Eval(rphiz,b); 
562     return;
563   }
564   b[0] = b[1] = b[2] = 0;
565   return;
566   //
567 }
568
569 //__________________________________________________________________________________________
570 void AliMagWrapCheb::GetTPCRatIntCyl(const Double_t *rphiz, Double_t *b) const
571 {
572   // compute field integral in TPCRat region in Cylindircal coordinates
573   // note: the check for the point being inside the parameterized region is done outside
574   int id = FindTPCRatSegment(rphiz);
575   if (id<0) {
576     b[0] = b[1] = b[2] = 0;
577     return;
578   }
579   if (id>=fNParamsTPCRat) {
580     AliError(Form("Wrong TPCRatParam segment %d",id));
581     b[0] = b[1] = b[2] = 0;
582     return;
583   }
584   AliCheb3D* par = GetParamTPCRatInt(id);
585   if (par->IsInside(rphiz)) {
586     par->Eval(rphiz,b); 
587     return;
588   }
589   b[0] = b[1] = b[2] = 0;
590   return;
591   //
592 }
593
594
595 #ifdef  _INC_CREATION_ALICHEB3D_
596 //_______________________________________________
597 void AliMagWrapCheb::LoadData(const char* inpfile)
598 {
599   // read coefficients data from the text file
600   //
601   TString strf = inpfile;
602   gSystem->ExpandPathName(strf);
603   FILE* stream = fopen(strf,"r");
604   if (!stream) {
605     printf("Did not find input file %s\n",strf.Data());
606     return;
607   }
608   //
609   TString buffs;
610   AliCheb3DCalc::ReadLine(buffs,stream);
611   if (!buffs.BeginsWith("START")) {
612     Error("LoadData","Expected: \"START <name>\", found \"%s\"\nStop\n",buffs.Data());
613     exit(1);
614   }
615   if (buffs.First(' ')>0) SetName(buffs.Data()+buffs.First(' ')+1);
616   //
617   // Solenoid part    -----------------------------------------------------------
618   AliCheb3DCalc::ReadLine(buffs,stream);
619   if (!buffs.BeginsWith("START SOLENOID")) {
620     Error("LoadData","Expected: \"START SOLENOID\", found \"%s\"\nStop\n",buffs.Data());
621     exit(1);
622   }
623   AliCheb3DCalc::ReadLine(buffs,stream); // nparam
624   int nparSol = buffs.Atoi(); 
625   //
626   for (int ip=0;ip<nparSol;ip++) {
627     AliCheb3D* cheb = new AliCheb3D();
628     cheb->LoadData(stream);
629     AddParamSol(cheb);
630   }
631   //
632   AliCheb3DCalc::ReadLine(buffs,stream);
633   if (!buffs.BeginsWith("END SOLENOID")) {
634     Error("LoadData","Expected \"END SOLENOID\", found \"%s\"\nStop\n",buffs.Data());
635     exit(1);
636   }
637   //
638   // TPCInt part     -----------------------------------------------------------
639   AliCheb3DCalc::ReadLine(buffs,stream);
640   if (!buffs.BeginsWith("START TPCINT")) {
641     Error("LoadData","Expected: \"START TPCINT\", found \"%s\"\nStop\n",buffs.Data());
642     exit(1);
643   }
644   AliCheb3DCalc::ReadLine(buffs,stream); // nparam
645   int nparTPCInt = buffs.Atoi(); 
646   //
647   for (int ip=0;ip<nparTPCInt;ip++) {
648     AliCheb3D* cheb = new AliCheb3D();
649     cheb->LoadData(stream);
650     AddParamTPCInt(cheb);
651   }
652   //
653   AliCheb3DCalc::ReadLine(buffs,stream);
654   if (!buffs.BeginsWith("END TPCINT")) {
655     Error("LoadData","Expected \"END TPCINT\", found \"%s\"\nStop\n",buffs.Data());
656     exit(1);
657   }
658   //
659   // TPCRatInt part     -----------------------------------------------------------
660   AliCheb3DCalc::ReadLine(buffs,stream);
661   if (!buffs.BeginsWith("START TPCRatINT")) {
662     Error("LoadData","Expected: \"START TPCRatINT\", found \"%s\"\nStop\n",buffs.Data());
663     exit(1);
664   }
665   AliCheb3DCalc::ReadLine(buffs,stream); // nparam
666   int nparTPCRatInt = buffs.Atoi(); 
667   //
668   for (int ip=0;ip<nparTPCRatInt;ip++) {
669     AliCheb3D* cheb = new AliCheb3D();
670     cheb->LoadData(stream);
671     AddParamTPCRatInt(cheb);
672   }
673   //
674   AliCheb3DCalc::ReadLine(buffs,stream);
675   if (!buffs.BeginsWith("END TPCRatINT")) {
676     Error("LoadData","Expected \"END TPCRatINT\", found \"%s\"\nStop\n",buffs.Data());
677     exit(1);
678   }
679   //
680   // Dipole part    -----------------------------------------------------------
681   AliCheb3DCalc::ReadLine(buffs,stream);
682   if (!buffs.BeginsWith("START DIPOLE")) {
683     Error("LoadData","Expected: \"START DIPOLE\", found \"%s\"\nStop\n",buffs.Data());
684     exit(1);
685   }
686   AliCheb3DCalc::ReadLine(buffs,stream); // nparam
687   int nparDip = buffs.Atoi();  
688   //
689   for (int ip=0;ip<nparDip;ip++) {
690     AliCheb3D* cheb = new AliCheb3D();
691     cheb->LoadData(stream);
692     AddParamDip(cheb);
693   }
694   //
695   AliCheb3DCalc::ReadLine(buffs,stream);
696   if (!buffs.BeginsWith("END DIPOLE")) {
697     Error("LoadData","Expected \"END DIPOLE\", found \"%s\"\nStop\n",GetName(),buffs.Data());
698     exit(1);
699   }
700   //
701   AliCheb3DCalc::ReadLine(buffs,stream);
702   if (!buffs.BeginsWith("END") || !buffs.Contains(GetName())) {
703     Error("LoadData","Expected: \"END %s\", found \"%s\"\nStop\n",GetName(),buffs.Data());
704     exit(1);
705   }
706   //
707   // ---------------------------------------------------------------------------
708   fclose(stream);
709   BuildTableSol();
710   BuildTableDip();
711   BuildTableTPCInt();
712   BuildTableTPCRatInt();
713   //
714   printf("Loaded magnetic field \"%s\" from %s\n",GetName(),strf.Data());
715   //
716 }
717
718 //__________________________________________________________________________________________
719 void AliMagWrapCheb::BuildTableSol()
720 {
721   BuildTable(fNParamsSol,fParamsSol,
722              fNZSegSol,fNPSegSol,fNRSegSol,
723              fMinZSol,fMaxZSol, 
724              &fSegZSol,&fSegPSol,&fSegRSol,
725              &fBegSegPSol,&fNSegPSol,
726              &fBegSegRSol,&fNSegRSol, 
727              &fSegIDSol);
728 }
729
730 //__________________________________________________________________________________________
731 void AliMagWrapCheb::BuildTableDip()
732 {
733   BuildTable(fNParamsDip,fParamsDip,
734              fNZSegDip,fNYSegDip,fNXSegDip,
735              fMinZDip,fMaxZDip, 
736              &fSegZDip,&fSegYDip,&fSegXDip,
737              &fBegSegYDip,&fNSegYDip,
738              &fBegSegXDip,&fNSegXDip, 
739              &fSegIDDip);
740 }
741
742 //__________________________________________________________________________________________
743 void AliMagWrapCheb::BuildTableTPCInt()
744 {
745   BuildTable(fNParamsTPC,fParamsTPC,
746              fNZSegTPC,fNPSegTPC,fNRSegTPC,
747              fMinZTPC,fMaxZTPC, 
748              &fSegZTPC,&fSegPTPC,&fSegRTPC,
749              &fBegSegPTPC,&fNSegPTPC,
750              &fBegSegRTPC,&fNSegRTPC, 
751              &fSegIDTPC);
752 }
753
754 //__________________________________________________________________________________________
755 void AliMagWrapCheb::BuildTableTPCRatInt()
756 {
757   BuildTable(fNParamsTPCRat,fParamsTPCRat,
758              fNZSegTPCRat,fNPSegTPCRat,fNRSegTPCRat,
759              fMinZTPCRat,fMaxZTPCRat, 
760              &fSegZTPCRat,&fSegPTPCRat,&fSegRTPCRat,
761              &fBegSegPTPCRat,&fNSegPTPCRat,
762              &fBegSegRTPCRat,&fNSegRTPCRat, 
763              &fSegIDTPCRat);
764 }
765
766 #endif
767
768 //_______________________________________________
769 #ifdef  _INC_CREATION_ALICHEB3D_
770
771 //__________________________________________________________________________________________
772 AliMagWrapCheb::AliMagWrapCheb(const char* inputFile) : 
773   fNParamsSol(0),fNZSegSol(0),fNPSegSol(0),fNRSegSol(0),
774   fSegZSol(0),fSegPSol(0),fSegRSol(0),
775   fBegSegPSol(0),fNSegPSol(0),fBegSegRSol(0),fNSegRSol(0),fSegIDSol(0),fMinZSol(1.e6),fMaxZSol(-1.e6),fParamsSol(0),fMaxRSol(0),
776 //
777   fNParamsTPC(0),fNZSegTPC(0),fNPSegTPC(0),fNRSegTPC(0),
778   fSegZTPC(0),fSegPTPC(0),fSegRTPC(0),
779   fBegSegPTPC(0),fNSegPTPC(0),fBegSegRTPC(0),fNSegRTPC(0),fSegIDTPC(0),fMinZTPC(1.e6),fMaxZTPC(-1.e6),fParamsTPC(0),fMaxRTPC(0),
780 //
781   fNParamsTPCRat(0),fNZSegTPCRat(0),fNPSegTPCRat(0),fNRSegTPCRat(0),
782   fSegZTPCRat(0),fSegPTPCRat(0),fSegRTPCRat(0),
783   fBegSegPTPCRat(0),fNSegPTPCRat(0),fBegSegRTPCRat(0),fNSegRTPCRat(0),fSegIDTPCRat(0),fMinZTPCRat(1.e6),fMaxZTPCRat(-1.e6),fParamsTPCRat(0),fMaxRTPCRat(0),
784 //
785   fNParamsDip(0),fNZSegDip(0),fNYSegDip(0),fNXSegDip(0),
786   fSegZDip(0),fSegYDip(0),fSegXDip(0),
787   fBegSegYDip(0),fNSegYDip(0),fBegSegXDip(0),fNSegXDip(0),fSegIDDip(0),fMinZDip(1.e6),fMaxZDip(-1.e6),fParamsDip(0)
788 //
789 {
790   // construct from coeffs from the text file
791   LoadData(inputFile);
792 }
793
794 //__________________________________________________________________________________________
795 void AliMagWrapCheb::AddParamSol(const AliCheb3D* param)
796 {
797   // adds new parameterization piece for Sol
798   // NOTE: pieces must be added strictly in increasing R then increasing Z order
799   //
800   if (!fParamsSol) fParamsSol = new TObjArray();
801   fParamsSol->Add( (AliCheb3D*)param );
802   fNParamsSol++;
803   if (fMaxRSol<param->GetBoundMax(0)) fMaxRSol = param->GetBoundMax(0);
804   //
805 }
806
807 //__________________________________________________________________________________________
808 void AliMagWrapCheb::AddParamTPCInt(const AliCheb3D* param)
809 {
810   // adds new parameterization piece for TPCInt
811   // NOTE: pieces must be added strictly in increasing R then increasing Z order
812   //
813   if (!fParamsTPC) fParamsTPC = new TObjArray();
814   fParamsTPC->Add( (AliCheb3D*)param);
815   fNParamsTPC++;
816   if (fMaxRTPC<param->GetBoundMax(0)) fMaxRTPC = param->GetBoundMax(0);
817   //
818 }
819
820 //__________________________________________________________________________________________
821 void AliMagWrapCheb::AddParamTPCRatInt(const AliCheb3D* param)
822 {
823   // adds new parameterization piece for TPCRatInt
824   // NOTE: pieces must be added strictly in increasing R then increasing Z order
825   //
826   if (!fParamsTPCRat) fParamsTPCRat = new TObjArray();
827   fParamsTPCRat->Add( (AliCheb3D*)param);
828   fNParamsTPCRat++;
829   if (fMaxRTPCRat<param->GetBoundMax(0)) fMaxRTPCRat = param->GetBoundMax(0);
830   //
831 }
832
833 //__________________________________________________________________________________________
834 void AliMagWrapCheb::AddParamDip(const AliCheb3D* param)
835 {
836   // adds new parameterization piece for Dipole
837   //
838   if (!fParamsDip) fParamsDip = new TObjArray();
839   fParamsDip->Add( (AliCheb3D*)param);
840   fNParamsDip++;
841   //
842 }
843
844 //__________________________________________________________________________________________
845 void AliMagWrapCheb::ResetTPCInt()
846 {
847   // clean TPC field integral (used for update)
848   if (fNParamsTPC) {
849     delete   fParamsTPC;  fParamsTPC = 0;
850     delete[] fSegZTPC;    fSegZTPC   = 0;
851     delete[] fSegPTPC;    fSegPTPC   = 0;
852     delete[] fSegRTPC;    fSegRTPC   = 0;
853     delete[] fBegSegPTPC; fBegSegPTPC = 0;
854     delete[] fNSegPTPC;   fNSegPTPC   = 0;
855     delete[] fBegSegRTPC; fBegSegRTPC = 0;
856     delete[] fNSegRTPC;   fNSegRTPC   = 0;
857     delete[] fSegIDTPC;   fSegIDTPC   = 0;   
858   }
859   fNParamsTPC = fNZSegTPC = fNPSegTPC = fNRSegTPC = 0;
860   fMinZTPC = 1e6;
861   fMaxZTPC = -1e6;
862   fMaxRTPC = 0;
863   //
864 }
865
866 //__________________________________________________________________________________________
867 void AliMagWrapCheb::ResetTPCRatInt()
868 {
869   // clean TPCRat field integral (used for update)
870   if (fNParamsTPCRat) {
871     delete   fParamsTPCRat;  fParamsTPCRat = 0;
872     delete[] fSegZTPCRat;    fSegZTPCRat   = 0;
873     delete[] fSegPTPCRat;    fSegPTPCRat   = 0;
874     delete[] fSegRTPCRat;    fSegRTPCRat   = 0;
875     delete[] fBegSegPTPCRat; fBegSegPTPCRat = 0;
876     delete[] fNSegPTPCRat;   fNSegPTPCRat   = 0;
877     delete[] fBegSegRTPCRat; fBegSegRTPCRat = 0;
878     delete[] fNSegRTPCRat;   fNSegRTPCRat   = 0;
879     delete[] fSegIDTPCRat;   fSegIDTPCRat   = 0;   
880   }
881   fNParamsTPCRat = fNZSegTPCRat = fNPSegTPCRat = fNRSegTPCRat = 0;
882   fMinZTPCRat = 1e6;
883   fMaxZTPCRat = -1e6;
884   fMaxRTPCRat = 0;
885   //
886 }
887
888
889 //__________________________________________________
890 void AliMagWrapCheb::BuildTable(Int_t npar,TObjArray *parArr, Int_t &nZSeg, Int_t &nYSeg, Int_t &nXSeg,
891                                 Float_t &minZ,Float_t &maxZ,
892                                 Float_t **segZ,Float_t **segY,Float_t **segX,
893                                 Int_t **begSegY,Int_t **nSegY,
894                                 Int_t **begSegX,Int_t **nSegX,
895                                 Int_t **segID)
896 {
897   // build lookup table for dipole
898   //
899   if (npar<1) return;
900   TArrayF segYArr,segXArr;
901   TArrayI begSegYDipArr,begSegXDipArr;
902   TArrayI nSegYDipArr,nSegXDipArr;
903   TArrayI segIDArr;
904   float *tmpSegZ,*tmpSegY,*tmpSegX;
905   //
906   // create segmentation in Z
907   nZSeg = SegmentDimension(&tmpSegZ, parArr, npar, 2, 1,-1, 1,-1, 1,-1) - 1;
908   nYSeg = 0;
909   nXSeg = 0;
910   //
911   // for each Z slice create segmentation in Y
912   begSegYDipArr.Set(nZSeg);
913   nSegYDipArr.Set(nZSeg);
914   float xyz[3];
915   for (int iz=0;iz<nZSeg;iz++) {
916     printf("\nZSegment#%d  %+e : %+e\n",iz,tmpSegZ[iz],tmpSegZ[iz+1]);
917     int ny = SegmentDimension(&tmpSegY, parArr, npar, 1, 
918                               1,-1, 1,-1, tmpSegZ[iz],tmpSegZ[iz+1]) - 1;
919     segYArr.Set(ny + nYSeg);
920     for (int iy=0;iy<ny;iy++) segYArr[nYSeg+iy] = tmpSegY[iy];
921     begSegYDipArr[iz] = nYSeg;
922     nSegYDipArr[iz] = ny;
923     printf(" Found %d YSegments, to start from %d\n",ny, begSegYDipArr[iz]);
924     //
925     // for each slice in Z and Y create segmentation in X
926     begSegXDipArr.Set(nYSeg+ny);
927     nSegXDipArr.Set(nYSeg+ny);
928     xyz[2] = (tmpSegZ[iz]+tmpSegZ[iz+1])/2.; // mean Z of this segment
929     //
930     for (int iy=0;iy<ny;iy++) {
931       int isg = nYSeg+iy;
932       printf("\n   YSegment#%d  %+e : %+e\n",iy, tmpSegY[iy],tmpSegY[iy+1]);
933       int nx = SegmentDimension(&tmpSegX, parArr, npar, 0, 
934                                 1,-1, tmpSegY[iy],tmpSegY[iy+1], tmpSegZ[iz],tmpSegZ[iz+1]) - 1;
935       //
936       segXArr.Set(nx + nXSeg);
937       for (int ix=0;ix<nx;ix++) segXArr[nXSeg+ix] = tmpSegX[ix];
938       begSegXDipArr[isg] = nXSeg;
939       nSegXDipArr[isg] = nx;
940       printf("   Found %d XSegments, to start from %d\n",nx, begSegXDipArr[isg]);
941       //
942       segIDArr.Set(nXSeg+nx);
943       //
944       // find corresponding params
945       xyz[1] = (tmpSegY[iy]+tmpSegY[iy+1])/2.; // mean Y of this segment
946       //
947       for (int ix=0;ix<nx;ix++) {
948         xyz[0] = (tmpSegX[ix]+tmpSegX[ix+1])/2.; // mean X of this segment
949         for (int ipar=0;ipar<npar;ipar++) {
950           AliCheb3D* cheb = (AliCheb3D*) parArr->At(ipar);
951           if (!cheb->IsInside(xyz)) continue;
952           segIDArr[nXSeg+ix] = ipar;
953           break;
954         }
955       }
956       nXSeg += nx;
957       //
958       delete[] tmpSegX;
959     }
960     delete[] tmpSegY;
961     nYSeg += ny;
962   }
963   //
964   minZ = tmpSegZ[0];
965   maxZ = tmpSegZ[nZSeg];
966   (*segZ)  = new Float_t[nZSeg];
967   for (int i=nZSeg;i--;) (*segZ)[i] = tmpSegZ[i];
968   delete[] tmpSegZ;
969   //
970   (*segY)    = new Float_t[nYSeg];
971   (*segX)    = new Float_t[nXSeg];
972   (*begSegY) = new Int_t[nZSeg];
973   (*nSegY)   = new Int_t[nZSeg];
974   (*begSegX) = new Int_t[nYSeg];
975   (*nSegX)   = new Int_t[nYSeg];
976   (*segID)   = new Int_t[nXSeg];
977   //
978   for (int i=nYSeg;i--;) (*segY)[i] = segYArr[i];
979   for (int i=nXSeg;i--;) (*segX)[i] = segXArr[i];
980   for (int i=nZSeg;i--;) {(*begSegY)[i] = begSegYDipArr[i]; (*nSegY)[i] = nSegYDipArr[i];}
981   for (int i=nYSeg;i--;) {(*begSegX)[i] = begSegXDipArr[i]; (*nSegX)[i] = nSegXDipArr[i];}
982   for (int i=nXSeg;i--;) {(*segID)[i]   = segIDArr[i];}
983   //
984 }
985
986 /*
987 //__________________________________________________
988 void AliMagWrapCheb::BuildTableDip()
989 {
990   // build lookup table for dipole
991   //
992   if (fNParamsDip<1) return;
993   TArrayF segY,segX;
994   TArrayI begSegYDip,begSegXDip;
995   TArrayI nsegYDip,nsegXDip;
996   TArrayI segID;
997   float *tmpSegZ,*tmpSegY,*tmpSegX;
998   //
999   // create segmentation in Z
1000   fNZSegDip = SegmentDimension(&tmpSegZ, fParamsDip, fNParamsDip, 2, 1,-1, 1,-1, 1,-1) - 1;
1001   fNYSegDip = 0;
1002   fNXSegDip = 0;
1003   //
1004   // for each Z slice create segmentation in Y
1005   begSegYDip.Set(fNZSegDip);
1006   nsegYDip.Set(fNZSegDip);
1007   float xyz[3];
1008   for (int iz=0;iz<fNZSegDip;iz++) {
1009     printf("\nZSegment#%d  %+e : %+e\n",iz,tmpSegZ[iz],tmpSegZ[iz+1]);
1010     int ny = SegmentDimension(&tmpSegY, fParamsDip, fNParamsDip, 1, 
1011                                  1,-1, 1,-1, tmpSegZ[iz],tmpSegZ[iz+1]) - 1;
1012     segY.Set(ny + fNYSegDip);
1013     for (int iy=0;iy<ny;iy++) segY[fNYSegDip+iy] = tmpSegY[iy];
1014     begSegYDip[iz] = fNYSegDip;
1015     nsegYDip[iz] = ny;
1016     printf(" Found %d YSegments, to start from %d\n",ny, begSegYDip[iz]);
1017     //
1018     // for each slice in Z and Y create segmentation in X
1019     begSegXDip.Set(fNYSegDip+ny);
1020     nsegXDip.Set(fNYSegDip+ny);
1021     xyz[2] = (tmpSegZ[iz]+tmpSegZ[iz+1])/2.; // mean Z of this segment
1022     //
1023     for (int iy=0;iy<ny;iy++) {
1024       int isg = fNYSegDip+iy;
1025       printf("\n   YSegment#%d  %+e : %+e\n",iy, tmpSegY[iy],tmpSegY[iy+1]);
1026       int nx = SegmentDimension(&tmpSegX, fParamsDip, fNParamsDip, 0, 
1027                                 1,-1, tmpSegY[iy],tmpSegY[iy+1], tmpSegZ[iz],tmpSegZ[iz+1]) - 1;
1028       //
1029       segX.Set(nx + fNXSegDip);
1030       for (int ix=0;ix<nx;ix++) segX[fNXSegDip+ix] = tmpSegX[ix];
1031       begSegXDip[isg] = fNXSegDip;
1032       nsegXDip[isg] = nx;
1033       printf("   Found %d XSegments, to start from %d\n",nx, begSegXDip[isg]);
1034       //
1035       segID.Set(fNXSegDip+nx);
1036       //
1037       // find corresponding params
1038       xyz[1] = (tmpSegY[iy]+tmpSegY[iy+1])/2.; // mean Y of this segment
1039       //
1040       for (int ix=0;ix<nx;ix++) {
1041         xyz[0] = (tmpSegX[ix]+tmpSegX[ix+1])/2.; // mean X of this segment
1042         for (int ipar=0;ipar<fNParamsDip;ipar++) {
1043           AliCheb3D* cheb = (AliCheb3D*) fParamsDip->At(ipar);
1044           if (!cheb->IsInside(xyz)) continue;
1045           segID[fNXSegDip+ix] = ipar;
1046           break;
1047         }
1048       }
1049       fNXSegDip += nx;
1050       //
1051       delete[] tmpSegX;
1052     }
1053     delete[] tmpSegY;
1054     fNYSegDip += ny;
1055   }
1056   //
1057   fMinZDip = tmpSegZ[0];
1058   fMaxZDip = tmpSegZ[fNZSegDip];
1059   fSegZDip    = new Float_t[fNZSegDip];
1060   for (int i=fNZSegDip;i--;) fSegZDip[i] = tmpSegZ[i];
1061   delete[] tmpSegZ;
1062   //
1063   fSegYDip    = new Float_t[fNYSegDip];
1064   fSegXDip    = new Float_t[fNXSegDip];
1065   fBegSegYDip = new Int_t[fNZSegDip];
1066   fNSegYDip   = new Int_t[fNZSegDip];
1067   fBegSegXDip = new Int_t[fNYSegDip];
1068   fNSegXDip   = new Int_t[fNYSegDip];
1069   fSegIDDip   = new Int_t[fNXSegDip];
1070   //
1071   for (int i=fNYSegDip;i--;) fSegYDip[i] = segY[i];
1072   for (int i=fNXSegDip;i--;) fSegXDip[i] = segX[i];
1073   for (int i=fNZSegDip;i--;) {fBegSegYDip[i] = begSegYDip[i]; fNSegYDip[i] = nsegYDip[i];}
1074   for (int i=fNYSegDip;i--;) {fBegSegXDip[i] = begSegXDip[i]; fNSegXDip[i] = nsegXDip[i];}
1075   for (int i=fNXSegDip;i--;) {fSegIDDip[i]   = segID[i];}
1076   //
1077 }
1078 */
1079
1080 //________________________________________________________________
1081 void AliMagWrapCheb::SaveData(const char* outfile) const
1082 {
1083   // writes coefficients data to output text file
1084   TString strf = outfile;
1085   gSystem->ExpandPathName(strf);
1086   FILE* stream = fopen(strf,"w+");
1087   //
1088   // Sol part    ---------------------------------------------------------
1089   fprintf(stream,"# Set of Chebyshev parameterizations for ALICE magnetic field\nSTART %s\n",GetName());
1090   fprintf(stream,"START SOLENOID\n#Number of pieces\n%d\n",fNParamsSol);
1091   for (int ip=0;ip<fNParamsSol;ip++) GetParamSol(ip)->SaveData(stream);
1092   fprintf(stream,"#\nEND SOLENOID\n");
1093   //
1094   // TPCInt part ---------------------------------------------------------
1095   fprintf(stream,"# Set of Chebyshev parameterizations for ALICE magnetic field\nSTART %s\n",GetName());
1096   fprintf(stream,"START TPCINT\n#Number of pieces\n%d\n",fNParamsTPC);
1097   for (int ip=0;ip<fNParamsTPC;ip++) GetParamTPCInt(ip)->SaveData(stream);
1098   fprintf(stream,"#\nEND TPCINT\n");
1099   //
1100   // TPCRatInt part ---------------------------------------------------------
1101   fprintf(stream,"# Set of Chebyshev parameterizations for ALICE magnetic field\nSTART %s\n",GetName());
1102   fprintf(stream,"START TPCRatINT\n#Number of pieces\n%d\n",fNParamsTPCRat);
1103   for (int ip=0;ip<fNParamsTPCRat;ip++) GetParamTPCRatInt(ip)->SaveData(stream);
1104   fprintf(stream,"#\nEND TPCRatINT\n");
1105   //
1106   // Dip part   ---------------------------------------------------------
1107   fprintf(stream,"START DIPOLE\n#Number of pieces\n%d\n",fNParamsDip);
1108   for (int ip=0;ip<fNParamsDip;ip++) GetParamDip(ip)->SaveData(stream);
1109   fprintf(stream,"#\nEND DIPOLE\n");
1110   //
1111   fprintf(stream,"#\nEND %s\n",GetName());
1112   //
1113   fclose(stream);
1114   //
1115 }
1116
1117 Int_t AliMagWrapCheb::SegmentDimension(float** seg,const TObjArray* par,int npar, int dim, 
1118                                        float xmn,float xmx,float ymn,float ymx,float zmn,float zmx)
1119 {
1120   // find all boundaries in deimension dim for boxes in given region.
1121   // if mn>mx for given projection the check is not done for it.
1122   float *tmpC = new float[2*npar];
1123   int *tmpInd = new int[2*npar];
1124   int nseg0 = 0;
1125   for (int ip=0;ip<npar;ip++) {
1126     AliCheb3D* cheb = (AliCheb3D*) par->At(ip);
1127     if (xmn<xmx && (cheb->GetBoundMin(0)>(xmx+xmn)/2 || cheb->GetBoundMax(0)<(xmn+xmx)/2)) continue;
1128     if (ymn<ymx && (cheb->GetBoundMin(1)>(ymx+ymn)/2 || cheb->GetBoundMax(1)<(ymn+ymx)/2)) continue;
1129     if (zmn<zmx && (cheb->GetBoundMin(2)>(zmx+zmn)/2 || cheb->GetBoundMax(2)<(zmn+zmx)/2)) continue;
1130     //
1131     tmpC[nseg0++] = cheb->GetBoundMin(dim);
1132     tmpC[nseg0++] = cheb->GetBoundMax(dim);    
1133   }
1134   // range Dim's boundaries in increasing order
1135   TMath::Sort(nseg0,tmpC,tmpInd,kFALSE);
1136   // count number of really different Z's
1137   int nseg = 0;
1138   float cprev = -1e6;
1139   for (int ip=0;ip<nseg0;ip++) {
1140     if (TMath::Abs(cprev-tmpC[ tmpInd[ip] ])>1e-4) {
1141       cprev = tmpC[ tmpInd[ip] ];
1142       nseg++;
1143     }
1144     else tmpInd[ip] = -1; // supress redundant Z
1145   }
1146   // 
1147   *seg  = new float[nseg]; // create final Z segmenations
1148   nseg = 0;
1149   for (int ip=0;ip<nseg0;ip++) if (tmpInd[ip]>=0) (*seg)[nseg++] = tmpC[ tmpInd[ip] ];
1150   //
1151   delete[] tmpC;
1152   delete[] tmpInd;
1153   return nseg;
1154 }
1155
1156 #endif
1157