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 <TGeoMatrix.h>
27 #include <TGeoPhysicalNode.h>
29 #include <TMatrixDSym.h>
31 #include "AliAlignObj.h"
32 #include "AliTrackPointArray.h"
37 //_____________________________________________________________________________
38 AliAlignObj::AliAlignObj():
42 // default constructor
43 for(Int_t i=0; i<6; i++) fDiag[i]=-999.;
44 for(Int_t i=0; i<15; i++) fODia[i]=-999.;
47 //_____________________________________________________________________________
48 AliAlignObj::AliAlignObj(const char* symname, UShort_t voluid) :
53 // standard constructor
55 for(Int_t i=0; i<6; i++) fDiag[i]=-999.;
56 for(Int_t i=0; i<15; i++) fODia[i]=-999.;
59 //_____________________________________________________________________________
60 AliAlignObj::AliAlignObj(const char* symname, UShort_t voluid, Double_t* cmat) :
65 // standard constructor
70 //_____________________________________________________________________________
71 AliAlignObj::AliAlignObj(const AliAlignObj& theAlignObj) :
73 fVolPath(theAlignObj.GetSymName()),
74 fVolUID(theAlignObj.GetVolUID())
77 for(Int_t i=0; i<6; i++) fDiag[i]=theAlignObj.fDiag[i];
78 for(Int_t i=0; i<15; i++) fODia[i]=theAlignObj.fODia[i];
81 //_____________________________________________________________________________
82 AliAlignObj &AliAlignObj::operator =(const AliAlignObj& theAlignObj)
84 // assignment operator
85 if(this==&theAlignObj) return *this;
86 fVolPath = theAlignObj.GetSymName();
87 fVolUID = theAlignObj.GetVolUID();
88 for(Int_t i=0; i<6; i++) fDiag[i]=theAlignObj.fDiag[i];
89 for(Int_t i=0; i<15; i++) fODia[i]=theAlignObj.fODia[i];
93 //_____________________________________________________________________________
94 AliAlignObj &AliAlignObj::operator*=(const AliAlignObj& theAlignObj)
96 // multiplication operator
97 // The operator can be used to 'combine'
98 // two alignment objects
102 theAlignObj.GetMatrix(m2);
103 m1.MultiplyLeft(&m2);
105 // temporary solution: the covariance matrix of the resulting combined object
106 // is set equal to the covariance matrix of the right operand
107 // (not to be used for combining alignment objects for different levels)
108 for(Int_t i=0; i<6; i++) fDiag[i] = theAlignObj.fDiag[i];
109 for(Int_t i=0; i<15; i++) fODia[i] = theAlignObj.fODia[i];
113 //_____________________________________________________________________________
114 AliAlignObj::~AliAlignObj()
119 //_____________________________________________________________________________
120 void AliAlignObj::SetVolUID(AliGeomManager::ELayerID detId, Int_t modId)
122 // From detector name and module number (according to detector numbering)
123 // build fVolUID, unique numerical identity of that volume inside ALICE
124 // fVolUID is 16 bits, first 5 reserved for detID (32 possible values),
125 // remaining 11 for module ID inside det (2048 possible values).
127 fVolUID = AliGeomManager::LayerToVolUID(detId,modId);
130 //_____________________________________________________________________________
131 void AliAlignObj::GetVolUID(AliGeomManager::ELayerID &layerId, Int_t &modId) const
133 // From the fVolUID, unique numerical identity of that volume inside ALICE,
134 // (voluid is 16 bits, first 5 reserved for layerID (32 possible values),
135 // remaining 11 for module ID inside det (2048 possible values)), sets
136 // the argument layerId to the identity of the layer to which that volume
137 // belongs and sets the argument modId to the identity of that volume
138 // internally to the layer.
140 layerId = AliGeomManager::VolUIDToLayer(fVolUID,modId);
143 //_____________________________________________________________________________
144 Bool_t AliAlignObj::GetPars(Double_t tr[], Double_t angles[]) const
147 return GetAngles(angles);
150 //_____________________________________________________________________________
151 Int_t AliAlignObj::GetLevel() const
153 // Return the geometry level of the alignable volume to which
154 // the alignment object is associated; this is the number of
155 // slashes in the corresponding volume path
158 AliWarning("gGeoManager doesn't exist or it is still opened: unable to return meaningful level value.");
161 const char* symname = GetSymName();
163 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
165 path = pne->GetTitle();
170 TString pathStr = path;
171 if(pathStr[0]!='/') pathStr.Prepend('/');
172 return pathStr.CountChar('/');
175 //_____________________________________________________________________________
176 Int_t AliAlignObj::Compare(const TObject *obj) const
178 // Compare the levels of two
180 // Used in the sorting during
181 // the application of alignment
182 // objects to the geometry
184 Int_t level = GetLevel();
185 Int_t level2 = ((AliAlignObj *)obj)->GetLevel();
189 return ((level > level2) ? 1 : -1);
192 //______________________________________________________________________________
193 void AliAlignObj::GetCovMatrix(Double_t *cmat) const
195 // Fills the cmat argument with the coefficients of the external cov matrix (21 elements)
196 // calculating them from the correlation matrix data member
199 for(Int_t i=0; i<6; ++i) {
200 // Off diagonal elements
201 for(Int_t j=0; j<i; ++j) {
202 cmat[i*(i+1)/2+j] = (fDiag[j] >= 0. && fDiag[i] >= 0.) ? fODia[(i-1)*i/2+j]*fDiag[j]*fDiag[i]: -999.;
206 cmat[i*(i+1)/2+i] = (fDiag[i] >= 0.) ? fDiag[i]*fDiag[i] : -999.;
212 //______________________________________________________________________________
213 void AliAlignObj::GetCovMatrix(TMatrixDSym& mcov) const
215 // Fills the matrix m passed as argument as the covariance matrix calculated
216 // from the coefficients of the reduced covariance matrix data members
219 for(Int_t i=0; i<6; ++i) {
220 // Off diagonal elements
221 for(Int_t j=0; j<i; ++j) {
222 mcov(j,i) = mcov(i,j) = (fDiag[j] >= 0. && fDiag[i] >= 0.) ? fODia[(i-1)*i/2+j]*fDiag[j]*fDiag[i]: -999.;
226 mcov(i,i) = (fDiag[i] >= 0.) ? fDiag[i]*fDiag[i] : -999.;
231 //______________________________________________________________________________
232 void AliAlignObj::SetCorrMatrix(Double_t *cmat)
234 // Sets the correlation matrix data member from the coefficients of the external covariance
235 // matrix (21 elements passed as argument).
239 // Diagonal elements first
240 for(Int_t i=0; i<6; ++i) {
241 fDiag[i] = (cmat[i*(i+1)/2+i] >= 0.) ? TMath::Sqrt(cmat[i*(i+1)/2+i]) : -999.;
244 // ... then the ones off diagonal
245 for(Int_t i=0; i<6; ++i)
246 // Off diagonal elements
247 for(Int_t j=0; j<i; ++j) {
248 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).
249 if (fODia[(i-1)*i/2+j]>1.) fODia[(i-1)*i/2+j] = 1.; // check upper boundary
250 if (fODia[(i-1)*i/2+j]<-1.) fODia[(i-1)*i/2+j] = -1.; // check lower boundary
253 for(Int_t i=0; i< 6; ++i) fDiag[i]=-999.;
254 for(Int_t i=0; i< 6*(6-1)/2; ++i) fODia[i]=0.;
260 //______________________________________________________________________________
261 void AliAlignObj::SetCorrMatrix(TMatrixDSym& mcov)
263 // Sets the correlation matrix data member from the covariance matrix mcov passed
264 // passed as argument.
268 // Diagonal elements first
269 for(Int_t i=0; i<6; ++i) {
270 fDiag[i] = (mcov(i,i) >= 0.) ? TMath::Sqrt(mcov(i,i)) : -999.;
273 // ... then the ones off diagonal
274 for(Int_t i=0; i<6; ++i)
275 // Off diagonal elements
276 for(Int_t j=0; j<i; ++j) {
277 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).
278 if (fODia[(i-1)*i/2+j]>1.) fODia[(i-1)*i/2+j] = 1.; // check upper boundary
279 if (fODia[(i-1)*i/2+j]<-1.) fODia[(i-1)*i/2+j] = -1.; // check lower boundary
282 for(Int_t i=0; i< 6; ++i) fDiag[i]=-999.;
283 for(Int_t i=0; i< 6*(6-1)/2; ++i) fODia[i]=0.;
289 //_____________________________________________________________________________
290 void AliAlignObj::AnglesToMatrix(const Double_t *angles, Double_t *rot) const
292 // Calculates the rotation matrix using the
293 // Euler angles in "x y z" notation
295 Double_t degrad = TMath::DegToRad();
296 Double_t sinpsi = TMath::Sin(degrad*angles[0]);
297 Double_t cospsi = TMath::Cos(degrad*angles[0]);
298 Double_t sinthe = TMath::Sin(degrad*angles[1]);
299 Double_t costhe = TMath::Cos(degrad*angles[1]);
300 Double_t sinphi = TMath::Sin(degrad*angles[2]);
301 Double_t cosphi = TMath::Cos(degrad*angles[2]);
303 rot[0] = costhe*cosphi;
304 rot[1] = -costhe*sinphi;
306 rot[3] = sinpsi*sinthe*cosphi + cospsi*sinphi;
307 rot[4] = -sinpsi*sinthe*sinphi + cospsi*cosphi;
308 rot[5] = -costhe*sinpsi;
309 rot[6] = -cospsi*sinthe*cosphi + sinpsi*sinphi;
310 rot[7] = cospsi*sinthe*sinphi + sinpsi*cosphi;
311 rot[8] = costhe*cospsi;
314 //_____________________________________________________________________________
315 Bool_t AliAlignObj::MatrixToAngles(const Double_t *rot, Double_t *angles) const
317 // Calculates the Euler angles in "x y z" notation
318 // using the rotation matrix
319 // Returns false in case the rotation angles can not be
320 // extracted from the matrix
322 if(TMath::Abs(rot[0])<1e-7 || TMath::Abs(rot[8])<1e-7) {
323 AliError("Failed to extract roll-pitch-yall angles!");
326 Double_t raddeg = TMath::RadToDeg();
327 angles[0]=raddeg*TMath::ATan2(-rot[5],rot[8]);
328 angles[1]=raddeg*TMath::ASin(rot[2]);
329 angles[2]=raddeg*TMath::ATan2(-rot[1],rot[0]);
333 //______________________________________________________________________________
334 void AliAlignObj::Transform(AliTrackPoint &p, Bool_t copycov) const
336 // The method transforms the space-point coordinates using the
337 // transformation matrix provided by the AliAlignObj
338 // In case the copycov flag is set to kTRUE, the covariance matrix
339 // of the alignment object is copied into the space-point
341 if (fVolUID != p.GetVolumeID())
342 AliWarning(Form("Alignment object ID is not equal to the space-point ID (%d != %d)",fVolUID,p.GetVolumeID()));
346 Double_t *rot = m.GetRotationMatrix();
347 Double_t *tr = m.GetTranslation();
349 Float_t xyzin[3],xyzout[3];
351 for (Int_t i = 0; i < 3; i++)
359 TMatrixDSym covmat(6);
360 GetCovMatrix(covmat);
361 p.SetAlignCovMatrix(covmat);
366 //_____________________________________________________________________________
367 void AliAlignObj::Transform(AliTrackPointArray &array) const
369 // This method is used to transform all the track points
370 // from the input AliTrackPointArray
373 for (Int_t i = 0; i < array.GetNPoints(); i++) {
376 array.AddPoint(i,&p);
380 //_____________________________________________________________________________
381 void AliAlignObj::Print(Option_t *) const
383 // Print the contents of the
384 // alignment object in angles and
385 // matrix representations
393 const Double_t *rot = m.GetRotationMatrix();
395 printf("Volume=%s\n",GetSymName());
396 if (GetVolUID() != 0) {
397 AliGeomManager::ELayerID layerId;
399 GetVolUID(layerId,modId);
400 printf("VolumeID=%d LayerID=%d ( %s ) ModuleID=%d\n", GetVolUID(),layerId,AliGeomManager::LayerName(layerId),modId);
402 printf("%12.8f%12.8f%12.8f Tx = %12.8f Psi = %12.8f\n", rot[0], rot[1], rot[2], tr[0], angles[0]);
403 printf("%12.8f%12.8f%12.8f Ty = %12.8f Theta = %12.8f\n", rot[3], rot[4], rot[5], tr[1], angles[1]);
404 printf("%12.8f%12.8f%12.8f Tz = %12.8f Phi = %12.8f\n", rot[6], rot[7], rot[8], tr[2], angles[2]);
408 //_____________________________________________________________________________
409 void AliAlignObj::SetPars(Double_t x, Double_t y, Double_t z,
410 Double_t psi, Double_t theta, Double_t phi)
412 // Set the global delta transformation by passing 3 angles (expressed in
413 // degrees) and 3 shifts (in centimeters)
415 SetTranslation(x,y,z);
416 SetRotation(psi,theta,phi);
419 //_____________________________________________________________________________
420 Bool_t AliAlignObj::SetLocalPars(Double_t x, Double_t y, Double_t z,
421 Double_t psi, Double_t theta, Double_t phi)
423 // Set the global delta transformation by passing the parameters
424 // for the local delta transformation (3 shifts and 3 angles).
425 // In case that the TGeo was not initialized or not closed,
426 // returns false and the object parameters are not set.
429 Double_t tr[3] = {x, y, z};
430 m.SetTranslation(tr);
431 Double_t angles[3] = {psi, theta, phi};
433 AnglesToMatrix(angles,rot);
436 return SetLocalMatrix(m);
440 //_____________________________________________________________________________
441 Bool_t AliAlignObj::SetLocalTranslation(Double_t x, Double_t y, Double_t z)
443 // Set the global delta transformation by passing the three shifts giving
444 // the translation in the local reference system of the alignable
445 // volume (known by TGeo geometry).
446 // In case that the TGeo was not initialized or not closed,
447 // returns false and the object parameters are not set.
450 Double_t tr[3] = {x, y, z};
451 m.SetTranslation(tr);
453 return SetLocalMatrix(m);
457 //_____________________________________________________________________________
458 Bool_t AliAlignObj::SetLocalTranslation(const TGeoMatrix& m)
460 // Set the global delta transformation by passing the matrix of
461 // the local delta transformation and taking its translational part
462 // In case that the TGeo was not initialized or not closed,
463 // returns false and the object parameters are not set.
465 const Double_t* tr = m.GetTranslation();
467 mtr.SetTranslation(tr);
469 return SetLocalMatrix(mtr);
473 //_____________________________________________________________________________
474 Bool_t AliAlignObj::SetLocalRotation(Double_t psi, Double_t theta, Double_t phi)
476 // Set the global delta transformation by passing the three angles giving
477 // the rotation in the local reference system of the alignable
478 // volume (known by TGeo geometry).
479 // In case that the TGeo was not initialized or not closed,
480 // returns false and the object parameters are not set.
483 Double_t angles[3] = {psi, theta, phi};
485 AnglesToMatrix(angles,rot);
488 return SetLocalMatrix(m);
492 //_____________________________________________________________________________
493 Bool_t AliAlignObj::SetLocalRotation(const TGeoMatrix& m)
495 // Set the global delta transformation by passing the matrix of
496 // the local delta transformation and taking its rotational part
497 // In case that the TGeo was not initialized or not closed,
498 // returns false and the object parameters are not set.
501 const Double_t* rot = m.GetRotationMatrix();
502 rotm.SetRotation(rot);
504 return SetLocalMatrix(rotm);
508 //_____________________________________________________________________________
509 Bool_t AliAlignObj::SetLocalMatrix(const TGeoMatrix& m)
511 // Set the global delta transformation by passing the TGeo matrix
512 // for the local delta transformation.
513 // In case that the TGeo was not initialized or not closed,
514 // returns false and the object parameters are not set.
516 if (!gGeoManager || !gGeoManager->IsClosed()) {
517 AliError("Can't set the alignment object parameters! gGeoManager doesn't exist or it is still opened!");
521 const char* symname = GetSymName();
522 TGeoPhysicalNode* node;
523 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
525 node = gGeoManager->MakeAlignablePN(pne);
527 AliWarning(Form("The symbolic volume name %s does not correspond to a physical entry. Using it as volume path!",symname));
528 node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(symname);
532 AliError(Form("Volume name or path %s not valid!",symname));
535 if (node->IsAligned())
536 AliWarning(Form("Volume %s has been already misaligned!",symname));
539 const Double_t *tr = m.GetTranslation();
540 m1.SetTranslation(tr);
541 const Double_t* rot = m.GetRotationMatrix();
544 TGeoHMatrix align,gprime,gprimeinv;
545 gprime = *node->GetMatrix();
546 gprimeinv = gprime.Inverse();
547 m1.Multiply(&gprimeinv);
548 m1.MultiplyLeft(&gprime);
550 return SetMatrix(m1);
553 //_____________________________________________________________________________
554 Bool_t AliAlignObj::SetMatrix(const TGeoMatrix& m)
556 // Set the global delta transformation by passing the TGeoMatrix
560 return SetRotation(m);
563 //_____________________________________________________________________________
564 Bool_t AliAlignObj::GetLocalPars(Double_t transl[], Double_t angles[]) const
566 // Get the translations and angles (in degrees) expressing the
567 // local delta transformation.
568 // In case that the TGeo was not initialized or not closed,
569 // returns false and the object parameters are not set.
571 if(!GetLocalTranslation(transl)) return kFALSE;
572 return GetLocalAngles(angles);
575 //_____________________________________________________________________________
576 Bool_t AliAlignObj::GetLocalTranslation(Double_t* tr) const
578 // Get the 3 shifts giving the translational part of the local
579 // delta transformation.
580 // In case that the TGeo was not initialized or not closed,
581 // returns false and the object parameters are not set.
584 if(!GetLocalMatrix(ml)) return kFALSE;
585 const Double_t* transl;
586 transl = ml.GetTranslation();
593 //_____________________________________________________________________________
594 Bool_t AliAlignObj::GetLocalAngles(Double_t* angles) const
596 // Get the 3 angles giving the rotational part of the local
597 // delta transformation.
598 // In case that the TGeo was not initialized or not closed,
599 // returns false and the object parameters are not set.
602 if(!GetLocalMatrix(ml)) return kFALSE;
603 const Double_t *rot = ml.GetRotationMatrix();
604 return MatrixToAngles(rot,angles);
607 //_____________________________________________________________________________
608 Bool_t AliAlignObj::GetLocalMatrix(TGeoHMatrix& m) const
610 // Get the matrix for the local delta transformation.
611 // In case that the TGeo was not initialized or not closed,
612 // returns false and the object parameters are not set.
614 if (!gGeoManager || !gGeoManager->IsClosed()) {
615 AliError("Can't set the alignment object parameters! gGeoManager doesn't exist or it is still opened!");
619 const char* symname = GetSymName();
620 TGeoPhysicalNode* node;
621 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
623 node = gGeoManager->MakeAlignablePN(pne);
625 AliWarning(Form("The symbolic volume name %s does not correspond to a physical entry. Using it as volume path!",symname));
626 node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(symname);
630 AliError(Form("Volume name or path %s not valid!",symname));
633 if (node->IsAligned())
634 AliWarning(Form("Volume %s has been already misaligned!",symname));
637 TGeoHMatrix gprime,gprimeinv;
638 gprime = *node->GetMatrix();
639 gprimeinv = gprime.Inverse();
641 m.MultiplyLeft(&gprimeinv);
646 //_____________________________________________________________________________
647 Bool_t AliAlignObj::ApplyToGeometry(Bool_t ovlpcheck)
649 // Apply the current alignment object to the TGeo geometry
650 // This method returns FALSE if the symname of the object was not
651 // valid neither to get a TGeoPEntry nor as a volume path
653 if (!gGeoManager || !gGeoManager->IsClosed()) {
654 AliError("Can't apply the alignment object! gGeoManager doesn't exist or it is still opened!");
658 const char* symname = GetSymName();
660 TGeoPhysicalNode* node;
661 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
663 path = pne->GetTitle();
664 node = gGeoManager->MakeAlignablePN(pne);
666 AliDebug(1,Form("The symbolic volume name %s does not correspond to a physical entry. Using it as a volume path!",symname));
668 if (!gGeoManager->CheckPath(path)) {
669 AliDebug(1,Form("Volume path %s not valid!",path));
672 if (gGeoManager->GetListOfPhysicalNodes()->FindObject(path)) {
673 AliError(Form("Volume %s has already been misaligned!",path));
676 node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(path);
680 AliError(Form("Volume path %s not valid!",path));
684 TGeoHMatrix align,gprime;
685 gprime = *node->GetMatrix();
687 gprime.MultiplyLeft(&align);
688 TGeoHMatrix *ginv = new TGeoHMatrix;
689 TGeoHMatrix *g = node->GetMatrix(node->GetLevel()-1);
690 *ginv = g->Inverse();
692 AliGeomManager::ELayerID layerId; // unique identity for layer in the alobj
693 Int_t modId; // unique identity for volume inside layer in the alobj
694 GetVolUID(layerId, modId);
695 AliDebug(2,Form("Aligning volume %s of detector layer %d with local ID %d",symname,layerId,modId));
696 node->Align(ginv,0,ovlpcheck);
698 Int_t novex=((TObjArray*)gGeoManager->GetListOfOverlaps())->GetEntriesFast();
700 TString error(Form("The alignment of volume %s introduced %d new overlap",GetSymName(),novex));
701 if(novex>1) error+="s";
702 AliError(error.Data());