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