20 import matplotlib.pyplot
as plt
23 import multiprocessing
31 from typing
import Optional, Union
32 from pathlib
import Path
34 from optuna.visualization
import plot_contour
35 from optuna.visualization
import plot_edf
36 from optuna.visualization
import plot_intermediate_values
37 from optuna.visualization
import plot_optimization_history
38 from optuna.visualization
import plot_parallel_coordinate
39 from optuna.visualization
import plot_param_importances
40 from optuna.visualization
import plot_slice
42 srcDir = Path(__file__).resolve().parent
47 if len(params) != len(names):
48 raise Exception(
"Length of Params must equal names")
50 ckf_script = srcDir /
"ckf.py"
52 indir =
"--indir=" +
str(srcDir)
53 outdir =
"--output=" +
str(outDir)
56 ret.append(ckf_script)
63 arg =
"--sf_" + names[i] +
"=" +
str(param)
86 maxSeedsPerSpM = trial.suggest_int(
"maxSeedsPerSpM", 0, 10)
87 params.append(maxSeedsPerSpM)
88 cotThetaMax = trial.suggest_float(
"cotThetaMax", 5.0, 10.0)
89 params.append(cotThetaMax)
90 sigmaScattering = trial.suggest_float(
"sigmaScattering", 0.2, 50)
91 params.append(sigmaScattering)
92 radLengthPerSeed = trial.suggest_float(
"radLengthPerSeed", 0.001, 0.1)
93 params.append(radLengthPerSeed)
94 impactMax = trial.suggest_float(
"impactMax", 0.1, 25)
95 params.append(impactMax)
96 maxPtScattering = trial.suggest_float(
"maxPtScattering", 1, 50)
97 params.append(maxPtScattering)
98 deltaRMin = trial.suggest_float(
"deltaRMin", 0.25, 30)
99 params.append(deltaRMin)
100 deltaRMax = trial.suggest_float(
"deltaRMax", 50, 300)
101 params.append(deltaRMax)
113 outputDir = Path(srcDir /
"Output_CKF")
114 outputfile = srcDir /
"Output_CKF/performance_ckf.root"
115 outputDir.mkdir(exist_ok=
True)
116 run_ckf(params, keys, outputDir)
117 rootFile = uproot.open(outputfile)
118 self.
res[
"eff"].append(rootFile[
"eff_particles"].member(
"fElements")[0])
119 self.
res[
"fakerate"].append(rootFile[
"fakerate_tracks"].member(
"fElements")[0])
120 self.
res[
"duplicaterate"].append(
121 rootFile[
"duplicaterate_tracks"].member(
"fElements")[0]
124 timingfile = srcDir /
"Output_CKF/timing.tsv"
125 timing = pd.read_csv(timingfile, sep=
"\t")
127 timing[timing[
"identifier"].str.match(
"Algorithm:TrackFindingAlgorithm")][
131 time_seeding = float(
132 timing[timing[
"identifier"].str.match(
"Algorithm:SeedingAlgorithm")][
136 self.
res[
"runtime"].append(time_ckf + time_seeding)
138 efficiency = self.
res[
"eff"][-1]
140 self.
res[
"fakerate"][-1]
141 + self.
res[
"duplicaterate"][-1] / self.
k_dup
145 return efficiency - penalty
159 "cotThetaMax": 7.40627,
160 "sigmaScattering": 50,
161 "radLengthPerSeed": 0.1,
163 "maxPtScattering": 10.0,
170 optuna.logging.get_logger(
"optuna").addHandler(logging.StreamHandler(sys.stdout))
171 study_name =
"test_study"
172 storage_name =
"sqlite:///{}.db".
format(study_name)
175 study = optuna.create_study(
176 study_name=study_name,
177 storage=
"sqlite:///{}.db".
format(study_name),
178 direction=
"maximize",
184 study.optimize(objective, n_trials=3)
187 print(
"Best Trial until now", flush=
True)
188 for key, value
in study.best_trial.params.items():
189 print(f
" {key}: {value}", flush=
True)
191 outputDir = Path(
"OptunaResults")
192 outputDir.mkdir(exist_ok=
True)
194 with
open(outputDir /
"results.json",
"w")
as fp:
195 json.dump(study.best_params, fp)
198 if __name__ ==
"__main__":