ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4UIcmdWithADoubleAndUnit.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4UIcmdWithADoubleAndUnit.cc
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 //
28 //
29 
31 #include "G4Tokenizer.hh"
32 #include "G4UnitsTable.hh"
33 #include "G4UIcommandStatus.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  if(CategoryOf(token_vector[1])!=CategoryOf(default_unit))
62  { return fParameterOutOfCandidates+1; }
63  G4double value_given = ValueOf(token_vector[1]);
64  G4double value_default = ValueOf(default_unit);
65  G4double value = ConvertToDouble(token_vector[0])
66  * value_given / value_default;
67  // reconstruct parameter list
68  converted_parameter += ConvertToString(value);
69  converted_parameter += " ";
70  converted_parameter += default_unit;
71  for ( size_t i=2 ; i< token_vector.size(); i++) {
72  converted_parameter += " ";
73  converted_parameter += token_vector[i];
74  }
75  } else {
76  converted_parameter = parameterList;
77  }
78 
79  return G4UIcommand::DoIt(converted_parameter);
80 }
81 
83 {
84  return ConvertToDimensionedDouble(paramString);
85 }
86 
88 {
89  G4double vl;
90  char unts[30];
91 
92  std::istringstream is(paramString);
93  is >> vl >> unts;
94 
95  return vl;
96 }
97 
99 {
100  G4double vl;
101  char unts[30];
102 
103  std::istringstream is(paramString);
104  is >> vl >> unts;
105  G4String unt = unts;
106 
107  return ValueOf(unt);
108 }
109 
111 {
112  G4UIparameter* unitParam = GetParameter(1);
113  G4String canList = unitParam->GetParameterCandidates();
114  G4Tokenizer candidateTokenizer(canList);
115  G4String aToken = candidateTokenizer();
116  std::ostringstream os;
117  os << G4BestUnit(val,CategoryOf(aToken));
118 
119  G4String st = os.str();
120  return st;
121 }
122 
124 {
125  G4UIparameter* unitParam = GetParameter(1);
126  G4String st;
127  if(unitParam->IsOmittable())
128  { st = ConvertToString(val,unitParam->GetDefaultValue()); }
129  else
130  { st = ConvertToStringWithBestUnit(val); }
131  return st;
132 }
133 
135 (const char * theName,G4bool omittable,G4bool currentAsDefault)
136 {
137  G4UIparameter * theParam = GetParameter(0);
138  theParam->SetParameterName(theName);
139  theParam->SetOmittable(omittable);
140  theParam->SetCurrentAsDefault(currentAsDefault);
141 }
142 
144 {
145  G4UIparameter * theParam = GetParameter(0);
146  theParam->SetDefaultValue(defVal);
147 }
148 
149 void G4UIcmdWithADoubleAndUnit::SetUnitCategory(const char * unitCategory)
150 {
151  SetUnitCandidates(UnitsList(unitCategory));
152 }
153 
154 void G4UIcmdWithADoubleAndUnit::SetUnitCandidates(const char * candidateList)
155 {
156  G4UIparameter * untParam = GetParameter(1);
157  G4String canList = candidateList;
158  untParam->SetParameterCandidates(canList);
159 }
160 
162 {
163  G4UIparameter * untParam = GetParameter(1);
164  untParam->SetOmittable(true);
165  untParam->SetDefaultValue(defUnit);
166  SetUnitCategory(CategoryOf(defUnit));
167 }
168