1 | // |
---|
2 | // ******************************************************************** |
---|
3 | // * License and Disclaimer * |
---|
4 | // * * |
---|
5 | // * The Geant4 software is copyright of the Copyright Holders of * |
---|
6 | // * the Geant4 Collaboration. It is provided under the terms and * |
---|
7 | // * conditions of the Geant4 Software License, included in the file * |
---|
8 | // * LICENSE and available at http://cern.ch/geant4/license . These * |
---|
9 | // * include a list of copyright holders. * |
---|
10 | // * * |
---|
11 | // * Neither the authors of this software system, nor their employing * |
---|
12 | // * institutes,nor the agencies providing financial support for this * |
---|
13 | // * work make any representation or warranty, express or implied, * |
---|
14 | // * regarding this software system or assume any liability for its * |
---|
15 | // * use. Please see the license in the file LICENSE and URL above * |
---|
16 | // * for the full disclaimer and the limitation of liability. * |
---|
17 | // * * |
---|
18 | // * This code implementation is the result of the scientific and * |
---|
19 | // * technical work of the GEANT4 collaboration. * |
---|
20 | // * By using, copying, modifying or distributing the software (or * |
---|
21 | // * any work based on the software) you agree to acknowledge its * |
---|
22 | // * use in resulting scientific publications, and indicate your * |
---|
23 | // * acceptance of all terms of the Geant4 Software license. * |
---|
24 | // ******************************************************************** |
---|
25 | // |
---|
26 | /////////////////////////////////////////////////////////////////////////////// |
---|
27 | // File: CCalRotationMatrixFactory.cc |
---|
28 | // Description: CCalRotationFactory is a factory class to define all rotation |
---|
29 | // matrices used in geometry building |
---|
30 | /////////////////////////////////////////////////////////////////////////////// |
---|
31 | #include "CCalRotationMatrixFactory.hh" |
---|
32 | |
---|
33 | #include "CCalutils.hh" |
---|
34 | |
---|
35 | #include <fstream> |
---|
36 | #include <stdlib.h> |
---|
37 | |
---|
38 | //#define debug |
---|
39 | //#define ddebug |
---|
40 | |
---|
41 | CCalRotationMatrixFactory * CCalRotationMatrixFactory::instance = 0; |
---|
42 | G4String CCalRotationMatrixFactory::file=""; |
---|
43 | |
---|
44 | CCalRotationMatrixFactory* CCalRotationMatrixFactory::getInstance(const G4String & rotfile){ |
---|
45 | if (rotfile=="" || rotfile==file) |
---|
46 | return getInstance(); |
---|
47 | else if (file="") { |
---|
48 | file=rotfile; |
---|
49 | return getInstance(); |
---|
50 | } else { |
---|
51 | G4cerr << "ERROR: Trying to get Rotation Matrices from " << rotfile |
---|
52 | << " when previously were retrieved from " << file <<"." << G4endl; |
---|
53 | return 0; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | CCalRotationMatrixFactory* CCalRotationMatrixFactory::getInstance(){ |
---|
59 | if (file=="") { |
---|
60 | G4cerr << "ERROR: You haven't defined which file to use for materials in " |
---|
61 | << "CCalRotationMatrixFactory::getInstance(G4String)" << G4endl; |
---|
62 | return 0; |
---|
63 | } |
---|
64 | |
---|
65 | if (instance==0) { |
---|
66 | instance = new CCalRotationMatrixFactory; |
---|
67 | return instance; |
---|
68 | } |
---|
69 | else |
---|
70 | return instance; |
---|
71 | } |
---|
72 | |
---|
73 | void CCalRotationMatrixFactory::setFileName(const G4String& rotfile) { |
---|
74 | if (rotfile!=file && file!="") { |
---|
75 | G4cerr << "ERROR: Trying to change Rotation Matrices file name to " |
---|
76 | << rotfile << "." << G4endl; |
---|
77 | G4cerr << " Using previous file: " << file << G4endl; |
---|
78 | } |
---|
79 | file=rotfile; |
---|
80 | } |
---|
81 | |
---|
82 | CCalRotationMatrixFactory::~CCalRotationMatrixFactory(){ |
---|
83 | G4RotationMatrixTableIterator i; |
---|
84 | for(i=theMatrices.begin(); i != theMatrices.end(); ++i) { |
---|
85 | delete (*i).second; |
---|
86 | }; |
---|
87 | theMatrices.clear(); |
---|
88 | } |
---|
89 | |
---|
90 | G4RotationMatrix* CCalRotationMatrixFactory::findMatrix(const G4String & rot) { |
---|
91 | G4RotationMatrix* retrot=0; |
---|
92 | //Rotation :NULL is no rotation so a null pointer is returned |
---|
93 | if (rot != ":NULL") { |
---|
94 | //retrot untouched if rot not found!!! |
---|
95 | G4RotationMatrixTableIterator it = theMatrices.find(rot); |
---|
96 | if (it != theMatrices.end()) |
---|
97 | retrot = (*it).second; |
---|
98 | } |
---|
99 | |
---|
100 | return retrot; //!!!Maybe a treatment on not-found case needed. |
---|
101 | } |
---|
102 | |
---|
103 | G4RotationMatrix* CCalRotationMatrixFactory::AddMatrix(const G4String& name, |
---|
104 | G4double th1, |
---|
105 | G4double phi1, |
---|
106 | G4double th2, |
---|
107 | G4double phi2, |
---|
108 | G4double th3, |
---|
109 | G4double phi3){ |
---|
110 | G4double sinth1, sinth2, sinth3, costh1, costh2, costh3; |
---|
111 | G4double sinph1, sinph2, sinph3, cosph1, cosph2, cosph3; |
---|
112 | G4double TH1 = th1/deg, TH2 = th2/deg, TH3 = th3/deg; |
---|
113 | G4double PH1 = phi1/deg, PH2 = phi2/deg, PH3 = phi3/deg; |
---|
114 | |
---|
115 | if (TH1 == 0.0 || TH1 == 360) { |
---|
116 | sinth1 = 0.0; costh1 = 1.0; |
---|
117 | } else if (TH1 == 90.0 || TH1 == -270) { |
---|
118 | sinth1 = 1.0; costh1 = 0.0; |
---|
119 | } else if (TH1 == 180.0 || TH1 == -180.0) { |
---|
120 | sinth1 = 0.0; costh1 = -1.0; |
---|
121 | } else if (TH1 == 270.0 || TH1 == -90.0) { |
---|
122 | sinth1 = -1.0; costh1 = 0.0; |
---|
123 | } else { |
---|
124 | sinth1 = std::sin(th1); costh1 = std::cos(th1); |
---|
125 | } |
---|
126 | |
---|
127 | if (TH2 == 0.0 || TH2 == 360) { |
---|
128 | sinth2 = 0.0; costh2 = 1.0; |
---|
129 | } else if (TH2 == 90.0 || TH2 == -270) { |
---|
130 | sinth2 = 1.0; costh2 = 0.0; |
---|
131 | } else if (TH2 == 180.0 || TH2 == -180.0) { |
---|
132 | sinth2 = 0.0; costh2 = -1.0; |
---|
133 | } else if (TH2 == 270.0 || TH2 == -90.0) { |
---|
134 | sinth2 = -1.0; costh2 = 0.0; |
---|
135 | } else { |
---|
136 | sinth2 = std::sin(th2); costh2 = std::cos(th2); |
---|
137 | } |
---|
138 | |
---|
139 | if (TH3 == 0.0 || TH3 == 360) { |
---|
140 | sinth3 = 0.0; costh3 = 1.0; |
---|
141 | } else if (TH3 == 90.0 || TH2 == -270) { |
---|
142 | sinth3 = 1.0; costh3 = 0.0; |
---|
143 | } else if (TH3 == 180.0 || TH3 == -180.0) { |
---|
144 | sinth3 = 0.0; costh3 = -1.0; |
---|
145 | } else if (TH3 == 270.0 || TH3 == -90.0) { |
---|
146 | sinth3 = -1.0; costh3 = 0.0; |
---|
147 | } else { |
---|
148 | sinth3 = std::sin(th3); costh3 = std::cos(th3); |
---|
149 | } |
---|
150 | |
---|
151 | if (PH1 == 0.0 || PH1 == 360) { |
---|
152 | sinph1 = 0.0; cosph1 = 1.0; |
---|
153 | } else if (PH1 == 90.0 || PH1 == -270) { |
---|
154 | sinph1 = 1.0; cosph1 = 0.0; |
---|
155 | } else if (PH1 == 180.0 || PH1 == -180.0) { |
---|
156 | sinph1 = 0.0; cosph1 = -1.0; |
---|
157 | } else if (PH1 == 270.0 || PH1 == -90.0) { |
---|
158 | sinph1 = -1.0; cosph1 = 0.0; |
---|
159 | } else { |
---|
160 | sinph1 = std::sin(phi1); cosph1 = std::cos(phi1); |
---|
161 | } |
---|
162 | |
---|
163 | if (PH2 == 0.0 || PH2 == 360) { |
---|
164 | sinph2 = 0.0; cosph2 = 1.0; |
---|
165 | } else if (PH2 == 90.0 || PH2 == -270) { |
---|
166 | sinph2 = 1.0; cosph2 = 0.0; |
---|
167 | } else if (PH2 == 180.0 || PH2 == -180.0) { |
---|
168 | sinph2 = 0.0; cosph2 = -1.0; |
---|
169 | } else if (PH2 == 270.0 || PH2 == -90.0) { |
---|
170 | sinph2 = -1.0; cosph2 = 0.0; |
---|
171 | } else { |
---|
172 | sinph2 = std::sin(phi2); cosph2 = std::cos(phi2); |
---|
173 | } |
---|
174 | |
---|
175 | if (PH3 == 0.0 || PH3 == 360) { |
---|
176 | sinph3 = 0.0; cosph3 = 1.0; |
---|
177 | } else if (PH3 == 90.0 || PH3 == -270) { |
---|
178 | sinph3 = 1.0; cosph3 = 0.0; |
---|
179 | } else if (PH3 == 180.0 || PH3 == -180.0) { |
---|
180 | sinph3 = 0.0; cosph3 = -1.0; |
---|
181 | } else if (PH3 == 270.0 || PH3 == -90.0) { |
---|
182 | sinph3 = -1.0; cosph3 = 0.0; |
---|
183 | } else { |
---|
184 | sinph3 = std::sin(phi3); cosph3 = std::cos(phi3); |
---|
185 | } |
---|
186 | |
---|
187 | //xprime axis coordinates |
---|
188 | CLHEP::Hep3Vector xprime(sinth1*cosph1,sinth1*sinph1,costh1); |
---|
189 | //yprime axis coordinates |
---|
190 | CLHEP::Hep3Vector yprime(sinth2*cosph2,sinth2*sinph2,costh2); |
---|
191 | //zprime axis coordinates |
---|
192 | CLHEP::Hep3Vector zprime(sinth3*cosph3,sinth3*sinph3,costh3); |
---|
193 | |
---|
194 | #ifdef ddebug |
---|
195 | G4cout << xprime << '\t'; G4cout << yprime << '\t'; G4cout << zprime << G4endl; |
---|
196 | #endif |
---|
197 | G4RotationMatrix *rotMat = new G4RotationMatrix(); |
---|
198 | rotMat->rotateAxes(xprime, yprime, zprime); |
---|
199 | if (*rotMat == G4RotationMatrix()) { |
---|
200 | // G4cerr << "WARNING: Matrix " << name << " will not be created as a rotation matrix." |
---|
201 | // G4cerr << "WARNING: Matrix " << name << " is = identity matrix. It will not be created as a rotation matrix." << G4endl; |
---|
202 | delete rotMat; |
---|
203 | rotMat=0; |
---|
204 | } else { |
---|
205 | rotMat->invert(); |
---|
206 | theMatrices[name]=rotMat; |
---|
207 | #ifdef ddebug |
---|
208 | G4cout << *rotMat << G4endl; |
---|
209 | #endif |
---|
210 | } |
---|
211 | |
---|
212 | return rotMat; |
---|
213 | } |
---|
214 | |
---|
215 | CCalRotationMatrixFactory::CCalRotationMatrixFactory():theMatrices(){ |
---|
216 | |
---|
217 | G4String path = getenv("CCAL_GLOBALPATH"); |
---|
218 | G4cout << " ==> Opening file " << file << "..." << G4endl; |
---|
219 | std::ifstream is; |
---|
220 | bool ok = openGeomFile(is, path, file); |
---|
221 | if (!ok) { |
---|
222 | G4cerr << "ERROR: Could not open file " << file << " ... Exiting!" << G4endl; |
---|
223 | exit(-1); |
---|
224 | } |
---|
225 | |
---|
226 | ////////////////////////////////////////////////// |
---|
227 | // Find *DO ROTM |
---|
228 | findDO(is, G4String("ROTM")); |
---|
229 | |
---|
230 | char rubish[256]; |
---|
231 | G4String name; |
---|
232 | |
---|
233 | #ifdef debug |
---|
234 | G4cout << " ==> Reading Rotation Matrices... " << G4endl; |
---|
235 | G4cout << " Name\tTheta1\tPhi1\tTheta2\tPhi2\tTheta3\tPhi3"<< G4endl; |
---|
236 | #endif |
---|
237 | |
---|
238 | is >> name; |
---|
239 | while(name!="*ENDDO") { |
---|
240 | if (name.index("#.")==0) { //It is a comment.Skip line. |
---|
241 | is.getline(rubish,256,'\n'); |
---|
242 | } else { |
---|
243 | #ifdef debug |
---|
244 | G4cout << " " << name <<'\t'; |
---|
245 | #endif |
---|
246 | G4double th1, phi1, th2, phi2, th3, phi3; |
---|
247 | //Get xprime axis angles |
---|
248 | is >> th1 >> phi1; |
---|
249 | #ifdef debug |
---|
250 | G4cout << th1 << '\t' << phi1 << '\t'; |
---|
251 | #endif |
---|
252 | //Get yprime axis angles |
---|
253 | is >> th2 >> phi2; |
---|
254 | #ifdef debug |
---|
255 | G4cout << th2 << '\t' << phi2 << '\t'; |
---|
256 | #endif |
---|
257 | //Get zprime axis angles |
---|
258 | is >> th3 >> phi3; |
---|
259 | #ifdef debug |
---|
260 | G4cout << th3 << '\t' << phi3 << '\t'; |
---|
261 | #endif |
---|
262 | |
---|
263 | is.getline(rubish,256,'\n'); |
---|
264 | #ifdef debug |
---|
265 | G4cout << rubish << G4endl; |
---|
266 | #endif |
---|
267 | |
---|
268 | AddMatrix(name, th1*deg, phi1*deg, th2*deg, phi2*deg, th3*deg, phi3*deg); |
---|
269 | } |
---|
270 | |
---|
271 | is >> name; |
---|
272 | }; |
---|
273 | |
---|
274 | is.close(); |
---|
275 | G4cout << " " << theMatrices.size() << " rotation matrices read in." << G4endl; |
---|
276 | } |
---|
277 | |
---|
278 | |
---|
279 | // 29-Jan-2004 A.R. : commented to avoid clashes with CLHEP. |
---|
280 | // Streaming operators for rotation matrices are |
---|
281 | // already defined in CLHEP::HepRotation. |
---|
282 | // std::ostream& operator<<(std::ostream& os , const G4RotationMatrix & rot){ |
---|
283 | // // os << "( " << rot.xx() << tab << rot.xy() << tab << rot.xz() << " )" << G4endl; |
---|
284 | // // os << "( " << rot.yx() << tab << rot.yy() << tab << rot.yz() << " )" << G4endl; |
---|
285 | // // os << "( " << rot.zx() << tab << rot.zy() << tab << rot.zz() << " )" << G4endl; |
---|
286 | // |
---|
287 | // os << "[" |
---|
288 | // << rot.thetaX()/deg << tab << rot.phiX()/deg << tab |
---|
289 | // << rot.thetaY()/deg << tab << rot.phiY()/deg << tab |
---|
290 | // << rot.thetaZ()/deg << tab << rot.phiZ()/deg << "]" |
---|
291 | // << G4endl; |
---|
292 | // |
---|
293 | // return os; |
---|
294 | // } |
---|