Update HFE v2 analyses
[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
2942f542 146 Int_t nofDaughters = TVirtualMC::GetMC()->NofVolDaughters(volName);
2811276d 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('_');
14a02207 152 TString volName0(path0(npos1, npos2-npos1));
2811276d 153
154 // Check if it is sensitive volume
e118b27e 155 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
14a02207 156 if ( geometry->IsSensitiveVolume(volName0) &&
fc01c870 157 ! svMap->Get(path0) ) {
2811276d 158 //cout << ".. adding to the map "
159 // << path0 << " " << detElemId << endl;
6cfb12b4 160
161 // Map the sensitive volume to detection element
162 svMap->Add(path0, detElemId);
2811276d 163 }
164 return;
165 }
166
167 for (Int_t i=0; i<nofDaughters; i++) {
2942f542 168 Int_t copyNo = TVirtualMC::GetMC()->VolDaughterCopyNo(volName, i);
169 TString newName = TVirtualMC::GetMC()->VolDaughterName(volName, i);
2811276d 170
171 TString path = path0;
2811276d 172 path += ComposePath(newName, copyNo);
173
174 MapSV(path, newName, detElemId);
175 }
176}
e118b27e 177
2811276d 178//
179// protected methods
d1cd2474 180//
181
182//______________________________________________________________________________
e118b27e 183AliMUONGeometryModule*
184AliMUONVGeometryBuilder::GetGeometry(Int_t moduleId) const
d1cd2474 185{
a9aad96e 186/// Return the module geometry specified by moduleId
d1cd2474 187
6cfb12b4 188 for (Int_t i=0; i<fGeometryModules->GetEntriesFast(); i++) {
e118b27e 189
190 AliMUONGeometryModule* geometry
6cfb12b4 191 = (AliMUONGeometryModule*)fGeometryModules->At(i);
e118b27e 192
193 if ( geometry->GetModuleId() == moduleId) return geometry;
d1cd2474 194 }
195
196 return 0;
197}
2811276d 198
199//______________________________________________________________________________
200AliMUONGeometryEnvelopeStore*
e118b27e 201AliMUONVGeometryBuilder::GetEnvelopes(Int_t moduleId) const
2811276d 202{
a9aad96e 203/// Return the envelope store of the module geometry specified by moduleId
2811276d 204
e118b27e 205 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
2811276d 206
e118b27e 207 if (!geometry) {
208 AliFatal(Form("Module geometry %d is not defined", moduleId));
2811276d 209 return 0;
210 }
211
e118b27e 212 return geometry->GetEnvelopeStore();
2811276d 213}
214
215//______________________________________________________________________________
fc01c870 216AliMUONStringIntMap*
6cfb12b4 217AliMUONVGeometryBuilder::GetSVMap(Int_t moduleId) const
2811276d 218{
a9aad96e 219/// Return the transformation store of the module geometry specified by moduleId
2811276d 220
e118b27e 221 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
2811276d 222
e118b27e 223 if (!geometry) {
6cfb12b4 224 AliFatal(Form("Geometry %d is not defined", moduleId));
2811276d 225 return 0;
226 }
227
6cfb12b4 228 return geometry->GetSVMap();
2811276d 229}
230
231//______________________________________________________________________________
31607b6f 232Int_t
233AliMUONVGeometryBuilder::GetModuleId(const TString& envName) const
234{
235/// Return module Id which has the envelope with given name
236
237 for (Int_t i=0; i<fGeometryModules->GetEntriesFast(); i++) {
238
239 AliMUONGeometryModule* geometry
240 = (AliMUONGeometryModule*)fGeometryModules->At(i);
241
242 if ( geometry->GetEnvelopeStore()->FindEnvelope(envName) )
243 return geometry->GetModuleId();
244 }
245
246 return -1;
247}
248
249
250//______________________________________________________________________________
6cfb12b4 251void AliMUONVGeometryBuilder::SetTranslation(Int_t moduleId,
252 const TGeoTranslation& translation)
2811276d 253{
a9aad96e 254/// Set the translation to the geometry module given by moduleId,
255/// apply reference frame transformation
2811276d 256
e118b27e 257 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
2811276d 258
e118b27e 259 if (!geometry) {
260 AliFatal(Form("Geometry %d is not defined", moduleId));
6cfb12b4 261 return;
2811276d 262 }
263
6cfb12b4 264 // Apply frame transform
265 TGeoHMatrix newTransform = ConvertTransform(translation);
266
267 // Set new transformation
268 geometry->SetTransformation(newTransform);
269}
270
271
272//______________________________________________________________________________
273void AliMUONVGeometryBuilder::SetTransformation(Int_t moduleId,
274 const TGeoTranslation& translation,
275 const TGeoRotation& rotation)
276{
a9aad96e 277/// Set the transformation to the geometry module given by moduleId,
278/// apply reference frame transformation
6cfb12b4 279
280 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
281
282 if (!geometry) {
283 AliFatal(Form("Geometry %d is not defined", moduleId));
284 return;
285 }
286
287 TGeoCombiTrans transformation
288 = TGeoCombiTrans(translation, rotation);
289
290 // Apply frame transform
55afe185 291 TGeoHMatrix newTransform = ConvertTransform(transformation);
6cfb12b4 292
293 // Set new transformation
294 geometry->SetTransformation(newTransform);
2811276d 295}
296
fc01c870 297//______________________________________________________________________________
298void AliMUONVGeometryBuilder::SetVolume(Int_t moduleId,
299 const TString& volumeName,
300 Bool_t isVirtual)
301{
302/// Set volume name, virtuality
303
304 TString path = GetGeometry(moduleId)->GetVolumePath();
305 // cout << "in AliMUONVGeometryBuilder::SetVolume " << path.Data() << endl;
306
307 if ( path == "" ) path = "/ALIC_1";
308 path += ComposePath(volumeName, 1);
309
310 GetGeometry(moduleId)->SetVolumePath(path);
311 GetGeometry(moduleId)->SetIsVirtual(isVirtual);
312 // cout << "... set " << path.Data() << endl;
313}
314
315//______________________________________________________________________________
316void AliMUONVGeometryBuilder::SetMotherVolume(Int_t moduleId,
317 const TString& volumeName)
318{
319/// Set mother volume name
320
321 TString motherVolumeName = ComposePath(volumeName, 1);
322
323 TString path = GetGeometry(moduleId)->GetVolumePath();
324 if ( path == "" ) path = "/ALIC_1";
325 path.Insert(7, motherVolumeName);
326
327 GetGeometry(moduleId)->SetVolumePath(path);
328}
329
2811276d 330//
331// public functions
332//
333
334//______________________________________________________________________________
6cfb12b4 335void AliMUONVGeometryBuilder::SetReferenceFrame(
336 const TGeoCombiTrans& referenceFrame)
337{
490da820 338/// Set reference frame to builder and to all associated geometry
339/// modules
340
6cfb12b4 341 fReferenceFrame = referenceFrame;
342
343 for (Int_t i=0; i<fGeometryModules->GetEntriesFast(); i++) {
344 AliMUONGeometryModule* geometry
345 = (AliMUONGeometryModule*)fGeometryModules->At(i);
346 AliMUONGeometryEnvelopeStore* envelopeStore
347 = geometry->GetEnvelopeStore();
348
349 envelopeStore->SetReferenceFrame(referenceFrame);
350 }
351}
352
6cfb12b4 353//______________________________________________________________________________
b96f7067 354void AliMUONVGeometryBuilder::UpdateDetElements(Bool_t create) const
2811276d 355{
b96f7067 356/// Create or update detection elements:
357/// - if parameter create is true: detection elements are
358/// created and their global and local transformations are filled from geometry.
359/// - otherwise: only the volume path is passed from geometry
360/// to detection elements
2811276d 361
6cfb12b4 362 for (Int_t i=0; i<fGeometryModules->GetEntriesFast(); i++) {
e118b27e 363 AliMUONGeometryModule* geometry
6cfb12b4 364 = (AliMUONGeometryModule*)fGeometryModules->At(i);
fc01c870 365
2811276d 366 const TObjArray* envelopes
e118b27e 367 = geometry->GetEnvelopeStore()->GetEnvelopes();
368
31607b6f 369 AliMpExMap* detElements
6cfb12b4 370 = geometry->GetTransformer()->GetDetElementStore();
e118b27e 371
2811276d 372 for (Int_t j=0; j<envelopes->GetEntriesFast(); j++) {
373 AliMUONGeometryEnvelope* envelope
374 = (AliMUONGeometryEnvelope*)envelopes->At(j);
375
376 // skip envelope not corresponding to detection element
6ec9cd4e 377 if ( envelope->GetUniqueID() == 0) continue;
2811276d 378
379 // Get envelope data
380 Int_t detElemId = envelope->GetUniqueID();
2811276d 381
fc01c870 382 // Compose full volume path
383 TString volPath = geometry->GetVolumePath();
384 volPath += ComposePath(envelope->GetName(), envelope->GetCopyNo());
385
b96f7067 386 if ( create ) {
387 // Create detection element
388 AliMUONGeometryDetElement* detElement
389 = new AliMUONGeometryDetElement(detElemId, volPath);
390 detElements->Add(detElemId, detElement);
31607b6f 391
b96f7067 392 // Compose local transformation
393 const TGeoCombiTrans* transform = envelope->GetTransformation();
394 // Apply frame transform
395 TGeoHMatrix localTransform = ConvertDETransform(*transform);
396 detElement->SetLocalTransformation(localTransform);
397
398 // Compose global transformation
399 TGeoHMatrix globalTransform
400 = AliMUONGeometryBuilder::Multiply(
6ec9cd4e 401 (*geometry->GetTransformer()->GetTransformation()),
402 localTransform );
403 ;
b96f7067 404 // Set the global transformation to detection element
405 detElement->SetGlobalTransformation(globalTransform);
406 }
407 else {
408 // Get existing det elements
409 AliMUONGeometryDetElement* detElement
410 = (AliMUONGeometryDetElement*)detElements->GetValue(detElemId);
411
412 // Set volume path
413 detElement->SetVolumePath(volPath);
414 }
415 }
2811276d 416 }
417}
b96f7067 418
2811276d 419//_____ _________________________________________________________________________
fc01c870 420void AliMUONVGeometryBuilder::RebuildSVMaps(Bool_t withEnvelopes) const
2811276d 421{
a9aad96e 422/// Clear the SV maps in memory and fill them from defined geometry.
2811276d 423
6cfb12b4 424 for (Int_t i=0; i<fGeometryModules->GetEntriesFast(); i++) {
e118b27e 425 AliMUONGeometryModule* geometry
6cfb12b4 426 = (AliMUONGeometryModule*)fGeometryModules->At(i);
2811276d 427
428 // Clear the map
e118b27e 429 geometry->GetSVMap()->Clear();
2811276d 430
431 // Fill the map from geometry
432 const TObjArray* envelopes
e118b27e 433 = geometry->GetEnvelopeStore()->GetEnvelopes();
2811276d 434
435 for (Int_t j=0; j<envelopes->GetEntriesFast(); j++) {
436 AliMUONGeometryEnvelope* envelope
437 = (AliMUONGeometryEnvelope*)envelopes->At(j);
438
439 // skip envelope not corresponding to detection element
fc01c870 440 if ( envelope->GetUniqueID() == 0 ) continue;
441
442 // Get volume path of detection element
443 AliMUONGeometryDetElement* detElement
444 = geometry->GetTransformer()->GetDetElement(envelope->GetUniqueID());
445 std::string path0 = detElement->GetVolumePath().Data();
446
447 if ( ! withEnvelopes && geometry->IsVirtual() ) {
448 std::string vName = geometry->GetTransformer()->GetVolumeName().Data();
449 std::string vPath = ComposePath(vName, 1).Data();
1a8a2a9e 450 std::string::size_type vPathPos = path0.find(vPath);
451 if ( vPathPos != std::string::npos )
452 path0.erase(vPathPos, vPath.size());
d639fb9f 453 }
2811276d 454
fc01c870 455 if ( ! withEnvelopes && envelope->IsVirtual()) {
456 std::string eName = envelope->GetName();
457 std::string ePath = ComposePath(eName, envelope->GetCopyNo()).Data();
1a8a2a9e 458 std::string::size_type ePathPos = path0.find(ePath);
459 if ( ePathPos != std::string::npos )
5d48f035 460 path0.erase(ePathPos, ePath.size());
2811276d 461 }
fc01c870 462
463 if ( ! envelope->IsVirtual() )
464 MapSV(path0, envelope->GetName(), envelope->GetUniqueID());
465 else {
2811276d 466 for (Int_t k=0; k<envelope->GetConstituents()->GetEntriesFast(); k++) {
467 AliMUONGeometryConstituent* constituent
468 = (AliMUONGeometryConstituent*)envelope->GetConstituents()->At(k);
469 TString path = path0;
2811276d 470 path += ComposePath(constituent->GetName(), constituent->GetCopyNo());
471 MapSV(path, constituent->GetName(), envelope->GetUniqueID());
472 }
473 }
474 }
475 }
476}
477