]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliAlignObj.cxx
bug
[u/mrichter/AliRoot.git] / STEER / AliAlignObj.cxx
index 228afbf0e1e841179234ff73d36e76716f289f5d..c98fa986c983f16bdd736fc8d7b06b1dc6deaafd 100644 (file)
 //  Implementation of the alignment object class, holding the alignment
 //  constants for a single volume, through the abstract class AliAlignObj.
 //  From it two derived concrete representation of alignment object class
-//  (AliAlignObjAngles, AliAlignObjMatrix) are derived in separate files.
+//  (AliAlignObjParams, AliAlignObjMatrix) are derived in separate files.
 //-----------------------------------------------------------------
 
-#include <TClass.h>
 #include <TGeoManager.h>
+#include <TGeoMatrix.h>
 #include <TGeoPhysicalNode.h>
 #include <TMath.h>
-#include "TObjString.h"
+#include <TMatrixDSym.h>
 
 #include "AliAlignObj.h"
 #include "AliTrackPointArray.h"
 #include "AliLog.h"
-#include "AliAlignObjAngles.h"
  
 ClassImp(AliAlignObj)
 
@@ -41,7 +40,8 @@ AliAlignObj::AliAlignObj():
   fVolUID(0)
 {
   // default constructor
-  // InitSymNames();
+  for(Int_t i=0; i<6; i++) fDiag[i]=-999.;
+  for(Int_t i=0; i<15; i++) fODia[i]=-999.;
 }
 
 //_____________________________________________________________________________
@@ -52,6 +52,19 @@ AliAlignObj::AliAlignObj(const char* symname, UShort_t voluid) :
 {
   // standard constructor
   //
+  for(Int_t i=0; i<6; i++) fDiag[i]=-999.;
+  for(Int_t i=0; i<15; i++) fODia[i]=-999.;
+}
+
+//_____________________________________________________________________________
+AliAlignObj::AliAlignObj(const char* symname, UShort_t voluid, Double_t* cmat) :
+  TObject(),
+  fVolPath(symname),
+  fVolUID(voluid)
+{
+  // standard constructor
+  //
+  SetCorrMatrix(cmat);
 }
 
 //_____________________________________________________________________________
@@ -61,6 +74,8 @@ AliAlignObj::AliAlignObj(const AliAlignObj& theAlignObj) :
   fVolUID(theAlignObj.GetVolUID())
 {
   //copy constructor
+  for(Int_t i=0; i<6; i++) fDiag[i]=theAlignObj.fDiag[i];
+  for(Int_t i=0; i<15; i++) fODia[i]=theAlignObj.fODia[i];
 }
 
 //_____________________________________________________________________________
@@ -70,6 +85,8 @@ AliAlignObj &AliAlignObj::operator =(const AliAlignObj& theAlignObj)
   if(this==&theAlignObj) return *this;
   fVolPath = theAlignObj.GetSymName();
   fVolUID = theAlignObj.GetVolUID();
+  for(Int_t i=0; i<6; i++) fDiag[i]=theAlignObj.fDiag[i];
+  for(Int_t i=0; i<15; i++) fODia[i]=theAlignObj.fODia[i];
   return *this;
 }
 
@@ -85,6 +102,11 @@ AliAlignObj &AliAlignObj::operator*=(const AliAlignObj& theAlignObj)
   theAlignObj.GetMatrix(m2);
   m1.MultiplyLeft(&m2);
   SetMatrix(m1);
+  // temporary solution: the covariance matrix of the resulting combined object
+  // is set equal to the covariance matrix of the right operand
+  // (not to be used for combining alignment objects for different levels)
+  for(Int_t i=0; i<6; i++)  fDiag[i] = theAlignObj.fDiag[i];
+  for(Int_t i=0; i<15; i++)  fODia[i] = theAlignObj.fODia[i];  
   return *this;
 }
 
@@ -133,7 +155,7 @@ Int_t AliAlignObj::GetLevel() const
   // slashes in the corresponding volume path
   //
   if(!gGeoManager){
-    AliWarning("gGeoManager doesn't exist or it is still opened: unable to return meaningful level value.");
+    AliWarning("gGeoManager doesn't exist or it is still open: unable to return meaningful level value.");
     return (-1);
   }
   const char* symname = GetSymName();
@@ -145,9 +167,9 @@ Int_t AliAlignObj::GetLevel() const
     path = symname;
   }
 
-  TString path_str = path;
-  if(path_str[0]!='/') path_str.Prepend('/');
-  return path_str.CountChar('/');
+  TString pathStr = path;
+  if(pathStr[0]!='/') pathStr.Prepend('/');
+  return pathStr.CountChar('/');
 }
 
 //_____________________________________________________________________________
@@ -167,6 +189,103 @@ Int_t AliAlignObj::Compare(const TObject *obj) const
     return ((level > level2) ? 1 : -1);
 }
 
