]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSgeom.cxx
Initialization of AliITSgeom from TGeo for the reconstruction pass. AliITSLoader...
[u/mrichter/AliRoot.git] / ITS / AliITSgeom.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 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////
19 // ITS geometry manipulation routines.                               //
20 // Created April 15 1999.                                            //
21 // version: 0.0.0                                                    //
22 // By: Bjorn S. Nilsen                                               //
23 // version: 0.0.1                                                    //
24 // Updated May 27 1999.                                              //
25 // Added Cylindrical random and global based changes.                //
26 // Added  function PrintComparison.                                  //
27 // Modified and added functions Feb. 7 2006                          //
28 ///////////////////////////////////////////////////////////////////////
29
30
31 ////////////////////////////////////////////////////////////////////////
32 //     The local coordinate system by, default, is show in the following
33 // figures. Also shown are the ladder numbering scheme.
34 //Begin_Html
35 /*
36 <img src="picts/ITS/AliITSgeomMatrix_L1.gif">
37 </pre>
38 <br clear=left>
39 <font size=+2 color=blue>
40 <p>This shows the relative geometry differences between the ALICE Global
41 coordinate system and the local detector coordinate system.
42 </font>
43 <pre>
44
45 <pre>
46 <img src="picts/ITS/its1+2_convention_front_5.gif">
47 </pre>
48 <br clear=left>
49 <font size=+2 color=blue>
50 <p>This shows the front view of the SPDs and the orientation of the local
51 pixel coordinate system. Note that the inner pixel layer has its y coordinate
52 in the opposite direction from all of the other layers.
53 </font>
54 <pre>
55
56 <pre>
57 <img src="picts/ITS/its3+4_convention_front_5.gif">
58 </pre>
59 <br clear=left>
60 <font size=+2 color=blue>
61 <p>This shows the front view of the SDDs and the orientation of the local
62 pixel coordinate system.
63 </font>
64 <pre>
65
66 <pre>
67 <img src="picts/ITS/its5+6_convention_front_5.gif">
68 </pre>
69 <br clear=left>
70 <font size=+2 color=blue>
71 <p>This shows the front view of the SSDs and the orientation of the local
72 pixel coordinate system.
73 </font>
74 <pre>
75 */
76 //End_Html
77 //
78 ////////////////////////////////////////////////////////////////////////
79
80 ////////////////////////////////////////////////////////////////////////
81 //
82 // version: 0
83 // Written by Bjorn S. Nilsen
84 //
85 // Data Members:
86 //
87 // TString    fVersion 
88 //     Transformation version.
89 // Int_t      fTrans
90 //     Flag to keep track of which transformation 
91 // Int_t      fNmodules
92 //      The total number of modules
93 // Int_t fNlayers
94 //     The number of ITS layers for this geometry. By default this
95 //  is 6, but can be modified by the creator function if there are
96 // more layers defined.
97 //
98 // TArrayI fNlad
99 //     A pointer to an array fNlayers long containing the number of 
100 // ladders for each layer. This array is typically created and filled 
101 // by the AliITSgeom creator function.
102 //
103 // TArrayI fNdet
104 //     A pointer to an array fNlayers long containing the number of
105 // active detector volumes for each ladder. This array is typically
106 // created and filled by the AliITSgeom creator function.
107 //
108 // TObjArray fGm containing objects of type AliITSgeomMatrix
109 //     A pointer to an array of AliITSgeomMatrix classes. One element 
110 // per module (detector) in the ITS. AliITSgeomMatrix basicly contains
111 // all of the necessary information about the detector and it's coordinate
112 // transformations.
113 //
114 // TObjArray fShape containting objects of type AliITSgeom
115 //     A pointer to an array of TObjects containing the detailed shape
116 // information for each type of detector used in the ITS. For example
117 // I have created AliITSgeomSPD, AliITSgeomSDD, and 
118 // AliITSsegmenttionSSD as example structures, derived from TObjects, 
119 // to hold the detector information. I would recommend that one element 
120 // in each of these structures, that which describes the shape of the 
121 // active volume, be one of the ROOT classes derived from TShape. In this 
122 // way it would be easy to have the display program display the correct 
123 // active ITS volumes. See the example classes AliITSgeomSPD, 
124 // AliITSgeomSDD, and AliITSgeomSSD for a more detailed 
125 // example.
126 ////////////////////////////////////////////////////////////////////////
127 #include <Riostream.h>
128
129 #include <TRandom.h>
130 #include <TSystem.h>
131 #include <TArrayI.h>
132
133 #include "AliITSgeomSPD.h"
134 #include "AliITSgeomSDD.h"
135 #include "AliITSgeomSSD.h"
136 #include "AliITSgeom.h"
137 #include "AliLog.h"
138
139 ClassImp(AliITSgeom)
140
141 //______________________________________________________________________
142 AliITSgeom::AliITSgeom():
143 TObject(),
144 fVersion("GEANT"),// Transformation version.
145 fTrans(0),       // Flag to keep track of which transformation 
146 fNmodules(0),    // The total number of modules
147 fNlayers(0),     // The number of layers.
148 fNlad(),         //[] Array of the number of ladders/layer(layer)
149 fNdet(),         //[] Array of the number of detector/ladder(layer)
150 fGm(0,0),        // Structure of translation. and rotation.
151 fShape(0,0)      // Array of shapes and detector information.
152 {
153     //     The default constructor for the AliITSgeom class. It, by default,
154     // sets fNlayers to zero and zeros all pointers.
155     // Do not allocate anything zero everything.
156     // Inputs:
157     //    none.
158     // Outputs:
159     //    none.
160     // Return:
161     //    a zeroed AliITSgeom object.
162
163     fGm.SetOwner(kTRUE);
164     fShape.SetOwner(kTRUE);
165     return;
166 }
167 //______________________________________________________________________
168 AliITSgeom::AliITSgeom(Int_t itype,Int_t nlayers,const Int_t *nlads,
169                        const Int_t *ndets,Int_t mods):
170 TObject(),
171 fVersion("GEANT"),    // Transformation version.
172 fTrans(itype),       // Flag to keep track of which transformation 
173 fNmodules(mods),     // The total number of modules
174 fNlayers(nlayers),   // The number of layers.
175 fNlad(nlayers,nlads),//[] Array of the number of ladders/layer(layer)
176 fNdet(nlayers,ndets),//[] Array of the number of detector/ladder(layer)
177 fGm(mods,0),         // Structure of translation. and rotation.
178 fShape(5,0)          // Array of shapes and detector information.
179 {
180     //     A simple constructor to set basic geometry class variables
181     // Inputs:
182     //      Int_t itype   the type of transformation kept.
183     //                    bit 0 => Standard GEANT
184     //                    bit 1 => ITS tracking
185     //                    bit 2 => A change in the coordinate system has been made.
186     //                    others are still to be defined as needed.
187     //      Int_t nlayers The number of ITS layers also set the size of the arrays
188     //      Int_t *nlads  an array of the number of ladders for each layer. This
189     //                    array must be nlayers long.
190     //      Int_t *ndets  an array of the number of detectors per ladder for each
191     //                    layer. This array must be nlayers long.
192     //      Int_t mods    The number of modules. Typically the sum of all the 
193     //                    detectors on every layer and ladder.
194     // Outputs:
195     //     none
196     // Return:
197     //     A properly inilized AliITSgeom object.
198
199     fGm.SetOwner(kTRUE);
200     fShape.SetOwner(kTRUE);
201     return;
202 }
203 //______________________________________________________________________
204 void AliITSgeom::Init(Int_t itype,Int_t nlayers,const Int_t *nlads,
205                       const Int_t *ndets,Int_t mods){
206     //     A simple Inilizer to set basic geometry class variables
207     // Inputs:
208     //      Int_t itype   the type of transformation kept.
209     //                    bit 0 => Standard GEANT
210     //                    bit 1 => ITS tracking
211     //                    bit 2 => A change in the coordinate system has been made.
212     //                    others are still to be defined as needed.
213     //      Int_t nlayers The number of ITS layers also set the size of the arrays
214     //      Int_t *nlads  an array of the number of ladders for each layer. This
215     //                    array must be nlayers long.
216     //      Int_t *ndets  an array of the number of detectors per ladder for each
217     //                    layer. This array must be nlayers long.
218     //      Int_t mods    The number of modules. Typically the sum of all the 
219     //                    detectors on every layer and ladder.
220     // Outputs:
221     //     none
222     // Return:
223     //     A properly inilized AliITSgeom object.
224
225     fVersion  = "GEANT";     // Transformation version.
226     fTrans    = itype;       // Flag to keep track of which transformation 
227     fNmodules = mods;        // The total number of modules
228     fNlayers  = nlayers;     // The number of layers.
229     fNlad.Set(nlayers,nlads);//[] Array of the number of ladders/layer(layer)
230     fNdet.Set(nlayers,ndets);//[] Array of the number of detector/ladder(layer)
231     fGm.Clear();
232     fGm.Expand(mods);        // Structure of translation. and rotation.
233     fGm.SetOwner(kTRUE);
234     fShape.Clear();
235     fShape.Expand(5);         // Array of shapes and detector information.
236     fShape.SetOwner(kTRUE);
237     return;
238 }
239 //______________________________________________________________________
240 void AliITSgeom::CreateMatrix(Int_t mod,Int_t lay,Int_t lad,Int_t det,
241                               AliITSDetector idet,const Double_t tran[3],
242                               const Double_t rot[10]){
243     // Given the translation vector tran[3] and the rotation matrix rot[1],
244     // this function creates and adds to the TObject Array fGm the
245     // AliITSgeomMatrix object.
246     // The rot[10] matrix is set up like:
247     /*   / rot[0]  rot[1]  rot[2] \
248     //  |  rot[3]  rot[4]  rot[5]  |
249     //   \ rot[6]  rot[7]  rot[8] /  if(rot[9]!=0) then the Identity matrix
250     // is used regardless of the values in rot[0]-rot[8].
251     */
252     // Inputs:
253     //    Int_t           mod     The module number. The location in TObjArray
254     //    Int_t           lay     The layer where this module is
255     //    Int_t           lad     On which ladder this module is
256     //    Int_t           det     Which detector on this ladder this module is
257     //    AliITSDetector idet     The type of detector see AliITSgeom.h
258     //    Double_t       tran[3]  The translation vector
259     //    Double_t       rot[10]  The rotation matrix.
260     // Outputs:
261     //    none
262     // Return:
263     //    none.
264     Int_t id[3];
265     Double_t r[3][3] = {{1.0,0.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0}};
266
267     if(mod<0||mod>=fGm.GetSize()){ 
268         Error("CreateMatrix","mod=%d is out of bounds max value=%d",mod,
269               fGm.GetSize());
270         return;
271     } // end if
272     delete fGm.At(mod);
273     id[0] = lay; id[1] = lad; id[2] = det;
274     if(rot[9]!=0.0) { // null rotation
275         r[0][0] = rot[0]; r[0][1] = rot[1]; r[0][2] = rot[2];
276         r[1][0] = rot[3]; r[1][1] = rot[4]; r[1][2] = rot[5];
277         r[2][0] = rot[6]; r[2][1] = rot[7]; r[2][2] = rot[8];
278     } // end if
279     fGm.AddAt(new AliITSgeomMatrix(idet,id,r,tran),mod);
280 }
281 //______________________________________________________________________
282 AliITSgeom::~AliITSgeom(){
283     //     The destructor for the AliITSgeom class. If the arrays fNlad,
284     // fNdet, or fGm have had memory allocated to them, there pointer values
285     // are non zero, then this memory space is freed and they are set
286     // to zero. In addition, fNlayers is set to zero. The destruction of
287     // TObjArray fShape is, by default, handled by the TObjArray destructor.
288     // Inputs:
289     //    none.
290     // Outputs:
291     //    none.
292     // Return:
293     //    none.
294
295     return;
296 }
297 //______________________________________________________________________
298 void AliITSgeom::ReadNewFile(const char *filename){
299     // It is generally preferred to define the geometry in AliITSgeom
300     // directly from the GEANT geometry, see AliITSvPPRasymm.cxx for
301     // and example. Under some circumstances this may not be possible.
302     // This function will read in a formatted file for all of the
303     // information needed to define the geometry in AliITSgeom.
304     // Unlike the older file format, this file may contain comments
305     // and the order of the data does not need to be completely
306     // respected. A file can be created using the function WriteNewFile
307     // defined below.
308     // Inputs:
309     //    const char *filename The file name of the file to be read in.
310     // Outputs:
311     //     none
312     // Return:
313     //     none.
314     Int_t ncmd=9;
315     const char *cmda[]={"Version"        ,"fTrans"  ,"fNmodules",
316                         "fNlayers"       ,"fNladers","fNdetectors",
317                         "fNDetectorTypes","fShape"  ,"Matrix"};
318     Int_t i,j,lNdetTypes,ldet;
319     char cmd[20],c;
320     AliITSgeomMatrix *m=0;
321     ifstream *fp=0;
322     char *filtmp=0;
323     Bool_t arrayGm = kFALSE, arrayShape = kFALSE;
324
325     filtmp = gSystem->ExpandPathName(filename);
326     AliInfo(Form("Reading New .det file %s",filtmp));
327     fp = new ifstream(filtmp,ios::in);  // open file to write
328     while(fp->get(c)!=NULL){ // for ever loop
329         if(c==' ') continue; // remove blanks
330         if(c=='\n') continue;
331         if(c=='#' || c=='!') {while(fp->get(c)) if(c=='\n') break; continue;}
332         if(c=='/'){
333             fp->get(c);{
334                 if(c=='/') {while(fp->get(c)) if(c=='\n') break; continue;}
335                 if(c=='*'){
336                   NotYet:
337                     while(fp->get(c)) if(c=='*') break;
338                     fp->get(c);{
339                         if(c=='/') continue;
340                         goto NotYet;
341                     } //
342                 } // end if c=='*'
343             } // end if second /
344         } // end if first /
345         fp->putback(c);
346         *fp >> cmd;
347         for(i=0;i<ncmd;i++) if(strcmp(cmd,cmda[i])==0) break;
348         switch (i){
349         case 0:   // Version
350             *fp >> j;
351             fVersion.Resize(j);
352             for(j=0;j<fVersion.Length();j++) *fp >> fVersion[j];
353             break;
354         case 1:  // fTrans
355             *fp >> fTrans;
356             break;
357         case 2:  // fNModules
358             *fp >> fNmodules;
359             fGm.Clear();
360             fGm.Expand(fNmodules);
361             fGm.SetOwner(kTRUE);
362             arrayGm = kTRUE;
363             break;
364         case 3:  // fNlayers
365             *fp >> fNlayers;
366             fNlad.Set(fNlayers);
367             fNdet.Set(fNlayers);
368             break;
369         case 4:  // fNladers
370             for(j=0;j<fNlayers;j++) *fp >> fNlad[j];
371             break;
372         case 5:  // fNdetectors
373             for(j=0;j<fNlayers;j++) *fp >> fNdet[j];
374             break;
375         case 6:  // fNDetectorTypes
376             *fp >> lNdetTypes;
377             fShape.Clear();
378             fShape.Expand(lNdetTypes);
379             fShape.SetOwner(kTRUE);
380             arrayShape = kTRUE;
381             break;
382         case 7:  // fShape
383             *fp >> ldet;
384             if(!arrayShape) fShape.Expand(5);
385             fShape.SetOwner(kTRUE);
386             switch (ldet){
387             case kSPD :{
388                 AliITSgeomSPD *spd = new AliITSgeomSPD();
389                 *fp >> *spd;
390                 ReSetShape(ldet,spd);
391             } break;
392             case kSDD : case kSDDp:{
393                 AliITSgeomSDD *sdd = new AliITSgeomSDD();
394                 *fp >> *sdd;
395                 ReSetShape(ldet,sdd);
396             }break;
397             case kSSD : case kSSDp :{
398                 AliITSgeomSSD *ssd = new AliITSgeomSSD();
399                 *fp >> *ssd;
400                 ReSetShape(ldet,ssd);
401             }break;
402             default:{
403                 AliError(Form("Unknown fShape type number=%d c=%c",ldet,c));
404                 while(fp->get(c)) if(c=='\n') break; // skip to end of line.
405             }break;
406             } // end switch
407             break;
408         case 8:  // Matrix
409             *fp >> ldet;
410             if(!arrayGm){
411                 fGm.Clear();
412                 fGm.Expand(2270);
413                 arrayGm = kTRUE;
414             } // end if
415             if(ldet<0||ldet>=fGm.GetSize()){
416                 Error("ReadNewFile","ldet<0||ldet>=fGm.GetSize()=%d",
417                       ldet,fGm.GetSize());
418                 return;
419             } // end if
420             delete fGm.At(ldet);
421             fGm.AddAt((TObject*)new AliITSgeomMatrix(),ldet);
422             m = (AliITSgeomMatrix*) fGm.At(ldet);
423             *fp >> *m;
424             m = 0;
425             break;
426         default:
427             AliError(Form("ReadNewFile","Data line i=%d c=%c",i,c));
428             while(fp->get(c)) if(c=='\n') break; // skip this line
429             break;
430         } // end switch i
431     } // end while
432     delete fp;
433
434     return;
435 }
436 //______________________________________________________________________
437 void AliITSgeom::WriteNewFile(const char *filename){
438     // Writes AliITSgeom, AliITSgeomMatrix, and the defined 
439     // AliITSgeomS*D classes to a file in a format that 
440     // is more readable and commendable.
441     // Inputs:
442     //     const char *filename The file name of the file to be write to.
443     // Outputs:
444     //     none
445     // Return:
446     //     none
447     ofstream *fp;
448     Int_t i;
449     char *filtmp;
450
451     filtmp = gSystem->ExpandPathName(filename);
452     fp = new ofstream(filtmp,ios::out);  // open file to write
453     *fp << "//Comment lines begin with two //, one #, or one !" << endl;
454     *fp << "#Blank lines are skipped including /* and */ sections." << endl;
455     *fp << "!and, in principle the order of the lines is not important" <<endl;
456     *fp << "/* In AliITSgeom.h are defined an enumerated type called" << endl;
457     *fp << " AliITSDetectors These are kSPD=" << (Int_t) kSPD ;
458     *fp << ", kSDD=" << (Int_t) kSDD << ", kSSD=" << (Int_t) kSSD;
459     *fp << ", kSSDp=" << (Int_t) kSSDp << ", and kSDDp=" << (Int_t) kSDDp;
460     *fp << "*/" << endl;
461     *fp << "Version "<< fVersion.Length()<<" " << fVersion.Data() << endl;//This should be consistent
462                                            // with the geometry version.
463     *fp << "fTrans " << fTrans << endl;
464     *fp << "fNmodules " << fNmodules << endl;
465     *fp << "fNlayers " << fNlayers << endl;
466     *fp << "fNladers ";
467     for(i=0;i<fNlayers;i++) *fp << fNlad[i] << " ";
468     *fp << endl;
469     *fp << "fNdetectors ";
470     for(i=0;i<fNlayers;i++) *fp << fNdet[i] << " ";
471     *fp << endl;
472     *fp << "fNDetectorTypes " << fShape.GetEntriesFast() << endl;
473     for(i=0;i<fShape.GetEntriesFast();i++){
474         if(!IsShapeDefined(i)) continue; // only print out used shapes.
475         switch (i){
476         case kSPD :
477             *fp << "fShape " << (Int_t) kSPD << " ";
478             *fp << *((AliITSgeomSPD*)(fShape.At(i)));
479             break;
480         case kSDD :
481             *fp << "fShape " << (Int_t) kSDD << " ";
482             *fp << *((AliITSgeomSDD*)(fShape.At(i)));
483             break;
484         case kSSD : case kSSDp :
485             *fp << "fShape " << i << " ";
486             *fp << *((AliITSgeomSSD*)(fShape.At(i)));
487             break;
488         default:
489             Error("AliITSgeom::WriteNewFile","Unknown Shape value");
490         } // end switch (i)
491     } // end for i
492     for(i=0;i<fNmodules;i++){
493         *fp << "Matrix " << i << " ";
494         *fp << *GetGeomMatrix(i);
495     } // end for i
496     *fp << "//End of File" << endl;;
497
498     delete fp;
499     return;
500 }
501 //______________________________________________________________________
502 AliITSgeom::AliITSgeom(const char *filename):
503 TObject(),
504 fVersion("test"),// Transformation version.
505 fTrans(0),       // Flag to keep track of which transformation 
506 fNmodules(0),    // The total number of modules
507 fNlayers(0),     // The number of layers.
508 fNlad(),         // TArrayI of the number of ladders/layer(layer)
509 fNdet(),         // TArrayI of the number of detector/ladder(layer)
510 fGm(0,0),        // TObjArray Structure of translation. and rotation.
511 fShape(0,0)      // TObjArray of detector geom.
512 {
513     //     The constructor for the AliITSgeom class. All of the data to fill
514     // this structure is read in from the file given my the input filename.
515     // Inputs:
516     //    const char *filename The file name of the file to be read in.
517     // Outputs:
518     //    none
519     // Return:
520     //    An AliITSgeom class initialized from a file.
521     FILE     *pf=0;
522     Int_t    i,lm=0,id[3];
523     Int_t    l,a,d;
524     Float_t  x,y,z,o,p,q,r,s,t;
525     Double_t rot6[6],tran[3];
526     char     buf[200],*buff=0; // input character buffer;
527     char    *filtmp;
528
529     filtmp = gSystem->ExpandPathName(filename);
530     Info("AliITSgeom","reading old .det file %s",filtmp);
531     fVersion="GEANT5";
532     pf = fopen(filtmp,"r");
533
534     fNlayers = 6; // set default number of ladders
535   TryAgain:
536     fNlad.Set(fNlayers);
537     fNdet.Set(fNlayers);
538     fNmodules = 0;
539     // find the number of ladders and detectors in this geometry.
540     for(i=0;i<fNlayers;i++){fNlad[i]=fNdet[i]=0;} // zero out arrays
541     while(fgets(buf,200,pf)!=NULL){ // for ever loop
542         for(i=0;i<200;i++)if(buf[i]!=' '){ // remove blank spaces.
543             buff = &(buf[i]);
544             break;
545         } // end for i
546         // remove blank lines and comments.
547         if(buff[0]=='\n'||buff[0]=='#'||buff[0]=='!'||
548            (buff[0]=='/'&&buff[1]=='/')) continue;
549         if(isalpha(buff[0])) { // must be the new file formated file.
550             fclose(pf);
551             ReadNewFile(filename);
552             return;
553         } // end if isalpha(buff[0])
554         sscanf(buff,"%d %d %d %f %f %f %f %f %f %f %f %f",
555                &l,&a,&d,&x,&y,&z,&o,&p,&q,&r,&s,&t);
556         if(l>lm) lm = l;
557         if(l<1 || l>fNlayers) {
558             printf("error in file %s layer=%d min. is 1 max is %d\n",
559                    filename,l,fNlayers);
560             continue;
561         }// end if l
562         fNmodules++;
563         if(l<=fNlayers&&fNlad[l-1]<a) fNlad[l-1] = a;
564         if(l<=fNlayers&&fNdet[l-1]<d) fNdet[l-1] = d;
565     } // end while ever loop
566     if(lm>fNlayers){
567         fNlayers = lm;
568         goto TryAgain;
569     } // end if lm>fNlayers
570     // counted the number of ladders and detectors now allocate space.
571     fGm.Expand(fNmodules);
572     fGm.SetOwner(kTRUE);
573     fShape.SetOwner(kTRUE);
574
575     // Set up Shapes for a default configuration of 6 layers.
576     fTrans   = 0; // standard GEANT global/local coordinate system.
577     // prepare to read in transforms
578     lm = 0; // reuse lm as counter of modules.
579     rewind(pf); // start over reading file
580     while(fgets(buf,200,pf)!=NULL){ // for ever loop
581         for(i=0;i<200;i++)if(buf[i]!=' '){ // remove blank spaces.
582             buff = &(buf[i]);
583             break;
584         } // end for i
585         // remove blank lines and comments.
586         if(buff[0]=='\n'||buff[0]=='#'||buff[0]=='!'||
587            (buff[0]=='/'&&buff[1]=='/')) continue;
588         x = y = z = o = p = q = r = s = t = 0.0;
589         sscanf(buff,"%d %d %d %f %f %f %f %f %f %f %f %f",
590                &l,&a,&d,&x,&y,&z,&o,&p,&q,&r,&s,&t);
591         if(l<1 || l>fNlayers) {
592             Warning("AliITSgeom","error in file %s layer=%d min. is 1 max is %d",
593                    filename,l,fNlayers);
594             continue;
595         }// end if l
596         id[0] = l;id[1] = a;id[2] = d;
597         tran[0] = tran[1] = tran[2]  = 0.0;
598         tran[0] = (Double_t)x;tran[1] = (Double_t)y;tran[2] = (Double_t)z;
599         rot6[0] = rot6[1] = rot6[2] = rot6[3] = rot6[4] = rot6[5] =0.0;
600         rot6[0] = (Double_t)o;rot6[1] = (Double_t)p;rot6[2] = (Double_t)q;
601         rot6[3] = (Double_t)r;rot6[4] = (Double_t)s;rot6[5] = (Double_t)t;
602         if(lm<0||lm>=fGm.GetSize()){
603             Error("AliITSgeom(filename)","lm<0||lm>=fGm.GetSize()=%d",
604                   lm,fGm.GetSize());
605             return;
606         } // end if
607         switch (l){
608         case 1: case 2: // layer 1 or2 SPD
609             fGm.AddAt(new AliITSgeomMatrix(rot6,kSPD,id,tran),lm++);
610             break;
611         case 3: case 4: // layer 3 or 4 SDD
612             fGm.AddAt(new AliITSgeomMatrix(rot6,kSDD,id,tran),lm++);
613             break;
614         case 5: case 6: // layer 5 or 6 SSD
615             fGm.AddAt(new AliITSgeomMatrix(rot6,kSSD,id,tran),lm++);
616             break;
617         } // end switch
618     } // end while ever loop
619     fclose(pf);
620 }
621 //______________________________________________________________________
622 AliITSgeom::AliITSgeom(const AliITSgeom &source) : TObject(source){
623     //     The copy constructor for the AliITSgeom class. It calls the
624     // = operator function. See the = operator function for more details.
625     // Inputs:
626     //     AliITSgeom &source  The AliITSgeom class with which to make this
627     //                         a copy of.
628     // Outputs:
629     //     none.
630     // Return:
631     //     none.
632
633     *this = source;  // Just use the = operator for now.
634     return;
635 }
636 //______________________________________________________________________
637 AliITSgeom& AliITSgeom::operator=(const AliITSgeom &source){
638     //     The = operator function for the AliITSgeom class. It makes an
639     // independent copy of the class in such a way that any changes made
640     // to the copied class will not affect the source class in any way.
641     // This is required for many ITS alignment studies where the copied
642     // class is then modified by introducing some misalignment.
643     // Inputs:
644     //     AliITSgeom &source  The AliITSgeom class with which to make this
645     //                         a copy of.
646     // Outputs:
647     //     none.
648     // Return:
649     //     *this The a new copy of source.
650     Int_t i;
651
652     if(this == &source) return *this; // don't assign to ones self.
653
654     // if there is an old structure allocated delete it first.
655     this->fGm.Clear();
656     this->fShape.Clear();
657
658     this->fVersion  = source.fVersion;
659     this->fTrans    = source.fTrans;
660     this->fNmodules = source.fNmodules;
661     this->fNlayers = source.fNlayers;
662     this->fNlad.Set(fNlayers,source.fNlad.GetArray());
663     this->fNdet.Set(fNlayers,source.fNdet.GetArray());
664     this->fShape.Expand(source.fShape.GetEntriesFast());
665     for(i=0;i<source.fShape.GetEntriesFast();i++)
666         this->fShape.AddAt(new TObject(*(source.fShape.At(i))),i);
667     this->fShape.SetOwner(kTRUE);
668     this->fGm.Expand(this->fNmodules);
669     this->fGm.SetOwner(kTRUE);
670     for(i=0;i<this->fNmodules;i++)
671         if(i<0||i>=fGm.GetSize()){
672             Error("ReadNewFile","i<0||i>=fGm.GetSize()=%d",
673                   i,fGm.GetSize());
674             return *this;
675         } // end if
676         this->fGm.AddAt(new TObject(*(source.fGm.At(i))),i);
677     return *this;
678 }
679 //______________________________________________________________________
680 Int_t AliITSgeom::GetModuleIndex(Int_t lay,Int_t lad,Int_t det){
681     //      This routine computes the module index number from the layer,
682     // ladder, and detector numbers. The number of ladders and detectors
683     // per layer is determined when this geometry package is constructed,
684     // see AliITSgeom(const char *filename) for specifics.
685     // Inputs:
686     //    Int_t lay  The layer number. Starting from 1.
687     //    Int_t lad  The ladder number. Starting from 1.
688     //    Int_t det  The detector number. Starting from 1.
689     // Outputs:
690     //    none.
691     // Return:
692     //    the module index number, starting from zero.
693     Int_t i,j,k,id[3];
694
695     i = fNdet[lay-1] * (lad-1) + det - 1;
696     j = 0;
697     for(k=0;k<lay-1;k++) j += fNdet[k]*fNlad[k];
698     i = i+j;
699     if(i>=fNmodules) return -1;
700     GetGeomMatrix(i)->GetIndex(id);
701     if(id[0]==lay&&id[1]==lad&&id[2]==det) return i;
702     // Array of modules fGm is not in expected order. Search for this index
703     for(i=0;i<fNmodules;i++){
704         GetGeomMatrix(i)->GetIndex(id);
705         if(id[0]==lay&&id[1]==lad&&id[2]==det) return i;
706     } // end for i
707     // This layer ladder and detector combination does not exist return -1.
708     return -1;
709 }
710 //______________________________________________________________________
711 void AliITSgeom::GetModuleId(Int_t index,Int_t &lay,Int_t &lad,Int_t &det){
712     //      This routine computes the layer, ladder and detector number 
713     // given the module index number. The number of ladders and detectors
714     // per layer is determined when this geometry package is constructed,
715     // see AliITSgeom(const char *filename) for specifics.
716     // Inputs:
717     //     Int_t index  The module index number, starting from zero.
718     // Outputs:
719     //     Int_t lay    The layer number. Starting from 1.
720     //     Int_t lad    The ladder number. Starting from 1.
721     //     Int_t det    The detector number. Starting from 1.
722     // Return:
723     //     none.
724     Int_t id[3];
725     AliITSgeomMatrix *g = GetGeomMatrix(index);
726
727     if (g == 0x0){
728         Error("GetModuleId","Can not get GeoMatrix for index = %d",index);
729         lay = -1; lad = -1; det = -1;
730     }else{
731         g->GetIndex(id);
732         lay = id[0]; lad = id[1]; det = id[2];
733     }// End if
734     return;
735     // The old way kept for posterity.
736 /*
737     Int_t i,j,k;
738     j = 0;
739     for(k=0;k<fNlayers;k++){
740         j += fNdet[k]*fNlad[k];
741         if(j>index)break;
742     } // end for k
743     lay = k+1;
744     i = index -j + fNdet[k]*fNlad[k];
745     j = 0;
746     for(k=0;k<fNlad[lay-1];k++){
747         j += fNdet[lay-1];
748         if(j>i)break;
749     } // end for k
750     lad = k+1;
751     det = 1+i-fNdet[lay-1]*k;
752     return;
753 */
754 }
755 //______________________________________________________________________
756 Int_t AliITSgeom::GetNDetTypes(Int_t &max){
757     // Finds and returns the number of detector types used and the
758     // maximum detector type value. Only counts id >=0 (no undefined
759     // values. See AliITSgeom.h for list of AliITSDetecor enumerated types.
760     // Inputs:
761     //    none.
762     // Outputs:
763     //    The maximum detector type used
764     // Return:
765     //    The number of detector types used
766     Int_t i,*n,id;
767
768     max = -1;
769     for(i=0;i<GetIndexMax();i++){
770         id = GetModuleType(i);
771         if(id>max) max=id;
772     } // end for i
773     n = new Int_t[max+1];
774     for(i=0;i<max;i++) n[i] = 0;
775     for(i=0;i<GetIndexMax();i++){
776         id = GetModuleType(i);
777         if(id>-1)n[id]++; // note id=-1 => undefined.
778     } // end for i
779     id = 0;
780     for(i=0;i<max;i++) if(n[i]!=0) id++;
781     delete[] n;
782     return id+1;
783 }
784 //______________________________________________________________________
785 Int_t AliITSgeom::GetNDetTypes(TArrayI &maxs,AliITSDetector *types){
786     // Finds and returns the number of detector types used and the
787     // number of each detector type. Only counts id >=0 (no undefined
788     // values. See AliITSgeom.h for list of AliITSDetecor enumerated types.
789     // Inputs:
790     //    none.
791     // Outputs:
792     //    The maximum detector type used
793     // Return:
794     //    The number of detector types used
795     Int_t i,j,*n,id,max;
796
797     max = -1;
798     for(i=0;i<GetIndexMax();i++){
799         id = GetModuleType(i);
800         if(id>max) max=id;
801     } // end for i
802     n = new Int_t[max+1];
803     for(i=0;i<max;i++) n[i] = 0;
804     for(i=0;i<GetIndexMax();i++){
805         id = GetModuleType(i);
806         if(id>-1)n[id]++; // note id=-1 => undefined.
807     } // end for i
808     id = 0;
809     for(i=0;i<=max;i++) if(n[i]!=0) id++;
810     maxs.Set(id);
811     j = 0;
812     for(i=0;i<=max;i++) if(n[i]!=0){
813         maxs[j] = n[i];
814         types[j++] = (AliITSDetector) i;
815     } // end for i/end if
816     delete[] n;
817     return id;
818 }
819 //______________________________________________________________________
820 Int_t AliITSgeom::GetStartDet(Int_t dtype){
821     // returns the starting module index value for a give type of detector id.
822     // This assumes that the detector types are different on different layers
823     // and that they are not mixed up.
824     // Inputs:
825     //    Int_t dtype A detector type number. 0 for SPD, 1 for SDD, and 2 for SSD.
826     // Outputs:
827     //    none.
828     // Return:
829     //    the module index for the first occurrence of that detector type.
830
831     switch(dtype){
832     case 0:
833         return GetModuleIndex(1,1,1);
834         break;
835     case 1:
836         return GetModuleIndex(3,1,1);
837         break;
838     case 2:
839         return GetModuleIndex(5,1,1);
840         break;
841     default:
842         Warning("GetStartDet","undefined detector type %d",dtype);
843         return 0;
844     } // end switch
845
846     Warning("GetStartDet","undefined detector type %d",dtype);
847     return 0;
848 }
849 //______________________________________________________________________
850 Int_t AliITSgeom::GetLastDet(Int_t dtype){
851     // returns the last module index value for a give type of detector id.
852     // This assumes that the detector types are different on different layers
853     // and that they are not mixed up.
854     // Inputs:
855     //     Int_t dtype A detector type number. 0 for SPD, 1 for SDD, and 2 for SSD.
856     // Outputs:
857     // Return:
858     //     the module index for the last occurrence of that detector type.
859
860     switch(dtype){
861     case 0:
862         return GetLastSPD();
863         break;
864     case 1:
865         return GetLastSDD();
866         break;
867     case 2:
868         return GetLastSSD();
869         break;
870     default:
871         Warning("GetLastDet","undefined detector type %d",dtype);
872         return 0;
873     } // end switch
874
875     Warning("GetLastDet","undefined detector type %d",dtype);
876     return 0;
877 }
878 //______________________________________________________________________
879 void AliITSgeom::PrintComparison(FILE *fp,AliITSgeom *other){
880     //     This function was primarily created for diagnostic reasons. It
881     // print to a file pointed to by the file pointer fp the difference
882     // between two AliITSgeom classes. The format of the file is basically,
883     // define d? to be the difference between the same element of the two
884     // classes. For example dfrx = this->GetGeomMatrix(i)->frx 
885     // - other->GetGeomMatrix(i)->frx.
886     // if(at least one of dfx0, dfy0, dfz0,dfrx,dfry,dfrz are non zero) then
887     // print layer ladder detector dfx0 dfy0 dfz0 dfrx dfry dfrz
888     // if(at least one of the 9 elements of dfr[] are non zero) then print
889     // layer ladder detector dfr[0] dfr[1] dfr[2]
890     //                       dfr[3] dfr[4] dfr[5]
891     //                       dfr[6] dfr[7] dfr[8]
892     // Only non zero values are printed to save space. The differences are
893     // typical written to a file because there are usually a lot of numbers
894     // printed out and it is usually easier to read them in some nice editor
895     // rather than zooming quickly past you on a screen. fprintf is used to
896     // do the printing. The fShapeIndex difference is not printed at this time.
897     // Inputs:
898     //    FILE *fp           A file pointer to an opened file for writing in which
899     //                       the results of the comparison will be written.
900     //    AliITSgeom *other  The other AliITSgeom class to which this one is
901     //                       being compared.
902     // Outputs:
903     //    none.
904     // Return:
905     //    none.
906     Int_t    i,j,idt[3],ido[3];
907     Double_t tt[3],to[3];  // translation
908     Double_t rt[3],ro[3];  // phi in radians
909     Double_t mt[3][3],mo[3][3]; // matrices
910     AliITSgeomMatrix *gt,*go;
911     Bool_t   t;
912
913     for(i=0;i<this->fNmodules;i++){
914         gt  =  this->GetGeomMatrix(i);
915         go  = other->GetGeomMatrix(i);
916         gt->GetIndex(idt);
917         go->GetIndex(ido);
918         t = kFALSE;
919         for(i=0;i<3;i++) t = t&&idt[i]!=ido[i];
920         if(t) fprintf(fp,"%4.4d %1.1d %2.2d %2.2d %1.1d %2.2d %2.2d\n",i,
921                       idt[0],idt[1],idt[2],ido[0],ido[1],ido[2]);
922         gt->GetTranslation(tt);
923         go->GetTranslation(to);
924         gt->GetAngles(rt);
925         go->GetAngles(ro);
926         t = kFALSE;
927         for(i=0;i<3;i++) t = t&&tt[i]!=to[i];
928         if(t) fprintf(fp,"%1.1d %2.2d %2.2d dTrans=%f %f %f drot=%f %f %f\n",
929                       idt[0],idt[1],idt[2],
930                       tt[0]-to[0],tt[1]-to[1],tt[2]-to[2],
931                       rt[0]-ro[0],rt[1]-ro[1],rt[2]-ro[2]);
932         t = kFALSE;
933         gt->GetMatrix(mt);
934         go->GetMatrix(mo);
935         for(i=0;i<3;i++)for(j=0;j<3;j++)  t = mt[i][j] != mo[i][j];
936         if(t){
937             fprintf(fp,"%1.1d %2.2d %2.2d dfr= %e %e %e\n",
938                     idt[0],idt[1],idt[2],
939                     mt[0][0]-mo[0][0],mt[0][1]-mo[0][1],mt[0][2]-mo[0][2]);
940             fprintf(fp,"        dfr= %e %e %e\n",
941                     mt[1][0]-mo[1][0],mt[1][1]-mo[1][1],mt[1][2]-mo[1][2]);
942             fprintf(fp,"        dfr= %e %e %e\n",
943                     mt[2][0]-mo[2][0],mt[2][1]-mo[2][1],mt[2][2]-mo[2][2]);
944         } // end if t
945     } // end for i
946     return;
947 }
948 //______________________________________________________________________
949 void AliITSgeom::PrintData(FILE *fp,Int_t lay,Int_t lad,Int_t det){
950     //     This function prints out the coordinate transformations for
951     // the particular detector defined by layer, ladder, and detector
952     // to the file pointed to by the File pointer fp. fprintf statements
953     // are used to print out the numbers. The format is
954     // layer ladder detector Trans= fx0 fy0 fz0 rot= frx fry frz 
955     // Shape=fShapeIndex
956     //                         dfr= fr[0] fr[1] fr[2]
957     //                         dfr= fr[3] fr[4] fr[5]
958     //                         dfr= fr[6] fr[7] fr[8]
959     // By indicating which detector, some control over the information 
960     // is given to the user. The output it written to the file pointed
961     // to by the file pointer fp. This can be set to stdout if you want.
962     // Inputs:
963     //     FILE *fp           A file pointer to an opened file for writing in which
964     //                        the results of the comparison will be written.
965     //     Int_t lay          The layer number. Starting from 1.
966     //     Int_t lad          The ladder number. Starting from 1.
967     //     Int_t det          The detector number. Starting from 1.
968     // Outputs:
969     //     none
970     // Return:
971     //     none.
972     AliITSgeomMatrix *gt;
973     Double_t t[3],r[3],m[3][3];
974
975     gt = this->GetGeomMatrix(GetModuleIndex(lay,lad,det));
976     gt->GetTranslation(t);
977     gt->GetAngles(r);
978     fprintf(fp,"%1.1d %2.2d %2.2d Trans=%f %f %f rot=%f %f %f Shape=%d\n",
979             lay,lad,det,t[0],t[1],t[2],r[0],r[1],r[2],
980             gt->GetDetectorIndex());
981     gt->GetMatrix(m);
982     fprintf(fp,"        dfr= %e %e %e\n",m[0][0],m[0][1],m[0][2]);
983     fprintf(fp,"        dfr= %e %e %e\n",m[1][0],m[1][1],m[1][2]);
984     fprintf(fp,"        dfr= %e %e %e\n",m[2][0],m[2][1],m[2][2]);
985     return;
986 }
987 //______________________________________________________________________
988 ofstream & AliITSgeom::PrintGeom(ofstream &rb){
989     //     Stream out an object of class AliITSgeom to standard output.
990     // Intputs:
991     //     ofstream &rb    The output streaming buffer.
992     // Outputs:
993     //     none.
994     // Return:
995     //     ofstream &rb    The output streaming buffer.
996     Int_t i,nshapes;
997
998     rb.setf(ios::scientific);
999     rb << fTrans << " ";
1000     rb << fNmodules << " ";
1001     rb << fNlayers << " ";
1002     for(i=0;i<fNlayers;i++) rb << fNlad[i] << " ";
1003     for(i=0;i<fNlayers;i++) rb << fNdet[i] << "\n";
1004     for(i=0;i<fNmodules;i++) {
1005         rb <<setprecision(16) << *(GetGeomMatrix(i)) << "\n";
1006     } // end for i
1007     nshapes = fShape.GetEntries();
1008     rb << nshapes <<endl;
1009     for(i=0;i<nshapes;i++) if(fShape.At(i)!=0) switch (i){
1010     case kSPD:
1011         rb << kSPD <<","<< (AliITSgeomSPD*)(fShape.At(kSPD));
1012         break;
1013     case kSDD:
1014         rb << kSDD <<","<< (AliITSgeomSDD*)(fShape.At(kSDD));
1015         break;
1016     case kSSD:
1017         rb << kSSD <<","<< (AliITSgeomSSD*)(fShape.At(kSSD));
1018         break;
1019     case kSSDp:
1020         rb << kSSDp <<","<< (AliITSgeomSSD*)(fShape.At(kSSDp));
1021         break;
1022     case kSDDp:
1023         rb << kSDDp <<","<< (AliITSgeomSDD*)(fShape.At(kSDDp));
1024         break;
1025     } // end for i / switch
1026     return rb;
1027 }
1028 //______________________________________________________________________
1029 ifstream & AliITSgeom::ReadGeom(ifstream &rb){
1030     //     Stream in an object of class AliITSgeom from standard input.
1031     // Intputs:
1032     //     ifstream &rb    The input streaming buffer.
1033     // Outputs:
1034     //     none.
1035     // Return:
1036     //     ifstream &rb    The input streaming buffer.
1037     Int_t i,j;
1038
1039     fGm.Clear();
1040
1041     rb >> fTrans >> fNmodules >> fNlayers;
1042     fNlad.Set(fNlayers);
1043     fNdet.Set(fNlayers);
1044     for(i=0;i<fNlayers;i++) rb >> fNlad[i];
1045     for(i=0;i<fNlayers;i++) rb >> fNdet[i];
1046     fGm.Expand(fNmodules);
1047     fGm.SetOwner(kTRUE);
1048     for(i=0;i<fNmodules;i++){
1049         if(i<0||i>=fGm.GetSize()){
1050             Error("ReadGeom","i<0||i>=fGm.GetSize()=%d",
1051                   i,fGm.GetSize());
1052             return rb;
1053         } // end if
1054         fGm.AddAt(new AliITSgeomMatrix,i);
1055         rb >> *(GetGeomMatrix(i));
1056     } // end for i
1057     rb >> i;
1058     fShape.Expand(i);
1059     fShape.SetOwner(kTRUE);
1060     for(i=0;i<fShape.GetEntries();i++) {
1061         rb >> j;
1062         switch (j){
1063         case kSPD:{
1064             AliITSgeomSPD *s = new AliITSgeomSPD();
1065             rb >> *s;
1066             fShape.AddAt(s,kSPD);}
1067             break;
1068         case kSDD:{
1069             AliITSgeomSDD *s = new AliITSgeomSDD();
1070             rb >> *s;
1071             fShape.AddAt(s,kSDD);}
1072             break;
1073         case kSSD:{
1074             AliITSgeomSSD *s = new AliITSgeomSSD();
1075             rb >> *s;
1076             fShape.AddAt(s,kSSD);}
1077             break;
1078         case kSSDp:{
1079             AliITSgeomSSD *s = new AliITSgeomSSD();
1080             rb >> *s;
1081             fShape.AddAt(s,kSSDp);}
1082             break;
1083         case kSDDp:{
1084             AliITSgeomSDD *s = new AliITSgeomSDD();
1085             rb >> *s;
1086             fShape.AddAt(s,kSDDp);}
1087             break;
1088         } // end  switch
1089     } //  end for i
1090     return rb;
1091 }
1092 //______________________________________________________________________
1093 //     The following routines modify the transformation of "this"
1094 // geometry transformations in a number of different ways.
1095 //______________________________________________________________________
1096 void AliITSgeom::GlobalChange(const Float_t *tran,const Float_t *rot){
1097     //     This function performs a Cartesian translation and rotation of
1098     // the full ITS from its default position by an amount determined by
1099     // the three element arrays tran and rot. If every element
1100     // of tran and rot are zero then there is no change made
1101     // the geometry. The change is global in that the exact same translation
1102     // and rotation is done to every detector element in the exact same way.
1103     // The units of the translation are those of the Monte Carlo, usually cm,
1104     // and those of the rotation are in radians. The elements of tran
1105     // are tran[0] = x, tran[1] = y, and tran[2] = z.
1106     // The elements of rot are rot[0] = rx, rot[1] = ry, and
1107     // rot[2] = rz. A change in x will move the hole ITS in the ALICE
1108     // global x direction, the same for a change in y. A change in z will
1109     // result in a translation of the ITS as a hole up or down the beam line.
1110     // A change in the angles will result in the inclination of the ITS with
1111     // respect to the beam line, except for an effective rotation about the
1112     // beam axis which will just rotate the ITS as a hole about the beam axis.
1113     // Intputs:
1114     //     Float_t *tran   A 3 element array representing the global translations.
1115     //                     the elements are x,y,z in cm.
1116     //     Float_t *rot    A 3 element array representing the global rotation
1117     //                     angles about the three axis x,y,z in radians
1118     // Outputs:
1119     //     none.
1120     // Return:
1121     //     none.
1122     Int_t    i,j;
1123     Double_t t[3],r[3];
1124     AliITSgeomMatrix *g;
1125
1126     fTrans = (fTrans && 0xfffd) + 2;  // set bit 1 true.
1127     for(i=0;i<fNmodules;i++){
1128         g = this->GetGeomMatrix(i);
1129         g->GetTranslation(t);
1130         g->GetAngles(r);
1131         for(j=0;j<3;j++){
1132             t[j] += tran[j];
1133             r[j] += rot[j];
1134         } // end for j
1135         g->SetTranslation(t);
1136         g->SetAngles(r);
1137     } // end for i
1138     return;
1139 }
1140 //______________________________________________________________________
1141 void AliITSgeom::GlobalCylindericalChange(const Float_t *tran,
1142                                           const Float_t *rot){
1143     //     This function performs a cylindrical translation and rotation of
1144     // each ITS element by a fixed about in radius, rphi, and z from its
1145     // default position by an amount determined by the three element arrays
1146     // tran and rot. If every element of tran and
1147     // rot are zero then there is no change made the geometry. The
1148     // change is global in that the exact same distance change in translation
1149     // and rotation is done to every detector element in the exact same way.
1150     // The units of the translation are those of the Monte Carlo, usually cm,
1151     // and those of the rotation are in radians. The elements of tran
1152     // are tran[0] = r, tran[1] = rphi, and tran[2] = z.
1153     // The elements of rot are rot[0] = rx, rot[1] = ry, and
1154     // rot[2] = rz. A change in r will results in the increase of the
1155     // radius of each layer by the same about. A change in rphi will results in
1156     // the rotation of each layer by a different angle but by the same
1157     // circumferential distance. A change in z will result in a translation
1158     // of the ITS as a hole up or down the beam line. A change in the angles
1159     // will result in the inclination of the ITS with respect to the beam
1160     // line, except for an effective rotation about the beam axis which will
1161     // just rotate the ITS as a hole about the beam axis.
1162     // Intputs:
1163     //     Float_t *tran   A 3 element array representing the global translations.
1164     //                     the elements are r,theta,z in cm/radians.
1165     //     Float_t *rot    A 3 element array representing the global rotation
1166     //                     angles about the three axis x,y,z in radians
1167     // Outputs:
1168     //     none.
1169     // Return:
1170     //     none.
1171     Int_t    i,j;
1172     Double_t t[3],ro[3],r,r0,phi,rphi;
1173     AliITSgeomMatrix *g;
1174
1175     fTrans = (fTrans && 0xfffd) + 2;  // set bit 1 true.
1176     for(i=0;i<fNmodules;i++){
1177         g = this->GetGeomMatrix(i);
1178         g->GetTranslation(t);
1179         g->GetAngles(ro);
1180         r = r0= TMath::Hypot(t[1],t[0]);
1181         phi   = TMath::ATan2(t[1],t[0]);
1182         rphi  = r0*phi;
1183         r    += tran[0];
1184         rphi += tran[1];
1185         phi   = rphi/r0;
1186         t[0]  = r*TMath::Cos(phi);
1187         t[1]  = r*TMath::Sin(phi);
1188         t[2] += tran[2];
1189         for(j=0;j<3;j++){
1190             ro[j] += rot[j];
1191         } // end for j
1192         g->SetTranslation(t);
1193         g->SetAngles(ro);
1194     } // end for i
1195     return;
1196 }
1197 //______________________________________________________________________
1198 void AliITSgeom::RandomChange(const Float_t *stran,const Float_t *srot){
1199     //     This function performs a Gaussian random displacement and/or
1200     // rotation about the present global position of each active
1201     // volume/detector of the ITS. The sigma of the random displacement
1202     // is determined by the three element array stran, for the
1203     // x y and z translations, and the three element array srot,
1204     // for the three rotation about the axis x y and z.
1205     // Intputs:
1206     //     Float_t *stran  A 3 element array representing the global translations
1207     //                     variances. The elements are x,y,z in cm.
1208     //     Float_t *srot   A 3 element array representing the global rotation
1209     //                     angles variances about the three axis x,y,z in radians.
1210     // Outputs:
1211     //     none.
1212     // Return:
1213     //     none.
1214     Int_t    i,j;
1215     Double_t t[3],r[3];
1216     AliITSgeomMatrix *g;
1217
1218     fTrans = (fTrans && 0xfffd) + 2;  // set bit 1 true.
1219     for(i=0;i<fNmodules;i++){
1220         g = this->GetGeomMatrix(i);
1221         g->GetTranslation(t);
1222         g->GetAngles(r);
1223         for(j=0;j<3;j++){
1224             t[j] += gRandom->Gaus(0.0,stran[j]);
1225             r[j] += gRandom->Gaus(0.0, srot[j]);
1226         } // end for j
1227         g->SetTranslation(t);
1228         g->SetAngles(r);
1229     } // end for i
1230     return;
1231 }
1232 //______________________________________________________________________
1233 void AliITSgeom::RandomCylindericalChange(const Float_t *stran,
1234                                           const Float_t *srot){
1235     //     This function performs a Gaussian random displacement and/or
1236     // rotation about the present global position of each active
1237     // volume/detector of the ITS. The sigma of the random displacement
1238     // is determined by the three element array stran, for the
1239     // r rphi and z translations, and the three element array srot,
1240     // for the three rotation about the axis x y and z. This random change
1241     // in detector position allow for the simulation of a random uncertainty
1242     // in the detector positions of the ITS.
1243     // Intputs:
1244     //     Float_t *stran  A 3 element array representing the global translations
1245     //                     variances. The elements are r,theta,z in cm/radians.
1246     //     Float_t *srot   A 3 element array representing the global rotation
1247     //                     angles variances about the three axis x,y,z in radians.
1248     // Outputs:
1249     //     none.
1250     // Return:
1251     //     none.
1252     Int_t    i,j;
1253     Double_t t[3],ro[3],r,r0,phi,rphi;
1254     TRandom ran;
1255     AliITSgeomMatrix *g;
1256
1257     fTrans = (fTrans && 0xfffd) + 2;  // set bit 1 true.
1258     for(i=0;i<fNmodules;i++){
1259         g = this->GetGeomMatrix(i);
1260         g->GetTranslation(t);
1261         g->GetAngles(ro);
1262         r = r0= TMath::Hypot(t[1],t[0]);
1263         phi   = TMath::ATan2(t[1],t[0]);
1264         rphi  = r0*phi;
1265         r    += ran.Gaus(0.0,stran[0]);
1266         rphi += ran.Gaus(0.0,stran[1]);
1267         phi   = rphi/r0;
1268         t[0]  = r*TMath::Cos(phi);
1269         t[1]  = r*TMath::Sin(phi);
1270         t[2] += ran.Gaus(0.0,stran[2]);
1271         for(j=0;j<3;j++){
1272             ro[j] += ran.Gaus(0.0, srot[j]);
1273         } // end for j
1274         g->SetTranslation(t);
1275         g->SetAngles(ro);
1276     } // end for i
1277     return;
1278 }
1279 //______________________________________________________________________
1280 void AliITSgeom::GeantToTracking(AliITSgeom &source){
1281     //     Copy the geometry data but change it to go between the ALICE
1282     // Global coordinate system to that used by the ITS tracking. A slightly
1283     // different coordinate system is used when tracking. This coordinate 
1284     // system is only relevant when the geometry represents the cylindrical
1285     // ALICE ITS geometry. For tracking the Z axis is left alone but X-> -Y
1286     // and Y-> X such that X always points out of the ITS cylinder for every
1287     // layer including layer 1 (where the detectors are mounted upside down).
1288     //Begin_Html
1289     /*
1290       <img src="picts/ITS/AliITSgeomMatrix_T1.gif">
1291     */
1292     //End_Html
1293     // Input:
1294     //     AliITSgeom &source  The AliITSgeom class with which to make this
1295     //                         a copy of.
1296     // Output:
1297     //     none.
1298     // Return:
1299     //     none.
1300     Int_t    i,j,k,l,id[3];
1301     Double_t r0[3][3],r1[3][3];
1302     Double_t a0[3][3] = {{0.,+1.,0.},{-1.,0.,0.},{0.,0.,+1.}};
1303     Double_t a1[3][3] = {{0.,-1.,0.},{+1.,0.,0.},{0.,0.,+1.}};
1304
1305     *this = source;  // copy everything
1306     for(i=0;i<GetIndexMax();i++){
1307         GetGeomMatrix(i)->GetIndex(id);
1308         GetGeomMatrix(i)->GetMatrix(r0);
1309         if(id[0]==1){ // Layer 1 is treated different from the others.
1310             for(j=0;j<3;j++) for(k=0;k<3;k++){
1311                 r1[j][k] = 0.;
1312                 for(l=0;l<3;l++) r1[j][k] += a0[j][l]*r0[l][k];
1313             } // end for j,k
1314         }else{
1315             for(j=0;j<3;j++) for(k=0;k<3;k++){
1316                 r1[j][k] = 0.;
1317                 for(l=0;l<3;l++) r1[j][k] += a1[j][l]*r0[l][k];
1318             } // end for j,k
1319         } // end if
1320         GetGeomMatrix(i)->SetMatrix(r1);
1321     } // end for i
1322     this->fTrans = (this->fTrans && 0xfffe) + 1;  // set bit 0 true.
1323     return;
1324 }
1325 //______________________________________________________________________
1326 Int_t AliITSgeom::GetNearest(const Double_t g[3],Int_t lay){
1327     //      Finds the Detector (Module) that is nearest the point g [cm] in
1328     // ALICE Global coordinates. If layer !=0 then the search is restricted
1329     // to Detectors (Modules) in that particular layer.
1330     // Inputs:
1331     //     Double_t g[3]  The ALICE Cartesian global coordinate from which the
1332     //                    distance is to be calculated with.
1333     //     Int_t lay      The layer to restrict the search to. If layer=0 then
1334     //                    all layers are searched. Default is lay=0.
1335     // Output:
1336     //     none.
1337     // Return:
1338     //     The module number representing the nearest module.
1339     Int_t    i,l,a,e,in=0;
1340     Double_t d,dn=1.0e10;
1341     Bool_t   t=lay!=0; // skip if lay = 0 default value check all layers.
1342
1343     for(i=0;i<fNmodules;i++){
1344         if(t){GetModuleId(i,l,a,e);if(l!=lay) continue;}
1345         if((d=GetGeomMatrix(i)->Distance2(g))<dn){
1346             dn = d;
1347             in = i;
1348         } // end if
1349     } // end for i
1350     return in;
1351 }
1352 //______________________________________________________________________
1353 void AliITSgeom::GetNearest27(const Double_t g[3],Int_t n[27],Int_t lay){
1354     //      Finds 27 Detectors (Modules) that are nearest the point g [cm] in
1355     // ALICE Global coordinates. If layer !=0 then the search is restricted
1356     // to Detectors (Modules) in that particular layer. The number 27 comes 
1357     // from including the nearest detector and all those around it (up, down,
1358     // left, right, forwards, backwards, and the corners).
1359     // Input:
1360     //     Double_t g[3]  The ALICE Cartesian global coordinate from which the
1361     //                    distance is to be calculated with.
1362     //     Int_t lay      The layer to restrict the search to. If layer=0 then
1363     //                    all layers are searched. Default is lay=0.
1364     // Output:
1365     //     Int_t n[27]    The module number representing the nearest 27 modules
1366     //                    in order.
1367     // Return:
1368     //     none.
1369     Int_t    i,l,a,e,in[27]={0,0,0,0,0,0,0,0,0,
1370                              0,0,0,0,0,0,0,0,0,
1371                              0,0,0,0,0,0,0,0,0,};
1372     Double_t d,dn[27]={1.0e10,1.0e10,1.0e10,1.0e10,1.0e10,1.0e10,
1373                        1.0e10,1.0e10,1.0e10,1.0e10,1.0e10,1.0e10,
1374                        1.0e10,1.0e10,1.0e10,1.0e10,1.0e10,1.0e10,
1375                        1.0e10,1.0e10,1.0e10,1.0e10,1.0e10,1.0e10,
1376                        1.0e10,1.0e10,1.0e10};
1377     Bool_t   t=(lay!=0); // skip if lay = 0 default value check all layers.
1378
1379     for(i=0;i<fNmodules;i++){
1380         if(t){GetModuleId(i,l,a,e);if(l!=lay) continue;}
1381         for(a=0;a<27;a++){
1382             d = GetGeomMatrix(i)->Distance2(g);
1383             if(d<dn[a]){
1384                 for(e=26;e>a;e--){dn[e] = dn[e-1];in[e] = in[e-1];}
1385                 dn[a] = d; in[a] = i;
1386             } // end if d<dn[i]
1387         } // end for a
1388     } // end for i
1389     for(i=0;i<27;i++) n[i] = in[i];
1390 }
1391 //----------------------------------------------------------------------
1392 Double_t AliITSgeom::GetAverageRadiusOfLayer(Int_t layer,Double_t &range){
1393     // Loops over all modules for a given layer and computes the
1394     // average cylindrical radius (about the z axis) and the range of
1395     // radii covered by this layer. Units, [cm] the Alice default unit.
1396     // Input:
1397     //    Int_t layer     The layer for which the average radii is to be found
1398     // Output:
1399     //    Double_t &range The range of radii covered by this layer
1400     // Return:
1401     //    The average radii for this layer.
1402     Double_t r=0.0,rmin=1.0e6,rmax=-1.0,rp,t[3],l[3],dl[3];
1403     Int_t    n=0,i,j,lay,lad,det;
1404
1405     for(i=0;i<GetIndexMax();i++) {
1406         GetModuleId(i,lay,lad,det);
1407         if(lay!=layer) continue;
1408         dl[0] = dl[1] = dl[2] = 0.0;
1409         if(GetShape(i)!=0) {
1410             switch(i){
1411             case 0:{
1412                 dl[0] = ((AliITSgeomSPD*)GetShape(i))->GetDx();
1413                 dl[1] = ((AliITSgeomSPD*)GetShape(i))->GetDy();
1414                 dl[2] = ((AliITSgeomSPD*)GetShape(i))->GetDz();
1415             } break;
1416             case 1: case 4:{
1417                 dl[0] = ((AliITSgeomSDD*)GetShape(i))->GetDx();
1418                 dl[1] = ((AliITSgeomSDD*)GetShape(i))->GetDy();
1419                 dl[2] = ((AliITSgeomSDD*)GetShape(i))->GetDz();
1420             } break;
1421             case 2: case 3:{
1422                 dl[0] = ((AliITSgeomSSD*)GetShape(i))->GetDx();
1423                 dl[1] = ((AliITSgeomSSD*)GetShape(i))->GetDy();
1424                 dl[2] = ((AliITSgeomSSD*)GetShape(i))->GetDz();
1425             } break;
1426             }// end switch.
1427         } // end of
1428         n++;
1429         GetTransCyln(i,t);
1430         rp = t[0];
1431         r += rp;
1432         if(rmin>rp) rmin = rp;
1433         if(rmax<rp) rmax = rp;
1434         for(j=0;j<8;j++){ // loop over the corners
1435             l[0] = dl[0];if(j%2==0) l[0] = -dl[0];
1436             l[1] = dl[1];if(j==2||j==3||j==6||j==7) l[1] = -dl[1];
1437             l[2] = dl[2];if(j>3) l[2] = -dl[2];
1438             LtoG(i,l,t);
1439             rp = TMath::Sqrt(t[0]*t[0]+t[1]*t[1]);
1440             if(rmin>rp) rmin = rp;
1441             if(rmax<rp) rmax = rp;
1442         } // end for j
1443     } // end for i
1444     r /= (Double_t)n;
1445     range = TMath::Max(rmax-r,r-rmin);
1446     return r;
1447 }
1448 //_______________________________________________________________________
1449 void AliITSgeom::DetLToTrackingV2(Int_t md, Float_t xin, Float_t zin, Float_t &yout, Float_t &zout) {
1450
1451   //Conversion from local coordinates on detectors to local
1452   //coordinates used for tracking ("v2")
1453   Float_t x,y,z; Double_t rt[9];GetTrans(md,x,y,z);GetRotMatrix(md,rt);
1454   Double_t al=TMath::ATan2(rt[1],rt[0])+TMath::Pi();
1455   yout=-(-xin+(x*TMath::Cos(al)+y*TMath::Sin(al)));
1456   if(md<(GetModuleIndex(2,1,1)-1))yout*=-1; zout=-zin+(Double_t)z; 
1457 }
1458
1459 //_______________________________________________________________________
1460 void AliITSgeom::TrackingV2ToDetL(Int_t md,Float_t yin,Float_t zin,Float_t &xout,Float_t &zout) {
1461   //Conversion from local coordinates used for tracking ("v2") to
1462   //local detector coordinates
1463   
1464   Float_t x,y,z; Double_t rt[9];GetTrans(md,x,y,z);GetRotMatrix(md,rt);
1465   Double_t al=TMath::ATan2(rt[1],rt[0])+TMath::Pi();
1466   xout=yin;if(md<(GetModuleIndex(2,1,1)-1))xout=-xout;
1467   xout+=(x*TMath::Cos(al)+y*TMath::Sin(al));
1468   zout=-zin+(Double_t)z; 
1469 }