]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/AliMUONGeometryBuilder.cxx
recover the detector tag int and not the string from the AOD particle
[u/mrichter/AliRoot.git] / MUON / AliMUONGeometryBuilder.cxx
index b7e351bd9b58b20f39c856d52ded2993eb61160e..1811e43e154309d30bf814333d37864bb1b6ad86 100644 (file)
  **************************************************************************/
 
 // $Id$
-//
+
+//-----------------------------------------------------------------------------
 // Class AliMUONGeometryBuilder
 // ----------------------------
 // Manager class for geometry construction via geometry builders.
-//
 // Author: Ivana Hrivnacova, IPN Orsay
-
-#include <TObjArray.h>
-#include <TVirtualMC.h>
+//-----------------------------------------------------------------------------
 
 #include "AliMUONGeometryBuilder.h"
 #include "AliMUONVGeometryBuilder.h"   
+#include "AliMUONGeometry.h"
+#include "AliMUONGeometryTransformer.h"
 #include "AliMUONGeometryModule.h"     
+#include "AliMUONGeometryModuleTransformer.h"  
 #include "AliMUONGeometryEnvelope.h"   
 #include "AliMUONGeometryEnvelopeStore.h"
 #include "AliMUONGeometryDetElement.h"
-#include "AliMUONGeometryStore.h"
-#include "AliMUONGeometryConstituent.h"        
+#include "AliMUONGeometryConstituent.h"
+
+#include "AliMpDEManager.h"
+
 #include "AliModule.h"
+#include "AliSimulation.h"
 #include "AliLog.h"
+#include "AliRun.h"
+
+#include <TObjArray.h>
+#include <TVirtualMC.h>
+#include <TGeoManager.h>
 
+using std::endl;
+/// \cond CLASSIMP
 ClassImp(AliMUONGeometryBuilder)
+/// \endcond
+
+//
+// static functions
+//
+
+//______________________________________________________________________________
+const TString& AliMUONGeometryBuilder::GetDefaultTransformFileName()
+{
+  ///< default transformations file name                                     
+  static const TString kDefaultTransformFileName = "transform.dat";   
+  return kDefaultTransformFileName;
+}    
+
+//______________________________________________________________________________
+const TString& AliMUONGeometryBuilder::GetDefaultSVMapFileName() 
+{
+  ///< default svmaps file name                                      
+  static const TString kDefaultSVMapFileName = "svmap.dat";    
+  return kDefaultSVMapFileName;
+}    
+
+//______________________________________________________________________________
+const TString& AliMUONGeometryBuilder::GetOutFileNameExtension()
+{
+  ///< default output file name extension                                    
+  static const TString kOutFileNameExtension = ".out";    
+  return kOutFileNameExtension;
+}    
+
+
+//______________________________________________________________________________
+TGeoHMatrix AliMUONGeometryBuilder::Multiply(const TGeoMatrix& m1, 
+                                             const TGeoMatrix& m2)
+{
+/// Temporary fix for problem with matrix multiplication in Root 5.02/00
+
+  if (m1.IsIdentity() && m2.IsIdentity()) return TGeoHMatrix();
+  
+  if (m1.IsIdentity()) return m2;
+  
+  if (m2.IsIdentity()) return m1;
+  
+  return m1 * m2;
+}
+
+//______________________________________________________________________________
+TGeoHMatrix AliMUONGeometryBuilder::Multiply(const TGeoMatrix& m1, 
+                                             const TGeoMatrix& m2,
+                                             const TGeoMatrix& m3)
+{                                           
+/// Temporary fix for problem with matrix multiplication in Root 5.02/00
+
+  if (m1.IsIdentity() && m2.IsIdentity() & m3.IsIdentity())  
+    return TGeoHMatrix();
+  
+  if (m1.IsIdentity()) return Multiply(m2, m3);
+  
+  if (m2.IsIdentity()) return Multiply(m1, m3);
+  
+  if (m3.IsIdentity()) return Multiply(m1, m2);
+  
+  return m1 * m2 * m3;
+}
+
+//______________________________________________________________________________
+TGeoHMatrix AliMUONGeometryBuilder::Multiply(const TGeoMatrix& m1, 
+                                             const TGeoMatrix& m2,
+                                             const TGeoMatrix& m3,
+                                             const TGeoMatrix& m4)
+{                                           
+/// Temporary fix for problem with matrix multiplication in Root 5.02/00
+
+  if (m1.IsIdentity() && m2.IsIdentity() & m3.IsIdentity() & m4.IsIdentity())  
+    return TGeoHMatrix();
+  
+  if (m1.IsIdentity()) return Multiply(m2, m3, m4);
+  
+  if (m2.IsIdentity()) return Multiply(m1, m3, m4);
+  
+  if (m3.IsIdentity()) return Multiply(m1, m2, m4);
+  
+  if (m4.IsIdentity()) return Multiply(m1, m2, m3);
+  
+  return m1 * m2 * m3 * m4;
+}
+
+//
+// ctors, dtor
+//
+
 //______________________________________________________________________________
 AliMUONGeometryBuilder::AliMUONGeometryBuilder(AliModule* module)
   : TObject(),
     fModule(module),
     fAlign(false),
