]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliAlignObj.cxx
Addition of getter/setter of the reduced covariance matrix data members by means...
[u/mrichter/AliRoot.git] / STEER / AliAlignObj.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 //  Implementation of the alignment object class, holding the alignment
20 //  constants for a single volume, through the abstract class AliAlignObj.
21 //  From it two derived concrete representation of alignment object class
22 //  (AliAlignObjParams, AliAlignObjMatrix) are derived in separate files.
23 //-----------------------------------------------------------------
24
25 #include <TClass.h>
26 #include <TGeoManager.h>
27 #include <TGeoPhysicalNode.h>
28 #include <TMath.h>
29 #include "TObjString.h"
30
31 #include "AliAlignObj.h"
32 #include "AliTrackPointArray.h"
33 #include "AliLog.h"
34 #include "AliAlignObjParams.h"
35  
36 ClassImp(AliAlignObj)
37
38 //_____________________________________________________________________________
39 AliAlignObj::AliAlignObj():
40   fVolPath(),
41   fVolUID(0)
42 {
43   // default constructor
44   for(Int_t i=0; i<6; i++) fDiag[i]=-999.;
45   for(Int_t i=0; i<21; i++) fODia[i]=-999.;
46 }
47
48 //_____________________________________________________________________________
49 AliAlignObj::AliAlignObj(const char* symname, UShort_t voluid) :
50   TObject(),
51   fVolPath(symname),
52   fVolUID(voluid)
53 {
54   // standard constructor
55   //
56   for(Int_t i=0; i<6; i++) fDiag[i]=-999.;
57   for(Int_t i=0; i<21; i++) fODia[i]=-999.;
58 }
59
60 //_____________________________________________________________________________
61 AliAlignObj::AliAlignObj(const char* symname, UShort_t voluid, Double_t* cmat) :
62   TObject(),
63   fVolPath(symname),
64   fVolUID(voluid)
65 {
66   // standard constructor
67   //
68   SetCorrMatrix(cmat);
69 }
70
71 //_____________________________________________________________________________
72 AliAlignObj::AliAlignObj(const AliAlignObj& theAlignObj) :
73   TObject(theAlignObj),
74   fVolPath(theAlignObj.GetSymName()),
75   fVolUID(theAlignObj.GetVolUID())
76 {
77   //copy constructor
78   for(Int_t i=0; i<6; i++) fDiag[i]=theAlignObj.fDiag[i];
79   for(Int_t i=0; i<21; i++) fODia[i]=theAlignObj.fODia[i];
80 }
81
82 //_____________________________________________________________________________
83 AliAlignObj &AliAlignObj::operator =(const AliAlignObj& theAlignObj)
84 {
85   // assignment operator
86   if(this==&theAlignObj) return *this;
87   fVolPath = theAlignObj.GetSymName();
88   fVolUID = theAlignObj.GetVolUID();
89   for(Int_t i=0; i<6; i++) fDiag[i]=theAlignObj.fDiag[i];
90   for(Int_t i=0; i<21; i++) fODia[i]=theAlignObj.fODia[i];
91   return *this;
92 }
93
94 //_____________________________________________________________________________
95 AliAlignObj &AliAlignObj::operator*=(const AliAlignObj& theAlignObj)
96 {
97   // multiplication operator
98   // The operator can be used to 'combine'
99   // two alignment objects
100   TGeoHMatrix m1;
101   GetMatrix(m1);
102   TGeoHMatrix m2;
103   theAlignObj.GetMatrix(m2);
104   m1.MultiplyLeft(&m2);
105   SetMatrix(m1);
106   // temporary solution: consider parameters indipendent 
107   for(Int_t i=0; i<6; i++)  fDiag[i] = TMath::Sqrt((fDiag[i]*fDiag[i])+(theAlignObj.fDiag[i]*theAlignObj.fDiag[i]));
108   return *this;
109 }
110
111 //_____________________________________________________________________________
112 AliAlignObj::~AliAlignObj()
113 {
114   // dummy destructor
115 }
116
117 //_____________________________________________________________________________
118 void AliAlignObj::SetVolUID(AliGeomManager::ELayerID detId, Int_t modId)
119 {
120   // From detector name and module number (according to detector numbering)
121   // build fVolUID, unique numerical identity of that volume inside ALICE
122   // fVolUID is 16 bits, first 5 reserved for detID (32 possible values),
123   // remaining 11 for module ID inside det (2048 possible values).
124   //
125   fVolUID = AliGeomManager::LayerToVolUID(detId,modId);
126 }
127
128 //_____________________________________________________________________________
129 void AliAlignObj::GetVolUID(AliGeomManager::ELayerID &layerId, Int_t &modId) const
130 {
131   // From the fVolUID, unique numerical identity of that volume inside ALICE,
132   // (voluid is 16 bits, first 5 reserved for layerID (32 possible values),
133   // remaining 11 for module ID inside det (2048 possible values)), sets
134   // the argument layerId to the identity of the layer to which that volume
135   // belongs and sets the argument modId to the identity of that volume
136   // internally to the layer.
137   //
138   layerId = AliGeomManager::VolUIDToLayer(fVolUID,modId);
139 }
140
141 //_____________________________________________________________________________
142 Bool_t AliAlignObj::GetPars(Double_t tr[], Double_t angles[]) const
143 {
144   GetTranslation(tr);
145   return GetAngles(angles);
146 }
147
148 //_____________________________________________________________________________
149 Int_t AliAlignObj::GetLevel() const
150 {
151   // Return the geometry level of the alignable volume to which
152   // the alignment object is associated; this is the number of
153   // slashes in the corresponding volume path
154   //
155   if(!gGeoManager){
156     AliWarning("gGeoManager doesn't exist or it is still opened: unable to return meaningful level value.");
157     return (-1);
158   }
159   const char* symname = GetSymName();
160   const char* path;
161   TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
162   if(pne){
163     path = pne->GetTitle();
164   }else{
165     path = symname;
166   }
167
168   TString path_str = path;
169   if(path_str[0]!='/') path_str.Prepend('/');
170   return path_str.CountChar('/');
171 }
172
173 //_____________________________________________________________________________
174 Int_t AliAlignObj::Compare(const TObject *obj) const
175 {
176   // Compare the levels of two
177   // alignment objects
178   // Used in the sorting during
179   // the application of alignment
180   // objects to the geometry
181   //
182   Int_t level = GetLevel();
183   Int_t level2 = ((AliAlignObj *)obj)->GetLevel();
184   if (level == level2)
185     return 0;
186   else
187     return ((level > level2) ? 1 : -1);
188 }
189
190 //______________________________________________________________________________
191 void AliAlignObj::GetCovMatrix(Double_t *cmat) const
192 {
193   // Fills the cmat argument with the coefficients of the external cov matrix (21 elements)
194   // calculating them from the correlation matrix data member
195   //
196
197   for(Int_t i=0; i<6; ++i) {
198     // Off diagonal elements
199     for(Int_t j=0; j<i; ++j) {
200       cmat[i*(i+1)/2+j] = (fDiag[j] >= 0. && fDiag[i] >= 0.) ? fODia[(i-1)*i/2+j]*fDiag[j]*fDiag[i]: -999.;
201     }
202
203     // Diagonal elements
204     cmat[i*(i+1)/2+i] = (fDiag[i] >= 0.) ? fDiag[i]*fDiag[i] : -999.;
205   }
206
207   return;
208 }
209
210 //______________________________________________________________________________
211 void AliAlignObj::GetCovMatrix(TMatrixDSym& mcov) const
212 {
213   // Fills the matrix m passed as argument as the covariance matrix calculated
214   // from the coefficients of the reduced covariance matrix data members
215   //
216
217   for(Int_t i=0; i<6; ++i) {
218     // Off diagonal elements
219     for(Int_t j=0; j<i; ++j) {
220       mcov(j,i) = mcov(i,j) = (fDiag[j] >= 0. && fDiag[i] >= 0.) ? fODia[(i-1)*i/2+j]*fDiag[j]*fDiag[i]: -999.;
221     }
222
223     // Diagonal elements
224     mcov(i,i) = (fDiag[i] >= 0.) ? fDiag[i]*fDiag[i] : -999.;
225   }
226
227 }
228
229 //______________________________________________________________________________
230 void AliAlignObj::SetCorrMatrix(Double_t *cmat)
231 {
232   // Sets the correlation matrix data member from the coefficients of the external covariance
233   // matrix (21 elements passed as argument). 
234   //
235   if(cmat) {
236
237     // Diagonal elements first
238     for(Int_t i=0; i<6; ++i) {
239       fDiag[i] = (cmat[i*(i+1)/2+i] >= 0.) ? TMath::Sqrt(cmat[i*(i+1)/2+i]) : -999.;
240     }
241
242     // ... then the ones off diagonal
243     for(Int_t i=0; i<6; ++i)
244       // Off diagonal elements
245       for(Int_t j=0; j<i; ++j) {
246         fODia[(i-1)*i/2+j] = (fDiag[i] > 0. && fDiag[j] > 0.) ? cmat[i*(i+1)/2+j]/(fDiag[j]*fDiag[i]) : 0.;       // check for division by zero (due to diagonal element of 0) and for fDiag != -999. (due to negative input diagonal element).
247         if (fODia[(i-1)*i/2+j]>1.)  fODia[(i-1)*i/2+j] =  1.; // check upper boundary
248         if (fODia[(i-1)*i/2+j]<-1.) fODia[(i-1)*i/2+j] = -1.; // check lower boundary
249       }
250   } else {
251     for(Int_t i=0; i< 6; ++i) fDiag[i]=-999.;
252     for(Int_t i=0; i< 6*(6-1)/2; ++i) fODia[i]=0.;
253   }
254
255   return;
256 }
257
258 //______________________________________________________________________________
259 void AliAlignObj::SetCorrMatrix(TMatrixDSym& mcov)
260 {
261   // Sets the correlation matrix data member from the covariance matrix mcov passed
262   // passed as argument. 
263   //
264   if(mcov.IsValid()) {
265
266     // Diagonal elements first
267     for(Int_t i=0; i<6; ++i) {
268       fDiag[i] = (mcov(i,i) >= 0.) ? TMath::Sqrt(mcov(i,i)) : -999.;
269     }
270
271     // ... then the ones off diagonal
272     for(Int_t i=0; i<6; ++i)
273       // Off diagonal elements
274       for(Int_t j=0; j<i; ++j) {
275         fODia[(i-1)*i/2+j] = (fDiag[i] > 0. && fDiag[j] > 0.) ? mcov(i,j)/(fDiag[j]*fDiag[i]) : 0.;       // check for division by zero (due to diagonal element of 0) and for fDiag != -999. (due to negative input diagonal element).
276         if (fODia[(i-1)*i/2+j]>1.)  fODia[(i-1)*i/2+j] =  1.; // check upper boundary
277         if (fODia[(i-1)*i/2+j]<-1.) fODia[(i-1)*i/2+j] = -1.; // check lower boundary
278       }
279   } else {
280     for(Int_t i=0; i< 6; ++i) fDiag[i]=-999.;
281     for(Int_t i=0; i< 6*(6-1)/2; ++i) fODia[i]=0.;
282   }
283
284   return;
285 }
286
287 //_____________________________________________________________________________
288 void AliAlignObj::AnglesToMatrix(const Double_t *angles, Double_t *rot) const
289 {
290   // Calculates the rotation matrix using the 
291   // Euler angles in "x y z" notation
292   //
293   Double_t degrad = TMath::DegToRad();
294   Double_t sinpsi = TMath::Sin(degrad*angles[0]);
295   Double_t cospsi = TMath::Cos(degrad*angles[0]);
296   Double_t sinthe = TMath::Sin(degrad*angles[1]);
297   Double_t costhe = TMath::Cos(degrad*angles[1]);
298   Double_t sinphi = TMath::Sin(degrad*angles[2]);
299   Double_t cosphi = TMath::Cos(degrad*angles[2]);
300
301   rot[0] =  costhe*cosphi;
302   rot[1] = -costhe*sinphi;
303   rot[2] =  sinthe;
304   rot[3] =  sinpsi*sinthe*cosphi + cospsi*sinphi;
305   rot[4] = -sinpsi*sinthe*sinphi + cospsi*cosphi;
306   rot[5] = -costhe*sinpsi;
307   rot[6] = -cospsi*sinthe*cosphi + sinpsi*sinphi;
308   rot[7] =  cospsi*sinthe*sinphi + sinpsi*cosphi;
309   rot[8] =  costhe*cospsi;
310 }
311
312 //_____________________________________________________________________________
313 Bool_t AliAlignObj::MatrixToAngles(const Double_t *rot, Double_t *angles) const
314 {
315   // Calculates the Euler angles in "x y z" notation
316   // using the rotation matrix
317   // Returns false in case the rotation angles can not be
318   // extracted from the matrix
319   //
320   if(TMath::Abs(rot[0])<1e-7 || TMath::Abs(rot[8])<1e-7) {
321     AliError("Failed to extract roll-pitch-yall angles!");
322     return kFALSE;
323   }
324   Double_t raddeg = TMath::RadToDeg();
325   angles[0]=raddeg*TMath::ATan2(-rot[5],rot[8]);
326   angles[1]=raddeg*TMath::ASin(rot[2]);
327   angles[2]=raddeg*TMath::ATan2(-rot[1],rot[0]);
328   return kTRUE;
329 }
330
331 //______________________________________________________________________________
332 void AliAlignObj::Transform(AliTrackPoint &p) const
333 {
334   // The method transforms the space-point coordinates using the
335   // transformation matrix provided by the AliAlignObj
336   // The covariance matrix is not affected since we assume
337   // that the transformations are sufficiently small
338   //
339   if (fVolUID != p.GetVolumeID())
340     AliWarning(Form("Alignment object ID is not equal to the space-point ID (%d != %d)",fVolUID,p.GetVolumeID())); 
341
342   TGeoHMatrix m;
343   GetMatrix(m);
344   Double_t *rot = m.GetRotationMatrix();
345   Double_t *tr  = m.GetTranslation();
346
347   Float_t xyzin[3],xyzout[3];
348   p.GetXYZ(xyzin);
349   for (Int_t i = 0; i < 3; i++)
350     xyzout[i] = tr[i]+
351                 xyzin[0]*rot[3*i]+
352                 xyzin[1]*rot[3*i+1]+
353                 xyzin[2]*rot[3*i+2];
354   p.SetXYZ(xyzout);
355   
356 }
357
358 //_____________________________________________________________________________
359 void AliAlignObj::Transform(AliTrackPointArray &array) const
360 {
361   // This method is used to transform all the track points
362   // from the input AliTrackPointArray
363   // 
364   AliTrackPoint p;
365   for (Int_t i = 0; i < array.GetNPoints(); i++) {
366     array.GetPoint(p,i);
367     Transform(p);
368     array.AddPoint(i,&p);
369   }
370 }
371
372 //_____________________________________________________________________________
373 void AliAlignObj::Print(Option_t *) const
374 {
375   // Print the contents of the
376   // alignment object in angles and
377   // matrix representations
378   //
379   Double_t tr[3];
380   GetTranslation(tr);
381   Double_t angles[3];
382   GetAngles(angles);
383   TGeoHMatrix m;
384   GetMatrix(m);
385   const Double_t *rot = m.GetRotationMatrix();
386
387   printf("Volume=%s\n",GetSymName());
388   if (GetVolUID() != 0) {
389     AliGeomManager::ELayerID layerId;
390     Int_t modId;
391     GetVolUID(layerId,modId);
392     printf("VolumeID=%d LayerID=%d ( %s ) ModuleID=%d\n", GetVolUID(),layerId,AliGeomManager::LayerName(layerId),modId);
393   }
394   printf("%12.8f%12.8f%12.8f    Tx = %12.8f    Psi   = %12.8f\n", rot[0], rot[1], rot[2], tr[0], angles[0]);
395   printf("%12.8f%12.8f%12.8f    Ty = %12.8f    Theta = %12.8f\n", rot[3], rot[4], rot[5], tr[1], angles[1]);
396   printf("%12.8f%12.8f%12.8f    Tz = %12.8f    Phi   = %12.8f\n", rot[6], rot[7], rot[8], tr[2], angles[2]);
397
398 }
399
400 //_____________________________________________________________________________
401 void AliAlignObj::SetPars(Double_t x, Double_t y, Double_t z,
402                           Double_t psi, Double_t theta, Double_t phi)
403 {
404   // Set the global delta transformation by passing 3 angles (expressed in
405   // degrees) and 3 shifts (in centimeters)
406   // 
407   SetTranslation(x,y,z);
408   SetRotation(psi,theta,phi);
409 }
410
411 //_____________________________________________________________________________
412 Bool_t AliAlignObj::SetLocalPars(Double_t x, Double_t y, Double_t z,
413                                  Double_t psi, Double_t theta, Double_t phi)
414 {
415   // Set the global delta transformation by passing the parameters
416   // for the local delta transformation (3 shifts and 3 angles).
417   // In case that the TGeo was not initialized or not closed,
418   // returns false and the object parameters are not set.
419   //
420   TGeoHMatrix m;
421   Double_t tr[3] = {x, y, z};
422   m.SetTranslation(tr);
423   Double_t angles[3] = {psi, theta, phi};
424   Double_t rot[9];
425   AnglesToMatrix(angles,rot);
426   m.SetRotation(rot);
427
428   return SetLocalMatrix(m);
429
430 }
431
432 //_____________________________________________________________________________
433 Bool_t AliAlignObj::SetLocalTranslation(Double_t x, Double_t y, Double_t z)
434 {
435   // Set the global delta transformation by passing the three shifts giving
436   // the translation in the local reference system of the alignable
437   // volume (known by TGeo geometry).
438   // In case that the TGeo was not initialized or not closed,
439   // returns false and the object parameters are not set.
440   //
441   TGeoHMatrix m;
442   Double_t tr[3] = {x, y, z};
443   m.SetTranslation(tr);
444
445   return SetLocalMatrix(m);
446
447 }
448
449 //_____________________________________________________________________________
450 Bool_t AliAlignObj::SetLocalTranslation(const TGeoMatrix& m)
451 {
452   // Set the global delta transformation by passing the matrix of
453   // the local delta transformation and taking its translational part
454   // In case that the TGeo was not initialized or not closed,
455   // returns false and the object parameters are not set.
456   //
457   const Double_t* tr = m.GetTranslation();
458   TGeoHMatrix mtr;
459   mtr.SetTranslation(tr);
460
461   return SetLocalMatrix(mtr);
462
463 }
464
465 //_____________________________________________________________________________
466 Bool_t AliAlignObj::SetLocalRotation(Double_t psi, Double_t theta, Double_t phi)
467 {
468   // Set the global delta transformation by passing the three angles giving
469   // the rotation in the local reference system of the alignable
470   // volume (known by TGeo geometry).
471   // In case that the TGeo was not initialized or not closed,
472   // returns false and the object parameters are not set.
473   //
474   TGeoHMatrix m;
475   Double_t angles[3] = {psi, theta, phi};
476   Double_t rot[9];
477   AnglesToMatrix(angles,rot);
478   m.SetRotation(rot);
479
480   return SetLocalMatrix(m);
481
482 }
483
484 //_____________________________________________________________________________
485 Bool_t AliAlignObj::SetLocalRotation(const TGeoMatrix& m)
486 {
487   // Set the global delta transformation by passing the matrix of
488   // the local delta transformation and taking its rotational part
489   // In case that the TGeo was not initialized or not closed,
490   // returns false and the object parameters are not set.
491   //
492   TGeoHMatrix rotm;
493   const Double_t* rot = m.GetRotationMatrix();
494   rotm.SetRotation(rot);
495
496   return SetLocalMatrix(rotm);
497
498 }
499
500 //_____________________________________________________________________________
501 Bool_t AliAlignObj::SetLocalMatrix(const TGeoMatrix& m)
502 {
503   // Set the global delta transformation by passing the TGeo matrix
504   // for the local delta transformation.
505   // In case that the TGeo was not initialized or not closed,
506   // returns false and the object parameters are not set.
507   //
508   if (!gGeoManager || !gGeoManager->IsClosed()) {
509     AliError("Can't set the alignment object parameters! gGeoManager doesn't exist or it is still opened!");
510     return kFALSE;
511   }
512
513   const char* symname = GetSymName();
514   TGeoPhysicalNode* node;
515   TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
516   if(pne){
517     node = gGeoManager->MakeAlignablePN(pne);
518   }else{
519     AliWarning(Form("The symbolic volume name %s does not correspond to a physical entry. Using it as volume path!",symname));
520     node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(symname);
521   }
522
523   if (!node) {
524     AliError(Form("Volume name or path %s not valid!",symname));
525     return kFALSE;
526   }
527   if (node->IsAligned())
528     AliWarning(Form("Volume %s has been already misaligned!",symname));
529
530   TGeoHMatrix m1;
531   const Double_t *tr = m.GetTranslation();
532   m1.SetTranslation(tr);
533   const Double_t* rot = m.GetRotationMatrix();
534   m1.SetRotation(rot);
535
536   TGeoHMatrix align,gprime,gprimeinv;
537   gprime = *node->GetMatrix();
538   gprimeinv = gprime.Inverse();
539   m1.Multiply(&gprimeinv);
540   m1.MultiplyLeft(&gprime);
541
542   return SetMatrix(m1);
543 }
544
545 //_____________________________________________________________________________
546 Bool_t AliAlignObj::SetMatrix(const TGeoMatrix& m)
547 {
548   // Set the global delta transformation by passing the TGeoMatrix
549   // for it
550   //
551   SetTranslation(m);
552   return SetRotation(m);
553 }
554
555 //_____________________________________________________________________________
556 Bool_t AliAlignObj::GetLocalPars(Double_t transl[], Double_t angles[]) const
557 {
558   // Get the translations and angles (in degrees) expressing the
559   // local delta transformation.
560   // In case that the TGeo was not initialized or not closed,
561   // returns false and the object parameters are not set.
562   //
563   if(!GetLocalTranslation(transl)) return kFALSE;
564   return GetLocalAngles(angles);
565 }
566
567 //_____________________________________________________________________________
568 Bool_t AliAlignObj::GetLocalTranslation(Double_t* tr) const
569 {
570   // Get the 3 shifts giving the translational part of the local
571   // delta transformation.
572   // In case that the TGeo was not initialized or not closed,
573   // returns false and the object parameters are not set.
574   //
575   TGeoHMatrix ml;
576   if(!GetLocalMatrix(ml)) return kFALSE;
577   const Double_t* transl;
578   transl = ml.GetTranslation();
579   tr[0]=transl[0];
580   tr[1]=transl[1];
581   tr[2]=transl[2];
582   return kTRUE;
583 }
584
585 //_____________________________________________________________________________
586 Bool_t AliAlignObj::GetLocalAngles(Double_t* angles) const
587 {
588   // Get the 3 angles giving the rotational part of the local
589   // delta transformation.
590   // In case that the TGeo was not initialized or not closed,
591   // returns false and the object parameters are not set.
592   //
593   TGeoHMatrix ml;
594   if(!GetLocalMatrix(ml)) return kFALSE;
595   const Double_t *rot = ml.GetRotationMatrix();
596   return MatrixToAngles(rot,angles);
597 }
598
599 //_____________________________________________________________________________
600 Bool_t AliAlignObj::GetLocalMatrix(TGeoHMatrix& m) const
601 {
602   // Get the matrix for the local delta transformation.
603   // In case that the TGeo was not initialized or not closed,
604   // returns false and the object parameters are not set.
605   //
606   if (!gGeoManager || !gGeoManager->IsClosed()) {
607     AliError("Can't set the alignment object parameters! gGeoManager doesn't exist or it is still opened!");
608     return kFALSE;
609   }
610
611   const char* symname = GetSymName();
612   TGeoPhysicalNode* node;
613   TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
614   if(pne){
615     node = gGeoManager->MakeAlignablePN(pne);
616   }else{
617     AliWarning(Form("The symbolic volume name %s does not correspond to a physical entry. Using it as volume path!",symname));
618     node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(symname);
619   }
620
621   if (!node) {
622     AliError(Form("Volume name or path %s not valid!",symname));
623     return kFALSE;
624   }
625   if (node->IsAligned())
626     AliWarning(Form("Volume %s has been already misaligned!",symname));
627
628   GetMatrix(m);
629   TGeoHMatrix gprime,gprimeinv;
630   gprime = *node->GetMatrix();
631   gprimeinv = gprime.Inverse();
632   m.Multiply(&gprime);
633   m.MultiplyLeft(&gprimeinv);
634
635   return kTRUE;
636 }
637
638 //_____________________________________________________________________________
639 Bool_t AliAlignObj::ApplyToGeometry()
640 {
641   // Apply the current alignment object to the TGeo geometry
642   // This method returns FALSE if the symname of the object was not
643   // valid neither to get a TGeoPEntry nor as a volume path
644   //
645   if (!gGeoManager || !gGeoManager->IsClosed()) {
646     AliError("Can't apply the alignment object! gGeoManager doesn't exist or it is still opened!");
647     return kFALSE;
648   }
649   
650   const char* symname = GetSymName();
651   const char* path;
652   TGeoPhysicalNode* node;
653   TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
654   if(pne){
655     path = pne->GetTitle();
656     node = gGeoManager->MakeAlignablePN(pne);
657   }else{
658     AliDebug(1,Form("The symbolic volume name %s does not correspond to a physical entry. Using it as a volume path!",symname));
659     path=symname;
660     if (!gGeoManager->CheckPath(path)) {
661       AliDebug(1,Form("Volume path %s not valid!",path));
662       return kFALSE;
663     }
664     if (gGeoManager->GetListOfPhysicalNodes()->FindObject(path)) {
665       AliError(Form("Volume %s has already been misaligned!",path));
666       return kFALSE;
667     }
668     node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(path);
669   }
670
671   if (!node) {
672     AliError(Form("Volume path %s not valid!",path));
673     return kFALSE;
674   }
675
676   TGeoHMatrix align,gprime;
677   gprime = *node->GetMatrix();
678   GetMatrix(align);
679   gprime.MultiplyLeft(&align);
680   TGeoHMatrix *ginv = new TGeoHMatrix;
681   TGeoHMatrix *g = node->GetMatrix(node->GetLevel()-1);
682   *ginv = g->Inverse();
683   *ginv *= gprime;
684   AliGeomManager::ELayerID layerId; // unique identity for layer in the alobj
685   Int_t modId; // unique identity for volume inside layer in the alobj
686   GetVolUID(layerId, modId);
687   AliDebug(2,Form("Aligning volume %s of detector layer %d with local ID %d",symname,layerId,modId));
688   node->Align(ginv);
689
690   return kTRUE;
691 }
692