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 Ev# TrkId Parent 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, EV#, TrkId, and Parent 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.

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

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, mode, status, and unit.

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

BLTrackFile::~BLTrackFile (  ) 

Destructor. Closes the file.

References BLTF_ERROR, file, and status.

00066 {
00067         if(file) fclose(file);
00068         file = 0;
00069         status = BLTF_ERROR;
00070 }


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

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

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

00091 {
00092         G4double weight;        // ignored
00093         return read(pos,time,momentum,PDGid,eventId,trackId,parentId,weight);
00094 }

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.

00099 {
00100         if(status == BLTF_DUMMY) return BLTF_ENDOFFILE;
00101         if(status != BLTF_OK || mode != "r") return BLTF_ERROR;
00102 
00103         char line[1024];
00104         do {
00105                 if(!fgets(line,sizeof(line),file)) {
00106                         status = BLTF_ENDOFFILE;
00107                         return status;
00108                 }
00109                 if(line[0] == '#' && strstr(line,"cm cm cm"))
00110                         unit = cm;
00111         } while(line[0] == '#' || line[0] == '\n');
00112 
00113         float data[12];
00114 
00115         //Ajit Kurup 2008-06-02
00116         //Read in PDGid, eventId,trackId and parentId as integers
00117         //since the float->int conversion doesn't work properly for
00118         //large numbers
00119         int intData[4];
00120 
00121         data[11] = 1.0; // in case weight is missing from file
00122         if(sscanf(line,"%f%f%f%f%f%f%f%d%d%d%d%f",
00123                                 data+0,data+1,data+2,data+3,data+4,data+5,
00124                                 data+6,intData+0,intData+1,intData+2,intData+3,
00125                                 data+11) < 11) {
00126                 printf("BLTrackFile invalid input line - file abandoned:\n%s\n",
00127                                 line);
00128                 fflush(stdout);
00129                 status = BLTF_ERROR;
00130                 return status;
00131         }
00132 
00133         pos[0] = data[0]*unit;
00134         pos[1] = data[1]*unit;
00135         pos[2] = data[2]*unit;
00136         momentum[0] = data[3]*MeV;
00137         momentum[1] = data[4]*MeV;
00138         momentum[2] = data[5]*MeV;
00139         time = data[6]*ns;
00140         PDGid = intData[0];
00141         eventId = intData[1];
00142         trackId = intData[2];
00143         parentId = intData[3];
00144         weight = data[11];
00145 
00146         return status;
00147 }

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