-
Notifications
You must be signed in to change notification settings - Fork 1
/
unfold_roounfold.py
157 lines (128 loc) · 4.56 KB
/
unfold_roounfold.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
146
147
148
149
150
151
152
153
154
155
156
157
# Trying to unfold gamma-ray spectra via RooUnfold
from utilities import *
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
from ROOT import gRandom, TH1, TH2, TH1D, TH2D, cout, gROOT, TCanvas, TLegend
from ROOT import RooUnfoldResponse
from ROOT import RooUnfoldBayes
# from ROOT import RooUnfoldSvd
# from ROOT import RooUnfoldTUnfold
# from ROOT import RooUnfoldIds
# from ROOT import RooUnfoldInvert
fname_resp = 'resp-SuN2015-20keV-1p0FWHM.dat'
fname_resp_mat = 'response_matrix-SuN2015-20keV-1p0FWHM.dat'
R_2D, cal_resp, E_resp_array, tmp = read_mama_2D(fname_resp_mat)
# R_2D = div0(R_2D , R_2D.sum(rebin_axis=1))
# Read efficiency and other 1-D response variables:
resp = []
with open(fname_resp) as file:
# Read line by line as there is crazyness in the file format
lines = file.readlines()
for i in range(4,len(lines)):
try:
row = np.array(lines[i].split(), dtype="double")
resp.append(row)
except:
break
resp = np.array(resp)
# Name the columns for ease of reading
FWHM = resp[:,1]
eff = resp[:,2]
pf = resp[:,3]
pc = resp[:,4]
ps = resp[:,5]
pd = resp[:,6]
pa = resp[:,7]
# Assumed lower threshold for gammas in response matrix
E_thres = 100
i_thres = np.argmin(np.abs(E_resp_array - E_thres))
R_2D[:,:i_thres] = 0
for i in range(R_2D.shape[0]):
norm = R_2D[i,:].sum()
if(norm>0):
R_2D[i,:] = R_2D[i,:] / norm * eff[i]
else:
R_2D[i,:] = 0
# f_cmp, ax_cmp = plt.subplots(1,1)
# ax_cmp.plot(E_resp_array, R_2D[400,:])
# fig, ax = plt.subplots(1,1)
# im1 = ax.pcolormesh(E_resp_array, E_resp_array, R_2D, norm=LogNorm(vmin=1e-3, vmax=1e-1))
# plt.colorbar(im1)
# plt.show()
# ==============================================================================
# Example Unfolding
# ==============================================================================
Nbins = len(E_resp_array)
Emin = E_resp_array[0]
Emax = E_resp_array[-1]
hTrue= TH1D ("true", "Test Truth", Nbins, Emin, Emax);
hMeas= TH1D ("meas", "Test Measured", Nbins, Emin, Emax);
print("==================================== TRAIN ====================================")
response= RooUnfoldResponse (hMeas, hTrue);
for i in range(Nbins): # x_true
Ei = E_resp_array[i] # x_true
for j in range(Nbins): # x_measured
Ej = E_resp_array[j] # x_measured
mc = R_2D[i,j]
# response.Fill (x_measured, x_true)
response.Fill (Ej, Ei, mc);
# account for eff < 1
eff_ = R_2D[i,:].sum()
pmisses = 1-eff_ # probability of misses
response.Miss(Ei,pmisses)
print("==================================== TEST =====================================")
# # "True" Eg in keV, counts
# Eg_choose = np.array([[4000,2000]])
# Eg_choose = np.array([[4000,2000],
# [2000,1000],
# [1500,1000],
# [3000,500],
# ])
Eg_min = 1e3
i_Eg_choose = np.argmin(np.abs(E_resp_array - Eg_min))
N_in=40
Egs_in = E_resp_array[i_Eg_choose:i_Eg_choose+N_in]
def cnt(E):
# some dummy funciton to create a number of counts
return (0.2*(E-Egs_in[int(N_in/2)])**2 + 0.05* E)/100
Eg_choose = np.array([(Eg,cnt(Eg)) for Eg in Egs_in])
# Fill true and measured histograms
for Eg in Eg_choose:
i_Eg_choose = np.argmin(np.abs(E_resp_array - Eg[0]))
# # immitate statistical fluctuations of incident gamma rays
# ncounts = np.random.normal(loc=Eg[1],scale=np.sqrt(Eg[1]))
ncounts = Eg[1]
for i in range(Nbins):
Ei = E_resp_array[i]
ncounts_ = ncounts * R_2D[i_Eg_choose,i]
# immitate statistical fluctuations of response gamma rays
ncounts_ = np.random.normal(loc=ncounts_,scale=np.sqrt(ncounts_))
hMeas.Fill(Ei,ncounts_)
hTrue.Fill(Eg[0],Eg[1])
# hack to recalculate the Uncertainties now, after the histogram is filled
hMeas.Sumw2(False)
hMeas.Sumw2(True)
hTrue.Sumw2(False) # doesn't work yet?
hTrue.Sumw2(True) # doesn't work yet?
print("==================================== UNFOLD ===================================")
Niterations = 10
unfold= RooUnfoldBayes (response, hMeas, Niterations); # OR
# unfold= RooUnfoldSvd (response, hMeas, 20); # OR
#unfold= RooUnfoldTUnfold (response, hMeas); # OR
# unfold= RooUnfoldIds (response, hMeas, 3); # OR
# unfold= RooUnfoldInvert (response, hMeas); # OR
hReco= unfold.Hreco();
unfold.PrintTable (cout, hTrue);
c1 = TCanvas()
hReco.Draw();
hMeas.SetLineColor(2)
hMeas.Draw("same");
hTrue.SetLineColor(8);
hTrue.Draw("same");
# c1.SetLogy()
legend = TLegend(0.8,0.8,0.9,0.9);
legend.AddEntry(hReco,"Unfolded","l");
legend.AddEntry(hMeas,"Measured","l");
legend.AddEntry(hTrue,"True","l");
legend.Draw();