Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // G4tgrUtils implementation << 27 // 26 // 28 // Author: P.Arce, CIEMAT (November 2007) << 27 // 29 // ------------------------------------------- << 28 // class G4tgrUtils >> 29 >> 30 // History: >> 31 // - Created. P.Arce, CIEMAT (November 2007) >> 32 // ------------------------------------------------------------------------- >> 33 >> 34 #include "geomdefs.hh" 30 35 31 #include <iomanip> 36 #include <iomanip> 32 #include <set> 37 #include <set> 33 38 34 #include "G4tgrUtils.hh" 39 #include "G4tgrUtils.hh" 35 << 36 #include "geomdefs.hh" << 37 #include "G4PhysicalConstants.hh" << 38 #include "G4tgrParameterMgr.hh" 40 #include "G4tgrParameterMgr.hh" 39 #include "G4tgrMessenger.hh" 41 #include "G4tgrMessenger.hh" 40 #include "G4UnitsTable.hh" 42 #include "G4UnitsTable.hh" 41 #include "G4GeometryTolerance.hh" 43 #include "G4GeometryTolerance.hh" 42 #include "G4UIcommand.hh" 44 #include "G4UIcommand.hh" 43 45 44 G4ThreadLocal G4tgrEvaluator* G4tgrUtils::theE << 45 46 46 // ------------------------------------------- << 47 G4tgrEvaluator* G4tgrUtils::theEvaluator = new G4tgrEvaluator; >> 48 >> 49 >> 50 //------------------------------------------------------------- 47 G4tgrUtils::G4tgrUtils() 51 G4tgrUtils::G4tgrUtils() 48 { 52 { 49 if(theEvaluator == nullptr) << 50 { << 51 theEvaluator = new G4tgrEvaluator; << 52 } << 53 } 53 } 54 54 55 // ------------------------------------------- << 55 >> 56 //------------------------------------------------------------- 56 G4tgrUtils::~G4tgrUtils() 57 G4tgrUtils::~G4tgrUtils() 57 { 58 { 58 delete theEvaluator; << 59 theEvaluator = nullptr; << 60 } 59 } 61 60 62 // ------------------------------------------- << 61 63 G4bool G4tgrUtils::IsSeparator(const char ch) << 62 //------------------------------------------------------------- >> 63 G4bool G4tgrUtils::IsSeparator( const char ch) 64 { 64 { 65 char nonCharacters[7] = { "()+-*/" }; << 65 char nonCharacters[7] = {"()+-*/"}; 66 for(std::size_t ii = 0; ii < 6; ++ii) << 66 for( size_t ii = 0; ii < 6; ii++ ) 67 { 67 { 68 if(ch == nonCharacters[ii]) << 68 if( ch == nonCharacters[ii] ) 69 { 69 { 70 return true; 70 return true; 71 } 71 } 72 } 72 } 73 return false; 73 return false; 74 } 74 } 75 75 76 // ------------------------------------------- << 76 77 G4bool G4tgrUtils::IsNumber(const G4String& st << 77 //------------------------------------------------------------- >> 78 G4bool G4tgrUtils::IsNumber( const G4String& str) 78 { 79 { 79 G4int isnum = 1; 80 G4int isnum = 1; 80 G4int numE = 0; << 81 G4int numE = 0; 81 for(G4int ii = 0; ii < (G4int)str.length(); << 82 for(size_t ii=0; ii<str.length(); ii++) 82 { 83 { 83 if(!isdigit(str[ii]) && (str[ii] != '.') & << 84 if(!isdigit(str[ii]) && (str[ii]!='.') && (str[ii]!='-') && (str[ii]!='+')) 84 (str[ii] != '+')) << 85 { 85 { 86 //--- check for E(xponential) 86 //--- check for E(xponential) 87 if(str[ii] == 'E' || str[ii] == 'e') << 87 if(str[ii] == 'E' || str[ii] == 'e' ) 88 { 88 { 89 if(ii == 0) << 89 if( ii == 0 ) { return 0; } 90 { << 90 if(numE != 0 || ii == str.length()-1) 91 return 0; << 92 } << 93 if(numE != 0 || ii == G4int(str.length << 94 { 91 { 95 isnum = 0; 92 isnum = 0; 96 break; 93 break; 97 } 94 } 98 numE++; 95 numE++; 99 } 96 } 100 else 97 else 101 { 98 { 102 isnum = 0; << 99 isnum = 0; 103 break; 100 break; 104 } 101 } 105 } 102 } 106 } 103 } 107 return isnum; 104 return isnum; 108 } 105 } 109 106 110 // ------------------------------------------- << 107 111 G4bool G4tgrUtils::IsInteger(const G4double va << 108 //------------------------------------------------------------- >> 109 G4bool G4tgrUtils::IsInteger( const G4double val, const G4double precision ) 112 { 110 { 113 if(G4int(val) / val - 1 > precision) << 111 if( G4int(val) / val - 1 > precision ) 114 { << 112 { 115 return false; << 113 return 0; 116 } 114 } 117 else 115 else 118 { << 116 { 119 return true; << 117 return 1; 120 } 118 } 121 } 119 } 122 120 123 // ------------------------------------------- << 121 124 void G4tgrUtils::Dump3v(const G4ThreeVector& v << 122 //------------------------------------------------------------- >> 123 void G4tgrUtils::Dump3v( const G4ThreeVector& vec, const char* msg) 125 { 124 { 126 G4cout << msg << std::setprecision(8) << vec << 125 G4cout << msg << std::setprecision(8) 127 << G4endl; << 126 << vec << std::setprecision(6) << G4endl; 128 } 127 } 129 128 130 // ------------------------------------------- << 129 131 void G4tgrUtils::Dumprm(const G4RotationMatrix << 130 //------------------------------------------------------------- >> 131 void G4tgrUtils::Dumprm( const G4RotationMatrix& rm, const char* msg) 132 { 132 { 133 G4cout << msg << G4endl << " xx=" << rm.xx() << 133 G4cout << msg << G4endl 134 << " zx=" << rm.zx() << G4endl << " x << 134 << " xx=" << rm.xx() << " yx=" << rm.yx() << " zx=" << rm.zx() << G4endl 135 << " yy=" << rm.yy() << " zy=" << rm. << 135 << " xy=" << rm.xy() << " yy=" << rm.yy() << " zy=" << rm.zy() << G4endl 136 << " xz=" << rm.xz() << " yz=" << rm. << 136 << " xz=" << rm.xz() << " yz=" << rm.yz() << " zz=" << rm.zz() << G4endl; 137 << G4endl; << 138 } 137 } 139 138 140 // ------------------------------------------- << 139 141 void G4tgrUtils::DumpVS(const std::vector<G4St << 140 //------------------------------------------------------------- 142 std::ostream& outs) << 141 void G4tgrUtils::DumpVS( const std::vector<G4String>& wl, >> 142 const char* msg, std::ostream& outs ) 143 { 143 { 144 outs << msg << G4endl; 144 outs << msg << G4endl; 145 for(auto ite = wl.cbegin(); ite != wl.cend() << 145 std::vector<G4String>::const_iterator ite; >> 146 for( ite = wl.begin(); ite != wl.end(); ite++ ) 146 { 147 { 147 outs << *ite << " "; 148 outs << *ite << " "; 148 } 149 } 149 outs << G4endl; 150 outs << G4endl; 150 } 151 } 151 152 152 // ------------------------------------------- << 153 153 void G4tgrUtils::DumpVS(const std::vector<G4St << 154 //------------------------------------------------------------- >> 155 void G4tgrUtils::DumpVS( const std::vector<G4String>& wl , const char* msg) 154 { 156 { 155 DumpVS(wl, msg, G4cout); << 157 DumpVS( wl, msg, G4cout); 156 } 158 } 157 159 158 // ------------------------------------------- << 160 159 G4String G4tgrUtils::SubColon(const G4String& << 161 //------------------------------------------------------------- >> 162 G4String G4tgrUtils::SubColon( const G4String& str ) 160 { 163 { 161 if(str.find(':') != 0) << 164 if( str.find(':') != 0 ) 162 { 165 { 163 G4String ErrMessage = "Trying to subtract << 166 G4String ErrMessage = "Trying to subtract leading colon from a word\n" 164 G4String("that has n << 167 + G4String("that has no leading colon: ") + str; 165 G4Exception("G4tgrUtils::SubColon()", "Par << 168 G4Exception("G4tgrUtils::SubColon()", "ParseError", 166 ErrMessage); << 169 FatalException, ErrMessage); 167 } 170 } 168 G4String strt = str.substr(1, str.size() - 1 << 171 G4String strt = str.substr(1,str.size()-1); 169 return strt; 172 return strt; 170 } 173 } 171 174 172 // ------------------------------------------- << 175 173 G4String G4tgrUtils::GetString(const G4String& << 176 //------------------------------------------------------------- >> 177 G4String G4tgrUtils::GetString( const G4String& str ) 174 { 178 { 175 //----------- first check if it is parameter 179 //----------- first check if it is parameter 176 const char* cstr = str.c_str(); 180 const char* cstr = str.c_str(); 177 if(cstr[0] == '$') << 181 if( cstr[0] == '$' ) 178 { 182 { 179 #ifdef G4VERBOSE 183 #ifdef G4VERBOSE 180 if(G4tgrMessenger::GetVerboseLevel() >= 3) << 184 if( G4tgrMessenger::GetVerboseLevel() >= 3 ) 181 { 185 { 182 G4cout << " G4tgrUtils::GetString() - Su 186 G4cout << " G4tgrUtils::GetString() - Substitute parameter: " 183 << G4tgrParameterMgr::GetInstance << 187 << G4tgrParameterMgr::GetInstance() 184 str.substr(1, str.size())) << 188 ->FindParameter( str.substr(1,str.size())) << G4endl; 185 << G4endl; << 186 } 189 } 187 #endif 190 #endif 188 return G4tgrParameterMgr::GetInstance()->F << 191 return G4tgrParameterMgr::GetInstance() 189 str.substr(1, str.size())); << 192 ->FindParameter( str.substr(1,str.size()) ); 190 } 193 } 191 else 194 else 192 { 195 { 193 return str; 196 return str; 194 } 197 } 195 } 198 } 196 199 197 // ------------------------------------------- << 200 198 G4double G4tgrUtils::GetDouble(const G4String& << 201 //------------------------------------------------------------- >> 202 G4double G4tgrUtils::GetDouble( const G4String& str, G4double unitval ) 199 { 203 { 200 if(!theEvaluator) << 201 { << 202 theEvaluator = new G4tgrEvaluator; << 203 } << 204 #ifdef G4VERBOSE 204 #ifdef G4VERBOSE 205 if(G4tgrMessenger::GetVerboseLevel() >= 3) << 205 if( G4tgrMessenger::GetVerboseLevel() >= 3 ) 206 { 206 { 207 G4cout << "G4tgrUtils::GetDouble() - Proce << 207 G4cout << "G4tgrUtils::GetDouble() - Processing: " 208 << " default unit " << unitval << G << 208 << str << " default unit " << unitval << G4endl; 209 } 209 } 210 #endif 210 #endif 211 if(str == "DBL_MAX") << 211 if( str == "DBL_MAX" ) { 212 { << 213 return DBL_MAX; 212 return DBL_MAX; 214 } << 213 }else if( str == "DBL_MIN" ) { 215 else if(str == "DBL_MIN") << 216 { << 217 return DBL_MIN; 214 return DBL_MIN; 218 } << 215 }else if( str == "FLT_MAX" ) { 219 else if(str == "FLT_MAX") << 220 { << 221 return FLT_MAX; 216 return FLT_MAX; 222 } << 217 }else if( str == "FLT_MIN" ) { 223 else if(str == "FLT_MIN") << 224 { << 225 return FLT_MIN; 218 return FLT_MIN; 226 } << 219 }else if( str == "INT_MAX" ) { 227 else if(str == "INT_MAX") << 228 { << 229 return INT_MAX; 220 return INT_MAX; 230 } << 221 }else if( str == "INT_MIN" ) { 231 else if(str == "INT_MIN") << 232 { << 233 return INT_MIN; 222 return INT_MIN; 234 } 223 } 235 //----- Look for arithmetic symbols, (, ) 224 //----- Look for arithmetic symbols, (, ) 236 const char* cstr = str.c_str(); 225 const char* cstr = str.c_str(); 237 std::set<G4int> separators; 226 std::set<G4int> separators; 238 separators.insert(-1); 227 separators.insert(-1); 239 G4int strlen = G4int(str.length()); 228 G4int strlen = G4int(str.length()); 240 for(G4int ii = 0; ii < strlen; ++ii) << 229 for(G4int ii=0; ii<strlen; ii++) 241 { 230 { 242 char cs = cstr[ii]; 231 char cs = cstr[ii]; 243 if(cs == '*' || cs == '/' || cs == '(' || << 232 if( cs == '*' || cs == '/' || cs == '(' || cs == ')' ) 244 { << 233 { 245 separators.insert(ii); 234 separators.insert(ii); 246 } 235 } 247 else if(cs == '+' || cs == '-') << 236 else if( cs == '+' || cs == '-' ) 248 { 237 { 249 // Check if it is not an exponential 238 // Check if it is not an exponential 250 // 239 // 251 if((ii < 2) || ((cstr[ii - 1] != 'E') && << 240 if( (ii < 2) 252 !IsNumber(G4String(1,cstr[ii - 2]))) << 241 || ( (cstr[ii-1] != 'E') && (cstr[ii-1] != 'e') ) 253 { << 242 || !IsNumber(cstr[ii-2]) ) >> 243 { 254 separators.insert(ii); 244 separators.insert(ii); 255 } << 245 } 256 } << 246 } 257 } 247 } 258 separators.insert(strlen); 248 separators.insert(strlen); 259 std::string strnew; // build a new word wit << 249 std::string strnew; // build a new word with Parameters 260 // and units substitute << 250 // and units substituted by values 261 //----- Process words, defined as characters 251 //----- Process words, defined as characters between two separators 262 G4int nUnits = 0; 252 G4int nUnits = 0; 263 std::set<G4int>::const_iterator site, site2; 253 std::set<G4int>::const_iterator site, site2; 264 site = separators.cbegin(); << 254 site = separators.begin(); 265 site2 = site; << 255 site2 = site; site2++; 266 ++site2; << 256 for( ; site2 != separators.end(); site++,site2++) 267 for(; site2 != separators.cend(); ++site, ++ << 268 { 257 { 269 #ifdef G4VERBOSE 258 #ifdef G4VERBOSE 270 if(G4tgrMessenger::GetVerboseLevel() >= 3) << 259 if( G4tgrMessenger::GetVerboseLevel() >= 3 ) 271 { 260 { 272 G4cout << " Loop to find word between << 261 G4cout << " Loop to find word between " << *site 273 << G4endl; << 262 << " " << *site2 << G4endl; 274 } 263 } 275 #endif 264 #endif 276 265 277 if(*site != -1) << 266 if( *site != -1 ) { strnew += str.substr(*site,1); } 278 { << 279 strnew += str.substr(*site, 1); << 280 } << 281 267 282 G4int wlen = (*site2) - (*site) - 1; // d << 268 G4int wlen = (*site2)-(*site)-1; //do not count contiguous separators 283 std::string word; 269 std::string word; 284 if(wlen != 0) 270 if(wlen != 0) 285 { 271 { 286 word = str.substr((*site) + 1, (*site2) << 272 word = str.substr((*site)+1,(*site2)-(*site)-1); 287 } 273 } 288 else 274 else 289 { 275 { 290 //--- Check combination of separators 276 //--- Check combination of separators 291 //--- Check number of parentheses 277 //--- Check number of parentheses 292 continue; 278 continue; 293 } 279 } 294 << 280 295 #ifdef G4VERBOSE 281 #ifdef G4VERBOSE 296 if(G4tgrMessenger::GetVerboseLevel() >= 3) << 282 if( G4tgrMessenger::GetVerboseLevel() >= 3 ) 297 { 283 { 298 G4cout << " Processing word: " << word 284 G4cout << " Processing word: " << word << G4endl; 299 } 285 } 300 #endif 286 #endif 301 //----------- first check if it is paramet 287 //----------- first check if it is parameter 302 const char* cword = word.c_str(); 288 const char* cword = word.c_str(); 303 if(cword[0] == '$') << 289 if( cword[0] == '$' ) 304 { 290 { 305 G4String parstr = G4tgrParameterMgr::Get << 291 G4String parstr = G4tgrParameterMgr::GetInstance() 306 word.substr(1, word.size())); << 292 ->FindParameter( word.substr(1,word.size())); 307 if(parstr.substr(0, 1) == "-") << 293 if( parstr.substr(0,1) == "-" ) 308 { 294 { 309 strnew += "("; 295 strnew += "("; 310 } 296 } 311 strnew += parstr; 297 strnew += parstr; 312 if(parstr.substr(0, 1) == "-") << 298 if( parstr.substr(0,1) == "-" ) 313 { 299 { 314 strnew += ")"; 300 strnew += ")"; 315 } 301 } 316 #ifdef G4VERBOSE 302 #ifdef G4VERBOSE 317 if(G4tgrMessenger::GetVerboseLevel() >= << 303 if( G4tgrMessenger::GetVerboseLevel() >= 3 ) 318 { 304 { 319 G4cout << " G4tgrutils::GetDouble() - << 305 G4cout << " G4tgrutils::GetDouble() - Param found: " 320 << " in string " << str << " , << 306 << word << " in string " << str 321 << G4endl; << 307 << " , substituted by " << parstr << G4endl; 322 } 308 } 323 #endif 309 #endif 324 } 310 } 325 else 311 else 326 { 312 { 327 //----- Get if it is a number 313 //----- Get if it is a number 328 if(IsNumber(word)) << 314 if( IsNumber(word) ) 329 { 315 { 330 //--- left separator cannot be ')' 316 //--- left separator cannot be ')' 331 if((*site != -1) && (cstr[*site] == ') << 317 if( (*site != -1) && (cstr[*site] == ')') ) 332 { 318 { 333 G4String ErrMessage = << 319 G4String ErrMessage = "There cannot be a ')' before a number: " 334 "There cannot be a ')' before a nu << 320 + word + " in string: " + str; 335 " in string: " + str; << 321 G4Exception("G4tgrUtils::GetDouble()", "ParseError", 336 G4Exception("G4tgrUtils::GetDouble() << 322 FatalException, ErrMessage); 337 ErrMessage); << 338 } 323 } 339 //--- right separator cannot be '(' 324 //--- right separator cannot be '(' 340 if((*site2 != strlen) && (cstr[*site2] << 325 if( (*site2 != strlen) && (cstr[*site2] == '(') ) 341 { 326 { 342 G4String ErrMessage = << 327 G4String ErrMessage = "There cannot be a '(' after a number: " 343 "There cannot be a '(' after a num << 328 + word + " in string: " + str; 344 " in string: " + str; << 329 G4Exception("G4tgrUtils::GetDouble()", "ParseError", 345 G4Exception("G4tgrUtils::GetDouble() << 330 FatalException, ErrMessage); 346 ErrMessage); << 347 } 331 } 348 strnew += word; 332 strnew += word; 349 << 333 350 //------ If it is an string, check if 334 //------ If it is an string, check if it is a unit 351 } 335 } 352 else 336 else 353 { 337 { 354 //--- First character cannot be a digi 338 //--- First character cannot be a digit 355 if(isdigit(word[0])) << 339 if( isdigit(word[0]) ) 356 { 340 { 357 G4String ErrMessage = << 341 G4String ErrMessage = "String words cannot start with a digit: " 358 "String words cannot start with a << 342 + word + " in string: " + str; 359 " in string: " + str; << 343 G4Exception("G4tgrUtils::GetDouble()", "ParseError", 360 G4Exception("G4tgrUtils::GetDouble() << 344 FatalException, ErrMessage ); 361 ErrMessage); << 362 } 345 } 363 << 346 364 //----- Check if it is a function 347 //----- Check if it is a function 365 G4bool bWordOK = false; << 348 G4bool bWordOK = false; 366 if(G4tgrUtils::IsFunction(word)) << 349 if( G4tgrUtils::IsFunction( word ) ) 367 { 350 { 368 //--- It must be followed by '(' 351 //--- It must be followed by '(' 369 if((*site2 == strlen) || (cstr[*site << 352 if( (*site2 == strlen) || (cstr[*site2] != '(') ) 370 { 353 { 371 G4String ErrMessage = << 354 G4String ErrMessage = "There must be a '(' after a function: " 372 "There must be a '(' after a fun << 355 + word + " in string: " + str; 373 " in string: " + str; << 356 G4Exception("G4tgrUtils::GetDouble()", "ParseError", 374 G4Exception("G4tgrUtils::GetDouble << 357 FatalException, ErrMessage ); 375 ErrMessage); << 376 } 358 } 377 strnew += word; 359 strnew += word; 378 bWordOK = true; << 360 bWordOK = true; 379 //----- Check if it is a unit << 361 //----- Check if it is a unit 380 } 362 } 381 else if(G4tgrUtils::WordIsUnit(word)) << 363 else if( G4tgrUtils::WordIsUnit( word ) ) 382 { 364 { 383 //--- It must be preceded by a * 365 //--- It must be preceded by a * 384 if((*site == -1) || ((cstr[*site] != << 366 if( (*site == -1) >> 367 || ( (cstr[*site] != '*') && (cstr[*site] != '/') ) ) 385 { 368 { 386 G4String ErrMess = << 369 G4String ErrMess = "There must be a '*' before a unit definition: " 387 "There must be a '*' before a un << 370 + word + " in string " + str; 388 " in string " + str; << 371 G4Exception("G4tgrUtils::GetDouble()", "ParseError", 389 G4Exception("G4tgrUtils::GetDouble << 372 FatalException, ErrMess ); 390 ErrMess); << 391 } 373 } 392 //--- check that it is indeed a CLHE 374 //--- check that it is indeed a CLHEP unit 393 if(G4UnitDefinition::GetValueOf(word << 375 if( G4UnitDefinition::GetValueOf(word) != 0. ) 394 { 376 { 395 bWordOK = true; << 377 bWordOK = true; 396 nUnits++; 378 nUnits++; 397 if(nUnits > 1) << 379 if( nUnits > 1 ) 398 { 380 { 399 // G4String ErrMess = "There can 381 // G4String ErrMess = "There cannot be two unit definitions: " 400 // + word + " i 382 // + word + " in string " + str; 401 // G4Exception("G4tgrUtils::GetD 383 // G4Exception("G4tgrUtils::GetDouble()", "ParseError", 402 // FatalException, E 384 // FatalException, ErrMess ); 403 } 385 } 404 strnew += << 386 strnew += G4UIcommand::ConvertToString( G4UnitDefinition::GetValueOf(word) ); 405 G4UIcommand::ConvertToString(G4U << 406 } 387 } 407 } 388 } 408 if(!bWordOK) << 389 if( !bWordOK ) 409 { 390 { 410 G4String ErrMess = "String word is n << 391 G4String ErrMess = "String word is not a parameter, nor a unit\n"; 411 G4String("definit << 392 + G4String("definition nor a function: ") + word 412 G4String(" in str << 393 + G4String(" in string: ") + str; 413 G4Exception("G4tgrUtils::GetDouble() << 394 G4Exception("G4tgrUtils::GetDouble()", "ParseError", 414 ErrMess); << 395 FatalException, ErrMess ); 415 } 396 } 416 } 397 } 417 } 398 } 418 } 399 } 419 400 420 G4double val = theEvaluator->evaluate(strnew << 401 G4double val = theEvaluator->evaluate( strnew.c_str() ); 421 if(theEvaluator->status() != HepTool::Evalua << 402 if (theEvaluator->status() != HepTool::Evaluator::OK) 422 { 403 { 423 theEvaluator->print_error(theEvaluator->st 404 theEvaluator->print_error(theEvaluator->status()); 424 G4String ErrMessage = "Evaluator error: " 405 G4String ErrMessage = "Evaluator error: " + strnew; 425 G4Exception("G4tgrUtils::GetDouble()", "Pa << 406 G4Exception("G4tgrUtils::GetDouble()", "ParseError", 426 ErrMessage); << 407 FatalException, ErrMessage ); 427 } << 428 << 429 if(nUnits == 0) << 430 { << 431 val *= unitval; << 432 } 408 } >> 409 >> 410 if( nUnits == 0 ) { val *= unitval; } 433 411 434 #ifdef G4VERBOSE 412 #ifdef G4VERBOSE 435 if(G4tgrMessenger::GetVerboseLevel() >= 3) << 413 if( G4tgrMessenger::GetVerboseLevel() >= 3 ) 436 { 414 { 437 G4cout << " G4tgrUtils::GetDouble() - RESU 415 G4cout << " G4tgrUtils::GetDouble() - RESULT= " << val << G4endl 438 << " from string: " << str << " c 416 << " from string: " << str << " converted to: " << strnew.c_str() 439 << " with unit val: " << unitval << 417 << " with unit val: " << unitval << G4endl; 440 } 418 } 441 #endif 419 #endif 442 420 443 return val; 421 return val; 444 } 422 } 445 423 446 // ------------------------------------------- << 424 447 G4int G4tgrUtils::GetInt(const G4String& str) << 425 //------------------------------------------------------------- >> 426 G4int G4tgrUtils::GetInt( const G4String& str ) 448 { 427 { 449 //----- Convert it to a number (it can be a 428 //----- Convert it to a number (it can be a parameter) 450 G4double val = GetDouble(str); 429 G4double val = GetDouble(str); 451 430 452 //----- Check it is an integer 431 //----- Check it is an integer 453 if(!IsInteger(val)) << 432 if( !IsInteger(val) ) 454 { 433 { 455 G4String ErrMessage = G4String("Trying to << 434 G4String ErrMessage = G4String("Trying to get the integer from a number") 456 G4String(" which is << 435 + G4String(" which is not an integer ") + str; 457 G4Exception("G4tgrUtils::GetInt()", "Parse << 436 G4Exception("G4tgrUtils::GetInt()", "ParseError", 458 ErrMessage); << 437 FatalException, ErrMessage ); 459 } 438 } 460 return G4int(val); << 439 return G4int( val ); 461 } 440 } 462 441 463 // ------------------------------------------- << 442 464 G4bool G4tgrUtils::GetBool(const G4String& str << 443 //------------------------------------------------------------- >> 444 G4bool G4tgrUtils::GetBool( const G4String& str ) 465 { 445 { 466 G4bool val = false; 446 G4bool val = false; 467 << 447 468 //----------- first check that it is a not n 448 //----------- first check that it is a not number 469 if((str == "ON") || (str == "TRUE")) << 449 if( (str == "ON") || (str == "TRUE") ) 470 { 450 { 471 val = true; 451 val = true; 472 } 452 } 473 else if((str == "OFF") || (str == "FALSE")) << 453 else if( (str == "OFF") || (str == "FALSE") ) 474 { 454 { 475 val = false; 455 val = false; 476 } 456 } 477 else 457 else 478 { 458 { 479 G4String ErrMessage = G4String("Trying to << 459 G4String ErrMessage = G4String("Trying to get a float from a string") 480 G4String(" which is << 460 + G4String(" which is not 'ON'/'OFF'/'TRUE'/'FALSE' ") 481 str; << 461 + str; 482 G4Exception("G4tgrUtils::GetBool()", "Pars << 462 G4Exception("G4tgrUtils::GetBool()", "ParseError", 483 ErrMessage); << 463 FatalException, ErrMessage ); 484 } 464 } 485 465 486 return val; 466 return val; 487 } 467 } 488 468 489 // ------------------------------------------- << 469 490 void G4tgrUtils::CheckWLsize(const std::vector << 470 //------------------------------------------------------------- 491 unsigned int nWch << 471 void G4tgrUtils::CheckWLsize( const std::vector<G4String>& wl, 492 const G4String& m << 472 unsigned int nWcheck, WLSIZEtype st, >> 473 const G4String& methodName ) 493 { 474 { >> 475 494 G4String outStr = methodName + G4String(". 476 G4String outStr = methodName + G4String(". Line read with number of words "); 495 unsigned int wlsize = (unsigned int)wl.size( << 477 unsigned int wlsize = wl.size(); 496 478 497 G4bool isOK = CheckListSize(wlsize, nWcheck, << 479 G4bool isOK = CheckListSize( wlsize, nWcheck, st, outStr ); 498 480 499 if(!isOK) << 481 if( !isOK ) 500 { << 482 { 501 G4String chartmp = G4UIcommand::ConvertToS << 483 G4String chartmp = G4UIcommand::ConvertToString( G4int(nWcheck) ); 502 outStr += chartmp + G4String(" words"); 484 outStr += chartmp + G4String(" words"); 503 DumpVS(wl, outStr.c_str()); << 485 DumpVS( wl, outStr.c_str() ); 504 G4String ErrMessage = << 486 G4String ErrMessage = " NUMBER OF WORDS: " 505 " NUMBER OF WORDS: " + G4UIcommand::Conv << 487 + G4UIcommand::ConvertToString(G4int(wlsize)); 506 G4Exception("G4tgrUtils::CheckWLsize()", " << 488 G4Exception("G4tgrUtils::CheckWLsize()", "ParseError", 507 ErrMessage); << 489 FatalException, ErrMessage); 508 } 490 } 509 } 491 } 510 492 511 // ------------------------------------------- << 493 //------------------------------------------------------------- 512 G4bool G4tgrUtils::CheckListSize(unsigned int << 494 G4bool G4tgrUtils::CheckListSize( unsigned int nWreal, unsigned int nWcheck, 513 WLSIZEtype st << 495 WLSIZEtype st, G4String& outStr ) 514 { 496 { 515 G4bool isOK = true; 497 G4bool isOK = true; 516 switch(st) << 498 switch (st) 517 { 499 { 518 case WLSIZE_EQ: << 500 case WLSIZE_EQ: 519 if(nWreal != nWcheck) << 501 if( nWreal != nWcheck ) 520 { << 502 { 521 isOK = false; << 503 isOK = false; 522 outStr += G4String("not equal than "); << 504 outStr += G4String("not equal than "); 523 } << 505 } 524 break; << 506 break; 525 case WLSIZE_NE: << 507 case WLSIZE_NE: 526 if(nWreal == nWcheck) << 508 if( nWreal == nWcheck ) 527 { << 509 { 528 isOK = false; << 510 isOK = false; 529 outStr += G4String("equal than "); << 511 outStr += G4String("equal than "); 530 } << 512 } 531 break; << 513 break; 532 case WLSIZE_LE: << 514 case WLSIZE_LE: 533 if(nWreal > nWcheck) << 515 if( nWreal > nWcheck ) 534 { << 516 { 535 isOK = false; << 517 isOK = false; 536 outStr += G4String("greater than "); << 518 outStr += G4String("greater than "); 537 } << 519 } 538 break; << 520 break; 539 case WLSIZE_LT: << 521 case WLSIZE_LT: 540 if(nWreal >= nWcheck) << 522 if( nWreal >= nWcheck ) 541 { << 523 { 542 isOK = false; << 524 isOK = false; 543 outStr += G4String("greater or equal t << 525 outStr += G4String("greater or equal than "); 544 } << 526 } 545 break; << 527 break; 546 case WLSIZE_GE: << 528 case WLSIZE_GE: 547 if(nWreal < nWcheck) << 529 if( nWreal < nWcheck ) 548 { << 530 { 549 isOK = false; << 531 isOK = false; 550 outStr += G4String("less than "); << 532 outStr += G4String("less than "); 551 } << 533 } 552 break; << 534 break; 553 case WLSIZE_GT: << 535 case WLSIZE_GT: 554 if(nWreal <= nWcheck) << 536 if( nWreal <= nWcheck ) 555 { << 537 { 556 isOK = false; << 538 isOK = false; 557 outStr += G4String("less or equal than << 539 outStr += G4String("less or equal than "); 558 } << 540 } 559 break; << 541 break; 560 default: << 542 default: 561 G4cerr << " ERROR!! - G4tgrUtils::CheckL << 543 G4cerr << " ERROR!! - G4tgrUtils::CheckListSize()" << G4endl 562 << " Type of WLSIZE typ << 544 << " Type of WLSIZE type not found " << st << G4endl; 563 break; << 545 break; 564 } 546 } 565 547 566 return isOK; 548 return isOK; 567 } 549 } 568 550 569 // ------------------------------------------- << 551 570 G4bool G4tgrUtils::WordIsUnit(const G4String& << 552 //------------------------------------------------------------- >> 553 G4bool G4tgrUtils::WordIsUnit( const G4String& word ) 571 { 554 { 572 return !IsNumber(word); 555 return !IsNumber(word); 573 if(word == "mm" || word == "cm" || word == " << 556 if( word == "mm" 574 word == "millimeter" || word == "centimet << 557 || word == "cm" 575 word == "kilometer" || word == "parsec" | << 558 || word == "m" 576 word == "nanometer" || word == "angstrom" << 559 || word == "km" 577 word == "nm" || word == "um" || word == " << 560 || word == "millimeter" 578 word == "milliradian" || word == "degree" << 561 || word == "centimeter" 579 word == "mrad" || word == "deg" || word = << 562 || word == "meter" 580 word == "curie") << 563 || word == "kilometer" 581 { << 564 || word == "parsec" >> 565 || word == "micrometer" >> 566 || word == "nanometer" >> 567 || word == "angstrom" >> 568 || word == "fermi" >> 569 || word == "nm" >> 570 || word == "um" >> 571 || word == "pc" >> 572 || word == "radian" >> 573 || word == "milliradian" >> 574 || word == "degree" >> 575 || word == "rad" >> 576 || word == "mrad" >> 577 || word == "deg" >> 578 || word == "ns" >> 579 || word == "curie" >> 580 || word == "curie" ) >> 581 { 582 return true; 582 return true; 583 } 583 } 584 else 584 else 585 { << 585 { 586 return false; 586 return false; 587 } 587 } 588 } 588 } 589 589 590 // ------------------------------------------- << 590 591 G4bool G4tgrUtils::IsFunction(const G4String& << 591 //------------------------------------------------------------- >> 592 G4bool G4tgrUtils::IsFunction( const G4String& word ) 592 { 593 { 593 if(word == "sin" || word == "cos" || word == << 594 if( word == "sin" 594 word == "acos" || word == "atan" || word << 595 || word == "cos" 595 word == "cosh" || word == "tanh" || word << 596 || word == "tan" 596 word == "atanh" || word == "sqrt" || word << 597 || word == "asin" 597 word == "log10" || word == "pow") << 598 || word == "acos" 598 { << 599 || word == "atan" >> 600 || word == "atan2" >> 601 || word == "sinh" >> 602 || word == "cosh" >> 603 || word == "tanh" >> 604 || word == "asinh" >> 605 || word == "acosh" >> 606 || word == "atanh" >> 607 || word == "sqrt" >> 608 || word == "exp" >> 609 || word == "log" >> 610 || word == "log10" >> 611 || word == "pow" ) >> 612 { 599 return true; 613 return true; 600 } 614 } 601 else 615 else 602 { << 616 { 603 return false; 617 return false; 604 } 618 } 605 } 619 } 606 620 607 // ------------------------------------------- << 621 608 G4RotationMatrix G4tgrUtils::GetRotationFromDi << 622 //------------------------------------------------------------- >> 623 G4RotationMatrix G4tgrUtils::GetRotationFromDirection( G4ThreeVector dir ) 609 { 624 { 610 G4RotationMatrix rotation; 625 G4RotationMatrix rotation; 611 626 612 if(std::fabs(dir.mag() - 1.) > << 627 if( std::fabs(dir.mag()-1.) > G4GeometryTolerance::GetInstance() 613 G4GeometryTolerance::GetInstance()->GetSu << 628 ->GetSurfaceTolerance() ) 614 { 629 { 615 G4String WarMessage = "Direction cosines h << 630 G4String WarMessage = "Direction cosines have been normalized to one.\n" 616 G4String("They were << 631 + G4String("They were normalized to ") 617 G4UIcommand::Convert << 632 + G4UIcommand::ConvertToString(dir.mag()); 618 G4Exception("G4tgrUtils::GetRotationFromDi 633 G4Exception("G4tgrUtils::GetRotationFromDirection()", "WrongArgument", 619 JustWarning, WarMessage); 634 JustWarning, WarMessage); 620 dir /= dir.mag(); 635 dir /= dir.mag(); 621 } << 636 } 622 G4double angx = -std::asin(dir.y()); 637 G4double angx = -std::asin(dir.y()); 623 638 624 // There are always two solutions angx, angy 639 // There are always two solutions angx, angy and PI-angx, 625 // PI+angy, choose first 640 // PI+angy, choose first 626 // 641 // 627 G4double angy; 642 G4double angy; 628 if(dir.y() == 1.) << 643 if( dir.y() == 1. ) 629 { 644 { 630 angy = 0.; 645 angy = 0.; 631 } 646 } 632 else if(dir.y() == 0.) << 647 else if( dir.y() == 0. ) 633 { 648 { 634 angy = 0.; 649 angy = 0.; 635 } 650 } 636 else 651 else 637 { 652 { 638 angy = std::asin(dir.x() / std::sqrt(1 - d << 653 angy = std::asin( dir.x()/std::sqrt(1-dir.y()*dir.y()) ); 639 } 654 } 640 << 655 641 // choose between angy and PI-angy 656 // choose between angy and PI-angy 642 if(dir.z() * std::cos(angx) * std::cos(angy) << 657 if( dir.z() * std::cos(angx)*std::cos(angy) < 0 ) 643 { 658 { 644 angy = pi - angy; 659 angy = pi - angy; 645 } 660 } 646 rotation.rotateX(angx); << 661 rotation.rotateX( angx ); 647 rotation.rotateY(angy); << 662 rotation.rotateY( angy ); 648 663 649 return rotation; 664 return rotation; 650 } << 665 } >> 666 651 667 652 // ------------------------------------------- << 668 //------------------------------------------------------------- 653 G4bool G4tgrUtils::AreWordsEquivalent(const G4 << 669 G4bool G4tgrUtils::AreWordsEquivalent( const G4String& word1, 654 const G4 << 670 const G4String& word2 ) 655 { 671 { 656 G4bool bEqual = true; 672 G4bool bEqual = true; 657 std::vector<std::pair<size_t, size_t>> strin << 673 std::vector< std::pair<G4int,G4int> > stringPairs; 658 // start of substring, number of characters << 674 // start of substring, number of characters 659 675 660 //--- Get string limits between asterisks in 676 //--- Get string limits between asterisks in word1 661 677 662 std::size_t cStart = 0; << 678 size_t cStart = 0; 663 for(;;) << 679 for( ;; ) 664 { 680 { 665 size_t cAster = word1.find("*", cStart); << 681 size_t cAster = word1.find("*",cStart); 666 if(cAster != std::string::npos) << 682 if( cAster != std::string::npos ) 667 { 683 { 668 if(cAster == cStart) << 684 if( cAster == cStart ) 669 { 685 { 670 if(cAster != 0) << 686 if( cAster != 0 ) 671 { 687 { 672 G4Exception("G4tgrUtils::AreWordsEqu 688 G4Exception("G4tgrUtils::AreWordsEquivalent()", 673 "A word has two asterisk 689 "A word has two asterisks together, please correct it", 674 FatalException, ("Offend << 690 FatalException,("Offending word is: " + word1).c_str() ); 675 } 691 } 676 else 692 else 677 { 693 { 678 // word1 == * 694 // word1 == * 679 if(word1.size() == 1) << 695 if(word1.size() == 1 ) { return true; } 680 { << 681 return true; << 682 } << 683 } 696 } 684 } << 697 } 685 if(cAster != cStart) << 698 if( cAster!= cStart ) 686 { 699 { 687 stringPairs.push_back( << 700 stringPairs.push_back( std::pair<G4int,G4int>(cStart, cAster-cStart) ); 688 std::pair<size_t, size_t>(cStart, cA << 689 } 701 } 690 cStart = cAster + 1; << 702 cStart = cAster+1; 691 } 703 } 692 else 704 else 693 { 705 { 694 if(cStart == 0) << 706 if( cStart == 0 ) 695 { 707 { 696 //--- If there is no asterisk check if 708 //--- If there is no asterisk check if they are the same 697 return word1 == word2; 709 return word1 == word2; 698 } 710 } 699 break; 711 break; 700 } 712 } 701 } 713 } 702 << 714 703 //---- Add characters after last asterisk as << 715 //---- Add characters after last asterisk as string pair 704 if(cStart <= word1.length()) << 716 if( cStart <= word1.length() ) 705 { 717 { 706 if(word1.length() != cStart) << 718 if( word1.length() != cStart ) 707 { 719 { 708 stringPairs.push_back( << 720 stringPairs.push_back( std::pair<G4int,G4int>(cStart, 709 std::pair<size_t, size_t>(cStart, word << 721 word1.length()-cStart) ); 710 } 722 } 711 } 723 } 712 724 713 //--- If there are not asterisk, simple comp 725 //--- If there are not asterisk, simple comparison 714 if(stringPairs.size() == 0) << 726 if( stringPairs.size() == 0 ) 715 { 727 { 716 if(word1 == word2) << 728 if( word1 == word2 ) 717 { 729 { 718 return true; 730 return true; 719 } 731 } 720 else 732 else 721 { 733 { 722 return false; 734 return false; 723 } 735 } 724 } 736 } 725 << 737 726 //--- Find substrings in word2, in same orde 738 //--- Find substrings in word2, in same order as in word1 727 cStart = 0; 739 cStart = 0; 728 for(std::size_t ii = 0; ii < stringPairs.siz << 740 for( size_t ii = 0; ii < stringPairs.size(); ii++ ) 729 { 741 { 730 std::pair<size_t, size_t> spair = stringPa << 742 std::pair<G4int,G4int> spair = stringPairs[ii]; 731 size_t sFound = word2.find(word1.substr(sp << 743 size_t sFound = word2.find(word1.substr(spair.first, spair.second),cStart); 732 if(sFound == std::string::npos) << 744 if( sFound == std::string::npos ) 733 { 745 { 734 bEqual = false; 746 bEqual = false; 735 break; 747 break; 736 } 748 } 737 else 749 else 738 { 750 { 739 //---- If there is no asterisk before fi 751 //---- If there is no asterisk before first character, 740 // the fisrt string pair found has t 752 // the fisrt string pair found has to start at the first character 741 if(spair.first == 0 && sFound != 0) << 753 if( spair.first == 0 && sFound != 0 ) 742 { 754 { 743 bEqual = false; 755 bEqual = false; 744 break; 756 break; 745 //---- If there is no asterisk after l 757 //---- If there is no asterisk after last character, 746 // the last string pair found has 758 // the last string pair found has to end at the last character 747 } 759 } 748 else if((spair.first + spair.second - 1 << 760 else if( (spair.first+spair.second-1 == G4int(word1.length())) 749 (sFound + spair.second - 1 != wo << 761 && (G4int(sFound)+spair.second-1 != G4int(word2.length())) ) 750 { 762 { 751 bEqual = false; 763 bEqual = false; 752 break; 764 break; 753 } 765 } 754 cStart += spair.second; 766 cStart += spair.second; 755 } << 767 } 756 } 768 } 757 769 758 return bEqual; 770 return bEqual; 759 } 771 } 760 772