]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/AliMUONGeometryTransformer.cxx
Updated list of MUON libraries
[u/mrichter/AliRoot.git] / MUON / AliMUONGeometryTransformer.cxx
index db04b12e2b34680afa285c2f89e85176b25fb790..9665ec067b4540315f13a8c2327269d544b7cc16 100644 (file)
 
 // $Id$
 //
+// ----------------------------
 // Class AliMUONGeometryTransformer
 // ----------------------------
 // Top container class for geometry transformations
-//
 // Author: Ivana Hrivnacova, IPN Orsay
 
-#include <sstream>
-
-#include <Riostream.h>
-#include <TSystem.h>
-
-#include "AliLog.h"
-
 #include "AliMUONGeometryTransformer.h"
 #include "AliMUONGeometryModuleTransformer.h"
 #include "AliMUONGeometryDetElement.h"
-#include "AliMUONGeometryDEIndexing.h"
-#include "AliMUONGeometryStore.h"
 #include "AliMUONGeometryBuilder.h"
 
+#include "AliMpDEManager.h"
+#include "AliMpExMap.h"
+
+#include "AliLog.h"
+#include "AliAlignObjMatrix.h"
+#include "AliAlignObj.h"
+
+#include <Riostream.h>
+#include <TSystem.h>
+#include <TClonesArray.h>
+#include <TGeoManager.h>
+#include <TGeoPhysicalNode.h>
+#include <TFile.h>
+
+#include <sstream>
 
+/// \cond CLASSIMP
 ClassImp(AliMUONGeometryTransformer)
+/// \endcond
  
 //______________________________________________________________________________
-AliMUONGeometryTransformer::AliMUONGeometryTransformer(Bool_t isOwner)
+AliMUONGeometryTransformer::AliMUONGeometryTransformer(Bool_t isOwner,
+                                          const TString& detectorName)
+
   : TObject(),
-    fModuleTransformers(0)
+    fDetectorName(detectorName),
+    fModuleTransformers(0),
+    fMisAlignArray(0)
 {
 /// Standard constructor
 
   // Create array for geometry modules
-  fModuleTransformers = new TObjArray();
+  fModuleTransformers = new TObjArray(100);
   fModuleTransformers->SetOwner(isOwner);
 }
 
 //______________________________________________________________________________
 AliMUONGeometryTransformer::AliMUONGeometryTransformer() 
   : TObject(),
-    fModuleTransformers(0)
+    fDetectorName(),
+    fModuleTransformers(0),
+    fMisAlignArray(0)
 {
 /// Default constructor
 } 
 
-//______________________________________________________________________________
-AliMUONGeometryTransformer::AliMUONGeometryTransformer(
-                                   const AliMUONGeometryTransformer& right) 
-  : TObject(right) 
-{  
-/// Copy constructor (not implemented)
-
-  AliFatal("Copy constructor not provided.");
-}
-
 //______________________________________________________________________________
 AliMUONGeometryTransformer::~AliMUONGeometryTransformer()
 {
 /// Destructor
 
   delete fModuleTransformers;
+  delete fMisAlignArray;
 }
 
-//______________________________________________________________________________
-AliMUONGeometryTransformer& 
-AliMUONGeometryTransformer::operator=(const AliMUONGeometryTransformer& right)
-{
-/// Assignement operator (not implemented)
-
-  // check assignement to self
-  if (this == &right) return *this;
-
-  AliFatal("Assignement operator not provided.");
-    
-  return *this;  
-}    
-
 //
 // private methods
 //
@@ -112,28 +103,13 @@ AliMUONGeometryTransformer::GetModuleTransformerNonConst(
   return (AliMUONGeometryModuleTransformer*) fModuleTransformers->At(index);
 }    
 
