BLCMDprintf Class Reference

Inheritance diagram for BLCMDprintf:

BLCommand BLManager::ZSteppingAction BLCallback

List of all members.


Detailed Description

class BLCMDprintf will print track variables and expressions at a given z position.

Public Member Functions

 BLCMDprintf ()
 Constructor.
 BLCMDprintf (BLCMDprintf &r)
virtual G4String commandName ()
 commandName() returns "printf".
virtual int command (BLArgumentVector &argv, BLArgumentMap &namedArgs)
 command() implements the printf command.
virtual void defineNamedArgs ()
 defineNamedArgs() defines the named arguments for this command.
void UserZSteppingAction (const G4Track *track)
 UserZSteppingAction() from BLManager::ZSteppingAction.
virtual void callback (int type)
 callback from BLCallback.

Private Attributes

G4String z
G4String zloop
G4String require
G4String file
G4String format
G4double noNewline
G4String coordinates
BLCoordinateType coordinateType
FILE * fd
std::vector< G4String > expr
BLEvaluator eval

Static Private Attributes

static bool init = true
static std::map< G4String, FILE * > filemap

Constructor & Destructor Documentation

BLCMDprintf::BLCMDprintf (  ) 

Constructor.

References BLCMDTYPE_DATA, BLCOORD_CENTERLINE, coordinates, coordinateType, fd, file, format, noNewline, BLCommand::registerCommand(), require, BLCommand::setDescription(), BLCommand::setSynopsis(), z, and zloop.

Referenced by command().

00073                          : BLCommand(), BLManager::ZSteppingAction(), 
00074                                 BLCallback(), expr(), eval()
00075 {
00076         registerCommand(BLCMDTYPE_DATA);
00077         setSynopsis("prints track variables and expressions");
00078         setDescription("This is an interface to the C printf() function.\n"
00079                 "The first positional argument is the format, and the "
00080                 "following positional arguments are double expressions "
00081                 "printed with the % fields in the format. Up to 16 "
00082                 "expressions can be printed. The print is performed "
00083                 "only if the 'required' expression is nonzero, as each "
00084                 "track reaches one of the Z positions in the 'z' argument "
00085                 "(centerline coordinates). Multiple printf commands "
00086                 "with the same 'file' will be combined into the file "
00087                 "as tracks reach any of their Z positions. More than 16 "
00088                 "expressions can be broken into multiple printf-s with "
00089                 "noNewline=1 for all but the last.\n\n"
00090                 "The following variables can be used in expressions:\n"
00091                 "   x,y,z,t,Px,Py,Pz\n"
00092                 "   PDGid,EvNum,TrkId,ParentId,Weight\n"
00093                 "   Bx,By,Bz, Ex,Ey,Ez\n"
00094                 "   tune (nonzero only for the Tune particle)\n"
00095                 "   reference (nonzero only for the Reference particle)\n"
00096                 "   beam (nonzero for any Beam particle)\n\n"
00097                 "Each value in z and zloop can be an expression using double "
00098                 "constants and the usual C operators and functions.\n\n"
00099                 "Example:\n"
00100                 " printf z=0 'Momentum is %.3f GeV/c' sqrt(Px*Px+Py*Py+Pz*Pz)/1000\n\n"
00101                 "NOTE: if format begins 'Ptot=...' the parsing will think it is "
00102                 "a named argument; put a space before the '=' to avoid that error."
00103         );
00104 
00105         z = "";
00106         zloop = "";
00107         require = "1";
00108         file = "";
00109         fd = stdout;
00110         format = "";
00111         noNewline = 0.0;
00112         coordinates = "Centerline";
00113         coordinateType = BLCOORD_CENTERLINE;
00114 }

BLCMDprintf::BLCMDprintf ( BLCMDprintf r  ) 

References coordinates, coordinateType, fd, file, format, noNewline, require, z, and zloop.

00116                                        : BLCommand(r), 
00117                 BLManager::ZSteppingAction(r), BLCallback(), expr(), eval()
00118 {
00119         z = r.z;
00120         zloop = r.zloop;
00121         require = r.require;
00122         file = r.file;
00123         fd = r.fd;
00124         format = r.format;
00125         noNewline = r.noNewline;
00126         coordinates = r.coordinates;
00127         coordinateType = r.coordinateType;
00128 }


Member Function Documentation

virtual G4String BLCMDprintf::commandName (  )  [inline, virtual]

commandName() returns "printf".

Implements BLCommand.

00054 { return "printf"; }

int BLCMDprintf::command ( BLArgumentVector argv,
BLArgumentMap namedArgs 
) [virtual]

command() implements the printf command.

Implements BLCommand.

References BLCMDprintf(), coordinates, coordinateType, expr, fd, file, filemap, format, BLCoordinates::getCoordinateType(), BLCommand::getList(), BLManager::getObject(), BLCommand::handleNamedArgs(), init, BLCommand::print(), BLCommand::printError(), BLManager::registerCallback(), BLManager::registerZStep(), z, and zloop.

