73 int *output_bank_size);
78 int *output_bank_size,
bool *bad_block,
79 caen_hit *caen_trailing_hits,
int *num_caen_trailing_hits);
82 int *output_bank_size,
bool *bad_block);
88 "MCaenCompProcessRaw",
112 if (db_find_key(
hDB, 0, key_path, &hKey) ==
SUCCESS) {
115 sprintf(key_name,
"%s/ext clock frequency MHz", key_path);
117 int size =
sizeof(caen_freq);
118 db_get_value(
hDB, 0, key_name, &caen_freq, &size, TID_INT, 0);
119 if (caen_freq != 25) {
120 fprintf(stderr,
"Error: CAEN %d clock freq found as %d, should be 25 (ODB problem)\n",
caen, caen_freq);
125 sprintf(key_name,
"%s/wiring/channel", key_path);
127 size =
sizeof(caen_wiring);
128 db_get_value(
hDB, 0, key_name, caen_wiring, &size, TID_INT, 0);
139 for (
int comp = 0; comp <
kMaxComps; comp++) {
143 sprintf(key_path,
"Equipment/Crate %d/Settings/COMP %d",
crate, comp);
144 if (db_find_key(
hDB, 0, key_path, &hKey) ==
SUCCESS) {
147 sprintf(key_name,
"%s/ext clock frequency MHz", key_path);
149 int size =
sizeof(comp_freq);
150 db_get_value(
hDB, 0, key_name, &comp_freq, &size, TID_INT, 0);
151 if (comp_freq != 50) {
152 fprintf(stderr,
"Error: COMP %d clock freq found as %d, should be 50 (ODB problem)\n", comp, comp_freq);
158 sprintf(key_name,
"%s/wiring/FPGA %d/channel", key_path, fpga);
159 if (db_find_key(
hDB, 0, key_name, &hKey) ==
SUCCESS) {
161 size =
sizeof(comp_wiring);
162 db_get_value(
hDB, 0, key_name, comp_wiring, &size, TID_INT, 0);
189 for(
int comp = 0; comp <
kMaxComps; comp++) {
194 char name[80],
title[80];
196 sprintf(name,
"blocks_skipped_caen_error");
197 sprintf(title,
"Blocks skipped due to CAEN errors");
201 sprintf(name,
"blocks_skipped_comp_error");
202 sprintf(title,
"Blocks skipped due to irresolvable COMP errors");
215 bool skip_block =
false;
218 int num_caen_trailing_hits = 0;
220 bool bad_block =
false;
221 num_hits[stream] =
make_caen(
caen, pevent, &temp_hits[stream],
223 caen_trailing_hits, &num_caen_trailing_hits);
224 if(bad_block) skip_block =
true;
225 if(num_hits[stream] != 0) {
231 fprintf(stderr,
"Skipping block %d due to CAEN processing errors.\n", pheader->serial_number);
237 for(
int comp = 0; comp <
kMaxComps; comp++) {
238 bool bad_block =
false;
239 num_hits[stream] =
make_comp(comp, pevent, &temp_hits[stream],
241 if(bad_block) skip_block =
true;
242 if(num_hits[stream] != 0) {
248 fprintf(stderr,
"Skipping block %d due to COMP processing errors.\n", pheader->serial_number);
255 bk_create(pevent,
"HITS", TID_DWORD, (
DWORD *) &output_bank);
256 int output_bank_size =
merge_hits(stream, num_hits, temp_hits, output_bank);
257 bk_close(pevent, output_bank + output_bank_size);
261 bk_create(pevent,
"CAET", TID_DWORD, (
DWORD *) &caen_trailing_hit_bank);
262 for (
int index = 0; index < num_caen_trailing_hits; index++) {
263 caen_trailing_hit_bank[index].
time = caen_trailing_hits[index].
time;
264 caen_trailing_hit_bank[index].
caen = caen_trailing_hits[index].
caen;
266 bk_close(pevent, caen_trailing_hit_bank + num_caen_trailing_hits);
275 int new_output_bank_size = *output_bank_size * 2;
277 memcpy(new_output_bank, *output_bank_handle,
279 delete[] *output_bank_handle;
280 *output_bank_handle = new_output_bank;
281 *output_bank_size = new_output_bank_size;
291 }
else if(hit1->
time < hit2->
time) {
336 int stream_ptr[num_streams];
337 int total_num_hits = 0;
339 for(
int stream = 0; stream < num_streams; stream++) {
340 total_num_hits += num_hits[stream];
341 stream_ptr[stream] = 0;
344 for(
int i = 0;
i < total_num_hits;
i++) {
348 double best_time = 1e99;
349 for(
int stream = 0; stream < num_streams; stream++) {
350 if(stream_ptr[stream] < num_hits[stream] &&
351 hits[stream][stream_ptr[stream]].
time < best_time) {
352 best_stream = stream;
353 best_time = hits[stream][stream_ptr[stream]].
time;
358 output[
i] = hits[best_stream][stream_ptr[best_stream]];
359 stream_ptr[best_stream]++;
362 return total_num_hits;
368 int *output_bank_size,
bool *bad_block,
369 caen_hit *caen_trailing_hits,
int *num_caen_trailing_hits)
376 sprintf(bank_name,
"CAE%d", caen);
377 int caen_bank_size = bk_locate(pevent, bank_name, (
DWORD *) &caen_bank);
378 if(caen_bank == NULL)
return 0;
379 int block_number = ((EVENT_HEADER *) pevent - 1)->serial_number;
396 double caen_bank_abstime[caen_bank_size];
397 int output_pointer = 0;
398 int prev_time_word = 0;
399 int num_overflows = 0;
400 bool header_found =
false;
401 bool EOB_found =
false;
402 int EOB_reported_data_count = 0;
403 int EOB_reported_error_status = 0;
404 int num_trailing_edges = 0;
407 for (
int j=0; j<caen_bank_size; j++) {
411 if(caen_bank[j].r2 == 3) {
412 fprintf(stderr,
"Encountered DAQ-generated error flag in block %d CAEN%d data at j=%d.\n",
413 block_number, caen, j);
418 int error_bits = caen_bank[j].
time;
419 for (
int TDC=0; TDC<4; TDC++) {
420 if (error_bits & (1<<TDC)) {
427 if (caen_bank[j].r2 == 2) {
433 if(caen_bank[j].r2 == 1) {
435 EOB_reported_data_count = (caen_bank[j].
time & 0xffff);
436 EOB_reported_error_status = (caen_bank[j].
channel & 0x07);
437 if (EOB_reported_error_status) {
438 fprintf(stderr,
"Encountered EOB error flag in block %d CAEN%d data.\n",
449 if(caen_bank[j].start != 0 ||
450 caen_bank[j].r2 != 0) {
456 if(caen_bank[j].edge == 1) {
461 if (!((parameter>=6500) && (parameter<=6502))) {
465 num_trailing_edges++;
467 fprintf(stderr,
"Encountered too many trailing edges in block %d CAEN%d data (%d).\n",
468 block_number, caen, num_trailing_edges);
485 if(caen_bank[j].
time < prev_time_word &&
486 prev_time_word - caen_bank[j].
time > 0x80000) {
488 }
else if(caen_bank[j].
time > prev_time_word &&
489 caen_bank[j].
time - prev_time_word > 0x80000) {
492 prev_time_word = caen_bank[j].
time;
493 caen_bank_abstime[j] =
497 if (caen_bank[j].edge == 1) {
500 if(output_pointer >= *output_bank_size) {
502 output_bank = *output_bank_handle;
505 output_bank[output_pointer].
time = caen_bank_abstime[j];
513 if (caen_bank[j].edge == 0) {
515 caen_trailing_hits[*num_caen_trailing_hits].
time = caen_bank_abstime[j];
516 caen_trailing_hits[*num_caen_trailing_hits].
caen =
caen;
517 (*num_caen_trailing_hits)++;
518 }
else if (!(*bad_block)) {
519 fprintf(stderr,
"Exceeded trailing edge data stream limit in block %d CAEN%d data.\n",
532 double lastTime = -1;
533 if(output_pointer != 0) {
534 lastTime = output_bank[output_pointer-1].
time;
538 return output_pointer;
546 char name[80],
title[80];
548 sprintf(name,
"DAQ_caen%d_edges_leading_by_channel", caen);
549 sprintf(title,
"CAEN %d leading edges vs. channel", caen);
553 sprintf(name,
"DAQ_caen%d_edges_leading_by_block", caen);
554 sprintf(title,
"CAEN %d leading edges vs. block number", caen);
558 sprintf(name,
"DAQ_caen%d_edges_leading_32BitInterp", caen);
559 sprintf(title,
"CAEN %d leading edges' 32-bit Interpolator Dist", caen);
561 new TH1D(name, title, 32, -0.5, 31.5);
563 sprintf(name,
"DAQ_caen%d_edges_leading_32BitInterp_noRollover", caen);
564 sprintf(title,
"CAEN %d leading edges' 32-bit Interpolator Dist, No Rollover", caen);
566 new TH1D(name, title, 32, -0.5, 31.5);
568 sprintf(name,
"DAQ_caen%d_edges_trailing_by_channel", caen);
569 sprintf(title,
"CAEN %d trailing edges vs. channel", caen);
573 sprintf(name,
"DAQ_caen%d_edges_trailing_by_block", caen);
574 sprintf(title,
"CAEN %d trailing edges vs. block number", caen);
578 sprintf(name,
"DAQ_caen%d_error_DAQ", caen);
579 sprintf(title,
"Blocks with DAQ-inserted CAEN %d errors", caen);
581 new TH1D(name, title, 3, -0.5, 2.5);
582 sprintf(name,
"DAQ_caen%d_error_DAQ_by_TDC", caen);
583 sprintf(title,
"CAEN %d DAQ-inserted errors: TDC vs. block number", caen);
587 sprintf(name,
"DAQ_caen%d_error_EOB", caen);
588 sprintf(title,
"Blocks with CAEN %d EOB error flag", caen);
590 new TH1D(name, title, 3, -0.5, 2.5);
592 sprintf(name,
"DAQ_caen%d_error_trailing_edge_stream_limit", caen);
593 sprintf(title,
"Blocks where CAEN %d trailing edges exceeded stream limit", caen);
595 new TH1D(name, title, 3, -0.5, 2.5);
597 sprintf(name,
"DAQ_caen%d_error_trailing_edge_block_limit", caen);
598 sprintf(title,
"Blocks with too many CAEN %d trailing edges", caen);
600 new TH1D(name, title, 3, -0.5, 2.5);
602 sprintf(name,
"DAQ_caen%d_last_time", caen);
603 sprintf(title,
"CAEN %d last time vs. block number", caen);
613 int *output_bank_size,
bool *bad_block)
616 int output_pointer = 0;
617 int block_number = ((EVENT_HEADER *) pevent - 1)->serial_number;
622 sprintf(bank_name,
"CMP%d", comp);
623 int comp_bank_size = bk_locate(pevent, bank_name, (
DWORD *) &comp_bank);
624 if(comp_bank == NULL) {
638 int num_backwards = 0;
639 bool gone_backwards_here[comp_bank_size];
640 for(
int i = 0;
i < comp_bank_size;
i++) {
641 gone_backwards_here[
i] =
false;
644 for(
int i = 0;
i < comp_bank_size;
i++) {
645 int fpga = comp_bank[
i].
r1.
fpga;
647 int a = comp_bank[
i].
r1.
a;
648 int b = comp_bank[
i].
r1.
b;
651 if((fpga >= kMaxCompFpgas) || (time < prev_time[fpga]) || (!a && !b)) {
660 if(!gone_backwards_here[
i]) {
661 gone_backwards_here[
i] =
true;
668 prev_time[fpga] =
time;
673 unsigned int word = comp_bank[
i].
word;
674 for(
int j = 0; j < 32; j++) {
675 if(word & (1 << j)) {
678 if(output_pointer >= *output_bank_size) {
680 output_bank = *output_bank_handle;
694 unsigned int word = comp_bank[
i].
word;
695 for(
int j = 0; j < 32; j++) {
696 if(word & (1 << j)) {
698 if(output_pointer >= *output_bank_size) {
700 output_bank = *output_bank_handle;
714 if (num_backwards > 0 && num_backwards <= 10) {
715 fprintf(stderr,
"Encountered %d backward time steps in block %d COMP%d data.\n",
716 num_backwards, block_number, comp);
719 else if (num_backwards > 10) {
720 fprintf(stderr,
"Encountered too many (%d) backward time steps in block %d COMP%d data: skipping block.\n",
721 num_backwards, block_number, comp);
735 SetBinContent(block_number, output_bank[output_pointer-1].
time);
737 return output_pointer;
745 char name[80],
title[80];
747 sprintf(name,
"DAQ_comp%d_error_timing_per_block", comp);
748 sprintf(title,
"COMP %d timing errors vs. block number", comp);
752 sprintf(name,
"DAQ_comp%d_error_timing_limit", comp);
753 sprintf(title,
"Blocks where COMP %d backwards steps exceeded limit", comp);
755 new TH1D(name, title, 3, -0.5, 2.5);
757 sprintf(name,
"DAQ_comp%d_error_stream_limit", comp);
758 sprintf(title,
"Blocks where COMP %d hits exceeded stream limit", comp);
760 new TH1D(name, title, 3, -0.5, 2.5);
762 sprintf(name,
"DAQ_comp%d_last_time", comp);
763 sprintf(title,
"COMP %d last time vs. block number", comp);