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

Source Code for Module madgraph.various.gen_crossxhtml

   1  ################################################################################ 
   2  # 
   3  # Copyright (c) 2011 The MadGraph5_aMC@NLO Development team and Contributors 
   4  # 
   5  # This file is a part of the MadGraph5_aMC@NLO 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 MadGraph5_aMC@NLO license which should accompany this  
  10  # distribution. 
  11  # 
  12  # For more information, visit madgraph.phys.ucl.ac.be and amcatnlo.web.cern.ch 
  13  # 
  14  ################################################################################ 
  15  """ Create gen_crossxhtml """ 
  16   
  17   
  18  import os 
  19  import math 
  20  import re 
  21  import pickle 
  22  import re 
  23  import glob 
  24  import logging 
  25   
  26  try: 
  27      import internal.files as files 
  28      import internal.save_load_object as save_load_object 
  29      import internal.lhe_parser as lhe_parser 
  30      import internal.misc as misc 
  31  except ImportError: 
  32      import madgraph.iolibs.files as files 
  33      import madgraph.iolibs.save_load_object as save_load_object 
  34      import madgraph.various.lhe_parser as lhe_parser 
  35      import madgraph.various.misc as misc 
  36  pjoin = os.path.join 
  37  exists = os.path.exists 
  38  logger = logging.getLogger('madgraph.stdout') # -> stdout 
  39   
  40   
  41   
  42  crossxhtml_template = """ 
  43  <HTML>  
  44  <HEAD>  
  45      %(refresh)s  
  46      <META HTTP-EQUIV="EXPIRES" CONTENT="20" >  
  47      <TITLE>Online Event Generation</TITLE> 
  48      <link rel=stylesheet href="./HTML/mgstyle.css" type="text/css"> 
  49  </HEAD> 
  50  <BODY> 
  51  <script type="text/javascript"> 
  52  function UrlExists(url) { 
  53    var http = new XMLHttpRequest(); 
  54    http.open('HEAD', url, false); 
  55    try{ 
  56       http.send() 
  57       } 
  58    catch(err){ 
  59     return 1==2; 
  60    } 
  61    return http.status!=404; 
  62  } 
  63  function check_link(url,alt, id){ 
  64      var obj = document.getElementById(id); 
  65      if ( ! UrlExists(url)){ 
  66         if ( ! UrlExists(alt)){ 
  67           obj.href = url; 
  68           return 1==1; 
  69          } 
  70         obj.href = alt; 
  71         return 1 == 2; 
  72      } 
  73      obj.href = url; 
  74      return 1==1; 
  75  } 
  76  </script>     
  77      <H2 align=center> Results in the %(model)s for %(process)s </H2>  
  78      <HR> 
  79      %(status)s 
  80      <br> 
  81      <br> 
  82      <H2 align="center"> Available Results </H2> 
  83          <TABLE BORDER=2 align="center">   
  84              <TR align="center"> 
  85                  <TH>Run</TH>  
  86                  <TH>Collider</TH>  
  87                  <TH> Banner </TH> 
  88                  <TH> %(numerical_title)s </TH>  
  89                  <TH> Events  </TH> 
  90                  <TH> Data </TH>   
  91                  <TH>Output</TH> 
  92                  <TH>Action</TH>  
  93              </TR>       
  94              %(old_run)s 
  95          </TABLE> 
  96      <H3 align=center><A HREF="./index.html"> Main Page </A></H3> 
  97  </BODY>  
  98  </HTML>  
  99  """ 
 100   
 101  status_template = """ 
 102  <H2 ALIGN=CENTER> Currently Running %(run_mode_string)s</H2> 
 103  <TABLE BORDER=2 ALIGN=CENTER> 
 104      <TR ALIGN=CENTER> 
 105          <TH nowrap ROWSPAN=2 font color="#0000FF"> Run Name </TH> 
 106          <TH nowrap ROWSPAN=2 font color="#0000FF"> Tag Name </TH> 
 107          <TH nowrap ROWSPAN=2 font color="#0000FF"> Cards </TH>    
 108          <TH nowrap ROWSPAN=2 font color="#0000FF"> Results </TH>  
 109          <TH nowrap ROWSPAN=1 COLSPAN=3 font color="#0000FF"> Status/Jobs </TH> 
 110      </TR> 
 111          <TR>  
 112              <TH>   Queued </TH> 
 113              <TH>  Running </TH> 
 114              <TH> Done  </TH> 
 115          </TR> 
 116      <TR ALIGN=CENTER>  
 117          <TD nowrap ROWSPAN=2> %(run_name)s </TD> 
 118          <TD nowrap ROWSPAN=2> %(tag_name)s </TD> 
 119          <TD nowrap ROWSPAN=2> <a href="./Cards/param_card.dat">param_card</a><BR> 
 120                      <a href="./Cards/run_card.dat">run_card</a><BR> 
 121                      %(plot_card)s 
 122                      %(pythia_card)s 
 123                      %(pgs_card)s 
 124                      %(delphes_card)s 
 125                      %(shower_card)s 
 126                      %(fo_analyse_card)s 
 127          </TD> 
 128          <TD nowrap ROWSPAN=2> %(results)s </TD>  
 129          %(status)s 
 130   </TR> 
 131   <TR></TR> 
 132     %(stop_form)s 
 133   </TABLE> 
 134  """ 
 135   
