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>
28 #include <TGeoOverlap.h>
30 #include <TMatrixDSym.h>
32 #include "AliAlignObj.h"
33 #include "AliTrackPointArray.h"
38 //_____________________________________________________________________________
39 AliAlignObj::AliAlignObj():
43 // default constructor
44 for(Int_t i=0; i<6; i++) fDiag[i]=-999.;
45 for(Int_t i=0; i<15; i++) fODia[i]=-999.;
48 //_____________________________________________________________________________
49 AliAlignObj::AliAlignObj(const char* symname, UShort_t voluid) :
54 // standard constructor
56 for(Int_t i=0; i<6; i++) fDiag[i]=-999.;
57 for(Int_t i=0; i<15; i++) fODia[i]=-999.;
60 //_____________________________________________________________________________
61 AliAlignObj::AliAlignObj(const char* symname, UShort_t voluid, Double_t* cmat) :
66 // standard constructor
71 //_____________________________________________________________________________
72 AliAlignObj::AliAlignObj(const AliAlignObj& theAlignObj) :
74 fVolPath(theAlignObj.GetSymName()),
75 fVolUID(theAlignObj.GetVolUID())
78 for(Int_t i=0; i<6; i++) fDiag[i]=theAlignObj.fDiag[i];
79 for(Int_t i=0; i<15; i++) fODia[i]=theAlignObj.fODia[i];
82 //_____________________________________________________________________________
83 AliAlignObj &AliAlignObj::operator =(const AliAlignObj& theAlignObj)
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<15; i++) fODia[i]=theAlignObj.fODia[i];
94 //_____________________________________________________________________________
95 AliAlignObj &AliAlignObj::operator*=(const AliAlignObj& theAlignObj)
97 // multiplication operator
98 // The operator can be used to 'combine'
99 // two alignment objects
103 theAlignObj.GetMatrix(m2);
104 m1.MultiplyLeft(&m2);
106 // temporary solution: the covariance matrix of the resulting combined object
107 // is set equal to the covariance matrix of the right operand
108 // (not to be used for combining alignment objects for different levels)
109 for(Int_t i=0; i<6; i++) fDiag[i] = theAlignObj.fDiag[i];
110 for(Int_t i=0; i<15; i++) fODia[i] = theAlignObj.fODia[i];
114 //_____________________________________________________________________________
115 AliAlignObj::~AliAlignObj()
120 //_____________________________________________________________________________
121 void AliAlignObj::SetVolUID(AliGeomManager::ELayerID detId, Int_t modId)
123 // From detector name and module number (according to detector numbering)
124 // build fVolUID, unique numerical identity of that volume inside ALICE
125 // fVolUID is 16 bits, first 5 reserved for detID (32 possible values),
126 // remaining 11 for module ID inside det (2048 possible values).
128 fVolUID = AliGeomManager::LayerToVolUID(detId,modId);
131 //_____________________________________________________________________________
132 void AliAlignObj::GetVolUID(AliGeomManager::ELayerID &layerId, Int_t &modId) const
134 // From the fVolUID, unique numerical identity of that volume inside ALICE,
135 // (voluid is 16 bits, first 5 reserved for layerID (32 possible values),
136 // remaining 11 for module ID inside det (2048 possible values)), sets
137 // the argument layerId to the identity of the layer to which that volume
138 // belongs and sets the argument modId to the identity of that volume
139 // internally to the layer.
141 layerId = AliGeomManager::VolUIDToLayer(fVolUID,modId);
144 //_____________________________________________________________________________
145 Bool_t AliAlignObj::GetPars(Double_t tr[], Double_t angles[]) const
148 return GetAngles(angles);
151 //_____________________________________________________________________________
152 Int_t AliAlignObj::GetLevel() const
154 // Return the geometry level of the alignable volume to which
155 // the alignment object is associated; this is the number of
156 // slashes in the corresponding volume path
159 AliWarning("gGeoManager doesn't exist or it is still open: unable to return meaningful level value.");
162 const char* symname = GetSymName();
164 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
166 path = pne->GetTitle();
171 TString pathStr = path;
172 if(pathStr[0]!='/') pathStr.Prepend('/');
173 return pathStr.CountChar('/');
176 //_____________________________________________________________________________
177 Int_t AliAlignObj::Compare(const TObject *obj) const
179 // Compare the levels of two
181 // Used in the sorting during
182 // the application of alignment
183 // objects to the geometry
185 Int_t level = GetLevel();
186 Int_t level2 = ((AliAlignObj *)obj)->GetLevel();
190 return ((level > level2) ? 1 : -1);
193 //______________________________________________________________________________
194 void AliAlignObj::GetCovMatrix(Double_t *cmat) const
196 // Fills the cmat argument with the coefficients of the external cov matrix (21 elements)
197 // calculating them from the correlation matrix data member
200 for(Int_t i=0; i<6; ++i) {
201 // Off diagonal elements
202 for(Int_t j=0; j<i; ++j) {
203 cmat[i*(i+1)/2+j] = (fDiag[j] >= 0. && fDiag[i] >= 0.) ? fODia[(i-1)*i/2+j]*fDiag[j]*fDiag[i]: -999.;
207 cmat[i*(i+1)/2+i] = (fDiag[i] >= 0.) ? fDiag[i]*fDiag[i] : -999.;
213 //______________________________________________________________________________
214 void AliAlignObj::GetCovMatrix(TMatrixDSym& mcov) const
216 // Fills the matrix m passed as argument as the covariance matrix calculated
217 // from the coefficients of the reduced covariance matrix data members
220 for(Int_t i=0; i<6; ++i) {
221 // Off diagonal elements
222 for(Int_t j=0; j<i; ++j) {
223 mcov(j,i) = mcov(i,j) = (fDiag[j] >= 0. && fDiag[i] >= 0.) ? fODia[(i-1)*i/2+j]*fDiag[j]*fDiag[i]: -999.;
227 mcov(i,i) = (fDiag[i] >= 0.) ? fDiag[i]*fDiag[i] : -999.;
232 //______________________________________________________________________________
233 void AliAlignObj::SetCorrMatrix(Double_t *cmat)
235 // Sets the correlation matrix data member from the coefficients of the external covariance
236 // matrix (21 elements passed as argument).
240 // Diagonal elements first
241 for(Int_t i=0; i<6; ++i) {
242 fDiag[i] = (cmat[i*(i+1)/2+i] >= 0.) ? TMath::Sqrt(cmat[i*(i+1)/2+i]) : -999.;
245 // ... then the ones off diagonal
246 for(Int_t i=0; i<6; ++i)
247 // Off diagonal elements
248 for(Int_t j=0; j<i; ++j) {
249 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).
250 if (fODia[(i-1)*i/2+j]>1.) fODia[(i-1)*i/2+j] = 1.; // check upper boundary
251 if (fODia[(i-1)*i/2+j]<-1.) fODia[(i-1)*i/2+j] = -1.; // check lower boundary
254 for(Int_t i=0; i< 6; ++i) fDiag[i]=-999.;
255 for(Int_t i=0; i< 6*(6-1)/2; ++i) fODia[i]=0.;
261 //______________________________________________________________________________
262 void AliAlignObj::SetCorrMatrix(TMatrixDSym& mcov)
264 // Sets the correlation matrix data member from the covariance matrix mcov passed
265 // passed as argument.
269 // Diagonal elements first
270 for(Int_t i=0; i<6; ++i) {
271 fDiag[i] = (mcov(i,i) >= 0.) ? TMath::Sqrt(mcov(i,i)) : -999.;
274 // ... then the ones off diagonal
275 for(Int_t i=0; i<6; ++i)
276 // Off diagonal elements
277 for(Int_t j=0; j<i; ++j) {
278 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).
279 if (fODia[(i-1)*i/2+j]>1.) fODia[(i-1)*i/2+j] = 1.; // check upper boundary
280 if (fODia[(i-1)*i/2+j]<-1.) fODia[(i-1)*i/2+j] = -1.; // check lower boundary
283 for(Int_t i=0; i< 6; ++i) fDiag[i]=-999.;
284 for(Int_t i=0; i< 6*(6-1)/2; ++i) fODia[i]=0.;
290 //_____________________________________________________________________________
291 void AliAlignObj::AnglesToMatrix(const Double_t *angles, Double_t *rot) const
293 // Calculates the rotation matrix using the
294 // Euler angles in "x y z" notation
296 Double_t degrad = TMath::DegToRad();
297 Double_t sinpsi = TMath::Sin(degrad*angles[0]);
298 Double_t cospsi = TMath::Cos(degrad*angles[0]);
299 Double_t sinthe = TMath::Sin(degrad*angles[1]);
300 Double_t costhe = TMath::Cos(degrad*angles[1]);
301 Double_t sinphi = TMath::Sin(degrad*angles[2]);
302 Double_t cosphi = TMath::Cos(degrad*angles[2]);
304 rot[0] = costhe*cosphi;
305 rot[1] = -costhe*sinphi;
307 rot[3] = sinpsi*sinthe*cosphi + cospsi*sinphi;
308 rot[4] = -sinpsi*sinthe*sinphi + cospsi*cosphi;
309 rot[5] = -costhe*sinpsi;
310 rot[6] = -cospsi*sinthe*cosphi + sinpsi*sinphi;
311 rot[7] = cospsi*sinthe*sinphi + sinpsi*cosphi;
312 rot[8] = costhe*cospsi;
315 //_____________________________________________________________________________
316 Bool_t AliAlignObj::MatrixToAngles(const Double_t *rot, Double_t *angles) const
318 // Calculates the Euler angles in "x y z" notation
319 // using the rotation matrix
320 // Returns false in case the rotation angles can not be
321 // extracted from the matrix
323 if(TMath::Abs(rot[0])<1e-7 || TMath::Abs(rot[8])<1e-7) {
324 AliError("Failed to extract roll-pitch-yall angles!");
327 Double_t raddeg = TMath::RadToDeg();
328 angles[0]=raddeg*TMath::ATan2(-rot[5],rot[8]);
329 angles[1]=raddeg*TMath::ASin(rot[2]);
330 angles[2]=raddeg*TMath::ATan2(-rot[1],rot[0]);
334 //______________________________________________________________________________
335 void AliAlignObj::Transform(AliTrackPoint &p, Bool_t copycov) const
337 // The method transforms the space-point coordinates using the
338 // transformation matrix provided by the AliAlignObj
339 // In case the copycov flag is set to kTRUE, the covariance matrix
340 // of the alignment object is copied into the space-point
342 if (fVolUID != p.GetVolumeID())
343 AliWarning(Form("Alignment object ID is not equal to the space-point ID (%d != %d)",fVolUID,p.GetVolumeID()));
347 Double_t *rot = m.GetRotationMatrix();
348 Double_t *tr = m.GetTranslation();
350 Float_t xyzin[3],xyzout[3];
352 for (Int_t i = 0; i < 3; i++)
360 TMatrixDSym covmat(6);
361 GetCovMatrix(covmat);
362 p.SetAlignCovMatrix(covmat);
367 //_____________________________________________________________________________
368 void AliAlignObj::Transform(AliTrackPointArray &array) const
370 // This method is used to transform all the track points
371 // from the input AliTrackPointArray
374 for (Int_t i = 0; i < array.GetNPoints(); i++) {
377 array.AddPoint(i,&p);
381 //_____________________________________________________________________________
382 void AliAlignObj::Print(Option_t *) const
384 // Print the contents of the
385 // alignment object in angles and
386 // matrix representations
394 const Double_t *rot = m.GetRotationMatrix();
396 printf("Volume=%s\n",GetSymName());
397 if (GetVolUID() != 0) {
398 AliGeomManager::ELayerID layerId;
400 GetVolUID(layerId,modId);
401 printf("VolumeID=%d LayerID=%d ( %s ) ModuleID=%d\n", GetVolUID(),layerId,AliGeomManager::LayerName(layerId),modId);
403 printf("%12.8f%12.8f%12.8f Tx = %12.8f Psi = %12.8f\n", rot[0], rot[1], rot[2], tr[0], angles[0]);
404 printf("%12.8f%12.8f%12.8f Ty = %12.8f Theta = %12.8f\n", rot[3], rot[4], rot[5], tr[1], angles[1]);
405 printf("%12.8f%12.8f%12.8f Tz = %12.8f Phi = %12.8f\n", rot[6], rot[7], rot[8], tr[2], angles[2]);
409 //_____________________________________________________________________________
410 void AliAlignObj::SetPars(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 3 angles (expressed in
414 // degrees) and 3 shifts (in centimeters)
416 SetTranslation(x,y,z);
417 SetRotation(psi,theta,phi);
420 //_____________________________________________________________________________
421 Bool_t AliAlignObj::SetLocalPars(Double_t x, Double_t y, Double_t z,
422 Double_t psi, Double_t theta, Double_t phi)
424 // Set the global delta transformation by passing the parameters
425 // for the local delta transformation (3 shifts and 3 angles).
426 // In case that the TGeo was not initialized or not closed,
427 // returns false and the object parameters are not set.
430 Double_t tr[3] = {x, y, z};
431 m.SetTranslation(tr);
432 Double_t angles[3] = {psi, theta, phi};
434 AnglesToMatrix(angles,rot);
437 return SetLocalMatrix(m);
441 //_____________________________________________________________________________
442 Bool_t AliAlignObj::SetLocalTranslation(Double_t x, Double_t y, Double_t z)
444 // Set the global delta transformation by passing the three shifts giving
445 // the translation in the local reference system of the alignable
446 // volume (known by TGeo geometry).
447 // In case that the TGeo was not initialized or not closed,
448 // returns false and the object parameters are not set.
451 Double_t tr[3] = {x, y, z};
452 m.SetTranslation(tr);
454 return SetLocalMatrix(m);
458 //_____________________________________________________________________________
459 Bool_t AliAlignObj::SetLocalTranslation(const TGeoMatrix& m)
461 // Set the global delta transformation by passing the matrix of
462 // the local delta transformation and taking its translational part
463 // In case that the TGeo was not initialized or not closed,
464 // returns false and the object parameters are not set.
466 const Double_t* tr = m.GetTranslation();
468 mtr.SetTranslation(tr);
470 return SetLocalMatrix(mtr);
474 //_____________________________________________________________________________
475 Bool_t AliAlignObj::SetLocalRotation(Double_t psi, Double_t theta, Double_t phi)
477 // Set the global delta transformation by passing the three angles giving
478 // the rotation in the local reference system of the alignable
479 // volume (known by TGeo geometry).
480 // In case that the TGeo was not initialized or not closed,
481 // returns false and the object parameters are not set.
484 Double_t angles[3] = {psi, theta, phi};
486 AnglesToMatrix(angles,rot);
489 return SetLocalMatrix(m);
493 //_____________________________________________________________________________
494 Bool_t AliAlignObj::SetLocalRotation(const TGeoMatrix& m)
496 // Set the global delta transformation by passing the matrix of
497 // the local delta transformation and taking its rotational part
498 // In case that the TGeo was not initialized or not closed,
499 // returns false and the object parameters are not set.
502 const Double_t* rot = m.GetRotationMatrix();
503 rotm.SetRotation(rot);
505 return SetLocalMatrix(rotm);
509 //_____________________________________________________________________________
510 Bool_t AliAlignObj::SetLocalMatrix(const TGeoMatrix& m)
512 // Set the global delta transformation by passing the TGeo matrix
513 // for the local delta transformation.
514 // In case that the TGeo was not initialized or not closed,
515 // returns false and the object parameters are not set.
517 if (!gGeoManager || !gGeoManager->IsClosed()) {
518 AliError("Can't set the local alignment object parameters! gGeoManager doesn't exist or it is still open!");
522 const char* symname = GetSymName();
523 TGeoPhysicalNode* node;
524 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
526 if(!pne->GetPhysicalNode()){
527 node = gGeoManager->MakeAlignablePN(pne);
529 node = pne->GetPhysicalNode();
532 AliWarning(Form("The symbolic volume name %s does not correspond to a physical entry. Using it as volume path!",symname));
533 node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(symname);
537 AliError(Form("Volume name or path %s not valid!",symname));
540 if (node->IsAligned())
541 AliWarning(Form("Volume %s has been already misaligned!",symname));
544 const Double_t *tr = m.GetTranslation();
545 m1.SetTranslation(tr);
546 const Double_t* rot = m.GetRotationMatrix();
549 TGeoHMatrix align,gprime,gprimeinv;
550 gprime = *node->GetMatrix();
551 gprimeinv = gprime.Inverse();
552 m1.Multiply(&gprimeinv);
553 m1.MultiplyLeft(&gprime);
555 return SetMatrix(m1);
558 //_____________________________________________________________________________
559 Bool_t AliAlignObj::SetMatrix(const TGeoMatrix& m)
561 // Set the global delta transformation by passing the TGeoMatrix
565 return SetRotation(m);
568 //_____________________________________________________________________________
569 Bool_t AliAlignObj::GetLocalPars(Double_t transl[], Double_t angles[]) const
571 // Get the translations and angles (in degrees) expressing the
572 // local delta transformation.
573 // In case that the TGeo was not initialized or not closed,
574 // returns false and the object parameters are not set.
576 if(!GetLocalTranslation(transl)) return kFALSE;
577 return GetLocalAngles(angles);
580 //_____________________________________________________________________________
581 Bool_t AliAlignObj::GetLocalTranslation(Double_t* tr) const
583 // Get the 3 shifts giving the translational part of the local
584 // delta transformation.
585 // In case that the TGeo was not initialized or not closed,
586 // returns false and the object parameters are not set.
589 if(!GetLocalMatrix(ml)) return kFALSE;
590 const Double_t* transl;
591 transl = ml.GetTranslation();
598 //_____________________________________________________________________________
599 Bool_t AliAlignObj::GetLocalAngles(Double_t* angles) const
601 // Get the 3 angles giving the rotational part of the local
602 // delta transformation.
603 // In case that the TGeo was not initialized or not closed,
604 // returns false and the object parameters are not set.
607 if(!GetLocalMatrix(ml)) return kFALSE;
608 const Double_t *rot = ml.GetRotationMatrix();
609 return MatrixToAngles(rot,angles);
612 //_____________________________________________________________________________
613 Bool_t AliAlignObj::GetLocalMatrix(TGeoHMatrix& m) const
615 // Get the matrix for the local delta transformation.
616 // In case that the TGeo was not initialized or not closed,
617 // returns false and the object parameters are not set.
619 if (!gGeoManager || !gGeoManager->IsClosed()) {
620 AliError("Can't get the local alignment object parameters! gGeoManager doesn't exist or it is still open!");
624 const char* symname = GetSymName();
625 TGeoPhysicalNode* node;
626 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
628 if(!pne->GetPhysicalNode()){
629 node = gGeoManager->MakeAlignablePN(pne);
631 node = pne->GetPhysicalNode();
634 AliWarning(Form("The symbolic volume name %s does not correspond to a physical entry. Using it as volume path!",symname));
635 node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(symname);
639 AliError(Form("Volume name or path %s not valid!",symname));
642 if (node->IsAligned())
643 AliWarning(Form("Volume %s has been already misaligned!",symname));
646 TGeoHMatrix gprime,gprimeinv;
647 gprime = *node->GetMatrix();
648 gprimeinv = gprime.Inverse();
650 m.MultiplyLeft(&gprimeinv);
655 //_____________________________________________________________________________
656 Bool_t AliAlignObj::ApplyToGeometry(Bool_t ovlpcheck)
658 // Apply the current alignment object to the TGeo geometry
659 // This method returns FALSE if the symname of the object was not
660 // valid neither to get a TGeoPEntry nor as a volume path
662 if (!gGeoManager || !gGeoManager->IsClosed()) {
663 AliError("Can't apply the alignment object! gGeoManager doesn't exist or it is still open!");
667 if (gGeoManager->IsLocked()){
668 AliError("Can't apply the alignment object! Geometry is locked!");
672 const char* symname = GetSymName();
674 TGeoPhysicalNode* node;
675 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
677 path = pne->GetTitle();
678 node = gGeoManager->MakeAlignablePN(pne);
680 AliDebug(1,Form("The symbolic volume name %s does not correspond to a physical entry. Using it as a volume path!",symname));
682 if (!gGeoManager->CheckPath(path)) {
683 AliDebug(1,Form("Volume path %s not valid!",path));
686 if (gGeoManager->GetListOfPhysicalNodes()->FindObject(path)) {
687 AliError(Form("Volume %s has already been misaligned!",path));
690 node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(path);
694 AliError(Form("Volume path %s not valid!",path));
698 Double_t threshold = 0.001;
700 TGeoHMatrix align,gprime;
701 gprime = *node->GetMatrix();
703 gprime.MultiplyLeft(&align);
704 TGeoHMatrix *ginv = new TGeoHMatrix;
705 TGeoHMatrix *g = node->GetMatrix(node->GetLevel()-1);
706 *ginv = g->Inverse();
708 AliGeomManager::ELayerID layerId; // unique identity for layer in the alobj
709 Int_t modId; // unique identity for volume inside layer in the alobj
710 GetVolUID(layerId, modId);
711 AliDebug(2,Form("Aligning volume %s of detector layer %d with local ID %d",symname,layerId,modId));
713 node->Align(ginv,0,kTRUE); //(trunk of root takes threshold as additional argument)
715 node->Align(ginv,0,kFALSE);
719 TObjArray* ovlpArray = gGeoManager->GetListOfOverlaps();
720 Int_t nOvlp = ovlpArray->GetEntriesFast();
723 AliInfo(Form("Misalignment of node %s generated the following overlaps/extrusions:",node->GetName()));
724 for(Int_t i=0; i<nOvlp; i++)
725 ((TGeoOverlap*)ovlpArray->UncheckedAt(i))->PrintInfo();