]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONGeometryTransformer.cxx
- Adding HasDE() method;
[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
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 transform 
192     = GetTransform(x, y, z, a1, a2, a3, a4, a5, a6);
193    
194   // Compose TGeoCombiTrans
195   TGeoCombiTrans newCombiTransform(transform);
196     
197   // Get detection element store
198   AliMUONGeometryStore* detElements = 
199     GetModuleTransformer(moduleId)->GetDetElementStore();     
200
201   // Add detection element
202   detElements->Add(detElemId,
203      new AliMUONGeometryDetElement(detElemId, path, newCombiTransform)); 
204 }                  
205   
206 //______________________________________________________________________________
207 TString  AliMUONGeometryTransformer::ReadData1(ifstream& in)
208 {
209 // Reads and fills modules transformations from a file
210 // Returns true, if reading finished correctly.
211 // ---
212
213   TString key("CH");
214   while ( key == TString("CH") ) {
215     Int_t id, n;
216     Double_t  x, y, z;
217     Double_t  a1, a2, a3, a4, a5, a6;
218     TString dummy;
219   
220     in >> id;
221     in >> n;
222     in >> dummy;
223     in >> x;
224     in >> y;
225     in >> z;
226     in >> dummy;
227     in >> a1; 
228     in >> a2; 
229     in >> a3; 
230     in >> a4; 
231     in >> a5; 
232     in >> a6; 
233
234     //cout << "id="     << id << "  "
235     //   << "position= " << x << ", " << y << ", " << z << "  "
236     //   << "rotation= " << a1 << ", " << a2 << ", " << a3  << ", "
237     //                   << a4 << ", " << a5 << ", " << a6 
238     //   << endl;   
239          
240     // Fill data
241     FillData(id, x, y, z, a1, a2, a3, a4, a5, a6);
242     
243     // Go to next line
244     in >> key;
245   }
246   
247   return key;            
248 }
249
250 //______________________________________________________________________________
251 TString  AliMUONGeometryTransformer::ReadData2(ifstream& in)
252 {
253 // Reads detection elements transformations from a file
254 // Returns true, if reading finished correctly.
255 // ---
256
257   TString key("DE");
258   while ( key == TString("DE") ) {
259
260     // Input data
261     Int_t detElemId;
262     TString   volumeName;
263     Int_t     copyNo;
264     Double_t  x, y, z;
265     Double_t  a1, a2, a3, a4, a5, a6;
266     TString dummy;
267   
268     in >> detElemId;
269     in >> volumeName;
270     in >> copyNo;
271     in >> dummy;
272     in >> x;
273     in >> y;
274     in >> z;
275     in >> dummy;
276     in >> a1; 
277     in >> a2; 
278     in >> a3; 
279     in >> a4; 
280     in >> a5; 
281     in >> a6; 
282
283     //cout << "detElemId=" << detElemId << "  "
284     //     << "volume=" << volumeName << "  "
285     //     << "copyNo=" << copyNo << "  "
286     //     << "position= " << x << ", " << y << ", " << z << "  "
287     //     << "rotation= " << a1 << ", " << a2 << ", " << a3  << ", "
288     //                   << a4 << ", " << a5 << ", " << a6 
289     //     << endl;   
290          
291     // Fill data
292     FillData(detElemId, volumeName, copyNo, x, y, z, a1, a2, a3, a4, a5, a6);    
293     
294     // Go to next line
295     in >> key;
296   } 
297   
298   return key;
299 }
300
301 //______________________________________________________________________________
302 void AliMUONGeometryTransformer::WriteTransform(ofstream& out,
303                                    const TGeoCombiTrans* transform) const
304 {
305 // Writes the transformations 
306 // ---
307
308   out << "   pos: ";
309   const Double_t* xyz = transform->GetTranslation();
310   out << setw(10) << setprecision(4) << xyz[0] << "  " 
311       << setw(10) << setprecision(4) << xyz[1] << "  " 
312       << setw(10) << setprecision(4) << xyz[2];
313
314   out << "   rot: ";
315   const Double_t* rm = transform->GetRotationMatrix();
316   TGeoRotation rotation;
317   rotation.SetMatrix(const_cast<Double_t*>(rm));
318   Double_t a1, a2, a3, a4, a5, a6;
319   rotation.GetAngles(a1, a2, a3, a4, a5, a6);
320       
321   out << setw(8) << setprecision(4) << a1 << "  " 
322       << setw(8) << setprecision(4) << a2 << "  " 
323       << setw(8) << setprecision(4) << a3 << "  " 
324       << setw(8) << setprecision(4) << a4 << "  " 
325       << setw(8) << setprecision(4) << a5 << "  " 
326       << setw(8) << setprecision(4) << a6 << "  " << endl; 
327 }
328
329 //______________________________________________________________________________
330 void AliMUONGeometryTransformer::WriteData1(ofstream& out) const
331 {
332 // Writes modules transformations
333 // ---
334
335   for (Int_t i=0; i<fModuleTransformers->GetEntriesFast(); i++) {
336     AliMUONGeometryModuleTransformer* moduleTransformer 
337       = (AliMUONGeometryModuleTransformer*)fModuleTransformers->At(i);
338     const TGeoCombiTrans* transform 
339       = moduleTransformer->GetTransformation();    
340
341     out << "CH " 
342         << setw(4) << moduleTransformer->GetModuleId() + 1 << "  "
343         << setw(4) << moduleTransformer->GetDetElementStore()->GetNofEntries() << "  ";
344     
345     WriteTransform(out, transform);
346   }
347   out << endl;
348 }
349
350 //______________________________________________________________________________
351 void AliMUONGeometryTransformer::WriteData2(ofstream& out) const
352 {
353 // Writes detection elements (envelopes) transformations
354 // ---
355
356
357   for (Int_t i=0; i<fModuleTransformers->GetEntriesFast(); i++) {
358     AliMUONGeometryModuleTransformer* moduleTransformer 
359       = (AliMUONGeometryModuleTransformer*)fModuleTransformers->At(i);
360     AliMUONGeometryStore* detElements 
361       = moduleTransformer->GetDetElementStore();    
362
363     for (Int_t j=0; j<detElements->GetNofEntries(); j++) {
364       AliMUONGeometryDetElement* detElement
365         = (AliMUONGeometryDetElement*)detElements->GetEntry(j);
366       const TGeoCombiTrans* transform 
367         = detElement->GetLocalTransformation(); 
368         
369       // Get volume name & copy number from aligned volume
370       string volCopyNo = detElement->GetAlignedVolume().Data();
371       std::string::size_type first = volCopyNo.find('.');
372       std::string volName = volCopyNo.substr(0, first);
373       std::string copyNoStr = volCopyNo.substr(first+1, volCopyNo.length());
374       std::istringstream in(copyNoStr);
375       Int_t copyNo;
376       in >> copyNo;
377       
378       // Write data on out
379       out << "DE " 
380           << setw(4) << detElement->GetId() << "    " 
381           << volName << " "
382           << setw(4) << copyNo;
383      
384       WriteTransform(out, transform);
385     }
386     out << endl;                        
387   }     
388 }
389
390 //
391 // public functions
392 //
393
394 //______________________________________________________________________________
395 Bool_t  
396 AliMUONGeometryTransformer::ReadTransformations(const TString& fileName)
397 {
398 // Reads transformations from a file
399 // Returns true, if reading finished correctly.
400 // ---
401
402   // File path
403   TString filePath = gSystem->Getenv("ALICE_ROOT");
404   filePath += "/MUON/data/";
405   filePath += fileName;
406   
407   // Open input file
408   ifstream in(filePath, ios::in);
409   if (!in) {
410     cerr << filePath << endl;   
411     AliFatal("File not found.");
412     return false;
413   }
414
415   TString key;
416   in >> key;
417   while ( !in.eof() ) {
418     if (key == TString("CH")) 
419       key = ReadData1(in);
420     else if (key == TString("DE"))
421       key = ReadData2(in);
422     else {
423       AliFatal(Form("%s key not recognized",  key.Data()));
424       return false;
425     }
426   }     
427
428   return true;
429 }
430
431 //______________________________________________________________________________
432 Bool_t  
433 AliMUONGeometryTransformer::WriteTransformations(const TString& fileName) const
434 {
435 // Writes transformations into a file
436 // Returns true, if writing finished correctly.
437 // ---
438
439   // No writing
440   // if builder is not associated with any geometry module
441   if (fModuleTransformers->GetEntriesFast() == 0) return false;
442
443   // File path
444   TString filePath = gSystem->Getenv("ALICE_ROOT");
445   filePath += "/MUON/data/";
446   filePath += fileName;
447   
448   // Open input file
449   ofstream out(filePath, ios::out);
450   if (!out) {
451     cerr << filePath << endl;   
452     AliError("File not found.");
453     return false;
454   }
455 #if !defined (__DECCXX)
456   out.setf(std::ios::fixed);
457 #endif
458   WriteData1(out);
459   WriteData2(out);
460   
461   return true;
462 }  
463
464 //_____________________________________________________________________________
465 void AliMUONGeometryTransformer::AddModuleTransformer(
466                           AliMUONGeometryModuleTransformer* moduleTransformer)
467 {
468 /// Add the geometrymodule to the array
469
470   fModuleTransformers->AddAt(moduleTransformer, 
471                              moduleTransformer->GetModuleId());
472 }
473
474 //_____________________________________________________________________________
475 void AliMUONGeometryTransformer::Global2Local(Int_t detElemId,
476                  Float_t xg, Float_t yg, Float_t zg, 
477                  Float_t& xl, Float_t& yl, Float_t& zl) const
478 {
479 /// Transform point from the global reference frame (ALIC)
480 /// to the local reference frame of the detection element specified
481 /// by detElemId.
482    
483   const AliMUONGeometryModuleTransformer* kTransformer 
484     = GetModuleTransformerByDEId(detElemId);
485   
486   if (kTransformer) 
487     kTransformer->Global2Local(detElemId, xg, yg, zg, xl, yl, zl);
488 }   
489                  
490 //_____________________________________________________________________________
491 void AliMUONGeometryTransformer::Global2Local(Int_t detElemId,
492                  Double_t xg, Double_t yg, Double_t zg, 
493                  Double_t& xl, Double_t& yl, Double_t& zl) const
494 {
495 /// Transform point from the global reference frame (ALIC)
496 /// to the local reference frame of the detection element specified
497 /// by detElemId.
498    
499   const AliMUONGeometryModuleTransformer* kTransformer 
500     = GetModuleTransformerByDEId(detElemId);
501   
502   if (kTransformer) 
503     kTransformer->Global2Local(detElemId, xg, yg, zg, xl, yl, zl);
504 }   
505
506 //_____________________________________________________________________________
507 void AliMUONGeometryTransformer::Local2Global(Int_t detElemId,
508                  Float_t xl, Float_t yl, Float_t zl, 
509                  Float_t& xg, Float_t& yg, Float_t& zg) const
510 {                
511 /// Transform point from the local reference frame of the detection element 
512 /// specified by detElemId to the global reference frame (ALIC).
513
514   const AliMUONGeometryModuleTransformer* kTransformer 
515     = GetModuleTransformerByDEId(detElemId);
516     
517   if (kTransformer) 
518     kTransformer->Local2Global(detElemId, xl, yl, zl, xg, yg, zg);
519 }   
520
521 //_____________________________________________________________________________
522 void AliMUONGeometryTransformer::Local2Global(Int_t detElemId,
523                  Double_t xl, Double_t yl, Double_t zl, 
524                  Double_t& xg, Double_t& yg, Double_t& zg) const
525 {                
526 /// Transform point from the local reference frame of the detection element 
527 /// specified by detElemId to the global reference frame (ALIC).
528
529   const AliMUONGeometryModuleTransformer* kTransformer 
530     = GetModuleTransformerByDEId(detElemId);
531     
532   if (kTransformer) 
533     kTransformer->Local2Global(detElemId, xl, yl, zl, xg, yg, zg);
534 }   
535
536 //_____________________________________________________________________________
537 const AliMUONGeometryModuleTransformer* 
538 AliMUONGeometryTransformer::GetModuleTransformer(Int_t index, Bool_t warn) const
539 {
540 /// Return the geometry module specified by index
541
542   return GetModuleTransformerNonConst(index, warn);
543 }    
544
545 //_____________________________________________________________________________
546 const AliMUONGeometryModuleTransformer* 
547 AliMUONGeometryTransformer::GetModuleTransformerByDEId(Int_t detElemId, 
548                                                        Bool_t warn) const
549 {
550 /// Return the geometry module specified by index
551
552   // Get module index
553   Int_t index = AliMUONGeometryDEIndexing::GetModuleId(detElemId);
554
555   return GetModuleTransformer(index, warn);
556 }    
557
558 //_____________________________________________________________________________
559 Bool_t  AliMUONGeometryTransformer::HasDE(Int_t detElemId) const
560 {
561 /// Return true if detection element with given detElemId is defined
562
563   const AliMUONGeometryModuleTransformer* kTransformer 
564     = GetModuleTransformerByDEId(detElemId, false);
565     
566   if (!kTransformer) return false;
567     
568   return ( kTransformer->GetDetElement(detElemId, false) != 0 );
569 }  
570     
571