G4UIcmdWith3VectorAndUnit.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 "G4UIcmdWith3VectorAndUnit.hh"
00032 #include "G4Tokenizer.hh"
00033 #include "G4UnitsTable.hh"
00034 #include <sstream>
00035 
00036 G4UIcmdWith3VectorAndUnit::G4UIcmdWith3VectorAndUnit
00037 (const char * theCommandPath,G4UImessenger * theMessenger)
00038 :G4UIcommand(theCommandPath,theMessenger)
00039 {
00040   G4UIparameter * dblParamX = new G4UIparameter('d');
00041   SetParameter(dblParamX);
00042   G4UIparameter * dblParamY = new G4UIparameter('d');
00043   SetParameter(dblParamY);
00044   G4UIparameter * dblParamZ = new G4UIparameter('d');
00045   SetParameter(dblParamZ);
00046   G4UIparameter * untParam = new G4UIparameter('s');
00047   SetParameter(untParam);
00048   untParam->SetParameterName("Unit");
00049 }
00050 
00051 G4int G4UIcmdWith3VectorAndUnit::DoIt(G4String parameterList)
00052 {
00053   std::vector<G4String> token_vector;
00054   G4Tokenizer tkn(parameterList);
00055   G4String str;
00056   while( (str = tkn()) != "" ) {
00057     token_vector.push_back(str);
00058   }
00059 
00060   // convert a value in default unit
00061   G4String converted_parameter;
00062   G4String default_unit = GetParameter(3)-> GetDefaultValue();
00063   if (default_unit != "" && token_vector.size() >= 4) {
00064     G4double value_given = ValueOf(token_vector[3]);
00065     G4double value_default = ValueOf(default_unit);
00066     G4double x = ConvertToDouble(token_vector[0]) * value_given / value_default;
00067     G4double y = ConvertToDouble(token_vector[1]) * value_given / value_default;
00068     G4double z = ConvertToDouble(token_vector[2]) * value_given / value_default;
00069 
00070     // reconstruct parameter list
00071     converted_parameter += ConvertToString(x);
00072     converted_parameter += " ";
00073     converted_parameter += ConvertToString(y);
00074     converted_parameter += " ";
00075     converted_parameter += ConvertToString(z);
00076     converted_parameter += " ";
00077     converted_parameter += default_unit;
00078     for ( size_t i=4 ; i< token_vector.size(); i++) {
00079       converted_parameter += " ";
00080       converted_parameter += token_vector[i];
00081     }
00082   } else {
00083     converted_parameter = parameterList;
00084   }
00085 
00086   return G4UIcommand::DoIt(converted_parameter);
00087 }
00088 
00089 G4ThreeVector G4UIcmdWith3VectorAndUnit::GetNew3VectorValue(const char* paramString)
00090 {
00091   return ConvertToDimensioned3Vector(paramString);
00092 }
00093 
00094 G4ThreeVector G4UIcmdWith3VectorAndUnit::GetNew3VectorRawValue(const char* paramString)
00095 {
00096   G4double vx;
00097   G4double vy;
00098   G4double vz;
00099   char unts[30];
00100   std::istringstream is(paramString);
00101   is >> vx >> vy >> vz >> unts;
00102   return G4ThreeVector(vx,vy,vz);
00103 }
00104 
00105 G4double G4UIcmdWith3VectorAndUnit::GetNewUnitValue(const char* paramString)
00106 {
00107   G4double vx;
00108   G4double vy;
00109   G4double vz;
00110   char unts[30];
00111   std::istringstream is(paramString);
00112   is >> vx >> vy >> vz >> unts;
00113   G4String unt = unts;
00114   return ValueOf(unt);
00115 }
00116 
00117 G4String G4UIcmdWith3VectorAndUnit::ConvertToStringWithBestUnit(G4ThreeVector vec)
00118 {
00119   G4UIparameter* unitParam = GetParameter(3);
00120   G4String canList = unitParam->GetParameterCandidates();
00121   G4Tokenizer candidateTokenizer(canList);
00122   G4String aToken = candidateTokenizer();
00123 
00124   std::ostringstream os;
00125   os << G4BestUnit(vec,CategoryOf(aToken));
00126   G4String st = os.str();
00127 
00128   return st;
00129 }
00130 
00131 G4String G4UIcmdWith3VectorAndUnit::ConvertToStringWithDefaultUnit(G4ThreeVector vec)
00132 {
00133   G4UIparameter* unitParam = GetParameter(3);
00134   G4String st;
00135   if(unitParam->IsOmittable())
00136   { st = ConvertToString(vec,unitParam->GetDefaultValue()); }
00137   else
00138   { st = ConvertToStringWithBestUnit(vec); }
00139   return st;
00140 }
00141 
00142 void G4UIcmdWith3VectorAndUnit::SetParameterName
00143 (const char * theNameX,const char * theNameY,const char * theNameZ,
00144 G4bool omittable,G4bool currentAsDefault)
00145 {
00146   G4UIparameter * theParamX = GetParameter(0);
00147   theParamX->SetParameterName(theNameX);
00148   theParamX->SetOmittable(omittable);
00149   theParamX->SetCurrentAsDefault(currentAsDefault);
00150   G4UIparameter * theParamY = GetParameter(1);
00151   theParamY->SetParameterName(theNameY);
00152   theParamY->SetOmittable(omittable);
00153   theParamY->SetCurrentAsDefault(currentAsDefault);
00154   G4UIparameter * theParamZ = GetParameter(2);
00155   theParamZ->SetParameterName(theNameZ);
00156   theParamZ->SetOmittable(omittable);
00157   theParamZ->SetCurrentAsDefault(currentAsDefault);
00158 }
00159 
00160 void G4UIcmdWith3VectorAndUnit::SetDefaultValue(G4ThreeVector vec)
00161 {
00162   G4UIparameter * theParamX = GetParameter(0);
00163   theParamX->SetDefaultValue(vec.x());
00164   G4UIparameter * theParamY = GetParameter(1);
00165   theParamY->SetDefaultValue(vec.y());
00166   G4UIparameter * theParamZ = GetParameter(2);
00167   theParamZ->SetDefaultValue(vec.z());
00168 }
00169 
00170 void G4UIcmdWith3VectorAndUnit::SetUnitCategory(const char * unitCategory)
00171 {
00172   SetUnitCandidates(UnitsList(unitCategory));
00173 }
00174 
00175 void G4UIcmdWith3VectorAndUnit::SetUnitCandidates(const char * candidateList)
00176 {
00177   G4UIparameter * untParam = GetParameter(3);
00178   G4String canList = candidateList;
00179   untParam->SetParameterCandidates(canList);
00180 }
00181 
00182 void G4UIcmdWith3VectorAndUnit::SetDefaultUnit(const char * defUnit)
00183 {
00184   G4UIparameter * untParam = GetParameter(3);
00185   untParam->SetOmittable(true);
00186   untParam->SetDefaultValue(defUnit);
00187   SetUnitCategory(CategoryOf(defUnit));
00188 }
00189 

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