+    fTransformFileName(GetDefaultTransformFileName()),
+    fSVMapFileName(GetDefaultSVMapFileName()),
     fGlobalTransformation(), 
-    fGeometryBuilders(0)
+    fGeometryBuilders(0),
+    fGeometry(0)
 {
-// Standard constructor
+/// Standard constructor
 
-  fGeometryBuilders = new TObjArray(100);
+  fGeometryBuilders = new TObjArray();
+  fGeometryBuilders->SetOwner(true);
+  
+  fGeometry = new AliMUONGeometry(true);
+
+  // Define the global transformation:
+  // Transformation from the old ALICE coordinate system to a new one:
+  // x->-x, z->-z 
+  TGeoRotation* rotGlobal 
+    = new TGeoRotation("rotGlobal", 90., 180., 90., 90., 180., 0.);
+  fGlobalTransformation = TGeoCombiTrans(0., 0., 0., rotGlobal);
 }
 
 //______________________________________________________________________________
@@ -55,45 +169,24 @@ AliMUONGeometryBuilder::AliMUONGeometryBuilder()
   : TObject(),
     fModule(0),
     fAlign(false),
+    fTransformFileName(),
+    fSVMapFileName(),
     fGlobalTransformation(),
-    fGeometryBuilders(0)
+    fGeometryBuilders(0),
+    fGeometry(0)
 {
-// Default constructor
+/// Default constructor
 } 
 
-//______________________________________________________________________________
-AliMUONGeometryBuilder::AliMUONGeometryBuilder(const AliMUONGeometryBuilder& right) 
-  : TObject(right) 
-{  
-  // copy constructor (not implemented)
-
-  AliFatal("Copy constructor not provided.");
-}
-
 //______________________________________________________________________________
 AliMUONGeometryBuilder::~AliMUONGeometryBuilder()
 {
-// Destructor
-  if (fGeometryBuilders){
-    fGeometryBuilders->Delete();
-    delete fGeometryBuilders;
-  }
+/// Destructor
+  
+  delete fGeometryBuilders;
+  delete fGeometry;
 }
 
-//______________________________________________________________________________
-AliMUONGeometryBuilder& 
-AliMUONGeometryBuilder::operator=(const AliMUONGeometryBuilder& right)
-{
-  // assignement operator (not implemented)
-
-  // check assignement to self
-  if (this == &right) return *this;
-
-  AliFatal("Assignement operator not provided.");
-    
-  return *this;  
-}    
-
 //
 // private functions
 //
