]> git.uio.no Git - u/mrichter/AliRoot.git/blame - MUON/AliMUONGeometryTransformer.cxx
Better selection between menus
[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//
f7006443 18// ----------------------------
afc8e661 19// Class AliMUONGeometryTransformer
20// ----------------------------
21// Top container class for geometry transformations
afc8e661 22// Author: Ivana Hrivnacova, IPN Orsay
23
afc8e661 24#include "AliMUONGeometryTransformer.h"
25#include "AliMUONGeometryModuleTransformer.h"
26#include "AliMUONGeometryDetElement.h"
2864a3ac 27#include "AliMUONGeometryBuilder.h"
afc8e661 28
ea7ea157 29#include "AliMpDEManager.h"
30#include "AliMpExMap.h"
31
a7d4e65b 32#include "AliLog.h"
33#include "AliAlignObjMatrix.h"
bf16b32c 34#include "AliAlignObj.h"
a7d4e65b 35
36#include <Riostream.h>
37#include <TSystem.h>
38#include <TClonesArray.h>
39#include <TGeoManager.h>
40#include <TGeoPhysicalNode.h>
a6269834 41#include <TFile.h>
a7d4e65b 42
43#include <sstream>
afc8e661 44
a9aad96e 45/// \cond CLASSIMP
afc8e661 46ClassImp(AliMUONGeometryTransformer)
a9aad96e 47/// \endcond
afc8e661 48
49//______________________________________________________________________________
d4d05625 50AliMUONGeometryTransformer::AliMUONGeometryTransformer(Bool_t isOwner,
51 const TString& detectorName)
52
afc8e661 53 : TObject(),
d4d05625 54 fDetectorName(detectorName),
a7d4e65b 55 fModuleTransformers(0),
56 fMisAlignArray(0)
afc8e661 57{
58/// Standard constructor
59
60 // Create array for geometry modules
ea7ea157 61 fModuleTransformers = new TObjArray(100);
afc8e661 62 fModuleTransformers->SetOwner(isOwner);
63}
64
65//______________________________________________________________________________
66AliMUONGeometryTransformer::AliMUONGeometryTransformer()
67 : TObject(),
d4d05625 68 fDetectorName(),
a7d4e65b 69 fModuleTransformers(0),
70 fMisAlignArray(0)
afc8e661 71{
72/// Default constructor
73}
74
afc8e661 75//______________________________________________________________________________
76AliMUONGeometryTransformer::~AliMUONGeometryTransformer()
77{
78/// Destructor
79
80 delete fModuleTransformers;
a7d4e65b 81 delete fMisAlignArray;
afc8e661 82}
83
afc8e661 84//
85// private methods
86//
87
88//_____________________________________________________________________________
89AliMUONGeometryModuleTransformer*
90AliMUONGeometryTransformer::GetModuleTransformerNonConst(
91 Int_t index, Bool_t warn) const
92{
93/// Return the geometry module specified by index
94
95 if (index < 0 || index >= fModuleTransformers->GetEntriesFast()) {
96 if (warn) {
97 AliWarningStream()
98 << "Index: " << index << " outside limits" << std::endl;
99 }
100 return 0;
101 }
102
103 return (AliMUONGeometryModuleTransformer*) fModuleTransformers->At(index);
104}
105
afc8e661 106//______________________________________________________________________________
107TGeoHMatrix AliMUONGeometryTransformer::GetTransform(
108 Double_t x, Double_t y, Double_t z,
109 Double_t a1, Double_t a2, Double_t a3,
110 Double_t a4, Double_t a5, Double_t a6) const
111{
a9aad96e 112/// Build the transformation from the given parameters
afc8e661 113
114 // Compose transform
115 return TGeoCombiTrans(TGeoTranslation(x, y, z),
116 TGeoRotation("rot", a1, a2, a3, a4, a5, a6));
117}
118
119
120//______________________________________________________________________________
a7d4e65b 121void AliMUONGeometryTransformer::FillModuleVolPath(Int_t moduleId,
122 const TString& volPath)
123{
a9aad96e 124/// Create module with the given moduleId and volPath
a7d4e65b 125
126 // Get/Create geometry module transformer
127 AliMUONGeometryModuleTransformer* moduleTransformer
128 = GetModuleTransformerNonConst(moduleId, false);
129
130 if ( !moduleTransformer ) {
131 moduleTransformer = new AliMUONGeometryModuleTransformer(moduleId);
132 AddModuleTransformer(moduleTransformer);
133 }
134 moduleTransformer->SetVolumePath(volPath);
135}
136
137//______________________________________________________________________________
138void AliMUONGeometryTransformer::FillDetElemVolPath(Int_t detElemId,
139 const TString& volPath)
140{
a9aad96e 141/// Create detection element with the given detElemId and volPath
a7d4e65b 142
143 // Module Id
ea7ea157 144 Int_t moduleId = AliMpDEManager::GetGeomModuleId(detElemId);
a7d4e65b 145
146 // Get detection element store
ea7ea157 147 AliMpExMap* detElements =
a7d4e65b 148 GetModuleTransformer(moduleId)->GetDetElementStore();
149
150 // Add detection element
151 AliMUONGeometryDetElement* detElement
152 = new AliMUONGeometryDetElement(detElemId, volPath);
153 detElements->Add(detElemId, detElement);
154}
155
156
157//______________________________________________________________________________
158void AliMUONGeometryTransformer::FillModuleTransform(Int_t moduleId,
afc8e661 159 Double_t x, Double_t y, Double_t z,
160 Double_t a1, Double_t a2, Double_t a3,
161 Double_t a4, Double_t a5, Double_t a6)
162{
a9aad96e 163/// Fill the transformation of the module.
afc8e661 164
afc8e661 165 AliMUONGeometryModuleTransformer* moduleTransformer
317aa7dc 166 = GetModuleTransformerNonConst(moduleId, false);
afc8e661 167
168 if ( !moduleTransformer) {
a7d4e65b 169 AliErrorStream()
170 << "Module " << moduleId << " has not volume path defined." << endl;
afc8e661 171 }
172
173 // Build the transformation from the parameters
174 TGeoHMatrix transform
175 = GetTransform(x, y, z, a1, a2, a3, a4, a5, a6);
176
177 moduleTransformer->SetTransformation(transform);
afc8e661 178}
179
180//______________________________________________________________________________
a7d4e65b 181void AliMUONGeometryTransformer::FillDetElemTransform(
182 Int_t detElemId,
afc8e661 183 Double_t x, Double_t y, Double_t z,
184 Double_t a1, Double_t a2, Double_t a3,
185 Double_t a4, Double_t a5, Double_t a6)
186{
a9aad96e 187/// Fill the transformation of the detection element.
afc8e661 188
189 // Module Id
ea7ea157 190 Int_t moduleId = AliMpDEManager::GetGeomModuleId(detElemId);
afc8e661 191
a7d4e65b 192 // Get module transformer
2864a3ac 193 const AliMUONGeometryModuleTransformer* kModuleTransformer
194 = GetModuleTransformer(moduleId);
a7d4e65b 195
2864a3ac 196 if ( ! kModuleTransformer ) {
a7d4e65b 197 AliFatal(Form("Module transformer not defined, detElemId: %d", detElemId));
198 return;
2864a3ac 199 }
200
a7d4e65b 201 // Get detection element
202 AliMUONGeometryDetElement* detElement
203 = kModuleTransformer->GetDetElement(detElemId);
204
205 if ( ! detElement ) {
206 AliFatal(Form("Det element %d has not volume path defined", detElemId));
207 return;
208 }
209
210 // Build the transformation from the parameters
211 TGeoHMatrix localTransform
212 = GetTransform(x, y, z, a1, a2, a3, a4, a5, a6);
213 detElement->SetLocalTransformation(localTransform);
214
215 // Compute global transformation
2864a3ac 216 TGeoHMatrix globalTransform
217 = AliMUONGeometryBuilder::Multiply(
218 *kModuleTransformer->GetTransformation(),
219 localTransform );
220 detElement->SetGlobalTransformation(globalTransform);
afc8e661 221}
a7d4e65b 222
223//______________________________________________________________________________
224Bool_t
225AliMUONGeometryTransformer::ReadVolPaths(ifstream& in)
226{
a9aad96e 227/// Read modules and detection element volume paths from stream
a7d4e65b 228
229 Int_t id;
230 TString key, volumePath;
231 in >> key;
afc8e661 232
a7d4e65b 233 while ( !in.eof() ) {
234
235 in >> id >> volumePath;
236
237 // cout << "id=" << id << " "
238 // << "volPath= " << volumePath
239 // << endl;
240
ea7ea157 241 if ( key == AliMUONGeometryModuleTransformer::GetModuleNamePrefix() )
a7d4e65b 242 FillModuleVolPath(id, volumePath);
243
ea7ea157 244 else if ( key == AliMUONGeometryDetElement::GetDENamePrefix() )
a7d4e65b 245 FillDetElemVolPath(id, volumePath);
246
247 else {
248 AliFatal(Form("%s key not recognized", key.Data()));
249 return false;
250 }
251 in >> key;
252 }
253
254 return true;
255}
256
afc8e661 257//______________________________________________________________________________
a7d4e65b 258TString AliMUONGeometryTransformer::ReadModuleTransforms(ifstream& in)
afc8e661 259{
a9aad96e 260/// Read and fill modules transformations from the stream.
261/// Return true, if reading finished correctly.
afc8e661 262
ea7ea157 263 TString key(AliMUONGeometryModuleTransformer::GetModuleNamePrefix());
264 while ( key == AliMUONGeometryModuleTransformer::GetModuleNamePrefix() ) {
a7d4e65b 265 Int_t id;
afc8e661 266 Double_t x, y, z;
267 Double_t a1, a2, a3, a4, a5, a6;
268 TString dummy;
269
270 in >> id;
afc8e661 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
a7d4e65b 283 //cout << "moduleId=" << id << " "
afc8e661 284 // << "position= " << x << ", " << y << ", " << z << " "
285 // << "rotation= " << a1 << ", " << a2 << ", " << a3 << ", "
286 // << a4 << ", " << a5 << ", " << a6
287 // << endl;
288
289 // Fill data
a7d4e65b 290 FillModuleTransform(id, x, y, z, a1, a2, a3, a4, a5, a6);
afc8e661 291
292 // Go to next line
293 in >> key;
294 }
295
296 return key;
297}
298
299//______________________________________________________________________________
a7d4e65b 300TString AliMUONGeometryTransformer::ReadDetElemTransforms(ifstream& in)
afc8e661 301{
a9aad96e 302/// Read detection elements transformations from the stream.
303/// Return true, if reading finished correctly.
afc8e661 304
ea7ea157 305 TString key(AliMUONGeometryDetElement::GetDENamePrefix());
306 while ( key == AliMUONGeometryDetElement::GetDENamePrefix() ) {
afc8e661 307
308 // Input data
309 Int_t detElemId;
afc8e661 310 Double_t x, y, z;
311 Double_t a1, a2, a3, a4, a5, a6;
312 TString dummy;
313
314 in >> detElemId;
afc8e661 315 in >> dummy;
316 in >> x;
317 in >> y;
318 in >> z;
319 in >> dummy;
320 in >> a1;
321 in >> a2;
322 in >> a3;
323 in >> a4;
324 in >> a5;
325 in >> a6;
326
327 //cout << "detElemId=" << detElemId << " "
afc8e661 328 // << "position= " << x << ", " << y << ", " << z << " "
329 // << "rotation= " << a1 << ", " << a2 << ", " << a3 << ", "
a7d4e65b 330 // << a4 << ", " << a5 << ", " << a6
afc8e661 331 // << endl;
332
333 // Fill data
a7d4e65b 334 FillDetElemTransform(detElemId, x, y, z, a1, a2, a3, a4, a5, a6);
afc8e661 335
336 // Go to next line
337 in >> key;
338 }
339
340 return key;
341}
342
a7d4e65b 343//______________________________________________________________________________
344Bool_t
345AliMUONGeometryTransformer::LoadTransforms(TGeoManager* tgeoManager)
346{
a9aad96e 347/// Load transformations for defined modules and detection elements
a7d4e65b 348/// from the root file
349
39ea8a2a 350 if ( !tgeoManager) {
351 AliFatal("No TGeoManager defined.");
352 return false;
353 }
354
a7d4e65b 355 for (Int_t i=0; i<fModuleTransformers->GetEntriesFast(); i++) {
356 AliMUONGeometryModuleTransformer* moduleTransformer
357 = (AliMUONGeometryModuleTransformer*)fModuleTransformers->At(i);
358
359 // Module path
360 TString path = moduleTransformer->GetVolumePath();
361
362 // Make physical node
363 TGeoPhysicalNode* moduleNode = tgeoManager->MakePhysicalNode(path);
364 if ( ! moduleNode ) {
365 AliErrorStream()
366 << "Module id: " << moduleTransformer->GetModuleId()
367 << " volume path: " << path << " not found in geometry." << endl;
368 return false;
369 }
370
371 // Set matrix from physical node
372 TGeoHMatrix matrix = *moduleNode->GetMatrix();
373 moduleTransformer->SetTransformation(matrix);
374
375 // Loop over detection elements
ea7ea157 376 AliMpExMap* detElements = moduleTransformer->GetDetElementStore();
a7d4e65b 377
ea7ea157 378 for (Int_t j=0; j<detElements->GetSize(); j++) {
a7d4e65b 379 AliMUONGeometryDetElement* detElement
ea7ea157 380 = (AliMUONGeometryDetElement*)detElements->GetObject(j);
a7d4e65b 381
382 // Det element path
383 TString dePath = detElement->GetVolumePath();
384
385 // Make physical node
386 TGeoPhysicalNode* deNode = tgeoManager->MakePhysicalNode(dePath);
387 if ( ! deNode ) {
388 AliErrorStream()
389 << "Det element id: " << detElement->GetId()
390 << " volume path: " << path << " not found in geometry." << endl;
391 return false;
392 }
393
394 // Set global matrix from physical node
395 TGeoHMatrix globalMatrix = *deNode->GetMatrix();
396 detElement->SetGlobalTransformation(globalMatrix);
397
398 // Set local matrix
399 TGeoHMatrix localMatrix =
400 AliMUONGeometryBuilder::Multiply(
401 matrix.Inverse(), globalMatrix );
402 detElement->SetLocalTransformation(localMatrix);
403 }
404 }
405 return true;
406}
407
408//______________________________________________________________________________
409Bool_t
410AliMUONGeometryTransformer::ReadVolPaths(const TString& fileName)
411{
a9aad96e 412/// Read detection element volume paths from a file.
413/// Return true, if reading finished correctly.
a7d4e65b 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 ReadVolPaths(in);
429 return true;
430}
431
432//______________________________________________________________________________
433Bool_t
434AliMUONGeometryTransformer::ReadTransformations(const TString& fileName)
435{
a9aad96e 436/// Read transformations from a file.
437/// Return true, if reading finished correctly.
a7d4e65b 438
439 // File path
440 TString filePath = gSystem->Getenv("ALICE_ROOT");
441 filePath += "/MUON/data/";
442 filePath += fileName;
443
444 // Open input file
445 ifstream in(filePath, ios::in);
446 if (!in) {
447 cerr << filePath << endl;
448 AliFatal("File not found.");
449 return false;
450 }
451
452 TString key;
453 in >> key;
454 while ( !in.eof() ) {
ea7ea157 455 if ( key == AliMUONGeometryModuleTransformer::GetModuleNamePrefix() )
a7d4e65b 456 key = ReadModuleTransforms(in);
ea7ea157 457 else if ( key == AliMUONGeometryDetElement::GetDENamePrefix() )
a7d4e65b 458 key = ReadDetElemTransforms(in);
459 else {
460 AliFatal(Form("%s key not recognized", key.Data()));
461 return false;
462 }
463 }
464
465 return true;
466}
467
468//______________________________________________________________________________
469Bool_t
470AliMUONGeometryTransformer::ReadTransformations2(const TString& fileName)
471{
a9aad96e 472/// Read transformations from root geometry file.
473/// Return true, if reading finished correctly.
a7d4e65b 474
475 // File path
476 TString filePath = gSystem->Getenv("ALICE_ROOT");
477 filePath += "/MUON/data/";
478 filePath += fileName;
479
480 // Load root geometry
2068c05f 481 TGeoManager* tgeoManager = gGeoManager;
482 if (!tgeoManager)
483 tgeoManager = TGeoManager::Import(fileName);
a7d4e65b 484
485 // Retrieve matrices
486 LoadTransforms(tgeoManager);
2068c05f 487
a7d4e65b 488 return true;
489}
490
afc8e661 491//______________________________________________________________________________
492void AliMUONGeometryTransformer::WriteTransform(ofstream& out,
a7d4e65b 493 const TGeoMatrix* transform) const
afc8e661 494{
a9aad96e 495/// Write given transformation
afc8e661 496
497 out << " pos: ";
498 const Double_t* xyz = transform->GetTranslation();
499 out << setw(10) << setprecision(4) << xyz[0] << " "
500 << setw(10) << setprecision(4) << xyz[1] << " "
501 << setw(10) << setprecision(4) << xyz[2];
502
503 out << " rot: ";
504 const Double_t* rm = transform->GetRotationMatrix();
505 TGeoRotation rotation;
506 rotation.SetMatrix(const_cast<Double_t*>(rm));
507 Double_t a1, a2, a3, a4, a5, a6;
508 rotation.GetAngles(a1, a2, a3, a4, a5, a6);
509
510 out << setw(8) << setprecision(4) << a1 << " "
511 << setw(8) << setprecision(4) << a2 << " "
512 << setw(8) << setprecision(4) << a3 << " "
513 << setw(8) << setprecision(4) << a4 << " "
514 << setw(8) << setprecision(4) << a5 << " "
515 << setw(8) << setprecision(4) << a6 << " " << endl;
516}
517
518//______________________________________________________________________________
a7d4e65b 519void AliMUONGeometryTransformer::WriteModuleVolPaths(ofstream& out) const
afc8e661 520{
a9aad96e 521/// Write module volume paths for all module transformers
a7d4e65b 522
523 for (Int_t i=0; i<fModuleTransformers->GetEntriesFast(); i++) {
524 AliMUONGeometryModuleTransformer* moduleTransformer
525 = (AliMUONGeometryModuleTransformer*)fModuleTransformers->At(i);
526
527 // Write data on out
ea7ea157 528 out << AliMUONGeometryModuleTransformer::GetModuleNamePrefix() << " "
a7d4e65b 529 << setw(4) << moduleTransformer->GetModuleId() << " "
530 << moduleTransformer->GetVolumePath() << endl;
531 }
532 out << endl;
533}
534
535//______________________________________________________________________________
536void AliMUONGeometryTransformer::WriteDetElemVolPaths(ofstream& out) const
537{
a9aad96e 538/// Write detection element volume paths for all detection elements in all
539/// module transformers
a7d4e65b 540
541 for (Int_t i=0; i<fModuleTransformers->GetEntriesFast(); i++) {
542 AliMUONGeometryModuleTransformer* moduleTransformer
543 = (AliMUONGeometryModuleTransformer*)fModuleTransformers->At(i);
ea7ea157 544 AliMpExMap* detElements = moduleTransformer->GetDetElementStore();
a7d4e65b 545
ea7ea157 546 for (Int_t j=0; j<detElements->GetSize(); j++) {
a7d4e65b 547 AliMUONGeometryDetElement* detElement
ea7ea157 548 = (AliMUONGeometryDetElement*)detElements->GetObject(j);
a7d4e65b 549
550 // Write data on out
ea7ea157 551 out << AliMUONGeometryDetElement::GetDENamePrefix() << " "
a7d4e65b 552 << setw(4) << detElement->GetId() << " "
553 << detElement->GetVolumePath() << endl;
554 }
555 out << endl;
556 }
557}
558
559//______________________________________________________________________________
560void AliMUONGeometryTransformer::WriteModuleTransforms(ofstream& out) const
561{
a9aad96e 562/// Write module transformations for all module transformers
afc8e661 563
564 for (Int_t i=0; i<fModuleTransformers->GetEntriesFast(); i++) {
565 AliMUONGeometryModuleTransformer* moduleTransformer
566 = (AliMUONGeometryModuleTransformer*)fModuleTransformers->At(i);
a7d4e65b 567 const TGeoMatrix* transform
afc8e661 568 = moduleTransformer->GetTransformation();
569
a7d4e65b 570 // Write data on out
ea7ea157 571 out << AliMUONGeometryModuleTransformer::GetModuleNamePrefix() << " "
572 << setw(4) << moduleTransformer->GetModuleId();
afc8e661 573
574 WriteTransform(out, transform);
575 }
576 out << endl;
577}
578
579//______________________________________________________________________________
a7d4e65b 580void AliMUONGeometryTransformer::WriteDetElemTransforms(ofstream& out) const
afc8e661 581{
a9aad96e 582/// Write detection element transformations for all detection elements in all
583/// module transformers
afc8e661 584
afc8e661 585 for (Int_t i=0; i<fModuleTransformers->GetEntriesFast(); i++) {
586 AliMUONGeometryModuleTransformer* moduleTransformer
587 = (AliMUONGeometryModuleTransformer*)fModuleTransformers->At(i);
ea7ea157 588 AliMpExMap* detElements = moduleTransformer->GetDetElementStore();
afc8e661 589
ea7ea157 590 for (Int_t j=0; j<detElements->GetSize(); j++) {
afc8e661 591 AliMUONGeometryDetElement* detElement
ea7ea157 592 = (AliMUONGeometryDetElement*)detElements->GetObject(j);
a7d4e65b 593 const TGeoMatrix* transform
afc8e661 594 = detElement->GetLocalTransformation();
595
afc8e661 596 // Write data on out
ea7ea157 597 out << AliMUONGeometryDetElement::GetDENamePrefix() << " "
598 << setw(4) << detElement->GetId();
afc8e661 599
600 WriteTransform(out, transform);
601 }
602 out << endl;
603 }
604}
605
d4d05625 606//______________________________________________________________________________
607TString AliMUONGeometryTransformer::GetModuleSymName(Int_t moduleId) const
608{
609/// Return the module symbolic name (use for alignment)
610
611 const AliMUONGeometryModuleTransformer* kTransformer
612 = GetModuleTransformer(moduleId);
613 if ( ! kTransformer ) {
614 AliErrorStream() << "Module " << moduleId << " not found." << endl;
615 return "";
616 }
617
618 return "/" + fDetectorName + "/" + kTransformer->GetModuleName();
619}
620
621//______________________________________________________________________________
622TString AliMUONGeometryTransformer::GetDESymName(Int_t detElemId) const
623{
9c4d2d12 624/// Return the detection element symbolic name (used for alignment)
d4d05625 625
626 const AliMUONGeometryDetElement* kDetElement
627 = GetDetElement(detElemId);
628 if ( ! kDetElement ) {
629 AliErrorStream() << "Det element " << detElemId << " not found." << endl;
630 return "";
631 }
632
633 // Module Id
ea7ea157 634 Int_t moduleId = AliMpDEManager::GetGeomModuleId(detElemId);
d4d05625 635
636 return GetModuleSymName(moduleId) + "/" + kDetElement->GetDEName();
637}
638
afc8e661 639//
640// public functions
641//
642
643//______________________________________________________________________________
644Bool_t
a7d4e65b 645AliMUONGeometryTransformer::ReadGeometryData(
646 const TString& volPathFileName,
647 const TString& transformFileName)
afc8e661 648{
a7d4e65b 649/// Read geometry data from given files;
650/// if transformFileName has ".root" extension, the transformations
651/// are loaded from root geometry file, otherwise ASCII file
652/// format is supposed
653
654 Bool_t result1 = ReadVolPaths(volPathFileName);
655
656 // Get file extension
657 std::string fileName = transformFileName.Data();
658 std::string rootExt = fileName.substr(fileName.size()-5, fileName.size());
659 Bool_t result2;
660 if ( rootExt != ".root" )
661 result2 = ReadTransformations(transformFileName);
662 else
663 result2 = ReadTransformations2(transformFileName);
664
665 return result1 && result2;
666}
667
668//______________________________________________________________________________
669Bool_t
670AliMUONGeometryTransformer::ReadGeometryData(
671 const TString& volPathFileName,
672 TGeoManager* tgeoManager)
673{
674/// Load geometry data from root geometry using defined
a9aad96e 675/// volume paths from file
a7d4e65b 676
677 Bool_t result1 = ReadVolPaths(volPathFileName);
678
679 Bool_t result2 = LoadTransforms(tgeoManager);
680
681 return result1 && result2;
682}
683
684//______________________________________________________________________________
685Bool_t
686AliMUONGeometryTransformer::WriteGeometryData(
687 const TString& volPathFileName,
688 const TString& transformFileName,
689 const TString& misalignFileName) const
690{
691/// Write geometry data into given files
692
693 Bool_t result1 = WriteVolumePaths(volPathFileName);
694 Bool_t result2 = WriteTransformations(transformFileName);
695
696 Bool_t result3 = true;
697 if ( misalignFileName != "" )
698 result3 = WriteMisAlignmentData(misalignFileName);
699
700 return result1 && result2 && result3;
701}
702
703//______________________________________________________________________________
704Bool_t
705AliMUONGeometryTransformer::WriteVolumePaths(const TString& fileName) const
706{
a9aad96e 707/// Write volume paths for modules and detection element volumes into a file.
708/// Return true, if writing finished correctly.
afc8e661 709
a7d4e65b 710 // No writing
711 // if builder is not associated with any geometry module
712 if (fModuleTransformers->GetEntriesFast() == 0) return false;
713
afc8e661 714 // File path
715 TString filePath = gSystem->Getenv("ALICE_ROOT");
716 filePath += "/MUON/data/";
717 filePath += fileName;
718
a7d4e65b 719 // Open output file
720 ofstream out(filePath, ios::out);
721 if (!out) {
afc8e661 722 cerr << filePath << endl;
a7d4e65b 723 AliError("File not found.");
afc8e661 724 return false;
725 }
a7d4e65b 726#if !defined (__DECCXX)
727 out.setf(std::ios::fixed);
728#endif
729 WriteModuleVolPaths(out);
730 WriteDetElemVolPaths(out);
731
afc8e661 732 return true;
a7d4e65b 733}
afc8e661 734
735//______________________________________________________________________________
736Bool_t
737AliMUONGeometryTransformer::WriteTransformations(const TString& fileName) const
738{
a9aad96e 739/// Write transformations into a file.
740/// Return true, if writing finished correctly.
afc8e661 741
742 // No writing
743 // if builder is not associated with any geometry module
744 if (fModuleTransformers->GetEntriesFast() == 0) return false;
745
746 // File path
747 TString filePath = gSystem->Getenv("ALICE_ROOT");
748 filePath += "/MUON/data/";
749 filePath += fileName;
750
a7d4e65b 751 // Open output file
afc8e661 752 ofstream out(filePath, ios::out);
753 if (!out) {
754 cerr << filePath << endl;
755 AliError("File not found.");
756 return false;
757 }
758#if !defined (__DECCXX)
759 out.setf(std::ios::fixed);
760#endif
a7d4e65b 761 WriteModuleTransforms(out);
762 WriteDetElemTransforms(out);
763
764 return true;
765}
766
767//______________________________________________________________________________
768Bool_t
769AliMUONGeometryTransformer::WriteMisAlignmentData(const TString& fileName) const
770{
a9aad96e 771/// Write misalignment data into a file
772/// Return true, if writing finished correctly.
a7d4e65b 773
774 // No writing
775 // if builder is not associated with any geometry module
776 if ( fModuleTransformers->GetEntriesFast() == 0 ) {
777 AliWarningStream() << "No geometry modules defined." << endl;
778 return false;
779 }
780
781 // No writing
782 // if builder has no mis-alignment data
783 if ( ! fMisAlignArray ) {
784 AliWarningStream() << "No mis-alignment data defined." << endl;
785 return false;
786 }
787
788 // File path
789 TString filePath = gSystem->Getenv("ALICE_ROOT");
790 filePath += "/MUON/data/";
791 filePath += fileName;
792
793 // Write mis-alignment data in the root file
794 TFile file(fileName.Data(), "RECREATE");
795 fMisAlignArray->Write();
796 file.Close();
afc8e661 797
798 return true;
799}
800
801//_____________________________________________________________________________
802void AliMUONGeometryTransformer::AddModuleTransformer(
803 AliMUONGeometryModuleTransformer* moduleTransformer)
804{
a9aad96e 805/// Add the module transformer to the array
afc8e661 806
ea7ea157 807 // Expand the size if not sufficient
808 Int_t moduleId = moduleTransformer->GetModuleId();
809 if ( moduleId >= fModuleTransformers->GetSize() )
810 fModuleTransformers->Expand(moduleId+1);
811
812 fModuleTransformers->AddAt(moduleTransformer, moduleId);
afc8e661 813}
814
a7d4e65b 815//_____________________________________________________________________________
816void AliMUONGeometryTransformer::AddMisAlignModule(Int_t moduleId,
817 const TGeoHMatrix& matrix)
818{
a9aad96e 819/// Build AliAlignObjMatrix with module ID, its volumePath
a7d4e65b 820/// and the given delta transformation matrix
821
822 if ( ! fMisAlignArray )
823 fMisAlignArray = new TClonesArray("AliAlignObjMatrix", 200);
824
825 const AliMUONGeometryModuleTransformer* kTransformer
826 = GetModuleTransformer(moduleId);
827 if ( ! kTransformer ) {
828 AliErrorStream() << "Module " << moduleId << " not found." << endl;
829 return;
830 }
831
bf16b32c 832 // Get unique align object ID
833 Int_t volId = AliAlignObj::LayerToVolUID(AliAlignObj::kMUON, moduleId);
834
a7d4e65b 835 // Create mis align matrix
836 TClonesArray& refArray =*fMisAlignArray;
837 Int_t pos = fMisAlignArray->GetEntriesFast();
d4d05625 838 new (refArray[pos]) AliAlignObjMatrix(GetModuleSymName(moduleId), volId,
b760c02e 839 const_cast<TGeoHMatrix&>(matrix),kTRUE);
a7d4e65b 840}
841
842//_____________________________________________________________________________
843void AliMUONGeometryTransformer::AddMisAlignDetElement(Int_t detElemId,
844 const TGeoHMatrix& matrix)
845{
a9aad96e 846/// Build AliAlignObjMatrix with detection element ID, its volumePath
a7d4e65b 847/// and the given delta transformation matrix
848
849 if ( ! fMisAlignArray )
850 fMisAlignArray = new TClonesArray("AliAlignObjMatrix", 200);
851
852 const AliMUONGeometryDetElement* kDetElement
853 = GetDetElement(detElemId);
854
855 if ( ! kDetElement ) {
856 AliErrorStream() << "Det element " << detElemId << " not found." << endl;
857 return;
858 }
859
bf16b32c 860 // Get unique align object ID
861 Int_t volId = AliAlignObj::LayerToVolUID(AliAlignObj::kMUON, detElemId);
862
a7d4e65b 863 // Create mis align matrix
864 TClonesArray& refArray =*fMisAlignArray;
865 Int_t pos = fMisAlignArray->GetEntriesFast();
d4d05625 866 new(refArray[pos]) AliAlignObjMatrix(GetDESymName(detElemId), volId,
b760c02e 867 const_cast<TGeoHMatrix&>(matrix),kTRUE);
a7d4e65b 868}
869
d4d05625 870//_____________________________________________________________________________
871void AliMUONGeometryTransformer::AddAlignableVolumes() const
872{
df22c125 873/// Set symbolic names and matrices to alignable objects to TGeo
d4d05625 874
875 if ( ! gGeoManager ) {
876 AliWarning("TGeoManager not defined.");
877 return;
878 }
879
880 // Modules
881 for (Int_t i=0; i<fModuleTransformers->GetEntriesFast(); i++) {
882 AliMUONGeometryModuleTransformer* module
883 = (AliMUONGeometryModuleTransformer*)fModuleTransformers->At(i);
884
885 // Set module symbolic name
df22c125 886 TGeoPNEntry* pnEntry
887 = gGeoManager->SetAlignableEntry(GetModuleSymName(module->GetModuleId()),
888 module->GetVolumePath());
889 // Set module matrix
890 pnEntry->SetMatrix(new TGeoHMatrix(*module->GetTransformation()));
891 // the matrix will be deleted via TGeoManager
892
d4d05625 893 //cout << "Module sym name: " << GetModuleSymName(module->GetModuleId())
894 // << " volPath: " << module->GetVolumePath() << endl;
895
896 // Detection elements
ea7ea157 897 AliMpExMap* detElements = module->GetDetElementStore();
d4d05625 898
ea7ea157 899 for (Int_t j=0; j<detElements->GetSize(); j++) {
d4d05625 900 AliMUONGeometryDetElement* detElement
ea7ea157 901 = (AliMUONGeometryDetElement*)detElements->GetObject(j);
d4d05625 902
903 // Set detection element symbolic name
df22c125 904 TGeoPNEntry* pnEntry
905 = gGeoManager->SetAlignableEntry(GetDESymName(detElement->GetId()),
906 detElement->GetVolumePath());
907 // Set detection element matrix
908 pnEntry->SetMatrix(new TGeoHMatrix(*detElement->GetGlobalTransformation()));
909 // the matrix will be deleted via TGeoManager
910
d4d05625 911 //cout << "DE name: " << GetDESymName(detElement->GetId())
912 // << " volPath: " << detElement->GetVolumePath() << endl;
913 }
914 }
915}
916
afc8e661 917//_____________________________________________________________________________
ae612121 918TClonesArray* AliMUONGeometryTransformer::CreateZeroAlignmentData() const
919{
920/// Create array with zero alignment data
921
922 // Create array for zero-alignment objects
923 TClonesArray* array = new TClonesArray("AliAlignObjMatrix", 200);
924 TClonesArray& refArray =*array;
925 array->SetOwner(true);
926
927 // Identity matrix
928 TGeoHMatrix matrix;
929
930 // Modules
931 for (Int_t i=0; i<fModuleTransformers->GetEntriesFast(); i++) {
932 AliMUONGeometryModuleTransformer* module
933 = (AliMUONGeometryModuleTransformer*)fModuleTransformers->At(i);
934
ae612121 935 Int_t moduleId = module->GetModuleId();
936
937 // Align object ID
938 Int_t volId = AliAlignObj::LayerToVolUID(AliAlignObj::kMUON, moduleId);
939
940 // Create mis align matrix
941 Int_t pos = array->GetEntriesFast();
d4d05625 942 new (refArray[pos]) AliAlignObjMatrix(GetModuleSymName(moduleId), volId, matrix, kTRUE);
ae612121 943 }
944
945 // Detection elements
946 for (Int_t i=0; i<fModuleTransformers->GetEntriesFast(); i++) {
947 AliMUONGeometryModuleTransformer* moduleTransformer
948 = (AliMUONGeometryModuleTransformer*)fModuleTransformers->At(i);
ea7ea157 949 AliMpExMap* detElements = moduleTransformer->GetDetElementStore();
ae612121 950
ea7ea157 951 for (Int_t j=0; j<detElements->GetSize(); j++) {
ae612121 952 AliMUONGeometryDetElement* detElement
ea7ea157 953 = (AliMUONGeometryDetElement*)detElements->GetObject(j);
ae612121 954
ae612121 955 Int_t detElemId = detElement->GetId();
956
957 // Align object ID
958 Int_t volId = AliAlignObj::LayerToVolUID(AliAlignObj::kMUON, detElemId);
959
960 // Create mis align matrix
961 Int_t pos = array->GetEntriesFast();
d4d05625 962 new (refArray[pos]) AliAlignObjMatrix(GetDESymName(detElemId), volId, matrix, kTRUE);
ae612121 963 }
964 }
965
966 return array;
967}
968
969//_____________________________________________________________________________
f384585c 970void AliMUONGeometryTransformer::ClearMisAlignmentData()
971{
972/// Clear the array of misalignment data
973
974 if ( ! fMisAlignArray ) return;
975
976 fMisAlignArray->Delete();
977}
978
979//_____________________________________________________________________________
afc8e661 980void AliMUONGeometryTransformer::Global2Local(Int_t detElemId,
981 Float_t xg, Float_t yg, Float_t zg,
982 Float_t& xl, Float_t& yl, Float_t& zl) const
983{
984/// Transform point from the global reference frame (ALIC)
985/// to the local reference frame of the detection element specified
986/// by detElemId.
f7006443 987
afc8e661 988 const AliMUONGeometryModuleTransformer* kTransformer
989 = GetModuleTransformerByDEId(detElemId);
990
991 if (kTransformer)
992 kTransformer->Global2Local(detElemId, xg, yg, zg, xl, yl, zl);
993}
994
995//_____________________________________________________________________________
996void AliMUONGeometryTransformer::Global2Local(Int_t detElemId,
997 Double_t xg, Double_t yg, Double_t zg,
998 Double_t& xl, Double_t& yl, Double_t& zl) const
999{
1000/// Transform point from the global reference frame (ALIC)
1001/// to the local reference frame of the detection element specified
1002/// by detElemId.
f7006443 1003
afc8e661 1004 const AliMUONGeometryModuleTransformer* kTransformer
1005 = GetModuleTransformerByDEId(detElemId);
1006
1007 if (kTransformer)
1008 kTransformer->Global2Local(detElemId, xg, yg, zg, xl, yl, zl);
1009}
1010
1011//_____________________________________________________________________________
1012void AliMUONGeometryTransformer::Local2Global(Int_t detElemId,
1013 Float_t xl, Float_t yl, Float_t zl,
1014 Float_t& xg, Float_t& yg, Float_t& zg) const
1015{
1016/// Transform point from the local reference frame of the detection element
1017/// specified by detElemId to the global reference frame (ALIC).
1018
1019 const AliMUONGeometryModuleTransformer* kTransformer
1020 = GetModuleTransformerByDEId(detElemId);
1021
1022 if (kTransformer)
1023 kTransformer->Local2Global(detElemId, xl, yl, zl, xg, yg, zg);
1024}
1025
1026//_____________________________________________________________________________
1027void AliMUONGeometryTransformer::Local2Global(Int_t detElemId,
1028 Double_t xl, Double_t yl, Double_t zl,
1029 Double_t& xg, Double_t& yg, Double_t& zg) const
1030{
1031/// Transform point from the local reference frame of the detection element
1032/// specified by detElemId to the global reference frame (ALIC).
1033
1034 const AliMUONGeometryModuleTransformer* kTransformer
1035 = GetModuleTransformerByDEId(detElemId);
1036
1037 if (kTransformer)
1038 kTransformer->Local2Global(detElemId, xl, yl, zl, xg, yg, zg);
1039}
1040
1041//_____________________________________________________________________________
1042const AliMUONGeometryModuleTransformer*
1043AliMUONGeometryTransformer::GetModuleTransformer(Int_t index, Bool_t warn) const
1044{
a9aad96e 1045/// Return the geometry module transformer specified by index
afc8e661 1046
1047 return GetModuleTransformerNonConst(index, warn);
1048}
1049
1050//_____________________________________________________________________________
1051const AliMUONGeometryModuleTransformer*
1052AliMUONGeometryTransformer::GetModuleTransformerByDEId(Int_t detElemId,
1053 Bool_t warn) const
1054{
a9aad96e 1055/// Return the geometry module transformer specified by detection element ID
afc8e661 1056
1057 // Get module index
ea7ea157 1058 Int_t index = AliMpDEManager::GetGeomModuleId(detElemId);
afc8e661 1059
1060 return GetModuleTransformer(index, warn);
1061}
317aa7dc 1062
a7d4e65b 1063//_____________________________________________________________________________
1064const AliMUONGeometryDetElement*
1065AliMUONGeometryTransformer::GetDetElement(Int_t detElemId, Bool_t warn) const
1066{
a9aad96e 1067/// Return detection element with given detElemId
a7d4e65b 1068
1069 const AliMUONGeometryModuleTransformer* kTransformer
1070 = GetModuleTransformerByDEId(detElemId, warn);
1071
1072 if (!kTransformer) return 0;
1073
1074 return kTransformer->GetDetElement(detElemId, warn);
1075}
1076
317aa7dc 1077//_____________________________________________________________________________
1078Bool_t AliMUONGeometryTransformer::HasDE(Int_t detElemId) const
1079{
1080/// Return true if detection element with given detElemId is defined
1081
1082 const AliMUONGeometryModuleTransformer* kTransformer
1083 = GetModuleTransformerByDEId(detElemId, false);
1084
1085 if (!kTransformer) return false;
1086
1087 return ( kTransformer->GetDetElement(detElemId, false) != 0 );
1088}
1089
1090