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