Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4UIcmdWithADoubleAndUnit.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// G4UIcmdWithADoubleAndUnit
27//
28// Author: M.Asai, 1998
29// --------------------------------------------------------------------
30
32#include "G4Tokenizer.hh"
33#include "G4UnitsTable.hh"
34#include "G4UIcommandStatus.hh"
35#include <sstream>
36#include <vector>
37
38// --------------------------------------------------------------------
40 const char* theCommandPath, G4UImessenger* theMessenger)
41 : G4UIcommand(theCommandPath, theMessenger)
42{
43 G4UIparameter* dblParam = new G4UIparameter('d');
44 SetParameter(dblParam);
45 G4UIparameter* untParam = new G4UIparameter('s');
46 SetParameter(untParam);
47 untParam->SetParameterName("Unit");
48}
49
50// --------------------------------------------------------------------
52{
53 std::vector<G4String> token_vector;
54 G4Tokenizer tkn(parameterList);
55 G4String str;
56 while((str = tkn()) != "")
57 {
58 token_vector.push_back(str);
59 }
60
61 // convert a value in default unit
62 G4String converted_parameter;
63 G4String default_unit = GetParameter(1)->GetDefaultValue();
64 if(default_unit != "" && token_vector.size() >= 2)
65 {
66 if(CategoryOf(token_vector[1]) != CategoryOf(default_unit))
67 {
69 }
70 G4double value_given = ValueOf(token_vector[1]);
71 G4double value_default = ValueOf(default_unit);
72 G4double value =
73 ConvertToDouble(token_vector[0]) * value_given / value_default;
74 // reconstruct parameter list
75 converted_parameter += ConvertToString(value);
76 converted_parameter += " ";
77 converted_parameter += default_unit;
78 for(std::size_t i = 2; i < token_vector.size(); ++i)
79 {
80 converted_parameter += " ";
81 converted_parameter += token_vector[i];
82 }
83 }
84 else
85 {
86 converted_parameter = parameterList;
87 }
88
89 return G4UIcommand::DoIt(converted_parameter);
90}
91
92// --------------------------------------------------------------------
94{
95 return ConvertToDimensionedDouble(paramString);
96}
97
98// --------------------------------------------------------------------
100 const char* paramString)
101{
102 G4double vl;
103 char unts[30];
104
105 std::istringstream is(paramString);
106 is >> vl >> unts;
107
108 return vl;
109}
110
111// --------------------------------------------------------------------
113{
114 G4double vl;
115 char unts[30];
116
117 std::istringstream is(paramString);
118 is >> vl >> unts;
119 G4String unt = unts;
120
121 return ValueOf(unt);
122}
123
124// --------------------------------------------------------------------
126{
127 G4UIparameter* unitParam = GetParameter(1);
128 G4String canList = unitParam->GetParameterCandidates();
129 G4Tokenizer candidateTokenizer(canList);
130 G4String aToken = candidateTokenizer();
131 std::ostringstream os;
132 os << G4BestUnit(val, CategoryOf(aToken));
133
134 G4String st = os.str();
135 return st;
136}
137
138// --------------------------------------------------------------------
140{
141 G4UIparameter* unitParam = GetParameter(1);
142 G4String st;
143 if(unitParam->IsOmittable())
144 {
145 st = ConvertToString(val, unitParam->GetDefaultValue());
146 }
147 else
148 {
150 }
151 return st;
152}
153
154// --------------------------------------------------------------------
156 G4bool omittable,
157 G4bool currentAsDefault)
158{
159 G4UIparameter* theParam = GetParameter(0);
160 theParam->SetParameterName(theName);
161 theParam->SetOmittable(omittable);
162 theParam->SetCurrentAsDefault(currentAsDefault);
163}
164
165// --------------------------------------------------------------------
167{
168 G4UIparameter* theParam = GetParameter(0);
169 theParam->SetDefaultValue(defVal);
170}
171
172// --------------------------------------------------------------------
173void G4UIcmdWithADoubleAndUnit::SetUnitCategory(const char* unitCategory)
174{
175 SetUnitCandidates(UnitsList(unitCategory));
176}
177
178// --------------------------------------------------------------------
179void G4UIcmdWithADoubleAndUnit::SetUnitCandidates(const char* candidateList)
180{
181 G4UIparameter* untParam = GetParameter(1);
182 G4String canList = candidateList;
183 untParam->SetParameterCandidates(canList);
184}
185
186// --------------------------------------------------------------------
188{
189 G4UIparameter* untParam = GetParameter(1);
190 untParam->SetOmittable(true);
191 untParam->SetDefaultValue(defUnit);
192 SetUnitCategory(CategoryOf(defUnit));
193}
#define G4BestUnit(a, b)
double G4double
Definition: G4Types.hh:83
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
@ fParameterOutOfCandidates
void SetUnitCandidates(const char *candidateList)
G4String ConvertToStringWithDefaultUnit(G4double val)
void SetUnitCategory(const char *unitCategory)
static G4double GetNewUnitValue(const char *paramString)
void SetDefaultUnit(const char *defUnit)
static G4double GetNewDoubleValue(const char *paramString)
G4UIcmdWithADoubleAndUnit(const char *theCommandPath, G4UImessenger *theMessenger)
static G4double GetNewDoubleRawValue(const char *paramString)
void SetParameterName(const char *theName, G4bool omittable, G4bool currentAsDefault=false)
G4String ConvertToStringWithBestUnit(G4double val)
virtual G4int DoIt(G4String parameterList)
G4UIparameter * GetParameter(G4int i) const
Definition: G4UIcommand.hh:139
static G4String CategoryOf(const char *unitName)
Definition: G4UIcommand.cc:356
static G4double ValueOf(const char *unitName)
Definition: G4UIcommand.cc:348
virtual G4int DoIt(G4String parameterList)
Definition: G4UIcommand.cc:135
static G4String ConvertToString(G4bool boolVal)
Definition: G4UIcommand.cc:430
void SetParameter(G4UIparameter *const newParameter)
Definition: G4UIcommand.hh:146
static G4String UnitsList(const char *unitCategory)
Definition: G4UIcommand.cc:362
static G4double ConvertToDouble(const char *st)
Definition: G4UIcommand.cc:561
static G4double ConvertToDimensionedDouble(const char *st)
Definition: G4UIcommand.cc:570
const G4String & GetParameterCandidates() const
void SetDefaultValue(const char *theDefaultValue)
void SetParameterName(const char *pName)
G4bool IsOmittable() const
void SetOmittable(G4bool om)
void SetParameterCandidates(const char *theString)
void SetCurrentAsDefault(G4bool val)
const G4String & GetDefaultValue() const