]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TRD/AliTRDalignment.cxx
Modifications in AliRoot for re-processing a selected event.
[u/mrichter/AliRoot.git] / TRD / AliTRDalignment.cxx
index 3d4786124a1645f1ffa90ace284f4a65eb7091f4..28afe2aae6b9a0688d814c5c0e3a2c125dfaeb82 100644 (file)
  **************************************************************************/
 
 /* $Id$ */
-
-//////////////////////////////////////////////////////////////////////////////////
-//                                                                              //
-// An AliTRDalignment object contains the alignment data (3 shifts and 3 tilts) //
-// for all the alignable volumes of the TRD, i.e. for 18 supermodules and 540   //
-// chambers. The class provides simple tools for reading and writing these data //
-// in different formats, and for generating fake data that can be used to       //
-// simulate misalignment.                                                       //
-// The six alignment variables have the following meaning:                      //
-// shift in rphi                                                                //
-// shift in z                                                                   //
-// shift in r                                                                   //
-// tilt around rphi                                                             //
-// tilt around z                                                                //
-// tilt around r                                                                //
-// The shifts are in cm and the tilts are in degrees.                           //
-// The currently supported formats are:                                         //
-// - ascii                                                                      //
-// - root file containing a TClonesArray of alignment objects                   //
-// - offline conditions database                                                //
-// - OCDB-like root file                                                        //
-// - geometry file (like misaligned_geometry.root)                              //
-//                                                                              //
-// D.Miskowiec, November 2006                                                   //
-//                                                                              //
-//////////////////////////////////////////////////////////////////////////////////
-
+///////////////////////////////////////////////////////////////////////////////
+//                                                                           //
+// An AliTRDalignment object contains the alignment data (3 shifts and 3     //
+// tilts) for all the alignable volumes of the TRD, i.e. for 18 supermodules //
+// and 540 chambers. The class provides simple tools for reading and writing //
+// these data in different formats, and for generating fake data that can be //
+// used to simulate misalignment.                                            //
+// The six alignment variables have the following meaning:                   //
+// shift in rphi                                                             //
+// shift in z                                                                //
+// shift in r                                                                //
+// tilt around rphi                                                          //
+// tilt around z                                                             //
+// tilt around r                                                             //
+// The shifts are in cm and the tilts are in degrees.                        //
+// The currently supported formats are:                                      //
+// - ascii                                                                   //
+// - root file containing a TClonesArray of alignment objects                //
+// - offline conditions database                                             //
+// - OCDB-like root file                                                     //
+// - geometry file (like misaligned_geometry.root)                           //
+//                                                                           //
+// Some examples of usage (in an aliroot session):                           //
+// AliTRDalignment a,b,c,d,e;                                                //
+// double xsm[]={0,0,0,-70,0,0};                                             //
+// double xch[]={0,0,-50,0,0,0};                                             //
+// a.SetSm(4,xsm);                                                           // 
+// a.SetCh(120,xch);                                                         //
+// a.WriteAscii("kuku.dat");                                                 //
+// TGeoManager::Import("geometry.root"); a.WriteRoot("kuku.root");           //
+// TGeoManager::Import("geometry.root"); a.WriteDB("kukudb.root",0,0);       //
+// TGeoManager::Import("geometry.root");                                     //
+// a.WriteDB("local://$ALICE_ROOT/OCDB", "TRD/Align/Data", 0,0);                  //
+// TGeoManager::Import("geometry.root"); a.WriteGeo("kukugeometry.root");    //
+//                                                                           //
+// b.ReadAscii("kuku.dat");                                                  //
+// TGeoManager::Import("geometry.root"); c.ReadRoot("kuku.root");            //
+// TGeoManager::Import("geometry.root"); d.ReadDB("kukudb.root");            //
+// TGeoManager::Import("kukugeometry.root"); e.ReadCurrentGeo();             //
+//                                                                           //
+// e.PrintSm(4);                                                             //
+// e.PrintCh(120);                                                           // 
+// a.PrintRMS();                                                             //
+// b.PrintRMS();                                                             //
+// e.PrintRMS();                                                             //
+//                                                                           //
+//                                                                           //
+// D.Miskowiec, November 2006                                                //
+//                                                                           //
+///////////////////////////////////////////////////////////////////////////////
+
+#include <iostream>
 #include <fstream>
-#include <string>
 
 #include "TMath.h"
 #include "TFile.h"
 #include "TGeoManager.h"
 #include "TGeoPhysicalNode.h"
 #include "TClonesArray.h"
+#include "TString.h"
+#include "TFitter.h"
+#include "TMinuit.h"
 
 #include "AliLog.h"
 #include "AliAlignObj.h"
-#include "AliAlignObjAngles.h"
+#include "AliAlignObjParams.h"
 #include "AliCDBManager.h"
 #include "AliCDBStorage.h"
 #include "AliCDBMetaData.h"
 #include "AliCDBEntry.h"
-#include "AliCDBId.h"
+#include "AliSurveyObj.h"
+#include "AliSurveyPoint.h"
 
 #include "AliTRDalignment.h"
 
+void trdAlignmentFcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *x, Int_t iflag);
+
 ClassImp(AliTRDalignment)
 
 //_____________________________________________________________________________
 AliTRDalignment::AliTRDalignment() 
   :TObject()
+  ,fComment()
   ,fRan(0)
 {
   //
@@ -74,22 +105,82 @@ AliTRDalignment::AliTRDalignment()
 
   SetZero();
 
+  for (int i=0; i<18; i++) for (int j=0; j<2; j++) for (int k=0; k<2; k++) for (int l=0; l<2; l++) {
+    fSurveyX[i][j][k][l] = 0.0;
+    fSurveyY[i][j][k][l] = 0.0;
+    fSurveyZ[i][j][k][l] = 0.0;
+    fSurveyEX[i][j][k][l] = 0.0;
+    fSurveyEY[i][j][k][l] = 0.0;
+    fSurveyEZ[i][j][k][l] = 0.0;
+  }
+
+  // Initialize the nominal positions of the survey points 
+  // in the local frame of supermodule (where y is the long side, 
+  // z corresponds to the radius in lab, and x to the phi in lab).
+  // Four survey marks are on each z-side of the supermodule. 
+  //               A           B
+  //           ----o-----------o----        x |
+  //           \                   /          |
+  //            \                 /           |
+  //             \               /            |
+  //              \             /             |
+  //               ---o-----o---              -------------->
+  //                  C     D                              y
+  // 
+  // For the purpose of this explanation lets define the origin such that 
+  // the supermodule occupies 0 < x < 77.9 cm. Then the coordinates (x,y) 
+  // are (in cm) 
+  // A (76.2,-30.25)
+  // B (76.2,+30.25)
+  // C ( 2.2,-22.5 )
+  // D ( 2.2,+22.5 )
+  // 
+
+  double x[2] = {22.5,30.25};                   // lab phi, or tracking-y
+  double y[2] = {353.0, -353.0};                // lab z; inc. 2 cm survey target offset
+  double z[2] = {-(77.9/2.0-2.0),77.9/2.0-1.5}; // lab r, or better tracking-x
+
+  for (int j=0; j<2; j++) for (int k=0; k<2; k++) for (int l=0; l<2; l++) {
+    fSurveyX0[j][k][l] = -TMath::Power(-1,l) * x[k];
+    fSurveyY0[j][k][l] = y[j];
+    fSurveyZ0[j][k][l] = z[k];
+  }
+
+  for (int i=0; i<1000; i++) {
+    fIbuffer[i] = 0;
+    fDbuffer[i] = 0.0;
+  }
+
 }
 
 //_____________________________________________________________________________
 AliTRDalignment::AliTRDalignment(const AliTRDalignment& source) 
   :TObject(source)
