Get the Bfield from AliL3Transform
[u/mrichter/AliRoot.git] / HLT / src / AliL3Transform.cxx
1 // $Id$
2 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>, Uli Frankenfeld <mailto:franken@fi.uib.no>
3 //*-- Copyright &copy ASV
4 // changes done by Constantin Loizides <mailto:loizides@ikf.physik.uni-frankfurt.de>
5
6
7 #include "AliL3Logging.h"
8 #include "AliL3Transform.h"
9 #include <math.h>
10
11 //_____________________________________________________________
12 // AliL3Transform
13 //
14 // Transformation class for ALICE TPC.
15 //
16 // Class which contains all detector specific parameters for the TPC,
17 // and different useful functions for coordinate transforms.
18 //
19 // The class is completely static, which means that no object needs
20 // to be instantiated. Function calls should then be done like:
21 //
22 // AliL3Transform::GetEta(xyz);
23 //
24 // IMPORTANT: If used as is, default detector parameters will be used,
25 //            and you really have to make sure that these correspond to
26 //            the AliROOT version you are currently working on!!
27 //            You should therefore always initialize the parameters by
28 //
29 //            AliL3Transform::Init(path);
30 // 
31 //            where path is a char*, giving the path to where file containing
32 //            the detector parameter is located. This file should be called
33 //            "l3transform.config", and can be created with macro exa/Make_Init.C.
34
35 ClassImp(AliL3Transform)
36
37 Double_t AliL3Transform::fBField = 0.2;
38 Int_t AliL3Transform::fVersion = 0;
39 Int_t AliL3Transform::fNTimeBins = 446 ;
40 Int_t AliL3Transform::fNRowLow = 64 ;
41 Int_t AliL3Transform::fNRowUp = 112 ;
42 Int_t AliL3Transform::fNSectorLow = 36 ;
43 Int_t AliL3Transform::fNSectorUp = 36 ;
44 Int_t AliL3Transform::fNSector = 72 ;
45 Double_t AliL3Transform::fPadPitchWidthLow = 0.400000 ;
46 Double_t AliL3Transform::fPadPitchWidthUp = 0.600000 ;
47 Double_t AliL3Transform::fZWidth = 0.56599998474121093750 ;
48 Double_t AliL3Transform::fZSigma = 0.22880849748219134199 ;
49 Double_t AliL3Transform::fZOffset = 0.68642549244657402596;
50 Double_t AliL3Transform::fZLength = 250.;
51 Int_t AliL3Transform::fNSlice = 36 ;
52 Int_t AliL3Transform::fNRow = 176 ;
53 Double_t AliL3Transform::fNRotShift = 0.5 ;
54 Double_t AliL3Transform::fPi = 3.141592653589793 ;
55 Double_t AliL3Transform::fX[176] = {84.570007324218750,
56                                     85.320007324218750,
57                                     86.070007324218750,
58                                     86.820007324218750,
59                                     87.570007324218750,
60                                     88.320007324218750,
61                                     89.070007324218750,
62                                     89.820007324218750,
63                                     90.570007324218750,
64                                     91.320007324218750,
65                                     92.070007324218750,
66                                     92.820007324218750,
67                                     93.570007324218750,
68                                     94.320007324218750,
69                                     95.070007324218750,
70                                     95.820007324218750,
71                                     96.570007324218750,
72                                     97.320007324218750,
73                                     98.070007324218750,
74                                     98.820007324218750,
75                                     99.570007324218750,
76                                     100.320007324218750,
77                                     101.070007324218750,
78                                     101.820007324218750,
79                                     102.570007324218750,
80                                     103.320007324218750,
81                                     104.070007324218750,
82                                     104.820007324218750,
83                                     105.570007324218750,
84                                     106.320007324218750,
85                                     107.070007324218750,
86                                     107.820007324218750,
87                                     108.570007324218750,
88                                     109.320007324218750,
89                                     110.070007324218750,
90                                     110.820007324218750,
91                                     111.570007324218750,
92                                     112.320007324218750,
93                                     113.070007324218750,
94                                     113.820007324218750,
95                                     114.570007324218750,
96                                     115.320007324218750,
97                                     116.070007324218750,
98                                     116.820007324218750,
99                                     117.570007324218750,
100                                     118.320007324218750,
101                                     119.070007324218750,
102                                     119.820007324218750,
103                                     120.570007324218750,
104                                     121.320007324218750,
105                                     122.070007324218750,
106                                     122.820007324218750,
107                                     123.570007324218750,
108                                     124.320007324218750,
109                                     125.070007324218750,
110                                     125.820007324218750,
111                                     126.570007324218750,
112                                     127.320007324218750,
113                                     128.070007324218750,
114                                     128.820007324218750,
115                                     129.570007324218750,
116                                     130.320007324218750,
117                                     131.070007324218750,
118                                     131.820007324218750,
119                                     135.054992675781250,
120                                     136.054992675781250,
121                                     137.054992675781250,
122                                     138.054992675781250,
123                                     139.054992675781250,
124                                     140.054992675781250,
125                                     141.054992675781250,
126                                     142.054992675781250,
127                                     143.054992675781250,
128                                     144.054992675781250,
129                                     145.054992675781250,
130                                     146.054992675781250,
131                                     147.054992675781250,
132                                     148.054992675781250,
133                                     149.054992675781250,
134                                     150.054992675781250,
135                                     151.054992675781250,
136                                     152.054992675781250,
137                                     153.054992675781250,
138                                     154.054992675781250,
139                                     155.054992675781250,
140                                     156.054992675781250,
141                                     157.054992675781250,
142                                     158.054992675781250,
143                                     159.054992675781250,
144                                     160.054992675781250,
145                                     161.054992675781250,
146                                     162.054992675781250,
147                                     163.054992675781250,
148                                     164.054992675781250,
149                                     165.054992675781250,
150                                     166.054992675781250,
151                                     167.054992675781250,
152                                     168.054992675781250,
153                                     169.054992675781250,
154                                     170.054992675781250,
155                                     171.054992675781250,
156                                     172.054992675781250,
157                                     173.054992675781250,
158                                     174.054992675781250,
159                                     175.054992675781250,
160                                     176.054992675781250,
161                                     177.054992675781250,
162                                     178.054992675781250,
163                                     179.054992675781250,
164                                     180.054992675781250,
165                                     181.054992675781250,
166                                     182.054992675781250,
167                                     183.054992675781250,
168                                     184.054992675781250,
169                                     185.054992675781250,
170                                     186.054992675781250,
171                                     187.054992675781250,
172                                     188.054992675781250,
173                                     189.054992675781250,
174                                     190.054992675781250,
175                                     191.054992675781250,
176                                     192.054992675781250,
177                                     193.054992675781250,
178                                     194.054992675781250,
179                                     195.054992675781250,
180                                     196.054992675781250,
181                                     197.054992675781250,
182                                     198.054992675781250,
183                                     199.054992675781250,
184                                     200.054992675781250,
185                                     201.054992675781250,
186                                     202.054992675781250,
187                                     203.054992675781250,
188                                     204.054992675781250,
189                                     205.054992675781250,
190                                     206.054992675781250,
191                                     207.054992675781250,
192                                     208.054992675781250,
193                                     209.054992675781250,
194                                     210.054992675781250,
195                                     211.054992675781250,
196                                     212.054992675781250,
197                                     213.054992675781250,
198                                     214.054992675781250,
199                                     215.054992675781250,
200                                     216.054992675781250,
201                                     217.054992675781250,
202                                     218.054992675781250,
203                                     219.054992675781250,
204                                     220.054992675781250,
205                                     221.054992675781250,
206                                     222.054992675781250,
207                                     223.054992675781250,
208                                     224.054992675781250,
209                                     225.054992675781250,
210                                     226.054992675781250,
211                                     227.054992675781250,
212                                     228.054992675781250,
213                                     229.054992675781250,
214                                     230.054992675781250,
215                                     231.054992675781250,
216                                     232.054992675781250,
217                                     233.054992675781250,
218                                     234.054992675781250,
219                                     235.054992675781250,
220                                     236.054992675781250,
221                                     237.054992675781250,
222                                     238.054992675781250,
223                                     239.054992675781250,
224                                     240.054992675781250,
225                                     241.054992675781250,
226                                     242.054992675781250,
227                                     243.054992675781250,
228                                     244.054992675781250,
229                                     245.054992675781250,
230                                     246.054992675781250,
231 };
232
233 Int_t AliL3Transform::fNPads[176] = {67,
234                                      67,
235                                      67,
236                                      69,
237                                      69,
238                                      69,
239                                      71,
240                                      71,
241                                      71,
242                                      73,
243                                      73,
244                                      73,
245                                      75,
246                                      75,
247                                      75,
248                                      77,
249                                      77,
250                                      77,
251                                      79,
252                                      79,
253                                      79,
254                                      81,
255                                      81,
256                                      81,
257                                      83,
258                                      83,
259                                      83,
260                                      85,
261                                      85,
262                                      85,
263                                      87,
264                                      87,
265                                      87,
266                                      89,
267                                      89,
268                                      89,
269                                      91,
270                                      91,
271                                      91,
272                                      93,
273                                      93,
274                                      93,
275                                      95,
276                                      95,
277                                      95,
278                                      97,
279                                      97,
280                                      97,
281                                      99,
282                                      99,
283                                      99,
284                                      101,
285                                      101,
286                                      101,
287                                      103,
288                                      103,
289                                      103,
290                                      105,
291                                      105,
292                                      105,
293                                      107,
294                                      107,
295                                      107,
296                                      109,
297                                      73,
298                                      75,
299                                      75,
300                                      75,
301                                      77,
302                                      77,
303                                      77,
304                                      77,
305                                      79,
306                                      79,
307                                      79,
308                                      81,
309                                      81,
310                                      81,
311                                      83,
312                                      83,
313                                      83,
314                                      83,
315                                      85,
316                                      85,
317                                      85,
318                                      87,
319                                      87,
320                                      87,
321                                      87,
322                                      89,
323                                      89,
324                                      89,
325                                      91,
326                                      91,
327                                      91,
328                                      93,
329                                      93,
330                                      93,
331                                      93,
332                                      95,
333                                      95,
334                                      95,
335                                      97,
336                                      97,
337                                      97,
338                                      97,
339                                      99,
340                                      99,
341                                      99,
342                                      101,
343                                      101,
344                                      101,
345                                      103,
346                                      103,
347                                      103,
348                                      103,
349                                      105,
350                                      105,
351                                      105,
352                                      107,
353                                      107,
354                                      107,
355                                      107,
356                                      109,
357                                      109,
358                                      109,
359                                      111,
360                                      111,
361                                      111,
362                                      113,
363                                      113,
364                                      113,
365                                      113,
366                                      115,
367                                      115,
368                                      115,
369                                      117,
370                                      117,
371                                      117,
372                                      117,
373                                      119,
374                                      119,
375                                      119,
376                                      121,
377                                      121,
378                                      121,
379                                      123,
380                                      123,
381                                      123,
382                                      123,
383                                      125,
384                                      125,
385                                      125,
386                                      127,
387                                      127,
388                                      127,
389                                      127,
390                                      129,
391                                      129,
392                                      129,
393                                      131,
394                                      131,
395                                      131,
396                                      133,
397                                      133,
398                                      133,
399                                      133,
400                                      135,
401                                      135,
402                                      135,
403                                      137,
404                                      137,
405                                      137,
406                                      137,
407                                      139,
408                                      139,
409 };
410
411
412
413 void AliL3Transform::Init(const Char_t* path)
414 {
415   //Overwrite the parameters with values stored in file "l3transform.config" in path.
416   //If file does not exist, old default values will be used.
417   
418   Char_t *pathname=new Char_t[1024];
419   strcpy(pathname,path);
420   strcat(pathname,"/l3transform.config");
421   
422   FILE *fptr=fopen(pathname,"r");
423   if(!fptr){
424     LOG(AliL3Log::kWarning,"AliL3Transform::Init","File Open")
425       <<"Pointer to Config File \""<<pathname<<"\" 0x0!"<<ENDLOG;
426     return;
427   }
428
429   Char_t d1[250], d2[100], d3[100];
430   Int_t dummy=0;
431   Double_t ddummy=0.0;
432
433   while(!feof(fptr)) {
434     fscanf(fptr,"%s",d1);
435
436     if(strcmp(d1,"fNTimeBins")==0){fscanf(fptr,"%s %d %s",d2,&dummy,d3);fNTimeBins=(Int_t)dummy;}
437     else if(strcmp(d1,"fNRowLow")==0){fscanf(fptr,"%s %d %s",d2,&dummy,d3);fNRowLow=(Int_t)dummy;}    
438     else if(strcmp(d1,"fNRowUp")==0){fscanf(fptr,"%s %d %s",d2,&dummy,d3);fNRowUp=(Int_t)dummy;}
439     else if(strcmp(d1,"fNSectorLow")==0){fscanf(fptr,"%s %d %s",d2,&dummy,d3);fNSectorLow=(Int_t)dummy;}
440     else if(strcmp(d1,"fNSectorUp")==0){fscanf(fptr,"%s %d %s",d2,&dummy,d3);fNSectorUp=(Int_t)dummy;}
441     else if(strcmp(d1,"fNSector")==0){fscanf(fptr,"%s %d %s",d2,&dummy,d3);fNSector=(Int_t)dummy;}
442     else if(strcmp(d1,"fPadPitchWidthLow")==0){fscanf(fptr,"%s %lf %s",d2,&ddummy,d3);fPadPitchWidthLow=(Double_t)ddummy;}
443     else if(strcmp(d1,"fPadPitchWidthUp")==0){fscanf(fptr,"%s %lf %s",d2,&ddummy,d3);fPadPitchWidthUp=(Double_t)ddummy;}
444     else if(strcmp(d1,"fZWidth")==0){fscanf(fptr,"%s %lf %s",d2,&ddummy,d3);fZWidth=(Double_t)ddummy;}
445     else if(strcmp(d1,"fZSigma")==0){fscanf(fptr,"%s %lf %s",d2,&ddummy,d3);fZSigma=(Double_t)ddummy;}
446     else if(strcmp(d1,"fZLength")==0){fscanf(fptr,"%s %lf %s",d2,&ddummy,d3);fZLength=(Double_t)ddummy;}
447     else if(strcmp(d1,"fZOffset")==0){fscanf(fptr,"%s %lf %s",d2,&ddummy,d3);fZOffset=(Double_t)ddummy;}
448     else if(strcmp(d1,"fNSlice")==0){fscanf(fptr,"%s %d %s",d2,&dummy,d3);fNSlice=(Int_t)dummy;}
449     else if(strcmp(d1,"fNRow")==0){
450       fscanf(fptr,"%s %d %s",d2,&dummy,d3);fNRow=(Int_t)dummy;
451       if(fNRow!=176){
452         LOG(AliL3Log::kError,"AliL3Transform::Init","Overflow")<<"Number of PadRows should be 176! Check and fgrep the code for 176 to see the consequences of this major change!"<<ENDLOG;
453       }
454     }
455     else if(strcmp(d1,"fNRotShift")==0){fscanf(fptr,"%s %lf %s",d2,&ddummy,d3);fNRotShift=(Double_t)ddummy;}
456     else if(strcmp(d1,"fPi")==0){fscanf(fptr,"%s %lf %s",d2,&ddummy,d3);fPi=(Double_t)ddummy;}
457     else if(strcmp(d1,"fX[0]")==0){
458       fscanf(fptr,"%s %lf %s",d2,&ddummy,d3);fX[0]=(Double_t)ddummy;
459       for(Int_t i=1;i<fNRow;i++){fscanf(fptr,"%s %s %lf %s",d1,d2,&ddummy,d3);fX[i]=(Double_t)ddummy;}
460     }
461     else if(strcmp(d1,"fNPads[0]")==0){
462       fscanf(fptr,"%s %d %s",d2,&dummy,d3);fNPads[0]=(Int_t)dummy;
463       for(Int_t i=1;i<fNRow;i++){fscanf(fptr,"%s %s %d %s",d1,d2,&dummy,d3);fNPads[i]=(Int_t)dummy;}
464     }
465   }
466
467   fclose(fptr);
468   delete pathname;
469   fVersion=1; //new version
470
471 }
472
473
474 Double_t AliL3Transform::GetEta(Float_t *xyz)
475 {
476   Double_t r3 = sqrt(xyz[0]*xyz[0]+xyz[1]*xyz[1]+xyz[2]*xyz[2]);
477   Double_t eta = 0.5 * log((r3+xyz[2])/(r3-xyz[2]));
478   return eta;
479 }
480
481 void AliL3Transform::XYZtoRPhiEta(Float_t *rpe, Float_t *xyz)
482 {
483   rpe[0] = sqrt(xyz[0]*xyz[0]+xyz[1]*xyz[1]+xyz[2]*xyz[2]);
484   rpe[1] = atan2(xyz[1],xyz[0]);
485   rpe[2] = 0.5 * log((rpe[0]+xyz[2])/(rpe[0]-xyz[2]));
486 }
487
488 Double_t AliL3Transform::GetEta(Int_t padrow,Int_t pad,Int_t time)
489 {
490   Float_t xyz[3];
491   Int_t sector,row;
492   Slice2Sector(0,padrow,sector,row);
493   Raw2Local(xyz,sector,row,pad,time);
494   
495   return GetEta(xyz);
496 }
497
498 Double_t AliL3Transform::GetPhi(Float_t *xyz)
499 {
500   Double_t phi = atan2(xyz[1],xyz[0]);
501   //if(phi<0) phi=phi+2*TMath::Pi();
502   return phi;
503 }
504
505 Bool_t AliL3Transform::Slice2Sector(Int_t slice, Int_t slicerow, Int_t & sector, Int_t &row)
506 {
507   if(slicerow<0&&slicerow>=fNRow) return kFALSE;
508   if(slice<0||slice>=fNSlice) return kFALSE;
509
510   if(slicerow<fNRowLow){
511     sector = slice;
512     row    = slicerow;
513   }
514   else {
515     sector = slice+fNSlice;
516     row    = slicerow-fNRowLow;
517   }
518   return kTRUE;
519 }
520
521 Bool_t AliL3Transform::Sector2Slice(Int_t & slice, Int_t  sector)
522 {
523   if(sector<0||sector>=fNSector) return kFALSE;
524   if(sector<fNSectorLow) slice = sector;
525   else          slice = sector - fNSectorLow;
526   return kTRUE;
527 }
528
529 Bool_t AliL3Transform::Sector2Slice(Int_t & slice, Int_t & slicerow,Int_t  sector, Int_t row)
530 {
531   if(sector<0||sector>=fNSector||row<0) return kFALSE;
532   if(sector<fNSectorLow){
533     if(row>=fNRowLow) return kFALSE;
534     slice = sector;
535     slicerow = row;
536   }
537   else{
538     if(row>=fNRowUp) return kFALSE;
539     slice = sector - fNSectorLow;
540     slicerow = row + fNRowLow;
541   }
542   return kTRUE;
543 }
544
545 Double_t AliL3Transform::Row2X(Int_t slicerow){
546   if(slicerow<0||slicerow>=fNRow) return 0;
547   return fX[slicerow];
548 }
549
550 void AliL3Transform::Local2Global(Float_t *xyz,Int_t slice)
551 {
552   //Transformation to global coordinate system
553   Float_t x0 = xyz[0];
554   Float_t y0 = xyz[1];
555   Float_t cs,sn;
556   cs = cos( (2*fPi/18) * (slice+fNRotShift) );
557   sn = sin( (2*fPi/18) * (slice+fNRotShift) );
558   xyz[0]=x0*cs-y0*sn;
559   xyz[1]=x0*sn+y0*cs;
560   xyz[2]=xyz[2];//global z=local z
561 }
562
563 void AliL3Transform::Local2GlobalAngle(Float_t *angle,Int_t slice){
564   angle[0] = fmod(angle[0]+(slice+fNRotShift)*(2*fPi/18),2*fPi);
565 }
566
567 void AliL3Transform::Global2LocalAngle(Float_t *angle,Int_t slice){
568   angle[0] = angle[0]-(slice+fNRotShift)*(2*fPi/18);
569   if(angle[0]<0) angle[0]+=2*fPi;
570 }
571
572 void AliL3Transform::Raw2Local(Float_t *xyz,Int_t sector,Int_t row,Float_t pad,Float_t time)
573 {
574   //Transformation from rawdata to local coordinate system
575   
576   Int_t slice,slicerow;
577   Sector2Slice(slice, slicerow, sector, row);  
578
579   //X-Value
580   xyz[0]=Row2X(slicerow); 
581
582   //Y-Value
583   Int_t npads= fNPads[slicerow];
584   if(sector<fNSectorLow)
585     xyz[1]=(pad-0.5*(npads-1))*fPadPitchWidthLow;
586   else
587     xyz[1]=(pad-0.5*(npads-1))*fPadPitchWidthUp;
588
589   //Z-Value (remember PULSA Delay)
590   //xyz[2]=fZWidth*time-3.*fZSigma;
591   xyz[2]=fZWidth*time-fZOffset;
592   if(slice < 18)
593     xyz[2]=fZLength-xyz[2];
594   else
595     xyz[2]=xyz[2]-fZLength;
596   
597 }
598
599 void AliL3Transform::Local2Global(Float_t *xyz,Int_t sector,Int_t row)
600 {
601   //Transformation to global coordinate system
602   Int_t slice,slicerow;
603   Sector2Slice(slice, slicerow, sector, row);  
604   Float_t r=Row2X(slicerow);
605   Float_t cs = cos( (2*fPi/18) * (slice+fNRotShift) );
606   Float_t sn = sin( (2*fPi/18) * (slice+fNRotShift) );
607
608   xyz[0]=r*cs-xyz[1]*sn;
609   xyz[1]=r*sn+xyz[1]*cs;
610   xyz[2]=xyz[2];//global z=local z
611 }
612
613 Double_t AliL3Transform::GetMaxY(Int_t slicerow)
614 {
615
616  if(slicerow < fNRowLow)
617      return fPadPitchWidthLow*fNPads[slicerow]/2; 
618  
619  else
620      return fPadPitchWidthUp*fNPads[slicerow]/2;
621
622 }
623
624 void AliL3Transform::Global2Local(Float_t *xyz,Int_t sector,Bool_t isSlice)
625 {
626   
627   Int_t slice;
628   if(!isSlice)
629     Sector2Slice(slice, sector);  
630   else
631     slice = sector;
632   Float_t cs = cos( (2*fPi/18) * (slice+fNRotShift) );
633   Float_t sn = sin( (2*fPi/18) * (slice+fNRotShift) );
634   Float_t x1 = xyz[0]*cs + xyz[1]*sn;
635   Float_t y1 = -xyz[0]*sn + xyz[1]*cs;
636   xyz[0] = x1;
637   xyz[1] = y1;
638 }
639
640 void AliL3Transform::Raw2Global(Float_t *xyz,Int_t sector,Int_t row,Float_t pad,Float_t time)
641 {
642   //Transformation from raw to global coordinates
643   
644   Raw2Local(xyz,sector,row,pad,time);
645   Local2Global(xyz,sector,row);
646 }
647
648 void AliL3Transform::Local2Raw(Float_t *xyz,Int_t sector,Int_t row)
649 {
650   //Transformation from local coordinates to raw
651
652   Int_t slice,slicerow;
653   Sector2Slice(slice, slicerow, sector, row);  
654    
655   if(sector<fNSectorLow)
656     xyz[1]=xyz[1]/fPadPitchWidthLow+0.5*(fNPads[slicerow]-1);
657   else
658     xyz[1]=xyz[1]/fPadPitchWidthUp+0.5*(fNPads[slicerow]-1);
659   Int_t sign=-1;
660   Int_t nis=fNSectorLow;
661   Int_t nos=fNSectorUp;
662  
663   if ((sector<nis/2) || ((sector-nis)<nos/2)) sign=1; 
664   xyz[2]=fZLength-sign*xyz[2];
665   xyz[2]=(xyz[2]+fZOffset)/fZWidth;
666
667 }
668
669 void AliL3Transform::Global2Raw(Float_t *xyz,Int_t sector,Int_t row)
670 {
671   //Transformation from global coordinates to raw. 
672
673   Global2Local(xyz,sector);
674   Local2Raw(xyz,sector,row);
675
676 }