]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/AliMUONGeometryTransformer.cxx
Fixing FORWARD_NULL defects reported by Coverity
[u/mrichter/AliRoot.git] / MUON / AliMUONGeometryTransformer.cxx
index a95e284ea80bc79a982e67de34d44df75c9fb802..e7ac07ff7e65e303d6a1a178ca9213a0bd46762a 100644 (file)
  **************************************************************************/
 
 // $Id$
-//
-// ----------------------------
+
+//-----------------------------------------------------------------------------
 // Class AliMUONGeometryTransformer
 // ----------------------------
 // Top container class for geometry transformations
 // Author: Ivana Hrivnacova, IPN Orsay
+//-----------------------------------------------------------------------------
 
 #include "AliMUONGeometryTransformer.h"
 #include "AliMUONGeometryModuleTransformer.h"
 #include "AliMUONGeometryBuilder.h"
 
 #include "AliMpDEManager.h"
+#include "AliMpConstants.h"
 #include "AliMpExMap.h"
-
+#include "AliMpCDB.h"
+#include "AliMpArea.h"
+#include <float.h>
+#include "AliMpVPadIterator.h"
+#include "AliMpPad.h"
+#include "AliMpDEIterator.h"
+#include <TVector2.h>
+#include "AliMpVSegmentation.h"
+#include "AliMpSegmentation.h"
+#include "AliMpExMapIterator.h"
 #include "AliLog.h"
 #include "AliAlignObjMatrix.h"
 #include "AliAlignObj.h"
 #include <TGeoManager.h>
 #include <TGeoPhysicalNode.h>
 #include <TFile.h>
+#include <TString.h>
 
 #include <sstream>
 
 /// \cond CLASSIMP
 ClassImp(AliMUONGeometryTransformer)
 /// \endcond
+
+const TString  AliMUONGeometryTransformer::fgkDefaultDetectorName = "MUON";
  
 //______________________________________________________________________________
-AliMUONGeometryTransformer::AliMUONGeometryTransformer(Bool_t isOwner,
-                                          const TString& detectorName)
+AliMUONGeometryTransformer::AliMUONGeometryTransformer()
 
   : TObject(),
-    fDetectorName(detectorName),
+    fDetectorName(fgkDefaultDetectorName),
     fModuleTransformers(0),
-    fMisAlignArray(0)
+    fMisAlignArray(0),
+    fDEAreas(0x0)
 {
 /// Standard constructor
 
   // Create array for geometry modules
   fModuleTransformers = new TObjArray(100);
-  fModuleTransformers->SetOwner(isOwner);
+  fModuleTransformers->SetOwner(true);
 }
 
 //______________________________________________________________________________
-AliMUONGeometryTransformer::AliMUONGeometryTransformer() 
+AliMUONGeometryTransformer::AliMUONGeometryTransformer(TRootIOCtor* /*ioCtor*/
   : TObject(),
     fDetectorName(),
     fModuleTransformers(0),
-    fMisAlignArray(0)
+    fMisAlignArray(0),
+    fDEAreas(0x0)
 {
 /// Default constructor
 } 
@@ -79,12 +94,119 @@ AliMUONGeometryTransformer::~AliMUONGeometryTransformer()
 
   delete fModuleTransformers;
   delete fMisAlignArray;
+  delete fDEAreas;
 }
 
 //
 // private methods
 //
 