-  ,fRan(source.fRan) 
+  ,fComment(source.fComment)
+  ,fRan(source.fRan)
 {
   //
   // copy constructor
   //
 
-  for (int i = 0; i <  18; i++) {
-    SetSm(i,source.fSm[i]);
+  for (int i=0; i<18; i++) SetSm(i,source.fSm[i]);
+  for (int i=0; i<540; i++) SetCh(i,source.fCh[i]);
+  for (int i=0; i<18; i++) for (int j=0; j<2; j++) for (int k=0; k<2; k++) for (int l=0; l<2; l++) {
+    fSurveyX[i][j][k][l] = source.fSurveyX[i][j][k][l];
+    fSurveyY[i][j][k][l] = source.fSurveyY[i][j][k][l];
+    fSurveyZ[i][j][k][l] = source.fSurveyZ[i][j][k][l];
+    fSurveyEX[i][j][k][l] = source.fSurveyEX[i][j][k][l];
+    fSurveyEY[i][j][k][l] = source.fSurveyEY[i][j][k][l];
+    fSurveyEZ[i][j][k][l] = source.fSurveyEZ[i][j][k][l];
   }
-  for (int i = 0; i < 540; i++) {
-    SetCh(i,source.fCh[i]);
+  for (int j=0; j<2; j++) for (int k=0; k<2; k++) for (int l=0; l<2; l++) {
+    fSurveyX0[j][k][l] = source.fSurveyX0[j][k][l];
+    fSurveyY0[j][k][l] = source.fSurveyY0[j][k][l];
+    fSurveyZ0[j][k][l] = source.fSurveyZ0[j][k][l];
+  }
+  for (int i=0; i<1000; i++) {
+    fIbuffer[i] = 0;
+    fDbuffer[i] = 0.0;
   }
 
 }
@@ -104,12 +195,40 @@ AliTRDalignment& AliTRDalignment::operator=(const AliTRDalignment &source)
   if (this != &source) {
     for (int i = 0; i <  18; i++) SetSm(i,source.fSm[i]);
     for (int i = 0; i < 540; i++) SetCh(i,source.fCh[i]);
+    for (int i=0; i<18; i++) for (int j=0; j<2; j++) for (int k=0; k<2; k++) for (int l=0; l<2; l++) {
+      fSurveyX[i][j][k][l] = source.fSurveyX[i][j][k][l];
+      fSurveyY[i][j][k][l] = source.fSurveyY[i][j][k][l];
+      fSurveyZ[i][j][k][l] = source.fSurveyZ[i][j][k][l];
+      fSurveyEX[i][j][k][l] = source.fSurveyEX[i][j][k][l];
+      fSurveyEY[i][j][k][l] = source.fSurveyEY[i][j][k][l];
+      fSurveyEZ[i][j][k][l] = source.fSurveyEZ[i][j][k][l];
+    }
+    for (int j=0; j<2; j++) for (int k=0; k<2; k++) for (int l=0; l<2; l++) {
+      fSurveyX0[j][k][l] = source.fSurveyX0[j][k][l];
+      fSurveyY0[j][k][l] = source.fSurveyY0[j][k][l];
+      fSurveyZ0[j][k][l] = source.fSurveyZ0[j][k][l];
+    }
+    fComment = source.fComment;
   }
 
   return *this;
 
 }
 
+//_____________________________________________________________________________
+AliTRDalignment& AliTRDalignment::operator*=(double fac) 
+{
+  //
+  // multiplication operator
+  //
+
+  for (int i = 0; i <  18; i++) for (int j = 0; j < 6; j++) this->fSm[i][j] *= fac;
+  for (int i = 0; i < 540; i++) for (int j = 0; j < 6; j++) this->fCh[i][j] *= fac;
+
+  return *this;
+
+}
+
 //_____________________________________________________________________________
 AliTRDalignment& AliTRDalignment::operator+=(const AliTRDalignment &source) 
 {
@@ -117,16 +236,8 @@ AliTRDalignment& AliTRDalignment::operator+=(const AliTRDalignment &source)
   // addition operator
   //
 
-  for (int i = 0; i <  18; i++) {
-    for (int j = 0; j < 6; j++) {
-      this->fSm[i][j] =+ source.fSm[i][j];
-    }
-  }
-  for (int i = 0; i < 540; i++) {
-    for (int j = 0; j < 6; j++) {
-      this->fCh[i][j] =+ source.fCh[i][j];
-    }
-  }
+  for (int i = 0; i <  18; i++) for (int j = 0; j < 6; j++) this->fSm[i][j] += source.fSm[i][j];
+  for (int i = 0; i < 540; i++) for (int j = 0; j < 6; j++) this->fCh[i][j] += source.fCh[i][j];
 
   return *this;
 
@@ -139,16 +250,8 @@ AliTRDalignment& AliTRDalignment::operator-=(const AliTRDalignment &source)
   // subtraction operator
   //
 
-  for (int i = 0; i <  18; i++) {
-    for (int j = 0; j < 6; j++) {
-      fSm[i][j] -= source.fSm[i][j];
-    }
-  }
-  for (int i = 0; i < 540; i++) {
-    for (int j = 0; j < 6; j++) {
-      fCh[i][j] -= source.fCh[i][j];
-    }
-  }
+  for (int i = 0; i <  18; i++) for (int j = 0; j < 6; j++) fSm[i][j] -= source.fSm[i][j];
+  for (int i = 0; i < 540; i++) for (int j = 0; j < 6; j++) fCh[i][j] -= source.fCh[i][j];
 
   return *this;
 
@@ -163,16 +266,8 @@ Bool_t AliTRDalignment::operator==(const AliTRDalignment &source) const
 
   Bool_t areEqual = 1;
 
-  for (int i = 0; i <  18; i++) {
-    for (int j = 0; j < 6; j++) {
-      areEqual &= (fSm[i][j] == source.fSm[i][j]);
-    }
-  }
-  for (int i = 0; i < 540; i++) {
-    for (int j = 0; j < 6; j++) {
-      areEqual &= (fCh[i][j] == source.fCh[i][j]);
-    }
-  }
+  for (int i = 0; i <  18; i++) for (int j = 0; j < 6; j++) areEqual &= (fSm[i][j] == source.fSm[i][j]);
+  for (int i = 0; i < 540; i++) for (int j = 0; j < 6; j++) areEqual &= (fCh[i][j] == source.fCh[i][j]);
 
   return areEqual;
 
@@ -201,20 +296,18 @@ void AliTRDalignment::SetChZero()
 }
 
 //_____________________________________________________________________________
-void AliTRDalignment::SetSmRandom(Double_t a[6]) 
+void AliTRDalignment::SetSmRandom(double a[6]) 
 {
   //
   // generate random gaussian supermodule data with sigmas a
   //
 
   double x[6];
+  double xmax[6]={999, 0.6, 999, 999, 999, 999};
 
-  for (Int_t i = 0; i < 18; i++) {
-    fRan.Rannor(x[0],x[1]);
-    fRan.Rannor(x[2],x[3]);
-    fRan.Rannor(x[4],x[5]);
-    for (Int_t j = 0; j < 6; j++) {
-      x[j] *= a[j];
+  for (int i = 0; i < 18; i++) {
+    for (int j = 0; j < 6; j++) {
+      do {x[j] = fRan.Gaus(0,a[j]);} while (TMath::Abs(x[j]) > xmax[j]);
     }
     SetSm(i,x);
     //PrintSm(i);
@@ -223,7 +316,7 @@ void AliTRDalignment::SetSmRandom(Double_t a[6])
 }
 
 //_____________________________________________________________________________
-void AliTRDalignment::SetChRandom(Double_t a[6]) 
+void AliTRDalignment::SetChRandom(double a[6]) 
 {
   //
   // generate random gaussian chamber data with sigmas a
@@ -231,13 +324,11 @@ void AliTRDalignment::SetChRandom(Double_t a[6])
 
   double x[6];
 
-  for (Int_t i = 0; i < 540; i++) {
+  for (int i = 0; i < 540; i++) {
     fRan.Rannor(x[0],x[1]);
     fRan.Rannor(x[2],x[3]);
     fRan.Rannor(x[4],x[5]);
-    for (Int_t j = 0; j < 6; j++) {
-      x[j] *= a[j];
-    }
+    for (int j = 0; j < 6; j++) x[j] *= a[j];
     SetCh(i,x);
     //PrintCh(i);
   }
@@ -252,7 +343,7 @@ void AliTRDalignment::SetSmFull()
   // expected from the mechanical precision 
   //
 
-  Double_t a[6];
+  double a[6];
 
   a[0] = 0.3; // phi
   a[1] = 0.3; // z
@@ -273,7 +364,7 @@ void AliTRDalignment::SetChFull()
   // expected from the mechanical precision 
   //
 
-  Double_t a[6];
+  double a[6];
 
   a[0] = 0.1; // phi
   a[1] = 0.1; // z
@@ -307,7 +398,7 @@ void AliTRDalignment::SetChResidual()
   // remaining after full calibration
   //
 
-  Double_t a[6];
+  double a[6];
 
   a[0] = 0.002; // phi
   a[1] = 0.003; // z
@@ -321,7 +412,7 @@ void AliTRDalignment::SetChResidual()
 }
 
 //_____________________________________________________________________________
-void AliTRDalignment::PrintSm(Int_t i, FILE *fp) const 
+void AliTRDalignment::PrintSm(int i, FILE * const fp) const 
 {
   //
   // print the supermodule data
@@ -334,7 +425,7 @@ void AliTRDalignment::PrintSm(Int_t i, FILE *fp) const
 }
 
 //_____________________________________________________________________________
-void AliTRDalignment::PrintCh(Int_t i, FILE *fp) const 
+void AliTRDalignment::PrintCh(int i, FILE * const fp) const 
 {
   //
   // print the chamber data
@@ -347,7 +438,7 @@ void AliTRDalignment::PrintCh(Int_t i, FILE *fp) const
 }
 
 //_____________________________________________________________________________
-void AliTRDalignment::ReadAscii(char *filename) 
+void AliTRDalignment::ReadAscii(const char * const filename) 
 {
   //
   // read the alignment data from ascii file
@@ -360,23 +451,18 @@ void AliTRDalignment::ReadAscii(char *filename)
 
   fstream fi(filename,fstream::in);
   if (!fi) {
-    AliFatal(Form("cannot open input file %s",filename));
+    AliError(Form("cannot open input file %s",filename));
+    return;
   }
 
   // supermodules
 
   for (int i = 0; i < 18; i++) {
     fi>>j>>x[0]>>x[1]>>x[2]>>x[3]>>x[4]>>x[5]>>volid>>syna;
-    if (j != i) {
-      AliError(Form("sm %d expected, %d found",i,j));
-    }
-    if (volid != 0) {
-      AliError(Form("sm %d volume id %d expected, %d found",i,0,volid));
-    }
+    if (j != i) AliError(Form("sm %d expected, %d found",i,j));
+    if (volid != 0) AliError(Form("sm %d volume id %d expected, %d found",i,0,volid));
     std::string symnam = GetSmName(i);
-    if (syna != symnam) {
-      AliError(Form("sm %d name %s expected, %s found",i,symnam.data(),syna.data()));
-    }
+    if (syna != symnam) AliError(Form("sm %d name %s expected, %s found",i,symnam.data(),syna.data()));
     SetSm(i,x);
   }
 
@@ -384,16 +470,10 @@ void AliTRDalignment::ReadAscii(char *filename)
 
   for (int i = 0; i < 540; i++) {
     fi>>j>>x[0]>>x[1]>>x[2]>>x[3]>>x[4]>>x[5]>>volid>>syna;
-    if (j != i) {
-      AliError(Form("ch %d expected, %d found",i,j));
-    }
-    if (volid != GetVoi(i)) {
-      AliError(Form("ch %d volume id %d expected, %d found",i,GetVoi(i),volid));
-    }
+    if (j != i) AliError(Form("ch %d expected, %d found",i,j));
+    if (volid != GetVoi(i)) AliError(Form("ch %d volume id %d expected, %d found",i,GetVoi(i),volid));
     std::string symnam = GetChName(i);
-    if (syna != symnam) {
-      AliError(Form("ch %d name %s expected, %s found",i,symnam.data(),syna.data()));
-    }
+    if (syna != symnam) AliError(Form("ch %d name %s expected, %s found",i,symnam.data(),syna.data()));
     SetCh(i,x);
   }
 
@@ -402,13 +482,98 @@ void AliTRDalignment::ReadAscii(char *filename)
 }
 
 //_____________________________________________________________________________
-void AliTRDalignment::ReadRoot(char *filename) 
+void AliTRDalignment::ReadCurrentGeo() 
+{
+  //
+  // use currently loaded geometry to determine misalignment by comparing 
+  // original and misaligned matrix of the last node
+  // Now, original, does not mean "ideal". It is the matrix before the alignment. 
+  // So, if alignment was applied more than once, the numbers extracted will 
+  // represent just the last alignment. -- check this!
+  //
+
+  TGeoPNEntry *pne;
+  TGeoHMatrix *ideSm[18];  // ideal
+  TGeoHMatrix *misSm[18];  // misaligned
+  for (int i = 0; i < 18; i++) if ((pne = gGeoManager->GetAlignableEntry(GetSmName(i)))) {
+
+    // read misaligned and original matrices
+
+    TGeoPhysicalNode *node = pne->GetPhysicalNode();
+    if (!node) AliError(Form("physical node entry %s has no physical node",GetSmName(i)));
+    if (!node) continue;
+    misSm[i] = new TGeoHMatrix(*node->GetNode(node->GetLevel())->GetMatrix());
+    ideSm[i] = new TGeoHMatrix(*node->GetOriginalMatrix());
+
+    // calculate the local misalignment matrices as inverse misaligned times ideal
+
+    TGeoHMatrix mat(ideSm[i]->Inverse()); 
+    mat.Multiply(misSm[i]);
+    double *tra = mat.GetTranslation();
+    double *rot = mat.GetRotationMatrix();
+    double pars[6];
+    pars[0] = tra[0];
+    pars[1] = tra[1];
+    pars[2] = tra[2];
+    if (TMath::Abs(rot[0])<1e-7 || TMath::Abs(rot[8])<1e-7) AliError("Failed to extract roll-pitch-yall angles!");
+    double raddeg = TMath::RadToDeg();
+    pars[3] = raddeg * TMath::ATan2(-rot[5],rot[8]);
+    pars[4] = raddeg * TMath::ASin(rot[2]);
+    pars[5] = raddeg * TMath::ATan2(-rot[1],rot[0]);
+    SetSm(i,pars);
+
+    // cleanup
+
+    delete ideSm[i];
+    delete misSm[i];
+  }
+
+  TGeoHMatrix *ideCh[540]; // ideal
+  TGeoHMatrix *misCh[540]; // misaligned
+  for (int i = 0; i < 540; i++) if ((pne = gGeoManager->GetAlignableEntry(GetChName(i)))) {
+
+    // read misaligned and original matrices
+
+    TGeoPhysicalNode *node = pne->GetPhysicalNode();
+    if (!node) AliError(Form("physical node entry %s has no physical node",GetChName(i)));
+    if (!node) continue;
+    misCh[i] = new TGeoHMatrix(*node->GetNode(node->GetLevel())->GetMatrix());
+    ideCh[i] = new TGeoHMatrix(*node->GetOriginalMatrix());
+
+    // calculate the local misalignment matrices as inverse misaligned times ideal
+
+    TGeoHMatrix mat(ideCh[i]->Inverse()); 
+    mat.Multiply(misCh[i]);
+    double *tra = mat.GetTranslation();
+    double *rot = mat.GetRotationMatrix();
+    double pars[6];
+    pars[0] = tra[0];
+    pars[1] = tra[1];
+    pars[2] = tra[2];
+    if(TMath::Abs(rot[0])<1e-7 || TMath::Abs(rot[8])<1e-7) {
+      AliError("Failed to extract roll-pitch-yall angles!");
+      return;
+    }
+    double raddeg = TMath::RadToDeg();
+    pars[3] = raddeg * TMath::ATan2(-rot[5],rot[8]);
+    pars[4] = raddeg * TMath::ASin(rot[2]);
+    pars[5] = raddeg * TMath::ATan2(-rot[1],rot[0]);
+    SetCh(i,pars);
+
+    // cleanup
+    delete ideCh[i];
+    delete misCh[i];
+  }
+
+  return;
+
+}
+
+//_____________________________________________________________________________
+void AliTRDalignment::ReadRoot(const char * const filename) 
 {
   //
   // read the alignment data from root file
-  // here I expect a fixed order and number of elements
-  // it would be much better to identify the alignment objects 
-  // one by one and set the parameters of the corresponding sm or ch
   //
 
   TFile fi(filename,"READ");
@@ -418,16 +583,14 @@ void AliTRDalignment::ReadRoot(char *filename)
     ArToNumbers(ar);
     fi.Close();
   } 
-  else {
-    AliError(Form("cannot open input file %s",filename));
-  }
+  else AliError(Form("cannot open input file %s",filename));
 
   return;
 
 }
 
 //_____________________________________________________________________________
-void AliTRDalignment::ReadDB(char *filename) 
+void AliTRDalignment::ReadDB(const char * const filename) 
 {
   //
   // read the alignment data from database file
@@ -438,21 +601,20 @@ void AliTRDalignment::ReadDB(char *filename)
   if (fi.IsOpen()) {
     AliCDBEntry  *e  = (AliCDBEntry *) fi.Get("AliCDBEntry");
     e->PrintMetaData();
+    fComment.SetString(e->GetMetaData()->GetComment());
     TClonesArray *ar = (TClonesArray *) e->GetObject();
     ArToNumbers(ar);
     fi.Close();
   } 
-  else {
-    AliError(Form("cannot open input file %s",filename));
-  }
+  else AliError(Form("cannot open input file %s",filename));
 
   return;
 
 }
 
 //_____________________________________________________________________________
-void AliTRDalignment::ReadDB(char *db, char *path, Int_t run
-                           , Int_t version, Int_t subversion)
+void AliTRDalignment::ReadDB(const char * const db, const char * const path, 
+                            int run, int version, int subversion)
 {
   //
   // read the alignment data from database
@@ -461,151 +623,379 @@ void AliTRDalignment::ReadDB(char *db, char *path, Int_t run
   AliCDBManager *cdb     = AliCDBManager::Instance();
   AliCDBStorage *storLoc = cdb->GetStorage(db);
   AliCDBEntry   *e       = storLoc->Get(path,run,version,subversion);
-  e->PrintMetaData();
-  TClonesArray  *ar      =  (TClonesArray *) e->GetObject();
-  ArToNumbers(ar);
+  if (e) {
+    e->PrintMetaData();
+    fComment.SetString(e->GetMetaData()->GetComment());
+    TClonesArray  *ar      =  (TClonesArray *) e->GetObject();
+    ArToNumbers(ar);
+  }
+}
 
+//_____________________________________________________________________________
+Bool_t AliTRDalignment::DecodeSurveyPointName(TString pna, Int_t &sm, Int_t &iz, 
+                                             Int_t &ir, Int_t &iphi) {
+  // decode the survey point name and extract the sm, z, r and phi indices
+  
+  if (pna(0,6)!="TRD_sm") {
+    AliError(Form("unexpected point name: %s",pna.Data()));
+    return kFALSE;
+  }
+  sm = atoi(pna(6,2).Data()); // supermodule number
+  iz = -1;
+  if (pna(8) == 'a') iz=0; // anticlockwise, positive z
+  if (pna(8) == 'c') iz=1; // clockwise, negative z
+  ir = -1;
+  if (pna(9) == 'l') ir=0; // low radius
+  if (pna(9) == 'h') ir=1; // high radius
+  iphi = -1;
+  if (pna(10) == '0') iphi = 0; // low phi within supermodule
+  if (pna(10) == '1') iphi = 1; // high phi within supermodule
+  if (sm>=0 && sm<18 && iz>=0 && iz<2 && ir>=0 && ir<2 && iphi>=0 && iphi<2) return kTRUE;
+  AliError(Form("cannot decode point name: %s",pna.Data()));
+  return kFALSE;
 }
 
 //_____________________________________________________________________________
-void AliTRDalignment::ReadGeo(char *misaligned
+void AliTRDalignment::ReadSurveyReport(const char * const filename
 {
   //
-  // determine misalignment by comparing original and misaligned matrix 
-  // of the last node on the misaligned_geometry file 
-  // an alternative longer way is in attic.C
+  // Read survey report and store the numbers in fSurveyX, fSurveyY, fSurveyZ, 
+  // and fSurveyE.  Store the survey info in the fComment.
+  // Each supermodule has 8 survey points. The point names look like 
+  // TRD_sm08ah0 and have the following meaning. 
+  //
+  // sm00..17 mean supermodule 0 through 17, following the phi.
+  // Supermodule 00 is between phi=0 and phi=20 degrees.
+  //
+  // a or c denotes the anticlockwise and clockwise end of the supermodule
+  // in z. Clockwise end is where z is negative and where the muon arm sits.
+  //
+  // l or h denote low radius and high radius holes
+  //
+  // 0 or 1 denote the hole at smaller and at larger phi, respectively.
   //
 
-  TGeoHMatrix *ideSm[18];  // ideal
-  TGeoHMatrix *ideCh[540];
-  TGeoHMatrix *misSm[18];  // misaligned
-  TGeoHMatrix *misCh[540];
+  // read the survey file
 
-  // read misaligned and original matrices
+  fstream in(filename,fstream::in);
+  if (!in) {
+    AliError(Form("cannot open input file %s",filename));
+    return;
+  }
 
-  TGeoManager::Import(misaligned);
-  for (int i = 0; i < 18; i++) {
-    TGeoPNEntry      *pne  = gGeoManager->GetAlignableEntry(GetSmName(i));
-    if (!pne) {
-      AliError(Form("no such physical node entry: %s",GetSmName(i))); 
-      return;
-    }
-    TGeoPhysicalNode *node = pne->GetPhysicalNode();
-    if (!node) {
-      AliError(Form("physical node entry %s has no physical node",GetSmName(i))); 
-      return;
-    }
-    misSm[i] = new TGeoHMatrix(*node->GetNode(node->GetLevel())->GetMatrix());
-    ideSm[i] = new TGeoHMatrix(*node->GetOriginalMatrix());
+  // loop through the lines of the file until the beginning of data
+
+  TString title,date,subdetector,url,version,observations,system,units;
+  while (1) {
+    char pee=in.peek();
+    if (pee==EOF) break; 
+    TString line;
+    line.ReadLine(in);
+    if (line.Contains("Title:"))        title.ReadLine(in);
+    if (line.Contains("Date:"))         date.ReadLine(in);
+    if (line.Contains("Subdetector:"))  subdetector.ReadLine(in);
+    if (line.Contains("URL:"))          url.ReadLine(in);
+    if (line.Contains("Version:"))      version.ReadLine(in);
+    if (line.Contains("Observations:")) observations.ReadLine(in);
+    if (line.Contains("System:"))       system.ReadLine(in);
+    if (line.Contains("Units:"))        units.ReadLine(in);
+    if (line.Contains("Data:"))         break;
   }
-  for (int i = 0; i < 540; i++) {
-    TGeoPNEntry      *pne  = gGeoManager->GetAlignableEntry(GetChName(i));
-    if (!pne) {
-      AliError(Form("no such physical node entry: %s",GetChName(i))); 
-      return;
-    }
-    TGeoPhysicalNode *node = pne->GetPhysicalNode();
-    if (!node) {
-      AliError(Form("physical node entry %s has no physical node",GetChName(i))); 
-      return;
-    }
-    misCh[i] = new TGeoHMatrix(*node->GetNode(node->GetLevel())->GetMatrix());
-    ideCh[i] = new TGeoHMatrix(*node->GetOriginalMatrix());
+
+  // check what we found so far (watch out, they have \r at the end)
+
+  std::cout<<"title .........."<<title<<std::endl;
+  std::cout<<"date ..........."<<date<<std::endl;
+  std::cout<<"subdetector ...."<<subdetector<<std::endl;
+  std::cout<<"url ............"<<url<<std::endl;
+  std::cout<<"version ........"<<version<<std::endl;
+  std::cout<<"observations ..."<<observations<<std::endl;
+  std::cout<<"system ........."<<system<<std::endl;
+  std::cout<<"units .........."<<units<<std::endl;
+
+  if (!subdetector.Contains("TRD")) {
+    AliWarning(Form("Not a TRD survey file, subdetector = %s",subdetector.Data()));
+    return;
+  }
+  double tocm = 0; // we want to have it in cm
+  if (units.Contains("mm"))      tocm = 0.1;
+  else if (units.Contains("cm")) tocm = 1.0;
+  else if (units.Contains("m"))  tocm = 100.0;
+  else if (units.Contains("pc")) tocm = 3.24078e-15;
+  else {
+    AliError(Form("unexpected units: %s",units.Data()));
+    return;
+  }
+  if (!system.Contains("ALICEPH")) {
+    AliError(Form("wrong system: %s, should be ALICEPH",system.Data()));
+    return;
   }
 
-  // calculate the local misalignment matrices as inverse misaligned times ideal
+  // scan the rest of the file which should contain list of surveyed points
+  // for every point, decode the point name and store the numbers in the right 
+  // place in the arrays fSurveyX etc.
+
+  while (1) {
+    TString pna; // point name
+    char type, target;
+    double x,y,z,precision;
+    
+    in >> pna >> x >> y >> z >> type >> target >> precision;  
+    if (in.fail()) break;
+    Int_t i,j,k,l;
+    if (DecodeSurveyPointName(pna,i,j,k,l)) {
+      fSurveyX[i][j][k][l] = tocm*x;
+      fSurveyY[i][j][k][l] = tocm*y;
+      fSurveyZ[i][j][k][l] = tocm*z;
+      fSurveyEX[i][j][k][l] = precision/10; // "precision" is supposed to be in mm
+      fSurveyEY[i][j][k][l] = precision/10; // "precision" is supposed to be in mm
+      fSurveyEZ[i][j][k][l] = precision/10; // "precision" is supposed to be in mm
+      // if, at some point, separate precision numbers for x,y,z show up in the 
+      // survey reports the function will fail here
+      printf("decoded %s %02d %d %d %d  %8.2f %8.2f %8.2f %6.2f %6.2f %6.2f\n", 
+            pna.Data(), i, j, k, l,
+            fSurveyX[i][j][k][l], fSurveyY[i][j][k][l], fSurveyZ[i][j][k][l],
+            fSurveyEX[i][j][k][l], fSurveyEY[i][j][k][l], fSurveyEZ[i][j][k][l]);
+    } else AliError(Form("cannot decode point name: %s",pna.Data()));
+  }
+  in.close();
+  TString info = "Survey "+title+" "+date+" "+url+" "+version+" "+observations;
+  info.ReplaceAll("\r","");
+  fComment.SetString(info.Data());
+}
 
-  for (int i = 0; i < 18; i++) {
-    TGeoHMatrix mat(ideSm[i]->Inverse()); 
-    mat.Multiply(misSm[i]);
-    double *tra = mat.GetTranslation();
-    double *rot = mat.GetRotationMatrix();
-    double pars[6];
-    pars[0] = tra[0];
-    pars[1] = tra[1];
-    pars[2] = tra[2];
-    if (TMath::Abs(rot[0])<1e-7 || TMath::Abs(rot[8])<1e-7) {
-      AliError("Failed to extract roll-pitch-yall angles!");
-      return;
-    }
-    double raddeg = TMath::RadToDeg();
-    pars[3] = raddeg * TMath::ATan2(-rot[5],rot[8]);
-    pars[4] = raddeg * TMath::ASin(rot[2]);
-    pars[5] = raddeg * TMath::ATan2(-rot[1],rot[0]);
-    SetSm(i,pars);
+//_____________________________________________________________________________
+void AliTRDalignment::ReadSurveyReport(const AliSurveyObj * const so) 
+{
+  //
+  // Read survey report and store the numbers in fSurveyX, fSurveyY, fSurveyZ, 
+  // and fSurveyE.  Store the survey info in the fComment.
+  // Each supermodule has 8 survey points. The point names look like 
+  // TRD_sm08ah0 and have the following meaning. 
+  //
+  // sm00..17 mean supermodule 0 through 17, following the phi.
+  // Supermodule 00 is between phi=0 and phi=20 degrees.
+  //
+  // a or c denotes the anticlockwise and clockwise end of the supermodule
+  // in z. Clockwise end is where z is negative and where the muon arm sits.
+  //
+  // l or h denote low radius and high radius holes
+  //
+  // 0 or 1 denote the hole at smaller and at larger phi, respectively.
+  //
+
+  // read and process the data from the survey object
+
+  Int_t size = so->GetEntries();
+  printf("-> %d\n", size);
+
+  TString title        = so->GetReportTitle();
+  TString date         = so->GetReportDate();
+  TString subdetector  = so->GetDetector();
+  TString url          = so->GetURL();
+  TString report       = so->GetReportNumber();
+  TString version      = so->GetReportVersion();
+  TString observations = so->GetObservations();
+  TString system       = so->GetCoordSys();
+  TString units        = so->GetUnits();
+
+  // check what we found so far (watch out, they have \r at the end)
+
+  std::cout<<"title .........."<<title<<std::endl;
+  std::cout<<"date ..........."<<date<<std::endl;
+  std::cout<<"subdetector ...."<<subdetector<<std::endl;
+  std::cout<<"url ............"<<url<<std::endl;
+  std::cout<<"version ........"<<version<<std::endl;
+  std::cout<<"observations ..."<<observations<<std::endl;
+  std::cout<<"system ........."<<system<<std::endl;
+  std::cout<<"units .........."<<units<<std::endl;
+
+  if (!subdetector.Contains("TRD")) {
+    AliWarning(Form("Not a TRD survey file, subdetector = %s",subdetector.Data()));
+    return;
+  }
+  double tocm = 0; // we want to have it in cm
+  if (units.Contains("mm"))      tocm = 0.1;
+  else if (units.Contains("cm")) tocm = 1.0;
+  else if (units.Contains("m"))  tocm = 100.0;
+  else if (units.Contains("pc")) tocm = 3.24078e-15;
+  else {
+    AliError(Form("unexpected units: %s",units.Data()));
+    return;
+  }
+  if (!system.Contains("ALICEPH")) {
+    AliError(Form("wrong system: %s, should be ALICEPH",system.Data()));
+    return;
   }
 
-  for (int i = 0; i < 540; i++) {
-    TGeoHMatrix mat(ideCh[i]->Inverse()); 
-    mat.Multiply(misCh[i]);
-    double *tra = mat.GetTranslation();
-    double *rot = mat.GetRotationMatrix();
-    double pars[6];
-    pars[0] = tra[0];
-    pars[1] = tra[1];
-    pars[2] = tra[2];
-    if(TMath::Abs(rot[0])<1e-7 || TMath::Abs(rot[8])<1e-7) {
-      AliError("Failed to extract roll-pitch-yall angles!");
-      return;
-    }
-    double raddeg = TMath::RadToDeg();
-    pars[3] = raddeg * TMath::ATan2(-rot[5],rot[8]);
-    pars[4] = raddeg * TMath::ASin(rot[2]);
-    pars[5] = raddeg * TMath::ATan2(-rot[1],rot[0]);
-    SetCh(i,pars);
+  // for every survey point, decode the point name and store the numbers in 
+  // the right place in the arrays fSurveyX etc.
+
+  TObjArray *points = so->GetData();
+  for (int ip = 0; ip<points->GetEntries(); ++ip) {
+    AliSurveyPoint *po = (AliSurveyPoint *) points->At(ip);
+    TString pna = po->GetPointName();
+    Int_t i,j,k,l;
+    if (DecodeSurveyPointName(pna,i,j,k,l)) {
+      fSurveyX[i][j][k][l] = tocm*po->GetX();
+      fSurveyY[i][j][k][l] = tocm*po->GetY();
+      fSurveyZ[i][j][k][l] = tocm*po->GetZ();
+      fSurveyEX[i][j][k][l] = po->GetPrecisionX()/10; // "precision" is supposed to be in mm
+      fSurveyEY[i][j][k][l] = po->GetPrecisionY()/10;
+      fSurveyEZ[i][j][k][l] = po->GetPrecisionZ()/10;
+      printf("decoded %s %02d %d %d %d  %8.2f %8.2f %8.2f %6.2f %6.2f %6.2f\n", 
+            pna.Data(), i, j, k, l,
+            fSurveyX[i][j][k][l], fSurveyY[i][j][k][l], fSurveyZ[i][j][k][l],
+            fSurveyEX[i][j][k][l], fSurveyEY[i][j][k][l], fSurveyEZ[i][j][k][l]);
+    } else AliError(Form("cannot decode point name: %s",pna.Data()));
   }
 
-  // cleanup
-  for (int i = 0; i <  18; i++) delete ideSm[i];
-  for (int i = 0; i <  18; i++) delete misSm[i];
-  for (int i = 0; i < 540; i++) delete ideCh[i];
-  for (int i = 0; i < 540; i++) delete misCh[i];
+  TString info = "Survey "+title+" "+date+" "+url+" "+report+" "+version+" "+observations;
+  info.ReplaceAll("\r","");
+  fComment.SetString(info.Data());                      
+}
 
-  return;
+//_____________________________________________________________________________
+double AliTRDalignment::SurveyChi2(int i, const double * const a) {
+
+  //
+  // Compare the survey results to the ideal positions of the survey marks
+  // in the local frame of supermodule. When transforming, use the alignment 
+  // parameters a[6]. Return chi-squared.
+  //
 
+  if (!IsGeoLoaded()) return 0;
+  printf("Survey of supermodule %d\n",i);
+  AliAlignObjParams al(GetSmName(i),0,a[0],a[1],a[2],a[3],a[4],a[5],0);
+
+  TGeoPNEntry      *pne  = gGeoManager->GetAlignableEntry(GetSmName(i));
+  if (!pne) AliError(Form("no such physical node entry: %s",GetSmName(i)));
+  TGeoPhysicalNode *node = pne->GetPhysicalNode();
+  if (!node) {
+    AliWarning(Form("physical node entry %s has no physical node; making a new one",GetSmName(i))); 
+    node = gGeoManager->MakeAlignablePN(pne);
+  }
+
+  //  al.ApplyToGeometry();    
+  //  node = pne->GetPhysicalNode(); // changed in the meantime
+  //  TGeoHMatrix *ma = node->GetMatrix();
+
+  // a less destructive method (it does not modify geometry), gives the same result:
+
+  TGeoHMatrix *ma = new TGeoHMatrix();
+  al.GetLocalMatrix(*ma);
+  ma->MultiplyLeft(node->GetMatrix()); // global trafo, modified by a[]
+
+  double chi2=0;
+  printf("              sm   z   r  phi    x (lab phi)  y (lab z)   z (lab r)   all in cm\n");
+  for (int j=0; j<2; j++) for (int k=0; k<2; k++) for (int l=0; l<2; l++) {
+    if (fSurveyEX[i][j][k][l] == 0.0 
+       && fSurveyEY[i][j][k][l] == 0.0 
+       && fSurveyEZ[i][j][k][l] == 0.0) continue; // no data for this survey point
+    double master[3] = {fSurveyX[i][j][k][l],fSurveyY[i][j][k][l],fSurveyZ[i][j][k][l]};
+    double local[3];
+    ma->MasterToLocal(master,local);
+    double dx = local[0]-fSurveyX0[j][k][l];
+    double dy = local[1]-fSurveyY0[j][k][l];
+    double dz = local[2]-fSurveyZ0[j][k][l];
+    chi2 += dx*dx/fSurveyEX[i][j][k][l]/fSurveyEX[i][j][k][l];
+    chi2 += dy*dy/fSurveyEY[i][j][k][l]/fSurveyEY[i][j][k][l];
+    chi2 += dz*dz/fSurveyEZ[i][j][k][l]/fSurveyEZ[i][j][k][l];
+    printf("local survey %3d %3d %3d %3d %12.3f %12.3f %12.3f\n",i,j,k,l,local[0],local[1],local[2]);
+    printf("local ideal                  %12.3f %12.3f %12.3f\n",fSurveyX0[j][k][l],
+          fSurveyY0[j][k][l],fSurveyZ0[j][k][l]);
+    printf("difference                   %12.3f %12.3f %12.3f\n",dx,dy,dz);
+  }
+  printf("chi2 = %.2f\n",chi2);
+  return chi2;
 }
 
 //_____________________________________________________________________________
-void AliTRDalignment::ReadSurveyReport(char *filename) 
-{
-  // read survey report and set the supermodule parameters correspondingly
+void trdAlignmentFcn(int &npar, double *g, double &f, double *par, int iflag) {
 
-  fstream fi(filename,fstream::in);
-  if (!fi) {
-    AliFatal(Form("cannot open input file %s",filename));
+  // 
+  // Standard function as needed by Minuit-like minimization procedures. 
+  // For the set of parameters par calculates and returns chi-squared.
+  //
+
+  // smuggle a C++ object into a C function
+  AliTRDalignment *alignment = (AliTRDalignment*) gMinuit->GetObjectFit(); 
+
+  f = alignment->SurveyChi2(par);
+  if (iflag==3) {}
+  if (npar) {} 
+  if (g) {} // no warnings about unused stuff...
+
+}
+
+//_____________________________________________________________________________
+void AliTRDalignment::SurveyToAlignment(int i, const char * const flag) {
+
+  //
+  // Find the supermodule alignment parameters needed to make the survey 
+  // results coincide with the ideal positions of the survey marks.
+  // The string flag should look like "101000"; the six characters corresponds 
+  // to the six alignment parameters and 0/1 mean that the parameter should 
+  // be fixed/released in the fit. 
+
+  if (strlen(flag)!=6) {
+    AliError(Form("unexpected flag: %s",flag));
+    return;
   }
 
-  // to be continued...
+  printf("Finding alignment matrix for supermodule %d\n",i);
+  fIbuffer[0] = i; // store the sm number in the buffer so minuit can see it
+
+  TFitter fitter(100);
+  gMinuit->SetObjectFit(this);
+  fitter.SetFCN(trdAlignmentFcn);
+  fitter.SetParameter(0,"dx",0,0.5,0,0);
+  fitter.SetParameter(1,"dy",0,0.5,0,0);
+  fitter.SetParameter(2,"dz",0,0.5,0,0);
+  fitter.SetParameter(3,"rx",0,0.1,0,0);
+  fitter.SetParameter(4,"ry",0,0.1,0,0);
+  fitter.SetParameter(5,"rz",0,0.1,0,0);
+
+  for (int j=0; j<6; j++) if (flag[j]=='0') fitter.FixParameter(j);
+
+  double arglist[100];
+  arglist[0] = 2;
+  fitter.ExecuteCommand("SET PRINT", arglist, 1);
+  fitter.ExecuteCommand("SET ERR", arglist, 1);
+  arglist[0]=50;
+  //fitter.ExecuteCommand("SIMPLEX", arglist, 1);
+  fitter.ExecuteCommand("MINIMIZE", arglist, 1);
+  fitter.ExecuteCommand("CALL 3", arglist,0);
+  double a[6];
+  for (int j=0; j<6; j++) a[j] = fitter.GetParameter(j);
+  SetSm(i,a);
+  for (int j=0; j<6; j++) printf("%10.3f ",fitter.GetParameter(j));   
+  printf("\n");
+  for (int j=0; j<6; j++) printf("%10.3f ",fitter.GetParError(j));
+  printf("\n");
 
 }
 
 //_____________________________________________________________________________
-void AliTRDalignment::ReadAny(char *filename) 
+void AliTRDalignment::ReadAny(const char * const filename) 
 {
   //
   // read the alignment data from any kind of file
   //
 
   TString fist(filename);
-  if (fist.EndsWith(".txt")) {
-    ReadAscii(filename);
-  }
-  if (fist.EndsWith(".dat")) {
-    ReadAscii(filename);
-  }
+  if (fist.EndsWith(".txt")) ReadAscii(filename);
+  if (fist.EndsWith(".dat")) ReadAscii(filename);
   if (fist.EndsWith(".root")) {
-    if (fist.Contains("Run")) {
-      ReadDB(filename);
-    }
-    else {
-      ReadRoot(filename);
-    }
+    if (fist.Contains("Run")) ReadDB(filename);
+    else ReadRoot(filename);
   }
 
 }
 
 //_____________________________________________________________________________
-void AliTRDalignment::WriteAscii(char *filename) const
+void AliTRDalignment::WriteAscii(const char * const filename) const
 {
   //
   // store the alignment data on ascii file
@@ -625,13 +1015,13 @@ void AliTRDalignment::WriteAscii(char *filename) const
 }
 
 //_____________________________________________________________________________
-void AliTRDalignment::WriteRoot(char *filename) 
+void AliTRDalignment::WriteRoot(const char * const filename) 
 {
   //
   // store the alignment data on root file
   //
 
-  TClonesArray *ar = new TClonesArray("AliAlignObjAngles",10000);
+  TClonesArray *ar = new TClonesArray("AliAlignObjParams",10000);
   NumbersToAr(ar);
   TFile fo(filename,"RECREATE");
   if (fo.IsOpen()) {
@@ -639,37 +1029,35 @@ void AliTRDalignment::WriteRoot(char *filename)
     fo.WriteObject(ar,"TRDAlignObjs","kSingleKey");
     fo.Close();
   } 
-  else {
-    AliError(Form("cannot open output file %s",filename));
-  }
+  else AliError(Form("cannot open output file %s",filename));
 
   delete ar;
 
 }
 
 //_____________________________________________________________________________
-void AliTRDalignment::WriteDB(char *filename, char *comment, Int_t run0, Int_t run1
+void AliTRDalignment::WriteDB(const char * const filename, int run0, int run1, int ver, int subver
 {
   //
   // dumping on a DB-like file
   //
 
-  TClonesArray   *ar = new TClonesArray("AliAlignObjAngles",10000);
+  TClonesArray   *ar = new TClonesArray("AliAlignObjParams",10000);
   NumbersToAr(ar);
-  char *path = "di1/di2/di3";
+  const Char_t *path = "TRD/Align/Data";
   AliCDBId id(path,run0,run1);
   AliCDBMetaData *md = new AliCDBMetaData();
   md->SetResponsible("Dariusz Miskowiec");
-  md->SetComment(comment);
-  AliCDBEntry    *e  = new AliCDBEntry(ar, id, md);
+  md->SetComment(fComment.GetString().Data());
+  AliCDBEntry *e  = new AliCDBEntry(ar, id, md);
+  e->SetVersion(ver);
+  e->SetSubVersion(subver);
   TFile fi(filename,"RECREATE");
   if (fi.IsOpen()) {
     e->Write();
     fi.Close();
   } 
-  else {
-    AliError(Form("cannot open input file %s",filename));
-  }
+  else AliError(Form("cannot open input file %s",filename));
 
   delete e;
   delete md;
@@ -680,19 +1068,19 @@ void AliTRDalignment::WriteDB(char *filename, char *comment, Int_t run0, Int_t r
 }
 
 //_____________________________________________________________________________
-void AliTRDalignment::WriteDB(char *db, char *path, char *comment, Int_t run0, Int_t run1) 
+void AliTRDalignment::WriteDB(char * const db, const char * const path, int run0, int run1) 
 {
   //
   // store the alignment data in database
   //
 
-  TClonesArray   *ar      = new TClonesArray("AliAlignObjAngles",10000);
+  TClonesArray   *ar      = new TClonesArray("AliAlignObjParams",10000);
   NumbersToAr(ar);
   AliCDBManager  *cdb     = AliCDBManager::Instance();
   AliCDBStorage  *storLoc = cdb->GetStorage(db);
   AliCDBMetaData *md      = new AliCDBMetaData();
   md->SetResponsible("Dariusz Miskowiec");
-  md->SetComment(comment);
+  md->SetComment(fComment.GetString().Data());
   AliCDBId id(path,run0,run1);
   storLoc->Put(ar,id,md);
   md->Delete();
@@ -704,54 +1092,50 @@ void AliTRDalignment::WriteDB(char *db, char *path, char *comment, Int_t run0, I
 void AliTRDalignment::WriteGeo(char *filename) 
 {
   //
-  // apply misalignment to (currently loaded ideal) geometry and store the 
+  // apply misalignment to current geometry and store the 
   // resulting geometry on a root file
   //
 
-  TClonesArray *ar = new TClonesArray("AliAlignObjAngles",10000);
+  TClonesArray *ar = new TClonesArray("AliAlignObjParams",10000);
   NumbersToAr(ar);
-  for (int i = 0; i < ar->GetEntriesFast(); i++) {
-    AliAlignObj *alobj = (AliAlignObj *) ar->UncheckedAt(i);
-    alobj->ApplyToGeometry();
-  }
   delete ar;
   gGeoManager->Export(filename);
 
 }
 
 //_____________________________________________________________________________
-Double_t AliTRDalignment::GetSmRMS(Int_t xyz) const 
+double AliTRDalignment::GetSmRMS(int xyz) const 
 {
   //
   // rms fSm[][xyz]
   //
 
-  Double_t s1 = 0.0;
-  Double_t s2 = 0.0;
+  double s1 = 0.0;
+  double s2 = 0.0;
   for (int i = 0; i < 18; i++) {
     s1 += fSm[i][xyz];
     s2 += fSm[i][xyz]*fSm[i][xyz];
   }
-  Double_t rms2 = s2/18.0 - s1*s1/18.0/18.0;
+  double rms2 = s2/18.0 - s1*s1/18.0/18.0;
 
   return rms2>0 ? sqrt(rms2) : 0.0;
 
 }
 
 //_____________________________________________________________________________
-Double_t AliTRDalignment::GetChRMS(Int_t xyz) const
+double AliTRDalignment::GetChRMS(int xyz) const
 {
   //
   // rms fCh[][xyz]
   //
 
-  Double_t s1 =0.0;
-  Double_t s2 =0.0;
+  double s1 =0.0;
+  double s2 =0.0;
   for (int i = 0; i < 540; i++) {
     s1 += fCh[i][xyz];
     s2 += fCh[i][xyz]*fCh[i][xyz];
   }
-  Double_t rms2 = s2/540.0 - s1*s1/540.0/540.0;
+  double rms2 = s2/540.0 - s1*s1/540.0/540.0;
 
   return rms2>0 ? sqrt(rms2) : 0.0;
 
@@ -782,88 +1166,79 @@ void AliTRDalignment::PrintChRMS() const
 }
 
 //_____________________________________________________________________________
-void AliTRDalignment::ArToNumbers(TClonesArray *ar) 
+void AliTRDalignment::ArToNumbers(TClonesArray * const ar) 
 {
   //
-  // read numbers from the array of AliAlignObj objects and fill fSm and fCh
+  // for each of the alignment objects in array ar extract the six local 
+  // alignment parameters; recognize by name to which supermodule or chamber 
+  // the alignment object pertains; set the respective fSm or fCh
   //
 
-  LoadIdealGeometry();
-  SetZero();
-  double pa[6];
-  for (int i = 0; i <  18; i++) {
+  ar->Sort();
+  if (!IsGeoLoaded()) return;
+  for (int i = 0; i < ar->GetEntries(); i++) {
     AliAlignObj *aao = (AliAlignObj *) ar->At(i);
-    aao->GetLocalPars(pa,pa+3);
-    SetSm(i,pa);
-  }
-  for (int i = 0; i < 540; i++) {
-    AliAlignObj *aao = (AliAlignObj *) ar->At(18+i);
-    aao->GetLocalPars(pa,pa+3);
-    SetCh(i,pa);
+    aao->ApplyToGeometry();
   }
+  SetZero();
+  ReadCurrentGeo();
 
 }
 
 //_____________________________________________________________________________
-void AliTRDalignment::NumbersToAr(TClonesArray *ar) 
+void AliTRDalignment::NumbersToAr(TClonesArray * const ar) 
 {
   //
   // build array of AliAlignObj objects based on fSm and fCh data
+  // at the same time, apply misalignment to the currently loaded geometry
+  // it is important to apply misalignment of supermodules before creating 
+  // alignment objects for chambers
   //
 
-  LoadIdealGeometry();
+  if (!IsGeoLoaded()) return;
   TClonesArray &alobj = *ar;
   int nobj = 0;
   for (int i = 0; i <  18; i++) {      
-      new(alobj[nobj]) AliAlignObjAngles(GetSmName(i)
+      new(alobj[nobj]) AliAlignObjParams(GetSmName(i)
                                         ,0 
                                        ,fSm[i][0],fSm[i][1],fSm[i][2]
                                        ,fSm[i][3],fSm[i][4],fSm[i][5]
                                        ,0);
+    ((AliAlignObj *) alobj[nobj])->ApplyToGeometry();
     nobj++;
   }
 
   for (int i = 0; i < 540; i++) {
-    new(alobj[nobj]) AliAlignObjAngles(GetChName(i)
-                                      ,GetVoi(i)
-                                     ,fCh[i][0],fCh[i][1],fCh[i][2]
-                                     ,fCh[i][3],fCh[i][4],fCh[i][5]
-                                     ,0);
-    nobj++;
+    if (gGeoManager->GetAlignableEntry(GetChName(i))) {
+      new(alobj[nobj]) AliAlignObjParams(GetChName(i)
+                                        ,GetVoi(i)
+                                        ,fCh[i][0],fCh[i][1],fCh[i][2]
+                                        ,fCh[i][3],fCh[i][4],fCh[i][5]
+                                        ,0);
+      ((AliAlignObj *) alobj[nobj])->ApplyToGeometry();
+      nobj++;
+    }
   }
+  AliInfo("current geometry modified");
 
 }
 
 //_____________________________________________________________________________
-void AliTRDalignment::LoadIdealGeometry(char *filename
+int AliTRDalignment::IsGeoLoaded(
 {
   //
-  // load ideal geometry from filename
-  // it is needed for operations on AliAlignObj objects
-  // this needs to be straightened out
-  // particularly, sequences LoadIdealGeometry("file1"); LoadIdealGeometry("file2"); 
-  // do not work as one would naturally expect
+  // check whether a geometry is loaded
+  // issue a warning if geometry is not ideal
   //
 
-  static int attempt = 0; // which reload attempt is it? just to avoid endless loops
-
-  if (!gGeoManager) {
-    TGeoManager::Import(filename);
-  }
-  if (!gGeoManager) {
-    AliFatal(Form("cannot open geometry file %s",filename));
-  }
-  if (gGeoManager->GetListOfPhysicalNodes()->GetEntries()) {
-    if (attempt) {
-      AliFatal(Form("geometry on file %s is not ideal",filename));
-    }
-    AliWarning("current geometry is not ideal - it contains physical nodes");
-    AliWarning(Form("reloading geometry from %s - attempt nr %d",filename,attempt));
-    gGeoManager = 0;
-    attempt++;
-    LoadIdealGeometry(filename);
+  if (gGeoManager) {
+    if (gGeoManager->GetListOfPhysicalNodes()->GetEntries()) AliWarning("current geometry is not ideal");
+    return 1;
+  } else {
+    AliError("first load geometry by calling TGeoManager::Import(filename)");
+    return 0;
   }
 
-  attempt = 0;
-
 }
+
+//_____________________________________________________________________________