Geant4  10.02.p02
clparse.cc
Go to the documentation of this file.
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 // $Id: clparse.cc 67982 2013-03-13 10:36:03Z gcosmo $
28 //
29 // modified by I.Hrivnacova
30 // added G3SensVol
31 
32 #include "globals.hh"
33 #include <fstream>
34 #include "G4Tokenizer.hh"
35 #include "G3toG4.hh"
36 #include "G3EleTable.hh"
37 #include "G3VolTable.hh"
38 #include "G3MatTable.hh"
39 #include "G3MedTable.hh"
40 #include "G3RotTable.hh"
41 #include "G3PartTable.hh"
42 #include "G3DetTable.hh"
43 #include "G3SensVolVector.hh"
44 
45 std::ofstream ofile;
46 
47 extern "C"
48 {
49 #include <stdlib.h>
50 }
51 
52 extern std::ofstream ofile;
53 
55 G3MatTable G3Mat; // material G3 ID <-> G4 pointer table
56 G3MedTable G3Med; // trk media G3 ID <-> G4 pointer table
57 G3RotTable G3Rot; // rotation ID <-> G4 transform object table
58 G3PartTable G3Part; // particle ID <-> ParticleDefinition pointer
59 G3DetTable G3Det; // sensitive detector name <-> pointer
60 G3EleTable G3Ele; // element names table
61 G3SensVolVector G3SensVol; // vector of sensitive logical volumes
62 char gSeparator('_');
63 
65 
66 G4int Ipar[1000];
67 G4double Rpar[1000];
68 G4String Spar[1000];
69 
70 G4int G3CLTokens(G4String *line, G4String *tokens);
71 void G3CLEval(G4String *tokens, char *select);
72 
73 // front-end decoders for G3 routines
74 //
75 void PG4gsvolu(G4String *tokens);
76 void PG4gspos (G4String *tokens);
77 void PG4gsposp(G4String *tokens);
78 void PG4gsatt (G4String *tokens);
79 void PG4gsrotm(G4String *tokens);
80 void PG4gsdvn (G4String *tokens);
81 void PG4gsdvt (G4String *tokens);
82 void PG4gsdvx (G4String *tokens);
83 void PG4gsdvn2(G4String *tokens);
84 void PG4gsdvt2(G4String *tokens);
85 void PG4gsmate(G4String *tokens);
86 void PG4gsmixt(G4String *tokens);
87 void PG4gstmed(G4String *tokens);
88 void PG4gstpar(G4String *tokens);
89 void PG4gspart(G4String *tokens);
90 void PG4gsdk (G4String *tokens);
91 void PG4gsdet (G4String *tokens);
92 void PG4gsdetv(G4String *tokens);
93 void PG4gsdeta(G4String *tokens);
94 void PG4gsdeth(G4String *tokens);
95 void PG4gsdetd(G4String *tokens);
96 void PG4gsdetu(G4String *tokens);
97 void PG4ggclos();
98 
99 void G3CLRead(G4String & fname, char *select = 0)
100 {
101  //
102  // G3CLRead
103  // Read the call List file, parse the tokens, and pass the token
104  // List to the Geant4 interpreter
105  //
106  // fname: call List filename
107 
108  G4String line;
109  G4String tokens[1000];
110 
111  const char* ofname = "clparse.out";
112  ofile.open(ofname);
113  ofile << "Output file open\n";
114 
115  G4int count = 0;
116  G4int ntokens = 0;
117  std::ifstream istr(fname);
118 
119  while (line.readLine(istr) && ! istr.eof())
120  {
121  count++;
122  ntokens = G3CLTokens(&line,tokens); // tokenize the line
123  for (G4int i=0; i < ntokens; i++)
124  {
125  ofile << tokens[i] << G4endl;
126  }
127 
128  // interpret the line as a Geant call
129  //
130  G3CLEval(tokens, select);
131  }
132 }
133 
134 
136 {
137  //
138  // G3CLTokens
139  //
140  // Tokenize line, returning tokens in tokens[]. Items in ".."
141  // are extracted as single tokens, despite embedded spaces.
142 
143  G4Tokenizer next(*line);
144 
145  // first tokenize using " to identify strings
146  //
147  G4int itok = 0;
148  G4int ntokens = 0;
149  G4String token1, token2;
150  while (!(token1=next("\"")).isNull())
151  {
152  itok++;
153  if (itok%2 == 0 ) // even: inside a string
154  {
155  tokens[ntokens++] = token1;
156  }
157  else // not in a quoted string: finish tokenization
158  {
159  G4Tokenizer lev2(token1);
160  while (!(token2=lev2()).isNull())
161  {
162  tokens[ntokens] = token2;
163  ntokens++;
164  }
165  }
166  }
167  return ntokens;
168 }
169 
170 
171 void G3CLEval(G4String tokens[], char *select)
172 {
173  //
174  // G3CLEval
175  //
176  // Evaluate the token List as a Geant3 call, and execute it as
177  // a Geant4 call.
178 
179  const char* context = tokens[0];
180  const char* routine = tokens[1];
181  const char* wcard = "*";
182 
183  // If context is selected, return unless context matches
184  //
185  if ((select != 0) && (select != wcard))
186  {
187  if ( strcmp(select,context) ) { return; }
188  }
189 
190  // Branch on Geant3 routine name
191  //
192  ofile << "Do routine " << routine << " in context " << context << G4endl;
193 
194  if ( !strcmp(routine,"GSVOLU") ) { PG4gsvolu(&tokens[2]); return;}
195  if ( !strcmp(routine,"GSPOS") ) { PG4gspos (&tokens[2]); return;}
196  if ( !strcmp(routine,"GSPOSP") ) { PG4gsposp(&tokens[2]); return;}
197  if ( !strcmp(routine,"GSATT") ) { PG4gsatt (&tokens[2]); return;}
198  if ( !strcmp(routine,"GSROTM") ) { PG4gsrotm(&tokens[2]); return;}
199  if ( !strcmp(routine,"GSDVN") ) { PG4gsdvn (&tokens[2]); return;}
200  if ( !strcmp(routine,"GSDVT") ) { PG4gsdvt (&tokens[2]); return;}
201  if ( !strcmp(routine,"GSDVX") ) { PG4gsdvx (&tokens[2]); return;}
202  if ( !strcmp(routine,"GSDVN2") ) { PG4gsdvn2(&tokens[2]); return;}
203  if ( !strcmp(routine,"GSDVT2") ) { PG4gsdvt2(&tokens[2]); return;}
204  if ( !strcmp(routine,"GSMATE") ) { PG4gsmate(&tokens[2]); return;}
205  if ( !strcmp(routine,"GSMIXT") ) { PG4gsmixt(&tokens[2]); return;}
206  if ( !strcmp(routine,"GSTMED") ) { PG4gstmed(&tokens[2]); return;}
207  if ( !strcmp(routine,"GSTPAR") ) { PG4gstpar(&tokens[2]); return;}
208  if ( !strcmp(routine,"GSPART") ) { PG4gspart(&tokens[2]); return;}
209  if ( !strcmp(routine,"GSDK") ) { PG4gsdk (&tokens[2]); return;}
210  if ( !strcmp(routine,"GSDET") ) { PG4gsdet (&tokens[2]); return;}
211  if ( !strcmp(routine,"GSDETV") ) { PG4gsdetv(&tokens[2]); return;}
212  if ( !strcmp(routine,"GSDETA") ) { PG4gsdeta(&tokens[2]); return;}
213  if ( !strcmp(routine,"GSDETH") ) { PG4gsdeth(&tokens[2]); return;}
214  if ( !strcmp(routine,"GSDETD") ) { PG4gsdetd(&tokens[2]); return;}
215  if ( !strcmp(routine,"GSDETU") ) { PG4gsdetu(&tokens[2]); return;}
216  if ( !strcmp(routine,"GGCLOS") ) { PG4ggclos(); return;}
217 }
218 
219 void G3fillParams(G4String *tokens, const char *ptypes)
220 {
221  //
222  // G3fillParams
223  //
224  // Interpret tokens to fill call parameters, based on parameter types ptypes
225 
226  // loop over ptypes
227  //
228  G4int i =0, ipt = 0, k = 0;
229  G4int ni =0, nr = 0, nq = 0;
230  while (ptypes[i] != '\0')
231  {
232  switch (ptypes[i])
233  {
234  case 'i':
235  Ipar[ni] = atoi(tokens[ipt].data());
236  narray = Ipar[ni];
237  ni++; ipt++;
238  break;
239  case 'r':
240  Rpar[nr] = atof(tokens[ipt].data());
241  nr++; ipt++;
242  break;
243  case 's':
244  Spar[nq] = tokens[ipt];
245  nq++; ipt++;
246  break;
247  case 'I':
248  for (k=0; k < narray; k++)
249  {
250  Ipar[ni] = atoi(tokens[ipt].data());
251  ni++; ipt++;
252  }
253  break;
254  case 'R':
255  for (k=0; k < narray; k++)
256  {
257  Rpar[nr] = atof(tokens[ipt].data());
258  nr++; ipt++;
259  }
260  break;
261  case 'Q':
262  // special case of reading three successive R arrays
263  // into one (used in gsmixt)
264  //
265  narray = 3 * std::abs(narray);
266  for (k=0; k < narray; k++)
267  {
268  Rpar[nr] = atof(tokens[ipt].data());
269  nr++; ipt++;
270  }
271  break;
272  case 'S':
273  for (k=0; k < narray; k++)
274  {
275  Spar[nq] = tokens[ipt];
276  nq++; ipt++;
277  }
278  break;
279  default:
280  ofile << "unidentified ptype '" << ptypes[i] << G4endl;
281  };
282  i++;
283  }
284 }
void PG4gsvolu(G4String *tokens)
Definition: G4gsvolu.cc:37
void PG4gspos(G4String *tokens)
Definition: G4gspos.cc:42
G4int Ipar[1000]
Definition: clparse.cc:66
void PG4gsdetd(G4String *tokens)
Definition: G4gsdetd.cc:34
G4int narray
Definition: clparse.cc:64
G4String Spar[1000]
Definition: clparse.cc:68
void PG4gstmed(G4String *tokens)
Definition: G4gstmed.cc:44
void PG4gsdvt2(G4String *tokens)
Definition: G4gsdvt2.cc:41
void PG4gsposp(G4String *tokens)
Definition: G4gsposp.cc:42
G3SensVolVector G3SensVol
Definition: clparse.cc:61
void PG4gsdk(G4String *tokens)
Definition: G4gsdk.cc:33
void PG4gsdetv(G4String *tokens)
Definition: G4gsdetv.cc:36
G3EleTable G3Ele
Definition: clparse.cc:60
void PG4gstpar(G4String *tokens)
Definition: G4gstpar.cc:32
void PG4gsdvt(G4String *tokens)
Definition: G4gsdvt.cc:41
std::istream & readLine(std::istream &, G4bool skipWhite=true)
int G4int
Definition: G4Types.hh:78
void PG4ggclos()
Definition: G4ggclos.cc:32
void PG4gsdeth(G4String *tokens)
Definition: G4gsdeth.cc:34
G3DetTable G3Det
Definition: clparse.cc:59
char gSeparator('_')
std::vector< G4LogicalVolume * > G3SensVolVector
G4int G3CLTokens(G4String *line, G4String *tokens)
G3MatTable G3Mat
Definition: clparse.cc:55
G3VolTable G3Vol
Definition: clparse.cc:54
G3PartTable G3Part
Definition: clparse.cc:58
void PG4gsmixt(G4String *tokens)
Definition: G4gsmixt.cc:43
void PG4gsrotm(G4String *tokens)
Definition: G4gsrotm.cc:35
void PG4gsmate(G4String *tokens)
Definition: G4gsmate.cc:42
void G3CLRead(G4String &fname, char *select=0)
Definition: clparse.cc:99
std::ofstream ofile
Definition: clparse.cc:45
void PG4gsdvx(G4String *tokens)
Definition: G4gsdvx.cc:40
void G3CLEval(G4String *tokens, char *select)
void PG4gspart(G4String *tokens)
Definition: G4gspart.cc:33
#define G4endl
Definition: G4ios.hh:61
void PG4gsatt(G4String *tokens)
Definition: G4gsatt.cc:33
void PG4gsdvn(G4String *tokens)
Definition: G4gsdvn.cc:38
void PG4gsdetu(G4String *tokens)
Definition: G4gsdetu.cc:31
double G4double
Definition: G4Types.hh:76
G3MedTable G3Med
Definition: clparse.cc:56
void G3fillParams(G4String *tokens, const char *ptypes)
Definition: clparse.cc:219
G4double Rpar[1000]
Definition: clparse.cc:67
void PG4gsdet(G4String *tokens)
Definition: G4gsdet.cc:32
G3RotTable G3Rot
Definition: clparse.cc:57
void PG4gsdvn2(G4String *tokens)
Definition: G4gsdvn2.cc:41
void PG4gsdeta(G4String *tokens)
Definition: G4gsdeta.cc:38