import os import math import re #mapping of STXS bins from yoda to top #input is the lines between: # xlow xhigh val errminus errplus #xxxx #xxxx # END HISTO1D start_marker = "# xlow" end_marker = "# END HISTO1D" def mapping_signDPhijj_STXS(filename, histo_values): #map from ##HISTO: sign_DPhijjQQ2HQQ_GE2J_MJJ_1000_1500_PTH_0_200_PTHJJ_0_25 #to ##STXS-MJJ-350-700-PTH-0-200-PTHJJ-0-25-DPHIJJ-0-PIov2 and STXS-MJJ-350-700-PTH-0-200-PTHJJ-0-25-DPHIJJ-PIov2-PI mapped_histo_name = filename.replace(".dat","").replace("sign_DPhijjQQ2HQQ_GE2J_","").replace("_","-") print(mapped_histo_name) # Define the pattern to match "GTxxx" pattern = r'GT(\d+)' # Find all matches of the pattern in the input string matches = re.findall(pattern, mapped_histo_name) # Iterate over each match and replace with "xxx-INFTY" for match in matches: replacement = match + "-INFTY" mapped_histo_name = mapped_histo_name.replace("GT" + match, replacement) print(mapped_histo_name) #merge first and last dphijj bin: val_0_PIov2 = "{:.6e}".format(histo_values[0][2] + histo_values[3][2]) err_0_PIov2 = "{:.6e}".format(math.sqrt(histo_values[0][3]**2 + histo_values[3][3]**2)) text_0_PIov2 = " 0.00000000D+00 0.10000000D+01 %s %s" %(val_0_PIov2, err_0_PIov2) #merge first and last dphijj bin: val_PIov2_PI = "{:.6e}".format(histo_values[1][2] + histo_values[2][2]) err_PIov2_PI = "{:.6e}".format(math.sqrt(histo_values[1][3]**2 + histo_values[2][3]**2)) text_PIov2_PI = " 0.00000000D+00 0.10000000D+01 %s %s" %(val_PIov2_PI, err_PIov2_PI) mapped_histo={} mapped_histo[mapped_histo_name+"-DPHIJJ-0-PIov"] = text_0_PIov2 mapped_histo[mapped_histo_name+"-DPHIJJ-PIov2-PI"] = text_PIov2_PI #return {"MJJ-350-700-PTH-0-200-PTHJJ-0-25-DPHIJJ-0-PIov2": "xx", "MJJ-350-700-PTH-0-200-PTHJJ-0-25-DPHIJJ-PIov2-PI": "xx"} return mapped_histo def main(inputFolder, file_prefix): #open the template with open('top-output-template.txt', 'r') as file: # Read all lines into a list template_lines = file.readlines() #iterate through all files with the file prefix, e.g. "sign_DPhijj" for filename in os.listdir(inputFolder): if not (filename.endswith('.dat') and filename.startswith(file_prefix)): continue #read the yoda/dat file and keep only the histo value with open(os.path.join(inputFolder,filename), 'r') as f: # Read all lines into a list yoda_lines = f.readlines() #print(yoda_lines) keeplines = False yoda_histo_values = [] for l in yoda_lines: if l.startswith(end_marker): break if keeplines: yoda_histo_values.append([float(value) for value in l.split('\t')]) if l.startswith(start_marker): keeplines = True #for this .dat do the mapping mapped_histo = mapping_signDPhijj_STXS(filename, yoda_histo_values) copy_template_lines = template_lines #fill the proper line in the template for i,l in enumerate(template_lines): if l == "\n": continue for mapped_histo_name in mapped_histo.keys(): if mapped_histo_name in l: copy_template_lines[i+1] = mapped_histo[mapped_histo_name] #write out the .top format with open('output.txt', 'w') as file: # Write each line to the file file.writelines(copy_template_lines) main("/Users/chenjiayi/cernbox/LHCWG-VBF/vbf-higgs-wg/hw7/NLO-noshower/rivet-plots/MY_ANALYSIS/","sign_DPhijjQQ2HQQ")