def finalize(self, nfiles_expected): if not all((self.name, self.label, self.caption, type(self.caption) == str, len(self.files) == nfiles_expected, )): return False self.fig = int(self.name.replace('fig', '')) self.uno = len(self.files) == 1 for fn in self.files: if not os.path.isfile(fn): print 'no file', fn return False rootfn = fn.replace('.pdf', '.root') if not os.path.isfile(rootfn): rootfn = None subfig = fn[fn.index('.pdf')-1] if subfig not in string.ascii_lowercase: assert self.uno subfig = 'a' self.subfigs.append(subfig) self.pdfs[subfig] = fn self.rootfns[subfig] = rootfn r = self.roots[subfig] = hepdata.RootFileReader(rootfn) if rootfn else rootfn if rootfn: c = self.canvases[subfig] = r.tfile.Get('c0') if not c: self.canvases[subfig] = r.tfile.Get('c') self.objs[subfig] = {} self.reps[subfig] = None table_name = 'Figure %i' % self.fig if not self.uno: table_name += subfig t = self.tables[subfig] = hepdata.Table(table_name) t.description = self.caption t.location = table_name t.add_image(fn) return True
def convert_histograms_to_hepdata(input_path: Path, output_path: Path) -> bool: """ Convert 2D correlations into HEPdata YAML files. Args: input_path: Path to the files containing the correlations to be converted. output_path: Path where the HEPdata files should be written. Returns: True if successful. """ # Move the import inside the function so that we don't have to explicitly depend on it. import hepdata_lib as hepdata # NOTE: hist_names are the same as file names! ep_orientations, assoc_pt_bin_edges, hist_names = define_2d_correlations_hists() logger.debug("Writing the correlations to the HEPdata format.") submission = hepdata.Submission() for ep, pt_1, pt_2, hist_name in hist_names: logger.debug(f"Converting ep: {ep}, pt: {pt_1}-{pt_2}, hist_name: {hist_name}") filename = input_path / f"{hist_name}.root" # There's only one histogram, so we just take it. h = histogram.get_histograms_in_file(str(filename))[hist_name] correlation = hepdata.root_utils.get_hist_2d_points(h) phi = hepdata.Variable(r"$\varphi$", is_binned = False) phi.values = correlation["x"] eta = hepdata.Variable(r"$\eta$", is_binned = False) eta.values = correlation["y"] corr = hepdata.Variable(r"(1/N_{\textrm{trig}})\textrm{d}^{2}N/\textrm{d}\Delta\varphi\textrm{d}\Delta\eta", is_binned = False, is_independent = False) corr.values = correlation["z"] table = hepdata.Table(f"raw_correlation_ep_{ep}_pt_assoc_{pt_1}_{pt_2}_hepdata") table.add_variable(phi) table.add_variable(eta) table.add_variable(corr) # Add basic labeling. table.description = fr"Correlation function for 20-40 $\textrm{{GeV}}/c$ {_ep_orientation_full_name_map[ep]} jets with ${pt_1} < p_{{\textrm{{T}}}} {pt_2}$ $\textrm{{GeV}}/c$ hadrons." # This location depends on the layout of the public note. table.location = fr"Figure {_figure_location_map[ep]}" submission.add_table(table) hepdata_output = output_path / "hepdata" # Create the YAML files submission.create_files(str(hepdata_output)) return True
import hepdata_lib hepdata_submission = hepdata_lib.Submission() table = hepdata_lib.Table('Table 1') x = hepdata_lib.Variable('x', is_independent=True, is_binned=False, units='GeV') x.values = [ -0.05, 0.025, 0.07500000000000001, 0.125, 0.175, 0.225, 0.275, 0.32499999999999996, 0.375, 0.42500000000000004, 0.475 ] table.add_variable(x) y = hepdata_lib.Variable('y', is_independent=False, is_binned=False, units='GeV') y.values = [ 0.051528779333327564, 1.6546832058356766, 6.320493448437572, 3.6741550071911946, 2.2145138336614534, 1.520367789083682, 1.2488500519692582, 0.866646224820301, 0.8382429211482135, 0.7893714421512917, 0.7696185170346984 ] table.add_variable(y) yerr1 = hepdata_lib.Uncertainty('err1', is_symmetric=True) yerr1.values = [ 0.021099959427066828, 0.15962100141035176, 0.3694098739929236, 0.2663855349397847, 0.16382789804566147, 0.1589536500131101,
def add_hepdata_table(self, i, jetR, obs_label, obs_setting, grooming_setting, min_pt, max_pt): index = self.set_hepdata_table_index(i, jetR, min_pt) table = hepdata_lib.Table(f'Table {index}') table.keywords["reactions"] = ['P P --> jet+X'] table.keywords["cmenergies"] = ['5020'] # Set observable-specific info in table x_label, y_label = self.set_hepdata_table_descriptors(table, jetR, obs_label, obs_setting, grooming_setting, min_pt, max_pt) # Create readers to read histograms final_result_root_filename = os.path.join(getattr(self, 'output_dir_final_results'), 'fFinalResults.root') hepdata_reader = hepdata_lib.RootFileReader(final_result_root_filename) systematics_root_filename = os.path.join(self.output_dir_systematics, 'fSystematics.root') hepdata_reader_systematics = hepdata_lib.RootFileReader(systematics_root_filename) # Define variables h_name = 'hmain_{}_R{}_{}_{}-{}'.format(self.observable, jetR, obs_label, min_pt, max_pt) if self.observable == 'ang': h_name += '_trunc' h = hepdata_reader.read_hist_1d(h_name) n_significant_digits = 3 # Note: use 2 significant digits for uncertainties x = hepdata_lib.Variable(x_label, is_independent=True, is_binned=True, units='') x.digits = n_significant_digits x.values = h['x_edges'] y = hepdata_lib.Variable(y_label, is_independent=False, is_binned=False, units='') y.digits = n_significant_digits y.values = h['y'] if self.is_pp: y.add_qualifier('RE', 'P P --> jet+X') else: y.add_qualifier('RE', 'Pb Pb --> jet+X') y.add_qualifier('SQRT(S)', 5.02, 'TeV') y.add_qualifier('ETARAP', '|0.9-R|') y.add_qualifier('jet radius', jetR) y.add_qualifier('jet method', 'Anti-$k_{T}$') # Define uncertainties stat = hepdata_lib.Uncertainty('stat', is_symmetric=True) stat.values = [float('{:.2g}'.format(dy)) for dy in h['dy']] # Add tables to submission table.add_variable(x) table.add_variable(y) y.add_uncertainty(stat) # Add unfolding systematic name = 'hSystematic_Unfolding_R{}_{}_{}-{}'.format(self.utils.remove_periods(jetR), obs_label, int(min_pt), int(max_pt)) h_sys_unfolding = hepdata_reader_systematics.read_hist_1d(getattr(self, name).GetName()) sys_unfolding = hepdata_lib.Uncertainty('sys,unfolding', is_symmetric=True) sys_unfolding.values = ['{:.2g}%'.format(y) for y in h_sys_unfolding['y']] y.add_uncertainty(sys_unfolding) # Add systematic uncertainty breakdown for systematic in self.systematics_list: if systematic in ['main', 'prior1', 'truncation', 'binning']: continue h_sys = self.retrieve_systematic(systematic, jetR, obs_label, None, min_pt, max_pt) if not h_sys: continue if 'generator' in systematic: sys_label = 'generator' else: sys_label = systematic h_sys = hepdata_reader_systematics.read_hist_1d(h_sys.GetName()) sys = hepdata_lib.Uncertainty('sys,{}'.format(sys_label), is_symmetric=True) sys.values = ['{:.2g}%'.format(y) for y in h_sys['y']] y.add_uncertainty(sys) # Add table to the submission self.hepdata_submission.add_table(table)
def add_hepdata_table(self, is_pp=False, is_ratio=False): result_index = self.set_hepdata_table_index() if is_ratio: h = self.h_ratio h_sys = self.h_ratio_sys index = 3*result_index elif is_pp: h = self.h_main_pp h_sys = self.h_sys_pp index = 3*result_index-2 else: h = self.h_main_AA h_sys = self.h_sys_AA index = 3*result_index-1 table = hepdata_lib.Table(f'Table {index}') if is_ratio: table.keywords["reactions"] = ['P P --> jet+X, Pb Pb --> jet+X'] elif is_pp: table.keywords["reactions"] = ['P P --> jet+X'] else: table.keywords["reactions"] = ['Pb Pb --> jet+X'] table.keywords["cmenergies"] = ['5020'] # Set observable-specific info in table x_label, y_label = self.set_hepdata_table_descriptors(table, result_index, is_pp, is_ratio) # Define variables h = hepdata_lib.root_utils.get_hist_1d_points(h) n_significant_digits = 3 # Note: use 2 significant digits for uncertainties x = hepdata_lib.Variable(x_label, is_independent=True, is_binned=True, units='') x.digits = n_significant_digits x.values = h['x_edges'] y = hepdata_lib.Variable(y_label, is_independent=False, is_binned=False, units='') y.digits = n_significant_digits y.values = h['y'] if is_ratio: y.add_qualifier('RE', 'P P --> jet+X, Pb Pb --> jet+X') y.add_qualifier('CENTRALITY', str(self.centrality)) elif is_pp: y.add_qualifier('RE', 'P P --> jet+X') else: y.add_qualifier('RE', 'Pb Pb --> jet+X') y.add_qualifier('CENTRALITY', str(self.centrality)) y.add_qualifier('SQRT(S)', 5.02, 'TeV') y.add_qualifier('ETARAP', '|0.9-R|') y.add_qualifier('jet radius', self.jetR) y.add_qualifier('jet method', 'Anti-$k_{T}$') # Define uncertainties stat = hepdata_lib.Uncertainty('stat', is_symmetric=True) stat.values = [float('{:.2g}'.format(dy)) for dy in h['dy']] # Add tables to submission table.add_variable(x) table.add_variable(y) y.add_uncertainty(stat) # Add unfolding systematic if self.centrality == [0,10]: h_sys = hepdata_lib.root_utils.get_hist_1d_points(h_sys) sys = hepdata_lib.Uncertainty('sys', is_symmetric=True) sys.values = [float('{:.2g}'.format(dy)) for dy in h_sys['dy']] elif self.centrality == [30,50]: h_sys = hepdata_lib.root_utils.get_graph_points(h_sys) sys = hepdata_lib.Uncertainty('sys', is_symmetric=False) sys.values = [(float('{:.2g}'.format(dy[0])), float('{:.2g}'.format(dy[1]))) for dy in h_sys['dy']] y.add_uncertainty(sys) # Add table to the submission self.hepdata_submission.add_table(table)