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

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().

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

BLCMDprintf::BLCMDprintf ( BLCMDprintf r  ) 

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

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


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, BLAsciiFile::fopen(), format, BLCoordinates::getCoordinateType(), BLCommand::getList(), BLManager::getObject(), BLCommand::handleNamedArgs(), init, BLCommand::print(), BLCommand::printError(), BLManager::registerCallback(), BLManager::registerZStep(), z, and zloop.

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

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.

00186 {
00187         argString(z,"z","Comma-separated list of Z positions for printing (mm)");
00188         argString(zloop,"zloop","Loop in z, first:last:incr (mm)");
00189         argString(require,"require","logical expression for cutting (default=true)");
00190         argString(file,"file","Output filename (default=stdout)");
00191         argString(file,"filename","Synonym for file");
00192         argDouble(noNewline,"noNewline","set nonzero to omit final newline.");
00193         argString(coordinates,"coordinates","Coordinates: global, centerline, or reference (default=c).");
00194 }

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.

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

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

callback from BLCallback.

Reimplemented from BLCallback.

References BLAsciiFile::fclose(), and fd.

00222 {
00223         if(type != 2) return;
00224         BLAsciiFile::fclose(fd);
00225 }


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().


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