BLTrackFile Class Reference

#include <BLTrackFile.hh>

List of all members.


Detailed Description

class BLTrackFile creates or reads an ASCII file containing tracks.

The file format is ASCII: Lines begining with # are comments First line is a structured comment (if not present the input routine issues a warning): BLTrackFile ... user comment... Second line is a comment giving the column names: x y z Px Py Pz t PDGid EventID TrackID ParentID weight Third line is a comment giving the units: cm cm cm MeV/c MeV/c MeV/c ns - - - - - OR: mm mm mm MeV/c MeV/c MeV/c ns - - - - - (When writing, mm are used; on reading, mm are assumed, but a comment line containing "cm cm cm" switches to cm.) Thereafter follow the tracks, one per line. While the input routine can handle initial spaces in the first column, it is STRONGLY suggested you not put any there (so cut/grep will work). Any fixed or floating-point format will do; PDGid, EventID, TrackID, and ParentID are integers (but .00 can be appended). Common PDGid-s: e- 11 e+ -11 mu- 13 mu+ -13 pi+ 211 pi- -211 proton 2212 anti_proton -2212 neutron 2112 anti_neutron -2112 gamma 22

Public Member Functions

 BLTrackFile ()
 Default constructor. Reads return immediate EOF; writes do nothing.
 BLTrackFile (G4String filename, G4String comment, G4String mode)
 Constructor. Opens the file; mode="r" or "w". comment is an identifying comment in the first line of the file (ignored for mode=r).
 ~BLTrackFile ()
 Destructor. Closes the file.
void setLengthUnit (G4double mmorcm)
 setLengthUnit() will select either mm or cm. default is mm.
BLTrackFileStatus write (G4ThreeVector &pos, G4double time, G4ThreeVector &momentum, int PDGid, int eventId, int trackId, int parentId, G4double weight=1.0)
 write() will write one track to the file (mode must have been "w"). Units are standard geant4 internal units. Note that weight is optional.
BLTrackFileStatus read (G4ThreeVector &pos, G4double &time, G4ThreeVector &momentum, int &PDGid, int &eventId, int &trackId, int &parentId)
 read() will read one track from the file (mode must have been "r"). Units are standard geant4 internal units. Use the other version of read() if you want to use the weight.
BLTrackFileStatus read (G4ThreeVector &pos, G4double &time, G4ThreeVector &momentum, int &PDGid, int &eventId, int &trackId, int &parentId, G4double &weight)
 read() will read one track from the file (mode must have been "r"). Units are standard geant4 internal units.
void flush ()
 flush() will flush buffers to the file.

Private Attributes

BLTrackFileStatus status
FILE * file
G4String mode
G4double unit


Constructor & Destructor Documentation

BLTrackFile::BLTrackFile (  ) 

Default constructor. Reads return immediate EOF; writes do nothing.

References BLTF_DUMMY, file, mode, status, and unit.

00024 {
00025         status = BLTF_DUMMY;
00026         mode = "";
00027         unit = mm;
00028         file = 0;
00029 }

BLTrackFile::BLTrackFile ( G4String  filename,
G4String  comment,
G4String  mode 
)

Constructor. Opens the file; mode="r" or "w". comment is an identifying comment in the first line of the file (ignored for mode=r).

References BLTF_ENDOFFILE, BLTF_ERROR, BLTF_OK, file, BLAsciiFile::fopen(), mode, status, and unit.

00032 {
00033         status = BLTF_ERROR;
00034         mode = _mode;
00035         unit = mm;
00036         if(mode == "w")
00037                 file = BLAsciiFile::fopen(filename);
00038         else
00039                 file = fopen(filename.c_str(),"r");
00040         if(!file || (mode != "r" && mode != "w")) {
00041                 fprintf(stderr,"BLTrackFile: cannot open '%s' in mode '%s'",
00042                                         filename.c_str(),mode.c_str());
00043                 return;
00044         }
00045         status = BLTF_OK;
00046         if(mode == "w") {
00047                 fprintf(file,"#BLTrackFile %s\n",comment.c_str());
00048                 fprintf(file,"#x y z Px Py Pz t PDGid EventID TrackID ParentID Weight\n");
00049                 if(unit == mm)
00050                     fprintf(file,"#mm mm mm MeV/c MeV/c MeV/c ns - - - - -\n");
00051                 else
00052                     fprintf(file,"#cm cm cm MeV/c MeV/c MeV/c ns - - - - -\n");
00053         } else if(mode == "r") {
00054                 char line[512];
00055                 if(!fgets(line,sizeof(line),file)) {
00056                         status = BLTF_ENDOFFILE;
00057                         return;
00058                 }
00059                 G4String s(line);
00060                 if(s.substr(0,12) != "#BLTrackFile") {
00061                         printf("*** BLTrackFile: WARNING input file '%s'"
00062                                " may be wrong format -- assumed OK\n",
00063                                         filename.c_str());
00064                         rewind(file);
00065                 }
00066         }
00067 }

BLTrackFile::~BLTrackFile (  ) 

Destructor. Closes the file.

References BLTF_ERROR, BLAsciiFile::fclose(), file, and status.

