1+ import yaml
2+ import numpy as np
3+ from nnpdf_data .filter_utils .utils import prettify_float
4+
5+ yaml .add_representer (float , prettify_float )
6+
7+ def read_metadata ():
8+ '''
9+ this function reads metadata
10+ and returns the list of tables used
11+ in the implementation
12+ '''
13+ temp_dict = {}
14+ with open ('metadata.yaml' , 'r' ) as file :
15+ temp_dict = yaml .safe_load (file )
16+ observable = temp_dict ['implemented_observables' ][0 ]
17+ return observable ['tables' ]
18+
19+ def read_table (table_no : int ):
20+ '''
21+ this function takes the table number and
22+ returns the corresponding:
23+ kinematic bins
24+ central values corrected as per eq 28
25+ uncertainties corrected as per eq 29
26+ in paper https://arxiv.org/pdf/physics/0403086
27+ '''
28+ temp_dict = dict ()
29+ with open (f'rawdata/Table{ table_no } .yaml' , 'r' ) as file :
30+ temp_dict = yaml .safe_load (file )
31+
32+ # sort out kinematic bins:
33+ sqrts_val = float (temp_dict ['dependent_variables' ][0 ]['qualifiers' ][1 ]['value' ])
34+ ybin = temp_dict ['dependent_variables' ][0 ]['qualifiers' ][0 ]['value' ]
35+ ymin , ymax = float (ybin [:3 ]), float (ybin [4 :7 ])
36+ ymid = (ymin + ymax )/ 2
37+ bins_in_table = list ()
38+ for ptbin in temp_dict ['independent_variables' ][0 ]['values' ]:
39+ y_dict = {'y' : {'min' : ymin , 'mid' : ymid , 'max' : ymax }}
40+ sqrts_dict = {'sqrts' : {'min' : None , 'mid' : sqrts_val , 'max' : None }}
41+ pT_dict = {'pT' : {'min' : ptbin ['low' ], 'mid' : (ptbin ['low' ]+ ptbin ['high' ])/ 2 , 'max' : ptbin ['high' ]}}
42+ bins_in_table .append (y_dict | sqrts_dict | pT_dict )
43+
44+ # read the central values and the uncertainties
45+ central_values = list ()
46+ stat = list ()
47+ sys = list ()
48+ for dep_var in temp_dict ['dependent_variables' ]:
49+ if dep_var ['header' ]['name' ] == 'D2(SIG)/DPT/DABS(YRAP)' :
50+ for bin_val in dep_var ['values' ]:
51+ central_values .append (bin_val ['value' ])
52+ for err in bin_val ['errors' ]:
53+ if err ['label' ] == 'stat' :
54+ stat .append (err ['symerror' ])
55+ elif err ['label' ] == 'sys' :
56+ sys .append ((np .abs (err ['asymerror' ]['minus' ]), err ['asymerror' ]['plus' ]))
57+
58+ # process the asymmetric uncertainties
59+ sys_processed = list ()
60+ shifts_in_central = list ()
61+ for (del_minus , del_plus ) in sys :
62+ # calculating delta and Deltabar as per eqs 23, 24
63+ delta = (del_plus - del_minus )/ 2
64+ Deltabar = float ((del_plus + del_minus )/ 2 )
65+ # each delta/Deltabar is of order 0.02, so using eq 28, 29 is justified
66+ shifts_in_central .append (delta )
67+ sys_processed .append (Deltabar )
68+
69+ corrected_centrals_in_table = list (np .array (central_values )+ np .array (shifts_in_central ))
70+ uncertainties_in_table = list ()
71+ for i in range (len (stat )):
72+ unc_dict = {'stat' : stat [i ], 'sys' : sys_processed [i ]}
73+ uncertainties_in_table .append (unc_dict )
74+
75+ return bins_in_table , corrected_centrals_in_table , uncertainties_in_table
76+
77+ def main_filter () -> None :
78+ '''
79+ main filter that reads all the tables and saves the dataset in .yaml files
80+ '''
81+
82+ tables = read_metadata ()
83+ kinematics = list ()
84+ data_central = list ()
85+ uncertainties = list ()
86+ for table_no in tables :
87+ current_bins , current_central , current_unc = read_table (table_no )
88+ kinematics += current_bins
89+ data_central += current_central
90+ uncertainties += current_unc
91+
92+ with open ('kinematics_R07.yaml' , 'w' ) as file :
93+ yaml .safe_dump ({'bins' : kinematics }, file , sort_keys = False )
94+
95+ data_central_float = [float (central_value ) for central_value in data_central ]
96+ with open ('data_R07.yaml' , 'w' ) as file :
97+ yaml .safe_dump ({'data_central' : data_central_float }, file , sort_keys = False )
98+
99+ unc_definitions = {'definitions' : {'sys' : {'description' : 'combined systematic ucertainties (symmetrised), including JES correction, pT resolution, luminosity' , 'treatment' : 'MULT' , 'type' : 'CORR' }, 'stat' : {'description' : 'combined statistical uncertainties' , 'treatment' : 'MULT' , 'type' : 'CORR' }}}
100+
101+ with open ('uncertainties_R07.yaml' , 'w' ) as file :
102+ yaml .safe_dump (unc_definitions | {'bins' : uncertainties }, file , sort_keys = False )
103+
104+
105+ if __name__ == '__main__' :
106+ main_filter ()
0 commit comments