G4UIcmdWithADoubleAndUnit.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 //
00030 
00031 #include "G4UIcmdWithADoubleAndUnit.hh"
00032 #include "G4Tokenizer.hh"
00033 #include "G4UnitsTable.hh"
00034 #include <sstream>
00035 #include <vector>
00036 
00037 G4UIcmdWithADoubleAndUnit::G4UIcmdWithADoubleAndUnit
00038 (const char * theCommandPath,G4UImessenger * theMessenger)
00039 :G4UIcommand(theCommandPath,theMessenger)
00040 {
00041   G4UIparameter * dblParam = new G4UIparameter('d');
00042   SetParameter(dblParam);
00043   G4UIparameter * untParam = new G4UIparameter('s');
00044   SetParameter(untParam);
00045   untParam->SetParameterName("Unit");
00046 }
00047 
00048 G4int G4UIcmdWithADoubleAndUnit::DoIt(G4String parameterList)
00049 {
00050   std::vector<G4String> token_vector;
00051   G4Tokenizer tkn(parameterList);
00052   G4String str;
00053   while( (str = tkn()) != "" ) {
00054     token_vector.push_back(str);
00055   }
00056 
00057   // convert a value in default unit
00058   G4String converted_parameter;
00059   G4String default_unit = GetParameter(1)-> GetDefaultValue();
00060   if (default_unit != "" && token_vector.size() >= 2) {
00061     G4double value_given = ValueOf(token_vector[1]);
00062     G4double value_default = ValueOf(default_unit);
00063     G4double value = ConvertToDouble(token_vector[0])
00064                       * value_given / value_default;
00065     // reconstruct parameter list
00066     converted_parameter += ConvertToString(value);
00067     converted_parameter += " ";
00068     converted_parameter += default_unit;
00069     for ( size_t i=2 ; i< token_vector.size(); i++) {
00070       converted_parameter += " ";
00071       converted_parameter += token_vector[i];
00072     }
00073   } else {
00074     converted_parameter = parameterList;
00075   }
00076 
00077   return G4UIcommand::DoIt(converted_parameter);
00078 }
00079 
00080 G4double G4UIcmdWithADoubleAndUnit::GetNewDoubleValue(const char* paramString)
00081 {
00082   return ConvertToDimensionedDouble(paramString);
00083 }
00084 
00085 G4double G4UIcmdWithADoubleAndUnit::GetNewDoubleRawValue(const char* paramString)
00086 {
00087   G4double vl;
00088   char unts[30];
00089 
00090   std::istringstream is(paramString);
00091   is >> vl >> unts;
00092 
00093   return vl;
00094 }
00095 
00096 G4double G4UIcmdWithADoubleAndUnit::GetNewUnitValue(const char* paramString)
00097 {
00098   G4double vl;
00099   char unts[30];
00100 
00101   std::istringstream is(paramString);
00102   is >> vl >> unts;
00103   G4String unt = unts;
00104 
00105   return ValueOf(unt);
00106 }
00107 
00108 G4String G4UIcmdWithADoubleAndUnit::ConvertToStringWithBestUnit(G4double val)
00109 {
00110   G4UIparameter* unitParam = GetParameter(1);
00111   G4String canList = unitParam->GetParameterCandidates();
00112   G4Tokenizer candidateTokenizer(canList);
00113   G4String aToken = candidateTokenizer();
00114   std::ostringstream os;
00115   os << G4BestUnit(val,CategoryOf(aToken));
00116 
00117   G4String st = os.str();
00118   return st;
00119 }
00120 
00121 G4String G4UIcmdWithADoubleAndUnit::ConvertToStringWithDefaultUnit(G4double val)
00122 {
00123   G4UIparameter* unitParam = GetParameter(1);
00124   G4String st;
00125   if(unitParam->IsOmittable())
00126   { st = ConvertToString(val,unitParam->GetDefaultValue()); }
00127   else
00128   { st = ConvertToStringWithBestUnit(val); }
00129   return st;
00130 }
00131 
00132 void G4UIcmdWithADoubleAndUnit::SetParameterName
00133 (const char * theName,G4bool omittable,G4bool currentAsDefault)
00134 {
00135   G4UIparameter * theParam = GetParameter(0);
00136   theParam->SetParameterName(theName);
00137   theParam->SetOmittable(omittable);
00138   theParam->SetCurrentAsDefault(currentAsDefault);
00139 }
00140 
00141 void G4UIcmdWithADoubleAndUnit::SetDefaultValue(G4double defVal)
00142 {
00143   G4UIparameter * theParam = GetParameter(0);
00144   theParam->SetDefaultValue(defVal);
00145 }
00146 
00147 void G4UIcmdWithADoubleAndUnit::SetUnitCategory(const char * unitCategory)
00148 {
00149   SetUnitCandidates(UnitsList(unitCategory));
00150 }
00151 
00152 void G4UIcmdWithADoubleAndUnit::SetUnitCandidates(const char * candidateList)
00153 {
00154   G4UIparameter * untParam = GetParameter(1);
00155   G4String canList = candidateList;
00156   untParam->SetParameterCandidates(canList);
00157 }
00158 
00159 void G4UIcmdWithADoubleAndUnit::SetDefaultUnit(const char * defUnit)
00160 {
00161   G4UIparameter * untParam = GetParameter(1);
00162   untParam->SetOmittable(true);
00163   untParam->SetDefaultValue(defUnit);
00164   SetUnitCategory(CategoryOf(defUnit));
00165 }
00166 

Generated on Mon May 27 17:50:05 2013 for Geant4 by  doxygen 1.4.7