12from pathlib
import Path
15from subprocess
import run, CalledProcessError
16from shutil
import copy2
19import matplotlib.pyplot
as plt
21from prompt
import ValidationSettings
27r.PyConfig.IgnoreCommandLineOptions =
True
31plt.style.use(
"belle2")
34settings = ValidationSettings(name=
"Full VXD and CDC Alignment",
41 '''job_path will be replaced with path/to/calibration_results
42 input_data_path will be replaced with path/to/data_path used for calibration
43 e.g. /group/belle2/dataprod/Data/PromptSkim/'''
45 collector_output_dir_cosmic = Path(job_path) /
'VXDCDCalignment_validation/0/collector_output/cosmic/'
46 collector_output_dir_mumu = Path(job_path) /
'VXDCDCalignment_validation/0/collector_output/mumu/'
48 output_dir = Path(kwargs.get(
'output_dir',
'VXDCDCAlignmentValidation_output'))
50 output_dir.mkdir(parents=
True, exist_ok=
True)
52 pattern_cosmic = str(collector_output_dir_cosmic) +
"/*/cosmic_ana.root"
53 pattern_mumu = str(collector_output_dir_mumu) +
"/*/dimuon_ana.root"
55 def hadd_and_get_merged_file(filenames_pattern, input_type):
56 root_files = glob(filenames_pattern)
57 merged_file = output_dir / f
"{input_type}.root"
59 if len(root_files) > 1:
62 [
"hadd",
"-f", merged_file, *root_files],
66 except CalledProcessError
as e:
67 print(f
"hadd failed with exit code {e.returncode}")
71 elif len(root_files) == 1:
72 copy2(root_files[0], merged_file)
74 raise FileNotFoundError(f
"No root files found for pattern: {filenames_pattern}")
76 return str(merged_file)
78 cosmic_file = hadd_and_get_merged_file(pattern_cosmic,
"cosmics")
79 mumu_file = hadd_and_get_merged_file(pattern_mumu,
"dimuon")
81 print(f
"Merged ntuples saved in {cosmic_file} and {mumu_file}")
82 print(
"Running validation...")
84 cosmicval.run_validation([cosmic_file], output_dir=str(output_dir /
"cosmics/"))
85 dimuonval.run_validation([mumu_file], output_dir=str(output_dir /
"dimuon/"))
88 histo_pattern_cosmic = str(collector_output_dir_cosmic) +
"/*/CollectorOutput.root"
89 histo_pattern_mumu = str(collector_output_dir_mumu) +
"/*/CollectorOutput.root"
91 histo_cosmic = hadd_and_get_merged_file(histo_pattern_cosmic,
"cosmic_CollectorOutput")
92 histo_mumu = hadd_and_get_merged_file(histo_pattern_mumu,
"dimuon_CollectorOutput")
94 print(f
"Merged CollectorOutput histograms saved in {histo_cosmic} and {histo_mumu}")
96 print(
"Alignment validation completed.")
99if __name__ ==
'__main__':
100 parser = argparse.ArgumentParser(description=__doc__,
101 formatter_class=argparse.RawTextHelpFormatter)
106 parser.add_argument(
'calibration_results_dir',
107 help=
'The directory that contains the collector outputs',
110 parser.add_argument(
'-o',
'--output_dir',
111 help=
'The directory where all the output will be saved',
112 default=
'VXDCDCAlignmentValidation_output')
113 args = parser.parse_args()
115 run_validation(args.calibration_results_dir[0], output_dir=args.output_dir)