136 -class AllResults(dict):
137 """Store the results for all the run of a given directory""" 138 139 web = False 140
141 - def __init__(self, model, process, path, recreateold=True):
142 143 dict.__init__(self) 144 self.order = [] 145 self.lastrun = None 146 self.process = ', '.join(process) 147 if len(self.process) > 60: 148 pos = self.process[50:].find(',') 149 if pos != -1: 150 self.process = self.process[:50+pos] + ', ...' 151 self.path = path 152 self.model = model 153 self.status = '' 154 self.unit = 'pb' 155 self.current = None 156 157 # Check if some directory already exists and if so add them 158 runs = [d for d in os.listdir(pjoin(path, 'Events')) if 159 os.path.isdir(pjoin(path, 'Events', d))] 160 if runs: 161 if recreateold: 162 for run in runs: 163 self.readd_old_run(run) 164 self.current = self[run] 165 else: 166 logger.warning("Previous runs exists but they will not be present in the html output.")
167
168 - def readd_old_run(self, run_name):
169 """ re-create the data-base from scratch if the db was remove """ 170 171 event_path = pjoin(self.path, "Events", run_name, "unweighted_events.lhe") 172 173 import banner as bannerlib 174 175 if os.path.exists("%s.gz" % event_path): 176 misc.gunzip(event_path, keep=True) 177 if not os.path.exists(event_path): 178 return 179 banner = bannerlib.Banner(event_path) 180 181 # load the information to add a new Run: 182 run_card = banner.charge_card("run_card") 183 process = banner.get_detail("proc_card", "generate") 184 #create the new object 185 run = RunResults(run_name, run_card, process, self.path) 186 run.recreate(banner) 187 self[run_name] = run 188 self.order.append(run_name)
189 190
191 - def def_current(self, run, tag=None):
192 """define the name of the current run 193 The first argument can be a OneTagResults 194 """ 195 196 if isinstance(run, OneTagResults): 197 self.current = run 198 self.lastrun = run['run_name'] 199 return 200 201 assert run in self or run == None 202 self.lastrun = run 203 if run: 204 if not tag: 205 self.current = self[run][-1] 206 else: 207 assert tag in self[run].tags 208 index = self[run].tags.index(tag) 209 self.current = self[run][index] 210 211 else: 212 self.current = None
213
214 - def delete_run(self, run_name, tag=None):
215 """delete a run from the database""" 216 217 assert run_name in self 218 219 if not tag : 220 if self.current and self.current['run_name'] == run_name: 221 self.def_current(None) 222 del self[run_name] 223 self.order.remove(run_name) 224 if self.lastrun == run_name: 225 self.lastrun = None 226 else: 227 assert tag in [a['tag'] for a in self[run_name]] 228 RUN = self[run_name] 229 if len(RUN) == 1: 230 self.delete_run(run_name) 231 return 232 RUN.remove(tag) 233 234 #update the html 235 self.output()
236
237 - def def_web_mode(self, web):
238 """define if we are in web mode or not """ 239 if web is True: 240 try: 241 web = os.environ['SERVER_NAME'] 242 except Exception: 243 web = 'my_computer' 244 self['web'] = web 245 self.web = web
246
247 - def add_run(self, name, run_card, current=True):
248 """ Adding a run to this directory""" 249 250 tag = run_card['run_tag'] 251 if name in self.order: 252 #self.order.remove(name) # Reorder the run to put this one at the end 253 if tag in self[name].tags: 254 if self[name].return_tag(tag).parton and len(self[name]) > 1: 255 #move the parton information before the removr 256 self[name].return_tag(self[name][1]['tag']).parton = \ 257 self[name].return_tag(tag).parton 258 if len(self[name]) > 1: 259 self[name].remove(tag) # Remove previous tag if define 260 self[name].add(OneTagResults(name, run_card, self.path)) 261 else: 262 #add the new tag run 263 self[name].add(OneTagResults(name, run_card, self.path)) 264 new = self[name] 265 else: 266 new = RunResults(name, run_card, self.process, self.path) 267 self[name] = new 268 self.order.append(name) 269 270 if current: 271 self.def_current(name) 272 if new.info['unit'] == 'GeV': 273 self.unit = 'GeV'
274
275 - def update(self, status, level, makehtml=True, error=False):
276 """update the current run status""" 277 if self.current: 278 self.current.update_status(level) 279 self.status = status 280 if self.current and self.current.debug and self.status and not error: 281 self.current.debug = None 282 283 if makehtml: 284 self.output()
285
286 - def resetall(self, main_path=None):
287 """check the output status of all run 288 main_path redefines the path associated to the run (allowing to move 289 the directory) 290 """ 291 292 self.path = main_path 293 294 for key,run in self.items(): 295 if key == 'web': 296 continue 297 for i,subrun in enumerate(run): 298 self.def_current(subrun) 299 self.clean() 300 self.current.event_path = pjoin(main_path,'Events') 301 self.current.me_dir = main_path 302 if i==0: 303 self.current.update_status() 304 else: 305 self.current.update_status(nolevel='parton') 306 self.output()
307
308 - def clean(self, levels = ['all'], run=None, tag=None):
309 """clean the run for the levels""" 310 311 if not run and not self.current: 312 return 313 to_clean = self.current 314 if run and not tag: 315 for tagrun in self[run]: 316 self.clean(levels, run, tagrun['tag']) 317 return 318 319 if run: 320 to_clean = self[run].return_tag(tag) 321 else: 322 run = to_clean['run_name'] 323 324 if 'all' in levels: 325 levels = ['parton', 'pythia', 'pgs', 'delphes', 'channel'] 326 327 if 'parton' in levels: 328 to_clean.parton = [] 329 if 'pythia' in levels: 330 to_clean.pythia = [] 331 if 'pgs' in levels: 332 to_clean.pgs = [] 333 if 'delphes' in levels: 334 to_clean.delphes = []
335 336
337 - def save(self):
338 """Save the results of this directory in a pickle file""" 339 filename = pjoin(self.path, 'HTML', 'results.pkl') 340 save_load_object.save_to_file(filename, self)
341
342 - def add_detail(self, name, value, run=None, tag=None):
343 """ add information to current run (cross/error/event)""" 344 assert name in ['cross', 'error', 'nb_event', 'cross_pythia', 345 'nb_event_pythia','error_pythia', 'run_mode'] 346 347 if not run and not self.current: 348 return 349 350 if not run: 351 run = self.current 352 else: 353 run = self[run].return_tag(tag) 354 355 if name == 'cross_pythia': 356 run['cross_pythia'] = float(value) 357 elif name == 'nb_event': 358 run[name] = int(value) 359 elif name == 'nb_event_pythia': 360 run[name] = int(value) 361 elif name == 'run_mode': 362 run[name] = value 363 else: 364 run[name] = float(value)
365
366 - def output(self):
367 """ write the output file """ 368 369 # 1) Create the text for the status directory 370 if self.status and self.current: 371 if isinstance(self.status, str): 372 status = '<td ROWSPAN=2 colspan=4>%s</td>' % self.status 373 else: 374 s = list(self.status) 375 if s[0] == '$events': 376 if self.current['nb_event']: 377 nevent = self.current['nb_event'] 378 else: 379 nevent = self[self.current['run_name']][0]['nb_event'] 380 if nevent: 381 s[0] = nevent - int(s[1]) -int(s[2]) 382 else: 383 s[0] = '' 384 status ='''<td> %s </td> <td> %s </td> <td> %s </td> 385 </tr><tr><td colspan=3><center> %s </center></td>''' % (s[0],s[1], s[2], s[3]) 386 387 388 status_dict = {'status': status, 389 'cross': self.current['cross'], 390 'error': self.current['error'], 391 'run_name': self.current['run_name'], 392 'tag_name': self.current['tag'], 393 'unit': self[self.current['run_name']].info['unit']} 394 # add the run_mode_string for amcatnlo_run 395 if 'run_mode' in self.current.keys(): 396 run_mode_string = {'aMC@NLO': '(aMC@NLO)', 397 'aMC@LO': '(aMC@LO)', 398 'noshower': '(aMC@NLO)', 399 'noshowerLO': '(aMC@LO)', 400 'NLO': '(NLO f.o.)', 401 'LO': '(LO f.o.)', 402 'madevent':''} 403 status_dict['run_mode_string'] = run_mode_string[self.current['run_mode']] 404 else: 405 status_dict['run_mode_string'] = '' 406 407 408 if exists(pjoin(self.path, 'HTML',self.current['run_name'], 409 'results.html')): 410 status_dict['results'] = """<A HREF="./HTML/%(run_name)s/results.html">%(cross).4g <font face=symbol>&#177;</font> %(error).4g (%(unit)s)</A>""" % status_dict 411 else: 412 status_dict['results'] = "No results yet" 413 if exists(pjoin(self.path, 'Cards', 'plot_card.dat')): 414 status_dict['plot_card'] = """ <a href="./Cards/plot_card.dat">plot_card</a><BR>""" 415 else: 416 status_dict['plot_card'] = "" 417 if exists(pjoin(self.path, 'Cards', 'pythia_card.dat')): 418 status_dict['pythia_card'] = """ <a href="./Cards/pythia_card.dat">pythia_card</a><BR>""" 419 else: 420 status_dict['pythia_card'] = "" 421 if exists(pjoin(self.path, 'Cards', 'pgs_card.dat')): 422 status_dict['pgs_card'] = """ <a href="./Cards/pgs_card.dat">pgs_card</a><BR>""" 423 else: 424 status_dict['pgs_card'] = "" 425 if exists(pjoin(self.path, 'Cards', 'delphes_card.dat')): 426 status_dict['delphes_card'] = """ <a href="./Cards/delphes_card.dat">delphes_card</a><BR>""" 427 else: 428 status_dict['delphes_card'] = "" 429 if exists(pjoin(self.path, 'Cards', 'shower_card.dat')): 430 status_dict['shower_card'] = """ <a href="./Cards/shower_card.dat">shower_card</a><BR>""" 431 else: 432 status_dict['shower_card'] = "" 433 if exists(pjoin(self.path, 'Cards', 'FO_analyse_card.dat')): 434 status_dict['fo_analyse_card'] = """ <a href="./Cards/FO_analyse_card.dat">FO_analyse_card</a><BR>""" 435 else: 436 status_dict['fo_analyse_card'] = "" 437 438 if self.web: 439 status_dict['stop_form'] = """ 440 <TR ALIGN=CENTER><TD COLSPAN=7 text-align=center> 441 <FORM ACTION="http://%(web)s/cgi-bin/RunProcess/handle_runs-pl" ENCTYPE="multipart/form-data" METHOD="POST"> 442 <INPUT TYPE=HIDDEN NAME=directory VALUE="%(me_dir)s"> 443 <INPUT TYPE=HIDDEN NAME=whattodo VALUE="stop_job"> 444 <INPUT TYPE=SUBMIT VALUE="Stop Current Job"> 445 </FORM></TD></TR>""" % {'me_dir': self.path, 'web': self.web} 446 else: 447 status_dict['stop_form'] = "" 448 449 450 status = status_template % status_dict 451 refresh = "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"10\">" 452 else: 453 status ='' 454 refresh = '' 455 456 457 # See if we need to incorporate the button for submission 458 if os.path.exists(pjoin(self.path, 'RunWeb')): 459 running = True 460 else: 461 running = False 462 463 # 2) Create the text for the old run: 464 old_run = '' 465 for key in self.order: 466 old_run += self[key].get_html(self.path, web=self.web, running=running) 467 468 text_dict = {'process': self.process, 469 'model': self.model, 470 'status': status, 471 'old_run': old_run, 472 'refresh': refresh, 473 'numerical_title': self.unit == 'pb' and 'Cross section (pb)'\ 474 or 'Width (GeV)'} 475 476 text = crossxhtml_template % text_dict 477 open(pjoin(self.path,'crossx.html'),'w').write(text)
478 479
480 -class AllResultsNLO(AllResults):
481 """Store the results for a NLO run of a given directory""" 482
483 - def __init__(self,model, process, path, recreateold=False):
484 return AllResults.__init__(self, model, process, path, recreateold=recreateold)
485 486
487 -class RunResults(list):
488 """The list of all OneTagResults""" 489
490 - def __init__(self, run_name, run_card, process, path):
491 """initialize the object""" 492 493 self.info = {'run_name': run_name,'me_dir':path} 494 self.tags = [run_card['run_tag']] 495 # Set the collider information 496 data = process.split('>',1)[0].split() 497 if len(data) == 2: 498 name1,name2 = data 499 if run_card['lpp1'] == '-1': 500 name1 = ' p~' 501 elif run_card['lpp1'] == '1': 502 name1 = ' p' 503 elif run_card['lpp1'] == '2': 504 name1 = ' a' 505 if run_card['lpp2'] == '-1': 506 name2 = 'p~' 507 elif run_card['lpp2'] == '1': 508 name2 = ' p' 509 elif run_card['lpp2'] == '2': 510 name2 = ' a' 511 self.info['collider'] = '''%s %s <br> %s x %s GeV''' % \ 512 (name1, name2, run_card['ebeam1'], run_card['ebeam2']) 513 self.info['unit'] = 'pb' 514 else: 515 self.info['collider'] = 'decay' 516 self.info['unit'] = 'GeV' 517 518 self.append(OneTagResults(run_name, run_card, path))
519 520
521 - def get_html(self, output_path, **opt):
522 """WRITE HTML OUTPUT""" 523 try: 524 self.web = opt['web'] 525 self.info['web'] = self.web 526 except Exception: 527 self.web = False 528 529 # check if more than one parton output 530 parton = [r for r in self if r.parton] 531 # clean wrong previous run link 532 if len(parton)>1: 533 for p in parton[:-1]: 534 p.parton = [] 535 536 dico = self.info 537 dico['run_span'] = sum([tag.get_nb_line() for tag in self], 1) -1 538 dico['tag_data'] = '\n'.join([tag.get_html(self) for tag in self]) 539 text = """ 540 <tr> 541 <td rowspan=%(run_span)s>%(run_name)s</td> 542 <td rowspan=%(run_span)s><center> %(collider)s </center></td> 543 %(tag_data)s 544 </tr> 545 """ % dico 546 547 if self.web: 548 549 text = text % self.info 550 551 return text
552 553
554 - def return_tag(self, name):
555 556 for data in self: 557 if data['tag'] == name: 558 return data 559 560 if name is None: 561 # return last entry 562 return self[-1] 563 564 raise Exception, '%s is not a valid tag' % name
565
566 - def recreate(self, banner):
567 """Fully recreate the information due to a hard removal of the db 568 Work for LO ONLY!""" 569 570 run_name = self.info["run_name"] 571 run_card = banner.get("run_card") 572 path = self.info["me_dir"] 573 # Recover the main information (cross-section/number of event) 574 informations = banner['mggenerationinfo'] 575 #number of events 576 nb_event = re.search(r"Number\s*of\s*Events\s*:\s*(\d*)", informations) 577 if nb_event: 578 nb_event = int(nb_event.group(1)) 579 else: 580 nb_event = 0 581 582 # cross-section 583 cross = re.search(r"Integrated\s*weight\s*\(\s*pb\s*\)\s*:\s*([\+\-\d.e]+)", informations, 584 re.I) 585 if cross: 586 cross = float(cross.group(1)) 587 else: 588 cross = 0 589 590 # search pythia file for tag: tag_1_pythia.log 591 path = pjoin(self.info['me_dir'],'Events', self.info['run_name']) 592 files = [pjoin(path, f) for f in os.listdir(path) if 593 os.path.isfile(pjoin(path,f)) and f.endswith('pythia.log')] 594 #order them by creation date. 595 files.sort(key=lambda x: os.path.getmtime(x)) 596 tags = [os.path.basename(name[:-11]) for name in files] 597 598 599 # No pythia only a single run: 600 if not tags: 601 self[0]['nb_event'] = nb_event 602 self[0]['cross'] = cross 603 604 #Loop over pythia run 605 for tag in tags: 606 if tag not in self.tags: 607 tagresult = OneTagResults(run_name, run_card, path) 608 tagresult['tag'] = tag 609 self.add(tagresult) 610 else: 611 tagresult = self.return_tag(tag) 612 tagresult['nb_event'] = nb_event 613 tagresult['cross'] = cross 614 if run_card['ickkw'] != '0': 615 #parse the file to have back the information 616 pythia_log = misc.BackRead(pjoin(path, '%s_pythia.log' % tag)) 617 pythiare = re.compile("\s*I\s+0 All included subprocesses\s+I\s+(?P<generated>\d+)\s+(?P<tried>\d+)\s+I\s+(?P<xsec>[\d\.D\-+]+)\s+I") 618 for line in pythia_log: 619 info = pythiare.search(line) 620 if not info: 621 continue 622 try: 623 # Pythia cross section in mb, we want pb 624 sigma_m = float(info.group('xsec').replace('D','E')) *1e9 625 Nacc = int(info.group('generated')) 626 except ValueError: 627 # xsec is not float - this should not happen 628 tagresult['cross_pythia'] = 0 629 tagresult['nb_event_pythia'] = 0 630 tagresult['error_pythia'] = 0 631 else: 632 tagresult['cross_pythia'] = sigma_m 633 tagresult['nb_event_pythia'] = Nacc 634 tagresult['error_pythia'] = 0 635 break 636 pythia_log.close()
637 638
639 - def is_empty(self):
640 """Check if this run contains smtg else than html information""" 641 642 if not self: 643 return True 644 if len(self) > 1: 645 return False 646 647 data = self[0] 648 if data.parton or data.pythia or data.pgs or data.delphes: 649 return False 650 else: 651 return True
652
653 - def add(self, obj):
654 """ """ 655 656 assert isinstance(obj, OneTagResults) 657 tag = obj['tag'] 658 assert tag not in self.tags 659 self.tags.append(tag) 660 self.append(obj)
661
662 - def get_last_pythia(self):
663 for i in range(1, len(self)+1): 664 if self[-i].pythia: 665 return self[-i]['tag']
666
667 - def get_current_info(self):
668 669 output = {} 670 current = self[-1] 671 # Check that cross/nb_event/error are define 672 if current.pythia and not current['nb_event'] and len(self) > 1: 673 output['nb_event'] = self[-2]['nb_event'] 674 output['cross'] = self[-2]['cross'] 675 output['error'] = self[-2]['error'] 676 elif (current.pgs or current.delphes) and not current['nb_event'] and len(self) > 1: 677 if self[-2]['cross_pythia'] and self[-2]['nb_event_pythia']: 678 output['cross'] = self[-2]['cross_pythia'] 679 output['nb_event'] = self[-2]['nb_event_pythia'] 680 output['error'] = self[-2]['error_pythia'] 681 else: 682 output['nb_event'] = self[-2]['nb_event'] 683 output['cross'] = self[-2]['cross'] 684 output['error'] = self[-2]['error'] 685 elif current['cross']: 686 return current 687 elif len(self) > 1: 688 output['nb_event'] = self[-2]['nb_event'] 689 output['cross'] = self[-2]['cross'] 690 output['error'] = self[-2]['error'] 691 else: 692 output['nb_event'] = 0 693 output['cross'] = 0 694 output['error'] = 1e-99 695 return output
696 697
698 - def remove(self, tag):
699 700 assert tag in self.tags 701 702 obj = [o for o in self if o['tag']==tag][0] 703 self.tags.remove(tag) 704 list.remove(self, obj)
705 706 707
708 -class OneTagResults(dict):
709 """ Store the results of a specific run """ 710
711 - def __init__(self, run_name, run_card, path):
712 """initialize the object""" 713 714 # define at run_result 715 self['run_name'] = run_name 716 self['tag'] = run_card['run_tag'] 717 self.event_path = pjoin(path,'Events') 718 self.me_dir = path 719 self.debug = None 720 721 # Default value 722 self['nb_event'] = 0 723 self['cross'] = 0 724 self['cross_pythia'] = '' 725 self['nb_event_pythia'] = 0 726 self['error'] = 0 727 self['run_mode'] = 'madevent' 728 self.parton = [] 729 self.reweight = [] 730 self.pythia = [] 731 self.pgs = [] 732 self.delphes = [] 733 self.shower = [] 734 # data 735 self.status = ''
736 737 738
739 - def update_status(self, level='all', nolevel=[]):
740 """update the status of the current run """ 741 742 import misc as misc 743 exists = os.path.exists 744 run = self['run_name'] 745 tag =self['tag'] 746 747 path = pjoin(self.event_path, run) 748 html_path = pjoin(self.event_path, os.pardir, 'HTML', run) 749 750 # Check if the output of the last status exists 751 if level in ['gridpack','all']: 752 if 'gridpack' not in self.parton and \ 753 exists(pjoin(path,os.pardir ,os.pardir,"%s_gridpack.tar.gz" % run)): 754 self.parton.append('gridpack') 755 # Check if the output of the last status exists 756 if level in ['reweight','all']: 757 if 'plot' not in self.reweight and \ 758 exists(pjoin(html_path,"plots_%s.html" % tag)): 759 self.reweight.append('plot') 760 761 if level in ['parton','all'] and 'parton' not in nolevel: 762 763 if 'lhe' not in self.parton and \ 764 (exists(pjoin(path,"unweighted_events.lhe.gz")) or 765 exists(pjoin(path,"unweighted_events.lhe")) or 766 exists(pjoin(path,"events.lhe.gz")) or 767 exists(pjoin(path,"events.lhe"))): 768 self.parton.append('lhe') 769 770 if 'root' not in self.parton and \ 771 exists(pjoin(path,"unweighted_events.root")): 772 self.parton.append('root') 773 774 if 'plot' not in self.parton and \ 775 exists(pjoin(html_path,"plots_parton.html")): 776 self.parton.append('plot') 777 778 if 'param_card' not in self.parton and \ 779 exists(pjoin(path, "param_card.dat")): 780 self.parton.append('param_card') 781 782 if 'syst' not in self.parton and \ 783 exists(pjoin(path, "%s_parton_syscalc.log" %self['tag'])): 784 self.parton.append('syst') 785 786 if glob.glob(pjoin(path,"*.top")): 787 if self['run_mode'] in ['LO', 'NLO']: 788 self.parton.append('top') 789 790 if level in ['shower','all'] and 'shower' not in nolevel \ 791 and self['run_mode'] != 'madevent': 792 # this is for hep/top files from amcatnlo 793 if glob.glob(pjoin(path,"*.hep")) + \ 794 glob.glob(pjoin(path,"*.hep.gz")): 795 self.shower.append('hep') 796 797 if 'plot' not in self.shower and \ 798 exists(pjoin(html_path,"plots_shower_%s.html" % tag)): 799 self.shower.append('plot') 800 801 if glob.glob(pjoin(path,"*.hepmc")) + \ 802 glob.glob(pjoin(path,"*.hepmc.gz")): 803 self.shower.append('hepmc') 804 805 if glob.glob(pjoin(path,"*.top")): 806 if self['run_mode'] in ['LO', 'NLO']: 807 self.parton.append('top') 808 else: 809 self.shower.append('top') 810 811 812 813 if level in ['pythia', 'all']: 814 815 if 'plot' not in self.pythia and \ 816 exists(pjoin(html_path,"plots_pythia_%s.html" % tag)): 817 self.pythia.append('plot') 818 819 if 'lhe' not in self.pythia and \ 820 (exists(pjoin(path,"%s_pythia_events.lhe.gz" % tag)) or 821 exists(pjoin(path,"%s_pythia_events.lhe" % tag))): 822 self.pythia.append('lhe') 823 824 825 if 'hep' not in self.pythia and \ 826 (exists(pjoin(path,"%s_pythia_events.hep.gz" % tag)) or 827 exists(pjoin(path,"%s_pythia_events.hep" % tag))): 828 self.pythia.append('hep') 829 830 if 'rwt' not in self.pythia and \ 831 (exists(pjoin(path,"%s_syscalc.dat.gz" % tag)) or 832 exists(pjoin(path,"%s_syscalc.dat" % tag))): 833 self.pythia.append('rwt') 834 835 if 'root' not in self.pythia and \ 836 exists(pjoin(path,"%s_pythia_events.root" % tag)): 837 self.pythia.append('root') 838 839 if 'lheroot' not in self.pythia and \ 840 exists(pjoin(path,"%s_pythia_lhe_events.root" % tag)): 841 self.pythia.append('lheroot') 842 843 if 'log' not in self.pythia and \ 844 exists(pjoin(path,"%s_pythia.log" % tag)): 845 self.pythia.append('log') 846 847 if level in ['pgs', 'all']: 848 849 if 'plot' not in self.pgs and \ 850 exists(pjoin(html_path,"plots_pgs_%s.html" % tag)): 851 self.pgs.append('plot') 852 853 if 'lhco' not in self.pgs and \ 854 (exists(pjoin(path,"%s_pgs_events.lhco.gz" % tag)) or 855 exists(pjoin(path,"%s_pgs_events.lhco." % tag))): 856 self.pgs.append('lhco') 857 858 if 'root' not in self.pgs and \ 859 exists(pjoin(path,"%s_pgs_events.root" % tag)): 860 self.pgs.append('root') 861 862 if 'log' not in self.pgs and \ 863 exists(pjoin(path,"%s_pgs.log" % tag)): 864 self.pgs.append('log') 865 866 if level in ['delphes', 'all']: 867 868 if 'plot' not in self.delphes and \ 869 exists(pjoin(html_path,"plots_delphes_%s.html" % tag)): 870 self.delphes.append('plot') 871 872 if 'lhco' not in self.delphes and \ 873 (exists(pjoin(path,"%s_delphes_events.lhco.gz" % tag)) or 874 exists(pjoin(path,"%s_delphes_events.lhco" % tag))): 875 self.delphes.append('lhco') 876 877 if 'root' not in self.delphes and \ 878 exists(pjoin(path,"%s_delphes_events.root" % tag)): 879 self.delphes.append('root') 880 881 if 'log' not in self.delphes and \ 882 exists(pjoin(path,"%s_delphes.log" % tag)): 883 self.delphes.append('log')
884 891 896 1002 1003
1004 - def get_nb_line(self):
1005 1006 nb_line = 0 1007 for i in [self.parton, self.reweight, self.pythia, self.pgs, self.delphes, self.shower]: 1008 if len(i): 1009 nb_line += 1 1010 return max([nb_line,1])
1011 1012
1013 - def get_html(self, runresults):
1014 """create the html output linked to the this tag 1015 RunResults is given in case of cross-section need to be taken 1016 from a previous run 1017 """ 1018 1019 1020 tag_template = """ 1021 <td rowspan=%(tag_span)s> <a href="./Events/%(run)s/%(run)s_%(tag)s_banner.txt">%(tag)s</a>%(debug)s</td> 1022 %(subruns)s""" 1023 1024 # Compute the text for eachsubpart 1025 1026 sub_part_template_parton = """ 1027 <td rowspan=%(cross_span)s><center><a href="./HTML/%(run)s/results.html"> %(cross).4g <font face=symbol>&#177;</font> %(err).2g</a> %(syst)s </center></td> 1028 <td rowspan=%(cross_span)s><center> %(nb_event)s<center></td><td> %(type)s </td> 1029 <td> %(links)s</td> 1030 <td> %(action)s</td> 1031 </tr>""" 1032 1033 sub_part_template_reweight = """ 1034 <td rowspan=%(cross_span)s><center> %(cross).4g </center></td> 1035 <td rowspan=%(cross_span)s><center> %(nb_event)s<center></td><td> %(type)s </td> 1036 <td> %(links)s</td> 1037 <td> %(action)s</td> 1038 </tr>""" 1039 1040 sub_part_template_pgs = """ 1041 <td> %(type)s </td> 1042 <td> %(links)s</td> 1043 <td> %(action)s</td> 1044 </tr>""" 1045 1046 sub_part_template_shower = """ 1047 <td> %(type)s %(run_mode)s </td> 1048 <td> %(links)s</td> 1049 <td> %(action)s</td> 1050 </tr>""" 1051 1052 # Compute the HTMl output for subpart 1053 nb_line = self.get_nb_line() 1054 # Check that cross/nb_event/error are define 1055 if self.pythia and not self['nb_event']: 1056 try: 1057 self['nb_event'] = runresults[-2]['nb_event'] 1058 self['cross'] = runresults[-2]['cross'] 1059 self['error'] = runresults[-2]['error'] 1060 except Exception: 1061 pass 1062 1063 elif (self.pgs or self.delphes) and not self['nb_event'] and \ 1064 len(runresults) > 1: 1065 if runresults[-2]['cross_pythia'] and runresults[-2]['cross']: 1066 self['cross'] = runresults[-2]['cross_pythia'] 1067 self['error'] = runresults[-2]['error_pythia'] 1068 self['nb_event'] = runresults[-2]['nb_event_pythia'] 1069 else: 1070 self['nb_event'] = runresults[-2]['nb_event'] 1071 self['cross'] = runresults[-2]['cross'] 1072 self['error'] = runresults[-2]['error'] 1073 1074 1075 first = None 1076 subresults_html = '' 1077 for ttype in ['parton', 'pythia', 'pgs', 'delphes','reweight','shower']: 1078 data = getattr(self, ttype) 1079 if not data: 1080 continue 1081 1082 local_dico = {'type': ttype, 'run': self['run_name'], 'syst': ''} 1083 if 'run_mode' in self.keys(): 1084 local_dico['run_mode'] = self['run_mode'] 1085 else: 1086 local_dico['run_mode'] = "" 1087 if not first: 1088 if ttype == 'reweight': 1089 template = sub_part_template_reweight 1090 else: 1091 template = sub_part_template_parton 1092 first = ttype 1093 if ttype=='parton' and self['cross_pythia']: 1094 local_dico['cross_span'] = 1 1095 local_dico['cross'] = self['cross'] 1096 local_dico['err'] = self['error'] 1097 local_dico['nb_event'] = self['nb_event'] 1098 if 'syst' in self.parton: 1099 local_dico['syst'] = '<font face=symbol>&#177;</font> <a href="./Events/%(run_name)s/%(tag)s_parton_syscalc.log">systematics</a>' \ 1100 % {'run_name':self['run_name'], 'tag': self['tag']} 1101 elif self['cross_pythia']: 1102 if self.parton: 1103 local_dico['cross_span'] = nb_line -1 1104 else: 1105 local_dico['cross_span'] = nb_line 1106 if self['nb_event_pythia']: 1107 local_dico['nb_event'] = self['nb_event_pythia'] 1108 else: 1109 local_dico['nb_event'] = 0 1110 local_dico['cross'] = self['cross_pythia'] 1111 local_dico['err'] = self['error_pythia'] 1112 if 'rwt' in self.pythia: 1113 local_dico['syst'] = '<font face=symbol>&#177;</font> <a href="./Events/%(run_name)s/%(tag)s_Pythia_syscalc.log">systematics</a>' \ 1114 % {'run_name':self['run_name'], 'tag': self['tag']} 1115 else: 1116 local_dico['type'] += ' %s' % self['run_mode'] 1117 local_dico['cross_span'] = nb_line 1118 local_dico['cross'] = self['cross'] 1119 local_dico['err'] = self['error'] 1120 local_dico['nb_event'] = self['nb_event'] 1121 if 'syst' in self.parton: 1122 local_dico['syst'] = '<font face=symbol>&#177;</font> <a href="./Events/%(run_name)s/%(tag)s_parton_syscalc.log">systematics</a>' \ 1123 % {'run_name':self['run_name'], 'tag': self['tag']} 1124 1125 elif ttype == 'pythia' and self['cross_pythia']: 1126 template = sub_part_template_parton 1127 if self.parton: 1128 local_dico['cross_span'] = nb_line - 1 1129 if self['nb_event_pythia']: 1130 local_dico['nb_event'] = self['nb_event_pythia'] 1131 else: 1132 local_dico['nb_event'] = 0 1133 else: 1134 local_dico['cross_span'] = nb_line 1135 local_dico['nb_event'] = self['nb_event'] 1136 if 'rwt' in self.pythia: 1137 local_dico['syst'] = '<font face=symbol>&#177;</font> <a href="./Events/%(run_name)s/%(tag)s_Pythia_syscalc.log">systematics</a>' \ 1138 % {'run_name':self['run_name'], 'tag': self['tag']} 1139 local_dico['cross'] = self['cross_pythia'] 1140 local_dico['err'] = self['error_pythia'] 1141 1142 elif ttype == 'shower': 1143 template = sub_part_template_shower 1144 if self.parton: 1145 local_dico['cross_span'] = nb_line - 1 1146 else: 1147 local_dico['cross_span'] = nb_line 1148 else: 1149 template = sub_part_template_pgs 1150 1151 # Fill the links 1152 local_dico['links'] = self.get_links(ttype) 1153 1154 # Fill the actions 1155 if ttype == 'parton': 1156 if runresults.web: 1157 local_dico['action'] = """ 1158 <FORM ACTION="http://%(web)s/cgi-bin/RunProcess/handle_runs-pl" ENCTYPE="multipart/form-data" METHOD="POST"> 1159 <INPUT TYPE=HIDDEN NAME=directory VALUE="%(me_dir)s"> 1160 <INPUT TYPE=HIDDEN NAME=whattodo VALUE="remove_level"> 1161 <INPUT TYPE=HIDDEN NAME=level VALUE="all"> 1162 <INPUT TYPE=HIDDEN NAME=tag VALUE=\"""" + self['tag'] + """\"> 1163 <INPUT TYPE=HIDDEN NAME=run VALUE="%(run_name)s"> 1164 <INPUT TYPE=SUBMIT VALUE="Remove run"> 1165 </FORM> 1166 1167 <FORM ACTION="http://%(web)s/cgi-bin/RunProcess/handle_runs-pl" ENCTYPE="multipart/form-data" METHOD="POST"> 1168 <INPUT TYPE=HIDDEN NAME=directory VALUE="%(me_dir)s"> 1169 <INPUT TYPE=HIDDEN NAME=whattodo VALUE="pythia"> 1170 <INPUT TYPE=HIDDEN NAME=run VALUE="%(run_name)s"> 1171 <INPUT TYPE=SUBMIT VALUE="Run Pythia"> 1172 </FORM>""" 1173 else: 1174 local_dico['action'] = self.command_suggestion_html('remove %s parton --tag=%s' \ 1175 % (self['run_name'], self['tag'])) 1176 # this the detector simulation and pythia should be available only for madevent 1177 if self['run_mode'] == 'madevent': 1178 local_dico['action'] += self.command_suggestion_html('pythia %s ' % self['run_name']) 1179 else: 1180 pass 1181 1182 elif ttype == 'shower': 1183 if runresults.web: 1184 local_dico['action'] = """ 1185 <FORM ACTION="http://%(web)s/cgi-bin/RunProcess/handle_runs-pl" ENCTYPE="multipart/form-data" METHOD="POST"> 1186 <INPUT TYPE=HIDDEN NAME=directory VALUE="%(me_dir)s"> 1187 <INPUT TYPE=HIDDEN NAME=whattodo VALUE="remove_level"> 1188 <INPUT TYPE=HIDDEN NAME=level VALUE="all"> 1189 <INPUT TYPE=HIDDEN NAME=tag VALUE=\"""" + self['tag'] + """\"> 1190 <INPUT TYPE=HIDDEN NAME=run VALUE="%(run_name)s"> 1191 <INPUT TYPE=SUBMIT VALUE="Remove run"> 1192 </FORM> 1193 1194 <FORM ACTION="http://%(web)s/cgi-bin/RunProcess/handle_runs-pl" ENCTYPE="multipart/form-data" METHOD="POST"> 1195 <INPUT TYPE=HIDDEN NAME=directory VALUE="%(me_dir)s"> 1196 <INPUT TYPE=HIDDEN NAME=whattodo VALUE="pythia"> 1197 <INPUT TYPE=HIDDEN NAME=run VALUE="%(run_name)s"> 1198 <INPUT TYPE=SUBMIT VALUE="Run Pythia"> 1199 </FORM>""" 1200 else: 1201 local_dico['action'] = self.command_suggestion_html('remove %s parton --tag=%s' \ 1202 % (self['run_name'], self['tag'])) 1203 # this the detector simulation and pythia should be available only for madevent 1204 if self['run_mode'] == 'madevent': 1205 local_dico['action'] += self.command_suggestion_html('pythia %s ' % self['run_name']) 1206 else: 1207 pass 1208 1209 elif ttype == 'pythia': 1210 if self['tag'] == runresults.get_last_pythia(): 1211 if runresults.web: 1212 local_dico['action'] = """ 1213 <FORM ACTION="http://%(web)s/cgi-bin/RunProcess/handle_runs-pl" ENCTYPE="multipart/form-data" METHOD="POST"> 1214 <INPUT TYPE=HIDDEN NAME=directory VALUE="%(me_dir)s"> 1215 <INPUT TYPE=HIDDEN NAME=whattodo VALUE="remove_level"> 1216 <INPUT TYPE=HIDDEN NAME=level VALUE="pythia"> 1217 <INPUT TYPE=HIDDEN NAME=run VALUE="%(run_name)s"> 1218 <INPUT TYPE=HIDDEN NAME=tag VALUE=\"""" + self['tag'] + """\"> 1219 <INPUT TYPE=SUBMIT VALUE="Remove pythia"> 1220 </FORM> 1221 1222 <FORM ACTION="http://%(web)s/cgi-bin/RunProcess/handle_runs-pl" ENCTYPE="multipart/form-data" METHOD="POST"> 1223 <INPUT TYPE=HIDDEN NAME=directory VALUE="%(me_dir)s"> 1224 <INPUT TYPE=HIDDEN NAME=whattodo VALUE="pgs"> 1225 <INPUT TYPE=HIDDEN NAME=run VALUE="%(run_name)s"> 1226 <INPUT TYPE=SUBMIT VALUE="Run Detector"> 1227 </FORM>""" 1228 else: 1229 local_dico['action'] = self.command_suggestion_html( 1230 'remove %s pythia --tag=%s' % \ 1231 (self['run_name'], self['tag'])) 1232 local_dico['action'] += self.command_suggestion_html( 1233 'pgs %(1)s or delphes %(1)s' % {'1': self['run_name']}) 1234 else: 1235 if runresults.web: 1236 local_dico['action'] = '' 1237 else: 1238 local_dico['action'] = self.command_suggestion_html('remove %s pythia --tag=%s'\ 1239 % (self['run_name'], self['tag'])) 1240 else: 1241 if runresults.web: 1242 local_dico['action'] = """ 1243 <FORM ACTION="http://%(web)s/cgi-bin/RunProcess/handle_runs-pl" ENCTYPE="multipart/form-data" METHOD="POST"> 1244 <INPUT TYPE=HIDDEN NAME=directory VALUE="%(me_dir)s"> 1245 <INPUT TYPE=HIDDEN NAME=whattodo VALUE="remove_level"> 1246 <INPUT TYPE=HIDDEN NAME=level VALUE=\"""" + str(type) + """\"> 1247 <INPUT TYPE=HIDDEN NAME=tag VALUE=\"""" + self['tag'] + """\"> 1248 <INPUT TYPE=HIDDEN NAME=run VALUE="%(run_name)s"> 1249 <INPUT TYPE=SUBMIT VALUE="Remove """ + str(ttype) + """\"> 1250 </FORM>""" 1251 else: 1252 local_dico['action'] = self.command_suggestion_html('remove %s %s --tag=%s' %\ 1253 (self['run_name'], ttype, self['tag'])) 1254 1255 # create the text 1256 subresults_html += template % local_dico 1257 1258 1259 if subresults_html == '': 1260 if runresults.web: 1261 action = """ 1262 <FORM ACTION="http://%(web)s/cgi-bin/RunProcess/handle_runs-pl" ENCTYPE="multipart/form-data" METHOD="POST"> 1263 <INPUT TYPE=HIDDEN NAME=directory VALUE="%(me_dir)s"> 1264 <INPUT TYPE=HIDDEN NAME=whattodo VALUE="remove_level"> 1265 <INPUT TYPE=HIDDEN NAME=level VALUE="banner"> 1266 <INPUT TYPE=HIDDEN NAME=tag VALUE=\"""" + self['tag'] + """\"> 1267 <INPUT TYPE=HIDDEN NAME=run VALUE="%(run_name)s"> 1268 <INPUT TYPE=SUBMIT VALUE="Remove Banner"> 1269 </FORM> 1270 1271 <FORM ACTION="http://%(web)s/cgi-bin/RunProcess/handle_runs-pl" ENCTYPE="multipart/form-data" METHOD="POST"> 1272 <INPUT TYPE=HIDDEN NAME=directory VALUE="%(me_dir)s"> 1273 <INPUT TYPE=HIDDEN NAME=whattodo VALUE="banner"> 1274 <INPUT TYPE=HIDDEN NAME=run VALUE="%(run_name)s"> 1275 <INPUT TYPE=SUBMIT VALUE="Run the banner"> 1276 </FORM>""" 1277 else: 1278 action = self.command_suggestion_html('remove %s banner --tag=%s' \ 1279 % (self['run_name'], self['tag'])) 1280 action += self.command_suggestion_html('banner_run %s ' % self['run_name']) 1281 1282 1283 1284 subresults_html = sub_part_template_parton % \ 1285 {'type': '', 1286 'run': self['run_name'], 1287 'cross_span': 1, 1288 'cross': self['cross'], 1289 'err': self['error'], 1290 'nb_event': self['nb_event'] and self['nb_event'] or 'No events yet', 1291 'links': 'banner only', 1292 'action': action, 1293 'run_mode': '', 1294 'syst':'' 1295 } 1296 1297 if self.debug is KeyboardInterrupt: 1298 debug = '<br><font color=red>Interrupted</font>' 1299 elif isinstance(self.debug, basestring): 1300 if not os.path.isabs(self.debug) and not self.debug.startswith('./'): 1301 self.debug = './' + self.debug 1302 elif os.path.isabs(self.debug): 1303 self.debug = os.path.relpath(self.debug, self.me_dir) 1304 debug = '<br> <a href=\'%s\'> <font color=red>ERROR</font></a>' \ 1305 % (self.debug) 1306 elif self.debug: 1307 text = str(self.debug).replace('. ','.<br>') 1308 if 'http' in text: 1309 pat = re.compile('(http[\S]*)') 1310 text = pat.sub(r'<a href=\1> here </a>', text) 1311 debug = '<br><font color=red>%s<BR>%s</font>' % \ 1312 (self.debug.__class__.__name__, text) 1313 else: 1314 debug = '' 1315 text = tag_template % {'tag_span': nb_line, 1316 'run': self['run_name'], 'tag': self['tag'], 1317 'subruns' : subresults_html, 1318 'debug':debug} 1319 1320 return text
1321 1322
1323 - def command_suggestion_html(self, command):
1324 """return html button with code suggestion""" 1325 1326 if command.startswith('pythia'): 1327 button = 'launch pythia' 1328 if command.startswith('shower'): 1329 button = 'shower events' 1330 elif command.startswith('remove banner'): 1331 button = 'remove banner' 1332 elif command.startswith('remove'): 1333 button = 'remove run' 1334 elif command.startswith('banner_run'): 1335 button = 're-run from the banner' 1336 else: 1337 button = 'launch detector simulation' 1338 if self['run_mode'] == 'madevent': 1339 header = 'Launch ./bin/madevent in a shell, and run the following command: ' 1340 else: 1341 header = 'Launch ./bin/aMCatNLO in a shell, and run the following command: ' 1342 1343 return "<INPUT TYPE=SUBMIT VALUE='%s' onClick=\"alert('%s')\">" % (button, header + command) 1344 1345 1346 return + '<br>'
1347