ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4ConversionUtils.hh
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4ConversionUtils.hh
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 // Jane Tinslay September 2006
28 //
29 // Conversion utility functions.
30 //
31 #ifndef G4CONVERSIONUTILS_HH
32 #define G4CONVERSIONUTILS_HH
33 
34 #include "globals.hh"
35 #include "G4DimensionedDouble.hh"
37 #include <sstream>
38 
39 namespace G4ConversionUtils
40 {
41  // Generic single value istringstream conversion.
42  // Returns false if conversion failed or if extra characters
43  // exist in input.
44  template <typename Value>
45  G4bool Convert(const G4String& myInput, Value& output)
46  {
47  G4String input(myInput);
48  input = input.strip();
49 
50  std::istringstream is(input);
51  char tester;
52 
53  return ((is >> output) && !is.get(tester));
54  }
55 
56  // Conversion specialisations.
57  template<>
58  inline G4bool Convert(const G4String& myInput, G4DimensionedDouble& output)
59  {
60  G4String input(myInput);
61  input = input.strip();
62 
64  G4String unit;
65 
66  std::istringstream is(input);
67  char tester;
68 
69  if (!(is >> value >> unit) || is.get(tester)) return false;
70 
71  output = G4DimensionedDouble(value, unit);
72 
73  return true;
74  }
75 
76  template<> inline G4bool Convert(const G4String& myInput,
77  G4DimensionedThreeVector& output)
78  {
79  G4String input(myInput);
80  input = input.strip();
81 
82  G4double value1, value2, value3;
83  G4String unit;
84 
85  std::istringstream is(input);
86  char tester;
87 
88  if (!(is >> value1 >> value2 >> value3 >>unit) || is.get(tester)) return false;
89 
90  output = G4DimensionedThreeVector(G4ThreeVector(value1, value2, value3), unit);
91 
92  return true;
93  }
94 
95  template<> inline G4bool Convert(const G4String& myInput, G4ThreeVector& output)
96  {
97  G4String input(myInput);
98  input = input.strip();
99 
100  G4double value1, value2, value3;
101 
102  std::istringstream is(input);
103  char tester;
104 
105  if (!(is >> value1 >> value2 >> value3) || is.get(tester)) return false;
106  output = G4ThreeVector(value1, value2, value3);
107 
108  return true;
109  }
110 
111  // Generic double value istringstream conversion.
112  // Return false if conversion failed or if extra characters
113  // exist in input.
114  template <typename Value> G4bool Convert(const G4String& myInput, Value& value1,
115  Value& value2)
116  {
117  G4String input(myInput);
118  input = input.strip();
119 
120  std::istringstream is(input);
121  char tester;
122 
123  return ((is >> value1 >> value2) && (!is.get(tester)));
124  }
125 
126  // Conversion specialisations.
127  template<> inline G4bool Convert(const G4String& myInput, G4DimensionedDouble& min,
129  {
130  G4String input(myInput);
131  input = input.strip();
132 
133  G4double valueMin, valueMax;
134  G4String unitsMin, unitsMax;
135 
136  std::istringstream is(input);
137  char tester;
138 
139  if (!(is >> valueMin >> unitsMin >> valueMax >> unitsMax) || is.get(tester)) return false;;
140 
141  min = G4DimensionedDouble(valueMin, unitsMin);
142  max = G4DimensionedDouble(valueMax, unitsMax);
143 
144  return true;
145  }
146 
147  template<> inline G4bool Convert(const G4String& myInput, G4DimensionedThreeVector& min,
149  {
150  G4String input(myInput);
151  input = input.strip();
152 
153  G4double valueMinX, valueMinY, valueMinZ;
154  G4double valueMaxX, valueMaxY, valueMaxZ;
155  G4String unitMin, unitMax;
156 
157  std::istringstream is(input);
158  char tester;
159 
160  if (!(is >> valueMinX >> valueMinY >> valueMinZ >> unitMin >> valueMaxX >> valueMaxY >> valueMaxZ >> unitMax)
161  || is.get(tester)) return false;
162 
163  min = G4DimensionedThreeVector(G4ThreeVector(valueMinX, valueMinY, valueMinZ), unitMin);
164  max = G4DimensionedThreeVector(G4ThreeVector(valueMaxX, valueMaxY, valueMaxZ), unitMax);
165 
166  return true;
167  }
168 
169  template<> inline G4bool Convert(const G4String& myInput, G4ThreeVector& min,
170  G4ThreeVector& max)
171  {
172  G4String input(myInput);
173  input = input.strip();
174 
175  G4double valueMinX, valueMinY, valueMinZ;
176  G4double valueMaxX, valueMaxY, valueMaxZ;
177 
178  std::istringstream is(input);
179  char tester;
180 
181  if (!(is >> valueMinX >> valueMinY >> valueMinZ >> valueMaxX >> valueMaxY >> valueMaxZ)
182  || is.get(tester)) return false;
183 
184  min = G4ThreeVector(valueMinX, valueMinY, valueMinZ);
185  max = G4ThreeVector(valueMaxX, valueMaxY, valueMaxZ);
186 
187  return true;
188  }
189 
190 }
191 
192 #endif