+
+//_____________________________________________________________________________
+AliMpArea*
+AliMUONGeometryTransformer::GetDEArea(Int_t detElemId) const
+{
+  /// Get area (in global coordinates) covered by a given detection element
+  if (!fDEAreas)
+  {
+    CreateDEAreas();
+  }
+  return static_cast<AliMpArea*>(fDEAreas->GetValue(detElemId));
+}
+
+//_____________________________________________________________________________
+void
+AliMUONGeometryTransformer::CreateDEAreas() const
+{
+  /// Create DE areas
+  
+  fDEAreas = new AliMpExMap;
+  
+  AliMpDEIterator it;
+
+  it.First();
+
+  /// Generate the DE areas in global coordinates
+
+  while ( !it.IsDone() )
+  {
+    Int_t detElemId = it.CurrentDEId();
+    
+    if ( !HasDE(detElemId) ) continue;
+    
+    const AliMpVSegmentation* seg = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::kCath0);
+    
+    Double_t xg,yg,zg;
+    
+    AliMp::StationType stationType = AliMpDEManager::GetStationType(detElemId);
+    
+    Double_t xl(0.0), yl(0.0), zl(0.0);
+    Double_t dx(seg->GetDimensionX());
+    Double_t dy(seg->GetDimensionY());
+    
+    if ( stationType == AliMp::kStation12 ) 
+    {
+      Double_t xmin(FLT_MAX);
+      Double_t xmax(-FLT_MAX);
+      Double_t ymin(FLT_MAX);
+      Double_t ymax(-FLT_MAX);
+      
+      for ( Int_t icathode = 0; icathode < 2; ++icathode ) 
+      {
+        const AliMpVSegmentation* cathode 
+        = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(icathode));
+        
+        AliMpVPadIterator* itp = cathode->CreateIterator();
+        
+        itp->First();
+        
+        while ( !itp->IsDone() ) 
+        {
+          AliMpPad pad = itp->CurrentItem();
+          AliMpArea a(pad.GetPositionX(),pad.GetPositionY(),
+                      pad.GetDimensionX(), pad.GetDimensionY());
+          xmin = TMath::Min(xmin,a.LeftBorder());
+          xmax = TMath::Max(xmax,a.RightBorder());
+          ymin = TMath::Min(ymin,a.DownBorder());
+          ymax = TMath::Max(ymax,a.UpBorder());
+          itp->Next();
+        }
+        
+        delete itp;
+      }
+      
+      xl = (xmin+xmax)/2.0;
+      yl = (ymin+ymax)/2.0;
+      dx = (xmax-xmin)/2.0;
+      dy = (ymax-ymin)/2.0;
+      
+      Local2Global(detElemId,xl,yl,zl,xg,yg,zg);
+    }
+    else
+    {
+      Local2Global(detElemId,xl,yl,zl,xg,yg,zg);
+    }
+    
+    fDEAreas->Add(detElemId,new AliMpArea(xg,yg,dx,dy));
+    
+    it.Next();
+  }
+}
+
+//_____________________________________________________________________________
+Bool_t AliMUONGeometryTransformer::LoadMapping() const
+{
+/// Load mapping from CDB
+
+  if ( ! AliMpCDB::LoadMpSegmentation() ) 
+  {
+    AliFatal("Could not access mapping from OCDB !");
+    return false;
+  }
+  
+  return true;
+}  
+
 //_____________________________________________________________________________
 AliMUONGeometryModuleTransformer* 
 AliMUONGeometryTransformer::GetModuleTransformerNonConst(
@@ -117,43 +239,6 @@ TGeoHMatrix AliMUONGeometryTransformer::GetTransform(
 }
 
 
-//______________________________________________________________________________
-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,
@@ -168,6 +253,7 @@ void AliMUONGeometryTransformer::FillModuleTransform(Int_t moduleId,
   if ( !moduleTransformer) {
     AliErrorStream() 
       << "Module " << moduleId << " has not volume path defined." << endl;
+    return;  
   }  
       
   // Build the transformation from the parameters
@@ -220,40 +306,6 @@ void AliMUONGeometryTransformer::FillDetElemTransform(
   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::ReadModuleTransforms(ifstream& in)
 {
@@ -340,95 +392,6 @@ TString  AliMUONGeometryTransformer::ReadDetElemTransforms(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)
@@ -465,29 +428,6 @@ AliMUONGeometryTransformer::ReadTransformations(const TString& fileName)
   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 TGeoMatrix* transform) const
@@ -515,47 +455,6 @@ void AliMUONGeometryTransformer::WriteTransform(ofstream& out,
       << setw(8) << setprecision(4) << a6 << "  " << endl; 
 }
 
-//______________________________________________________________________________
-void AliMUONGeometryTransformer::WriteModuleVolPaths(ofstream& out) const
-{
-/// Write module volume paths for all module transformers
-
-  for (Int_t i=0; i<fModuleTransformers->GetEntriesFast(); i++) {
-    AliMUONGeometryModuleTransformer* moduleTransformer 
-      = (AliMUONGeometryModuleTransformer*)fModuleTransformers->At(i);
-
-    // 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
 {
@@ -586,10 +485,10 @@ void AliMUONGeometryTransformer::WriteDetElemTransforms(ofstream& out) const
     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);
+    TIter next(detElements->CreateIterator());
+    AliMUONGeometryDetElement* detElement;
+    while ( ( detElement = static_cast<AliMUONGeometryDetElement*>(next()) ) )
+    {
       const TGeoMatrix* transform 
         = detElement->GetLocalTransformation(); 
        
@@ -608,14 +507,8 @@ 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();
+  return "/" + fDetectorName + "/" 
+             + AliMUONGeometryModuleTransformer::GetModuleName(moduleId);
 }  
 
 //______________________________________________________________________________
@@ -623,17 +516,11 @@ 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();
+  return GetModuleSymName(moduleId) + "/" 
+         + AliMUONGeometryDetElement::GetDEName(detElemId);
 }  
 
 //
@@ -642,94 +529,95 @@ TString AliMUONGeometryTransformer::GetDESymName(Int_t detElemId) const
 
 //______________________________________________________________________________
 Bool_t  
-AliMUONGeometryTransformer::ReadGeometryData(
-                                const TString& volPathFileName,
-                                const TString& transformFileName)
+AliMUONGeometryTransformer::LoadTransformations()
 {
-/// 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
+/// Load transformations for defined modules and detection elements
+/// using AliGeomManager
 
-  Bool_t result1 = ReadVolPaths(volPathFileName);
+  if ( ! AliGeomManager::GetGeometry() ) {
+    AliFatal("Geometry has to be laoded in AliGeomManager first.");
+    return false;
+  }   
 
-  // 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;
+  for (Int_t i=0; i<fModuleTransformers->GetEntriesFast(); i++) {
+    AliMUONGeometryModuleTransformer* moduleTransformer 
+      = (AliMUONGeometryModuleTransformer*)fModuleTransformers->At(i);
+
+    // Module symbolic name
+    TString symname = GetModuleSymName(moduleTransformer->GetModuleId());
+    
+    // Set matrix from physical node
+    TGeoHMatrix* matrix = AliGeomManager::GetMatrix(symname);
+    if ( ! matrix ) {
+      AliErrorStream() << "Geometry module matrix not found." << endl;
+      return false;
+    }  
+    moduleTransformer->SetTransformation(*matrix);
+    
+    // Loop over detection elements
+    AliMpExMap* detElements = moduleTransformer->GetDetElementStore();    
+    TIter next(detElements->CreateIterator());    
+    AliMUONGeometryDetElement* detElement;
+    
+    while ( ( detElement = static_cast<AliMUONGeometryDetElement*>(next()) ) )
+    {
+      // Det element  symbolic name
+      TString symnameDE = GetDESymName(detElement->GetId());
+    
+      // Set global matrix from physical node
+      TGeoHMatrix* globalMatrix = AliGeomManager::GetMatrix(symnameDE);
+      if ( ! globalMatrix ) {
+        AliErrorStream() << "Detection element matrix not found." << endl;
+        return false;
+      }  
+      detElement->SetGlobalTransformation(*globalMatrix, false);
+
+      // Set local matrix
+      TGeoHMatrix localMatrix = 
+        AliMUONGeometryBuilder::Multiply(
+          (*matrix).Inverse(), (*globalMatrix) );
+      detElement->SetLocalTransformation(localMatrix, false);
+    }  
+  } 
+  return true;    
 }  
 
 //______________________________________________________________________________
 Bool_t  
-AliMUONGeometryTransformer::ReadGeometryData(
-                                const TString& volPathFileName,
-                                TGeoManager* tgeoManager)
+AliMUONGeometryTransformer::LoadGeometryData(const TString& fileName)
 {
-/// Load geometry data from root geometry using defined
-/// volume paths from file
+/// Read geometry data either from ASCII file with transformations or
+/// from root geometry file (if fileName has ".root" extension)
 
-  Bool_t result1 = ReadVolPaths(volPathFileName);
+  CreateModules();
 
-  Bool_t result2 = LoadTransforms(tgeoManager);
+  // Get file extension
+  std::string fileName2 = fileName.Data();
+  std::string rootExt = fileName2.substr(fileName2.size()-5, fileName2.size());
   
-  return result1 && result2;
+  if ( rootExt != ".root" ) 
+    return ReadTransformations(fileName);
+  else  { 
+    // Load root geometry
+    AliGeomManager::LoadGeometry(fileName.Data());
+    return LoadTransformations();
+  }  
 }  
 
 //______________________________________________________________________________
 Bool_t  
-AliMUONGeometryTransformer::WriteGeometryData(
-                                 const TString& volPathFileName,
-                                 const TString& transformFileName,
-                                const TString& misalignFileName) const
+AliMUONGeometryTransformer::LoadGeometryData()
 {
-/// Write geometry data into given files
+/// Load geometry data from already loaded Root geometry using AliGeomManager
 
-  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
-{
-/// Write volume paths for modules and detection element volumes into a file.
-/// Return true, if writing finished correctly.
+  if ( ! AliGeomManager::GetGeometry() ) {
+    AliErrorStream() << "Geometry has not been loaded in AliGeomManager" << endl;
+    return false;
+  }    
 
-  // No writing
-  // if builder is not associated with any geometry module
-  if (fModuleTransformers->GetEntriesFast() == 0) return false;
+  CreateModules();
 
-  // File path
-  TString filePath = gSystem->Getenv("ALICE_ROOT");
-  filePath += "/MUON/data/";
-  filePath += fileName;
-  
-  // Open output file
-  ofstream out(filePath, ios::out);
-  if (!out) {
-    cerr << filePath << endl;  
-    AliError("File not found.");
-    return false;
-  }
-#if !defined (__DECCXX)
-  out.setf(std::ios::fixed);
-#endif
-  WriteModuleVolPaths(out);
-  WriteDetElemVolPaths(out);
-  
-  return true;
+  return LoadTransformations();
 }  
 
 //______________________________________________________________________________
@@ -814,7 +702,7 @@ void AliMUONGeometryTransformer::AddModuleTransformer(
 
 //_____________________________________________________________________________
 void  AliMUONGeometryTransformer::AddMisAlignModule(Int_t moduleId, 
-                                              const TGeoHMatrix& matrix)
+                                                   const TGeoHMatrix& matrix, Bool_t bGlobal)
 {
 /// Build AliAlignObjMatrix with module ID, its volumePath
 /// and the given delta transformation matrix                                        
@@ -830,18 +718,18 @@ void  AliMUONGeometryTransformer::AddMisAlignModule(Int_t moduleId,
   }   
   
   // Get unique align object ID
-  Int_t volId = AliAlignObj::LayerToVolUID(AliAlignObj::kMUON, moduleId); 
+  Int_t volId = AliGeomManager::LayerToVolUID(AliGeomManager::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);
+                                       const_cast<TGeoHMatrix&>(matrix),bGlobal);
 }
 
 //_____________________________________________________________________________
 void  AliMUONGeometryTransformer::AddMisAlignDetElement(Int_t detElemId, 
-                                              const TGeoHMatrix& matrix)
+                                                       const TGeoHMatrix& matrix, Bool_t bGlobal)
 {
 /// Build AliAlignObjMatrix with detection element ID, its volumePath
 /// and the given delta transformation matrix                                        
@@ -858,19 +746,59 @@ void  AliMUONGeometryTransformer::AddMisAlignDetElement(Int_t detElemId,
   }   
   
   // Get unique align object ID
-  Int_t volId = AliAlignObj::LayerToVolUID(AliAlignObj::kMUON, detElemId); 
+  Int_t volId = AliGeomManager::LayerToVolUID(AliGeomManager::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);
+                                      const_cast<TGeoHMatrix&>(matrix),bGlobal);
+}
+
+//______________________________________________________________________________
+void AliMUONGeometryTransformer::CreateModules()
+{
+/// Create modules and their detection elements using info from mapping;
+/// but do not fill matrices
+
+  // Load mapping as its info is used to define modules & DEs
+  LoadMapping();
+
+  if ( fModuleTransformers->GetEntriesFast() == 0 ) {
+    // Create modules only if they do not yet exist
+
+    // Loop over geometry module
+    for (Int_t moduleId = 0; moduleId < AliMpConstants::NofGeomModules(); ++moduleId ) {
+    
+      // Create geometry module transformer
+      AliMUONGeometryModuleTransformer* moduleTransformer
+        = new AliMUONGeometryModuleTransformer(moduleId);
+      AddModuleTransformer(moduleTransformer);
+    }
+  }     
+    
+  // Loop over detection elements
+  AliMpDEIterator it;
+  for ( it.First(); ! it.IsDone(); it.Next() ) {
+    
+    Int_t detElemId = it.CurrentDEId();
+    Int_t moduleId = AliMpDEManager::GetGeomModuleId(detElemId);
+
+    // Get detection element store
+    AliMpExMap* detElements = 
+      GetModuleTransformer(moduleId)->GetDetElementStore();     
+
+    // Add detection element
+    AliMUONGeometryDetElement* detElement 
+      = new AliMUONGeometryDetElement(detElemId);
+    detElements->Add(detElemId, detElement);
+  }   
 }
 
 //_____________________________________________________________________________
 void AliMUONGeometryTransformer::AddAlignableVolumes() const
 {
-/// Set symbolic names to alignable objects to TGeo
+/// Set symbolic names and matrices to alignable objects to TGeo
 
   if ( ! gGeoManager ) {
     AliWarning("TGeoManager not defined.");
@@ -883,23 +811,44 @@ void AliMUONGeometryTransformer::AddAlignableVolumes() const
       = (AliMUONGeometryModuleTransformer*)fModuleTransformers->At(i);
 
     // Set module symbolic name
-    gGeoManager->SetAlignableEntry(GetModuleSymName(module->GetModuleId()), 
-                                   module->GetVolumePath());
-    //cout << "Module sym name: " << GetModuleSymName(module->GetModuleId()) 
-    //     << "  volPath: " << module->GetVolumePath() << endl;
+    TGeoPNEntry* pnEntry
+      = gGeoManager->SetAlignableEntry(GetModuleSymName(module->GetModuleId()),
+                                       module->GetVolumePath());
+    if ( ! pnEntry ) {
+      AliErrorStream() 
+        << "Volume path " << module->GetVolumePath().Data()
+        << " for geometry module " << module->GetModuleId() << " " << module
+        << " not found in geometry." << endl;
+    }
+    else {
+      // Set module matrix
+      pnEntry->SetMatrix(new TGeoHMatrix(*module->GetTransformation()));  
+       // the matrix will be deleted via TGeoManager  
+    }                                     
 
     // Detection elements
     AliMpExMap* detElements = module->GetDetElementStore();    
-
-    for (Int_t j=0; j<detElements->GetSize(); j++) {
-      AliMUONGeometryDetElement* detElement
-        = (AliMUONGeometryDetElement*)detElements->GetObject(j);
-       
+    TIter next(detElements->CreateIterator());    
+    AliMUONGeometryDetElement* detElement;
+    
+    while ( ( detElement = static_cast<AliMUONGeometryDetElement*>(next()) ) )
+    {
       // Set detection element symbolic name
-      gGeoManager->SetAlignableEntry(GetDESymName(detElement->GetId()), 
-                                     detElement->GetVolumePath());
-      //cout << "DE name: " << GetDESymName(detElement->GetId()) 
-      //     << "  volPath: " << detElement->GetVolumePath() << endl;
+      TGeoPNEntry* pnEntryDE
+        = gGeoManager->SetAlignableEntry(GetDESymName(detElement->GetId()), 
+                                         detElement->GetVolumePath());
+      if ( ! pnEntryDE ) {
+        AliErrorStream() 
+          << "Volume path " 
+          << detElement->GetVolumePath().Data() 
+          << " for detection element " << detElement->GetId()
+          << " not found in geometry." << endl;
+      }
+      else {
+        // Set detection element matrix
+        pnEntryDE->SetMatrix(new TGeoHMatrix(*detElement->GetGlobalTransformation()));                                      
+         // the matrix will be deleted via TGeoManager 
+      }                                      
     }  
   }     
 }           
@@ -925,7 +874,7 @@ TClonesArray* AliMUONGeometryTransformer::CreateZeroAlignmentData() const
     Int_t moduleId = module->GetModuleId();
   
     // Align object ID
-    Int_t volId = AliAlignObj::LayerToVolUID(AliAlignObj::kMUON, moduleId); 
+    Int_t volId = AliGeomManager::LayerToVolUID(AliGeomManager::kMUON, moduleId); 
 
     // Create mis align matrix
     Int_t pos = array->GetEntriesFast();
@@ -936,16 +885,17 @@ TClonesArray* AliMUONGeometryTransformer::CreateZeroAlignmentData() const
   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);
-       
+    AliMpExMap* detElements = moduleTransformer->GetDetElementStore();    
+    TIter next(detElements->CreateIterator());    
+    AliMUONGeometryDetElement* detElement;
+    
+    while ( ( detElement = static_cast<AliMUONGeometryDetElement*>(next()) ) )
+    {
       Int_t detElemId = detElement->GetId();
   
       // Align object ID
-      Int_t volId = AliAlignObj::LayerToVolUID(AliAlignObj::kMUON, detElemId); 
+      Int_t volId = AliGeomManager::LayerToVolUID(AliGeomManager::kMUON, detElemId); 
 
       // Create mis align matrix
       Int_t pos = array->GetEntriesFast();
@@ -956,6 +906,16 @@ TClonesArray* AliMUONGeometryTransformer::CreateZeroAlignmentData() const
   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,