00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 #include <sys/types.h>
00112 #include <sys/stat.h>
00113 #include <sys/mman.h>
00114 #include <time.h>
00115 #include <fcntl.h>
00116 #include <stdlib.h>
00117 #include <sys/uio.h>
00118 #include <unistd.h>
00119 #include "RichEventReader.hh"
00120 #include "RICH_Reader.hh"
00121
00122 using namespace OLDEVP;
00123
00124 RichEventReader *getRichEventReader(int fd, long offset, int MMap)
00125 {
00126 RichEventReader *er = new RichEventReader();
00127 if (MMap) {
00128 er->InitEventReader(fd, offset, MMap);
00129 if(er->errorNo())
00130 {
00131 cout << er->errstr().c_str() << endl;
00132 cout << (er->err_string[er->errorNo()-1]) << endl;
00133 delete er;
00134 return NULL;
00135 }
00136 }
00137 else {
00138 er->InitEventReader(fd, offset);
00139 if(er->errorNo())
00140 {
00141 cout << er->errstr().c_str() << endl;
00142 cout << (er->err_string[er->errorNo()-1]) << endl;
00143 delete er;
00144 return NULL;
00145 }
00146 }
00147
00148 return er;
00149 }
00150
00151
00152
00153 void RichEventReader::InitEventReader(int fdes, long offset, int MMap)
00154 {
00155
00156 #define MX_MAP_SIZE 32768*4
00157
00158 off_t FileLength;
00159
00160
00161
00162
00163 strcpy(err_string[0],"ERROR: FILE");
00164 strcpy(err_string[1],"ERROR: CRC");
00165 strcpy(err_string[2],"ERROR: SWAP");
00166 strcpy(err_string[3],"ERROR: BANK");
00167 strcpy(err_string[4],"ERROR: MEM");
00168 strcpy(err_string[5],"ERROR: NOT DATA BANK");
00169 strcpy(err_string[6],"ERROR: BAD ARG");
00170 strcpy(err_string[7],"ERROR: ENDR ENCOUNTERED");
00171 strcpy(err_string[8],"ERROR: BAD HEADER");
00172 strcpy(err_string[9],"INFO: MISSING BANK");
00173 strcpy(err_string[10],"INFO: END OF FILE");
00174
00175
00176 fd = fdes;
00177 struct stat buf;
00178 next_event_offset = 0;
00179 if (fstat(fd,&buf)<0){
00180 perror("error in DaqOpenTag");
00181 ERROR(ERR_FILE);
00182 next_event_offset = -1;
00183 }
00184 FileLength = buf.st_size;
00185
00186 Bank_RICP lr;
00187
00188
00189 long pagesize = sysconf(_SC_PAGESIZE);
00190 int mmap_offset = (offset/pagesize)*pagesize;
00191
00192 if(mmap_offset < 0) ERROR(ERR_FILE);
00193
00194 int mapsize = buf.st_size - offset + pagesize;
00195
00196 if (mapsize<=0) {
00197 if (verbose) printf("end of file encountered\n");
00198 ERROR(INFO_END_FILE) ;
00199 next_event_offset= -1;
00200 }
00201
00202 if (mapsize>MX_MAP_SIZE) mapsize = MX_MAP_SIZE;
00203 event_size = mapsize ;
00204
00205 DATAP = NULL;
00206
00207 if((MMAPP = (char *)mmap(0, mapsize, PROT_READ | PROT_WRITE,
00208 MAP_PRIVATE, fd, mmap_offset)) == (caddr_t) -1) {
00209 char myerr[100];
00210 sprintf(myerr,"mapping file request 0x%x bytes 0x%x",mapsize,mmap_offset);
00211 perror(myerr);
00212 ERROR(ERR_MEM);
00213 next_event_offset=-1;
00214 }
00215
00216
00217
00218 DATAP = MMAPP + (offset-mmap_offset);
00219
00220 if (offset>=buf.st_size) {
00221 ERROR(ERR_FILE) ;
00222 next_event_offset = -1;
00223 }
00224
00225 if (strncmp(DATAP,"RICP", 4) == 0) {
00226
00227 if (memcpy(&lr,DATAP,sizeof(lr))<0) perror("error in memcpy");
00228
00229 if (!lr.test_CRC()) ERROR(ERR_CRC);
00230
00231 if (lr.swap() < 0) ERROR(ERR_SWAP);
00232
00233 lr.header.CRC = 0;
00234
00235 char lcopy[10];
00236
00237 strncpy(lcopy,lr.header.BankType,8);
00238 lcopy[8] = 0;
00239
00240
00241 if(strncmp(lr.header.BankType, "RICP", 4) != 0) {
00242 perror("Have problem will the file\n");
00243 next_event_offset = -1;
00244 }
00245 else {
00246
00247
00248 runnum = 0;
00249
00250 next_event_offset = 4 * lr.Reserved[0].off+offset;
00251
00252 if (lr.header.Token==0){
00253 next_event_offset = -1;
00254 printf("hack: do not know how to deal with token==0\n");
00255 DATAP = NULL;
00256 }
00257
00258
00259 }
00260 }
00261 printf("======= Event number: %d with size %d============\n",lr.header.Token,lr.Reserved[0].off);
00262 }
00263
00264 RichEventReader::RichEventReader()
00265 {
00266 DATAP = NULL;
00267 MMAPP = NULL;
00268 errnum = 0;
00269 runnum = 0;
00270 memset(errstr0, '\0', sizeof(errstr0));
00271 event_size = 0;
00272 fd = -1;
00273 next_event_offset = -1;
00274 verbose = 0;
00275 logfd = NULL;
00276 }
00277
00278
00279 void RichEventReader::InitEventReader(int fdes, long offset)
00280 {
00281 long c_offset = offset;
00282 next_event_offset = -1;
00283
00284 if (verbose) cout << "Initializing RichEventReader with a file" << endl;
00285
00286
00287 strcpy(err_string[0],"ERROR: FILE");
00288 strcpy(err_string[1],"ERROR: CRC");
00289 strcpy(err_string[2],"ERROR: SWAP");
00290 strcpy(err_string[3],"ERROR: BANK");
00291 strcpy(err_string[4],"ERROR: MEM");
00292 strcpy(err_string[5],"ERROR: NOT DATA BANK");
00293 strcpy(err_string[6],"ERROR: BAD ARG");
00294 strcpy(err_string[7],"ERROR: ENDR ENCOUNTERED");
00295 strcpy(err_string[8],"ERROR: BAD HEADER");
00296 strcpy(err_string[9],"INFO: MISSING BANK");
00297 strcpy(err_string[10],"INFO: END OF FILE");
00298
00299
00300 fd = fdes;
00301 int DATAPEVENTLENGTH=0;
00302
00303 char bank[9];
00304 memset(bank,'\0',sizeof(bank));
00305
00306 int ret = lseek(fd, offset, SEEK_SET);
00307 if(ret < 0) {
00308 next_event_offset = -1;
00309 ERROR(ERR_FILE);
00310 }
00311
00312 ret = read(fd,bank,8);
00313 if(ret < 0) {
00314 next_event_offset = -1;
00315 ERROR(ERR_FILE);
00316 }
00317
00318 ret = lseek(fd, offset, SEEK_SET);
00319 if(ret < 0) ERROR(ERR_FILE);
00320
00321
00322 Bank_RICP lr;
00323
00324 if (strncmp(bank,"RICP", 4) == 0) {
00325
00326 ret = read(fd,&lr,sizeof(lr));
00327 if(ret < 0) ERROR(ERR_FILE);
00328 ret = lseek(fd, offset, SEEK_SET);
00329 if(ret < 0) ERROR(ERR_FILE);
00330 c_offset += sizeof(lr);
00331 if (!lr.test_CRC()) ERROR(ERR_CRC);
00332 if (lr.swap() < 0) ERROR(ERR_SWAP);
00333 lr.header.CRC = 0;
00334
00335
00336
00337 DATAPEVENTLENGTH = lr.Reserved[0].off;
00338
00339 runnum = 0;
00340 next_event_offset = offset+lr.Reserved[0].off * 4;
00341
00342
00343
00344 struct stat statbuf;
00345 if(fstat(fd, &statbuf) < 0) ERROR(ERR_FILE);
00346
00347 DATAP = (char *)malloc(DATAPEVENTLENGTH * 4);
00348 if(!DATAP) ERROR(ERR_MEM);
00349
00350 ret = read(fd, DATAP, DATAPEVENTLENGTH*4);
00351 if(ret < 0) ERROR(ERR_FILE);
00352
00353 if (!((Bank_RICP *)DATAP)->test_CRC()) ERROR(ERR_CRC);
00354 if (((Bank_RICP *)DATAP)->swap() < 0) ERROR(ERR_SWAP);
00355
00356 event_size = DATAPEVENTLENGTH;
00357
00358 }
00359 if (lr.header.Token==0){
00360 next_event_offset = -1;
00361 printf("hack: do not know how to deal with token==0\n");
00362 free(DATAP);
00363 DATAP = NULL;
00364 }
00365 printf("======= Event number: %d size %d============\n",lr.header.Token, DATAPEVENTLENGTH);
00366
00367 }
00368
00369 RichEventReader::~RichEventReader()
00370 {
00371
00372
00373 if(fd == -1)
00374 {
00375
00376 }
00377 else if(MMAPP != NULL)
00378 {
00379
00380
00381 munmap(MMAPP,event_size);
00382 }
00383 else
00384 {
00385
00386 free(DATAP);
00387 }
00388 if (logfd!=NULL) fclose(logfd);
00389 }
00390
00391 EventInfo RichEventReader::getEventInfo()
00392 {
00393
00394 EventInfo ei;
00395 Bank_RICP *dp = (Bank_RICP *)DATAP;
00396 ei.EventLength = dp->Reserved[0].off;
00397 ei.EventSeqNo = dp->header.Token;
00398 ei.RICHPresent = (0x80);
00399 return ei;
00400 }
00401
00402 void RichEventReader::printEventInfo(FILE * fd)
00403 {
00404 EventInfo ei = getEventInfo();
00405
00406 }
00407
00408 long RichEventReader::NextEventOffset()
00409 {
00410 return next_event_offset;
00411 }
00412
00413 int RichEventReader::MemUsed()
00414 {
00415 return event_size;
00416 }
00417
00418 void RichEventReader::setVerbose(int v)
00419 {
00420 verbose = v;
00421 }
00422
00423 char * RichEventReader::findBank(char *bankid)
00424 {
00425
00426 Bank_RICP *pBankDATAP = (Bank_RICP *)this->getDATAP();
00427 return (char *)pBankDATAP;
00428
00429 }
00430
00431 void RichEventReader::fprintError(int err, char *file, int line, char *userstring)
00432 {
00433 if (logfd==NULL) return;
00434 if (err<0 || err>MX_MESSAGE) return;
00435 fprintf(logfd,"%s %s::%d %s\n",err_string[err-1],file,line,userstring);
00436 fflush(logfd);
00437 }