]> git.uio.no Git - u/mrichter/AliRoot.git/blame_incremental - MUON/AliMUONGeometryBuilder.cxx
Added protection and 2 levels for problems
[u/mrichter/AliRoot.git] / MUON / AliMUONGeometryBuilder.cxx
... / ...
CommitLineData
1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * SigmaEffect_thetadegrees *
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 purpeateose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
16// $Id$
17//
18// ----------------------------
19// Class AliMUONGeometryBuilder
20// ----------------------------
21// Manager class for geometry construction via geometry builders.
22// Author: Ivana Hrivnacova, IPN Orsay
23
24#include "AliMUONGeometryBuilder.h"
25#include "AliMUONVGeometryBuilder.h"
26#include "AliMUONGeometry.h"
27#include "AliMUONGeometryTransformer.h"
28#include "AliMUONGeometryModule.h"
29#include "AliMUONGeometryModuleTransformer.h"
30#include "AliMUONGeometryEnvelope.h"
31#include "AliMUONGeometryEnvelopeStore.h"
32#include "AliMUONGeometryDetElement.h"
33#include "AliMUONGeometryConstituent.h"
34
35#include "AliMpDEManager.h"
36
37#include "AliModule.h"
38#include "AliLog.h"
39#include "AliRun.h"
40
41#include <TObjArray.h>
42#include <TVirtualMC.h>
43#include <TGeoManager.h>
44
45// static data members
46
47const TString AliMUONGeometryBuilder::fgkDefaultVolPathsFileName = "volpath.dat";
48const TString AliMUONGeometryBuilder::fgkDefaultTransformFileName = "transform.dat";
49const TString AliMUONGeometryBuilder::fgkDefaultSVMapFileName = "svmap.dat";
50const TString AliMUONGeometryBuilder::fgkOutFileNameExtension = ".out";
51
52/// \cond CLASSIMP
53ClassImp(AliMUONGeometryBuilder)
54/// \endcond
55
56// static functions
57
58//______________________________________________________________________________
59TGeoHMatrix AliMUONGeometryBuilder::Multiply(const TGeoMatrix& m1,
60 const TGeoMatrix& m2)
61{
62/// Temporary fix for problem with matrix multiplication in Root 5.02/00
63
64 if (m1.IsIdentity() && m2.IsIdentity()) return TGeoHMatrix();
65
66 if (m1.IsIdentity()) return m2;
67
68 if (m2.IsIdentity()) return m1;
69
70 return m1 * m2;
71}
72
73//______________________________________________________________________________
74TGeoHMatrix AliMUONGeometryBuilder::Multiply(const TGeoMatrix& m1,
75 const TGeoMatrix& m2,
76 const TGeoMatrix& m3)
77{
78/// Temporary fix for problem with matrix multiplication in Root 5.02/00
79
80 if (m1.IsIdentity() && m2.IsIdentity() & m3.IsIdentity())
81 return TGeoHMatrix();
82
83 if (m1.IsIdentity()) return Multiply(m2, m3);
84
85 if (m2.IsIdentity()) return Multiply(m1, m3);
86
87 if (m3.IsIdentity()) return Multiply(m1, m2);
88
89 return m1 * m2 * m3;
90}
91
92//______________________________________________________________________________
93TGeoHMatrix AliMUONGeometryBuilder::Multiply(const TGeoMatrix& m1,
94 const TGeoMatrix& m2,
95 const TGeoMatrix& m3,
96 const TGeoMatrix& m4)
97{
98/// Temporary fix for problem with matrix multiplication in Root 5.02/00
99
100 if (m1.IsIdentity() && m2.IsIdentity() & m3.IsIdentity() & m4.IsIdentity())
101 return TGeoHMatrix();
102
103 if (m1.IsIdentity()) return Multiply(m2, m3, m4);
104
105 if (m2.IsIdentity()) return Multiply(m1, m3, m4);
106
107 if (m3.IsIdentity()) return Multiply(m1, m2, m4);
108
109 if (m4.IsIdentity()) return Multiply(m1, m2, m3);
110
111 return m1 * m2 * m3 * m4;
112}
113
114//______________________________________________________________________________
115AliMUONGeometryBuilder::AliMUONGeometryBuilder(AliModule* module)
116 : TObject(),
117 fModule(module),
118 fAlign(false),
119 fTransformFileName(fgkDefaultTransformFileName),
120 fSVMapFileName(fgkDefaultSVMapFileName),
121 fGlobalTransformation(),
122 fGeometryBuilders(0),
123 fGeometry(0)
124{
125/// Standard constructor
126
127 fGeometryBuilders = new TObjArray();
128 fGeometryBuilders->SetOwner(true);
129
130 fGeometry = new AliMUONGeometry(true);
131
132 // Define the global transformation:
133 // Transformation from the old ALICE coordinate system to a new one:
134 // x->-x, z->-z
135 TGeoRotation* rotGlobal
136 = new TGeoRotation("rotGlobal", 90., 180., 90., 90., 180., 0.);
137 fGlobalTransformation = TGeoCombiTrans(0., 0., 0., rotGlobal);
138}
139
140//______________________________________________________________________________
141AliMUONGeometryBuilder::AliMUONGeometryBuilder()
142 : TObject(),
143 fModule(0),
144 fAlign(false),
145 fTransformFileName(),
146 fSVMapFileName(),
147 fGlobalTransformation(),
148 fGeometryBuilders(0),
149 fGeometry(0)
150{
151/// Default constructor
152}
153
154//______________________________________________________________________________
155AliMUONGeometryBuilder::~AliMUONGeometryBuilder()
156{
157/// Destructor
158
159 delete fGeometryBuilders;
160 delete fGeometry;
161}
162
163//
164// private functions
165//
166
167//______________________________________________________________________________
168void AliMUONGeometryBuilder::PlaceVolume(const TString& name, const TString& mName,
169 Int_t copyNo, const TGeoHMatrix& matrix,
170 Int_t npar, Double_t* param, const char* only,
171 Bool_t makeAssembly) const
172{
173/// Place the volume specified by name with the given transformation matrix
174
175 if (makeAssembly)
176 gGeoManager->MakeVolumeAssembly(name.Data());
177
178 TGeoHMatrix transform(matrix);
179 // Do not apply global transformation
180 // if mother volume was already placed in
181 // the new system of coordinates (that is MUON in negative Z)
182 // (as it is applied on the mother volume)
183 if (mName == TString("DDIP"))
184 transform = fGlobalTransformation.Inverse() * transform;
185
186 // Decompose transformation
187 const Double_t* xyz = transform.GetTranslation();
188 const Double_t* rm = transform.GetRotationMatrix();
189
190 //cout << "Got translation: "
191 // << xyz[0] << " " << xyz[1] << " " << xyz[2] << endl;
192
193 //cout << "Got rotation: "
194 // << rm[0] << " " << rm[1] << " " << rm[2] << endl
195 // << rm[3] << " " << rm[4] << " " << rm[5] << endl
196 // << rm[6] << " " << rm[7] << " " << rm[8] << endl;
197
198 // Check for presence of rotation
199 // (will be nice to be available in TGeo)
200 const Double_t kTolerance = 1e-04;
201 Bool_t isRotation = true;
202 if (TMath::Abs(rm[0] - 1.) < kTolerance &&
203 TMath::Abs(rm[1] - 0.) < kTolerance &&
204 TMath::Abs(rm[2] - 0.) < kTolerance &&
205 TMath::Abs(rm[3] - 0.) < kTolerance &&
206 TMath::Abs(rm[4] - 1.) < kTolerance &&
207 TMath::Abs(rm[5] - 0.) < kTolerance &&
208 TMath::Abs(rm[6] - 0.) < kTolerance &&
209 TMath::Abs(rm[7] - 0.) < kTolerance &&
210 TMath::Abs(rm[8] - 1.) < kTolerance) isRotation = false;
211
212 Int_t krot = 0;
213 if (isRotation) {
214 TGeoRotation rot;
215 rot.SetMatrix(const_cast<Double_t*>(transform.GetRotationMatrix()));
216 Double_t theta1, phi1, theta2, phi2, theta3, phi3;
217 rot.GetAngles(theta1, phi1, theta2, phi2, theta3, phi3);
218
219 //cout << "angles: "
220 // << theta1 << " " << phi1 << " "
221 // << theta2 << " " << phi2 << " "
222 // << theta3 << " " << phi3 << endl;
223
224 fModule->AliMatrix(krot, theta1, phi1, theta2, phi2, theta3, phi3);
225 }
226
227 // Place the volume
228 if (npar == 0)
229 gMC->Gspos(name, copyNo, mName, xyz[0], xyz[1], xyz[2] , krot, only);
230 else
231 gMC->Gsposp(name, copyNo, mName, xyz[0], xyz[1], xyz[2] , krot, only,
232 param, npar);
233}
234
235//______________________________________________________________________________
236void AliMUONGeometryBuilder::CreateGeometryWithTGeo()
237{
238/// Construct geometry using geometry builders.
239/// Virtual modules/envelopes are placed as TGeoVolume assembly
240
241 if (fAlign) {
242 // Read transformations from ASCII data file
243 fGeometry->GetTransformer()
244 ->ReadGeometryData(fgkDefaultVolPathsFileName, fTransformFileName);
245 }
246
247 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
248
249 // Get the builder
250 AliMUONVGeometryBuilder* builder
251 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
252
253 // Create geometry + envelopes
254 //
255 builder->CreateGeometry();
256 if (!fAlign) builder->SetTransformations();
257
258 // Place module volumes and envelopes
259 //
260 for (Int_t j=0; j<builder->NofGeometries(); j++) {
261
262 AliMUONGeometryModule* geometry = builder->Geometry(j);
263 AliMUONGeometryModuleTransformer* transformer= geometry->GetTransformer();
264 const TGeoHMatrix* kModuleTransform = transformer->GetTransformation();
265 TString volName = transformer->GetVolumeName();
266 TString motherVolName = transformer->GetMotherVolumeName();
267
268 // Place the module volume
269 PlaceVolume(volName, motherVolName,
270 1, *kModuleTransform, 0, 0, "ONLY", geometry->IsVirtual());
271
272 TGeoCombiTrans appliedGlobalTransform;
273 if (builder->ApplyGlobalTransformation())
274 appliedGlobalTransform = fGlobalTransformation;
275
276 // Loop over envelopes
277 const TObjArray* kEnvelopes
278 = geometry->GetEnvelopeStore()->GetEnvelopes();
279 for (Int_t k=0; k<kEnvelopes->GetEntriesFast(); k++) {
280
281 // Get envelope
282 AliMUONGeometryEnvelope* env
283 = (AliMUONGeometryEnvelope*)kEnvelopes->At(k);
284
285 // Check consistency of detElemId and module Id
286 if ( env->GetUniqueID() > 0 &&
287 AliMpDEManager::GetGeomModuleId(env->GetUniqueID())
288 != geometry->GetModuleId() ) {
289
290 AliErrorStream()
291 << "Detection element " << env->GetUniqueID()
292 << " is being placed in geometry module " << geometry->GetModuleId()
293 << " but should go in "
294 << AliMpDEManager::GetGeomModuleId(env->GetUniqueID())
295 << endl;
296 AliFatal("Inconsistent IDs");
297 }
298
299 const TGeoCombiTrans* kEnvTrans = env->GetTransformation();
300 const char* only = "ONLY";
301 if (env->IsMANY()) only = "MANY";
302
303 if (env->IsVirtual() && env->GetConstituents()->GetEntriesFast() == 0 ) {
304 // virtual envelope + nof constituents = 0
305 // => not allowed;
306 // empty virtual envelope has no sense
307 AliFatal("Virtual envelope must have constituents.");
308 return;
309 }
310
311 if (!env->IsVirtual() && env->GetConstituents()->GetEntriesFast() > 0 ) {
312 // non virtual envelope + nof constituents > 0
313 // => not allowed;
314 // use VMC to place constituents
315 AliFatal("Non virtual envelope cannot have constituents.");
316 return;
317 }
318
319 // Place envelope in geometry module by composed transformation:
320 // [Tglobal] * Tenv
321 TGeoHMatrix total
322 = Multiply( appliedGlobalTransform,
323 (*kEnvTrans) );
324 PlaceVolume(env->GetName(), volName,
325 env->GetCopyNo(), total, 0, 0, only, env->IsVirtual());
326
327 if ( env->IsVirtual() ) {
328 // Place constituents in the envelope
329 for (Int_t l=0; l<env->GetConstituents()->GetEntriesFast(); l++) {
330 AliMUONGeometryConstituent* constituent
331 = (AliMUONGeometryConstituent*)env->GetConstituents()->At(l);
332
333 PlaceVolume(constituent->GetName(), env->GetName(),
334 constituent->GetCopyNo(),
335 *constituent->GetTransformation() ,
336 constituent->GetNpar(), constituent->GetParam(), only);
337 }
338 }
339 } // end of loop over envelopes
340 } // end of loop over builder geometries
341 } // end of loop over builders
342}
343
344//______________________________________________________________________________
345void AliMUONGeometryBuilder::CreateGeometryWithoutTGeo()
346{
347/// Construct geometry using geometry builders.
348/// Virtual modules/envelopes are not placed
349
350 if (fAlign) {
351 // Read transformations from ASCII data file
352 fGeometry->GetTransformer()
353 ->ReadGeometryData(fgkDefaultVolPathsFileName, fTransformFileName);
354 }
355
356 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
357
358 // Get the builder
359 AliMUONVGeometryBuilder* builder
360 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
361
362 // Create geometry + envelopes
363 //
364 builder->CreateGeometry();
365 if (!fAlign) builder->SetTransformations();
366
367 // Place module volumes and envelopes
368 //
369 for (Int_t j=0; j<builder->NofGeometries(); j++) {
370
371 AliMUONGeometryModule* geometry = builder->Geometry(j);
372 AliMUONGeometryModuleTransformer* transformer= geometry->GetTransformer();
373 const TGeoHMatrix* kModuleTransform = transformer->GetTransformation();
374 TString volName = transformer->GetVolumeName();
375 TString motherVolName = transformer->GetMotherVolumeName();
376
377 // Place the module volume
378 if ( !geometry->IsVirtual() ) {
379 PlaceVolume(volName, motherVolName,
380 1, *kModuleTransform, 0, 0, "ONLY");
381 }
382
383 TGeoCombiTrans appliedGlobalTransform;
384 if (builder->ApplyGlobalTransformation())
385 appliedGlobalTransform = fGlobalTransformation;
386
387 // Loop over envelopes
388 const TObjArray* kEnvelopes
389 = geometry->GetEnvelopeStore()->GetEnvelopes();
390 for (Int_t k=0; k<kEnvelopes->GetEntriesFast(); k++) {
391
392 // Get envelope
393 AliMUONGeometryEnvelope* env
394 = (AliMUONGeometryEnvelope*)kEnvelopes->At(k);
395
396 // Check consistency of detElemId and module Id
397 if ( env->GetUniqueID() > 0 &&
398 AliMpDEManager::GetGeomModuleId(env->GetUniqueID())
399 != geometry->GetModuleId() ) {
400
401 AliErrorStream()
402 << "Detection element " << env->GetUniqueID()
403 << " is being placed in geometry module " << geometry->GetModuleId()
404 << " but should go in "
405 << AliMpDEManager::GetGeomModuleId(env->GetUniqueID())
406 << endl;
407 AliFatal("Inconsistent IDs");
408 }
409
410 const TGeoCombiTrans* kEnvTrans = env->GetTransformation();
411 const char* only = "ONLY";
412 if (env->IsMANY()) only = "MANY";
413
414 if (env->IsVirtual() && env->GetConstituents()->GetEntriesFast() == 0 ) {
415 // virtual envelope + nof constituents = 0
416 // => not allowed;
417 // empty virtual envelope has no sense
418 AliFatal("Virtual envelope must have constituents.");
419 return;
420 }
421
422 if (!env->IsVirtual() && env->GetConstituents()->GetEntriesFast() > 0 ) {
423 // non virtual envelope + nof constituents > 0
424 // => not allowed;
425 // use VMC to place constituents
426 AliFatal("Non virtual envelope cannot have constituents.");
427 return;
428 }
429
430 if (!env->IsVirtual() && env->GetConstituents()->GetEntriesFast() == 0 ) {
431 // non virtual envelope + nof constituents = 0
432 // => place envelope by composed transformation:
433 // Tch * [Tglobal] * Tenv
434
435 // Compound chamber transformation with the envelope one
436 if (geometry->IsVirtual()) {
437 TGeoHMatrix total
438 = Multiply( (*kModuleTransform),
439 appliedGlobalTransform,
440 (*kEnvTrans) );
441 PlaceVolume(env->GetName(), motherVolName,
442 env->GetCopyNo(), total, 0, 0, only);
443 }
444 else {
445 TGeoHMatrix total
446 = Multiply( appliedGlobalTransform,
447 (*kEnvTrans) );
448 PlaceVolume(env->GetName(), volName,
449 env->GetCopyNo(), total, 0, 0, only);
450 }
451 }
452
453 if (env->IsVirtual() && env->GetConstituents()->GetEntriesFast() > 0 ) {
454 // virtual envelope + nof constituents > 0
455 // => do not place envelope and place constituents
456 // by composed transformation:
457 // Tch * [Tglobal] * Tenv * Tconst
458
459 for (Int_t l=0; l<env->GetConstituents()->GetEntriesFast(); l++) {
460 AliMUONGeometryConstituent* constituent
461 = (AliMUONGeometryConstituent*)env->GetConstituents()->At(l);
462
463 // Compound chamber transformation with the envelope one + the constituent one
464 if (geometry->IsVirtual()) {
465 TGeoHMatrix total
466 = Multiply ( (*kModuleTransform),
467 appliedGlobalTransform,
468 (*kEnvTrans),
469 (*constituent->GetTransformation()) );
470
471 PlaceVolume(constituent->GetName(), motherVolName,
472 constituent->GetCopyNo(), total,
473 constituent->GetNpar(), constituent->GetParam(), only);
474 }
475 else {
476 TGeoHMatrix total
477 = Multiply ( appliedGlobalTransform,
478 (*kEnvTrans),
479 (*constituent->GetTransformation()) );
480
481 PlaceVolume(constituent->GetName(), volName,
482 constituent->GetCopyNo(), total,
483 constituent->GetNpar(), constituent->GetParam(), only);
484 }
485 }
486 }
487 } // end of loop over envelopes
488 } // end of loop over builder geometries
489 } // end of loop over builders
490}
491
492//_____________________________________________________________________________
493void AliMUONGeometryBuilder::SetAlign(AliMUONVGeometryBuilder* builder)
494{
495/// Set align option to all geometry modules associated with the builder
496
497 for (Int_t j=0; j<builder->NofGeometries(); j++) {
498
499 AliMUONGeometryModule* geometry = builder->Geometry(j);
500
501 geometry->SetAlign(fAlign);
502 }
503}
504
505//
506// public functions
507//
508
509//_____________________________________________________________________________
510void AliMUONGeometryBuilder::AddBuilder(AliMUONVGeometryBuilder* geomBuilder)
511{
512/// Add the geometry builder to the list
513
514 fGeometryBuilders->Add(geomBuilder);
515
516 // Pass geometry modules created in the to the geometry parametrisation
517 for (Int_t i=0; i<geomBuilder->NofGeometries(); i++) {
518 fGeometry->AddModule(geomBuilder->Geometry(i));
519 }
520
521 if (geomBuilder->ApplyGlobalTransformation())
522 geomBuilder->SetReferenceFrame(fGlobalTransformation);
523
524 SetAlign(geomBuilder);
525}
526
527//______________________________________________________________________________
528void AliMUONGeometryBuilder::CreateGeometry()
529{
530/// Construct geometry using geometry builders.
531
532 if ( gMC->IsRootGeometrySupported() ) {
533
534 CreateGeometryWithTGeo();
535 }
536 else
537 CreateGeometryWithoutTGeo();
538}
539
540//_____________________________________________________________________________
541void AliMUONGeometryBuilder::CreateMaterials()
542{
543/// Construct materials specific to modules via builders
544
545 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
546
547 // Get the builder
548 AliMUONVGeometryBuilder* builder
549 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
550
551 // Create materials with each builder
552 if (builder) builder->CreateMaterials();
553 }
554}
555
556//______________________________________________________________________________
557void AliMUONGeometryBuilder::InitGeometry(const TString& svmapFileName)
558{
559/// Initialize geometry
560
561 // Load alignement data from geometry if geometry is read from Root file
562 if ( gAlice->IsRootGeometry() ) {
563 fAlign = true;
564
565 fGeometry->GetTransformer()
566 ->ReadGeometryData(fgkDefaultVolPathsFileName, gGeoManager);
567 }
568
569 // Read sensitive volume map from a file
570 fGeometry->ReadSVMap(svmapFileName);
571
572 // Set the chamber (sensitive region) GEANT identifier
573 //
574 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
575
576 // Get the builder
577 AliMUONVGeometryBuilder* builder
578 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
579
580 // Set sensitive volumes with each builder
581 builder->SetSensitiveVolumes();
582
583 if (!fAlign) {
584 // Create detection elements from built geometry
585 builder->CreateDetElements();
586 }
587 }
588}
589
590//______________________________________________________________________________
591void AliMUONGeometryBuilder::WriteSVMaps(const TString& fileName,
592 Bool_t rebuild, Bool_t writeEnvelopes)
593{
594/// Write sensitive volume maps into files per builder
595
596 // Rebuild sv maps
597 //
598 if (rebuild)
599 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
600
601 AliMUONVGeometryBuilder* builder
602 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
603
604 builder->RebuildSVMaps(writeEnvelopes);
605 }
606
607 // Write maps in file
608 fGeometry->WriteSVMap(fileName);
609}
610
611//_____________________________________________________________________________
612void AliMUONGeometryBuilder::SetAlign(Bool_t align)
613{
614/// Set the option for alignement
615
616 fAlign = align;
617
618 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
619
620 AliMUONVGeometryBuilder* builder
621 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
622
623 SetAlign(builder);
624 }
625}
626
627//_____________________________________________________________________________
628void AliMUONGeometryBuilder::SetAlign(const TString& fileName, Bool_t align)
629{
630/// Set the option for alignement and the transformations file name
631
632 fTransformFileName = fileName;
633 fAlign = align;
634
635 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
636
637 AliMUONVGeometryBuilder* builder
638 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
639
640 SetAlign(builder);
641 }
642}