clparse.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 // modified by I.Hrivnacova
00030 // added G3SensVol
00031 
00032 #include "globals.hh"
00033 #include <fstream>
00034 #include "G4Tokenizer.hh"
00035 #include "G3toG4.hh"
00036 #include "G3EleTable.hh"
00037 #include "G3VolTable.hh"
00038 #include "G3MatTable.hh"
00039 #include "G3MedTable.hh"
00040 #include "G3RotTable.hh"
00041 #include "G3PartTable.hh"
00042 #include "G3DetTable.hh"
00043 #include "G3SensVolVector.hh"
00044 
00045 std::ofstream ofile;
00046 
00047 extern "C"
00048 {
00049 #include <stdlib.h>
00050 }
00051 
00052 extern std::ofstream ofile;
00053 
00054 G3VolTable G3Vol;
00055 G3MatTable G3Mat;   // material G3 ID <-> G4 pointer table
00056 G3MedTable G3Med;   // trk media G3 ID <-> G4 pointer table
00057 G3RotTable G3Rot;   // rotation ID <-> G4 transform object table
00058 G3PartTable G3Part; // particle ID <-> ParticleDefinition pointer
00059 G3DetTable G3Det;   // sensitive detector name <-> pointer
00060 G3EleTable G3Ele;   // element names table
00061 G3SensVolVector G3SensVol; // vector of sensitive logical volumes
00062 char gSeparator('_');
00063 
00064 G4int narray;
00065 
00066 G4int Ipar[1000];
00067 G4double Rpar[1000];
00068 G4String Spar[1000];
00069 
00070 G4int G3CLTokens(G4String *line, G4String *tokens);
00071 void G3CLEval(G4String *tokens, char *select);
00072 
00073 // front-end decoders for G3 routines
00074 //
00075 void PG4gsvolu(G4String *tokens);
00076 void PG4gspos (G4String *tokens);
00077 void PG4gsposp(G4String *tokens);
00078 void PG4gsatt (G4String *tokens);
00079 void PG4gsrotm(G4String *tokens);
00080 void PG4gsdvn (G4String *tokens);
00081 void PG4gsdvt (G4String *tokens);
00082 void PG4gsdvx (G4String *tokens);
00083 void PG4gsdvn2(G4String *tokens);
00084 void PG4gsdvt2(G4String *tokens);
00085 void PG4gsmate(G4String *tokens);
00086 void PG4gsmixt(G4String *tokens);
00087 void PG4gstmed(G4String *tokens);
00088 void PG4gstpar(G4String *tokens);
00089 void PG4gspart(G4String *tokens);
00090 void PG4gsdk  (G4String *tokens);
00091 void PG4gsdet (G4String *tokens);
00092 void PG4gsdetv(G4String *tokens);
00093 void PG4gsdeta(G4String *tokens);
00094 void PG4gsdeth(G4String *tokens);
00095 void PG4gsdetd(G4String *tokens);
00096 void PG4gsdetu(G4String *tokens);
00097 void PG4ggclos();
00098 
00099 void G3CLRead(G4String & fname, char *select = 0)
00100 {
00101   //
00102   //  G3CLRead
00103   //  Read the call List file, parse the tokens, and pass the token
00104   //  List to the Geant4 interpreter
00105   //
00106   //  fname: call List filename
00107   
00108     G4String line;
00109     G4String tokens[1000];
00110 
00111     const char* ofname = "clparse.out";
00112     ofile.open(ofname);
00113     ofile << "Output file open\n";
00114 
00115     G4int count = 0;
00116     G4int ntokens = 0;
00117     std::ifstream istr(fname);
00118     
00119     while (line.readLine(istr) && ! istr.eof())
00120     {
00121         count++;
00122         ntokens = G3CLTokens(&line,tokens);  // tokenize the line
00123         for (G4int i=0; i < ntokens; i++)
00124         {
00125           ofile << tokens[i] << G4endl;
00126         }
00127 
00128         // interpret the line as a Geant call
00129         //
00130         G3CLEval(tokens, select);
00131     }
00132 }
00133 
00134 
00135 G4int G3CLTokens(G4String *line, G4String tokens[])
00136 {
00137   //
00138   // G3CLTokens
00139   //
00140   // Tokenize line, returning tokens in tokens[]. Items in ".."
00141   // are extracted as single tokens, despite embedded spaces.
00142 
00143     G4Tokenizer next(*line);
00144 
00145     // first tokenize using " to identify strings
00146     //
00147     G4int itok = 0;
00148     G4int ntokens = 0;
00149     G4String token1, token2;
00150     while (!(token1=next("\"")).isNull())
00151     {
00152         itok++;
00153         if (itok%2 == 0 ) // even: inside a string
00154         {
00155             tokens[ntokens++] = token1;
00156         }
00157         else              // not in a quoted string: finish tokenization
00158         {
00159             G4Tokenizer lev2(token1);
00160             while (!(token2=lev2()).isNull())
00161             {
00162                 tokens[ntokens] = token2;
00163                 ntokens++;
00164             }
00165         }
00166     }
00167     return ntokens;
00168 }
00169 
00170 
00171 void G3CLEval(G4String tokens[], char *select)
00172 {
00173   //
00174   // G3CLEval
00175   //
00176   // Evaluate the token List as a Geant3 call, and execute it as
00177   // a Geant4 call.
00178 
00179     const char* context = tokens[0];
00180     const char* routine = tokens[1];
00181     const char* wcard = "*";
00182 
00183     // If context is selected, return unless context matches
00184     //
00185     if ((select != 0) && (select != wcard))
00186     {
00187       if ( strcmp(select,context) )  { return; }
00188     }
00189 
00190     // Branch on Geant3 routine name
00191     //
00192     ofile << "Do routine " << routine << " in context " << context << G4endl;
00193     
00194     if ( !strcmp(routine,"GSVOLU") ) { PG4gsvolu(&tokens[2]); return;}
00195     if ( !strcmp(routine,"GSPOS") )  { PG4gspos (&tokens[2]); return;}
00196     if ( !strcmp(routine,"GSPOSP") ) { PG4gsposp(&tokens[2]); return;}
00197     if ( !strcmp(routine,"GSATT") )  { PG4gsatt (&tokens[2]); return;}
00198     if ( !strcmp(routine,"GSROTM") ) { PG4gsrotm(&tokens[2]); return;}
00199     if ( !strcmp(routine,"GSDVN") )  { PG4gsdvn (&tokens[2]); return;}
00200     if ( !strcmp(routine,"GSDVT") )  { PG4gsdvt (&tokens[2]); return;}
00201     if ( !strcmp(routine,"GSDVX") )  { PG4gsdvx (&tokens[2]); return;}
00202     if ( !strcmp(routine,"GSDVN2") ) { PG4gsdvn2(&tokens[2]); return;}
00203     if ( !strcmp(routine,"GSDVT2") ) { PG4gsdvt2(&tokens[2]); return;}
00204     if ( !strcmp(routine,"GSMATE") ) { PG4gsmate(&tokens[2]); return;}
00205     if ( !strcmp(routine,"GSMIXT") ) { PG4gsmixt(&tokens[2]); return;}
00206     if ( !strcmp(routine,"GSTMED") ) { PG4gstmed(&tokens[2]); return;}
00207     if ( !strcmp(routine,"GSTPAR") ) { PG4gstpar(&tokens[2]); return;}
00208     if ( !strcmp(routine,"GSPART") ) { PG4gspart(&tokens[2]); return;}
00209     if ( !strcmp(routine,"GSDK") )   { PG4gsdk  (&tokens[2]); return;}
00210     if ( !strcmp(routine,"GSDET") )  { PG4gsdet (&tokens[2]); return;}
00211     if ( !strcmp(routine,"GSDETV") ) { PG4gsdetv(&tokens[2]); return;}
00212     if ( !strcmp(routine,"GSDETA") ) { PG4gsdeta(&tokens[2]); return;}
00213     if ( !strcmp(routine,"GSDETH") ) { PG4gsdeth(&tokens[2]); return;}
00214     if ( !strcmp(routine,"GSDETD") ) { PG4gsdetd(&tokens[2]); return;}
00215     if ( !strcmp(routine,"GSDETU") ) { PG4gsdetu(&tokens[2]); return;}
00216     if ( !strcmp(routine,"GGCLOS") ) { PG4ggclos(); return;}
00217 }
00218 
00219 void G3fillParams(G4String *tokens, const char *ptypes)
00220 {
00221   //
00222   // G3fillParams
00223   //
00224   // Interpret tokens to fill call parameters, based on parameter types ptypes
00225 
00226     // loop over ptypes
00227     //
00228     G4int i =0, ipt = 0, k = 0;
00229     G4int ni =0, nr = 0, nq = 0;
00230     while (ptypes[i] != '\0')
00231     {
00232         switch (ptypes[i])
00233         {
00234             case 'i':
00235                 Ipar[ni] = atoi(tokens[ipt].data());
00236                 narray = Ipar[ni];
00237                 ni++; ipt++;
00238                 break;
00239             case 'r':
00240                 Rpar[nr] = atof(tokens[ipt].data());
00241                 nr++; ipt++;
00242                 break;
00243             case 's':
00244                 Spar[nq] = tokens[ipt];
00245                 nq++; ipt++;
00246                 break;
00247             case 'I':
00248                 for (k=0; k < narray; k++)
00249                 {
00250                     Ipar[ni] = atoi(tokens[ipt].data());
00251                     ni++; ipt++;
00252                 }
00253                 break;
00254             case 'R':
00255                 for (k=0; k < narray; k++) 
00256                 { 
00257                     Rpar[nr] = atof(tokens[ipt].data()); 
00258                     nr++; ipt++;
00259                 }
00260                 break;
00261             case 'Q':
00262                 // special case of reading three successive R arrays 
00263                 // into one (used in gsmixt)
00264                 //
00265                 narray = 3 * std::abs(narray);
00266                 for (k=0; k < narray; k++) 
00267                 { 
00268                     Rpar[nr] = atof(tokens[ipt].data()); 
00269                     nr++; ipt++;
00270                 }
00271                 break;
00272             case 'S':
00273                 for (k=0; k < narray; k++)
00274                 {
00275                     Spar[nq] = tokens[ipt];
00276                     nq++; ipt++;
00277                 }
00278                 break;
00279             default:
00280                 ofile << "unidentified ptype '" << ptypes[i] << G4endl;
00281         };
00282         i++;
00283     }
00284 }

Generated on Mon May 27 17:47:34 2013 for Geant4 by  doxygen 1.4.7