51 printf(
"mc2codaInitExp: ERROR: Too many crates defined for the system\n");
57 printf(
"mc2codaInitExp: ERROR: Experiment already Initialized - Call mc2codaFree(expid) first\n");
61 printf(
"Exp name = %s\n",name);
65 strncpy(mc2coda_expid.
expname,name,64);
67 mc2coda_expid.
ncrates = nCrates;
69 for(ii=0;ii<nCrates;ii++) {
70 mc2coda_expid.
rocid[ii] = (ii+1);
79 printf(
"Initialized: Experiment %s (id = 0x%lx) consisting of %d crates/ROCs\n",
80 mc2coda_expid.
expname, (
unsigned long)&mc2coda_expid,mc2coda_expid.
ncrates);
83 gettimeofday(&tp, NULL);
84 start_time = ((double)tp.tv_sec)*1000000.0 + (double)tp.tv_usec;
87 return(&mc2coda_expid);
109 if ((crateid<0)||(crateid > expID->
ncrates)) {
110 printf(
"mc2codaSetCrate: ERROR: crate ID out of range %d > %d (ncrates) \n",
115 printf(
"mc2codaSetCrate: ERROR: Number of modules out of range %d\n",nmod);
121 printf(
"mc2codaSetCrate: WARN: overwriting Crate %d module map\n",crateid);
125 printf(
"mc2codSetCrate: INFO: Crate %d has %d modules for readout\n",crateid, nmod);
126 expID->
crate[(crateid-1)]->nModules = nmod;
127 expID->
crate[(crateid-1)]->moduleMask = 0;
130 expID->
crate[(crateid-1)]->module_map[ii] = modules[ii];
131 expID->
crate[(crateid-1)]->det_map[ii] = detid[ii];
132 if(modules[ii] != 0) expID->
crate[(crateid-1)]->moduleMask |= (1<<(ii+1));
158 int ii, jj, has_tt = 0;
163 printf(
"m2codaOpenEvent: ERROR: expID not defined\n");
167 printf(
"mc2codaOpenEvent: ERROR: event Number invalid (0)\n");
178 gettimeofday(&tp, NULL);
179 double now = ((double)tp.tv_sec)*1000000.0 + (double)tp.tv_usec;
181 uint64_t trel_ns = (uint64_t)(trel*1000.0);
187 evinfo->
trigtime = trigTime + trel_ns;
188 evinfo->
evtype = eventType&0x0000ffff;
189 evinfo->
expid = expID;
192 for (ii=0; ii<(expID->
ncrates); ii++) {
195 evinfo->
hcount[ii][jj] = 0;
202 evinfo->
hits[ii][jj] = NULL;
210 evbuf = (
unsigned int *) malloc (maxSize);
218 evinfo->
evbuf = (
unsigned int *)evbuf;
226 evbuf[1] = 0xff511001;
228 evbuf[3] = 0xff272000 | ((expID->
ncrates)&0xff);
229 evbuf[4] = 0x010a0006;
230 memcpy((
char *)&evbuf[5],(
char *)&eventNum,8);
231 memcpy((
char *)&evbuf[7],(
char *)&evinfo->
trigtime,8);
234 evbuf[11] = 0x01050001;
235 evbuf[12] = (eventType);
239 evbuf[1] = 0xff501001;
241 evbuf[3] = 0xff262000 | ((expID->
ncrates)&0xff);
242 evbuf[4] = 0x010a0004;
243 memcpy((
char *)&evbuf[5],(
char *)&eventNum,8);
246 evbuf[9] = 0x01050001;
247 evbuf[10] = (eventType);
276 int ii, cnt, lcnt, crate, slot;
279 static int bad_crate_slot_warning_issued = 0;
282 if((nHits <= 0) || (codaHits == NULL)) {
290 printf(
"mc2codaWrite: ERROR: Invalid or Corrupted event ID\n");
294 printf(
"mc2codaWrite: ERROR: Null event ID\n");
301 for (ii=0; ii<nHits; ii++) {
303 slot = codaHits[ii].
slot_id - 1;
305 if(crate<0 || slot<0){
306 if(!bad_crate_slot_warning_issued){
307 printf(
"mc2codaWrite: ERROR: Invalid crate(%d) or slot(%d)! This could be due to\n", crate, slot);
308 printf(
" data for a channel not defined in the crate map.\n");
309 printf(
" This is only reported once.\n");
310 bad_crate_slot_warning_issued = 1;
314 cnt =
event->hcount[crate][slot];
316 printf(
"mc2codaWrite: ERROR: No available space to store hit %d for crate/slot = %d/%d\n",
317 codaHits->
hit_id,crate,slot);
321 printf(
"%s:%d ERROR!! no CODA_HIT_INFO structure allocated for crate=%d, slot=%d\n", __FILE__, __LINE__, crate+1, slot+1);
327 tmpH->
hdata = (uint32_t *) malloc((codaHits[ii].nwords)<<2);
328 memcpy((
char *)(tmpH->
hdata), (
char *)(codaHits[ii].
hdata),(codaHits[ii].
nwords)<<2);
330 event->hcount[crate][slot] += 1;
350 unsigned int roc, det, mod;
351 unsigned int *StartofEvent;
356 printf(
"mc2codaCloseEvent: ERROR: Null Event Pointer!!\n");
361 StartofEvent = &(
event->evbuf[0]);
362 dabufp = StartofEvent + (
event->evbuf[0] + 1);
366 for(ii=0; ii<expID->
ncrates; ii++) {
368 roc = expID->
rocid[ii];
369 crate = expID->
crate[ii];
393 if(((1<<(jj+1))&(crate->
moduleMask)) == 0)
continue;
399 if(det == 0)
continue;
412 uint32_t *StartOfBank =
dabufp;
414 uint32_t nevents = 1;
415 *(++
dabufp) = (((status) << 28) | (det) << 16) | 0x0100 | (nevents);
420 if(((1<<(jj+1))&(crate->
moduleMask)) == 0)
continue;
426 *StartOfBank = (uint32_t) (
dabufp - StartOfBank - 1);
432 *(++
dabufp) = (((status) << 28) | (det) << 16) | 0x0100 | (nevents);
465 *StartOfBank = (uint32_t) (
dabufp - StartOfBank - 1);
469 *StartofEvent = (uint32_t) (
dabufp - StartofEvent - 1);
471 return((*StartofEvent+1));
488 int ii, jj, kk, ccnt, lcnt;
491 if(eventID != NULL) {
492 exp = eventID->
expid;
494 printf(
"mc2codaResetEvent: ERROR: Invalid or Corrupted event ID\n");
500 printf(
"mc2codaResetEvent: ERROR: NULL pointer to Event Buffer\n");
505 for (ii=0; ii<ccnt; ii++) {
507 if(eventID->
hits[ii][jj] != NULL) {
509 lcnt = eventID->
hcount[ii][jj];
512 for(kk=0;kk<lcnt;kk++) {
516 eventID->
hcount[ii][jj] = 0;
524 gettimeofday(&tp, NULL);
525 double now = ((double)tp.tv_sec)*1000000.0 + (double)tp.tv_usec;
527 uint64_t trel_ns = (uint64_t)(trel*1000.0);
532 eventID->
trigtime = trigTime + trel_ns;
533 eventID->
evtype = eventType&0x0000ffff;
538 printf(
"mc2codaResetEvent: ERROR: Event buffer size is invalid (%d)\n",eventID->
maxBytes);
545 eventID->
evbuf[0] = 12;
546 eventID->
evbuf[1] = 0xff511001;
547 eventID->
evbuf[2] = 10;
549 eventID->
evbuf[4] = 0x010a0006;
550 memcpy((
char *)&eventID->
evbuf[5],(
char *)&eventNum,8);
551 memcpy((
char *)&eventID->
evbuf[7],(
char *)&eventID->
trigtime,8);
552 eventID->
evbuf[ 9] = 0x01;
554 eventID->
evbuf[11] = 0x01050001;
555 eventID->
evbuf[12] = (eventType);
557 eventID->
evbuf[0] = 10;
558 eventID->
evbuf[1] = 0xff501001;
559 eventID->
evbuf[2] = 8;
561 eventID->
evbuf[4] = 0x010a0004;
562 memcpy((
char *)&eventID->
evbuf[5],(
char *)&eventNum,8);
563 eventID->
evbuf[7] = 0x01;
565 eventID->
evbuf[9] = 0x01050001;
566 eventID->
evbuf[10] = (eventType);
582 int ii, jj, kk, ccnt, lcnt;
585 exp = eventID->
expid;
589 if(eventID != NULL) {
591 if(eventID->
evbuf != NULL) {
592 free(eventID->
evbuf);
593 eventID->
evbuf = NULL;
595 printf(
"mc2codaFreeEvent: ERROR invalid pointer to Event Buffer\n");
599 for (ii=0; ii<ccnt; ii++) {
602 if(eventID->
hits[ii][jj] != NULL) {
604 lcnt = eventID->
hcount[ii][jj];
607 for(kk=0;kk<lcnt;kk++) {
615 free(eventID->
hits[ii][jj]);
616 eventID->
hits[ii][jj] = NULL;
629 printf(
"mc2codaFreeEvent: ERROR invalid eventID \n");
644 printf(
"mc2codaFree: ERROR invalid expID\n");
650 printf(
"mc2codaFree: ERROR: Cannot Free experiment. There are currently %d open events\n",
656 printf(
"mc2codaFree: WARN: Deleting Experiment %s\n",expID->
expname);
657 for(ii=0;ii<(expID->
ncrates);ii++)
658 free(expID->
crate[ii]);
674 int ii, jj, ccnt, hcnt;
677 if(eventID == NULL) {
678 printf(
"mc2codaStats: ERROR: Null Event ID\n");
683 exp = eventID->
expid;
685 hcnt = eventID->
nhits;
690 printf(
" Total Crates: %d Total Hits: %d\n",ccnt, hcnt);
693 printf(
"Crate Map: moduleID( hit_count) for each slot in each crate\n");
694 for(ii=0;ii<ccnt;ii++) {
696 printf(
" Crate %3d: ",(ii+1));
698 chits[ii] += eventID->
hcount[ii][jj];
struct coda_hit_info * hits[MAX_CRATES][MAX_SLOTS]
CODA_EXP_INFO * mc2codaInitExp(int nCrates, const char *name)
void mc2codaSetRunNumber(unsigned int run_number)
unsigned int mc2codaCloseEvent(CODA_EVENT_INFO *event)
struct coda_exp_info * expid
static int mc2coda_ncrates_defined
int fadc250_write_data(CODA_EVENT_INFO *event, int roc, int slot, int mode)
CODA_CRATE_MAP * crate[MAX_CRATES]
int hcount[MAX_CRATES][MAX_SLOTS]
int fadc125_write_data(CODA_EVENT_INFO *event, int roc, int slot, int mode)
#define ROC_BANK_OPEN(status, id, nevents)
int module_map[MAX_SLOTS]
int f1tdc48_write_data(CODA_EVENT_INFO *event, int roc, int slot, int mode)
static unsigned int * StartOfRocBank
int mc2codaSetCrate(CODA_EXP_INFO *expID, int crateid, int nmod, int *modules, int *detid)
int caen1290_write_data(CODA_EVENT_INFO *event, int roc, int slot, int mode)
#define MC2CINIT_COMPLETE
unsigned short rocid[MAX_CRATES]
int mc2codaFreeEvent(CODA_EVENT_INFO *eventID)
#define MAX_HITS_PER_SLOT
static unsigned int RUN_NUMBER
void WriteDAQconfigBank(CODA_CRATE_MAP *crate, int roc)
static unsigned int * dabufp
printf("string=%s", string)
void mc2codaStats(CODA_EVENT_INFO *eventID, int sflag)
CODA_EXP_INFO mc2coda_expid
int mc2codaWrite(CODA_EVENT_INFO *event, int nHits, struct coda_hit_info *codaHits)
void mc2codaFree(CODA_EXP_INFO *expID)
int f1tdc32_write_data(CODA_EVENT_INFO *event, int roc, int slot, int mode)
int mc2codaResetEvent(CODA_EVENT_INFO *eventID, uint64_t eventNum, uint64_t trigTime, unsigned short eventType)
CODA_EVENT_INFO * mc2codaOpenEvent(CODA_EXP_INFO *expID, uint64_t eventNum, uint64_t trigTime, unsigned short eventType, int maxSize)