ROOT  6.06/09
Reference Guide
TRotMatrix.cxx
Go to the documentation of this file.
1 // @(#)root/g3d:$Id$
2 // Author: Rene Brun 14/09/95
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "TGeometry.h"
13 #include "TRotMatrix.h"
14 #include "TClass.h"
15 #include "TMath.h"
16 
17 ClassImp(TRotMatrix)
18 
19 /** \class TRotMatrix
20 \ingroup g3d
21 Manages a detector rotation matrix. See class TGeometry.
22 */
23 
24 ////////////////////////////////////////////////////////////////////////////////
25 /// RotMatrix default constructor.
26 
27 TRotMatrix::TRotMatrix()
28 {
29  for (int i=0;i<9;i++) fMatrix[i] = 0;
30  fNumber = 0;
31  fPhi = 0;
32  fPsi = 0;
33  fTheta = 0;
34  fType = 0;
35 }
36 
37 ////////////////////////////////////////////////////////////////////////////////
38 /// RotMatrix normal constructor.
39 
40 TRotMatrix::TRotMatrix(const char *name, const char *title, Double_t *matrix)
41  :TNamed(name,title)
42 {
43  if (!matrix) { Error("ctor","No rotation is supplied"); return; }
44 
45  fNumber = 0;
46  fPhi = 0;
47  fPsi = 0;
48  fTheta = 0;
49  fType = 0;
50 
51  SetMatrix(matrix);
52  if (!gGeometry) gGeometry = new TGeometry();
53  fNumber = gGeometry->GetListOfMatrices()->GetSize();
55 }
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 /// RotMatrix normal constructor.
59 
60 TRotMatrix::TRotMatrix(const char *name, const char *title, Double_t theta, Double_t phi, Double_t psi)
61  :TNamed(name,title)
62 {
63  printf("ERROR: This form of TRotMatrix constructor not implemented yet\n");
64 
65  Int_t i;
66  fTheta = theta;
67  fPhi = phi;
68  fPsi = psi;
69  fType = 2;
70  for (i=0;i<9;i++) fMatrix[i] = 0;
71  fMatrix[0] = 1; fMatrix[4] = 1; fMatrix[8] = 1;
72 
73  if (!gGeometry) gGeometry = new TGeometry();
74  fNumber = gGeometry->GetListOfMatrices()->GetSize();
76 }
77 
78 ////////////////////////////////////////////////////////////////////////////////
79 /// RotMatrix normal constructor defined a la GEANT.
80 ///
81 /// The TRotMatrix constructor with six angles uses the GEANT convention:
82 ///
83 /// theta1 is the polar angle of the x-prim axis in the main reference system
84 /// (MRS), theta2 and theta3 have the same meaning for the y-prim and z-prim
85 /// axis.
86 ///
87 /// Phi1 is the azimuthal angle of the x-prim in the MRS and phi2 and phi3
88 /// have the same meaning for y-prim and z-prim.
89 ///
90 ///
91 /// for example, the unit matrix is defined in the following way.
92 /// ~~~ {.cpp}
93 /// x-prim || x, y-prim || y, z-prim || z
94 ///
95 /// means: theta1=90, theta2=90, theta3=0, phi1=0, phi2=90, phi3=0
96 /// ~~~
97 
98 TRotMatrix::TRotMatrix(const char *name, const char *title, Double_t theta1, Double_t phi1
99  , Double_t theta2, Double_t phi2
100  , Double_t theta3, Double_t phi3)
101  :TNamed(name,title)
102 {
103  SetAngles(theta1,phi1,theta2,phi2,theta3,phi3);
104 
105  if (!gGeometry) gGeometry = new TGeometry();
106  fNumber = gGeometry->GetListOfMatrices()->GetSize();
107  gGeometry->GetListOfMatrices()->Add(this);
108 }
109 
110 ////////////////////////////////////////////////////////////////////////////////
111 /// RotMatrix default destructor.
112 
114 {
116 }
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 /// Returns the value of the determinant of this matrix
120 
122 {
123  return
124  fMatrix[0] * (fMatrix[4]*fMatrix[8] - fMatrix[7]*fMatrix[5])
125  - fMatrix[3] * (fMatrix[1]*fMatrix[8] - fMatrix[7]*fMatrix[2])
126  + fMatrix[6] * (fMatrix[1]*fMatrix[5] - fMatrix[4]*fMatrix[2]);
127 }
128 
129 ////////////////////////////////////////////////////////////////////////////////
130 /// Convert this matrix to the OpenGL [4x4]
131 ///
132 /// ~~~ {.cpp}
133 /// [ fMatrix[0] fMatrix[1] fMatrix[2] 0 ]
134 /// [ fMatrix[3] fMatrix[4] fMatrix[5] 0 ]
135 /// [ fMatrix[6] fMatrix[7] fMatrix[8] 0 ]
136 /// [ 0 0 0 1 ]
137 /// ~~~
138 ///
139 /// Input:
140 ///
141 /// Double_t *rGLMatrix: pointer to Double_t 4x4 buffer array
142 ///
143 /// Return:
144 ///
145 /// Double_t*: pointer to the input buffer
146 
148 {
149  Double_t *glmatrix = rGLMatrix;
150  const Double_t *matrix = fMatrix;
151  if (rGLMatrix)
152  {
153  for (Int_t i=0;i<3;i++) {
154  for (Int_t j=0;j<3;j++) memcpy(glmatrix,matrix,3*sizeof(Double_t));
155  matrix += 3;
156  glmatrix += 3;
157  *glmatrix = 0.0;
158  glmatrix++;
159  }
160  for (Int_t j=0;j<3;j++) {
161  *glmatrix = 0.0;
162  glmatrix++;
163  }
164  *glmatrix = 1.0;
165  }
166  return rGLMatrix;
167 }
168 
169 ////////////////////////////////////////////////////////////////////////////////
170 /// theta1 is the polar angle of the x-prim axis in the main reference system
171 /// (MRS), theta2 and theta3 have the same meaning for the y-prim and z-prim
172 /// axis.
173 ///
174 /// Phi1 is the azimuthal angle of the x-prim in the MRS and phi2 and phi3
175 /// have the same meaning for y-prim and z-prim.
176 ///
177 ///
178 /// for example, the unit matrix is defined in the following way.
179 ///
180 /// ~~~ {.cpp}
181 /// x-prim || x, y-prim || y, z-prim || z
182 ///
183 /// means: theta1=90, theta2=90, theta3=0, phi1=0, phi2=90, phi3=0
184 /// ~~~
185 
187  Double_t theta2, Double_t phi2,Double_t theta3, Double_t phi3)
188 {
189  const Double_t degrad = 0.0174532925199432958;
190 
191  fTheta = theta1;
192  fPhi = phi1;
193  fPsi = theta2;
194 
195  fType = 2;
196  if (!strcmp(GetName(),"Identity")) fType = 0;
197 
198  fMatrix[0] = TMath::Sin(theta1*degrad)*TMath::Cos(phi1*degrad);
199  fMatrix[1] = TMath::Sin(theta1*degrad)*TMath::Sin(phi1*degrad);
200  fMatrix[2] = TMath::Cos(theta1*degrad);
201  fMatrix[3] = TMath::Sin(theta2*degrad)*TMath::Cos(phi2*degrad);
202  fMatrix[4] = TMath::Sin(theta2*degrad)*TMath::Sin(phi2*degrad);
203  fMatrix[5] = TMath::Cos(theta2*degrad);
204  fMatrix[6] = TMath::Sin(theta3*degrad)*TMath::Cos(phi3*degrad);
205  fMatrix[7] = TMath::Sin(theta3*degrad)*TMath::Sin(phi3*degrad);
206  fMatrix[8] = TMath::Cos(theta3*degrad);
207 
208  SetReflection();
209  return fMatrix;
210 }
211 
212 ////////////////////////////////////////////////////////////////////////////////
213 /// copy predefined 3x3 matrix into TRotMatrix object
214 
215 void TRotMatrix::SetMatrix(const Double_t *matrix)
216 {
217  fTheta = 0;
218  fPhi = 0;
219  fPsi = 0;
220  fType = 0;
221  if (!matrix) return;
222  fType = 2;
223  memcpy(fMatrix,matrix,9*sizeof(Double_t));
224  SetReflection();
225 }
226 
227 ////////////////////////////////////////////////////////////////////////////////
228 /// Checks whether the determinant of this
229 /// matrix defines the reflection transformation
230 /// and set the "reflection" flag if any
231 
233 {
235  if (Determinant() < 0) { fType=1; SetBit(kReflection);}
236 }
237 
238 ////////////////////////////////////////////////////////////////////////////////
239 /// Stream an object of class TRotMatrix.
240 
241 void TRotMatrix::Streamer(TBuffer &R__b)
242 {
243  if (R__b.IsReading()) {
244  UInt_t R__s, R__c;
245  Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
246  if (R__v > 1) {
247  R__b.ReadClassBuffer(TRotMatrix::Class(), this, R__v, R__s, R__c);
248  return;
249  }
250  //====process old versions before automatic schema evolution
251  TNamed::Streamer(R__b);
252  R__b >> fNumber;
253  R__b >> fType;
254  R__b >> fTheta;
255  R__b >> fPhi;
256  R__b >> fPsi;
257  R__b.ReadStaticArray(fMatrix);
258  R__b.CheckByteCount(R__s, R__c, TRotMatrix::IsA());
259  //====end of old versions
260 
261  } else {
262  R__b.WriteClassBuffer(TRotMatrix::Class(),this);
263  }
264 }
THashList * GetListOfMatrices() const
Definition: TGeometry.h:82
Double_t fTheta
Definition: TRotMatrix.h:37
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
Bool_t IsReading() const
Definition: TBuffer.h:81
short Version_t
Definition: RtypesCore.h:61
Double_t fPsi
Definition: TRotMatrix.h:39
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)=0
Double_t fPhi
Definition: TRotMatrix.h:38
Int_t fNumber
Definition: TRotMatrix.h:35
int Int_t
Definition: RtypesCore.h:41
virtual void SetMatrix(const Double_t *matrix)
copy predefined 3x3 matrix into TRotMatrix object
Definition: TRotMatrix.cxx:215
virtual Int_t ReadStaticArray(Bool_t *b)=0
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:732
virtual Double_t * GetGLMatrix(Double_t *rGLMatrix) const
Convert this matrix to the OpenGL [4x4].
Definition: TRotMatrix.cxx:147
void Class()
Definition: Class.C:29
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
virtual void SetReflection()
Checks whether the determinant of this matrix defines the reflection transformation and set the "refl...
Definition: TRotMatrix.cxx:232
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
TGeometry description.
Definition: TGeometry.h:43
virtual Double_t Determinant() const
Returns the value of the determinant of this matrix.
Definition: TRotMatrix.cxx:121
TObject * Remove(TObject *obj)
Remove object from the list.
Definition: THashList.cxx:284
TClass * IsA() const
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
virtual const Double_t * SetAngles(Double_t theta1, Double_t phi1, Double_t theta2, Double_t phi2, Double_t theta3, Double_t phi3)
theta1 is the polar angle of the x-prim axis in the main reference system (MRS), theta2 and theta3 ha...
Definition: TRotMatrix.cxx:186
virtual ~TRotMatrix()
RotMatrix default destructor.
Definition: TRotMatrix.cxx:113
Double_t Cos(Double_t)
Definition: TMath.h:424
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
Int_t fType
Definition: TRotMatrix.h:36
virtual Int_t GetSize() const
Definition: TCollection.h:95
#define ClassImp(name)
Definition: Rtypes.h:279
double Double_t
Definition: RtypesCore.h:55
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
#define name(a, b)
Definition: linkTestLib0.cpp:5
virtual void Add(TObject *obj)
Definition: TList.h:81
Double_t Sin(Double_t)
Definition: TMath.h:421
void ResetBit(UInt_t f)
Definition: TObject.h:172
TGMatrixLayout * fMatrix
R__EXTERN TGeometry * gGeometry
Definition: TGeometry.h:162
Double_t fMatrix[9]
Definition: TRotMatrix.h:40
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0