]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONGeometryTransformer.cxx
New TGeo features allow us to avoid the use of MANY.
[u/mrichter/AliRoot.git] / MUON / AliMUONGeometryTransformer.cxx
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 AliMUONGeometryTransformer
19 // ----------------------------
20 // Top container class for geometry transformations
21 //
22 // Author: Ivana Hrivnacova, IPN Orsay
23
24 #include <sstream>
25
26 #include <Riostream.h>
27 #include <TSystem.h>
28
29 #include "AliLog.h"
30
31 #include "AliMUONGeometryTransformer.h"
32 #include "AliMUONGeometryModuleTransformer.h"
33 #include "AliMUONGeometryDetElement.h"
34 #include "AliMUONGeometryStore.h"
35 #include "AliMUONGeometryBuilder.h"
36
37
38 ClassImp(AliMUONGeometryTransformer)
39  
40 //______________________________________________________________________________
41 AliMUONGeometryTransformer::AliMUONGeometryTransformer(Bool_t isOwner)
42   : TObject(),
43     fModuleTransformers(0)
44 {
45 /// Standard constructor
46
47   // Create array for geometry modules
48   fModuleTransformers = new TObjArray();
49   fModuleTransformers->SetOwner(isOwner);
50 }
51
52 //______________________________________________________________________________
53 AliMUONGeometryTransformer::AliMUONGeometryTransformer() 
54   : TObject(),
55     fModuleTransformers(0)
56 {
57 /// Default constructor
58
59
60 //______________________________________________________________________________
61 AliMUONGeometryTransformer::AliMUONGeometryTransformer(
62                                    const AliMUONGeometryTransformer& right) 
63   : TObject(right) 
64 {  
65 /// Copy constructor (not implemented)
66
67   AliFatal("Copy constructor not provided.");
68 }
69
70 //______________________________________________________________________________
71 AliMUONGeometryTransformer::~AliMUONGeometryTransformer()
72 {
73 /// Destructor
74
75   delete fModuleTransformers;
76 }
77
78 //______________________________________________________________________________
79 AliMUONGeometryTransformer& 
80 AliMUONGeometryTransformer::operator=(const AliMUONGeometryTransformer& right)
81 {
82 /// Assignement operator (not implemented)
83
84   // check assignement to self
85   if (this == &right) return *this;
86
87   AliFatal("Assignement operator not provided.");
88     
89   return *this;  
90 }    
91
92 //
93 // private methods
94 //
95
96 //_____________________________________________________________________________
97 AliMUONGeometryModuleTransformer* 
98 AliMUONGeometryTransformer::GetModuleTransformerNonConst(
99                                           Int_t index, Bool_t warn) const
100 {
101 /// Return the geometry module specified by index
102
103   if (index < 0 || index >= fModuleTransformers->GetEntriesFast()) {
104     if (warn) {
105       AliWarningStream() 
106         << "Index: " << index << " outside limits" << std::endl;
107     }                    
108     return 0;  
109   }  
110
111   return (AliMUONGeometryModuleTransformer*) fModuleTransformers->At(index);
112 }    
113
114 //______________________________________________________________________________
115 TString  AliMUONGeometryTransformer::ComposePath(const TString& volName, 
116                                                  Int_t copyNo) const
117 {
118 // Compose path from given volName and copyNo
119 // ---
120
121   TString path(volName);
122   path += ".";
123   path += copyNo;
124   
125   return path;
126 }  
127
128 //______________________________________________________________________________
129 TGeoHMatrix AliMUONGeometryTransformer::GetTransform(
130                   Double_t x, Double_t y, Double_t z,
131                   Double_t a1, Double_t a2, Double_t a3, 
132                   Double_t a4, Double_t a5, Double_t a6) const
133 {                 
134 // Builds the transformation from the given parameters
135 // ---
136
137   // Compose transform
138   return TGeoCombiTrans(TGeoTranslation(x, y, z), 
139                         TGeoRotation("rot", a1, a2, a3, a4, a5, a6));
140 }
141
142
143 //______________________________________________________________________________
144 void AliMUONGeometryTransformer::FillData(Int_t moduleId,
145                   Double_t x, Double_t y, Double_t z,
146                   Double_t a1, Double_t a2, Double_t a3,
147                   Double_t a4, Double_t a5, Double_t a6) 
148 {
149 // Fill the transformation of the module.
150 // ---
151
152   // Get/Create geometry module parametrisation
153   moduleId--;
154       // Modules numbers in the file are starting from 1
155
156   AliMUONGeometryModuleTransformer* moduleTransformer
157     = GetModuleTransformerNonConst(moduleId, false);
158
159   if ( !moduleTransformer) {
160     moduleTransformer = new AliMUONGeometryModuleTransformer(moduleId);
161     AddModuleTransformer(moduleTransformer);
162   }  
163       
164   // Build the transformation from the parameters
165   TGeoHMatrix transform 
166     = GetTransform(x, y, z, a1, a2, a3, a4, a5, a6);
167       
168   moduleTransformer->SetTransformation(transform);
169
170 }                  
171   
172 //______________________________________________________________________________
173 void AliMUONGeometryTransformer::FillData(
174                   Int_t detElemId, const TString& volName, Int_t copyNo,
175                   Double_t x, Double_t y, Double_t z,
176                   Double_t a1, Double_t a2, Double_t a3,
177                   Double_t a4, Double_t a5, Double_t a6) 
178 {
179 // Fill the transformation of the detection element.
180 // ---
181
182   // Module Id
183   Int_t moduleId = AliMUONGeometryStore::GetModuleId(detElemId);
184
185   // Compose path
186   TString path = ComposePath(volName, copyNo);
187   
188   // Build the transformation from the parameters
189   TGeoHMatrix localTransform 
190     = GetTransform(x, y, z, a1, a2, a3, a4, a5, a6);
191    
192   // Get detection element store
193   AliMUONGeometryStore* detElements = 
194     GetModuleTransformer(moduleId)->GetDetElementStore();     
195
196   // Add detection element
197   AliMUONGeometryDetElement* detElement
198     = new AliMUONGeometryDetElement(detElemId, path, localTransform);
199   detElements->Add(detElemId, detElement);
200   
201   // Compute global transformation
202   const AliMUONGeometryModuleTransformer* kModuleTransformer
203     = GetModuleTransformer(moduleId);
204   if ( ! kModuleTransformer ) {
205     AliFatal(Form("Module transformation not defined, detElemId %d",
206                   detElemId));
207   }  
208
209   TGeoHMatrix globalTransform 
210     = AliMUONGeometryBuilder::Multiply( 
211                                   *kModuleTransformer->GetTransformation(),
212                                   localTransform );
213   detElement->SetGlobalTransformation(globalTransform);
214 }                  
215   
216 //______________________________________________________________________________
217 TString  AliMUONGeometryTransformer::ReadData1(ifstream& in)
218 {
219 // Reads and fills modules transformations from a file
220 // Returns true, if reading finished correctly.
221 // ---
222
223   TString key("CH");
224   while ( key == TString("CH") ) {
225     Int_t id, n;
226     Double_t  x, y, z;
227     Double_t  a1, a2, a3, a4, a5, a6;
228     TString dummy;
229   
230     in >> id;
231     in >> n;
232     in >> dummy;
233     in >> x;
234     in >> y;
235     in >> z;
236     in >> dummy;
237     in >> a1; 
238     in >> a2; 
239     in >> a3; 
240     in >> a4; 
241     in >> a5; 
242     in >> a6; 
243
244     //cout << "id="     << id << "  "
245     //   << "position= " << x << ", " << y << ", " << z << "  "
246     //   << "rotation= " << a1 << ", " << a2 << ", " << a3  << ", "
247     //                   << a4 << ", " << a5 << ", " << a6 
248     //   << endl;   
249          
250     // Fill data
251     FillData(id, x, y, z, a1, a2, a3, a4, a5, a6);
252     
253     // Go to next line
254     in >> key;
255   }
256   
257   return key;            
258 }
259
260 //______________________________________________________________________________
261 TString  AliMUONGeometryTransformer::ReadData2(ifstream& in)
262 {
263 // Reads detection elements transformations from a file
264 // Returns true, if reading finished correctly.
265 // ---
266
267   TString key("DE");
268   while ( key == TString("DE") ) {
269
270     // Input data
271     Int_t detElemId;
272     TString   volumeName;
273     Int_t     copyNo;
274     Double_t  x, y, z;
275     Double_t  a1, a2, a3, a4, a5, a6;
276     TString dummy;
277   
278     in >> detElemId;
279     in >> volumeName;
280     in >> copyNo;
281     in >> dummy;
282     in >> x;
283     in >> y;
284     in >> z;
285     in >> dummy;
286     in >> a1; 
287     in >> a2; 
288     in >> a3; 
289     in >> a4; 
290     in >> a5; 
291     in >> a6; 
292
293     //cout << "detElemId=" << detElemId << "  "
294     //     << "volume=" << volumeName << "  "
295     //     << "copyNo=" << copyNo << "  "
296     //     << "position= " << x << ", " << y << ", " << z << "  "
297     //     << "rotation= " << a1 << ", " << a2 << ", " << a3  << ", "
298     //                   << a4 << ", " << a5 << ", " << a6 
299     //     << endl;   
300          
301     // Fill data
302     FillData(detElemId, volumeName, copyNo, x, y, z, a1, a2, a3, a4, a5, a6);    
303     
304     // Go to next line
305     in >> key;
306   } 
307   
308   return key;
309 }
310
311 //______________________________________________________________________________
312 void AliMUONGeometryTransformer::WriteTransform(ofstream& out,
313                                    const TGeoCombiTrans* transform) const
314 {
315 // Writes the transformations 
316 // ---
317
318   out << "   pos: ";
319   const Double_t* xyz = transform->GetTranslation();
320   out << setw(10) << setprecision(4) << xyz[0] << "  " 
321       << setw(10) << setprecision(4) << xyz[1] << "  " 
322       << setw(10) << setprecision(4) << xyz[2];
323
324   out << "   rot: ";
325   const Double_t* rm = transform->GetRotationMatrix();
326   TGeoRotation rotation;
327   rotation.SetMatrix(const_cast<Double_t*>(rm));
328   Double_t a1, a2, a3, a4, a5, a6;
329   rotation.GetAngles(a1, a2, a3, a4, a5, a6);
330       
331   out << setw(8) << setprecision(4) << a1 << "  " 
332       << setw(8) << setprecision(4) << a2 << "  " 
333       << setw(8) << setprecision(4) << a3 << "  " 
334       << setw(8) << setprecision(4) << a4 << "  " 
335       << setw(8) << setprecision(4) << a5 << "  " 
336       << setw(8) << setprecision(4) << a6 << "  " << endl; 
337 }
338
339 //______________________________________________________________________________
340 void AliMUONGeometryTransformer::WriteData1(ofstream& out) const
341 {
342 // Writes modules transformations
343 // ---
344
345   for (Int_t i=0; i<fModuleTransformers->GetEntriesFast(); i++) {
346     AliMUONGeometryModuleTransformer* moduleTransformer 
347       = (AliMUONGeometryModuleTransformer*)fModuleTransformers->At(i);
348     const TGeoCombiTrans* transform 
349       = moduleTransformer->GetTransformation();    
350
351     out << "CH " 
352         << setw(4) << moduleTransformer->GetModuleId() + 1 << "  "
353         << setw(4) << moduleTransformer->GetDetElementStore()->GetNofEntries() << "  ";
354     
355     WriteTransform(out, transform);
356   }
357   out << endl;
358 }
359
360 //______________________________________________________________________________
361 void AliMUONGeometryTransformer::WriteData2(ofstream& out) const
362 {
363 // Writes detection elements (envelopes) transformations
364 // ---
365
366
367   for (Int_t i=0; i<fModuleTransformers->GetEntriesFast(); i++) {
368     AliMUONGeometryModuleTransformer* moduleTransformer 
369       = (AliMUONGeometryModuleTransformer*)fModuleTransformers->At(i);
370     AliMUONGeometryStore* detElements 
371       = moduleTransformer->GetDetElementStore();    
372
373     for (Int_t j=0; j<detElements->GetNofEntries(); j++) {
374       AliMUONGeometryDetElement* detElement
375         = (AliMUONGeometryDetElement*)detElements->GetEntry(j);
376       const TGeoCombiTrans* transform 
377         = detElement->GetLocalTransformation(); 
378         
379       // Get volume name & copy number from aligned volume
380       string volCopyNo = detElement->GetAlignedVolume().Data();
381       std::string::size_type first = volCopyNo.find('.');
382       std::string volName = volCopyNo.substr(0, first);
383       std::string copyNoStr = volCopyNo.substr(first+1, volCopyNo.length());
384       std::istringstream in(copyNoStr);
385       Int_t copyNo;
386       in >> copyNo;
387       
388       // Write data on out
389       out << "DE " 
390           << setw(4) << detElement->GetId() << "    " 
391           << volName << " "
392           << setw(4) << copyNo;
393      
394       WriteTransform(out, transform);
395     }
396     out << endl;                        
397   }     
398 }
399
400 //
401 // public functions
402 //
403
404 //______________________________________________________________________________
405 Bool_t  
406 AliMUONGeometryTransformer::ReadTransformations(const TString& fileName)
407 {
408 // Reads transformations from a file
409 // Returns true, if reading finished correctly.
410 // ---
411
412   // File path
413   TString filePath = gSystem->Getenv("ALICE_ROOT");
414   filePath += "/MUON/data/";
415   filePath += fileName;
416   
417   // Open input file
418   ifstream in(filePath, ios::in);
419   if (!in) {
420     cerr << filePath << endl;   
421     AliFatal("File not found.");
422     return false;
423   }
424
425   TString key;
426   in >> key;
427   while ( !in.eof() ) {
428     if (key == TString("CH")) 
429       key = ReadData1(in);
430     else if (key == TString("DE"))
431       key = ReadData2(in);
432     else {
433       AliFatal(Form("%s key not recognized",  key.Data()));
434       return false;
435     }
436   }     
437
438   return true;
439 }
440
441 //______________________________________________________________________________
442 Bool_t  
443 AliMUONGeometryTransformer::WriteTransformations(const TString& fileName) const
444 {
445 // Writes transformations into a file
446 // Returns true, if writing finished correctly.
447 // ---
448
449   // No writing
450   // if builder is not associated with any geometry module
451   if (fModuleTransformers->GetEntriesFast() == 0) return false;
452
453   // File path
454   TString filePath = gSystem->Getenv("ALICE_ROOT");
455   filePath += "/MUON/data/";
456   filePath += fileName;
457   
458   // Open input file
459   ofstream out(filePath, ios::out);
460   if (!out) {
461     cerr << filePath << endl;   
462     AliError("File not found.");
463     return false;
464   }
465 #if !defined (__DECCXX)
466   out.setf(std::ios::fixed);
467 #endif
468   WriteData1(out);
469   WriteData2(out);
470   
471   return true;
472 }  
473
474 //_____________________________________________________________________________
475 void AliMUONGeometryTransformer::AddModuleTransformer(
476                           AliMUONGeometryModuleTransformer* moduleTransformer)
477 {
478 /// Add the geometrymodule to the array
479
480   fModuleTransformers->AddAt(moduleTransformer, 
481                              moduleTransformer->GetModuleId());
482 }
483
484 //_____________________________________________________________________________
485 void AliMUONGeometryTransformer::Global2Local(Int_t detElemId,
486                  Float_t xg, Float_t yg, Float_t zg, 
487                  Float_t& xl, Float_t& yl, Float_t& zl) const
488 {
489 /// Transform point from the global reference frame (ALIC)
490 /// to the local reference frame of the detection element specified
491 /// by detElemId.
492    
493   const AliMUONGeometryModuleTransformer* kTransformer 
494     = GetModuleTransformerByDEId(detElemId);
495   
496   if (kTransformer) 
497     kTransformer->Global2Local(detElemId, xg, yg, zg, xl, yl, zl);
498 }   
499                  
500 //_____________________________________________________________________________
501 void AliMUONGeometryTransformer::Global2Local(Int_t detElemId,
502                  Double_t xg, Double_t yg, Double_t zg, 
503                  Double_t& xl, Double_t& yl, Double_t& zl) const
504 {
505 /// Transform point from the global reference frame (ALIC)
506 /// to the local reference frame of the detection element specified
507 /// by detElemId.
508    
509   const AliMUONGeometryModuleTransformer* kTransformer 
510     = GetModuleTransformerByDEId(detElemId);
511   
512   if (kTransformer) 
513     kTransformer->Global2Local(detElemId, xg, yg, zg, xl, yl, zl);
514 }   
515
516 //_____________________________________________________________________________
517 void AliMUONGeometryTransformer::Local2Global(Int_t detElemId,
518                  Float_t xl, Float_t yl, Float_t zl, 
519                  Float_t& xg, Float_t& yg, Float_t& zg) const
520 {                
521 /// Transform point from the local reference frame of the detection element 
522 /// specified by detElemId to the global reference frame (ALIC).
523
524   const AliMUONGeometryModuleTransformer* kTransformer 
525     = GetModuleTransformerByDEId(detElemId);
526     
527   if (kTransformer) 
528     kTransformer->Local2Global(detElemId, xl, yl, zl, xg, yg, zg);
529 }   
530
531 //_____________________________________________________________________________
532 void AliMUONGeometryTransformer::Local2Global(Int_t detElemId,
533                  Double_t xl, Double_t yl, Double_t zl, 
534                  Double_t& xg, Double_t& yg, Double_t& zg) const
535 {                
536 /// Transform point from the local reference frame of the detection element 
537 /// specified by detElemId to the global reference frame (ALIC).
538
539   const AliMUONGeometryModuleTransformer* kTransformer 
540     = GetModuleTransformerByDEId(detElemId);
541     
542   if (kTransformer) 
543     kTransformer->Local2Global(detElemId, xl, yl, zl, xg, yg, zg);
544 }   
545
546 //_____________________________________________________________________________
547 const AliMUONGeometryModuleTransformer* 
548 AliMUONGeometryTransformer::GetModuleTransformer(Int_t index, Bool_t warn) const
549 {
550 /// Return the geometry module specified by index
551
552   return GetModuleTransformerNonConst(index, warn);
553 }    
554
555 //_____________________________________________________________________________
556 const AliMUONGeometryModuleTransformer* 
557 AliMUONGeometryTransformer::GetModuleTransformerByDEId(Int_t detElemId, 
558                                                        Bool_t warn) const
559 {
560 /// Return the geometry module specified by index
561
562   // Get module index
563   Int_t index = AliMUONGeometryStore::GetModuleId(detElemId);
564
565   return GetModuleTransformer(index, warn);
566 }    
567
568 //_____________________________________________________________________________
569 Bool_t  AliMUONGeometryTransformer::HasDE(Int_t detElemId) const
570 {
571 /// Return true if detection element with given detElemId is defined
572
573   const AliMUONGeometryModuleTransformer* kTransformer 
574     = GetModuleTransformerByDEId(detElemId, false);
575     
576   if (!kTransformer) return false;
577     
578   return ( kTransformer->GetDetElement(detElemId, false) != 0 );
579 }  
580     
581