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