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