@@ -101,16 +194,20 @@ AliMUONGeometryBuilder::operator=(const AliMUONGeometryBuilder& right)
 //______________________________________________________________________________
 void AliMUONGeometryBuilder::PlaceVolume(const TString& name, const TString& mName, 
                             Int_t copyNo, const TGeoHMatrix& matrix, 
-                           Int_t npar, Double_t* param, const char* only) const
+                           Int_t npar, Double_t* param, const char* only,
+                           Bool_t makeAssembly) const
 {
-// Place the volume specified by name with the given transformation matrix
-// ---
+/// Place the volume specified by name with the given transformation matrix
+
+  if (makeAssembly)
+    gGeoManager->MakeVolumeAssembly(name.Data());
 
+  TGeoHMatrix transform(matrix);
   // Do not apply global transformation 
-  // if mother volume != ALIC
+  // if mother volume was already placed in 
+  // the new system of coordinates (that is MUON in negative Z)
   // (as it is applied on the mother volume)
-  TGeoHMatrix transform(matrix);
-  if (mName != TString("ALIC"))
+  if (mName == TString("DDIP"))
     transform = fGlobalTransformation.Inverse() * transform;
      
   // Decompose transformation
@@ -154,83 +251,134 @@ void AliMUONGeometryBuilder::PlaceVolume(const TString& name, const TString& mNa
     fModule->AliMatrix(krot, theta1, phi1, theta2, phi2, theta3, phi3);
   }    
        
-  // Place the volume in ALIC
+  // Place the volume
   if (npar == 0)
-    gMC->Gspos(name, copyNo, mName, xyz[0], xyz[1], xyz[2] , krot, only);
+    TVirtualMC::GetMC()->Gspos(name, copyNo, mName, xyz[0], xyz[1], xyz[2] , krot, only);
   else 
-    gMC->Gsposp(name, copyNo, mName, xyz[0], xyz[1], xyz[2] , krot, only,
+    TVirtualMC::GetMC()->Gsposp(name, copyNo, mName, xyz[0], xyz[1], xyz[2] , krot, only,
                 param, npar);
-
 } 
 
 //______________________________________________________________________________
-void AliMUONGeometryBuilder::FillGlobalTransformations(
-                                 AliMUONVGeometryBuilder* builder)
+void AliMUONGeometryBuilder::CreateGeometryWithTGeo()
 {
-// Compute and set global transformations to detection elements
-// for each chamber geometry
-// ---
+/// Construct geometry using geometry builders.
+/// Virtual modules/envelopes are placed as TGeoVolume assembly
+
+  if (fAlign) {
+    // Read transformations from ASCII data file  
+    fGeometry->GetTransformer()
+      ->LoadGeometryData(fTransformFileName);
+  }    
+  for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
 
-  for (Int_t j=0; j<builder->NofGeometries(); j++) {
+    // Get the builder
+    AliMUONVGeometryBuilder* builder
+      = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
 
-    AliMUONGeometryModule* geometry = builder->Geometry(j);
-    AliMUONGeometryStore* detElements = geometry->GetDetElementStore();
+    // Create geometry + envelopes
+    //
+    builder->CreateGeometry();
+    builder->SetVolumes();
+    if (!fAlign) builder->SetTransformations();
+    
+    // Place module volumes and envelopes
+    //
+    for (Int_t j=0; j<builder->NofGeometries(); j++) {
 
-    for (Int_t k=0; k<detElements->GetNofEntries(); k++) {
-     
-      AliMUONGeometryDetElement* detElement 
-       = (AliMUONGeometryDetElement*)detElements->GetEntry(k);
-         
-      if (!detElement) AliFatal("Detection element not found.") 
+      AliMUONGeometryModule* geometry = builder->Geometry(j);
+      AliMUONGeometryModuleTransformer* transformer= geometry->GetTransformer();
+      const TGeoHMatrix* kModuleTransform = transformer->GetTransformation();
+      TString volName       = transformer->GetVolumeName(); 
+      TString motherVolName = transformer->GetMotherVolumeName(); 
+      
+      // Place the module volume
+      PlaceVolume(volName, motherVolName, 
+                  1, *kModuleTransform, 0, 0, "ONLY", geometry->IsVirtual());
+  
+      TGeoCombiTrans appliedGlobalTransform;
+      if (builder->ApplyGlobalTransformation())
+        appliedGlobalTransform = fGlobalTransformation;
+
+      // Loop over envelopes
+      const TObjArray* kEnvelopes 
+        = geometry->GetEnvelopeStore()->GetEnvelopes();
+      for (Int_t k=0; k<kEnvelopes->GetEntriesFast(); k++) {
+
+        // Get envelope
+        AliMUONGeometryEnvelope* env 
+         = (AliMUONGeometryEnvelope*)kEnvelopes->At(k);
          
-      const TGeoCombiTrans* localTransform 
-        = detElement->GetLocalTransformation();
-
-      // Compose global transformation
-      TGeoHMatrix total 
-       = fGlobalTransformation *
-         (*geometry->GetTransformation()) * 
-          fGlobalTransformation.Inverse() *
-         (*localTransform);
-                 // !! The local detection element frame is 
-                 // defined wrt the new ALICE coordinate system:
-                 // TGL = Tglobal * Tchamber * Tde
-                 //     =  Tglobal * Tchamber * Tglobal.inv  *  Tglobal * Tde
-                 //     = (Tglobal * Tchamber * Tglobal.inv) * (Tglobal * Tde)
-                 //     = Ttotal * Tde'
+       // Check consistency of detElemId and module Id
+       if ( env->GetUniqueID() > 0 && 
+            AliMpDEManager::GetGeomModuleId(env->GetUniqueID()) 
+            != geometry->GetModuleId() ) {
+            
+         AliErrorStream() 
+           << "Detection element " << env->GetUniqueID() 
+           << " is being placed in geometry module " << geometry->GetModuleId()
+           << " but should go in " 
+           << AliMpDEManager::GetGeomModuleId(env->GetUniqueID())
+           <<  endl;
+         AliFatal("Inconsistent IDs");
+       }          
          
-      // Convert TGeoHMatrix to TGeoCombiTrans
-      TGeoCombiTrans globalTransform(localTransform->GetName());
-      globalTransform.SetTranslation(total.GetTranslation());  
-      TGeoRotation rotation;
-      rotation.SetMatrix(total.GetRotationMatrix());
-      globalTransform.SetRotation(rotation);  
-      // Set the global transformation to detection element
-      detElement->SetGlobalTransformation(globalTransform);
-    }
-  }                        
-}           
+        const TGeoCombiTrans* kEnvTrans = env->GetTransformation();
+        const char* only = "ONLY";
+        if (env->IsMANY()) only = "MANY";
 
-//
-// public functions
-//
+        if (env->IsVirtual() && env->GetConstituents()->GetEntriesFast() == 0 ) {
+          // virtual envelope + nof constituents = 0 
+          //         => not allowed;
+          //            empty virtual envelope has no sense 
+          AliFatal("Virtual envelope must have constituents.");
+          return;
+        }
 
-//_____________________________________________________________________________
-void AliMUONGeometryBuilder::AddBuilder(AliMUONVGeometryBuilder* geomBuilder)
-{
-// Adds the geometry builder to the list
-// ---
+        if (!env->IsVirtual() && env->GetConstituents()->GetEntriesFast() > 0 ) {
+          // non virtual envelope + nof constituents > 0 
+          //        => not allowed;
+          //           use VMC to place constituents
+          AliFatal("Non virtual envelope cannot have constituents.");
+          return;
+        }
 
-  fGeometryBuilders->Add(geomBuilder);
+        // Place envelope in geometry module by composed transformation:
+        // [Tglobal] * Tenv
+        TGeoHMatrix total 
+         = Multiply( appliedGlobalTransform, 
+                     (*kEnvTrans) );
+        PlaceVolume(env->GetName(), volName,
+                   env->GetCopyNo(), total, 0, 0, only, env->IsVirtual());
+       
+        if ( env->IsVirtual() )  {
+          //  Place constituents in the envelope
+          for  (Int_t l=0; l<env->GetConstituents()->GetEntriesFast(); l++) {
+            AliMUONGeometryConstituent* constituent
+              = (AliMUONGeometryConstituent*)env->GetConstituents()->At(l);
+            PlaceVolume(constituent->GetName(), env->GetName(),
+                       constituent->GetCopyNo(),
+                       *constituent->GetTransformation() ,
+                        constituent->GetNpar(), constituent->GetParam(), only);
+          }
+        }
+      } // end of loop over envelopes
+    } // end of loop over builder geometries
+  } // end of loop over builders
 }
 
 //______________________________________________________________________________
