]> git.uio.no Git - u/mrichter/AliRoot.git/blame - MUON/AliMUONGeometryBuilder.cxx
In SetAlign() - adding the fileName argument
[u/mrichter/AliRoot.git] / MUON / AliMUONGeometryBuilder.cxx
CommitLineData
d4bb94a1 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// Class AliMUONGeometryBuilder
19// ----------------------------
e118b27e 20// Manager class for geometry construction via geometry builders.
d4bb94a1 21//
22// Author: Ivana Hrivnacova, IPN Orsay
23
e118b27e 24#include <TObjArray.h>
d4bb94a1 25#include <TVirtualMC.h>
26
27#include "AliMUONGeometryBuilder.h"
d4bb94a1 28#include "AliMUONVGeometryBuilder.h"
6cfb12b4 29#include "AliMUONGeometry.h"
30#include "AliMUONGeometryTransformer.h"
e118b27e 31#include "AliMUONGeometryModule.h"
6cfb12b4 32#include "AliMUONGeometryModuleTransformer.h"
d4bb94a1 33#include "AliMUONGeometryEnvelope.h"
76497dec 34#include "AliMUONGeometryEnvelopeStore.h"
e118b27e 35#include "AliMUONGeometryDetElement.h"
36#include "AliMUONGeometryStore.h"
6cfb12b4 37#include "AliMUONGeometryConstituent.h"
e118b27e 38#include "AliModule.h"
8c343c7c 39#include "AliLog.h"
4a9de4af 40#include "AliRun.h"
41
d4bb94a1 42
43ClassImp(AliMUONGeometryBuilder)
44
067866a3 45// static functions
46
47//______________________________________________________________________________
48TGeoHMatrix AliMUONGeometryBuilder::Multiply(const TGeoMatrix& m1,
49 const TGeoMatrix& m2)
50{
51/// Temporary fix for problem with matrix multiplication in Root 5.02/00
52
53 if (m1.IsIdentity() && m2.IsIdentity()) return TGeoHMatrix();
54
55 if (m1.IsIdentity()) return m2;
56
57 if (m2.IsIdentity()) return m1;
58
59 return m1 * m2;
60}
61
62//______________________________________________________________________________
63TGeoHMatrix AliMUONGeometryBuilder::Multiply(const TGeoMatrix& m1,
64 const TGeoMatrix& m2,
65 const TGeoMatrix& m3)
66{
67/// Temporary fix for problem with matrix multiplication in Root 5.02/00
68
69 if (m1.IsIdentity() && m2.IsIdentity() & m3.IsIdentity())
70 return TGeoHMatrix();
71
72 if (m1.IsIdentity()) return Multiply(m2, m3);
73
74 if (m2.IsIdentity()) return Multiply(m1, m3);
75
76 if (m3.IsIdentity()) return Multiply(m1, m2);
77
78 return m1 * m2 * m3;
79}
80
81//______________________________________________________________________________
82TGeoHMatrix AliMUONGeometryBuilder::Multiply(const TGeoMatrix& m1,
83 const TGeoMatrix& m2,
84 const TGeoMatrix& m3,
85 const TGeoMatrix& m4)
86{
87/// Temporary fix for problem with matrix multiplication in Root 5.02/00
88
89 if (m1.IsIdentity() && m2.IsIdentity() & m3.IsIdentity() & m4.IsIdentity())
90 return TGeoHMatrix();
91
92 if (m1.IsIdentity()) return Multiply(m2, m3, m4);
93
94 if (m2.IsIdentity()) return Multiply(m1, m3, m4);
95
96 if (m3.IsIdentity()) return Multiply(m1, m2, m4);
97
98 if (m4.IsIdentity()) return Multiply(m1, m2, m3);
99
100 return m1 * m2 * m3 * m4;
101}
102
e118b27e 103//______________________________________________________________________________
104AliMUONGeometryBuilder::AliMUONGeometryBuilder(AliModule* module)
d4bb94a1 105 : TObject(),
e118b27e 106 fModule(module),
76497dec 107 fAlign(false),
e118b27e 108 fGlobalTransformation(),
6cfb12b4 109 fGeometryBuilders(0),
110 fGeometry(0)
d4bb94a1 111{
692de412 112/// Standard constructor
e118b27e 113
6cfb12b4 114 fGeometryBuilders = new TObjArray();
115 fGeometryBuilders->SetOwner(true);
116
117 fGeometry = new AliMUONGeometry(true);
118
119 // Define the global transformation:
120 // Transformation from the old ALICE coordinate system to a new one:
121 // x->-x, z->-z
122 TGeoRotation* rotGlobal
123 = new TGeoRotation("rotGlobal", 90., 180., 90., 90., 180., 0.);
124 fGlobalTransformation = TGeoCombiTrans(0., 0., 0., rotGlobal);
e118b27e 125}
d4bb94a1 126
e118b27e 127//______________________________________________________________________________
128AliMUONGeometryBuilder::AliMUONGeometryBuilder()
d4bb94a1 129 : TObject(),
e118b27e 130 fModule(0),
76497dec 131 fAlign(false),
e118b27e 132 fGlobalTransformation(),
6cfb12b4 133 fGeometryBuilders(0),
134 fGeometry(0)
d4bb94a1 135{
692de412 136/// Default constructor
e118b27e 137}
d4bb94a1 138
139//______________________________________________________________________________
140AliMUONGeometryBuilder::AliMUONGeometryBuilder(const AliMUONGeometryBuilder& right)
141 : TObject(right)
142{
692de412 143/// Copy constructor (not implemented)
d4bb94a1 144
8c343c7c 145 AliFatal("Copy constructor not provided.");
d4bb94a1 146}
147
148//______________________________________________________________________________
149AliMUONGeometryBuilder::~AliMUONGeometryBuilder()
150{
692de412 151/// Destructor
6cfb12b4 152
153 delete fGeometryBuilders;
154 delete fGeometry;
d4bb94a1 155}
156
157//______________________________________________________________________________
158AliMUONGeometryBuilder&
159AliMUONGeometryBuilder::operator=(const AliMUONGeometryBuilder& right)
160{
692de412 161/// Assignement operator (not implemented)
d4bb94a1 162
163 // check assignement to self
164 if (this == &right) return *this;
165
8c343c7c 166 AliFatal("Assignement operator not provided.");
d4bb94a1 167
168 return *this;
169}
170
171//
172// private functions
173//
174
175//______________________________________________________________________________
176void AliMUONGeometryBuilder::PlaceVolume(const TString& name, const TString& mName,
177 Int_t copyNo, const TGeoHMatrix& matrix,
178 Int_t npar, Double_t* param, const char* only) const
179{
692de412 180/// Place the volume specified by name with the given transformation matrix
d4bb94a1 181
6cfb12b4 182 TGeoHMatrix transform(matrix);
d4bb94a1 183 // Do not apply global transformation
cfbf2f7d 184 // if mother volume was already placed in
185 // the new system of coordinates (that is MUON in negative Z)
e118b27e 186 // (as it is applied on the mother volume)
cfbf2f7d 187 if (mName == TString("DDIP"))
e118b27e 188 transform = fGlobalTransformation.Inverse() * transform;
d4bb94a1 189
190 // Decompose transformation
191 const Double_t* xyz = transform.GetTranslation();
192 const Double_t* rm = transform.GetRotationMatrix();
193
194 //cout << "Got translation: "
195 // << xyz[0] << " " << xyz[1] << " " << xyz[2] << endl;
196
197 //cout << "Got rotation: "
198 // << rm[0] << " " << rm[1] << " " << rm[2] << endl
199 // << rm[3] << " " << rm[4] << " " << rm[5] << endl
200 // << rm[6] << " " << rm[7] << " " << rm[8] << endl;
201
202 // Check for presence of rotation
203 // (will be nice to be available in TGeo)
204 const Double_t kTolerance = 1e-04;
205 Bool_t isRotation = true;
206 if (TMath::Abs(rm[0] - 1.) < kTolerance &&
207 TMath::Abs(rm[1] - 0.) < kTolerance &&
208 TMath::Abs(rm[2] - 0.) < kTolerance &&
209 TMath::Abs(rm[3] - 0.) < kTolerance &&
210 TMath::Abs(rm[4] - 1.) < kTolerance &&
211 TMath::Abs(rm[5] - 0.) < kTolerance &&
212 TMath::Abs(rm[6] - 0.) < kTolerance &&
213 TMath::Abs(rm[7] - 0.) < kTolerance &&
214 TMath::Abs(rm[8] - 1.) < kTolerance) isRotation = false;
215
216 Int_t krot = 0;
217 if (isRotation) {
218 TGeoRotation rot;
219 rot.SetMatrix(const_cast<Double_t*>(transform.GetRotationMatrix()));
220 Double_t theta1, phi1, theta2, phi2, theta3, phi3;
221 rot.GetAngles(theta1, phi1, theta2, phi2, theta3, phi3);
222
223 //cout << "angles: "
224 // << theta1 << " " << phi1 << " "
225 // << theta2 << " " << phi2 << " "
226 // << theta3 << " " << phi3 << endl;
227
e118b27e 228 fModule->AliMatrix(krot, theta1, phi1, theta2, phi2, theta3, phi3);
d4bb94a1 229 }
230
231 // Place the volume in ALIC
232 if (npar == 0)
233 gMC->Gspos(name, copyNo, mName, xyz[0], xyz[1], xyz[2] , krot, only);
234 else
235 gMC->Gsposp(name, copyNo, mName, xyz[0], xyz[1], xyz[2] , krot, only,
236 param, npar);
237
238}
239
8618c022 240//_____________________________________________________________________________
241void AliMUONGeometryBuilder::SetAlign(AliMUONVGeometryBuilder* builder)
242{
243/// Set align option to all geometry modules associated with the builder
244
245 for (Int_t j=0; j<builder->NofGeometries(); j++) {
246
247 AliMUONGeometryModule* geometry = builder->Geometry(j);
248
249 geometry->SetAlign(fAlign);
250 }
251}
252
d4bb94a1 253//
254// public functions
255//
256
e118b27e 257//_____________________________________________________________________________
258void AliMUONGeometryBuilder::AddBuilder(AliMUONVGeometryBuilder* geomBuilder)
259{
067866a3 260/// Add the geometry builder to the list
e118b27e 261
262 fGeometryBuilders->Add(geomBuilder);
8618c022 263
6cfb12b4 264 // Pass geometry modules created in the to the geometry parametrisation
265 for (Int_t i=0; i<geomBuilder->NofGeometries(); i++) {
266 fGeometry->AddModule(geomBuilder->Geometry(i));
267 }
268
269 if (geomBuilder->ApplyGlobalTransformation())
270 geomBuilder->SetReferenceFrame(fGlobalTransformation);
271
8618c022 272 SetAlign(geomBuilder);
e118b27e 273}
274
d4bb94a1 275//______________________________________________________________________________
276void AliMUONGeometryBuilder::CreateGeometry()
277{
692de412 278/// Construct geometry using geometry builders.
d4bb94a1 279
6cfb12b4 280 if (fAlign) ReadTransformations();
281
d4bb94a1 282 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
283
284 // Get the builder
285 AliMUONVGeometryBuilder* builder
286 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
287
e118b27e 288 // Create geometry + envelopes
289 //
6cfb12b4 290 builder->CreateGeometry();
291 if (!fAlign) builder->SetTransformations();
292
cfbf2f7d 293 // Place module volumes and envelopes
e118b27e 294 //
295 for (Int_t j=0; j<builder->NofGeometries(); j++) {
d4bb94a1 296
e118b27e 297 AliMUONGeometryModule* geometry = builder->Geometry(j);
6cfb12b4 298 const TGeoCombiTrans* kModuleTransform
299 = geometry->GetTransformer()->GetTransformation();
cfbf2f7d 300
301 // Place the module volume
302 if ( !geometry->IsVirtual() ) {
6cfb12b4 303 PlaceVolume(geometry->GetVolume(), geometry->GetMotherVolume(),
304 1, *kModuleTransform, 0, 0, "ONLY");
cfbf2f7d 305 }
e118b27e 306
6cfb12b4 307 TGeoCombiTrans appliedGlobalTransform;
308 if (builder->ApplyGlobalTransformation())
309 appliedGlobalTransform = fGlobalTransformation;
310
e118b27e 311 // Loop over envelopes
6cfb12b4 312 const TObjArray* kEnvelopes
313 = geometry->GetEnvelopeStore()->GetEnvelopes();
e118b27e 314 for (Int_t k=0; k<kEnvelopes->GetEntriesFast(); k++) {
315
316 // Get envelope
6cfb12b4 317 AliMUONGeometryEnvelope* env
318 = (AliMUONGeometryEnvelope*)kEnvelopes->At(k);
319
e118b27e 320 const TGeoCombiTrans* kEnvTrans = env->GetTransformation();
321 const char* only = "ONLY";
322 if (env->IsMANY()) only = "MANY";
323
324 if (env->IsVirtual() && env->GetConstituents()->GetEntriesFast() == 0 ) {
325 // virtual envelope + nof constituents = 0
326 // => not allowed;
327 // empty virtual envelope has no sense
328 AliFatal("Virtual envelope must have constituents.");
329 return;
330 }
d4bb94a1 331
e118b27e 332 if (!env->IsVirtual() && env->GetConstituents()->GetEntriesFast() > 0 ) {
333 // non virtual envelope + nof constituents > 0
334 // => not allowed;
335 // use VMC to place constituents
336 AliFatal("Non virtual envelope cannot have constituents.");
337 return;
338 }
339
340 if (!env->IsVirtual() && env->GetConstituents()->GetEntriesFast() == 0 ) {
341 // non virtual envelope + nof constituents = 0
342 // => place envelope in ALICE by composed transformation:
6cfb12b4 343 // Tch * [Tglobal] * Tenv
e118b27e 344
345 // Compound chamber transformation with the envelope one
cfbf2f7d 346 if (geometry->IsVirtual()) {
347 TGeoHMatrix total
6cfb12b4 348 = Multiply( (*kModuleTransform),
349 appliedGlobalTransform,
067866a3 350 (*kEnvTrans) );
cfbf2f7d 351 PlaceVolume(env->GetName(), geometry->GetMotherVolume(),
352 env->GetCopyNo(), total, 0, 0, only);
353 }
354 else {
355 TGeoHMatrix total
6cfb12b4 356 = Multiply( appliedGlobalTransform,
357 (*kEnvTrans) );
cfbf2f7d 358 PlaceVolume(env->GetName(), geometry->GetVolume(),
359 env->GetCopyNo(), total, 0, 0, only);
360 }
e118b27e 361 }
362
363 if (env->IsVirtual() && env->GetConstituents()->GetEntriesFast() > 0 ) {
364 // virtual envelope + nof constituents > 0
365 // => do not place envelope and place constituents
366 // in ALICE by composed transformation:
6cfb12b4 367 // Tch * [Tglobal] * Tenv * Tconst
d4bb94a1 368
e118b27e 369 for (Int_t l=0; l<env->GetConstituents()->GetEntriesFast(); l++) {
370 AliMUONGeometryConstituent* constituent
371 = (AliMUONGeometryConstituent*)env->GetConstituents()->At(l);
372
373 // Compound chamber transformation with the envelope one + the constituent one
cfbf2f7d 374 if (geometry->IsVirtual()) {
375 TGeoHMatrix total
6cfb12b4 376 = Multiply ( (*kModuleTransform),
377 appliedGlobalTransform,
378 (*kEnvTrans),
067866a3 379 (*constituent->GetTransformation()) );
cfbf2f7d 380
381 PlaceVolume(constituent->GetName(), geometry->GetMotherVolume(),
382 constituent->GetCopyNo(), total,
383 constituent->GetNpar(), constituent->GetParam(), only);
384 }
385 else {
386 TGeoHMatrix total
6cfb12b4 387 = Multiply ( appliedGlobalTransform,
388 (*kEnvTrans),
067866a3 389 (*constituent->GetTransformation()) );
cfbf2f7d 390
391 PlaceVolume(constituent->GetName(), geometry->GetVolume(),
392 constituent->GetCopyNo(), total,
393 constituent->GetNpar(), constituent->GetParam(), only);
394 }
e118b27e 395 }
d4bb94a1 396 }
e118b27e 397 } // end of loop over envelopes
398 } // end of loop over builder geometries
399 } // end of loop over builders
d4bb94a1 400}
401
402//_____________________________________________________________________________
403void AliMUONGeometryBuilder::CreateMaterials()
404{
692de412 405/// Construct materials specific to modules via builders
d4bb94a1 406
407 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
408
409 // Get the builder
410 AliMUONVGeometryBuilder* builder
411 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
412
413 // Create materials with each builder
414 if (builder) builder->CreateMaterials();
415 }
416}
417
418//______________________________________________________________________________
6cfb12b4 419void AliMUONGeometryBuilder::InitGeometry(const TString& svmapFileName)
d4bb94a1 420{
692de412 421/// Initialize geometry
d4bb94a1 422
6cfb12b4 423 // Read alignement data if geometry is read from Root file
063619ad 424 if ( gAlice->IsRootGeometry() ) {
6cfb12b4 425 fAlign = true;
426 ReadTransformations();
427 }
428
429 // Read sensitive volume map from a file
430 fGeometry->ReadSVMap(svmapFileName);
431
d4bb94a1 432 // Set the chamber (sensitive region) GEANT identifier
433 //
434 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
435
436 // Get the builder
437 AliMUONVGeometryBuilder* builder
438 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
439
6cfb12b4 440 // Set sensitive volumes with each builder
76497dec 441 builder->SetSensitiveVolumes();
4a9de4af 442
6cfb12b4 443 if (!fAlign) {
444 // Fill local transformations from built geometry
445 builder->FillTransformations();
6cfb12b4 446 }
e118b27e 447 }
76497dec 448}
449
450//______________________________________________________________________________
6cfb12b4 451void AliMUONGeometryBuilder::ReadTransformations(const TString& fileName)
76497dec 452{
6cfb12b4 453/// Read transformations from ASCII files
454/// and store them in the geometry parametrisation
76497dec 455
6cfb12b4 456 // Read transformations
457 //
458 AliMUONGeometryTransformer* geomTransformer = fGeometry->GetTransformer();
459 geomTransformer->ReadTransformations(fileName);
76497dec 460}
461
462//______________________________________________________________________________
6cfb12b4 463void AliMUONGeometryBuilder::WriteTransformations(const TString& fileName)
76497dec 464{
6cfb12b4 465/// Write transformations into files per builder
76497dec 466
6cfb12b4 467 AliMUONGeometryTransformer* geomTransformer = fGeometry->GetTransformer();
468 geomTransformer->WriteTransformations(fileName);
d4bb94a1 469}
470
6cfb12b4 471//______________________________________________________________________________
472void AliMUONGeometryBuilder::WriteSVMaps(Bool_t rebuild,
473 const TString& fileName)
d4bb94a1 474{
6cfb12b4 475/// Write sensitive volume maps into files per builder
476
477 // Rebuild sv maps
478 //
479 if (rebuild)
480 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
d4bb94a1 481
6cfb12b4 482 AliMUONVGeometryBuilder* builder
483 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
484
485 builder->RebuildSVMaps();
486 }
487
488 // Write maps in file
489 fGeometry->WriteSVMap(fileName);
490}
d4bb94a1 491
76497dec 492//_____________________________________________________________________________
493void AliMUONGeometryBuilder::SetAlign(Bool_t align)
494{
067866a3 495/// Set the option for alignement
76497dec 496
497 fAlign = align;
498
e118b27e 499 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
500
e118b27e 501 AliMUONVGeometryBuilder* builder
502 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
8618c022 503
504 SetAlign(builder);
505 }
76497dec 506}