1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
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 purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
18 // Class AliMUONVGeometryBuilder
19 // -----------------------------
20 // Abstract base class for geometry construction per geometry module(s).
21 // Author: Ivana Hrivnacova, IPN Orsay
24 #include <Riostream.h>
25 #include <TObjArray.h>
27 #include <TGeoMatrix.h>
28 #include <TVirtualMC.h>
30 #include "AliMUONVGeometryBuilder.h"
31 #include "AliMUONGeometryModule.h"
32 #include "AliMUONGeometryDetElement.h"
33 #include "AliMUONGeometryStore.h"
34 #include "AliMUONGeometrySVMap.h"
35 #include "AliMUONGeometryEnvelopeStore.h"
36 #include "AliMUONGeometryEnvelope.h"
37 #include "AliMUONGeometryConstituent.h"
38 #include "AliMUONVGeometryDEIndexing.h"
41 ClassImp(AliMUONVGeometryBuilder)
43 const TString AliMUONVGeometryBuilder::fgkTransformFileNamePrefix = "transform_";
44 const TString AliMUONVGeometryBuilder::fgkSVMapFileNamePrefix = "svmap_";
45 const TString AliMUONVGeometryBuilder::fgkOutFileNameSuffix = ".out";
47 //______________________________________________________________________________
48 AliMUONVGeometryBuilder::AliMUONVGeometryBuilder(const TString& fileName,
49 AliMUONGeometryModule* mg1, AliMUONGeometryModule* mg2,
50 AliMUONGeometryModule* mg3, AliMUONGeometryModule* mg4,
51 AliMUONGeometryModule* mg5, AliMUONGeometryModule* mg6)
53 fTransformFileName(fgkTransformFileNamePrefix+fileName),
54 fSVMapFileName(fgkSVMapFileNamePrefix+fileName),
57 // Standard constructor
59 // Create the module geometries array
60 fModuleGeometries = new TObjArray();
62 if (mg1) fModuleGeometries->Add(mg1);
63 if (mg2) fModuleGeometries->Add(mg2);
64 if (mg3) fModuleGeometries->Add(mg3);
65 if (mg4) fModuleGeometries->Add(mg4);
66 if (mg5) fModuleGeometries->Add(mg5);
67 if (mg6) fModuleGeometries->Add(mg6);
71 //______________________________________________________________________________
72 AliMUONVGeometryBuilder::AliMUONVGeometryBuilder()
78 // Default constructor
82 //______________________________________________________________________________
83 AliMUONVGeometryBuilder::AliMUONVGeometryBuilder(const AliMUONVGeometryBuilder& rhs)
86 // Protected copy constructor
88 AliFatal("Copy constructor is not implemented.");
91 //______________________________________________________________________________
92 AliMUONVGeometryBuilder::~AliMUONVGeometryBuilder() {
94 if (fModuleGeometries) {
95 fModuleGeometries->Clear(); // Sets pointers to 0 since it is not the owner
96 delete fModuleGeometries;
100 //______________________________________________________________________________
101 AliMUONVGeometryBuilder&
102 AliMUONVGeometryBuilder::operator = (const AliMUONVGeometryBuilder& rhs)
104 // Protected assignement operator
106 // check assignement to self
107 if (this == &rhs) return *this;
109 AliFatal("Assignment operator is not implemented.");
118 //______________________________________________________________________________
119 TString AliMUONVGeometryBuilder::ComposePath(const TString& volName,
122 // Compose path from given volName and copyNo
125 TString path(volName);
132 //______________________________________________________________________________
133 void AliMUONVGeometryBuilder::MapSV(const TString& path0,
134 const TString& volName, Int_t detElemId) const
136 // Update the path with all daughters volumes recursively
137 // and map it to the detection element Id if it is a sensitive volume
140 Int_t nofDaughters = gMC->NofVolDaughters(volName);
141 if (nofDaughters == 0) {
143 // Get the name of the last volume in the path
144 Ssiz_t npos1 = path0.Last('/')+1;
145 Ssiz_t npos2 = path0.Last('.');
146 TString volName(path0(npos1, npos2-npos1));
148 // Check if it is sensitive volume
149 Int_t moduleId = AliMUONVGeometryDEIndexing::GetModuleId(detElemId);
150 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
151 if (geometry->IsSensitiveVolume(volName)) {
152 //cout << ".. adding to the map "
153 // << path0 << " " << detElemId << endl;
154 FillData(path0, detElemId);
159 for (Int_t i=0; i<nofDaughters; i++) {
160 Int_t copyNo = gMC->VolDaughterCopyNo(volName, i);
161 TString newName = gMC->VolDaughterName(volName, i);
163 TString path = path0;
165 path += ComposePath(newName, copyNo);
167 MapSV(path, newName, detElemId);
171 //______________________________________________________________________________
172 void AliMUONVGeometryBuilder::FillData(Int_t moduleId, Int_t nofDetElements,
173 Double_t x, Double_t y, Double_t z,
174 Double_t a1, Double_t a2, Double_t a3,
175 Double_t a4, Double_t a5, Double_t a6) const
177 // Fill the transformation of the module.
181 // Modules numbers in the file are starting from 1
183 GetGeometry(moduleId)
184 ->GetDEIndexing()->SetNofDetElements(nofDetElements);
185 GetGeometry(moduleId)
186 ->SetTranslation(TGeoTranslation(x, y, z));
187 GetGeometry(moduleId)
188 ->SetRotation(TGeoRotation("rot", a1, a2, a3, a4, a5, a6));
191 //______________________________________________________________________________
192 void AliMUONVGeometryBuilder::FillData(
193 Int_t detElemId, const TString& volName, Int_t copyNo,
194 Double_t x, Double_t y, Double_t z,
195 Double_t a1, Double_t a2, Double_t a3,
196 Double_t a4, Double_t a5, Double_t a6) const
198 // Fill the transformation of the detection element.
203 = AliMUONVGeometryDEIndexing::GetModuleId(detElemId);
206 TString path = ComposePath(volName, copyNo);
209 TGeoCombiTrans transform(path, x, y, z,
210 new TGeoRotation(path, a1, a2, a3, a4, a5, a6));
212 // Get detection element store
213 AliMUONGeometryStore* detElements = GetDetElements(moduleId);
215 // Add detection element
216 detElements->Add(detElemId,
217 new AliMUONGeometryDetElement(detElemId, path, transform));
220 //______________________________________________________________________________
221 void AliMUONVGeometryBuilder::FillData(
222 const TString& sensVolumePath, Int_t detElemId) const
224 // Fill the mapping of the sensitive volume path to the detection element.
229 = AliMUONVGeometryDEIndexing::GetModuleId(detElemId);
231 // Get module sensitive volumes map
232 AliMUONGeometrySVMap* svMap = GetSVMap(moduleId);
234 // Map the sensitive volume to detection element
235 svMap->Add(sensVolumePath, detElemId);
238 //______________________________________________________________________________
239 TString AliMUONVGeometryBuilder::ReadData1(ifstream& in) const
241 // Reads and fills modules transformations from a file
242 // Returns true, if reading finished correctly.
246 while ( key == TString("CH") ) {
249 Double_t a1, a2, a3, a4, a5, a6;
266 //cout << "id=" << id << " "
267 // << "position= " << x << ", " << y << ", " << z << " "
268 // << "rotation= " << a1 << ", " << a2 << ", " << a3 << ", "
269 // << a4 << ", " << a5 << ", " << a6
273 FillData(id, n, x, y, z, a1, a2, a3, a4, a5, a6);
282 //______________________________________________________________________________
283 TString AliMUONVGeometryBuilder::ReadData2(ifstream& in) const
285 // Reads detection elements transformations from a file
286 // Returns true, if reading finished correctly.
290 while ( key == TString("DE") ) {
297 Double_t a1, a2, a3, a4, a5, a6;
315 //cout << "detElemId=" << detElemId << " "
316 // << "volume=" << volumeName << " "
317 // << "copyNo=" << copyNo << " "
318 // << "position= " << x << ", " << y << ", " << z << " "
319 // << "rotation= " << a1 << ", " << a2 << ", " << a3 << ", "
320 // << a4 << ", " << a5 << ", " << a6
324 FillData(detElemId, volumeName, copyNo, x, y, z, a1, a2, a3, a4, a5, a6);
333 //______________________________________________________________________________
334 TString AliMUONVGeometryBuilder::ReadData3(ifstream& in) const
336 // Reads detection elements transformations from a file
337 // Returns true, if reading finished correctly.
341 while ( key == TString("SV") ) {
350 //cout << "volumePath=" << volumePath << " "
351 // << "detElemId=" << detElemId
355 FillData(volumePath, detElemId);
364 //______________________________________________________________________________
365 void AliMUONVGeometryBuilder::WriteTransform(ofstream& out,
366 const TGeoCombiTrans* transform) const
368 // Writes the transformations
372 const Double_t* xyz = transform->GetTranslation();
373 out << setw(10) << setprecision(4) << xyz[0] << " "
374 << setw(10) << setprecision(4) << xyz[1] << " "
375 << setw(10) << setprecision(4) << xyz[2];
378 Double_t a1, a2, a3, a4, a5, a6;
379 TGeoRotation* rotation = transform->GetRotation();
381 rotation->GetAngles(a1, a2, a3, a4, a5, a6);
384 TGeoRotation rotation2;
385 rotation2.GetAngles(a1, a2, a3, a4, a5, a6);
388 out << setw(8) << setprecision(4) << a1 << " "
389 << setw(8) << setprecision(4) << a2 << " "
390 << setw(8) << setprecision(4) << a3 << " "
391 << setw(8) << setprecision(4) << a4 << " "
392 << setw(8) << setprecision(4) << a5 << " "
393 << setw(8) << setprecision(4) << a6 << " " << endl;
396 //______________________________________________________________________________
397 void AliMUONVGeometryBuilder::WriteData1(ofstream& out) const
399 // Writes modules transformations
402 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
403 AliMUONGeometryModule* geometry
404 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
405 const TGeoCombiTrans* transform
406 = geometry->GetTransformation();
409 << setw(4) << geometry->GetModuleId() + 1 << " "
410 << setw(4) << geometry->GetDetElementStore()->GetNofEntries() << " ";
412 WriteTransform(out, transform);
417 //______________________________________________________________________________
418 void AliMUONVGeometryBuilder::WriteData2(ofstream& out) const
420 // Writes detection elements (envelopes) transformations
424 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
425 AliMUONGeometryModule* geometry
426 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
427 const TObjArray* envelopes
428 = geometry->GetEnvelopeStore()->GetEnvelopes();
430 for (Int_t j=0; j<envelopes->GetEntriesFast(); j++) {
431 AliMUONGeometryEnvelope* envelope
432 = (AliMUONGeometryEnvelope*)envelopes->At(j);
433 const TGeoCombiTrans* transform
434 = envelope->GetTransformation();
436 // skip envelope not corresponding to detection element
437 if(envelope->GetUniqueID() == 0) continue;
440 << setw(4) << envelope->GetUniqueID() << " "
441 << envelope->GetName() << " "
442 << setw(4)<< envelope->GetCopyNo();
444 WriteTransform(out, transform);
450 //______________________________________________________________________________
451 void AliMUONVGeometryBuilder::WriteData3(ofstream& out) const
453 // Writes association of sensitive volumes and detection elements
454 // from the sensitive volume map
457 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
458 AliMUONGeometryModule* geometry
459 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
460 AliMUONGeometrySVMap* svMap
461 = geometry->GetSVMap();
463 svMap->WriteMap(out);
472 //______________________________________________________________________________
473 AliMUONGeometryModule*
474 AliMUONVGeometryBuilder::GetGeometry(Int_t moduleId) const
476 // Returns the module geometry specified by moduleId
479 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
481 AliMUONGeometryModule* geometry
482 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
484 if ( geometry->GetModuleId() == moduleId) return geometry;
490 //______________________________________________________________________________
491 AliMUONGeometryEnvelopeStore*
492 AliMUONVGeometryBuilder::GetEnvelopes(Int_t moduleId) const
494 // Returns the envelope store of the module geometry specified by moduleId
497 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
500 AliFatal(Form("Module geometry %d is not defined", moduleId));
504 return geometry->GetEnvelopeStore();
507 //______________________________________________________________________________
508 AliMUONGeometryStore*
509 AliMUONVGeometryBuilder::GetDetElements(Int_t moduleId) const
511 // Returns the detection elemnts store of the module geometry specified
515 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
518 AliFatal(Form("Module geometry %d is not defined", moduleId));
522 return geometry->GetDetElementStore();
525 //______________________________________________________________________________
526 AliMUONGeometrySVMap*
527 AliMUONVGeometryBuilder::GetSVMap(Int_t moduleId) const
529 // Returns the transformation store of the module geometry specified by moduleId
532 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
535 AliFatal(Form("Geometry %d is not defined", moduleId));
539 return geometry->GetSVMap();
546 //______________________________________________________________________________
547 void AliMUONVGeometryBuilder::FillTransformations() const
549 // Fills transformations store from defined geometry.
552 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
553 AliMUONGeometryModule* geometry
554 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
555 const TObjArray* envelopes
556 = geometry->GetEnvelopeStore()->GetEnvelopes();
558 AliMUONGeometryStore* detElements = geometry->GetDetElementStore();
560 // Set nof detection elements to the indexing
561 geometry->GetDEIndexing()
562 ->SetNofDetElements(geometry->GetEnvelopeStore()->GetNofDetElements());
564 for (Int_t j=0; j<envelopes->GetEntriesFast(); j++) {
565 AliMUONGeometryEnvelope* envelope
566 = (AliMUONGeometryEnvelope*)envelopes->At(j);
568 // skip envelope not corresponding to detection element
569 if(envelope->GetUniqueID() == 0) continue;
572 Int_t detElemId = envelope->GetUniqueID();
573 TString path = ComposePath(envelope->GetName(),
574 envelope->GetCopyNo());
575 const TGeoCombiTrans* transform = envelope->GetTransformation();
577 // Add detection element transformation
578 detElements->Add(detElemId,
579 new AliMUONGeometryDetElement(detElemId, path, *transform));
584 //_____ _________________________________________________________________________
585 void AliMUONVGeometryBuilder::RebuildSVMaps() const
587 // Clear the SV maps in memory and fill them from defined geometry.
590 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
591 AliMUONGeometryModule* geometry
592 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
595 geometry->GetSVMap()->Clear();
597 // Fill the map from geometry
598 const TObjArray* envelopes
599 = geometry->GetEnvelopeStore()->GetEnvelopes();
601 for (Int_t j=0; j<envelopes->GetEntriesFast(); j++) {
602 AliMUONGeometryEnvelope* envelope
603 = (AliMUONGeometryEnvelope*)envelopes->At(j);
605 // skip envelope not corresponding to detection element
606 if(envelope->GetUniqueID() == 0) continue;
608 TString path0("/ALIC.1");
609 if (geometry->GetMotherVolume() != "ALIC") {
611 path0 += ComposePath(geometry->GetMotherVolume(), 1);
614 if (!envelope->IsVirtual()) {
615 TString path = path0;
617 path += ComposePath(envelope->GetName(), envelope->GetCopyNo());
618 MapSV(path, envelope->GetName(), envelope->GetUniqueID());
621 for (Int_t k=0; k<envelope->GetConstituents()->GetEntriesFast(); k++) {
622 AliMUONGeometryConstituent* constituent
623 = (AliMUONGeometryConstituent*)envelope->GetConstituents()->At(k);
624 TString path = path0;
626 path += ComposePath(constituent->GetName(), constituent->GetCopyNo());
627 MapSV(path, constituent->GetName(), envelope->GetUniqueID());
634 //______________________________________________________________________________
635 Bool_t AliMUONVGeometryBuilder::ReadTransformations() const
637 // Reads transformations from a file
638 // Returns true, if reading finished correctly.
642 // if builder is not associated with any geometry module
643 if (fModuleGeometries->GetEntriesFast() == 0) return false;
646 TString filePath = gSystem->Getenv("ALICE_ROOT");
647 filePath += "/MUON/data/";
648 filePath += fTransformFileName;
651 ifstream in(filePath, ios::in);
653 cerr << filePath << endl;
654 AliFatal("File not found.");
660 while ( !in.eof() ) {
661 if (key == TString("CH"))
663 else if (key == TString("DE"))
666 AliFatal(Form("%s key not recognized", key.Data()));
674 //______________________________________________________________________________
675 Bool_t AliMUONVGeometryBuilder::ReadSVMap() const
677 // Reads the sensitive volume from a file
678 // Returns true, if reading finished correctly.
682 // if builder is not associated with any geometry module
683 if (fModuleGeometries->GetEntriesFast() == 0) return false;
686 TString filePath = gSystem->Getenv("ALICE_ROOT");
687 filePath += "/MUON/data/";
688 filePath += fSVMapFileName;
691 ifstream in(filePath, ios::in);
693 cerr << filePath << endl;
694 AliFatal("File not found.");
700 while ( !in.eof() ) {
701 if (key == TString("SV"))
704 AliFatal(Form("%s key not recognized", key.Data()));
712 //______________________________________________________________________________
713 Bool_t AliMUONVGeometryBuilder::WriteTransformations() const
715 // Writes transformations into a file
716 // Returns true, if writing finished correctly.
720 // if builder is not associated with any geometry module
721 if (fModuleGeometries->GetEntriesFast() == 0) return false;
724 TString filePath = gSystem->Getenv("ALICE_ROOT");
725 filePath += "/MUON/data/";
726 filePath += fTransformFileName;
727 filePath += fgkOutFileNameSuffix;
730 ofstream out(filePath, ios::out);
732 cerr << filePath << endl;
733 AliError("File not found.");
736 #if !defined (__DECCXX)
737 out.setf(std::ios::fixed);
745 //______________________________________________________________________________
746 Bool_t AliMUONVGeometryBuilder::WriteSVMap(Bool_t rebuild) const
748 // Writes sensitive volume map into a file
749 // Returns true, if writing finished correctly.
753 // if builder is not associated with any geometry module
754 if (fModuleGeometries->GetEntriesFast() == 0) return false;
757 TString filePath = gSystem->Getenv("ALICE_ROOT");
758 filePath += "/MUON/data/";
759 filePath += fSVMapFileName;
760 filePath += fgkOutFileNameSuffix;
763 ofstream out(filePath, ios::out);
765 cerr << filePath << endl;
766 AliError("File not found.");
769 #if !defined (__DECCXX)
770 out.setf(std::ios::fixed);
772 if (rebuild) RebuildSVMaps();