]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliMagWrapCheb.cxx
MakeImage is now a method of AliCheckerBase, was AliQADataMaker before. This will...
[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 <TSystem.h>
18 #include <TArrayF.h>
19 #include <TArrayI.h>
20
21 ClassImp(AliMagWrapCheb)
22
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),
28 //
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),
32 //
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)
36 //
37 {
38   // default constructor
39 }
40
41 //__________________________________________________________________________________________
42 AliMagWrapCheb::AliMagWrapCheb(const AliMagWrapCheb& src) : 
43   TNamed(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),
47 //
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),
51 //
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)
55 {
56   // copy constructor
57   CopyFrom(src);
58 }
59
60 //__________________________________________________________________________________________
61 void AliMagWrapCheb::CopyFrom(const AliMagWrapCheb& src) 
62
63   // copy method
64   Clear();
65   SetName(src.GetName());
66   SetTitle(src.GetTitle());
67   //
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);
86   }
87   //
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);
106   }
107   //
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);
125   }
126   //
127 }
128
129 //__________________________________________________________________________________________
130 AliMagWrapCheb& AliMagWrapCheb::operator=(const AliMagWrapCheb& rhs)
131 {
132   // assignment
133   if (this != &rhs) {  
134     Clear();
135     CopyFrom(rhs);
136   }
137   return *this;  
138   //
139 }
140
141 //__________________________________________________________________________________________
142 void AliMagWrapCheb::Clear(const Option_t *)
143 {
144   // clear all dynamic parts
145   if (fNParamsSol) {
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;   
155   }
156   fNParamsSol = fNZSegSol = fNPSegSol = fNRSegSol = 0;
157   fMinZSol = 1e6;
158   fMaxZSol = -1e6;
159   fMaxRSol = 0;
160   //
161   if (fNParamsTPC) {
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;   
171   }
172   fNParamsTPC = fNZSegTPC = fNPSegTPC = fNRSegTPC = 0;
173   fMinZTPC = 1e6;
174   fMaxZTPC = -1e6;
175   fMaxRTPC = 0;
176   //
177   if (fNParamsDip) {
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;
187   }
188   fNParamsDip = fNZSegDip = fNYSegDip = fNXSegDip = 0;
189   fMinZDip = 1e6;
190   fMaxZDip = -1e6;
191   //
192 }
193
194 //__________________________________________________________________________________________
195 void AliMagWrapCheb::Field(const Double_t *xyz, Double_t *b) const
196 {
197   // compute field in cartesian coordinates. If point is outside of the parameterized region
198   // get it at closest valid point
199   Double_t rphiz[3];
200   //
201 #ifndef _BRING_TO_BOUNDARY_  // exact matching to fitted volume is requested
202   b[0] = b[1] = b[2] = 0;
203 #endif
204   //
205   if (xyz[2]>fMinZSol) {
206     CartToCyl(xyz,rphiz);
207     FieldCylSol(rphiz,b);
208     // convert field to cartesian system
209     CylToCartCylB(rphiz, b,b);  
210     return;
211   }
212   //
213   int iddip = FindDipSegment(xyz);
214   if (iddip<0) return;
215   AliCheb3D* par = GetParamDip(iddip);
216 #ifndef _BRING_TO_BOUNDARY_
217   if (!par->IsInside(xyz)) return;
218 #endif
219   par->Eval(xyz,b); 
220   //
221 }
222
223 //__________________________________________________________________________________________
224 Double_t AliMagWrapCheb::GetBz(const Double_t *xyz) const
225 {
226   // compute Bz for the point in cartesian coordinates. If point is outside of the parameterized region
227   // get it at closest valid point
228   Double_t rphiz[3];
229   //
230   if (xyz[2]>fMinZSol) {
231     CartToCyl(xyz,rphiz);
232     return FieldCylSolBz(rphiz);
233   }
234   //
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.;
240 #endif
241   return par->Eval(xyz,2);
242 }
243
244
245 //__________________________________________________________________________________________
246 void AliMagWrapCheb::Print(Option_t *) const
247 {
248   // print info
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);
251   //
252   if (fParamsSol) {
253     for (int i=0;i<fNParamsSol;i++) {
254       printf("SOL%4d ",i);
255       GetParamSol(i)->Print();
256     }
257   }
258   //
259   printf("Segmentation for TPC field integral (%+.2f<Z<%+.2f cm | R<%.2f cm)\n",fMinZTPC,fMaxZTPC,fMaxRTPC);
260   //
261   if (fParamsTPC) {
262     for (int i=0;i<fNParamsTPC;i++) {
263       printf("TPC%4d ",i);
264       GetParamTPCInt(i)->Print();
265     }
266   }
267   //
268   printf("Segmentation for Dipole (%+.2f<Z<%+.2f cm)\n",fMinZDip,fMaxZDip);
269   if (fParamsDip) {
270     for (int i=0;i<fNParamsDip;i++) {
271       printf("DIP%4d ",i);
272       GetParamDip(i)->Print();
273     }
274   }
275   //
276 }
277
278 //__________________________________________________________________________________________________
279 Int_t AliMagWrapCheb::FindDipSegment(const Double_t *xyz) const 
280 {
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];
285   //
286   for (yid=0;yid<fNSegYDip[zid];yid++) if (xyz[1]<fSegYDip[ysegBeg+yid]) break;
287   if ( --yid < 0 ) yid = 0;
288   yid +=  ysegBeg;
289   //
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;
293   xid +=  xsegBeg;
294   //
295   return fSegIDDip[xid];
296 }
297
298 //__________________________________________________________________________________________________
299 Int_t AliMagWrapCheb::FindSolSegment(const Double_t *rpz) const 
300 {
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];
305   //
306   for (pid=0;pid<fNSegPSol[zid];pid++) if (rpz[1]<fSegPSol[psegBeg+pid]) break;
307   if ( --pid < 0 ) pid = 0;
308   pid +=  psegBeg;
309   //
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;
313   rid +=  rsegBeg;
314   //
315   return fSegIDSol[rid];
316 }
317
318 //__________________________________________________________________________________________________
319 Int_t AliMagWrapCheb::FindTPCSegment(const Double_t *rpz) const 
320 {
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];
325   //
326   for (pid=0;pid<fNSegPTPC[zid];pid++) if (rpz[1]<fSegPTPC[psegBeg+pid]) break;
327   if ( --pid < 0 ) pid = 0;
328   pid +=  psegBeg;
329   //
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;
333   rid +=  rsegBeg;
334   //
335   return fSegIDTPC[rid];
336 }
337
338
339 //__________________________________________________________________________________________
340 void AliMagWrapCheb::GetTPCInt(const Double_t *xyz, Double_t *b) const
341 {
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];
345   //
346   // TPCInt region
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;}
352 #endif
353   //
354   GetTPCIntCyl(rphiz,b);
355   //
356   // convert field to cartesian system
357   CylToCartCylB(rphiz, b,b);
358   //
359 }
360
361 //__________________________________________________________________________________________
362 void AliMagWrapCheb::FieldCylSol(const Double_t *rphiz, Double_t *b) const
363 {
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);
367   if (id<0) return;
368   AliCheb3D* par = GetParamSol(id);
369 #ifndef _BRING_TO_BOUNDARY_  // exact matching to fitted volume is requested  
370   if (!par->IsInside(rphiz)) return;
371 #endif
372   par->Eval(rphiz,b);
373   return;
374   //
375 }
376
377 //__________________________________________________________________________________________
378 Double_t AliMagWrapCheb::FieldCylSolBz(const Double_t *rphiz) const
379 {
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);
383   if (id<0) return 0.;
384   AliCheb3D* par = GetParamSol(id);
385 #ifndef _BRING_TO_BOUNDARY_  
386   return par->IsInside(rphiz) ? par->Eval(rphiz,2) : 0;
387 #endif
388   return par->Eval(rphiz,2);
389   //
390 }
391
392 //__________________________________________________________________________________________
393 void AliMagWrapCheb::GetTPCIntCyl(const Double_t *rphiz, Double_t *b) const
394 {
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);
398   if (id<0) {
399     b[0] = b[1] = b[2] = 0;
400     return;
401   }
402   AliCheb3D* par = GetParamTPCInt(id);
403   if (par->IsInside(rphiz)) {par->Eval(rphiz,b); return;}
404   b[0] = b[1] = b[2] = 0;
405   return;
406   //
407 }
408
409
410 #ifdef  _INC_CREATION_ALICHEB3D_
411 //_______________________________________________
412 void AliMagWrapCheb::LoadData(const char* inpfile)
413 {
414   // read coefficients data from the text file
415   //
416   TString strf = inpfile;
417   gSystem->ExpandPathName(strf);
418   FILE* stream = fopen(strf,"r");
419   if (!stream) {
420     printf("Did not find input file %s\n",strf.Data());
421     return;
422   }
423   //
424   TString buffs;
425   AliCheb3DCalc::ReadLine(buffs,stream);
426   if (!buffs.BeginsWith("START")) {
427     Error("LoadData","Expected: \"START <name>\", found \"%s\"\nStop\n",buffs.Data());
428     exit(1);
429   }
430   if (buffs.First(' ')>0) SetName(buffs.Data()+buffs.First(' ')+1);
431   //
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());
436     exit(1);
437   }
438   AliCheb3DCalc::ReadLine(buffs,stream); // nparam
439   int nparSol = buffs.Atoi(); 
440   //
441   for (int ip=0;ip<nparSol;ip++) {
442     AliCheb3D* cheb = new AliCheb3D();
443     cheb->LoadData(stream);
444     AddParamSol(cheb);
445   }
446   //
447   AliCheb3DCalc::ReadLine(buffs,stream);
448   if (!buffs.BeginsWith("END SOLENOID")) {
449     Error("LoadData","Expected \"END SOLENOID\", found \"%s\"\nStop\n",buffs.Data());
450     exit(1);
451   }
452   //
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());
457     exit(1);
458   }
459   AliCheb3DCalc::ReadLine(buffs,stream); // nparam
460   int nparTPCInt = buffs.Atoi(); 
461   //
462   for (int ip=0;ip<nparTPCInt;ip++) {
463     AliCheb3D* cheb = new AliCheb3D();
464     cheb->LoadData(stream);
465     AddParamTPCInt(cheb);
466   }
467   //
468   AliCheb3DCalc::ReadLine(buffs,stream);
469   if (!buffs.BeginsWith("END TPCINT")) {
470     Error("LoadData","Expected \"END TPCINT\", found \"%s\"\nStop\n",buffs.Data());
471     exit(1);
472   }
473   //
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());
478     exit(1);
479   }
480   AliCheb3DCalc::ReadLine(buffs,stream); // nparam
481   int nparDip = buffs.Atoi();  
482   //
483   for (int ip=0;ip<nparDip;ip++) {
484     AliCheb3D* cheb = new AliCheb3D();
485     cheb->LoadData(stream);
486     AddParamDip(cheb);
487   }
488   //
489   AliCheb3DCalc::ReadLine(buffs,stream);
490   if (!buffs.BeginsWith("END DIPOLE")) {
491     Error("LoadData","Expected \"END DIPOLE\", found \"%s\"\nStop\n",GetName(),buffs.Data());
492     exit(1);
493   }
494   //
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());
498     exit(1);
499   }
500   //
501   // ---------------------------------------------------------------------------
502   fclose(stream);
503   BuildTableSol();
504   BuildTableDip();
505   BuildTableTPCInt();
506   //
507   printf("Loaded magnetic field \"%s\" from %s\n",GetName(),strf.Data());
508   //
509 }
510
511 //__________________________________________________________________________________________
512 void AliMagWrapCheb::BuildTableSol()
513 {
514   BuildTable(fNParamsSol,fParamsSol,
515              fNZSegSol,fNPSegSol,fNRSegSol,
516              fMinZSol,fMaxZSol, 
517              &fSegZSol,&fSegPSol,&fSegRSol,
518              &fBegSegPSol,&fNSegPSol,
519              &fBegSegRSol,&fNSegRSol, 
520              &fSegIDSol);
521 }
522
523 //__________________________________________________________________________________________
524 void AliMagWrapCheb::BuildTableDip()
525 {
526   BuildTable(fNParamsDip,fParamsDip,
527              fNZSegDip,fNYSegDip,fNXSegDip,
528              fMinZDip,fMaxZDip, 
529              &fSegZDip,&fSegYDip,&fSegXDip,
530              &fBegSegYDip,&fNSegYDip,
531              &fBegSegXDip,&fNSegXDip, 
532              &fSegIDDip);
533 }
534
535 //__________________________________________________________________________________________
536 void AliMagWrapCheb::BuildTableTPCInt()
537 {
538   BuildTable(fNParamsTPC,fParamsTPC,
539              fNZSegTPC,fNPSegTPC,fNRSegTPC,
540              fMinZTPC,fMaxZTPC, 
541              &fSegZTPC,&fSegPTPC,&fSegRTPC,
542              &fBegSegPTPC,&fNSegPTPC,
543              &fBegSegRTPC,&fNSegRTPC, 
544              &fSegIDTPC);
545 }
546
547 #endif
548
549 //_______________________________________________
550 #ifdef  _INC_CREATION_ALICHEB3D_
551
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),
557 //
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),
561 //
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)
565 //
566 {
567   // construct from coeffs from the text file
568   LoadData(inputFile);
569 }
570
571 //__________________________________________________________________________________________
572 void AliMagWrapCheb::AddParamSol(const AliCheb3D* param)
573 {
574   // adds new parameterization piece for Sol
575   // NOTE: pieces must be added strictly in increasing R then increasing Z order
576   //
577   if (!fParamsSol) fParamsSol = new TObjArray();
578   fParamsSol->Add( (AliCheb3D*)param );
579   fNParamsSol++;
580   if (fMaxRSol<param->GetBoundMax(0)) fMaxRSol = param->GetBoundMax(0);
581   //
582 }
583
584 //__________________________________________________________________________________________
585 void AliMagWrapCheb::AddParamTPCInt(const AliCheb3D* param)
586 {
587   // adds new parameterization piece for TPCInt
588   // NOTE: pieces must be added strictly in increasing R then increasing Z order
589   //
590   if (!fParamsTPC) fParamsTPC = new TObjArray();
591   fParamsTPC->Add( (AliCheb3D*)param);
592   fNParamsTPC++;
593   if (fMaxRTPC<param->GetBoundMax(0)) fMaxRTPC = param->GetBoundMax(0);
594   //
595 }
596
597 //__________________________________________________________________________________________
598 void AliMagWrapCheb::AddParamDip(const AliCheb3D* param)
599 {
600   // adds new parameterization piece for Dipole
601   //
602   if (!fParamsDip) fParamsDip = new TObjArray();
603   fParamsDip->Add( (AliCheb3D*)param);
604   fNParamsDip++;
605   //
606 }
607
608 //__________________________________________________________________________________________
609 void AliMagWrapCheb::ResetTPCInt()
610 {
611   // clean TPC field integral (used for update)
612   if (fNParamsTPC) {
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;   
622   }
623   fNParamsTPC = fNZSegTPC = fNPSegTPC = fNRSegTPC = 0;
624   fMinZTPC = 1e6;
625   fMaxZTPC = -1e6;
626   fMaxRTPC = 0;
627   //
628 }
629
630
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,
637                                 Int_t **segID)
638 {
639   // build lookup table for dipole
640   //
641   if (npar<1) return;
642   TArrayF segYArr,segXArr;
643   TArrayI begSegYDipArr,begSegXDipArr;
644   TArrayI nSegYDipArr,nSegXDipArr;
645   TArrayI segIDArr;
646   float *tmpSegZ,*tmpSegY,*tmpSegX;
647   //
648   // create segmentation in Z
649   nZSeg = SegmentDimension(&tmpSegZ, parArr, npar, 2, 1,-1, 1,-1, 1,-1) - 1;
650   nYSeg = 0;
651   nXSeg = 0;
652   //
653   // for each Z slice create segmentation in Y
654   begSegYDipArr.Set(nZSeg);
655   nSegYDipArr.Set(nZSeg);
656   float xyz[3];
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]);
666     //
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
671     //
672     for (int iy=0;iy<ny;iy++) {
673       int isg = nYSeg+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;
677       //
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]);
683       //
684       segIDArr.Set(nXSeg+nx);
685       //
686       // find corresponding params
687       xyz[1] = (tmpSegY[iy]+tmpSegY[iy+1])/2.; // mean Y of this segment
688       //
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;
695           break;
696         }
697       }
698       nXSeg += nx;
699       //
700       delete[] tmpSegX;
701     }
702     delete[] tmpSegY;
703     nYSeg += ny;
704   }
705   //
706   minZ = tmpSegZ[0];
707   maxZ = tmpSegZ[nZSeg];
708   (*segZ)  = new Float_t[nZSeg];
709   for (int i=nZSeg;i--;) (*segZ)[i] = tmpSegZ[i];
710   delete[] tmpSegZ;
711   //
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];
719   //
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];}
725   //
726 }
727
728 /*
729 //__________________________________________________
730 void AliMagWrapCheb::BuildTableDip()
731 {
732   // build lookup table for dipole
733   //
734   if (fNParamsDip<1) return;
735   TArrayF segY,segX;
736   TArrayI begSegYDip,begSegXDip;
737   TArrayI nsegYDip,nsegXDip;
738   TArrayI segID;
739   float *tmpSegZ,*tmpSegY,*tmpSegX;
740   //
741   // create segmentation in Z
742   fNZSegDip = SegmentDimension(&tmpSegZ, fParamsDip, fNParamsDip, 2, 1,-1, 1,-1, 1,-1) - 1;
743   fNYSegDip = 0;
744   fNXSegDip = 0;
745   //
746   // for each Z slice create segmentation in Y
747   begSegYDip.Set(fNZSegDip);
748   nsegYDip.Set(fNZSegDip);
749   float xyz[3];
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;
757     nsegYDip[iz] = ny;
758     printf(" Found %d YSegments, to start from %d\n",ny, begSegYDip[iz]);
759     //
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
764     //
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;
770       //
771       segX.Set(nx + fNXSegDip);
772       for (int ix=0;ix<nx;ix++) segX[fNXSegDip+ix] = tmpSegX[ix];
773       begSegXDip[isg] = fNXSegDip;
774       nsegXDip[isg] = nx;
775       printf("   Found %d XSegments, to start from %d\n",nx, begSegXDip[isg]);
776       //
777       segID.Set(fNXSegDip+nx);
778       //
779       // find corresponding params
780       xyz[1] = (tmpSegY[iy]+tmpSegY[iy+1])/2.; // mean Y of this segment
781       //
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;
788           break;
789         }
790       }
791       fNXSegDip += nx;
792       //
793       delete[] tmpSegX;
794     }
795     delete[] tmpSegY;
796     fNYSegDip += ny;
797   }
798   //
799   fMinZDip = tmpSegZ[0];
800   fMaxZDip = tmpSegZ[fNZSegDip];
801   fSegZDip    = new Float_t[fNZSegDip];
802   for (int i=fNZSegDip;i--;) fSegZDip[i] = tmpSegZ[i];
803   delete[] tmpSegZ;
804   //
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];
812   //
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];}
818   //
819 }
820 */
821
822 //________________________________________________________________
823 void AliMagWrapCheb::SaveData(const char* outfile) const
824 {
825   // writes coefficients data to output text file
826   TString strf = outfile;
827   gSystem->ExpandPathName(strf);
828   FILE* stream = fopen(strf,"w+");
829   //
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");
835   //
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");
841   //
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");
846   //
847   fprintf(stream,"#\nEND %s\n",GetName());
848   //
849   fclose(stream);
850   //
851 }
852
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)
855 {
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];
860   int nseg0 = 0;
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;
866     //
867     tmpC[nseg0++] = cheb->GetBoundMin(dim);
868     tmpC[nseg0++] = cheb->GetBoundMax(dim);    
869   }
870   // range Dim's boundaries in increasing order
871   TMath::Sort(nseg0,tmpC,tmpInd,kFALSE);
872   // count number of really different Z's
873   int nseg = 0;
874   float cprev = -1e6;
875   for (int ip=0;ip<nseg0;ip++) {
876     if (TMath::Abs(cprev-tmpC[ tmpInd[ip] ])>1e-4) {
877       cprev = tmpC[ tmpInd[ip] ];
878       nseg++;
879     }
880     else tmpInd[ip] = -1; // supress redundant Z
881   }
882   // 
883   *seg  = new float[nseg]; // create final Z segmenations
884   nseg = 0;
885   for (int ip=0;ip<nseg0;ip++) if (tmpInd[ip]>=0) (*seg)[nseg++] = tmpC[ tmpInd[ip] ];
886   //
887   delete[] tmpC;
888   delete[] tmpInd;
889   return nseg;
890 }
891
892 #endif
893