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);
133 //______________________________________________________________________________
134 void AliMUONVGeometryBuilder::MapSV(const TString& path0,
135 const TString& volName, Int_t detElemId) const
137 // Update the path with all daughters volumes recursively
138 // and map it to the detection element Id if it is a sensitive volume
141 Int_t nofDaughters = gMC->NofVolDaughters(volName);
142 if (nofDaughters == 0) {
144 // Get the name of the last volume in the path
145 Ssiz_t npos1 = path0.Last('/')+1;
146 Ssiz_t npos2 = path0.Last('.');
147 TString volName(path0(npos1, npos2-npos1));
149 // Check if it is sensitive volume
150 Int_t moduleId = AliMUONVGeometryDEIndexing::Instance()
151 ->GetModuleId(detElemId);
152 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
153 if (geometry->IsSensitiveVolume(volName)) {
154 //cout << ".. adding to the map "
155 // << path0 << " " << detElemId << endl;
156 FillData(path0, detElemId);
161 for (Int_t i=0; i<nofDaughters; i++) {
162 Int_t copyNo = gMC->VolDaughterCopyNo(volName, i);
163 TString newName = gMC->VolDaughterName(volName, i);
165 TString path = path0;
167 path += ComposePath(newName, copyNo);
169 MapSV(path, newName, detElemId);
174 //______________________________________________________________________________
175 void AliMUONVGeometryBuilder::MapSV(const TString& /*path0*/,
176 const TString& /*volName*/,
177 Int_t /*detElemId*/) const
179 // Update the path with all daughters volumes recursively
180 // and map it to the detection element Id if it is a sensitive volume
183 AliWarning("Not yet available");
187 //______________________________________________________________________________
188 void AliMUONVGeometryBuilder::FillData(Int_t moduleId, Int_t nofDetElements,
189 Double_t x, Double_t y, Double_t z,
190 Double_t a1, Double_t a2, Double_t a3,
191 Double_t a4, Double_t a5, Double_t a6) const
193 // Fill the transformation of the module.
197 // Modules numbers in the file are starting from 1
199 GetGeometry(moduleId)
200 ->GetDEIndexing()->SetNofDetElements(nofDetElements);
201 GetGeometry(moduleId)
202 ->SetTranslation(TGeoTranslation(x, y, z));
203 GetGeometry(moduleId)
204 ->SetRotation(TGeoRotation("rot", a1, a2, a3, a4, a5, a6));
207 //______________________________________________________________________________
208 void AliMUONVGeometryBuilder::FillData(
209 Int_t detElemId, const TString& volName, Int_t copyNo,
210 Double_t x, Double_t y, Double_t z,
211 Double_t a1, Double_t a2, Double_t a3,
212 Double_t a4, Double_t a5, Double_t a6) const
214 // Fill the transformation of the detection element.
219 = AliMUONVGeometryDEIndexing::GetModuleId(detElemId);
222 TString path = ComposePath(volName, copyNo);
225 TGeoCombiTrans transform(path, x, y, z,
226 new TGeoRotation(path, a1, a2, a3, a4, a5, a6));
228 // Get detection element store
229 AliMUONGeometryStore* detElements = GetDetElements(moduleId);
231 // Add detection element
232 detElements->Add(detElemId,
233 new AliMUONGeometryDetElement(detElemId, path, transform));
236 //______________________________________________________________________________
237 void AliMUONVGeometryBuilder::FillData(
238 const TString& sensVolumePath, Int_t detElemId) const
240 // Fill the mapping of the sensitive volume path to the detection element.
245 = AliMUONVGeometryDEIndexing::GetModuleId(detElemId);
247 // Get module sensitive volumes map
248 AliMUONGeometrySVMap* svMap = GetSVMap(moduleId);
250 // Map the sensitive volume to detection element
251 svMap->Add(sensVolumePath, detElemId);
254 //______________________________________________________________________________
255 TString AliMUONVGeometryBuilder::ReadData1(ifstream& in) const
257 // Reads and fills modules transformations from a file
258 // Returns true, if reading finished correctly.
262 while ( key == TString("CH") ) {
265 Double_t a1, a2, a3, a4, a5, a6;
282 //cout << "id=" << id << " "
283 // << "position= " << x << ", " << y << ", " << z << " "
284 // << "rotation= " << a1 << ", " << a2 << ", " << a3 << ", "
285 // << a4 << ", " << a5 << ", " << a6
289 FillData(id, n, x, y, z, a1, a2, a3, a4, a5, a6);
298 //______________________________________________________________________________
299 TString AliMUONVGeometryBuilder::ReadData2(ifstream& in) const
301 // Reads detection elements transformations from a file
302 // Returns true, if reading finished correctly.
306 while ( key == TString("DE") ) {
313 Double_t a1, a2, a3, a4, a5, a6;
331 //cout << "detElemId=" << detElemId << " "
332 // << "volume=" << volumeName << " "
333 // << "copyNo=" << copyNo << " "
334 // << "position= " << x << ", " << y << ", " << z << " "
335 // << "rotation= " << a1 << ", " << a2 << ", " << a3 << ", "
336 // << a4 << ", " << a5 << ", " << a6
340 FillData(detElemId, volumeName, copyNo, x, y, z, a1, a2, a3, a4, a5, a6);
349 //______________________________________________________________________________
350 TString AliMUONVGeometryBuilder::ReadData3(ifstream& in) const
352 // Reads detection elements transformations from a file
353 // Returns true, if reading finished correctly.
357 while ( key == TString("SV") ) {
366 //cout << "volumePath=" << volumePath << " "
367 // << "detElemId=" << detElemId
371 FillData(volumePath, detElemId);
380 //______________________________________________________________________________
381 void AliMUONVGeometryBuilder::WriteTransform(ofstream& out,
382 const TGeoCombiTrans* transform) const
384 // Writes the transformations
388 const Double_t* xyz = transform->GetTranslation();
389 out << setw(10) << setprecision(4) << xyz[0] << " "
390 << setw(10) << setprecision(4) << xyz[1] << " "
391 << setw(10) << setprecision(4) << xyz[2];
394 Double_t a1, a2, a3, a4, a5, a6;
395 TGeoRotation* rotation = transform->GetRotation();
397 rotation->GetAngles(a1, a2, a3, a4, a5, a6);
400 TGeoRotation rotation2;
401 rotation2.GetAngles(a1, a2, a3, a4, a5, a6);
404 out << setw(8) << setprecision(4) << a1 << " "
405 << setw(8) << setprecision(4) << a2 << " "
406 << setw(8) << setprecision(4) << a3 << " "
407 << setw(8) << setprecision(4) << a4 << " "
408 << setw(8) << setprecision(4) << a5 << " "
409 << setw(8) << setprecision(4) << a6 << " " << endl;
412 //______________________________________________________________________________
413 void AliMUONVGeometryBuilder::WriteData1(ofstream& out) const
415 // Writes modules transformations
418 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
419 AliMUONGeometryModule* geometry
420 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
421 const TGeoCombiTrans* transform
422 = geometry->GetTransformation();
425 << setw(4) << geometry->GetModuleId() + 1 << " "
426 << setw(4) << geometry->GetDetElementStore()->GetNofEntries() << " ";
428 WriteTransform(out, transform);
433 //______________________________________________________________________________
434 void AliMUONVGeometryBuilder::WriteData2(ofstream& out) const
436 // Writes detection elements (envelopes) transformations
440 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
441 AliMUONGeometryModule* geometry
442 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
443 const TObjArray* envelopes
444 = geometry->GetEnvelopeStore()->GetEnvelopes();
446 for (Int_t j=0; j<envelopes->GetEntriesFast(); j++) {
447 AliMUONGeometryEnvelope* envelope
448 = (AliMUONGeometryEnvelope*)envelopes->At(j);
449 const TGeoCombiTrans* transform
450 = envelope->GetTransformation();
452 // skip envelope not corresponding to detection element
453 if(envelope->GetUniqueID() == 0) continue;
456 << setw(4) << envelope->GetUniqueID() << " "
457 << envelope->GetName() << " "
458 << setw(4)<< envelope->GetCopyNo();
460 WriteTransform(out, transform);
466 //______________________________________________________________________________
467 void AliMUONVGeometryBuilder::WriteData3(ofstream& out) const
469 // Writes association of sensitive volumes and detection elements
470 // from the sensitive volume map
473 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
474 AliMUONGeometryModule* geometry
475 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
476 AliMUONGeometrySVMap* svMap
477 = geometry->GetSVMap();
479 svMap->WriteMap(out);
488 //______________________________________________________________________________
489 AliMUONGeometryModule*
490 AliMUONVGeometryBuilder::GetGeometry(Int_t moduleId) const
492 // Returns the module geometry specified by moduleId
495 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
497 AliMUONGeometryModule* geometry
498 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
500 if ( geometry->GetModuleId() == moduleId) return geometry;
506 //______________________________________________________________________________
507 AliMUONGeometryEnvelopeStore*
508 AliMUONVGeometryBuilder::GetEnvelopes(Int_t moduleId) const
510 // Returns the envelope store of the module geometry specified by moduleId
513 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
516 AliFatal(Form("Module geometry %d is not defined", moduleId));
520 return geometry->GetEnvelopeStore();
523 //______________________________________________________________________________
524 AliMUONGeometryStore*
525 AliMUONVGeometryBuilder::GetDetElements(Int_t moduleId) const
527 // Returns the detection elemnts store of the module geometry specified
531 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
534 AliFatal(Form("Module geometry %d is not defined", moduleId));
538 return geometry->GetDetElementStore();
541 //______________________________________________________________________________
542 AliMUONGeometrySVMap*
543 AliMUONVGeometryBuilder::GetSVMap(Int_t moduleId) const
545 // Returns the transformation store of the module geometry specified by moduleId
548 AliMUONGeometryModule* geometry = GetGeometry(moduleId);
551 AliFatal(Form("Geometry %d is not defined", moduleId));
555 return geometry->GetSVMap();
562 //______________________________________________________________________________
563 void AliMUONVGeometryBuilder::FillTransformations() const
565 // Fills transformations store from defined geometry.
568 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
569 AliMUONGeometryModule* geometry
570 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
571 const TObjArray* envelopes
572 = geometry->GetEnvelopeStore()->GetEnvelopes();
574 AliMUONGeometryStore* detElements = geometry->GetDetElementStore();
576 // Set nof detection elements to the indexing
577 geometry->GetDEIndexing()
578 ->SetNofDetElements(geometry->GetEnvelopeStore()->GetNofDetElements());
580 for (Int_t j=0; j<envelopes->GetEntriesFast(); j++) {
581 AliMUONGeometryEnvelope* envelope
582 = (AliMUONGeometryEnvelope*)envelopes->At(j);
584 // skip envelope not corresponding to detection element
585 if(envelope->GetUniqueID() == 0) continue;
588 Int_t detElemId = envelope->GetUniqueID();
589 TString path = ComposePath(envelope->GetName(),
590 envelope->GetCopyNo());
591 const TGeoCombiTrans* transform = envelope->GetTransformation();
593 // Add detection element transformation
594 detElements->Add(detElemId,
595 new AliMUONGeometryDetElement(detElemId, path, *transform));
600 //_____ _________________________________________________________________________
601 void AliMUONVGeometryBuilder::RebuildSVMaps() const
603 // Clear the SV maps in memory and fill them from defined geometry.
606 for (Int_t i=0; i<fModuleGeometries->GetEntriesFast(); i++) {
607 AliMUONGeometryModule* geometry
608 = (AliMUONGeometryModule*)fModuleGeometries->At(i);
611 geometry->GetSVMap()->Clear();
613 // Fill the map from geometry
614 const TObjArray* envelopes
615 = geometry->GetEnvelopeStore()->GetEnvelopes();
617 for (Int_t j=0; j<envelopes->GetEntriesFast(); j++) {
618 AliMUONGeometryEnvelope* envelope
619 = (AliMUONGeometryEnvelope*)envelopes->At(j);
621 // skip envelope not corresponding to detection element
622 if(envelope->GetUniqueID() == 0) continue;
624 TString path0("/ALIC.1");
625 if (geometry->GetMotherVolume() != "ALIC") {
627 path0 += ComposePath(geometry->GetMotherVolume(), 1);
630 if (!envelope->IsVirtual()) {
631 TString path = path0;
633 path += ComposePath(envelope->GetName(), envelope->GetCopyNo());
634 MapSV(path, envelope->GetName(), envelope->GetUniqueID());
637 for (Int_t k=0; k<envelope->GetConstituents()->GetEntriesFast(); k++) {
638 AliMUONGeometryConstituent* constituent
639 = (AliMUONGeometryConstituent*)envelope->GetConstituents()->At(k);
640 TString path = path0;
642 path += ComposePath(constituent->GetName(), constituent->GetCopyNo());
643 MapSV(path, constituent->GetName(), envelope->GetUniqueID());
650 //______________________________________________________________________________
651 Bool_t AliMUONVGeometryBuilder::ReadTransformations() const
653 // Reads transformations from a file
654 // Returns true, if reading finished correctly.
658 // if builder is not associated with any geometry module
659 if (fModuleGeometries->GetEntriesFast() == 0) return false;
662 TString filePath = gSystem->Getenv("ALICE_ROOT");
663 filePath += "/MUON/data/";
664 filePath += fTransformFileName;
667 ifstream in(filePath, ios::in);
669 cerr << filePath << endl;
670 AliFatal("File not found.");
676 while ( !in.eof() ) {
677 if (key == TString("CH"))
679 else if (key == TString("DE"))
682 AliFatal(Form("%s key not recognized", key.Data()));
690 //______________________________________________________________________________
691 Bool_t AliMUONVGeometryBuilder::ReadSVMap() const
693 // Reads the sensitive volume from a file
694 // Returns true, if reading finished correctly.
698 // if builder is not associated with any geometry module
699 if (fModuleGeometries->GetEntriesFast() == 0) return false;
702 TString filePath = gSystem->Getenv("ALICE_ROOT");
703 filePath += "/MUON/data/";
704 filePath += fSVMapFileName;
707 ifstream in(filePath, ios::in);
709 cerr << filePath << endl;
710 AliFatal("File not found.");
716 while ( !in.eof() ) {
717 if (key == TString("SV"))
720 AliFatal(Form("%s key not recognized", key.Data()));
728 //______________________________________________________________________________
729 Bool_t AliMUONVGeometryBuilder::WriteTransformations() const
731 // Writes transformations into a file
732 // Returns true, if writing finished correctly.
736 // if builder is not associated with any geometry module
737 if (fModuleGeometries->GetEntriesFast() == 0) return false;
740 TString filePath = gSystem->Getenv("ALICE_ROOT");
741 filePath += "/MUON/data/";
742 filePath += fTransformFileName;
743 filePath += fgkOutFileNameSuffix;
746 ofstream out(filePath, ios::out);
748 cerr << filePath << endl;
749 AliError("File not found.");
752 #if !defined (__DECCXX)
753 out.setf(std::ios::fixed);
761 //______________________________________________________________________________
762 Bool_t AliMUONVGeometryBuilder::WriteSVMap(Bool_t rebuild) const
764 // Writes sensitive volume map into a file
765 // Returns true, if writing finished correctly.
769 // if builder is not associated with any geometry module
770 if (fModuleGeometries->GetEntriesFast() == 0) return false;
773 TString filePath = gSystem->Getenv("ALICE_ROOT");
774 filePath += "/MUON/data/";
775 filePath += fSVMapFileName;
776 filePath += fgkOutFileNameSuffix;
779 ofstream out(filePath, ios::out);
781 cerr << filePath << endl;
782 AliError("File not found.");
785 #if !defined (__DECCXX)
786 out.setf(std::ios::fixed);
788 if (rebuild) RebuildSVMaps();