/*CMZ : 23/02/2008 09.47.19 by Samo Korpar*/
/*CMZ : 0.02/00 04/07/2001 00.38.08 by Samo Korpar*/
/*CMZ : 0.01/00 30/11/99 16.50.35 by Samo Korpar*/
/*-- Author : Samo Korpar 18/10/99*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
/*KEEP,evdp,T=C++.*/
#include "evdp.h"
/*KEND.*/
#define swap_4(x) swap_long((int32_t *) x)
#define swap_2(x) swap_short((short *) x)
int isLE, isLOG;
char datafilename[300];
EVT_DATA evdata;
#define EVT_DATA_SIZE (sizeof(EVT_DATA))
LOG_header logh;
EVT_header evh;
EVT_trailer evt;
FILE *evfid;
FILE *oldfid;
int is_little_endian()
{
int test=1;
isLE = (int) *((char *) &test);
return isLE;
}
void swap_short(short *shtosw)
{
short stmp;
char *b1 = (char *) shtosw, *b2 = (char *) &stmp;
stmp=*shtosw;
b1[0]=b2[1];
b1[1]=b2[0];
return;
}
void swap_long(int32_t *lotosw)
{
int32_t ltmp;
char *b1 = (char *) lotosw, *b2 = (char *) <mp;
ltmp=*lotosw;
b1[0]=b2[3];
b1[1]=b2[2];
b1[2]=b2[1];
b1[3]=b2[0];
return;
}
int open_data_file(char *dfn)
{
if (dfn != NULL)
{
isLOG=0;
if (strstr(datafilename
, ".daq")!=NULL
) isLOG
=1;
}
if ((evfid
=fopen(datafilename
, "r"))==NULL
) return 1;
return 0;
}
int close_data_file()
{
if (fclose(evfid
)==EOF
) return 1;
return 0;
}
int reopen_data_file()
{
{
oldfid=evfid;
}
if (open_data_file((char *) NULL)) return 1;
return 0;
}
int reclose_data_file()
{
if (close_data_file()) return 1;
{
evfid=oldfid;
}
return 0;
}
int next_buf(int len, void *to)
{
if (fread( to
, 1, len
, evfid
) != len
) return -1;
return len;
}
int skip_buf(int len)
{
if (fseek(evfid
, len
, SEEK_CUR
)) return -1;
return len;
}
int goto_event(int evgoto)
{
int pos, fpos, bc;
pos=0;
while ( ++pos != evgoto )
{
if (fread((void *) &bc
, 1, 4, evfid
) == 4)
{
if (isLE) swap_4(&bc);
if (fseek(evfid
, bc
-4, SEEK_CUR
)) return pos
-1;
}
}
return pos;
}
int get_nextev(FED_record *fdrp, unsigned short cid, int prmes)
{
int nr,i;
SLB_header slbh;
SLB_trailer slbt;
nr=0;
if (isLOG)
{
if (next_buf(20, (void *) &logh) != 20) return 0;
if (isLE)
{
swap_4(&logh.bc);
swap_4(&logh.run);
swap_4(&logh.evt);
swap_4(&logh.dtm);
}
}
if (next_buf(20, (void *) &evh) != 20) return 0;
if (isLE)
{
swap_4(&evh.bc);
swap_2(&evh.recid);
swap_2(&evh.sp);
swap_4(&evh.slt);
swap_4(&evh.cm);
swap_4(&evh.em);
}
if (prmes)
printf("EVT: bc=%u, recid=0x%X, sp=0x%X, slt=%u, cm=0x%X, em=0x%X\n",
evh.bc, evh.recid, evh.sp, evh.slt, evh.cm, evh.em);
evh.bc-=20;
while (evh.bc>4)
{
if (next_buf(16, (void *) &slbh) != 16) return 0;
if (isLE)
{
swap_4(&slbh.bc);
swap_2(&slbh.recid);
swap_2(&slbh.fltev);
swap_2(&slbh.compid);
swap_2(&slbh.slbn);
swap_4(&slbh.dest);
}
if (prmes)
printf(" >SLB bc=%u, recid=0x%X, fltev=%d,"
" compid=0x%X, slbn=%d, dest=0x%X\n", slbh.bc, slbh.recid,
slbh.fltev, slbh.compid, slbh.slbn, slbh.dest);
evh.bc-=slbh.bc;
slbh.bc-=16;
if (slbh.compid==0)
{
if (next_buf(EVT_DATA_SIZE, (void *) &evdata) != EVT_DATA_SIZE) return 0;
slbh.bc-=EVT_DATA_SIZE;
if (skip_buf(slbh.bc-4) != slbh.bc-4 ) return 0;
if (isLE)
{
swap_4(&evdata.run_number);
swap_4(&evdata.event_time);
swap_4(&evdata.event_status);
swap_4(&evdata.flt_status);
swap_4(&evdata.slp_node);
swap_4(&evdata.slt_status);
swap_4(&evdata.exp_number);
swap_4(&evdata.farm_node);
swap_4(&evdata.BX_long);
swap_2(&evdata.BX_upper);
swap_2(&evdata.FCS_tcode);
swap_2(&evdata.physBX);
swap_2(&evdata.FLT_BX);
swap_2(&evdata.fltevt);
}
}
else if (slbh.compid==cid)
{
while (slbh.bc>4)
{
if (next_buf(8, (void *) fdrp) != 8) return 0;
if (isLE)
{
swap_2(&fdrp->bc);
swap_2(&fdrp->fedid);
swap_2(&fdrp->recid);
swap_2(&fdrp->bxn);
}
if (prmes)
printf(" >FED bc=%d, fedid=0x%X, recid=0x%X, bxn=%d\n",
fdrp->bc, fdrp->fedid, fdrp->recid, fdrp->bxn);
slbh.bc-=fdrp->bc;
fdrp->bc-=8;
if (0x8000&fdrp->recid)
{
if (skip_buf(fdrp->bc) != fdrp->bc) return 0;
}
else
{
if (next_buf(fdrp->bc, (void *) fdrp->data) != fdrp->bc) return 0;
nr++;
fdrp++;
}
/* fdrp->bc-=8;
switch (fdrp->recid)
{
case 0x1002:
if (next_buf(128, (void *) fdrp->data) != 128) return 0;
nr++;
fdrp++;
break;
case 0x100a:
if (next_buf(fdrp->bc, (void *) fdr.data) != fdrp->bc) return 0;
memset((void *) fdrp->data, 0xff, 128);
for (i=0;i<fdrp->bc;i+=2)
fdrp->data[fdr.data[i]]=fdr.data[i+1];
nr++;
fdrp++;
break;
default:
if (skip_buf(fdrp->bc) != fdrp->bc) return 0;
}
*/
}
}
else if (skip_buf(slbh.bc-4) != slbh.bc-4 ) return 0;
/*
else if (cid==0x400)
{
if (prmes)
{
while (slbh.bc>4)
{
if (next_buf(8, (void *) &fdr) != 8) return 0;
if (isLE)
{
swap_2(&fdr.bc);
swap_2(&fdr.fedid);
swap_2(&fdr.recid);
swap_2(&fdr.bxn);
}
otfid[nf]=fdr.fedid;
printf(" >FED%d bc=%d, fedid=0x%X, recid=0x%X, bxn=%d",
++nf, fdr.bc, fdr.fedid, fdr.recid, fdr.bxn);
slbh.bc-=fdr.bc;
fdr.bc-=8;
if (next_buf(fdr.bc, (void *) fdr.data) != fdr.bc) return 0;
for (i=0;i<fdr.bc;i+=4)
{
if (! (i%16)) printf("\n");
printf("0x%02X 0x%02X 0x%02X 0x%02X ",
fdr.data[i], fdr.data[i+1], fdr.data[i+2], fdr.data[i+3]);
}
printf("\n");
}
}
else
{
if (skip_buf(slbh.bc-4) != slbh.bc-4 ) return 0;
}
}
else if (cid==0x600)
{
if (prmes)
{
while (slbh.bc>4)
{
if (next_buf(8, (void *) &fdr) != 8) return 0;
if (isLE)
{
swap_2(&fdr.bc);
swap_2(&fdr.fedid);
swap_2(&fdr.recid);
swap_2(&fdr.bxn);
}
printf(" >FED bc=%d, fedid=0x%X, recid=0x%X, bxn=%d\n",
fdr.bc, fdr.fedid, fdr.recid, fdr.bxn);
slbh.bc-=fdr.bc;
fdr.bc-=8;
if (skip_buf(fdr.bc) != fdr.bc ) return 0;
}
}
else
{
if (skip_buf(slbh.bc-4) != slbh.bc-4 ) return 0;
}
*/
if (next_buf( 4, (void *) &slbt) != 4) return 0;
if (isLE)
{
swap_2(&slbt.recid);
swap_2(&slbt.fltev);
}
if (prmes)
printf(" >SLB recid=0x%X, fltev=%d\n", slbt.
recid, slbt.
fltev);
}
if (next_buf( 4, (void *) &evt) != 4) return 0;
if (isLE) swap_4(&evt.slt);
if (prmes)
{
printf("EVT: slt=%u, tcode=0x%x, flt_status=0x%x\n",
evh.slt, evdata.FCS_tcode, evdata.flt_status);
}
if (prmes && isLOG)
printf("LOG: bc=%u, exp=%u, run=%u, evt=%u, dtm=%s",
logh.
bc, logh.
exp, logh.
run, logh.
evt, ctime((time_t
*)&logh.
dtm));
return nr;
}
int dump_cmp(FILE *fout, unsigned short cid)
{
int pos;
int evbc, slbbc, compid;
char evbf[10000];
LOG_header *loghb;
EVT_header *evhb;
SLB_header *slbh;
if (isLOG)
{
if (skip_buf(20) != 20) return 0;
}
pos=0;
evhb=(EVT_header *) &evbf[pos];
if (next_buf(20, (void *) evhb) != 20) return 0;
pos+=20;
evbc=evhb->bc;
if (isLE) swap_4(&evbc);
evbc-=20;
while (evbc>4)
{
slbh=(SLB_header *) &evbf[pos];
if (next_buf(16, (void *) slbh) != 16) return 0;
slbbc=slbh->bc;
compid=slbh->compid;
if (isLE)
{
swap_4(&slbbc);
swap_2(&compid);
}
evbc-=slbbc;
slbbc-=16;
if ((compid==cid)||(compid==0x0))
{
pos+=16;
if (next_buf(slbbc, (void *) &evbf[pos]) != slbbc) return 0;
pos+=slbbc;
}
else
{
if (skip_buf(slbbc) != slbbc) return 0;
}
}
if (next_buf( 4, (void *) &evbf[pos]) != 4) return 0;
pos+=4;
evhb->bc=pos;
if (isLE) swap_4(&evhb->bc);
if (fwrite((void *) evbf
, 1, pos
, fout
) != pos
) return 0;
return 1;
}