Package madgraph :: Package madweight :: Module MW_driver
[hide private]
[frames] | no frames]

Source Code for Module madgraph.madweight.MW_driver

  1  #! /usr/bin/env python 
  2  ################################################################################ 
  3  # Copyright (c) 2012 The MadGraph Development team and Contributors              
  4  # 
  5  # This file is a part of the MadGraph 5 project, an application which            
  6  # automatically generates Feynman diagrams and matrix elements for arbitrary     
  7  # high-energy processes in the Standard Model and beyond.                        
  8  # 
  9  # It is subject to the MadGraph license which should accompany this              
 10  # distribution.                                                                  
 11  #                                                                                
 12  # For more information, please visit: http://madgraph.phys.ucl.ac.be             
 13  #                                                                                
 14  ################################################################################ 
 15  from __future__ import division 
 16  from __future__ import absolute_import 
 17  from __future__ import print_function 
 18  import math 
 19  import os  
 20  import sys 
 21  import subprocess 
 22  from six.moves import map 
 23  from six.moves import range 
 24   
25 -class RunningMW(object):
26
27 - def __init__(self, card_nb, first_event, nb_events, evt_file, mw_int_points, \ 28 log_level, sample_nb):
29 """store the data""" 30 31 self.card_nb = int(card_nb) 32 self.first_event = int(first_event) 33 self.evtfile = evt_file 34 self.nb_events = int(nb_events) # number of events to run 35 self.mw_int_points = int(mw_int_points) 36 self.log_level = log_level # weight | permutation | channel | iteration | full_log 37 if log_level == 'debug': 38 self.log_level = 'iteration' 39 self.debug = True 40 else: 41 self.debug = False 42 self.sample_nb = int(sample_nb) 43 44 self.current_event = -1 45 self.last_line = '' 46 self.nb_line_by_event = 0 47 48 restrict_path = evt_file.replace('verif','restrict%i' % self.card_nb).replace('.lhco','.dat') 49 if os.path.exists(restrict_path): 50 allow = list(map(int, open(restrict_path).read().split())) 51 self.allow_event = lambda x: int(x) in allow 52 else: 53 self.allow_event = lambda x: True
54
55 - class output_handler(object):
56
57 - def __init__(self, card_nb, sample_nb):
58 self.fsock = open('output_%s_%s.xml' % (card_nb, sample_nb), 'w') 59 self.fsock.write('<card id=\'%s\'>\n' % card_nb)
60
61 - def __enter__(self):
62 return self.fsock
63
64 - def __exit__(self, type, value, traceback):
65 if type is None: 66 self.fsock.write('</card>\n') 67 else: 68 self.fsock.write('<failmsg>%s\n%s\n%s</failmsg></card>\n' % (type, value, traceback))
69
70 - def run(self):
71 """Run the computation""" 72 73 fsock = open('param.dat','w') 74 fsock.writelines('param_card_'+str(self.card_nb)+'.dat\n') 75 fsock.writelines(str(self.mw_int_points)+'\n') 76 fsock.close() 77 78 with self.output_handler(self.card_nb, self.sample_nb) as self.fsock: 79 while self.get_next_event(create=True): 80 if not self.debug: 81 subprocess.call('./comp_madweight', stdout=open('log.txt','w')) 82 else: 83 print('submit in debug mode') 84 85 os.system('echo "./comp_madweight" > log.txt') 86 os.system('bash log.txt') 87 self.get_one_job_result()
88
89 - def get_next_event(self, create=True, update_event_nb=True):
90 """prepare the verif.lhco""" 91 92 93 94 if self.current_event == -1: 95 self.input_file = open(self.evtfile) 96 self.current_event +=1 97 for i in range(self.first_event): 98 self.get_next_event(False) 99 100 if update_event_nb: 101 self.current_event +=1 102 if self.current_event >= self.first_event + self.nb_events + 1: 103 return False 104 105 evt = self.last_line 106 self.last_line = '' 107 if evt: 108 nb_line = 1 109 else: 110 nb_line = 0 111 for line in self.input_file: 112 nb_line +=1 113 if not self.nb_line_by_event: 114 if len(line.split()) == 3 and nb_line > 1: 115 self.last_line = line 116 self.nb_line_by_event = nb_line -1 117 break 118 else: 119 evt += line 120 else: 121 evt += line 122 if nb_line == self.nb_line_by_event: 123 break 124 125 126 if not evt: 127 return False 128 129 130 try: 131 self.lhco_number = int(evt.split('\n')[0].split()[1]) 132 except ValueError: 133 self.lhco_number = evt.split('\n')[0].split()[1] 134 evt = evt.split('\n') 135 id, nblhco, trigger = evt[0].split() 136 if '.' in nblhco: 137 nblhco, _ = nblhco.split('.',1) 138 elif ',' in nblhco: 139 nblhco, _ = nblhco.split(',',1) 140 nblhco = ''.join(i for i in nblhco if i.isdigit()) 141 if not nblhco: 142 nblhco = '1' 143 144 evt[0] = ' '.join([id, nblhco,trigger]) 145 evt = '\n'.join(evt) 146 if self.allow_event(self.lhco_number): 147 # now write the verif.lhco event: 148 if create: 149 fsock = open('verif.lhco', 'w') 150 fsock.write(evt) 151 fsock.close() 152 else: 153 return self.get_next_event(create, update_event_nb=False) 154 155 return evt
156
157 - def get_one_job_result(self):
158 """collect the associate result and update the final output file""" 159 160 #fsock = open('output_%s_%s.xml' % (self.card_nb, self.sample_nb), 'a') 161 162 weight = Weight(self.lhco_number, log_level) 163 weight.get() 164 weight.write(self.fsock)
165 166
167 -class TFsets(dict):
168 """ """ 169 nb_space=4
170 - def __init__(self, tf_set):
171 self.value = 0 172 self.error = 0 173 self.tf_set = tf_set 174 175 dict.__init__(self)
176
177 - def add(self, perm_id, channel_id, value, error, perm_order):
178 179 if perm_id in self: 180 perm_obj = self[perm_id] 181 else: 182 perm_obj = Permutation(perm_id, perm_order) 183 self[perm_id] = perm_obj 184 perm_obj.add(channel_id, value, error)
185
186 - def write(self, fsock, log_level):
187 """ """ 188 189 self.value, self.error = self.calculate_total() 190 fsock.write('%s<tfset id=\'%s\' value=\'%s\' error=\'%s\'>' % \ 191 (' '*self.nb_space,self.tf_set, self.value, self.error)) 192 193 if log_level in ['permutation','channel', 'iterations', 'full']: 194 fsock.write('\n') 195 perm_ids = list(self.keys()) 196 perm_ids.sort() 197 for perm_id in perm_ids: 198 obj = self[perm_id] 199 obj.write(fsock, log_level) 200 fsock.write('%s</tfset>' % (' ' * self.nb_space)) 201 else: 202 fsock.write('</tfset>\n')
203
204 - def calculate_total(self):
205 206 if self.value: 207 return self.value, self.error 208 total = 0 209 total_error = 0 210 if '0' in list(self.keys()): 211 self.value, self.error = self['0'].calculate_total() 212 return self.value, self.error 213 else: 214 for perm in self.values(): 215 value, error = perm.calculate_total() 216 total += value 217 total_error += error**2 218 self.value = total / len(self) 219 self.error = math.sqrt(total_error) / len(self) 220 221 return self.value, self.error
222
223 -class Weight(dict):
224
225 - def __init__(self, lhco_number, log_level):
226 self.log_level = log_level 227 self.value = 0 228 self.error = 0 229 self.lhco_number = lhco_number 230 dict.__init__(self) 231 self.log = ''
232
233 - def get(self):
234 235 #1. get the weight, error for this object 236 try: 237 ff=open('weights.out','r') 238 except Exception: 239 return 240 for line in ff: 241 line = line.strip() 242 if not line: 243 continue 244 value, error = line.split() 245 self.value = float(value) 246 self.error = float(error) 247 break 248 os.remove('weights.out') 249 #2. details 250 self.get_details() 251 252 #3 full log 253 if self.log_level == 'full': 254 self.log = open('log.txt').read().replace('<','!>')
255
256 - def get_details(self):
257 """ """ 258 try: 259 ff=open('details.out', 'r') 260 except Exception: 261 return 262 263 for line in ff: 264 split = line.split() 265 perm_id, channel_id, tf_id, value, error = split[:5] 266 perm_order = split[5:] 267 value = float(value) 268 error = float(error) 269 if tf_id not in self: 270 tfsets = TFsets(tf_id) 271 self[tf_id] = tfsets 272 else: 273 tfsets = self[tf_id] 274 tfsets.add(perm_id, channel_id, value, error, perm_order)
275
276 - def write(self, fsock):
277 """ """ 278 279 fsock.write('<event id=\'%s\' value=\'%s\' error=\'%s\'>\n' % \ 280 (self.lhco_number, self.value, self.error)) 281 tfsets = list(self.keys()) 282 tfsets.sort() 283 for tf_id in tfsets: 284 self[tf_id].write(fsock, self.log_level) 285 if 'full' == self.log_level: 286 fsock.write('\n <log>\n%s\n</log>\n' % self.log)#.replace('\n','\n<br></br>')) 287 fsock.write('</event>\n')
288
289 - def __str__(self):
290 return 'Weight(%s)' % self.value
291
292 - def __repr__(self):
293 return 'Weight(%s)' % self.value
294
295 -class Permutation(dict):
296 nb_space=8
297 - def __init__(self, perm_id, perm_order):
298 self.value = 0 299 self.error = 0 300 self.error2 = 0 301 self.id = perm_id 302 self.perm_order = ' '.join(perm_order) 303 304 dict.__init__(self)
305
306 - def add(self, channel_id, value, error):
307 308 self[channel_id] = Channel(channel_id, value, error)
309
310 - def write(self, fsock, log_level):
311 """ """ 312 313 self.value, self.error = self.calculate_total() 314 if self.id =='0': 315 tag = 'all' 316 else: 317 tag = self.id 318 319 fsock.write('%s<permutation id=\'%s\' value=\'%s\' error=\'%s\'>\n%s%s' % \ 320 (' '*self.nb_space, tag, self.value, self.error, 321 ' '*(self.nb_space+2), self.perm_order)) 322 323 if log_level in ['channel', 'iterations', 'full']: 324 fsock.write('\n') 325 ids = list(self.keys()) 326 ids.sort() 327 for pid in ids: 328 channel = self[pid] 329 channel.write(fsock, log_level) 330 fsock.write('\n') 331 fsock.write('%s</permutation>\n' % (' '*self.nb_space)) 332 else: 333 fsock.write('</permutation>\n')
334
335 - def calculate_total(self):
336 337 if self.value: 338 self.error = math.sqrt(self.error2) 339 return self.value, self.error 340 total = 0 341 error = 0 342 for channel in self.values(): 343 total += channel.value 344 error += channel.error**2 345 self.value = total 346 self.error2 = error 347 self.error = math.sqrt(self.error2) 348 return total, self.error
349
350 -class Channel(object):
351 """ """ 352 nb_space=12
353 - def __init__(self, channel_id, value, error):
354 """ """ 355 self.channel_id = channel_id 356 self.value = float(value) 357 self.error = float(error)
358
359 - def write(self, fsock, log_level):
360 361 fsock.write('%s<channel id=\'%s\' value=\'%s\' error=\'%s\'></channel>' % 362 (' '*self.nb_space,self.channel_id, self.value, self.error))
363 364 if __name__ == '__main__': 365 try: 366 card_nb, first_event, nb_event, evt, mw_int_points, log_level, sample_nb = sys.argv[1:] 367 except: 368 card_nb, first_event, nb_event, evt, mw_int_points, log_level, sample_nb = open('arguments').read().split() 369 else: 370 fsock = open('arguments', 'w') 371 fsock.write(' '.join(sys.argv[1:])) 372 fsock.close() 373 running_mw = RunningMW(card_nb, first_event, nb_event, evt, mw_int_points, log_level, sample_nb) 374 running_mw.run() 375