~maddevelopers/mg5amcnlo/2.6.3_rwgt

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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
################################################################################
#
# 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
#
################################################################################
""" How to import a UFO model to the MG5 format """


import fractions
import logging
import os
import re
import sys
import time


from madgraph import MadGraph5Error, MG5DIR, ReadWrite
import madgraph.core.base_objects as base_objects
import madgraph.loop.loop_base_objects as loop_base_objects
import madgraph.core.color_algebra as color
import madgraph.iolibs.files as files
import madgraph.iolibs.save_load_object as save_load_object
from madgraph.core.color_algebra import *
import madgraph.various.misc as misc
import madgraph.iolibs.ufo_expression_parsers as parsers

import aloha
import aloha.create_aloha as create_aloha
import aloha.aloha_fct as aloha_fct

import models as ufomodels
import models.model_reader as model_reader
logger = logging.getLogger('madgraph.model')
logger_mod = logging.getLogger('madgraph.model')

root_path = os.path.dirname(os.path.realpath( __file__ ))
sys.path.append(root_path)

sys.path.append(os.path.join(root_path, os.path.pardir, 'Template', 'bin', 'internal'))
import check_param_card 

pjoin = os.path.join

# Suffixes to employ for the various poles of CTparameters
pole_dict = {-2:'2EPS',-1:'1EPS',0:'FIN'}

class UFOImportError(MadGraph5Error):
    """ a error class for wrong import of UFO model""" 

class InvalidModel(MadGraph5Error):
    """ a class for invalid Model """

last_model_path =''
def find_ufo_path(model_name, web_search=True):
    """ find the path to a model """

    global last_model_path

    # Check for a valid directory
    if model_name.startswith(('./','../')) and os.path.isdir(model_name):
        return model_name
    elif os.path.isdir(os.path.join(MG5DIR, 'models', model_name)):
        return os.path.join(MG5DIR, 'models', model_name)
    elif 'PYTHONPATH' in os.environ:
        for p in os.environ['PYTHONPATH'].split(':'):
            if os.path.isdir(os.path.join(MG5DIR, p, model_name)):
                if last_model_path != os.path.join(MG5DIR, p, model_name):
                    logger.info("model loaded from PYTHONPATH: %s", os.path.join(MG5DIR, p, model_name))
                    last_model_path = os.path.join(MG5DIR, p, model_name)
                return os.path.join(MG5DIR, p, model_name)
    if os.path.isdir(model_name):
        logger.warning('No model %s found in default path. Did you mean \'import model ./%s\'',
                       model_name, model_name)
        if os.path.sep in model_name:
            raise UFOImportError("Path %s is not a valid pathname" % model_name)    
    elif web_search and '-' not in model_name:
        found = import_model_from_db(model_name)
        if found:
            return find_ufo_path(model_name, web_search=False)
        else:
            raise UFOImportError("Path %s is not a valid pathname" % model_name)    
    else:
        raise UFOImportError("Path %s is not a valid pathname" % model_name)    
    
    raise UFOImportError("Path %s is not a valid pathname" % model_name)
    return


def get_model_db():
    """return the file with the online model database"""

    data_path = ['http://madgraph.phys.ucl.ac.be/models_db.dat',
                     'http://madgraph.physics.illinois.edu/models_db.dat']
    import random
    import urllib
    r = random.randint(0,1)
    r = [r, (1-r)]

    for index in r:
        cluster_path = data_path[index]
        try:
            data = urllib.urlopen(cluster_path)
        except Exception:
            continue
        break
    else:
        raise MadGraph5Error, '''Model not found locally and Impossible to connect any of us servers.
        Please check your internet connection or retry later'''
    return data

def import_model_from_db(model_name, local_dir=False):
    """ import the model with a given name """

    data =get_model_db()
    link = None
    for line in data:
        split = line.split()
        if model_name == split[0]:
            link = split[1]
            break
    else:
        logger.debug('no model with that name found online')
        return False
    
    #get target directory
    # 1. PYTHONPATH containing UFO
    # 2. models directory
    target = None 
    if 'PYTHONPATH' in os.environ and not local_dir:
        for directory in os.environ['PYTHONPATH'].split(':'):
            if 'UFO' in os.path.basename(directory) and os.path.exists(directory):
                target= directory 
    if target is None:
        target = pjoin(MG5DIR, 'models')    
    try:
        os.remove(pjoin(target, 'tmp.tgz'))
    except Exception:
        pass
    logger.info("download model from %s to the following directory: %s", link, target, '$MG:color:BLACK')
    misc.wget(link, 'tmp.tgz', cwd=target)

    #untar the file.
    # .tgz
    if link.endswith(('.tgz','.tar.gz','.tar')):
        try:
            proc = misc.call('tar -xzpvf tmp.tgz', shell=True, cwd=target)#, stdout=devnull, stderr=devnull)
            if proc: raise Exception
        except:
            proc = misc.call('tar -xpvf tmp.tgz', shell=True, cwd=target)#, stdout=devnull, stderr=devnull)
    # .zip
    if link.endswith(('.zip')):
        try:
            proc = misc.call('unzip tmp.tgz', shell=True, cwd=target)#, stdout=devnull, stderr=devnull)
            if proc: raise Exception
        except:
            proc = misc.call('tar -xzpvf tmp.tgz', shell=True, cwd=target)#, stdout=devnull, stderr=devnull)
    if proc:
        raise Exception, "Impossible to unpack the model. Please install it manually"
    return True

def import_model(model_name, decay=False, restrict=True, prefix='mdl_',
                                                    complex_mass_scheme = None):
    """ a practical and efficient way to import a model"""
    
    # check if this is a valid path or if this include restriction file       
    try:
        model_path = find_ufo_path(model_name)
    except UFOImportError:
        if '-' not in model_name:
            if model_name == "mssm":
                logger.error("mssm model has been replaced by MSSM_SLHA2 model.\n The new model require SLHA2 format. You can use the \"update to_slha2\" command to convert your slha1 param_card.\n That option is available at the time of the edition of the cards.")
            raise
        split = model_name.split('-')
        model_name = '-'.join([text for text in split[:-1]])
        try:
            model_path = find_ufo_path(model_name)
        except UFOImportError:
            if model_name == "mssm":
                logger.error("mssm model has been replaced by MSSM_SLHA2 model.\n The new model require SLHA2 format. You can use the \"update to_slha2\" command to convert your slha1 param_card.\n That option is available at the time of the edition of the cards.")
            raise
        restrict_name = split[-1]
         
        restrict_file = os.path.join(model_path, 'restrict_%s.dat'% restrict_name)
        
        #if restriction is full, then we by pass restriction (avoid default)
        if split[-1] == 'full':
            restrict_file = None
    else:
        # Check if by default we need some restrictions
        restrict_name = ""
        if restrict and os.path.exists(os.path.join(model_path,'restrict_default.dat')):
            restrict_file = os.path.join(model_path,'restrict_default.dat')
        else:
            restrict_file = None

        if isinstance(restrict, str):
            if os.path.exists(os.path.join(model_path, restrict)):
                restrict_file = os.path.join(model_path, restrict)
            elif os.path.exists(restrict):
                restrict_file = restrict
            else:
                raise Exception, "%s is not a valid path for restrict file" % restrict
    
    #import the FULL model
    model = import_full_model(model_path, decay, prefix)

    if os.path.exists(pjoin(model_path, "README")):
        logger.info("Please read carefully the README of the model file for instructions/restrictions of the model.",'$MG:color:BLACK') 
    # restore the model name
    if restrict_name:
        model["name"] += '-' + restrict_name
    
    # Decide whether complex mass scheme is on or not
    useCMS = (complex_mass_scheme is None and aloha.complex_mass) or \
                                                      complex_mass_scheme==True
    #restrict it if needed       
    if restrict_file:
        try:
            logger.info('Restrict model %s with file %s .' % (model_name, os.path.relpath(restrict_file)))
        except OSError:
            # sometimes has trouble with relative path
            logger.info('Restrict model %s with file %s .' % (model_name, restrict_file))
            
        if logger_mod.getEffectiveLevel() > 10:
            logger.info('Run \"set stdout_level DEBUG\" before import for more information.')
        # Modify the mother class of the object in order to allow restriction
        model = RestrictModel(model)

        # Change to complex mass scheme if necessary. This must be done BEFORE
        # the restriction.
        if useCMS:
            # We read the param_card a first time so that the function 
            # change_mass_to_complex_scheme can know if a particle is to
            # be considered massive or not and with zero width or not.
            # So we read the restrict card a first time, with the CMS set to
            # False because we haven't changed the model yet.
            model.set_parameters_and_couplings(param_card = restrict_file,
                                                      complex_mass_scheme=False)
            model.change_mass_to_complex_scheme(toCMS=True)
        else:
            # Make sure that the parameter 'CMSParam' of the model is set to 0.0
            # as it should in order to have the correct NWA renormalization condition.
            # It might be that the default of the model is CMS.
            model.change_mass_to_complex_scheme(toCMS=False)

        blocks = model.get_param_block()
        if model_name == 'mssm' or os.path.basename(model_name) == 'mssm':
            keep_external=True
        elif all( b in blocks for b in ['USQMIX', 'SL2', 'MSOFT', 'YE', 'NMIX', 'TU','MSE2','UPMNS']):
            keep_external=True
        elif model_name == 'MSSM_SLHA2' or os.path.basename(model_name) == 'MSSM_SLHA2':
            keep_external=True            
        else:
            keep_external=False
        if keep_external:
            logger.info('Detect SLHA2 format. keeping restricted parameter in the param_card')
            
        model.restrict_model(restrict_file, rm_parameter=not decay,
           keep_external=keep_external, complex_mass_scheme=complex_mass_scheme)
        model.path = model_path
    else:
        # Change to complex mass scheme if necessary
        if useCMS:
            model.change_mass_to_complex_scheme(toCMS=True)
        else:
            # It might be that the default of the model (i.e. 'CMSParam') is CMS.
            model.change_mass_to_complex_scheme(toCMS=False)
      
    return model
    