00070 {
00071         if(file) BLAsciiFile::fclose(file);
00072         file = 0;
00073         status = BLTF_ERROR;
00074 }


Member Function Documentation

void BLTrackFile::setLengthUnit ( G4double  mmorcm  )  [inline]

setLengthUnit() will select either mm or cm. default is mm.

References unit.

00076 { unit = mmorcm; }

BLTrackFileStatus BLTrackFile::write ( G4ThreeVector &  pos,
G4double  time,
G4ThreeVector &  momentum,
int  PDGid,
int  eventId,
int  trackId,
int  parentId,
G4double  weight = 1.0 
)

write() will write one track to the file (mode must have been "w"). Units are standard geant4 internal units. Note that weight is optional.

References BLTF_DUMMY, BLTF_ERROR, BLTF_OK, file, mode, status, and unit.

Referenced by TrackFileNTuple::appendRow().

00079 {
00080         if(status == BLTF_DUMMY) return BLTF_OK;
00081         if(status != BLTF_OK || mode != "w") return BLTF_ERROR;
00082 
00083         // left justified to avoid pesky initial spaces in column 1
00084         fprintf(file,"%.6g %.6g %.6g ",pos[0]/unit,pos[1]/unit,pos[2]/unit);
00085         fprintf(file,"%.6g %.6g %.6g ",momentum[0]/MeV,momentum[1]/MeV,
00086                                                 momentum[2]/MeV);
00087         fprintf(file,"%.6g %d %d %d %d %.6g\n",time/ns,PDGid,eventId,
00088                                                 trackId,parentId,weight);
00089         return status;
00090 }

BLTrackFileStatus BLTrackFile::read ( G4ThreeVector &  pos,
G4double &  time,
G4ThreeVector &  momentum,
int &  PDGid,
int &  eventId,
int &  trackId,
int &  parentId 
)

read() will read one track from the file (mode must have been "r"). Units are standard geant4 internal units. Use the other version of read() if you want to use the weight.

Referenced by BLCMDbeam::nextBeamEvent().

00095 {
00096         G4double weight;        // ignored
00097         return read(pos,time,momentum,PDGid,eventId,trackId,parentId,weight);
00098 }

BLTrackFileStatus BLTrackFile::read ( G4ThreeVector &  pos,
G4double &  time,
G4ThreeVector &  momentum,
int &  PDGid,
int &  eventId,
int &  trackId,
int &  parentId,
G4double &  weight 
)

read() will read one track from the file (mode must have been "r"). Units are standard geant4 internal units.

References BLTF_DUMMY, BLTF_ENDOFFILE, BLTF_ERROR, BLTF_OK, file, mode, status, and unit.

00103 {
00104         if(status == BLTF_DUMMY) return BLTF_ENDOFFILE;
00105         if(status != BLTF_OK || mode != "r") return BLTF_ERROR;
00106 
00107         char line[1024];
00108         do {
00109                 if(!fgets(line,sizeof(line),file)) {
00110                         status = BLTF_ENDOFFILE;
00111                         return status;
00112                 }
00113                 if(line[0] == '#' && strstr(line,"cm cm cm"))
00114                         unit = cm;
00115         } while(line[0] == '#' || line[0] == '\n' || line[0] == '\r' || 
00116                                                         line[0] == '\0');
00117 
00118         // Tom Roberts 2010-04-05
00119         // Read into doubles, and then convert. This permits event numbers 1.0e7
00120         // and 10000001 to be read as accurately as possible.
00121         double data[12];
00122 
00123         //Ajit Kurup 2008-06-02
00124         //Read in PDGid, eventId,trackId and parentId as integers
00125         //since the float->int conversion doesn't work properly for
00126         //large numbers
00127 
00128         data[11] = 1.0; // in case weight is missing from file
00129         if(sscanf(line,"%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",
00130                                 data+0,data+1,data+2,data+3,data+4,data+5,
00131                                 data+6,data+7,data+8,data+9,data+10,
00132                                 data+11) < 11) {
00133                 printf("BLTrackFile invalid input line (file abandoned): '%s'\n",
00134                                 line);
00135                 fflush(stdout);
00136                 status = BLTF_ERROR;
00137                 return status;
00138         }
00139 
00140         pos[0] = data[0]*unit;
00141         pos[1] = data[1]*unit;
00142         pos[2] = data[2]*unit;
00143         momentum[0] = data[3]*MeV;
00144         momentum[1] = data[4]*MeV;
00145         momentum[2] = data[5]*MeV;
00146         time = data[6]*ns;
00147         PDGid = (int)data[7];
00148         eventId = (int)data[8];
00149         trackId = (int)data[9];
00150         parentId = (int)data[10];
00151         weight = data[11];
00152 
00153         return status;
00154 }

void BLTrackFile::flush (  )  [inline]

flush() will flush buffers to the file.

References file.

00098 { fflush(file); }


Member Data Documentation

Referenced by BLTrackFile(), read(), write(), and ~BLTrackFile().

FILE* BLTrackFile::file [private]

G4String BLTrackFile::mode [private]

Referenced by BLTrackFile(), read(), and write().

G4double BLTrackFile::unit [private]


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