+//______________________________________________________________________________
+void AliAlignObj::GetCovMatrix(Double_t *cmat) const
+{
+  // Fills the cmat argument with the coefficients of the external cov matrix (21 elements)
+  // calculating them from the correlation matrix data member
+  //
+
+  for(Int_t i=0; i<6; ++i) {
+    // Off diagonal elements
+    for(Int_t j=0; j<i; ++j) {
+      cmat[i*(i+1)/2+j] = (fDiag[j] >= 0. && fDiag[i] >= 0.) ? fODia[(i-1)*i/2+j]*fDiag[j]*fDiag[i]: -999.;
+    }
+
+    // Diagonal elements
+    cmat[i*(i+1)/2+i] = (fDiag[i] >= 0.) ? fDiag[i]*fDiag[i] : -999.;
+  }
+
+  return;
+}
+
+//______________________________________________________________________________
+void AliAlignObj::GetCovMatrix(TMatrixDSym& mcov) const
+{
+  // Fills the matrix m passed as argument as the covariance matrix calculated
+  // from the coefficients of the reduced covariance matrix data members
+  //
+
+  for(Int_t i=0; i<6; ++i) {
+    // Off diagonal elements
+    for(Int_t j=0; j<i; ++j) {
+      mcov(j,i) = mcov(i,j) = (fDiag[j] >= 0. && fDiag[i] >= 0.) ? fODia[(i-1)*i/2+j]*fDiag[j]*fDiag[i]: -999.;
+    }
+
+    // Diagonal elements
+    mcov(i,i) = (fDiag[i] >= 0.) ? fDiag[i]*fDiag[i] : -999.;
+  }
+
+}
+
+//______________________________________________________________________________
+void AliAlignObj::SetCorrMatrix(Double_t *cmat)
+{
+  // Sets the correlation matrix data member from the coefficients of the external covariance
+  // matrix (21 elements passed as argument). 
+  //
+  if(cmat) {
+
+    // Diagonal elements first
+    for(Int_t i=0; i<6; ++i) {
+      fDiag[i] = (cmat[i*(i+1)/2+i] >= 0.) ? TMath::Sqrt(cmat[i*(i+1)/2+i]) : -999.;
+    }
+
+    // ... then the ones off diagonal
+    for(Int_t i=0; i<6; ++i)
+      // Off diagonal elements
+      for(Int_t j=0; j<i; ++j) {
+       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).
+       if (fODia[(i-1)*i/2+j]>1.)  fODia[(i-1)*i/2+j] =  1.; // check upper boundary
+       if (fODia[(i-1)*i/2+j]<-1.) fODia[(i-1)*i/2+j] = -1.; // check lower boundary
+      }
+  } else {
+    for(Int_t i=0; i< 6; ++i) fDiag[i]=-999.;
+    for(Int_t i=0; i< 6*(6-1)/2; ++i) fODia[i]=0.;
+  }
+
+  return;
+}
+
+//______________________________________________________________________________
+void AliAlignObj::SetCorrMatrix(TMatrixDSym& mcov)
+{
+  // Sets the correlation matrix data member from the covariance matrix mcov passed
+  // passed as argument. 
+  //
+  if(mcov.IsValid()) {
+
+    // Diagonal elements first
+    for(Int_t i=0; i<6; ++i) {
+      fDiag[i] = (mcov(i,i) >= 0.) ? TMath::Sqrt(mcov(i,i)) : -999.;
+    }
+
+    // ... then the ones off diagonal
+    for(Int_t i=0; i<6; ++i)
+      // Off diagonal elements
+      for(Int_t j=0; j<i; ++j) {
+       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).
+       if (fODia[(i-1)*i/2+j]>1.)  fODia[(i-1)*i/2+j] =  1.; // check upper boundary
+       if (fODia[(i-1)*i/2+j]<-1.) fODia[(i-1)*i/2+j] = -1.; // check lower boundary
+      }
+  } else {
+    for(Int_t i=0; i< 6; ++i) fDiag[i]=-999.;
+    for(Int_t i=0; i< 6*(6-1)/2; ++i) fODia[i]=0.;
+  }
+
+  return;
+}
+
 //_____________________________________________________________________________
 void AliAlignObj::AnglesToMatrix(const Double_t *angles, Double_t *rot) const
 {
@@ -212,12 +331,12 @@ Bool_t AliAlignObj::MatrixToAngles(const Double_t *rot, Double_t *angles) const
 }
 
 //______________________________________________________________________________
