Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4AnalysisUtilities.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26
27// Author: Ivana Hrivnacova, 22/08/2013 ([email protected])
28
30#include "G4BinScheme.hh"
31#include "G4UnitsTable.hh"
32#include "G4String.hh"
33#include "G4Threading.hh"
34
35namespace {
36
37//_____________________________________________________________________________
38G4bool GetToken(const G4String& line, G4String& token,
39 std::string::size_type begIdx, std::string::size_type& endIdx)
40{
41 while ( line[begIdx] == ' ') ++begIdx; // Loop checking, 23.06.2015, I. Hrivnacova
42 if ( line[begIdx] == '"' ) {
43 endIdx = line.find('"', begIdx+1);
44 if ( endIdx == std::string::npos ) endIdx = line.length();
45 token = line.substr(begIdx+1, (endIdx-1)-begIdx);
46 ++endIdx;
47 }
48 else {
49 endIdx = line.find(' ', begIdx);
50 if ( endIdx == std::string::npos ) endIdx = line.length();
51 token = line.substr(begIdx, endIdx-begIdx);
52 }
53 return ( token.length() > 0 );
54}
55
56}
57
58namespace G4Analysis
59{
60
61//_____________________________________________________________________________
63{
64 if ( nbins <= 0 ) {
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//_____________________________________________________________________________
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//_____________________________________________________________________________
120G4bool 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//_____________________________________________________________________________
136G4bool 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;
149}
150
151//_____________________________________________________________________________
153{
154 G4double value = 1.;
155 if ( unit != "none" ) {
156 value = G4UnitDefinition::GetValueOf(unit);
157 if ( value == 0. ) value = 1.;
158 }
159 return value;
160}
161
162//_____________________________________________________________________________
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}
171
172//_____________________________________________________________________________
173void Tokenize(const G4String& line, std::vector<G4String>& tokens)
174{
175 // Define start values
176 std::string::size_type begIdx = 0;
177 std::string::size_type endIdx = 0;
178 G4String token;
179
180 do {
181 if ( GetToken(line, token, begIdx, endIdx) ) {
182 //G4cout << "got token: '" << token << "'" << G4endl;
183 //G4cout << "beg, end: " << begIdx << ", " << endIdx << G4endl;
184 tokens.push_back(token);
185 }
186 begIdx = endIdx + 1;
187 }
188 while ( endIdx < line.length() ); // Loop checking, 23.06.2015, I. Hrivnacova
189}
190
191//_____________________________________________________________________________
192G4AnalysisOutput GetOutput(const G4String& outputName, G4bool warn) {
193 if ( outputName == "csv" ) { return G4AnalysisOutput::kCsv; }
194 else if ( outputName == "hdf5" ) { return G4AnalysisOutput::kHdf5; }
195 else if ( outputName == "root" ) { return G4AnalysisOutput::kRoot; }
196 else if ( outputName == "xml" ) { return G4AnalysisOutput::kXml; }
197 else if ( outputName == "none" ) { return G4AnalysisOutput::kNone; }
198 else {
199 if (warn) {
200 G4ExceptionDescription description;
201 description
202 << " \"" << outputName << "\" output type is not supported." << G4endl;
203 G4Exception("G4Analysis::GetOutputType",
204 "Analysis_W051", JustWarning, description);
205 }
206 return G4AnalysisOutput::kNone;
207 }
208}
209
210//_____________________________________________________________________________
212 switch ( output ) {
213 case G4AnalysisOutput::kCsv:
214 return "csv";
215 break;
216 case G4AnalysisOutput::kHdf5:
217 return "hdf5";
218 break;
219 case G4AnalysisOutput::kRoot:
220 return "root";
221 break;
222 case G4AnalysisOutput::kXml:
223 return "xml";
224 break;
225 case G4AnalysisOutput::kNone:
226 return "none";
227 break;
228 }
229 // should never reach this line
230 G4ExceptionDescription description;
231 description
232 << " \"" << static_cast<int>(output) << "\" is not handled." << G4endl
233 << " " << "none type will be used.";
234 G4Exception("G4Analysis::GetOutputName",
235 "Analysis_W051", JustWarning, description);
236 return "none";
237}
238
239//_____________________________________________________________________________
241{
242// Get file base name (without dot)
243
244 G4String name = fileName;
245 if ( name.rfind(".") != std::string::npos ) {
246 name = name.substr(0, name.rfind("."));
247 }
248 return name;
249}
250
251//_____________________________________________________________________________
253 const G4String& defaultExtension)
254{
255// Get file base extension (without dot)
256// If fileName is provided without extension, return defaultExtension
257
258 G4String extension;
259 if ( fileName.rfind(".") != std::string::npos ) {
260 extension = fileName.substr(fileName.rfind(".") + 1);
261 }
262 if ( ! extension.size() ) {
263 extension = defaultExtension;
264 }
265 return extension;
266}
267
268//_____________________________________________________________________________
270 const G4String& fileName,
271 const G4String& fileType,
272 const G4String& hnType,
273 const G4String& hnName)
274{
275// Compose and return the histogram or profile specific file name:
276// - add _hn_hnName suffix to the file base name
277// - add file extension if not present
278
279 auto name = GetBaseName(fileName);
280
281 // Add _hnType_hnName
282 name.append("_");
283 name.append(hnType);
284 name.append("_");
285 name.append(hnName);
286
287 // Add file extension
288 auto extension = GetExtension(fileName, fileType);
289 if ( extension.size() ) {
290 name.append(".");
291 name.append(extension);
292 }
293
294 return name;
295}
296
297//_____________________________________________________________________________
299 const G4String& fileName,
300 const G4String& fileType,
301 const G4String& ntupleName)
302{
303// Compose and return the ntuple specific file name:
304// - add _nt_ntupleName suffix to the file base name
305// - add _tN suffix if called on thread worker
306// - add file extension if not present
307
308 auto name = GetBaseName(fileName);
309
310 // Add ntupleName
311 name.append("_nt_");
312 name.append(ntupleName);
313
314 // Add thread Id to a file name if MT processing
315 if ( ! G4Threading::IsMasterThread() ) {
316 std::ostringstream os;
318 name.append("_t");
319 name.append(os.str());
320 }
321
322 // Add file extension
323 auto extension = GetExtension(fileName, fileType);
324 if ( extension.size() ) {
325 name.append(".");
326 name.append(extension);
327 }
328
329 return name;
330}
331
332//_____________________________________________________________________________
334 const G4String& fileName,
335 const G4String& fileType,
336 G4int ntupleFileNumber)
337{
338// Compose and return the ntuple specific file name:
339// - add _mFN suffix to the file base name where FN = ntupleFileNumber
340// - add file extension if not present
341
342 auto name = GetBaseName(fileName);
343
344 // Add _M followed by ntupleFileNumber
345 std::ostringstream os;
346 os << ntupleFileNumber;
347 name.append("_m");
348 name.append(os.str());
349
350 // Add file extension
351 auto extension = GetExtension(fileName, fileType);
352 if ( extension.size() ) {
353 name.append(".");
354 name.append(extension);
355 }
356
357 return name;
358}
359
360//_____________________________________________________________________________
362 const G4String& fileName,
363 const G4String& fileType)
364{
365// Update file base name with the thread suffix:
366// - add _tN suffix if called on thread worker
367// - add file extension if not present
368
369 auto name = GetBaseName(fileName);
370
371 // Add thread Id to a file name if MT processing
372 if ( ! G4Threading::IsMasterThread() ) {
373 std::ostringstream os;
375 name.append("_t");
376 name.append(os.str());
377 }
378
379 // Add file extension
380 auto extension = GetExtension(fileName, fileType);
381 if ( extension.size() ) {
382 name.append(".");
383 name.append(extension);
384 }
385
386 return name;
387}
388
389//_____________________________________________________________________________
391{
392// Generate plot file name for an output file name
393
394 auto name = GetBaseName(fileName);
395
396 // Add .ps extension
397 name.append(".ps");
398
399 return name;
400}
401
402}
G4AnalysisOutput
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:40
double G4double
Definition: G4Types.hh:83
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
#define G4endl
Definition: G4ios.hh:57
static G4double GetValueOf(const G4String &)
G4BinScheme GetBinScheme(const G4String &binSchemeName)
Definition: G4BinScheme.cc:35
G4String GetExtension(const G4String &fileName, const G4String &defaultExtension="")
G4bool CheckMinMax(G4double xmin, G4double xmax, const G4String &fcnName="none", const G4String &binSchemeName="linear")
void Tokenize(const G4String &line, std::vector< G4String > &tokens)
G4String GetTnFileName(const G4String &fileName, const G4String &fileType)
G4bool CheckNbins(G4int nbins)
G4String GetPlotFileName(const G4String &fileName)
G4double GetUnitValue(const G4String &unit)
G4String GetOutputName(G4AnalysisOutput outputType)
G4AnalysisOutput GetOutput(const G4String &outputName, G4bool warn=true)
void UpdateTitle(G4String &title, const G4String &unitName, const G4String &fcnName)
G4bool CheckEdges(const std::vector< G4double > &edges)
G4String GetHnFileName(const G4String &fileName, const G4String &fileType, const G4String &hnType, const G4String &hnName)
G4String GetBaseName(const G4String &fileName)
G4bool CheckName(const G4String &name, const G4String &objectType)
G4String GetNtupleFileName(const G4String &fileName, const G4String &fileType, const G4String &ntupleName)
G4bool IsMasterThread()
Definition: G4Threading.cc:124
G4int G4GetThreadId()
Definition: G4Threading.cc:122