_import_once = []
def import_full_model(model_path, decay=False, prefix=''):
    """ a practical and efficient way to import one of those models 
        (no restriction file use)"""

    assert model_path == find_ufo_path(model_path)
    
    if prefix is True:
        prefix='mdl_'
        
    # Check the validity of the model
    files_list_prov = ['couplings.py','lorentz.py','parameters.py',
                       'particles.py', 'vertices.py', 'function_library.py',
                       'propagators.py', 'coupling_orders.py']
    
    if decay:
        files_list_prov.append('decays.py')    
    
    files_list = []
    for filename in files_list_prov:
        filepath = os.path.join(model_path, filename)
        if not os.path.isfile(filepath):
            if filename not in ['propagators.py', 'decays.py', 'coupling_orders.py']:
                raise UFOImportError,  "%s directory is not a valid UFO model: \n %s is missing" % \
                                                         (model_path, filename)
        files_list.append(filepath)
    # use pickle files if defined and up-to-date
    if aloha.unitary_gauge: 
        pickle_name = 'model.pkl'
    else:
        pickle_name = 'model_Feynman.pkl'
    if decay:
        pickle_name = 'dec_%s' % pickle_name
    
    allow_reload = False
    if files.is_uptodate(os.path.join(model_path, pickle_name), files_list):
        allow_reload = True
        try:
            model = save_load_object.load_from_file( \
                                          os.path.join(model_path, pickle_name))
        except Exception, error:
            logger.info('failed to load model from pickle file. Try importing UFO from File')
        else:
            # We don't care about the restrict_card for this comparison
            if model.has_key('version_tag') and not model.get('version_tag') is None and \
                model.get('version_tag').startswith(os.path.realpath(model_path)) and \
                model.get('version_tag').endswith('##' + str(misc.get_pkg_info())):
                #check if the prefix is correct one.
                for key in model.get('parameters'):
                    for param in model['parameters'][key]:
                        value = param.name.lower()
                        if value in ['as','mu_r', 'zero','aewm1']:
                            continue
                        if prefix:
                            if value.startswith(prefix):
                                _import_once.append((model_path, aloha.unitary_gauge, prefix, decay))
                                return model
                            else:
                                logger.info('reload from .py file')
                                break
                        else:
                            if value.startswith('mdl_'):
                                logger.info('reload from .py file')
                                break                   
                            else:
                                _import_once.append((model_path, aloha.unitary_gauge, prefix, decay))
                                return model
                    else:
                        continue
                    break                                         
            else:
                logger.info('reload from .py file')

    if (model_path, aloha.unitary_gauge, prefix, decay) in _import_once and not allow_reload:
        raise MadGraph5Error, 'This model %s is modified on disk. To reload it you need to quit/relaunch MG5_aMC ' % model_path
     
    # Load basic information
    ufo_model = ufomodels.load_model(model_path, decay)
    ufo2mg5_converter = UFOMG5Converter(ufo_model)    
    model = ufo2mg5_converter.load_model()
    if model_path[-1] == '/': model_path = model_path[:-1] #avoid empty name
    model.set('name', os.path.split(model_path)[-1])

    # Load the Parameter/Coupling in a convenient format.
    parameters, couplings = OrganizeModelExpression(ufo_model).main(\
             additional_couplings =(ufo2mg5_converter.wavefunction_CT_couplings
                           if ufo2mg5_converter.perturbation_couplings else []))
    
    model.set('parameters', parameters)
    model.set('couplings', couplings)
    model.set('functions', ufo_model.all_functions)

    # Optional UFO part: decay_width information


    if decay and hasattr(ufo_model, 'all_decays') and ufo_model.all_decays:       
        start = time.time()
        for ufo_part in ufo_model.all_particles:
            name =  ufo_part.name
            if not model['case_sensitive']:
                name = name.lower() 
            p = model['particles'].find_name(name)
            if hasattr(ufo_part, 'partial_widths'):
                p.partial_widths = ufo_part.partial_widths
            elif p and not hasattr(p, 'partial_widths'):
                p.partial_widths = {}
            # might be None for ghost
        logger.debug("load width takes %s", time.time()-start)
    
    if prefix:
        start = time.time()
        model.change_parameter_name_with_prefix()
        logger.debug("model prefixing  takes %s", time.time()-start)
                     
    path = os.path.dirname(os.path.realpath(model_path))
    path = os.path.join(path, model.get('name'))
    model.set('version_tag', os.path.realpath(path) +'##'+ str(misc.get_pkg_info()))
    
    # save in a pickle files to fasten future usage
    if ReadWrite:
        save_load_object.save_to_file(os.path.join(model_path, pickle_name),
                                   model, log=False)

    #if default and os.path.exists(os.path.join(model_path, 'restrict_default.dat')):
    #    restrict_file = os.path.join(model_path, 'restrict_default.dat') 
    #    model = import_ufo.RestrictModel(model)
    #    model.restrict_model(restrict_file)

    return model

