New class - container of AliMUONGeometryModuleTransformer objects
[u/mrichter/AliRoot.git] / MUON / AliMUONGeometryTransformer.cxx
CommitLineData
afc8e661 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
39ClassImp(AliMUONGeometryTransformer)
40
41//______________________________________________________________________________
42AliMUONGeometryTransformer::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//______________________________________________________________________________
54AliMUONGeometryTransformer::AliMUONGeometryTransformer()
55 : TObject(),
56 fModuleTransformers(0)
57{
58/// Default constructor
59}
60
61//______________________________________________________________________________
62AliMUONGeometryTransformer::AliMUONGeometryTransformer(
63 const AliMUONGeometryTransformer& right)
64 : TObject(right)
65{
66/// Copy constructor (not implemented)
67
68 AliFatal("Copy constructor not provided.");
69}
70
71//______________________________________________________________________________
72AliMUONGeometryTransformer::~AliMUONGeometryTransformer()
73{
74/// Destructor
75
76 delete fModuleTransformers;
77}
78
79//______________________________________________________________________________
80AliMUONGeometryTransformer&
81AliMUONGeometryTransformer::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//_____________________________________________________________________________
98AliMUONGeometryModuleTransformer*
99AliMUONGeometryTransformer::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//______________________________________________________________________________
116TString 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//______________________________________________________________________________
130TGeoHMatrix 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//______________________________________________________________________________
145void 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);
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//______________________________________________________________________________
174void 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//______________________________________________________________________________
207TString 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//______________________________________________________________________________
251TString 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//______________________________________________________________________________
302void 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//______________________________________________________________________________
330void 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//______________________________________________________________________________
351void 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//______________________________________________________________________________
395Bool_t
396AliMUONGeometryTransformer::ReadTransformations(const TString& fileName)
397{
398// Reads transformations from a file
399// Returns true, if reading finished correctly.
400// ---
401
402 // No reading
403 // if builder is not associated with any geometry module
404 if (fModuleTransformers->GetEntriesFast() == 0) return false;
405
406 // File path
407 TString filePath = gSystem->Getenv("ALICE_ROOT");
408 filePath += "/MUON/data/";
409 filePath += fileName;
410
411 // Open input file
412 ifstream in(filePath, ios::in);
413 if (!in) {
414 cerr << filePath << endl;
415 AliFatal("File not found.");
416 return false;
417 }
418
419 TString key;
420 in >> key;
421 while ( !in.eof() ) {
422 if (key == TString("CH"))
423 key = ReadData1(in);
424 else if (key == TString("DE"))
425 key = ReadData2(in);
426 else {
427 AliFatal(Form("%s key not recognized", key.Data()));
428 return false;
429 }
430 }
431
432 return true;
433}
434
435//______________________________________________________________________________
436Bool_t
437AliMUONGeometryTransformer::WriteTransformations(const TString& fileName) const
438{
439// Writes transformations into a file
440// Returns true, if writing finished correctly.
441// ---
442
443 // No writing
444 // if builder is not associated with any geometry module
445 if (fModuleTransformers->GetEntriesFast() == 0) return false;
446
447 // File path
448 TString filePath = gSystem->Getenv("ALICE_ROOT");
449 filePath += "/MUON/data/";
450 filePath += fileName;
451
452 // Open input file
453 ofstream out(filePath, ios::out);
454 if (!out) {
455 cerr << filePath << endl;
456 AliError("File not found.");
457 return false;
458 }
459#if !defined (__DECCXX)
460 out.setf(std::ios::fixed);
461#endif
462 WriteData1(out);
463 WriteData2(out);
464
465 return true;
466}
467
468//_____________________________________________________________________________
469void AliMUONGeometryTransformer::AddModuleTransformer(
470 AliMUONGeometryModuleTransformer* moduleTransformer)
471{
472/// Add the geometrymodule to the array
473
474 fModuleTransformers->AddAt(moduleTransformer,
475 moduleTransformer->GetModuleId());
476}
477
478//_____________________________________________________________________________
479void AliMUONGeometryTransformer::Global2Local(Int_t detElemId,
480 Float_t xg, Float_t yg, Float_t zg,
481 Float_t& xl, Float_t& yl, Float_t& zl) const
482{
483/// Transform point from the global reference frame (ALIC)
484/// to the local reference frame of the detection element specified
485/// by detElemId.
486
487 const AliMUONGeometryModuleTransformer* kTransformer
488 = GetModuleTransformerByDEId(detElemId);
489
490 if (kTransformer)
491 kTransformer->Global2Local(detElemId, xg, yg, zg, xl, yl, zl);
492}
493
494//_____________________________________________________________________________
495void AliMUONGeometryTransformer::Global2Local(Int_t detElemId,
496 Double_t xg, Double_t yg, Double_t zg,
497 Double_t& xl, Double_t& yl, Double_t& zl) const
498{
499/// Transform point from the global reference frame (ALIC)
500/// to the local reference frame of the detection element specified
501/// by detElemId.
502
503 const AliMUONGeometryModuleTransformer* kTransformer
504 = GetModuleTransformerByDEId(detElemId);
505
506 if (kTransformer)
507 kTransformer->Global2Local(detElemId, xg, yg, zg, xl, yl, zl);
508}
509
510//_____________________________________________________________________________
511void AliMUONGeometryTransformer::Local2Global(Int_t detElemId,
512 Float_t xl, Float_t yl, Float_t zl,
513 Float_t& xg, Float_t& yg, Float_t& zg) const
514{
515/// Transform point from the local reference frame of the detection element
516/// specified by detElemId to the global reference frame (ALIC).
517
518 const AliMUONGeometryModuleTransformer* kTransformer
519 = GetModuleTransformerByDEId(detElemId);
520
521 if (kTransformer)
522 kTransformer->Local2Global(detElemId, xl, yl, zl, xg, yg, zg);
523}
524
525//_____________________________________________________________________________
526void AliMUONGeometryTransformer::Local2Global(Int_t detElemId,
527 Double_t xl, Double_t yl, Double_t zl,
528 Double_t& xg, Double_t& yg, Double_t& zg) const
529{
530/// Transform point from the local reference frame of the detection element
531/// specified by detElemId to the global reference frame (ALIC).
532
533 const AliMUONGeometryModuleTransformer* kTransformer
534 = GetModuleTransformerByDEId(detElemId);
535
536 if (kTransformer)
537 kTransformer->Local2Global(detElemId, xl, yl, zl, xg, yg, zg);
538}
539
540//_____________________________________________________________________________
541const AliMUONGeometryModuleTransformer*
542AliMUONGeometryTransformer::GetModuleTransformer(Int_t index, Bool_t warn) const
543{
544/// Return the geometry module specified by index
545
546 return GetModuleTransformerNonConst(index, warn);
547}
548
549//_____________________________________________________________________________
550const AliMUONGeometryModuleTransformer*
551AliMUONGeometryTransformer::GetModuleTransformerByDEId(Int_t detElemId,
552 Bool_t warn) const
553{
554/// Return the geometry module specified by index
555
556 // Get module index
557 Int_t index = AliMUONGeometryDEIndexing::GetModuleId(detElemId);
558
559 return GetModuleTransformer(index, warn);
560}