ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4GDMLEvaluator.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4GDMLEvaluator.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 // GEANT4 tag $ Name:$
28 //
29 // class G4GDMLEvaluator Implementation
30 //
31 // Original author: Zoltan Torzsok, November 2007
32 //
33 // --------------------------------------------------------------------
34 
35 #include <sstream>
36 
37 #include "G4GDMLEvaluator.hh"
38 #include "G4SystemOfUnits.hh"
39 
41 {
42  eval.clear();
43  eval.setStdMath();
45 }
46 
48 {
49  eval.clear();
50  eval.setStdMath();
52 
53  variableList.clear();
54 }
55 
57 {
58  if (eval.findVariable(name))
59  {
60  G4String error_msg = "Redefinition of constant or variable: "+name;
61  G4Exception("G4GDMLEvaluator::DefineConstant()", "InvalidExpression",
62  FatalException, error_msg);
63  }
64  eval.setVariable(name.c_str(),value);
65 }
66 
68 {
69  if (eval.findVariable(name))
70  {
71  G4String error_msg = "Redefinition of constant or variable: "+name;
72  G4Exception("G4GDMLEvaluator::DefineVariable()", "InvalidExpression",
73  FatalException, error_msg);
74  }
75  eval.setVariable(name.c_str(),value);
76  variableList.push_back(name);
77 }
78 
80  G4int coldim,
81  std::vector<G4double> valueList)
82 {
83  const G4int size = valueList.size();
84 
85  if (size == 0)
86  {
87  G4String error_msg = "Matrix '"+name+"' is empty!";
88  G4Exception("G4GDMLEvaluator::DefineMatrix()", "InvalidSize",
89  FatalException, error_msg);
90  }
91  /*
92  if (size == 1)
93  {
94  G4String error_msg = "Matrix '" + name
95  + "' has only one element! "
96  + "Define a constant instead!!";
97  G4Exception("G4GDMLEvaluator::DefineMatrix()", "InvalidSize",
98  FatalException, error_msg);
99  }
100  */
101 
102  if (size % coldim != 0)
103  {
104  G4String error_msg = "Matrix '" + name + "' is not filled correctly!";
105  G4Exception("G4GDMLEvaluator::DefineMatrix()", "InvalidSize",
106  FatalException, error_msg);
107  }
108 
109  if ((size == coldim) || (coldim == 1)) // Row- or column matrix
110  {
111  for (G4int i=0;i<size;i++)
112  {
113  std::stringstream MatrixElementNameStream;
114  MatrixElementNameStream << name << "_" << i;
115  DefineConstant(MatrixElementNameStream.str(),valueList[i]);
116  }
117  }
118  else // Normal matrix
119  {
120  const G4int rowdim = size/coldim;
121 
122  for (G4int i=0;i<rowdim;i++)
123  {
124  for (G4int j=0;j<coldim;j++)
125  {
126  std::stringstream MatrixElementNameStream;
127  MatrixElementNameStream << name << "_" << i << "_" << j;
128  DefineConstant(MatrixElementNameStream.str(),valueList[coldim*i+j]);
129  }
130  }
131  }
132 }
133 
135 {
136  if (!IsVariable(name))
137  {
138  G4String error_msg = "Variable '" + name + "' is not defined!";
139  G4Exception("G4GDMLEvaluator::SetVariable()", "InvalidSetup",
140  FatalException, error_msg);
141  }
142  eval.setVariable(name.c_str(),value);
143 }
144 
146 {
147  const size_t variableCount = variableList.size();
148 
149  for (size_t i=0;i<variableCount;i++)
150  {
151  if (variableList[i] == name) { return true; }
152  }
153 
154  return false;
155 }
156 
158 {
159  std::string::size_type full = in.size();
160  std::string::size_type open = in.find("[",0);
161  std::string::size_type close = in.find("]",0);
162 
163  if (open==close) { return in; }
164 
165  if ((open>close) || (open==std::string::npos) || (close==std::string::npos))
166  {
167  G4String error_msg = "Bracket mismatch: " + in;
168  G4Exception("G4GDMLEvaluator::SolveBrackets()", "InvalidExpression",
169  FatalException, error_msg);
170  return in;
171  }
172 
173  std::string::size_type begin = open;
174  std::string::size_type end = 0;
175  std::string::size_type end1 = 0;
176  std::string out;
177  out.append(in,0,open);
178 
179  do // Loop for all possible matrix elements in 'in'
180  {
181  do // SolveBrackets for one matrix element
182  {
183  end = in.find(",",begin+1);
184  end1= in.find("]",begin+1);
185  if (end>end1) { end = end1; }
186  if (end==std::string::npos) { end = close;}
187 
188  std::stringstream indexStream;
189  indexStream << "_" << EvaluateInteger(in.substr(begin+1,end-begin-1))-1;
190 
191  out.append(indexStream.str());
192 
193  begin = end;
194 
195  } while (end<close);
196 
197  if (full==close) { return out; }
198 
199  open = in.find("[",begin);
200  close = in.find("]",begin+1);
201 
202  if (open==close) { out.append(in.substr(end+1,full-end-1)); return out; }
203  out.append(in.substr(end+1,open-end-1));
204 
205  begin=open;
206 
207  } while (close<full);
208 
209  return out;
210 }
211 
213 {
214  G4String expression = SolveBrackets(in);
215 
216  G4double value = 0.0;
217 
218  if (!expression.empty())
219  {
220  value = eval.evaluate(expression.c_str());
221 
222  if (eval.status() != G4Evaluator::OK)
223  {
224  eval.print_error();
225  G4String error_msg = "Error in expression: " + expression;
226  G4Exception("G4GDMLEvaluator::Evaluate()", "InvalidExpression",
227  FatalException, error_msg);
228  }
229  }
230  return value;
231 }
232 
234 {
235  // This function is for evaluating integer expressions,
236  // like loop variables and matrix indices.
237  // Complains if the evaluated expression has a fractional
238  // part different from zero
239 
240  G4double value = Evaluate(expression);
241 
242  G4int whole = (G4int)value;
243  G4double frac = value - (G4double)whole;
244 
245  if (frac != 0.0)
246  {
247  G4String error_msg = "Expression '" + expression
248  + "' is expected to have an integer value!";
249  G4Exception("G4GDMLEvaluator::EvaluateInteger()", "InvalidExpression",
250  FatalException, error_msg);
251  }
252  return whole;
253 }
254 
256 {
257  if (IsVariable(name))
258  {
259  G4String error_msg = "Constant '" + name
260  + "' is not defined! It is a variable!";
261  G4Exception("G4GDMLEvaluator::GetConstant()", "InvalidSetup",
262  FatalException, error_msg);
263  }
264  if (!eval.findVariable(name))
265  {
266  G4String error_msg = "Constant '" + name + "' is not defined!";
267  G4Exception("G4GDMLEvaluator::GetConstant()", "InvalidSetup",
268  FatalException, error_msg);
269  }
270  return Evaluate(name);
271 }
272 
274 {
275  if (!IsVariable(name))
276  {
277  G4String error_msg = "Variable '" + name + "' is not a defined!";
278  G4Exception("G4GDMLEvaluator::GetVariable()", "InvalidSetup",
279  FatalException, error_msg);
280  }
281  return Evaluate(name);
282 }
283 
285 {
286  std::ostringstream os;
287  os << ival;
288  G4String vl = os.str();
289  return vl;
290 }
291 
293 {
294  std::ostringstream os;
295  os << dval;
296  G4String vl = os.str();
297  return vl;
298 }