Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mc2coda.c
Go to the documentation of this file.
1 /* mc2coda - function to accept a list of data and
2  convert it into a CODA3 Raw Event format
3  */
4 
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <strings.h>
10 #include <stdint.h>
11 #include <sys/time.h>
12 
13 #include "mc2coda.h"
14 
15 /* Global mc2coda Library Variables */
19 
20 
21 /* Local variables */
22 static int mc2coda_ncrates_defined = 0;
23 static unsigned int *dabufp, *StartOfRocBank;
24 static unsigned int RUN_NUMBER = 1;
25 
26 static double start_time = 0.0; // (us) initialized in mc2codaInitExp to represent program start time
27 
28 /* Include module specific definitions */
29 #include "mc2coda_modules.h"
30 
31 
32 /* Allow user to set the run number (added 8/21/2013 DL)*/
33 void
34 mc2codaSetRunNumber(unsigned int run_number)
35 {
36  RUN_NUMBER = run_number;
37 }
38 
39 /* Initialize the Crate/Module maps for crates in the system
40  Must be called prior to calling the mc2coda event packer.
41 
42  - Returns an expID (experiment setup structure pointer) */
43 
45 mc2codaInitExp(int nCrates, const char *name)
46 {
47 
48  int ii;
49 
50  if(nCrates>MAX_CRATES) {
51  printf("mc2codaInitExp: ERROR: Too many crates defined for the system\n");
52  return (NULL);
53  }
54 
55  /* Initialize a new experiment config structure */
56  if(mc2coda_inited) {
57  printf("mc2codaInitExp: ERROR: Experiment already Initialized - Call mc2codaFree(expid) first\n");
58  return(NULL);
59  }
60 
61  printf("Exp name = %s\n",name);
62 
63  bzero((char *) &mc2coda_expid,sizeof(CODA_EXP_INFO));
64 
65  strncpy(mc2coda_expid.expname,name,64);
66 
67  mc2coda_expid.ncrates = nCrates;
68 
69  for(ii=0;ii<nCrates;ii++) {
70  mc2coda_expid.rocid[ii] = (ii+1);
71  mc2coda_expid.crate[ii] = (CODA_CRATE_MAP *) malloc(sizeof(CODA_CRATE_MAP));
72  bzero(mc2coda_expid.crate[ii],sizeof(CODA_CRATE_MAP));
73  mc2coda_expid.crate[ii]->crate_id = ii;
74  }
75 
76  mc2coda_expid.inited = MC2CINIT_DEFINED; /* Only partially initialized */
77  mc2coda_expid.openevents = 0; /* No open events using this experiment config */
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);
81 
82  struct timeval tp;
83  gettimeofday(&tp, NULL);
84  start_time = ((double)tp.tv_sec)*1000000.0 + (double)tp.tv_usec;
85 printf("%s:%d start_time=%f\n", __FILE__,__LINE__, start_time);
86 
87  return(&mc2coda_expid);
88 
89 }
90 
91 
92 /* Set Crate specific info for a given expID -
93  All crates must be defined - with module types before an experiment config
94  can generate RAW events. This function must be called at least once for each
95  crate defined by mc2codaInitExp
96 
97  pass number of modules, and module and detector id arrays
98 
99  returns Number of crates initialized or Error (-1) */
100 
101 int
102 mc2codaSetCrate(CODA_EXP_INFO *expID, int crateid, int nmod, int *modules, int *detid)
103 {
104 
105  int ii;
106 
107  /* check that parameters are valid */
108 
109  if ((crateid<0)||(crateid > expID->ncrates)) {
110  printf("mc2codaSetCrate: ERROR: crate ID out of range %d > %d (ncrates) \n",
111  crateid, expID->ncrates);
112  return(-1);
113  }
114  if(nmod >= MAX_SLOTS) {
115  printf("mc2codaSetCrate: ERROR: Number of modules out of range %d\n",nmod);
116  return(-1);
117  }
118 
119  if(mc2coda_ncrates_defined >= expID->ncrates) {
120  mc2coda_ncrates_defined = expID->ncrates - 1;
121  printf("mc2codaSetCrate: WARN: overwriting Crate %d module map\n",crateid);
122  }
123 
124  /* Fill the Crate structure */
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;
128  /*printf("slots: ");*/
129  for (ii=0;ii<MAX_SLOTS;ii++) {
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));
133  /*if(modules[ii] != 0) printf("%d ", ii+1);*/
134  }
135  /*printf("\n");*/
136 
138 
139  /* Initialization is Complete ?? */
140  if(mc2coda_ncrates_defined == expID->ncrates) {
141  printf("mc2codaSetCrate: All %d crates defined: Initialization complete.\n",mc2coda_ncrates_defined);
143  }
144 
145  return(mc2coda_ncrates_defined);
146 
147 }
148 
149 
150 
151 /* Open a RAW event for population
152 
153  - Returns an eventID */
155 mc2codaOpenEvent(CODA_EXP_INFO *expID, uint64_t eventNum, uint64_t trigTime, unsigned short eventType, int maxSize)
156 {
157 
158  int ii, jj, has_tt = 0;
159  unsigned int *evbuf;
160  CODA_EVENT_INFO *evinfo;
161 
162  if(expID == 0) {
163  printf("m2codaOpenEvent: ERROR: expID not defined\n");
164  return(NULL);
165  }else{ /*check if the event number is Valid */
166  if(eventNum == 0) {
167  printf("mc2codaOpenEvent: ERROR: event Number invalid (0)\n");
168  return(NULL);
169  }
170  if(trigTime == 0)
171  has_tt=0;
172  else
173  has_tt=1;
174  }
175 
176  /* Get current time relative to program start to record in trigger time */
177  struct timeval tp;
178  gettimeofday(&tp, NULL);
179  double now = ((double)tp.tv_sec)*1000000.0 + (double)tp.tv_usec;
180  double trel = now - start_time;
181  uint64_t trel_ns = (uint64_t)(trel*1000.0);
182 
183  /* Allocate an Event Info structure */
184  evinfo = (CODA_EVENT_INFO *) malloc(sizeof(CODA_EVENT_INFO));
185  evinfo->nhits = 0;
186  evinfo->eventid = eventNum;
187  evinfo->trigtime = trigTime + trel_ns;
188  evinfo->evtype = eventType&0x0000ffff;
189  evinfo->expid = expID;
190 
191  /* Allocate Hit Arrays for each valid crate/slot */
192  for (ii=0; ii<(expID->ncrates); ii++) {
193  for(jj=1; jj<(MAX_SLOTS); jj++) { /* Skip CPU slot */
194 
195  evinfo->hcount[ii][jj] = 0;
196  /* Check if there is a valid module for this crate/slot pair and
197  allocate the MAX number of HIT structures for that slot */
198  if((expID->crate[ii]->moduleMask)&(1<<(jj+1))) {
199  /* printf("DEBUG: Allocating hit array for crate,slot = %d,%d\n",ii,jj); */
200  evinfo->hits[ii][jj] = (CODA_HIT_INFO *) malloc(MAX_HITS_PER_SLOT * sizeof(CODA_HIT_INFO));
201  }else{
202  evinfo->hits[ii][jj] = NULL;
203  }
204 
205  }
206  }
207 
208  /* Allocate an Event buffer */
209  if(maxSize) {
210  evbuf = (unsigned int *) malloc (maxSize);
211  mc2coda_maxevsize = maxSize;
212  }else{
213  evbuf = (unsigned int *) malloc (MAX_EVENT_SIZE);
215  }
216 
217  bzero((char *)evbuf, mc2coda_maxevsize);
218  evinfo->evbuf = (unsigned int *)evbuf;
219  evinfo->maxBytes = mc2coda_maxevsize;
220 
221 
222  /* Populate the Event header and trigger Bank */
223  if(has_tt) {
224  /* Event header if we have a trigger time */
225  evbuf[0] = 12;
226  evbuf[1] = 0xff511001;
227  evbuf[2] = 10;
228  evbuf[3] = 0xff272000 | ((expID->ncrates)&0xff); /* changed from ff21 to include run number 8/21/2013 DL */
229  evbuf[4] = 0x010a0006; /* segment of 64 bit uints */
230  memcpy((char *)&evbuf[5],(char *)&eventNum,8);
231  memcpy((char *)&evbuf[7],(char *)&evinfo->trigtime,8);
232  evbuf[ 9] = 0x01; /* run type */
233  evbuf[10] = RUN_NUMBER; /* This goes into high 32 bits which seems backwards ?? */
234  evbuf[11] = 0x01050001; /* segment of shorts header with 1 value */
235  evbuf[12] = (eventType);
236  }else{
237  /* Event header if we don't have a trigger time */
238  evbuf[0] = 10;
239  evbuf[1] = 0xff501001;
240  evbuf[2] = 8;
241  evbuf[3] = 0xff262000 | ((expID->ncrates)&0xff); /* changed from ff20 to include run number 8/21/2013 DL */
242  evbuf[4] = 0x010a0004; /* segment of 64 bit uints */
243  memcpy((char *)&evbuf[5],(char *)&eventNum,8);
244  evbuf[7] = 0x01; /* run type */
245  evbuf[8] = RUN_NUMBER; /* This goes into high 32 bits which seems backwards ?? */
246  evbuf[9] = 0x01050001; /* segment of shorts header with 1 value */
247  evbuf[10] = (eventType);
248  }
249 
250 
251  /* Bump the openevents counter */
252  expID->openevents++;
253 
254 
255  return(evinfo);
256 }
257 
258 /* Write Monte Carlo hit(s) info into the event.
259 
260  This routine allocates local hit structures and copies hit infomation
261  into them and then updates a list. Once the function returns the original
262  hit structures can be freed or cleared.
263 
264  This routine must be reentrant so that different threads can write hits
265  asynchonously. No attempt is made to reorder hits. Only copy them and
266  updates list totals.
267 
268  Returns: # of hits written to the Event
269 
270  */
271 
272 int
273 mc2codaWrite(CODA_EVENT_INFO *event, int nHits, struct coda_hit_info *codaHits)
274 {
275 
276  int ii, cnt, lcnt, crate, slot;
277  CODA_EXP_INFO *exp;
278  CODA_HIT_INFO *tmpH;
279  static int bad_crate_slot_warning_issued = 0;
280 
281  /* check for valid hits */
282  if((nHits <= 0) || (codaHits == NULL)) {
283  return(0);
284  }
285 
286  /* Check for a valid event pointer */
287  if(event) {
288  exp = event->expid;
289  if(exp->openevents <= 0) {
290  printf("mc2codaWrite: ERROR: Invalid or Corrupted event ID\n");
291  return(-1);
292  }
293  }else{
294  printf("mc2codaWrite: ERROR: Null event ID\n");
295  return(-1);
296  }
297 
298 
299  /* Now loop through all new hits and sort/save them*/
300  lcnt = 0;
301  for (ii=0; ii<nHits; ii++) {
302  crate = codaHits[ii].crate_id - 1;
303  slot = codaHits[ii].slot_id - 1;
304  /* printf("DEBUG: Writing hit %d for crate,slot = %d,%d\n",cnt,crate,slot); */
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;
311  }
312  }else{
313 
314  cnt = event->hcount[crate][slot];
315  if(cnt > MAX_HITS_PER_SLOT) {
316  printf("mc2codaWrite: ERROR: No available space to store hit %d for crate/slot = %d/%d\n",
317  codaHits->hit_id,crate,slot);
318  } else {
319  tmpH = (CODA_HIT_INFO *)&event->hits[crate][slot][cnt];
320  if(tmpH == NULL){
321  printf("%s:%d ERROR!! no CODA_HIT_INFO structure allocated for crate=%d, slot=%d\n", __FILE__, __LINE__, crate+1, slot+1);
322  continue;
323  }
324 
325  memcpy((char *)&(tmpH->hit_id),(char *)&(codaHits[ii].hit_id),sizeof(CODA_HIT_INFO)) ;
326  /* printf("DEBUG: malloc data array\n"); */
327  tmpH->hdata = (uint32_t *) malloc((codaHits[ii].nwords)<<2);
328  memcpy((char *)(tmpH->hdata), (char *)(codaHits[ii].hdata),(codaHits[ii].nwords)<<2);
329 
330  event->hcount[crate][slot] += 1;
331  }
332  } // crate<0 || slot<0
333 
334  event->nhits++;
335  lcnt++;
336 
337  }
338 
339  return(lcnt);
340 }
341 
342 
343 
344 /* Close out the event packer and return pointer to Event Buffer for writing
345  function also returns number of total words in the event (eventID[0] */
346 unsigned int
348 {
349  int ii, jj;
350  unsigned int roc, det, mod;
351  unsigned int *StartofEvent;
352  CODA_EXP_INFO *expID = event->expid;
353  CODA_CRATE_MAP *crate;
354 
355  if(event == NULL) {
356  printf("mc2codaCloseEvent: ERROR: Null Event Pointer!!\n");
357  return(0);
358  }
359 
360  /* Setup pointers */
361  StartofEvent = &(event->evbuf[0]);
362  dabufp = StartofEvent + (event->evbuf[0] + 1); /* Set data pointer to end of the event */
363 
364 
365  /* Loop through all ROCs */
366  for(ii=0; ii<expID->ncrates; ii++) {
367 
368  roc = expID->rocid[ii];
369  crate = expID->crate[ii];
370 
371  // This needs to be fixed:
372  // Each ROC may put out one or more DATA BLOCK banks(DBB). If the ROC
373  // contains only JLab modules, it may concatentate the data from
374  // all of them into a single DBB. It may also split things up such
375  // that multiple DMA transfers are used, each going to a different
376  // DBB, but from the same ROC.
377  //
378  // Another possibility is that there are modules that are not JLab
379  // modules so the data itself cannot be interpreted to determine
380  // the exact module type. In this case, the detid bits (lower 8bits
381  // of "tag") must be used to specify the module type. This means that
382  // these modules must go in a separate DBB.
383  //
384  // To handle this, the DBB should be opened using the module type
385  // (0 for non-digitizing, 1 for JLab, 20 for CAEN1290, ....) of
386  // the first module in the crate. If a module with a different
387  // det_id is encountered, the DBB should be closed and another
388  // opened using that new det_id. This means we need to find the
389  // first non-empty slot, not counting the first slot which is
390  // the CPU.
391  det = 0;
392  for(jj=0; jj<MAX_SLOTS; jj++){
393  if(((1<<(jj+1))&(crate->moduleMask)) == 0) continue;
394  det = crate->det_map[jj];
395  if(det != 0) break;
396  }
397 
398  // If no digitization modules exist in this crate then skip it
399  if(det == 0) continue;
400 
401  /* Open a ROC data Bank */
402  ROC_BANK_OPEN(0,roc,1);
403 
404  // Write the data configuration bank
405  WriteDAQconfigBank(crate, roc);
406 
407  /* Open a Data Bank */
408  // Note that we can't use the DATA_BANK_OPEN and DATA_BANK_CLOSE macros
409  // since they open and close a curly bracket{} and we want to close
410  // an old bank and open a new one inside the for loop below.
411  //DATA_BANK_OPEN(0,det,1);
412  uint32_t *StartOfBank = dabufp;
413  uint32_t status = 0;
414  uint32_t nevents = 1;
415  *(++dabufp) = (((status) << 28) | (det) << 16) | 0x0100 | (nevents);
416  (dabufp)++;
417 
418  /* Loop through all modules */
419  for(jj=0; jj<MAX_SLOTS; jj++) {
420  if(((1<<(jj+1))&(crate->moduleMask)) == 0) continue;
421 
422  // If module type changes, then we need to open a new
423  // data bank
424  if(det != crate->det_map[jj]){
425  // Close existing bank
426  *StartOfBank = (uint32_t) (dabufp - StartOfBank - 1);
427 
428  // Open new bank with the new detid (but not for detid==0)
429  det = crate->det_map[jj];
430  StartOfBank = dabufp;
431  if(det != 0){
432  *(++dabufp) = (((status) << 28) | (det) << 16) | 0x0100 | (nevents);
433  (dabufp)++;
434  }
435  }
436 
437  mod = crate->module_map[jj];
438  /* printf(" slot %d module %d \n",(jj+1),mod); */
439 
440 
441  switch (mod) {
442  case FADC250:
443  fadc250_write_data (event, roc,(jj+1) , FADC250_MODE_IP);
444  break;
445  case FADC125:
446  fadc125_write_data (event, roc,(jj+1) , FADC125_MODE_IP);
447  break;
448  case F1TDC32:
449  f1tdc32_write_data (event, roc,(jj+1) ,0);
450  break;
451  case F1TDC48:
452  f1tdc48_write_data (event, roc,(jj+1), 0);
453  break;
454  case CAEN1290:
455  caen1290_write_data (event, roc,(jj+1), 0);
456  break;
457  default:
458  break;
459  }
460 
461  }
462 
463  // Close data bank (see note above regarding DATA_BANK_CLOSE)
464  //DATA_BANK_CLOSE;
465  *StartOfBank = (uint32_t) (dabufp - StartOfBank - 1);
466  }
467 
469  *StartofEvent = (uint32_t) (dabufp - StartofEvent - 1);
470 
471  return((*StartofEvent+1));
472 }
473 
474 
475 /* Reset the Existing event for use again. This is equivilent to OpenEvent
476  so that new event Number, trigger time and event type must be specified
477  but it avoids mallocing the necessary memory and structures all over again.
478 
479  returns 0 - OK or -1 Error
480 
481  */
482 int
483 mc2codaResetEvent(CODA_EVENT_INFO *eventID, uint64_t eventNum, uint64_t trigTime, unsigned short eventType)
484 {
485 
486  CODA_EXP_INFO *exp;
487  CODA_HIT_INFO *tmpH;
488  int ii, jj, kk, ccnt, lcnt;
489 
490 
491  if(eventID != NULL) {
492  exp = eventID->expid;
493  if(exp->openevents <= 0) {
494  printf("mc2codaResetEvent: ERROR: Invalid or Corrupted event ID\n");
495  return(-1);
496  }
497  ccnt = exp->ncrates;
498  // hcnt = eventID->nhits;
499  } else {
500  printf("mc2codaResetEvent: ERROR: NULL pointer to Event Buffer\n");
501  return(-1);
502  }
503 
504  /* First free the data arrays that have been allocated for individual hits */
505  for (ii=0; ii<ccnt; ii++) {
506  for(jj=1; jj<(MAX_SLOTS); jj++) { /* Skip CPU slot */
507  if(eventID->hits[ii][jj] != NULL) {
508  /* crate,slot array exists, now check if it has hits */
509  lcnt = eventID->hcount[ii][jj];
510  if(lcnt) {
511  /* free all the hit data */
512  for(kk=0;kk<lcnt;kk++) {
513  tmpH = (CODA_HIT_INFO *) &eventID->hits[ii][jj][kk];
514  free(tmpH->hdata);
515  }
516  eventID->hcount[ii][jj] = 0; /* Set Hit count to 0 */
517  }
518  }
519  }
520  }
521 
522  /* Get current time relative to program start to record in trigger time */
523  struct timeval tp;
524  gettimeofday(&tp, NULL);
525  double now = ((double)tp.tv_sec)*1000000.0 + (double)tp.tv_usec;
526  double trel = now - start_time;
527  uint64_t trel_ns = (uint64_t)(trel*1000.0);
528 
529  /* Now clear the existing event structure and buffer and reset with the new info */
530  eventID->nhits = 0;
531  eventID->eventid = eventNum;
532  eventID->trigtime = trigTime + trel_ns;
533  eventID->evtype = eventType&0x0000ffff;
534 
535  if(eventID->maxBytes > 0) {
536  bzero((char *)eventID->evbuf, eventID->maxBytes);
537  }else{
538  printf("mc2codaResetEvent: ERROR: Event buffer size is invalid (%d)\n",eventID->maxBytes);
539  return(-1);
540  }
541 
542 
543  /* Populate the Event header and trigger Bank */
544  if(trigTime) {
545  eventID->evbuf[0] = 12;
546  eventID->evbuf[1] = 0xff511001;
547  eventID->evbuf[2] = 10;
548  eventID->evbuf[3] = 0xff272000 | ((exp->ncrates)&0xff); /* changed from ff21 to include run number 9/04/2013 DL */
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; /* run type */
553  eventID->evbuf[10] = RUN_NUMBER; /* This goes into high 32 bits which seems backwards ?? */
554  eventID->evbuf[11] = 0x01050001; /* segment of shorts header with 1 value */
555  eventID->evbuf[12] = (eventType);
556  }else{
557  eventID->evbuf[0] = 10;
558  eventID->evbuf[1] = 0xff501001;
559  eventID->evbuf[2] = 8;
560  eventID->evbuf[3] = 0xff262000 | ((exp->ncrates)&0xff); /* changed from ff20 to include run number 9/04/2013 DL */
561  eventID->evbuf[4] = 0x010a0004;
562  memcpy((char *)&eventID->evbuf[5],(char *)&eventNum,8);
563  eventID->evbuf[7] = 0x01; /* run type */
564  eventID->evbuf[8] = RUN_NUMBER; /* This goes into high 32 bits which seems backwards ?? */
565  eventID->evbuf[9] = 0x01050001; /* segment of shorts header with 1 value */
566  eventID->evbuf[10] = (eventType);
567  }
568 
569 
570  return(0);
571 }
572 
573 
574 
575 /* Free the event buffer for the specified event */
576 int
578 {
579 
580  CODA_EXP_INFO *exp;
581  CODA_HIT_INFO *tmpH;
582  int ii, jj, kk, ccnt, lcnt;
583 
584  /* Get the crate and Hit counts */
585  exp = eventID->expid;
586  ccnt = exp->ncrates;
587  //hcnt = eventID->nhits;
588 
589  if(eventID != NULL) {
590  /* First free the Event Buffer */
591  if(eventID->evbuf != NULL) {
592  free(eventID->evbuf);
593  eventID->evbuf = NULL;
594  } else {
595  printf("mc2codaFreeEvent: ERROR invalid pointer to Event Buffer\n");
596  }
597 
598  /* Free all the allocated Arrays of Hit structures and their associated data array*/
599  for (ii=0; ii<ccnt; ii++) {
600  for(jj=1; jj<(MAX_SLOTS); jj++) { /* Skip CPU slot */
601 
602  if(eventID->hits[ii][jj] != NULL) {
603  /* crate,slot array exists, now check if it has hits */
604  lcnt = eventID->hcount[ii][jj];
605  if(lcnt) {
606  /* free all the hit data */
607  for(kk=0;kk<lcnt;kk++) {
608  /* printf("DEBUG: freeing data\n"); */
609  tmpH = (CODA_HIT_INFO *) &eventID->hits[ii][jj][kk];
610  free(tmpH->hdata);
611  }
612  }
613  /* Now free the hit structure array */
614  /* printf("DEBUG: freeing Hit array for crate,slot = %d,%d\n",ii,jj); */
615  free(eventID->hits[ii][jj]);
616  eventID->hits[ii][jj] = NULL;
617  }
618  }
619  }
620 
621  /* Free the Event Structure */
622  free(eventID);
623  eventID = NULL;
624 
625  /* Decrement the openevents counter */
626  exp->openevents--;
627 
628  }else{
629  printf("mc2codaFreeEvent: ERROR invalid eventID \n");
630  return(ERROR);
631  }
632 
633  return(0);
634 }
635 
636 
637 /* Free the Experiment Config (including any open events for it) */
638 void
640 {
641  int ii;
642 
643  if(expID == NULL) {
644  printf("mc2codaFree: ERROR invalid expID\n");
645  return;
646  }
647 
648  /* Check if there are open events */
649  if(expID->openevents) {
650  printf("mc2codaFree: ERROR: Cannot Free experiment. There are currently %d open events\n",
651  expID->openevents);
652  return;
653  }
654 
655  if(expID->inited > MC2CINIT_NULL) {
656  printf("mc2codaFree: WARN: Deleting Experiment %s\n",expID->expname);
657  for(ii=0;ii<(expID->ncrates);ii++)
658  free(expID->crate[ii]);
659 
660  bzero(expID,sizeof(CODA_EXP_INFO));
661  }
662 }
663 
664 
665 
666 
667 /* Print out various statistics about the event */
668 void
669 mc2codaStats(CODA_EVENT_INFO *eventID, int sflag)
670 {
671 
672  CODA_EXP_INFO *exp;
673  //CODA_HIT_INFO *tmpH;
674  int ii, jj, ccnt, hcnt;
675  int chits [MAX_CRATES];
676 
677  if(eventID == NULL) {
678  printf("mc2codaStats: ERROR: Null Event ID\n");
679  return;
680  }
681 
682  /* Get the crate and Hit counts */
683  exp = eventID->expid;
684  ccnt = exp->ncrates;
685  hcnt = eventID->nhits;
686 
687 
688  /* Print out Experiment Info */
689  printf("Experiment: %s # of Open Events: %d\n",exp->expname, exp->openevents);
690  printf(" Total Crates: %d Total Hits: %d\n",ccnt, hcnt);
691 
692  /* Count up Hits per Crate and print out Crate map with hit info*/
693  printf("Crate Map: moduleID( hit_count) for each slot in each crate\n");
694  for(ii=0;ii<ccnt;ii++) {
695  chits[ii]=0;
696  printf(" Crate %3d: ",(ii+1));
697  for(jj=0;jj<MAX_SLOTS;jj++) {
698  chits[ii] += eventID->hcount[ii][jj];
699  printf("%1d(%4d) ",exp->crate[ii]->module_map[jj],eventID->hcount[ii][jj]);
700  }
701  printf("\n");
702  }
703 
704 
705  return;
706 }
707 
uint32_t evtype
Definition: mc2coda.h:107
struct coda_hit_info * hits[MAX_CRATES][MAX_SLOTS]
Definition: mc2coda.h:111
char expname[64]
Definition: mc2coda.h:95
CODA_EXP_INFO * mc2codaInitExp(int nCrates, const char *name)
Definition: mc2coda.c:45
void mc2codaSetRunNumber(unsigned int run_number)
Definition: mc2coda.c:34
#define MC2CINIT_DEFINED
Definition: mc2coda.h:82
#define MAX_SLOTS
Definition: mc2coda.h:69
unsigned int mc2codaCloseEvent(CODA_EVENT_INFO *event)
Definition: mc2coda.c:347
int crate_id
Definition: mc2coda.h:120
struct coda_exp_info * expid
Definition: mc2coda.h:108
static int mc2coda_ncrates_defined
Definition: mc2coda.c:22
int fadc250_write_data(CODA_EVENT_INFO *event, int roc, int slot, int mode)
CODA_CRATE_MAP * crate[MAX_CRATES]
Definition: mc2coda.h:100
uint64_t eventid
Definition: mc2coda.h:105
uint64_t trigtime
Definition: mc2coda.h:106
int hcount[MAX_CRATES][MAX_SLOTS]
Definition: mc2coda.h:110
#define ERROR
Definition: mc2coda.h:7
int mc2coda_inited
Definition: mc2coda.c:16
unsigned int * evbuf
Definition: mc2coda.h:113
int fadc125_write_data(CODA_EVENT_INFO *event, int roc, int slot, int mode)
#define ROC_BANK_OPEN(status, id, nevents)
Definition: mc2coda.h:134
int module_map[MAX_SLOTS]
Definition: mc2coda.h:90
int crate_id
Definition: mc2coda.h:87
int f1tdc48_write_data(CODA_EVENT_INFO *event, int roc, int slot, int mode)
static unsigned int * StartOfRocBank
Definition: mc2coda.c:23
int det_map[MAX_SLOTS]
Definition: mc2coda.h:91
int mc2codaSetCrate(CODA_EXP_INFO *expID, int crateid, int nmod, int *modules, int *detid)
Definition: mc2coda.c:102
int inited
Definition: mc2coda.h:97
int caen1290_write_data(CODA_EVENT_INFO *event, int roc, int slot, int mode)
#define FADC125_MODE_IP
Definition: mc2coda.h:54
#define MC2CINIT_COMPLETE
Definition: mc2coda.h:83
int openevents
Definition: mc2coda.h:98
static double start_time
Definition: mc2coda.c:26
#define MAX_EVENT_SIZE
Definition: mc2coda.h:75
#define FADC250_MODE_IP
Definition: mc2coda.h:49
unsigned short rocid[MAX_CRATES]
Definition: mc2coda.h:99
#define MAX_CRATES
Definition: mc2coda.h:68
int mc2codaFreeEvent(CODA_EVENT_INFO *eventID)
Definition: mc2coda.c:577
int mc2coda_maxevsize
Definition: mc2coda.c:18
#define MAX_HITS_PER_SLOT
Definition: mc2coda.h:78
static unsigned int RUN_NUMBER
Definition: mc2coda.c:24
int ncrates
Definition: mc2coda.h:96
void WriteDAQconfigBank(CODA_CRATE_MAP *crate, int roc)
static unsigned int * dabufp
Definition: mc2coda.c:23
unsigned int moduleMask
Definition: mc2coda.h:89
printf("string=%s", string)
void mc2codaStats(CODA_EVENT_INFO *eventID, int sflag)
Definition: mc2coda.c:669
uint32_t nwords
Definition: mc2coda.h:126
#define MC2CINIT_NULL
Definition: mc2coda.h:81
#define ROC_BANK_CLOSE
Definition: mc2coda.h:139
CODA_EXP_INFO mc2coda_expid
Definition: mc2coda.c:17
int mc2codaWrite(CODA_EVENT_INFO *event, int nHits, struct coda_hit_info *codaHits)
Definition: mc2coda.c:273
void mc2codaFree(CODA_EXP_INFO *expID)
Definition: mc2coda.c:639
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)
Definition: mc2coda.c:483
uint32_t * hdata
Definition: mc2coda.h:127
CODA_EVENT_INFO * mc2codaOpenEvent(CODA_EXP_INFO *expID, uint64_t eventNum, uint64_t trigTime, unsigned short eventType, int maxSize)
Definition: mc2coda.c:155