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 26 27 // Author: Ivana Hrivnacova, 22/08/2013 (ivan 27 // Author: Ivana Hrivnacova, 22/08/2013 (ivana@ipno.in2p3.fr) 28 28 29 #include "G4AnalysisUtilities.hh" 29 #include "G4AnalysisUtilities.hh" 30 #include "G4BinScheme.hh" 30 #include "G4BinScheme.hh" 31 #include "G4Exception.hh" << 32 #include "G4UnitsTable.hh" 31 #include "G4UnitsTable.hh" 33 #include "G4String.hh" 32 #include "G4String.hh" 34 #include "G4Threading.hh" 33 #include "G4Threading.hh" 35 #include "G4Filesystem.hh" << 36 << 37 using std::to_string; << 38 34 39 namespace { 35 namespace { 40 36 41 //____________________________________________ 37 //_____________________________________________________________________________ 42 G4bool GetToken(const G4String& line, G4String << 38 G4bool GetToken(const G4String& line, G4String& token, 43 std::string::size_type begIdx, 39 std::string::size_type begIdx, std::string::size_type& endIdx) 44 { 40 { 45 while ( line[(G4int)begIdx] == ' ') ++begIdx << 41 while ( line[begIdx] == ' ') ++begIdx; // Loop checking, 23.06.2015, I. Hrivnacova 46 if ( line[(G4int)begIdx] == '"' ) { << 42 if ( line[begIdx] == '"' ) { 47 endIdx = line.find('"', begIdx+1); 43 endIdx = line.find('"', begIdx+1); 48 if ( endIdx == std::string::npos ) endIdx 44 if ( endIdx == std::string::npos ) endIdx = line.length(); 49 token = line.substr(begIdx+1, (endIdx-1)-b 45 token = line.substr(begIdx+1, (endIdx-1)-begIdx); 50 ++endIdx; 46 ++endIdx; 51 } 47 } 52 else { 48 else { 53 endIdx = line.find(' ', begIdx); 49 endIdx = line.find(' ', begIdx); 54 if ( endIdx == std::string::npos ) endIdx 50 if ( endIdx == std::string::npos ) endIdx = line.length(); 55 token = line.substr(begIdx, endIdx-begIdx) 51 token = line.substr(begIdx, endIdx-begIdx); 56 } 52 } 57 return ( token.length() > 0 ); 53 return ( token.length() > 0 ); 58 } << 54 } 59 55 60 } 56 } 61 57 62 namespace G4Analysis 58 namespace G4Analysis 63 { 59 { 64 60 65 //____________________________________________ 61 //_____________________________________________________________________________ 66 void Warn(const G4String& message, << 62 G4bool CheckNbins(G4int nbins) 67 const std::string_view inClass, << 68 const std::string_view inFunction) << 69 { 63 { 70 auto source = std::string(inClass) + "::" + << 64 if ( nbins <= 0 ) { 71 G4Exception(source.data(), "Analysis_W001", << 65 G4ExceptionDescription description; >> 66 description >> 67 << " Illegal value of number of bins: nbins <= 0" << G4endl; >> 68 G4Exception("G4VAnalysisManager::CheckNbins", >> 69 "Analysis_W013", JustWarning, description); >> 70 return false; >> 71 } >> 72 else >> 73 return true; >> 74 } >> 75 >> 76 >> 77 //_____________________________________________________________________________ >> 78 G4bool CheckMinMax(G4double xmin, G4double xmax, >> 79 const G4String& fcnName, const G4String& binSchemeName) >> 80 { >> 81 auto result = true; >> 82 >> 83 if ( xmax <= xmin ) { >> 84 G4ExceptionDescription description; >> 85 description >> 86 << " Illegal values of (xmin >= xmax)" << G4endl; >> 87 G4Exception("G4VAnalysisManager::CheckMinMax", >> 88 "Analysis_W013", JustWarning, description); >> 89 >> 90 result = false; >> 91 } >> 92 >> 93 if ( ( fcnName != "none" ) && ( binSchemeName != "linear" ) ) { >> 94 G4ExceptionDescription description; >> 95 description >> 96 << " Combining Function and Binning scheme is not supported." >> 97 << G4endl; >> 98 G4Exception("G4VAnalysisManager::CheckMinMax", >> 99 "Analysis_W013", JustWarning, description); >> 100 >> 101 result = false; >> 102 } >> 103 >> 104 if ( ( GetBinScheme(binSchemeName) == G4BinScheme::kLog || >> 105 fcnName == "log" || fcnName == "log10" ) && ( xmin == 0 ) ) { >> 106 G4ExceptionDescription description; >> 107 description >> 108 << " Illegal value of (xmin = 0) with logarithmic function or binning" >> 109 << G4endl; >> 110 G4Exception("G4VAnalysisManager::CheckMinMax", >> 111 "Analysis_W013", JustWarning, description); >> 112 >> 113 result = false; >> 114 } >> 115 >> 116 return result; >> 117 } >> 118 >> 119 //_____________________________________________________________________________ >> 120 G4bool CheckEdges(const std::vector<G4double>& edges) >> 121 { >> 122 if ( edges.size() <= 1 ) { >> 123 G4ExceptionDescription description; >> 124 description >> 125 << " Illegal edges vector (size <= 1)" << G4endl; >> 126 G4Exception("G4VAnalysisManager::CheckEdges", >> 127 "Analysis_W013", JustWarning, description); >> 128 return false; >> 129 } >> 130 else >> 131 return true; >> 132 >> 133 } >> 134 >> 135 //_____________________________________________________________________________ >> 136 G4bool CheckName(const G4String& name, const G4String& objectType) >> 137 { >> 138 if ( ! name.size() ) { >> 139 G4ExceptionDescription description; >> 140 description >> 141 << " Empty " << objectType << " name is not allowed." << G4endl >> 142 << " " << objectType << " was not created." << G4endl; >> 143 G4Exception("G4VAnalysisManager::CheckName", >> 144 "Analysis_W013", JustWarning, description); >> 145 return false; >> 146 } >> 147 else >> 148 return true; 72 } 149 } 73 150 74 //____________________________________________ 151 //_____________________________________________________________________________ 75 G4double GetUnitValue(const G4String& unit) 152 G4double GetUnitValue(const G4String& unit) 76 { 153 { 77 G4double value = 1.; 154 G4double value = 1.; 78 if ( unit != "none" ) { 155 if ( unit != "none" ) { 79 value = G4UnitDefinition::GetValueOf(unit 156 value = G4UnitDefinition::GetValueOf(unit); 80 if ( value == 0. ) value = 1.; << 157 if ( value == 0. ) value = 1.; 81 } << 158 } 82 return value; 159 return value; 83 } << 160 } >> 161 >> 162 //_____________________________________________________________________________ >> 163 void UpdateTitle(G4String& title, >> 164 const G4String& unitName, >> 165 const G4String& fcnName) >> 166 { >> 167 if ( fcnName != "none" ) { title += " "; title += fcnName; title += "("; } >> 168 if ( unitName != "none" ) { title += " ["; title += unitName; title += "]";} >> 169 if ( fcnName != "none" ) { title += ")"; } >> 170 } 84 171 85 //____________________________________________ 172 //_____________________________________________________________________________ 86 void Tokenize(const G4String& line, std::vect 173 void Tokenize(const G4String& line, std::vector<G4String>& tokens) 87 { 174 { 88 // Define start values 175 // Define start values 89 std::string::size_type begIdx = 0; 176 std::string::size_type begIdx = 0; 90 std::string::size_type endIdx = 0; << 177 std::string::size_type endIdx = 0; 91 G4String token; 178 G4String token; 92 << 179 93 do { 180 do { 94 if ( GetToken(line, token, begIdx, endIdx) 181 if ( GetToken(line, token, begIdx, endIdx) ) { 95 //G4cout << "got token: '" << token << " 182 //G4cout << "got token: '" << token << "'" << G4endl; 96 //G4cout << "beg, end: " << begIdx << ", 183 //G4cout << "beg, end: " << begIdx << ", " << endIdx << G4endl; 97 tokens.push_back(token); 184 tokens.push_back(token); 98 } << 185 } 99 begIdx = endIdx + 1; 186 begIdx = endIdx + 1; 100 } 187 } 101 while ( endIdx < line.length() ); // Loop ch 188 while ( endIdx < line.length() ); // Loop checking, 23.06.2015, I. Hrivnacova 102 } 189 } 103 190 104 //____________________________________________ 191 //_____________________________________________________________________________ 105 G4AnalysisOutput GetOutput(const G4String& out << 192 G4AnalysisOutput GetOutput(const G4String& outputName, G4bool warn) { 106 { << 193 if ( outputName == "csv" ) { return G4AnalysisOutput::kCsv; } 107 if (outputName == "csv") return G4AnalysisO << 194 else if ( outputName == "hdf5" ) { return G4AnalysisOutput::kHdf5; } 108 if (outputName == "hdf5") return G4AnalysisO << 195 else if ( outputName == "root" ) { return G4AnalysisOutput::kRoot; } 109 if (outputName == "root") return G4AnalysisO << 196 else if ( outputName == "xml" ) { return G4AnalysisOutput::kXml; } 110 if (outputName == "xml") return G4AnalysisO << 197 else if ( outputName == "none" ) { return G4AnalysisOutput::kNone; } 111 if (outputName == "none") return G4AnalysisO << 198 else { 112 << 199 if (warn) { 113 if (warn) { << 200 G4ExceptionDescription description; 114 Warn("\"" + outputName + "\" output type i << 201 description >> 202 << " \"" << outputName << "\" output type is not supported." << G4endl; >> 203 G4Exception("G4Analysis::GetOutputType", >> 204 "Analysis_W051", JustWarning, description); >> 205 } >> 206 return G4AnalysisOutput::kNone; 115 } 207 } 116 return G4AnalysisOutput::kNone; << 117 } 208 } 118 209 119 //____________________________________________ 210 //_____________________________________________________________________________ 120 G4String GetOutputName(G4AnalysisOutput output << 211 G4String GetOutputName(G4AnalysisOutput output) { 121 { << 122 switch ( output ) { 212 switch ( output ) { 123 case G4AnalysisOutput::kCsv: 213 case G4AnalysisOutput::kCsv: 124 return "csv"; 214 return "csv"; 125 break; 215 break; 126 case G4AnalysisOutput::kHdf5: << 216 case G4AnalysisOutput::kHdf5: 127 return "hdf5"; 217 return "hdf5"; 128 break; 218 break; 129 case G4AnalysisOutput::kRoot: << 219 case G4AnalysisOutput::kRoot: 130 return "root"; 220 return "root"; 131 break; 221 break; 132 case G4AnalysisOutput::kXml: 222 case G4AnalysisOutput::kXml: 133 return "xml"; 223 return "xml"; 134 break; 224 break; 135 case G4AnalysisOutput::kNone: 225 case G4AnalysisOutput::kNone: 136 return "none"; 226 return "none"; 137 break; 227 break; 138 } 228 } 139 // should never reach this line 229 // should never reach this line 140 Warn("\"" + to_string(static_cast<int>(outpu << 230 G4ExceptionDescription description; 141 "\" output type is not supported.", << 231 description 142 kNamespaceName, "CheckOutputName"); << 232 << " \"" << static_cast<int>(output) << "\" is not handled." << G4endl >> 233 << " " << "none type will be used."; >> 234 G4Exception("G4Analysis::GetOutputName", >> 235 "Analysis_W051", JustWarning, description); 143 return "none"; 236 return "none"; 144 } 237 } 145 238 146 //____________________________________________ 239 //_____________________________________________________________________________ 147 G4String GetBaseName(const G4String& fileName) 240 G4String GetBaseName(const G4String& fileName) 148 { 241 { 149 // Get file base name (without dot) 242 // Get file base name (without dot) 150 243 151 G4fs::path filePath(fileName.data()); << 244 G4String name = fileName; 152 if ( filePath.has_parent_path()) { << 245 if ( name.rfind(".") != std::string::npos ) { 153 return filePath.parent_path().string() + << 246 name = name.substr(0, name.rfind(".")); 154 } 247 } 155 << 248 return name; 156 return filePath.stem().string(); << 249 } 157 } << 158 250 159 //____________________________________________ 251 //_____________________________________________________________________________ 160 G4String GetExtension(const G4String& fileName 252 G4String GetExtension(const G4String& fileName, 161 const G4String& defaultE 253 const G4String& defaultExtension) 162 { 254 { 163 // Get file base extension (without dot) 255 // Get file base extension (without dot) 164 // If fileName is provided without extension, 256 // If fileName is provided without extension, return defaultExtension 165 257 166 G4fs::path filePath(fileName.data()); << 258 G4String extension; 167 if ( filePath.has_extension() ) { << 259 if ( fileName.rfind(".") != std::string::npos ) { 168 auto extension = filePath.extension().stri << 260 extension = fileName.substr(fileName.rfind(".") + 1); 169 // remove "." << 170 return extension.substr(1, extension.lengt << 171 } 261 } 172 << 262 if ( ! extension.size() ) { 173 return defaultExtension; << 263 extension = defaultExtension; >> 264 } >> 265 return extension; 174 } 266 } 175 267 176 //____________________________________________ 268 //_____________________________________________________________________________ 177 G4String GetHnFileName( 269 G4String GetHnFileName( 178 const G4String& fileName, 270 const G4String& fileName, 179 const G4String& fileType, << 271 const G4String& fileType, 180 const G4String& hnType, << 272 const G4String& hnType, 181 const G4String& hnName) 273 const G4String& hnName) 182 { 274 { 183 // Compose and return the histogram or profile 275 // Compose and return the histogram or profile specific file name: 184 // - add _hn_hnName suffix to the file base na 276 // - add _hn_hnName suffix to the file base name 185 // - add _vN suffix if cycle > 0 << 186 // - add file extension if not present 277 // - add file extension if not present 187 278 188 auto name = GetBaseName(fileName); 279 auto name = GetBaseName(fileName); 189 << 280 190 // Add _hnType_hnName 281 // Add _hnType_hnName 191 name.append("_"); 282 name.append("_"); 192 name.append(hnType); 283 name.append(hnType); 193 name.append("_"); 284 name.append("_"); 194 name.append(hnName); 285 name.append(hnName); 195 286 196 // Add file extension 287 // Add file extension 197 auto extension = GetExtension(fileName, file 288 auto extension = GetExtension(fileName, fileType); 198 if (extension.size() != 0u) { << 289 if ( extension.size() ) { 199 name.append("."); << 200 name.append(extension); << 201 } << 202 << 203 return name; << 204 } << 205 << 206 //____________________________________________ << 207 G4String GetHnFileName( << 208 const G4String& fileName, << 209 const G4String& fileType, << 210 G4int cycle) << 211 { << 212 // Update Hn file name: << 213 // - add _vN suffix to the base namer if cycl << 214 << 215 auto name = GetBaseName(fileName); << 216 << 217 // Add cycle number << 218 if (cycle > 0) { << 219 name.append("_v"); << 220 name.append(std::to_string(cycle)); << 221 } << 222 << 223 // Add file extension << 224 auto extension = GetExtension(fileName, file << 225 if (extension.size() != 0u) { << 226 name.append("."); 290 name.append("."); 227 name.append(extension); 291 name.append(extension); 228 } 292 } 229 293 230 return name; 294 return name; 231 } 295 } 232 296 233 //____________________________________________ 297 //_____________________________________________________________________________ 234 G4String GetNtupleFileName( 298 G4String GetNtupleFileName( 235 const G4String& fileName, 299 const G4String& fileName, 236 const G4String& fileType, << 300 const G4String& fileType, 237 const G4String& ntupleName, << 301 const G4String& ntupleName) 238 G4int cycle) << 239 { 302 { 240 // Compose and return the ntuple specific file 303 // Compose and return the ntuple specific file name: 241 // - add _nt_ntupleName suffix to the file bas 304 // - add _nt_ntupleName suffix to the file base name 242 // - add _vN suffix if cycle > 0 << 243 // - add _tN suffix if called on thread worker 305 // - add _tN suffix if called on thread worker 244 // - add file extension if not present 306 // - add file extension if not present 245 307 246 auto name = GetBaseName(fileName); 308 auto name = GetBaseName(fileName); 247 << 309 248 // Add ntupleName 310 // Add ntupleName 249 name.append("_nt_"); 311 name.append("_nt_"); 250 name.append(ntupleName); 312 name.append(ntupleName); 251 313 252 // Add cycle number << 253 if (cycle > 0) { << 254 name.append("_v"); << 255 name.append(std::to_string(cycle)); << 256 } << 257 << 258 // Add thread Id to a file name if MT proces 314 // Add thread Id to a file name if MT processing 259 if ( ! G4Threading::IsMasterThread() ) { 315 if ( ! G4Threading::IsMasterThread() ) { 260 std::ostringstream os; 316 std::ostringstream os; 261 os << G4Threading::G4GetThreadId(); 317 os << G4Threading::G4GetThreadId(); 262 name.append("_t"); 318 name.append("_t"); 263 name.append(os.str()); 319 name.append(os.str()); 264 } << 320 } 265 321 266 // Add file extension 322 // Add file extension 267 auto extension = GetExtension(fileName, file 323 auto extension = GetExtension(fileName, fileType); 268 if (extension.size() != 0u) { << 324 if ( extension.size() ) { 269 name.append("."); 325 name.append("."); 270 name.append(extension); 326 name.append(extension); 271 } 327 } 272 << 328 273 return name; 329 return name; 274 } 330 } 275 331 276 //____________________________________________ 332 //_____________________________________________________________________________ 277 G4String GetNtupleFileName( 333 G4String GetNtupleFileName( 278 const G4String& fileName, 334 const G4String& fileName, 279 const G4String& fileType, << 335 const G4String& fileType, 280 G4int ntupleFileNumber, << 336 G4int ntupleFileNumber) 281 G4int cycle) << 282 { 337 { 283 // Compose and return the ntuple specific file 338 // Compose and return the ntuple specific file name: 284 // - add _mFN suffix to the file base name whe 339 // - add _mFN suffix to the file base name where FN = ntupleFileNumber 285 // - add _vN suffix if cycle > 0 << 286 // - add file extension if not present 340 // - add file extension if not present 287 341 288 auto name = GetBaseName(fileName); 342 auto name = GetBaseName(fileName); 289 << 343 290 // Add _M followed by ntupleFileNumber 344 // Add _M followed by ntupleFileNumber 291 std::ostringstream os; 345 std::ostringstream os; 292 os << ntupleFileNumber; 346 os << ntupleFileNumber; 293 name.append("_m"); 347 name.append("_m"); 294 name.append(os.str()); 348 name.append(os.str()); 295 349 296 // Add cycle number << 297 if (cycle > 0) { << 298 name.append("_v"); << 299 name.append(std::to_string(cycle)); << 300 } << 301 << 302 // Add file extension 350 // Add file extension 303 auto extension = GetExtension(fileName, file 351 auto extension = GetExtension(fileName, fileType); 304 if (extension.size() != 0u) { << 352 if ( extension.size() ) { 305 name.append("."); 353 name.append("."); 306 name.append(extension); 354 name.append(extension); 307 } 355 } 308 << 356 309 return name; 357 return name; 310 } 358 } 311 359 312 //____________________________________________ 360 //_____________________________________________________________________________ 313 G4String GetTnFileName( 361 G4String GetTnFileName( 314 const G4String& fileName, 362 const G4String& fileName, 315 const G4String& fileType, << 363 const G4String& fileType) 316 G4int cycle) << 317 { 364 { 318 // Update file base name with the thread suffi 365 // Update file base name with the thread suffix: 319 // - add _tN suffix if called on thread worker 366 // - add _tN suffix if called on thread worker 320 // - add file extension if not present 367 // - add file extension if not present 321 368 322 auto name = GetBaseName(fileName); 369 auto name = GetBaseName(fileName); 323 370 324 // Add cycle number << 325 if (cycle > 0) { << 326 name.append("_v"); << 327 name.append(std::to_string(cycle)); << 328 } << 329 << 330 // Add thread Id to a file name if MT proces 371 // Add thread Id to a file name if MT processing 331 if ( ! G4Threading::IsMasterThread() ) { 372 if ( ! G4Threading::IsMasterThread() ) { 332 std::ostringstream os; 373 std::ostringstream os; 333 os << G4Threading::G4GetThreadId(); 374 os << G4Threading::G4GetThreadId(); 334 name.append("_t"); 375 name.append("_t"); 335 name.append(os.str()); 376 name.append(os.str()); 336 } 377 } 337 378 338 // Add file extension 379 // Add file extension 339 auto extension = GetExtension(fileName, file 380 auto extension = GetExtension(fileName, fileType); 340 if (extension.size() != 0u) { << 381 if ( extension.size() ) { 341 name.append("."); 382 name.append("."); 342 name.append(extension); 383 name.append(extension); 343 } 384 } 344 385 345 return name; 386 return name; 346 } 387 } 347 388 348 //____________________________________________ 389 //_____________________________________________________________________________ 349 G4String GetPlotFileName(const G4String& fileN 390 G4String GetPlotFileName(const G4String& fileName) 350 { 391 { 351 // Generate plot file name for an output file 392 // Generate plot file name for an output file name 352 393 353 auto name = GetBaseName(fileName); 394 auto name = GetBaseName(fileName); 354 395 355 // Add .ps extension 396 // Add .ps extension 356 name.append(".ps"); 397 name.append(".ps"); 357 398 358 return name; 399 return name; 359 } 400 } 360 401 361 } 402 } 362 403