Geant4  9.6.p02
 All Classes 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$
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 
147 void G4UIcmdWithADoubleAndUnit::SetUnitCategory(const char * unitCategory)
148 {
149  SetUnitCandidates(UnitsList(unitCategory));
150 }
151 
152 void 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