]> git.uio.no Git - u/mrichter/AliRoot.git/blame - MUON/AliMUONGeometryBuilder.cxx
Removed with merging transform/svmap data files in one
[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
e118b27e 240//______________________________________________________________________________
241void AliMUONGeometryBuilder::FillGlobalTransformations(
242 AliMUONVGeometryBuilder* builder)
243{
692de412 244/// Compute and set global transformations to detection elements
245/// for each chamber geometry
e118b27e 246
247 for (Int_t j=0; j<builder->NofGeometries(); j++) {
248
6cfb12b4 249 AliMUONGeometryModuleTransformer* transformer
250 = builder->Geometry(j)->GetTransformer();
251
252 AliMUONGeometryStore* detElements
253 = transformer->GetDetElementStore();
e118b27e 254
255 for (Int_t k=0; k<detElements->GetNofEntries(); k++) {
256
257 AliMUONGeometryDetElement* detElement
258 = (AliMUONGeometryDetElement*)detElements->GetEntry(k);
259
260 if (!detElement) AliFatal("Detection element not found.")
261
262 const TGeoCombiTrans* localTransform
263 = detElement->GetLocalTransformation();
264
067866a3 265 TGeoCombiTrans appliedGlobalTransform;
266 if (builder->ApplyGlobalTransformation())
267 appliedGlobalTransform = fGlobalTransformation;
268
e118b27e 269 // Compose global transformation
270 TGeoHMatrix total
6cfb12b4 271 = Multiply( (*transformer->GetTransformation()),
272 (*localTransform),
273 appliedGlobalTransform );
e118b27e 274
275 // Convert TGeoHMatrix to TGeoCombiTrans
276 TGeoCombiTrans globalTransform(localTransform->GetName());
277 globalTransform.SetTranslation(total.GetTranslation());
278 TGeoRotation rotation;
279 rotation.SetMatrix(total.GetRotationMatrix());
280 globalTransform.SetRotation(rotation);
281
282 // Set the global transformation to detection element
283 detElement->SetGlobalTransformation(globalTransform);
284 }
285 }
286}
287
8618c022 288//_____________________________________________________________________________
289void AliMUONGeometryBuilder::SetAlign(AliMUONVGeometryBuilder* builder)
290{
291/// Set align option to all geometry modules associated with the builder
292
293 for (Int_t j=0; j<builder->NofGeometries(); j++) {
294
295 AliMUONGeometryModule* geometry = builder->Geometry(j);
296
297 geometry->SetAlign(fAlign);
298 }
299}
300
d4bb94a1 301//
302// public functions
303//
304
e118b27e 305//_____________________________________________________________________________
306void AliMUONGeometryBuilder::AddBuilder(AliMUONVGeometryBuilder* geomBuilder)
307{
067866a3 308/// Add the geometry builder to the list
e118b27e 309
310 fGeometryBuilders->Add(geomBuilder);
8618c022 311
6cfb12b4 312 // Pass geometry modules created in the to the geometry parametrisation
313 for (Int_t i=0; i<geomBuilder->NofGeometries(); i++) {
314 fGeometry->AddModule(geomBuilder->Geometry(i));
315 }
316
317 if (geomBuilder->ApplyGlobalTransformation())
318 geomBuilder->SetReferenceFrame(fGlobalTransformation);
319
8618c022 320 SetAlign(geomBuilder);
e118b27e 321}
322
d4bb94a1 323//______________________________________________________________________________
324void AliMUONGeometryBuilder::CreateGeometry()
325{
692de412 326/// Construct geometry using geometry builders.
d4bb94a1 327
6cfb12b4 328 if (fAlign) ReadTransformations();
329
d4bb94a1 330 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
331
332 // Get the builder
333 AliMUONVGeometryBuilder* builder
334 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
335
e118b27e 336 // Create geometry + envelopes
337 //
6cfb12b4 338 builder->CreateGeometry();
339 if (!fAlign) builder->SetTransformations();
340
cfbf2f7d 341 // Place module volumes and envelopes
e118b27e 342 //
343 for (Int_t j=0; j<builder->NofGeometries(); j++) {
d4bb94a1 344
e118b27e 345 AliMUONGeometryModule* geometry = builder->Geometry(j);
6cfb12b4 346 const TGeoCombiTrans* kModuleTransform
347 = geometry->GetTransformer()->GetTransformation();
cfbf2f7d 348
349 // Place the module volume
350 if ( !geometry->IsVirtual() ) {
6cfb12b4 351 PlaceVolume(geometry->GetVolume(), geometry->GetMotherVolume(),
352 1, *kModuleTransform, 0, 0, "ONLY");
cfbf2f7d 353 }
e118b27e 354
6cfb12b4 355 TGeoCombiTrans appliedGlobalTransform;
356 if (builder->ApplyGlobalTransformation())
357 appliedGlobalTransform = fGlobalTransformation;
358
e118b27e 359 // Loop over envelopes
6cfb12b4 360 const TObjArray* kEnvelopes
361 = geometry->GetEnvelopeStore()->GetEnvelopes();
e118b27e 362 for (Int_t k=0; k<kEnvelopes->GetEntriesFast(); k++) {
363
364 // Get envelope
6cfb12b4 365 AliMUONGeometryEnvelope* env
366 = (AliMUONGeometryEnvelope*)kEnvelopes->At(k);
367
e118b27e 368 const TGeoCombiTrans* kEnvTrans = env->GetTransformation();
369 const char* only = "ONLY";
370 if (env->IsMANY()) only = "MANY";
371
372 if (env->IsVirtual() && env->GetConstituents()->GetEntriesFast() == 0 ) {
373 // virtual envelope + nof constituents = 0
374 // => not allowed;
375 // empty virtual envelope has no sense
376 AliFatal("Virtual envelope must have constituents.");
377 return;
378 }
d4bb94a1 379
e118b27e 380 if (!env->IsVirtual() && env->GetConstituents()->GetEntriesFast() > 0 ) {
381 // non virtual envelope + nof constituents > 0
382 // => not allowed;
383 // use VMC to place constituents
384 AliFatal("Non virtual envelope cannot have constituents.");
385 return;
386 }
387
388 if (!env->IsVirtual() && env->GetConstituents()->GetEntriesFast() == 0 ) {
389 // non virtual envelope + nof constituents = 0
390 // => place envelope in ALICE by composed transformation:
6cfb12b4 391 // Tch * [Tglobal] * Tenv
e118b27e 392
393 // Compound chamber transformation with the envelope one
cfbf2f7d 394 if (geometry->IsVirtual()) {
395 TGeoHMatrix total
6cfb12b4 396 = Multiply( (*kModuleTransform),
397 appliedGlobalTransform,
067866a3 398 (*kEnvTrans) );
cfbf2f7d 399 PlaceVolume(env->GetName(), geometry->GetMotherVolume(),
400 env->GetCopyNo(), total, 0, 0, only);
401 }
402 else {
403 TGeoHMatrix total
6cfb12b4 404 = Multiply( appliedGlobalTransform,
405 (*kEnvTrans) );
cfbf2f7d 406 PlaceVolume(env->GetName(), geometry->GetVolume(),
407 env->GetCopyNo(), total, 0, 0, only);
408 }
e118b27e 409 }
410
411 if (env->IsVirtual() && env->GetConstituents()->GetEntriesFast() > 0 ) {
412 // virtual envelope + nof constituents > 0
413 // => do not place envelope and place constituents
414 // in ALICE by composed transformation:
6cfb12b4 415 // Tch * [Tglobal] * Tenv * Tconst
d4bb94a1 416
e118b27e 417 for (Int_t l=0; l<env->GetConstituents()->GetEntriesFast(); l++) {
418 AliMUONGeometryConstituent* constituent
419 = (AliMUONGeometryConstituent*)env->GetConstituents()->At(l);
420
421 // Compound chamber transformation with the envelope one + the constituent one
cfbf2f7d 422 if (geometry->IsVirtual()) {
423 TGeoHMatrix total
6cfb12b4 424 = Multiply ( (*kModuleTransform),
425 appliedGlobalTransform,
426 (*kEnvTrans),
067866a3 427 (*constituent->GetTransformation()) );
cfbf2f7d 428
429 PlaceVolume(constituent->GetName(), geometry->GetMotherVolume(),
430 constituent->GetCopyNo(), total,
431 constituent->GetNpar(), constituent->GetParam(), only);
432 }
433 else {
434 TGeoHMatrix total
6cfb12b4 435 = Multiply ( appliedGlobalTransform,
436 (*kEnvTrans),
067866a3 437 (*constituent->GetTransformation()) );
cfbf2f7d 438
439 PlaceVolume(constituent->GetName(), geometry->GetVolume(),
440 constituent->GetCopyNo(), total,
441 constituent->GetNpar(), constituent->GetParam(), only);
442 }
e118b27e 443 }
d4bb94a1 444 }
e118b27e 445 } // end of loop over envelopes
446 } // end of loop over builder geometries
447 } // end of loop over builders
d4bb94a1 448}
449
450//_____________________________________________________________________________
451void AliMUONGeometryBuilder::CreateMaterials()
452{
692de412 453/// Construct materials specific to modules via builders
d4bb94a1 454
455 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
456
457 // Get the builder
458 AliMUONVGeometryBuilder* builder
459 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
460
461 // Create materials with each builder
462 if (builder) builder->CreateMaterials();
463 }
464}
465
466//______________________________________________________________________________
6cfb12b4 467void AliMUONGeometryBuilder::InitGeometry(const TString& svmapFileName)
d4bb94a1 468{
692de412 469/// Initialize geometry
d4bb94a1 470
6cfb12b4 471 // Read alignement data if geometry is read from Root file
472 if (gAlice->IsRootGeometry()) {
473 fAlign = true;
474 ReadTransformations();
475 }
476
477 // Read sensitive volume map from a file
478 fGeometry->ReadSVMap(svmapFileName);
479
d4bb94a1 480 // Set the chamber (sensitive region) GEANT identifier
481 //
482 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
483
484 // Get the builder
485 AliMUONVGeometryBuilder* builder
486 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
487
6cfb12b4 488 // Set sensitive volumes with each builder
76497dec 489 builder->SetSensitiveVolumes();
4a9de4af 490
6cfb12b4 491 if (!fAlign) {
492 // Fill local transformations from built geometry
493 builder->FillTransformations();
e118b27e 494
6cfb12b4 495 // Compute global transformations of detection elements
496 FillGlobalTransformations(builder);
497 }
e118b27e 498 }
76497dec 499}
500
501//______________________________________________________________________________
6cfb12b4 502void AliMUONGeometryBuilder::ReadTransformations(const TString& fileName)
76497dec 503{
6cfb12b4 504/// Read transformations from ASCII files
505/// and store them in the geometry parametrisation
76497dec 506
6cfb12b4 507 // Read transformations
508 //
509 AliMUONGeometryTransformer* geomTransformer = fGeometry->GetTransformer();
510 geomTransformer->ReadTransformations(fileName);
511
512 // Compute global transformations of detection elements
513 //
76497dec 514 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
515
76497dec 516 AliMUONVGeometryBuilder* builder
517 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
518
6cfb12b4 519 FillGlobalTransformations(builder);
76497dec 520 }
521}
522
523//______________________________________________________________________________
6cfb12b4 524void AliMUONGeometryBuilder::WriteTransformations(const TString& fileName)
76497dec 525{
6cfb12b4 526/// Write transformations into files per builder
76497dec 527
6cfb12b4 528 AliMUONGeometryTransformer* geomTransformer = fGeometry->GetTransformer();
529 geomTransformer->WriteTransformations(fileName);
d4bb94a1 530}
531
6cfb12b4 532//______________________________________________________________________________
533void AliMUONGeometryBuilder::WriteSVMaps(Bool_t rebuild,
534 const TString& fileName)
d4bb94a1 535{
6cfb12b4 536/// Write sensitive volume maps into files per builder
537
538 // Rebuild sv maps
539 //
540 if (rebuild)
541 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
d4bb94a1 542
6cfb12b4 543 AliMUONVGeometryBuilder* builder
544 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
545
546 builder->RebuildSVMaps();
547 }
548
549 // Write maps in file
550 fGeometry->WriteSVMap(fileName);
551}
d4bb94a1 552
76497dec 553//_____________________________________________________________________________
554void AliMUONGeometryBuilder::SetAlign(Bool_t align)
555{
067866a3 556/// Set the option for alignement
76497dec 557
558 fAlign = align;
559
e118b27e 560 for (Int_t i=0; i<fGeometryBuilders->GetEntriesFast(); i++) {
561
e118b27e 562 AliMUONVGeometryBuilder* builder
563 = (AliMUONVGeometryBuilder*)fGeometryBuilders->At(i);
8618c022 564
565 SetAlign(builder);
566 }
76497dec 567}