-void AliMUONGeometryBuilder::CreateGeometry()
+void AliMUONGeometryBuilder::CreateGeometryWithoutTGeo()
 {
-//
-// Construct geometry using geometry builders.
-//
+/// Construct geometry using geometry builders.
+/// Virtual modules/envelopes are not placed
+
+  if (fAlign) {
+    // Read transformations from ASCII data file  
+    fGeometry->GetTransformer()->LoadGeometryData(fTransformFileName);
+  }     
 
   for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
 
@@ -240,27 +388,52 @@ void AliMUONGeometryBuilder::CreateGeometry()
 
     // Create geometry + envelopes
     //
-    if (fAlign) {
-      builder->ReadTransformations();
-      builder->CreateGeometry();
-    }
-    else {  
-      builder->CreateGeometry();
-      builder->SetTransformations();
-    } 
-
-    // Place envelopes
+    builder->CreateGeometry();
+    if (!fAlign) builder->SetTransformations();
+    
+    // Place module volumes and envelopes
     //
     for (Int_t j=0; j<builder->NofGeometries(); j++) {
 
       AliMUONGeometryModule* geometry = builder->Geometry(j);
+      AliMUONGeometryModuleTransformer* transformer= geometry->GetTransformer();
+      const TGeoHMatrix* kModuleTransform = transformer->GetTransformation();
+      TString volName       = transformer->GetVolumeName(); 
+      TString motherVolName = transformer->GetMotherVolumeName(); 
+      
+      // Place the module volume
+      if ( !geometry->IsVirtual() ) {
+          PlaceVolume(volName, motherVolName, 
+                     1, *kModuleTransform, 0, 0, "ONLY");
+      }                      
   
+      TGeoCombiTrans appliedGlobalTransform;
+      if (builder->ApplyGlobalTransformation())
+        appliedGlobalTransform = fGlobalTransformation;
+
       // Loop over envelopes
-      const TObjArray* kEnvelopes = geometry->GetEnvelopeStore()->GetEnvelopes();
+      const TObjArray* kEnvelopes 
+        = geometry->GetEnvelopeStore()->GetEnvelopes();
       for (Int_t k=0; k<kEnvelopes->GetEntriesFast(); k++) {
 
         // Get envelope
-        AliMUONGeometryEnvelope* env = (AliMUONGeometryEnvelope*)kEnvelopes->At(k);
+        AliMUONGeometryEnvelope* env 
+         = (AliMUONGeometryEnvelope*)kEnvelopes->At(k);
+         
+       // Check consistency of detElemId and module Id
+       if ( env->GetUniqueID() > 0 && 
+            AliMpDEManager::GetGeomModuleId(env->GetUniqueID()) 
+            != geometry->GetModuleId() ) {
+            
+         AliErrorStream() 
+           << "Detection element " << env->GetUniqueID() 
+           << " is being placed in geometry module " << geometry->GetModuleId()
+           << " but should go in " 
+           << AliMpDEManager::GetGeomModuleId(env->GetUniqueID())
+           <<  endl;
+         AliFatal("Inconsistent IDs");
+       }          
+         
         const TGeoCombiTrans* kEnvTrans = env->GetTransformation();
         const char* only = "ONLY";
         if (env->IsMANY()) only = "MANY";
@@ -283,38 +456,59 @@ void AliMUONGeometryBuilder::CreateGeometry()
 
         if (!env->IsVirtual() && env->GetConstituents()->GetEntriesFast() == 0 ) {
           // non virtual envelope + nof constituents = 0 
-          //        => place envelope in ALICE by composed transformation:
-          //           Tglobal * Tch * Tenv
+          //        => place envelope by composed transformation:
+          //           Tch * [Tglobal] * Tenv
 
           // Compound chamber transformation with the envelope one
-          TGeoHMatrix total 
-           = fGlobalTransformation * 
-             (*geometry->GetTransformation()) * 
-             (*kEnvTrans);
-          PlaceVolume(env->GetName(), geometry->GetMotherVolume(),
-                     env->GetCopyNo(), total, 0, 0, only);
+          if (geometry->IsVirtual()) {
+             TGeoHMatrix total 
+              = Multiply( (*kModuleTransform), 
+                           appliedGlobalTransform, 
+                          (*kEnvTrans) );
+             PlaceVolume(env->GetName(), motherVolName,
+                        env->GetCopyNo(), total, 0, 0, only);
+          }
+         else {
+             TGeoHMatrix total 
+              = Multiply( appliedGlobalTransform, 
+                          (*kEnvTrans) );
+             PlaceVolume(env->GetName(), volName,
+                        env->GetCopyNo(), total, 0, 0, only);
+          }                     
         }
 
         if (env->IsVirtual() && env->GetConstituents()->GetEntriesFast() > 0 ) {
           // virtual envelope + nof constituents > 0 
           //         => do not place envelope and place constituents
-          //            in ALICE by composed transformation:
-          //            Tglobal * Tch * Tenv * Tconst   
+          //            by composed transformation:
+          //            Tch * [Tglobal] * Tenv * Tconst   
 
           for  (Int_t l=0; l<env->GetConstituents()->GetEntriesFast(); l++) {
             AliMUONGeometryConstituent* constituent
               = (AliMUONGeometryConstituent*)env->GetConstituents()->At(l);
  
             // Compound chamber transformation with the envelope one + the constituent one
-            TGeoHMatrix total 
-             = fGlobalTransformation *
-               (*geometry->GetTransformation()) * 
-               (*kEnvTrans) * 
-               (*constituent->GetTransformation());
-
-            PlaceVolume(constituent->GetName(), geometry->GetMotherVolume(),
-                       constituent->GetCopyNo(), total,
-                        constituent->GetNpar(), constituent->GetParam(), only);
+            if (geometry->IsVirtual()) {
+              TGeoHMatrix total 
+               = Multiply ( (*kModuleTransform),
+                            appliedGlobalTransform, 
+                            (*kEnvTrans), 
+                            (*constituent->GetTransformation()) );
+
+              PlaceVolume(constituent->GetName(), motherVolName,
+                         constituent->GetCopyNo(), total,
+                          constituent->GetNpar(), constituent->GetParam(), only);
+            }
+           else {                        
+              TGeoHMatrix total 
+               = Multiply ( appliedGlobalTransform, 
+                            (*kEnvTrans),
+                            (*constituent->GetTransformation()) );
+
+              PlaceVolume(constituent->GetName(), volName,
+                         constituent->GetCopyNo(), total,
+                          constituent->GetNpar(), constituent->GetParam(), only);
+            }                    
           }
         }
       } // end of loop over envelopes
@@ -323,112 +517,166 @@ void AliMUONGeometryBuilder::CreateGeometry()
 }
 
 //_____________________________________________________________________________
-void AliMUONGeometryBuilder::CreateMaterials()
+void AliMUONGeometryBuilder::SetAlignToBuilder(AliMUONVGeometryBuilder* builder) const
 {
+/// Set align option to all geometry modules associated with the builder
+
+  for (Int_t j=0; j<builder->NofGeometries(); j++) {
+
+    AliMUONGeometryModule* geometry = builder->Geometry(j);
+  
+    geometry->SetAlign(fAlign);
+  }      
+}           
+
 //
-// Construct materials specific to modules via builders
+// public functions
 //
-  
-  for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
 
-    // Get the builder
-    AliMUONVGeometryBuilder* builder
-      = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
+//_____________________________________________________________________________
+void AliMUONGeometryBuilder::AddBuilder(AliMUONVGeometryBuilder* geomBuilder)
+{
+/// Add the geometry builder to the list
 
-    // Create materials with each builder
-    if (builder) builder->CreateMaterials();
-  }
+  fGeometryBuilders->Add(geomBuilder);
+  
+  // Pass geometry modules created in the to the geometry parametrisation
+  for (Int_t i=0; i<geomBuilder->NofGeometries(); i++) {
+    fGeometry->AddModule(geomBuilder->Geometry(i));
+  }  
+  
+  if (geomBuilder->ApplyGlobalTransformation())
+    geomBuilder->SetReferenceFrame(fGlobalTransformation);
+  
+  SetAlignToBuilder(geomBuilder);
 }
 
 //______________________________________________________________________________
