00001 // Param.hh 00002 /* 00003 This source file is part of G4beamline, http://g4beamline.muonsinc.com 00004 Copyright (C) 2003,2004,2005,2006 by Tom Roberts, all rights reserved. 00005 00006 This program is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU General Public License 00008 as published by the Free Software Foundation; either version 2 00009 of the License, or (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 http://www.gnu.org/copyleft/gpl.html 00017 */ 00018 00019 #ifndef BLPARAM_HH 00020 #define BLPARAM_HH 00021 00022 #include <math.h> 00023 #include "globals.hh" 00024 #include "G4Types.hh" 00025 #include "BLCommand.hh" 00026 00027 /** BLParam - parameter class 00028 * 00029 * The global object Param is used to access parameter values defined 00030 * via the param command. The param command and the setParam() 00031 * functions create parameter name/value pairs known to the Param object. 00032 * 00033 * The BLSetParam class can be used to set initial values of parameters. 00034 * 00035 * The internal representation of every value is a G4String, but that 00036 * can be converted to double or int via get...() functions. 00037 * 00038 * NOTE: the get*() functions are intended to be used during 00039 * initialization, not during tracking. They perform a format conversion 00040 * and can be expensive -- a single call to getDouble() in 00041 * UserSteppingAction() slowed tracking down by MORE THAN A FACTOR OF TEN! 00042 * 00043 * NOTE: static and global constructors CAN use Param; this is 00044 * explicitly allowed because init() guarantees initialization order. 00045 **/ 00046 class BLParam { 00047 static std::map<G4String,G4String> *paramMap; 00048 static std::map<G4String,G4String> *paramHelpText; 00049 static void init(); 00050 public: 00051 /// Constructor. 00052 BLParam() { init(); } 00053 00054 /// printParam() will display all defined parameters on stdout. 00055 void printParam(); 00056 00057 /// getString() returns the G4String value of a parameter. 00058 /// returns "" for unknown parameters, and displays the error on stdout. 00059 /// DO NOT USE DURING TRACKING! 00060 G4String getString(G4String name); 00061 00062 /// getDouble() returns the G4double value of a parameter. 00063 /// returns -HUGE_VAL for unknown or invalid-format parameters, 00064 /// and displays the error on stdout. 00065 /// DO NOT USE DURING TRACKING! 00066 G4double getDouble(G4String name); 00067 00068 /// getInt() returns the G4int value of a parameter. 00069 /// returns 0x80000000 for unknown or invalid-format parameters, and 00070 /// displays the error on stdout. 00071 /// DO NOT USE DURING TRACKING! 00072 G4int getInt(G4String name); 00073 00074 /// setParam() sets the value of a parameter. 00075 /// DO NOT USE DURING TRACKING! 00076 void setParam(G4String name, G4String value); 00077 00078 /// setParam() sets the value of a parameter. 00079 /// DO NOT USE DURING TRACKING! 00080 void setParam(G4String name, G4double value); 00081 00082 /// setParam() sets the value of a parameter. 00083 /// DO NOT USE DURING TRACKING! 00084 void setParam(G4String name, G4int value); 00085 00086 /// isDefined() returns true if the name has a defined value. 00087 bool isDefined(G4String name); 00088 00089 /// expand() will expand every $name in str with parameter values. 00090 /// DO NOT USE DURING TRACKING! 00091 G4String expand(G4String str); 00092 00093 /// setHelpText() will set help text for a parameter 00094 /// DO NOT USE DURING TRACKING! 00095 void setHelpText(G4String name, G4String text); 00096 00097 /// getHelpText() returns the (complete) help text string. 00098 /// DO NOT USE DURING TRACKING! 00099 G4String getHelpText(); 00100 }; 00101 00102 extern BLParam Param; /// the global Param object 00103 00104 /** class BLSetParam -- Parameter initialization class 00105 * 00106 * Classes that use Parameters should declare a static instance of 00107 * this class to provide a default value for each such parameter. 00108 * Take care that the value is of the proper format. 00109 * 00110 * These values are set initally, so "param -unset" will not set them. 00111 **/ 00112 class BLSetParam { 00113 public: 00114 BLSetParam(G4String name, G4String value, G4String helpText="") 00115 { Param.setParam(name,value); 00116 if(helpText.size() > 0) Param.setHelpText(name,helpText); 00117 } 00118 }; 00119 00120 #endif // BLPARAM_HH