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 //-----------------------------------------------------------------
26 #include <TGeoManager.h>
27 #include <TGeoPhysicalNode.h>
29 #include "TObjString.h"
31 #include "AliAlignObj.h"
32 #include "AliTrackPointArray.h"
34 #include "AliAlignObjParams.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<21; 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<21; 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<21; 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<21; 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: 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]));
111 //_____________________________________________________________________________
112 AliAlignObj::~AliAlignObj()
117 //_____________________________________________________________________________
118 void AliAlignObj::SetVolUID(AliGeomManager::ELayerID detId, Int_t modId)
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).
125 fVolUID = AliGeomManager::LayerToVolUID(detId,modId);
128 //_____________________________________________________________________________
129 void AliAlignObj::GetVolUID(AliGeomManager::ELayerID &layerId, Int_t &modId) const
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.
138 layerId = AliGeomManager::VolUIDToLayer(fVolUID,modId);
141 //_____________________________________________________________________________
142 Bool_t AliAlignObj::GetPars(Double_t tr[], Double_t angles[]) const
145 return GetAngles(angles);
148 //_____________________________________________________________________________
149 Int_t AliAlignObj::GetLevel() const
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
156 AliWarning("gGeoManager doesn't exist or it is still opened: unable to return meaningful level value.");
159 const char* symname = GetSymName();
161 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
163 path = pne->GetTitle();
168 TString path_str = path;
169 if(path_str[0]!='/') path_str.Prepend('/');
170 return path_str.CountChar('/');
173 //_____________________________________________________________________________
174 Int_t AliAlignObj::Compare(const TObject *obj) const
176 // Compare the levels of two
178 // Used in the sorting during
179 // the application of alignment
180 // objects to the geometry
182 Int_t level = GetLevel();
183 Int_t level2 = ((AliAlignObj *)obj)->GetLevel();
187 return ((level > level2) ? 1 : -1);
190 //______________________________________________________________________________
191 void AliAlignObj::GetCovMatrix(Double_t *cmat) const
193 // Fills the cmat argument with the coefficients of the external cov matrix (21 elements)
194 // calculating them from the correlation matrix data member
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.;
204 cmat[i*(i+1)/2+i] = (fDiag[i] >= 0.) ? fDiag[i]*fDiag[i] : -999.;
210 //______________________________________________________________________________
211 void AliAlignObj::GetCovMatrix(TMatrixDSym& mcov) const
213 // Fills the matrix m passed as argument as the covariance matrix calculated
214 // from the coefficients of the reduced covariance matrix data members
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.;
224 mcov(i,i) = (fDiag[i] >= 0.) ? fDiag[i]*fDiag[i] : -999.;
229 //______________________________________________________________________________
230 void AliAlignObj::SetCorrMatrix(Double_t *cmat)
232 // Sets the correlation matrix data member from the coefficients of the external covariance
233 // matrix (21 elements passed as argument).
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.;
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
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.;
258 //______________________________________________________________________________
259 void AliAlignObj::SetCorrMatrix(TMatrixDSym& mcov)
261 // Sets the correlation matrix data member from the covariance matrix mcov passed
262 // passed as argument.
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.;
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
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.;
287 //_____________________________________________________________________________
288 void AliAlignObj::AnglesToMatrix(const Double_t *angles, Double_t *rot) const
290 // Calculates the rotation matrix using the
291 // Euler angles in "x y z" notation
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]);
301 rot[0] = costhe*cosphi;
302 rot[1] = -costhe*sinphi;
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;
312 //_____________________________________________________________________________
313 Bool_t AliAlignObj::MatrixToAngles(const Double_t *rot, Double_t *angles) const
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
320 if(TMath::Abs(rot[0])<1e-7 || TMath::Abs(rot[8])<1e-7) {
321 AliError("Failed to extract roll-pitch-yall angles!");
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]);
331 //______________________________________________________________________________
332 void AliAlignObj::Transform(AliTrackPoint &p) const
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
339 if (fVolUID != p.GetVolumeID())
340 AliWarning(Form("Alignment object ID is not equal to the space-point ID (%d != %d)",fVolUID,p.GetVolumeID()));
344 Double_t *rot = m.GetRotationMatrix();
345 Double_t *tr = m.GetTranslation();
347 Float_t xyzin[3],xyzout[3];
349 for (Int_t i = 0; i < 3; i++)
358 //_____________________________________________________________________________
359 void AliAlignObj::Transform(AliTrackPointArray &array) const
361 // This method is used to transform all the track points
362 // from the input AliTrackPointArray
365 for (Int_t i = 0; i < array.GetNPoints(); i++) {
368 array.AddPoint(i,&p);
372 //_____________________________________________________________________________
373 void AliAlignObj::Print(Option_t *) const
375 // Print the contents of the
376 // alignment object in angles and
377 // matrix representations
385 const Double_t *rot = m.GetRotationMatrix();
387 printf("Volume=%s\n",GetSymName());
388 if (GetVolUID() != 0) {
389 AliGeomManager::ELayerID layerId;
391 GetVolUID(layerId,modId);
392 printf("VolumeID=%d LayerID=%d ( %s ) ModuleID=%d\n", GetVolUID(),layerId,AliGeomManager::LayerName(layerId),modId);
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]);
400 //_____________________________________________________________________________
401 void AliAlignObj::SetPars(Double_t x, Double_t y, Double_t z,
402 Double_t psi, Double_t theta, Double_t phi)
404 // Set the global delta transformation by passing 3 angles (expressed in
405 // degrees) and 3 shifts (in centimeters)
407 SetTranslation(x,y,z);
408 SetRotation(psi,theta,phi);
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)
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.
421 Double_t tr[3] = {x, y, z};
422 m.SetTranslation(tr);
423 Double_t angles[3] = {psi, theta, phi};
425 AnglesToMatrix(angles,rot);
428 return SetLocalMatrix(m);
432 //_____________________________________________________________________________
433 Bool_t AliAlignObj::SetLocalTranslation(Double_t x, Double_t y, Double_t z)
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.
442 Double_t tr[3] = {x, y, z};
443 m.SetTranslation(tr);
445 return SetLocalMatrix(m);
449 //_____________________________________________________________________________
450 Bool_t AliAlignObj::SetLocalTranslation(const TGeoMatrix& m)
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.
457 const Double_t* tr = m.GetTranslation();
459 mtr.SetTranslation(tr);
461 return SetLocalMatrix(mtr);
465 //_____________________________________________________________________________
466 Bool_t AliAlignObj::SetLocalRotation(Double_t psi, Double_t theta, Double_t phi)
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.
475 Double_t angles[3] = {psi, theta, phi};
477 AnglesToMatrix(angles,rot);
480 return SetLocalMatrix(m);
484 //_____________________________________________________________________________
485 Bool_t AliAlignObj::SetLocalRotation(const TGeoMatrix& m)
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.
493 const Double_t* rot = m.GetRotationMatrix();
494 rotm.SetRotation(rot);
496 return SetLocalMatrix(rotm);
500 //_____________________________________________________________________________
501 Bool_t AliAlignObj::SetLocalMatrix(const TGeoMatrix& m)
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.
508 if (!gGeoManager || !gGeoManager->IsClosed()) {
509 AliError("Can't set the alignment object parameters! gGeoManager doesn't exist or it is still opened!");
513 const char* symname = GetSymName();
514 TGeoPhysicalNode* node;
515 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
517 node = gGeoManager->MakeAlignablePN(pne);
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);
524 AliError(Form("Volume name or path %s not valid!",symname));
527 if (node->IsAligned())
528 AliWarning(Form("Volume %s has been already misaligned!",symname));
531 const Double_t *tr = m.GetTranslation();
532 m1.SetTranslation(tr);
533 const Double_t* rot = m.GetRotationMatrix();
536 TGeoHMatrix align,gprime,gprimeinv;
537 gprime = *node->GetMatrix();
538 gprimeinv = gprime.Inverse();
539 m1.Multiply(&gprimeinv);
540 m1.MultiplyLeft(&gprime);
542 return SetMatrix(m1);
545 //_____________________________________________________________________________
546 Bool_t AliAlignObj::SetMatrix(const TGeoMatrix& m)
548 // Set the global delta transformation by passing the TGeoMatrix
552 return SetRotation(m);
555 //_____________________________________________________________________________
556 Bool_t AliAlignObj::GetLocalPars(Double_t transl[], Double_t angles[]) const
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.
563 if(!GetLocalTranslation(transl)) return kFALSE;
564 return GetLocalAngles(angles);
567 //_____________________________________________________________________________
568 Bool_t AliAlignObj::GetLocalTranslation(Double_t* tr) const
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.
576 if(!GetLocalMatrix(ml)) return kFALSE;
577 const Double_t* transl;
578 transl = ml.GetTranslation();
585 //_____________________________________________________________________________
586 Bool_t AliAlignObj::GetLocalAngles(Double_t* angles) const
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.
594 if(!GetLocalMatrix(ml)) return kFALSE;
595 const Double_t *rot = ml.GetRotationMatrix();
596 return MatrixToAngles(rot,angles);
599 //_____________________________________________________________________________
600 Bool_t AliAlignObj::GetLocalMatrix(TGeoHMatrix& m) const
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.
606 if (!gGeoManager || !gGeoManager->IsClosed()) {
607 AliError("Can't set the alignment object parameters! gGeoManager doesn't exist or it is still opened!");
611 const char* symname = GetSymName();
612 TGeoPhysicalNode* node;
613 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
615 node = gGeoManager->MakeAlignablePN(pne);
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);
622 AliError(Form("Volume name or path %s not valid!",symname));
625 if (node->IsAligned())
626 AliWarning(Form("Volume %s has been already misaligned!",symname));
629 TGeoHMatrix gprime,gprimeinv;
630 gprime = *node->GetMatrix();
631 gprimeinv = gprime.Inverse();
633 m.MultiplyLeft(&gprimeinv);
638 //_____________________________________________________________________________
639 Bool_t AliAlignObj::ApplyToGeometry()
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
645 if (!gGeoManager || !gGeoManager->IsClosed()) {
646 AliError("Can't apply the alignment object! gGeoManager doesn't exist or it is still opened!");
650 const char* symname = GetSymName();
652 TGeoPhysicalNode* node;
653 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
655 path = pne->GetTitle();
656 node = gGeoManager->MakeAlignablePN(pne);
658 AliDebug(1,Form("The symbolic volume name %s does not correspond to a physical entry. Using it as a volume path!",symname));
660 if (!gGeoManager->CheckPath(path)) {
661 AliDebug(1,Form("Volume path %s not valid!",path));
664 if (gGeoManager->GetListOfPhysicalNodes()->FindObject(path)) {
665 AliError(Form("Volume %s has already been misaligned!",path));
668 node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(path);
672 AliError(Form("Volume path %s not valid!",path));
676 TGeoHMatrix align,gprime;
677 gprime = *node->GetMatrix();
679 gprime.MultiplyLeft(&align);
680 TGeoHMatrix *ginv = new TGeoHMatrix;
681 TGeoHMatrix *g = node->GetMatrix(node->GetLevel()-1);
682 *ginv = g->Inverse();
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));