Geant4-11
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 untParam->SetParameterName("Unit");
47 SetParameter(untParam);
49}
50
51// --------------------------------------------------------------------
53{
54 std::vector<G4String> token_vector;
55 G4Tokenizer tkn(parameterList);
56 G4String str;
57 while((str = tkn()) != "")
58 {
59 token_vector.push_back(str);
60 }
61
62 // convert a value in default unit
63 G4String converted_parameter;
64 G4String default_unit = GetParameter(1)->GetDefaultValue();
65 if(default_unit != "" && token_vector.size() >= 2)
66 {
67 if(CategoryOf(token_vector[1]) != CategoryOf(default_unit))
68 {
70 }
71 G4double value_given = ValueOf(token_vector[1]);
72 G4double value_default = ValueOf(default_unit);
73 G4double value =
74 ConvertToDouble(token_vector[0]) * value_given / value_default;
75 // reconstruct parameter list
76 converted_parameter += ConvertToString(value);
77 converted_parameter += " ";
78 converted_parameter += default_unit;
79 for(std::size_t i = 2; i < token_vector.size(); ++i)
80 {
81 converted_parameter += " ";
82 converted_parameter += token_vector[i];
83 }
84 }
85 else
86 {
87 converted_parameter = parameterList;
88 }
89
90 return G4UIcommand::DoIt(converted_parameter);
91}
92
93// --------------------------------------------------------------------
95{
96 return ConvertToDimensionedDouble(paramString);
97}
98
99// --------------------------------------------------------------------
101 const char* paramString)
102{
103 G4double vl;
104 char unts[30];
105
106 std::istringstream is(paramString);
107 is >> vl >> unts;
108
109 return vl;
110}
111
112// --------------------------------------------------------------------
114{
115 G4double vl;
116 char unts[30];
117
118 std::istringstream is(paramString);
119 is >> vl >> unts;
120 G4String unt = unts;
121
122 return ValueOf(unt);
123}
124
125// --------------------------------------------------------------------
127{
128 G4UIparameter* unitParam = GetParameter(1);
129 G4String canList = unitParam->GetParameterCandidates();
130 G4Tokenizer candidateTokenizer(canList);
131 G4String aToken = candidateTokenizer();
132 std::ostringstream os;
133 os << G4BestUnit(val, CategoryOf(aToken));
134
135 G4String st = os.str();
136 return st;
137}
138
139// --------------------------------------------------------------------
141{
142 G4UIparameter* unitParam = GetParameter(1);
143 G4String st;
144 if(unitParam->IsOmittable())
145 {
146 st = ConvertToString(val, unitParam->GetDefaultValue());
147 }
148 else
149 {
151 }
152 return st;
153}
154
155// --------------------------------------------------------------------
157 G4bool omittable,
158 G4bool currentAsDefault)
159{
160 G4UIparameter* theParam = GetParameter(0);
161 theParam->SetParameterName(theName);
162 theParam->SetOmittable(omittable);
163 theParam->SetCurrentAsDefault(currentAsDefault);
164}
165
166// --------------------------------------------------------------------
168{
169 G4UIparameter* theParam = GetParameter(0);
170 theParam->SetDefaultValue(defVal);
171}
172
173// --------------------------------------------------------------------
174void G4UIcmdWithADoubleAndUnit::SetUnitCategory(const char* unitCategory)
175{
176 SetUnitCandidates(UnitsList(unitCategory));
177}
178
179// --------------------------------------------------------------------
180void G4UIcmdWithADoubleAndUnit::SetUnitCandidates(const char* candidateList)
181{
182 G4UIparameter* untParam = GetParameter(1);
183 G4String canList = candidateList;
184 untParam->SetParameterCandidates(canList);
185}
186
187// --------------------------------------------------------------------
189{
190 G4UIparameter* untParam = GetParameter(1);
191 untParam->SetOmittable(true);
192 untParam->SetDefaultValue(defUnit);
193 SetUnitCategory(CategoryOf(defUnit));
194}
#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:371
static G4double ValueOf(const char *unitName)
Definition: G4UIcommand.cc:363
void SetCommandType(CommandType)
Definition: G4UIcommand.cc:94
virtual G4int DoIt(G4String parameterList)
Definition: G4UIcommand.cc:151
static G4String ConvertToString(G4bool boolVal)
Definition: G4UIcommand.cc:445
void SetParameter(G4UIparameter *const newParameter)
Definition: G4UIcommand.hh:146
@ WithADoubleAndUnitCmd
Definition: G4UIcommand.hh:201
static G4String UnitsList(const char *unitCategory)
Definition: G4UIcommand.cc:377
static G4double ConvertToDouble(const char *st)
Definition: G4UIcommand.cc:575
static G4double ConvertToDimensionedDouble(const char *st)
Definition: G4UIcommand.cc:584
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