Reader for MIDAS .mid files. More...
#include <TMidasFile.h>

Public Member Functions | |
| TMidasFile () | |
| default constructor | |
| ~TMidasFile () | |
| destructor | |
| bool | Open (const char *filename) |
| Open input file. | |
| bool | OutOpen (const char *filename) |
| Open output file. | |
| void | Close () |
| Close input file. | |
| void | OutClose () |
| Close output file. | |
| bool | Read (TMidasEvent *event) |
| Read one event from the file. | |
| bool | Write (TMidasEvent *event) |
| Write one event to the output file. | |
| const char * | GetFilename () const |
| Get the name of this file. | |
| int | GetLastErrno () const |
| Get error value for the last file error. | |
| const char * | GetLastError () const |
| Get error text for the last file error. | |
| TMidasFile () | |
| default constructor | |
| ~TMidasFile () | |
| destructor | |
| bool | Open (const char *filename) |
| Open input file. | |
| bool | OutOpen (const char *filename) |
| Open output file. | |
| void | Close () |
| Close input file. | |
| void | OutClose () |
| Close output file. | |
| bool | Read (TMidasEvent *event) |
| Read one event from the file. | |
| bool | Write (TMidasEvent *event) |
| Write one event to the output file. | |
| const char * | GetFilename () const |
| Get the name of this file. | |
| int | GetLastErrno () const |
| Get error value for the last file error. | |
| const char * | GetLastError () const |
| Get error text for the last file error. | |
Protected Attributes | |
| std::string | fFilename |
| name of the currently open file | |
| std::string | fOutFilename |
| name of the currently open file | |
| int | fLastErrno |
| errno from the last operation | |
| std::string | fLastError |
| error string from last errno | |
| bool | fDoByteSwap |
| "true" if file has to be byteswapped | |
| int | fFile |
| open input file descriptor | |
| void * | fGzFile |
| zlib compressed input file reader | |
| void * | fPoFile |
| popen() input file reader | |
| int | fOutFile |
| open output file descriptor | |
| void * | fOutGzFile |
| zlib compressed output file reader | |
Reader for MIDAS .mid files.
Definition at line 14 of file TMidasFile.h.
| TMidasFile::TMidasFile | ( | ) |
default constructor
Definition at line 20 of file TMidasFile.cxx.
References fDoByteSwap, fFile, fGzFile, fLastErrno, fOutFile, fOutGzFile, and fPoFile.
00021 { 00022 uint32_t endian = 0x12345678; 00023 00024 fFile = -1; 00025 fGzFile = NULL; 00026 fPoFile = NULL; 00027 fLastErrno = 0; 00028 00029 fOutFile = -1; 00030 fOutGzFile = NULL; 00031 00032 fDoByteSwap = *(char*)(&endian) != 0x78; 00033 }
| TMidasFile::~TMidasFile | ( | ) |
destructor
Definition at line 35 of file TMidasFile.cxx.
References Close(), and OutClose().

