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