Removing unneeded include (Laurent)
[u/mrichter/AliRoot.git] / MUON / AliMUONVGeometryBuilder.cxx
CommitLineData
30178c30 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
d1cd2474 16// $Id$
17//
18// Class AliMUONVGeometryBuilder
19// -----------------------------
e118b27e 20// Abstract base class for geometry construction per geometry module(s).
d1cd2474 21// Author: Ivana Hrivnacova, IPN Orsay
5f1df83a 22// 23/01/2004
d1cd2474 23
d1cd2474 24#include "AliMUONVGeometryBuilder.h"
e118b27e 25#include "AliMUONGeometryModule.h"
26#include "AliMUONGeometryDetElement.h"
2811276d 27#include "AliMUONGeometryEnvelopeStore.h"
28#include "AliMUONGeometryEnvelope.h"
29#include "AliMUONGeometryConstituent.h"
067866a3 30#include "AliMUONGeometryBuilder.h"
fc01c870 31#include "AliMUONStringIntMap.h"
31607b6f 32
33#include "AliMpDEManager.h"
34#include "AliMpExMap.h"
35
8c343c7c 36#include "AliLog.h"
d1cd2474 37
31607b6f 38#include <Riostream.h>
39#include <TObjArray.h>
40#include <TSystem.h>
41#include <TGeoMatrix.h>
42#include <TVirtualMC.h>
43
a9aad96e 44/// \cond CLASSIMP
d1cd2474 45ClassImp(AliMUONVGeometryBuilder)
a9aad96e 46/// \endcond
d1cd2474 47
48//______________________________________________________________________________
6cfb12b4 49AliMUONVGeometryBuilder::AliMUONVGeometryBuilder(
31607b6f 50 Int_t firstModuleId,
51 Int_t nofModules)
d1cd2474 52 : TObject(),
6cfb12b4 53 fGeometryModules(0),
067866a3 54 fReferenceFrame()
d1cd2474 55 {
a9aad96e 56/// Standard constructor
d1cd2474 57
e118b27e 58 // Create the module geometries array
6cfb12b4 59 fGeometryModules = new TObjArray();
d1cd2474 60
31607b6f 61 for (Int_t i=0; i<nofModules; i++ )
62 fGeometryModules->Add(new AliMUONGeometryModule(firstModuleId++));
6cfb12b4 63}
d1cd2474 64
65//______________________________________________________________________________
66AliMUONVGeometryBuilder::AliMUONVGeometryBuilder()
67 : TObject(),
6cfb12b4 68 fGeometryModules(0),
067866a3 69 fReferenceFrame()
d1cd2474 70{
a9aad96e 71/// Default constructor
d1cd2474 72}
73
d1cd2474 74//______________________________________________________________________________
a9aad96e 75AliMUONVGeometryBuilder::~AliMUONVGeometryBuilder()
d1cd2474 76{
a9aad96e 77/// Destructor
d1cd2474 78
6cfb12b4 79 if (fGeometryModules) {
80 fGeometryModules->Clear(); // Sets pointers to 0 since it is not the owner
81 delete fGeometryModules;
86b48c39 82 }
d1cd2474 83}
84
d1cd2474 85//
2811276d 86// private methods
87//
88
89//______________________________________________________________________________
6cfb12b4 90TGeoHMatrix
91AliMUONVGeometryBuilder::ConvertTransform(const TGeoHMatrix& transform) const
92{
a9aad96e 93/// Convert transformation into the reference frame
6cfb12b4 94
95 if ( fReferenceFrame.IsIdentity() )
96 return transform;
97 else {
6ec9cd4e 98 return AliMUONGeometryBuilder::Multiply( fReferenceFrame,
6cfb12b4 99 transform,
6ec9cd4e 100 fReferenceFrame.Inverse() );
101 }
102}
103
104//______________________________________________________________________________
105TGeoHMatrix
106AliMUONVGeometryBuilder::ConvertDETransform(const TGeoHMatrix& transform) const
107{
a9aad96e 108/// Convert DE transformation into the reference frame
6ec9cd4e 109
110 if ( fReferenceFrame.IsIdentity() )
111 return transform;
112 else {
113 return AliMUONGeometryBuilder::Multiply( fReferenceFrame,
114 transform );
6cfb12b4 115 }
116}
117
118//______________________________________________________________________________
fc01c870 119TString AliMUONVGeometryBuilder::ComposePath(const TString& volName,
120 Int_t copyNo) const
2811276d 121{
a9aad96e 122/// Compose path from given volName and copyNo
2811276d 123
fc01c870 124 TString path = "/";
125 path += volName;
126 path += '_';
2811276d 127 path += copyNo;
128
129 return path;
130}
131
2811276d 132//______________________________________________________________________________
133void AliMUONVGeometryBuilder::MapSV(const TString& path0,
134 const TString& volName, Int_t detElemId) const
135{
a9aad96e 136/// Update the path with all daughters volumes recursively
137/// and map it to the detection element Id if it is a sensitive volume
2811276d 138
6cfb12b4 139 // Get module sensitive volumes map
31607b6f 140 Int_t moduleId = AliMpDEManager::GetGeomModuleId(detElemId);
fc01c870 141 AliMUONStringIntMap* svMap = GetSVMap(moduleId);
6cfb12b4 142
2811276d 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;
fc01c870 148 Ssiz_t npos2 = path0.Last('_');
2811276d 149 TString volName(path0(npos1, npos2-npos1));
150
151 // Check if it is sensitive volume
31607b6f 152 Int_t moduleId = AliMpDEManager::GetGeomModuleId(detElemId);
e118b27e 153 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
fc01c870 154 if ( geometry->IsSensitiveVolume(volName) &&
155 ! svMap->Get(path0) ) {
2811276d 156 //cout << ".. adding to the map "
157 // << path0 << " " << detElemId << endl;
6cfb12b4 158
159 // Map the sensitive volume to detection element
160 svMap->Add(path0, detElemId);
2811276d 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;
2811276d 170 path += ComposePath(newName, copyNo);
171
172 MapSV(path, newName, detElemId);
173 }
174}
e118b27e 175
2811276d 176//
177// protected methods
d1cd2474 178//
179
180//______________________________________________________________________________
e118b27e 181AliMUONGeometryModule*
182AliMUONVGeometryBuilder::GetGeometry(Int_t moduleId) const
d1cd2474 183{
a9aad96e 184/// Return the module geometry specified by moduleId
d1cd2474 185
6cfb12b4 186 for (Int_t i=0; i<fGeometryModules->GetEntriesFast(); i++) {
e118b27e 187
188 AliMUONGeometryModule* geometry
6cfb12b4 189 = (AliMUONGeometryModule*)fGeometryModules->At(i);
e118b27e 190
191 if ( geometry->GetModuleId() == moduleId) return geometry;
d1cd2474 192 }
193
194 return 0;
195}
2811276d 196
197//______________________________________________________________________________
198AliMUONGeometryEnvelopeStore*
e118b27e 199AliMUONVGeometryBuilder::GetEnvelopes(Int_t moduleId) const
2811276d 200{
a9aad96e 201/// Return the envelope store of the module geometry specified by moduleId
2811276d 202
e118b27e 203 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
2811276d 204
e118b27e 205 if (!geometry) {
206 AliFatal(Form("Module geometry %d is not defined", moduleId));
2811276d 207 return 0;
208 }
209
e118b27e 210 return geometry->GetEnvelopeStore();
2811276d 211}
212
213//______________________________________________________________________________
fc01c870 214AliMUONStringIntMap*
6cfb12b4 215AliMUONVGeometryBuilder::GetSVMap(Int_t moduleId) const
2811276d 216{
a9aad96e 217/// Return the transformation store of the module geometry specified by moduleId
2811276d 218
e118b27e 219 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
2811276d 220
e118b27e 221 if (!geometry) {
6cfb12b4 222 AliFatal(Form("Geometry %d is not defined", moduleId));
2811276d 223 return 0;
224 }
225
6cfb12b4 226 return geometry->GetSVMap();
2811276d 227}
228
229//______________________________________________________________________________
31607b6f 230Int_t
231AliMUONVGeometryBuilder::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//______________________________________________________________________________
6cfb12b4 249void AliMUONVGeometryBuilder::SetTranslation(Int_t moduleId,
250 const TGeoTranslation& translation)
2811276d 251{
a9aad96e 252/// Set the translation to the geometry module given by moduleId,
253/// apply reference frame transformation
2811276d 254
e118b27e 255 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
2811276d 256
e118b27e 257 if (!geometry) {
258 AliFatal(Form("Geometry %d is not defined", moduleId));
6cfb12b4 259 return;
2811276d 260 }
261
6cfb12b4 262 // Apply frame transform
263 TGeoHMatrix newTransform = ConvertTransform(translation);
264
265 // Set new transformation
266 geometry->SetTransformation(newTransform);
267}
268
269
270//______________________________________________________________________________
271void AliMUONVGeometryBuilder::SetTransformation(Int_t moduleId,
272 const TGeoTranslation& translation,
273 const TGeoRotation& rotation)
274{
a9aad96e 275/// Set the transformation to the geometry module given by moduleId,
276/// apply reference frame transformation
6cfb12b4 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
55afe185 289 TGeoHMatrix newTransform = ConvertTransform(transformation);
6cfb12b4 290
291 // Set new transformation
292 geometry->SetTransformation(newTransform);
2811276d 293}
294
fc01c870 295//______________________________________________________________________________
296void 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//______________________________________________________________________________
314void 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
2811276d 328//
329// public functions
330//
331
332//______________________________________________________________________________
6cfb12b4 333void AliMUONVGeometryBuilder::SetReferenceFrame(
334 const TGeoCombiTrans& referenceFrame)
335{
490da820 336/// Set reference frame to builder and to all associated geometry
337/// modules
338
6cfb12b4 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//______________________________________________________________________________
fc01c870 353void AliMUONVGeometryBuilder::CreateDetElements() const
2811276d 354{
a9aad96e 355/// Create detection elements and fill their global and
fc01c870 356/// local transformations from geometry.
2811276d 357
6cfb12b4 358 for (Int_t i=0; i<fGeometryModules->GetEntriesFast(); i++) {
e118b27e 359 AliMUONGeometryModule* geometry
6cfb12b4 360 = (AliMUONGeometryModule*)fGeometryModules->At(i);
fc01c870 361
2811276d 362 const TObjArray* envelopes
e118b27e 363 = geometry->GetEnvelopeStore()->GetEnvelopes();
364
31607b6f 365 AliMpExMap* detElements
6cfb12b4 366 = geometry->GetTransformer()->GetDetElementStore();
e118b27e 367
2811276d 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
6ec9cd4e 373 if ( envelope->GetUniqueID() == 0) continue;
2811276d 374
375 // Get envelope data
376 Int_t detElemId = envelope->GetUniqueID();
2811276d 377
fc01c870 378 // Compose full volume path
379 TString volPath = geometry->GetVolumePath();
380 volPath += ComposePath(envelope->GetName(), envelope->GetCopyNo());
381
382 // Create detection element
6ec9cd4e 383 AliMUONGeometryDetElement* detElement
fc01c870 384 = new AliMUONGeometryDetElement(detElemId, volPath);
6ec9cd4e 385 detElements->Add(detElemId, detElement);
31607b6f 386
fc01c870 387 // Compose local transformation
388 const TGeoCombiTrans* transform = envelope->GetTransformation();
389 // Apply frame transform
390 TGeoHMatrix localTransform = ConvertDETransform(*transform);
391 detElement->SetLocalTransformation(localTransform);
392
6ec9cd4e 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);
fc01c870 401
2811276d 402 }
403 }
404}
2811276d 405//_____ _________________________________________________________________________
fc01c870 406void AliMUONVGeometryBuilder::RebuildSVMaps(Bool_t withEnvelopes) const
2811276d 407{
a9aad96e 408/// Clear the SV maps in memory and fill them from defined geometry.
2811276d 409
6cfb12b4 410 for (Int_t i=0; i<fGeometryModules->GetEntriesFast(); i++) {
e118b27e 411 AliMUONGeometryModule* geometry
6cfb12b4 412 = (AliMUONGeometryModule*)fGeometryModules->At(i);
2811276d 413
414 // Clear the map
e118b27e 415 geometry->GetSVMap()->Clear();
2811276d 416
417 // Fill the map from geometry
418 const TObjArray* envelopes
e118b27e 419 = geometry->GetEnvelopeStore()->GetEnvelopes();
2811276d 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
fc01c870 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());
d639fb9f 437 }
2811276d 438
fc01c870 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());
2811276d 443 }
fc01c870 444
445 if ( ! envelope->IsVirtual() )
446 MapSV(path0, envelope->GetName(), envelope->GetUniqueID());
447 else {
2811276d 448 for (Int_t k=0; k<envelope->GetConstituents()->GetEntriesFast(); k++) {
449 AliMUONGeometryConstituent* constituent
450 = (AliMUONGeometryConstituent*)envelope->GetConstituents()->At(k);
451 TString path = path0;
2811276d 452 path += ComposePath(constituent->GetName(), constituent->GetCopyNo());
453 MapSV(path, constituent->GetName(), envelope->GetUniqueID());
454 }
455 }
456 }
457 }
458}
459