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