-void AliAlignObj::Transform(AliTrackPoint &p) const
+void AliAlignObj::Transform(AliTrackPoint &p, Bool_t copycov) const
 {
   // The method transforms the space-point coordinates using the
   // transformation matrix provided by the AliAlignObj
-  // The covariance matrix is not affected since we assume
-  // that the transformations are sufficiently small
+  // In case the copycov flag is set to kTRUE, the covariance matrix 
+  // of the alignment object is copied into the space-point
   //
   if (fVolUID != p.GetVolumeID())
     AliWarning(Form("Alignment object ID is not equal to the space-point ID (%d != %d)",fVolUID,p.GetVolumeID())); 
@@ -235,6 +354,12 @@ void AliAlignObj::Transform(AliTrackPoint &p) const
                 xyzin[1]*rot[3*i+1]+
                 xyzin[2]*rot[3*i+2];
   p.SetXYZ(xyzout);
+
+  if(copycov){
+    TMatrixDSym covmat(6);
+    GetCovMatrix(covmat); 
+    p.SetAlignCovMatrix(covmat);
+  }
   
 }
 
@@ -389,7 +514,7 @@ Bool_t AliAlignObj::SetLocalMatrix(const TGeoMatrix& m)
   // returns false and the object parameters are not set.
   //
   if (!gGeoManager || !gGeoManager->IsClosed()) {
-    AliError("Can't set the alignment object parameters! gGeoManager doesn't exist or it is still opened!");
+    AliError("Can't set the local alignment object parameters! gGeoManager doesn't exist or it is still open!");
     return kFALSE;
   }
 
@@ -397,7 +522,11 @@ Bool_t AliAlignObj::SetLocalMatrix(const TGeoMatrix& m)
   TGeoPhysicalNode* node;
   TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
   if(pne){
-    node = gGeoManager->MakeAlignablePN(pne);
+    if(!pne->GetPhysicalNode()){
+      node = gGeoManager->MakeAlignablePN(pne);
+    }else{
+      node = pne->GetPhysicalNode();
+    }
   }else{
     AliWarning(Form("The symbolic volume name %s does not correspond to a physical entry. Using it as volume path!",symname));
     node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(symname);
@@ -487,7 +616,7 @@ Bool_t AliAlignObj::GetLocalMatrix(TGeoHMatrix& m) const
   // returns false and the object parameters are not set.
   //
   if (!gGeoManager || !gGeoManager->IsClosed()) {
-    AliError("Can't set the alignment object parameters! gGeoManager doesn't exist or it is still opened!");
+    AliError("Can't get the local alignment object parameters! gGeoManager doesn't exist or it is still open!");
     return kFALSE;
   }
 
@@ -495,7 +624,11 @@ Bool_t AliAlignObj::GetLocalMatrix(TGeoHMatrix& m) const
   TGeoPhysicalNode* node;
   TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
   if(pne){
-    node = gGeoManager->MakeAlignablePN(pne);
+    if(!pne->GetPhysicalNode()){
+      node = gGeoManager->MakeAlignablePN(pne);
+    }else{
+      node = pne->GetPhysicalNode();
+    }
   }else{
     AliWarning(Form("The symbolic volume name %s does not correspond to a physical entry. Using it as volume path!",symname));
     node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(symname);
@@ -519,17 +652,22 @@ Bool_t AliAlignObj::GetLocalMatrix(TGeoHMatrix& m) const
 }
 
 //_____________________________________________________________________________
-Bool_t AliAlignObj::ApplyToGeometry()
+Bool_t AliAlignObj::ApplyToGeometry(Bool_t ovlpcheck)
 {
   // Apply the current alignment object to the TGeo geometry
   // This method returns FALSE if the symname of the object was not
   // valid neither to get a TGeoPEntry nor as a volume path
   //
   if (!gGeoManager || !gGeoManager->IsClosed()) {
-    AliError("Can't apply the alignment object! gGeoManager doesn't exist or it is still opened!");
+    AliError("Can't apply the alignment object! gGeoManager doesn't exist or it is still open!");
     return kFALSE;
   }
   
+  if (gGeoManager->IsLocked()){
+    AliError("Can't apply the alignment object! Geometry is locked!");
+    return kFALSE;
+  }
+
   const char* symname = GetSymName();
   const char* path;
   TGeoPhysicalNode* node;
@@ -568,7 +706,16 @@ Bool_t AliAlignObj::ApplyToGeometry()
   Int_t modId; // unique identity for volume inside layer in the alobj
   GetVolUID(layerId, modId);
   AliDebug(2,Form("Aligning volume %s of detector layer %d with local ID %d",symname,layerId,modId));
-  node->Align(ginv);
+  node->Align(ginv,0,ovlpcheck);
+  if(ovlpcheck){
+    Int_t novex=((TObjArray*)gGeoManager->GetListOfOverlaps())->GetEntriesFast();
+    if(novex){
+      TString error(Form("The alignment of volume %s introduced %d new overlap",GetSymName(),novex));
+      if(novex>1) error+="s";
+      AliError(error.Data());
+      return kFALSE;
+    }
+  }
 
   return kTRUE;
 }