-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfit_h3l_h4l.py
301 lines (241 loc) · 13.5 KB
/
fit_h3l_h4l.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
from hipe4ml.tree_handler import TreeHandler
import yaml
import argparse
import uproot
import numpy as np
import os
import ROOT
ROOT.gROOT.SetBatch(True)
ROOT.RooMsgService.instance().setSilentMode(True)
ROOT.RooMsgService.instance().setGlobalKillBelow(ROOT.RooFit.ERROR)
ROOT.gStyle.SetOptStat(0)
ROOT.gStyle.SetOptFit(0)
from signal_extraction import SignalExtraction
kOrangeC = ROOT.TColor.GetColor('#ff7f00')
import sys
sys.path.append('utils')
import utils as utils
parser = argparse.ArgumentParser(description='Configure the parameters of the script.')
parser.add_argument('--config-file', dest='config_file', help="path to the YAML file with configuration.", default='')
args = parser.parse_args()
if args.config_file == "":
print('** No config file provided. Exiting. **')
exit()
config_file = open(args.config_file, 'r')
config = yaml.full_load(config_file)
input_file_name_data = config['input_files_data']
input_file_name_mc_h3l = config['input_files_mc_h3l']
input_file_name_mc_h4l = config['input_files_mc_h4l']
output_dir = config['output_dir']
output_file_name = config['output_file']
colliding_system = config['colliding_system']
selections = config['selection']
is_matter = config['is_matter']
calibrate_he_momentum = config['calibrate_he_momentum']
selections_string = utils.convert_sel_to_string(selections)
if is_matter == 'matter':
selections_string += ' and fIsMatter == True'
elif is_matter == 'antimatter':
selections_string += ' and fIsMatter == False'
tree_names = ['O2datahypcands','O2hypcands', 'O2hypcandsflow']
tree_keys = uproot.open(input_file_name_data[0]).keys()
for tree in tree_names:
for key in tree_keys:
if tree in key:
tree_name = tree
break
data_hdl = TreeHandler(input_file_name_data, tree_name, folder_name='DF*')
mc_hdl_h3l_full = TreeHandler(input_file_name_mc_h3l, 'O2mchypcands', folder_name='DF*')
mc_hdl_h4l_full = TreeHandler(input_file_name_mc_h4l, 'O2mchypcands', folder_name='DF*')
# Add columns to the handlers
utils.correct_and_convert_df(data_hdl, calibrate_he3_pt=calibrate_he_momentum, isMC=False)
utils.correct_and_convert_df(mc_hdl_h3l_full, calibrate_he3_pt=calibrate_he_momentum, isMC=True)
utils.correct_and_convert_df(mc_hdl_h4l_full, calibrate_he3_pt=calibrate_he_momentum, isMC=True)
## reweight the pt spectrum of the MCs
if colliding_system == 'pp':
spectra_file = ROOT.TFile.Open('utils/heliumSpectraMB.root')
he3_spectrum = ROOT.TF1('mtexpo', '[2]*x*exp(-TMath::Sqrt([0]*[0]+x*x)/[1])', 0.1, 6)
he3_spectrum.FixParameter(0, 2.99131)
he3_spectrum.FixParameter(1, 0.5199)
he3_spectrum.FixParameter(2, 1.0)
he4_spectrum = he3_spectrum.Clone('he4_spectrum')
he4_spectrum.FixParameter(0, 3.72738)
# print('he3_spectrum parameters: ', he3_spectrum.GetParameter(0), he3_spectrum.GetParameter(1), he3_spectrum.GetParameter(2), he3_spectrum.GetParameter(3))
else:
spectra_file = ROOT.TFile.Open('utils/bw.root')
he3_spectrum = spectra_file.Get('helium3')
he4_spectrum = spectra_file.Get('helium4')
spectra_file.Close()
utils.reweight_pt_spectrum(mc_hdl_h3l_full, 'fAbsGenPt', he3_spectrum)
mc_hdl_h3l_full.apply_preselections('rej==True')
mc_hdl_h3l = mc_hdl_h3l_full.apply_preselections('fIsReco==True', inplace=False)
th1_pt_h3l = ROOT.TH1D('pt_h3l_mc', 'pt_h3l_mc', 100, 0, 10)
th2_n_sigma_he3_pt_mc = ROOT.TH2D('n_sigma_he3_pt_mc', 'n_sigma_he3_pt_mc', 100, 0, 10, 100, -5, 5)
utils.reweight_pt_spectrum(mc_hdl_h4l_full, 'fAbsGenPt', he4_spectrum)
mc_hdl_h4l_full.apply_preselections('rej==True')
mc_hdl_h4l = mc_hdl_h4l_full.apply_preselections('fIsReco==True', inplace=False)
th1_pt_h4l = ROOT.TH1D('pt_h4l_mc', 'pt_h4l_mc', 100, 0, 10)
th2_n_sigma_he4_pt_mc = ROOT.TH2D('n_sigma_he4_pt_mc', 'n_sigma_he4_pt_mc', 100, 0, 10, 100, -5, 5)
## apply the selections
data_hdl.apply_preselections(selections_string)
mc_hdl_h3l.apply_preselections(selections_string)
mc_hdl_h4l.apply_preselections(selections_string)
utils.fill_th1_hist(th1_pt_h3l, mc_hdl_h3l, 'fAbsGenPt')
utils.fill_th1_hist(th1_pt_h4l, mc_hdl_h4l, 'fAbsGenPt')
utils.fill_th2_hist(th2_n_sigma_he3_pt_mc, mc_hdl_h3l, 'fAbsGenPt', 'fNSigmaHe3')
utils.fill_th2_hist(th2_n_sigma_he4_pt_mc, mc_hdl_h4l, 'fAbsGenPt', 'fNSigmaHe4')
data_hdl.apply_preselections('fMassH4L > 3.89 and fMassH4L < 3.97 and fMassH3L > 2.96 and fMassH3L < 3.04')
mc_hdl_h3l.apply_preselections('fMassH4L > 3.89 and fMassH4L < 3.97 and fMassH3L > 2.96 and fMassH3L < 3.04')
mc_hdl_h4l.apply_preselections('fMassH4L > 3.89 and fMassH4L < 3.97 and fMassH3L > 2.96 and fMassH3L < 3.04')
## get the invariant mass template from MC, use a rookeyspdf
inv_mass_string_H4L = '#it{M}_{^{4}He+#pi^{-}}' if is_matter else '#it{M}_{^{4}#bar{He}+#pi^{+}}'
inv_mass_string_H3L = '#it{M}_{^{3}He+#pi^{-}}' if is_matter else '#it{M}_{^{3}#bar{He}+#pi^{+}}'
mass3HL = ROOT.RooRealVar('mass3HL', inv_mass_string_H3L, data_hdl['fMassH3L'].min(), data_hdl['fMassH3L'].max(), 'GeV/c^{2}')
mass4HL = ROOT.RooRealVar('mass4HL', inv_mass_string_H4L, data_hdl['fMassH4L'].min(), data_hdl['fMassH4L'].max(), 'GeV/c^{2}')
## first extract h3l and h4l templates from MC with a double sided crystal ball
mass_roo_mc_h3l = utils.ndarray2roo(np.array(mc_hdl_h3l['fMassH3L'].values, dtype=np.float64), mass3HL, 'histo_mc_h3l')
mass_roo_mc_h4l = utils.ndarray2roo(np.array(mc_hdl_h4l['fMassH4L'].values, dtype=np.float64), mass4HL, 'histo_mc_h4l')
mu3HL = ROOT.RooRealVar('mu_h3l', 'hypernucl mass', data_hdl['fMassH3L'].min(), data_hdl['fMassH3L'].max(), 'GeV/c^{2}')
sigma_h3l = ROOT.RooRealVar('sigma_h3l', 'hypernucl width', 0.001, 0.0024, 'GeV/c^{2}')
a1_h3l = ROOT.RooRealVar('a1_h3l', 'a1_h3l', 0., 5.)
a2_h3l = ROOT.RooRealVar('a2_h3l', 'a2_h3l', 0., 5.)
n1_h3l = ROOT.RooRealVar('n1_h3l', 'n1_h3l', 0., 5.)
n2_h3l = ROOT.RooRealVar('n2_h3l', 'n2_h3l', 0., 5.)
pars_h3l = [mu3HL, sigma_h3l, a1_h3l, a2_h3l, n1_h3l, n2_h3l]
signal_h3l = ROOT.RooCrystalBall('cb_h3l', 'cb_h3l_cl', mass3HL, mu3HL, sigma_h3l, a1_h3l, n1_h3l, a2_h3l, n2_h3l)
signal_h3l.fitTo(mass_roo_mc_h3l, ROOT.RooFit.Extended(True))
frame_h3l = mass3HL.frame()
frame_h3l.SetName('frame_h3l_mc')
mass_roo_mc_h3l.plotOn(frame_h3l)
signal_h3l.plotOn(frame_h3l)
mu4HL = ROOT.RooRealVar('mu_h4l', 'hypernucl mass', 3.91, 3.95, 'GeV/c^{2}')
sigma_h4l = ROOT.RooRealVar('sigma_h4l', 'hypernucl width', 0.001, 0.004, 'GeV/c^{2}')
a1_h4l = ROOT.RooRealVar('a1_h4l', 'a1_h4l', 0., 5.)
a2_h4l = ROOT.RooRealVar('a2_h4l', 'a2_h4l', 0., 5.)
n1_h4l = ROOT.RooRealVar('n1_h4l', 'n1_h4l', 0., 5.)
n2_h4l = ROOT.RooRealVar('n2_h4l', 'n2_h4l', 0., 5.)
pars_h4l = [mu4HL, sigma_h4l, a1_h4l, a2_h4l, n1_h4l, n2_h4l]
signal_h4l = ROOT.RooCrystalBall('cb_h4l', 'cb_h4l_cl', mass4HL, mu4HL, sigma_h4l, a1_h4l, n1_h4l, a2_h4l, n2_h4l)
signal_h4l.fitTo(mass_roo_mc_h4l, ROOT.RooFit.Extended(True))
frame_h4l = mass4HL.frame()
frame_h4l.SetName('frame_h4l_mc')
mass_roo_mc_h4l.plotOn(frame_h4l)
signal_h4l.plotOn(frame_h4l)
## fix all the params except for the mu
for par in pars_h3l:
if par.GetName() == 'mu_h3l':
continue
if par.GetName() == 'sigma_h3l':
print('Range H3L: ', par.getVal(), par.getVal()*1.5)
par.setRange(par.getVal(), par.getVal()*1.5)
continue
par.setConstant(True)
for par in pars_h4l:
if par.GetName() == 'mu_h4l':
continue
if par.GetName() == 'sigma_h4l':
print('Range H4L: ', par.getVal(), par.getVal()*1.5)
par.setRange(par.getVal(), par.getVal()*1.5)
continue
par.setConstant(True)
### now get the mc template of h4l for real h3l candidates and vice versa
mass3HL.setBins(60)
mass4HL.setBins(60)
mass_roo_mc_h3l_wrong_mass = utils.ndarray2roo(np.array(mc_hdl_h4l['fMassH3L'].values, dtype=np.float64), mass3HL, 'histo_mc_h3l_wrong_mass')
mass_roo_mc_h4l_wrong_mass = utils.ndarray2roo(np.array(mc_hdl_h3l['fMassH4L'].values, dtype=np.float64), mass4HL, 'histo_mc_h4l_wrong_mass')
roo_hist_h3l_wrong_mass = ROOT.RooDataHist('mc_datahist_h3l_wrong_mass', 'mc_datahist_h3l_wrong_mass', ROOT.RooArgList(mass3HL), mass_roo_mc_h3l_wrong_mass)
roo_hist_h4l_wrong_mass = ROOT.RooDataHist('mc_datahist_h4l_wrong_mass', 'mc_datahist_h4l_wrong_mass', ROOT.RooArgList(mass4HL), mass_roo_mc_h4l_wrong_mass)
pdf_h3l_wrong_mass = ROOT.RooHistPdf('mc_pdf_h3l_wrong_mass', 'mc_pdf_h3l_wrong_mass', ROOT.RooArgSet(mass3HL), roo_hist_h3l_wrong_mass)
pdf_h4l_wrong_mass = ROOT.RooHistPdf('mc_pdf_h4l_wrong_mass', 'mc_pdf_h4l_wrong_mass', ROOT.RooArgSet(mass4HL), roo_hist_h4l_wrong_mass)
### plot the histograms
frame_h3l_wrong_mass = mass3HL.frame()
frame_h3l_wrong_mass.SetName('frame_h3l_wrong_mass')
mass_roo_mc_h3l_wrong_mass.plotOn(frame_h3l_wrong_mass)
frame_h4l_wrong_mass = mass4HL.frame()
frame_h4l_wrong_mass.SetName('frame_h4l_wrong_mass')
mass_roo_mc_h4l_wrong_mass.plotOn(frame_h4l_wrong_mass)
## building models for h3l and h4l
c0_bkg_h3l = ROOT.RooRealVar('c0_bkg_h3l', 'c0_bkg_h3l', -1, 1.)
c1_bkg_h3l = ROOT.RooRealVar('c1_bkg_h3l', 'c1_bkg_h3l', -1, 1.)
bkg_h3l = ROOT.RooChebychev('bkg_h3l', 'bkg_h3l', mass3HL, ROOT.RooArgList(c0_bkg_h3l, c1_bkg_h3l))
c0_bkg_h4l = ROOT.RooRealVar('c0_bkg_h4l', 'c0_bkg_h4l', -1, 1.)
c1_bkg_h4l = ROOT.RooRealVar('c1_bkg_h4l', 'c1_bkg_h4l', -1, 1.)
bkg_h4l = ROOT.RooChebychev('bkg_h4l', 'bkg_h4l', mass4HL, ROOT.RooArgList(c0_bkg_h4l, c1_bkg_h4l))
nsig_h3l = ROOT.RooRealVar('nsig_h3l', 'signal events', 100, 0, 1e5)
n_bkg = ROOT.RooRealVar('nbkg', 'background events', 100, 0, 1e5)
n_sig_h4l = ROOT.RooRealVar('nsig_h4l', 'signal events', 100, 0, 1e5)
model_h3l = ROOT.RooAddPdf('model_h3l', 'model_h3l', ROOT.RooArgList(signal_h3l, pdf_h3l_wrong_mass, bkg_h3l), ROOT.RooArgList(nsig_h3l, n_sig_h4l, n_bkg))
model_h4l = ROOT.RooAddPdf('model_h4l', 'model_h4l', ROOT.RooArgList(signal_h4l, pdf_h4l_wrong_mass, bkg_h4l), ROOT.RooArgList(n_sig_h4l, nsig_h3l, n_bkg))
## simultaneous fit
categories = ROOT.RooCategory('categories', 'categories')
categories.defineType('h3l')
categories.defineType('h4l')
mass_roo_data_h3l = utils.ndarray2roo(np.array(data_hdl['fMassH3L'].values, dtype=np.float64), mass3HL, 'histo_data_h3l')
mass_roo_data_h4l = utils.ndarray2roo(np.array(data_hdl['fMassH4L'].values, dtype=np.float64), mass4HL, 'histo_data_h4l')
data = ROOT.RooDataSet('data', 'data', ROOT.RooArgSet(mass3HL, mass4HL, categories), ROOT.RooFit.Index(categories), ROOT.RooFit.Import('h3l', mass_roo_data_h3l), ROOT.RooFit.Import('h4l', mass_roo_data_h4l))
roosim = ROOT.RooSimultaneous('roosim', 'roosim', categories)
roosim.addPdf(model_h3l, 'h3l')
roosim.addPdf(model_h4l, 'h4l')
fit_results = roosim.fitTo(data, ROOT.RooFit.Extended(True), ROOT.RooFit.Save(True))
## print the values of all the parameters
br_h3l = 0.25
br_h4l = 0.55
eff_3hl_mc = len(mc_hdl_h3l) / len(mc_hdl_h3l_full)
eff_4hl_mc = len(mc_hdl_h4l) / len(mc_hdl_h4l_full)
print ('----------------- Signal extraction -----------------')
print('efficiency h3l: ', len(mc_hdl_h3l) / len(mc_hdl_h3l_full))
print('efficiency h4l: ', len(mc_hdl_h4l) / len(mc_hdl_h4l_full))
print('nsig_h3l: ', nsig_h3l.getVal(), nsig_h3l.getError())
print('nsig_h4l: ', n_sig_h4l.getVal(), n_sig_h4l.getError())
print('h3l / h4l: ', nsig_h3l.getVal() / n_sig_h4l.getVal() / (eff_3hl_mc / eff_4hl_mc) / (br_h3l / br_h4l))
print ('----------------- Fit parameters -----------------')
print('nbkg: ', n_bkg.getVal(), n_bkg.getError())
print('c0_bkg_h3l: ', c0_bkg_h3l.getVal(), c0_bkg_h3l.getError())
print('c1_bkg_h3l: ', c1_bkg_h3l.getVal(), c1_bkg_h3l.getError())
print('c0_bkg_h4l: ', c0_bkg_h4l.getVal(), c0_bkg_h4l.getError())
print('c1_bkg_h4l: ', c1_bkg_h4l.getVal(), c1_bkg_h4l.getError())
print('mu_h3l: ', mu3HL.getVal(), mu3HL.getError())
print('sigma_h3l: ', sigma_h3l.getVal(), sigma_h3l.getError())
print('a1_h3l: ', a1_h3l.getVal(), a1_h3l.getError())
print('a2_h3l: ', a2_h3l.getVal(), a2_h3l.getError())
print('n1_h3l: ', n1_h3l.getVal(), n1_h3l.getError())
print('n2_h3l: ', n2_h3l.getVal(), n2_h3l.getError())
print('mu_h4l: ', mu4HL.getVal(), mu4HL.getError())
print('sigma_h4l: ', sigma_h4l.getVal(), sigma_h4l.getError())
print('a1_h4l: ', a1_h4l.getVal(), a1_h4l.getError())
print('a2_h4l: ', a2_h4l.getVal(), a2_h4l.getError())
print('n1_h4l: ', n1_h4l.getVal(), n1_h4l.getError())
print('n2_h4l: ', n2_h4l.getVal(), n2_h4l.getError())
mass3HL.setBins(30)
mass4HL.setBins(30)
## plot the results
frame_data_h3l = mass3HL.frame()
frame_data_h3l.SetName('frame_data_h3l')
mass_roo_data_h3l.plotOn(frame_data_h3l)
model_h3l.plotOn(frame_data_h3l)
model_h3l.plotOn(frame_data_h3l, ROOT.RooFit.Components('cb_h3l'), ROOT.RooFit.LineColor(kOrangeC), ROOT.RooFit.LineStyle(2))
model_h3l.plotOn(frame_data_h3l, ROOT.RooFit.Components('mc_pdf_h3l_wrong_mass'), ROOT.RooFit.LineColor(ROOT.kGreen), ROOT.RooFit.LineStyle(2))
model_h3l.plotOn(frame_data_h3l, ROOT.RooFit.Components('bkg_h3l'), ROOT.RooFit.LineColor(ROOT.kRed), ROOT.RooFit.LineStyle(2))
frame_data_h4l = mass4HL.frame()
frame_data_h4l.SetName('frame_data_h4l')
mass_roo_data_h4l.plotOn(frame_data_h4l)
model_h4l.plotOn(frame_data_h4l)
model_h4l.plotOn(frame_data_h4l, ROOT.RooFit.Components('cb_h4l'), ROOT.RooFit.LineColor(kOrangeC), ROOT.RooFit.LineStyle(2))
model_h4l.plotOn(frame_data_h4l, ROOT.RooFit.Components('mc_pdf_h4l_wrong_mass'), ROOT.RooFit.LineColor(ROOT.kGreen), ROOT.RooFit.LineStyle(2))
model_h4l.plotOn(frame_data_h4l, ROOT.RooFit.Components('bkg_h4l'), ROOT.RooFit.LineColor(ROOT.kRed), ROOT.RooFit.LineStyle(2))
tfile = ROOT.TFile.Open(f'{output_dir}/{output_file_name}', 'recreate')
tfile.cd()
he3_spectrum.Write()
he4_spectrum.Write()
th1_pt_h3l.Write()
th1_pt_h4l.Write()
th2_n_sigma_he3_pt_mc.Write()
th2_n_sigma_he4_pt_mc.Write()
frame_h3l.Write()
frame_h4l.Write()
frame_h3l_wrong_mass.Write()
frame_h4l_wrong_mass.Write()
frame_data_h3l.Write()
frame_data_h4l.Write()
tfile.Close()
exit()