class UFOMG5Converter(object):
    """Convert a UFO model to the MG5 format"""

    def __init__(self, model, auto=False):
        """ initialize empty list for particles/interactions """
       
        if hasattr(model, '__arxiv__'):
            logger.info('Please cite %s when using this model', model.__arxiv__, '$MG:color:BLACK')
       
        self.particles = base_objects.ParticleList()
        self.interactions = base_objects.InteractionList()
        self.wavefunction_CT_couplings = []
 
        # Check here if we can extract the couplings perturbed in this model
        # which indicate a loop model or if this model is only meant for 
        # tree-level computations
        self.perturbation_couplings = {}
        try:
            for order in model.all_orders:
                if(order.perturbative_expansion>0):
                    self.perturbation_couplings[order.name]=order.perturbative_expansion
        except AttributeError,error:
            pass

        if self.perturbation_couplings!={}:
            self.model = loop_base_objects.LoopModel({'perturbation_couplings':\
                                                self.perturbation_couplings.keys()})
        else:
            self.model = base_objects.Model()                        
        self.model.set('particles', self.particles)
        self.model.set('interactions', self.interactions)
        self.conservecharge = set(['charge'])
        
        self.ufomodel = model
        self.checked_lor = set()

        if auto:
            self.load_model()

    def load_model(self):
        """load the different of the model first particles then interactions"""

        # Check the validity of the model
        # 1) check that all lhablock are single word.
        def_name = []
        for param in self.ufomodel.all_parameters:
            if param.nature == "external":
                if len(param.lhablock.split())>1:
                    raise InvalidModel, '''LHABlock should be single word which is not the case for
    \'%s\' parameter with lhablock \'%s\' ''' % (param.name, param.lhablock)
            if param.name in def_name:
                raise InvalidModel, "name %s define multiple time. Please correct the UFO model!" \
                                                                  % (param.name)
            else:
                def_name.append(param.name)
                                                                  
        # For each CTParameter, check that there is no name conflict with the
        # set of re-defined CTParameters with EPS and FIN suffixes.
        if hasattr(self.ufomodel,'all_CTparameters'):
            for CTparam in self.ufomodel.all_CTparameters:
                for pole in pole_dict:
                    if CTparam.pole(pole)!='ZERO':
                        new_param_name = '%s_%s_'%(CTparam.name,pole_dict[pole])
                        if new_param_name in def_name:
                            raise InvalidModel, "CT name %s"% (new_param_name)+\
                                           " the model. Please change its name."

        if hasattr(self.ufomodel, 'gauge'):    
            self.model.set('gauge', self.ufomodel.gauge)
        logger.info('load particles')
        # Check if multiple particles have the same name but different case.
        # Otherwise, we can use lowercase particle names.
        if len(set([p.name for p in self.ufomodel.all_particles] + \
                   [p.antiname for p in self.ufomodel.all_particles])) == \
           len(set([p.name.lower() for p in self.ufomodel.all_particles] + \
                   [p.antiname.lower() for p in self.ufomodel.all_particles])):
            self.model['case_sensitive'] = False
            

        # check which of the fermion/anti-fermion should be set as incoming
        self.detect_incoming_fermion()

        for particle_info in self.ufomodel.all_particles:            
            self.add_particle(particle_info)

        # Find which particles is in the 3/3bar color states (retrun {id: 3/-3})
        color_info = self.find_color_anti_color_rep()

        # load the lorentz structure.
        self.model.set('lorentz', self.ufomodel.all_lorentz)
        
        # Substitute the expression of CT couplings which include CTparameters
        # in their definition with the corresponding dictionaries, e.g.
        #      CTCoupling.value = 2*CTParam           ->
        #      CTCoupling.value = {-1: 2*CTParam_1EPS_, 0: 2*CTParam_FIN_}
        # for example if CTParam had a non-zero finite and single pole.
        # This change affects directly the UFO model and it will be reverted in
        # OrganizeModelExpression only, so that the main() function of this class
        # *must* be run on the UFO to have this change reverted.
        if hasattr(self.ufomodel,'all_CTparameters'):
            logger.debug('Handling couplings defined with CTparameters...')
            start_treat_coupling = time.time()
            self.treat_couplings(self.ufomodel.all_couplings, 
                                                 self.ufomodel.all_CTparameters)
            tot_time = time.time()-start_treat_coupling
            if tot_time>5.0:
                logger.debug('... done in %s'%misc.format_time(tot_time))        

        logger.info('load vertices')
        for interaction_info in self.ufomodel.all_vertices:
            self.add_interaction(interaction_info, color_info)

        if self.perturbation_couplings:
            try:
                self.ufomodel.add_NLO()
            except Exception, error:
                pass 

            for interaction_info in self.ufomodel.all_CTvertices:
                self.add_CTinteraction(interaction_info, color_info)
    
        self.model.set('conserved_charge', self.conservecharge)

        # If we deal with a Loop model here, the order hierarchy MUST be 
        # defined in the file coupling_orders.py and we import it from 
        # there.
        all_orders = []
        try:
            all_orders = self.ufomodel.all_orders
        except AttributeError:
            if self.perturbation_couplings:
                raise MadGraph5Error, "The loop model MG5 attemps to import does not specify the attribute 'all_order'." 
            else:
                pass            

        hierarchy={}
        try:
            for order in all_orders:
                hierarchy[order.name]=order.hierarchy
        except AttributeError:
            if self.perturbation_couplings:
                raise MadGraph5Error, 'The loop model MG5 attemps to import does not specify an order hierarchy.' 
            else:
                pass
        else:
            self.model.set('order_hierarchy', hierarchy)            
        
        # Also set expansion_order, i.e., maximum coupling order per process
        expansion_order={}
        # And finally the UVCT coupling order counterterms        
        coupling_order_counterterms={}        
        try:
            for order in all_orders:
                expansion_order[order.name]=order.expansion_order
                coupling_order_counterterms[order.name]=order.expansion_order                
        except AttributeError:
            if self.perturbation_couplings:
                raise MadGraph5Error, 'The loop model MG5 attemps to import does not specify an expansion_order for all coupling orders.' 
            else:
                pass
        else:
            self.model.set('expansion_order', expansion_order)
            self.model.set('expansion_order', expansion_order)            
            
        #clean memory
        del self.checked_lor

        return self.model
        
    
    def add_particle(self, particle_info):
        """ convert and add a particle in the particle list """
                
        loop_particles = [[[]]]
        counterterms = {}
        
        # MG5 have only one entry for particle and anti particles.
        #UFO has two. use the color to avoid duplictions
        pdg = particle_info.pdg_code
        if pdg in self.incoming or (pdg not in self.outcoming and pdg <0):
            return
        
        # MG5 doesn't use ghost for tree models: physical sum on the polarization
        if not self.perturbation_couplings and particle_info.spin < 0:
            return
        
        if (aloha.unitary_gauge and 0 in self.model['gauge']) \
                            or (1 not in self.model['gauge']): 
        
            # MG5 doesn't use goldstone boson 
            if hasattr(particle_info, 'GoldstoneBoson') and particle_info.GoldstoneBoson:
                return
            elif hasattr(particle_info, 'goldstone') and particle_info.goldstone:
                return      
        # Initialize a particles
        particle = base_objects.Particle()

        # MG5 doesn't use goldstone boson 
        if (hasattr(particle_info, 'GoldstoneBoson') and particle_info.GoldstoneBoson) \
                or (hasattr(particle_info, 'goldstoneboson') and particle_info.goldstoneboson):
            particle.set('type', 'goldstone')
        elif hasattr(particle_info, 'goldstone') and particle_info.goldstone:
            particle.set('type', 'goldstone')
        
        nb_property = 0   #basic check that the UFO information is complete
        # Loop over the element defining the UFO particles
        for key,value in particle_info.__dict__.items():
            # Check if we use it in the MG5 definition of a particles
            if key in base_objects.Particle.sorted_keys and not key=='counterterm':
                nb_property +=1
                if key in ['name', 'antiname']:
                    if not self.model['case_sensitive']:
                        particle.set(key, value.lower())
                    else:
                        particle.set(key, value)
                elif key == 'charge':
                    particle.set(key, float(value))
                elif key in ['mass','width']:
                    particle.set(key, str(value))
                elif key == 'spin':
                    # MG5 internally treats ghost with positive spin for loop models and 
                    # ignore them otherwise
                    particle.set(key,abs(value))
                    if value<0:
                        particle.set('type','ghost')
                elif key == 'propagating':
                    if not value:
                        particle.set('line', None)
                elif key == 'line':
                    if particle.get('line') is None:
                        pass # This means that propagating is on False 
                    else:
                        particle.set('line', value)
                elif key == 'propagator':
                    if value:
                        if aloha.unitary_gauge:
                            particle.set(key, str(value[0]))
                        else: 
                            particle.set(key, str(value[1]))
                    else:
                        particle.set(key, '')
                else:
                    particle.set(key, value)    
            elif key == 'loop_particles':
                loop_particles = value
            elif key == 'counterterm':
                counterterms = value
            elif key.lower() not in ('ghostnumber','selfconjugate','goldstone',
                                             'goldstoneboson','partial_widths',
                                     'texname', 'antitexname', 'propagating', 'ghost'
                                             ):
                # add charge -we will check later if those are conserve 
                self.conservecharge.add(key)
                particle.set(key,value, force=True)

        if not hasattr(particle_info, 'propagator'):
            nb_property += 1
            if particle.get('spin') >= 3:
                if particle.get('mass').lower() == 'zero':
                    particle.set('propagator', 0) 
                elif particle.get('spin') == 3 and not aloha.unitary_gauge:
                    particle.set('propagator', 0)
               
        assert(10 == nb_property) #basic check that all the information is there         
        
        # Identify self conjugate particles
        if particle_info.name == particle_info.antiname:
            particle.set('self_antipart', True)
                    
        # Proceed only if we deal with a loop model and that this particle
        # has wavefunction renormalization
        if not self.perturbation_couplings or counterterms=={}:
            self.particles.append(particle)
            return
        
        # Set here the 'counterterm' attribute to the particle.
        # First we must change the couplings dictionary keys from the entry format
        # (order1,order2,...,orderN,loop_particle#):LaurentSerie
        # two a dictionary with format 
        # ('ORDER_OF_COUNTERTERM',((Particle_list_PDG))):{laurent_order:CTCouplingName}
        particle_counterterms = {}
        for key, counterterm in counterterms.items():
            # Makes sure this counterterm contributes at one-loop.
            if len([1 for k in key[:-1] if k==1])==1 and \
               not any(k>1 for k in key[:-1]):
                newParticleCountertermKey=[None,\
#                  The line below is for loop UFO Model with the 'attribute' 
#                  'loop_particles' of the Particle objects to be defined with
#                  instances of the particle class. The new convention is to use
#                  pdg numbers instead.
#                  tuple([tuple([abs(part.pdg_code) for part in loop_parts]) for\
                  tuple([tuple(loop_parts) for\
                    loop_parts in loop_particles[key[-1]]])]
                for i, order in enumerate(self.ufomodel.all_orders[:-1]):
                    if key[i]==1:
                        newParticleCountertermKey[0]=order.name
                newCouplingName='UVWfct_'+particle_info.name+'_'+str(key[-1])
                particle_counterterms[tuple(newParticleCountertermKey)]=\
                  dict([(key,newCouplingName+('' if key==0 else '_'+str(-key)+'eps'))\
                        for key in counterterm])
                # We want to create the new coupling for this wavefunction
                # renormalization.
                self.ufomodel.object_library.Coupling(\
                    name = newCouplingName,
                    value = counterterm,
                    order = {newParticleCountertermKey[0]:2})
                self.wavefunction_CT_couplings.append(self.ufomodel.all_couplings.pop())

        particle.set('counterterm',particle_counterterms)
        self.particles.append(particle)
        return

    def treat_couplings(self, couplings, all_CTparameters):
        """ This function scan each coupling to see if it contains a CT parameter.
        when it does, it changes its value to a dictionary with the CT parameter
        changed to a new parameter for each pole and finite part. For instance,
        the following coupling:
              coupling.value = '2*(myCTParam1 + myParam*(myCTParam2 + myCTParam3)'
        with CTparameters
              myCTParam1 = {0: Something, -1: SomethingElse}
              myCTParam2 = {0: OtherSomething }
              myCTParam3 = {-1: YetOtherSomething }              
        would be turned into
              coupling.value = {0: '2*(myCTParam1_FIN_ + myParam*(myCTParam2_FIN_ + ZERO)'
                               -1: '2*(myCTParam1_EPS_ + myParam*(ZERO + myCTParam2_EPS_)'}              
        
        all_CTParameter is the list of all CTParameters in the model"""
        
        # First define a list of regular expressions for each CT parameter 
        # and put them in a dictionary whose keys are the CT parameter names
        # and the values are a tuple with the substituting patter in the first
        # entry and the list of substituting functions (one for each pole)
        # as the second entry of this tuple.
        CTparameter_patterns = {}
        zero_substitution = lambda matchedObj: matchedObj.group('first')+\
                                               'ZERO'+matchedObj.group('second')
        def function_factory(arg):
            return lambda matchedObj: \
                    matchedObj.group('first')+arg+matchedObj.group('second')
        for CTparam in all_CTparameters:
            pattern_finder = re.compile(r"(?P<first>\A|\*|\+|\-|\(|\s)(?P<name>"+
                        CTparam.name+r")(?P<second>\Z|\*|\+|\-|\)|/|\\|\s)")
            
            sub_functions = [None if CTparam.pole(pole)=='ZERO' else
                function_factory('%s_%s_'%(CTparam.name,pole_dict[-pole]))
                                                           for pole in range(3)]
            CTparameter_patterns[CTparam.name] = (pattern_finder,sub_functions)
        
        times_zero = re.compile('\*\s*-?ZERO')
        zero_times = re.compile('ZERO\s*(\*|\/)')
        def is_expr_zero(expresson):
            """ Checks whether a single term (involving only the operations
            * or / is zero. """
            for term in expresson.split('-'):
                for t in term.split('+'):
                    t = t.strip()
                    if t in ['ZERO','']:
                        continue
                    if not (times_zero.search(t) or zero_times.search(t)):
                        return False
            return True
        
        def find_parenthesis(expr):
            end = expr.find(')')
            if end == -1:
                return None
            start = expr.rfind('(',0,end+1)
            if start ==-1:
                raise InvalidModel,\
                               'Parenthesis of expression %s are malformed'%expr
            return [expr[:start],expr[start+1:end],expr[end+1:]]
        
        start_parenthesis = re.compile(r".*\s*[\+\-\*\/\)\(]\s*$")

        def is_value_zero(value):
            """Check whether an expression like ((A+B)*ZERO+C)*ZERO is zero.
            Only +,-,/,* operations are allowed and 'ZERO' is a tag for an
            analytically zero quantity."""

            curr_value = value
            parenthesis = find_parenthesis(curr_value)
            while parenthesis:
                # Allow the complexconjugate function
                if parenthesis[0].endswith('complexconjugate'):
                    # Then simply remove it
                    parenthesis[0] = parenthesis[0][:-16]
                if parenthesis[0]=='' or re.match(start_parenthesis,
                                                                parenthesis[0]):
                    if is_value_zero(parenthesis[1]):
                        new_parenthesis = 'ZERO'
                    else:
                        new_parenthesis = 'PARENTHESIS'
                else:
                    new_parenthesis = '_FUNCTIONARGS'
                curr_value = parenthesis[0]+new_parenthesis+parenthesis[2]
                parenthesis = find_parenthesis(curr_value)
            return is_expr_zero(curr_value)

        def CTCoupling_pole(CTCoupling, pole):
            """Compute the pole of the CTCoupling in two cases:
               a) Its value is a dictionary, then just return the corresponding
                  entry in the dictionary.
               b) It is expressed in terms of CTParameters which are themselves
                  dictionary so we want to substitute their expression to get
                  the value of the pole. In the current implementation, this is
                  just to see if the pole is zero or not.
            """

            if isinstance(CTCoupling.value,dict):
                if -pole in CTCoupling.value.keys():
                    return CTCoupling.value[-pole], [], 0
                else:
                    return 'ZERO', [], 0              

            new_expression           = CTCoupling.value
            CTparamNames = []
            n_CTparams   = 0
            for paramname, value in CTparameter_patterns.items():
                pattern = value[0]
                # Keep track of which CT parameters enter in the definition of
                # which coupling.
                if not re.search(pattern,new_expression):
                    continue
                n_CTparams += 1
                # If the contribution of this CTparam to this pole is non
                # zero then the substituting function is not None:
                if not value[1][pole] is None:
                    CTparamNames.append('%s_%s_'%(paramname,pole_dict[-pole]))
  
                substitute_function = zero_substitution if \
                                      value[1][pole] is None else value[1][pole]
                new_expression = pattern.sub(substitute_function,new_expression)

            # If no CTParam was found and we ask for a pole, then it can only
            # be zero.
            if pole!=0 and n_CTparams==0:
                return 'ZERO', [], n_CTparams

            # Check if resulting expression is analytically zero or not.
            # Remember that when the value of a CT_coupling is not a dictionary
            # then the only operators allowed in the definition are +,-,*,/
            # and each term added or subtracted must contain *exactly one*
            # CTParameter and never at the denominator.   
            if n_CTparams > 0 and is_value_zero(new_expression):
                return 'ZERO', [], n_CTparams
            else:
                return new_expression, CTparamNames, n_CTparams

        # For each coupling we substitute its value if necessary
        for coupl in couplings:
            new_value = {}
            for pole in range(0,3):
                expression, CTparamNames, n_CTparams = CTCoupling_pole(coupl, pole)
                # Make sure it uses CT parameters, otherwise do nothing
                if n_CTparams == 0:
                    break
                elif expression!='ZERO':
                    new_value[-pole] = expression
                    couplname = coupl.name
                    if pole!=0:
                        couplname += "_%deps"%pole
                    # Add the parameter dependency found to the dependency map
                    # of the model being built. In principle, since we should
                    # be building a loop model now, it should always have this
                    # attribute defined, but it is better to make sure.
                    if hasattr(self.model, 'map_CTcoup_CTparam'):
                        self.model.map_CTcoup_CTparam[couplname] = CTparamNames

            # Finally modify the value of this CTCoupling so that it is no
            # longer a string expression in terms of CTParameters but rather
            # a dictionary with the CTparameters replaced by their _FIN_ and
            # _EPS_ counterparts.
            # This is useful for the addCT_interaction() step. I will be reverted
            # right after the addCT_interaction() function so as to leave
            # the UFO intact, as it should. 
            if new_value:
                coupl.old_value = coupl.value
                coupl.value = new_value

    def add_CTinteraction(self, interaction, color_info):
        """ Split this interaction in order to call add_interaction for
        interactions for each element of the loop_particles list. Also it
        is necessary to unfold here the contributions to the different laurent
        expansion orders of the couplings."""

        # Work on a local copy of the interaction provided
        interaction_info=copy.copy(interaction)
        
        intType=''
        if interaction_info.type not in ['UV','UVloop','UVtree','UVmass','R2']:
            raise MadGraph5Error, 'MG5 only supports the following types of'+\
              ' vertices, R2, UV and UVmass. %s is not in this list.'%interaction_info.type
        else:
            intType=interaction_info.type
            # If not specified and simply set to UV, guess the appropriate type
            if interaction_info.type=='UV':
                if len(interaction_info.particles)==2 and interaction_info.\
                          particles[0].name==interaction_info.particles[1].name:
                    intType='UVmass'
                else:
                    intType='UVloop'
        
        # Make sure that if it is a UV mass renromalization counterterm it is
        # defined as such.