| TMidasFile::TMidasFile | ( | ) |
default constructor
| TMidasFile::~TMidasFile | ( | ) |
destructor
| void TMidasFile::Close | ( | ) |
Close input file.
| void TMidasFile::Close | ( | ) |
Close input file.
Definition at line 374 of file TMidasFile.cxx.
References fFile, fFilename, fGzFile, and fPoFile.
Referenced by Open(), TRootanaEventLoop::ProcessMidasFile(), ProcessMidasFile(), and ~TMidasFile().
00375 { 00376 if (fPoFile) 00377 pclose((FILE*)fPoFile); 00378 fPoFile = NULL; 00379 #ifdef HAVE_ZLIB 00380 if (fGzFile) 00381 gzclose(*(gzFile*)fGzFile); 00382 fGzFile = NULL; 00383 #endif 00384 if (fFile > 0) 00385 close(fFile); 00386 fFile = -1; 00387 fFilename = ""; 00388 }

| const char* TMidasFile::GetFilename | ( | ) | const [inline] |
| const char* TMidasFile::GetFilename | ( | ) | const [inline] |
| int TMidasFile::GetLastErrno | ( | ) | const [inline] |
Get error value for the last file error.
Definition at line 30 of file TMidasFile.h.
References fLastErrno.
| int TMidasFile::GetLastErrno | ( | ) | const [inline] |
Get error value for the last file error.
Definition at line 30 of file TMidasFile.h.
References fLastErrno.
Referenced by ProcessMidasFile().

| const char* TMidasFile::GetLastError | ( | ) | const [inline] |
Get error text for the last file error.
Definition at line 31 of file TMidasFile.h.
References fLastError.
| const char* TMidasFile::GetLastError | ( | ) | const [inline] |
Get error text for the last file error.
Definition at line 31 of file TMidasFile.h.
References fLastError.
Referenced by ProcessMidasFile().

| bool TMidasFile::Open | ( | const char * | filename | ) |
Open input file.
| bool TMidasFile::Open | ( | const char * | filename | ) |
Open input file.
Open a midas .mid file with given file name.
Remote files can be accessed using these special file names:
Examples:
| [in] | filename | The file to open. |
Definition at line 50 of file TMidasFile.cxx.
References Close(), fFile, fFilename, fGzFile, fLastErrno, fLastError, fPoFile, hasSuffix(), and O_LARGEFILE.
Referenced by main(), TRootanaEventLoop::ProcessMidasFile(), and ProcessMidasFile().
00051 { 00052 /// Open a midas .mid file with given file name. 00053 /// 00054 /// Remote files can be accessed using these special file names: 00055 /// - pipein://command - read data produced by given command, see examples below 00056 /// - ssh://username\@hostname/path/file.mid - read remote file through an ssh pipe 00057 /// - ssh://username\@hostname/path/file.mid.gz and file.mid.bz2 - same for compressed files 00058 /// - dccp://path/file.mid (also file.mid.gz and file.mid.bz2) - read data from dcache, requires dccp in the PATH 00059 /// 00060 /// Examples: 00061 /// - ./event_dump.exe /ladd/data9/t2km11/data/run02696.mid.gz - read normal compressed file 00062 /// - ./event_dump.exe ssh://ladd09//ladd/data9/t2km11/data/run02696.mid.gz - read compressed file through ssh to ladd09 (note double "/") 00063 /// - ./event_dump.exe pipein://"cat /ladd/data9/t2km11/data/run02696.mid.gz | gzip -dc" - read data piped from a command or script (note quotes) 00064 /// - ./event_dump.exe pipein://"gzip -dc /ladd/data9/t2km11/data/run02696.mid.gz" - another way to read compressed files 00065 /// - ./event_dump.exe dccp:///pnfs/triumf.ca/data/t2km11/aug2008/run02837.mid.gz - read file directly from a dcache pool (note triple "/") 00066 /// 00067 /// \param [in] filename The file to open. 00068 /// \returns "true" for succes, "false" for error, use GetLastError() to see why 00069 00070 if (fFile > 0) 00071 Close(); 00072 00073 fFilename = filename; 00074 00075 std::string pipe; 00076 00077 // Do we need these? 00078 //signal(SIGPIPE,SIG_IGN); // crash if reading from closed pipe 00079 //signal(SIGXFSZ,SIG_IGN); // crash if reading from file >2GB without O_LARGEFILE 00080 00081 if (strncmp(filename, "ssh://", 6) == 0) 00082 { 00083 const char* name = filename + 6; 00084 const char* s = strstr(name, "/"); 00085 00086 if (s == NULL) 00087 { 00088 fLastErrno = -1; 00089 fLastError = "TMidasFile::Open: Invalid ssh:// URI. Should be: ssh://user@host/file/path/..."; 00090 return false; 00091 } 00092 00093 const char* remoteFile = s + 1; 00094 00095 std::string remoteHost; 00096 for (s=name; *s != '/'; s++) 00097 remoteHost += *s; 00098 00099 pipe = "ssh -e none -T -x -n "; 00100 pipe += remoteHost; 00101 pipe += " dd if="; 00102 pipe += remoteFile; 00103 pipe += " bs=1024k"; 00104 00105 if (hasSuffix(remoteFile,".gz")) 00106 pipe += " | gzip -dc"; 00107 else if (hasSuffix(remoteFile,".bz2")) 00108 pipe += " | bzip2 -dc"; 00109 } 00110 else if (strncmp(filename, "dccp://", 7) == 0) 00111 { 00112 const char* name = filename + 7; 00113 00114 pipe = "dccp "; 00115 pipe += name; 00116 pipe += " /dev/fd/1"; 00117 00118 if (hasSuffix(filename,".gz")) 00119 pipe += " | gzip -dc"; 00120 else if (hasSuffix(filename,".bz2")) 00121 pipe += " | bzip2 -dc"; 00122 } 00123 else if (strncmp(filename, "pipein://", 9) == 0) 00124 { 00125 pipe = filename + 9; 00126 } 00127 #if 0 // read compressed files using the zlib library 00128 else if (hasSuffix(filename, ".gz")) 00129 { 00130 pipe = "gzip -dc "; 00131 pipe += filename; 00132 } 00133 #endif 00134 else if (hasSuffix(filename, ".bz2")) 00135 { 00136 pipe = "bzip2 -dc "; 00137 pipe += filename; 00138 } 00139 00140 if (pipe.length() > 0) 00141 { 00142 fprintf(stderr,"TMidasFile::Open: Reading from pipe: %s\n", pipe.c_str()); 00143 00144 fPoFile = popen(pipe.c_str(), "r"); 00145 00146 if (fPoFile == NULL) 00147 { 00148 fLastErrno = errno; 00149 fLastError = strerror(errno); 00150 return false; 00151 } 00152 00153 fFile = fileno((FILE*)fPoFile); 00154 } 00155 else 00156 { 00157 #ifndef O_LARGEFILE 00158 #define O_LARGEFILE 0 00159 #endif 00160 00161 fFile = open(filename, O_RDONLY | O_LARGEFILE); 00162 00163 if (fFile <= 0) 00164 { 00165 fLastErrno = errno; 00166 fLastError = strerror(errno); 00167 return false; 00168 } 00169 00170 if (hasSuffix(filename, ".gz")) 00171 { 00172 // this is a compressed file 00173 #ifdef HAVE_ZLIB 00174 fGzFile = new gzFile; 00175 (*(gzFile*)fGzFile) = gzdopen(fFile,"rb"); 00176 if ((*(gzFile*)fGzFile) == NULL) 00177 { 00178 fLastErrno = -1; 00179 fLastError = "zlib gzdopen() error"; 00180 return false; 00181 } 00182 #else 00183 fLastErrno = -1; 00184 fLastError = "Do not know how to read compressed MIDAS files"; 00185 return false; 00186 #endif 00187 } 00188 } 00189 00190 return true; 00191 }


| void TMidasFile::OutClose | ( | ) |
Close output file.
| void TMidasFile::OutClose | ( | ) |
Close output file.
Definition at line 390 of file TMidasFile.cxx.
References fOutFile, fOutFilename, and fOutGzFile.
Referenced by OutOpen(), ProcessMidasFile(), and ~TMidasFile().
00391 { 00392 #ifdef HAVE_ZLIB 00393 if (fOutGzFile) { 00394 gzflush(*(gzFile*)fOutGzFile, Z_FULL_FLUSH); 00395 gzclose(*(gzFile*)fOutGzFile); 00396 } 00397 fOutGzFile = NULL; 00398 #endif 00399 if (fOutFile > 0) 00400 close(fOutFile); 00401 fOutFile = -1; 00402 fOutFilename = ""; 00403 }

| bool TMidasFile::OutOpen | ( | const char * | filename | ) |
Open output file.
| bool TMidasFile::OutOpen | ( | const char * | filename | ) |
Open output file.
Open a midas .mid file for OUTPUT with given file name.
Remote files not yet implemented
| [in] | filename | The file to open. |
Definition at line 193 of file TMidasFile.cxx.
References fLastErrno, fLastError, fOutFile, fOutFilename, fOutGzFile, hasSuffix(), O_LARGEFILE, and OutClose().
Referenced by ProcessMidasFile().
00194 { 00195 /// Open a midas .mid file for OUTPUT with given file name. 00196 /// 00197 /// Remote files not yet implemented 00198 /// 00199 /// \param [in] filename The file to open. 00200 /// \returns "true" for succes, "false" for error, use GetLastError() to see why 00201 00202 if (fOutFile > 0) 00203 OutClose(); 00204 00205 fOutFilename = filename; 00206 00207 printf ("Attempting normal open of file %s\n", filename); 00208 //fOutFile = open(filename, O_CREAT | O_WRONLY | O_LARGEFILE , S_IRUSR| S_IWUSR | S_IRGRP | S_IROTH ); 00209 //fOutFile = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 0644); 00210 fOutFile = open (filename, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, 0644); 00211 00212 if (fOutFile <= 0) 00213 { 00214 fLastErrno = errno; 00215 fLastError = strerror(errno); 00216 return false; 00217 } 00218 00219 printf("Opened output file %s ; return fOutFile is %i\n",filename,fOutFile); 00220 00221 if (hasSuffix(filename, ".gz")) 00222 // (hasSuffix(filename, ".dummy")) 00223 { 00224 // this is a compressed file 00225 #ifdef HAVE_ZLIB 00226 fOutGzFile = new gzFile; 00227 *(gzFile*)fOutGzFile = gzdopen(fOutFile,"wb"); 00228 if ((*(gzFile*)fOutGzFile) == NULL) 00229 { 00230 fLastErrno = -1; 00231 fLastError = "zlib gzdopen() error"; 00232 return false; 00233 } 00234 printf("Opened gz file successfully\n"); 00235 if (1) 00236 { 00237 if (gzsetparams(*(gzFile*)fOutGzFile, 1, Z_DEFAULT_STRATEGY) != Z_OK) { 00238 printf("Cannot set gzparams\n"); 00239 fLastErrno = -1; 00240 fLastError = "zlib gzsetparams() error"; 00241 return false; 00242 } 00243 printf("setparams for gz file successfully\n"); 00244 } 00245 #else 00246 fLastErrno = -1; 00247 fLastError = "Do not know how to write compressed MIDAS files"; 00248 return false; 00249 #endif 00250 } 00251 return true; 00252 }


| bool TMidasFile::Read | ( | TMidasEvent * | event | ) |
Read one event from the file.
| bool TMidasFile::Read | ( | TMidasEvent * | event | ) |
Read one event from the file.
| [in] | midasEvent | Pointer to an empty TMidasEvent |
Definition at line 279 of file TMidasFile.cxx.
References TMidasEvent::Clear(), fDoByteSwap, fFile, fGzFile, fLastErrno, fLastError, TMidasEvent::GetData(), TMidasEvent::GetDataSize(), TMidasEvent::GetEventHeader(), TMidasEvent::IsGoodSize(), readpipe(), TMidasEvent::SwapBytes(), and TMidasEvent::SwapBytesEventHeader().
Referenced by main(), TRootanaEventLoop::ProcessMidasFile(), and ProcessMidasFile().
00280 { 00281 /// \param [in] midasEvent Pointer to an empty TMidasEvent 00282 /// \returns "true" for success, "false" for failure, see GetLastError() to see why 00283 00284 midasEvent->Clear(); 00285 00286 int rd = 0; 00287 00288 if (fGzFile) 00289 #ifdef HAVE_ZLIB 00290 rd = gzread(*(gzFile*)fGzFile, (char*)midasEvent->GetEventHeader(), sizeof(TMidas_EVENT_HEADER)); 00291 #else 00292 assert(!"Cannot get here"); 00293 #endif 00294 else 00295 rd = readpipe(fFile, (char*)midasEvent->GetEventHeader(), sizeof(TMidas_EVENT_HEADER)); 00296 00297 if (rd == 0) 00298 { 00299 fLastErrno = 0; 00300 fLastError = "EOF"; 00301 return false; 00302 } 00303 else if (rd != sizeof(TMidas_EVENT_HEADER)) 00304 { 00305 fLastErrno = errno; 00306 fLastError = strerror(errno); 00307 return false; 00308 } 00309 00310 if (fDoByteSwap) 00311 midasEvent->SwapBytesEventHeader(); 00312 00313 if (!midasEvent->IsGoodSize()) 00314 { 00315 fLastErrno = -1; 00316 fLastError = "Invalid event size"; 00317 return false; 00318 } 00319 00320 if (fGzFile) 00321 #ifdef HAVE_ZLIB 00322 rd = gzread(*(gzFile*)fGzFile, midasEvent->GetData(), midasEvent->GetDataSize()); 00323 #else 00324 assert(!"Cannot get here"); 00325 #endif 00326 else 00327 rd = readpipe(fFile, midasEvent->GetData(), midasEvent->GetDataSize()); 00328 00329 if (rd != (int)midasEvent->GetDataSize()) 00330 { 00331 fLastErrno = errno; 00332 fLastError = strerror(errno); 00333 return false; 00334 } 00335 00336 midasEvent->SwapBytes(false); 00337 00338 return true; 00339 }


| bool TMidasFile::Write | ( | TMidasEvent * | event | ) |
Write one event to the output file.
| bool TMidasFile::Write | ( | TMidasEvent * | event | ) |
Write one event to the output file.
Definition at line 341 of file TMidasFile.cxx.
References fOutFile, fOutGzFile, TMidasEvent::GetData(), TMidasEvent::GetDataSize(), and TMidasEvent::GetEventHeader().
Referenced by ProcessMidasFile().
00342 { 00343 int wr = -2; 00344 00345 if (fOutGzFile) 00346 #ifdef HAVE_ZLIB 00347 wr = gzwrite(*(gzFile*)fOutGzFile, (char*)midasEvent->GetEventHeader(), sizeof(TMidas_EVENT_HEADER)); 00348 #else 00349 assert(!"Cannot get here"); 00350 #endif 00351 else 00352 wr = write(fOutFile, (char*)midasEvent->GetEventHeader(), sizeof(TMidas_EVENT_HEADER)); 00353 00354 if(wr != sizeof(TMidas_EVENT_HEADER)){ 00355 printf("TMidasFile: error on write event header, return %i, size requested %lu\n",wr,sizeof(TMidas_EVENT_HEADER)); 00356 return false; 00357 } 00358 00359 printf("Written event header to outfile , return is %i\n",wr); 00360 00361 if (fOutGzFile) 00362 #ifdef HAVE_ZLIB 00363 wr = gzwrite(*(gzFile*)fOutGzFile, (char*)midasEvent->GetData(), midasEvent->GetDataSize()); 00364 #else 00365 assert(!"Cannot get here"); 00366 #endif 00367 else 00368 wr = write(fOutFile, (char*)midasEvent->GetData(), midasEvent->GetDataSize()); 00369 printf("Written event to outfile , return is %d\n",wr); 00370 00371 return wr; 00372 }


bool TMidasFile::fDoByteSwap [protected] |
"true" if file has to be byteswapped
Definition at line 41 of file TMidasFile.h.
Referenced by Read(), and TMidasFile().
int TMidasFile::fFile [protected] |
open input file descriptor
Definition at line 43 of file TMidasFile.h.
Referenced by Close(), Open(), Read(), and TMidasFile().
std::string TMidasFile::fFilename [protected] |
name of the currently open file
Definition at line 35 of file TMidasFile.h.
Referenced by Close(), GetFilename(), and Open().
void * TMidasFile::fGzFile [protected] |
zlib compressed input file reader
Definition at line 44 of file TMidasFile.h.
Referenced by Close(), Open(), Read(), and TMidasFile().
int TMidasFile::fLastErrno [protected] |
errno from the last operation
Definition at line 38 of file TMidasFile.h.
Referenced by GetLastErrno(), Open(), OutOpen(), Read(), and TMidasFile().
std::string TMidasFile::fLastError [protected] |
error string from last errno
Definition at line 39 of file TMidasFile.h.
Referenced by GetLastError(), Open(), OutOpen(), and Read().
int TMidasFile::fOutFile [protected] |
open output file descriptor
Definition at line 46 of file TMidasFile.h.
Referenced by OutClose(), OutOpen(), TMidasFile(), and Write().
std::string TMidasFile::fOutFilename [protected] |
name of the currently open file
Definition at line 36 of file TMidasFile.h.
Referenced by OutClose(), and OutOpen().
void * TMidasFile::fOutGzFile [protected] |
zlib compressed output file reader
Definition at line 47 of file TMidasFile.h.
Referenced by OutClose(), OutOpen(), TMidasFile(), and Write().
void * TMidasFile::fPoFile [protected] |
popen() input file reader
Definition at line 45 of file TMidasFile.h.
Referenced by Close(), Open(), and TMidasFile().
1.6.1