Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:22:09

0001 #!/usr/bin/env python
0002 # coding: utf-8
0003 
0004 # In[31]:
0005 
0006 
0007 from hepdata_lib import Submission, Table, Variable, Uncertainty, RootFileReader
0008 
0009 # Load ROOT file using RootFileReader
0010 reader = RootFileReader("spectrum_output.root")
0011 
0012 # Initialize submission
0013 submission = Submission()
0014 
0015 # ------------------ Table 1: Spectrum -----------------------
0016 #This objects hold page on HEPDATA - idealy 1 Table per figure
0017 table1 = Table("Exponential Spectrum")
0018 table1.description = "Dummy cross-section with statistical uncertainties and asymmetric systematic uncertainties."
0019 table1.location = "Data from Figure 1, located on page 3."
0020 # Add some keywords so it is searchable on HEPDATA
0021 table1.keywords["observables"] = ["D2SIG/DPT/DETARAP"]
0022 table1.keywords["cmenergies"] = ["200"]
0023 table1.keywords["reactions"] = ["P P --> D0 X", "P P --> DBAR0 X"]
0024 table1.keywords["phrases"] = ["Cross Section", "Single Differential Cross Section","Proton-Proton Scattering","Transverse Momentum Dependence"]
0025 # Add image, can be pdf
0026 table1.add_image("spectrum.png")
0027 
0028 # Get data from TH1D and TGraphAsymmErrors
0029 hist = reader.read_hist_1d("hist_stat")
0030 graph = reader.read_graph("graph_asym")
0031 
0032 # This is the structure of the loaded objects (dictionary of lists)
0033 #print(graph.keys())
0034 #dict_keys(['x', 'y', 'dx', 'dy'])
0035 
0036 # Define x-axis (independent)
0037 histogram_x = Variable("$p_{T}$", is_independent=True, is_binned=False, units="GeV")
0038 histogram_x.values = hist["x"]
0039 # Define y-axis values (since we have 1D plot, this is dependent - ir depends on the independent variable x)
0040 histogram_y = Variable("Cross-section", is_independent=False, is_binned=False, units="mb")
0041 histogram_y.values = hist["y"]
0042 #Define urcertainties, you have have any number you want, you are naming them: norm,lumi, etc.
0043 #stat unc
0044 stat_unc = Uncertainty("stat", is_symmetric=True)
0045 stat_unc.values = hist["dy"]
0046 #sys unc
0047 sys_unc = Uncertainty("sys", is_symmetric=False)
0048 sys_unc.values = graph["dy"]
0049 
0050 #accidentelly saved symetrical errors in Assymerrors? No problem!
0051 #sys_unc = Uncertainty("sys", is_symmetric=True)
0052 #sys_unc.values =[high for (_, high) in graph["dy"]]
0053 
0054 # Add uncertainty to data
0055 histogram_y.add_uncertainty(stat_unc)
0056 histogram_y.add_uncertainty(sys_unc)
0057 
0058 # Add variables to the table
0059 table1.add_variable(histogram_x)
0060 table1.add_variable(histogram_y)
0061 
0062 #lets add another histogram in the same table, without any uncertainties
0063 hist_halved = reader.read_hist_1d("hist_halved")
0064 histogram_y_halved = Variable("Cross-section halved", is_independent=False, is_binned=False, units="mb")
0065 histogram_y_halved.values = hist_halved["y"]
0066 table1.add_variable(histogram_y_halved)
0067 
0068 # ------------------ Table 2: Correlation Matrix ------------------
0069 #lib also supports 2D hsitograms!
0070 table2 = Table("Bin-to-bin Correlation Matrix")
0071 table2.description = "Random symmetric bin-to-bin correlation matrix used for uncertainty modeling."
0072 table2.add_image("corr_matrix.png")
0073 corr_matrix = reader.read_hist_2d("corr_matrix")
0074 
0075 # Create variable objects, since we have 2D histogram, x and y are independent variables
0076 x = Variable("First Axis", is_independent=True, is_binned=True)
0077 x.values = corr_matrix["x_edges"]
0078 y = Variable("Second Axis", is_independent=True, is_binned=True)
0079 y.values = corr_matrix["y_edges"]
0080 correlation = Variable("Correlation coefficient", is_independent=False, is_binned=False)
0081 correlation.values = corr_matrix["z"]
0082 #Uncertainties can be added similarly to the 1D case
0083 
0084 for var in [x,y,correlation]:
0085     table2.add_variable(var)
0086 
0087 # Add tables to submission
0088 submission.add_table(table1)
0089 submission.add_table(table2)
0090 
0091 # Output submission to directory
0092 submission.create_files("submission_hepdata",remove_old=True)
0093 
0094 
0095 
0096 # In[ ]:
0097 
0098 
0099 
0100