]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliMagWrapCheb.cxx
changed the names of the histograms, requested by AMORE
[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   //
285   Bool_t reCheck = kFALSE;
286   while(1) {
287     int ysegBeg = fBegSegYDip[zid];
288     //
289     for (yid=0;yid<fNSegYDip[zid];yid++) if (xyz[1]<fSegYDip[ysegBeg+yid]) break;
290     if ( --yid < 0 ) yid = 0;
291     yid +=  ysegBeg;
292     //
293     int xsegBeg = fBegSegXDip[yid];
294     for (xid=0;xid<fNSegXDip[yid];xid++) if (xyz[0]<fSegXDip[xsegBeg+xid]) break;
295     //
296     if ( --xid < 0) xid = 0;
297     xid +=  xsegBeg;
298     //
299     // to make sure that due to the precision problems we did not pick the next Zbin    
300     if (!reCheck && (xyz[2] - fSegZDip[zid] < 3.e-5) && zid &&
301         !GetParamDip(fSegIDDip[xid])->IsInside(xyz)) {  // check the previous Z bin
302       zid--;
303       reCheck = kTRUE;
304       continue;
305     } 
306     break;
307   }
308   return fSegIDDip[xid];
309 }
310
311 //__________________________________________________________________________________________________
312 Int_t AliMagWrapCheb::FindSolSegment(const Double_t *rpz) const 
313 {
314   // find the segment containing point xyz. If it is outside find the closest segment 
315   if (!fNParamsSol) return -1;
316   int rid,pid,zid = TMath::BinarySearch(fNZSegSol,fSegZSol,(Float_t)rpz[2]); // find zsegment
317   //
318   Bool_t reCheck = kFALSE;
319   while(1) {
320     int psegBeg = fBegSegPSol[zid];
321     for (pid=0;pid<fNSegPSol[zid];pid++) if (rpz[1]<fSegPSol[psegBeg+pid]) break;
322     if ( --pid < 0 ) pid = 0;
323     pid +=  psegBeg;
324     //
325     int rsegBeg = fBegSegRSol[pid];
326     for (rid=0;rid<fNSegRSol[pid];rid++) if (rpz[0]<fSegRSol[rsegBeg+rid]) break;
327     if ( --rid < 0) rid = 0;
328     rid +=  rsegBeg;
329     //
330     // to make sure that due to the precision problems we did not pick the next Zbin    
331     if (!reCheck && (rpz[2] - fSegZSol[zid] < 3.e-5) && zid &&
332         !GetParamSol(fSegIDSol[rid])->IsInside(rpz)) {  // check the previous Z bin
333       zid--;
334       reCheck = kTRUE;
335       continue;
336     } 
337     break;
338   }
339   return fSegIDSol[rid];
340 }
341
342 //__________________________________________________________________________________________________
343 Int_t AliMagWrapCheb::FindTPCSegment(const Double_t *rpz) const 
344 {
345   // find the segment containing point xyz. If it is outside find the closest segment 
346   if (!fNParamsTPC) return -1;
347   int rid,pid,zid = TMath::BinarySearch(fNZSegTPC,fSegZTPC,(Float_t)rpz[2]); // find zsegment
348   //
349   Bool_t reCheck = kFALSE;
350   while(1) {
351     int psegBeg = fBegSegPTPC[zid];
352     //
353     for (pid=0;pid<fNSegPTPC[zid];pid++) if (rpz[1]<fSegPTPC[psegBeg+pid]) break;
354     if ( --pid < 0 ) pid = 0;
355     pid +=  psegBeg;
356     //
357     int rsegBeg = fBegSegRTPC[pid];
358     for (rid=0;rid<fNSegRTPC[pid];rid++) if (rpz[0]<fSegRTPC[rsegBeg+rid]) break;
359     if ( --rid < 0) rid = 0;
360     rid +=  rsegBeg;
361     //
362     // to make sure that due to the precision problems we did not pick the next Zbin    
363     if (!reCheck && (rpz[2] - fSegZSol[zid] < 3.e-5) && zid &&
364         !GetParamSol(fSegIDSol[rid])->IsInside(rpz)) {  // check the previous Z bin
365       zid--;
366       reCheck = kTRUE;
367       continue;
368     } 
369     break;
370   }
371   return fSegIDTPC[rid];
372 }
373
374
375 //__________________________________________________________________________________________
376 void AliMagWrapCheb::GetTPCInt(const Double_t *xyz, Double_t *b) const
377 {
378   // compute TPC region field integral in cartesian coordinates.
379   // If point is outside of the parameterized region get it at closeset valid point
380   static Double_t rphiz[3];
381   //
382   // TPCInt region
383   // convert coordinates to cyl system
384   CartToCyl(xyz,rphiz);
385 #ifndef _BRING_TO_BOUNDARY_
386   if ( (rphiz[2]>GetMaxZTPCInt()||rphiz[2]<GetMinZTPCInt()) ||
387        rphiz[0]>GetMaxRTPCInt()) {for (int i=3;i--;) b[i]=0; return;}
388 #endif
389   //
390   GetTPCIntCyl(rphiz,b);
391   //
392   // convert field to cartesian system
393   CylToCartCylB(rphiz, b,b);
394   //
395 }
396
397 //__________________________________________________________________________________________
398 void AliMagWrapCheb::FieldCylSol(const Double_t *rphiz, Double_t *b) const
399 {
400   // compute Solenoid field in Cylindircal coordinates
401   // note: if the point is outside the volume get the field in closest parameterized point
402   int id = FindSolSegment(rphiz);
403   if (id<0) return;
404   AliCheb3D* par = GetParamSol(id);
405 #ifndef _BRING_TO_BOUNDARY_  // exact matching to fitted volume is requested  
406   if (!par->IsInside(rphiz)) return;
407 #endif
408   par->Eval(rphiz,b);
409   return;
410   //
411 }
412
413 //__________________________________________________________________________________________
414 Double_t AliMagWrapCheb::FieldCylSolBz(const Double_t *rphiz) const
415 {
416   // compute Solenoid field in Cylindircal coordinates
417   // note: if the point is outside the volume get the field in closest parameterized point
418   int id = FindSolSegment(rphiz);
419   if (id<0) return 0.;
420   AliCheb3D* par = GetParamSol(id);
421 #ifndef _BRING_TO_BOUNDARY_  
422   return par->IsInside(rphiz) ? par->Eval(rphiz,2) : 0;
423 #endif
424   return par->Eval(rphiz,2);
425   //
426 }
427
428 //__________________________________________________________________________________________
429 void AliMagWrapCheb::GetTPCIntCyl(const Double_t *rphiz, Double_t *b) const
430 {
431   // compute field integral in TPC region in Cylindircal coordinates
432   // note: the check for the point being inside the parameterized region is done outside
433   int id = FindTPCSegment(rphiz);
434   if (id<0) {
435     b[0] = b[1] = b[2] = 0;
436     return;
437   }
438   AliCheb3D* par = GetParamTPCInt(id);
439   if (par->IsInside(rphiz)) {par->Eval(rphiz,b); return;}
440   b[0] = b[1] = b[2] = 0;
441   return;
442   //
443 }
444
445
446 #ifdef  _INC_CREATION_ALICHEB3D_
447 //_______________________________________________
448 void AliMagWrapCheb::LoadData(const char* inpfile)
449 {
450   // read coefficients data from the text file
451   //
452   TString strf = inpfile;
453   gSystem->ExpandPathName(strf);
454   FILE* stream = fopen(strf,"r");
455   if (!stream) {
456     printf("Did not find input file %s\n",strf.Data());
457     return;
458   }
459   //
460   TString buffs;
461   AliCheb3DCalc::ReadLine(buffs,stream);
462   if (!buffs.BeginsWith("START")) {
463     Error("LoadData","Expected: \"START <name>\", found \"%s\"\nStop\n",buffs.Data());
464     exit(1);
465   }
466   if (buffs.First(' ')>0) SetName(buffs.Data()+buffs.First(' ')+1);
467   //
468   // Solenoid part    -----------------------------------------------------------
469   AliCheb3DCalc::ReadLine(buffs,stream);
470   if (!buffs.BeginsWith("START SOLENOID")) {
471     Error("LoadData","Expected: \"START SOLENOID\", found \"%s\"\nStop\n",buffs.Data());
472     exit(1);
473   }
474   AliCheb3DCalc::ReadLine(buffs,stream); // nparam
475   int nparSol = buffs.Atoi(); 
476   //
477   for (int ip=0;ip<nparSol;ip++) {
478     AliCheb3D* cheb = new AliCheb3D();
479     cheb->LoadData(stream);
480     AddParamSol(cheb);
481   }
482   //
483   AliCheb3DCalc::ReadLine(buffs,stream);
484   if (!buffs.BeginsWith("END SOLENOID")) {
485     Error("LoadData","Expected \"END SOLENOID\", found \"%s\"\nStop\n",buffs.Data());
486     exit(1);
487   }
488   //
489   // TPCInt part     -----------------------------------------------------------
490   AliCheb3DCalc::ReadLine(buffs,stream);
491   if (!buffs.BeginsWith("START TPCINT")) {
492     Error("LoadData","Expected: \"START TPCINT\", found \"%s\"\nStop\n",buffs.Data());
493     exit(1);
494   }
495   AliCheb3DCalc::ReadLine(buffs,stream); // nparam
496   int nparTPCInt = buffs.Atoi(); 
497   //
498   for (int ip=0;ip<nparTPCInt;ip++) {
499     AliCheb3D* cheb = new AliCheb3D();
500     cheb->LoadData(stream);
501     AddParamTPCInt(cheb);
502   }
503   //
504   AliCheb3DCalc::ReadLine(buffs,stream);
505   if (!buffs.BeginsWith("END TPCINT")) {
506     Error("LoadData","Expected \"END TPCINT\", found \"%s\"\nStop\n",buffs.Data());
507     exit(1);
508   }
509   //
510   // Dipole part    -----------------------------------------------------------
511   AliCheb3DCalc::ReadLine(buffs,stream);
512   if (!buffs.BeginsWith("START DIPOLE")) {
513     Error("LoadData","Expected: \"START DIPOLE\", found \"%s\"\nStop\n",buffs.Data());
514     exit(1);
515   }
516   AliCheb3DCalc::ReadLine(buffs,stream); // nparam
517   int nparDip = buffs.Atoi();  
518   //
519   for (int ip=0;ip<nparDip;ip++) {
520     AliCheb3D* cheb = new AliCheb3D();
521     cheb->LoadData(stream);
522     AddParamDip(cheb);
523   }
524   //
525   AliCheb3DCalc::ReadLine(buffs,stream);
526   if (!buffs.BeginsWith("END DIPOLE")) {
527     Error("LoadData","Expected \"END DIPOLE\", found \"%s\"\nStop\n",GetName(),buffs.Data());
528     exit(1);
529   }
530   //
531   AliCheb3DCalc::ReadLine(buffs,stream);
532   if (!buffs.BeginsWith("END") || !buffs.Contains(GetName())) {
533     Error("LoadData","Expected: \"END %s\", found \"%s\"\nStop\n",GetName(),buffs.Data());
534     exit(1);
535   }
536   //
537   // ---------------------------------------------------------------------------
538   fclose(stream);
539   BuildTableSol();
540   BuildTableDip();
541   BuildTableTPCInt();
542   //
543   printf("Loaded magnetic field \"%s\" from %s\n",GetName(),strf.Data());
544   //
545 }
546
547 //__________________________________________________________________________________________
548 void AliMagWrapCheb::BuildTableSol()
549 {
550   BuildTable(fNParamsSol,fParamsSol,
551              fNZSegSol,fNPSegSol,fNRSegSol,
552              fMinZSol,fMaxZSol, 
553              &fSegZSol,&fSegPSol,&fSegRSol,
554              &fBegSegPSol,&fNSegPSol,
555              &fBegSegRSol,&fNSegRSol, 
556              &fSegIDSol);
557 }
558
559 //__________________________________________________________________________________________
560 void AliMagWrapCheb::BuildTableDip()
561 {
562   BuildTable(fNParamsDip,fParamsDip,
563              fNZSegDip,fNYSegDip,fNXSegDip,
564              fMinZDip,fMaxZDip, 
565              &fSegZDip,&fSegYDip,&fSegXDip,
566              &fBegSegYDip,&fNSegYDip,
567              &fBegSegXDip,&fNSegXDip, 
568              &fSegIDDip);
569 }
570
571 //__________________________________________________________________________________________
572 void AliMagWrapCheb::BuildTableTPCInt()
573 {
574   BuildTable(fNParamsTPC,fParamsTPC,
575              fNZSegTPC,fNPSegTPC,fNRSegTPC,
576              fMinZTPC,fMaxZTPC, 
577              &fSegZTPC,&fSegPTPC,&fSegRTPC,
578              &fBegSegPTPC,&fNSegPTPC,
579              &fBegSegRTPC,&fNSegRTPC, 
580              &fSegIDTPC);
581 }
582
583 #endif
584
585 //_______________________________________________
586 #ifdef  _INC_CREATION_ALICHEB3D_
587
588 //__________________________________________________________________________________________
589 AliMagWrapCheb::AliMagWrapCheb(const char* inputFile) : 
590   fNParamsSol(0),fNZSegSol(0),fNPSegSol(0),fNRSegSol(0),
591   fSegZSol(0),fSegPSol(0),fSegRSol(0),
592   fBegSegPSol(0),fNSegPSol(0),fBegSegRSol(0),fNSegRSol(0),fSegIDSol(0),fMinZSol(1.e6),fMaxZSol(-1.e6),fParamsSol(0),fMaxRSol(0),
593 //
594   fNParamsTPC(0),fNZSegTPC(0),fNPSegTPC(0),fNRSegTPC(0),
595   fSegZTPC(0),fSegPTPC(0),fSegRTPC(0),
596   fBegSegPTPC(0),fNSegPTPC(0),fBegSegRTPC(0),fNSegRTPC(0),fSegIDTPC(0),fMinZTPC(1.e6),fMaxZTPC(-1.e6),fParamsTPC(0),fMaxRTPC(0),
597 //
598   fNParamsDip(0),fNZSegDip(0),fNYSegDip(0),fNXSegDip(0),
599   fSegZDip(0),fSegYDip(0),fSegXDip(0),
600   fBegSegYDip(0),fNSegYDip(0),fBegSegXDip(0),fNSegXDip(0),fSegIDDip(0),fMinZDip(1.e6),fMaxZDip(-1.e6),fParamsDip(0)
601 //
602 {
603   // construct from coeffs from the text file
604   LoadData(inputFile);
605 }
606
607 //__________________________________________________________________________________________
608 void AliMagWrapCheb::AddParamSol(const AliCheb3D* param)
609 {
610   // adds new parameterization piece for Sol
611   // NOTE: pieces must be added strictly in increasing R then increasing Z order
612   //
613   if (!fParamsSol) fParamsSol = new TObjArray();
614   fParamsSol->Add( (AliCheb3D*)param );
615   fNParamsSol++;
616   if (fMaxRSol<param->GetBoundMax(0)) fMaxRSol = param->GetBoundMax(0);
617   //
618 }
619
620 //__________________________________________________________________________________________
621 void AliMagWrapCheb::AddParamTPCInt(const AliCheb3D* param)
622 {
623   // adds new parameterization piece for TPCInt
624   // NOTE: pieces must be added strictly in increasing R then increasing Z order
625   //
626   if (!fParamsTPC) fParamsTPC = new TObjArray();
627   fParamsTPC->Add( (AliCheb3D*)param);
628   fNParamsTPC++;
629   if (fMaxRTPC<param->GetBoundMax(0)) fMaxRTPC = param->GetBoundMax(0);
630   //
631 }
632
633 //__________________________________________________________________________________________
634 void AliMagWrapCheb::AddParamDip(const AliCheb3D* param)
635 {
636   // adds new parameterization piece for Dipole
637   //
638   if (!fParamsDip) fParamsDip = new TObjArray();
639   fParamsDip->Add( (AliCheb3D*)param);
640   fNParamsDip++;
641   //
642 }
643
644 //__________________________________________________________________________________________
645 void AliMagWrapCheb::ResetTPCInt()
646 {
647   // clean TPC field integral (used for update)
648   if (fNParamsTPC) {
649     delete   fParamsTPC;  fParamsTPC = 0;
650     delete[] fSegZTPC;    fSegZTPC   = 0;
651     delete[] fSegPTPC;    fSegPTPC   = 0;
652     delete[] fSegRTPC;    fSegRTPC   = 0;
653     delete[] fBegSegPTPC; fBegSegPTPC = 0;
654     delete[] fNSegPTPC;   fNSegPTPC   = 0;
655     delete[] fBegSegRTPC; fBegSegRTPC = 0;
656     delete[] fNSegRTPC;   fNSegRTPC   = 0;
657     delete[] fSegIDTPC;   fSegIDTPC   = 0;   
658   }
659   fNParamsTPC = fNZSegTPC = fNPSegTPC = fNRSegTPC = 0;
660   fMinZTPC = 1e6;
661   fMaxZTPC = -1e6;
662   fMaxRTPC = 0;
663   //
664 }
665
666
667 //__________________________________________________
668 void AliMagWrapCheb::BuildTable(Int_t npar,TObjArray *parArr, Int_t &nZSeg, Int_t &nYSeg, Int_t &nXSeg,
669                                 Float_t &minZ,Float_t &maxZ,
670                                 Float_t **segZ,Float_t **segY,Float_t **segX,
671                                 Int_t **begSegY,Int_t **nSegY,
672                                 Int_t **begSegX,Int_t **nSegX,
673                                 Int_t **segID)
674 {
675   // build lookup table for dipole
676   //
677   if (npar<1) return;
678   TArrayF segYArr,segXArr;
679   TArrayI begSegYDipArr,begSegXDipArr;
680   TArrayI nSegYDipArr,nSegXDipArr;
681   TArrayI segIDArr;
682   float *tmpSegZ,*tmpSegY,*tmpSegX;
683   //
684   // create segmentation in Z
685   nZSeg = SegmentDimension(&tmpSegZ, parArr, npar, 2, 1,-1, 1,-1, 1,-1) - 1;
686   nYSeg = 0;
687   nXSeg = 0;
688   //
689   // for each Z slice create segmentation in Y
690   begSegYDipArr.Set(nZSeg);
691   nSegYDipArr.Set(nZSeg);
692   float xyz[3];
693   for (int iz=0;iz<nZSeg;iz++) {
694     printf("\nZSegment#%d  %+e : %+e\n",iz,tmpSegZ[iz],tmpSegZ[iz+1]);
695     int ny = SegmentDimension(&tmpSegY, parArr, npar, 1, 
696                               1,-1, 1,-1, tmpSegZ[iz],tmpSegZ[iz+1]) - 1;
697     segYArr.Set(ny + nYSeg);
698     for (int iy=0;iy<ny;iy++) segYArr[nYSeg+iy] = tmpSegY[iy];
699     begSegYDipArr[iz] = nYSeg;
700     nSegYDipArr[iz] = ny;
701     printf(" Found %d YSegments, to start from %d\n",ny, begSegYDipArr[iz]);
702     //
703     // for each slice in Z and Y create segmentation in X
704     begSegXDipArr.Set(nYSeg+ny);
705     nSegXDipArr.Set(nYSeg+ny);
706     xyz[2] = (tmpSegZ[iz]+tmpSegZ[iz+1])/2.; // mean Z of this segment
707     //
708     for (int iy=0;iy<ny;iy++) {
709       int isg = nYSeg+iy;
710       printf("\n   YSegment#%d  %+e : %+e\n",iy, tmpSegY[iy],tmpSegY[iy+1]);
711       int nx = SegmentDimension(&tmpSegX, parArr, npar, 0, 
712                                 1,-1, tmpSegY[iy],tmpSegY[iy+1], tmpSegZ[iz],tmpSegZ[iz+1]) - 1;
713       //
714       segXArr.Set(nx + nXSeg);
715       for (int ix=0;ix<nx;ix++) segXArr[nXSeg+ix] = tmpSegX[ix];
716       begSegXDipArr[isg] = nXSeg;
717       nSegXDipArr[isg] = nx;
718       printf("   Found %d XSegments, to start from %d\n",nx, begSegXDipArr[isg]);
719       //
720       segIDArr.Set(nXSeg+nx);
721       //
722       // find corresponding params
723       xyz[1] = (tmpSegY[iy]+tmpSegY[iy+1])/2.; // mean Y of this segment
724       //
725       for (int ix=0;ix<nx;ix++) {
726         xyz[0] = (tmpSegX[ix]+tmpSegX[ix+1])/2.; // mean X of this segment
727         for (int ipar=0;ipar<npar;ipar++) {
728           AliCheb3D* cheb = (AliCheb3D*) parArr->At(ipar);
729           if (!cheb->IsInside(xyz)) continue;
730           segIDArr[nXSeg+ix] = ipar;
731           break;
732         }
733       }
734       nXSeg += nx;
735       //
736       delete[] tmpSegX;
737     }
738     delete[] tmpSegY;
739     nYSeg += ny;
740   }
741   //
742   minZ = tmpSegZ[0];
743   maxZ = tmpSegZ[nZSeg];
744   (*segZ)  = new Float_t[nZSeg];
745   for (int i=nZSeg;i--;) (*segZ)[i] = tmpSegZ[i];
746   delete[] tmpSegZ;
747   //
748   (*segY)    = new Float_t[nYSeg];
749   (*segX)    = new Float_t[nXSeg];
750   (*begSegY) = new Int_t[nZSeg];
751   (*nSegY)   = new Int_t[nZSeg];
752   (*begSegX) = new Int_t[nYSeg];
753   (*nSegX)   = new Int_t[nYSeg];
754   (*segID)   = new Int_t[nXSeg];
755   //
756   for (int i=nYSeg;i--;) (*segY)[i] = segYArr[i];
757   for (int i=nXSeg;i--;) (*segX)[i] = segXArr[i];
758   for (int i=nZSeg;i--;) {(*begSegY)[i] = begSegYDipArr[i]; (*nSegY)[i] = nSegYDipArr[i];}
759   for (int i=nYSeg;i--;) {(*begSegX)[i] = begSegXDipArr[i]; (*nSegX)[i] = nSegXDipArr[i];}
760   for (int i=nXSeg;i--;) {(*segID)[i]   = segIDArr[i];}
761   //
762 }
763
764 /*
765 //__________________________________________________
766 void AliMagWrapCheb::BuildTableDip()
767 {
768   // build lookup table for dipole
769   //
770   if (fNParamsDip<1) return;
771   TArrayF segY,segX;
772   TArrayI begSegYDip,begSegXDip;
773   TArrayI nsegYDip,nsegXDip;
774   TArrayI segID;
775   float *tmpSegZ,*tmpSegY,*tmpSegX;
776   //
777   // create segmentation in Z
778   fNZSegDip = SegmentDimension(&tmpSegZ, fParamsDip, fNParamsDip, 2, 1,-1, 1,-1, 1,-1) - 1;
779   fNYSegDip = 0;
780   fNXSegDip = 0;
781   //
782   // for each Z slice create segmentation in Y
783   begSegYDip.Set(fNZSegDip);
784   nsegYDip.Set(fNZSegDip);
785   float xyz[3];
786   for (int iz=0;iz<fNZSegDip;iz++) {
787     printf("\nZSegment#%d  %+e : %+e\n",iz,tmpSegZ[iz],tmpSegZ[iz+1]);
788     int ny = SegmentDimension(&tmpSegY, fParamsDip, fNParamsDip, 1, 
789                                  1,-1, 1,-1, tmpSegZ[iz],tmpSegZ[iz+1]) - 1;
790     segY.Set(ny + fNYSegDip);
791     for (int iy=0;iy<ny;iy++) segY[fNYSegDip+iy] = tmpSegY[iy];
792     begSegYDip[iz] = fNYSegDip;
793     nsegYDip[iz] = ny;
794     printf(" Found %d YSegments, to start from %d\n",ny, begSegYDip[iz]);
795     //
796     // for each slice in Z and Y create segmentation in X
797     begSegXDip.Set(fNYSegDip+ny);
798     nsegXDip.Set(fNYSegDip+ny);
799     xyz[2] = (tmpSegZ[iz]+tmpSegZ[iz+1])/2.; // mean Z of this segment
800     //
801     for (int iy=0;iy<ny;iy++) {
802       int isg = fNYSegDip+iy;
803       printf("\n   YSegment#%d  %+e : %+e\n",iy, tmpSegY[iy],tmpSegY[iy+1]);
804       int nx = SegmentDimension(&tmpSegX, fParamsDip, fNParamsDip, 0, 
805                                 1,-1, tmpSegY[iy],tmpSegY[iy+1], tmpSegZ[iz],tmpSegZ[iz+1]) - 1;
806       //
807       segX.Set(nx + fNXSegDip);
808       for (int ix=0;ix<nx;ix++) segX[fNXSegDip+ix] = tmpSegX[ix];
809       begSegXDip[isg] = fNXSegDip;
810       nsegXDip[isg] = nx;
811       printf("   Found %d XSegments, to start from %d\n",nx, begSegXDip[isg]);
812       //
813       segID.Set(fNXSegDip+nx);
814       //
815       // find corresponding params
816       xyz[1] = (tmpSegY[iy]+tmpSegY[iy+1])/2.; // mean Y of this segment
817       //
818       for (int ix=0;ix<nx;ix++) {
819         xyz[0] = (tmpSegX[ix]+tmpSegX[ix+1])/2.; // mean X of this segment
820         for (int ipar=0;ipar<fNParamsDip;ipar++) {
821           AliCheb3D* cheb = (AliCheb3D*) fParamsDip->At(ipar);
822           if (!cheb->IsInside(xyz)) continue;
823           segID[fNXSegDip+ix] = ipar;
824           break;
825         }
826       }
827       fNXSegDip += nx;
828       //
829       delete[] tmpSegX;
830     }
831     delete[] tmpSegY;
832     fNYSegDip += ny;
833   }
834   //
835   fMinZDip = tmpSegZ[0];
836   fMaxZDip = tmpSegZ[fNZSegDip];
837   fSegZDip    = new Float_t[fNZSegDip];
838   for (int i=fNZSegDip;i--;) fSegZDip[i] = tmpSegZ[i];
839   delete[] tmpSegZ;
840   //
841   fSegYDip    = new Float_t[fNYSegDip];
842   fSegXDip    = new Float_t[fNXSegDip];
843   fBegSegYDip = new Int_t[fNZSegDip];
844   fNSegYDip   = new Int_t[fNZSegDip];
845   fBegSegXDip = new Int_t[fNYSegDip];
846   fNSegXDip   = new Int_t[fNYSegDip];
847   fSegIDDip   = new Int_t[fNXSegDip];
848   //
849   for (int i=fNYSegDip;i--;) fSegYDip[i] = segY[i];
850   for (int i=fNXSegDip;i--;) fSegXDip[i] = segX[i];
851   for (int i=fNZSegDip;i--;) {fBegSegYDip[i] = begSegYDip[i]; fNSegYDip[i] = nsegYDip[i];}
852   for (int i=fNYSegDip;i--;) {fBegSegXDip[i] = begSegXDip[i]; fNSegXDip[i] = nsegXDip[i];}
853   for (int i=fNXSegDip;i--;) {fSegIDDip[i]   = segID[i];}
854   //
855 }
856 */
857
858 //________________________________________________________________
859 void AliMagWrapCheb::SaveData(const char* outfile) const
860 {
861   // writes coefficients data to output text file
862   TString strf = outfile;
863   gSystem->ExpandPathName(strf);
864   FILE* stream = fopen(strf,"w+");
865   //
866   // Sol part    ---------------------------------------------------------
867   fprintf(stream,"# Set of Chebyshev parameterizations for ALICE magnetic field\nSTART %s\n",GetName());
868   fprintf(stream,"START SOLENOID\n#Number of pieces\n%d\n",fNParamsSol);
869   for (int ip=0;ip<fNParamsSol;ip++) GetParamSol(ip)->SaveData(stream);
870   fprintf(stream,"#\nEND SOLENOID\n");
871   //
872   // TPCInt part ---------------------------------------------------------
873   fprintf(stream,"# Set of Chebyshev parameterizations for ALICE magnetic field\nSTART %s\n",GetName());
874   fprintf(stream,"START TPCINT\n#Number of pieces\n%d\n",fNParamsTPC);
875   for (int ip=0;ip<fNParamsTPC;ip++) GetParamTPCInt(ip)->SaveData(stream);
876   fprintf(stream,"#\nEND TPCINT\n");
877   //
878   // Dip part   ---------------------------------------------------------
879   fprintf(stream,"START DIPOLE\n#Number of pieces\n%d\n",fNParamsDip);
880   for (int ip=0;ip<fNParamsDip;ip++) GetParamDip(ip)->SaveData(stream);
881   fprintf(stream,"#\nEND DIPOLE\n");
882   //
883   fprintf(stream,"#\nEND %s\n",GetName());
884   //
885   fclose(stream);
886   //
887 }
888
889 Int_t AliMagWrapCheb::SegmentDimension(float** seg,const TObjArray* par,int npar, int dim, 
890                                        float xmn,float xmx,float ymn,float ymx,float zmn,float zmx)
891 {
892   // find all boundaries in deimension dim for boxes in given region.
893   // if mn>mx for given projection the check is not done for it.
894   float *tmpC = new float[2*npar];
895   int *tmpInd = new int[2*npar];
896   int nseg0 = 0;
897   for (int ip=0;ip<npar;ip++) {
898     AliCheb3D* cheb = (AliCheb3D*) par->At(ip);
899     if (xmn<xmx && (cheb->GetBoundMin(0)>(xmx+xmn)/2 || cheb->GetBoundMax(0)<(xmn+xmx)/2)) continue;
900     if (ymn<ymx && (cheb->GetBoundMin(1)>(ymx+ymn)/2 || cheb->GetBoundMax(1)<(ymn+ymx)/2)) continue;
901     if (zmn<zmx && (cheb->GetBoundMin(2)>(zmx+zmn)/2 || cheb->GetBoundMax(2)<(zmn+zmx)/2)) continue;
902     //
903     tmpC[nseg0++] = cheb->GetBoundMin(dim);
904     tmpC[nseg0++] = cheb->GetBoundMax(dim);    
905   }
906   // range Dim's boundaries in increasing order
907   TMath::Sort(nseg0,tmpC,tmpInd,kFALSE);
908   // count number of really different Z's
909   int nseg = 0;
910   float cprev = -1e6;
911   for (int ip=0;ip<nseg0;ip++) {
912     if (TMath::Abs(cprev-tmpC[ tmpInd[ip] ])>1e-4) {
913       cprev = tmpC[ tmpInd[ip] ];
914       nseg++;
915     }
916     else tmpInd[ip] = -1; // supress redundant Z
917   }
918   // 
919   *seg  = new float[nseg]; // create final Z segmenations
920   nseg = 0;
921   for (int ip=0;ip<nseg0;ip++) if (tmpInd[ip]>=0) (*seg)[nseg++] = tmpC[ tmpInd[ip] ];
922   //
923   delete[] tmpC;
924   delete[] tmpInd;
925   return nseg;
926 }
927
928 #endif
929