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