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