Geant4 9.6.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//
27// $Id$
28//
29//
30
32#include "G4Tokenizer.hh"
33#include "G4UnitsTable.hh"
34#include <sstream>
35#include <vector>
36
38(const char * theCommandPath,G4UImessenger * theMessenger)
39:G4UIcommand(theCommandPath,theMessenger)
40{
41 G4UIparameter * dblParam = new G4UIparameter('d');
42 SetParameter(dblParam);
43 G4UIparameter * untParam = new G4UIparameter('s');
44 SetParameter(untParam);
45 untParam->SetParameterName("Unit");
46}
47
49{
50 std::vector<G4String> token_vector;
51 G4Tokenizer tkn(parameterList);
52 G4String str;
53 while( (str = tkn()) != "" ) {
54 token_vector.push_back(str);
55 }
56
57 // convert a value in default unit
58 G4String converted_parameter;
59 G4String default_unit = GetParameter(1)-> GetDefaultValue();
60 if (default_unit != "" && token_vector.size() >= 2) {
61 G4double value_given = ValueOf(token_vector[1]);
62 G4double value_default = ValueOf(default_unit);
63 G4double value = ConvertToDouble(token_vector[0])
64 * value_given / value_default;
65 // reconstruct parameter list
66 converted_parameter += ConvertToString(value);
67 converted_parameter += " ";
68 converted_parameter += default_unit;
69 for ( size_t i=2 ; i< token_vector.size(); i++) {
70 converted_parameter += " ";
71 converted_parameter += token_vector[i];
72 }
73 } else {
74 converted_parameter = parameterList;
75 }
76
77 return G4UIcommand::DoIt(converted_parameter);
78}
79
81{
82 return ConvertToDimensionedDouble(paramString);
83}
84
86{
87 G4double vl;
88 char unts[30];
89
90 std::istringstream is(paramString);
91 is >> vl >> unts;
92
93 return vl;
94}
95
97{
98 G4double vl;
99 char unts[30];
100
101 std::istringstream is(paramString);
102 is >> vl >> unts;
103 G4String unt = unts;
104
105 return ValueOf(unt);
106}
107
109{
110 G4UIparameter* unitParam = GetParameter(1);
111 G4String canList = unitParam->GetParameterCandidates();
112 G4Tokenizer candidateTokenizer(canList);
113 G4String aToken = candidateTokenizer();
114 std::ostringstream os;
115 os << G4BestUnit(val,CategoryOf(aToken));
116
117 G4String st = os.str();
118 return st;
119}
120
122{
123 G4UIparameter* unitParam = GetParameter(1);
124 G4String st;
125 if(unitParam->IsOmittable())
126 { st = ConvertToString(val,unitParam->GetDefaultValue()); }
127 else
128 { st = ConvertToStringWithBestUnit(val); }
129 return st;
130}
131
133(const char * theName,G4bool omittable,G4bool currentAsDefault)
134{
135 G4UIparameter * theParam = GetParameter(0);
136 theParam->SetParameterName(theName);
137 theParam->SetOmittable(omittable);
138 theParam->SetCurrentAsDefault(currentAsDefault);
139}
140
142{
143 G4UIparameter * theParam = GetParameter(0);
144 theParam->SetDefaultValue(defVal);
145}
146
147void G4UIcmdWithADoubleAndUnit::SetUnitCategory(const char * unitCategory)
148{
149 SetUnitCandidates(UnitsList(unitCategory));
150}
151
152void G4UIcmdWithADoubleAndUnit::SetUnitCandidates(const char * candidateList)
153{
154 G4UIparameter * untParam = GetParameter(1);
155 G4String canList = candidateList;
156 untParam->SetParameterCandidates(canList);
157}
158
160{
161 G4UIparameter * untParam = GetParameter(1);
162 untParam->SetOmittable(true);
163 untParam->SetDefaultValue(defUnit);
164 SetUnitCategory(CategoryOf(defUnit));
165}
166
#define G4BestUnit(a, b)
#define G4_USE_G4BESTUNIT_FOR_VERBOSE 1
double G4double
Definition: G4Types.hh:64
int G4int
Definition: G4Types.hh:66
bool G4bool
Definition: G4Types.hh:67
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:140
static G4String CategoryOf(const char *unitName)
Definition: G4UIcommand.cc:295
static G4double ValueOf(const char *unitName)
Definition: G4UIcommand.cc:288
virtual G4int DoIt(G4String parameterList)
Definition: G4UIcommand.cc:104
static G4String ConvertToString(G4bool boolVal)
Definition: G4UIcommand.cc:349
void SetParameter(G4UIparameter *const newParameter)
Definition: G4UIcommand.hh:147
static G4String UnitsList(const char *unitCategory)
Definition: G4UIcommand.cc:300
static G4double ConvertToDouble(const char *st)
Definition: G4UIcommand.cc:421
static G4double ConvertToDimensionedDouble(const char *st)
Definition: G4UIcommand.cc:429
void SetDefaultValue(const char *theDefaultValue)
G4String GetDefaultValue() const
G4bool IsOmittable() const
void SetOmittable(G4bool om)
void SetParameterName(const char *theName)
G4String GetParameterCandidates() const
void SetParameterCandidates(const char *theString)
void SetCurrentAsDefault(G4bool val)