~maddevelopers/mg5amcnlo/3.0.2-alpha0-merged3.2.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#! /usr/bin/env python
################################################################################
#
# Copyright (c) 2009 The MadGraph5_aMC@NLO Development team and Contributors
#
# This file is a part of the MadGraph5_aMC@NLO project, an application which 
# automatically generates Feynman diagrams and matrix elements for arbitrary
# high-energy processes in the Standard Model and beyond.
#
# It is subject to the MadGraph5_aMC@NLO license which should accompany this 
# distribution.
#
# For more information, visit madgraph.phys.ucl.ac.be and amcatnlo.web.cern.ch
#
################################################################################

from __future__ import absolute_import
import string
import os
import re
import sys

import madgraph.various.misc as misc
from six.moves import range

template_text= string.Template("""
<HTML> 
<HEAD> 
<TITLE>Detail on the Generation</TITLE> 
<META $meta ></HEAD> 

<style type="text/css">

table.processes { border-collapse: collapse;
                  border: solid}

.processes td {
padding: 2 5 2 5;
border: solid thin;
}

th{
border-top: solid;
border-bottom: solid;
}

.first td{
border-top: solid;
}




</style>

<BODY> 
    <P> <H2 ALIGN=CENTER> SubProcesses and Feynman diagrams </H2>
    
    <TABLE BORDER=2 ALIGN=CENTER class=processes> 
        <TR>
           <TH>Directory</TH> 
           <TH NOWRAP># Diagrams </TH>
           <TH NOWRAP># Subprocesses </TH>
           <TH>FEYNMAN DIAGRAMS</TH> 
           <TH> SUBPROCESS </TH>
        </TR> 
        $info_lines
    </TABLE><BR> 
    <CENTER> $nb_diag diagrams ($nb_gen_diag independent).</CENTER>
    <br><br><br>
    <TABLE ALIGN=CENTER>
    $log
    <TR> 
        <TD ALIGN=CENTER> <A HREF="../Cards/proc_card_mg5.dat">proc_card_mg5.dat</A> </TD>
        <TD> Input file used for code generation.
    $model_info
    </TABLE><br>
    <center>
    <H3>Back to <A HREF="../index.html">Process main page</A></H3>
    </center>
 </BODY> 

</HTML>""")


template_text_nlo= string.Template("""
<HTML> 
<HEAD> 
<TITLE>Detail on the Generation</TITLE> 
<META $meta ></HEAD> 

<style type="text/css">

table.processes { border-collapse: collapse;
                  border: solid}

.processes td {
padding: 2 5 2 5;
border: solid thin;
}

th{
border-top: solid;
border-bottom: solid;
}

.first td{
border-top: solid;
}




</style>

<BODY> 
    <P> <H2 ALIGN=CENTER> SubProcesses and Feynman diagrams </H2>
    
    <TABLE BORDER=2 ALIGN=CENTER class=processes> 
        <TR>
           <TH>Directory</TH> 
           <TH NOWRAP>Type</TH>
           <TH NOWRAP># Diagrams </TH>
           <TH NOWRAP># Subprocesses </TH>
           <TH>FEYNMAN DIAGRAMS</TH> 
           <TH> SUBPROCESS </TH>
        </TR> 
        $info_lines
    </TABLE><BR> 
    <CENTER> $nb_diag diagrams ($nb_gen_diag independent).</CENTER>
    <br><br><br>
    <TABLE ALIGN=CENTER>
    $log
    <TR> 
        <TD ALIGN=CENTER> <A HREF="../Cards/proc_card_mg5.dat">proc_card_mg5.dat</A> </TD>
        <TD> Input file used for code generation.
    $model_info
    </TABLE><br>
    <center>
    <H3>Back to <A HREF="../index.html">Process main page</A></H3>
    </center>
 </BODY> 

</HTML>""")


