LCOV - code coverage report
Current view: top level - MUON/MUONgeometry - AliMUONVGeometryBuilder.cxx (source / functions) Hit Total Coverage
Test: coverage.info Lines: 105 174 60.3 %
Date: 2016-06-14 17:26:59 Functions: 16 21 76.2 %

          Line data    Source code
       1             : /**************************************************************************
       2             :  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
       3             :  *                                                                        *
       4             :  * Author: The ALICE Off-line Project.                                    *
       5             :  * Contributors are mentioned in the code where appropriate.              *
       6             :  *                                                                        *
       7             :  * Permission to use, copy, modify and distribute this software and its   *
       8             :  * documentation strictly for non-commercial purposes is hereby granted   *
       9             :  * without fee, provided that the above copyright notice appears in all   *
      10             :  * copies and that both the copyright notice and this permission notice   *
      11             :  * appear in the supporting documentation. The authors make no claims     *
      12             :  * about the suitability of this software for any purpose. It is          *
      13             :  * provided "as is" without express or implied warranty.                  *
      14             :  **************************************************************************/
      15             : 
      16             : // $Id$
      17             : 
      18             : //-----------------------------------------------------------------------------
      19             : // Class AliMUONVGeometryBuilder
      20             : // -----------------------------
      21             : // Abstract base class for geometry construction per geometry module(s).
      22             : // Author: Ivana Hrivnacova, IPN Orsay
      23             : // 23/01/2004
      24             : //-----------------------------------------------------------------------------
      25             : 
      26             : #include "AliMUONVGeometryBuilder.h"
      27             : #include "AliMUONGeometryModule.h"
      28             : #include "AliMUONGeometryDetElement.h"
      29             : #include "AliMUONGeometryEnvelopeStore.h"
      30             : #include "AliMUONGeometryEnvelope.h"
      31             : #include "AliMUONGeometryConstituent.h"
      32             : #include "AliMUONGeometryBuilder.h"
      33             : #include "AliMUONStringIntMap.h"
      34             : 
      35             : #include "AliMpDEManager.h"
      36             : #include "AliMpExMap.h"
      37             : 
      38             : #include "AliLog.h"
      39             : 
      40             : #include <Riostream.h>
      41             : #include <TObjArray.h>
      42             : #include <TSystem.h>
      43             : #include <TGeoMatrix.h>
      44             : #include <TVirtualMC.h>
      45             : 
      46             : /// \cond CLASSIMP
      47          18 : ClassImp(AliMUONVGeometryBuilder)
      48             : /// \endcond
      49             : 
      50             : //______________________________________________________________________________
      51             : AliMUONVGeometryBuilder::AliMUONVGeometryBuilder(
      52             :                             Int_t firstModuleId, 
      53             :                             Int_t nofModules)
      54           5 :  : TObject(),
      55           5 :    fGeometryModules(0),
      56           5 :    fReferenceFrame()
      57          15 :  {
      58             : /// Standard constructor
      59             : 
      60             :   // Create the module geometries array
      61          15 :   fGeometryModules = new TObjArray();
      62           5 :   fGeometryModules->SetOwner(kFALSE);
      63             :      
      64          50 :   for (Int_t i=0; i<nofModules; i++ )
      65          60 :     fGeometryModules->Add(new AliMUONGeometryModule(firstModuleId++));
      66           5 : }
      67             : 
      68             : //______________________________________________________________________________
      69             : AliMUONVGeometryBuilder::AliMUONVGeometryBuilder()
      70          60 :  : TObject(),
      71          60 :    fGeometryModules(0),
      72          60 :    fReferenceFrame()
      73         180 : {
      74             : /// Default constructor
      75          60 : }
      76             : 
      77             : //______________________________________________________________________________
      78             : AliMUONVGeometryBuilder::~AliMUONVGeometryBuilder() 
      79         130 : {
      80             : /// Destructor
      81             : 
      82          65 :   if (fGeometryModules) {
      83          65 :     fGeometryModules->Clear(); // Sets pointers to 0 since it is not the owner
      84         130 :     delete fGeometryModules;
      85             :   }
      86          65 : }
      87             : 
      88             : //
      89             : // private methods
      90             : //
      91             : 
      92             : //______________________________________________________________________________
      93             : TGeoHMatrix 
      94             : AliMUONVGeometryBuilder::ConvertTransform(const TGeoHMatrix& transform) const
      95             : {
      96             : /// Convert transformation into the reference frame
      97             : 
      98          40 :   if ( fReferenceFrame.IsIdentity() )
      99           4 :     return transform;
     100             :   else  {
     101          16 :     return AliMUONGeometryBuilder::Multiply( fReferenceFrame,
     102          16 :                                              transform,
     103          16 :                                              fReferenceFrame.Inverse() );  
     104             :   }                         
     105          20 : }
     106             : 
     107             : //______________________________________________________________________________
     108             : TGeoHMatrix 
     109             : AliMUONVGeometryBuilder::ConvertDETransform(const TGeoHMatrix& transform) const
     110             : {
     111             : /// Convert DE transformation into the reference frame
     112             : 
     113         456 :   if ( fReferenceFrame.IsIdentity() )
     114          72 :     return transform;
     115             :   else  {
     116         156 :     return AliMUONGeometryBuilder::Multiply( fReferenceFrame,
     117         156 :                                              transform );  
     118             :   }                         
     119         228 : }
     120             : 
     121             : //______________________________________________________________________________
     122             : TString  AliMUONVGeometryBuilder::ComposePath(const TString& volName,
     123             :                                               Int_t copyNo) const
     124             : {
     125             : /// Compose path from given volName and copyNo
     126             : 
     127         536 :   TString path = "/";
     128         268 :   path += volName;
     129         268 :   path += '_';
     130         268 :   path += copyNo;
     131             :   
     132             :   return path;
     133         536 : }  
     134             : 
     135             : //______________________________________________________________________________
     136             : void AliMUONVGeometryBuilder::MapSV(const TString& path0, 
     137             :                                     const TString& volName, Int_t detElemId) const
     138             : {
     139             : /// Update the path with all daughters volumes recursively
     140             : /// and map it to the detection element Id if it is a sensitive volume
     141             : 
     142             :   // Get module sensitive volumes map
     143           0 :   Int_t moduleId = AliMpDEManager::GetGeomModuleId(detElemId);
     144           0 :   AliMUONStringIntMap* svMap = GetSVMap(moduleId);     
     145             : 
     146           0 :   Int_t nofDaughters = TVirtualMC::GetMC()->NofVolDaughters(volName);
     147           0 :   if (nofDaughters == 0) {
     148             : 
     149             :     // Get the name of the last volume in the path
     150           0 :     Ssiz_t npos1 = path0.Last('/')+1; 
     151           0 :     Ssiz_t npos2 = path0.Last('_');
     152           0 :     TString volName0(path0(npos1, npos2-npos1));  
     153             :     
     154             :     // Check if it is sensitive volume
     155           0 :     AliMUONGeometryModule* geometry = GetGeometry(moduleId);
     156           0 :     if (  geometry->IsSensitiveVolume(volName0) &&
     157           0 :         ! svMap->Get(path0) ) {
     158             :       //cout << ".. adding to the map  " 
     159             :       //     <<  path0 << "  "  << detElemId << endl;
     160             :     
     161             :       // Map the sensitive volume to detection element
     162           0 :       svMap->Add(path0, detElemId); 
     163             :     }  
     164             :     return; 
     165           0 :   }  
     166             : 
     167           0 :   for (Int_t i=0; i<nofDaughters; i++) {
     168           0 :     Int_t copyNo = TVirtualMC::GetMC()->VolDaughterCopyNo(volName, i);
     169           0 :     TString newName =  TVirtualMC::GetMC()->VolDaughterName(volName, i);
     170             :             
     171           0 :     TString path = path0;
     172           0 :     path += ComposePath(newName, copyNo);
     173             : 
     174           0 :     MapSV(path, newName, detElemId);
     175           0 :   }
     176           0 : }     
     177             : 
     178             : //
     179             : // protected methods
     180             : //
     181             : 
     182             : //______________________________________________________________________________
     183             : AliMUONGeometryModule*  
     184             : AliMUONVGeometryBuilder::GetGeometry(Int_t moduleId) const
     185             : {
     186             : /// Return the module geometry specified by moduleId
     187             : 
     188       51152 :   for (Int_t i=0; i<fGeometryModules->GetEntriesFast(); i++) {
     189             : 
     190             :     AliMUONGeometryModule* geometry 
     191       23164 :       = (AliMUONGeometryModule*)fGeometryModules->At(i);
     192             : 
     193       27988 :     if ( geometry->GetModuleId() == moduleId) return geometry;
     194       18340 :   }   
     195             :   
     196           0 :   return 0;
     197        4824 : }  
     198             : 
     199             : //______________________________________________________________________________
     200             : AliMUONGeometryEnvelopeStore*  
     201             : AliMUONVGeometryBuilder::GetEnvelopes(Int_t moduleId) const
     202             : {
     203             : /// Return the envelope store of the module geometry specified by moduleId
     204             : 
     205        9328 :   AliMUONGeometryModule* geometry = GetGeometry(moduleId);
     206             :   
     207        4664 :   if (!geometry) {
     208           0 :     AliFatal(Form("Module geometry %d is not defined", moduleId)); 
     209           0 :     return 0;
     210             :   }
     211             :   
     212        4664 :   return geometry->GetEnvelopeStore();
     213        4664 : }  
     214             : 
     215             : //______________________________________________________________________________
     216             : AliMUONStringIntMap*  
     217             : AliMUONVGeometryBuilder::GetSVMap(Int_t moduleId) const
     218             : {
     219             : /// Return the transformation store of the module geometry specified by moduleId
     220             : 
     221           0 :   AliMUONGeometryModule* geometry = GetGeometry(moduleId);
     222             :   
     223           0 :   if (!geometry) {
     224           0 :     AliFatal(Form("Geometry %d is not defined", moduleId)); 
     225           0 :     return 0;
     226             :   }
     227             :   
     228           0 :   return geometry->GetSVMap();
     229           0 : }  
     230             : 
     231             : //______________________________________________________________________________
     232             : Int_t                          
     233             : AliMUONVGeometryBuilder::GetModuleId(const TString& envName) const
     234             : {
     235             : /// Return module Id which has the envelope with given name
     236             : 
     237        2088 :   for (Int_t i=0; i<fGeometryModules->GetEntriesFast(); i++) {
     238             : 
     239             :     AliMUONGeometryModule* geometry 
     240         974 :       = (AliMUONGeometryModule*)fGeometryModules->At(i);
     241             :       
     242         974 :     if ( geometry->GetEnvelopeStore()->FindEnvelope(envName) ) 
     243         140 :       return geometry->GetModuleId();
     244         834 :   }   
     245             :   
     246           0 :   return -1;
     247         140 : }  
     248             : 
     249             : 
     250             : //______________________________________________________________________________
     251             : void AliMUONVGeometryBuilder::SetTranslation(Int_t moduleId, 
     252             :                                   const TGeoTranslation& translation)
     253             : {
     254             : /// Set the translation to the geometry module given by moduleId,
     255             : /// apply reference frame transformation 
     256             : 
     257           8 :   AliMUONGeometryModule* geometry = GetGeometry(moduleId);
     258             :   
     259           4 :   if (!geometry) {
     260           0 :     AliFatal(Form("Geometry %d is not defined", moduleId)); 
     261           0 :     return;
     262             :   }
     263             :   
     264             :   // Apply frame transform
     265           8 :   TGeoHMatrix newTransform = ConvertTransform(translation);
     266             : 
     267             :   // Set new transformation
     268          12 :   geometry->SetTransformation(newTransform);
     269           8 : }  
     270             : 
     271             : 
     272             : //______________________________________________________________________________
     273             : void AliMUONVGeometryBuilder::SetTransformation(Int_t moduleId, 
     274             :                                   const TGeoTranslation& translation,
     275             :                                   const TGeoRotation& rotation)
     276             : {
     277             : /// Set the transformation to the geometry module given by moduleId,
     278             : /// apply reference frame transformation 
     279             : 
     280          32 :   AliMUONGeometryModule* geometry = GetGeometry(moduleId);
     281             :   
     282          16 :   if (!geometry) {
     283           0 :     AliFatal(Form("Geometry %d is not defined", moduleId)); 
     284           0 :     return;
     285             :   }
     286             :   
     287          16 :   TGeoCombiTrans transformation 
     288          16 :     = TGeoCombiTrans(translation, rotation);
     289             : 
     290             :   // Apply frame transform
     291          48 :   TGeoHMatrix newTransform = ConvertTransform(transformation);
     292             : 
     293             :   // Set new transformation
     294          48 :   geometry->SetTransformation(newTransform);
     295          32 : }  
     296             : 
     297             : //______________________________________________________________________________
     298             : void AliMUONVGeometryBuilder::SetVolume(Int_t moduleId, 
     299             :                                  const TString& volumeName, 
     300             :                                  Bool_t isVirtual)
     301             : {
     302             : /// Set volume name, virtuality
     303             : 
     304          20 :   TString path = GetGeometry(moduleId)->GetVolumePath();
     305             :   // cout << "in AliMUONVGeometryBuilder::SetVolume " << path.Data() << endl;
     306             :   
     307          40 :   if ( path == "" ) path = "/ALIC_1";
     308          60 :   path += ComposePath(volumeName, 1);
     309             : 
     310          40 :   GetGeometry(moduleId)->SetVolumePath(path);
     311          40 :   GetGeometry(moduleId)->SetIsVirtual(isVirtual);
     312             :   // cout << "... set " << path.Data() << endl;
     313          20 : }                                
     314             : 
     315             : //______________________________________________________________________________
     316             : void AliMUONVGeometryBuilder::SetMotherVolume(Int_t moduleId, 
     317             :                                  const TString& volumeName)
     318             : {
     319             : /// Set mother volume name
     320             : 
     321          40 :   TString motherVolumeName = ComposePath(volumeName, 1);
     322             : 
     323          40 :   TString path = GetGeometry(moduleId)->GetVolumePath();
     324          60 :   if ( path == "" ) path = "/ALIC_1";
     325          20 :   path.Insert(7, motherVolumeName);  
     326             :   
     327          40 :   GetGeometry(moduleId)->SetVolumePath(path);
     328          20 : }                                
     329             : 
     330             : //
     331             : // public functions
     332             : //
     333             : 
     334             : //______________________________________________________________________________
     335             : void  AliMUONVGeometryBuilder::SetReferenceFrame(
     336             :                                   const TGeoCombiTrans& referenceFrame)
     337             : { 
     338             : /// Set reference frame to builder and to all associated geometry 
     339             : /// modules
     340             : 
     341           8 :   fReferenceFrame = referenceFrame; 
     342             : 
     343          40 :   for (Int_t i=0; i<fGeometryModules->GetEntriesFast(); i++) {
     344             :     AliMUONGeometryModule* geometry 
     345          16 :       = (AliMUONGeometryModule*)fGeometryModules->At(i);
     346             :     AliMUONGeometryEnvelopeStore* envelopeStore 
     347          16 :       = geometry->GetEnvelopeStore();
     348             :       
     349          16 :     envelopeStore->SetReferenceFrame(referenceFrame);
     350             :   }          
     351           4 : }
     352             : 
     353             : //______________________________________________________________________________
     354             : void  AliMUONVGeometryBuilder::UpdateDetElements(Bool_t create) const
     355             : {
     356             : /// Create or update detection elements:
     357             : /// - if parameter create is true: detection elements are
     358             : /// created and their global and local transformations are filled from geometry.
     359             : /// - otherwise: only the volume path is passed from geometry
     360             : /// to detection elements
     361             : 
     362          50 :   for (Int_t i=0; i<fGeometryModules->GetEntriesFast(); i++) {
     363             :     AliMUONGeometryModule* geometry 
     364          20 :       = (AliMUONGeometryModule*)fGeometryModules->At(i);
     365             :       
     366             :     const TObjArray* envelopes 
     367          20 :       = geometry->GetEnvelopeStore()->GetEnvelopes();    
     368             :     
     369             :     AliMpExMap* detElements 
     370          20 :       = geometry->GetTransformer()->GetDetElementStore(); 
     371             :       
     372        2744 :     for (Int_t j=0; j<envelopes->GetEntriesFast(); j++) {
     373             :       AliMUONGeometryEnvelope* envelope
     374        1352 :         = (AliMUONGeometryEnvelope*)envelopes->At(j);
     375             : 
     376             :       // skip envelope not corresponding to detection element
     377        2476 :       if ( envelope->GetUniqueID() == 0) continue;
     378             :        
     379             :       // Get envelope data 
     380         228 :       Int_t detElemId = envelope->GetUniqueID();     
     381             : 
     382             :       // Compose full volume path
     383         228 :       TString volPath = geometry->GetVolumePath();
     384        1140 :       volPath += ComposePath(envelope->GetName(), envelope->GetCopyNo());
     385             : 
     386         228 :       if ( create ) {
     387             :         // Create detection element 
     388             :         AliMUONGeometryDetElement* detElement
     389         456 :           = new AliMUONGeometryDetElement(detElemId, volPath);
     390         228 :         detElements->Add(detElemId, detElement);
     391             :       
     392             :         // Compose  local transformation
     393         228 :         const TGeoCombiTrans* transform = envelope->GetTransformation(); 
     394             :         // Apply frame transform
     395         684 :         TGeoHMatrix localTransform = ConvertDETransform(*transform);
     396         228 :         detElement->SetLocalTransformation(localTransform);
     397             : 
     398             :         // Compose global transformation
     399         228 :         TGeoHMatrix globalTransform 
     400         228 :           = AliMUONGeometryBuilder::Multiply( 
     401         228 :                     (*geometry->GetTransformer()->GetTransformation()),
     402         228 :                      localTransform );
     403             :                     ;
     404             :         // Set the global transformation to detection element
     405         228 :         detElement->SetGlobalTransformation(globalTransform);
     406         228 :       }
     407             :       else {
     408             :         // Get existing det elements
     409             :         AliMUONGeometryDetElement* detElement
     410           0 :           = (AliMUONGeometryDetElement*)detElements->GetValue(detElemId);
     411             :           
     412             :         // Set volume path  
     413           0 :         detElement->SetVolumePath(volPath); 
     414             :       }
     415         228 :     }    
     416             :   }
     417           5 : }
     418             : 
     419             : //_____ _________________________________________________________________________
     420             : void  AliMUONVGeometryBuilder::RebuildSVMaps(Bool_t withEnvelopes) const
     421             : {
     422             : /// Clear the SV maps in memory and fill them from defined geometry.
     423             : 
     424           0 :   for (Int_t i=0; i<fGeometryModules->GetEntriesFast(); i++) {
     425             :     AliMUONGeometryModule* geometry 
     426           0 :       = (AliMUONGeometryModule*)fGeometryModules->At(i);
     427             :     
     428             :     // Clear the map   
     429           0 :     geometry->GetSVMap()->Clear();
     430             :      
     431             :     // Fill the map from geometry
     432             :     const TObjArray* envelopes 
     433           0 :       = geometry->GetEnvelopeStore()->GetEnvelopes();    
     434             : 
     435           0 :     for (Int_t j=0; j<envelopes->GetEntriesFast(); j++) {
     436             :       AliMUONGeometryEnvelope* envelope
     437           0 :         = (AliMUONGeometryEnvelope*)envelopes->At(j);
     438             : 
     439             :       // skip envelope not corresponding to detection element
     440           0 :       if ( envelope->GetUniqueID() == 0 ) continue;
     441             :       
     442             :       // Get volume path of detection element
     443             :       AliMUONGeometryDetElement* detElement
     444           0 :         = geometry->GetTransformer()->GetDetElement(envelope->GetUniqueID());
     445           0 :       std::string path0 = detElement->GetVolumePath().Data();        
     446             :         
     447           0 :       if ( ! withEnvelopes && geometry->IsVirtual() ) {
     448           0 :          std::string vName = geometry->GetTransformer()->GetVolumeName().Data();
     449           0 :          std::string vPath = ComposePath(vName, 1).Data();
     450           0 :          std::string::size_type vPathPos = path0.find(vPath);
     451           0 :          if ( vPathPos != std::string::npos )
     452           0 :            path0.erase(vPathPos, vPath.size());
     453           0 :       }  
     454             :        
     455           0 :       if ( ! withEnvelopes && envelope->IsVirtual()) {
     456           0 :          std::string eName = envelope->GetName();
     457           0 :          std::string ePath = ComposePath(eName, envelope->GetCopyNo()).Data();
     458           0 :          std::string::size_type ePathPos = path0.find(ePath);
     459           0 :          if ( ePathPos != std::string::npos )
     460           0 :            path0.erase(ePathPos, ePath.size());
     461           0 :       }
     462             : 
     463           0 :       if ( ! envelope->IsVirtual() )
     464           0 :         MapSV(path0, envelope->GetName(), envelope->GetUniqueID());
     465             :       else {    
     466           0 :         for  (Int_t k=0; k<envelope->GetConstituents()->GetEntriesFast(); k++) {
     467             :           AliMUONGeometryConstituent* constituent
     468           0 :             = (AliMUONGeometryConstituent*)envelope->GetConstituents()->At(k);
     469           0 :          TString path = path0;
     470           0 :          path += ComposePath(constituent->GetName(), constituent->GetCopyNo());
     471           0 :          MapSV(path, constituent->GetName(), envelope->GetUniqueID());
     472           0 :         }
     473             :       }
     474           0 :     }  
     475             :   }                  
     476           0 : }
     477             : 

Generated by: LCOV version 1.11