-//______________________________________________________________________________
-TString  AliMUONGeometryTransformer::ComposePath(const TString& volName, 
-                                                 Int_t copyNo) const
-{
-// Compose path from given volName and copyNo
-// ---
-
-  TString path(volName);
-  path += ".";
-  path += copyNo;
-  
-  return path;
-}  
-
 //______________________________________________________________________________
 TGeoHMatrix AliMUONGeometryTransformer::GetTransform(
                   Double_t x, Double_t y, Double_t z,
                  Double_t a1, Double_t a2, Double_t a3, 
                  Double_t a4, Double_t a5, Double_t a6) const
 {                
-// Builds the transformation from the given parameters
-// ---
+/// Build the transformation from the given parameters
 
   // Compose transform
   return TGeoCombiTrans(TGeoTranslation(x, y, z), 
@@ -142,24 +118,56 @@ TGeoHMatrix AliMUONGeometryTransformer::GetTransform(
 
 
 //______________________________________________________________________________
-void AliMUONGeometryTransformer::FillData(Int_t moduleId,
+void AliMUONGeometryTransformer::FillModuleVolPath(Int_t moduleId,
+                                           const TString& volPath) 
+{
+/// Create module with the given moduleId and volPath
+
+  // Get/Create geometry module transformer
+  AliMUONGeometryModuleTransformer* moduleTransformer
+    = GetModuleTransformerNonConst(moduleId, false);
+
+  if ( !moduleTransformer ) {
+    moduleTransformer = new AliMUONGeometryModuleTransformer(moduleId);
+    AddModuleTransformer(moduleTransformer);
+  }  
+  moduleTransformer->SetVolumePath(volPath);
+}                 
+  
+//______________________________________________________________________________
+void AliMUONGeometryTransformer::FillDetElemVolPath(Int_t detElemId, 
+                                           const TString& volPath) 
+{
+/// Create detection element with the given detElemId and volPath
+
+  // Module Id
+  Int_t moduleId = AliMpDEManager::GetGeomModuleId(detElemId);
+
+  // Get detection element store
+  AliMpExMap* detElements = 
+    GetModuleTransformer(moduleId)->GetDetElementStore();     
+
+  // Add detection element
+  AliMUONGeometryDetElement* detElement
+    = new AliMUONGeometryDetElement(detElemId, volPath);
+  detElements->Add(detElemId, detElement);
+}                 
+  
+
+//______________________________________________________________________________
+void AliMUONGeometryTransformer::FillModuleTransform(Int_t moduleId,
                   Double_t x, Double_t y, Double_t z,
                  Double_t a1, Double_t a2, Double_t a3,
                  Double_t a4, Double_t a5, Double_t a6) 
 {
-// Fill the transformation of the module.
-// ---
-
-  // Get/Create geometry module parametrisation
-  moduleId--;
-      // Modules numbers in the file are starting from 1
+/// Fill the transformation of the module.
 
   AliMUONGeometryModuleTransformer* moduleTransformer
     = GetModuleTransformerNonConst(moduleId, false);
 
   if ( !moduleTransformer) {
-    moduleTransformer = new AliMUONGeometryModuleTransformer(moduleId);
-    AddModuleTransformer(moduleTransformer);
+    AliErrorStream() 
+      << "Module " << moduleId << " has not volume path defined." << endl;
   }  
       
   // Build the transformation from the parameters
@@ -167,70 +175,99 @@ void AliMUONGeometryTransformer::FillData(Int_t moduleId,
     = GetTransform(x, y, z, a1, a2, a3, a4, a5, a6);
       
   moduleTransformer->SetTransformation(transform);
-
 }                 
   
 //______________________________________________________________________________
-void AliMUONGeometryTransformer::FillData(
-                  Int_t detElemId, const TString& volName, Int_t copyNo,
+void AliMUONGeometryTransformer::FillDetElemTransform(
+                  Int_t detElemId, 
                   Double_t x, Double_t y, Double_t z,
                  Double_t a1, Double_t a2, Double_t a3,
                  Double_t a4, Double_t a5, Double_t a6) 
 {
-// Fill the transformation of the detection element.
-// ---
+/// Fill the transformation of the detection element.
 
   // Module Id
-  Int_t moduleId 
-    = AliMUONGeometryDEIndexing::GetModuleId(detElemId);
-
-  // Compose path
-  TString path = ComposePath(volName, copyNo);
-  
-  // Build the transformation from the parameters
-  TGeoHMatrix localTransform 
-    = GetTransform(x, y, z, a1, a2, a3, a4, a5, a6);
-   
-  // Get detection element store
-  AliMUONGeometryStore* detElements = 
-    GetModuleTransformer(moduleId)->GetDetElementStore();     
+  Int_t moduleId = AliMpDEManager::GetGeomModuleId(detElemId);
 
-  // Add detection element
-  AliMUONGeometryDetElement* detElement
-    = new AliMUONGeometryDetElement(detElemId, path, localTransform);
-  detElements->Add(detElemId, detElement);
-  
-  // Compute global transformation
+  // Get module transformer
   const AliMUONGeometryModuleTransformer* kModuleTransformer
     = GetModuleTransformer(moduleId);
+
   if ( ! kModuleTransformer ) {
-    AliFatal(Form("Module transformation not defined, detElemId %d",
-                  detElemId));
+    AliFatal(Form("Module transformer not defined, detElemId: %d", detElemId));
+    return;  
   }  
 
+  // Get detection element
+  AliMUONGeometryDetElement* detElement 
+    = kModuleTransformer->GetDetElement(detElemId);     
+
+  if ( ! detElement ) {
+    AliFatal(Form("Det element %d has not volume path defined", detElemId));
+    return;  
+  }  
+      
+  // Build the transformation from the parameters
+  TGeoHMatrix localTransform 
+    = GetTransform(x, y, z, a1, a2, a3, a4, a5, a6);
+  detElement->SetLocalTransformation(localTransform); 
+   
+  // Compute global transformation
   TGeoHMatrix globalTransform 
     = AliMUONGeometryBuilder::Multiply( 
                                   *kModuleTransformer->GetTransformation(),
                                  localTransform );
   detElement->SetGlobalTransformation(globalTransform);
 }                 
+
+//______________________________________________________________________________
+Bool_t  
+AliMUONGeometryTransformer::ReadVolPaths(ifstream& in)
+{
+/// Read modules and detection element volume paths from stream
+
+  Int_t id;
+  TString key, volumePath;
+  in >> key;
+  
+  while ( !in.eof() ) {
+
+    in >> id >> volumePath;
+
+    // cout << "id="     << id << "  "
+    //          << "volPath= " << volumePath
+    //  << endl;   
+
+    if ( key == AliMUONGeometryModuleTransformer::GetModuleNamePrefix() ) 
+      FillModuleVolPath(id, volumePath);
+  
+    else if ( key == AliMUONGeometryDetElement::GetDENamePrefix() )
+      FillDetElemVolPath(id, volumePath);
   
+    else {
+      AliFatal(Form("%s key not recognized",  key.Data()));
+      return false;
+    }
+    in >> key;
+  }     
+
+  return true;
+}
+
 //______________________________________________________________________________
-TString  AliMUONGeometryTransformer::ReadData1(ifstream& in)
+TString  AliMUONGeometryTransformer::ReadModuleTransforms(ifstream& in)
 {
-// Reads and fills modules transformations from a file
-// Returns true, if reading finished correctly.
-// ---
+/// Read and fill modules transformations from the stream.
+/// Return true, if reading finished correctly.
 
-  TString key("CH");
-  while ( key == TString("CH") ) {
-    Int_t id, n;
+  TString key(AliMUONGeometryModuleTransformer::GetModuleNamePrefix());
+  while ( key == AliMUONGeometryModuleTransformer::GetModuleNamePrefix() ) {
+    Int_t id;
     Double_t  x, y, z;
     Double_t  a1, a2, a3, a4, a5, a6;
     TString dummy;
   
     in >> id;
-    in >> n;
     in >> dummy;
     in >> x;
     in >> y;
@@ -243,14 +280,14 @@ TString  AliMUONGeometryTransformer::ReadData1(ifstream& in)
     in >> a5; 
     in >> a6; 
 
-    //cout << "id="     << id << "  "
+    //cout << "moduleId="     << id << "  "
     //          << "position= " << x << ", " << y << ", " << z << "  "
     //  << "rotation= " << a1 << ", " << a2 << ", " << a3  << ", "
     //                  << a4 << ", " << a5 << ", " << a6 
     //  << endl;   
         
     // Fill data
-    FillData(id, x, y, z, a1, a2, a3, a4, a5, a6);
+    FillModuleTransform(id, x, y, z, a1, a2, a3, a4, a5, a6);
     
     // Go to next line
     in >> key;
@@ -260,26 +297,21 @@ TString  AliMUONGeometryTransformer::ReadData1(ifstream& in)
 }
 
 //______________________________________________________________________________
-TString  AliMUONGeometryTransformer::ReadData2(ifstream& in)
+TString  AliMUONGeometryTransformer::ReadDetElemTransforms(ifstream& in)
 {
-// Reads detection elements transformations from a file
-// Returns true, if reading finished correctly.
-// ---
+/// Read detection elements transformations from the stream.
+/// Return true, if reading finished correctly.
 
-  TString key("DE");
-  while ( key == TString("DE") ) {
+  TString key(AliMUONGeometryDetElement::GetDENamePrefix());
+  while ( key == AliMUONGeometryDetElement::GetDENamePrefix() ) {
 
     // Input data
     Int_t detElemId;
-    TString   volumeName;
-    Int_t     copyNo;
     Double_t  x, y, z;
     Double_t  a1, a2, a3, a4, a5, a6;
     TString dummy;
   
     in >> detElemId;
-    in >> volumeName;
-    in >> copyNo;
     in >> dummy;
     in >> x;
     in >> y;
@@ -293,15 +325,13 @@ TString  AliMUONGeometryTransformer::ReadData2(ifstream& in)
     in >> a6; 
 
     //cout << "detElemId=" << detElemId << "  "
-    //     << "volume=" << volumeName << "  "
-    //     << "copyNo=" << copyNo << "  "
     //     << "position= " << x << ", " << y << ", " << z << "  "
     //     << "rotation= " << a1 << ", " << a2 << ", " << a3  << ", "
-    //                  << a4 << ", " << a5 << ", " << a6 
+    //                    << a4 << ", " << a5 << ", " << a6 
     //     << endl;   
         
     // Fill data
-    FillData(detElemId, volumeName, copyNo, x, y, z, a1, a2, a3, a4, a5, a6);   
+    FillDetElemTransform(detElemId, x, y, z, a1, a2, a3, a4, a5, a6);   
     
     // Go to next line
     in >> key;
@@ -310,12 +340,159 @@ TString  AliMUONGeometryTransformer::ReadData2(ifstream& in)
   return key;
 }
 
+//______________________________________________________________________________
+Bool_t  
+AliMUONGeometryTransformer::LoadTransforms(TGeoManager* tgeoManager)
+{
+/// Load transformations for defined modules and detection elements
+/// from the root file
+
+  if ( !tgeoManager) {
+    AliFatal("No TGeoManager defined.");
+    return false;
+  }   
+
+  for (Int_t i=0; i<fModuleTransformers->GetEntriesFast(); i++) {
+    AliMUONGeometryModuleTransformer* moduleTransformer 
+      = (AliMUONGeometryModuleTransformer*)fModuleTransformers->At(i);
+
+    // Module path
+    TString path = moduleTransformer->GetVolumePath();
+    
+    // Make physical node
+    TGeoPhysicalNode* moduleNode = tgeoManager->MakePhysicalNode(path);
+    if ( ! moduleNode ) {
+      AliErrorStream() 
+        << "Module id: " << moduleTransformer->GetModuleId()
+       << " volume path: " << path << " not found in geometry." << endl;
+       return false;
+    }   
+    
+    // Set matrix from physical node
+    TGeoHMatrix matrix = *moduleNode->GetMatrix();
+    moduleTransformer->SetTransformation(matrix);
+    
+    // Loop over detection elements
+    AliMpExMap* detElements = moduleTransformer->GetDetElementStore();    
+   
+    for (Int_t j=0; j<detElements->GetSize(); j++) {
+      AliMUONGeometryDetElement* detElement
+        = (AliMUONGeometryDetElement*)detElements->GetObject(j);
+
+      // Det element path
+      TString dePath = detElement->GetVolumePath();
+
+      // Make physical node
+      TGeoPhysicalNode* deNode = tgeoManager->MakePhysicalNode(dePath);
+      if ( ! deNode ) {
+        AliErrorStream() 
+          << "Det element id: " << detElement->GetId()
+         << " volume path: " << path << " not found in geometry." << endl;
+         return false;
+      }        
+        
+      // Set global matrix from physical node
+      TGeoHMatrix globalMatrix = *deNode->GetMatrix();
+      detElement->SetGlobalTransformation(globalMatrix);
+
+      // Set local matrix
+      TGeoHMatrix localMatrix = 
+        AliMUONGeometryBuilder::Multiply(
+          matrix.Inverse(), globalMatrix );
+      detElement->SetLocalTransformation(localMatrix);
+    }  
+  } 
+  return true;    
+}  
+
+//______________________________________________________________________________
+Bool_t  
+AliMUONGeometryTransformer::ReadVolPaths(const TString& fileName)
+{
+/// Read detection element volume paths from a file.
+/// Return true, if reading finished correctly.
+
+  // File path
+  TString filePath = gSystem->Getenv("ALICE_ROOT");
+  filePath += "/MUON/data/";
+  filePath += fileName;
+  
+  // Open input file
+  ifstream in(filePath, ios::in);
+  if (!in) {
+    cerr << filePath << endl;  
+    AliFatal("File not found.");
+    return false;
+  }
+
+  ReadVolPaths(in);
+  return true;
+}
+
+//______________________________________________________________________________
+Bool_t  
+AliMUONGeometryTransformer::ReadTransformations(const TString& fileName)
+{
+/// Read transformations from a file.
+/// Return true, if reading finished correctly.
+
+  // File path
+  TString filePath = gSystem->Getenv("ALICE_ROOT");
+  filePath += "/MUON/data/";
+  filePath += fileName;
+  
+  // Open input file
+  ifstream in(filePath, ios::in);
+  if (!in) {
+    cerr << filePath << endl;  
+    AliFatal("File not found.");
+    return false;
+  }
+
+  TString key;
+  in >> key;
+  while ( !in.eof() ) {
+    if ( key == AliMUONGeometryModuleTransformer::GetModuleNamePrefix() ) 
+      key = ReadModuleTransforms(in);
+    else if ( key == AliMUONGeometryDetElement::GetDENamePrefix() )
+      key = ReadDetElemTransforms(in);
+    else {
+      AliFatal(Form("%s key not recognized",  key.Data()));
+      return false;
+    }
+  }     
+
+  return true;
+}
+
+//______________________________________________________________________________
+Bool_t  
+AliMUONGeometryTransformer::ReadTransformations2(const TString& fileName)
+{
+/// Read transformations from root geometry file.
+/// Return true, if reading finished correctly.
+
+  // File path
+  TString filePath = gSystem->Getenv("ALICE_ROOT");
+  filePath += "/MUON/data/";
+  filePath += fileName;
+  
+  // Load root geometry
+  TGeoManager* tgeoManager = gGeoManager;
+  if (!tgeoManager)
+    tgeoManager = TGeoManager::Import(fileName);
+
+  // Retrieve matrices
+  LoadTransforms(tgeoManager);     
+  
+  return true;
+}
+
 //______________________________________________________________________________
 void AliMUONGeometryTransformer::WriteTransform(ofstream& out,
-                                   const TGeoCombiTrans* transform) const
+                                   const TGeoMatrix* transform) const
 {
-// Writes the transformations 
-// ---
+/// Write given transformation 
 
   out << "   pos: ";
   const Double_t* xyz = transform->GetTranslation();
@@ -339,20 +516,60 @@ void AliMUONGeometryTransformer::WriteTransform(ofstream& out,
 }
 
 //______________________________________________________________________________
-void AliMUONGeometryTransformer::WriteData1(ofstream& out) const
+void AliMUONGeometryTransformer::WriteModuleVolPaths(ofstream& out) const
 {
-// Writes modules transformations
-// ---
+/// Write module volume paths for all module transformers
 
   for (Int_t i=0; i<fModuleTransformers->GetEntriesFast(); i++) {
     AliMUONGeometryModuleTransformer* moduleTransformer 
       = (AliMUONGeometryModuleTransformer*)fModuleTransformers->At(i);
-    const TGeoCombiTrans* transform 
+
+    // Write data on out
+    out << AliMUONGeometryModuleTransformer::GetModuleNamePrefix() << " "
+        << setw(4) << moduleTransformer->GetModuleId() << "    " 
+        << moduleTransformer->GetVolumePath() << endl;
+  }     
+  out << endl;                 
+}
+
+//______________________________________________________________________________
+void AliMUONGeometryTransformer::WriteDetElemVolPaths(ofstream& out) const
+{
+/// Write detection element volume paths for all detection elements in all 
+/// module transformers
+
+  for (Int_t i=0; i<fModuleTransformers->GetEntriesFast(); i++) {
+    AliMUONGeometryModuleTransformer* moduleTransformer 
+      = (AliMUONGeometryModuleTransformer*)fModuleTransformers->At(i);
+    AliMpExMap* detElements = moduleTransformer->GetDetElementStore();    
+
+    for (Int_t j=0; j<detElements->GetSize(); j++) {
+      AliMUONGeometryDetElement* detElement
+        = (AliMUONGeometryDetElement*)detElements->GetObject(j);
+       
+      // Write data on out
+      out << AliMUONGeometryDetElement::GetDENamePrefix() << " " 
+          << setw(4) << detElement->GetId() << "    " 
+          << detElement->GetVolumePath() << endl;
+    }
+    out << endl;                       
+  }     
+}
+
+//______________________________________________________________________________
+void AliMUONGeometryTransformer::WriteModuleTransforms(ofstream& out) const
+{
+/// Write module transformations for all module transformers
+
+  for (Int_t i=0; i<fModuleTransformers->GetEntriesFast(); i++) {
+    AliMUONGeometryModuleTransformer* moduleTransformer 
+      = (AliMUONGeometryModuleTransformer*)fModuleTransformers->At(i);
+    const TGeoMatrix* transform 
       = moduleTransformer->GetTransformation();    
 
-    out << "CH " 
-        << setw(4) << moduleTransformer->GetModuleId() + 1 << "  "
-        << setw(4) << moduleTransformer->GetDetElementStore()->GetNofEntries() << "  ";
+    // Write data on out
+    out << AliMUONGeometryModuleTransformer::GetModuleNamePrefix() << " " 
+        << setw(4) << moduleTransformer->GetModuleId();
     
     WriteTransform(out, transform);
   }
@@ -360,38 +577,25 @@ void AliMUONGeometryTransformer::WriteData1(ofstream& out) const
 }
 
 //______________________________________________________________________________
-void AliMUONGeometryTransformer::WriteData2(ofstream& out) const
+void AliMUONGeometryTransformer::WriteDetElemTransforms(ofstream& out) const
 {
-// Writes detection elements (envelopes) transformations
-// ---
-
+/// Write detection element transformations for all detection elements in all 
+/// module transformers
 
   for (Int_t i=0; i<fModuleTransformers->GetEntriesFast(); i++) {
     AliMUONGeometryModuleTransformer* moduleTransformer 
       = (AliMUONGeometryModuleTransformer*)fModuleTransformers->At(i);
-    AliMUONGeometryStore* detElements 
-      = moduleTransformer->GetDetElementStore();    
+    AliMpExMap* detElements = moduleTransformer->GetDetElementStore();    
 
-    for (Int_t j=0; j<detElements->GetNofEntries(); j++) {
+    for (Int_t j=0; j<detElements->GetSize(); j++) {
       AliMUONGeometryDetElement* detElement
-        = (AliMUONGeometryDetElement*)detElements->GetEntry(j);
-      const TGeoCombiTrans* transform 
+        = (AliMUONGeometryDetElement*)detElements->GetObject(j);
+      const TGeoMatrix* transform 
         = detElement->GetLocalTransformation(); 
        
-      // Get volume name & copy number from aligned volume
-      string volCopyNo = detElement->GetAlignedVolume().Data();
-      std::string::size_type first = volCopyNo.find('.');
-      std::string volName = volCopyNo.substr(0, first);
-      std::string copyNoStr = volCopyNo.substr(first+1, volCopyNo.length());
-      std::istringstream in(copyNoStr);
-      Int_t copyNo;
-      in >> copyNo;
-      
       // Write data on out
-      out << "DE " 
-          << setw(4) << detElement->GetId() << "    " 
-          << volName << " "
-         << setw(4) << copyNo;
+      out << AliMUONGeometryDetElement::GetDENamePrefix() << " " 
+          << setw(4) << detElement->GetId();
      
       WriteTransform(out, transform);
     }
@@ -399,54 +603,141 @@ void AliMUONGeometryTransformer::WriteData2(ofstream& out) const
   }     
 }
 
+//______________________________________________________________________________
+TString AliMUONGeometryTransformer::GetModuleSymName(Int_t moduleId) const
+{
+/// Return the module symbolic name (use for alignment)
+
+  const AliMUONGeometryModuleTransformer* kTransformer 
+    = GetModuleTransformer(moduleId);
+  if ( ! kTransformer ) {
+    AliErrorStream() << "Module " << moduleId << " not found." << endl; 
+    return "";
+  }   
+  
+  return "/" + fDetectorName + "/" + kTransformer->GetModuleName();
+}  
+
+//______________________________________________________________________________
+TString AliMUONGeometryTransformer::GetDESymName(Int_t detElemId) const
+{
+/// Return the detection element symbolic name (used for alignment)
+
+  const AliMUONGeometryDetElement* kDetElement 
+    = GetDetElement(detElemId);
+  if ( ! kDetElement ) {
+    AliErrorStream() << "Det element " << detElemId << " not found." << endl; 
+    return "";
+  }   
+  
+  // Module Id
+  Int_t moduleId = AliMpDEManager::GetGeomModuleId(detElemId);
+
+  return GetModuleSymName(moduleId) + "/" + kDetElement->GetDEName();
+}  
+
 //
 // public functions
 //
 
 //______________________________________________________________________________
 Bool_t  
-AliMUONGeometryTransformer::ReadTransformations(const TString& fileName)
+AliMUONGeometryTransformer::ReadGeometryData(
+                                const TString& volPathFileName,
+                                const TString& transformFileName)
+{
+/// Read geometry data from given files;
+/// if transformFileName has ".root" extension, the transformations
+/// are loaded from root geometry file, otherwise ASCII file
+/// format is supposed
+
+  Bool_t result1 = ReadVolPaths(volPathFileName);
+
+  // Get file extension
+  std::string fileName = transformFileName.Data();
+  std::string rootExt = fileName.substr(fileName.size()-5, fileName.size());
+  Bool_t result2;
+  if ( rootExt != ".root" ) 
+    result2 = ReadTransformations(transformFileName);
+  else   
+    result2 = ReadTransformations2(transformFileName);
+  
+  return result1 && result2;
+}  
+
+//______________________________________________________________________________
+Bool_t  
+AliMUONGeometryTransformer::ReadGeometryData(
+                                const TString& volPathFileName,
+                                TGeoManager* tgeoManager)
+{
+/// Load geometry data from root geometry using defined
+/// volume paths from file
+
+  Bool_t result1 = ReadVolPaths(volPathFileName);
+
+  Bool_t result2 = LoadTransforms(tgeoManager);
+  
+  return result1 && result2;
+}  
+
+//______________________________________________________________________________
+Bool_t  
+AliMUONGeometryTransformer::WriteGeometryData(
+                                 const TString& volPathFileName,
+                                 const TString& transformFileName,
+                                const TString& misalignFileName) const
+{
+/// Write geometry data into given files
+
+  Bool_t result1 = WriteVolumePaths(volPathFileName);
+  Bool_t result2 = WriteTransformations(transformFileName);
+  
+  Bool_t result3 = true;
+  if ( misalignFileName != "" )
+    result3 = WriteMisAlignmentData(misalignFileName);
+  
+  return result1 && result2 && result3;
+}
+                                
+//______________________________________________________________________________
+Bool_t  
+AliMUONGeometryTransformer::WriteVolumePaths(const TString& fileName) const
 {
-// Reads transformations from a file
-// Returns true, if reading finished correctly.
-// ---
+/// Write volume paths for modules and detection element volumes into a file.
+/// Return true, if writing finished correctly.
+
+  // No writing
+  // if builder is not associated with any geometry module
+  if (fModuleTransformers->GetEntriesFast() == 0) return false;
 
   // File path
   TString filePath = gSystem->Getenv("ALICE_ROOT");
   filePath += "/MUON/data/";
   filePath += fileName;
   
-  // Open input file
-  ifstream in(filePath, ios::in);
-  if (!in) {
+  // Open output file
+  ofstream out(filePath, ios::out);
+  if (!out) {
     cerr << filePath << endl;  
-    AliFatal("File not found.");
+    AliError("File not found.");
     return false;
   }
-
-  TString key;
-  in >> key;
-  while ( !in.eof() ) {
-    if (key == TString("CH")) 
-      key = ReadData1(in);
-    else if (key == TString("DE"))
-      key = ReadData2(in);
-    else {
-      AliFatal(Form("%s key not recognized",  key.Data()));
-      return false;
-    }
-  }     
-
+#if !defined (__DECCXX)
+  out.setf(std::ios::fixed);
+#endif
+  WriteModuleVolPaths(out);
+  WriteDetElemVolPaths(out);
+  
   return true;
-}
+}  
 
 //______________________________________________________________________________
 Bool_t  
 AliMUONGeometryTransformer::WriteTransformations(const TString& fileName) const
 {
-// Writes transformations into a file
-// Returns true, if writing finished correctly.
-// ---
+/// Write transformations into a file.
+/// Return true, if writing finished correctly.
 
   // No writing
   // if builder is not associated with any geometry module
@@ -457,7 +748,7 @@ AliMUONGeometryTransformer::WriteTransformations(const TString& fileName) const
   filePath += "/MUON/data/";
   filePath += fileName;
   
-  // Open input file
+  // Open output file
   ofstream out(filePath, ios::out);
   if (!out) {
     cerr << filePath << endl;  
@@ -467,8 +758,42 @@ AliMUONGeometryTransformer::WriteTransformations(const TString& fileName) const
 #if !defined (__DECCXX)
   out.setf(std::ios::fixed);
 #endif
-  WriteData1(out);
-  WriteData2(out);
+  WriteModuleTransforms(out);
+  WriteDetElemTransforms(out);
+  
+  return true;
+}  
+
+//______________________________________________________________________________
+Bool_t  
+AliMUONGeometryTransformer::WriteMisAlignmentData(const TString& fileName) const
+{
+/// Write misalignment data into a file
+/// Return true, if writing finished correctly.
+
+  // No writing
+  // if builder is not associated with any geometry module
+  if ( fModuleTransformers->GetEntriesFast() == 0 ) {
+    AliWarningStream() << "No geometry modules defined." << endl;
+    return false;
+  }  
+  
+  // No writing
+  // if builder has no mis-alignment data
+  if ( ! fMisAlignArray ) {
+    AliWarningStream() << "No mis-alignment data defined." << endl;
+    return false;
+  }  
+
+  // File path
+  TString filePath = gSystem->Getenv("ALICE_ROOT");
+  filePath += "/MUON/data/";
+  filePath += fileName;
+  
+  // Write mis-alignment data in the root file
+  TFile file(fileName.Data(), "RECREATE");
+  fMisAlignArray->Write();
+  file.Close();
   
   return true;
 }  
@@ -477,12 +802,180 @@ AliMUONGeometryTransformer::WriteTransformations(const TString& fileName) const
 void AliMUONGeometryTransformer::AddModuleTransformer(
                           AliMUONGeometryModuleTransformer* moduleTransformer)
 {
-/// Add the geometrymodule to the array
+/// Add the module transformer to the array
+
+  // Expand the size if not sufficient
+  Int_t moduleId = moduleTransformer->GetModuleId();
+  if (  moduleId >= fModuleTransformers->GetSize() )
+    fModuleTransformers->Expand(moduleId+1);
+
+  fModuleTransformers->AddAt(moduleTransformer, moduleId);
+}
+
+//_____________________________________________________________________________
+void  AliMUONGeometryTransformer::AddMisAlignModule(Int_t moduleId, 
+                                              const TGeoHMatrix& matrix)
+{
+/// Build AliAlignObjMatrix with module ID, its volumePath
+/// and the given delta transformation matrix                                        
+
+  if ( ! fMisAlignArray )
+    fMisAlignArray = new TClonesArray("AliAlignObjMatrix", 200);
+    
+  const AliMUONGeometryModuleTransformer* kTransformer 
+    = GetModuleTransformer(moduleId);
+  if ( ! kTransformer ) {
+    AliErrorStream() << "Module " << moduleId << " not found." << endl; 
+    return;
+  }   
+  
+  // Get unique align object ID
+  Int_t volId = AliAlignObj::LayerToVolUID(AliAlignObj::kMUON, moduleId); 
+
+  // Create mis align matrix
+  TClonesArray& refArray =*fMisAlignArray;
+  Int_t pos = fMisAlignArray->GetEntriesFast();
+  new (refArray[pos]) AliAlignObjMatrix(GetModuleSymName(moduleId), volId, 
+                                       const_cast<TGeoHMatrix&>(matrix),kTRUE);
+}
+
+//_____________________________________________________________________________
+void  AliMUONGeometryTransformer::AddMisAlignDetElement(Int_t detElemId, 
+                                              const TGeoHMatrix& matrix)
+{
+/// Build AliAlignObjMatrix with detection element ID, its volumePath
+/// and the given delta transformation matrix                                        
+
+  if ( ! fMisAlignArray )
+    fMisAlignArray = new TClonesArray("AliAlignObjMatrix", 200);
 
-  fModuleTransformers->AddAt(moduleTransformer, 
-                             moduleTransformer->GetModuleId());
+  const AliMUONGeometryDetElement* kDetElement 
+    = GetDetElement(detElemId);
+
+  if ( ! kDetElement ) {
+    AliErrorStream() << "Det element " << detElemId << " not found." << endl; 
+    return;
+  }   
+  
+  // Get unique align object ID
+  Int_t volId = AliAlignObj::LayerToVolUID(AliAlignObj::kMUON, detElemId); 
+
+  // Create mis align matrix
+  TClonesArray& refArray =*fMisAlignArray;
+  Int_t pos = fMisAlignArray->GetEntriesFast();
+  new(refArray[pos]) AliAlignObjMatrix(GetDESymName(detElemId), volId, 
+                                      const_cast<TGeoHMatrix&>(matrix),kTRUE);
 }
 
+//_____________________________________________________________________________
+void AliMUONGeometryTransformer::AddAlignableVolumes() const
+{
+/// Set symbolic names and matrices to alignable objects to TGeo
+
+  if ( ! gGeoManager ) {
+    AliWarning("TGeoManager not defined.");
+    return;
+  }  
+
+  // Modules 
+  for (Int_t i=0; i<fModuleTransformers->GetEntriesFast(); i++) {
+    AliMUONGeometryModuleTransformer* module 
+      = (AliMUONGeometryModuleTransformer*)fModuleTransformers->At(i);
+
+    // Set module symbolic name
+    TGeoPNEntry* pnEntry
+      = gGeoManager->SetAlignableEntry(GetModuleSymName(module->GetModuleId()), 
+                                       module->GetVolumePath());
+    // Set module matrix
+    pnEntry->SetMatrix(new TGeoHMatrix(*module->GetTransformation()));  
+       // the matrix will be deleted via TGeoManager                                    
+
+    //cout << "Module sym name: " << GetModuleSymName(module->GetModuleId()) 
+    //     << "  volPath: " << module->GetVolumePath() << endl;
+
+    // Detection elements
+    AliMpExMap* detElements = module->GetDetElementStore();    
+
+    for (Int_t j=0; j<detElements->GetSize(); j++) {
+      AliMUONGeometryDetElement* detElement
+        = (AliMUONGeometryDetElement*)detElements->GetObject(j);
+       
+      // Set detection element symbolic name
+      TGeoPNEntry* pnEntry
+        = gGeoManager->SetAlignableEntry(GetDESymName(detElement->GetId()), 
+                                         detElement->GetVolumePath());
+      // Set detection element matrix
+      pnEntry->SetMatrix(new TGeoHMatrix(*detElement->GetGlobalTransformation()));                                      
+       // the matrix will be deleted via TGeoManager                                    
+
+      //cout << "DE name: " << GetDESymName(detElement->GetId()) 
+      //     << "  volPath: " << detElement->GetVolumePath() << endl;
+    }  
+  }     
+}           
+    
+//_____________________________________________________________________________
+TClonesArray* AliMUONGeometryTransformer::CreateZeroAlignmentData() const
+{
+/// Create array with zero alignment data
+                              
+  // Create array for zero-alignment objects
+  TClonesArray* array = new TClonesArray("AliAlignObjMatrix", 200);
+  TClonesArray& refArray =*array;
+  array->SetOwner(true);
+
+  // Identity matrix
+  TGeoHMatrix matrix;
+
+  // Modules 
+  for (Int_t i=0; i<fModuleTransformers->GetEntriesFast(); i++) {
+    AliMUONGeometryModuleTransformer* module 
+      = (AliMUONGeometryModuleTransformer*)fModuleTransformers->At(i);
+
+    Int_t moduleId = module->GetModuleId();
+  
+    // Align object ID
+    Int_t volId = AliAlignObj::LayerToVolUID(AliAlignObj::kMUON, moduleId); 
+
+    // Create mis align matrix
+    Int_t pos = array->GetEntriesFast();
+    new (refArray[pos]) AliAlignObjMatrix(GetModuleSymName(moduleId), volId, matrix, kTRUE);
+  }     
+
+  // Detection elements
+  for (Int_t i=0; i<fModuleTransformers->GetEntriesFast(); i++) {
+    AliMUONGeometryModuleTransformer* moduleTransformer 
+      = (AliMUONGeometryModuleTransformer*)fModuleTransformers->At(i);
+    AliMpExMap* detElements = moduleTransformer->GetDetElementStore();    
+
+    for (Int_t j=0; j<detElements->GetSize(); j++) {
+      AliMUONGeometryDetElement* detElement
+        = (AliMUONGeometryDetElement*)detElements->GetObject(j);
+       
+      Int_t detElemId = detElement->GetId();
+  
+      // Align object ID
+      Int_t volId = AliAlignObj::LayerToVolUID(AliAlignObj::kMUON, detElemId); 
+
+      // Create mis align matrix
+      Int_t pos = array->GetEntriesFast();
+      new (refArray[pos]) AliAlignObjMatrix(GetDESymName(detElemId), volId, matrix, kTRUE);
+    }
+  }
+  
+  return array;
+}       
+
+//_____________________________________________________________________________
+void AliMUONGeometryTransformer::ClearMisAlignmentData()
+{
+/// Clear the array of misalignment data
+
+  if ( ! fMisAlignArray ) return;
+  
+  fMisAlignArray->Delete();
+}  
+                              
 //_____________________________________________________________________________
 void AliMUONGeometryTransformer::Global2Local(Int_t detElemId,
                  Float_t xg, Float_t yg, Float_t zg, 
@@ -491,7 +984,7 @@ void AliMUONGeometryTransformer::Global2Local(Int_t detElemId,
 /// Transform point from the global reference frame (ALIC)
 /// to the local reference frame of the detection element specified
 /// by detElemId.
-   
+
   const AliMUONGeometryModuleTransformer* kTransformer 
     = GetModuleTransformerByDEId(detElemId);
   
@@ -507,7 +1000,7 @@ void AliMUONGeometryTransformer::Global2Local(Int_t detElemId,
 /// Transform point from the global reference frame (ALIC)
 /// to the local reference frame of the detection element specified
 /// by detElemId.
-   
+
   const AliMUONGeometryModuleTransformer* kTransformer 
     = GetModuleTransformerByDEId(detElemId);
   
@@ -549,7 +1042,7 @@ void AliMUONGeometryTransformer::Local2Global(Int_t detElemId,
 const AliMUONGeometryModuleTransformer* 
 AliMUONGeometryTransformer::GetModuleTransformer(Int_t index, Bool_t warn) const
 {
-/// Return the geometry module specified by index
+/// Return the geometry module transformer specified by index
 
   return GetModuleTransformerNonConst(index, warn);
 }    
@@ -559,14 +1052,28 @@ const AliMUONGeometryModuleTransformer*
 AliMUONGeometryTransformer::GetModuleTransformerByDEId(Int_t detElemId, 
                                                        Bool_t warn) const
 {
-/// Return the geometry module specified by index
+/// Return the geometry module transformer specified by detection element ID
 
   // Get module index
-  Int_t index = AliMUONGeometryDEIndexing::GetModuleId(detElemId);
+  Int_t index = AliMpDEManager::GetGeomModuleId(detElemId);
 
   return GetModuleTransformer(index, warn);
 }    
 
+//_____________________________________________________________________________
+const AliMUONGeometryDetElement* 
+AliMUONGeometryTransformer::GetDetElement(Int_t detElemId, Bool_t warn) const
+{
+/// Return detection element with given detElemId                             
+
+  const AliMUONGeometryModuleTransformer* kTransformer 
+    = GetModuleTransformerByDEId(detElemId, warn);
+    
+  if (!kTransformer) return 0;
+    
+  return kTransformer->GetDetElement(detElemId, warn); 
+}
+
 //_____________________________________________________________________________
 Bool_t  AliMUONGeometryTransformer::HasDE(Int_t detElemId) const
 {