]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/STEER/AliAlignObj.cxx
Fixes for coverity.
[u/mrichter/AliRoot.git] / STEER / STEER / AliAlignObj.cxx
CommitLineData
c18195b9 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
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 **************************************************************************/
15
090026bf 16/* $Id$ */
17
c18195b9 18//-----------------------------------------------------------------
7e154d52 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
90dbf5fb 22// (AliAlignObjParams, AliAlignObjMatrix) are derived in separate files.
c18195b9 23//-----------------------------------------------------------------
a1e17193 24
995ad051 25#include <TGeoManager.h>
268f57b1 26#include <TGeoMatrix.h>
995ad051 27#include <TGeoPhysicalNode.h>
d555b92a 28#include <TGeoOverlap.h>
090026bf 29#include <TMath.h>
995ad051 30
c18195b9 31#include "AliAlignObj.h"
03b18860 32#include "AliTrackPointArray.h"
33#include "AliLog.h"
98937d93 34
c18195b9 35ClassImp(AliAlignObj)
36
37//_____________________________________________________________________________
38AliAlignObj::AliAlignObj():
4e6b0ada 39 TObject(),
fe12e09c 40 fVolPath(),
c18195b9 41 fVolUID(0)
42{
03b18860 43 // default constructor
90dbf5fb 44 for(Int_t i=0; i<6; i++) fDiag[i]=-999.;
6b1b1a3b 45 for(Int_t i=0; i<15; i++) fODia[i]=-999.;
c18195b9 46}
47
48//_____________________________________________________________________________
b760c02e 49AliAlignObj::AliAlignObj(const char* symname, UShort_t voluid) :
fe12e09c 50 TObject(),
b760c02e 51 fVolPath(symname),
fe12e09c 52 fVolUID(voluid)
d9cc42ed 53{
54 // standard constructor
55 //
90dbf5fb 56 for(Int_t i=0; i<6; i++) fDiag[i]=-999.;
6b1b1a3b 57 for(Int_t i=0; i<15; i++) fODia[i]=-999.;
90dbf5fb 58}
59
60//_____________________________________________________________________________
61AliAlignObj::AliAlignObj(const char* symname, UShort_t voluid, Double_t* cmat) :
62 TObject(),
63 fVolPath(symname),
64 fVolUID(voluid)
65{
66 // standard constructor
67 //
68 SetCorrMatrix(cmat);
d9cc42ed 69}
70
d9cc42ed 71//_____________________________________________________________________________
c18195b9 72AliAlignObj::AliAlignObj(const AliAlignObj& theAlignObj) :
fe12e09c 73 TObject(theAlignObj),
b760c02e 74 fVolPath(theAlignObj.GetSymName()),
fe12e09c 75 fVolUID(theAlignObj.GetVolUID())
c18195b9 76{
77 //copy constructor
90dbf5fb 78 for(Int_t i=0; i<6; i++) fDiag[i]=theAlignObj.fDiag[i];
6b1b1a3b 79 for(Int_t i=0; i<15; i++) fODia[i]=theAlignObj.fODia[i];
c18195b9 80}
81
82//_____________________________________________________________________________
83AliAlignObj &AliAlignObj::operator =(const AliAlignObj& theAlignObj)
84{
85 // assignment operator
86 if(this==&theAlignObj) return *this;
b760c02e 87 fVolPath = theAlignObj.GetSymName();
c18195b9 88 fVolUID = theAlignObj.GetVolUID();
90dbf5fb 89 for(Int_t i=0; i<6; i++) fDiag[i]=theAlignObj.fDiag[i];
6b1b1a3b 90 for(Int_t i=0; i<15; i++) fODia[i]=theAlignObj.fODia[i];
c18195b9 91 return *this;
92}
93
38b3a170 94//_____________________________________________________________________________
95AliAlignObj &AliAlignObj::operator*=(const AliAlignObj& theAlignObj)
96{
97 // multiplication operator
98 // The operator can be used to 'combine'
99 // two alignment objects
100 TGeoHMatrix m1;
101 GetMatrix(m1);
102 TGeoHMatrix m2;
103 theAlignObj.GetMatrix(m2);
104 m1.MultiplyLeft(&m2);
105 SetMatrix(m1);
6b1b1a3b 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];
38b3a170 111 return *this;
112}
113
c18195b9 114//_____________________________________________________________________________
115AliAlignObj::~AliAlignObj()
116{
117 // dummy destructor
118}
119
befe2c08 120//_____________________________________________________________________________
25be1e5c 121void AliAlignObj::SetVolUID(AliGeomManager::ELayerID detId, Int_t modId)
befe2c08 122{
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).
127 //
25be1e5c 128 fVolUID = AliGeomManager::LayerToVolUID(detId,modId);
befe2c08 129}
130
131//_____________________________________________________________________________
25be1e5c 132void AliAlignObj::GetVolUID(AliGeomManager::ELayerID &layerId, Int_t &modId) const
befe2c08 133{
7e154d52 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.
befe2c08 140 //
25be1e5c 141 layerId = AliGeomManager::VolUIDToLayer(fVolUID,modId);
befe2c08 142}
143
b760c02e 144//_____________________________________________________________________________
145Bool_t AliAlignObj::GetPars(Double_t tr[], Double_t angles[]) const
146{
147 GetTranslation(tr);
148 return GetAngles(angles);
149}
150
4b94e753 151//_____________________________________________________________________________
152Int_t AliAlignObj::GetLevel() const
153{
85fbf070 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
157 //
158 if(!gGeoManager){
44a3c417 159 AliWarning("gGeoManager doesn't exist or it is still open: unable to return meaningful level value.");
85fbf070 160 return (-1);
161 }
162 const char* symname = GetSymName();
163 const char* path;
164 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
165 if(pne){
166 path = pne->GetTitle();
167 }else{
168 path = symname;
169 }
170
2499b21b 171 TString pathStr = path;
172 if(pathStr[0]!='/') pathStr.Prepend('/');
173 return pathStr.CountChar('/');
4b94e753 174}
175
176//_____________________________________________________________________________
177Int_t AliAlignObj::Compare(const TObject *obj) const
178{
179 // Compare the levels of two
180 // alignment objects
181 // Used in the sorting during
182 // the application of alignment
183 // objects to the geometry
7e154d52 184 //
4b94e753 185 Int_t level = GetLevel();
186 Int_t level2 = ((AliAlignObj *)obj)->GetLevel();
187 if (level == level2)
188 return 0;
189 else
190 return ((level > level2) ? 1 : -1);
191}
192
90dbf5fb 193//______________________________________________________________________________
194void AliAlignObj::GetCovMatrix(Double_t *cmat) const
195{
196 // Fills the cmat argument with the coefficients of the external cov matrix (21 elements)
197 // calculating them from the correlation matrix data member
198 //
199
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.;
204 }
205
206 // Diagonal elements
207 cmat[i*(i+1)/2+i] = (fDiag[i] >= 0.) ? fDiag[i]*fDiag[i] : -999.;
208 }
e9304cb8 209
210 return;
211}
212
213//______________________________________________________________________________
214void AliAlignObj::GetCovMatrix(TMatrixDSym& mcov) const
215{
216 // Fills the matrix m passed as argument as the covariance matrix calculated
217 // from the coefficients of the reduced covariance matrix data members
218 //
219
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.;
224 }
225
226 // Diagonal elements
227 mcov(i,i) = (fDiag[i] >= 0.) ? fDiag[i]*fDiag[i] : -999.;
228 }
229
90dbf5fb 230}
231
46175b3a 232//______________________________________________________________________________
233Bool_t AliAlignObj::GetLocalCovMatrix(TMatrixDSym& lCov) const
234{
235 // Calculates the covariance matrix (6x6) associated to the six parameters
236 // defining the current alignment in the global coordinates system (and sets
237 // in the internal data members) from the covariance matrix (6x6) for the six
238 // parameters defining the alignment transformation in the local coordinates
239 // system, passed as an argument.
240 //
241 TMatrixD mJ(6,6);// the jacobian of the transformation from local to global parameters
242 if(!GetJacobian(mJ)) return kFALSE;
46175b3a 243
244 TMatrixDSym gCov(6);
245 GetCovMatrix(gCov);
246
8809be5b 247 // Compute the local covariance matrix lcov = mJ^T gcov mJ
248 TMatrixD gcovJ(gCov,TMatrixD::kMult,mJ);
249 TMatrixD lCovM(mJ,TMatrixD::kTransposeMult,gcovJ);
46175b3a 250 // To be done: somehow check that lCovM is close enough to be symmetric
251 for(Int_t i=0; i<6; i++)
252 {
253 lCov(i,i) = lCovM(i,i);
254 for(Int_t j=i+1; j<6; j++)
255 {
256 lCov(i,j)=lCovM(i,j);
257 lCov(j,i)=lCovM(i,j);
258 }
259 }
260
261 return kTRUE;
262
263}
264
265//______________________________________________________________________________
266Bool_t AliAlignObj::GetLocalCovMatrix(Double_t *lCov) const
267{
268 // Calculates the covariance matrix (6x6) associated to the six parameters
269 // defining the current alignment in the global coordinates system (and sets
270 // in the internal data members) from the covariance matrix (6x6) for the six
271 // parameters defining the alignment transformation in the local coordinates
272 // system, passed as an argument.
273 //
274 TMatrixDSym lCovMatrix(6);
275 GetLocalCovMatrix(lCovMatrix);
276
277 Int_t k=0;
278 for(Int_t i=0; i<6; i++)
279 for(Int_t j=i; j<6; j++)
280 {
281 lCov[k++] = lCovMatrix(i,j);
282 }
283
284 return kTRUE;
285}
286
287//______________________________________________________________________________
288Bool_t AliAlignObj::GetJacobian(TMatrixD& mJ) const
289{
290 // Compute the jacobian J of the transformation of the six local to the six global delta parameters
291 //
292 // R00 R01 R02 | (R01Rk2 - R02Rk1)Tk (R02Rk0 - R00Rk2)Tk (R00Rk1 - R01Rk0)Tk
293 // R00 R01 R02 | (R11Rk2 - R12Rk1)Tk (R12Rk0 - R10Rk2)Tk (R10Rk1 - R11Rk0)Tk
294 // R00 R01 R02 | (R21Rk2 - R22Rk1)Tk (R22Rk0 - R20Rk2)Tk (R20Rk1 - R21Rk0)Tk
295 // - - - - - - - - - - - - - - - - - - - - - - -
296 // 0 0 0 | R11R22 - R12R21 R12R20 - R10R22 R10R21 - R11R20
297 // 0 0 0 | R21R02 - R22R01 R22R00 - R20R02 R20R01 - R21R00
298 // 0 0 0 | R01R12 - R02R11 R02R10 - R00R12 R00R11 - R01R10
299 //
300 if (!gGeoManager || !gGeoManager->IsClosed()) {
301 AliError("Can't compute the global covariance matrix from the local one without an open geometry!");
302 return kFALSE;
303 }
304
305 const char* symname = GetSymName();
306 TGeoPhysicalNode* node;
307 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
308 if(pne){
309 if(!pne->GetPhysicalNode()){
310 node = gGeoManager->MakeAlignablePN(pne);
311 }else{
312 node = pne->GetPhysicalNode();
313 }
314 }else{
315 AliWarning(Form("The symbolic volume name %s does not correspond to a physical entry. Using it as volume path!",symname));
316 node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(symname);
317 }
318
319 if (!node) {
320 AliError(Form("Volume name or path %s not valid!",symname));
321 return kFALSE;
322 }
323
324 TGeoHMatrix gm; //global matrix
325 gm = *node->GetMatrix();
326 Double_t *tr = gm.GetTranslation();
327 Double_t *rot = gm.GetRotationMatrix();
328
329 TGeoHMatrix m; // global delta transformation matrix
330 GetMatrix(m);
331 // We should probably check that it's sufficinetly close to identity
332 // if it's not return because the "small angles" approximation cannot hold
333
334 // 3x3 upper left part (global shifts derived w.r.t. local shifts)
335 for(Int_t i=0; i<3; i++)
336 {
337 for(Int_t j=0; j<3; j++)
338 {
339 mJ(i,j) = rot[i+3*j];
340 }
341 }
342
343 // 3x3 lower left part (global angles derived w.r.t. local shifts)
344 for(Int_t i=0; i<3; i++)
345 {
346 for(Int_t j=0; j<3; j++)
347 {
348 mJ(i+3,j) = 0.;
349 }
350 }
351
352 // 3x3 upper right part (global shifts derived w.r.t. local angles)
353 for(Int_t i=0; i<3; i++)
354 {
355 for(Int_t j=0; j<3; j++)
356 {
357 Double_t mEl = 0.;
358 Int_t b = (j+1)%3;
359 Int_t d = (j+2)%3;
360 for(Int_t k=0; k<3; k++)
361 {
362 mEl += (rot[3*i+b]*rot[3*k+d])*tr[k]-(rot[3*i+d]*rot[3*k+b])*tr[k];
363 }
364 mJ(i,j+3) = mEl;
365 }
366 }
367
368 // 3x3 lower right part (global angles derived w.r.t. local angles)
369 for(Int_t i=0; i<3; i++)
370 for(Int_t j=0; j<3; j++)
371 {
372 Int_t a = (i+1)%3;
373 Int_t b = (j+1)%3;
374 Int_t c = (i+2)%3;
375 Int_t d = (j+2)%3;
376 mJ(i+3,j+3) = rot[3*a+b]*rot[3*c+d]-rot[3*a+d]*rot[3*c+b];
377 }
378
379 return kTRUE;
380
381}
382
383//______________________________________________________________________________
384Bool_t AliAlignObj::SetFromLocalCov(TMatrixDSym& lCov)
385{
386 // Calculates the covariance matrix (6x6) associated to the six parameters
387 // defining the current alignment in the global coordinates system (and sets
388 // in the internal data members) from the covariance matrix (6x6) for the six
389 // parameters defining the alignment transformation in the local coordinates
390 // system, passed as an argument.
391 //
392 TMatrixD mJ(6,6);// the jacobian of the transformation from local to global parameters
393 if(!GetJacobian(mJ)) return kFALSE;
46175b3a 394
8809be5b 395 // Compute the global covariance matrix gcov = mJ lcov mJ'
396 TMatrixD trJ(TMatrixD::kTransposed, mJ);
397 TMatrixD lcovTrJ(lCov,TMatrixD::kMult,trJ);
398 TMatrixD gCovM(mJ,TMatrixD::kMult,lcovTrJ);
46175b3a 399 // To be done: somehow check that gCovM is close enough to be symmetric
400 TMatrixDSym gCov(6);
401 for(Int_t i=0; i<6; i++)
402 {
403 gCov(i,i) = gCovM(i,i);
404 for(Int_t j=i+1; j<6; j++)
405 {
406 gCov(i,j)=gCovM(i,j);
407 gCov(j,i)=gCovM(i,j);
408 }
409 }
410 SetCorrMatrix(gCov);
411
412 return kTRUE;
413
414}
415
416//______________________________________________________________________________
417Bool_t AliAlignObj::SetFromLocalCov(Double_t *lCov)
418{
419 // Calculates the covariance matrix (6x6) associated to the six parameters
420 // defining the current alignment in the global coordinates system, and sets
421 // in the internal data members, from the 21 coefficients, passed as argument,
422 // of the covariance matrix (6x6) for the six parameters defining the
423 // alignment transformation in the local coordinates system.
424 //
425 TMatrixDSym lCovMatrix(6);
426
427 Int_t k=0;
428 for(Int_t i=0; i<6; i++)
429 for(Int_t j=i; j<6; j++)
430 {
431 lCovMatrix(i,j) = lCov[k++];
432 if(j!=i) lCovMatrix(j,i) = lCovMatrix(i,j);
433 }
434
435 return SetFromLocalCov(lCovMatrix);
436
437}
438
439
90dbf5fb 440//______________________________________________________________________________
441void AliAlignObj::SetCorrMatrix(Double_t *cmat)
442{
443 // Sets the correlation matrix data member from the coefficients of the external covariance
444 // matrix (21 elements passed as argument).
445 //
446 if(cmat) {
447
448 // Diagonal elements first
449 for(Int_t i=0; i<6; ++i) {
450 fDiag[i] = (cmat[i*(i+1)/2+i] >= 0.) ? TMath::Sqrt(cmat[i*(i+1)/2+i]) : -999.;
451 }
452
453 // ... then the ones off diagonal
454 for(Int_t i=0; i<6; ++i)
455 // Off diagonal elements
456 for(Int_t j=0; j<i; ++j) {
457 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).
458 if (fODia[(i-1)*i/2+j]>1.) fODia[(i-1)*i/2+j] = 1.; // check upper boundary
459 if (fODia[(i-1)*i/2+j]<-1.) fODia[(i-1)*i/2+j] = -1.; // check lower boundary
460 }
461 } else {
462 for(Int_t i=0; i< 6; ++i) fDiag[i]=-999.;
463 for(Int_t i=0; i< 6*(6-1)/2; ++i) fODia[i]=0.;
464 }
465
466 return;
467}
468
e9304cb8 469//______________________________________________________________________________
470void AliAlignObj::SetCorrMatrix(TMatrixDSym& mcov)
471{
472 // Sets the correlation matrix data member from the covariance matrix mcov passed
473 // passed as argument.
474 //
475 if(mcov.IsValid()) {
476
477 // Diagonal elements first
478 for(Int_t i=0; i<6; ++i) {
479 fDiag[i] = (mcov(i,i) >= 0.) ? TMath::Sqrt(mcov(i,i)) : -999.;
480 }
481
482 // ... then the ones off diagonal
483 for(Int_t i=0; i<6; ++i)
484 // Off diagonal elements
485 for(Int_t j=0; j<i; ++j) {
486 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).
487 if (fODia[(i-1)*i/2+j]>1.) fODia[(i-1)*i/2+j] = 1.; // check upper boundary
488 if (fODia[(i-1)*i/2+j]<-1.) fODia[(i-1)*i/2+j] = -1.; // check lower boundary
489 }
490 } else {
491 for(Int_t i=0; i< 6; ++i) fDiag[i]=-999.;
492 for(Int_t i=0; i< 6*(6-1)/2; ++i) fODia[i]=0.;
493 }
494
495 return;
496}
497
c18195b9 498//_____________________________________________________________________________
499void AliAlignObj::AnglesToMatrix(const Double_t *angles, Double_t *rot) const
500{
fdf65bb5 501 // Calculates the rotation matrix using the
502 // Euler angles in "x y z" notation
7e154d52 503 //
c18195b9 504 Double_t degrad = TMath::DegToRad();
505 Double_t sinpsi = TMath::Sin(degrad*angles[0]);
506 Double_t cospsi = TMath::Cos(degrad*angles[0]);
507 Double_t sinthe = TMath::Sin(degrad*angles[1]);
508 Double_t costhe = TMath::Cos(degrad*angles[1]);
509 Double_t sinphi = TMath::Sin(degrad*angles[2]);
510 Double_t cosphi = TMath::Cos(degrad*angles[2]);
511
512 rot[0] = costhe*cosphi;
513 rot[1] = -costhe*sinphi;
514 rot[2] = sinthe;
515 rot[3] = sinpsi*sinthe*cosphi + cospsi*sinphi;
516 rot[4] = -sinpsi*sinthe*sinphi + cospsi*cosphi;
517 rot[5] = -costhe*sinpsi;
518 rot[6] = -cospsi*sinthe*cosphi + sinpsi*sinphi;
519 rot[7] = cospsi*sinthe*sinphi + sinpsi*cosphi;
520 rot[8] = costhe*cospsi;
521}
522
523//_____________________________________________________________________________
524Bool_t AliAlignObj::MatrixToAngles(const Double_t *rot, Double_t *angles) const
525{
fdf65bb5 526 // Calculates the Euler angles in "x y z" notation
527 // using the rotation matrix
b760c02e 528 // Returns false in case the rotation angles can not be
529 // extracted from the matrix
7e154d52 530 //
b760c02e 531 if(TMath::Abs(rot[0])<1e-7 || TMath::Abs(rot[8])<1e-7) {
532 AliError("Failed to extract roll-pitch-yall angles!");
533 return kFALSE;
534 }
c18195b9 535 Double_t raddeg = TMath::RadToDeg();
536 angles[0]=raddeg*TMath::ATan2(-rot[5],rot[8]);
537 angles[1]=raddeg*TMath::ASin(rot[2]);
538 angles[2]=raddeg*TMath::ATan2(-rot[1],rot[0]);
539 return kTRUE;
540}
541
03b18860 542//______________________________________________________________________________
6b1b1a3b 543void AliAlignObj::Transform(AliTrackPoint &p, Bool_t copycov) const
03b18860 544{
545 // The method transforms the space-point coordinates using the
546 // transformation matrix provided by the AliAlignObj
6b1b1a3b 547 // In case the copycov flag is set to kTRUE, the covariance matrix
548 // of the alignment object is copied into the space-point
7e154d52 549 //
03b18860 550 if (fVolUID != p.GetVolumeID())
551 AliWarning(Form("Alignment object ID is not equal to the space-point ID (%d != %d)",fVolUID,p.GetVolumeID()));
552
553 TGeoHMatrix m;
554 GetMatrix(m);
555 Double_t *rot = m.GetRotationMatrix();
556 Double_t *tr = m.GetTranslation();
557
558 Float_t xyzin[3],xyzout[3];
559 p.GetXYZ(xyzin);
560 for (Int_t i = 0; i < 3; i++)
561 xyzout[i] = tr[i]+
562 xyzin[0]*rot[3*i]+
563 xyzin[1]*rot[3*i+1]+
564 xyzin[2]*rot[3*i+2];
565 p.SetXYZ(xyzout);
6b1b1a3b 566
567 if(copycov){
568 TMatrixDSym covmat(6);
569 GetCovMatrix(covmat);
570 p.SetAlignCovMatrix(covmat);
571 }
03b18860 572
573}
574
79e21da6 575//_____________________________________________________________________________
03b18860 576void AliAlignObj::Transform(AliTrackPointArray &array) const
577{
e1e6896f 578 // This method is used to transform all the track points
579 // from the input AliTrackPointArray
7e154d52 580 //
03b18860 581 AliTrackPoint p;
582 for (Int_t i = 0; i < array.GetNPoints(); i++) {
583 array.GetPoint(p,i);
584 Transform(p);
585 array.AddPoint(i,&p);
586 }
587}
588
c18195b9 589//_____________________________________________________________________________
590void AliAlignObj::Print(Option_t *) const
591{
592 // Print the contents of the
593 // alignment object in angles and
594 // matrix representations
7e154d52 595 //
c18195b9 596 Double_t tr[3];
597 GetTranslation(tr);
598 Double_t angles[3];
599 GetAngles(angles);
600 TGeoHMatrix m;
601 GetMatrix(m);
602 const Double_t *rot = m.GetRotationMatrix();
c18195b9 603
b760c02e 604 printf("Volume=%s\n",GetSymName());
c041444f 605 if (GetVolUID() != 0) {
25be1e5c 606 AliGeomManager::ELayerID layerId;
c041444f 607 Int_t modId;
608 GetVolUID(layerId,modId);
25be1e5c 609 printf("VolumeID=%d LayerID=%d ( %s ) ModuleID=%d\n", GetVolUID(),layerId,AliGeomManager::LayerName(layerId),modId);
c041444f 610 }
611 printf("%12.8f%12.8f%12.8f Tx = %12.8f Psi = %12.8f\n", rot[0], rot[1], rot[2], tr[0], angles[0]);
612 printf("%12.8f%12.8f%12.8f Ty = %12.8f Theta = %12.8f\n", rot[3], rot[4], rot[5], tr[1], angles[1]);
613 printf("%12.8f%12.8f%12.8f Tz = %12.8f Phi = %12.8f\n", rot[6], rot[7], rot[8], tr[2], angles[2]);
614
615}
616
b760c02e 617//_____________________________________________________________________________
618void AliAlignObj::SetPars(Double_t x, Double_t y, Double_t z,
619 Double_t psi, Double_t theta, Double_t phi)
620{
32898fe7 621 // Set the global delta transformation by passing 3 angles (expressed in
622 // degrees) and 3 shifts (in centimeters)
7e154d52 623 //
b760c02e 624 SetTranslation(x,y,z);
625 SetRotation(psi,theta,phi);
626}
627
1bfe7ffc 628//_____________________________________________________________________________
629Bool_t AliAlignObj::SetLocalPars(Double_t x, Double_t y, Double_t z,
630 Double_t psi, Double_t theta, Double_t phi)
631{
32898fe7 632 // Set the global delta transformation by passing the parameters
633 // for the local delta transformation (3 shifts and 3 angles).
7e154d52 634 // In case that the TGeo was not initialized or not closed,
635 // returns false and the object parameters are not set.
636 //
b760c02e 637 TGeoHMatrix m;
638 Double_t tr[3] = {x, y, z};
639 m.SetTranslation(tr);
640 Double_t angles[3] = {psi, theta, phi};
641 Double_t rot[9];
642 AnglesToMatrix(angles,rot);
643 m.SetRotation(rot);
644
645 return SetLocalMatrix(m);
646
647}
648
32898fe7 649//_____________________________________________________________________________
650Bool_t AliAlignObj::SetLocalTranslation(Double_t x, Double_t y, Double_t z)
651{
652 // Set the global delta transformation by passing the three shifts giving
653 // the translation in the local reference system of the alignable
654 // volume (known by TGeo geometry).
655 // In case that the TGeo was not initialized or not closed,
656 // returns false and the object parameters are not set.
657 //
658 TGeoHMatrix m;
659 Double_t tr[3] = {x, y, z};
660 m.SetTranslation(tr);
661
662 return SetLocalMatrix(m);
663
664}
665
666//_____________________________________________________________________________
667Bool_t AliAlignObj::SetLocalTranslation(const TGeoMatrix& m)
668{
669 // Set the global delta transformation by passing the matrix of
670 // the local delta transformation and taking its translational part
671 // In case that the TGeo was not initialized or not closed,
672 // returns false and the object parameters are not set.
673 //
674 const Double_t* tr = m.GetTranslation();
675 TGeoHMatrix mtr;
676 mtr.SetTranslation(tr);
677
678 return SetLocalMatrix(mtr);
679
680}
681
682//_____________________________________________________________________________
683Bool_t AliAlignObj::SetLocalRotation(Double_t psi, Double_t theta, Double_t phi)
684{
685 // Set the global delta transformation by passing the three angles giving
686 // the rotation in the local reference system of the alignable
687 // volume (known by TGeo geometry).
688 // In case that the TGeo was not initialized or not closed,
689 // returns false and the object parameters are not set.
690 //
691 TGeoHMatrix m;
692 Double_t angles[3] = {psi, theta, phi};
693 Double_t rot[9];
694 AnglesToMatrix(angles,rot);
695 m.SetRotation(rot);
696
697 return SetLocalMatrix(m);
698
699}
700
701//_____________________________________________________________________________
702Bool_t AliAlignObj::SetLocalRotation(const TGeoMatrix& m)
703{
704 // Set the global delta transformation by passing the matrix of
705 // the local delta transformation and taking its rotational part
706 // In case that the TGeo was not initialized or not closed,
707 // returns false and the object parameters are not set.
708 //
709 TGeoHMatrix rotm;
710 const Double_t* rot = m.GetRotationMatrix();
711 rotm.SetRotation(rot);
712
713 return SetLocalMatrix(rotm);
714
715}
716
b760c02e 717//_____________________________________________________________________________
718Bool_t AliAlignObj::SetLocalMatrix(const TGeoMatrix& m)
719{
32898fe7 720 // Set the global delta transformation by passing the TGeo matrix
721 // for the local delta transformation.
7e154d52 722 // In case that the TGeo was not initialized or not closed,
723 // returns false and the object parameters are not set.
724 //
1bfe7ffc 725 if (!gGeoManager || !gGeoManager->IsClosed()) {
44a3c417 726 AliError("Can't set the local alignment object parameters! gGeoManager doesn't exist or it is still open!");
1bfe7ffc 727 return kFALSE;
728 }
729
b760c02e 730 const char* symname = GetSymName();
3bb2760e 731 TGeoHMatrix gprime,gprimeinv;
732 TGeoPhysicalNode* pn = 0;
b760c02e 733 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
3bb2760e 734 if(pne)
735 {
736 pn = pne->GetPhysicalNode();
737 if(pn){
738 if (pn->IsAligned())
818a9d5f 739 AliWarning(Form("Volume %s has been misaligned already!",symname));
3bb2760e 740 gprime = *pn->GetMatrix();
44a3c417 741 }else{
3bb2760e 742 gprime = pne->GetGlobalOrig();
44a3c417 743 }
b760c02e 744 }else{
745 AliWarning(Form("The symbolic volume name %s does not correspond to a physical entry. Using it as volume path!",symname));
3bb2760e 746 if(!gGeoManager->cd(symname)) {
747 AliError(Form("Volume name or path %s not valid!",symname));
748 return kFALSE;
749 }
750 gprime = *gGeoManager->GetCurrentMatrix();
1bfe7ffc 751 }
1bfe7ffc 752
3bb2760e 753 TGeoHMatrix m1; // the TGeoHMatrix copy of the local delta "m"
b760c02e 754 const Double_t *tr = m.GetTranslation();
755 m1.SetTranslation(tr);
756 const Double_t* rot = m.GetRotationMatrix();
757 m1.SetRotation(rot);
1bfe7ffc 758
1bfe7ffc 759 gprimeinv = gprime.Inverse();
b760c02e 760 m1.Multiply(&gprimeinv);
761 m1.MultiplyLeft(&gprime);
1bfe7ffc 762
b760c02e 763 return SetMatrix(m1);
764}
1bfe7ffc 765
b760c02e 766//_____________________________________________________________________________
767Bool_t AliAlignObj::SetMatrix(const TGeoMatrix& m)
768{
32898fe7 769 // Set the global delta transformation by passing the TGeoMatrix
770 // for it
7e154d52 771 //
b760c02e 772 SetTranslation(m);
773 return SetRotation(m);
1bfe7ffc 774}
775
32898fe7 776//_____________________________________________________________________________
777Bool_t AliAlignObj::GetLocalPars(Double_t transl[], Double_t angles[]) const
778{
779 // Get the translations and angles (in degrees) expressing the
780 // local delta transformation.
781 // In case that the TGeo was not initialized or not closed,
782 // returns false and the object parameters are not set.
783 //
784 if(!GetLocalTranslation(transl)) return kFALSE;
785 return GetLocalAngles(angles);
786}
787
788//_____________________________________________________________________________
789Bool_t AliAlignObj::GetLocalTranslation(Double_t* tr) const
790{
791 // Get the 3 shifts giving the translational part of the local
792 // delta transformation.
793 // In case that the TGeo was not initialized or not closed,
794 // returns false and the object parameters are not set.
795 //
796 TGeoHMatrix ml;
797 if(!GetLocalMatrix(ml)) return kFALSE;
798 const Double_t* transl;
799 transl = ml.GetTranslation();
800 tr[0]=transl[0];
801 tr[1]=transl[1];
802 tr[2]=transl[2];
803 return kTRUE;
804}
805
806//_____________________________________________________________________________
807Bool_t AliAlignObj::GetLocalAngles(Double_t* angles) const
808{
809 // Get the 3 angles giving the rotational part of the local
810 // delta transformation.
811 // In case that the TGeo was not initialized or not closed,
812 // returns false and the object parameters are not set.
813 //
814 TGeoHMatrix ml;
815 if(!GetLocalMatrix(ml)) return kFALSE;
816 const Double_t *rot = ml.GetRotationMatrix();
817 return MatrixToAngles(rot,angles);
818}
819
820//_____________________________________________________________________________
821Bool_t AliAlignObj::GetLocalMatrix(TGeoHMatrix& m) const
822{
823 // Get the matrix for the local delta transformation.
824 // In case that the TGeo was not initialized or not closed,
825 // returns false and the object parameters are not set.
826 //
827 if (!gGeoManager || !gGeoManager->IsClosed()) {
44a3c417 828 AliError("Can't get the local alignment object parameters! gGeoManager doesn't exist or it is still open!");
32898fe7 829 return kFALSE;
830 }
831
832 const char* symname = GetSymName();
833 TGeoPhysicalNode* node;
834 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
835 if(pne){
44a3c417 836 if(!pne->GetPhysicalNode()){
837 node = gGeoManager->MakeAlignablePN(pne);
838 }else{
839 node = pne->GetPhysicalNode();
840 }
32898fe7 841 }else{
842 AliWarning(Form("The symbolic volume name %s does not correspond to a physical entry. Using it as volume path!",symname));
843 node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(symname);
844 }
845
846 if (!node) {
847 AliError(Form("Volume name or path %s not valid!",symname));
848 return kFALSE;
849 }
818a9d5f 850// if (node->IsAligned())
851// AliWarning(Form("Volume %s has been misaligned already!",symname));
32898fe7 852
853 GetMatrix(m);
854 TGeoHMatrix gprime,gprimeinv;
855 gprime = *node->GetMatrix();
856 gprimeinv = gprime.Inverse();
857 m.Multiply(&gprime);
858 m.MultiplyLeft(&gprimeinv);
859
860 return kTRUE;
861}
862
995ad051 863//_____________________________________________________________________________
5590c6c3 864Bool_t AliAlignObj::ApplyToGeometry(Bool_t ovlpcheck)
995ad051 865{
7e154d52 866 // Apply the current alignment object to the TGeo geometry
867 // This method returns FALSE if the symname of the object was not
868 // valid neither to get a TGeoPEntry nor as a volume path
869 //
995ad051 870 if (!gGeoManager || !gGeoManager->IsClosed()) {
44a3c417 871 AliError("Can't apply the alignment object! gGeoManager doesn't exist or it is still open!");
995ad051 872 return kFALSE;
873 }
171c4ef9 874
44a3c417 875 if (gGeoManager->IsLocked()){
876 AliError("Can't apply the alignment object! Geometry is locked!");
877 return kFALSE;
878 }
879
b760c02e 880 const char* symname = GetSymName();
881 const char* path;
882 TGeoPhysicalNode* node;
883 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
884 if(pne){
b760c02e 885 path = pne->GetTitle();
7e154d52 886 node = gGeoManager->MakeAlignablePN(pne);
b760c02e 887 }else{
5bd470e1 888 AliDebug(1,Form("The symbolic volume name %s does not correspond to a physical entry. Using it as a volume path!",symname));
b760c02e 889 path=symname;
7e154d52 890 if (!gGeoManager->CheckPath(path)) {
5bd470e1 891 AliDebug(1,Form("Volume path %s not valid!",path));
b760c02e 892 return kFALSE;
893 }
7e154d52 894 if (gGeoManager->GetListOfPhysicalNodes()->FindObject(path)) {
818a9d5f 895 AliError(Form("Volume %s has been misaligned already!",path));
b760c02e 896 return kFALSE;
897 }
898 node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(path);
995ad051 899 }
48cac49d 900
48cac49d 901 if (!node) {
b760c02e 902 AliError(Form("Volume path %s not valid!",path));
995ad051 903 return kFALSE;
904 }
905
9257a1bd 906 // Double_t threshold = 0.001;
171c4ef9 907
995ad051 908 TGeoHMatrix align,gprime;
909 gprime = *node->GetMatrix();
910 GetMatrix(align);
911 gprime.MultiplyLeft(&align);
912 TGeoHMatrix *ginv = new TGeoHMatrix;
913 TGeoHMatrix *g = node->GetMatrix(node->GetLevel()-1);
914 *ginv = g->Inverse();
915 *ginv *= gprime;
25be1e5c 916 AliGeomManager::ELayerID layerId; // unique identity for layer in the alobj
b760c02e 917 Int_t modId; // unique identity for volume inside layer in the alobj
995ad051 918 GetVolUID(layerId, modId);
b760c02e 919 AliDebug(2,Form("Aligning volume %s of detector layer %d with local ID %d",symname,layerId,modId));
5590c6c3 920 if(ovlpcheck){
d555b92a 921 node->Align(ginv,0,kTRUE); //(trunk of root takes threshold as additional argument)
922 }else{
923 node->Align(ginv,0,kFALSE);
924 }
925 if(ovlpcheck)
926 {
927 TObjArray* ovlpArray = gGeoManager->GetListOfOverlaps();
928 Int_t nOvlp = ovlpArray->GetEntriesFast();
929 if(nOvlp)
930 {
931 AliInfo(Form("Misalignment of node %s generated the following overlaps/extrusions:",node->GetName()));
932 for(Int_t i=0; i<nOvlp; i++)
933 ((TGeoOverlap*)ovlpArray->UncheckedAt(i))->PrintInfo();
5590c6c3 934 }
935 }
d555b92a 936
995ad051 937 return kTRUE;
938}
939
171c4ef9 940