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