Geant4-11
G4UIcmdWith3VectorAndUnit.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// G4UIcmdWith3VectorAndUnit
27//
28// Author: M.Asai, 1998
29// --------------------------------------------------------------------
30
32#include "G4Tokenizer.hh"
33#include "G4UnitsTable.hh"
34#include "G4UIcommandStatus.hh"
35#include <sstream>
36
37// --------------------------------------------------------------------
39 const char* theCommandPath, G4UImessenger* theMessenger)
40 : G4UIcommand(theCommandPath, theMessenger)
41{
42 G4UIparameter* dblParamX = new G4UIparameter('d');
43 SetParameter(dblParamX);
44 G4UIparameter* dblParamY = new G4UIparameter('d');
45 SetParameter(dblParamY);
46 G4UIparameter* dblParamZ = new G4UIparameter('d');
47 SetParameter(dblParamZ);
48 G4UIparameter* untParam = new G4UIparameter('s');
49 untParam->SetParameterName("Unit");
50 SetParameter(untParam);
52}
53
54// --------------------------------------------------------------------
56{
57 std::vector<G4String> token_vector;
58 G4Tokenizer tkn(parameterList);
59 G4String str;
60 while((str = tkn()) != "")
61 {
62 token_vector.push_back(str);
63 }
64
65 // convert a value in default unit
66 G4String converted_parameter;
67 G4String default_unit = GetParameter(3)->GetDefaultValue();
68 if(default_unit != "" && token_vector.size() >= 4)
69 {
70 if(CategoryOf(token_vector[3]) != CategoryOf(default_unit))
71 {
73 }
74 G4double value_given = ValueOf(token_vector[3]);
75 G4double value_default = ValueOf(default_unit);
76 G4double x = ConvertToDouble(token_vector[0]) * value_given / value_default;
77 G4double y = ConvertToDouble(token_vector[1]) * value_given / value_default;
78 G4double z = ConvertToDouble(token_vector[2]) * value_given / value_default;
79
80 // reconstruct parameter list
81 converted_parameter += ConvertToString(x);
82 converted_parameter += " ";
83 converted_parameter += ConvertToString(y);
84 converted_parameter += " ";
85 converted_parameter += ConvertToString(z);
86 converted_parameter += " ";
87 converted_parameter += default_unit;
88 for(std::size_t i = 4; i < token_vector.size(); ++i)
89 {
90 converted_parameter += " ";
91 converted_parameter += token_vector[i];
92 }
93 }
94 else
95 {
96 converted_parameter = parameterList;
97 }
98
99 return G4UIcommand::DoIt(converted_parameter);
100}
101
102// --------------------------------------------------------------------
104 const char* paramString)
105{
106 return ConvertToDimensioned3Vector(paramString);
107}
108
109// --------------------------------------------------------------------
111 const char* paramString)
112{
113 G4double vx;
114 G4double vy;
115 G4double vz;
116 char unts[30];
117 std::istringstream is(paramString);
118 is >> vx >> vy >> vz >> unts;
119 return G4ThreeVector(vx, vy, vz);
120}
121
122// --------------------------------------------------------------------
124{
125 G4double vx;
126 G4double vy;
127 G4double vz;
128 char unts[30];
129 std::istringstream is(paramString);
130 is >> vx >> vy >> vz >> unts;
131 G4String unt = unts;
132 return ValueOf(unt);
133}
134
135// --------------------------------------------------------------------
137 G4ThreeVector vec)
138{
139 G4UIparameter* unitParam = GetParameter(3);
140 G4String canList = unitParam->GetParameterCandidates();
141 G4Tokenizer candidateTokenizer(canList);
142 G4String aToken = candidateTokenizer();
143
144 std::ostringstream os;
145 os << G4BestUnit(vec, CategoryOf(aToken));
146 G4String st = os.str();
147
148 return st;
149}
150
151// --------------------------------------------------------------------
153 G4ThreeVector vec)
154{
155 G4UIparameter* unitParam = GetParameter(3);
156 G4String st;
157 if(unitParam->IsOmittable())
158 {
159 st = ConvertToString(vec, unitParam->GetDefaultValue());
160 }
161 else
162 {
164 }
165 return st;
166}
167
168// --------------------------------------------------------------------
170 const char* theNameY,
171 const char* theNameZ,
172 G4bool omittable,
173 G4bool currentAsDefault)
174{
175 G4UIparameter* theParamX = GetParameter(0);
176 theParamX->SetParameterName(theNameX);
177 theParamX->SetOmittable(omittable);
178 theParamX->SetCurrentAsDefault(currentAsDefault);
179 G4UIparameter* theParamY = GetParameter(1);
180 theParamY->SetParameterName(theNameY);
181 theParamY->SetOmittable(omittable);
182 theParamY->SetCurrentAsDefault(currentAsDefault);
183 G4UIparameter* theParamZ = GetParameter(2);
184 theParamZ->SetParameterName(theNameZ);
185 theParamZ->SetOmittable(omittable);
186 theParamZ->SetCurrentAsDefault(currentAsDefault);
187}
188
189// --------------------------------------------------------------------
191{
192 G4UIparameter* theParamX = GetParameter(0);
193 theParamX->SetDefaultValue(vec.x());
194 G4UIparameter* theParamY = GetParameter(1);
195 theParamY->SetDefaultValue(vec.y());
196 G4UIparameter* theParamZ = GetParameter(2);
197 theParamZ->SetDefaultValue(vec.z());
198}
199
200// --------------------------------------------------------------------
201void G4UIcmdWith3VectorAndUnit::SetUnitCategory(const char* unitCategory)
202{
203 SetUnitCandidates(UnitsList(unitCategory));
204}
205
206// --------------------------------------------------------------------
207void G4UIcmdWith3VectorAndUnit::SetUnitCandidates(const char* candidateList)
208{
209 G4UIparameter* untParam = GetParameter(3);
210 G4String canList = candidateList;
211 untParam->SetParameterCandidates(canList);
212}
213
214// --------------------------------------------------------------------
216{
217 G4UIparameter* untParam = GetParameter(3);
218 untParam->SetOmittable(true);
219 untParam->SetDefaultValue(defUnit);
220 SetUnitCategory(CategoryOf(defUnit));
221}
#define G4BestUnit(a, b)
CLHEP::Hep3Vector G4ThreeVector
double G4double
Definition: G4Types.hh:83
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
@ fParameterOutOfCandidates
double z() const
double x() const
double y() const
G4String ConvertToStringWithDefaultUnit(G4ThreeVector vec)
void SetDefaultUnit(const char *defUnit)
G4String ConvertToStringWithBestUnit(G4ThreeVector vec)
static G4ThreeVector GetNew3VectorValue(const char *paramString)
void SetParameterName(const char *theNameX, const char *theNameY, const char *theNameZ, G4bool omittable, G4bool currentAsDefault=false)
static G4double GetNewUnitValue(const char *paramString)
void SetUnitCandidates(const char *candidateList)
void SetUnitCategory(const char *unitCategory)
static G4ThreeVector GetNew3VectorRawValue(const char *paramString)
G4UIcmdWith3VectorAndUnit(const char *theCommandPath, G4UImessenger *theMessenger)
void SetDefaultValue(G4ThreeVector defVal)
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
@ With3VectorAndUnitCmd
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 G4ThreeVector ConvertToDimensioned3Vector(const char *st)
Definition: G4UIcommand.cc:608
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