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 are read as doubles, so .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                         G4Exception("BLTrackFIle input","Possible wrong format",
00062                                 JustWarning, "Assumed OK");
00063                         rewind(file);
00064                 }
00065         }
00066 }

BLTrackFile::~BLTrackFile (  ) 

Destructor. Closes the file.

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

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


Member Function Documentation

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

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

References unit.

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

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

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

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

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.

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

void BLTrackFile::flush (  )  [inline]

flush() will flush buffers to the file.

References file.

00099 { 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