class make_info_html:

    def __init__(self, cur_dir='./'):

        self.dir = cur_dir
        
        
        self.rep_rule = {'nb_diag': 0, 'nb_gen_diag': 0}
        
        self.define_meta()
        self.rep_rule['info_lines'] = self.define_info_tables()
        self.rep_rule['model_info']= self.give_model_info()
        self.rep_rule['log'] = self.check_log() 
        self.write()
        
        
    def give_model_info(self):
        """find path for the model"""
        
        path = os.path.join(self.dir, 'Source','MODEL','particles.dat')
        if os.path.exists(path):
            return """<TR> 
        <TD ALIGN=CENTER> <A HREF="../Source/MODEL/particles.dat">particles</A></TD> 
        <TD> Particles file used for code generation.</TD>
    </TR>
    <TR> 
        <TD ALIGN=CENTER> <A HREF="../Source/MODEL/interactions.dat">interactions</A></TD> 
        <TD> Interactions file used for code generation.</TD>
    </TR>"""
        else:
            return ''
        
        
    def define_meta(self):
        """add the meta in the replacement rule"""
        
        if os.path.exists(os.path.join(self.dir,'SubProcesses','done')):
            self.rep_rule['meta'] = 'HTTP-EQUIV=\"REFRESH\" CONTENT=\"30\"'
        else:
            self.rep_rule['meta'] = "HTTP-EQUIV=\"EXPIRES\" CONTENT=\"20\""
        

    def define_info_tables(self):
        """define the information table"""
        
        line_template = string.Template("""
        <TR class=$class> $first 
<TD> $diag </TD> 
<TD> $subproc </TD> 
<TD> <A HREF="../SubProcesses/$processdir/diagrams.html#$id" >html</A> $postscript
</TD><TD class=$class>
<SPAN style="white-space: nowrap;"> $subprocesslist</SPAN>
</TD></TR>""")
        
        #output text
        text = ''
        # list of valid P directory
        subproc = [content for content in os.listdir(os.path.join(self.dir,'SubProcesses'))
                                if content.startswith('P') and 
                                os.path.isdir(os.path.join(self.dir,'SubProcesses',content))
                                and os.path.exists(os.path.join(self.dir,'SubProcesses',content,'auto_dsig.f'))]
        
        for proc in subproc:
            
            idnames = self.get_subprocesses_info(proc)
               
            for id in range(1,len(idnames)+1):

                if id == 1:
                    
                    line_dict = {'processdir': proc,
                                 'class': 'first'}
                    line_dict['first']= '<TD class=$class rowspan=%s> %s </TD>' % (len(idnames), proc)
                else:
                    line_dict = {'processdir': proc,
                                 'class': 'second'}
                    line_dict['first'] = ''
                try:
                    names = idnames[id]
                except Exception:
                    names = idnames['']
                    id = ''
                line_dict['id'] = str(id)     
                line_dict['diag'] = self.get_diagram_nb(proc, id)
                line_dict['subproc'] = sum([len(data) for data in names])
                self.rep_rule['nb_diag'] += line_dict['diag'] * line_dict['subproc']
                self.rep_rule['nb_gen_diag'] += line_dict['diag']
                line_dict['subprocesslist'] = ', <br>'.join([' </SPAN> , <SPAN style="white-space: nowrap;"> '.join(info) for info in names])
                line_dict['postscript'] = self.check_postcript(proc, id)
                
                text += line_template.substitute(line_dict)
        return text
    
    def get_diagram_nb(self, proc, id):
        
        path = os.path.join(self.dir, 'SubProcesses', proc, 'matrix%s.f' % id)
        nb_diag = 0
                
        pat = re.compile(r'''Amplitude\(s\) for diagram number (\d+)''' )
        if not os.path.exists(path):
            path = os.path.join(self.dir, 'SubProcesses', proc, 'matrix%s_orig.f' % id)
        text = open(path).read()
        for match in re.finditer(pat, text):
            pass
        nb_diag += int(match.groups()[0])
        
        return nb_diag
            
            
    def get_subprocesses_info(self, proc):
        """ return the list of processes with their name"""    
        
        #no python information available (should not happen anymore)
        path = os.path.join(self.dir, 'SubProcesses', proc)        
        nb_sub = 0
        names = {}
        old_main = ''
        
        if not os.path.exists(os.path.join(path,'processes.dat')):
            return self.get_subprocess_info_v4(proc)
        
        for line in open(os.path.join(path,'processes.dat')):
            main = line[:8].strip()
            if main == 'mirror':
                main = old_main
                if line[8:].strip() == 'none':
                    continue 
            else:
                main = int(main)
                old_main = main

            sub_proccess = line[8:]
            nb_sub += sub_proccess.count(',') + 1
            if main in names:
                names[main] += [sub_proccess.split(',')]
            else: 
                names[main]= [sub_proccess.split(',')]
    

        return names

    def get_subprocess_info_v4(self, proc):
        """ return the list of processes with their name in case without grouping """
        
        nb_sub = 0
        names = {'':[[]]}
        path = os.path.join(self.dir, 'SubProcesses', proc,'auto_dsig.f')
        found = 0
        for line in open(path):
            if line.startswith('C     Process:'):
                found += 1
                names[''][0].append(line[15:])
            elif found >1:
                break    
        return names    
    
    def check_postcript(self, proc, id):
        """ check if matrix.ps is defined """
        path = os.path.join(self.dir, 'SubProcesses', proc,'matrix%s.f' % id) 
        if os.path.exists(path):
            return "<A HREF=\"../SubProcesses/%s/matrix%s.ps\" >postscript </A>" % \
                    (proc, id)
        else:
            return ''

    def check_log(self):
        path = os.path.join(self.dir, 'proc_log.txt') 
        if os.path.exists(path):
            return """<TR> 
        <TD ALIGN=CENTER> <A HREF="../proc_log.txt">proc_log.txt</A> </TD>
        <TD> Log file from MadGraph code generation. </TD>
        </TR>"""
        else:
            return ''
        
    def write(self):
        """write the info.html file"""
        
        fsock = open(os.path.join(self.dir,'HTML','info.html'),'w')
        text = template_text.substitute(self.rep_rule)
        fsock.write(text)

    