-void AliMUONGeometryBuilder::InitGeometry()
+void AliMUONGeometryBuilder::CreateGeometry()
 {
- // Initialize geometry
- // ---
+/// Construct geometry using geometry builders.
+
+  if ( TVirtualMC::GetMC()->IsRootGeometrySupported() ) {
+       
+   CreateGeometryWithTGeo();
+  } 
+  else
+   CreateGeometryWithoutTGeo();
 
-  // Set the chamber (sensitive region) GEANT identifier
-  //
   for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
 
     // Get the builder
     AliMUONVGeometryBuilder* builder
       = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
-
-    // Set sesitive volumes with each builder
-    builder->SetSensitiveVolumes();
-    
-    // Read sensitive volume map from a file
-    builder->ReadSVMap();
-    if (!fAlign)  builder->FillTransformations();
-
-    // Compute global transformations of detection elements
-    FillGlobalTransformations(builder);
-  }  
+    // Update detection elements from built geometry
+    Bool_t create = ! fAlign;
+    builder->UpdateDetElements(create);
+  }
 }
 
-//______________________________________________________________________________
-void AliMUONGeometryBuilder::WriteTransformations()
+//_____________________________________________________________________________
+void AliMUONGeometryBuilder::CreateMaterials()
 {
- // Writes transformations into files per builder
- // ---
-
+/// Construct materials specific to modules via builders
+  
   for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
 
     // Get the builder
     AliMUONVGeometryBuilder* builder
       = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
 
-    // Write transformations
-    builder->WriteTransformations();
+    // Create materials with each builder
+    if (builder) builder->CreateMaterials();
   }
 }
 
 //______________________________________________________________________________
