]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSv11GeomCableFlat.cxx
Using the right AliITSRecPoint constructor for the correct initialization of the...
[u/mrichter/AliRoot.git] / ITS / AliITSv11GeomCableFlat.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
17 //*************************************************************************
18 //   Class for flat cables
19 //
20 // Ludovic Gaudichet                                   gaudichet@to.infn.it
21 //*************************************************************************
22
23
24
25 // General Root includes
26 //#include <Riostream.h>
27 #include <TMath.h>
28 #include <TVectorD.h>
29
30 // Root Geometry includes
31 #include <TGeoManager.h>
32 #include <TGeoVolume.h>
33 #include <TGeoArb8.h>
34 #include <TGeoTube.h>
35 #include <TGeoMatrix.h>
36 #include <TGeoNode.h>
37
38 #include "AliITSv11GeomCableFlat.h"
39
40
41 ClassImp(AliITSv11GeomCableFlat)
42
43 //________________________________________________________________________
44 AliITSv11GeomCableFlat::AliITSv11GeomCableFlat():
45   AliITSv11GeomCable(),
46   fWidth(0),
47   fThick(0),
48   fNlayer(0)
49 {
50   // constructor
51   for (Int_t i=0; i<fgkCableMaxLayer ; i++) {
52     fLayThickness[i] = 0;
53     fTranslation[i]  = 0;
54     fLayColor[i]     = 0;
55     fLayMedia[i]     = 0;  
56  };
57 }
58
59 //________________________________________________________________________
60 AliITSv11GeomCableFlat::
61 AliITSv11GeomCableFlat(const char* name, Double_t width, Double_t thick) :
62   AliITSv11GeomCable(name),
63   fWidth(width),
64   fThick(thick),
65   fNlayer(0)
66  {
67   // standard constructor
68   for (Int_t i=0; i<fgkCableMaxLayer ; i++) {
69     fLayThickness[i] = 0;
70     fTranslation[i]  = 0;
71     fLayColor[i]     = 0;
72     fLayMedia[i]     = 0;  
73   }; 
74 }
75
76 //________________________________________________________________________
77 AliITSv11GeomCableFlat::AliITSv11GeomCableFlat(const AliITSv11GeomCableFlat &s) :
78   AliITSv11GeomCable(s),fWidth(s.fWidth),fThick(s.fThick),fNlayer(s.fNlayer)
79 {
80   //     Copy Constructor 
81   for (Int_t i=0; i<s.fNlayer; i++) {
82     fLayThickness[i] = s.fLayThickness[i];
83     fTranslation[i] = s.fTranslation[i];
84     fLayMedia[i] = s.fLayMedia[i];
85     fLayColor[i] = s.fLayColor[i];
86   }
87 }
88
89 //________________________________________________________________________
90 AliITSv11GeomCableFlat& AliITSv11GeomCableFlat::
91 operator=(const AliITSv11GeomCableFlat &s) {
92   //     Assignment operator
93   // Not fully inplemented yet !!!
94
95   if(&s == this) return *this;
96   *this = s;
97   fWidth = s.fWidth;
98   fThick = s.fThick;
99   fNlayer = s.fNlayer;
100   for (Int_t i=0; i<s.fNlayer; i++) {
101     fLayThickness[i] = s.fLayThickness[i];
102     fTranslation[i] = s.fTranslation[i];
103     fLayMedia[i] = s.fLayMedia[i];
104     fLayColor[i] = s.fLayColor[i];
105   };
106   return *this;
107 }
108
109 //________________________________________________________________________
110 Int_t AliITSv11GeomCableFlat::GetPoint( Int_t iCheckPt, Double_t *coord)
111   const {
112   // Get the correct point #iCheckPt
113   TVectorD *coordVector =(TVectorD *)fPointArray.At(2*iCheckPt);
114   if (coordVector) {
115 #if ROOT_VERSION_CODE < ROOT_VERSION(4,0,0)
116     CopyFrom(coord, coordVector->GetElements());
117 #else
118     CopyFrom(coord, coordVector->GetMatrixArray());
119 #endif 
120     return kTRUE;
121   } else {
122     return kFALSE;
123   };
124 }
125
126
127 //________________________________________________________________________
128 Int_t AliITSv11GeomCableFlat::GetVect( Int_t iCheckPt, Double_t *coord)
129   const {
130   // Get the correct vect corresponding to point #iCheckPt
131
132   TVectorD *coordVector =(TVectorD *)fPointArray.At(2*iCheckPt+1);
133   if (coordVector) {
134 #if ROOT_VERSION_CODE < ROOT_VERSION(4,0,0)
135     CopyFrom(coord, coordVector->GetElements());
136 #else
137     CopyFrom(coord, coordVector->GetMatrixArray());
138 #endif 
139     return kTRUE;
140   } else {
141     return kFALSE;
142   };
143 }
144
145
146 //________________________________________________________________________
147 void AliITSv11GeomCableFlat::AddCheckPoint( TGeoVolume *vol, Int_t iCheckPt,
148                                             Double_t *coord, Double_t *orthVect)
149 {
150   //
151   // Add a check point. In the fPointArray, the point is at i and its vector
152   // is at i+1.
153   //
154
155 //   if (iCheckPt>=fVolumeArray.GetEntriesFast()) {
156 //     fVolumeArray.AddLast(vol);
157 //     TVectorD *point = new TVectorD(3,coord);
158 //     TVectorD *vect  = new TVectorD(3,orthVect);
159 //     fPointArray.AddLast(point);
160 //     fPointArray.AddLast(vect);
161
162 //   } else if ((iCheckPt >= 0)&&(iCheckPt < fVolumeArray.GetEntriesFast())) {
163 //     fVolumeArray.AddAt(vol, iCheckPt);
164 //     TVectorD *point = new TVectorD(3,coord);
165 //     TVectorD *vect  = new TVectorD(3,orthVect);
166 //     fPointArray.AddAt(point, iCheckPt*2  );
167 //     fPointArray.AddAt(vect,  iCheckPt*2+1);
168 //   };
169   fVolumeArray.AddAtAndExpand(vol, iCheckPt);
170   TVectorD *point = new TVectorD(3,coord);
171   TVectorD *vect  = new TVectorD(3,orthVect);
172   fPointArray.AddAtAndExpand(point, iCheckPt*2  );
173   fPointArray.AddAtAndExpand(vect,  iCheckPt*2+1);
174 }
175
176 //________________________________________________________________________
177 void AliITSv11GeomCableFlat::PrintCheckPoints() const {
178   // print all check points of the cable
179   printf("  ---\n  Printing all check points of the flat cable\n");
180   for (Int_t i = 0; i<fVolumeArray.GetEntriesFast(); i++) {
181      Double_t coord[3];
182      if (GetPoint( i, coord))
183        printf("   ( %.2f, %.2f, %.2f )\n", coord[0], coord[1], coord[2]);
184   };
185 }
186
187 //________________________________________________________________________
188 TGeoVolume* AliITSv11GeomCableFlat::CreateAndInsertCableSegment(Int_t p2,
189                                                           Double_t rotation)
190 {
191 //    Creates a cable segment between points p1 and p2.
192 //    Rotation is the eventual rotation of the flat cable
193 //    along its length axis
194 //
195 // The segment volume is created inside the volume containing point2
196 // Therefore this segment should be defined in this volume only.
197 // I mean here that, if the previous point is in another volume,
198 // it should be just at the border between the 2 volumes. Also the
199 // orientation vector of the previous point should be orthogonal to
200 // the surface between the 2 volumes.
201
202   TGeoNode *mainNode;
203   if (fInitialNode==0) {
204     TObjArray *nodes = gGeoManager->GetListOfNodes();
205     if (nodes->GetEntriesFast()==0) return 0;
206     mainNode = (TGeoNode *) nodes->UncheckedAt(0);
207   } else {
208     mainNode = fInitialNode;
209   };
210
211   Int_t p1 = p2 - 1;
212   TGeoVolume *p2Vol = GetVolume(p2);
213   TGeoVolume *p1Vol = GetVolume(p1);
214
215   ResetCheckDaughter();
216   fCurrentVol = p1Vol;
217   if (! CheckDaughter(mainNode)) {
218     printf("Error::volume containing point is not visible in node tree!\n");
219     return 0;
220   };
221
222   Double_t coord1[3], coord2[3], vect1[3], vect2[3];
223   //=================================================
224   // Get p1 position in the systeme of p2
225   if (p1Vol!=p2Vol) {
226
227     Int_t p1nodeInd[fgkCableMaxNodeLevel]; 
228     for (Int_t i=0; i<fgkCableMaxNodeLevel; i++) p1nodeInd[i]=fNodeInd[i];
229     Int_t p1volLevel = 0;
230     while (p1nodeInd[p1volLevel]!=-1) p1volLevel++;
231     p1volLevel--;
232
233     ResetCheckDaughter();
234     fCurrentVol = p2Vol;
235     if (! CheckDaughter(mainNode)) {
236       printf("Error::volume containing point is not visible in node tree!\n");
237       return 0;
238     };
239     Int_t p2nodeInd[fgkCableMaxNodeLevel];
240     for (Int_t i=0; i<fgkCableMaxNodeLevel; i++) p2nodeInd[i]=fNodeInd[i];
241     Int_t commonMotherLevel = 0;
242     while (p1nodeInd[commonMotherLevel]==fNodeInd[commonMotherLevel])
243       commonMotherLevel++;
244     commonMotherLevel--;
245     Int_t p2volLevel = 0;
246     while (fNodeInd[p2volLevel]!=-1) p2volLevel++;
247     p2volLevel--;
248
249     // Get coord and vect of p1 in the common mother reference system
250     if (! GetCheckPoint(p1, 0, p1volLevel-commonMotherLevel, coord1) )
251       return 0;
252     if (! GetCheckVect( p1, 0, p1volLevel-commonMotherLevel, vect1) )
253       return 0;
254
255     // Translate them in the reference system of the volume containing p2    
256     TGeoNode *pathNode[fgkCableMaxNodeLevel];
257     pathNode[0] = mainNode;
258     for (Int_t i=0; i<=p2volLevel; i++) {
259       pathNode[i+1] = pathNode[i]->GetDaughter(p2nodeInd[i]);
260     };
261     Double_t globalCoord1[3] = {coord1[0], coord1[1], coord1[2]}; 
262     Double_t globalVect1[3]  = {vect1[0], vect1[1], vect1[2]};
263
264     for (Int_t i = commonMotherLevel+1; i <= p2volLevel; i++) {
265       pathNode[i+1]->GetMatrix()->MasterToLocal(globalCoord1, coord1);
266       pathNode[i+1]->GetMatrix()->MasterToLocalVect(globalVect1, vect1);
267       CopyFrom(globalCoord1, coord1);
268       CopyFrom(globalVect1, vect1);
269     };
270   } else {
271     if (! GetCheckPoint(p1, 0, 0, coord1) ) return 0;
272     if (! GetCheckVect(p1, 0, 0, vect1) ) return 0;
273   };
274   
275   //=================================================
276   // Get p2 position in the systeme of p2
277   if (! GetCheckPoint(p2, 0, 0, coord2) ) return 0;
278   if (! GetCheckVect(p2, 0, 0, vect2) ) return 0;
279
280   Double_t cx = (coord1[0]+coord2[0])/2;
281   Double_t cy = (coord1[1]+coord2[1])/2;
282   Double_t cz = (coord1[2]+coord2[2])/2;
283   Double_t dx = coord2[0]-coord1[0];
284   Double_t dy = coord2[1]-coord1[1];
285   Double_t dz = coord2[2]-coord1[2];
286
287   //=================================================
288   // Positionning of the segment between the 2 points
289   if (TMath::Abs(dy)<1e-231) dy = 1e-231;
290   if (TMath::Abs(dz)<1e-231) dz = 1e-231;
291   //Double_t angleRot1 = -TMath::ATan(dx/dy);
292   //Double_t planDiagL = -TMath::Sqrt(dy*dy+dx*dx);
293   //if (dy<0) planDiagL = -planDiagL;
294   //Double_t angleRotDiag = TMath::ATan(planDiagL/dz);
295
296   Double_t angleRot1    = -TMath::ATan2(dx,dy);
297   Double_t planDiagL    =  TMath::Sqrt(dy*dy+dx*dx);
298   Double_t angleRotDiag = -TMath::ATan2(planDiagL,dz);
299   //--- (Calculate rotation of segment on the Z axis)
300   //-- Here I'm trying to calculate the rotation to be applied in
301   //-- order to match as closer as possible this segment and the 
302   //-- previous one. 
303   //-- It seems that some times it doesn't work ...
304   Double_t angleRotZ = 0;
305   TGeoRotation rotTemp("",angleRot1*TMath::RadToDeg(),
306                        angleRotDiag*TMath::RadToDeg(), rotation);
307   Double_t localX[3] = {0,1,0};
308   Double_t globalX[3];
309   rotTemp.LocalToMasterVect(localX, globalX);
310   CopyFrom(localX, globalX);
311   GetCheckVect(localX, p2Vol, 0, fgkCableMaxNodeLevel+1, globalX);
312   Double_t orthVect[3];
313   GetCheckVect(vect1, p2Vol, 0, fgkCableMaxNodeLevel+1, orthVect);
314   if (p2>1) {
315     Double_t orthVectNorm2 = ScalProd(orthVect,orthVect);
316     Double_t alpha1 = ScalProd(fPreviousX,orthVect)/orthVectNorm2;
317     Double_t alpha2 = ScalProd(globalX,orthVect)/orthVectNorm2;
318     Double_t globalX1p[3], globalX2p[3];
319     globalX1p[0] = fPreviousX[0] - alpha1*orthVect[0];
320     globalX1p[1] = fPreviousX[1] - alpha1*orthVect[1];
321     globalX1p[2] = fPreviousX[2] - alpha1*orthVect[2];
322     globalX2p[0] = globalX[0] - alpha2*orthVect[0];
323     globalX2p[1] = globalX[1] - alpha2*orthVect[1];
324     globalX2p[2] = globalX[2] - alpha2*orthVect[2];
325     //-- now I'm searching the 3th vect which makes an orthogonal base
326     //-- with orthVect and globalX1p ...
327     Double_t nulVect[3] = {0,0,0};
328     Double_t axis3[3];
329     TMath::Normal2Plane(nulVect, orthVect, globalX1p, axis3);
330     Double_t globalX1pNorm2 = ScalProd(globalX1p, globalX1p);
331     Double_t beta = ScalProd(globalX2p, globalX1p)/globalX1pNorm2;
332     Double_t gamma = ScalProd(globalX2p, axis3);
333     angleRotZ = (TMath::ATan2(1,0) - TMath::ATan2(beta, gamma))
334                 *TMath::RadToDeg();
335   };
336   //   cout << "!!!!!!!!!!!!!!!!!!!  angle = " <<angleRotZ << endl;
337   CopyFrom(fPreviousX, globalX);
338   //---
339   Double_t localVect1[3], localVect2[3];
340   TGeoRotation rot("",angleRot1*TMath::RadToDeg(),
341                    angleRotDiag*TMath::RadToDeg(),
342                    rotation);
343 //                 rotation-angleRotZ);
344 // since angleRotZ doesn't always work, I won't use it ...
345
346   rot.MasterToLocalVect(vect1, localVect1);
347   rot.MasterToLocalVect(vect2, localVect2);
348
349   //=================================================
350   // Create the segment and add it to the mother volume
351   TGeoVolume *vCableSegB = CreateSegment(coord1, coord2,
352                                          localVect1, localVect2);
353 //   TGeoVolume *vCableSegB = CreateBoxSegment(coord1, coord2);
354
355   TGeoRotation rotArbSeg("", 0, 90, 0);
356   rotArbSeg.MultiplyBy(&rot, kFALSE);
357   TGeoTranslation trans("",cx, cy, cz);
358   TGeoCombiTrans  *combiB = new TGeoCombiTrans(trans, rotArbSeg);
359   p2Vol->AddNode(vCableSegB, p2, combiB);
360   //=================================================;
361
362   if (fDebug) {
363     printf("---\n  Cable segment points : ");
364     printf("%f, %f, %f\n",coord1[0], coord1[1], coord1[2]);
365     printf("%f, %f, %f\n",coord2[0], coord2[1], coord2[2]);
366   };
367
368 //   #include <TGeoSphere.h>
369 //   TGeoMedium *airSDD = gGeoManager->GetMedium("ITS_ITSsddAir");
370 //   TGeoSphere *sphere = new TGeoSphere(0, 0.05);
371 //   TGeoVolume *vSphere = new TGeoVolume("", sphere, airSDD);
372 //   TGeoTranslation *trC = new TGeoTranslation("", cx, cy, cz);
373 //   TGeoTranslation *tr1 = new TGeoTranslation("",coord1[0],
374 //                                           coord1[1],coord1[2]);
375 //   TGeoTranslation *tr2 = new TGeoTranslation("",coord2[0],
376 //                                           coord2[1],coord2[2]);
377 //   p2Vol->AddNode(vSphere, p2*3-2, trC);
378 //   p2Vol->AddNode(vSphere, p2*3-1, tr1);
379 //   p2Vol->AddNode(vSphere, p2*3  , tr2);
380
381   return vCableSegB;
382 }
383
384 //________________________________________________________________________
385 TGeoVolume* AliITSv11GeomCableFlat::CreateAndInsertBoxCableSegment(Int_t p2,
386                                                           Double_t rotation)
387 {
388   // This function is to be use only when the segment has the shape
389   // of a simple box, i.e. the normal vector to its end is perpendicular
390   // to the segment own axis
391 //    Creates a cable segment between points p1 and p2.
392 //    Rotation is the eventual rotation of the flat cable
393 //    along its length axis
394 //
395 // The segment volume is created inside the volume containing point2
396 // Therefore this segment should be defined in this volume only.
397 // I mean here that, if the previous point is in another volume,
398 // it should be just at the border between the 2 volumes. Also the
399 // orientation vector of the previous point should be orthogonal to
400 // the surface between the 2 volumes.
401
402   TGeoNode *mainNode;
403   if (fInitialNode==0) {
404     TObjArray *nodes = gGeoManager->GetListOfNodes();
405     if (nodes->GetEntriesFast()==0) return 0;
406     mainNode = (TGeoNode *) nodes->UncheckedAt(0);
407   } else {
408     mainNode = fInitialNode;
409   };
410
411   Int_t p1 = p2 - 1;
412   TGeoVolume *p2Vol = GetVolume(p2);
413   TGeoVolume *p1Vol = GetVolume(p1);
414
415   ResetCheckDaughter();
416   fCurrentVol = p1Vol;
417   if (! CheckDaughter(mainNode)) {
418     printf("Error::volume containing point is not visible in node tree!\n");
419     return 0;
420   };
421
422   Double_t coord1[3], coord2[3], vect1[3], vect2[3];
423   //=================================================
424   // Get p1 position in the systeme of p2
425   if (p1Vol!=p2Vol) {
426
427     Int_t p1nodeInd[fgkCableMaxNodeLevel]; 
428     for (Int_t i=0; i<fgkCableMaxNodeLevel; i++) p1nodeInd[i]=fNodeInd[i];
429     Int_t p1volLevel = 0;
430     while (p1nodeInd[p1volLevel]!=-1) p1volLevel++;
431     p1volLevel--;
432
433     ResetCheckDaughter();
434     fCurrentVol = p2Vol;
435     if (! CheckDaughter(mainNode)) {
436       printf("Error::volume containing point is not visible in node tree!\n");
437       return 0;
438     };
439     Int_t p2nodeInd[fgkCableMaxNodeLevel];
440     for (Int_t i=0; i<fgkCableMaxNodeLevel; i++) p2nodeInd[i]=fNodeInd[i];
441     Int_t commonMotherLevel = 0;
442     while (p1nodeInd[commonMotherLevel]==fNodeInd[commonMotherLevel])
443       commonMotherLevel++;
444     commonMotherLevel--;
445     Int_t p2volLevel = 0;
446     while (fNodeInd[p2volLevel]!=-1) p2volLevel++;
447     p2volLevel--;
448
449     // Get coord and vect of p1 in the common mother reference system
450     if (! GetCheckPoint(p1, 0, p1volLevel-commonMotherLevel, coord1) )
451       return 0;
452     if (! GetCheckVect( p1, 0, p1volLevel-commonMotherLevel, vect1) )
453       return 0;
454
455     // Translate them in the reference system of the volume containing p2    
456     TGeoNode *pathNode[fgkCableMaxNodeLevel];
457     pathNode[0] = mainNode;
458     for (Int_t i=0; i<=p2volLevel; i++) {
459       pathNode[i+1] = pathNode[i]->GetDaughter(p2nodeInd[i]);
460     };
461     Double_t globalCoord1[3] = {coord1[0], coord1[1], coord1[2]}; 
462     Double_t globalVect1[3]  = {vect1[0], vect1[1], vect1[2]};
463
464     for (Int_t i = commonMotherLevel+1; i <= p2volLevel; i++) {
465       pathNode[i+1]->GetMatrix()->MasterToLocal(globalCoord1, coord1);
466       pathNode[i+1]->GetMatrix()->MasterToLocalVect(globalVect1, vect1);
467       CopyFrom(globalCoord1, coord1);
468       CopyFrom(globalVect1, vect1);
469     };
470   } else {
471     if (! GetCheckPoint(p1, 0, 0, coord1) ) return 0;
472     if (! GetCheckVect(p1, 0, 0, vect1) ) return 0;
473   };
474   
475   //=================================================
476   // Get p2 position in the systeme of p2
477   if (! GetCheckPoint(p2, 0, 0, coord2) ) return 0;
478   if (! GetCheckVect(p2, 0, 0, vect2) ) return 0;
479
480   Double_t cx = (coord1[0]+coord2[0])/2;
481   Double_t cy = (coord1[1]+coord2[1])/2;
482   Double_t cz = (coord1[2]+coord2[2])/2;
483   Double_t dx = coord2[0]-coord1[0];
484   Double_t dy = coord2[1]-coord1[1];
485   Double_t dz = coord2[2]-coord1[2];
486
487   //=================================================
488   // Positionning of the segment between the 2 points
489   if (TMath::Abs(dy)<1e-231) dy = 1e-231;
490   if (TMath::Abs(dz)<1e-231) dz = 1e-231;
491   //Double_t angleRot1 = -TMath::ATan(dx/dy);
492   //Double_t planDiagL = -TMath::Sqrt(dy*dy+dx*dx);
493   //if (dy<0) planDiagL = -planDiagL;
494   //Double_t angleRotDiag = TMath::ATan(planDiagL/dz);
495
496   Double_t angleRot1    = -TMath::ATan2(dx,dy);
497   Double_t planDiagL    =  TMath::Sqrt(dy*dy+dx*dx);
498   Double_t angleRotDiag = -TMath::ATan2(planDiagL,dz);
499   //--- (Calculate rotation of segment on the Z axis)
500   //-- Here I'm trying to calculate the rotation to be applied in
501   //-- order to match as closer as possible this segment and the 
502   //-- previous one. 
503   //-- It seems that some times it doesn't work ...
504   Double_t angleRotZ = 0;
505   TGeoRotation rotTemp("",angleRot1*TMath::RadToDeg(),
506                        angleRotDiag*TMath::RadToDeg(), rotation);
507   Double_t localX[3] = {0,1,0};
508   Double_t globalX[3];
509   rotTemp.LocalToMasterVect(localX, globalX);
510   CopyFrom(localX, globalX);
511   GetCheckVect(localX, p2Vol, 0, fgkCableMaxNodeLevel+1, globalX);
512   Double_t orthVect[3];
513   GetCheckVect(vect1, p2Vol, 0, fgkCableMaxNodeLevel+1, orthVect);
514   if (p2>1) {
515     Double_t orthVectNorm2 = ScalProd(orthVect,orthVect);
516     Double_t alpha1 = ScalProd(fPreviousX,orthVect)/orthVectNorm2;
517     Double_t alpha2 = ScalProd(globalX,orthVect)/orthVectNorm2;
518     Double_t globalX1p[3], globalX2p[3];
519     globalX1p[0] = fPreviousX[0] - alpha1*orthVect[0];
520     globalX1p[1] = fPreviousX[1] - alpha1*orthVect[1];
521     globalX1p[2] = fPreviousX[2] - alpha1*orthVect[2];
522     globalX2p[0] = globalX[0] - alpha2*orthVect[0];
523     globalX2p[1] = globalX[1] - alpha2*orthVect[1];
524     globalX2p[2] = globalX[2] - alpha2*orthVect[2];
525     //-- now I'm searching the 3th vect which makes an orthogonal base
526     //-- with orthVect and globalX1p ...
527     Double_t nulVect[3] = {0,0,0};
528     Double_t axis3[3];
529     TMath::Normal2Plane(nulVect, orthVect, globalX1p, axis3);
530     Double_t globalX1pNorm2 = ScalProd(globalX1p, globalX1p);
531     Double_t beta = ScalProd(globalX2p, globalX1p)/globalX1pNorm2;
532     Double_t gamma = ScalProd(globalX2p, axis3);
533     angleRotZ = (TMath::ATan2(1,0) - TMath::ATan2(beta, gamma))
534                 *TMath::RadToDeg();
535   };
536   //   cout << "!!!!!!!!!!!!!!!!!!!  angle = " <<angleRotZ << endl;
537   CopyFrom(fPreviousX, globalX);
538   //---
539   Double_t localVect1[3], localVect2[3];
540   TGeoRotation rot("",angleRot1*TMath::RadToDeg(),
541                    angleRotDiag*TMath::RadToDeg(),
542                    rotation);
543 //                 rotation-angleRotZ);
544 // since angleRotZ doesn't always work, I won't use it ...
545
546   rot.MasterToLocalVect(vect1, localVect1);
547   rot.MasterToLocalVect(vect2, localVect2);
548
549   //=================================================
550   // Create the segment and add it to the mother volume
551   TGeoVolume *vCableSegB = CreateBoxSegment(coord1, coord2);
552
553   TGeoRotation rotArbSeg("", 0, 90, 0);
554   rotArbSeg.MultiplyBy(&rot, kFALSE);
555   TGeoTranslation trans("",cx, cy, cz);
556   TGeoCombiTrans  *combiB = new TGeoCombiTrans(trans, rotArbSeg);
557   p2Vol->AddNode(vCableSegB, p2, combiB);
558   //=================================================;
559
560   if (fDebug) {
561     printf("---\n  Cable segment points : ");
562     printf("%f, %f, %f\n",coord1[0], coord1[1], coord1[2]);
563     printf("%f, %f, %f\n",coord2[0], coord2[1], coord2[2]);
564   };
565
566   return vCableSegB;
567 }
568
569 //________________________________________________________________________
570 TGeoVolume* AliITSv11GeomCableFlat::CreateAndInsertCableCylSegment(Int_t p2,
571                                                           Double_t rotation)
572 {
573   // Create a flat cable segment with a curvature between points p1 and p2.
574   // The radius and position of the curve is defined by the
575   // perpendicular vector of point p2 (the orientation of this vector
576   // and the position of the 2 check points are enough to completely
577   // define the curve)
578   //    Rotation is the eventual rotation of the flat cable
579   //    along its length axis
580   //
581
582   TGeoNode *mainNode;
583   if (fInitialNode==0) {
584     TObjArray *nodes = gGeoManager->GetListOfNodes();
585     if (nodes->GetEntriesFast()==0) return 0;
586     mainNode = (TGeoNode *) nodes->UncheckedAt(0);
587   } else {
588     mainNode = fInitialNode;
589   };
590
591   Int_t p1 = p2 - 1;
592   TGeoVolume *p1Vol = GetVolume(p1);
593   TGeoVolume *p2Vol = GetVolume(p2);
594
595   ResetCheckDaughter();
596   fCurrentVol = p1Vol;
597   if (! CheckDaughter(mainNode)) {
598     printf("Error::volume containing point is not visible in node tree!\n");
599     return 0;
600   };
601
602   Double_t coord1[3], coord2[3], vect1[3], vect2[3];
603   //=================================================
604   // Get p1 position in the systeme of p2
605   if (p1Vol!=p2Vol) {
606
607     Int_t p1nodeInd[fgkCableMaxNodeLevel]; 
608     for (Int_t i=0; i<fgkCableMaxNodeLevel; i++) p1nodeInd[i]=fNodeInd[i];
609     Int_t p1volLevel = 0;
610     while (p1nodeInd[p1volLevel]!=-1) p1volLevel++;
611     p1volLevel--;
612
613     ResetCheckDaughter();
614     fCurrentVol = p2Vol;
615     if (! CheckDaughter(mainNode)) {
616       printf("Error::volume containing point is not visible in node tree!\n");
617       return 0;
618     };
619     Int_t p2nodeInd[fgkCableMaxNodeLevel];
620     for (Int_t i=0; i<fgkCableMaxNodeLevel; i++) p2nodeInd[i]=fNodeInd[i];
621     Int_t commonMotherLevel = 0;
622     while (p1nodeInd[commonMotherLevel]==fNodeInd[commonMotherLevel])
623       commonMotherLevel++;
624     commonMotherLevel--;
625     Int_t p2volLevel = 0;
626     while (fNodeInd[p2volLevel]!=-1) p2volLevel++;
627     p2volLevel--;
628
629     // Get coord and vect of p1 in the common mother reference system
630     GetCheckPoint(p1, 0, p1volLevel-commonMotherLevel, coord1);
631     GetCheckVect( p1, 0, p1volLevel-commonMotherLevel, vect1);
632     // Translate them in the reference system of the volume containing p2    
633     TGeoNode *pathNode[fgkCableMaxNodeLevel];
634     pathNode[0] = mainNode;
635     for (Int_t i=0; i<=p2volLevel; i++) {
636       pathNode[i+1] = pathNode[i]->GetDaughter(p2nodeInd[i]);
637     };
638     Double_t globalCoord1[3] = {coord1[0], coord1[1], coord1[2]}; 
639     Double_t globalVect1[3]  = {vect1[0], vect1[1], vect1[2]};
640
641     for (Int_t i = commonMotherLevel+1; i<=p2volLevel; i++) {
642       pathNode[i+1]->GetMatrix()->MasterToLocal(globalCoord1, coord1);
643       pathNode[i+1]->GetMatrix()->MasterToLocalVect(globalVect1, vect1);
644       CopyFrom(globalCoord1, coord1);
645       CopyFrom(globalVect1, vect1);
646     };
647   } else {
648     GetCheckPoint(p1, 0, 0, coord1);
649     GetCheckVect(p1, 0, 0, vect1);
650   };
651   
652   //=================================================
653   // Get p2 position in the systeme of p2
654   GetCheckPoint(p2, 0, 0, coord2);
655   GetCheckVect(p2, 0, 0, vect2);
656
657   Double_t cx = (coord1[0]+coord2[0])/2;
658   Double_t cy = (coord1[1]+coord2[1])/2;
659   Double_t cz = (coord1[2]+coord2[2])/2;
660   Double_t dx = coord2[0]-coord1[0];
661   Double_t dy = coord2[1]-coord1[1];
662   Double_t dz = coord2[2]-coord1[2];
663   Double_t length = TMath::Sqrt(dx*dx+dy*dy+dz*dz);
664
665   //=================================================
666   // Positionning of the segment between the 2 points
667   if ((dy<1e-31)&&(dy>0)) dy = 1e-31;
668   if ((dz<1e-31)&&(dz>0)) dz = 1e-31;
669   if ((dy>-1e-31)&&(dy<0)) dy = -1e-31;
670   if ((dz>-1e-31)&&(dz<0)) dz = -1e-31;
671
672   Double_t angleRot1 = -TMath::ATan2(dx,dy);
673   Double_t planDiagL = TMath::Sqrt(dy*dy+dx*dx);
674   Double_t angleRotDiag = -TMath::ATan2(planDiagL,dz);
675
676   TGeoRotation rotTorusTemp("",angleRot1*TMath::RadToDeg(),
677                             angleRotDiag*TMath::RadToDeg(),0);
678   TGeoRotation rotTorusToZ("",0,90,0);
679   rotTorusTemp.MultiplyBy(&rotTorusToZ, kTRUE);
680   Double_t localVect2[3];
681   rotTorusTemp.MasterToLocalVect(vect2, localVect2);
682   if (localVect2[1]<0) {
683     localVect2[0] = -localVect2[0];
684     localVect2[1] = -localVect2[1];
685     localVect2[2] = -localVect2[2];
686   };
687   Double_t normVect2 = TMath::Sqrt(localVect2[0]*localVect2[0]+
688                                    localVect2[1]*localVect2[1]+
689                                    localVect2[2]*localVect2[2]);
690   Double_t axisX[3] = {1,0,0};
691   Double_t cosangleTorusSeg = (localVect2[0]*axisX[0]+
692                                localVect2[1]*axisX[1]+
693                                localVect2[2]*axisX[2])/normVect2;
694   Double_t angleTorusSeg = TMath::ACos(cosangleTorusSeg)*TMath::RadToDeg();
695   TGeoRotation rotTorus("",angleRot1*TMath::RadToDeg(),
696                         angleRotDiag*TMath::RadToDeg(),
697                         45-angleTorusSeg+rotation);
698                         //180-angleTorusSeg+rotation);
699   rotTorus.MultiplyBy(&rotTorusToZ, kTRUE);
700   rotTorus.MasterToLocalVect(vect2, localVect2);
701   if (localVect2[1]<0) {
702     localVect2[0] = -localVect2[0];
703     localVect2[1] = -localVect2[1];
704     localVect2[2] = -localVect2[2];
705   };
706   normVect2 = TMath::Sqrt(localVect2[0]*localVect2[0]+
707                           localVect2[1]*localVect2[1]+
708                           localVect2[2]*localVect2[2]);
709   Double_t axisY[3] = {0,1,0};
710   Double_t cosPhi = (localVect2[0]*axisY[0]+localVect2[1]*axisY[1]+
711                      localVect2[2]*axisY[2])/normVect2;
712   Double_t torusPhi1 = TMath::ACos(cosPhi);
713   Double_t torusR = (length/2)/TMath::Sin(torusPhi1);
714   torusPhi1 = torusPhi1*TMath::RadToDeg();
715   Double_t perpLength = TMath::Sqrt(torusR*torusR-length*length/4);
716   Double_t localTransT[3] = {-perpLength,0,0};
717   Double_t globalTransT[3];
718   rotTorus.LocalToMasterVect(localTransT, globalTransT);
719   TGeoTranslation transTorus("",cx+globalTransT[0],cy+globalTransT[1],
720                              cz+globalTransT[2]);
721
722   TGeoCombiTrans  *combiTorus = new TGeoCombiTrans(transTorus, rotTorus);
723
724   //=================================================
725   // Create the segment and add it to the mother volume
726   TGeoVolume *vCableSegT = CreateCylSegment(torusPhi1, torusR);
727   p2Vol->AddNode(vCableSegT, p2, combiTorus);
728
729   if (fDebug) {
730     printf("---\n  Cable segment points : ");
731     printf("%f, %f, %f\n",coord1[0], coord1[1], coord1[2]);
732     printf("%f, %f, %f\n",coord2[0], coord2[1], coord2[2]);
733   };
734
735   return vCableSegT;
736 }
737
738
739 //________________________________________________________________________
740 TGeoVolume *AliITSv11GeomCableFlat::CreateSegment( Double_t *coord1,
741                                                    Double_t *coord2,
742                                                    Double_t *localVect1,
743                                                    Double_t *localVect2 )
744 {
745
746   //=================================================
747   // Calculate segment "deformation"
748   Double_t dx = coord2[0]-coord1[0];
749   Double_t dy = coord2[1]-coord1[1];
750   Double_t dz = coord2[2]-coord1[2];
751   Double_t length = TMath::Sqrt(dx*dx+dy*dy+dz*dz);
752
753   Double_t cosTheta1 = -1./TMath::Sqrt( 1 + localVect1[0]*localVect1[0]
754                                         /localVect1[2]/localVect1[2] );
755   Double_t cosTheta2 = 1./TMath::Sqrt( 1 + localVect2[0]*localVect2[0]
756                                         /localVect2[2]/localVect2[2] );
757   if (localVect1[2]<0) cosTheta1 = -cosTheta1;
758   if (localVect2[2]<0) cosTheta2 = -cosTheta2;
759
760   Double_t dL1 = 0.5*fWidth*TMath::Tan(TMath::ACos(cosTheta1));
761   Double_t dL2 = 0.5*fWidth*TMath::Tan(TMath::ACos(cosTheta2));
762   if (localVect1[0]<0) dL1 = - dL1;
763   if (localVect2[0]<0) dL2 = - dL2;
764   //---
765   Double_t cosPhi1 = -1./TMath::Sqrt( 1 + localVect1[1]*localVect1[1]
766                                         /localVect1[2]/localVect1[2] );
767   Double_t cosPhi2 = 1./TMath::Sqrt( 1 + localVect2[1]*localVect2[1]
768                                         /localVect2[2]/localVect2[2] );
769   if (localVect1[2]<0) cosPhi1 = -cosPhi1;
770   if (localVect2[2]<0) cosPhi2 = -cosPhi2;
771
772   Double_t tanACosCosPhi1 = TMath::Tan(TMath::ACos(cosPhi1));
773   Double_t tanACosCosPhi2 = TMath::Tan(TMath::ACos(cosPhi2));
774   if (localVect1[1]<0) tanACosCosPhi1 = -tanACosCosPhi1;
775   if (localVect2[1]<0) tanACosCosPhi2 = -tanACosCosPhi2;
776
777   Double_t dl1 = 0.5*fThick*tanACosCosPhi1;
778   Double_t dl2 = 0.5*fThick*tanACosCosPhi2;
779
780   //=================================================
781   // Create the segment
782   TGeoArb8 *cableSeg = new TGeoArb8(fThick/2);
783   cableSeg->SetVertex( 0, -fWidth/2, -length/2 - dL1 + dl1);
784   cableSeg->SetVertex( 1, -fWidth/2,  length/2 + dL2 - dl2);
785   cableSeg->SetVertex( 2,  fWidth/2,  length/2 - dL2 - dl2);
786   cableSeg->SetVertex( 3,  fWidth/2, -length/2 + dL1 + dl1);
787   cableSeg->SetVertex( 4, -fWidth/2, -length/2 - dL1 - dl1);
788   cableSeg->SetVertex( 5, -fWidth/2,  length/2 + dL2 + dl2);
789   cableSeg->SetVertex( 6,  fWidth/2,  length/2 - dL2 + dl2);
790   cableSeg->SetVertex( 7,  fWidth/2, -length/2 + dL1 - dl1);
791
792   TGeoMedium *airSDD = gGeoManager->GetMedium("ITS_ITSair");
793   TGeoVolume *vCableSeg = new TGeoVolume(GetName(), cableSeg, airSDD);
794
795   // add all cable layers
796   for (Int_t iLay=0; iLay<fNlayer; iLay++) {
797
798     Double_t dl1Lay = 0.5*fLayThickness[iLay]*tanACosCosPhi1;
799     Double_t dl2Lay = 0.5*fLayThickness[iLay]*tanACosCosPhi2;
800  
801     Double_t ztr = -fThick/2;
802     for (Int_t i=0;i<iLay; i++) ztr+= fLayThickness[i];
803     ztr+= fLayThickness[iLay]/2;
804
805     Double_t dl1LayS = ztr*tanACosCosPhi1;
806     Double_t dl2LayS = ztr*tanACosCosPhi2;
807
808     TGeoArb8 *lay = new TGeoArb8(fLayThickness[iLay]/2);
809     lay->SetVertex( 0, -fWidth/2, -length/2 - dL1 + dl1Lay - dl1LayS);
810     lay->SetVertex( 1, -fWidth/2,  length/2 + dL2 - dl2Lay + dl2LayS);
811     lay->SetVertex( 2,  fWidth/2,  length/2 - dL2 - dl2Lay + dl2LayS);
812     lay->SetVertex( 3,  fWidth/2, -length/2 + dL1 + dl1Lay - dl1LayS);
813     lay->SetVertex( 4, -fWidth/2, -length/2 - dL1 - dl1Lay - dl1LayS);
814     lay->SetVertex( 5, -fWidth/2,  length/2 + dL2 + dl2Lay + dl2LayS);
815     lay->SetVertex( 6,  fWidth/2,  length/2 - dL2 + dl2Lay + dl2LayS);
816     lay->SetVertex( 7,  fWidth/2, -length/2 + dL1 - dl1Lay - dl1LayS);
817     TGeoVolume *vLay = new TGeoVolume("vCableSegLay", lay, fLayMedia[iLay]);
818     vLay->SetLineColor(fLayColor[iLay]);
819     
820     if (fTranslation[iLay]==0)
821       fTranslation[iLay] = new TGeoTranslation(0, 0, ztr);
822     vCableSeg->AddNode(vLay, iLay+1, fTranslation[iLay]);
823   };
824
825   vCableSeg->SetVisibility(kFALSE);
826   return vCableSeg;
827 }
828
829
830 //________________________________________________________________________
831 TGeoVolume *AliITSv11GeomCableFlat::CreateCylSegment(Double_t &phi,
832                                                      Double_t &r)
833 {
834
835   Double_t phi1 = 360-phi;
836   Double_t phi2 = 360+phi;
837
838   Double_t rMin = r-fThick/2;
839   Double_t rMax = r+fThick/2;
840   //=================================================
841   // Create the segment
842
843   TGeoTubeSeg *cableSeg = new TGeoTubeSeg(rMin, rMax, fWidth/2,
844                                           phi1, phi2);
845   TGeoMedium *airSDD = gGeoManager->GetMedium("ITS_ITSair");
846   TGeoVolume *vCableSeg = new TGeoVolume(GetName(), cableSeg, airSDD);
847
848   // add all cable layers
849   for (Int_t iLay=0; iLay<fNlayer; iLay++) {
850  
851     Double_t ztr = -fThick/2;
852     for (Int_t i=0;i<iLay; i++) ztr+= fLayThickness[i];
853
854     rMin = r  + ztr;
855     rMax = r  + ztr + fLayThickness[iLay];
856     TGeoTubeSeg *lay = new TGeoTubeSeg(rMin, rMax, fWidth/2,
857                                        phi1, phi2);
858
859     TGeoVolume *vLay = new TGeoVolume("vCableSegLay", lay, fLayMedia[iLay]);
860     vLay->SetLineColor(fLayColor[iLay]);
861     
862     vCableSeg->AddNode(vLay, iLay+1, 0);
863   };
864
865   vCableSeg->SetVisibility(kFALSE);
866   return vCableSeg;
867 }
868
869
870 //________________________________________________________________________
871 TGeoVolume *AliITSv11GeomCableFlat::CreateBoxSegment( Double_t *coord1,
872                                                       Double_t *coord2)
873 {
874
875   //=================================================
876   // Create a segment for the case it is a simple box
877   Double_t dx = coord2[0]-coord1[0];
878   Double_t dy = coord2[1]-coord1[1];
879   Double_t dz = coord2[2]-coord1[2];
880   Double_t length = TMath::Sqrt(dx*dx+dy*dy+dz*dz);
881
882   TGeoBBox *cableSeg = new  TGeoBBox(fWidth/2, length/2, fThick/2);
883
884   TGeoMedium *airSDD = gGeoManager->GetMedium("ITS_ITSair");
885   TGeoVolume *vCableSeg = new TGeoVolume(GetName(), cableSeg, airSDD);
886
887   // add all cable layers
888   for (Int_t iLay=0; iLay<fNlayer; iLay++) {
889  
890     Double_t ztr = -fThick/2;
891     for (Int_t i=0;i<iLay; i++) ztr+= fLayThickness[i];
892     ztr+= fLayThickness[iLay]/2;
893
894     TGeoBBox *lay = new  TGeoBBox(fWidth/2, length/2, fLayThickness[iLay]/2);
895
896
897     TGeoVolume *vLay = new TGeoVolume("vCableSegLay", lay, fLayMedia[iLay]);
898     vLay->SetLineColor(fLayColor[iLay]);
899     
900     if (fTranslation[iLay]==0)
901       fTranslation[iLay] = new TGeoTranslation(0, 0, ztr);
902     vCableSeg->AddNode(vLay, iLay+1, fTranslation[iLay]);
903   };
904
905   vCableSeg->SetVisibility(kFALSE);
906   return vCableSeg;
907 }
908
909 //________________________________________________________________________
910 void AliITSv11GeomCableFlat::SetNLayers(Int_t nLayers) {
911   // Set the number of layers
912   if((nLayers>0) &&(nLayers<=fgkCableMaxLayer)) {
913
914     fNlayer = nLayers;
915     for (Int_t i=0; i<fgkCableMaxLayer ; i++) {
916       fLayThickness[i] = 0;
917       fTranslation[i]  = 0;
918       fLayColor[i]     = 0;
919       fLayMedia[i]     = 0;  
920     }; 
921   };
922 }
923
924 //________________________________________________________________________
925 Int_t AliITSv11GeomCableFlat::SetLayer(Int_t nLayer, Double_t thick,
926                                           TGeoMedium *medium, Int_t color) {
927   // Set the layer number nLayer
928   if ((nLayer<0)||(nLayer>=fNlayer)) {
929     printf("Set wrong layer number of the cable\n");
930     return kFALSE;
931   };
932   if (nLayer>0)
933     if (fLayThickness[nLayer-1]<=0) {
934       printf("AliITSv11GeomCableFlat::SetLayer():"
935              " You must define cable layer %i first !",nLayer-1);
936       return kFALSE;
937     };
938
939   Double_t thickTot = 0;
940   for (Int_t i=0; i<nLayer; i++) thickTot += fLayThickness[i];
941   thickTot += thick;
942   if (thickTot-1e-10>fThick) {
943     printf("Can't add this layer, cable thickness would be higher than total\n");
944     return kFALSE;
945   };
946
947   fLayThickness[nLayer] = thick;
948   fLayMedia[nLayer] = medium;
949   fLayColor[nLayer] = color;
950   fTranslation[nLayer]  = 0;
951   return kTRUE;
952 }