#include <BLTrackFile.hh>
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 |
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 }
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,"%.6g %.6g %.6g ",pos[0]/unit,pos[1]/unit,pos[2]/unit); 00081 fprintf(file,"%.6g %.6g %.6g ",momentum[0]/MeV,momentum[1]/MeV, 00082 momentum[2]/MeV); 00083 fprintf(file,"%.6g %d %d %d %d %.6g\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' || line[0] == '\r' || 00112 line[0] == '\0'); 00113 00114 // Tom Roberts 2010-04-05 00115 // Read into doubles, and then convert. This permits event numbers 1.0e7 00116 // and 10000001 to be read as accurately as possible. 00117 double data[12]; 00118 00119 //Ajit Kurup 2008-06-02 00120 //Read in PDGid, eventId,trackId and parentId as integers 00121 //since the float->int conversion doesn't work properly for 00122 //large numbers 00123 00124 data[11] = 1.0; // in case weight is missing from file 00125 if(sscanf(line,"%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", 00126 data+0,data+1,data+2,data+3,data+4,data+5, 00127 data+6,data+7,data+8,data+9,data+10, 00128 data+11) < 11) { 00129 printf("BLTrackFile invalid input line (file abandoned): '%s'\n", 00130 line); 00131 fflush(stdout); 00132 status = BLTF_ERROR; 00133 return status; 00134 } 00135 00136 pos[0] = data[0]*unit; 00137 pos[1] = data[1]*unit; 00138 pos[2] = data[2]*unit; 00139 momentum[0] = data[3]*MeV; 00140 momentum[1] = data[4]*MeV; 00141 momentum[2] = data[5]*MeV; 00142 time = data[6]*ns; 00143 PDGid = (int)data[7]; 00144 eventId = (int)data[8]; 00145 trackId = (int)data[9]; 00146 parentId = (int)data[10]; 00147 weight = data[11]; 00148 00149 return status; 00150 }
void BLTrackFile::flush | ( | ) | [inline] |
BLTrackFileStatus BLTrackFile::status [private] |
Referenced by BLTrackFile(), read(), write(), and ~BLTrackFile().
FILE* BLTrackFile::file [private] |
Referenced by BLTrackFile(), flush(), read(), write(), and ~BLTrackFile().
G4String BLTrackFile::mode [private] |
Referenced by BLTrackFile(), read(), and write().
G4double BLTrackFile::unit [private] |
Referenced by BLTrackFile(), read(), setLengthUnit(), and write().