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. *
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 **************************************************************************/
18 // Class AliMUONGeometryBuilder
19 // ----------------------------
20 // Manager class for geometry construction via geometry builders.
22 // Author: Ivana Hrivnacova, IPN Orsay
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 "AliMUONGeometryStore.h"
34 #include "AliMUONGeometryConstituent.h"
36 #include "AliModule.h"
40 #include <TObjArray.h>
41 #include <TVirtualMC.h>
42 #include <TGeoManager.h>
45 ClassImp(AliMUONGeometryBuilder)
47 // static data members
49 const TString AliMUONGeometryBuilder::fgkDefaultVolPathsFileName = "volpath.dat";
50 const TString AliMUONGeometryBuilder::fgkDefaultTransformFileName = "transform.dat";
51 const TString AliMUONGeometryBuilder::fgkDefaultSVMapFileName = "svmap.dat";
52 const TString AliMUONGeometryBuilder::fgkOutFileNameExtension = ".out";
56 //______________________________________________________________________________
57 TGeoHMatrix AliMUONGeometryBuilder::Multiply(const TGeoMatrix& m1,
60 /// Temporary fix for problem with matrix multiplication in Root 5.02/00
62 if (m1.IsIdentity() && m2.IsIdentity()) return TGeoHMatrix();
64 if (m1.IsIdentity()) return m2;
66 if (m2.IsIdentity()) return m1;
71 //______________________________________________________________________________
72 TGeoHMatrix AliMUONGeometryBuilder::Multiply(const TGeoMatrix& m1,
76 /// Temporary fix for problem with matrix multiplication in Root 5.02/00
78 if (m1.IsIdentity() && m2.IsIdentity() & m3.IsIdentity())
81 if (m1.IsIdentity()) return Multiply(m2, m3);
83 if (m2.IsIdentity()) return Multiply(m1, m3);
85 if (m3.IsIdentity()) return Multiply(m1, m2);
90 //______________________________________________________________________________
91 TGeoHMatrix AliMUONGeometryBuilder::Multiply(const TGeoMatrix& m1,
96 /// Temporary fix for problem with matrix multiplication in Root 5.02/00
98 if (m1.IsIdentity() && m2.IsIdentity() & m3.IsIdentity() & m4.IsIdentity())
101 if (m1.IsIdentity()) return Multiply(m2, m3, m4);
103 if (m2.IsIdentity()) return Multiply(m1, m3, m4);
105 if (m3.IsIdentity()) return Multiply(m1, m2, m4);
107 if (m4.IsIdentity()) return Multiply(m1, m2, m3);
109 return m1 * m2 * m3 * m4;
112 //______________________________________________________________________________
113 AliMUONGeometryBuilder::AliMUONGeometryBuilder(AliModule* module)
117 fTransformFileName(fgkDefaultTransformFileName),
118 fSVMapFileName(fgkDefaultSVMapFileName),
119 fGlobalTransformation(),
120 fGeometryBuilders(0),
123 /// Standard constructor
125 fGeometryBuilders = new TObjArray();
126 fGeometryBuilders->SetOwner(true);
128 fGeometry = new AliMUONGeometry(true);
130 // Define the global transformation:
131 // Transformation from the old ALICE coordinate system to a new one:
133 TGeoRotation* rotGlobal
134 = new TGeoRotation("rotGlobal", 90., 180., 90., 90., 180., 0.);
135 fGlobalTransformation = TGeoCombiTrans(0., 0., 0., rotGlobal);
138 //______________________________________________________________________________
139 AliMUONGeometryBuilder::AliMUONGeometryBuilder()
143 fTransformFileName(),
145 fGlobalTransformation(),
146 fGeometryBuilders(0),
149 /// Default constructor
152 //______________________________________________________________________________
153 AliMUONGeometryBuilder::AliMUONGeometryBuilder(const AliMUONGeometryBuilder& right)
156 /// Copy constructor (not implemented)
158 AliFatal("Copy constructor not provided.");
161 //______________________________________________________________________________
162 AliMUONGeometryBuilder::~AliMUONGeometryBuilder()
166 delete fGeometryBuilders;
170 //______________________________________________________________________________
171 AliMUONGeometryBuilder&
172 AliMUONGeometryBuilder::operator=(const AliMUONGeometryBuilder& right)
174 /// Assignement operator (not implemented)
176 // check assignement to self
177 if (this == &right) return *this;
179 AliFatal("Assignement operator not provided.");
188 //______________________________________________________________________________
189 void AliMUONGeometryBuilder::PlaceVolume(const TString& name, const TString& mName,
190 Int_t copyNo, const TGeoHMatrix& matrix,
191 Int_t npar, Double_t* param, const char* only,
192 Bool_t makeAssembly) const
194 /// Place the volume specified by name with the given transformation matrix
197 gGeoManager->MakeVolumeAssembly(name.Data());
199 TGeoHMatrix transform(matrix);
200 // Do not apply global transformation
201 // if mother volume was already placed in
202 // the new system of coordinates (that is MUON in negative Z)
203 // (as it is applied on the mother volume)
204 if (mName == TString("DDIP"))
205 transform = fGlobalTransformation.Inverse() * transform;
207 // Decompose transformation
208 const Double_t* xyz = transform.GetTranslation();
209 const Double_t* rm = transform.GetRotationMatrix();
211 //cout << "Got translation: "
212 // << xyz[0] << " " << xyz[1] << " " << xyz[2] << endl;
214 //cout << "Got rotation: "
215 // << rm[0] << " " << rm[1] << " " << rm[2] << endl
216 // << rm[3] << " " << rm[4] << " " << rm[5] << endl
217 // << rm[6] << " " << rm[7] << " " << rm[8] << endl;
219 // Check for presence of rotation
220 // (will be nice to be available in TGeo)
221 const Double_t kTolerance = 1e-04;
222 Bool_t isRotation = true;
223 if (TMath::Abs(rm[0] - 1.) < kTolerance &&
224 TMath::Abs(rm[1] - 0.) < kTolerance &&
225 TMath::Abs(rm[2] - 0.) < kTolerance &&
226 TMath::Abs(rm[3] - 0.) < kTolerance &&
227 TMath::Abs(rm[4] - 1.) < kTolerance &&
228 TMath::Abs(rm[5] - 0.) < kTolerance &&
229 TMath::Abs(rm[6] - 0.) < kTolerance &&
230 TMath::Abs(rm[7] - 0.) < kTolerance &&
231 TMath::Abs(rm[8] - 1.) < kTolerance) isRotation = false;
236 rot.SetMatrix(const_cast<Double_t*>(transform.GetRotationMatrix()));
237 Double_t theta1, phi1, theta2, phi2, theta3, phi3;
238 rot.GetAngles(theta1, phi1, theta2, phi2, theta3, phi3);
241 // << theta1 << " " << phi1 << " "
242 // << theta2 << " " << phi2 << " "
243 // << theta3 << " " << phi3 << endl;
245 fModule->AliMatrix(krot, theta1, phi1, theta2, phi2, theta3, phi3);
248 // Place the volume in ALIC
250 gMC->Gspos(name, copyNo, mName, xyz[0], xyz[1], xyz[2] , krot, only);
252 gMC->Gsposp(name, copyNo, mName, xyz[0], xyz[1], xyz[2] , krot, only,
256 //______________________________________________________________________________
257 void AliMUONGeometryBuilder::CreateGeometryWithTGeo()
259 /// Construct geometry using geometry builders.
260 /// Virtual modules/envelopes are placed as TGeoVolume assembly
263 // Read transformations from ASCII data file
264 fGeometry->GetTransformer()
265 ->ReadGeometryData(fgkDefaultVolPathsFileName, fTransformFileName);
268 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
271 AliMUONVGeometryBuilder* builder
272 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
274 // Create geometry + envelopes
276 builder->CreateGeometry();
277 if (!fAlign) builder->SetTransformations();
279 // Place module volumes and envelopes
281 for (Int_t j=0; j<builder->NofGeometries(); j++) {
283 AliMUONGeometryModule* geometry = builder->Geometry(j);
284 AliMUONGeometryModuleTransformer* transformer= geometry->GetTransformer();
285 const TGeoHMatrix* kModuleTransform = transformer->GetTransformation();
286 TString volName = transformer->GetVolumeName();
287 TString motherVolName = transformer->GetMotherVolumeName();
289 // Place the module volume
290 PlaceVolume(volName, motherVolName,
291 1, *kModuleTransform, 0, 0, "ONLY", geometry->IsVirtual());
293 TGeoCombiTrans appliedGlobalTransform;
294 if (builder->ApplyGlobalTransformation())
295 appliedGlobalTransform = fGlobalTransformation;
297 // Loop over envelopes
298 const TObjArray* kEnvelopes
299 = geometry->GetEnvelopeStore()->GetEnvelopes();
300 for (Int_t k=0; k<kEnvelopes->GetEntriesFast(); k++) {
303 AliMUONGeometryEnvelope* env
304 = (AliMUONGeometryEnvelope*)kEnvelopes->At(k);
306 const TGeoCombiTrans* kEnvTrans = env->GetTransformation();
307 const char* only = "ONLY";
308 if (env->IsMANY()) only = "MANY";
310 if (env->IsVirtual() && env->GetConstituents()->GetEntriesFast() == 0 ) {
311 // virtual envelope + nof constituents = 0
313 // empty virtual envelope has no sense
314 AliFatal("Virtual envelope must have constituents.");
318 if (!env->IsVirtual() && env->GetConstituents()->GetEntriesFast() > 0 ) {
319 // non virtual envelope + nof constituents > 0
321 // use VMC to place constituents
322 AliFatal("Non virtual envelope cannot have constituents.");
326 // Place envelope in geometry module by composed transformation:
329 = Multiply( appliedGlobalTransform,
331 PlaceVolume(env->GetName(), volName,
332 env->GetCopyNo(), total, 0, 0, only, env->IsVirtual());
334 if ( env->IsVirtual() ) {
335 // Place constituents in the envelope
336 for (Int_t l=0; l<env->GetConstituents()->GetEntriesFast(); l++) {
337 AliMUONGeometryConstituent* constituent
338 = (AliMUONGeometryConstituent*)env->GetConstituents()->At(l);
340 PlaceVolume(constituent->GetName(), env->GetName(),
341 constituent->GetCopyNo(),
342 *constituent->GetTransformation() ,
343 constituent->GetNpar(), constituent->GetParam(), only);
346 } // end of loop over envelopes
347 } // end of loop over builder geometries
348 } // end of loop over builders
351 //______________________________________________________________________________
352 void AliMUONGeometryBuilder::CreateGeometryWithoutTGeo()
354 /// Construct geometry using geometry builders.
355 /// Virtual modules/enevlopes are not placed
358 // Read transformations from ASCII data file
359 fGeometry->GetTransformer()
360 ->ReadGeometryData(fgkDefaultVolPathsFileName, fTransformFileName);
363 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
366 AliMUONVGeometryBuilder* builder
367 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
369 // Create geometry + envelopes
371 builder->CreateGeometry();
372 if (!fAlign) builder->SetTransformations();
374 // Place module volumes and envelopes
376 for (Int_t j=0; j<builder->NofGeometries(); j++) {
378 AliMUONGeometryModule* geometry = builder->Geometry(j);
379 AliMUONGeometryModuleTransformer* transformer= geometry->GetTransformer();
380 const TGeoHMatrix* kModuleTransform = transformer->GetTransformation();
381 TString volName = transformer->GetVolumeName();
382 TString motherVolName = transformer->GetMotherVolumeName();
384 // Place the module volume
385 if ( !geometry->IsVirtual() ) {
386 PlaceVolume(volName, motherVolName,
387 1, *kModuleTransform, 0, 0, "ONLY");
390 TGeoCombiTrans appliedGlobalTransform;
391 if (builder->ApplyGlobalTransformation())
392 appliedGlobalTransform = fGlobalTransformation;
394 // Loop over envelopes
395 const TObjArray* kEnvelopes
396 = geometry->GetEnvelopeStore()->GetEnvelopes();
397 for (Int_t k=0; k<kEnvelopes->GetEntriesFast(); k++) {
400 AliMUONGeometryEnvelope* env
401 = (AliMUONGeometryEnvelope*)kEnvelopes->At(k);
403 const TGeoCombiTrans* kEnvTrans = env->GetTransformation();
404 const char* only = "ONLY";
405 if (env->IsMANY()) only = "MANY";
407 if (env->IsVirtual() && env->GetConstituents()->GetEntriesFast() == 0 ) {
408 // virtual envelope + nof constituents = 0
410 // empty virtual envelope has no sense
411 AliFatal("Virtual envelope must have constituents.");
415 if (!env->IsVirtual() && env->GetConstituents()->GetEntriesFast() > 0 ) {
416 // non virtual envelope + nof constituents > 0
418 // use VMC to place constituents
419 AliFatal("Non virtual envelope cannot have constituents.");
423 if (!env->IsVirtual() && env->GetConstituents()->GetEntriesFast() == 0 ) {
424 // non virtual envelope + nof constituents = 0
425 // => place envelope in ALICE by composed transformation:
426 // Tch * [Tglobal] * Tenv
428 // Compound chamber transformation with the envelope one
429 if (geometry->IsVirtual()) {
431 = Multiply( (*kModuleTransform),
432 appliedGlobalTransform,
434 PlaceVolume(env->GetName(), motherVolName,
435 env->GetCopyNo(), total, 0, 0, only);
439 = Multiply( appliedGlobalTransform,
441 PlaceVolume(env->GetName(), volName,
442 env->GetCopyNo(), total, 0, 0, only);
446 if (env->IsVirtual() && env->GetConstituents()->GetEntriesFast() > 0 ) {
447 // virtual envelope + nof constituents > 0
448 // => do not place envelope and place constituents
449 // in ALICE by composed transformation:
450 // Tch * [Tglobal] * Tenv * Tconst
452 for (Int_t l=0; l<env->GetConstituents()->GetEntriesFast(); l++) {
453 AliMUONGeometryConstituent* constituent
454 = (AliMUONGeometryConstituent*)env->GetConstituents()->At(l);
456 // Compound chamber transformation with the envelope one + the constituent one
457 if (geometry->IsVirtual()) {
459 = Multiply ( (*kModuleTransform),
460 appliedGlobalTransform,
462 (*constituent->GetTransformation()) );
464 PlaceVolume(constituent->GetName(), motherVolName,
465 constituent->GetCopyNo(), total,
466 constituent->GetNpar(), constituent->GetParam(), only);
470 = Multiply ( appliedGlobalTransform,
472 (*constituent->GetTransformation()) );
474 PlaceVolume(constituent->GetName(), volName,
475 constituent->GetCopyNo(), total,
476 constituent->GetNpar(), constituent->GetParam(), only);
480 } // end of loop over envelopes
481 } // end of loop over builder geometries
482 } // end of loop over builders
485 //_____________________________________________________________________________
486 void AliMUONGeometryBuilder::SetAlign(AliMUONVGeometryBuilder* builder)
488 /// Set align option to all geometry modules associated with the builder
490 for (Int_t j=0; j<builder->NofGeometries(); j++) {
492 AliMUONGeometryModule* geometry = builder->Geometry(j);
494 geometry->SetAlign(fAlign);
502 //_____________________________________________________________________________
503 void AliMUONGeometryBuilder::AddBuilder(AliMUONVGeometryBuilder* geomBuilder)
505 /// Add the geometry builder to the list
507 fGeometryBuilders->Add(geomBuilder);
509 // Pass geometry modules created in the to the geometry parametrisation
510 for (Int_t i=0; i<geomBuilder->NofGeometries(); i++) {
511 fGeometry->AddModule(geomBuilder->Geometry(i));
514 if (geomBuilder->ApplyGlobalTransformation())
515 geomBuilder->SetReferenceFrame(fGlobalTransformation);
517 SetAlign(geomBuilder);
520 //______________________________________________________________________________
521 void AliMUONGeometryBuilder::CreateGeometry()
523 /// Construct geometry using geometry builders.
525 if ( gMC->IsRootGeometrySupported() &&
526 TString(gMC->ClassName()) != "TGeant4" ) {
528 CreateGeometryWithTGeo();
531 CreateGeometryWithoutTGeo();
534 //_____________________________________________________________________________
535 void AliMUONGeometryBuilder::CreateMaterials()
537 /// Construct materials specific to modules via builders
539 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
542 AliMUONVGeometryBuilder* builder
543 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
545 // Create materials with each builder
546 if (builder) builder->CreateMaterials();
550 //______________________________________________________________________________
551 void AliMUONGeometryBuilder::InitGeometry(const TString& svmapFileName)
553 /// Initialize geometry
555 // Load alignement data from geometry if geometry is read from Root file
556 if ( gAlice->IsRootGeometry() ) {
559 fGeometry->GetTransformer()
560 ->ReadGeometryData(fgkDefaultVolPathsFileName, gGeoManager);
563 // Read sensitive volume map from a file
564 fGeometry->ReadSVMap(svmapFileName);
566 // Set the chamber (sensitive region) GEANT identifier
568 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
571 AliMUONVGeometryBuilder* builder
572 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
574 // Set sensitive volumes with each builder
575 builder->SetSensitiveVolumes();
578 // Create detection elements from built geometry
579 builder->CreateDetElements();
584 //______________________________________________________________________________
585 void AliMUONGeometryBuilder::WriteSVMaps(const TString& fileName,
588 /// Write sensitive volume maps into files per builder
593 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
595 AliMUONVGeometryBuilder* builder
596 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
598 Bool_t writeEnvelopes = false;
599 if ( gMC->IsRootGeometrySupported() &&
600 TString(gMC->ClassName()) != "TGeant4") writeEnvelopes = true;
602 builder->RebuildSVMaps(writeEnvelopes);
605 // Write maps in file
606 fGeometry->WriteSVMap(fileName);
609 //_____________________________________________________________________________
610 void AliMUONGeometryBuilder::SetAlign(Bool_t align)
612 /// Set the option for alignement
616 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
618 AliMUONVGeometryBuilder* builder
619 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
625 //_____________________________________________________________________________
626 void AliMUONGeometryBuilder::SetAlign(const TString& fileName, Bool_t align)
628 /// Set the option for alignement
630 fTransformFileName = fileName;
633 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
635 AliMUONVGeometryBuilder* builder
636 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);