// BLCMDprobefield.cc /* This source file is part of G4beamline, http://g4beamline.muonsinc.com Copyright (C) 2003,2004,2005,2006 by Tom Roberts, all rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. http://www.gnu.org/copyleft/gpl.html */ #include #include #include #ifndef WIN32 #include #endif #include "BLCommand.hh" #include "BLManager.hh" #include "BLGlobalField.hh" extern void g4bl_exit(int); /** class BLCMDprobefield is a command to print fields at specified points * **/ class BLCMDprobefield : public BLCommand, public BLCallback { G4String file; G4String outputFile; FILE *in; FILE *out; G4int exit; std::vector args; public: BLCMDprobefield(); ~BLCMDprobefield() { } G4String commandName() { return "probefield"; } int command(BLArgumentVector& argv, BLArgumentMap& namedArgs); void defineNamedArgs(); /// callback() from BLCallback. void callback(int type); void handleLine(G4String line); }; BLCMDprobefield defaultProbeField; // registers the command, and holds // default values of the arguments. BLCMDprobefield::BLCMDprobefield() { registerCommand(BLCMDTYPE_DATA); setSynopsis("Prints B and E fields at specified points."); setDescription("Intended primarily for debugging. Prints Bx,By,Bz " "in Tesla, and Ex,Ey,Ez in MegaVolts/meter. " "Each input line is x,y,z,t separated by spaces or commas; " "omitted values are set to 0.0.\n\n" "Field Values are printed after the reference particle is " "tracked. Only global coordinates are used.\n\n" "Positional arguments are used as input lines before reading " "the file."); file = "-"; outputFile = "-"; in = 0; out = 0; exit = 0; } int BLCMDprobefield::command(BLArgumentVector& argv, BLArgumentMap& namedArgs) { BLCMDprobefield *p = new BLCMDprobefield(defaultProbeField); int retval = p->handleNamedArgs(namedArgs); if(p->file == "-") { p->in = stdin; } else { p->in = fopen(p->file.c_str(),"r"); if(!p->in) printError("probefield cannot open '%s'", p->file.c_str()); ++retval; } if(p->outputFile == "-") { p->out = stdout; } else { p->out = fopen(p->outputFile.c_str(),"w"); if(!p->out) printError("probefield cannot write '%s'", p->outputFile.c_str()); ++retval; } p->args = argv; // register the object to be called back to do its print. // 1 => after reference particle is tracked. BLManager::getObject()->registerCallback(p,1); return retval; } void BLCMDprobefield::defineNamedArgs() { argString(file,"file","Filename for reading list of points (- = stdin)"); argString(outputFile,"outputFile","Filename for output (- = stdout)"); argInt(exit,"exit","Set nonzero to exit after printing field"); } void BLCMDprobefield::callback(int callback_type) { printf("probefield is printing field values:\n"); fprintf(out,"#x y z t Bx By Bz Ex Ey Ez\n"); for(unsigned i=0; iGetFieldValue(pos,field); fprintf(out,"%.3f %.3f %.3f %.3f %.6f %.6f %.6f %.6f %.6f %.6f\n", pos[0]/mm,pos[1]/mm,pos[2]/mm,pos[3]/ns, field[0]/tesla,field[1]/tesla,field[2]/tesla, field[3]/(megavolt/meter),field[4]/(megavolt/meter), field[5]/(megavolt/meter)); }