1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 from __future__ import division
16 import os
17 import math
18 import logging
19 import re
20 import xml.dom.minidom as minidom
21
22 logger = logging.getLogger('madevent.stdout')
23
24 pjoin = os.path.join
25 try:
26 import madgraph
27 except ImportError:
28 import internal.cluster as cluster
29 import internal.misc as misc
30 from internal import MadGraph5Error
31 else:
32 import madgraph.various.cluster as cluster
33 import madgraph.various.misc as misc
34 from madgraph import MadGraph5Error
35
37 """ A class to store statistics about a MadEvent run. """
38
40 """ Initialize the run dictionary. For now, the same as a regular
41 dictionary, except that we specify some default statistics. """
42
43 madloop_statistics = {
44 'unknown_stability' : 0,
45 'stable_points' : 0,
46 'unstable_points' : 0,
47 'exceptional_points' : 0,
48 'DP_usage' : 0,
49 'QP_usage' : 0,
50 'DP_init_usage' : 0,
51 'QP_init_usage' : 0,
52 'CutTools_DP_usage' : 0,
53 'CutTools_QP_usage' : 0,
54 'PJFry_usage' : 0,
55 'Golem_usage' : 0,
56 'IREGI_usage' : 0,
57 'max_precision' : 1.0e99,
58 'min_precision' : 0.0,
59 'averaged_timing' : 0.0,
60 'n_madloop_calls' : 0,
61 'cumulative_timing' : 0.0,
62 'skipped_subchannel' : 0
63
64 }
65
66 for key, value in madloop_statistics.items():
67 self[key] = value
68
69 super(dict,self).__init__(*args, **opts)
70
72 """ Update the current statitistics with the new_stats specified."""
73
74 if isinstance(new_stats,RunStatistics):
75 new_stats = [new_stats, ]
76 elif isinstance(new_stats,list):
77 if any(not isinstance(_,RunStatistics) for _ in new_stats):
78 raise MadGraph5Error, "The 'new_stats' argument of the function "+\
79 "'updtate_statistics' must be a (possibly list of) "+\
80 "RunStatistics instance."
81
82 keys = set([])
83 for stat in [self,]+new_stats:
84 keys |= set(stat.keys())
85
86 new_stats = new_stats+[self,]
87 for key in keys:
88
89 if key=='max_precision':
90
91 self[key] = min( _[key] for _ in new_stats if key in _)
92 elif key=='min_precision':
93
94 self[key] = max( _[key] for _ in new_stats if key in _)
95 elif key=='averaged_timing':
96 n_madloop_calls = sum(_['n_madloop_calls'] for _ in new_stats if
97 'n_madloop_calls' in _)
98 if n_madloop_calls > 0 :
99 self[key] = sum(_[key]*_['n_madloop_calls'] for _ in
100 new_stats if (key in _ and 'n_madloop_calls' in _) )/n_madloop_calls
101 else:
102
103 self[key] = sum(_[key] for _ in new_stats if key in _)
104
106 """ Load the statistics from an xml node. """
107
108 def getData(Node):
109 return Node.childNodes[0].data
110
111 u_return_code = xml_node.getElementsByTagName('u_return_code')
112 u_codes = [int(_) for _ in getData(u_return_code[0]).split(',')]
113 self['CutTools_DP_usage'] = u_codes[1]
114 self['PJFry_usage'] = u_codes[2]
115 self['IREGI_usage'] = u_codes[3]
116 self['Golem_usage'] = u_codes[4]
117 self['CutTools_QP_usage'] = u_codes[9]
118 t_return_code = xml_node.getElementsByTagName('t_return_code')
119 t_codes = [int(_) for _ in getData(t_return_code[0]).split(',')]
120 self['DP_usage'] = t_codes[1]
121 self['QP_usage'] = t_codes[2]
122 self['DP_init_usage'] = t_codes[3]
123 self['DP_init_usage'] = t_codes[4]
124 h_return_code = xml_node.getElementsByTagName('h_return_code')
125 h_codes = [int(_) for _ in getData(h_return_code[0]).split(',')]
126 self['unknown_stability'] = h_codes[1]
127 self['stable_points'] = h_codes[2]
128 self['unstable_points'] = h_codes[3]
129 self['exceptional_points'] = h_codes[4]
130 average_time = xml_node.getElementsByTagName('average_time')
131 avg_time = float(getData(average_time[0]))
132 self['averaged_timing'] = avg_time
133 cumulated_time = xml_node.getElementsByTagName('cumulated_time')
134 cumul_time = float(getData(cumulated_time[0]))
135 self['cumulative_timing'] = cumul_time
136 max_prec = xml_node.getElementsByTagName('max_prec')
137 max_prec = float(getData(max_prec[0]))
138
139 self['min_precision'] = max_prec
140 min_prec = xml_node.getElementsByTagName('min_prec')
141 min_prec = float(getData(min_prec[0]))
142
143 self['max_precision'] = min_prec
144 n_evals = xml_node.getElementsByTagName('n_evals')
145 n_evals = int(getData(n_evals[0]))
146 self['n_madloop_calls'] = n_evals
147
149 """Returns a one-line string summarizing the run statistics
150 gathered for the channel G."""
151
152
153
154 if self['n_madloop_calls']==0:
155 return ''
156
157 stability = [
158 ('tot#',self['n_madloop_calls']),
159 ('unkwn#',self['unknown_stability']),
160 ('UPS%',float(self['unstable_points'])/self['n_madloop_calls']),
161 ('EPS#',self['exceptional_points'])]
162
163 stability = [_ for _ in stability if _[1] > 0 or _[0] in ['UPS%','EPS#']]
164 stability = [(_[0],'%i'%_[1]) if isinstance(_[1], int) else
165 (_[0],'%.3g'%(100.0*_[1])) for _ in stability]
166
167 tools_used = [
168 ('CT_DP',float(self['CutTools_DP_usage'])/self['n_madloop_calls']),
169 ('CT_QP',float(self['CutTools_QP_usage'])/self['n_madloop_calls']),
170 ('PJFry',float(self['PJFry_usage'])/self['n_madloop_calls']),
171 ('Golem',float(self['Golem_usage'])/self['n_madloop_calls']),
172 ('IREGI',float(self['IREGI_usage'])/self['n_madloop_calls'])]
173
174 tools_used = [(_[0],'%.3g'%(100.0*_[1])) for _ in tools_used if _[1] > 0.0 ]
175
176 to_print = [('%s statistics:'%(G if isinstance(G,str) else
177 str(os.path.join(list(G))))\
178 +(' %s,'%misc.format_time(int(self['cumulative_timing'])) if
179 int(self['cumulative_timing']) > 0 else '')
180 +((' Avg. ML timing = %i ms'%int(1.0e3*self['averaged_timing'])) if
181 self['averaged_timing'] > 0.001 else
182 (' Avg. ML timing = %i mus'%int(1.0e6*self['averaged_timing']))) \
183 +', Min precision = %.2e'%self['min_precision'])
184 ,' -> Stability %s'%dict(stability)
185 ,' -> Red. tools usage in %% %s'%dict(tools_used)
186
187
188
189
190
191 ]
192
193 if self['skipped_subchannel'] > 0 and not no_warning:
194 to_print.append("WARNING: Some event with large weight have been "+\
195 "discarded. This happened %s times." % self['skipped_subchannel'])
196
197 return ('\n'.join(to_print)).replace("'"," ")
198
200 """return if any stat needs to be reported as a warning
201 When this is True, the print_warning doit retourner un warning
202 """
203
204 if self['n_madloop_calls'] > 0:
205 fraction = self['exceptional_points']/float(self['n_madloop_calls'])
206 else:
207 fraction = 0.0
208
209 if self['skipped_subchannel'] > 0:
210 return True
211 elif fraction > 1.0e-4:
212 return True
213 else:
214 return False
215
217 """get a string with all the identified warning"""
218
219 to_print = []
220 if self['skipped_subchannel'] > 0:
221 to_print.append("Some event with large weight have been discarded."+\
222 " This happens %s times." % self['skipped_subchannel'])
223 if self['n_madloop_calls'] > 0:
224 fraction = self['exceptional_points']/float(self['n_madloop_calls'])
225 if fraction > 1.0e-4:
226 to_print.append("Some PS with numerical instability have been set "+\
227 "to a zero matrix-element (%.3g%%)" % (100.0*fraction))
228
229 return ('\n'.join(to_print)).replace("'"," ")
230
232
234 """Initialize all data """
235
236 self.run_statistics = RunStatistics()
237 self.name = name
238 self.parent_name = ''
239 self.axsec = 0
240 self.xsec = 0
241 self.xerru = 0
242 self.xerrc = 0
243 self.nevents = 0
244 self.nw = 0
245 self.maxit = 0
246 self.nunwgt = 0
247 self.luminosity = 0
248 self.mfactor = 1
249 self.ysec_iter = []
250 self.yerr_iter = []
251 self.yasec_iter = []
252 self.eff_iter = []
253 self.maxwgt_iter = []
254 self.maxwgt = 0
255 self.th_maxwgt= 0
256
257 self.th_nunwgt = 0
258
259
260 return
261
262
264 """read results.dat and fullfill information"""
265
266 if isinstance(filepath, str):
267 finput = open(filepath)
268 elif isinstance(filepath, file):
269 finput = filepath
270 else:
271 raise Exception, "filepath should be a path or a file descriptor"
272
273 i=0
274 found_xsec_line = False
275 for line in finput:
276
277
278 if '<' in line:
279 break
280 i+=1
281 if i == 1:
282 def secure_float(d):
283 try:
284 return float(d)
285 except ValueError:
286 m=re.search(r'''([+-]?[\d.]*)([+-]\d*)''', d)
287 if m:
288 return float(m.group(1))*10**(float(m.group(2)))
289 return
290 data = [secure_float(d) for d in line.split()]
291 self.axsec, self.xerru, self.xerrc, self.nevents, self.nw,\
292 self.maxit, self.nunwgt, self.luminosity, self.wgt, \
293 self.xsec = data[:10]
294 if len(data) > 10:
295 self.maxwgt = data[10]
296 if len(data) >12:
297 self.th_maxwgt, self.th_nunwgt = data[11:13]
298 if self.mfactor > 1:
299 self.luminosity /= self.mfactor
300 continue
301 try:
302 l, sec, err, eff, maxwgt, asec = line.split()
303 found_xsec_line = True
304 except:
305 break
306 self.ysec_iter.append(secure_float(sec))
307 self.yerr_iter.append(secure_float(err))
308 self.yasec_iter.append(secure_float(asec))
309 self.eff_iter.append(secure_float(eff))
310 self.maxwgt_iter.append(secure_float(maxwgt))
311
312 finput.seek(0)
313 xml = []
314 for line in finput:
315 if re.match('^.*<.*>',line):
316 xml.append(line)
317 break
318 for line in finput:
319 xml.append(line)
320
321 if xml:
322 self.parse_xml_results('\n'.join(xml))
323
324
325 if self.nevents == 0 and self.nunwgt == 0 and isinstance(filepath, str) and \
326 os.path.exists(pjoin(os.path.split(filepath)[0], 'nevts')):
327 nevts = int(open(pjoin(os.path.split(filepath)[0], 'nevts')).read())
328 self.nevents = nevts
329 self.nunwgt = nevts
330
332 """ Parse the xml part of the results.dat file."""
333
334 dom = minidom.parseString(xml)
335
336 statistics_node = dom.getElementsByTagName("run_statistics")
337
338 if statistics_node:
339 try:
340 self.run_statistics.load_statistics(statistics_node[0])
341 except ValueError, IndexError:
342 logger.warning('Fail to read run statistics from results.dat')
343
345 self.mfactor = int(value)
346
348 """Change the number of iterations for this process"""
349
350 if len(self.ysec_iter) <= nb_iter:
351 return
352
353
354 nb_to_rm = len(self.ysec_iter) - nb_iter
355 ysec = [0]
356 yerr = [0]
357 for i in range(nb_to_rm):
358 ysec[0] += self.ysec_iter[i]
359 yerr[0] += self.yerr_iter[i]**2
360 ysec[0] /= (nb_to_rm+1)
361 yerr[0] = math.sqrt(yerr[0]) / (nb_to_rm + 1)
362
363 for i in range(1, nb_iter):
364 ysec[i] = self.ysec_iter[nb_to_rm + i]
365 yerr[i] = self.yerr_iter[nb_to_rm + i]
366
367 self.ysec_iter = ysec
368 self.yerr_iter = yerr
369
370 - def get(self, name):
371
372 if name in ['xsec', 'xerru','xerrc']:
373 return getattr(self, name) * self.mfactor
374 elif name in ['luminosity']:
375
376
377 return getattr(self, name)
378 elif (name == 'eff'):
379 return self.xerr*math.sqrt(self.nevents/(self.xsec+1e-99))
380 elif name == 'xerr':
381 return math.sqrt(self.xerru**2+self.xerrc**2)
382 elif name == 'name':
383 return pjoin(self.parent_name, self.name)
384 else:
385 return getattr(self, name)
386
388
393
395 """read the data in the file"""
396 try:
397 oneresult = OneResult(name)
398 oneresult.set_mfactor(mfactor)
399 oneresult.read_results(filepath)
400 oneresult.parent_name = self.name
401 self.append(oneresult)
402 return oneresult
403 except Exception:
404 logger.critical("Error when reading %s" % filepath)
405 raise
406
407
409 """compute the value associate to this combination"""
410
411 self.compute_iterations()
412 self.axsec = sum([one.axsec for one in self])
413 self.xsec = sum([one.xsec for one in self])
414 self.xerrc = sum([one.xerrc for one in self])
415 self.xerru = math.sqrt(sum([one.xerru**2 for one in self]))
416
417 self.nevents = sum([one.nevents for one in self])
418 self.nw = sum([one.nw for one in self])
419 self.maxit = len(self.yerr_iter)
420 self.nunwgt = sum([one.nunwgt for one in self])
421 self.wgt = 0
422 self.luminosity = min([0]+[one.luminosity for one in self])
423 if update_statistics:
424 self.run_statistics.aggregate_statistics([_.run_statistics for _ in self])
425
427 """compute the value associate to this combination"""
428
429 nbjobs = len(self)
430 if not nbjobs:
431 return
432 self.axsec = sum([one.axsec for one in self]) / nbjobs
433 self.xsec = sum([one.xsec for one in self]) /nbjobs
434 self.xerrc = sum([one.xerrc for one in self]) /nbjobs
435 self.xerru = math.sqrt(sum([one.xerru**2 for one in self])) /nbjobs
436
437 self.nevents = sum([one.nevents for one in self])
438 self.nw = 0
439 self.maxit = 0
440 self.nunwgt = sum([one.nunwgt for one in self])
441 self.wgt = 0
442 self.luminosity = sum([one.luminosity for one in self])
443 self.ysec_iter = []
444 self.yerr_iter = []
445 self.th_maxwgt = 0.0
446 self.th_nunwgt = 0
447 for result in self:
448 self.ysec_iter+=result.ysec_iter
449 self.yerr_iter+=result.yerr_iter
450 self.yasec_iter += result.yasec_iter
451 self.eff_iter += result.eff_iter
452 self.maxwgt_iter += result.maxwgt_iter
453
454
455
457 """Compute iterations to have a chi-square on the stability of the
458 integral"""
459
460 nb_iter = min([len(a.ysec_iter) for a in self], 0)
461
462 for oneresult in self:
463 oneresult.change_iterations_number(nb_iter)
464
465
466 for i in range(nb_iter):
467 value = [one.ysec_iter[i] for one in self]
468 error = [one.yerr_iter[i]**2 for one in self]
469
470
471 self.ysec_iter.append(sum(value))
472 self.yerr_iter.append(math.sqrt(sum(error)))
473
474
475 template_file = \
476 """
477 %(diagram_link)s
478 <BR>
479 <b>s= %(cross).5g ± %(error).3g (%(unit)s)</b><br><br>
480 <table class="sortable" id='tablesort'>
481 <tr><th>Graph</th>
482 <th> %(result_type)s</th>
483 <th>Error</th>
484 <th>Events (K)</th>
485 <th>Unwgt</th>
486 <th>Luminosity</th>
487 </tr>
488 %(table_lines)s
489 </table>
490 </center>
491 <br><br><br>
492 """
493 table_line_template = \
494 """
495 <tr><td align=right>%(P_title)s</td>
496 <td align=right><a id="%(P_link)s" href=%(P_link)s onClick="check_link('%(P_link)s','%(mod_P_link)s','%(P_link)s')"> %(cross)s </a> </td>
497 <td align=right> %(error)s</td>
498 <td align=right> %(events)s</td>
499 <td align=right> %(unweighted)s</td>
500 <td align=right> %(luminosity)s</td>
501 </tr>
502 """
503
504 - def get_html(self,run, unit, me_dir = []):
505 """write html output"""
506
507
508 P_grouping = {}
509
510 tables_line = ''
511 for oneresult in self:
512 if oneresult.name.startswith('P'):
513 title = '<a href=../../SubProcesses/%(P)s/diagrams.html>%(P)s</a>' \
514 % {'P':oneresult.name}
515 P = oneresult.name.split('_',1)[0]
516 if P in P_grouping:
517 P_grouping[P] += float(oneresult.xsec)
518 else:
519 P_grouping[P] = float(oneresult.xsec)
520 else:
521 title = oneresult.name
522
523 if not isinstance(oneresult, Combine_results):
524
525 if os.path.exists(pjoin(me_dir, 'Events', run, 'alllogs_1.html')):
526 link = '../../Events/%(R)s/alllogs_1.html#/%(P)s/%(G)s' % \
527 {'P': self.name,
528 'G': oneresult.name,
529 'R': run}
530 mod_link = link
531 elif os.path.exists(pjoin(me_dir, 'Events', run, 'alllogs_0.html')):
532 link = '../../Events/%(R)s/alllogs_0.html#/%(P)s/%(G)s' % \
533 {'P': self.name,
534 'G': oneresult.name,
535 'R': run}
536 mod_link = link
537 else:
538
539 link = '../../SubProcesses/%(P)s/%(G)s/%(R)s_log.txt' % \
540 {'P': self.name,
541 'G': oneresult.name,
542 'R': run}
543 mod_link = '../../SubProcesses/%(P)s/%(G)s/log.txt' % \
544 {'P': self.name,
545 'G': oneresult.name}
546 else:
547 link = '#%s' % oneresult.name
548 mod_link = link
549
550 dico = {'P_title': title,
551 'P_link': link,
552 'mod_P_link': mod_link,
553 'cross': '%.4g' % oneresult.xsec,
554 'error': '%.3g' % oneresult.xerru,
555 'events': oneresult.nevents/1000.0,
556 'unweighted': oneresult.nunwgt,
557 'luminosity': '%.3g' % oneresult.luminosity
558 }
559
560 tables_line += self.table_line_template % dico
561
562 for P_name, cross in P_grouping.items():
563 dico = {'P_title': '%s sum' % P_name,
564 'P_link': './results.html',
565 'mod_P_link':'',
566 'cross': cross,
567 'error': '',
568 'events': '',
569 'unweighted': '',
570 'luminosity': ''
571 }
572 tables_line += self.table_line_template % dico
573
574 if self.name.startswith('P'):
575 title = '<dt><a name=%(P)s href=../../SubProcesses/%(P)s/diagrams.html>%(P)s</a></dt><dd>' \
576 % {'P':self.name}
577 else:
578 title = ''
579
580 dico = {'cross': self.xsec,
581 'abscross': self.axsec,
582 'error': self.xerru,
583 'unit': unit,
584 'result_type': 'Cross-Section',
585 'table_lines': tables_line,
586 'diagram_link': title
587 }
588
589 html_text = self.template_file % dico
590 return html_text
591
593 """write a correctly formatted results.dat"""
594
595 def fstr(nb):
596 data = '%E' % nb
597 if data == 'NAN':
598 nb, power = 0,0
599 else:
600 nb, power = data.split('E')
601 nb = float(nb) /10
602 power = int(power) + 1
603 return '%.5fE%+03i' %(nb,power)
604
605 line = '%s %s %s %i %i %i %i %s %s %s %s %s %i\n' % (fstr(self.axsec), fstr(self.xerru),
606 fstr(self.xerrc), self.nevents, self.nw, self.maxit, self.nunwgt,
607 fstr(self.luminosity), fstr(self.wgt), fstr(self.xsec), fstr(self.maxwgt),
608 fstr(self.th_maxwgt), self.th_nunwgt)
609 fsock = open(output_path,'w')
610 fsock.writelines(line)
611 for i in range(len(self.ysec_iter)):
612 line = '%s %s %s %s %s %s\n' % (i+1, self.ysec_iter[i], self.yerr_iter[i],
613 self.eff_iter[i], self.maxwgt_iter[i], self.yasec_iter[i])
614 fsock.writelines(line)
615
616
617
618 results_header = """
619 <head>
620 <title>Process results</title>
621 <script type="text/javascript" src="../sortable.js"></script>
622 <link rel=stylesheet href="../mgstyle.css" type="text/css">
623 </head>
624 <body>
625 <script type="text/javascript">
626 function UrlExists(url) {
627 var http = new XMLHttpRequest();
628 http.open('HEAD', url, false);
629 try{
630 http.send()
631 }
632 catch(err){
633 return 1==2;
634 }
635 return http.status!=404;
636 }
637 function check_link(url,alt, id){
638 var obj = document.getElementById(id);
639 if ( ! UrlExists(url)){
640 if ( ! UrlExists(alt)){
641 obj.href = alt;
642 return true;
643 }
644 obj.href = alt;
645 return false;
646 }
647 obj.href = url;
648 return 1==1;
649 }
650 </script>
651 """
652
654 """ """
655
656 run = cmd.results.current['run_name']
657 all = Combine_results(run)
658
659 for Pdir in open(pjoin(cmd.me_dir, 'SubProcesses','subproc.mg')):
660 Pdir = Pdir.strip()
661 P_comb = Combine_results(Pdir)
662
663 P_path = pjoin(cmd.me_dir, 'SubProcesses', Pdir)
664 G_dir = [G for G in os.listdir(P_path) if G.startswith('G') and
665 os.path.isdir(pjoin(P_path,G))]
666
667 for line in open(pjoin(P_path, 'symfact.dat')):
668 name, mfactor = line.split()
669 if float(mfactor) < 0:
670 continue
671 if os.path.exists(pjoin(P_path, 'ajob.no_ps.log')):
672 continue
673
674 if not folder_names:
675 name = 'G' + name
676 P_comb.add_results(name, pjoin(P_path,name,'results.dat'), mfactor)
677 else:
678 for folder in folder_names:
679 if 'G' in folder:
680 dir = folder.replace('*', name)
681 else:
682 dir = folder.replace('*', '_G' + name)
683 P_comb.add_results(dir, pjoin(P_path,dir,'results.dat'), mfactor)
684 P_comb.compute_values()
685 all.append(P_comb)
686 all.compute_values()
687 return all
688
689
691 """ folder_names has been added for the amcatnlo runs """
692 run = cmd.results.current['run_name']
693 if not os.path.exists(pjoin(cmd.me_dir, 'HTML', run)):
694 os.mkdir(pjoin(cmd.me_dir, 'HTML', run))
695
696 unit = cmd.results.unit
697 P_text = ""
698 Presults = collect_result(cmd, folder_names=folder_names)
699
700
701 for P_comb in Presults:
702 P_text += P_comb.get_html(run, unit, cmd.me_dir)
703 P_comb.compute_values()
704 P_comb.write_results_dat(pjoin(cmd.me_dir, 'SubProcesses', P_comb.name,
705 '%s_results.dat' % run))
706
707
708 Presults.write_results_dat(pjoin(cmd.me_dir,'SubProcesses', 'results.dat'))
709
710 fsock = open(pjoin(cmd.me_dir, 'HTML', run, 'results.html'),'w')
711 fsock.write(results_header)
712 fsock.write('%s <dl>' % Presults.get_html(run, unit, cmd.me_dir))
713 fsock.write('%s </dl></body>' % P_text)
714
715 return Presults.xsec, Presults.xerru
716