-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_gsf.py
147 lines (110 loc) · 4.85 KB
/
run_gsf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import logging
import pickle
import warnings
import json
import datetime
from typing import NamedTuple
from pathlib import Path
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from apyts.geometry import *
from apyts.simulation import *
from apyts.kalman_fitter import *
from apyts.gsf import *
from apyts.gsf_utils import *
from apyts.constants import *
from apyts.test_utils import *
import apyts.units as u
def run(args):
if args["output_path"] is not None:
output_path = Path(args["output_path"])
assert output_path.exists()
else:
output_path = None
logging.info("Start")
geometry = Geometry([0, 100, 200, 300, 400, 500, 600, 700, 800, 900],
surface_radius=300 * u.mm,
thickness_in_x0=0.5*u.mm / kSiRadiationLength,
b_field=2 * u.Tesla,
no_plot=True,
)
############
# Simulate #
############
logging.info("Simulation: smearing std = {}, radiation_loss = {}".format(args["simulation_std"], args["radiation_loss"]))
simulation = Simulation(geometry, smearing_stddev=args["simulation_std"], simulate_radiation_loss=args["radiation_loss"])
locs = np.full(args["n_particles"], 0*u.mm)
phis = np.full(args["n_particles"], 90*u.degree)
qops = -1./np.full(args["n_particles"], 4*u.GeV)
logging.info("Fixed start parameters ({:.2f},{:.2f},{:.2f})".format(locs[0], phis[0], qops[0]))
true_pars = np.stack((locs, phis, qops), axis=1)
sim_results = [ simulation.simulate(pars, kElectronMass) for pars in true_pars ]
if output_path:
with open(output_path / "sim_result.pickle", 'wb') as f:
pickle.dump(dict(
true_pars=true_pars,
sim_results=sim_results,
stddev_sim=args["simulation_std"],
simulate_rad_loss=args["radiation_loss"]
), f)
logging.info("Wrote simulation data to '{}'".format(f.name))
logging.info("Done with simulation")
##########################
# Smear start parameters #
##########################
# Fixed initial cov for loc and phi
std_loc = 20*u.um
std_phi = 1*u.degree
# Momentum dependent cov for qop
std_p_rel = 0.05
p = abs(1./true_pars[:, eBoundQoP])
std_qop = std_p_rel * p / p**2
logging.info("Start parameter smearing std: ({:.2f}, {:.2f}, {:.2f}+-{:.2f})".format(std_loc, std_phi, np.mean(std_qop), np.std(std_qop)))
smearing = np.stack([
std_loc * np.random.normal(0, 1, size=args["n_particles"]),
std_phi * np.random.normal(0, 1, size=args["n_particles"]),
std_qop * np.random.normal(0, 1, size=args["n_particles"]),
], axis=-1)
smeared_pars = true_pars + smearing
covs = np.zeros((args["n_particles"],3,3))
covs[:,eBoundLoc, eBoundLoc] = std_loc**2
covs[:,eBoundPhi, eBoundPhi] = std_phi**2
covs[:,eBoundQoP, eBoundQoP] = std_qop**2
if output_path:
with open(output_path / "start_parameters.pickle", 'wb') as f:
pickle.dump(dict(parameters=smeared_pars, covs=covs, smeared=True), f)
logging.info("Wrote start data to '{}'".format(f.name))
##################
# Fit & Analysis #
##################
logging.info("variance inflation: {}".format(args["variance_inflation"]))
covs = covs * args["variance_inflation"]
projector = np.array([[1, 0, 0]])
kf = KalmanFitter(geometry, projector)
gsf = GSF(geometry, projector, max_components=12, weight_cutoff=1.e-8, full_kl_divergence=False)
for fitter, name in zip([kf, gsf], ["KF", "GSF"]):
fit_result = [
fitter.fit(pars, cov, surfaces, measuements, len(surfaces)*[args["simulation_std"]**2])
for pars, cov, (measuements, _, surfaces) in zip(smeared_pars, covs, sim_results)
]
if output_path:
with open(output_path / "{}_fit_result.pickle".format(name.lower()), 'wb') as f:
pickle.dump(dict(fit_result=fit_result, var_inflation=args["variance_inflation"]), f)
logging.info("Wrote fit data to '{}'".format(f.name))
logging.info("Done with {} fitting".format(name))
if __name__ == "__main__":
args = {
"simulation_std": 0.01 * u.mm,
"n_particles" : 2000,
"radiation_loss": True,
"variance_inflation": 100.0,
}
args["output_path"] = "output/{}-{}particles".format(datetime.datetime.now().strftime("%Y%m%d_%H%M%S"), args["n_particles"])
Path(args["output_path"]).mkdir(parents=True, exist_ok=True)
logging.getLogger().setLevel(logging.INFO)
logging.basicConfig(format='%(asctime)s\t%(levelname)s\t%(message)s', datefmt='%H:%M:%S')
pprint.pprint(args, indent=4)
with open(Path(args["output_path"]) / "args.json", 'w') as f:
json.dump(args, f, indent=4)
run(args)