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 if ( fReferenceFrame.IsIdentity() )
190 newTransform = transform;
193 = AliMUONGeometryBuilder::Multiply( fReferenceFrame.Inverse(),
202 //______________________________________________________________________________
203 void AliMUONVGeometryBuilder::FillData(Int_t moduleId, Int_t nofDetElements,
204 Double_t x, Double_t y, Double_t z,
205 Double_t a1, Double_t a2, Double_t a3,
206 Double_t a4, Double_t a5, Double_t a6) const
208 // Fill the transformation of the module.
212 // Modules numbers in the file are starting from 1
214 // Build the transformation from the parameters
215 TGeoHMatrix newTransform
216 = GetTransform(x, y, z, a1, a2, a3, a4, a5, a6);
218 const Double_t* xyz = newTransform.GetTranslation();
219 const Double_t* rm = newTransform.GetRotationMatrix();
220 TGeoRotation rotation2;
221 rotation2.SetMatrix(const_cast<Double_t*>(rm));
223 GetGeometry(moduleId)
224 ->GetDEIndexing()->SetNofDetElements(nofDetElements);
225 GetGeometry(moduleId)
226 ->SetTranslation(TGeoTranslation(xyz[0], xyz[1], xyz[2]));
227 GetGeometry(moduleId)
228 ->SetRotation(rotation2);
231 //______________________________________________________________________________
232 void AliMUONVGeometryBuilder::FillData(
233 Int_t detElemId, const TString& volName, Int_t copyNo,
234 Double_t x, Double_t y, Double_t z,
235 Double_t a1, Double_t a2, Double_t a3,
236 Double_t a4, Double_t a5, Double_t a6) const
238 // Fill the transformation of the detection element.
243 = AliMUONVGeometryDEIndexing::GetModuleId(detElemId);
246 TString path = ComposePath(volName, copyNo);
248 // Build the transformation from the parameters
249 TGeoHMatrix newTransform
250 = GetTransform(x, y, z, a1, a2, a3, a4, a5, a6);
252 // Compose TGeoCombiTrans
253 TGeoCombiTrans newCombiTransform(newTransform);
255 // Get detection element store
256 AliMUONGeometryStore* detElements = GetDetElements(moduleId);
258 // Add detection element
259 detElements->Add(detElemId,
260 new AliMUONGeometryDetElement(detElemId, path, newCombiTransform));
263 //______________________________________________________________________________
264 void AliMUONVGeometryBuilder::FillData(
265 const TString& sensVolumePath, Int_t detElemId) const
267 // Fill the mapping of the sensitive volume path to the detection element.
272 = AliMUONVGeometryDEIndexing::GetModuleId(detElemId);
274 // Get module sensitive volumes map
275 AliMUONGeometrySVMap* svMap = GetSVMap(moduleId);
277 // Map the sensitive volume to detection element
278 svMap->Add(sensVolumePath, detElemId);
281 //______________________________________________________________________________
282 TString AliMUONVGeometryBuilder::ReadData1(ifstream& in) const
284 // Reads and fills modules transformations from a file
285 // Returns true, if reading finished correctly.
289 while ( key == TString("CH") ) {
292 Double_t a1, a2, a3, a4, a5, a6;
309 //cout << "id=" << id << " "
310 // << "position= " << x << ", " << y << ", " << z << " "
311 // << "rotation= " << a1 << ", " << a2 << ", " << a3 << ", "
312 // << a4 << ", " << a5 << ", " << a6
316 FillData(id, n, x, y, z, a1, a2, a3, a4, a5, a6);
325 //______________________________________________________________________________
326 TString AliMUONVGeometryBuilder::ReadData2(ifstream& in) const
328 // Reads detection elements transformations from a file
329 // Returns true, if reading finished correctly.
333 while ( key == TString("DE") ) {
340 Double_t a1, a2, a3, a4, a5, a6;
358 //cout << "detElemId=" << detElemId << " "
359 // << "volume=" << volumeName << " "
360 // << "copyNo=" << copyNo << " "
361 // << "position= " << x << ", " << y << ", " << z << " "
362 // << "rotation= " << a1 << ", " << a2 << ", " << a3 << ", "
363 // << a4 << ", " << a5 << ", " << a6
367 FillData(detElemId, volumeName, copyNo, x, y, z, a1, a2, a3, a4, a5, a6);
376 //______________________________________________________________________________
377 TString AliMUONVGeometryBuilder::ReadData3(ifstream& in) const
379 // Reads detection elements transformations from a file
380 // Returns true, if reading finished correctly.
384 while ( key == TString("SV") ) {
393 //cout << "volumePath=" << volumePath << " "
394 // << "detElemId=" << detElemId
398 FillData(volumePath, detElemId);
407 //______________________________________________________________________________
408 void AliMUONVGeometryBuilder::WriteTransform(ofstream& out,
409 const TGeoCombiTrans* transform) const
411 // Writes the transformations
412 // after converting them into the specified reference frame
415 // Convert transform to the given reference frame
416 TGeoHMatrix newTransform;
417 if ( fReferenceFrame.IsIdentity() )
418 newTransform = *transform;
420 newTransform = AliMUONGeometryBuilder::Multiply( fReferenceFrame,
422 fReferenceFrame.Inverse() );
426 const Double_t* xyz = newTransform.GetTranslation();
427 out << setw(10) << setprecision(4) << xyz[0] << " "
428 << setw(10) << setprecision(4) << xyz[1] << " "
429 << setw(10) << setprecision(4) << xyz[2];
432 const Double_t* rm = newTransform.GetRotationMatrix();
433 TGeoRotation rotation;
434 rotation.SetMatrix(const_cast<Double_t*>(rm));
435 Double_t a1, a2, a3, a4, a5, a6;
436 rotation.GetAngles(a1, a2, a3, a4, a5, a6);
438 out << setw(8) << setprecision(4) << a1 << " "
439 << setw(8) << setprecision(4) << a2 << " "
440 << setw(8) << setprecision(4) << a3 << " "
441 << setw(8) << setprecision(4) << a4 << " "
442 << setw(8) << setprecision(4) << a5 << " "
443 << setw(8) << setprecision(4) << a6 << " " << endl;
446 //______________________________________________________________________________
447 void AliMUONVGeometryBuilder::WriteData1(ofstream& out) const
449 // Writes modules transformations
452 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
453 AliMUONGeometryModule* geometry
454 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
455 const TGeoCombiTrans* transform
456 = geometry->GetTransformation();
459 << setw(4) << geometry->GetModuleId() + 1 << " "
460 << setw(4) << geometry->GetDetElementStore()->GetNofEntries() << " ";
462 WriteTransform(out, transform);
467 //______________________________________________________________________________
468 void AliMUONVGeometryBuilder::WriteData2(ofstream& out) const
470 // Writes detection elements (envelopes) transformations
474 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
475 AliMUONGeometryModule* geometry
476 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
477 const TObjArray* envelopes
478 = geometry->GetEnvelopeStore()->GetEnvelopes();
480 for (Int_t j=0; j<envelopes->GetEntriesFast(); j++) {
481 AliMUONGeometryEnvelope* envelope
482 = (AliMUONGeometryEnvelope*)envelopes->At(j);
483 const TGeoCombiTrans* transform
484 = envelope->GetTransformation();
486 // skip envelope not corresponding to detection element
487 if(envelope->GetUniqueID() == 0) continue;
490 << setw(4) << envelope->GetUniqueID() << " "
491 << envelope->GetName() << " "
492 << setw(4)<< envelope->GetCopyNo();
494 WriteTransform(out, transform);
500 //______________________________________________________________________________
501 void AliMUONVGeometryBuilder::WriteData3(ofstream& out) const
503 // Writes association of sensitive volumes and detection elements
504 // from the sensitive volume map
507 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
508 AliMUONGeometryModule* geometry
509 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
510 AliMUONGeometrySVMap* svMap
511 = geometry->GetSVMap();
513 svMap->WriteMap(out);
522 //______________________________________________________________________________
523 AliMUONGeometryModule*
524 AliMUONVGeometryBuilder::GetGeometry(Int_t moduleId) const
526 // Returns the module geometry specified by moduleId
529 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
531 AliMUONGeometryModule* geometry
532 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
534 if ( geometry->GetModuleId() == moduleId) return geometry;
540 //______________________________________________________________________________
541 AliMUONGeometryEnvelopeStore*
542 AliMUONVGeometryBuilder::GetEnvelopes(Int_t moduleId) const
544 // Returns the envelope store of the module geometry specified by moduleId
547 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
550 AliFatal(Form("Module geometry %d is not defined", moduleId));
554 return geometry->GetEnvelopeStore();
557 //______________________________________________________________________________
558 AliMUONGeometryStore*
559 AliMUONVGeometryBuilder::GetDetElements(Int_t moduleId) const
561 // Returns the detection elemnts store of the module geometry specified
565 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
568 AliFatal(Form("Module geometry %d is not defined", moduleId));
572 return geometry->GetDetElementStore();
575 //______________________________________________________________________________
576 AliMUONGeometrySVMap*
577 AliMUONVGeometryBuilder::GetSVMap(Int_t moduleId) const
579 // Returns the transformation store of the module geometry specified by moduleId
582 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
585 AliFatal(Form("Geometry %d is not defined", moduleId));
589 return geometry->GetSVMap();
596 //______________________________________________________________________________
597 void AliMUONVGeometryBuilder::FillTransformations() const
599 // Fills transformations store from defined geometry.
602 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
603 AliMUONGeometryModule* geometry
604 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
605 const TObjArray* envelopes
606 = geometry->GetEnvelopeStore()->GetEnvelopes();
608 AliMUONGeometryStore* detElements = geometry->GetDetElementStore();
610 // Set nof detection elements to the indexing
611 geometry->GetDEIndexing()
612 ->SetNofDetElements(geometry->GetEnvelopeStore()->GetNofDetElements());
614 for (Int_t j=0; j<envelopes->GetEntriesFast(); j++) {
615 AliMUONGeometryEnvelope* envelope
616 = (AliMUONGeometryEnvelope*)envelopes->At(j);
618 // skip envelope not corresponding to detection element
619 if(envelope->GetUniqueID() == 0) continue;
622 Int_t detElemId = envelope->GetUniqueID();
623 TString path = ComposePath(envelope->GetName(),
624 envelope->GetCopyNo());
625 const TGeoCombiTrans* transform = envelope->GetTransformation();
627 // Add detection element transformation
628 detElements->Add(detElemId,
629 new AliMUONGeometryDetElement(detElemId, path, *transform));
634 //_____ _________________________________________________________________________
635 void AliMUONVGeometryBuilder::RebuildSVMaps() const
637 // Clear the SV maps in memory and fill them from defined geometry.
640 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
641 AliMUONGeometryModule* geometry
642 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
645 geometry->GetSVMap()->Clear();
647 // Fill the map from geometry
648 const TObjArray* envelopes
649 = geometry->GetEnvelopeStore()->GetEnvelopes();
651 for (Int_t j=0; j<envelopes->GetEntriesFast(); j++) {
652 AliMUONGeometryEnvelope* envelope
653 = (AliMUONGeometryEnvelope*)envelopes->At(j);
655 // skip envelope not corresponding to detection element
656 if(envelope->GetUniqueID() == 0) continue;
658 TString path0("/ALIC.1");
659 if (geometry->GetMotherVolume() != "ALIC") {
661 path0 += ComposePath(geometry->GetMotherVolume(), 1);
663 if (! geometry->IsVirtual() ) {
665 path0 += ComposePath(geometry->GetVolume(), 1);
668 if (!envelope->IsVirtual()) {
669 TString path = path0;
671 path += ComposePath(envelope->GetName(), envelope->GetCopyNo());
672 MapSV(path, envelope->GetName(), envelope->GetUniqueID());
675 for (Int_t k=0; k<envelope->GetConstituents()->GetEntriesFast(); k++) {
676 AliMUONGeometryConstituent* constituent
677 = (AliMUONGeometryConstituent*)envelope->GetConstituents()->At(k);
678 TString path = path0;
680 path += ComposePath(constituent->GetName(), constituent->GetCopyNo());
681 MapSV(path, constituent->GetName(), envelope->GetUniqueID());
688 //______________________________________________________________________________
690 AliMUONVGeometryBuilder::ReadTransformations() const
692 // Reads transformations from a file
693 // Returns true, if reading finished correctly.
697 // if builder is not associated with any geometry module
698 if (fModuleGeometries->GetEntriesFast() == 0) return false;
701 TString filePath = gSystem->Getenv("ALICE_ROOT");
702 filePath += "/MUON/data/";
703 filePath += fTransformFileName;
706 ifstream in(filePath, ios::in);
708 cerr << filePath << endl;
709 AliFatal("File not found.");
715 while ( !in.eof() ) {
716 if (key == TString("CH"))
718 else if (key == TString("DE"))
721 AliFatal(Form("%s key not recognized", key.Data()));
729 //______________________________________________________________________________
730 Bool_t AliMUONVGeometryBuilder::ReadSVMap() const
732 // Reads the sensitive volume from a file
733 // Returns true, if reading finished correctly.
737 // if builder is not associated with any geometry module
738 if (fModuleGeometries->GetEntriesFast() == 0) return false;
741 TString filePath = gSystem->Getenv("ALICE_ROOT");
742 filePath += "/MUON/data/";
743 filePath += fSVMapFileName;
746 ifstream in(filePath, ios::in);
748 cerr << filePath << endl;
749 AliFatal("File not found.");
755 while ( !in.eof() ) {
756 if (key == TString("SV"))
759 AliFatal(Form("%s key not recognized", key.Data()));
767 //______________________________________________________________________________
769 AliMUONVGeometryBuilder::WriteTransformations() const
771 // Writes transformations into a file
772 // Returns true, if writing finished correctly.
776 // if builder is not associated with any geometry module
777 if (fModuleGeometries->GetEntriesFast() == 0) return false;
780 TString filePath = gSystem->Getenv("ALICE_ROOT");
781 filePath += "/MUON/data/";
782 filePath += fTransformFileName;
783 filePath += fgkOutFileNameSuffix;
786 ofstream out(filePath, ios::out);
788 cerr << filePath << endl;
789 AliError("File not found.");
792 #if !defined (__DECCXX)
793 out.setf(std::ios::fixed);
801 //______________________________________________________________________________
802 Bool_t AliMUONVGeometryBuilder::WriteSVMap(Bool_t rebuild) const
804 // Writes sensitive volume map into a file
805 // Returns true, if writing finished correctly.
809 // if builder is not associated with any geometry module
810 if (fModuleGeometries->GetEntriesFast() == 0) return false;
813 TString filePath = gSystem->Getenv("ALICE_ROOT");
814 filePath += "/MUON/data/";
815 filePath += fSVMapFileName;
816 filePath += fgkOutFileNameSuffix;
819 ofstream out(filePath, ios::out);
821 cerr << filePath << endl;
822 AliError("File not found.");
825 #if !defined (__DECCXX)
826 out.setf(std::ios::fixed);
828 if (rebuild) RebuildSVMaps();