Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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: G4UIcmdWithADoubleAndUnit.cc 77308 2013-11-22 11:22:28Z gcosmo $
28 //
29 //
30 
32 #include "G4Tokenizer.hh"
33 #include "G4UnitsTable.hh"
34 #include "G4UIcommandStatus.hh"
35 #include <sstream>
36 #include <vector>
37 
39 (const char * theCommandPath,G4UImessenger * theMessenger)
40 :G4UIcommand(theCommandPath,theMessenger)
41 {
42  G4UIparameter * dblParam = new G4UIparameter('d');
43  SetParameter(dblParam);
44  G4UIparameter * untParam = new G4UIparameter('s');
45  SetParameter(untParam);
46  untParam->SetParameterName("Unit");
47 }
48 
50 {
51  std::vector<G4String> token_vector;
52  G4Tokenizer tkn(parameterList);
53  G4String str;
54  while( (str = tkn()) != "" ) {
55  token_vector.push_back(str);
56  }
57 
58  // convert a value in default unit
59  G4String converted_parameter;
60  G4String default_unit = GetParameter(1)-> GetDefaultValue();
61  if (default_unit != "" && token_vector.size() >= 2) {
62  if(CategoryOf(token_vector[1])!=CategoryOf(default_unit))
63  { return fParameterOutOfCandidates+1; }
64  G4double value_given = ValueOf(token_vector[1]);
65  G4double value_default = ValueOf(default_unit);
66  G4double value = ConvertToDouble(token_vector[0])
67  * value_given / value_default;
68  // reconstruct parameter list
69  converted_parameter += ConvertToString(value);
70  converted_parameter += " ";
71  converted_parameter += default_unit;
72  for ( size_t i=2 ; i< token_vector.size(); i++) {
73  converted_parameter += " ";
74  converted_parameter += token_vector[i];
75  }
76  } else {
77  converted_parameter = parameterList;
78  }
79 
80  return G4UIcommand::DoIt(converted_parameter);
81 }
82 
84 {
85  return ConvertToDimensionedDouble(paramString);
86 }
87 
89 {
90  G4double vl;
91  char unts[30];
92 
93  std::istringstream is(paramString);
94  is >> vl >> unts;
95 
96  return vl;
97 }
98 
100 {
101  G4double vl;
102  char unts[30];
103 
104  std::istringstream is(paramString);
105  is >> vl >> unts;
106  G4String unt = unts;
107 
108  return ValueOf(unt);
109 }
110 
112 {
113  G4UIparameter* unitParam = GetParameter(1);
114  G4String canList = unitParam->GetParameterCandidates();
115  G4Tokenizer candidateTokenizer(canList);
116  G4String aToken = candidateTokenizer();
117  std::ostringstream os;
118  os << G4BestUnit(val,CategoryOf(aToken));
119 
120  G4String st = os.str();
121  return st;
122 }
123 
125 {
126  G4UIparameter* unitParam = GetParameter(1);
127  G4String st;
128  if(unitParam->IsOmittable())
129  { st = ConvertToString(val,unitParam->GetDefaultValue()); }
130  else
131  { st = ConvertToStringWithBestUnit(val); }
132  return st;
133 }
134 
136 (const char * theName,G4bool omittable,G4bool currentAsDefault)
137 {
138  G4UIparameter * theParam = GetParameter(0);
139  theParam->SetParameterName(theName);
140  theParam->SetOmittable(omittable);
141  theParam->SetCurrentAsDefault(currentAsDefault);
142 }
143 
145 {
146  G4UIparameter * theParam = GetParameter(0);
147  theParam->SetDefaultValue(defVal);
148 }
149 
150 void G4UIcmdWithADoubleAndUnit::SetUnitCategory(const char * unitCategory)
151 {
152  SetUnitCandidates(UnitsList(unitCategory));
153 }
154 
155 void G4UIcmdWithADoubleAndUnit::SetUnitCandidates(const char * candidateList)
156 {
157  G4UIparameter * untParam = GetParameter(1);
158  G4String canList = candidateList;
159  untParam->SetParameterCandidates(canList);
160 }
161 
163 {
164  G4UIparameter * untParam = GetParameter(1);
165  untParam->SetOmittable(true);
166  untParam->SetDefaultValue(defUnit);
167  SetUnitCategory(CategoryOf(defUnit));
168 }
169 
G4String GetParameterCandidates() const
void SetOmittable(G4bool om)
void SetParameterCandidates(const char *theString)
void SetDefaultValue(const char *theDefaultValue)
static G4String ConvertToString(G4bool boolVal)
Definition: G4UIcommand.cc:357
#define G4BestUnit(a, b)
#define G4_USE_G4BESTUNIT_FOR_VERBOSE 1
void SetUnitCategory(const char *unitCategory)
void SetParameterName(const char *theName)
static G4double GetNewDoubleValue(const char *paramString)
int G4int
Definition: G4Types.hh:78
static G4double ConvertToDimensionedDouble(const char *st)
Definition: G4UIcommand.cc:437
static G4double GetNewDoubleRawValue(const char *paramString)
void SetCurrentAsDefault(G4bool val)
G4bool IsOmittable() const
static G4String UnitsList(const char *unitCategory)
Definition: G4UIcommand.cc:306
G4UIcmdWithADoubleAndUnit(const char *theCommandPath, G4UImessenger *theMessenger)
bool G4bool
Definition: G4Types.hh:79
G4UIparameter * GetParameter(G4int i) const
Definition: G4UIcommand.hh:145
static G4double ConvertToDouble(const char *st)
Definition: G4UIcommand.cc:429
G4String ConvertToStringWithDefaultUnit(G4double val)
G4String GetDefaultValue() const
static G4double ValueOf(const char *unitName)
Definition: G4UIcommand.cc:294
void SetDefaultUnit(const char *defUnit)
const XML_Char int const XML_Char * value
double G4double
Definition: G4Types.hh:76
virtual G4int DoIt(G4String parameterList)
G4String ConvertToStringWithBestUnit(G4double val)
static G4String CategoryOf(const char *unitName)
Definition: G4UIcommand.cc:301
void SetParameterName(const char *theName, G4bool omittable, G4bool currentAsDefault=false)
virtual G4int DoIt(G4String parameterList)
Definition: G4UIcommand.cc:108
static G4double GetNewUnitValue(const char *paramString)
void SetUnitCandidates(const char *candidateList)