]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONGeometryDetElement.cxx
Additional protection in case of negative indexes. More investigation is needed
[u/mrichter/AliRoot.git] / MUON / AliMUONGeometryDetElement.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
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 purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 // $Id$
17 //
18 // --------------------------------------
19 // Class AliMUONGeometryDetElement
20 // --------------------------------------
21 // The class defines the detection element.
22 // Author: Ivana Hrivnacova, IPN Orsay
23
24 #include "AliMUONGeometryDetElement.h"
25
26 #include "AliLog.h"
27
28 #include <TGeoMatrix.h>
29 #include <Riostream.h>
30
31 #include <sstream>
32
33 ClassImp(AliMUONGeometryDetElement)
34
35 //______________________________________________________________________________
36 AliMUONGeometryDetElement::AliMUONGeometryDetElement(
37                                         Int_t detElemId,
38                                         const TString& volumePath)
39  : TObject(),
40    fVolumePath(volumePath),
41    fLocalTransformation(0),
42    fGlobalTransformation(0)
43
44 /// Standard constructor
45
46   SetUniqueID(detElemId);
47 }
48
49 //______________________________________________________________________________
50 AliMUONGeometryDetElement::AliMUONGeometryDetElement()
51  : TObject(),
52    fVolumePath(),
53    fLocalTransformation(0),
54    fGlobalTransformation(0)
55 {
56 /// Default constructor
57 }
58
59 //______________________________________________________________________________
60 AliMUONGeometryDetElement::AliMUONGeometryDetElement(
61                                    const AliMUONGeometryDetElement& rhs)
62   : TObject(rhs)
63 {
64 /// Protected copy constructor
65
66   AliFatal("Copy constructor is not implemented.");
67 }
68
69 //______________________________________________________________________________
70 AliMUONGeometryDetElement::~AliMUONGeometryDetElement() 
71 {
72 /// Destructor
73
74   delete fLocalTransformation;
75   delete fGlobalTransformation;
76 }
77
78 //______________________________________________________________________________
79 AliMUONGeometryDetElement& 
80 AliMUONGeometryDetElement::operator = (const AliMUONGeometryDetElement& rhs) 
81 {
82 /// Protected assignement operator
83
84   // check assignement to self
85   if (this == &rhs) return *this;
86
87   AliFatal("Assignment operator is not implemented.");
88     
89   return *this;  
90 }
91
92 //
93 // private methods
94 //
95
96 //______________________________________________________________________________
97 void  AliMUONGeometryDetElement::PrintTransform(
98                                             const TGeoHMatrix* transform) const
99 {
100 /// Prints the detection element transformation
101
102   cout << "DetElemId: " << GetUniqueID();
103   cout << "  name: " << fVolumePath << endl;
104
105   if ( !transform ) {
106     cout << "    Transformation not defined." << endl;
107     return;
108   }  
109
110   const double* translation = transform->GetTranslation();
111   cout << "   translation: "
112 #if defined (__DECCXX)
113        << translation[0] << ", " 
114        << translation[1] << ", "
115        << translation[2] << endl;
116 #else
117        << std::fixed
118        << std::setw(7) << std::setprecision(4) << translation[0] << ", " 
119        << std::setw(7) << std::setprecision(4) << translation[1] << ", "
120        << std::setw(7) << std::setprecision(4) << translation[2] << endl;
121 #endif
122          
123   const double* rotation = transform->GetRotationMatrix();
124   cout << "   rotation matrix:  "
125 #if defined (__DECCXX)
126        << rotation[0] << ", " << rotation[1] << ", " << rotation[2] << endl
127        << "                     "           
128        << rotation[3] << ", " << rotation[4] << ", " << rotation[5] << endl         
129        << "                     "           
130        << rotation[6] << ", " << rotation[7] << ", " << rotation[8] << endl;
131 #else
132        << std::fixed
133        << std::setw(7) << std::setprecision(4) 
134        << rotation[0] << ", " << rotation[1] << ", " << rotation[2] << endl
135        << "                     "           
136        << rotation[3] << ", " << rotation[4] << ", " << rotation[5] << endl         
137        << "                     "           
138        << rotation[6] << ", " << rotation[7] << ", " << rotation[8] << endl;
139 #endif
140 }     
141
142 //
143 // public methods
144 //
145
146 //______________________________________________________________________________
147 void  AliMUONGeometryDetElement::Global2Local(
148                                   Float_t xg, Float_t yg, Float_t zg, 
149                                   Float_t& xl, Float_t& yl, Float_t& zl) const
150 {
151 /// Transforms point from the global reference frame (ALIC)
152 /// to the local reference frame of the detection element specified
153 /// by detElemId.
154
155   Double_t dxg = xg;
156   Double_t dyg = yg;
157   Double_t dzg = zg;
158
159   Double_t dxl, dyl, dzl;
160   Global2Local(dxg, dyg, dzg, dxl, dyl, dzl);  
161   
162   xl = dxl;
163   yl = dyl;
164   zl = dzl;
165 }
166                                   
167 //______________________________________________________________________________
168 void  AliMUONGeometryDetElement::Global2Local(
169                                   Double_t xg, Double_t yg, Double_t zg, 
170                                   Double_t& xl, Double_t& yl, Double_t& zl) const
171 {
172 /// Transforms point from the global reference frame (ALIC)
173 /// to the local reference frame of the detection element specified
174 /// by detElemId.
175
176    // Check transformation
177    if (!fGlobalTransformation) {
178      AliError(Form("Global transformation for detection element %d not defined.",
179                     GetUniqueID()));
180      return;
181    }  
182    
183    // Transform point 
184    Double_t pg[3] = { xg, yg, zg };
185    Double_t pl[3] = { 0., 0., 0. };
186    fGlobalTransformation->MasterToLocal(pg, pl);
187    
188    // Return transformed point
189    xl = pl[0];
190    yl = pl[1];     
191    zl = pl[2];  
192 }
193                                   
194 //______________________________________________________________________________
195 void  AliMUONGeometryDetElement::Local2Global(
196                  Float_t xl, Float_t yl, Float_t zl, 
197                  Float_t& xg, Float_t& yg, Float_t& zg) const
198 {
199 /// Transforms point from the local reference frame of the detection element 
200 /// specified by detElemId to the global reference frame (ALIC).
201
202   Double_t dxl = xl;
203   Double_t dyl = yl;
204   Double_t dzl = zl;
205
206   Double_t dxg, dyg, dzg;
207   Local2Global(dxl, dyl, dzl, dxg, dyg, dzg);  
208   
209   xg = dxg;
210   yg = dyg;
211   zg = dzg;
212 }
213
214 //______________________________________________________________________________
215 void  AliMUONGeometryDetElement::Local2Global(
216                  Double_t xl, Double_t yl, Double_t zl, 
217                  Double_t& xg, Double_t& yg, Double_t& zg) const
218 {
219 /// Transforms point from the local reference frame of the detection element 
220 /// specified by detElemId to the global reference frame (ALIC).
221
222    // Check transformation
223    if (!fGlobalTransformation) {
224      AliError(Form("Global transformation for detection element %d not defined.",
225                     GetUniqueID()));
226      return;
227    }  
228    
229    // Transform point 
230    Double_t pl[3] = { xl, yl, zl };
231    Double_t pg[3] = { 0., 0., 0. };
232    fGlobalTransformation->LocalToMaster(pl, pg);
233    
234    // Return transformed point
235    xg = pg[0];
236    yg = pg[1];     
237    zg = pg[2];  
238 }
239
240 //______________________________________________________________________________
241 void AliMUONGeometryDetElement::SetLocalTransformation(
242                                                 const TGeoHMatrix& transform)
243
244 /// Sets global transformation;
245 /// gives warning if the global transformation is already defined.
246  
247   if (fLocalTransformation) {
248     delete fLocalTransformation;
249     AliWarning("Local transformation already defined was deleted.");
250   }  
251
252   fLocalTransformation = new TGeoHMatrix(transform);
253 }  
254                                               
255 //______________________________________________________________________________
256 void AliMUONGeometryDetElement::SetGlobalTransformation(
257                                                 const TGeoHMatrix& transform)
258
259 /// Sets global transformation;
260 /// gives warning if the global transformation is already defined.
261  
262   if (fGlobalTransformation) {
263     delete fGlobalTransformation;
264     AliWarning("Global transformation already defined was deleted.");
265   }  
266
267   fGlobalTransformation = new TGeoHMatrix(transform);
268 }  
269                                               
270 //______________________________________________________________________________
271 void AliMUONGeometryDetElement::PrintLocalTransform() const
272 {
273 /// Prints detection element relative transformation
274 /// (the transformation wrt module frame)
275
276   PrintTransform(fLocalTransformation);
277 }  
278
279 //______________________________________________________________________________
280 void AliMUONGeometryDetElement::PrintGlobalTransform() const
281 {
282 /// Prints detection element global transformation
283 /// (the transformation wrt global frame)
284
285   PrintTransform(fGlobalTransformation);
286 }  
287
288 //______________________________________________________________________________
289 TString AliMUONGeometryDetElement::GetVolumeName() const
290
291 /// Extract volume name from the path
292   
293   std::string volPath = fVolumePath.Data();
294   std::string::size_type first = volPath.rfind('/')+1;
295   std::string::size_type last = volPath.rfind('_');
296   
297   return volPath.substr(first, last-first );
298 }
299
300 //______________________________________________________________________________
301 Int_t AliMUONGeometryDetElement::GetVolumeCopyNo() const
302
303 /// Extract volume copyNo from the path
304   
305   string volPath = fVolumePath.Data();
306   std::string::size_type first = volPath.rfind('_');
307   std::string copyNoStr = volPath.substr(first+1, volPath.length());
308   std::istringstream in(copyNoStr);
309   Int_t copyNo;
310   in >> copyNo;
311   
312   return copyNo;
313 }
314