00131 {
00132         BLCMDprintf *p = new BLCMDprintf(defaultPrintf);
00133 
00134         int retval = p->handleNamedArgs(namedArgs);
00135 
00136         p->coordinateType = BLCoordinates::getCoordinateType(p->coordinates);
00137 
00138         p->format = argv[0];
00139         for(unsigned i=1; i<argv.size(); ++i)
00140                 p->expr.push_back(argv[i]);
00141         if(p->file != "") {
00142                 // first, check if file is already open
00143                 if(filemap.count(p->file) > 0) {
00144                         p->fd = filemap[p->file];
00145                 } else {
00146                         p->fd = fopen(p->file.c_str(),"w");
00147                         if(!p->fd) {
00148                                 printError("printf cannot write to file '%s'\n",
00149                                         p->file.c_str());
00150                                 p->fd = stdout;
00151                         } else {
00152                                 filemap[p->file] = p->fd;
00153                         }
00154                 }
00155         }
00156 
00157         // register the z position(s) in the string z
00158         std::vector<G4double> vect = getList(p->z,",");
00159         if(vect.size() > 0) {
00160                 for(unsigned i=0; i<vect.size(); ++i) {
00161                         BLManager::getObject()->registerZStep(vect[i],p,7);
00162                 }
00163         } else if(p->z.size() > 0) {
00164                 printError("printf: Syntax error in z");
00165         }
00166 
00167         // register the z position(s) in the string zloop
00168         vect = getList(p->zloop,",:");
00169         if(vect.size() == 3) {
00170                 if(vect[2] < 1.0*mm || vect[0] > vect[1]) {
00171                         printError("printf: invalid zloop '%s'",
00172                                                         p->zloop.c_str());
00173                 } else {
00174                         while(vect[0] <= vect[1]) {
00175                             BLManager::getObject()->registerZStep(vect[0],p,7);
00176                             vect[0] += vect[2];
00177                         }
00178                 }
00179         } else if(p->zloop.size() > 0) {
00180                 printError("printf: invalid zloop '%s'",p->zloop.c_str());
00181         }
00182 
00183         p->print("");
00184 
00185         if(init) {
00186                 BLManager::getObject()->registerCallback(this,2);
00187                 init = false;
00188         }
00189 
00190         return retval;
00191 }

void BLCMDprintf::defineNamedArgs (  )  [virtual]

defineNamedArgs() defines the named arguments for this command.

Reimplemented from BLCommand.

References BLCommand::argDouble(), BLCommand::argString(), coordinates, file, noNewline, require, z, and zloop.

00194 {
00195         argString(z,"z","Comma-separated list of Z positions for printing (mm)");
00196         argString(zloop,"zloop","Loop in z, first:last:incr (mm)");
00197         argString(require,"require","logical expression for cutting (default=true)");
00198         argString(file,"file","Output filename (default=stdout)");
00199         argString(file,"filename","Synonym for file");
00200         argDouble(noNewline,"noNewline","set nonzero to omit final newline.");
00201         argString(coordinates,"coordinates","Coordinates: global, centerline, or reference (default=c).");
00202 }

void BLCMDprintf::UserZSteppingAction ( const G4Track *  track  )  [virtual]

UserZSteppingAction() from BLManager::ZSteppingAction.

Implements BLManager::ZSteppingAction.

References BEAM, BLCOORD_REFERENCE, coordinateType, eval, BLEvaluator::evaluate(), expr, fd, format, BLManager::getObject(), BLManager::getState(), MAX_EXPR, noNewline, REFERENCE, require, BLEvaluator::setTrackVariables(), and TUNE.

00205 {
00206         BLManagerState state = BLManager::getObject()->getState();
00207         if(coordinateType == BLCOORD_REFERENCE && state != BEAM) return;
00208 
00209         eval.setTrackVariables(track,coordinateType,"",true);
00210 
00211         eval.setVariable("tune",state==TUNE ? 1.0 : 0.0);
00212         eval.setVariable("reference",state==REFERENCE ? 1.0 : 0.0);
00213         eval.setVariable("beam",state==BEAM ? 1.0 : 0.0);
00214 
00215         if(eval.evaluate(require) != 0.0) {
00216                 double a[MAX_EXPR];
00217                 for(unsigned i=0; i<MAX_EXPR; ++i) {
00218                         if(i<expr.size())
00219                                 a[i] = eval.evaluate(expr[i]);
00220                         else
00221                                 a[i] = 0.0;
00222                 }
00223                 fprintf(fd,format.c_str(),a[0],a[1],a[2],a[3],a[4],a[5],a[6],
00224                         a[7],a[8],a[9],a[10],a[11],a[12],a[13],a[14],a[15]);
00225                 if(noNewline == 0.0) fprintf(fd,"\n");
00226         }
00227 }

void BLCMDprintf::callback ( int  type  )  [virtual]

callback from BLCallback.

Reimplemented from BLCallback.

References filemap.

00230 {
00231         // after beam tracking is complete, close all files
00232         if(type != 2) return;
00233         std::map<G4String,FILE*>::iterator i;
00234         for(i=filemap.begin(); i!=filemap.end(); ++i)
00235                 fclose(i->second);
00236         filemap.clear();
00237 }


Member Data Documentation

G4String BLCMDprintf::z [private]

G4String BLCMDprintf::zloop [private]

G4String BLCMDprintf::require [private]

G4String BLCMDprintf::file [private]

G4String BLCMDprintf::format [private]

G4double BLCMDprintf::noNewline [private]

G4String BLCMDprintf::coordinates [private]

FILE* BLCMDprintf::fd [private]

std::vector<G4String> BLCMDprintf::expr [private]

Referenced by command(), and UserZSteppingAction().

Referenced by UserZSteppingAction().

bool BLCMDprintf::init = true [static, private]

Referenced by command().

std::map< G4String, FILE * > BLCMDprintf::filemap [static, private]

Referenced by callback(), and command().


The documentation for this class was generated from the following file:
g4beamline