ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4AnalysisUtilities.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4AnalysisUtilities.cc
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 
29 #include "G4AnalysisUtilities.hh"
30 #include "G4BinScheme.hh"
31 #include "G4UnitsTable.hh"
32 #include "G4String.hh"
33 
34 namespace {
35 
36 //_____________________________________________________________________________
37 G4bool GetToken(const G4String& line, G4String& token,
38  std::string::size_type begIdx, std::string::size_type& endIdx)
39 {
40  while ( line[begIdx] == ' ') ++begIdx; // Loop checking, 23.06.2015, I. Hrivnacova
41  if ( line[begIdx] == '"' ) {
42  endIdx = line.find('"', begIdx+1);
43  if ( endIdx == std::string::npos ) endIdx = line.length();
44  token = line.substr(begIdx+1, (endIdx-1)-begIdx);
45  ++endIdx;
46  }
47  else {
48  endIdx = line.find(' ', begIdx);
49  if ( endIdx == std::string::npos ) endIdx = line.length();
50  token = line.substr(begIdx, endIdx-begIdx);
51  }
52  return ( token.length() > 0 );
53 }
54 
55 }
56 
57 namespace G4Analysis
58 {
59 
60 //_____________________________________________________________________________
62 {
63  if ( nbins <= 0 ) {
64  G4ExceptionDescription description;
65  description
66  << " Illegal value of number of bins: nbins <= 0" << G4endl;
67  G4Exception("G4VAnalysisManager::CheckNbins",
68  "Analysis_W013", JustWarning, description);
69  return false;
70  }
71  else
72  return true;
73 }
74 
75 
76 //_____________________________________________________________________________
78  const G4String& fcnName, const G4String& binSchemeName)
79 {
80  auto result = true;
81 
82  if ( xmax <= xmin ) {
83  G4ExceptionDescription description;
84  description
85  << " Illegal values of (xmin >= xmax)" << G4endl;
86  G4Exception("G4VAnalysisManager::CheckMinMax",
87  "Analysis_W013", JustWarning, description);
88 
89  result = false;
90  }
91 
92  if ( ( fcnName != "none" ) && ( binSchemeName != "linear" ) ) {
93  G4ExceptionDescription description;
94  description
95  << " Combining Function and Binning scheme is not supported."
96  << G4endl;
97  G4Exception("G4VAnalysisManager::CheckMinMax",
98  "Analysis_W013", JustWarning, description);
99 
100  result = false;
101  }
102 
103  if ( ( GetBinScheme(binSchemeName) == G4BinScheme::kLog ||
104  fcnName == "log" || fcnName == "log10" ) && ( xmin == 0 ) ) {
105  G4ExceptionDescription description;
106  description
107  << " Illegal value of (xmin = 0) with logarithmic function or binning"
108  << G4endl;
109  G4Exception("G4VAnalysisManager::CheckMinMax",
110  "Analysis_W013", JustWarning, description);
111 
112  result = false;
113  }
114 
115  return result;
116 }
117 
118 //_____________________________________________________________________________
119 G4bool CheckEdges(const std::vector<G4double>& edges)
120 {
121  if ( edges.size() <= 1 ) {
122  G4ExceptionDescription description;
123  description
124  << " Illegal edges vector (size <= 1)" << G4endl;
125  G4Exception("G4VAnalysisManager::CheckEdges",
126  "Analysis_W013", JustWarning, description);
127  return false;
128  }
129  else
130  return true;
131 
132 }
133 
134 //_____________________________________________________________________________
135 G4bool CheckName(const G4String& name, const G4String& objectType)
136 {
137  if ( ! name.size() ) {
138  G4ExceptionDescription description;
139  description
140  << " Empty " << objectType << " name is not allowed." << G4endl
141  << " " << objectType << " was not created." << G4endl;
142  G4Exception("G4VAnalysisManager::CheckName",
143  "Analysis_W013", JustWarning, description);
144  return false;
145  }
146  else
147  return true;
148 }
149 
150 //_____________________________________________________________________________
152 {
153  G4double value = 1.;
154  if ( unit != "none" ) {
155  value = G4UnitDefinition::GetValueOf(unit);
156  if ( value == 0. ) value = 1.;
157  }
158  return value;
159 }
160 
161 //_____________________________________________________________________________
163  const G4String& unitName,
164  const G4String& fcnName)
165 {
166  if ( fcnName != "none" ) { title += " "; title += fcnName; title += "("; }
167  if ( unitName != "none" ) { title += " ["; title += unitName; title += "]";}
168  if ( fcnName != "none" ) { title += ")"; }
169 }
170 
171 //_____________________________________________________________________________
172 void Tokenize(const G4String& line, std::vector<G4String>& tokens)
173 {
174  // Define start values
175  std::string::size_type begIdx = 0;
176  std::string::size_type endIdx = 0;
177  G4String token;
178 
179  do {
180  if ( GetToken(line, token, begIdx, endIdx) ) {
181  //G4cout << "got token: '" << token << "'" << G4endl;
182  //G4cout << "beg, end: " << begIdx << ", " << endIdx << G4endl;
183  tokens.push_back(token);
184  }
185  begIdx = endIdx + 1;
186  }
187  while ( endIdx < line.length() ); // Loop checking, 23.06.2015, I. Hrivnacova
188 }
189 
190 //_____________________________________________________________________________
191 G4AnalysisOutput GetOutput(const G4String& outputName, G4bool warn) {
192  if ( outputName == "csv" ) { return G4AnalysisOutput::kCsv; }
193 #ifdef TOOLS_USE_HDF5
194  else if ( outputName == "hdf5" ) { return G4AnalysisOutput::kHdf5; }
195 #endif
196  else if ( outputName == "root" ) { return G4AnalysisOutput::kRoot; }
197  else if ( outputName == "xml" ) { return G4AnalysisOutput::kXml; }
198  else if ( outputName == "none" ) { return G4AnalysisOutput::kNone; }
199  else {
200  if (warn) {
201  G4ExceptionDescription description;
202  description
203  << " \"" << outputName << "\" output type is not supported." << G4endl;
204  G4Exception("G4Analysis::GetOutputType",
205  "Analysis_W013", JustWarning, description);
206  }
207  return G4AnalysisOutput::kNone;
208  }
209 }
210 
211 //_____________________________________________________________________________
213  switch ( output ) {
214  case G4AnalysisOutput::kCsv:
215  return "csv";
216  break;
217 #ifdef TOOLS_USE_HDF5
218  case G4AnalysisOutput::kHdf5:
219  return "hdf5";
220  break;
221 #endif
222  case G4AnalysisOutput::kRoot:
223  return "root";
224  break;
225  case G4AnalysisOutput::kXml:
226  return "xml";
227  break;
228  case G4AnalysisOutput::kNone:
229  return "none";
230  break;
231  }
232  // should never reach this line
233  G4ExceptionDescription description;
234  description
235  << " \"" << static_cast<int>(output) << "\" is not handled." << G4endl
236  << " " << "none type will be used.";
237  G4Exception("G4Analysis::GetOutputName",
238  "Analysis_W013", JustWarning, description);
239  return "none";
240 }
241 
242 }