Package madgraph :: Package various :: Module combine_plots
[hide private]
[frames] | no frames]

Source Code for Module madgraph.various.combine_plots

  1  #!/usr/bin/env python 
  2  ################################################################################ 
  3  # 
  4  # Copyright (c) 2013 The MadGraph5_aMC@NLO Development team and Contributors 
  5  # 
  6  # This file is a part of the MadGraph5_aMC@NLO project, an application which  
  7  # automatically generates Feynman diagrams and matrix elements for arbitrary 
  8  # high-energy processes in the Standard Model and beyond. 
  9  # 
 10  # It is subject to the MadGraph5_aMC@NLO license which should accompany this  
 11  # distribution. 
 12  # 
 13  # For more information, visit madgraph.phys.ucl.ac.be and amcatnlo.web.cern.ch 
 14  # 
 15  ################################################################################ 
 16  from __future__ import absolute_import 
 17  from __future__ import print_function 
 18  import glob 
 19  import os 
 20  import re 
 21  import subprocess 
 22   
 23  pjoin = os.path.join 
 24   
25 -class histograms:
26 """ x and y values """
27 - def __init__(self):
28 pass
29 30
31 -class one_plot:
32 """ All information about one plot """
33 - def __init__(self):
34 self.histo = {} 35 self.top = "" 36 self.end = "" 37 self.max = 0 38 self.max_file =''
39
40 - def add_comment(self, top_comment, end_comment=""):
41 42 self.top = top_comment 43 self.end = end_comment
44 45
46 - def add_histo(self, string_values, tag):
47 """Add the histogram in the data base (string format) """ 48 if tag in list(self.histo.values()): 49 print("Warning: skyping the histogram with tag " + tag) 50 print(" since it is already present in the data base") 51 return 52 self.histo[tag] = {} 53 self.histo[tag]["values"] = string_values 54 old_max = self.max 55 56 for line in string_values.split('\n'): 57 split = line.split() 58 if len(split) in [2,3]: 59 self.max = max(self.max, float(line.split()[1])) 60 if self.max != old_max: 61 self.max_file = tag
62
63 - def get_histo(self, tag, norm):
64 """return a string with the histogram values, and the normalization """ 65 if tag not in self.histo or self.histo[tag]["values"] == '': 66 return '' 67 histo = "SET ORDER X Y " + str(norm) + " \n" 68 histo += self.histo[tag]["values"] 69 return histo
70 71
72 -class load_data:
73 """ Import all the plots from top drawer files """
74 - def __init__(self):
75 76 self.plots = {} # list of the tags associated with the plots 77 self.normalization = 1.0 78 self.order_plots = [] 79 self.cross = {}
80
81 - def import_data(self, file_name, file_nb, pretag=""):
82 """ Read the file with the name file_name, and import the data for all the plots""" 83 84 trappe = open(file_name, 'r') 85 while 1: 86 line = trappe.readline() 87 if line == "": break 88 if line.find("Cross-section") > -1: 89 pos = line.find("is:") 90 list = line[pos + 4:].split() 91 self.cross1 = float(list[0]) 92 if line.find("NEW PLOT") > -1 or line.find("SET INTENSITY") > -1: 93 self.readplot(trappe, file_nb)
94
95 - def print_plots(self, outputfile, file1, file2):
96 """Write out histograms """ 97 trappe = open(outputfile, 'w') 98 trappe.write("SET DEVICE POSTSCRIPT ORIENTATION=3 \n") 99 trappe.write("set intensity 5 \n") 100 101 102 103 for index, tag_plot in enumerate(self.order_plots): 104 norm1 = 1 105 norm2 = 1 106 107 if self.plots[tag_plot].max_file == file1: 108 color1, color2 = 'WHITE','BLUE' 109 histtype1, histtype2 = 'HIST SOLID\n', 'SET PATTERN .05 .07\n' 110 else: 111 file1, file2 = file2, file1 112 color1, color2 = 'BLUE', 'WHITE' 113 histtype2, histtype1 = 'HIST SOLID\n', 'SET PATTERN .05 .07\n' 114 if self.plots[tag_plot].get_histo(file1, norm1) == '': 115 continue 116 117 trappe.write("NEW PLOT \n") 118 top = self.plots[tag_plot].top 119 if top.find('# particles') > -1: 120 top = top.replace('LOG', 'LIN') 121 trappe.write(top) 122 # trappe.write("SET COLOR %s \n" % color2) 123 # trappe.write("HIST PATTERNED %s \n" % color1) 124 125 126 try: 127 trappe.write(histtype1) 128 trappe.write(self.plots[tag_plot].get_histo(file1, norm1)) 129 trappe.write(histtype1) 130 trappe.write("HIST PATTERNED %s \n" % color1) 131 # trappe.write("SET COLOR %s \n" % color1) 132 except: 133 print("warning: cannot find histo in file 1 for tag " + tag_plot) 134 135 try: 136 trappe.write(self.plots[tag_plot].get_histo(file2, norm2)) 137 trappe.write(histtype2) 138 trappe.write("HIST PATTERNED %s \n" % color2) 139 trappe.write("SET COLOR WHITE \n") 140 except Exception as error: 141 print(error) 142 print("warning: cannot find histo in file 2 for tag " + tag_plot) 143 raise 144 145 #trappe.write(self.plots[tag_plot].end) 146 147 trappe.write("\n") 148 trappe.write("\n") 149 trappe.close()
150
151 - def readplot(self, trappe, file_nb):
152 """ import the data of a single plot """ 153 top = "" 154 end = "" 155 histo = "" 156 phase = 0 157 plot_tag = "" 158 newplot = 1 159 while 1: 160 line = trappe.readline() 161 #print line 162 if line == "": break 163 if phase == 0: # top of the histogram 164 if line.find("TITLE TOP") > -1: 165 index = line.find('''"''') 166 if index < 0: 167 print("warning: unable to find the name of the plot in the title") 168 print(" skipping this plot (might not be a real plot)") 169 return 170 else: 171 plot_tag = line[index + 1:] 172 plot_tag = plot_tag.replace('''"''', "") 173 plot_tag = plot_tag.replace("\n", "") 174 plot_tag = plot_tag.replace(" ", "") 175 if line.find("SET LIMITS X") > -1: 176 tag = line.replace(".0 ", "") 177 tag = tag.replace(".00 ", "") 178 tag = tag.replace(".000 ", "") 179 tag = tag.replace(".0000 ", "") 180 tag = tag.replace(".00000 ", "") 181 tag = tag.replace(".0\n", "") 182 tag = tag.replace(".00\n", "") 183 tag = tag.replace(".000\n", "") 184 tag = tag.replace(".0000\n", "") 185 tag = tag.replace(".00000\n", "") 186 tag = tag.replace(" ", "") 187 tag = tag.replace("\n", "") 188 tag = tag.replace("3.14160", "3.14159") 189 tag = tag.replace("9.42480", "9.42478") 190 tag = tag.replace("4.18880", "4.18879") 191 if plot_tag != "": 192 plot_tag += tag 193 if plot_tag not in self.plots: 194 self.plots[plot_tag] = one_plot() 195 self.order_plots.append(plot_tag) 196 else: 197 newplot = 0 198 else: 199 print("warning: unusual format, unable to extract the tag of the plot") 200 print(" skipping this plot (might not be a real plot)") 201 return 202 203 if line.find("SET ORDER") > -1 and plot_tag != "": 204 line = trappe.readline() 205 phase = 1 206 else: 207 top += line # store the line 208 if phase == 1: # histogram values 209 if line.find("PLOT") < 0 and line.find("SET PATTERN") < 0 and line.find("HIST") < 0: 210 histo += line # store the line 211 else: 212 if line.find("HISTO") > -1: 213 histo_tag = file_nb 214 self.plots[plot_tag].add_histo(histo, file_nb) 215 216 if plot_tag.find("Weights") > -1: 217 pos = histo.find("1.0100") 218 if pos > -1: 219 list = histo[pos:].split() 220 self.cross[file_nb] = float(list[1]) 221 histo = "" 222 phase = 2 223 224 225 if phase == 2: 226 if line.find("NEW PLOT") > -1: 227 if (newplot): 228 self.plots[plot_tag].add_comment(top, end_comment=end) 229 self.readplot(trappe, file_nb) 230 else: 231 if line != "":end += line 232 233 if plot_tag and plot_tag in self.plots: 234 self.plots[plot_tag].add_comment(top, end_comment=end)
235
236 -def merge_all_plots(path1, path2, outputpath='/tmp', td='../../td/td', MA=None):
237 """take a MA4 output and merge all the plots present in the HTML output""" 238 239 #find which plots correspond to what 240 pattern = re.compile(r'''TITLE TOP\s+\"\s*([^\"]*)\s*\"''') 241 all_plot1 = {} 242 for filepath in misc.glob('ma_*.top', path1): 243 filename = os.path.basename(filepath) 244 text = open(filepath).read() 245 try: 246 title = pattern.search(text).groups()[0].strip() 247 except AttributeError: 248 continue 249 all_plot1[title] = filename 250 251 # find the plot to add: 252 for filepath in misc.glob('ma_*.top', path2): 253 filename = os.path.basename(filepath) 254 text = open(filepath).read() 255 try: 256 title = pattern.search(text).groups()[0].strip() 257 except AttributeError: 258 continue 259 if title not in all_plot1: 260 continue 261 my_data = load_data() 262 my_data.import_data(pjoin(path1, all_plot1[title]), 1) 263 my_data.import_data(pjoin(path2, filename), 2) 264 my_data.print_plots(pjoin(outputpath, filename), 1, 2) 265 if 'DYLD_LIBRARY_PATH' not in os.environ: 266 os.environ['DYLD_LIBRARY_PATH'] = os.path.dirname(td) 267 elif os.path.dirname(td) not in os.environ['DYLD_LIBRARY_PATH']: 268 os.environ['DYLD_LIBRARY_PATH'] = '%s:%s' %( os.environ['DYLD_LIBRARY_PATH'], os.path.dirname(td)) 269 devnull = open(os.devnull,'w') 270 subprocess.call([td, filename], cwd=outputpath, stdout=devnull) 271 devnull.close() 272 if MA: 273 subprocess.call([pjoin(MA, 'epstosmth'),"--gsopt=\'-r60x60 -dGraphicsAlphaBits=4\'", 274 "--gsdev=jpeg", filename.replace('.top', '.ps')], cwd=outputpath)
275 #os.system('%s %s' % (td, path)) 276 277 278 279 if __name__ == "__main__": 280 281 merge_all_plots('PROC_EWdim6_1/HTML/run_12/plots_parton_tag_1/', 282 'PROC_EWdim6_1/HTML/run_11_rw_15/plots_parton_reweight_tag_1/') 283