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