1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
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 //-----------------------------------------------------------------
25 #include <TGeoManager.h>
26 #include <TGeoPhysicalNode.h>
28 #include <TMatrixDSym.h>
30 #include "AliAlignObj.h"
31 #include "AliTrackPointArray.h"
36 //_____________________________________________________________________________
37 AliAlignObj::AliAlignObj():
41 // default constructor
42 for(Int_t i=0; i<6; i++) fDiag[i]=-999.;
43 for(Int_t i=0; i<21; i++) fODia[i]=-999.;
46 //_____________________________________________________________________________
47 AliAlignObj::AliAlignObj(const char* symname, UShort_t voluid) :
52 // standard constructor
54 for(Int_t i=0; i<6; i++) fDiag[i]=-999.;
55 for(Int_t i=0; i<21; i++) fODia[i]=-999.;
58 //_____________________________________________________________________________
59 AliAlignObj::AliAlignObj(const char* symname, UShort_t voluid, Double_t* cmat) :
64 // standard constructor
69 //_____________________________________________________________________________
70 AliAlignObj::AliAlignObj(const AliAlignObj& theAlignObj) :
72 fVolPath(theAlignObj.GetSymName()),
73 fVolUID(theAlignObj.GetVolUID())
76 for(Int_t i=0; i<6; i++) fDiag[i]=theAlignObj.fDiag[i];
77 for(Int_t i=0; i<21; i++) fODia[i]=theAlignObj.fODia[i];
80 //_____________________________________________________________________________
81 AliAlignObj &AliAlignObj::operator =(const AliAlignObj& theAlignObj)
83 // assignment operator
84 if(this==&theAlignObj) return *this;
85 fVolPath = theAlignObj.GetSymName();
86 fVolUID = theAlignObj.GetVolUID();
87 for(Int_t i=0; i<6; i++) fDiag[i]=theAlignObj.fDiag[i];
88 for(Int_t i=0; i<21; i++) fODia[i]=theAlignObj.fODia[i];
92 //_____________________________________________________________________________
93 AliAlignObj &AliAlignObj::operator*=(const AliAlignObj& theAlignObj)
95 // multiplication operator
96 // The operator can be used to 'combine'
97 // two alignment objects
101 theAlignObj.GetMatrix(m2);
102 m1.MultiplyLeft(&m2);
104 // temporary solution: consider parameters indipendent
105 for(Int_t i=0; i<6; i++) fDiag[i] = TMath::Sqrt((fDiag[i]*fDiag[i])+(theAlignObj.fDiag[i]*theAlignObj.fDiag[i]));
109 //_____________________________________________________________________________
110 AliAlignObj::~AliAlignObj()
115 //_____________________________________________________________________________
116 void AliAlignObj::SetVolUID(AliGeomManager::ELayerID detId, Int_t modId)
118 // From detector name and module number (according to detector numbering)
119 // build fVolUID, unique numerical identity of that volume inside ALICE
120 // fVolUID is 16 bits, first 5 reserved for detID (32 possible values),
121 // remaining 11 for module ID inside det (2048 possible values).
123 fVolUID = AliGeomManager::LayerToVolUID(detId,modId);
126 //_____________________________________________________________________________
127 void AliAlignObj::GetVolUID(AliGeomManager::ELayerID &layerId, Int_t &modId) const
129 // From the fVolUID, unique numerical identity of that volume inside ALICE,
130 // (voluid is 16 bits, first 5 reserved for layerID (32 possible values),
131 // remaining 11 for module ID inside det (2048 possible values)), sets
132 // the argument layerId to the identity of the layer to which that volume
133 // belongs and sets the argument modId to the identity of that volume
134 // internally to the layer.
136 layerId = AliGeomManager::VolUIDToLayer(fVolUID,modId);
139 //_____________________________________________________________________________
140 Bool_t AliAlignObj::GetPars(Double_t tr[], Double_t angles[]) const
143 return GetAngles(angles);
146 //_____________________________________________________________________________
147 Int_t AliAlignObj::GetLevel() const
149 // Return the geometry level of the alignable volume to which
150 // the alignment object is associated; this is the number of
151 // slashes in the corresponding volume path
154 AliWarning("gGeoManager doesn't exist or it is still opened: unable to return meaningful level value.");
157 const char* symname = GetSymName();
159 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
161 path = pne->GetTitle();
166 TString pathStr = path;
167 if(pathStr[0]!='/') pathStr.Prepend('/');
168 return pathStr.CountChar('/');
171 //_____________________________________________________________________________
172 Int_t AliAlignObj::Compare(const TObject *obj) const
174 // Compare the levels of two
176 // Used in the sorting during
177 // the application of alignment
178 // objects to the geometry
180 Int_t level = GetLevel();
181 Int_t level2 = ((AliAlignObj *)obj)->GetLevel();
185 return ((level > level2) ? 1 : -1);
188 //______________________________________________________________________________
189 void AliAlignObj::GetCovMatrix(Double_t *cmat) const
191 // Fills the cmat argument with the coefficients of the external cov matrix (21 elements)
192 // calculating them from the correlation matrix data member
195 for(Int_t i=0; i<6; ++i) {
196 // Off diagonal elements
197 for(Int_t j=0; j<i; ++j) {
198 cmat[i*(i+1)/2+j] = (fDiag[j] >= 0. && fDiag[i] >= 0.) ? fODia[(i-1)*i/2+j]*fDiag[j]*fDiag[i]: -999.;
202 cmat[i*(i+1)/2+i] = (fDiag[i] >= 0.) ? fDiag[i]*fDiag[i] : -999.;
208 //______________________________________________________________________________
209 void AliAlignObj::GetCovMatrix(TMatrixDSym& mcov) const
211 // Fills the matrix m passed as argument as the covariance matrix calculated
212 // from the coefficients of the reduced covariance matrix data members
215 for(Int_t i=0; i<6; ++i) {
216 // Off diagonal elements
217 for(Int_t j=0; j<i; ++j) {
218 mcov(j,i) = mcov(i,j) = (fDiag[j] >= 0. && fDiag[i] >= 0.) ? fODia[(i-1)*i/2+j]*fDiag[j]*fDiag[i]: -999.;
222 mcov(i,i) = (fDiag[i] >= 0.) ? fDiag[i]*fDiag[i] : -999.;
227 //______________________________________________________________________________
228 void AliAlignObj::SetCorrMatrix(Double_t *cmat)
230 // Sets the correlation matrix data member from the coefficients of the external covariance
231 // matrix (21 elements passed as argument).
235 // Diagonal elements first
236 for(Int_t i=0; i<6; ++i) {
237 fDiag[i] = (cmat[i*(i+1)/2+i] >= 0.) ? TMath::Sqrt(cmat[i*(i+1)/2+i]) : -999.;
240 // ... then the ones off diagonal
241 for(Int_t i=0; i<6; ++i)
242 // Off diagonal elements
243 for(Int_t j=0; j<i; ++j) {
244 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).
245 if (fODia[(i-1)*i/2+j]>1.) fODia[(i-1)*i/2+j] = 1.; // check upper boundary
246 if (fODia[(i-1)*i/2+j]<-1.) fODia[(i-1)*i/2+j] = -1.; // check lower boundary
249 for(Int_t i=0; i< 6; ++i) fDiag[i]=-999.;
250 for(Int_t i=0; i< 6*(6-1)/2; ++i) fODia[i]=0.;
256 //______________________________________________________________________________
257 void AliAlignObj::SetCorrMatrix(TMatrixDSym& mcov)
259 // Sets the correlation matrix data member from the covariance matrix mcov passed
260 // passed as argument.
264 // Diagonal elements first
265 for(Int_t i=0; i<6; ++i) {
266 fDiag[i] = (mcov(i,i) >= 0.) ? TMath::Sqrt(mcov(i,i)) : -999.;
269 // ... then the ones off diagonal
270 for(Int_t i=0; i<6; ++i)
271 // Off diagonal elements
272 for(Int_t j=0; j<i; ++j) {
273 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).
274 if (fODia[(i-1)*i/2+j]>1.) fODia[(i-1)*i/2+j] = 1.; // check upper boundary
275 if (fODia[(i-1)*i/2+j]<-1.) fODia[(i-1)*i/2+j] = -1.; // check lower boundary
278 for(Int_t i=0; i< 6; ++i) fDiag[i]=-999.;
279 for(Int_t i=0; i< 6*(6-1)/2; ++i) fODia[i]=0.;
285 //_____________________________________________________________________________
286 void AliAlignObj::AnglesToMatrix(const Double_t *angles, Double_t *rot) const
288 // Calculates the rotation matrix using the
289 // Euler angles in "x y z" notation
291 Double_t degrad = TMath::DegToRad();
292 Double_t sinpsi = TMath::Sin(degrad*angles[0]);
293 Double_t cospsi = TMath::Cos(degrad*angles[0]);
294 Double_t sinthe = TMath::Sin(degrad*angles[1]);
295 Double_t costhe = TMath::Cos(degrad*angles[1]);
296 Double_t sinphi = TMath::Sin(degrad*angles[2]);
297 Double_t cosphi = TMath::Cos(degrad*angles[2]);
299 rot[0] = costhe*cosphi;
300 rot[1] = -costhe*sinphi;
302 rot[3] = sinpsi*sinthe*cosphi + cospsi*sinphi;
303 rot[4] = -sinpsi*sinthe*sinphi + cospsi*cosphi;
304 rot[5] = -costhe*sinpsi;
305 rot[6] = -cospsi*sinthe*cosphi + sinpsi*sinphi;
306 rot[7] = cospsi*sinthe*sinphi + sinpsi*cosphi;
307 rot[8] = costhe*cospsi;
310 //_____________________________________________________________________________
311 Bool_t AliAlignObj::MatrixToAngles(const Double_t *rot, Double_t *angles) const
313 // Calculates the Euler angles in "x y z" notation
314 // using the rotation matrix
315 // Returns false in case the rotation angles can not be
316 // extracted from the matrix
318 if(TMath::Abs(rot[0])<1e-7 || TMath::Abs(rot[8])<1e-7) {
319 AliError("Failed to extract roll-pitch-yall angles!");
322 Double_t raddeg = TMath::RadToDeg();
323 angles[0]=raddeg*TMath::ATan2(-rot[5],rot[8]);
324 angles[1]=raddeg*TMath::ASin(rot[2]);
325 angles[2]=raddeg*TMath::ATan2(-rot[1],rot[0]);
329 //______________________________________________________________________________
330 void AliAlignObj::Transform(AliTrackPoint &p) const
332 // The method transforms the space-point coordinates using the
333 // transformation matrix provided by the AliAlignObj
334 // The covariance matrix is not affected since we assume
335 // that the transformations are sufficiently small
337 if (fVolUID != p.GetVolumeID())
338 AliWarning(Form("Alignment object ID is not equal to the space-point ID (%d != %d)",fVolUID,p.GetVolumeID()));
342 Double_t *rot = m.GetRotationMatrix();
343 Double_t *tr = m.GetTranslation();
345 Float_t xyzin[3],xyzout[3];
347 for (Int_t i = 0; i < 3; i++)
356 //_____________________________________________________________________________
357 void AliAlignObj::Transform(AliTrackPointArray &array) const
359 // This method is used to transform all the track points
360 // from the input AliTrackPointArray
363 for (Int_t i = 0; i < array.GetNPoints(); i++) {
366 array.AddPoint(i,&p);
370 //_____________________________________________________________________________
371 void AliAlignObj::Print(Option_t *) const
373 // Print the contents of the
374 // alignment object in angles and
375 // matrix representations
383 const Double_t *rot = m.GetRotationMatrix();
385 printf("Volume=%s\n",GetSymName());
386 if (GetVolUID() != 0) {
387 AliGeomManager::ELayerID layerId;
389 GetVolUID(layerId,modId);
390 printf("VolumeID=%d LayerID=%d ( %s ) ModuleID=%d\n", GetVolUID(),layerId,AliGeomManager::LayerName(layerId),modId);
392 printf("%12.8f%12.8f%12.8f Tx = %12.8f Psi = %12.8f\n", rot[0], rot[1], rot[2], tr[0], angles[0]);
393 printf("%12.8f%12.8f%12.8f Ty = %12.8f Theta = %12.8f\n", rot[3], rot[4], rot[5], tr[1], angles[1]);
394 printf("%12.8f%12.8f%12.8f Tz = %12.8f Phi = %12.8f\n", rot[6], rot[7], rot[8], tr[2], angles[2]);
398 //_____________________________________________________________________________
399 void AliAlignObj::SetPars(Double_t x, Double_t y, Double_t z,
400 Double_t psi, Double_t theta, Double_t phi)
402 // Set the global delta transformation by passing 3 angles (expressed in
403 // degrees) and 3 shifts (in centimeters)
405 SetTranslation(x,y,z);
406 SetRotation(psi,theta,phi);
409 //_____________________________________________________________________________
410 Bool_t AliAlignObj::SetLocalPars(Double_t x, Double_t y, Double_t z,
411 Double_t psi, Double_t theta, Double_t phi)
413 // Set the global delta transformation by passing the parameters
414 // for the local delta transformation (3 shifts and 3 angles).
415 // In case that the TGeo was not initialized or not closed,
416 // returns false and the object parameters are not set.
419 Double_t tr[3] = {x, y, z};
420 m.SetTranslation(tr);
421 Double_t angles[3] = {psi, theta, phi};
423 AnglesToMatrix(angles,rot);
426 return SetLocalMatrix(m);
430 //_____________________________________________________________________________
431 Bool_t AliAlignObj::SetLocalTranslation(Double_t x, Double_t y, Double_t z)
433 // Set the global delta transformation by passing the three shifts giving
434 // the translation in the local reference system of the alignable
435 // volume (known by TGeo geometry).
436 // In case that the TGeo was not initialized or not closed,
437 // returns false and the object parameters are not set.
440 Double_t tr[3] = {x, y, z};
441 m.SetTranslation(tr);
443 return SetLocalMatrix(m);
447 //_____________________________________________________________________________
448 Bool_t AliAlignObj::SetLocalTranslation(const TGeoMatrix& m)
450 // Set the global delta transformation by passing the matrix of
451 // the local delta transformation and taking its translational part
452 // In case that the TGeo was not initialized or not closed,
453 // returns false and the object parameters are not set.
455 const Double_t* tr = m.GetTranslation();
457 mtr.SetTranslation(tr);
459 return SetLocalMatrix(mtr);
463 //_____________________________________________________________________________
464 Bool_t AliAlignObj::SetLocalRotation(Double_t psi, Double_t theta, Double_t phi)
466 // Set the global delta transformation by passing the three angles giving
467 // the rotation in the local reference system of the alignable
468 // volume (known by TGeo geometry).
469 // In case that the TGeo was not initialized or not closed,
470 // returns false and the object parameters are not set.
473 Double_t angles[3] = {psi, theta, phi};
475 AnglesToMatrix(angles,rot);
478 return SetLocalMatrix(m);
482 //_____________________________________________________________________________
483 Bool_t AliAlignObj::SetLocalRotation(const TGeoMatrix& m)
485 // Set the global delta transformation by passing the matrix of
486 // the local delta transformation and taking its rotational part
487 // In case that the TGeo was not initialized or not closed,
488 // returns false and the object parameters are not set.
491 const Double_t* rot = m.GetRotationMatrix();
492 rotm.SetRotation(rot);
494 return SetLocalMatrix(rotm);
498 //_____________________________________________________________________________
499 Bool_t AliAlignObj::SetLocalMatrix(const TGeoMatrix& m)
501 // Set the global delta transformation by passing the TGeo matrix
502 // for the local delta transformation.
503 // In case that the TGeo was not initialized or not closed,
504 // returns false and the object parameters are not set.
506 if (!gGeoManager || !gGeoManager->IsClosed()) {
507 AliError("Can't set the alignment object parameters! gGeoManager doesn't exist or it is still opened!");
511 const char* symname = GetSymName();
512 TGeoPhysicalNode* node;
513 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
515 node = gGeoManager->MakeAlignablePN(pne);
517 AliWarning(Form("The symbolic volume name %s does not correspond to a physical entry. Using it as volume path!",symname));
518 node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(symname);
522 AliError(Form("Volume name or path %s not valid!",symname));
525 if (node->IsAligned())
526 AliWarning(Form("Volume %s has been already misaligned!",symname));
529 const Double_t *tr = m.GetTranslation();
530 m1.SetTranslation(tr);
531 const Double_t* rot = m.GetRotationMatrix();
534 TGeoHMatrix align,gprime,gprimeinv;
535 gprime = *node->GetMatrix();
536 gprimeinv = gprime.Inverse();
537 m1.Multiply(&gprimeinv);
538 m1.MultiplyLeft(&gprime);
540 return SetMatrix(m1);
543 //_____________________________________________________________________________
544 Bool_t AliAlignObj::SetMatrix(const TGeoMatrix& m)
546 // Set the global delta transformation by passing the TGeoMatrix
550 return SetRotation(m);
553 //_____________________________________________________________________________
554 Bool_t AliAlignObj::GetLocalPars(Double_t transl[], Double_t angles[]) const
556 // Get the translations and angles (in degrees) expressing the
557 // local delta transformation.
558 // In case that the TGeo was not initialized or not closed,
559 // returns false and the object parameters are not set.
561 if(!GetLocalTranslation(transl)) return kFALSE;
562 return GetLocalAngles(angles);
565 //_____________________________________________________________________________
566 Bool_t AliAlignObj::GetLocalTranslation(Double_t* tr) const
568 // Get the 3 shifts giving the translational part of the local
569 // delta transformation.
570 // In case that the TGeo was not initialized or not closed,
571 // returns false and the object parameters are not set.
574 if(!GetLocalMatrix(ml)) return kFALSE;
575 const Double_t* transl;
576 transl = ml.GetTranslation();
583 //_____________________________________________________________________________
584 Bool_t AliAlignObj::GetLocalAngles(Double_t* angles) const
586 // Get the 3 angles giving the rotational part of the local
587 // delta transformation.
588 // In case that the TGeo was not initialized or not closed,
589 // returns false and the object parameters are not set.
592 if(!GetLocalMatrix(ml)) return kFALSE;
593 const Double_t *rot = ml.GetRotationMatrix();
594 return MatrixToAngles(rot,angles);
597 //_____________________________________________________________________________
598 Bool_t AliAlignObj::GetLocalMatrix(TGeoHMatrix& m) const
600 // Get the matrix for the local delta transformation.
601 // In case that the TGeo was not initialized or not closed,
602 // returns false and the object parameters are not set.
604 if (!gGeoManager || !gGeoManager->IsClosed()) {
605 AliError("Can't set the alignment object parameters! gGeoManager doesn't exist or it is still opened!");
609 const char* symname = GetSymName();
610 TGeoPhysicalNode* node;
611 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
613 node = gGeoManager->MakeAlignablePN(pne);
615 AliWarning(Form("The symbolic volume name %s does not correspond to a physical entry. Using it as volume path!",symname));
616 node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(symname);
620 AliError(Form("Volume name or path %s not valid!",symname));
623 if (node->IsAligned())
624 AliWarning(Form("Volume %s has been already misaligned!",symname));
627 TGeoHMatrix gprime,gprimeinv;
628 gprime = *node->GetMatrix();
629 gprimeinv = gprime.Inverse();
631 m.MultiplyLeft(&gprimeinv);
636 //_____________________________________________________________________________
637 Bool_t AliAlignObj::ApplyToGeometry()
639 // Apply the current alignment object to the TGeo geometry
640 // This method returns FALSE if the symname of the object was not
641 // valid neither to get a TGeoPEntry nor as a volume path
643 if (!gGeoManager || !gGeoManager->IsClosed()) {
644 AliError("Can't apply the alignment object! gGeoManager doesn't exist or it is still opened!");
648 const char* symname = GetSymName();
650 TGeoPhysicalNode* node;
651 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
653 path = pne->GetTitle();
654 node = gGeoManager->MakeAlignablePN(pne);
656 AliDebug(1,Form("The symbolic volume name %s does not correspond to a physical entry. Using it as a volume path!",symname));
658 if (!gGeoManager->CheckPath(path)) {
659 AliDebug(1,Form("Volume path %s not valid!",path));
662 if (gGeoManager->GetListOfPhysicalNodes()->FindObject(path)) {
663 AliError(Form("Volume %s has already been misaligned!",path));
666 node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(path);
670 AliError(Form("Volume path %s not valid!",path));
674 TGeoHMatrix align,gprime;
675 gprime = *node->GetMatrix();
677 gprime.MultiplyLeft(&align);
678 TGeoHMatrix *ginv = new TGeoHMatrix;
679 TGeoHMatrix *g = node->GetMatrix(node->GetLevel()-1);
680 *ginv = g->Inverse();
682 AliGeomManager::ELayerID layerId; // unique identity for layer in the alobj
683 Int_t modId; // unique identity for volume inside layer in the alobj
684 GetVolUID(layerId, modId);
685 AliDebug(2,Form("Aligning volume %s of detector layer %d with local ID %d",symname,layerId,modId));