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"
39 #include "AliMUONGeometryBuilder.h"
42 ClassImp(AliMUONVGeometryBuilder)
44 const TString AliMUONVGeometryBuilder::fgkTransformFileNamePrefix = "transform_";
45 const TString AliMUONVGeometryBuilder::fgkSVMapFileNamePrefix = "svmap_";
46 const TString AliMUONVGeometryBuilder::fgkOutFileNameSuffix = ".out";
48 //______________________________________________________________________________
49 AliMUONVGeometryBuilder::AliMUONVGeometryBuilder(const TString& fileName,
50 AliMUONGeometryModule* mg1, AliMUONGeometryModule* mg2,
51 AliMUONGeometryModule* mg3, AliMUONGeometryModule* mg4,
52 AliMUONGeometryModule* mg5, AliMUONGeometryModule* mg6)
54 fTransformFileName(fgkTransformFileNamePrefix+fileName),
55 fSVMapFileName(fgkSVMapFileNamePrefix+fileName),
59 // Standard constructor
61 // Create the module geometries array
62 fModuleGeometries = new TObjArray();
64 if (mg1) fModuleGeometries->Add(mg1);
65 if (mg2) fModuleGeometries->Add(mg2);
66 if (mg3) fModuleGeometries->Add(mg3);
67 if (mg4) fModuleGeometries->Add(mg4);
68 if (mg5) fModuleGeometries->Add(mg5);
69 if (mg6) fModuleGeometries->Add(mg6);
73 //______________________________________________________________________________
74 AliMUONVGeometryBuilder::AliMUONVGeometryBuilder()
81 // Default constructor
85 //______________________________________________________________________________
86 AliMUONVGeometryBuilder::AliMUONVGeometryBuilder(const AliMUONVGeometryBuilder& rhs)
89 // Protected copy constructor
91 AliFatal("Copy constructor is not implemented.");
94 //______________________________________________________________________________
95 AliMUONVGeometryBuilder::~AliMUONVGeometryBuilder() {
97 if (fModuleGeometries) {
98 fModuleGeometries->Clear(); // Sets pointers to 0 since it is not the owner
99 delete fModuleGeometries;
103 //______________________________________________________________________________
104 AliMUONVGeometryBuilder&
105 AliMUONVGeometryBuilder::operator = (const AliMUONVGeometryBuilder& rhs)
107 // Protected assignement operator
109 // check assignement to self
110 if (this == &rhs) return *this;
112 AliFatal("Assignment operator is not implemented.");
121 //______________________________________________________________________________
122 TString AliMUONVGeometryBuilder::ComposePath(const TString& volName,
125 // Compose path from given volName and copyNo
128 TString path(volName);
135 //______________________________________________________________________________
136 void AliMUONVGeometryBuilder::MapSV(const TString& path0,
137 const TString& volName, Int_t detElemId) const
139 // Update the path with all daughters volumes recursively
140 // and map it to the detection element Id if it is a sensitive volume
143 Int_t nofDaughters = gMC->NofVolDaughters(volName);
144 if (nofDaughters == 0) {
146 // Get the name of the last volume in the path
147 Ssiz_t npos1 = path0.Last('/')+1;
148 Ssiz_t npos2 = path0.Last('.');
149 TString volName(path0(npos1, npos2-npos1));
151 // Check if it is sensitive volume
152 Int_t moduleId = AliMUONVGeometryDEIndexing::GetModuleId(detElemId);
153 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
154 if (geometry->IsSensitiveVolume(volName)) {
155 //cout << ".. adding to the map "
156 // << path0 << " " << detElemId << endl;
157 FillData(path0, detElemId);
162 for (Int_t i=0; i<nofDaughters; i++) {
163 Int_t copyNo = gMC->VolDaughterCopyNo(volName, i);
164 TString newName = gMC->VolDaughterName(volName, i);
166 TString path = path0;
168 path += ComposePath(newName, copyNo);
170 MapSV(path, newName, detElemId);
174 //______________________________________________________________________________
175 TGeoHMatrix AliMUONVGeometryBuilder::GetTransform(
176 Double_t x, Double_t y, Double_t z,
177 Double_t a1, Double_t a2, Double_t a3,
178 Double_t a4, Double_t a5, Double_t a6) const
180 // Builds the transformation from the given parameters
184 TGeoCombiTrans transform(TGeoTranslation(x, y, z),
185 TGeoRotation("rot", a1, a2, a3, a4, a5, a6));
187 // Convert transform to the given reference frame
188 TGeoHMatrix newTransform
189 = AliMUONGeometryBuilder::Multiply( fReferenceFrame.Inverse(),
197 //______________________________________________________________________________
198 void AliMUONVGeometryBuilder::FillData(Int_t moduleId, Int_t nofDetElements,
199 Double_t x, Double_t y, Double_t z,
200 Double_t a1, Double_t a2, Double_t a3,
201 Double_t a4, Double_t a5, Double_t a6) const
203 // Fill the transformation of the module.
207 // Modules numbers in the file are starting from 1
209 // Build the transformation from the parameters
210 TGeoHMatrix newTransform
211 = GetTransform(x, y, z, a1, a2, a3, a4, a5, a6);
213 const Double_t* xyz = newTransform.GetTranslation();
214 const Double_t* rm = newTransform.GetRotationMatrix();
215 TGeoRotation rotation2;
216 rotation2.SetMatrix(const_cast<Double_t*>(rm));
218 GetGeometry(moduleId)
219 ->GetDEIndexing()->SetNofDetElements(nofDetElements);
220 GetGeometry(moduleId)
221 ->SetTranslation(TGeoTranslation(xyz[0], xyz[1], xyz[2]));
222 GetGeometry(moduleId)
223 ->SetRotation(rotation2);
226 //______________________________________________________________________________
227 void AliMUONVGeometryBuilder::FillData(
228 Int_t detElemId, const TString& volName, Int_t copyNo,
229 Double_t x, Double_t y, Double_t z,
230 Double_t a1, Double_t a2, Double_t a3,
231 Double_t a4, Double_t a5, Double_t a6) const
233 // Fill the transformation of the detection element.
238 = AliMUONVGeometryDEIndexing::GetModuleId(detElemId);
241 TString path = ComposePath(volName, copyNo);
243 // Build the transformation from the parameters
244 TGeoHMatrix newTransform
245 = GetTransform(x, y, z, a1, a2, a3, a4, a5, a6);
247 // Compose TGeoCombiTrans
248 TGeoCombiTrans newCombiTransform(newTransform);
250 // Get detection element store
251 AliMUONGeometryStore* detElements = GetDetElements(moduleId);
253 // Add detection element
254 detElements->Add(detElemId,
255 new AliMUONGeometryDetElement(detElemId, path, newCombiTransform));
258 //______________________________________________________________________________
259 void AliMUONVGeometryBuilder::FillData(
260 const TString& sensVolumePath, Int_t detElemId) const
262 // Fill the mapping of the sensitive volume path to the detection element.
267 = AliMUONVGeometryDEIndexing::GetModuleId(detElemId);
269 // Get module sensitive volumes map
270 AliMUONGeometrySVMap* svMap = GetSVMap(moduleId);
272 // Map the sensitive volume to detection element
273 svMap->Add(sensVolumePath, detElemId);
276 //______________________________________________________________________________
277 TString AliMUONVGeometryBuilder::ReadData1(ifstream& in) const
279 // Reads and fills modules transformations from a file
280 // Returns true, if reading finished correctly.
284 while ( key == TString("CH") ) {
287 Double_t a1, a2, a3, a4, a5, a6;
304 //cout << "id=" << id << " "
305 // << "position= " << x << ", " << y << ", " << z << " "
306 // << "rotation= " << a1 << ", " << a2 << ", " << a3 << ", "
307 // << a4 << ", " << a5 << ", " << a6
311 FillData(id, n, x, y, z, a1, a2, a3, a4, a5, a6);
320 //______________________________________________________________________________
321 TString AliMUONVGeometryBuilder::ReadData2(ifstream& in) const
323 // Reads detection elements transformations from a file
324 // Returns true, if reading finished correctly.
328 while ( key == TString("DE") ) {
335 Double_t a1, a2, a3, a4, a5, a6;
353 //cout << "detElemId=" << detElemId << " "
354 // << "volume=" << volumeName << " "
355 // << "copyNo=" << copyNo << " "
356 // << "position= " << x << ", " << y << ", " << z << " "
357 // << "rotation= " << a1 << ", " << a2 << ", " << a3 << ", "
358 // << a4 << ", " << a5 << ", " << a6
362 FillData(detElemId, volumeName, copyNo, x, y, z, a1, a2, a3, a4, a5, a6);
371 //______________________________________________________________________________
372 TString AliMUONVGeometryBuilder::ReadData3(ifstream& in) const
374 // Reads detection elements transformations from a file
375 // Returns true, if reading finished correctly.
379 while ( key == TString("SV") ) {
388 //cout << "volumePath=" << volumePath << " "
389 // << "detElemId=" << detElemId
393 FillData(volumePath, detElemId);
402 //______________________________________________________________________________
403 void AliMUONVGeometryBuilder::WriteTransform(ofstream& out,
404 const TGeoCombiTrans* transform) const
406 // Writes the transformations
407 // after converting them into the specified reference frame
410 // Convert transform to the given reference frame
411 TGeoHMatrix newTransform
412 = fReferenceFrame * (*transform) * (fReferenceFrame.Inverse());
415 const Double_t* xyz = newTransform.GetTranslation();
416 out << setw(10) << setprecision(4) << xyz[0] << " "
417 << setw(10) << setprecision(4) << xyz[1] << " "
418 << setw(10) << setprecision(4) << xyz[2];
421 const Double_t* rm = newTransform.GetRotationMatrix();
422 TGeoRotation rotation;
423 rotation.SetMatrix(const_cast<Double_t*>(rm));
424 Double_t a1, a2, a3, a4, a5, a6;
425 rotation.GetAngles(a1, a2, a3, a4, a5, a6);
427 out << setw(8) << setprecision(4) << a1 << " "
428 << setw(8) << setprecision(4) << a2 << " "
429 << setw(8) << setprecision(4) << a3 << " "
430 << setw(8) << setprecision(4) << a4 << " "
431 << setw(8) << setprecision(4) << a5 << " "
432 << setw(8) << setprecision(4) << a6 << " " << endl;
435 //______________________________________________________________________________
436 void AliMUONVGeometryBuilder::WriteData1(ofstream& out) const
438 // Writes modules transformations
441 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
442 AliMUONGeometryModule* geometry
443 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
444 const TGeoCombiTrans* transform
445 = geometry->GetTransformation();
448 << setw(4) << geometry->GetModuleId() + 1 << " "
449 << setw(4) << geometry->GetDetElementStore()->GetNofEntries() << " ";
451 WriteTransform(out, transform);
456 //______________________________________________________________________________
457 void AliMUONVGeometryBuilder::WriteData2(ofstream& out) const
459 // Writes detection elements (envelopes) transformations
463 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
464 AliMUONGeometryModule* geometry
465 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
466 const TObjArray* envelopes
467 = geometry->GetEnvelopeStore()->GetEnvelopes();
469 for (Int_t j=0; j<envelopes->GetEntriesFast(); j++) {
470 AliMUONGeometryEnvelope* envelope
471 = (AliMUONGeometryEnvelope*)envelopes->At(j);
472 const TGeoCombiTrans* transform
473 = envelope->GetTransformation();
475 // skip envelope not corresponding to detection element
476 if(envelope->GetUniqueID() == 0) continue;
479 << setw(4) << envelope->GetUniqueID() << " "
480 << envelope->GetName() << " "
481 << setw(4)<< envelope->GetCopyNo();
483 WriteTransform(out, transform);
489 //______________________________________________________________________________
490 void AliMUONVGeometryBuilder::WriteData3(ofstream& out) const
492 // Writes association of sensitive volumes and detection elements
493 // from the sensitive volume map
496 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
497 AliMUONGeometryModule* geometry
498 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
499 AliMUONGeometrySVMap* svMap
500 = geometry->GetSVMap();
502 svMap->WriteMap(out);
511 //______________________________________________________________________________
512 AliMUONGeometryModule*
513 AliMUONVGeometryBuilder::GetGeometry(Int_t moduleId) const
515 // Returns the module geometry specified by moduleId
518 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
520 AliMUONGeometryModule* geometry
521 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
523 if ( geometry->GetModuleId() == moduleId) return geometry;
529 //______________________________________________________________________________
530 AliMUONGeometryEnvelopeStore*
531 AliMUONVGeometryBuilder::GetEnvelopes(Int_t moduleId) const
533 // Returns the envelope store of the module geometry specified by moduleId
536 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
539 AliFatal(Form("Module geometry %d is not defined", moduleId));
543 return geometry->GetEnvelopeStore();
546 //______________________________________________________________________________
547 AliMUONGeometryStore*
548 AliMUONVGeometryBuilder::GetDetElements(Int_t moduleId) const
550 // Returns the detection elemnts store of the module geometry specified
554 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
557 AliFatal(Form("Module geometry %d is not defined", moduleId));
561 return geometry->GetDetElementStore();
564 //______________________________________________________________________________
565 AliMUONGeometrySVMap*
566 AliMUONVGeometryBuilder::GetSVMap(Int_t moduleId) const
568 // Returns the transformation store of the module geometry specified by moduleId
571 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
574 AliFatal(Form("Geometry %d is not defined", moduleId));
578 return geometry->GetSVMap();
585 //______________________________________________________________________________
586 void AliMUONVGeometryBuilder::FillTransformations() const
588 // Fills transformations store from defined geometry.
591 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
592 AliMUONGeometryModule* geometry
593 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
594 const TObjArray* envelopes
595 = geometry->GetEnvelopeStore()->GetEnvelopes();
597 AliMUONGeometryStore* detElements = geometry->GetDetElementStore();
599 // Set nof detection elements to the indexing
600 geometry->GetDEIndexing()
601 ->SetNofDetElements(geometry->GetEnvelopeStore()->GetNofDetElements());
603 for (Int_t j=0; j<envelopes->GetEntriesFast(); j++) {
604 AliMUONGeometryEnvelope* envelope
605 = (AliMUONGeometryEnvelope*)envelopes->At(j);
607 // skip envelope not corresponding to detection element
608 if(envelope->GetUniqueID() == 0) continue;
611 Int_t detElemId = envelope->GetUniqueID();
612 TString path = ComposePath(envelope->GetName(),
613 envelope->GetCopyNo());
614 const TGeoCombiTrans* transform = envelope->GetTransformation();
616 // Add detection element transformation
617 detElements->Add(detElemId,
618 new AliMUONGeometryDetElement(detElemId, path, *transform));
623 //_____ _________________________________________________________________________
624 void AliMUONVGeometryBuilder::RebuildSVMaps() const
626 // Clear the SV maps in memory and fill them from defined geometry.
629 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
630 AliMUONGeometryModule* geometry
631 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
634 geometry->GetSVMap()->Clear();
636 // Fill the map from geometry
637 const TObjArray* envelopes
638 = geometry->GetEnvelopeStore()->GetEnvelopes();
640 for (Int_t j=0; j<envelopes->GetEntriesFast(); j++) {
641 AliMUONGeometryEnvelope* envelope
642 = (AliMUONGeometryEnvelope*)envelopes->At(j);
644 // skip envelope not corresponding to detection element
645 if(envelope->GetUniqueID() == 0) continue;
647 TString path0("/ALIC.1");
648 if (geometry->GetMotherVolume() != "ALIC") {
650 path0 += ComposePath(geometry->GetMotherVolume(), 1);
652 if (! geometry->IsVirtual() ) {
654 path0 += ComposePath(geometry->GetVolume(), 1);
657 if (!envelope->IsVirtual()) {
658 TString path = path0;
660 path += ComposePath(envelope->GetName(), envelope->GetCopyNo());
661 MapSV(path, envelope->GetName(), envelope->GetUniqueID());
664 for (Int_t k=0; k<envelope->GetConstituents()->GetEntriesFast(); k++) {
665 AliMUONGeometryConstituent* constituent
666 = (AliMUONGeometryConstituent*)envelope->GetConstituents()->At(k);
667 TString path = path0;
669 path += ComposePath(constituent->GetName(), constituent->GetCopyNo());
670 MapSV(path, constituent->GetName(), envelope->GetUniqueID());
677 //______________________________________________________________________________
679 AliMUONVGeometryBuilder::ReadTransformations() const
681 // Reads transformations from a file
682 // Returns true, if reading finished correctly.
686 // if builder is not associated with any geometry module
687 if (fModuleGeometries->GetEntriesFast() == 0) return false;
690 TString filePath = gSystem->Getenv("ALICE_ROOT");
691 filePath += "/MUON/data/";
692 filePath += fTransformFileName;
695 ifstream in(filePath, ios::in);
697 cerr << filePath << endl;
698 AliFatal("File not found.");
704 while ( !in.eof() ) {
705 if (key == TString("CH"))
707 else if (key == TString("DE"))
710 AliFatal(Form("%s key not recognized", key.Data()));
718 //______________________________________________________________________________
719 Bool_t AliMUONVGeometryBuilder::ReadSVMap() const
721 // Reads the sensitive volume from a file
722 // Returns true, if reading finished correctly.
726 // if builder is not associated with any geometry module
727 if (fModuleGeometries->GetEntriesFast() == 0) return false;
730 TString filePath = gSystem->Getenv("ALICE_ROOT");
731 filePath += "/MUON/data/";
732 filePath += fSVMapFileName;
735 ifstream in(filePath, ios::in);
737 cerr << filePath << endl;
738 AliFatal("File not found.");
744 while ( !in.eof() ) {
745 if (key == TString("SV"))
748 AliFatal(Form("%s key not recognized", key.Data()));
756 //______________________________________________________________________________
758 AliMUONVGeometryBuilder::WriteTransformations() const
760 // Writes transformations into a file
761 // Returns true, if writing finished correctly.
765 // if builder is not associated with any geometry module
766 if (fModuleGeometries->GetEntriesFast() == 0) return false;
769 TString filePath = gSystem->Getenv("ALICE_ROOT");
770 filePath += "/MUON/data/";
771 filePath += fTransformFileName;
772 filePath += fgkOutFileNameSuffix;
775 ofstream out(filePath, ios::out);
777 cerr << filePath << endl;
778 AliError("File not found.");
781 #if !defined (__DECCXX)
782 out.setf(std::ios::fixed);
790 //______________________________________________________________________________
791 Bool_t AliMUONVGeometryBuilder::WriteSVMap(Bool_t rebuild) const
793 // Writes sensitive volume map into a file
794 // Returns true, if writing finished correctly.
798 // if builder is not associated with any geometry module
799 if (fModuleGeometries->GetEntriesFast() == 0) return false;
802 TString filePath = gSystem->Getenv("ALICE_ROOT");
803 filePath += "/MUON/data/";
804 filePath += fSVMapFileName;
805 filePath += fgkOutFileNameSuffix;
808 ofstream out(filePath, ios::out);
810 cerr << filePath << endl;
811 AliError("File not found.");
814 #if !defined (__DECCXX)
815 out.setf(std::ios::fixed);
817 if (rebuild) RebuildSVMaps();