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