279 """
280 Drives the processing and plotting.
281 """
282
283 args = parse()
284 filename = args.filename
285 verbosity = args.verbose
286 no_displays = args.no_displays
287 start_frag = args.start_frag
288 end_frag = args.end_frag
289 no_anomaly = args.no_anomaly
290 seconds = args.seconds
291 overwrite = args.overwrite
292 batch_mode = args.batch_mode
293
294 linear = args.linear
295 log = args.log
296
297
298 if (not linear) and (not log):
299 linear = True
300 log = True
301
303
304
305 if len(data.get_fragment_paths()) == 0:
306 print("File doesn't contain any TriggerActivity fragments.")
307 return 1
308
309
310 if start_frag == 0 and end_frag == -1:
311 data.read_all_fragments()
312 else:
313 if end_frag != 0:
314 frag_paths = data.get_fragment_paths()[start_frag:end_frag]
315 elif end_frag == 0:
316 frag_paths = data.get_fragment_paths()[start_frag:]
317
318 for path in frag_paths:
319 data.read_fragment(path)
320
321
322 save_name = find_save_name(data.run_id, data.file_index, overwrite)
323
324 print(f"Number of TAs: {data.ta_data.shape[0]}")
325
326
327
328 if not no_anomaly:
329 anomaly_filename = f"{save_name}.txt"
330 print(f"Writing descriptive statistics to {anomaly_filename}.")
331 if os.path.isfile(anomaly_filename):
332
333 os.remove(anomaly_filename)
334
335 time_label = "Time (s)" if seconds else "Time (Ticks)"
336
337
338 plot_hist_dict = {
339 'adc_integral': {
340 'title': "ADC Integral Histogram",
341 'xlabel': "ADC Integral",
342 'ylabel': "Count",
343 'linear': linear,
344 'linear_style': dict(color='#63ACBE', alpha=0.6, label='Linear'),
345 'log': log,
346 'log_style': dict(color='#EE442F', alpha=0.6, label='Log')
347 },
348 'adc_peak': {
349 'title': "ADC Peak Histogram",
350 'xlabel': "ADC Count",
351 'ylabel': "Count",
352 'linear': linear,
353 'linear_style': dict(color='#63ACBE', alpha=0.6, label='Linear'),
354 'log': log,
355 'log_style': dict(color='#EE442F', alpha=0.6, label='Log')
356 },
357 'algorithm': {
358 'bins': np.sort(np.array([(tick-0.45, tick+0.45) for tick in ALGORITHM_TICKS]).flatten()),
359 'title': "Algorithm Histogram",
360 'xlabel': 'Algorithm Type',
361 'ylabel': "Count",
362 'linear': True,
363 'linear_style': dict(color='k'),
364 'log': False,
365 'xticks': {
366 'labels': ALGORITHM_LABELS,
367 'ticks': ALGORITHM_TICKS,
368 'fontsize': 6,
369 'rotation': 60,
370 'ha': 'right'
371 }
372 },
373
374
375
376
377 'channel_end': {
378 'title': "Channel End Histogram",
379 'xlabel': "Channel Number",
380 'ylabel': "Count",
381 'linear': linear,
382 'linear_style': dict(color='#63ACBE', alpha=0.6, label='Linear'),
383 'log': log,
384 'log_style': dict(color='#EE442F', alpha=0.6, label='Log')
385 },
386 'channel_peak': {
387 'title': "Channel Peak Histogram",
388 'xlabel': "Channel Number",
389 'ylabel': "Count",
390 'linear': linear,
391 'linear_style': dict(color='#63ACBE', alpha=0.6, label='Linear'),
392 'log': log,
393 'log_style': dict(color='#EE442F', alpha=0.6, label='Log')
394 },
395 'channel_start': {
396 'title': "Channel Start Histogram",
397 'xlabel': "Channel Number",
398 'ylabel': "Count",
399 'linear': linear,
400 'linear_style': dict(color='#63ACBE', alpha=0.6, label='Linear'),
401 'log': log,
402 'log_style': dict(color='#EE442F', alpha=0.6, label='Log')
403 },
404 'detid': {
405 'title': "Detector ID Histogram",
406 'xlabel': "Detector IDs",
407 'ylabel': "Count",
408 'linear': linear,
409 'linear_style': dict(color='#63ACBE', alpha=0.6, label='Linear'),
410 'log': log,
411 'log_style': dict(color='#EE442F', alpha=0.6, label='Log'),
412 'use_integer_xticks': True
413 },
414 'num_tps': {
415 'title': "Number of TPs per TA Histogram",
416 'xlabel': "Number of TPs",
417 'ylabel': "Count",
418 'linear': linear,
419 'linear_style': dict(color='#63ACBE', alpha=0.6, label='Linear'),
420 'log': log,
421 'log_style': dict(color='#EE442F', alpha=0.6, label='Log')
422 },
423 'time_activity': {
424 'title': "Relative Time Activity Histogram",
425 'xlabel': time_label,
426 'ylabel': "Count",
427 'linear': linear,
428 'linear_style': dict(color='#63ACBE', alpha=0.6, label='Linear'),
429 'log': log,
430 'log_style': dict(color='#EE442F', alpha=0.6, label='Log')
431 },
432 'time_end': {
433 'title': "Relative Time End Histogram",
434 'xlabel': time_label,
435 'ylabel': "Count",
436 'linear': linear,
437 'linear_style': dict(color='#63ACBE', alpha=0.6, label='Linear'),
438 'log': log,
439 'log_style': dict(color='#EE442F', alpha=0.6, label='Log')
440 },
441 'time_peak': {
442 'title': "Relative Time Peak Histogram",
443 'xlabel': time_label,
444 'ylabel': "Count",
445 'linear': linear,
446 'linear_style': dict(color='#63ACBE', alpha=0.6, label='Linear'),
447 'log': log,
448 'log_style': dict(color='#EE442F', alpha=0.6, label='Log')
449 },
450 'time_start': {
451 'title': "Relative Time Start Histogram",
452 'xlabel': time_label,
453 'ylabel': "Count",
454 'linear': linear,
455 'linear_style': dict(color='#63ACBE', alpha=0.6, label='Linear'),
456 'log': log,
457 'log_style': dict(color='#EE442F', alpha=0.6, label='Log')
458 },
459 'type': {
460 'bins': np.sort(np.array([(tick-0.45, tick+0.45) for tick in TYPE_TICKS]).flatten()),
461 'title': "Type Histogram",
462 'xlabel': "Type",
463 'ylabel': "Count",
464 'linear': True,
465 'linear_style': dict(color='k'),
466 'log': False,
467 'xticks': {
468 'labels': TYPE_LABELS,
469 'ticks': TYPE_TICKS,
470 'fontsize': 6,
471 'rotation': 60,
472 'ha': 'right'
473 }
474 },
475 'version': {
476 'title': "Version Histogram",
477 'xlabel': "Versions",
478 'ylabel': "Count",
479 'linear': linear,
480 'linear_style': dict(color='#63ACBE', alpha=0.6, label='Linear'),
481 'log': log,
482 'log_style': dict(color='#EE442F', alpha=0.6, label='Log'),
483 'use_integer_xticks': True
484 }
485 }
486
487 pdf_plotter = PDFPlotter(f"{save_name}.pdf")
488
489 for ta_key in data.ta_data.dtype.names:
490 if 'time' in ta_key:
491 time = data.ta_data[ta_key]
492 if seconds:
493 time = time * TICK_TO_SEC_SCALE
494 min_time = np.min(time)
495 pdf_plotter.plot_histogram(time - min_time, plot_hist_dict[ta_key])
496 if not no_anomaly:
497 write_summary_stats(time - min_time, anomaly_filename, ta_key)
498 continue
499
500 if ta_key == 'algorithm' or ta_key == 'type':
501 plot_data = np.array([datum.value for datum in data.ta_data[ta_key]], dtype=int)
502 pdf_plotter.plot_histogram(plot_data, plot_hist_dict[ta_key])
503 if not no_anomaly:
504 write_summary_stats(plot_data, anomaly_filename, ta_key)
505 del plot_data
506 continue
507
508 pdf_plotter.plot_histogram(data.ta_data[ta_key], plot_hist_dict[ta_key])
509 if not no_anomaly:
510 write_summary_stats(data.ta_data[ta_key], anomaly_filename, ta_key)
511
512
513 pdf = pdf_plotter.get_pdf()
514
515 if linear:
516 plot_pdf_time_delta_histograms(data.ta_data, data.tp_data, pdf, time_label, False)
517 if log:
518 plot_pdf_time_delta_histograms(data.ta_data, data.tp_data, pdf, time_label, True)
519
520
521
522 time_peak = data.ta_data['time_peak']
523 time_end = data.ta_data['time_end']
524 time_start = data.ta_data['time_start']
525 ta_min_time = np.min((time_peak, time_end, time_start))
526
527 time_peak -= ta_min_time
528 time_end -= ta_min_time
529 time_start -= ta_min_time
530
531 if seconds:
532 ta_min_time = ta_min_time * TICK_TO_SEC_SCALE
533 time_peak = time_peak * TICK_TO_SEC_SCALE
534 time_end = time_end * TICK_TO_SEC_SCALE
535 time_start = time_start * TICK_TO_SEC_SCALE
536
537 yerr = np.array([time_peak - time_start, time_end - time_peak]).astype(np.int64)
538 time_unit = "Seconds" if seconds else "Ticks"
539 time_spans_dict = {
540 'title': "TA Relative Time Spans",
541 'xlabel': "TA",
542 'ylabel': time_label,
543 'errorbar_style': {
544 'yerr': yerr,
545 'capsize': 4,
546 'color': 'k',
547 'ecolor': "#EE442F",
548 'label': f"Avg {time_unit} / TA: {(time_peak[-1] - time_peak[0]) / len(time_peak):.2f}",
549 'mec': "#EE442F",
550 'mfc': "#EE442F",
551 'marker': 'h',
552 'markersize': 4.00
553 }
554 }
555 ta_count = np.arange(len(time_peak))
556 pdf_plotter.plot_errorbar(ta_count, time_peak, time_spans_dict)
557
558 pdf_plotter.close()
559
560 if not no_displays:
561 plot_all_event_displays(data.tp_data, data.run_id, data.file_index, seconds)
562
563 return None
564
565
int main(int argc, char **argv)