class make_info_html_nlo(make_info_html):


    def define_info_tables(self):
        """define the information table"""
        
        # we do not export the diagrams.html so far
        line_template = string.Template("""
        <TR class=$class> $first 
<TD> $type </TD>
<TD> $diag </TD> 
<TD> $subproc </TD> 
<TD>$postscript </TD>
<TD class=$class>
<SPAN style="white-space: nowrap;"> $subprocesslist</SPAN>
</TD></TR>""")
        

        #output text
        text = ''
        # list of valid P directory
        subproc = [content for content in os.listdir(os.path.join(self.dir,'SubProcesses'))
                                if content.startswith('P') and 
                                os.path.isdir(os.path.join(self.dir,'SubProcesses',content))
                                and os.path.islink(os.path.join(self.dir,'SubProcesses',content,'fks_singular.f'))]
        
        for proc in subproc:
            files_dict = {'born': ['born.f'], 
                          'virt': [os.path.join('V' + proc[1:], 'loop_matrix.f')],
                          'real': [file for file in os.listdir(os.path.join(self.dir,'SubProcesses', proc)) if 
                                   file.startswith('matrix_') and file.endswith('.f')]}
            
            for type in ['born', 'virt', 'real']:
                for file in files_dict[type]:
                    idnames = self.get_subprocesses_info_from_file(proc, file)
               
                    for id in range(1,len(idnames)+1):

                        if type == 'born':
                            line_dict = {'processdir': proc,
                                         'class': 'first'}
                            line_dict['first']= '<TD class=$class rowspan=%s> %s </TD>' % (len(idnames), proc)
                        else:
                            line_dict = {'processdir': 'proc',
                                         'class': 'second'}
                            line_dict['first'] = '<TD class=$class rowspan=%s>  </TD>' % (len(idnames))
                        try:
                            names = idnames[id]
                        except Exception:
                            names = idnames['']
                            id = ''
                        line_dict['type'] = type
                        line_dict['id'] = str(id)     
                        line_dict['diag'] = self.get_diagram_nb_from_file(proc, file.replace('.f', '.ps'))
                        line_dict['subproc'] = sum([len(data) for data in names])
                        self.rep_rule['nb_diag'] += line_dict['diag'] * line_dict['subproc']
                        self.rep_rule['nb_gen_diag'] += line_dict['diag']
                        line_dict['subprocesslist'] = ', <br>'.join([' </SPAN> , <SPAN style="white-space: nowrap;"> '.join(info) for info in names])
                        line_dict['postscript'] = self.check_postcript_from_file(proc, file)
                       
                        text += line_template.substitute(line_dict)
        return text


    def get_subprocesses_info_from_file(self, proc, filename):
        """ return the list of processes with their name in case without grouping
         type can be 0 for born, i > 0 for ith real and -1 for virtual"""

        nb_sub = 0
        names = {'':[[]]}
        path = os.path.join(self.dir, 'SubProcesses', proc, filename)
        if not os.path.exists(path):
            return []
        found = 0
        start= 0
        for line in open(path):
            if line.startswith('C     Process:'):
                found += 1
                names[''][0].append(line[15:-1])
                start =1
            elif found >0 and 'IMPLICIT NONE' in line:
                break    
            elif start:
                names[''][0][-1] += line[2:-1].strip()
        return names    


    def get_diagram_nb_from_file(self, proc, filename):
        
        path = os.path.join(self.dir, 'SubProcesses', proc, filename)
        nb_diag = 0
                
        pat = re.compile(r'''diagram (\d+)''' )
       
        text = open(path).read()
        for match in re.finditer(pat, text):
            pass
        try:
            nb_diag += int(match.groups()[0])
        except Exception:
            pass
        
        return nb_diag


    def check_postcript_from_file(self, proc, filename):
        """ check if matrix.ps is defined """
        psname = filename[:-1] + 'ps'
        path = os.path.join(self.dir, 'SubProcesses', proc, psname) 
        if os.path.exists(path):
            return "<A HREF=\"../SubProcesses/%s/%s\" >postscript </A>" % \
                    (proc, psname)
        else:
            return ''
            

    def write(self):
        """write the info.html file"""
        
        fsock = open(os.path.join(self.dir,'HTML','info.html'),'w')
        text = template_text_nlo.substitute(self.rep_rule)
        fsock.write(text)