Belle II Software development
vxdcdc_alignment.py
1#!/usr/bin/env python3
2
3
10
11import os
12from glob import glob
13from pathlib import Path
14import argparse
15
16import matplotlib
17import matplotlib.pyplot as plt
18
19from prompt import ValidationSettings
20
21import alignment_validation.dimuon as dimuonval
22import alignment_validation.cosmics as cosmicval
23
24import ROOT as r
25r.PyConfig.IgnoreCommandLineOptions = True
26r.gROOT.SetBatch()
27
28matplotlib.use('Agg')
29plt.style.use("belle2")
30
31
32settings = ValidationSettings(name="Full VXD and CDC Alignment",
33 description=__doc__,
34 download_files=[],
35 expert_config=None)
36
37
38def run_validation(job_path, input_data_path=None, **kwargs):
39 '''job_path will be replaced with path/to/calibration_results
40 input_data_path will be replaced with path/to/data_path used for calibration
41 e.g. /group/belle2/dataprod/Data/PromptSkim/'''
42
43 collector_output_dir_cosmic = Path(job_path) / 'VXDCDCalignment_validation/0/collector_output/cosmic/'
44 collector_output_dir_mumu = Path(job_path) / 'VXDCDCalignment_validation/0/collector_output/mumu/'
45
46 output_dir = Path(kwargs.get('output_dir', 'VXDCDCAlignmentValidation_output'))
47 # create output directory if it does not exist
48 output_dir.mkdir(parents=True, exist_ok=True)
49
50 pattern_cosmic = str(collector_output_dir_cosmic) + "/*/cosmic_ana.root"
51 pattern_mumu = str(collector_output_dir_mumu) + "/*/dimuon_ana.root"
52
53 def hadd_and_get_merged_file(filenames_pattern, input_type):
54 root_files = glob(filenames_pattern)
55 merged_file = output_dir / f"{input_type}.root"
56
57 if len(root_files) > 1:
58 os.system(f"hadd -f {merged_file} {' '.join(root_files)}")
59 elif len(root_files) == 1:
60 os.system(f"cp {root_files[0]} {merged_file}")
61 else:
62 raise FileNotFoundError(f"No root files found for pattern: {filenames_pattern}")
63
64 return str(merged_file)
65
66 cosmic_file = hadd_and_get_merged_file(pattern_cosmic, "cosmics")
67 mumu_file = hadd_and_get_merged_file(pattern_mumu, "dimuon")
68
69 print(f"Merged ntuples saved in {cosmic_file} and {mumu_file}")
70 print("Running validation...")
71
72 cosmicval.run_validation([cosmic_file], output_dir=str(output_dir / "cosmics/"))
73 dimuonval.run_validation([mumu_file], output_dir=str(output_dir / "dimuon/"))
74
75 # Now merge std histograms stored in ColectorOutput.root
76 histo_pattern_cosmic = str(collector_output_dir_cosmic) + "/*/CollectorOutput.root"
77 histo_pattern_mumu = str(collector_output_dir_mumu) + "/*/CollectorOutput.root"
78
79 histo_cosmic = hadd_and_get_merged_file(histo_pattern_cosmic, "cosmic_CollectorOutput")
80 histo_mumu = hadd_and_get_merged_file(histo_pattern_mumu, "dimuon_CollectorOutput")
81
82 print(f"Merged CollectorOutput histograms saved in {histo_cosmic} and {histo_mumu}")
83
84 print("Alignment validation completed.")
85
86
87if __name__ == '__main__':
88 parser = argparse.ArgumentParser(description=__doc__,
89 formatter_class=argparse.RawTextHelpFormatter)
90
91 # b2val-prompt-run wants to pass to the script also input_data_path
92 # and requested_iov. As they are not required by this validation I just accept
93 # them together with calibration_results_dir and then ignore them
94 parser.add_argument('calibration_results_dir',
95 help='The directory that contains the collector outputs',
96 nargs='+')
97
98 parser.add_argument('-o', '--output_dir',
99 help='The directory where all the output will be saved',
100 default='VXDCDCAlignmentValidation_output')
101 args = parser.parse_args()
102
103 run_validation(args.calibration_results_dir[0], output_dir=args.output_dir)