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