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