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<15; 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<15; 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<15; 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<15; 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: the covariance matrix of the resulting combined object
105 // is set equal to the covariance matrix of the right operand
106 // (not to be used for combining alignment objects for different levels)
107 for(Int_t i=0; i<6; i++) fDiag[i] = theAlignObj.fDiag[i];
108 for(Int_t i=0; i<15; i++) fODia[i] = theAlignObj.fODia[i];
112 //_____________________________________________________________________________
113 AliAlignObj::~AliAlignObj()
118 //_____________________________________________________________________________
119 void AliAlignObj::SetVolUID(AliGeomManager::ELayerID detId, Int_t modId)
121 // From detector name and module number (according to detector numbering)
122 // build fVolUID, unique numerical identity of that volume inside ALICE
123 // fVolUID is 16 bits, first 5 reserved for detID (32 possible values),
124 // remaining 11 for module ID inside det (2048 possible values).
126 fVolUID = AliGeomManager::LayerToVolUID(detId,modId);
129 //_____________________________________________________________________________
130 void AliAlignObj::GetVolUID(AliGeomManager::ELayerID &layerId, Int_t &modId) const
132 // From the fVolUID, unique numerical identity of that volume inside ALICE,
133 // (voluid is 16 bits, first 5 reserved for layerID (32 possible values),
134 // remaining 11 for module ID inside det (2048 possible values)), sets
135 // the argument layerId to the identity of the layer to which that volume
136 // belongs and sets the argument modId to the identity of that volume
137 // internally to the layer.
139 layerId = AliGeomManager::VolUIDToLayer(fVolUID,modId);
142 //_____________________________________________________________________________
143 Bool_t AliAlignObj::GetPars(Double_t tr[], Double_t angles[]) const
146 return GetAngles(angles);
149 //_____________________________________________________________________________
150 Int_t AliAlignObj::GetLevel() const
152 // Return the geometry level of the alignable volume to which
153 // the alignment object is associated; this is the number of
154 // slashes in the corresponding volume path
157 AliWarning("gGeoManager doesn't exist or it is still opened: unable to return meaningful level value.");
160 const char* symname = GetSymName();
162 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
164 path = pne->GetTitle();
169 TString pathStr = path;
170 if(pathStr[0]!='/') pathStr.Prepend('/');
171 return pathStr.CountChar('/');
174 //_____________________________________________________________________________
175 Int_t AliAlignObj::Compare(const TObject *obj) const
177 // Compare the levels of two
179 // Used in the sorting during
180 // the application of alignment
181 // objects to the geometry
183 Int_t level = GetLevel();
184 Int_t level2 = ((AliAlignObj *)obj)->GetLevel();
188 return ((level > level2) ? 1 : -1);
191 //______________________________________________________________________________
192 void AliAlignObj::GetCovMatrix(Double_t *cmat) const
194 // Fills the cmat argument with the coefficients of the external cov matrix (21 elements)
195 // calculating them from the correlation matrix data member
198 for(Int_t i=0; i<6; ++i) {
199 // Off diagonal elements
200 for(Int_t j=0; j<i; ++j) {
201 cmat[i*(i+1)/2+j] = (fDiag[j] >= 0. && fDiag[i] >= 0.) ? fODia[(i-1)*i/2+j]*fDiag[j]*fDiag[i]: -999.;
205 cmat[i*(i+1)/2+i] = (fDiag[i] >= 0.) ? fDiag[i]*fDiag[i] : -999.;
211 //______________________________________________________________________________
212 void AliAlignObj::GetCovMatrix(TMatrixDSym& mcov) const
214 // Fills the matrix m passed as argument as the covariance matrix calculated
215 // from the coefficients of the reduced covariance matrix data members
218 for(Int_t i=0; i<6; ++i) {
219 // Off diagonal elements
220 for(Int_t j=0; j<i; ++j) {
221 mcov(j,i) = mcov(i,j) = (fDiag[j] >= 0. && fDiag[i] >= 0.) ? fODia[(i-1)*i/2+j]*fDiag[j]*fDiag[i]: -999.;
225 mcov(i,i) = (fDiag[i] >= 0.) ? fDiag[i]*fDiag[i] : -999.;
230 //______________________________________________________________________________
231 void AliAlignObj::SetCorrMatrix(Double_t *cmat)
233 // Sets the correlation matrix data member from the coefficients of the external covariance
234 // matrix (21 elements passed as argument).
238 // Diagonal elements first
239 for(Int_t i=0; i<6; ++i) {
240 fDiag[i] = (cmat[i*(i+1)/2+i] >= 0.) ? TMath::Sqrt(cmat[i*(i+1)/2+i]) : -999.;
243 // ... then the ones off diagonal
244 for(Int_t i=0; i<6; ++i)
245 // Off diagonal elements
246 for(Int_t j=0; j<i; ++j) {
247 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).
248 if (fODia[(i-1)*i/2+j]>1.) fODia[(i-1)*i/2+j] = 1.; // check upper boundary
249 if (fODia[(i-1)*i/2+j]<-1.) fODia[(i-1)*i/2+j] = -1.; // check lower boundary
252 for(Int_t i=0; i< 6; ++i) fDiag[i]=-999.;
253 for(Int_t i=0; i< 6*(6-1)/2; ++i) fODia[i]=0.;
259 //______________________________________________________________________________
260 void AliAlignObj::SetCorrMatrix(TMatrixDSym& mcov)
262 // Sets the correlation matrix data member from the covariance matrix mcov passed
263 // passed as argument.
267 // Diagonal elements first
268 for(Int_t i=0; i<6; ++i) {
269 fDiag[i] = (mcov(i,i) >= 0.) ? TMath::Sqrt(mcov(i,i)) : -999.;
272 // ... then the ones off diagonal
273 for(Int_t i=0; i<6; ++i)
274 // Off diagonal elements
275 for(Int_t j=0; j<i; ++j) {
276 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).
277 if (fODia[(i-1)*i/2+j]>1.) fODia[(i-1)*i/2+j] = 1.; // check upper boundary
278 if (fODia[(i-1)*i/2+j]<-1.) fODia[(i-1)*i/2+j] = -1.; // check lower boundary
281 for(Int_t i=0; i< 6; ++i) fDiag[i]=-999.;
282 for(Int_t i=0; i< 6*(6-1)/2; ++i) fODia[i]=0.;
288 //_____________________________________________________________________________
289 void AliAlignObj::AnglesToMatrix(const Double_t *angles, Double_t *rot) const
291 // Calculates the rotation matrix using the
292 // Euler angles in "x y z" notation
294 Double_t degrad = TMath::DegToRad();
295 Double_t sinpsi = TMath::Sin(degrad*angles[0]);
296 Double_t cospsi = TMath::Cos(degrad*angles[0]);
297 Double_t sinthe = TMath::Sin(degrad*angles[1]);
298 Double_t costhe = TMath::Cos(degrad*angles[1]);
299 Double_t sinphi = TMath::Sin(degrad*angles[2]);
300 Double_t cosphi = TMath::Cos(degrad*angles[2]);
302 rot[0] = costhe*cosphi;
303 rot[1] = -costhe*sinphi;
305 rot[3] = sinpsi*sinthe*cosphi + cospsi*sinphi;
306 rot[4] = -sinpsi*sinthe*sinphi + cospsi*cosphi;
307 rot[5] = -costhe*sinpsi;
308 rot[6] = -cospsi*sinthe*cosphi + sinpsi*sinphi;
309 rot[7] = cospsi*sinthe*sinphi + sinpsi*cosphi;
310 rot[8] = costhe*cospsi;
313 //_____________________________________________________________________________
314 Bool_t AliAlignObj::MatrixToAngles(const Double_t *rot, Double_t *angles) const
316 // Calculates the Euler angles in "x y z" notation
317 // using the rotation matrix
318 // Returns false in case the rotation angles can not be
319 // extracted from the matrix
321 if(TMath::Abs(rot[0])<1e-7 || TMath::Abs(rot[8])<1e-7) {
322 AliError("Failed to extract roll-pitch-yall angles!");
325 Double_t raddeg = TMath::RadToDeg();
326 angles[0]=raddeg*TMath::ATan2(-rot[5],rot[8]);
327 angles[1]=raddeg*TMath::ASin(rot[2]);
328 angles[2]=raddeg*TMath::ATan2(-rot[1],rot[0]);
332 //______________________________________________________________________________
333 void AliAlignObj::Transform(AliTrackPoint &p, Bool_t copycov) const
335 // The method transforms the space-point coordinates using the
336 // transformation matrix provided by the AliAlignObj
337 // In case the copycov flag is set to kTRUE, the covariance matrix
338 // of the alignment object is copied into the space-point
340 if (fVolUID != p.GetVolumeID())
341 AliWarning(Form("Alignment object ID is not equal to the space-point ID (%d != %d)",fVolUID,p.GetVolumeID()));
345 Double_t *rot = m.GetRotationMatrix();
346 Double_t *tr = m.GetTranslation();
348 Float_t xyzin[3],xyzout[3];
350 for (Int_t i = 0; i < 3; i++)
358 TMatrixDSym covmat(6);
359 GetCovMatrix(covmat);
360 p.SetAlignCovMatrix(covmat);
365 //_____________________________________________________________________________
366 void AliAlignObj::Transform(AliTrackPointArray &array) const
368 // This method is used to transform all the track points
369 // from the input AliTrackPointArray
372 for (Int_t i = 0; i < array.GetNPoints(); i++) {
375 array.AddPoint(i,&p);
379 //_____________________________________________________________________________
380 void AliAlignObj::Print(Option_t *) const
382 // Print the contents of the
383 // alignment object in angles and
384 // matrix representations
392 const Double_t *rot = m.GetRotationMatrix();
394 printf("Volume=%s\n",GetSymName());
395 if (GetVolUID() != 0) {
396 AliGeomManager::ELayerID layerId;
398 GetVolUID(layerId,modId);
399 printf("VolumeID=%d LayerID=%d ( %s ) ModuleID=%d\n", GetVolUID(),layerId,AliGeomManager::LayerName(layerId),modId);
401 printf("%12.8f%12.8f%12.8f Tx = %12.8f Psi = %12.8f\n", rot[0], rot[1], rot[2], tr[0], angles[0]);
402 printf("%12.8f%12.8f%12.8f Ty = %12.8f Theta = %12.8f\n", rot[3], rot[4], rot[5], tr[1], angles[1]);
403 printf("%12.8f%12.8f%12.8f Tz = %12.8f Phi = %12.8f\n", rot[6], rot[7], rot[8], tr[2], angles[2]);
407 //_____________________________________________________________________________
408 void AliAlignObj::SetPars(Double_t x, Double_t y, Double_t z,
409 Double_t psi, Double_t theta, Double_t phi)
411 // Set the global delta transformation by passing 3 angles (expressed in
412 // degrees) and 3 shifts (in centimeters)
414 SetTranslation(x,y,z);
415 SetRotation(psi,theta,phi);
418 //_____________________________________________________________________________
419 Bool_t AliAlignObj::SetLocalPars(Double_t x, Double_t y, Double_t z,
420 Double_t psi, Double_t theta, Double_t phi)
422 // Set the global delta transformation by passing the parameters
423 // for the local delta transformation (3 shifts and 3 angles).
424 // In case that the TGeo was not initialized or not closed,
425 // returns false and the object parameters are not set.
428 Double_t tr[3] = {x, y, z};
429 m.SetTranslation(tr);
430 Double_t angles[3] = {psi, theta, phi};
432 AnglesToMatrix(angles,rot);
435 return SetLocalMatrix(m);
439 //_____________________________________________________________________________
440 Bool_t AliAlignObj::SetLocalTranslation(Double_t x, Double_t y, Double_t z)
442 // Set the global delta transformation by passing the three shifts giving
443 // the translation in the local reference system of the alignable
444 // volume (known by TGeo geometry).
445 // In case that the TGeo was not initialized or not closed,
446 // returns false and the object parameters are not set.
449 Double_t tr[3] = {x, y, z};
450 m.SetTranslation(tr);
452 return SetLocalMatrix(m);
456 //_____________________________________________________________________________
457 Bool_t AliAlignObj::SetLocalTranslation(const TGeoMatrix& m)
459 // Set the global delta transformation by passing the matrix of
460 // the local delta transformation and taking its translational part
461 // In case that the TGeo was not initialized or not closed,
462 // returns false and the object parameters are not set.
464 const Double_t* tr = m.GetTranslation();
466 mtr.SetTranslation(tr);
468 return SetLocalMatrix(mtr);
472 //_____________________________________________________________________________
473 Bool_t AliAlignObj::SetLocalRotation(Double_t psi, Double_t theta, Double_t phi)
475 // Set the global delta transformation by passing the three angles giving
476 // the rotation in the local reference system of the alignable
477 // volume (known by TGeo geometry).
478 // In case that the TGeo was not initialized or not closed,
479 // returns false and the object parameters are not set.
482 Double_t angles[3] = {psi, theta, phi};
484 AnglesToMatrix(angles,rot);
487 return SetLocalMatrix(m);
491 //_____________________________________________________________________________
492 Bool_t AliAlignObj::SetLocalRotation(const TGeoMatrix& m)
494 // Set the global delta transformation by passing the matrix of
495 // the local delta transformation and taking its rotational part
496 // In case that the TGeo was not initialized or not closed,
497 // returns false and the object parameters are not set.
500 const Double_t* rot = m.GetRotationMatrix();
501 rotm.SetRotation(rot);
503 return SetLocalMatrix(rotm);
507 //_____________________________________________________________________________
508 Bool_t AliAlignObj::SetLocalMatrix(const TGeoMatrix& m)
510 // Set the global delta transformation by passing the TGeo matrix
511 // for the local delta transformation.
512 // In case that the TGeo was not initialized or not closed,
513 // returns false and the object parameters are not set.
515 if (!gGeoManager || !gGeoManager->IsClosed()) {
516 AliError("Can't set the alignment object parameters! gGeoManager doesn't exist or it is still opened!");
520 const char* symname = GetSymName();
521 TGeoPhysicalNode* node;
522 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
524 node = gGeoManager->MakeAlignablePN(pne);
526 AliWarning(Form("The symbolic volume name %s does not correspond to a physical entry. Using it as volume path!",symname));
527 node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(symname);
531 AliError(Form("Volume name or path %s not valid!",symname));
534 if (node->IsAligned())
535 AliWarning(Form("Volume %s has been already misaligned!",symname));
538 const Double_t *tr = m.GetTranslation();
539 m1.SetTranslation(tr);
540 const Double_t* rot = m.GetRotationMatrix();
543 TGeoHMatrix align,gprime,gprimeinv;
544 gprime = *node->GetMatrix();
545 gprimeinv = gprime.Inverse();
546 m1.Multiply(&gprimeinv);
547 m1.MultiplyLeft(&gprime);
549 return SetMatrix(m1);
552 //_____________________________________________________________________________
553 Bool_t AliAlignObj::SetMatrix(const TGeoMatrix& m)
555 // Set the global delta transformation by passing the TGeoMatrix
559 return SetRotation(m);
562 //_____________________________________________________________________________
563 Bool_t AliAlignObj::GetLocalPars(Double_t transl[], Double_t angles[]) const
565 // Get the translations and angles (in degrees) expressing the
566 // local delta transformation.
567 // In case that the TGeo was not initialized or not closed,
568 // returns false and the object parameters are not set.
570 if(!GetLocalTranslation(transl)) return kFALSE;
571 return GetLocalAngles(angles);
574 //_____________________________________________________________________________
575 Bool_t AliAlignObj::GetLocalTranslation(Double_t* tr) const
577 // Get the 3 shifts giving the translational part of the local
578 // delta transformation.
579 // In case that the TGeo was not initialized or not closed,
580 // returns false and the object parameters are not set.
583 if(!GetLocalMatrix(ml)) return kFALSE;
584 const Double_t* transl;
585 transl = ml.GetTranslation();
592 //_____________________________________________________________________________
593 Bool_t AliAlignObj::GetLocalAngles(Double_t* angles) const
595 // Get the 3 angles giving the rotational part of the local
596 // delta transformation.
597 // In case that the TGeo was not initialized or not closed,
598 // returns false and the object parameters are not set.
601 if(!GetLocalMatrix(ml)) return kFALSE;
602 const Double_t *rot = ml.GetRotationMatrix();
603 return MatrixToAngles(rot,angles);
606 //_____________________________________________________________________________
607 Bool_t AliAlignObj::GetLocalMatrix(TGeoHMatrix& m) const
609 // Get the matrix for the local delta transformation.
610 // In case that the TGeo was not initialized or not closed,
611 // returns false and the object parameters are not set.
613 if (!gGeoManager || !gGeoManager->IsClosed()) {
614 AliError("Can't set the alignment object parameters! gGeoManager doesn't exist or it is still opened!");
618 const char* symname = GetSymName();
619 TGeoPhysicalNode* node;
620 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
622 node = gGeoManager->MakeAlignablePN(pne);
624 AliWarning(Form("The symbolic volume name %s does not correspond to a physical entry. Using it as volume path!",symname));
625 node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(symname);
629 AliError(Form("Volume name or path %s not valid!",symname));
632 if (node->IsAligned())
633 AliWarning(Form("Volume %s has been already misaligned!",symname));
636 TGeoHMatrix gprime,gprimeinv;
637 gprime = *node->GetMatrix();
638 gprimeinv = gprime.Inverse();
640 m.MultiplyLeft(&gprimeinv);
645 //_____________________________________________________________________________
646 Bool_t AliAlignObj::ApplyToGeometry(Bool_t ovlpcheck)
648 // Apply the current alignment object to the TGeo geometry
649 // This method returns FALSE if the symname of the object was not
650 // valid neither to get a TGeoPEntry nor as a volume path
652 if (!gGeoManager || !gGeoManager->IsClosed()) {
653 AliError("Can't apply the alignment object! gGeoManager doesn't exist or it is still opened!");
657 const char* symname = GetSymName();
659 TGeoPhysicalNode* node;
660 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
662 path = pne->GetTitle();
663 node = gGeoManager->MakeAlignablePN(pne);
665 AliDebug(1,Form("The symbolic volume name %s does not correspond to a physical entry. Using it as a volume path!",symname));
667 if (!gGeoManager->CheckPath(path)) {
668 AliDebug(1,Form("Volume path %s not valid!",path));
671 if (gGeoManager->GetListOfPhysicalNodes()->FindObject(path)) {
672 AliError(Form("Volume %s has already been misaligned!",path));
675 node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(path);
679 AliError(Form("Volume path %s not valid!",path));
683 TGeoHMatrix align,gprime;
684 gprime = *node->GetMatrix();
686 gprime.MultiplyLeft(&align);
687 TGeoHMatrix *ginv = new TGeoHMatrix;
688 TGeoHMatrix *g = node->GetMatrix(node->GetLevel()-1);
689 *ginv = g->Inverse();
691 AliGeomManager::ELayerID layerId; // unique identity for layer in the alobj
692 Int_t modId; // unique identity for volume inside layer in the alobj
693 GetVolUID(layerId, modId);
694 AliDebug(2,Form("Aligning volume %s of detector layer %d with local ID %d",symname,layerId,modId));
695 node->Align(ginv,0,ovlpcheck);
697 Int_t novex=((TObjArray*)gGeoManager->GetListOfOverlaps())->GetEntriesFast();
699 TString error(Form("The alignment of volume %s introduced %d new overlap",GetSymName(),novex));
700 if(novex>1) error+="s";
701 AliError(error.Data());