-void AliMUONGeometryBuilder::WriteSVMaps(Bool_t rebuild)
+void AliMUONGeometryBuilder::InitGeometry(const TString& svmapFileName)
 {
- // Writes sensitive volume maps into files per builder
- // ---
+/// Initialize geometry
+
+  // Load alignement data from geometry if geometry is read from Root file
+  if ( AliSimulation::Instance()->IsGeometryFromFile() ) {
+    fAlign = true;
+    fGeometry->GetTransformer()->LoadGeometryData();
+ }    
 
+  // Read sensitive volume map from a file
+  fGeometry->ReadSVMap(svmapFileName);
+
+  // Set the chamber (sensitive region) GEANT identifier
+  //
   for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
 
     // Get the builder
     AliMUONVGeometryBuilder* builder
       = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
 
-    // Write transformations      
-    builder->WriteSVMap(rebuild);
-  }
+    // Set sensitive volumes with each builder
+    builder->SetSensitiveVolumes();
+  }  
 }
 
-//_____________________________________________________________________________
-void  AliMUONGeometryBuilder::SetGlobalTransformation(
-                                       const TGeoCombiTrans& transform)
+//________________________________________________________________
+void AliMUONGeometryBuilder::UpdateInternalGeometry()
 {
-// Sets the global transformation
-// ---
+/// Update geometry after applying mis-alignment:
+/// reload transformations in geometry builder.
+
+  fGeometry->GetTransformer()->LoadTransformations();
+}
 
