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