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.;
208 //______________________________________________________________________________
209 void AliAlignObj::SetCorrMatrix(Double_t *cmat)
211 // Sets the correlation matrix data member from the coefficients of the external covariance
212 // matrix (21 elements passed as argument).
216 // Diagonal elements first
217 for(Int_t i=0; i<6; ++i) {
218 fDiag[i] = (cmat[i*(i+1)/2+i] >= 0.) ? TMath::Sqrt(cmat[i*(i+1)/2+i]) : -999.;
221 // ... then the ones off diagonal
222 for(Int_t i=0; i<6; ++i)
223 // Off diagonal elements
224 for(Int_t j=0; j<i; ++j) {
225 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).
226 if (fODia[(i-1)*i/2+j]>1.) fODia[(i-1)*i/2+j] = 1.; // check upper boundary
227 if (fODia[(i-1)*i/2+j]<-1.) fODia[(i-1)*i/2+j] = -1.; // check lower boundary
230 for(Int_t i=0; i< 6; ++i) fDiag[i]=-999.;
231 for(Int_t i=0; i< 6*(6-1)/2; ++i) fODia[i]=0.;
237 //_____________________________________________________________________________
238 void AliAlignObj::AnglesToMatrix(const Double_t *angles, Double_t *rot) const
240 // Calculates the rotation matrix using the
241 // Euler angles in "x y z" notation
243 Double_t degrad = TMath::DegToRad();
244 Double_t sinpsi = TMath::Sin(degrad*angles[0]);
245 Double_t cospsi = TMath::Cos(degrad*angles[0]);
246 Double_t sinthe = TMath::Sin(degrad*angles[1]);
247 Double_t costhe = TMath::Cos(degrad*angles[1]);
248 Double_t sinphi = TMath::Sin(degrad*angles[2]);
249 Double_t cosphi = TMath::Cos(degrad*angles[2]);
251 rot[0] = costhe*cosphi;
252 rot[1] = -costhe*sinphi;
254 rot[3] = sinpsi*sinthe*cosphi + cospsi*sinphi;
255 rot[4] = -sinpsi*sinthe*sinphi + cospsi*cosphi;
256 rot[5] = -costhe*sinpsi;
257 rot[6] = -cospsi*sinthe*cosphi + sinpsi*sinphi;
258 rot[7] = cospsi*sinthe*sinphi + sinpsi*cosphi;
259 rot[8] = costhe*cospsi;
262 //_____________________________________________________________________________
263 Bool_t AliAlignObj::MatrixToAngles(const Double_t *rot, Double_t *angles) const
265 // Calculates the Euler angles in "x y z" notation
266 // using the rotation matrix
267 // Returns false in case the rotation angles can not be
268 // extracted from the matrix
270 if(TMath::Abs(rot[0])<1e-7 || TMath::Abs(rot[8])<1e-7) {
271 AliError("Failed to extract roll-pitch-yall angles!");
274 Double_t raddeg = TMath::RadToDeg();
275 angles[0]=raddeg*TMath::ATan2(-rot[5],rot[8]);
276 angles[1]=raddeg*TMath::ASin(rot[2]);
277 angles[2]=raddeg*TMath::ATan2(-rot[1],rot[0]);
281 //______________________________________________________________________________
282 void AliAlignObj::Transform(AliTrackPoint &p) const
284 // The method transforms the space-point coordinates using the
285 // transformation matrix provided by the AliAlignObj
286 // The covariance matrix is not affected since we assume
287 // that the transformations are sufficiently small
289 if (fVolUID != p.GetVolumeID())
290 AliWarning(Form("Alignment object ID is not equal to the space-point ID (%d != %d)",fVolUID,p.GetVolumeID()));
294 Double_t *rot = m.GetRotationMatrix();
295 Double_t *tr = m.GetTranslation();
297 Float_t xyzin[3],xyzout[3];
299 for (Int_t i = 0; i < 3; i++)
308 //_____________________________________________________________________________
309 void AliAlignObj::Transform(AliTrackPointArray &array) const
311 // This method is used to transform all the track points
312 // from the input AliTrackPointArray
315 for (Int_t i = 0; i < array.GetNPoints(); i++) {
318 array.AddPoint(i,&p);
322 //_____________________________________________________________________________
323 void AliAlignObj::Print(Option_t *) const
325 // Print the contents of the
326 // alignment object in angles and
327 // matrix representations
335 const Double_t *rot = m.GetRotationMatrix();
337 printf("Volume=%s\n",GetSymName());
338 if (GetVolUID() != 0) {
339 AliGeomManager::ELayerID layerId;
341 GetVolUID(layerId,modId);
342 printf("VolumeID=%d LayerID=%d ( %s ) ModuleID=%d\n", GetVolUID(),layerId,AliGeomManager::LayerName(layerId),modId);
344 printf("%12.8f%12.8f%12.8f Tx = %12.8f Psi = %12.8f\n", rot[0], rot[1], rot[2], tr[0], angles[0]);
345 printf("%12.8f%12.8f%12.8f Ty = %12.8f Theta = %12.8f\n", rot[3], rot[4], rot[5], tr[1], angles[1]);
346 printf("%12.8f%12.8f%12.8f Tz = %12.8f Phi = %12.8f\n", rot[6], rot[7], rot[8], tr[2], angles[2]);
350 //_____________________________________________________________________________
351 void AliAlignObj::SetPars(Double_t x, Double_t y, Double_t z,
352 Double_t psi, Double_t theta, Double_t phi)
354 // Set the global delta transformation by passing 3 angles (expressed in
355 // degrees) and 3 shifts (in centimeters)
357 SetTranslation(x,y,z);
358 SetRotation(psi,theta,phi);
361 //_____________________________________________________________________________
362 Bool_t AliAlignObj::SetLocalPars(Double_t x, Double_t y, Double_t z,
363 Double_t psi, Double_t theta, Double_t phi)
365 // Set the global delta transformation by passing the parameters
366 // for the local delta transformation (3 shifts and 3 angles).
367 // In case that the TGeo was not initialized or not closed,
368 // returns false and the object parameters are not set.
371 Double_t tr[3] = {x, y, z};
372 m.SetTranslation(tr);
373 Double_t angles[3] = {psi, theta, phi};
375 AnglesToMatrix(angles,rot);
378 return SetLocalMatrix(m);
382 //_____________________________________________________________________________
383 Bool_t AliAlignObj::SetLocalTranslation(Double_t x, Double_t y, Double_t z)
385 // Set the global delta transformation by passing the three shifts giving
386 // the translation in the local reference system of the alignable
387 // volume (known by TGeo geometry).
388 // In case that the TGeo was not initialized or not closed,
389 // returns false and the object parameters are not set.
392 Double_t tr[3] = {x, y, z};
393 m.SetTranslation(tr);
395 return SetLocalMatrix(m);
399 //_____________________________________________________________________________
400 Bool_t AliAlignObj::SetLocalTranslation(const TGeoMatrix& m)
402 // Set the global delta transformation by passing the matrix of
403 // the local delta transformation and taking its translational part
404 // In case that the TGeo was not initialized or not closed,
405 // returns false and the object parameters are not set.
407 const Double_t* tr = m.GetTranslation();
409 mtr.SetTranslation(tr);
411 return SetLocalMatrix(mtr);
415 //_____________________________________________________________________________
416 Bool_t AliAlignObj::SetLocalRotation(Double_t psi, Double_t theta, Double_t phi)
418 // Set the global delta transformation by passing the three angles giving
419 // the rotation in the local reference system of the alignable
420 // volume (known by TGeo geometry).
421 // In case that the TGeo was not initialized or not closed,
422 // returns false and the object parameters are not set.
425 Double_t angles[3] = {psi, theta, phi};
427 AnglesToMatrix(angles,rot);
430 return SetLocalMatrix(m);
434 //_____________________________________________________________________________
435 Bool_t AliAlignObj::SetLocalRotation(const TGeoMatrix& m)
437 // Set the global delta transformation by passing the matrix of
438 // the local delta transformation and taking its rotational part
439 // In case that the TGeo was not initialized or not closed,
440 // returns false and the object parameters are not set.
443 const Double_t* rot = m.GetRotationMatrix();
444 rotm.SetRotation(rot);
446 return SetLocalMatrix(rotm);
450 //_____________________________________________________________________________
451 Bool_t AliAlignObj::SetLocalMatrix(const TGeoMatrix& m)
453 // Set the global delta transformation by passing the TGeo matrix
454 // for the local delta transformation.
455 // In case that the TGeo was not initialized or not closed,
456 // returns false and the object parameters are not set.
458 if (!gGeoManager || !gGeoManager->IsClosed()) {
459 AliError("Can't set the alignment object parameters! gGeoManager doesn't exist or it is still opened!");
463 const char* symname = GetSymName();
464 TGeoPhysicalNode* node;
465 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
467 node = gGeoManager->MakeAlignablePN(pne);
469 AliWarning(Form("The symbolic volume name %s does not correspond to a physical entry. Using it as volume path!",symname));
470 node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(symname);
474 AliError(Form("Volume name or path %s not valid!",symname));
477 if (node->IsAligned())
478 AliWarning(Form("Volume %s has been already misaligned!",symname));
481 const Double_t *tr = m.GetTranslation();
482 m1.SetTranslation(tr);
483 const Double_t* rot = m.GetRotationMatrix();
486 TGeoHMatrix align,gprime,gprimeinv;
487 gprime = *node->GetMatrix();
488 gprimeinv = gprime.Inverse();
489 m1.Multiply(&gprimeinv);
490 m1.MultiplyLeft(&gprime);
492 return SetMatrix(m1);
495 //_____________________________________________________________________________
496 Bool_t AliAlignObj::SetMatrix(const TGeoMatrix& m)
498 // Set the global delta transformation by passing the TGeoMatrix
502 return SetRotation(m);
505 //_____________________________________________________________________________
506 Bool_t AliAlignObj::GetLocalPars(Double_t transl[], Double_t angles[]) const
508 // Get the translations and angles (in degrees) expressing the
509 // local delta transformation.
510 // In case that the TGeo was not initialized or not closed,
511 // returns false and the object parameters are not set.
513 if(!GetLocalTranslation(transl)) return kFALSE;
514 return GetLocalAngles(angles);
517 //_____________________________________________________________________________
518 Bool_t AliAlignObj::GetLocalTranslation(Double_t* tr) const
520 // Get the 3 shifts giving the translational part of the local
521 // delta transformation.
522 // In case that the TGeo was not initialized or not closed,
523 // returns false and the object parameters are not set.
526 if(!GetLocalMatrix(ml)) return kFALSE;
527 const Double_t* transl;
528 transl = ml.GetTranslation();
535 //_____________________________________________________________________________
536 Bool_t AliAlignObj::GetLocalAngles(Double_t* angles) const
538 // Get the 3 angles giving the rotational part of the local
539 // delta transformation.
540 // In case that the TGeo was not initialized or not closed,
541 // returns false and the object parameters are not set.
544 if(!GetLocalMatrix(ml)) return kFALSE;
545 const Double_t *rot = ml.GetRotationMatrix();
546 return MatrixToAngles(rot,angles);
549 //_____________________________________________________________________________
550 Bool_t AliAlignObj::GetLocalMatrix(TGeoHMatrix& m) const
552 // Get the matrix for the local delta transformation.
553 // In case that the TGeo was not initialized or not closed,
554 // returns false and the object parameters are not set.
556 if (!gGeoManager || !gGeoManager->IsClosed()) {
557 AliError("Can't set the alignment object parameters! gGeoManager doesn't exist or it is still opened!");
561 const char* symname = GetSymName();
562 TGeoPhysicalNode* node;
563 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
565 node = gGeoManager->MakeAlignablePN(pne);
567 AliWarning(Form("The symbolic volume name %s does not correspond to a physical entry. Using it as volume path!",symname));
568 node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(symname);
572 AliError(Form("Volume name or path %s not valid!",symname));
575 if (node->IsAligned())
576 AliWarning(Form("Volume %s has been already misaligned!",symname));
579 TGeoHMatrix gprime,gprimeinv;
580 gprime = *node->GetMatrix();
581 gprimeinv = gprime.Inverse();
583 m.MultiplyLeft(&gprimeinv);
588 //_____________________________________________________________________________
589 Bool_t AliAlignObj::ApplyToGeometry()
591 // Apply the current alignment object to the TGeo geometry
592 // This method returns FALSE if the symname of the object was not
593 // valid neither to get a TGeoPEntry nor as a volume path
595 if (!gGeoManager || !gGeoManager->IsClosed()) {
596 AliError("Can't apply the alignment object! gGeoManager doesn't exist or it is still opened!");
600 const char* symname = GetSymName();
602 TGeoPhysicalNode* node;
603 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
605 path = pne->GetTitle();
606 node = gGeoManager->MakeAlignablePN(pne);
608 AliDebug(1,Form("The symbolic volume name %s does not correspond to a physical entry. Using it as a volume path!",symname));
610 if (!gGeoManager->CheckPath(path)) {
611 AliDebug(1,Form("Volume path %s not valid!",path));
614 if (gGeoManager->GetListOfPhysicalNodes()->FindObject(path)) {
615 AliError(Form("Volume %s has already been misaligned!",path));
618 node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(path);
622 AliError(Form("Volume path %s not valid!",path));
626 TGeoHMatrix align,gprime;
627 gprime = *node->GetMatrix();
629 gprime.MultiplyLeft(&align);
630 TGeoHMatrix *ginv = new TGeoHMatrix;
631 TGeoHMatrix *g = node->GetMatrix(node->GetLevel()-1);
632 *ginv = g->Inverse();
634 AliGeomManager::ELayerID layerId; // unique identity for layer in the alobj
635 Int_t modId; // unique identity for volume inside layer in the alobj
636 GetVolUID(layerId, modId);
637 AliDebug(2,Form("Aligning volume %s of detector layer %d with local ID %d",symname,layerId,modId));