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