00001
00002
00003
00004
00005
00006 #include <vector>
00007
00008 #include "myglobals.hh"
00009
00010 #include "TF1.h"
00011
00012 #include "MyConfigure.hh"
00013
00014 std::vector<G4String> MyConfigure::knownValueNames;
00015 std::vector<G4String> MyConfigure::knownValues;
00016
00017 MyConfigure::MyConfigure()
00018 {}
00019
00020 MyConfigure::~MyConfigure()
00021 {}
00022
00023 double MyConfigure::CalFormula(G4String formula, int iRep){
00024
00025
00026 TF1 *f1 = new TF1("f1", formula);
00027 double value = f1->Eval(iRep);
00028
00029 delete f1;
00030 return value;
00031 }
00032
00033 G4String MyConfigure::ReplaceMacro(G4String formula){
00034
00035 std::vector<G4String> words = GetWords(formula);
00036
00037 for (int iWord = 0; iWord < words.size(); iWord++ ){
00038
00039 G4String value;
00040 if (FindMacro(words[iWord],value)){
00041 Replace(formula,words[iWord],value);
00042 }
00043 }
00044
00045 return formula;
00046 }
00047
00048 std::vector<G4String> MyConfigure::GetWords(G4String formula){
00049 std::vector<G4String> words;
00050 words.clear();
00051 const char* cformula = formula.c_str();
00052 int length = strlen(cformula);
00053 char temp[1240];
00054 int tempoffset = 0;
00055 for ( int offset = 0; offset < length; offset++ ){
00056 char c = cformula[offset];
00057 bool isword = false;
00058 if (c>='a'&&c<='z'
00059 ||c>='A'&&c<='Z'
00060 ||c>='0'&&c<='9'
00061 ||c=='_'
00062 ){
00063 temp[tempoffset++] = cformula[offset];
00064 isword = true;
00065 }
00066 if (!isword||offset==length-1){
00067 if (tempoffset>0){
00068 temp[tempoffset++] = '\0';
00069 tempoffset=0;
00070 G4String word = temp;
00071 bool found = false;
00072 for(int iWord = 0; iWord<words.size(); iWord++){
00073 if (words[iWord]==word){
00074 found = true;
00075 break;
00076 }
00077 }
00078 if (!found){
00079 words.push_back(word);
00080 }
00081 }
00082 }
00083 }
00084 return words;
00085 }
00086
00087 bool MyConfigure::FindMacro(G4String word, G4String& value){
00088 bool found = false;
00089 for (int i = 0; i< knownValues.size(); i++){
00090 if (knownValueNames[i]==word){
00091 value = knownValues[i];
00092 found = true;
00093 break;
00094 }
00095 }
00096 return found;
00097 }
00098
00099 void MyConfigure::Replace(G4String &formula, G4String word, G4String value){
00100
00101 G4String newform = "";
00102 const char* cformula = formula.c_str();
00103 int length = strlen(cformula);
00104 char temp[1024];
00105 int tempoffset = 0;
00106 char cnewform[1024];
00107 int newformoffset = 0;
00108 for ( int offset = 0; offset < length; offset++ ){
00109 char c = cformula[offset];
00110 bool isword = false;
00111 if (c>='a'&&c<='z'
00112 ||c>='A'&&c<='Z'
00113 ||c>='0'&&c<='9'
00114 ||c=='_'
00115 ){
00116 temp[tempoffset++] = cformula[offset];
00117 isword = true;
00118 }
00119 if (!isword||offset==length-1){
00120 if (tempoffset>0){
00121 temp[tempoffset] = '\0';
00122 tempoffset=0;
00123 if (newformoffset>0){
00124 cnewform[newformoffset] = '\0';
00125 newformoffset=0;
00126 G4String newformtemp = cnewform;
00127 newform=newform+newformtemp;
00128 }
00129 G4String word = temp;
00130
00131 G4String newword;
00132 bool found = FindMacro(word,newword);
00133 if (found){
00134 newform=newform+"("+newword+")";
00135 }
00136 else{
00137 newform=newform+word;
00138 }
00139
00140 }
00141 if(!isword){
00142 cnewform[newformoffset++] = cformula[offset];
00143 }
00144 if (offset==length-1){
00145 cnewform[newformoffset] = '\0';
00146 G4String newformtemp = cnewform;
00147 newform=newform+newformtemp;
00148 }
00149 }
00150 }
00151
00152 formula = newform;
00153 }
00154