#        if len(intType)>2 and intType[:2]=='UV' and len(interaction_info.particles)==2 \
#           and interaction_info.particles[0].name==interaction_info.particles[1].name:
#            intType='UVmass'

        # Now we create a couplings dictionary for each element of the loop_particles list
        # and for each expansion order of the laurent serie in the coupling.
        # and for each coupling order
        # Format is new_couplings[loop_particles][laurent_order] and each element
        # is a couplings dictionary.
        order_to_interactions= {}
        # will contains the new coupling of form
        #new_couplings=[[{} for j in range(0,3)] for i in \
        #               range(0,max(1,len(interaction_info.loop_particles)))]
        # So sort all entries in the couplings dictionary to put them a the
        # correct place in new_couplings.
        for key, couplings in interaction_info.couplings.items():
            if not isinstance(couplings, list):
                couplings = [couplings]
            for coupling in couplings:
                order = tuple(coupling.order.items())
                if order not in order_to_interactions:
                    order_to_interactions[order] = [
                           [{} for j in range(0,3)] for i in \
                           range(0,max(1,len(interaction_info.loop_particles)))]
                    new_couplings = order_to_interactions[order]
                else:
                    new_couplings = order_to_interactions[order]
                    
                for poleOrder in range(0,3):
                    expression = coupling.pole(poleOrder)
                    if expression!='ZERO':
                        if poleOrder==2:
                            raise InvalidModel, """
    The CT coupling %s was found with a contribution to the double pole. 
    This is either an error in the model or a parsing error in the function 'is_value_zero'.
    The expression of the non-zero double pole coupling is:
    %s
    """%(coupling.name,str(coupling.value))
                        # It is actually safer that the new coupling associated to
                        # the interaction added is not a reference to an original 
                        # coupling in the ufo model. So copy.copy is right here.   
                        newCoupling = copy.copy(coupling)
                        if poleOrder!=0:
                            newCoupling.name=newCoupling.name+"_"+str(poleOrder)+"eps"
                        newCoupling.value = expression
                        # assign the CT parameter dependences
                        #if hasattr(coupling,'CTparam_dependence') and \
                        #        (-poleOrder in coupling.CTparam_dependence) and \
                        #        coupling.CTparam_dependence[-poleOrder]:
                        #    newCoupling.CTparam_dependence = coupling.CTparam_dependence[-poleOrder]
                        #elif hasattr(newCoupling,'CTparam_dependence'):
                        #    delattr(newCoupling,"CTparam_dependence")
                        new_couplings[key[2]][poleOrder][(key[0],key[1])] = newCoupling  
            
        for new_couplings in order_to_interactions.values():
            # Now we can add an interaction for each.         
            for i, all_couplings in enumerate(new_couplings):
                loop_particles=[[]]
                if len(interaction_info.loop_particles)>0:
                    loop_particles=[[part.pdg_code for part in loop_parts] \
                        for loop_parts in interaction_info.loop_particles[i]]
                for poleOrder in range(0,3):
                    if all_couplings[poleOrder]!={}:
                        interaction_info.couplings=all_couplings[poleOrder]
                        self.add_interaction(interaction_info, color_info,\
                          (intType if poleOrder==0 else (intType+str(poleOrder)+\
                                                             'eps')),loop_particles)


    def find_color_anti_color_rep(self, output=None):
        """find which color are in the 3/3bar states"""
        # method look at the 3 3bar 8 configuration.
        # If the color is T(3,2,1) and the interaction F1 F2 V
        # Then set F1 to anticolor (and F2 to color)
        # if this is T(3,1,2) set the opposite
        if not output:
            output = {}
             
        for interaction_info in self.ufomodel.all_vertices:
            if len(interaction_info.particles) != 3:
                continue
            colors = [abs(p.color) for p in interaction_info.particles]
            if colors[:2] == [3,3]:
                if 'T(3,2,1)' in interaction_info.color:
                    color, anticolor, other = interaction_info.particles
                elif 'T(3,1,2)' in interaction_info.color:
                    anticolor, color, _ = interaction_info.particles
                elif 'Identity(1,2)' in interaction_info.color  or \
                     'Identity(2,1)' in interaction_info.color:
                    first, second, _ = interaction_info.particles
                    if first.pdg_code in output:
                        if output[first.pdg_code] == 3:
                            color, anticolor = first, second
                        else:
                            color, anticolor = second, first
                    elif second.pdg_code in output:
                        if output[second.pdg_code] == 3:
                            color, anticolor = second, first                        
                        else:
                            color, anticolor = first, second
                    else:
                        continue
                else:
                    continue
            elif colors[1:] == [3,3]:
                if 'T(1,2,3)' in interaction_info.color:
                    other, anticolor, color = interaction_info.particles
                elif 'T(1,3,2)' in interaction_info.color:
                    other, color, anticolor = interaction_info.particles
                elif 'Identity(2,3)' in interaction_info.color  or \
                     'Identity(3,2)' in interaction_info.color:
                    _, first, second = interaction_info.particles
                    if first.pdg_code in output:
                        if output[first.pdg_code] == 3:
                            color, anticolor = first, second
                        else:
                            color, anticolor = second, first
                    elif second.pdg_code in output:
                        if output[second.pdg_code] == 3:
                            color, anticolor = second, first                        
                        else:
                            color, anticolor = first, second
                    else:
                        continue
                else:
                    continue                  
               
            elif colors.count(3) == 2:
                if 'T(2,3,1)' in interaction_info.color:
                    color, other, anticolor = interaction_info.particles
                elif 'T(2,1,3)' in interaction_info.color:
                    anticolor, other, color = interaction_info.particles
                elif 'Identity(1,3)' in interaction_info.color  or \
                     'Identity(3,1)' in interaction_info.color:
                    first, _, second = interaction_info.particles
                    if first.pdg_code in output:
                        if output[first.pdg_code] == 3:
                            color, anticolor = first, second
                        else:
                            color, anticolor = second, first
                    elif second.pdg_code in output:
                        if output[second.pdg_code] == 3:
                            color, anticolor = second, first                        
                        else:
                            color, anticolor = first, second
                    else:
                        continue
                else:
                    continue                 
            else:
                continue    
            
            # Check/assign for the color particle
            if color.pdg_code in output: 
                if output[color.pdg_code] == -3:
                    raise InvalidModel, 'Particles %s is sometimes in the 3 and sometimes in the 3bar representations' \
                                    % color.name
            else:
                output[color.pdg_code] = 3
            
            # Check/assign for the anticolor particle
            if anticolor.pdg_code in output: 
                if output[anticolor.pdg_code] == 3:
                    raise InvalidModel, 'Particles %s is sometimes set as in the 3 and sometimes in the 3bar representations' \
                                    % anticolor.name
            else:
                output[anticolor.pdg_code] = -3
        
        return output
    
    def detect_incoming_fermion(self):
        """define which fermion should be incoming
           for that we look at F F~ X interactions
        """
        self.incoming = [] 
        self.outcoming = []       
        for interaction_info in self.ufomodel.all_vertices:
            # check if the interaction meet requirements:
            pdg = [p.pdg_code for p in interaction_info.particles if p.spin in [2,4]]
            if len(pdg) % 2:
                raise InvalidModel, 'Odd number of fermion in vertex: %s' % [p.pdg_code for p in interaction_info.particles]
            for i in range(0, len(pdg),2):
                if pdg[i] == - pdg[i+1]:
                    if pdg[i] in self.outcoming:
                        raise InvalidModel, '%s has not coherent incoming/outcoming status between interactions' %\
                            [p for p in interaction_info.particles if p.spin in [2,4]][i].name
                            
                    elif not pdg[i] in self.incoming:
                        self.incoming.append(pdg[i])
                        self.outcoming.append(pdg[i+1])
                     
    def add_interaction(self, interaction_info, color_info, type='base', loop_particles=None):            
        """add an interaction in the MG5 model. interaction_info is the 
        UFO vertices information."""
        # Import particles content:
        particles = [self.model.get_particle(particle.pdg_code) \
                                    for particle in interaction_info.particles]
        if None in particles:
            # Interaction with a ghost/goldstone
            return 
        particles = base_objects.ParticleList(particles)

        # Import Lorentz content:
        lorentz = [helas for helas in interaction_info.lorentz]            
        
        # Check the coherence of the Fermion Flow
        nb_fermion = sum([ 1 if p.is_fermion() else 0 for p in particles])
        try:
            if nb_fermion == 2:
                # Fermion Flow is suppose to be dealt by UFO
                [aloha_fct.check_flow_validity(helas.structure, nb_fermion) \
                                          for helas in interaction_info.lorentz
                                          if helas.name not in self.checked_lor]
                self.checked_lor.update(set([helas.name for helas in interaction_info.lorentz]))
            elif nb_fermion:
                if any(p.selfconjugate for p in interaction_info.particles if p.spin % 2 == 0):
                    text = "Majorana can not be dealt in 4/6/... fermion interactions"
                    raise InvalidModel, text
        except aloha_fct.WrongFermionFlow, error:
            text = 'Fermion Flow error for interactions %s: %s: %s\n %s' % \
             (', '.join([p.name for p in interaction_info.particles]), 
                                             helas.name, helas.structure, error)
            raise InvalidModel, text
        
        
        
        # Now consider the name only
        lorentz = [helas.name for helas in lorentz] 
        # Import color information:
        colors = [self.treat_color(color_obj, interaction_info, color_info) 
                                    for color_obj in interaction_info.color]
        
        
        order_to_int={}

        for key, couplings in interaction_info.couplings.items():
            if not isinstance(couplings, list):
                couplings = [couplings]
            if interaction_info.lorentz[key[1]].name not in lorentz:
                continue 
            # get the sign for the coupling (if we need to adapt the flow)
            if nb_fermion > 2:
                flow = aloha_fct.get_fermion_flow(interaction_info.lorentz[key[1]].structure, 
                                                                     nb_fermion)
                coupling_sign = self.get_sign_flow(flow, nb_fermion)
            else:                
                coupling_sign = ''            
            for coupling in couplings:
                order = tuple(coupling.order.items())
                if '1' in order:
                    raise InvalidModel, '''Some couplings have \'1\' order. 
                    This is not allowed in MG. 
                    Please defines an additional coupling to your model''' 
                if order in order_to_int:
                    order_to_int[order].get('couplings')[key] = '%s%s' % \
                                               (coupling_sign,coupling.name)
                else:
                    # Initialize a new interaction with a new id tag
                    interaction = base_objects.Interaction({'id':len(self.interactions)+1})                
                    interaction.set('particles', particles)              
                    interaction.set('lorentz', lorentz)
                    interaction.set('couplings', {key: 
                                     '%s%s' %(coupling_sign,coupling.name)})
                    interaction.set('orders', coupling.order)            
                    interaction.set('color', colors)
                    interaction.set('type', type)
                    interaction.set('loop_particles', loop_particles)                    
                    order_to_int[order] = interaction                        
                    # add to the interactions
                    self.interactions.append(interaction)
        
        # check if this interaction conserve the charge defined
 #       if type=='base':
        for charge in list(self.conservecharge): #duplicate to allow modification
            total = 0
            for part in interaction_info.particles:
                try:
                    total += getattr(part, charge)
                except AttributeError:
                    pass
            if abs(total) > 1e-12:
                logger.info('The model has interaction violating the charge: %s' % charge)
                self.conservecharge.discard(charge)
        
        
    def get_sign_flow(self, flow, nb_fermion):
        """ensure that the flow of particles/lorentz are coherent with flow 
           and return a correct version if needed"""
           
        if not flow or nb_fermion < 4:
            return ''
           
        expected = {}
        for i in range(nb_fermion//2):
            expected[i+1] = i+2
        
        if flow == expected:
            return ''

        switch = {}
        for i in range(1, nb_fermion+1):
            if not i in flow:
                continue
            switch[i] = len(switch)
            switch[flow[i]] = len(switch)

        # compute the sign of the permutation
        sign = 1
        done = []
   
        # make a list of consecutive number which correspond to the new
        # order of the particles in the new list.
        new_order = []
        for id in range(nb_fermion): # id is the position in the particles order (starts 0)
            nid = switch[id+1]-1 # nid is the position in the new_particles 
                                 #order (starts 0)
            new_order.append(nid)
             
        # compute the sign:
        sign =1
        for k in range(len(new_order)-1):
            for l in range(k+1,len(new_order)):
                if new_order[l] < new_order[k]:
                    sign *= -1     
                    
        return  '' if sign ==1 else '-'

    def add_lorentz(self, name, spins , expr):
        """ Add a Lorentz expression which is not present in the UFO """
        
        new = self.model['lorentz'][0].__class__(name = name,
                spins = spins,
                structure = expr)
        
        self.model['lorentz'].append(new)
        self.model.create_lorentz_dict()
        return name
    
    _pat_T = re.compile(r'T\((?P<first>\d*),(?P<second>\d*)\)')
    _pat_id = re.compile(r'Identity\((?P<first>\d*),(?P<second>\d*)\)')
    
    def treat_color(self, data_string, interaction_info, color_info):
        """ convert the string to ColorString"""
        
        #original = copy.copy(data_string)
        #data_string = p.sub('color.T(\g<first>,\g<second>)', data_string)
        
        
        output = []
        factor = 1
        for term in data_string.split('*'):
            pattern = self._pat_id.search(term)
            if pattern:
                particle = interaction_info.particles[int(pattern.group('first'))-1]
                particle2 = interaction_info.particles[int(pattern.group('second'))-1]
                if particle.color == particle2.color and particle.color in [-6, 6]:
                    error_msg = 'UFO model have inconsistency in the format:\n'
                    error_msg += 'interactions for  particles %s has color information %s\n'
                    error_msg += ' but both fermion are in the same representation %s'
                    raise InvalidModel, error_msg % (', '.join([p.name for p in interaction_info.particles]),data_string, particle.color)
                if particle.color == particle2.color and particle.color in [-3, 3]:
                    if particle.pdg_code in color_info and particle2.pdg_code in color_info:
                      if color_info[particle.pdg_code] == color_info[particle2.pdg_code]:
                        error_msg = 'UFO model have inconsistency in the format:\n'
                        error_msg += 'interactions for  particles %s has color information %s\n'
                        error_msg += ' but both fermion are in the same representation %s'
                        raise InvalidModel, error_msg % (', '.join([p.name for p in interaction_info.particles]),data_string, particle.color)
                    elif particle.pdg_code in color_info:
                        color_info[particle2.pdg_code] = -particle.pdg_code
                    elif particle2.pdg_code in color_info:
                        color_info[particle.pdg_code] = -particle2.pdg_code
                    else:
                        error_msg = 'UFO model have inconsistency in the format:\n'
                        error_msg += 'interactions for  particles %s has color information %s\n'
                        error_msg += ' but both fermion are in the same representation %s'
                        raise InvalidModel, error_msg % (', '.join([p.name for p in interaction_info.particles]),data_string, particle.color)
                
                
                if particle.color == 6:
                    output.append(self._pat_id.sub('color.T6(\g<first>,\g<second>)', term))
                elif particle.color == -6 :
                    output.append(self._pat_id.sub('color.T6(\g<second>,\g<first>)', term))
                elif particle.color == 8:
                    output.append(self._pat_id.sub('color.Tr(\g<first>,\g<second>)', term))
                    factor *= 2
                elif particle.color in [-3,3]:
                    if particle.pdg_code not in color_info:
                        #try to find it one more time 3 -3 1 might help
                        logger.debug('fail to find 3/3bar representation: Retry to find it')
                        color_info = self.find_color_anti_color_rep(color_info)
                        if particle.pdg_code not in color_info:
                            logger.debug('Not able to find the 3/3bar rep from the interactions for particle %s' % particle.name)
                            color_info[particle.pdg_code] = particle.color
                        else:
                            logger.debug('succeed')
                    if particle2.pdg_code not in color_info:
                        #try to find it one more time 3 -3 1 might help
                        logger.debug('fail to find 3/3bar representation: Retry to find it')
                        color_info = self.find_color_anti_color_rep(color_info)
                        if particle2.pdg_code not in color_info:
                            logger.debug('Not able to find the 3/3bar rep from the interactions for particle %s' % particle2.name)
                            color_info[particle2.pdg_code] = particle2.color                    
                        else:
                            logger.debug('succeed')
                
                    if color_info[particle.pdg_code] == 3 :
                        output.append(self._pat_id.sub('color.T(\g<second>,\g<first>)', term))
                    elif color_info[particle.pdg_code] == -3:
                        output.append(self._pat_id.sub('color.T(\g<first>,\g<second>)', term))
                else:
                    raise MadGraph5Error, \
                          "Unknown use of Identity for particle with color %d" \
                          % particle.color
            else:
                output.append(term)
        data_string = '*'.join(output)

        # Change convention for summed indices
        p = re.compile(r'\'\w(?P<number>\d+)\'')
        data_string = p.sub('-\g<number>', data_string)
         
        # Shift indices by -1
        new_indices = {}
        new_indices = dict([(j,i) for (i,j) in \
                           enumerate(range(1,
                                    len(interaction_info.particles)+1))])

                        
        output = data_string.split('*')
        output = color.ColorString([eval(data) \
                                    for data in output if data !='1'])
        output.coeff = fractions.Fraction(factor)
        for col_obj in output:
            col_obj.replace_indices(new_indices)

        return output
      
class OrganizeModelExpression:
    """Organize the couplings/parameters of a model"""
    
    track_dependant = ['aS','aEWM1','MU_R'] # list of variable from which we track 
                                   #dependencies those variables should be define
                                   #as external parameters
    
    # regular expression to shorten the expressions
    complex_number = re.compile(r'''complex\((?P<real>[^,\(\)]+),(?P<imag>[^,\(\)]+)\)''')
    expo_expr = re.compile(r'''(?P<expr>[\w.]+)\s*\*\*\s*(?P<expo>[+-]?[\d.]+)''')
    cmath_expr = re.compile(r'''cmath.(?P<operation>\w+)\((?P<expr>\w+)\)''')
    #operation is usualy sqrt / sin / cos / tan
    conj_expr = re.compile(r'''complexconjugate\((?P<expr>\w+)\)''')
    
    #RE expression for is_event_dependent
    separator = re.compile(r'''[+,\-*/()\s]*''')
    
    
    def __init__(self, model):
    
        self.model = model  # UFOMODEL
        self.perturbation_couplings = {}
        try:
            for order in model.all_orders: # Check if it is a loop model or not
                if(order.perturbative_expansion>0):
                    self.perturbation_couplings[order.name]=order.perturbative_expansion
        except AttributeError:
            pass
        self.params = {}     # depend on -> ModelVariable
        self.couplings = {}  # depend on -> ModelVariable
        self.all_expr = {} # variable_name -> ModelVariable
    
    def main(self, additional_couplings = []):
        """Launch the actual computation and return the associate 
        params/couplings. Possibly consider additional_couplings in addition
        to those defined in the UFO model attribute all_couplings """

        additional_params = []
        if hasattr(self.model,'all_CTparameters'):
            additional_params = self.get_additional_CTparameters()

        self.analyze_parameters(additional_params = additional_params)
        self.analyze_couplings(additional_couplings = additional_couplings)
        
        # Finally revert the possible modifications done by treat_couplings()
        if hasattr(self.model,'all_CTparameters'):
            self.revert_CTCoupling_modifications()

        return self.params, self.couplings

    def revert_CTCoupling_modifications(self):
        """ Finally revert the possible modifications done by treat_couplings()
        in UFOMG5Converter which were useful for the add_CTinteraction() in 
        particular. This modification consisted in expanding the value of a
        CTCoupling which consisted in an expression in terms of a CTParam to 
        its corresponding dictionary (e.g 
              CTCoupling.value = 2*CTParam           ->
              CTCoupling.value = {-1: 2*CTParam_1EPS_, 0: 2*CTParam_FIN_}
        for example if CTParam had a non-zero finite and single pole."""
        
        for coupl in self.model.all_couplings:
            if hasattr(coupl,'old_value'):
                coupl.value = coupl.old_value
                del(coupl.old_value)

    def get_additional_CTparameters(self):
        """ For each CTparameter split it into spimple parameter for each pole
        and the finite part if not zero."""

        additional_params = []
        for CTparam in self.model.all_CTparameters:
            for pole in range(3):
                if CTparam.pole(pole) != 'ZERO':
                  CTparam_piece = copy.copy(CTparam)
                  CTparam_piece.name = '%s_%s_'%(CTparam.name,pole_dict[-pole])
                  CTparam_piece.nature = 'internal'
                  CTparam_piece.type = CTparam.type
                  CTparam_piece.value = CTparam.pole(pole)
                  CTparam_piece.texname = '%s_{%s}'%\
                                              (CTparam.texname,pole_dict[-pole])
                  additional_params.append(CTparam_piece)
        return additional_params

    def analyze_parameters(self, additional_params=[]):
        """ separate the parameters needed to be recomputed events by events and
        the others"""
        # in order to match in Gmu scheme
        # test whether aEWM1 is the external or not
        # if not, take Gf as the track_dependant variable
        present_aEWM1 = any(param.name == 'aEWM1' for param in
                        self.model.all_parameters if param.nature == 'external')

        if not present_aEWM1:
            self.track_dependant = ['aS','Gf','MU_R']

        for param in self.model.all_parameters+additional_params:
            if param.nature == 'external':
                parameter = base_objects.ParamCardVariable(param.name, param.value, \
                                               param.lhablock, param.lhacode)
                
            else:
                expr = self.shorten_expr(param.value)
                depend_on = self.find_dependencies(expr)
                parameter = base_objects.ModelVariable(param.name, expr, param.type, depend_on)
            
            self.add_parameter(parameter)     
            
    def add_parameter(self, parameter):
        """ add consistently the parameter in params and all_expr.
        avoid duplication """
        
        assert isinstance(parameter, base_objects.ModelVariable)
        
        if parameter.name in self.all_expr:
            return
        
        self.all_expr[parameter.name] = parameter
        try:
            self.params[parameter.depend].append(parameter)
        except:
            self.params[parameter.depend] = [parameter]
            
    def add_coupling(self, coupling):
        """ add consistently the coupling in couplings and all_expr.
        avoid duplication """
        
        assert isinstance(coupling, base_objects.ModelVariable)
        
        if coupling.name in self.all_expr:
            return
        self.all_expr[coupling.value] = coupling
        try:
            self.coupling[coupling.depend].append(coupling)
        except:
            self.coupling[coupling.depend] = [coupling]

    def analyze_couplings(self,additional_couplings=[]):
        """creates the shortcut for all special function/parameter
        separate the couplings dependent of track variables of the others"""
        
        # For loop models, make sure that all couplings with dictionary values
        # are turned into set of couplings, one for each pole and finite part.
        if self.perturbation_couplings:
            couplings_list=[]
            for coupling in self.model.all_couplings + additional_couplings:
                if not isinstance(coupling.value,dict):
                    couplings_list.append(coupling)
                else:
                    for poleOrder in range(0,3):
                        if coupling.pole(poleOrder)!='ZERO':                    
                            newCoupling=copy.copy(coupling)
                            if poleOrder!=0:
                                newCoupling.name += "_%deps"%poleOrder
                            newCoupling.value=coupling.pole(poleOrder)
                            # assign the CT parameter dependences
#                             if hasattr(coupling,'CTparam_dependence') and \
#                                     (-poleOrder in coupling.CTparam_dependence) and \
#                                     coupling.CTparam_dependence[-poleOrder]:
#                                 newCoupling.CTparam_dependence = coupling.CTparam_dependence[-poleOrder]
#                             elif hasattr(newCoupling,'CTparam_dependence'):
#                                 delattr(newCoupling,"CTparam_dependence")
                            couplings_list.append(newCoupling)
        else:
            couplings_list = self.model.all_couplings + additional_couplings
            couplings_list = [c for c in couplings_list if not isinstance(c.value, dict)] 
            
        for coupling in couplings_list:
            # shorten expression, find dependencies, create short object
            expr = self.shorten_expr(coupling.value)
            depend_on = self.find_dependencies(expr)
            parameter = base_objects.ModelVariable(coupling.name, expr, 'complex', depend_on)
            # Add consistently in the couplings/all_expr
            try:
                self.couplings[depend_on].append(parameter)
            except KeyError:
                self.couplings[depend_on] = [parameter]
            self.all_expr[coupling.value] = parameter                

    def find_dependencies(self, expr):
        """check if an expression should be evaluated points by points or not
        """
        depend_on = set()

        # Treat predefined result
        #if name in self.track_dependant:  
        #    return tuple()
        
        # Split the different part of the expression in order to say if a 
        #subexpression is dependent of one of tracked variable
        expr = self.separator.split(expr)
        
        # look for each subexpression
        for subexpr in expr:
            if subexpr in self.track_dependant:
                depend_on.add(subexpr)
                
            elif subexpr in self.all_expr and self.all_expr[subexpr].depend:
                [depend_on.add(value) for value in self.all_expr[subexpr].depend 
                                if  self.all_expr[subexpr].depend != ('external',)]
        if depend_on:
            return tuple(depend_on)
        else:
            return tuple()


    def shorten_expr(self, expr):
        """ apply the rules of contraction and fullfill
        self.params with dependent part"""
        try:
            expr = self.complex_number.sub(self.shorten_complex, expr)
            expr = self.expo_expr.sub(self.shorten_expo, expr)
            expr = self.cmath_expr.sub(self.shorten_cmath, expr)
            expr = self.conj_expr.sub(self.shorten_conjugate, expr)
        except Exception:
            logger.critical("fail to handle expression: %s, type()=%s", expr,type(expr))
            raise
        return expr
    

    def shorten_complex(self, matchobj):
        """add the short expression, and return the nice string associate"""
        
        float_real = float(eval(matchobj.group('real')))
        float_imag = float(eval(matchobj.group('imag')))
        if float_real == 0 and float_imag ==1:
            new_param = base_objects.ModelVariable('complexi', 'complex(0,1)', 'complex')
            self.add_parameter(new_param)
            return 'complexi'
        else:
            return 'complex(%s, %s)' % (matchobj.group('real'), matchobj.group('imag'))
        
        
    def shorten_expo(self, matchobj):
        """add the short expression, and return the nice string associate"""
        
        expr = matchobj.group('expr')
        exponent = matchobj.group('expo')
        new_exponent = exponent.replace('.','_').replace('+','').replace('-','_m_')
        output = '%s__exp__%s' % (expr, new_exponent)
        old_expr = '%s**%s' % (expr,exponent)

        if expr.startswith('cmath'):
            return old_expr
        
        if expr.isdigit():
            output = 'nb__' + output #prevent to start with a number
            new_param = base_objects.ModelVariable(output, old_expr,'real')
        else:
            depend_on = self.find_dependencies(expr)
            type = self.search_type(expr)
            new_param = base_objects.ModelVariable(output, old_expr, type, depend_on)
        self.add_parameter(new_param)
        return output
        
    def shorten_cmath(self, matchobj):
        """add the short expression, and return the nice string associate"""
        
        expr = matchobj.group('expr')
        operation = matchobj.group('operation')
        output = '%s__%s' % (operation, expr)
        old_expr = ' cmath.%s(%s) ' %  (operation, expr)
        if expr.isdigit():
            new_param = base_objects.ModelVariable(output, old_expr , 'real')
        else:
            depend_on = self.find_dependencies(expr)
            type = self.search_type(expr)
            new_param = base_objects.ModelVariable(output, old_expr, type, depend_on)
        self.add_parameter(new_param)
        
        return output        
        
    def shorten_conjugate(self, matchobj):
        """add the short expression, and retrun the nice string associate"""
        
        expr = matchobj.group('expr')
        output = 'conjg__%s' % (expr)
        old_expr = ' complexconjugate(%s) ' % expr
        depend_on = self.find_dependencies(expr)
        type = 'complex'
        new_param = base_objects.ModelVariable(output, old_expr, type, depend_on)
        self.add_parameter(new_param)  
                    
        return output            
    

     
    def search_type(self, expr, dep=''):
        """return the type associate to the expression if define"""
        
        try:
            return self.all_expr[expr].type
        except:
            return 'complex'
            
class RestrictModel(model_reader.ModelReader):
    """ A class for restricting a model for a given param_card.
    rules applied:
     - Vertex with zero couplings are throw away
     - external parameter with zero/one input are changed into internal parameter.
     - identical coupling/mass/width are replace in the model by a unique one
     """
  
    def default_setup(self):
        """define default value"""
        self.del_coup = []
        super(RestrictModel, self).default_setup()
        self.rule_card = check_param_card.ParamCardRule()
        self.restrict_card = None
     
    def restrict_model(self, param_card, rm_parameter=True, keep_external=False,
                                                      complex_mass_scheme=None):
        """apply the model restriction following param_card.
        rm_parameter defines if the Zero/one parameter are removed or not from
        the model.
        keep_external if the param_card need to be kept intact
        """
        
        if self.get('name') == "mssm" and not keep_external:
            raise Exception

        self.restrict_card = param_card
        # Reset particle dict to ensure synchronized particles and interactions
        self.set('particles', self.get('particles'))

        # compute the value of all parameters
        # Get the list of definition of model functions, parameter values. 
        model_definitions = self.set_parameters_and_couplings(param_card, 
                                        complex_mass_scheme=complex_mass_scheme)
        
        # Simplify conditional statements
        logger.debug('Simplifying conditional expressions')
        modified_params, modified_couplings = \
            self.detect_conditional_statements_simplifications(model_definitions)
        
        # Apply simplifications
        self.apply_conditional_simplifications(modified_params, modified_couplings)
        
        # associate to each couplings the associated vertex: def self.coupling_pos
        self.locate_coupling()
        # deal with couplings
        zero_couplings, iden_couplings = self.detect_identical_couplings()

        # remove the out-dated interactions
        self.remove_interactions(zero_couplings)
        
        # replace in interactions identical couplings
        for iden_coups in iden_couplings:
            self.merge_iden_couplings(iden_coups)

        # remove zero couplings and other pointless couplings
        self.del_coup += zero_couplings
        self.remove_couplings(self.del_coup)
       
        # deal with parameters
        parameters = self.detect_special_parameters()
        self.fix_parameter_values(*parameters, simplify=rm_parameter, 
                                                    keep_external=keep_external)

        # deal with identical parameters
        if not keep_external:
            iden_parameters = self.detect_identical_parameters()
            for iden_param in iden_parameters:
                self.merge_iden_parameters(iden_param)
    
        iden_parameters = self.detect_identical_parameters()
        for iden_param in iden_parameters:
            self.merge_iden_parameters(iden_param, keep_external)
              
        # change value of default parameter if they have special value:
        # 9.999999e-1 -> 1.0
        # 0.000001e-99 -> 0 Those value are used to avoid restriction
        for name, value in self['parameter_dict'].items():
            if value == 9.999999e-1:
                self['parameter_dict'][name] = 1
            elif value == 0.000001e-99:
                self['parameter_dict'][name] = 0

                    
    def locate_coupling(self):
        """ create a dict couplings_name -> vertex or (particle, counterterm_key) """
        
        self.coupling_pos = {}
        for vertex in self['interactions']:
            for key, coupling in vertex['couplings'].items():
                if coupling.startswith('-'):
                    coupling = coupling[1:]
                if coupling in self.coupling_pos:
                    if vertex not in self.coupling_pos[coupling]:
                        self.coupling_pos[coupling].append(vertex)
                else:
                    self.coupling_pos[coupling] = [vertex]
        
        for particle in self['particles']:
            for key, coupling_dict in particle['counterterm'].items():
                for LaurentOrder, coupling in coupling_dict.items():
                    if coupling in self.coupling_pos:
                        if (particle,key) not in self.coupling_pos[coupling]:
                            self.coupling_pos[coupling].append((particle,key))
                    else:
                        self.coupling_pos[coupling] = [(particle,key)]

        return self.coupling_pos
        
    def detect_identical_couplings(self, strict_zero=False):
        """return a list with the name of all vanishing couplings"""
        
        dict_value_coupling = {}
        iden_key = set()
        zero_coupling = []
        iden_coupling = []
        
        for name, value in self['coupling_dict'].items():
            if value == 0:
                zero_coupling.append(name)
                continue
            elif not strict_zero and abs(value) < 1e-13:
                logger.debug('coupling with small value %s: %s treated as zero' %
                             (name, value))
                zero_coupling.append(name)
            elif not strict_zero and abs(value) < 1e-10:
                return self.detect_identical_couplings(strict_zero=True)

            
            if value in dict_value_coupling:
                iden_key.add(value)
                dict_value_coupling[value].append(name)
            else:
                dict_value_coupling[value] = [name]
        
        for key in iden_key:
            iden_coupling.append(dict_value_coupling[key])

        return zero_coupling, iden_coupling
    
    
    def detect_special_parameters(self):
        """ return the list of (name of) parameter which are zero """
        
        null_parameters = []
        one_parameters = []
        for name, value in self['parameter_dict'].items():
            if value == 0 and name != 'ZERO':
                null_parameters.append(name)
            elif value == 1:
                one_parameters.append(name)
        
        return null_parameters, one_parameters
    
    def apply_conditional_simplifications(self, modified_params,
                                                            modified_couplings):
        """ Apply the conditional statement simplifications for parameters and
        couplings detected by 'simplify_conditional_statements'.
        modified_params (modified_couplings) are list of tuples (a,b) with a
        parameter (resp. coupling) instance and b is the simplified expression."""
        
        if modified_params:
            logger.debug("Conditional expressions are simplified for parameters:")
            logger.debug(",".join("%s"%param[0].name for param in modified_params))
        for param, new_expr in modified_params:
            param.expr = new_expr
        
        if modified_couplings:
            logger.debug("Conditional expressions are simplified for couplings:")
            logger.debug(",".join("%s"%coupl[0].name for coupl in modified_couplings))
        for coupl, new_expr in modified_couplings:
            coupl.expr = new_expr
    
    def detect_conditional_statements_simplifications(self, model_definitions,
          objects=['couplings','parameters']):
        """ Simplifies the 'if' statements in the pythonic UFO expressions
        of parameters using the default variables specified in the restrict card.
        It returns a list of objects (parameters or couplings) and the new
        expression that they should take. Model definitions include all definitons
        of the model functions and parameters."""
        
        param_modifications = []
        coupl_modifications = []
        ifparser = parsers.UFOExpressionParserPythonIF(model_definitions)
        
        start_param = time.time()
        if 'parameters' in objects:
            for dependences, param_list in self['parameters'].items():
                if 'external' in dependences:
                    continue
                for param in param_list:
                    new_expr, n_changes = ifparser.parse(param.expr)
                    if n_changes > 0:
                        param_modifications.append((param, new_expr))
      
        end_param = time.time()
  
        if 'couplings' in objects:         
            for dependences, coupl_list in self['couplings'].items():
                for coupl in coupl_list:
                    new_expr, n_changes = ifparser.parse(coupl.expr)
                    if n_changes > 0:
                        coupl_modifications.append((coupl, new_expr))        

        end_coupl = time.time()
        
        tot_param_time = end_param-start_param
        tot_coupl_time = end_coupl-end_param
        if tot_param_time>5.0:
            logger.debug("Simplification of conditional statements"+\
              " in parameter expressions done in %s."%misc.format_time(tot_param_time))
        if tot_coupl_time>5.0:
            logger.debug("Simplification of conditional statements"+\
              " in couplings expressions done in %s."%misc.format_time(tot_coupl_time))

        return param_modifications, coupl_modifications
    
    def detect_identical_parameters(self):
        """ return the list of tuple of name of parameter with the same 
        input value """

        # Extract external parameters
        external_parameters = self['parameters'][('external',)]
        
        # define usefull variable to detect identical input
        block_value_to_var={} #(lhablok, value): list_of_var
        mult_param = set([])  # key of the previous dict with more than one
                              #parameter.
                              
        #detect identical parameter and remove the duplicate parameter
        for param in external_parameters[:]:
            value = self['parameter_dict'][param.name]
            if value in [0,1,0.000001e-99,9.999999e-1]:
                continue
            if param.lhablock.lower() == 'decay':
                continue
            key = (param.lhablock, value)
            mkey =  (param.lhablock, -value)

            if key in block_value_to_var:
                block_value_to_var[key].append((param,1))
                mult_param.add(key)
            elif mkey in block_value_to_var:
                block_value_to_var[mkey].append((param,-1))
                mult_param.add(mkey)
            else: 
                block_value_to_var[key] = [(param,1)]        
        
        output=[]  
        for key in mult_param:
            output.append(block_value_to_var[key])
            
        return output


    def merge_iden_couplings(self, couplings):
        """merge the identical couplings in the interactions and particle 
        counterterms"""

        
        logger_mod.debug(' Fuse the Following coupling (they have the same value): %s '% \
                        ', '.join([obj for obj in couplings]))
        
        main = couplings[0]
        self.del_coup += couplings[1:] # add the other coupl to the suppress list
        for coupling in couplings[1:]:
            # check if param is linked to an interaction
            if coupling not in self.coupling_pos:
                continue
            # replace the coupling, by checking all coupling of the interaction
            vertices = [ vert for vert in self.coupling_pos[coupling] if 
                         isinstance(vert, base_objects.Interaction)]
            for vertex in vertices:
                for key, value in vertex['couplings'].items():
                    if value == coupling:
                        vertex['couplings'][key] = main
                    elif value == '-%s' % coupling:
                        if main.startswith('-'):
                            vertex['couplings'][key] = main[1:]
                        else:
                            vertex['couplings'][key] = '-%s' % main
                        
            # replace the coupling appearing in the particle counterterm
            particles_ct = [ pct for pct in self.coupling_pos[coupling] if 
                         isinstance(pct, tuple)]
            for pct in particles_ct:
                for key, value in pct[0]['counterterm'][pct[1]].items():
                    if value == coupling:
                        pct[0]['counterterm'][pct[1]][key] = main


                
    def get_param_block(self):
        """return the list of block defined in the param_card"""
        
        blocks = set([p.lhablock for p in self['parameters'][('external',)]])
        return blocks
         
    def merge_iden_parameters(self, parameters, keep_external=False):
        """ merge the identical parameters given in argument.
        keep external force to keep the param_card untouched (up to comment)"""
            
        logger_mod.debug('Parameters set to identical values: %s '% \
                 ', '.join(['%s*%s' % (f, obj.name.replace('mdl_','')) for (obj,f) in parameters]))

        # Extract external parameters
        external_parameters = self['parameters'][('external',)]
        for i, (obj, factor) in enumerate(parameters):
            # Keeped intact the first one and store information
            if i == 0:
                obj.info = 'set of param :' + \
                                     ', '.join([str(f)+'*'+param.name.replace('mdl_','')
                                                 for (param, f) in parameters])
                expr = obj.name
                continue
            # Add a Rule linked to the param_card
            if factor ==1:
                self.rule_card.add_identical(obj.lhablock.lower(), obj.lhacode, 
                                                         parameters[0][0].lhacode )
            else:
                self.rule_card.add_opposite(obj.lhablock.lower(), obj.lhacode, 
                                                         parameters[0][0].lhacode )
            obj_name = obj.name
            # delete the old parameters
            if not keep_external:                
                external_parameters.remove(obj)
            elif obj.lhablock.upper() in ['MASS','DECAY']:
                external_parameters.remove(obj)
            else:
                obj.name = ''
                obj.info = 'MG5 will not use this value use instead %s*%s' %(factor,expr)    
            # replace by the new one pointing of the first obj of the class
            new_param = base_objects.ModelVariable(obj_name, '%s*%s' %(factor, expr), 'real')
            self['parameters'][()].insert(0, new_param)
        
        # For Mass-Width, we need also to replace the mass-width in the particles
        #This allows some optimization for multi-process.
        if parameters[0][0].lhablock in ['MASS','DECAY']:
            new_name = parameters[0][0].name
            if parameters[0][0].lhablock == 'MASS':
                arg = 'mass'
            else:
                arg = 'width'
            change_name = [p.name for (p,f) in parameters[1:]]
            [p.set(arg, new_name) for p in self['particle_dict'].values() 
                                                       if p[arg] in change_name]
            
    def remove_interactions(self, zero_couplings):
        """ remove the interactions and particle counterterms 
        associated to couplings"""
        
        
        mod_vertex = []
        mod_particle_ct = []
        for coup in zero_couplings:
            # some coupling might be not related to any interactions
            if coup not in self.coupling_pos:
                continue
            
            # Remove the corresponding interactions.

            vertices = [ vert for vert in self.coupling_pos[coup] if 
                         isinstance(vert, base_objects.Interaction) ]
            for vertex in vertices:
                modify = False
                for key, coupling in vertex['couplings'].items():
                    if coupling in zero_couplings:
                        modify=True
                        del vertex['couplings'][key]
                    elif coupling.startswith('-'):
                        coupling = coupling[1:]
                        if coupling in zero_couplings:
                            modify=True
                            del vertex['couplings'][key]                      
                        
                if modify:
                    mod_vertex.append(vertex)
            
            # Remove the corresponding particle counterterm
            particles_ct = [ pct for pct in self.coupling_pos[coup] if 
                         isinstance(pct, tuple)]
            for pct in particles_ct:
                modify = False
                for key, coupling in pct[0]['counterterm'][pct[1]].items():
                    if coupling in zero_couplings:
                        modify=True
                        del pct[0]['counterterm'][pct[1]][key]
                if modify:
                    mod_particle_ct.append(pct)

        # print useful log and clean the empty interaction
        for vertex in mod_vertex:
            part_name = [part['name'] for part in vertex['particles']]
            orders = ['%s=%s' % (order,value) for order,value in vertex['orders'].items()]
                                        
            if not vertex['couplings']:
                logger_mod.debug('remove interactions: %s at order: %s' % \
                                        (' '.join(part_name),', '.join(orders)))
                self['interactions'].remove(vertex)
            else:
                logger_mod.debug('modify interactions: %s at order: %s' % \
                                (' '.join(part_name),', '.join(orders)))

        # print useful log and clean the empty counterterm values
        for pct in mod_particle_ct:
            part_name = pct[0]['name']
            order = pct[1][0]
            loop_parts = ','.join(['('+','.join([\
                         self.get_particle(p)['name'] for p in part])+')' \
                         for part in pct[1][1]])
                                        
            if not pct[0]['counterterm'][pct[1]]:
                logger_mod.debug('remove counterterm of particle %s'%part_name+\
                                 ' with loop particles (%s)'%loop_parts+\
                                 ' perturbing order %s'%order)
                del pct[0]['counterterm'][pct[1]]
            else:
                logger_mod.debug('Modify counterterm of particle %s'%part_name+\
                                 ' with loop particles (%s)'%loop_parts+\
                                 ' perturbing order %s'%order)  

        return
                
    def remove_couplings(self, couplings):               
        #clean the coupling list:
        for name, data in self['couplings'].items():
            for coupling in data[:]:
                if coupling.name in couplings:
                    data.remove(coupling)
                            
        
    def fix_parameter_values(self, zero_parameters, one_parameters, 
                                            simplify=True, keep_external=False):
        """ Remove all instance of the parameters in the model and replace it by 
        zero when needed."""


        # treat specific cases for masses and width
        for particle in self['particles']:
            if particle['mass'] in zero_parameters:
                particle['mass'] = 'ZERO'
            if particle['width'] in zero_parameters:
                particle['width'] = 'ZERO'
            if particle['width'] in one_parameters:
                one_parameters.remove(particle['width'])                
                
        for pdg, particle in self['particle_dict'].items():
            if particle['mass'] in zero_parameters:
                particle['mass'] = 'ZERO'
            if particle['width'] in zero_parameters:
                particle['width'] = 'ZERO'


        # Add a rule for zero/one parameter
        external_parameters = self['parameters'][('external',)]
        for param in external_parameters[:]:
            value = self['parameter_dict'][param.name]
            block = param.lhablock.lower()
            if value == 0:
                self.rule_card.add_zero(block, param.lhacode)
            elif value == 1:
                self.rule_card.add_one(block, param.lhacode)

        special_parameters = zero_parameters + one_parameters
        
            

        if simplify:
            # check if the parameters is still useful:
            re_str = '|'.join(special_parameters)
            if len(re_str) > 25000: # size limit on mac
                split = len(special_parameters) // 2
                re_str = ['|'.join(special_parameters[:split]),
                          '|'.join(special_parameters[split:])]
            else:
                re_str = [ re_str ]
            used = set()
            for expr in re_str:
                re_pat = re.compile(r'''\b(%s)\b''' % expr)
                # check in coupling
                for name, coupling_list in self['couplings'].items():
                    for coupling in coupling_list:
                        for use in  re_pat.findall(coupling.expr):
                            used.add(use)
        else:
            used = set([i for i in special_parameters if i])
        
        # simplify the regular expression
        re_str = '|'.join([param for param in special_parameters if param not in used])
        if len(re_str) > 25000: # size limit on mac
            split = len(special_parameters) // 2
            re_str = ['|'.join(special_parameters[:split]),
                          '|'.join(special_parameters[split:])]
        else:
            re_str = [ re_str ]
        for expr in re_str:                                                      
            re_pat = re.compile(r'''\b(%s)\b''' % expr)
               
            param_info = {}
            # check in parameters
            for dep, param_list in self['parameters'].items():
                for tag, parameter in enumerate(param_list):
                    # update information concerning zero/one parameters
                    if parameter.name in special_parameters:
                        param_info[parameter.name]= {'dep': dep, 'tag': tag, 
                                                               'obj': parameter}
                        continue
                                        
                    # Bypass all external parameter
                    if isinstance(parameter, base_objects.ParamCardVariable):
                        continue
    
                    if simplify:
                        for use in  re_pat.findall(parameter.expr):
                            used.add(use)
                        
        # modify the object for those which are still used
        for param in used:
            if not param:
                continue
            data = self['parameters'][param_info[param]['dep']]
            data.remove(param_info[param]['obj'])
            tag = param_info[param]['tag']
            data = self['parameters'][()]
            if param in zero_parameters:
                data.insert(0, base_objects.ModelVariable(param, '0.0', 'real'))
            else:
                data.insert(0, base_objects.ModelVariable(param, '1.0', 'real'))
                
        # remove completely useless parameters
        for param in special_parameters:
            #by pass parameter still in use
            if param in used or \
                  (keep_external and param_info[param]['dep'] == ('external',)):
                logger_mod.debug('fix parameter value: %s' % param)
                continue 
            logger_mod.debug('remove parameters: %s' % (param))
            data = self['parameters'][param_info[param]['dep']]
            data.remove(param_info[param]['obj'])