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