|
| 1 | +""" |
| 2 | +This piece of code generates a simplified variant of the uncertainties.yaml |
| 3 | +files in which the 99 correlated systematic uncertainties of the legacy |
| 4 | +version are replaced with two systematic uncertainties, one that is uncorrelated |
| 5 | +and that is correlated. Statistical, luminosity and )optional) Monte Carlo |
| 6 | +uncertainties are as in the legacy version. |
| 7 | +""" |
| 8 | + |
| 9 | +import yaml |
| 10 | + |
| 11 | + |
| 12 | +def get_tables(observable): |
| 13 | + """ |
| 14 | + get the Hepdata tables, given the tables and version specified in metadata |
| 15 | + """ |
| 16 | + prefix = "rawdata/Table" |
| 17 | + with open("metadata.yaml", "r") as file: |
| 18 | + metadata = yaml.safe_load(file) |
| 19 | + |
| 20 | + if observable == "PT-Y": |
| 21 | + tables = metadata["implemented_observables"][0]["tables"] |
| 22 | + elif observable == "PT-M": |
| 23 | + tables = metadata["implemented_observables"][1]["tables"] |
| 24 | + else: |
| 25 | + print("Data set not implemented") |
| 26 | + print("Available data sets are:") |
| 27 | + print("- ATLAS_Z0J_8TEV_PT-Y") |
| 28 | + print("- ATLAS_Z0J_8TEV_PT-M") |
| 29 | + exit() |
| 30 | + |
| 31 | + hepdata_tables = [] |
| 32 | + |
| 33 | + for table in tables: |
| 34 | + hepdata_tables.append(f"{prefix}{table}.yaml") |
| 35 | + |
| 36 | + return hepdata_tables |
| 37 | + |
| 38 | + |
| 39 | +def get_uncertainties(observable): |
| 40 | + """ |
| 41 | + Returns uncertainties for dumping in the -yaml file |
| 42 | + """ |
| 43 | + data_central = [] |
| 44 | + uncertainties = [] |
| 45 | + |
| 46 | + hepdata_tables = get_tables(observable) |
| 47 | + for table in hepdata_tables: |
| 48 | + with open(table, 'r') as f: |
| 49 | + input = yaml.safe_load(f) |
| 50 | + # Central values |
| 51 | + data_values = input["dependent_variables"][5]["values"] |
| 52 | + for data_value in data_values: |
| 53 | + data_central.append(data_value["value"]) |
| 54 | + |
| 55 | + # Uncertainties |
| 56 | + for data_value in data_values: |
| 57 | + errors = data_value["errors"] |
| 58 | + uncertainty = {} |
| 59 | + for error in errors: |
| 60 | + uncertainty[error["label"]] = error["symerror"] |
| 61 | + uncertainty.update(uncertainty) |
| 62 | + |
| 63 | + uncertainties.append(uncertainty) |
| 64 | + |
| 65 | + return (data_central, uncertainties) |
| 66 | + |
| 67 | + |
| 68 | +def filter_unc_ATLAS_Z0J_8TEV(MC=False): |
| 69 | + """ |
| 70 | + Dumps uncertainties on .yaml files |
| 71 | + """ |
| 72 | + lumi_unc = 2.8 # % |
| 73 | + mc_unc = 1.0 # % |
| 74 | + observables = ["PT-Y", "PT-M"] |
| 75 | + for observable in observables: |
| 76 | + if MC == False: |
| 77 | + unc_file = "uncertainties_decorr_" + observable + ".yaml" |
| 78 | + else: |
| 79 | + unc_file = "uncertainties_decorr_sys_10_" + observable + ".yaml" |
| 80 | + central_values, uncertainties = get_uncertainties(observable) |
| 81 | + |
| 82 | + for i in range(len(central_values)): |
| 83 | + for k in uncertainties[i]: |
| 84 | + uncertainties[i][k] = ( |
| 85 | + float(uncertainties[i][k].replace("%", "")) / 100.0 * central_values[i] * 1000.0 |
| 86 | + ) |
| 87 | + uncertainties[i].update({"sys_lumi_corr": lumi_unc / 100 * central_values[i] * 1000.0}) |
| 88 | + if MC == True: |
| 89 | + uncertainties[i].update( |
| 90 | + {"sys_mc_uncorr": mc_unc / 100 * central_values[i] * 1000.0} |
| 91 | + ) |
| 92 | + |
| 93 | + treatment = { |
| 94 | + "stat": "ADD", |
| 95 | + "sys,Uncorrelated": "ADD", |
| 96 | + "sys,Correlated": "MULT", |
| 97 | + "sys_lumi_corr": "MULT", |
| 98 | + } |
| 99 | + correlation = { |
| 100 | + "stat": "UNCORR", |
| 101 | + "sys,Uncorrelated": "UNCORR", |
| 102 | + "sys,Correlated": "CORR", |
| 103 | + "sys_lumi_corr": "ATLASLUMI12", |
| 104 | + } |
| 105 | + if MC == True: |
| 106 | + treatment.update({"sys_mc_uncorr": "ADD"}) |
| 107 | + correlation.update({"sys_mc_uncorr": "UNCORR"}) |
| 108 | + |
| 109 | + definitions = {} |
| 110 | + for key, value in uncertainties[0].items(): |
| 111 | + definition = { |
| 112 | + key: { |
| 113 | + "description": key + " unc. from HepData", |
| 114 | + "treatment": treatment[key], |
| 115 | + "type": correlation[key], |
| 116 | + } |
| 117 | + } |
| 118 | + definitions.update(definition) |
| 119 | + uncertainties_yaml = {"definitions": definitions, "bins": uncertainties} |
| 120 | + |
| 121 | + if MC: |
| 122 | + uncertainties_yaml["definitions"][key][ |
| 123 | + "description" |
| 124 | + ] = "extra Monte Carlo statistical uncertainty" |
| 125 | + |
| 126 | + with open(unc_file, "w") as file: |
| 127 | + yaml.dump(uncertainties_yaml, file, sort_keys=False) |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + filter_unc_ATLAS_Z0J_8TEV(MC=False) |
| 132 | + filter_unc_ATLAS_Z0J_8TEV(MC=True) |
0 commit comments