-  fGlobalTransformation = transform;
-}                                     
+//______________________________________________________________________________
+void AliMUONGeometryBuilder::WriteSVMaps(const TString& fileName, 
+                                         Bool_t rebuild, Bool_t writeEnvelopes)
+{
+/// Write sensitive volume maps into files per builder
+
+  // Rebuild sv maps
+  //
+  if (rebuild) 
+    for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
+
+      AliMUONVGeometryBuilder* builder
+        = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
+
+      builder->RebuildSVMaps(writeEnvelopes);
+    }  
+    
+  // Write maps in file
+  fGeometry->WriteSVMap(fileName);
+}
 
 //_____________________________________________________________________________
 void AliMUONGeometryBuilder::SetAlign(Bool_t align)
 { 
-// Sets the option for alignement
-// ---
+/// Set the option for alignement
 
   fAlign = align; 
 
   for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
 
-    // Get the builder
     AliMUONVGeometryBuilder* builder
       = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
+    
+    SetAlignToBuilder(builder); 
+  }   
+}
 
-    for (Int_t j=0; j<builder->NofGeometries(); j++) {
+//_____________________________________________________________________________
+void AliMUONGeometryBuilder::SetAlign(const TString& fileName, Bool_t align)
+{ 
+/// Set the option for alignement and the transformations file name
 
-      AliMUONGeometryModule* geometry = builder->Geometry(j);
-  
-      geometry->SetAlign(align);
-    }            
-  }      
+  fTransformFileName = fileName;
+  fAlign = align; 
+
+  for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
+
+    AliMUONVGeometryBuilder* builder
+      = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
+    
+    SetAlignToBuilder(builder); 
+  }   
 }