Line Code
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 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084 10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344 10345 10346 10347 10348 10349 10350 10351 10352 10353 10354 10355 10356 10357 10358 10359 10360 10361 10362 10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 10385 10386 10387 10388 10389 10390 10391 10392 10393 10394 10395 10396 10397 10398 10399 10400 10401 10402 10403 10404 10405 10406 10407 10408 10409 10410 10411 10412 10413 10414 10415 10416 10417 10418 10419 10420 10421 10422 10423 10424 10425 10426 10427 10428 10429 10430 10431 10432 10433 10434 10435 10436 10437 10438 10439 10440 10441 10442 10443 10444 10445 10446 10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459 10460 10461 10462 10463 10464 10465 10466 10467 10468 10469 10470 10471 10472 10473 10474 10475 10476 10477 10478 10479 10480 10481 10482 10483 10484 10485 10486 10487 10488 10489 10490 10491 10492 10493 10494 10495 10496 10497 10498 10499 10500 10501 10502 10503 10504 10505 10506 10507 10508 10509 10510 10511 10512 10513 10514 10515 10516 10517 10518 10519 10520 10521 10522 10523 10524 10525 10526 10527 10528 10529 10530 10531 10532 10533 10534 10535 10536 10537 10538 10539 10540 10541 10542 10543 10544 10545 10546 10547 10548 10549 10550 10551 10552 10553 10554 10555 10556 10557 10558 10559 10560 10561 10562 10563 10564 10565 10566 10567 10568 10569 10570 10571 10572 10573 10574 10575 10576 10577 10578 10579 10580 10581 10582 10583 10584 10585 10586 10587 10588 10589 10590 10591 10592 10593 10594 10595 10596 10597 10598 10599 10600 10601 10602 10603 10604 10605 10606 10607 10608 10609 10610 10611 10612 10613 10614 10615 10616 10617 10618 10619 10620 10621 10622 10623 10624 10625 10626 10627 10628 10629 10630 10631 10632 10633 10634 10635 10636 10637 10638 10639 10640 10641 10642 10643 10644 10645 10646 10647 10648 10649 10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 10679 10680 10681 10682 10683 10684 10685 10686 10687 10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703 10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714 10715 10716 10717 10718 10719 10720 10721 10722 10723 10724 10725 10726 10727 10728 10729 10730 10731 10732 10733 10734 10735 10736 10737 10738 10739 10740 10741 10742 10743 10744 10745 10746 10747 10748 10749 10750 10751 10752 10753 10754 10755 10756 10757 10758 10759 10760 10761 10762 10763 10764 10765 10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777 10778 10779 10780 10781 10782 10783 10784 10785 10786 10787 10788 10789 10790 10791 10792 10793 10794 10795 10796 10797 10798 10799 10800 10801 10802 10803 10804 10805 10806 10807 10808 10809 10810 10811 10812 10813 10814 10815 10816 10817 10818 10819 10820 10821 10822 10823 10824 10825 10826 10827 10828 10829 10830 10831 10832 10833 10834 10835 10836 10837 10838 10839 10840 10841 10842 10843 10844 10845 10846 10847 10848 10849 10850 10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882 10883 10884 10885 10886 10887 10888 10889 10890 10891 10892 10893 10894 10895 10896 10897 10898 10899 10900 10901 10902 10903 10904 10905 10906 10907 10908 10909 10910 10911 10912 10913 10914 10915 10916 10917 10918 10919 10920 10921 10922 10923 10924 10925 10926 10927 10928 10929 10930 10931 10932 10933 10934 10935 10936 10937 10938 10939 10940 10941 10942 10943 10944 10945 10946 10947 10948 10949 10950 10951 10952 10953 10954 10955 10956 10957 10958 10959 10960 10961 10962 10963 10964 10965 10966 10967 10968 10969 10970 10971 10972 10973 10974 10975 10976 10977 10978 10979 10980 10981 10982 10983 10984 10985 10986 10987 10988 10989 10990 10991 10992 10993 10994 10995 10996 10997 10998 10999 11000 11001 11002 11003 11004 11005 11006 11007 11008 11009 11010 11011 11012 11013 11014 11015 11016 11017 11018 11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 11030 11031 11032 11033 11034 11035 11036 11037 11038 11039 11040 11041 11042 11043 11044 11045 11046 11047 11048 11049 11050 11051 11052 11053 11054 11055 11056 11057 11058 11059 11060 11061 11062 11063 11064 11065 11066 11067 11068 11069 11070 11071 11072 11073 11074 11075 11076 11077 11078 11079 11080 11081 11082 11083 11084 11085 11086 11087 11088 11089 11090 11091 11092 11093 11094 11095 11096 11097 11098 11099 11100 11101 11102 11103 11104 11105 11106 11107 11108 11109 11110 11111 11112 11113 11114 11115 11116 11117 11118 11119 11120 11121 11122 11123 11124 11125 11126 11127 11128 11129 11130 11131 11132 11133 11134 11135 11136 11137 11138 11139 11140 11141 11142 11143 11144 11145 11146 11147 11148 11149 11150 11151 11152 11153 11154 11155 11156 11157 11158 11159 11160 11161 11162 11163 11164 11165 11166 11167 11168 11169 11170 11171 11172 11173 11174 11175 11176 11177 11178 11179 11180 11181 11182 11183 11184 11185 11186 11187 11188 11189 11190 11191 11192 11193 11194 11195 11196 11197 11198 11199 11200 11201 11202 11203 11204 11205 11206 11207 11208 11209 11210 11211 11212 11213 11214 11215 11216 11217 11218 11219 11220 11221 11222 11223 11224 11225 11226 11227 11228 11229 11230 11231 11232 11233 11234 11235 11236 11237 11238 11239 11240 11241 11242 11243 11244 11245 11246 11247 11248 11249 11250 11251 11252 11253 11254 11255 11256 11257 11258 11259 11260 11261 11262 11263 11264 11265 11266 11267 11268 11269 11270 11271 11272 11273 11274 11275 11276 11277 11278 11279 11280 11281 11282 11283 11284 11285 11286 11287 11288 11289 11290 11291 11292 11293 11294 11295 11296 11297 11298 11299 11300 11301 11302 11303 11304 11305 11306 11307 11308 11309 11310 11311 11312 11313 11314 11315 11316 11317 11318 11319 11320 11321 11322 11323 11324 11325 11326 11327 11328 11329 11330 11331 11332 11333 11334 11335 11336 11337 11338 11339 11340 11341 11342 11343 11344 11345 11346 11347 11348 11349 11350 11351 11352 11353 11354 11355 11356 11357 11358 11359 11360 11361 11362 11363 11364 11365 11366 11367 11368 11369 11370 11371 11372 11373 11374 11375 11376 11377 11378 11379 11380 11381 11382 11383 11384 11385 11386 11387 11388 11389 11390 11391 11392 11393 11394 11395 11396 11397 11398 11399 11400 11401 11402 11403 11404 11405 11406 11407 11408 11409 11410 11411 11412 11413 11414 11415 11416 11417 11418 11419 11420 11421 11422 11423 11424 11425 11426 11427 11428 11429 11430 11431 11432 11433 11434 11435 11436 11437 11438 11439 11440 11441 11442 11443 11444 11445 11446 11447 11448 11449 11450 11451 11452 11453 11454 11455 11456 11457 11458 11459 11460 11461 11462 11463 11464 11465 11466 11467 11468 11469 11470 11471 11472 11473 11474 11475 11476 11477 11478 11479 11480 11481 11482 11483 11484 11485 11486 11487 11488 11489 11490 11491 11492 11493 11494 11495 11496 11497 11498 11499 11500 11501 11502 11503 11504 11505 11506 11507 11508 11509 11510 11511 11512 11513 11514 11515 11516 11517 11518 11519 11520 11521 11522 11523 11524 11525 11526 11527 11528 11529 11530 11531 11532 11533 11534 11535 11536 11537 11538 11539 11540 11541 11542 11543 11544 11545 11546 11547 11548 11549 11550 11551 11552 11553 11554 11555 11556 11557 11558 11559 11560 11561 11562 11563 11564 11565 11566 11567 11568 11569 11570 11571 11572 11573 11574 11575 11576 11577 11578 11579 11580 11581 11582 11583 11584 11585 11586 11587 11588 11589 11590 11591 11592 11593 11594 11595 11596 11597 11598 11599 11600 11601 11602 11603 11604 11605 11606 11607 11608 11609 11610 11611 11612 11613 11614 11615 11616 11617 11618 11619 11620 11621 11622 11623 11624 11625 11626 11627 11628 11629 11630 11631 11632 11633 11634 11635 11636 11637 11638 11639 11640 11641 11642 11643 11644 11645 11646 11647 11648 11649 11650 11651 11652 11653 11654 11655 11656 11657 11658 11659 11660 11661 11662 11663 11664 11665 11666 11667 11668 11669 11670 11671 11672 11673 11674 11675 11676 11677 11678 11679 11680 11681 11682 11683 11684 11685 11686 11687 11688 11689 11690 11691 11692 11693 11694 11695 11696 11697 11698 11699 11700 11701 11702 11703 11704 11705 11706 11707 11708 11709 11710 11711 11712 11713 11714 11715 11716 11717 11718 11719 11720 11721 11722 11723 11724 11725 11726 11727 11728 11729 11730 11731 11732 11733 11734 11735 11736 11737 11738 11739 11740 11741 11742 11743 11744 11745 11746 11747 11748 11749 11750 11751 11752 11753 11754 11755 11756 11757 11758 11759 11760 11761 11762 11763 11764 11765 11766 11767 11768 11769 11770 11771 11772 11773 11774 11775 11776 11777 11778 11779 11780 11781 11782 11783 11784 11785 11786 11787 11788 11789 11790 11791 11792 11793 11794 11795 11796 11797 11798 11799 11800 11801 11802 11803 11804 11805 11806 11807 11808 11809 11810 11811 11812 11813 11814 11815 11816 11817 11818 11819 11820 11821 11822 11823 11824 11825 11826 11827 11828 11829 11830 11831 11832 11833 11834 11835 11836 11837 11838 11839 11840 11841 11842 11843 11844 11845 11846 11847 11848 11849 11850 11851 11852 11853 11854 11855 11856 11857 11858 11859 11860 11861 11862 11863 11864 11865 11866 11867 11868 11869 11870 11871 11872 11873 11874 11875 11876 11877 11878 11879 11880 11881 11882 11883 11884 11885 11886 11887 11888 11889 11890 11891 11892 11893 11894 11895 11896 11897 11898 11899 11900 11901 11902 11903 11904 11905 11906 11907 11908 11909 11910 11911 11912 11913 11914 11915 11916 11917 11918 11919 11920 11921 11922 11923 11924 11925 11926 11927 11928 11929 11930 11931 11932 11933 11934 11935 11936 11937 11938 11939 11940 11941 11942 11943 11944 11945 11946 11947 11948 11949 11950 11951 11952 11953 11954 11955 11956 11957 11958 11959 11960 11961 11962 11963 11964 11965 11966 11967 11968 11969 11970 11971 11972 11973 11974 11975 11976 11977 11978 11979 11980 11981 11982 11983 11984 11985 11986 11987 11988 11989 11990 11991 11992 11993 11994 11995 11996 11997 11998 11999 12000 12001 12002 12003 12004 12005 12006 12007 12008 12009 12010 12011 12012 12013 12014 12015 12016 12017 12018 12019 12020 12021 12022 12023 12024 12025 12026 12027 12028 12029 12030 12031 12032 12033 12034 12035 12036 12037 12038 12039 12040 12041 12042 12043 12044 12045 12046 12047 12048 12049 12050 12051 12052 12053 12054 12055 12056 12057 12058 12059 12060 12061 12062 12063 12064 12065 12066 12067 12068 12069 12070 12071 12072 12073 12074 12075 12076 12077 12078 12079 12080 12081 12082 12083 12084 12085 12086 12087 12088 12089 12090 12091 12092 12093 12094 12095 12096 12097 12098 12099 12100 12101 12102 12103 12104 12105 12106 12107 12108 12109 12110 12111 12112 12113 12114 12115 12116 12117 12118 12119 12120 12121 12122 12123 12124 12125 12126 12127 12128 12129 12130 12131 12132 12133 12134 12135 12136 12137 12138 12139 12140 12141 12142 12143 12144 12145 12146 12147 12148 12149 12150 12151 12152 12153 12154 12155 12156 12157 12158 12159 12160 12161 12162 12163 12164 12165 12166 12167 12168 12169 12170 12171 12172 12173 12174 12175 12176 12177 12178 12179 12180 12181 12182 12183 12184 12185 12186 12187 12188 12189 12190 12191 12192 12193 12194 12195 12196 12197 12198 12199 12200 12201 12202 12203 12204 12205 12206 12207 12208 12209 12210 12211 12212 12213 12214 12215 12216 12217 12218 12219 12220 12221 12222 12223 12224 12225 12226 12227 12228 12229 12230 12231 12232 12233 12234 12235 12236 12237 12238 12239 12240 12241 12242 12243 12244 12245 12246 12247 12248 12249 12250 12251 12252 12253 12254 12255 12256 12257 12258 12259 12260 12261 12262 12263 12264 12265 12266 12267 12268 12269 12270 12271 12272 12273 12274 12275 12276 12277 12278 12279 12280 12281 12282 12283 12284 12285 12286 12287 12288 12289 12290 12291 12292 12293 12294 12295 12296 12297 12298 12299 12300 12301 12302 12303 12304 12305 12306 12307 12308 12309 12310 12311 12312 12313 12314 12315 12316 12317 12318 12319 12320 12321 12322 12323 12324 12325 12326 12327 12328 12329 12330 12331 12332 12333 12334 12335 12336 12337 12338 12339 12340 12341 12342 12343 12344 12345 12346 12347 12348 12349 12350 12351 12352 12353 12354 12355 12356 12357 12358 12359 12360 12361 12362 12363 12364 12365 12366 12367 12368 12369 12370 12371 12372 12373 12374 12375 12376 12377 12378 12379 12380 12381 12382 12383 12384 12385 12386 12387 12388 12389 12390 12391 12392 12393 12394 12395 12396 12397 12398 12399 12400 12401 12402 12403 12404 12405 12406 12407 12408 12409 12410 12411 12412 12413 12414 12415 12416 12417 12418 12419 12420 12421 12422 12423 12424 12425 12426 12427 12428 12429 12430 12431 12432 12433 12434 12435 12436 12437 12438 12439 12440 12441 12442 12443 12444 12445 12446 12447 12448 12449 12450 12451 12452 12453 12454 12455 12456 12457 12458 12459 12460 12461 12462 12463 12464 12465 12466 12467 12468 12469 12470 12471 12472 12473 12474 12475 12476 12477 12478 12479 12480 12481 12482 12483 12484 12485 12486 12487 12488 12489 12490 12491 12492 12493 12494 12495 12496 12497 12498 12499 12500 12501 12502 12503 12504 12505 12506 12507 12508 12509 12510 12511 12512 12513 12514 12515 12516 12517 12518 12519 12520 12521 12522 12523 12524 12525 12526 12527 12528 12529 12530 12531 12532 12533 12534 12535 12536 12537 12538 12539 12540 12541 12542 12543 12544 12545 12546 12547 12548 12549 12550 12551 12552
c-----------------------------------------------------------------------
      subroutine emsaa(iret)
c-----------------------------------------------------------------------
c  energy-momentum sharing
c-----------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'
      include 'epos.incsem'
      common/cwzero/wzero,wzerox
      double precision omega,omlog,oma,omb,wab,wba,wmatrix,wzero,nbar
     *,wzerox,rrr,eps,xprem,xmrem,om1intgck
      parameter(eps=1.d-30)
      common/col3/ncol,kolpt
c      logical modu
      common/cems5/plc,s
      double precision s,px,py,pomass,plc!,PhiExpo
      common/ems6/ivp0,iap0,idp0,isp0,ivt0,iat0,idt0,ist0
      common/emsptl/nppr(npommx,kollmx),npproj(mamx),nptarg(mamx)
      common/nucl3/phi,bimp
      common/epoquasi/iquasi
      logical vpom,difint
      dimension ishuff(2*mamx,2),icp(2),ict(2),jcp(nflav,2),jct(nflav,2)
     &          ,nishuff(2)
      call utpri('emsaa ',ish,ishini,4)

      irea=iret

      do j=1,2
        do i=1,nflav
          jcp(i,j)=0
          jct(i,j)=0
        enddo
      enddo

      iret=0
      iret2=0

c     initialize
c     ----------

      call emsipt   !initialize projectile and target
      call emsigr   !initialize grid



c Metropolis

      if(iokoll.ne.1)then

        nSprmx=0
        do k=1,koll
          nSprmx=nSprmx+nprmx(k)
        enddo

        omlog=0
        nemsi=nemsi+1
        if(nemsi.le.4.and.iemsi1.eq.1)call xEmsI1(1,0,omlog)
        if(ish.ge.6)write (ifch,*)'after xEmsI1'
        if(nemsi.le.4.and.iemsi2.eq.1)call xEmsI2(1,0)
        if(ish.ge.6)write (ifch,*)'after xEmsI2'
        if(ish.ge.6)call XPrint('Before Markov:&')


c     Markov
c     ------

      if(ish.ge.4)write(ifch,*)'Markov Process'
      kint=int(max(15.,2.*engy**0.2))
      if(koll.gt.50)kint=3*kint/int(log(float(koll)))
      kmcmx=nSprmx*kint        !50*kint  !100*kint


      do kmc=1,kmcmx               !-----> start Metropolis

       knprmx=0
       rrr=dble(rangen())
       do ik=1,koll
         knprmx=knprmx+nprmx(ik)
         if(rrr.le.dble(knprmx)/dble(nSprmx))then ! k-th pair
           k=ik
           goto 10
         endif
       enddo
 10    continue

       ip=iproj(k)
       it=itarg(k)
       n=1+int(rangen()*float(nprmx(k)))  ! n-th spot for k-th pair
       nbar=dble(npr(0,k))
       if(idpr(n,k).eq.0)nbar=nbar-1d0

       xprem=1.d0!xpp(ip)+xppr(n,k)        !consistently, it should be 1.
       xmrem=1.d0!xmt(it)+xmpr(n,k)
       wzerox=(nbar+1d0)
       wzero=wzerox    / ( wzerox
     &                    +om1intgck(k,xprem,xmrem)*gammaV(k) )

       if(ish.ge.8)write(ifch,*)'wzero',k,n,wzero,wzerox,gammaV(k)
     &                          ,om1intgck(k,xprem,xmrem)
       if(ish.ge.1.and.100000*(kmc/100000).eq.kmc)
     & write(ifmt,*)'kmc',kmc,kmcmx

       call StoCon(1,k,n)
       call RemPom(k,n)
       call ProPo(k,n)
       call ProXY(k,n)

       call StoCon(2,k,n)

       if(idpr(n,k).eq.0.and.idx0.eq.0)then
         accept=accept+1.
       else

         omb=omega(n,k)
         if(omb.le.0.d0)then
           reject=reject+1.
           call RemPom(k,n)
           call StoCon(-1,k,n)
         else

           wab=wmatrix(k,n)
           if(ish.ge.8)write(ifch,*)'omb',omb,wab,k,n
           if(wab.le.0.d0)then
             write (ifmt,*)'wab,kmc',wab,omb,kmc,k,n,xpr(n,k),ypr(n,k)
     &  ,xppr(n,k),xmpr(n,k),xpp(ip),xmt(it),ip,it,idpr(n,k)
             write(ifmt,'(a,i12,d25.15)')'ems,seedf',nrevt+1,seedc
             iret=1
             goto 1000
           endif
           call RemPom(k,n)
           call StoCon(-1,k,n)
           oma=omega(n,k)
           wba=wmatrix(k,n)
           if(oma.ge.0.d0.and.oma.le.eps*omb*wba/wab)then
             accept=accept+1.
             call RemPom(k,n)
             call StoCon(-2,k,n)
             omlog=omlog+dlog(omb)
             goto 500
           elseif(oma.le.1.d-300.or.oma.ne.oma.or.omb.ne.omb)then
             write (ifmt,*)'oma,kmc',oma,omb,kmc,k,n,xpr(n,k),ypr(n,k)
     &  ,xppr(n,k),xmpr(n,k),idpr(n,k),npr(1,k),xpp(ip),xmt(it),ip,it
             write(ifmt,'(a,i12,d25.15)')'ems,seedf',nrevt+1,seedc
             iret=1
             goto 1000
           endif

           z=sngl(omb/oma*wba/wab)
           if(ish.ge.8)write(ifch,*)'z,oma',z,oma,wba,k,n
           if(rangen().gt.z)then
             reject=reject+1.
           else
             accept=accept+1.
             call RemPom(k,n)
             call StoCon(-2,k,n)
             omlog=omlog-dlog(oma)+dlog(omb)
           endif

 500       continue

         endif

         endif

       if(nemsi.le.4)then
         kplot=int(float(kmc)/float(kmcmx)*100.)
         if(iemsi1.eq.1)call xEmsI1(1,kplot,omlog)
         if(iemsi2.eq.1)call xEmsI2(1,kplot)
       endif

      enddo                     !-----> end Metropolis


      else

        n=1
        
        do k=1,koll
          
          call ProPo(k,n)
          call ProXY(k,n)
          
        enddo

      endif

c --- Plot Pomeron b-distributions ---

      if(ish.ge.6)call XPrint('After Markov :&')

      if(iemsb.eq.1)then ! plot
       do k=1,koll
        call xEmsB(1,1,k)
        if(nprt(k).gt.0)call xEmsB(1,2,k)
       enddo
      endif

      if(iemsbg.eq.1)then ! plot
        call xEmsBg(3,0,0)
        do k=1,koll
          call xEmsBg(1,0,k)
          if(nprt(k).gt.0)then
            call xEmsBg(1,-1,k)
            do n=1,nprmx(k)
              if(idpr(n,k).ne.0)call xEmsBg(1,idpr(n,k),k)
            enddo
          endif
        enddo
      endif

c --- Plot distr of pomeron number ---


      if(iemspm.eq.1)then
       do k=1,koll
           call xEmsPm(1,k,nprt(k),nprmx(k))
       enddo
      endif


c --- Count all interactions ---

      ncol=0
      ncolh=0
      do k=1,koll
        if(nprt(k).gt.0)then
          ncol=ncol+1
          if(isplit.eq.1)then
            do n=1,nprmx(k)
              if(xpr(n,k).gt.xzcutpar(k))itpr(k)=1  !for nuclear splitting
            enddo
          endif
          ip=iproj(k)
          it=itarg(k)
          kolp(ip)=kolp(ip)+nprt(k) !number of cut Pomerons
          kolt(it)=kolt(it)+nprt(k) !on remnants
        endif
      enddo

c --- Calculate Z (written to zzremn)


      do ip=1,maproj
       call CalcZZ(1,ip)
      enddo
      do it=1,matarg
       call CalcZZ(-1,it)
      enddo

c -- Split Enhanced Pomerons and fix their nature ---

      if(isplit.eq.1.and.ncol.gt.0)then

        if (iLHC.eq.1)then  !make random selection to avoid assymetry
          
          nishuff(1)=0
          nishuff(2)=0
          do ip=1,maproj
            nishuff(1)=nishuff(1)+1
            ishuff(nishuff(1),1)=ip 
          enddo
          do it=1,matarg
            nishuff(2)=nishuff(2)+1
            ishuff(nishuff(2),2)=it 
          enddo

          do while(nishuff(1)+nishuff(2).gt.0)

c random selection
            if(nishuff(1).gt.0.and.nishuff(2).gt.0)then
              ir=1+int(rangen()+0.5)
            elseif(nishuff(1).gt.0)then
              ir=1
            else
              ir=2
            endif

            indx=1+int(rangen()*float(nishuff(ir)))
            if(ir.eq.1)then
              ip=ishuff(indx,ir)
              if(lproj3(ip).ne.0.and.kolp(ip).eq.0)call ProNucSpl( 1,ip)
            else
              it=ishuff(indx,ir)
              if(ltarg3(it).ne.0.and.kolt(it).eq.0)call ProNucSpl(-1,it)
            endif
            ishuff(indx,ir)=ishuff(nishuff(ir),ir)
            nishuff(ir)=nishuff(ir)-1

          enddo

        else

          do ip=1,maproj
            if(lproj3(ip).ne.0.and.kolp(ip).eq.0)call ProNucSpl( 1,ip)
          enddo
          do it=1,matarg
            if(ltarg3(it).ne.0.and.kolt(it).eq.0)call ProNucSpl(-1,it)
          enddo

        endif

        if(ish.ge.6)call XPrint('After ProNucSpl:&')

      endif

c -- Fix Pomeron type ---

      do k=1,koll
        itpr(k)=0
        do n=1,nprmx(k)
          if(idfpr(n,k).eq.0)call ProPoTy(k,n)
        enddo
      enddo



c --- Fix Remnant Excitation

      do ip=1,maproj
       if(lproj(ip).ne.0)then
         call ProReEx( 1,ip)
         if(iremn.ge.2)call UpdateFlav(ip,jcp,0) !reset jcpref to 0
       endif
      enddo
      do it=1,matarg
       if(ltarg(it).ne.0)then
         call ProReEx(-1,it)
         if(iremn.ge.2)call UpdateFlav(it,jct,0) !reset jctref to 0
       endif
      enddo


c --- LHC tune : remove unnecessary diffractive Pomerons
      if(iLHC.eq.1)then
        do k=1,koll
          ip=iproj(k)
          it=itarg(k)
c remove temporary diffractive Pomeron if at least on remnant excited
c          ymean=0.
          do n=1,nprmx(k)
            if(idpr(n,k).eq.-1)then
              idpr(n,k)=1
              if((iep(ip).gt.0.or.iet(it).gt.0)
     &             .and.xpr(n,k).le.xzcutpar(k))call VirPom(k,n,0)
c              if(iep(ip).gt.0.or.iet(it).gt.0)then
c                if(xpr(n,k).ge.xzcutpar(k))then
c                  call VirPom(k,n,0)
c                else
c                  ymean=ymean+ypr(n,k)
c                endif
c              endif
           endif
          enddo
cc put excitation on the side of the pomeron
c          if((ymean.gt.0..and.iet(it).gt.0).or.
c     &       (ymean.lt.0..and.iep(ip).gt.0))then
c            ietmp=iep(ip)
c            iep(ip)=iet(it)
c            iet(it)=ietmp
c          endif
        enddo
      endif

c --- Count real interactions ---

      ncol=0
      do k=1,koll
        if(nprt(k).gt.0)then        !inelastic
          ncol=ncol+1
          if(itpr(k).lt.0)then
            itpr(k)=-1
          else
            itpr(k)=1              !diffractive with Pomeron
          endif
        elseif(itpr(k).gt.0)then    !diffractive
          ncol=ncol+1
          call ProDiSc(k)
          itpr(k)=2
        endif
      enddo
      if(ish.ge.5)write(ifch,*)'ncol:',ncol



c --- fix all variables


      if(ish.ge.4)write(ifch,*)'fix all variables'


c ---  recalculate Zptn


c      if(irzptn.eq.1)call recalcZPtn


      typevt=0                !ela
      if(maproj+matarg.eq.2)then     !pp
        if(itpr(1).ne.0)then
          anintine=anintine+1.
          if(itpr(1).gt.0)then
            if(ionudi.eq.1
     &        .or.iep(1).ne.0.or.iet(1).ne.0.or.itpr(1).eq.1)then
              anintdiff=anintdiff+1.
              if((iep(1).eq.0.and.iet(1).eq.2).or.
     &           (iet(1).eq.0.and.iep(1).eq.2))anintsdif=anintsdif+1.
              if(iep(1).eq.0.and.iet(1).eq.2)typevt=-4    !SD tar
              if(iet(1).eq.0.and.iep(1).eq.2)typevt=4     !SD pro
              if(iep(1).eq.2.and.iet(1).eq.2)typevt=2     !DD
              if(iep(1).eq.0.and.iet(1).eq.0)typevt=3     !CD
            else
              anintine=anintine-1. !diffractive without excitation = elastic
            endif
          else
            typevt=1                                      !ND
          endif
        endif
      else
        aidif=0.
        aidifp=0.
        aidift=0.
        aiine=0.
        do k=1,koll
          ip=iproj(k)
          it=itarg(k)
          if(aidif.ge.0..and.itpr(k).gt.0)then
            aidifp=aidifp+iep(ip)+(2-itpr(k))*0.00001
            aidift=aidift+iet(it)+(2-itpr(k))*0.00001
            if(ionudi.eq.1)then !count all diff as inelastic (to compare to tabulated cs)
              aidif=aidif+1.
            endif
          elseif(itpr(k).eq.-1)then
            aiine=aiine+1.
            aidif=-ainfin
          endif
        enddo
        if(ionudi.eq.2)then
          aidif=aidif+aidifp
        else
          aidif=aidif+aidifp+aidift
        endif
        if(aidif.gt.0.)then
          anintdiff=anintdiff+1.
          anintine=anintine+1.
          if(aidifp.gt.0.5.and.aidift.le.0.5)then
            anintsdif=anintsdif+1.
            typevt=4                        !SD pro
          endif
          if(aidifp.gt.0.5.and.aidift.gt.0.5)then
            typevt=2                        !DD
          endif
          if(ionudi.ne.2)then
            if(aidifp.le.0.5.and.aidift.gt.0.5)then
              anintsdif=anintsdif+1.
              typevt=-4                      !SD tar
            elseif(typevt.le.0.5.and.aidifp.gt.0..and.aidift.gt.0.)then
              typevt=3                      !CD
            endif
          endif
        elseif(aiine.gt.0.)then
          anintine=anintine+1.
          typevt=1                          !ND
        endif
      endif

      if(ish.ge.6)call XPrint('After fixing:&')


c --- Plot MC pomeron number ---

      if(nemsi.le.4.and.irea.ge.0)then
       if(iemsi1.eq.1)call xEmsI1(1,100,omlog)
       if(iemsi2.eq.1)call xEmsI2(1,100)
       if(iemsi1.eq.1.and.ncol.gt.0)call xEmsI1(2,0,omlog)
       if(iemsi2.eq.1.and.ncol.gt.0)call xEmsI2(2,0)
       if((iemsi1.eq.1.or.iemsi2.eq.1).and.ncol.eq.0)nemsi=nemsi-1
      endif

      if(iemsb.eq.1)then        ! plot
        do k=1,koll
          if(itpr(k).eq.0)call xEmsB(1,3,k) !nothing
          if(itpr(k).eq.-1)call xEmsB(1,4,k) !cut
          if(itpr(k).gt.0)call xEmsB(1,5,k) !diffr
          if(abs(itpr(k)).eq.1)call xEmsB(1,6,k) !cut+diffr cut
        enddo
      endif


c check for diffractive interaction without excitation
      difint=.true.
      ieptot=0
      if(maproj+matarg.eq.2)ieptot=1     !not used for pp
      do k=1,koll
        if(itpr(k).eq.2)then
          ip=iproj(k)
          it=itarg(k)
          ieptot=ieptot+iep(ip)
          if(ionudi.ne.2)ieptot=ieptot+iet(it)
c for CR, ionudi=1, count diffraction without excitation as inelastic (part of the xs)
          if(ionudi.ne.1.and.iep(ip).eq.0.and.iet(it).eq.0)then
            ncol=ncol-1
            itpr(k)=0
            kolp(ip)=kolp(ip)-1
            kolt(it)=kolt(it)-1
          endif
        else
          if(iLHC.eq.1.and.abs(itpr(k)).eq.1)then
            difint=.false.
          elseif(iLHC.eq.0)then   !bug in CR version for ionudi=2 (difint=F always !)
            difint=.false.
          endif
        endif
      enddo
      if(difint.and.ionudi.eq.2.and.ieptot.eq.0)then
        ncol=0                  !for ionudi=2
        iret=0
        goto 1000               !no projectile excitation = elastic
      endif

      iquasi=0
      if(ncol.eq.0)goto 998
      if(difint.and.ieptot-1.le.0)then
        iquasi=1
        if(ish.ge.2)write(ifch,*)'EPOS Quasi-elastic event'
        goto 998
      endif

c --- Treat Pomerons ---------------------------------------


c --- Check minimum mass ---

      do k=1,koll
        ip=iproj(k)
        it=itarg(k)
      do n=1,nprmx(k)
        if(xpr(n,k).lt.(cumpom/engy)**2)then
          nnb=nbkpr(n,k)
          nnv=nvpr(n,k)
          if(nnv.ne.0)then
            nbkpr(nnv,k)=0                  !if bckp Pomeron
          endif
          if(nnb.ne.0)then
            ivi=1
            call VirPom(k,nnb,ivi)            !if hard backup exist
            nbkpr(n,k)=0                    !remove it
          endif
          ivi=2
          call VirPom(k,n,ivi)
        elseif(itpr(k).eq.1.and.abs(idfpr(n,k)).eq.1)then
c diffractive cut Pomeron should not change remnant excitation
          idfs=sign(1,idfpr(n,k))
          if(iep(ip).eq.0.and.iet(it).eq.0)then
            idfpr(n,k)=idfs*4       !not linked to both proj and targ
          elseif(iep(ip).eq.0)then
            idfpr(n,k)=idfs*3       !linked to targ
            iet(it)=1               !target excitation is inelastic type
          elseif(iet(it).eq.0)then
            idfpr(n,k)=idfs*2       !linked to proj
            iep(ip)=1               !projectile excitation is inelastic type
          endif
        endif
      enddo
      enddo

c --- Set String End Type and Pt

      do k=1,koll
        ip=iproj(k)
        it=itarg(k)
        do n=1,nprmx(k)

          if(idpr(n,k).gt.0)then

          ntry=0
          vpom=.false.
          ivpi=ivp(ip)
          ivti=ivt(it)
          idpi=idp(ip)
          idti=idt(it)
          do i=1,2
            icp(i)=icproj(i,ip)
            ict(i)=ictarg(i,it)
          enddo
          if(iremn.ge.2)then    !save jcpref and jctref into jcp and jct
            call UpdateFlav(ip,jcp,1)
            call UpdateFlav(it,jct,2)
          endif

 100      ntry=ntry+1
          iret=0
          if(ntry.ge.200)vpom=.true.
          if(ntry.gt.1)then
       if(ish.ge.4)write(ifch,*)'Try again setting string ends for k,n'
     &                               ,k,n,ntry
            ivp(ip)=ivpi
            ivt(it)=ivti
            idp(ip)=idpi
            idt(it)=idti
            do i=1,2
              icproj(i,ip)=icp(i)
              ictarg(i,it)=ict(i)
            enddo
            if(iremn.ge.2)then       !restore jcpref and jctref from jcp and jct
              call UpdateFlav(ip,jcp,-1)
              call UpdateFlav(it,jct,-2)
            endif
            call RmPt(k,n)
          endif

          if(nvpr(n,k).eq.0)call ProSeTy(k,n)      !Not for backup Pomeron
          call ProSePt(k,n,iret)
          if(iret.eq.1)then
            if(vpom)then
              ivi=13
              call VirPom(k,n,ivi)
            else
              goto 100
            endif
          endif

c      enddo
c      enddo

c --- Check Pomeron mass

c      do k=1,koll
c      do n=1,nprmx(k)
       if(idpr(n,k).ne.0.and.ivpr(n,k).ne.0)then
        px=xxp1pr(n,k)+xxp2pr(n,k)+xxm1pr(n,k)+xxm2pr(n,k)
        py=xyp1pr(n,k)+xyp2pr(n,k)+xym1pr(n,k)+xym2pr(n,k)
        pomass=xpr(n,k)*s-px*px-py*py
        if(pomass.lt.amprmn(idhpr(n,k)))then
          nnv=nvpr(n,k)
          nnb=nbkpr(n,k)
          idfpom=iabs(idfpr(n,k))
          if(vpom)then
            ivi=3
            call VirPom(k,n,ivi)  !call RmPt(k,n)
            if(nnv.ne.0)then    !bckp Pomeron
              nbkpr(nnv,k)=0
            endif
            if(nnb.ne.0)then    !big Pomeron with bckp one
              ivpr(nnb,k)=1
              nvpr(nnb,k)=0
              idfpr(nnb,k)=idfpom
              npr(1,k)=npr(1,k)+1
              npr(3,k)=npr(3,k)-1
            endif
          else
            goto 100
          endif
        endif
       endif
c      enddo
c      enddo

c --- Define String ends for "backup" Pomerons ---

c      do k=1,koll
c      do n=1,nprmx(k)
c        if(nvpr(n,k).ne.0)call ProSeX(k,n,iret)
c        if(iret.eq.1)then
c          if(vpom)then
c            nn=nvpr(n,k)
c            ivi=7
c            call VirPom(k,n,ivi)
c            nbkpr(nn,k)=0
c          else
c            goto 100
c          endif
c        endif
        iret=0
        iret2=0
c      enddo
c      enddo

c --- Define String ends for "normal" Pomerons ---

c      do k=1,koll
c      do n=1,nprmx(k)
        if(nvpr(n,k).eq.0)call ProSeX(k,n,iret)   !Not for backup Pomeron
        if(iret.eq.1)then
          if(vpom)then
            ivi=12
            call VirPom(k,n,ivi)
          else
            goto 100
          endif
        endif
        iret=0
        iret2=0

      endif

      enddo
      enddo


c --- Write ---

 998  call emszz
      if(ncol.eq.0)then
        iret=0
        goto 1000
      endif


      do k=1,koll
       if(abs(itpr(k)).eq.1)call emswrpom(k,iproj(k),maproj+itarg(k))
      enddo


c --- Treat hard Pomeron

      ncolh=0
      do k=1,koll
        ncolhp=0
        do n=1,nprmx(k)
          if(idpr(n,k).eq.3)then
            if(ishpom.eq.1)then
              call psahot(k,n,iret)
              if(iret.eq.0)ncolhp=ncolhp+1
              if(iret.eq.1)then
                if(nbkpr(n,k).ne.0)then
                  nn=nbkpr(n,k)
                  call ProSeTy(k,nn)
                  call ProSeX(k,nn,iret2)
                  if(iret2.eq.1)then
                    ivi=15
                    call VirPom(k,nn,ivi)
                    if(ivi.lt.0)then
                      jerr(7)=jerr(7)+1
                      iret=1
                      goto 1000
                    endif
                    istptl(nppr(nn,k))=32
                    nbkpr(n,k)=0
                  else
                    ivpr(nn,k)=1
                    nvpr(nn,k)=0
                    idfpr(nn,k)=idfpr(n,k)
                    npr(1,k)=npr(1,k)+1
                    npr(3,k)=npr(3,k)-1
                    ansff=ansff+1 !counters
                    anshf=anshf-1
                  endif
                endif
                ivi=16
                call VirPom(k,n,ivi)
                if(ivi.lt.0)then
                  jerr(7)=jerr(7)+1
                  iret=1
                  goto 1000
                endif
                istptl(nppr(n,k))=32
              elseif(nbkpr(n,k).ne.0)then
                nn=nbkpr(n,k)
                ivi=17
                call VirPom(k,nn,ivi)
                if(ivi.lt.0)then
                  jerr(7)=jerr(7)+1
                  iret=1
                  goto 1000
                endif
                istptl(nppr(nn,k))=32
                nbkpr(n,k)=0
              endif
              iret=0
            else
              istptl(nppr(n,k))=32
              if(nbkpr(n,k).ne.0)then
                nn=nbkpr(n,k)
                istptl(nppr(nn,k))=32
              endif
            endif
          endif
        enddo
        if(ncolhp.gt.0)ncolh=ncolh+1     !count hard binary collisions
      enddo
      kohevt=ncolh     !update number of hard collisions

      if(iLHC.eq.0.and.iremn.ge.2)then
c --- Add valence quark to jcpref and jctref for soft string ends ---
        do ip=1,maproj
          if(iep(ip).ne.-1)then
            call UpdateFlav(ip,jcp,10)
            do nnn=1,nrflav
              jcpval(nnn,1,ip)=jcp(nnn,1)
            enddo
            do nnn=1,nrflav
              jcpval(nnn,2,ip)=jcp(nnn,2)
            enddo
          else
            icp(1)=icproj(1,ip)
            icp(2)=icproj(2,ip)
            call iddeco(icp,jcp)
            do nnn=1,nrflav
              jcpval(nnn,1,ip)=jcp(nnn,1)
            enddo
            do nnn=1,nrflav
              jcpval(nnn,2,ip)=jcp(nnn,2)
            enddo
          endif
        enddo
        do it=1,matarg
          if(iet(it).ne.-1)then
            call UpdateFlav(it,jct,20)
            do nnn=1,nrflav
              jctval(nnn,1,it)=jct(nnn,1)
            enddo
            do nnn=1,nrflav
              jctval(nnn,2,it)=jct(nnn,2)
            enddo
          else
            ict(1)=ictarg(1,it)
            ict(2)=ictarg(2,it)
            call iddeco(ict,jct)
            do nnn=1,nrflav
              jctval(nnn,1,it)=jct(nnn,1)
            enddo
            do nnn=1,nrflav
              jctval(nnn,2,it)=jct(nnn,2)
            enddo
          endif
        enddo
      endif

c --- Treat "normal" soft Pomerons ---

      do k=1,koll
        do n=1,nprmx(k)
          if(nvpr(n,k).eq.0)then
            if(isopom.eq.1)then
              call ProSeF(k,n,iret)
              if(iret.eq.1)then
                ivi=18
                call VirPom(k,n,ivi)
                if(ivi.lt.0)then
                  jerr(7)=jerr(7)+1
                  iret=1
                  goto 1000
                endif
                istptl(nppr(n,k))=32
              endif
              iret=0
            else
              istptl(nppr(n,k))=32
            endif
          endif
        enddo
      enddo



c --- Treat Remnants -----------------------------------------

c --- Fix Pion Exchange in diffractive remnants

      do ip=1,maproj
       if(iep(ip).eq.2)call ProReEx( 2,ip)
      enddo
      do it=1,matarg
       if(iet(it).eq.2)call ProReEx( -2,it)
      enddo

c --- Diffractive Pt and check Pomeron status

      iret=1
      do k=1,koll
        call ProDiPt(k,1,iret)
      enddo
      if(iret.ne.0)then
        jerr(8)=jerr(8)+1
        ivi=99
        if(ish.ge.2)then
          write(ifch,*)'All Pomeron lost, redo event !'
          write(ifmt,*)'All Pomeron lost, redo event !'
        endif
        iret=1
        goto 1000
      endif

      if(iLHC.eq.1.and.iremn.ge.2)then
c --- Add valence quark to jcpref and jctref for soft string ends ---
        do ip=1,maproj
          if(iep(ip).ne.-1)then
            call UpdateFlav(ip,jcp,10)
            do nnn=1,nrflav
              jcpval(nnn,1,ip)=jcp(nnn,1)
            enddo
            do nnn=1,nrflav
              jcpval(nnn,2,ip)=jcp(nnn,2)
            enddo
          else
            icp(1)=icproj(1,ip)
            icp(2)=icproj(2,ip)
            call iddeco(icp,jcp)
            do nnn=1,nrflav
              jcpval(nnn,1,ip)=jcp(nnn,1)
            enddo
            do nnn=1,nrflav
              jcpval(nnn,2,ip)=jcp(nnn,2)
            enddo
          endif
        enddo
        do it=1,matarg
          if(iet(it).ne.-1)then
            call UpdateFlav(it,jct,20)
            do nnn=1,nrflav
              jctval(nnn,1,it)=jct(nnn,1)
            enddo
            do nnn=1,nrflav
              jctval(nnn,2,it)=jct(nnn,2)
            enddo
          else
            ict(1)=ictarg(1,it)
            ict(2)=ictarg(2,it)
            call iddeco(ict,jct)
            do nnn=1,nrflav
              jctval(nnn,1,it)=jct(nnn,1)
            enddo
            do nnn=1,nrflav
              jctval(nnn,2,it)=jct(nnn,2)
            enddo
          endif
        enddo
      endif

      do ip=1,maproj
c Here and later "kolp(ip).ne.0" replaced by "iep(ip).ne.-1" to count
c projectile and target nucleons which are counted in paires but are not used
c in collision (no diffractive or inelastic interaction) as slow particles
c at the end. Then we can use them in ProRem to give mass to all other nucleons
c and avoid energy conservation violation that utrescl can not treat
c (and it gives a reasonnable number of grey particles even if distributions
c are not really reproduced).
c       if(kolp(ip).ne.0)call ProCop(ip,ip)
       if(iep(ip).ne.-1)call ProCop(ip,ip)
      enddo
      do it=1,matarg
       if(iet(it).ne.-1)call ProCot(it,maproj+it)
c       if(kolt(it).ne.0)call ProCot(it,maproj+it)
      enddo


c ---- Remnant Masses (ProReM)


      if(ish.ge.6)call XPrint('Before  ProReM:&')
      ntry=0
      iret=0
      call StoRe(1)             !Store Remnant configuration
 123  ntry=ntry+1
      nishuff(1)=0
      nishuff(2)=0
      do ip=1,maproj
        if(iep(ip).eq.0)then
          nishuff(1)=nishuff(1)+1
          ishuff(nishuff(1),1)=ip      !positive for non excited projectile
        elseif(iep(ip).gt.0)then
          nishuff(2)=nishuff(2)+1
          ishuff(nishuff(2),2)=ip      !positive for excited projectile
        endif
      enddo
      do it=1,matarg
        if(iet(it).eq.0)then
          nishuff(1)=nishuff(1)+1
          ishuff(nishuff(1),1)=-it !negative for non excited  target
        elseif(iet(it).gt.0)then
          nishuff(2)=nishuff(2)+1
          ishuff(nishuff(2),2)=-it !negative for excited  target
        endif
      enddo

c      do ir=1,2         !first set mass of non excited remnant
cc      do ir=2,1,-1         !first set mass of excited remnant

c      do while(nishuff(ir).gt.0)
      do while(nishuff(1)+nishuff(2).gt.0)

c random selection
        if(nishuff(1).gt.0.and.nishuff(2).gt.0)then
          ir=1+int(rangen()+0.5)
        elseif(nishuff(1).gt.0)then
          ir=1
        else
          ir=2
        endif

        indx=1+int(rangen()*float(nishuff(ir)))
        if(ishuff(indx,ir).gt.0)then
          ip=ishuff(indx,ir)
          call ProReM( 1,ip,iret)
        else
          it=-ishuff(indx,ir)
          call ProReM(-1,it,iret)
        endif
        if(ish.ge.10)call XPrint('In  ProReM:&')

        if(iret.eq.1)then
          !----------------------------------------
          !If there is a problem, try again shuffle (30 times),
          !if it doesn't work, for pp, try 10 times with the same type
          !of event and if doesn't work redo event;
          !for pA redo event ; and for AB (with A or B >10)
          !continue with some ghosts ...
          !----------------------------------------
          if(ntry.lt.30)then
            if(ish.ge.3)write(ifch,*)'shuffle, try again',ntry
            call StoRe(-1)         !Restore Remnant configuration
            iret=0
            goto 123
          elseif(ntry.gt.30.or.maproj.le.20.or.matarg.le.20)then
            if(ish.ge.2)write(ifch,*)'ProRem, redo event ! ntry=',ntry
            if(ish.ge.1)write(ifmt,*)'ProRem, redo event ! ntry=',ntry
            iret=1
            goto 1000
          else
            if(ish.ge.3)write(ifch,*)'shuffle, try again forcing ...'
            call StoRe(-1)         !Restore Remnant configuration
            iret=10
            goto 123
          endif
        endif

        ishuff(indx,ir)=ishuff(nishuff(ir),ir)
        nishuff(ir)=nishuff(ir)-1

       enddo
c      enddo

c --- Correction for Diffractive Pt (from Ralph but seems to be less good for NA49)

c      do k=1,koll
c        call ProDiPt(k,2,idum)
c      enddo


      iret=0
      if(ish.ge.6)call XPrint('After ProReM:&')


c --- Write Z into zpaptl for connected strings


      do ip=1,maproj
        if(kolp(ip).ne.0)call WriteZZ(1,ip)
      enddo
      do it=1,matarg
        if(kolt(it).ne.0)call WriteZZ(-1,it)
      enddo


c --- Write Remnants


      do ip=1,maproj
c       if(kolp(ip).ne.0)call emswrp(ip,ip)
       if(iep(ip).ne.-1)call emswrp(ip,ip)
      enddo

      do it=1,matarg
c       if(kolt(it).ne.0)call emswrt(it,maproj+it)
       if(iet(it).ne.-1)call emswrt(it,maproj+it)
      enddo


c --- Remnant Flavors (ProReF)


      do ip=1,maproj
        call ProReF(1,ip,iret)
        if(iret.ne.0)goto 1000
      enddo
      do it=1,matarg
        call ProReF(-1,it,iret)
        if(iret.ne.0)goto 1000
      enddo


c     plot
c     ----

       if(iemspx.eq.1)then
       do ko=1,koll
        if(nprt(ko).gt.0)then
         do np=1,nprmx(ko)
          if(idpr(np,ko).gt.0)then
           call xEmsPx(1,sngl(xpr(np,ko)),sngl(ypr(np,ko)),nprt(ko))
          endif
         enddo
        endif
       enddo
      endif

      if(iemspbx.eq.1)then
       do k=1,koll
        if(nprt(k).gt.0)then
         do n=1,nprmx(k)
          if(idpr(n,k).eq.3)then
            je1=min(1,nemispr(1,n,k))
            je2=min(1,nemispr(2,n,k))
            jex=1+je1+2*je2
            if(itpr(k).eq.-1)then
              call xEmsP2(1,1+idhpr(n,k),jex
     *            ,sngl(xppr(n,k))
     *            ,sngl(xmpr(n,k))
     *            ,sngl(xpprbor(n,k)),sngl(xmprbor(n,k))
     *            ,ptprboo(1,n,k),ptprboo(2,n,k)  )
            else !diffractive hard pomeron
              call xEmsP2(1,0,jex
     *            ,sngl(xppr(n,k))
     *            ,sngl(xmpr(n,k))
     *            ,sngl(xpprbor(n,k)),sngl(xmprbor(n,k))
     *            ,ptprboo(1,n,k),ptprboo(2,n,k)  )
            endif
          endif
         enddo
        endif
       enddo
      endif


      if(iemsse.eq.1)then
       do ko=1,koll
        if(nprt(ko).gt.0)then
         do np=1,nprmx(ko)
          if(idpr(np,ko).gt.0)then
           ptp1=sngl(xxp1pr(np,ko)**2+xyp1pr(np,ko)**2)
           ptp2=sngl(xxp2pr(np,ko)**2+xyp2pr(np,ko)**2)
           ptm1=sngl(xxm1pr(np,ko)**2+xym1pr(np,ko)**2)
           ptm2=sngl(xxm2pr(np,ko)**2+xym2pr(np,ko)**2)
           call xEmsSe(1,sngl(xp1pr(np,ko)),ptp1,1,1)
           call xEmsSe(1,sngl(xp2pr(np,ko)),ptp2,1,1)
           call xEmsSe(1,sngl(xm1pr(np,ko)),ptm1,-1,1)
           call xEmsSe(1,sngl(xm2pr(np,ko)),ptm2,-1,1)
           call xEmsSe(1,sngl(xp1pr(np,ko)),sngl(xm1pr(np,ko)),1,2)
           call xEmsSe(1,sngl(xm2pr(np,ko)),sngl(xp2pr(np,ko)),1,2)
          endif
         enddo
        endif
       enddo
      endif

      if(iemsdr.eq.1)then
       do i=maproj+matarg+1,nptl
        if(istptl(iorptl(i)).eq.41)then
          xpdr=(pptl(4,i)+pptl(3,i))/sngl(plc)
          xmdr=(pptl(4,i)-pptl(3,i))/sngl(plc)
          if(ityptl(i).eq.41)call xEmsDr(1,xpdr,xmdr,1)
          if(ityptl(i).eq.51)call xEmsDr(1,xpdr,xmdr,2)
          if(ityptl(i).eq.42)call xEmsDr(1,xpdr,xmdr,3)
          if(ityptl(i).eq.52)call xEmsDr(1,xpdr,xmdr,4)
        endif
       enddo
      endif

      if(iemsrx.eq.1)then
       do i=1,maproj
        if(kolp(i).gt.0)call xEmsRx(1,1,sngl(xpp(i)),sngl(xmp(i)))
       enddo
       do j=1,matarg
        if(kolt(j).gt.0)call xEmsRx(1,2,sngl(xmt(j)),sngl(xpt(j)))
       enddo
      endif

      if(ixbDens.eq.1)call xbDens(1)

c     exit
c     ----

 1000 continue
c      write(*,*)'emsaa-iret',iret
      if(ish.ge.2.and.iret.ne.0)write(ifch,*)'iret not 0 (ems)=> redo'
     &                                       ,iret,ivi
      call utprix('emsaa ',ish,ishini,4)
      return
      end


c----------------------------------------------------------------------
      subroutine StoCon(mode,k,n)
c----------------------------------------------------------------------
c store or restore configuration
c   mode = 1 (store) or -1 (restore)
c   k = collision index
c   n = pomeron index
c----------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'

      ip=iproj(k)
      it=itarg(k)

      if(mode.eq.1)then

       do i=0,3
        nprx0(i)=npr(i,k)
       enddo
       nprtx0=nprt(k)
       idx0=idpr(n,k)
       xxpr0=xpr(n,k)
       yx0=ypr(n,k)
       xxppr0=xppr(n,k)
       xxmpr0=xmpr(n,k)
       nppx0=npp(ip)
       nptx0=npt(it)
       xppx0=xpp(ip)
       xppstx0=xppmx(ip)
       xmpstx0=xppmn(ip)
       xmtx0=xmt(it)
       xptstx0=xmtmx(it)
       xmtstx0=xmtmn(it)

      elseif(mode.eq.2)then

       do i=0,3
        nprx(i)=npr(i,k)
       enddo
       nprtx=nprt(k)
       idx=idpr(n,k)
       xxpr=xpr(n,k)
       yx=ypr(n,k)
       xxppr=xppr(n,k)
       xxmpr=xmpr(n,k)
       nppx=npp(ip)
       nptx=npt(it)
       xppx=xpp(ip)
       xppstx=xppmx(ip)
       xmpstx=xppmn(ip)
       xmtx=xmt(it)
       xptstx=xmtmx(it)
       xmtstx=xmtmn(it)

      elseif(mode.eq.-1)then

       do i=0,3
        npr(i,k)=nprx0(i)
       enddo
       nprt(k)=nprtx0
       idpr(n,k)=idx0
       xpr(n,k)=xxpr0
       ypr(n,k)=yx0
       xppr(n,k)=xxppr0
       xmpr(n,k)=xxmpr0
       npp(ip)=nppx0
       npt(it)=nptx0
       xpp(ip)=xppx0
       xppmx(ip)=xppstx0
       xppmn(ip)=xmpstx0
       xmt(it)=xmtx0
       xmtmx(it)=xptstx0
       xmtmn(it)=xmtstx0

      elseif(mode.eq.-2)then

       do i=0,3
        npr(i,k)=nprx(i)
       enddo
       nprt(k)=nprtx
       idpr(n,k)=idx
       xpr(n,k)=xxpr
       ypr(n,k)=yx
       xppr(n,k)=xxppr
       xmpr(n,k)=xxmpr
       npp(ip)=nppx
       npt(it)=nptx
       xpp(ip)=xppx
       xppmx(ip)=xppstx
       xppmn(ip)=xmpstx
       xmt(it)=xmtx
       xmtmx(it)=xptstx
       xmtmn(it)=xmtstx

      else
      call utstop('mode should integer from -2 to 2 (without 0)&')
      endif
      return
      end

c-------------------------------------------------------------------------
      subroutine RemPom(k,n)
c-------------------------------------------------------------------------
c remove pomeron
c-------------------------------------------------------------------------
      include 'epos.inc'
      include 'epos.incems'

      ip=iproj(k)
      it=itarg(k)
      npr(idpr(n,k),k)=npr(idpr(n,k),k)-1  !nr of pomerons
      nprt(k)=npr(1,k)+npr(3,k)
      if(idpr(n,k).gt.0)then
       npp(ip)=npp(ip)-1                     !nr of pomerons per proj
       npt(it)=npt(it)-1                     !nr of pomerons per targ
       idpr(n,k)=0
       xpp(ip)=xpp(ip)+xppr(n,k)
       xmt(it)=xmt(it)+xmpr(n,k)
       xpr(n,k)=0.d0
       ypr(n,k)=0.d0
       xppr(n,k)=0.d0
       xmpr(n,k)=0.d0



      endif

      end

c-------------------------------------------------------------------------
      subroutine ProPo(k,n)
c-------------------------------------------------------------------------
c propose pomeron type = idpr(n,k
c-------------------------------------------------------------------------
      include 'epos.inc'
      include 'epos.incems'
      double precision wzero,wzerox
      common/cwzero/wzero,wzerox

      ip=iproj(k)
      it=itarg(k)

      idpr(n,k)=0

      if(dble(rangen()).gt.wzero)then
        idpr(n,k)=1


c nbr of pomerons per proj
       npp(ip)=npp(ip)+1
c nbr of pomerons per targ
       npt(it)=npt(it)+1

      endif

      npr(idpr(n,k),k)=npr(idpr(n,k),k)+1 !nr of pomerons
      nprt(k)=npr(1,k)+npr(3,k)


      end


c-------------------------------------------------------------------------
      subroutine ProXY(k,n)
c-------------------------------------------------------------------------
c propose pomeron x,y
c-------------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incpar'
      include 'epos.incems'
      include 'epos.incsem'
      double precision xp,xm,om1xprk,om1xmrk,anip,anit,eps
     &,xprem,xmrem,xprm,xmrm
      parameter (eps=1.d-30)


      ip=iproj(k)
      it=itarg(k)


      xpr(n,k)=0.d0
      ypr(n,k)=0.d0

      if(idpr(n,k).ne.0)then
          xprem=xpp(ip)
          xmrem=xmt(it)
c because of fom, it's not symetric any more if we choose always xp first
c and then xm ... so choose it randomly.
          if(rangen().lt.0.5)then
            xp=om1xprk(k,xprem,xminDf,1)
            xmrm=xmrem
            xprm=xminDf
            xm=om1xmrk(k,xp,xprm,xmrm,1)
          else
            xm=om1xprk(k,xmrem,xminDf,-1)
            xmrm=xminDf
            xprm=xprem
            xp=om1xmrk(k,xm,xmrm,xprm,-1)
          endif
          xpr(n,k)=xp*xm
          ypr(n,k)=0.d0
          if(xm.gt.eps.and.xp.gt.eps)then
            ypr(n,k)=0.5D0*dlog(xp/xm)
            xppr(n,k)=xp
            xmpr(n,k)=xm
          else
            if(ish.ge.1)write(ifmt,*)'Warning in ProXY ',xp,xm
            npr(idpr(n,k),k)=npr(idpr(n,k),k)-1
            idpr(n,k)=0
            npr(idpr(n,k),k)=npr(idpr(n,k),k)+1
            xpr(n,k)=0.d0
            ypr(n,k)=0.d0
            xppr(n,k)=0.d0
            xmpr(n,k)=0.d0
            nprt(k)=npr(1,k)+npr(3,k)
            npp(ip)=npp(ip)-1   !nr of pomerons per proj
            npt(it)=npt(it)-1   !nr of pomerons per targ
          endif

c Update xp and xm of remnant, and change the limit to have big enought mass.

        anip=dble(npp(ip))
        anit=dble(npt(it))
        xpp(ip)=xpp(ip)-xppr(n,k)
        xppmn(ip)=min(1.d0,anip*xpmn(ip)/xmpmx(ip))
        xmt(it)=xmt(it)-xmpr(n,k)
        xmtmn(it)=min(1.d0,anit*xtmn(it)/xptmx(it))

      endif

      end

c-------------------------------------------------------------------------
      double precision function wmatrix(k,n)
c-------------------------------------------------------------------------
c proposal matrix w(a->b), considering pomeron type, x, y
c-------------------------------------------------------------------------

      include 'epos.incems'
      double precision wzero,wzerox,Womegak,xprem,xmrem,om1intgck
      common/cwzero/wzero,wzerox


c      ip=iproj(k)
c      it=itarg(k)

      if(idpr(n,k).eq.0)then
        wmatrix=wzero
      else
          xprem=1.d0!xpp(ip)+xppr(n,k)
          xmrem=1.d0!xmt(it)+xmpr(n,k)
          wmatrix=(1d0-wzero)/om1intgck(k,xprem,xmrem)
     *           *Womegak(xppr(n,k),xmpr(n,k),xprem,xmrem,k)
      endif


      end

c-------------------------------------------------------------------------
      double precision function omega(n,k)
c-------------------------------------------------------------------------
c calculates partial omega for spot (k,n)
c-------------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'
      include 'epos.incsem'
      common/cwzero/wzero,wzerox
      double precision wzero,wzerox,eps
      parameter(eps=1.d-15)
      double precision PhiExpoK,omGamk,xp,xm,fom
      double precision plc,s
      common/cems5/plc,s
      common/nucl3/phi,bimp

      omega=0.d0

      ip=iproj(k)
      it=itarg(k)

      if(xpp(ip).lt.xppmn(ip)+eps.or.xpp(ip).gt.1.d0+eps)goto 1001
      if(xmt(it).lt.xmtmn(it)+eps.or.xmt(it).gt.1.d0+eps)goto 1001

      omega=xpp(ip)**dble(alplea(iclpro))
     &     *xmt(it)**dble(alplea(icltar))

c      ztg=0
c      zpj=0
c      nctg=0
c      ncpj=0
c      zsame=nprt(k)
c      if(idpr(n,k).gt.0)then
c        if(nprt(k).le.0)stop'omega: nprt(k) should be positive !!!!    '
c        zsame=zsame-1
c      endif
c      nlpop=nint(zsame)
c      nlpot=nint(zsame)
c      bglaub2=sigine/10./pi        !10= fm^2 -> mb
c      bglaub=sqrt(bglaub2)
c      b2x=epscrp*epscrp*bglaub2
c      b2=bk(k)**2
c      ztgx=epscrw*exp(-b2/2./b2x)*fscra(engy/egyscr)
c      zpjx=epscrw*exp(-b2/2./b2x)*fscra(engy/egyscr)
c
c      if(koll.gt.1)then
c        do li=1,lproj(ip)
c          kk=kproj(ip,li)
c          if(kk.ne.k)then
c            b2=bk(kk)**2
c            if(b2.le.bglaub2)nctg=nctg+1
c            ztg=ztg+epscrw*exp(-b2/2./b2x)*fscro(engy/egyscr)
c            nlpop=nlpop+nprt(kk)
c          endif
c        enddo
c        do li=1,ltarg(it)
c          kk=ktarg(it,li)
c          if(kk.ne.k)then
c            b2=bk(kk)**2
c            if(b2.le.bglaub2)ncpj=ncpj+1
c            zpj=zpj+epscrw*exp(-b2/2./b2x)*fscro(engy/egyscr)
c            nlpot=nlpot+nprt(kk)
c          endif
c        enddo
c      endif
      !  zpjx+zpj is equal to zparpro(k)
      !  ztgx+ztg is equal to zpartar(k)
      zprj=zparpro(k)  !zsame+zpj
      ztgt=zpartar(k)  !zsame+ztg
c      if(npp(ip).gt.nfctrl)stop'nfctrl too small (1)         '
c      if(npt(it).gt.nfctrl)stop'nfctrl too small (2)         '
      if(idpr(n,k).eq.0)then
        omega=omega*wzerox
      else
        xp=xppr(n,k)
        xm=xmpr(n,k)
c        !-------------------------------------------------------------------------
c        ! fom : part of Phi regularization; Phi -> Phi^(n) (n = number of Poms)
c        ! Phi^(0) relevant for Xsect unchanged, apart of (maybe) normalization (Z)
c        !-------------------------------------------------------------------------
        omega=omega*omGamk(k,xp,xm)*gammaV(k)*fom(zprj,xm,bk(k))
     &                                       *fom(ztgt,xp,bk(k))
      endif

      omega=omega*PhiExpoK(k,xpp(ip),xmt(it))


      if(omega.le.0.d0)goto 1001

      if(koll.gt.1)then
        do li=1,lproj(ip)
          kk=kproj(ip,li)
          if(itarg(kk).ne.it)then
            ipl=iproj(kk)
            itl=itarg(kk)
            omega=omega*PhiExpoK(kk,xpp(ipl),xmt(itl))
            if(omega.le.0.d0)goto 1001
          endif
        enddo
        do li=1,ltarg(it)
          kk=ktarg(it,li)
          if(iproj(kk).ne.ip)then
            ipl=iproj(kk)
            itl=itarg(kk)
            omega=omega*PhiExpoK(kk,xpp(ipl),xmt(itl))
            if(omega.le.0.d0)goto 1001
          endif
        enddo
      endif

      if(omega.lt.1.d-100)then
        if(ish.ge.6)write(*,*)'omega-exit',omega
        omega=0.d0
      elseif(omega.gt.1.d100)then
        if(ish.ge.6)write(*,*)'omega-exit',omega
        omega=0.d0
      endif

      return

 1001 continue

      omega=0.d0
      return

      end

c-------------------------------------------------------------------------
      double precision function fom(z,x,b)
c-------------------------------------------------------------------------
      include 'epos.inc'
      double precision x,u,w,z0
      !----------------------------------------------------------------
      ! part of Phi regularization; Phi -> Phi^(n) (n = number of Poms)
      ! Phi^(0) relevant for Xsect unchanged
      !----------------------------------------------------------------
      fom=1d0
      if(z.gt.0..and.alpfomi.gt.0.)then
       z0=dble(alpfomi)
       u=dble(z**gamfom)
c       u=z0*dble(z/z0)**2.
       w=u/z0*exp(-dble(b*b/delD(1,iclpro,icltar)))
c       w=10.d0*u
       !---------------------------------------------------
       !e=exp(-0.05*u)  !analytic function with e(0)=1
       !fom=((1-u)+(u+w)*sqrt(x**2+((u-1+e)/(u+w))**2))
       !     fom(z=0)=1  fom(x=0)=e  fom(x=1)~w
       !---------------------------------------------------
       fom=1.d0+w*x**betfom
       !---------------------------------------------------
      endif
      end

c-------------------------------------------------------------------------
      subroutine ProNucSpl(ir,ii)
c-------------------------------------------------------------------------
c propose nuclear splitting
c for proj (iep) if ir=1 or target (iet) if ir=-1
c If remnant full of parton, force excitation to mimic fan diagram connections
c-------------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incsem'
      include 'epos.incems'
      double precision alp,eps,xrr,zfrac(kollmx),zsum,xk,proba,xp,xm,xr0
     &,drangen,omGamk,PomInt!,PomIncPExact,PomIncMExact
      integer knopp(kollmx)
      parameter(eps=1.d-10)


      if(ir.eq.1)then                   !proj

        ip=ii
        zzz=zzremn(ip,1)!excite more if many nucleon connected or if in nucleus
        if(ish.ge.4)write(ifch,*)'ProNucSpl proj:',ip,zzz
        r=rangen()
        if(r.gt.exp(-min(50.,zrminc*zzz)))then
          iep(ip)=5
          if(kolp(ip).eq.0)then
          if(1.d0-xpp(ip).gt.eps)stop'ProNucSpl: should not happen (2)'
            alp=1.d0/(1.d0+dble(alplea(iclpro)))
            ncon=0
            zsum=0d0
            do l=1,lproj3(ip)
              kp=kproj3(ip,l)
              it=itarg(kp)
              if(kolt(it).gt.0)then
                do m=1,ltarg3(it)
                  kt=ktarg3(it,m)
                  if(itpr(kt).gt.0)then
                    do n=1,nprmx(kt)
                      if(xpr(n,kt).gt.xzcutpar(kt))then
                        ncon=ncon+1
                        knopp(ncon)=kt
                        zfrac(ncon)=dble(zparpro(kt))
                        zsum=zsum+zfrac(ncon)
                      endif
                    enddo
                  endif
                enddo
              endif
            enddo
            if(ish.ge.4)write(ifch,*)'ProNucSpl zsum:',zsum,ncon
            if(zsum.gt.0d0)then
              xr0=xpp(ip)-drangen(xpp(ip))**alp
              xrr=xr0
              if(ish.ge.6)write(ifch,*)'xrr:',xrr
              do nc=1,ncon
                k=knopp(nc)
                xk=zfrac(nc)/zsum*xr0
                if(ish.ge.6)write(ifch,*)'xk:',nc,k,xk
                ipp=iproj(k)
                itt=itarg(k)
                do n=1,nprmx(k)
                  if(xpr(n,k).gt.xzcutpar(k))then
                    xp=xppr(n,k)+xk
                    if(xp.lt.1d0)then
c accept xp with probability GFF/PomIncExact
                     PomInt=PomInck(k)
                     if(PomInt.gt.0d0)then
                      proba=omGamk(k,xp,xmpr(n,k))
     &                   *xrr**dble(alplea(iclpro))
     &                   *xpp(ipp)**dble(alplea(iclpro))
     &                   *xmt(itt)**dble(alplea(icltar))
     &                   /PomInt
                      if(drangen(proba).lt.proba)then !accept xp for pair k
                        xppr(n,k)=xp
                        xpr(n,k)=xppr(n,k)*xmpr(n,k)
                        ypr(n,k)=0.5D0*log(xppr(n,k)/xmpr(n,k))
                        xpp(ip)=xpp(ip)-xk
                        knucnt(1,k)=knucnt(1,k)+1 !store info of momentum transfer
                        irnuc(knucnt(1,k),1,k)=ip !in case of virpom later
                        npnuc(knucnt(1,k),1,k)=n
                        xxnuc(knucnt(1,k),1,k)=xk
                        if(ish.ge.6)write(ifch,*)'Transfer:'
     &                                          ,knucnt(1,k),k,n,xk,ip
                        goto 10
                      endif
                     endif
                    endif
                  endif
                enddo
                xrr=xrr-xk
 10             continue
              enddo
              if(xrr.lt.eps)then
                iep(ip)=0       !excitation not possible
                zzremn(ip,1)=0.
              endif
              if(ish.ge.4)write(ifch,*)'ProNucSpl out:',iep(ip),xrr
            else
              iep(ip)=0
              zzremn(ip,1)=0.
              if(ish.ge.4)write(ifch,*)'ProNucSpl out:',iep(ip)
            endif
          else
            if(ish.ge.4)write(ifch,*)'ProNucSpl out:',iep(ip)
          endif
        else
          iep(ip)=0
        endif


      elseif(ir.eq.-1)then      !targ

        it=ii
        zzz=zzremn(it,2)!excite more if many nucleon connected or if in nucleus
        if(ish.ge.4)write(ifch,*)'ProNucSpl targ:',it,zzz
        r=rangen()
        if(r.gt.exp(-min(50.,zrminc*zzz)))then
          iet(it)=5
          if(kolt(it).eq.0)then
          if(1.d0-xmt(it).gt.eps)stop'ProNucSpl: should not happen (4)'
            alp=1.d0/(1.d0+dble(alplea(icltar)))
            ncon=0
            zsum=0d0
            do l=1,ltarg3(it)
              kt=ktarg3(it,l)
              ip=iproj(kt)
              if(kolp(ip).gt.0)then
                do m=1,lproj3(ip)
                  kp=kproj(ip,m)
                  if(itpr(kp).gt.0)then
                    do n=1,nprmx(kp)
                      if(xpr(n,kp).gt.xzcutpar(kp))then
                        ncon=ncon+1
                        knopp(ncon)=kp
                        zfrac(ncon)=dble(zpartar(kp))
                        zsum=zsum+zfrac(ncon)
                      endif
                    enddo
                  endif
                enddo
              endif
            enddo
            if(ish.ge.4)write(ifch,*)'ProNucSpl zsum:',zsum,ncon
            if(zsum.gt.0d0)then
              xr0=xmt(it)-drangen(xmt(it))**alp
              xrr=xr0
              if(ish.ge.6)write(ifch,*)'xrr:',xrr
              do nc=1,ncon
                k=knopp(nc)
                xk=zfrac(nc)/zsum*xr0
                if(ish.ge.6)write(ifch,*)'xk:',nc,k,xk
                ipp=iproj(k)
                itt=itarg(k)
                do n=1,nprmx(k)
                  if(xpr(n,k).gt.xzcutpar(k))then
                    xm=xmpr(n,k)+xk
                    if(xm.lt.1d0)then
c accept xp with probability GFF/PomIncExact
                     PomInt=PomInck(k)
                     if(PomInt.gt.0d0)then
                      proba=omGamk(k,xppr(n,k),xm)
     &                     *xpp(ipp)**dble(alplea(iclpro))
     &                     *xmt(itt)**dble(alplea(icltar))
     &                     *xrr**dble(alplea(icltar))
     &                     / PomInt
                      if(drangen(proba).lt.proba)then !accept xp for pair k
                        xmpr(n,k)=xm
                        xpr(n,k)=xppr(n,k)*xmpr(n,k)
                        ypr(n,k)=0.5D0*log(xppr(n,k)/xmpr(n,k))
                        xmt(it)=xmt(it)-xk
                        knucnt(2,k)=knucnt(2,k)+1 !store info of momentum transfer
                        irnuc(knucnt(2,k),2,k)=it !in case of virpom later
                        npnuc(knucnt(2,k),2,k)=n
                        xxnuc(knucnt(2,k),2,k)=xk
                        if(ish.ge.6)write(ifch,*)'Transfer:'
     &                                          ,knucnt(2,k),k,n,xk,it
                        goto 20
                      endif
                     endif
                    endif
                  endif
                enddo
                xrr=xrr-xk
 20             continue
              enddo
              if(xrr.lt.eps)then
                iet(it)=0       !excitation not possible
                zzremn(it,2)=0.
              endif
              if(ish.ge.4)write(ifch,*)'ProNucSpl out:',iet(it),xrr
            else
              iet(it)=0
              zzremn(it,2)=0.
              if(ish.ge.4)write(ifch,*)'ProNucSpl out:',iet(it)
            endif
          else
            if(ish.ge.4)write(ifch,*)'ProNucSpl out:',iet(it)
          endif
        else
          iet(it)=0
        endif
      endif

      end

c-------------------------------------------------------------------------
      subroutine ProPoTy(k,n)
c-------------------------------------------------------------------------
c propose pomeron type
c-------------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'
      include 'epos.incsem'
      common/cems5/plc,s
      double precision s,plc
      double precision ww,w0,w1,w2,w3,w4,w5,w(0:7),aks,eps,zdiff
     *,xh,yp!,xp,xm
      parameter(eps=1.d-10)
      logical cont
      dimension nnn(3),kkk(3)

      if(idpr(n,k).eq.0)return
      ip=iproj(k)
      it=itarg(k)
      if(ish.ge.4)write(ifch,*)'ProPoTy:k,n,idpr,x',k,n,ip,it,nprt(k)
     *                                              ,idpr(n,k),xpr(n,k)
      if(idpr(n,k).ne.1)call utstop('ProPoTy: should not happen&')

      cont=.true.
      do i=1,3
        nnn(i)=0
        kkk(i)=0
      enddo

      idfpr(n,k)=1
      xh=xpr(n,k)
      yp=ypr(n,k)
c      xp=xppr(n,k)
c      xm=xmpr(n,k)
      nnn(3)=n
      kkk(3)=k

      if(iep(ip).ne.5)iep(ip)=-1
      if(iet(it).ne.5)iet(it)=-1


      idpr(n,k)=1

        w0=0.d0
        w1=0.d0
        w2=0.d0
        w3=0.d0
        w4=0.d0
        w5=0.d0

        call WomTy(w,xh,yp,k)


        if(w(0).gt.0.d0)w0=w(0)
        if(w(1).gt.0.d0)w1=w(1)
        if(iremn.ge.2)then
          if(w(2).gt.0.d0)then                 !q-g
            if(ivp(ip).gt.0)then
              w2=w(2)
            else
              w1=w1+w(2)
            endif
          endif
          if(w(3).gt.0.d0)then                 !g-q
            if(ivt(it).gt.0)then
              w3=w(3)
            else
              w1=w1+w(3)
            endif
          endif
          if(w(4).gt.0.d0)then                 !q-q
            if(ivp(ip)*ivt(it).gt.0)then
              w4=w(4)
            else
              w1=w1+w(4)
            endif
          endif
        else
          if(w(2).gt.0.d0)w2=w(2)
          if(w(3).gt.0.d0)w3=w(3)
          if(w(4).gt.0.d0)w4=w(4)
        endif
        if(w(5).gt.0.d0)w5=w(5)

        ww=w0+w1+w2+w3+w4+w5
        if(ish.ge.4)write(ifch,*)'ProPoTy:ww,ww_i'
     *       ,ww,w0/ww*100.d0,w1/ww*100.d0,w2/ww*100.d0
     *       ,w3/ww*100.d0,w4/ww*100.d0,w5/ww*100.d0


        aks=dble(rangen())*ww

        if(ww.lt.eps.or.aks.le.w0)then            !soft pomeron

          itpr(k)=-2*npommx        !Pair is not diffractive
          if(ish.ge.5)write(ifch,*)'ProPoTy:idpr',idpr(n,k)

        elseif(aks.ge.ww-w5)then !diffractive interaction

          itpr(k)=itpr(k)+2
c the probability to have a real diffractive Pomeron increase with Z and hard P
c          zdiff=exp(-min(50.d0,dble(zdfinc*(zparpro(k)+zpartar(k)))*w1))
          zdiff=1d0/sqrt(1d0+dble(zdfinc*(zparpro(k)+zpartar(k)))*w1)
c          print *,xpr(n,k),zdiff,w1!,zdiff2
          if(ish.ge.5)write(ifch,*)'ProPoTy:itpr',itpr(k),zdiff
          if(xpr(n,k).gt.xzcutpar(k).and.rangen().gt.zdiff)then
c          if(rangen().gt.zdiff)then
c High mass diffraction : Keep pomeron and choose between soft and semi-hard
            aks=dble(rangen())*(w0+w1)
            if(aks.gt.w0)then
              idpr(n,k)=3
              npr(3,k)=npr(3,k)+1
              npr(1,k)=npr(1,k)-1
              bhpr(n,k)=bk(k)
              idhpr(n,k)=0
c            elseif(iLHC.eq.1)then
c              idpr(n,k)=-1
            endif
          else
           if(iLHC.eq.1)then     !LHC tune 
c keep soft Pomeron for later 
              idpr(n,k)=-1
           else                     !original CR
c Low mass diffraction : no pomeron
c restore x from nuclear splitting
            if(knucnt(1,k).gt.0)then
              do nuc=1,knucnt(1,k)
                if(npnuc(nuc,1,k).eq.n)then
                  ipp=irnuc(nuc,1,k)
                  xpp(ipp)=xpp(ipp)+xxnuc(nuc,1,k)
                  if(xpp(ipp)-1d0.ge.-1d-10)iep(ipp)=0
                  xppr(n,k)=xppr(n,k)-xxnuc(nuc,1,k)
                  xpr(n,k)=xppr(n,k)*xmpr(n,k)
                  ypr(n,k)=0.5D0*log(xppr(n,k)/xmpr(n,k))
                  npnuc(nuc,1,k)=0 !to be sure not to use it again
                endif
              enddo
            endif
            if(knucnt(2,k).gt.0)then
              do nuc=1,knucnt(2,k)
                if(npnuc(nuc,2,k).eq.n)then
                  itt=irnuc(nuc,2,k)
                  xmt(itt)=xmt(itt)+xxnuc(nuc,2,k)
                  if(xmt(itt)-1d0.ge.-1d-10)iet(itt)=0
                  xmpr(n,k)=xmpr(n,k)-xxnuc(nuc,2,k)
                  xpr(n,k)=xppr(n,k)*xmpr(n,k)
                  ypr(n,k)=0.5D0*log(xppr(n,k)/xmpr(n,k))
                  npnuc(nuc,2,k)=0 !to be sure not to use it again
                endif
              enddo
            endif
            call RemPom(k,n)
            idfpr(n,k)=0
            npr(0,k)=npr(0,k)+1 !nr of empty cells
            kolp(ip)=kolp(ip)-1 !suppress diffractive collision from the remnant
            kolt(it)=kolt(it)-1 !it will be restored if the pair is diffractive
            if(ish.ge.6)write(ifch,*)'ProPoTy:idpr',idpr(n,k)
          endif

         endif

        else

          itpr(k)=-2*npommx        !Pair is not diffractive
          idpr(n,k)=3
          if(ish.ge.5)write(ifch,*)'ProPoTy:idpr',idpr(n,k)
          npr(3,k)=npr(3,k)+1
          npr(1,k)=npr(1,k)-1
          bhpr(n,k)=bk(k)

          aks=aks-w0
          if(aks.le.w1)then                             !gg-pomeron
            idhpr(n,k)=0
          elseif(aks.le.w1+w2)then                      !qg-pomeron
            idhpr(n,k)=1
            ivp(ip)=ivp(ip)-1
          elseif(aks.le.w1+w2+w3)then                   !gq-pomeron
            idhpr(n,k)=2
            ivt(it)=ivt(it)-1
          elseif(aks.le.w1+w2+w3+w4)then                !qq-pomeron
            idhpr(n,k)=3
            ivp(ip)=ivp(ip)-1
            ivt(it)=ivt(it)-1
          else
            call utstop('ems-unknown pomeron&')
          endif
          if(ish.ge.6)write(ifch,*)'ProPoTy:idhpr',idhpr(n,k)
     &         ,' |',ip,ivp(ip),' |',it,ivt(it)

        endif
        
        if(idfpr(n,k).eq.1)then
          antot=antot+1
          antotf=antotf+1
          if(abs(idpr(n,k)).eq.1)then
            ansf=ansf+1
            ansff=ansff+1
          endif
          if(idpr(n,k).eq.3)then
            ansh=ansh+1
            anshf=anshf+1
          endif
        endif

      do i=3,1,-1

        if(nnn(i).ne.0.and.kkk(i).ne.0.and.cont)then

          if(idpr(nnn(i),kkk(i)).eq.3)then

                       !Backup soft Pomeron if sh not possible later

            kb=kkk(i)
            nb=nnn(i)
            ip=iproj(kb)
            it=itarg(kb)
            do nn=1,nprmx(kb)
              if(idpr(nn,kb).eq.0)then !empty spot
                nbkpr(nb,kb)=nn
                nvpr(nn,kb)=nb
                idpr(nn,kb)=1
                ivpr(nn,kb)=2
                xpr(nn,kb)=xpr(nb,kb)
                ypr(nn,kb)=ypr(nb,kb)
                xppr(nn,kb)=xppr(nb,kb)
                xmpr(nn,kb)=xmpr(nb,kb)
                idfpr(nn,kb)=-idfpr(nb,kb)
                bhpr(nn,kb)=bhpr(nb,kb)
                idp1pr(nn,kb)=0
                idp2pr(nn,kb)=0
                idm1pr(nn,kb)=0
                idm2pr(nn,kb)=0
                xm1pr(nn,kb)=0.d0
                xp1pr(nn,kb)=0.d0
                xm2pr(nn,kb)=0.d0
                xp2pr(nn,kb)=0.d0
                xxm1pr(nn,kb)=0.d0
                xym1pr(nn,kb)=0.d0
                xxp1pr(nn,kb)=0.d0
                xyp1pr(nn,kb)=0.d0
                xxm2pr(nn,kb)=0.d0
                xym2pr(nn,kb)=0.d0
                xxp2pr(nn,kb)=0.d0
                xyp2pr(nn,kb)=0.d0
                goto 10
              endif
            enddo
      if(ish.ge.2)write(ifmt,*)'no empty lattice site, backup lost'

 10         continue
          endif
        endif
      enddo

      return
      end

c-------------------------------------------------------------------------
      subroutine ProDiSc(k)
c-------------------------------------------------------------------------
c propose diffractive scattering
c-------------------------------------------------------------------------

      include 'epos.incems'

      ip=iproj(k)
      it=itarg(k)
      kolp(ip)=kolp(ip)+itpr(k)/2 !number of diffractive Pomerons
      kolt(it)=kolt(it)+itpr(k)/2 !on remnants


      end

c-------------------------------------------------------------------------
      subroutine ProReEx(ir,ii)
c-------------------------------------------------------------------------
c propose remnant excitation
c for proj (iep) if ir=1 or target (iet) if ir=-1:
c 0 = no,  1 = inel excitation,  2 = diffr excitation
c fixed before : 5 = excit due to split without connection
c fixed after : 3 = large excitation due to # quark > 3
c               6 = active spectator (get small pt and used for mass)
c propose "pion exchange" process for diffractive remnant excitation
c for proj (iep) if ir=2 or target (iet) if ir=-2:
c 4 = diffr excitation but with fixed minimum excited mass
c-------------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incsem'
      include 'epos.incems'
      include 'epos.incpar'


      if(ir.eq.1)then                   !proj

        ip=ii
        mine=0
        mdif=0
        do l=1,lproj(ip)
          kp=kproj(ip,l)
          if(itpr(kp).lt.0)mine=1
          if(itpr(kp).gt.0)mdif=1
        enddo
        r=rangen()
        if(mine.eq.1)then   !inelastic
          if(iremn.eq.1)then
c increase excitation probability with number of close enough nucleons
            if(r.lt.1.-(1.-rexndi(iclpro))**(kolp(ip)
     &          *(1.+rexres(iclpro)*log(max(1.,float(lproj(ip)))))))then
              iep(ip)=1
            else
              iep(ip)=0
            endif
          elseif(iremn.ne.0)then  
c increase of inelastic remnant excitation in pA needed for Barton
            if(r.lt.1.-(1.-rexndi(iclpro))
     &         **(1.+rexres(iclpro)*float(lproj(ip)-1)))then
              iep(ip)=1
            else
              iep(ip)=0
            endif
          else!if(iremn.ne.2)then
            if(r.lt.rexndi(iclpro))then
              iep(ip)=1
            else
              iep(ip)=0
            endif
          endif
        elseif(mdif.eq.1)then        !diffr
          if(iremn.eq.1)then
c increase excitation probability with number of close enough nucleons
            if(r.lt.1.-(1.-rexdif(iclpro))**(kolp(ip)
     &          *(1.+rexres(iclpro)*log(max(1.,float(lproj(ip)))))))then
              iep(ip)=2
            else
              iep(ip)=0
            endif
          elseif(iremn.ne.0)then
c increase of diffractive remnant excitation in pA needed for Barton
            if(r.lt.1.-(1.-rexdif(iclpro))
     &        **(1.+rexres(iclpro)*float(lproj(ip)-1)))then
              iep(ip)=2
            else
              iep(ip)=0
            endif
          else
            if(r.lt.1.-(1.-rexdif(iclpro)))then
              iep(ip)=2
            else
              iep(ip)=0
            endif
          endif
        elseif(iep(ip).ne.5)then
c for non-excited spectators
          iep(ip)=0
        endif

      elseif(ir.eq.-1)then                !targ

        it=ii
        mine=0
        mdif=0
        do l=1,ltarg(it)
          kt=ktarg(it,l)
          if(itpr(kt).lt.0)mine=1
          if(itpr(kt).gt.0)mdif=1
        enddo
        r=rangen()
        if(mine.eq.1)then   !inelastic
          if(iremn.eq.1)then
            if(r.lt.1.-(1.-rexndi(icltar))**(kolt(it)
     &          *(1.+rexres(icltar)*log(max(1.,float(ltarg(it)))))))then
              iet(it)=1
            else
              iet(it)=0
            endif
          elseif(iremn.ne.0)then
c increase of inelastic remnant excitation in pA needed for Barton
            if(r.lt.1.-(1.-rexndi(icltar))
     &         **(1.+rexres(icltar)*float(ltarg(it)-1)))then
              iet(it)=1
            else
              iet(it)=0
            endif
          else
            if(r.lt.rexndi(icltar))then
              iet(it)=1
             else
              iet(it)=0
            endif
         endif
        elseif(mdif.eq.1)then        !diffr
          if(iremn.eq.1)then
              if(r.lt.1.-(1.-rexdif(icltar))**(kolt(it)
     &      *(1.+rexres(icltar)*log(max(1.,float(ltarg(it)))))))then
                iet(it)=2
              else
                iet(it)=0
              endif
            elseif(iremn.ne.0)then
c increase of diffractive remnant excitation in pA needed for Barton anb xsection
              if(r.lt.1.-(1.-rexdif(icltar))
     &      **(1.+rexres(icltar)*float(ltarg(it)-1)))then
                iet(it)=2
              else
                iet(it)=0
              endif
            else
              if(r.lt.1.-(1.-rexdif(icltar)))then
                iet(it)=2
              else
                iet(it)=0
              endif
            endif
        elseif(iet(it).ne.5)then
          iet(it)=0
        endif

      elseif(ir.eq.2)then                !proj diff excitation

c minimum mass excitation
        ip=ii
        r=rangen()
c        if(r.lt.rexpdif(iclpro))iep(ip)=4  
        if(r.lt.rexpdif(iclpro)
     & **(1.+rexres(iclpro)*float(lproj(ip)-1)))iep(ip)=4  

      elseif(ir.eq.-2)then                !targ diff excitation

cminimum mass excitation
        it=ii
        r=rangen()
c        if(r.lt.rexpdif(icltar))iet(it)=4  
        if(r.lt.rexpdif(icltar)
     & **(1.+rexres(icltar)*float(ltarg(it)-1)))iet(it)=4  

      endif

      end


c-------------------------------------------------------------------------
      subroutine ProDiPt(k,iqq,iret)
c-------------------------------------------------------------------------
c propose transverse momentum for diffractive interaction
c iqq=1  : fix pt for non diffractive pair (and check if all pairs are still valid)
c iqq=2  : diffractive pt with mass dependence
c-------------------------------------------------------------------------

      include 'epos.incems'
      include 'epos.incsem'
      include 'epos.inc'
      double precision xxe(kollmx),xye(kollmx),pt2,am0,am1,am2!,p5sqpr,p5sqtg
      double precision plc,s,xxpnew,xypnew,xxtnew,xytnew,rannorm
      common/cems5/plc,s
      common/emsptl/nppr(npommx,kollmx),npproj(mamx),nptarg(mamx)
      save xxe,xye

      ip=iproj(k)
      it=itarg(k)
      pt=0.
      phi=0.


c generate p_t for diffractive
      if(iqq.eq.1)then

       if(ptdiff.gt.0.)then
         if(itpr(k).eq.2)then
           pt=ranpt()*ptdiff/(1.+0.02*max(0.,sngl(log(s))))
         elseif(itpr(k).eq.0)then   !pt for non-wounded nucleon (usefull in ProRem to avoid problem in utrescl)
           if(iLHC.eq.1)then
             pt = sngl(RANNORM(0.088D0,0.044D0))  !limited by some data like sal.optns
           else
             ptnw=0.005
             pt=ranptd()*ptnw
           endif
           if(kolp(ip).eq.0.and.iep(ip).le.0)iep(ip)=6   !active spectators
           if(kolt(it).eq.0.and.iet(it).le.0)iet(it)=6
         else
           xxe(k)=0d0
           xye(k)=0d0
           goto 10
         endif
         phi=2.*pi*rangen()
         xxe(k)=dble(pt*cos(phi))
         xye(k)=dble(pt*sin(phi))
       else
         xxe(k)=0d0
         xye(k)=0d0
       endif

c update remnant p_t

 10    xxp(ip)=xxp(ip)-xxe(k)
       xyp(ip)=xyp(ip)-xye(k)
       xxt(it)=xxt(it)+xxe(k)
       xyt(it)=xyt(it)+xye(k)

       if(ish.ge.8)write(ifch,'(a,i5,3i4,4g13.5)')
     &                    'ProDiPt',k,ip,it,itpr(k),pt,phi,xxe(k),xye(k)

       if(itpr(k).ne.0.and.itpr(k).ne.3)iret=0
!to simulate the fact that originally we had a Pomeron
c         if(koll.le.2)then
c           call StoCon(-k,k,1)  !to fixe mass of corresponding remnants
c           xpp(ip)=xpp(ip)-xppr(1,k)
c           xpt(it)=xpt(it)+xppr(1,k)
c           xmt(it)=xmt(it)-xmpr(1,k)
c           xmp(ip)=xmp(ip)+xmpr(1,k)
c           idpr(1,k)=0
c           xpr(1,k)=0.d0
c           ypr(1,k)=0.d0
c           xppr(1,k)=0.d0
c           xmpr(1,k)=0.d0
c         endif
c         p5sqpr=xpp(ip)*xmp(ip)*s-dble(amproj*amproj)
c         p5sqtg=xpt(it)*xmt(it)*s-dble(amtarg*amtarg)
c         phi=2.*pi*rangen()
c         ntry=0
c 20      ntry=ntry+1
c         pt=ranptcut(ptsems)*ptsend**2
c         if(ntry.lt.100.and.(p5sqpr-dble(pt*pt).lt.0.d0
c     &                   .or.p5sqtg-dble(pt*pt).lt.0.d0))then
c             goto 20
c         else
c           pt=ranptcut(ptsems)*ptsendi
c         endif
c         xxe(k)=dble(pt*cos(phi))
c         xye(k)=dble(pt*sin(phi))
c         xxp(ip)=xxp(ip)-xxe(k)
c         xyp(ip)=xyp(ip)-xye(k)
c         xxt(it)=xxt(it)+xxe(k)
c         xyt(it)=xyt(it)+xye(k)
c       endif

      elseif(itpr(k).eq.2.and.ptdiff.ne.0.)then

        pt2=xxe(k)*xxe(k)+xye(k)*xye(k)
        if(pt2.gt.0d0)then
          am0=dble(amproj**2*amtarg**2)
          am1=max(dble(amproj**2),xpp(ip)*xmp(ip)*s
     &              -xxp(ip)*xxp(ip)-xyp(ip)*xyp(ip))
          am2=max(dble(amtarg**2),xpt(it)*xmt(it)*s
     &              -xxp(it)*xxp(it)-xyp(it)*xyp(it))
          ptd=ptdiff/(1.+0.02*max(0.,sngl(log(s*am0/am1/am2)))) !0.02 comes from data (Z. Phys. C 67, 227-237, 1995)
c           ad=pi/4./ptd**2
c           r=rangen()
          pt=ranpt()*ptd        !sqrt(-alog(r)/ad)
        else
          return
        endif
        if(ish.ge.8)write(ifch,'(a,i5,2i4,5g13.5)')
     &                    'ProDiPt',k,ip,it,pt,sqrt(pt2),ptd,am1,am2
c suppress the pt given with iqq=1 and give a new one taking into account the mass (iqq=2) with the same angle phi
        pt=pt/sqrt(pt2)
        xxe(k)=xxe(k)*pt
        xye(k)=xye(k)*pt

c update remnant p_t if enough energy available
        xxpnew=xxp(ip)-xxe(k)
        xypnew=xyp(ip)-xye(k)
        xxtnew=xxt(it)+xxe(k)
        xytnew=xyt(it)+xye(k)
        if((iep(ip).eq.0.or.
     &      xpp(ip)*xmp(ip)*s-xxpnew*xxpnew-xypnew*xypnew
     &      .gt.1.3d0*dble(pptl(5,npproj(ip)))**2)
     &.and.(iet(it).eq.0.or.
     &      xpt(it)*xmt(it)*s-xxtnew*xxtnew-xytnew*xytnew
     &      .gt.1.3d0*dble(pptl(5,nptarg(it)))**2))then
          xxp(ip)=xxp(ip)-xxe(k)
          xyp(ip)=xyp(ip)-xye(k)
          xxt(it)=xxt(it)+xxe(k)
          xyt(it)=xyt(it)+xye(k)
        endif

       endif

       end

c-------------------------------------------------------------------------
      subroutine ProSePt(k,n,iret)
c-------------------------------------------------------------------------
c propose transverse momentum for string ends
c-------------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'
      common/cems5/plc,s
      double precision s,plc
      double precision x1p,x2p,x1t,x2t

      if(ivpr(n,k).eq.2)return            !Backup Pomeron

      ip=iproj(k)
      it=itarg(k)
      amk0=1. ! included in ptsend !(qmass(1)+qmass(2)+qmass(3))/3.     !mass for mt distribution

      ptsecut=ptsecu        !cut for gaussian distribution (center around 0.4)

c generate p_t for string ends  (proj)
      iret=0
      ntry=0
 10   ntry=ntry+1
      xxp1pr(n,k)=0d0
      xyp1pr(n,k)=0d0
      xxp2pr(n,k)=0d0
      xyp2pr(n,k)=0d0
      xxm1pr(n,k)=0d0
      xym1pr(n,k)=0d0
      xxm2pr(n,k)=0d0
      xym2pr(n,k)=0d0
      x1p=0d0
      x2p=0d0
      x1t=0d0
      x2t=0d0
      pt=0.
      phi=0.
      if(ntry.gt.100)then
        iret=1
        goto 1000               !no pt
      endif
c
c      !---proj-----
        ptsef=ptsend
        if(iep(ip).eq.0)ptsef=ptsendi
        ptsendx = ptsems
        ptsendy = ptsendx
        if(iLHC.eq.0)ptsendy = ptsendx*2

        ipt=1

c 2 step pt : first give pt between remnant and Pomeron and then between 
c string ends on the same side.
        if(iLHC.eq.-1)ipt=2

        do ii=1,ipt

      if(idp1pr(n,k).gt.0)then
        if(ii.eq.1)then
         if(idp1pr(n,k).eq.4.or.idp1pr(n,k).eq.5)then   !diquarks
           amk1=amk0*ptsendy+qmass(0) !mass for mt distribution with bounding energy for diquark
         else
           amk1=amk0*ptsendx
         endif
c         if(iep(ip).eq.0)amk1=0.
         if(iep(ip).eq.0)then
           pt=ranptd()*ptsef
         else
           pt=ranptcut(ptsecut)*ptsef
c           pt=ranptd()*ptsef
           pt=pt+amk1
         endif
c         pt=ranptcut(ptsecut)*ptsef
c         pt=pt+amk1
c         pt=ranptd()*ptsef
c         pt=sqrt(pt*pt+amk1*amk1)
       else
         pt=ranpt()*ptfraqq
       endif
         phi=2.*pi*rangen()
         xxp1pr(n,k)=xxp1pr(n,k)+dble(pt*cos(phi))
         xyp1pr(n,k)=xyp1pr(n,k)+dble(pt*sin(phi))
      else
         xxp1pr(n,k)=0d0
         xyp1pr(n,k)=0d0
      endif
      if(idp2pr(n,k).gt.0)then
        if(ii.eq.1)then
         if(idp2pr(n,k).eq.4.or.idp2pr(n,k).eq.5)then
           amk1=amk0*ptsendy+qmass(0) !mass for mt distribution with bounding energy for diquark
         else
           amk1=amk0*ptsendx
         endif
c         if(iep(ip).eq.0)amk1=0.
         if(iep(ip).eq.0)then
           pt=ranptd()*ptsef
         else
           pt=ranptcut(ptsecut)*ptsef
c           pt=ranptd()*ptsef
           pt=pt+amk1
         endif
c         pt=ranptcut(ptsecut)*ptsef
c         pt=pt+amk1
c         pt=ranptd()*ptsef
c         pt=sqrt(pt*pt+amk1*amk1)
         phi=2.*pi*rangen()
       else    !use pt and phi from other string ends
         pt=-pt
       endif 
         xxp2pr(n,k)=xxp2pr(n,k)+dble(pt*cos(phi))
         xyp2pr(n,k)=xyp2pr(n,k)+dble(pt*sin(phi))
      else
         xxp2pr(n,k)=0d0
         xyp2pr(n,k)=0d0
      endif
c generate p_t for string ends  (targ)


c      !---targ-----
        ptsef=ptsend
        if(iet(it).eq.0)ptsef=ptsendi
        ptsendx = ptsems
        ptsendy = ptsendx
        if(iLHC.eq.0)ptsendy = ptsendx*2.

      if(idm1pr(n,k).gt.0)then
        if(ii.eq.1)then
         if(idm1pr(n,k).eq.4.or.idm1pr(n,k).eq.5)then
           amk1=amk0*ptsendy+qmass(0) !mass for mt distribution with bounding energy for diquark
         else
           amk1=amk0*ptsendx
         endif
c         if(iet(it).eq.0)amk1=0.
         if(iet(it).eq.0)then
           pt=ranptd()*ptsef
         else
           pt=ranptcut(ptsecut)*ptsef
c           pt=ranptd()*ptsef
           pt=pt+amk1
         endif
c         pt=ranptcut(ptsecut)*ptsef
c         pt=pt+amk1
c         pt=ranptd()*ptsef
c         pt=sqrt(pt*pt+amk1*amk1)
       else
         pt=ranpt()*ptfraqq
       endif
         phi=2.*pi*rangen()
         xxm1pr(n,k)=xxm1pr(n,k)+dble(pt*cos(phi))
         xym1pr(n,k)=xym1pr(n,k)+dble(pt*sin(phi))
      else
         xxm1pr(n,k)=0d0
         xym1pr(n,k)=0d0
      endif
      if(idm2pr(n,k).gt.0)then
        if(ii.eq.1)then
         if(idm2pr(n,k).eq.4.or.idm2pr(n,k).eq.5)then
           amk1=amk0*ptsendy+qmass(0) !mass for mt distribution with bounding energy for diquark
         else
           amk1=amk0*ptsendx
         endif
c         if(iet(it).eq.0)amk1=0.
         if(iet(it).eq.0)then
           pt=ranptd()*ptsef
         else
           pt=ranptcut(ptsecut)*ptsef
c           pt=ranptd()*ptsef
           pt=pt+amk1
         endif
c         pt=ranptcut(ptsecut)*ptsef
c         pt=pt+amk1
c         pt=ranptd()*ptsef
c         pt=sqrt(pt*pt+amk1*amk1)
         phi=2.*pi*rangen()
       else    !use pt and phi from other string ends
         pt=-pt
       endif 
         xxm2pr(n,k)=xxm2pr(n,k)+dble(pt*cos(phi))
         xym2pr(n,k)=xym2pr(n,k)+dble(pt*sin(phi))
      else
         xxm2pr(n,k)=0d0
         xym2pr(n,k)=0d0
      endif

      if(ii.eq.1)then    !balance pt bwteen string ends and remnant

        x1p=xxp(ip)-xxp1pr(n,k)-xxp2pr(n,k)
        x2p=xyp(ip)-xyp1pr(n,k)-xyp2pr(n,k)
        x1t=xxt(it)-xxm1pr(n,k)-xxm2pr(n,k)
        x2t=xyt(it)-xym1pr(n,k)-xym2pr(n,k)

        if(iLHC.eq.1)then       !check energy
          if(x1p**2+x2p**2+2.*amproj**2.ge.xpp(ip)*s)goto 10
          if(x1t**2+x2t**2+2.*amtarg**2.ge.xmt(it)*s)goto 10
        endif

      endif

      if(ish.ge.8)write(ifch,*) 'ProSePt',ii,n,k
     *   ,sqrt(xxp1pr(n,k)**2+xyp1pr(n,k)**2)
     *   ,sqrt(xxp2pr(n,k)**2+xyp2pr(n,k)**2)
     *   ,sqrt(xxm1pr(n,k)**2+xym1pr(n,k)**2)
     *   ,sqrt(xxm2pr(n,k)**2+xym2pr(n,k)**2)

      enddo


c update remnant p_t (pomeron)
        xxp(ip)=x1p
        xyp(ip)=x2p
        xxt(it)=x1t
        xyt(it)=x2t

c update backup soft pomeron p_t if exist

 1000   if(nbkpr(n,k).ne.0)then
          nn=nbkpr(n,k)
          xxp1pr(nn,k)=xxp1pr(n,k)
          xyp1pr(nn,k)=xyp1pr(n,k)
          xxp2pr(nn,k)=xxp2pr(n,k)
          xyp2pr(nn,k)=xyp2pr(n,k)
          xxm1pr(nn,k)=xxm1pr(n,k)
          xym1pr(nn,k)=xym1pr(n,k)
          xxm2pr(nn,k)=xxm2pr(n,k)
          xym2pr(nn,k)=xym2pr(n,k)
        endif

        if(ish.ge.6)then
        write(ifch,*) 'ProSePt'
        write(ifch,'(4i14/4d14.3/4d14.3/)')
     * idp1pr(n,k),idp2pr(n,k),idm1pr(n,k),idm2pr(n,k)
     *,xxp1pr(n,k),xxp2pr(n,k),xxm1pr(n,k),xxm2pr(n,k)
     *,xyp1pr(n,k),xyp2pr(n,k),xym1pr(n,k),xym2pr(n,k)
        endif

        end

c-----------------------------------------------------------------------
      subroutine ProSeX(k,n,iret)
c-----------------------------------------------------------------------
c calculates x of string ends
c-----------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'
      common/cems5/plc,s
      double precision s,plc
      common/cems10/a(0:ntypmx),b(0:ntypmx),d(0:ntypmx)
      double precision a,b,d
     *,xp,xm,ap1,ap2,am1,am2,aamin1,aamin2,u
     *,xmn1,xmn2

      iret=0

      if(abs(itpr(k)).ne.1)return
      if(idpr(n,k).ne.1.or.ivpr(n,k).eq.0)return

      if(idp1pr(n,k).eq.0.and.idp2pr(n,k).eq.0
     * .and.idm1pr(n,k).eq.0.and.idm2pr(n,k).eq.0)
     *call utstop('no Pomeron in ProSex&')

      xp=xppr(n,k)
      xm=xmpr(n,k)
      ap1=a(idp1pr(n,k))
      ap2=a(idp2pr(n,k))
      am1=a(idm1pr(n,k))
      am2=a(idm2pr(n,k))
      aamin1=ammn(idp1pr(n,k)+idm2pr(n,k))
      aamin2=ammn(idp2pr(n,k)+idm1pr(n,k))
      xmn1=(aamin1**2+(xxp1pr(n,k)+xxm2pr(n,k))**2
     &               +(xyp1pr(n,k)+xym2pr(n,k))**2)/s
      xmn2=(aamin2**2+(xxp2pr(n,k)+xxm1pr(n,k))**2
     &               +(xyp2pr(n,k)+xym1pr(n,k))**2)/s

      ntry=0
 999  ntry=ntry+1
      if(ntry.gt.100)then
        iret=1
        if(ish.ge.5)write(ifch,*)'Problem in ProSex(k,n)',k,n
        return
      endif

    1 u=dble(rangen())**(1d0/(1d0+ap1))
      if(dble(rangen()).gt.(1d0-u)**ap2)goto1
      xp1pr(n,k)=u*xp
      xp2pr(n,k)=(1-u)*xp
    2 u=dble(rangen())**(1d0/(1d0+am1))
      if(dble(rangen()).gt.(1d0-u)**am2)goto2
      xm1pr(n,k)=u*xm
      xm2pr(n,k)=(1-u)*xm

      if(xp1pr(n,k)*xm2pr(n,k).lt.xmn1)then
      goto 999
c       fc=xp1pr(n,k)*xm2pr(n,k)/xmn1   !avoid virpom
c       if(fc.eq.0.)goto 999
c       xp1pr(n,k)=xp1pr(n,k)/sqrt(fc)
c       xm2pr(n,k)=xm2pr(n,k)/sqrt(fc)
      endif
      if(xp2pr(n,k)*xm1pr(n,k).lt.xmn2)then
      goto 999
c       fc=xp2pr(n,k)*xm1pr(n,k)/xmn2   !avoid virpom
c       if(fc.eq.0.)goto 999
c       xp2pr(n,k)=xp2pr(n,k)/sqrt(fc)
c       xm1pr(n,k)=xm1pr(n,k)/sqrt(fc)
      endif

      if(ish.ge.6)then
       write(ifch,*) 'ProSeX'
       write(ifch,'(2d28.3,i8)') xp,xm,ntry
       write(ifch,'(4d14.3)')xp1pr(n,k),xp2pr(n,k),xm1pr(n,k),xm2pr(n,k)
       write(ifch,'(4d14.3/)')xp1pr(n,k)*xm2pr(n,k)
     *                   ,xp2pr(n,k)*xm1pr(n,k),  xmn1, xmn2
      endif

      end
c-------------------------------------------------------------------------
      subroutine RmPt(k,n)
c-------------------------------------------------------------------------
c remove pt from pomeron
c-------------------------------------------------------------------------
      include 'epos.inc'
      include 'epos.incems'
      ip=iproj(k)
      it=itarg(k)
      xxp(ip)=xxp(ip)+xxp1pr(n,k)+xxp2pr(n,k)
      xyp(ip)=xyp(ip)+xyp1pr(n,k)+xyp2pr(n,k)
      xxt(it)=xxt(it)+xxm1pr(n,k)+xxm2pr(n,k)
      xyt(it)=xyt(it)+xym1pr(n,k)+xym2pr(n,k)
      xp1pr(n,k)=0d0
      xp2pr(n,k)=0d0
      xm1pr(n,k)=0d0
      xm2pr(n,k)=0d0
      xxm1pr(n,k)=0d0
      xym1pr(n,k)=0d0
      xxp1pr(n,k)=0d0
      xyp1pr(n,k)=0d0
      xxm2pr(n,k)=0d0
      xym2pr(n,k)=0d0
      xxp2pr(n,k)=0d0
      xyp2pr(n,k)=0d0
      idp1pr(n,k)=0
      idm2pr(n,k)=0
      idp2pr(n,k)=0
      idm1pr(n,k)=0
      end

c-------------------------------------------------------------------------
      subroutine VirPom(k,n,id)
c-------------------------------------------------------------------------
c create virtual pomeron
c virtual pomeron: ivpr(n,k)=0, otherwise ivpr(n,k)=1
c-------------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'
      include 'epos.incsem'
      common/col3/ncol,kolpt
      common/emsptl/nppr(npommx,kollmx),npproj(mamx),nptarg(mamx)
      double precision plc,s
      common/cems5/plc,s
      integer jcp(nflav,2),jct(nflav,2)
c      data nvir/0/
c      save nvir

      call utpri('VirPom',ish,ishini,3)

      if(idpr(n,k).eq.0)return

      ip=iproj(k)
      it=itarg(k)

      nnv=nvpr(n,k)
      nnb=nbkpr(n,k)

c                        nvir=nvir+1
c                   print *,'  ',id,'   ',nvir

      if(ish.ge.3)then
      write(ifch,*)"virpom ",id," (n,k)",n,k,nnb,nnv,nppr(n,k),itpr(k)
     &                            ,nprt(k),idpr(n,k),npr(1,k),npr(3,k)
      if(ish.ge.5)write(ifch,*)"remnant in",xpp(ip),xmt(it)
      endif

      if(nnv.ne.0)then
        nn=nnv
        kk=k
        if(idpr(nn,kk).eq.0)then
          nvpr(n,k)=0
        endif
      endif

      if(nnb.ne.0)then
        nn=nnb
        kk=k
        if(idpr(nn,kk).eq.0)then
          nbkpr(n,k)=0
        endif
      endif


      if(nbkpr(n,k).eq.0.and.nvpr(n,k).eq.0)then     !normal Pomeron

      npr(0,k)=npr(0,k)+1
      npp(ip)=npp(ip)-1
      npt(it)=npt(it)-1
      npr(idpr(n,k),k)=npr(idpr(n,k),k)-1
      nprt(k)=npr(1,k)+npr(3,k)
      antotf=antotf-1
      if(idpr(n,k).eq.1)ansff=ansff-1
      if(idpr(n,k).eq.3)anshf=anshf-1
      kolp(ip)=kolp(ip)-1
      kolt(it)=kolt(it)-1
      xxp(ip)=xxp(ip)+xxp1pr(n,k)+xxp2pr(n,k)
      xyp(ip)=xyp(ip)+xyp1pr(n,k)+xyp2pr(n,k)
      xxt(it)=xxt(it)+xxm1pr(n,k)+xxm2pr(n,k)
      xyt(it)=xyt(it)+xym1pr(n,k)+xym2pr(n,k)

c restore x from nuclear splitting
      if(knucnt(1,k).gt.0)then
        do nuc=1,knucnt(1,k)
          if(npnuc(nuc,1,k).eq.n)then
            ipp=irnuc(nuc,1,k)
            xpp(ipp)=xpp(ipp)+xxnuc(nuc,1,k)
            if(xpp(ipp).ge.1d0)iep(ipp)=0
            xppr(n,k)=xppr(n,k)-xxnuc(nuc,1,k)
            xpr(n,k)=xppr(n,k)*xmpr(n,k)
            ypr(n,k)=0.5D0*log(xppr(n,k)/xmpr(n,k))
            npnuc(nuc,1,k)=0    !to be sure not to use it again
          endif
        enddo
      endif
      if(knucnt(2,k).gt.0)then
        do nuc=1,knucnt(2,k)
          if(npnuc(nuc,2,k).eq.n)then
            itt=irnuc(nuc,2,k)
            xmt(itt)=xmt(itt)+xxnuc(nuc,2,k)
            if(xmt(itt).ge.1d0)iet(itt)=0
            xmpr(n,k)=xmpr(n,k)-xxnuc(nuc,2,k)
            xpr(n,k)=xppr(n,k)*xmpr(n,k)
            ypr(n,k)=0.5D0*log(xppr(n,k)/xmpr(n,k))
            npnuc(nuc,2,k)=0    !to be sure not to use it again
          endif
        enddo
      endif

      xpp(ip)=xpp(ip)+xppr(n,k)
      xmt(it)=xmt(it)+xmpr(n,k)


      if(abs(itpr(k)).eq.1.and.nprt(k).eq.0)then !no more Pomeron on this pair
        if(itpr(k).gt.0)then
          itpr(k)=2             !this pair is diffractive
          if(id.gt.0.and.iep(ip).eq.0.and.iet(it).eq.0)itpr(k)=3  !this pair is empty now
        else
          itpr(k)=3             !this pair is empty now
        endif
      endif

      endif

      istring=idp1pr(n,k)+idp2pr(n,k)+idm1pr(n,k)+idm2pr(n,k)
      if(istring.ne.0.and.iremn.ge.2)then
        if(ish.ge.7)write(ifch,*)"restore flavor:",istring

        if(idp1pr(n,k).eq.2)ivp(ip)=ivp(ip)+1 !update number of valence quark
        if(idm1pr(n,k).eq.2)ivt(it)=ivt(it)+1
        if(idp2pr(n,k).eq.2)ivp(ip)=ivp(ip)+1
        if(idm2pr(n,k).eq.2)ivt(it)=ivt(it)+1
        if(idp1pr(n,k).eq.5)idp(ip)=idp(ip)+1 !update number of valence diquark
        if(idm1pr(n,k).eq.5)idt(it)=idt(it)+1
        if(idp2pr(n,k).eq.5)idp(ip)=idp(ip)+1
        if(idm2pr(n,k).eq.5)idt(it)=idt(it)+1
        if(iLHC.eq.1)then
        if(idp1pr(n,k).eq.4)idp(ip)=idp(ip)-1 !update number of diquark
        if(idm1pr(n,k).eq.4)idt(it)=idt(it)-1
        if(idp2pr(n,k).eq.4)idp(ip)=idp(ip)-1
        if(idm2pr(n,k).eq.4)idt(it)=idt(it)-1
        endif

        if(iremn.eq.3)then      !virtual Pomeron (remove unnecessary flavors for string ends)
          do j=1,2
            do i=1,nrflav
              jcp(i,j)=jcpref(i,j,ip)
              jct(i,j)=jctref(i,j,it)
            enddo
            do i=nrflav+1,nflav
              jcp(i,j)=0
              jct(i,j)=0
            enddo
          enddo
          if(ish.ge.7)write(ifch,*)"in:",jcp,' |',jct
          iret=0

c Projectile diquark-antidiquark pair
          iaq=nint(1.5+sign(0.5,float(idproj)))
          iq=3-iaq
          if(idp1pr(n,k).eq.4)then  !diquark
c    first quark
            idum=idrafl(iclpro,jcp,iaq,'v',0,iret)      !pick anti-quark
            ntry=0
            do while (jcp(idum,iq).eq.0.and.ntry.lt.100)!look for the corresponding quark
              ntry=ntry+1
              idum=idrafl(iclpro,jcp,iaq,'v',0,iret)
            enddo
            if(ntry.lt.100)then          !if OK, then remove the pair and pick a second quark
              call idsufl3(idum,1,jcp)
              call idsufl3(idum,2,jcp)
              if(jcp(idum,1)-jcpval(idum,1,ip).lt.0) !check valence quark number
     &             jcpval(idum,1,ip)=jcpval(idum,1,ip)-1
              if(jcp(idum,2)-jcpval(idum,2,ip).lt.0)
     &             jcpval(idum,2,ip)=jcpval(idum,2,ip)-1

c   second quark
              idum=idrafl(iclpro,jcp,iaq,'v',0,iret)
              ntry2=0
              do while (jcp(idum,iq).eq.0.and.ntry2.lt.100)!look for the corresponding antiquark
                ntry2=ntry2+1
                idum=idrafl(iclpro,jcp,iaq,'v',0,iret)
              enddo
              if(ntry2.lt.100)then          !if OK, then remove the pair
                call idsufl3(idum,1,jcp)
                call idsufl3(idum,2,jcp)
                if(jcp(idum,1)-jcpval(idum,1,ip).lt.0)
     &               jcpval(idum,1,ip)=jcpval(idum,1,ip)-1
                if(jcp(idum,2)-jcpval(idum,2,ip).lt.0)
     &               jcpval(idum,2,ip)=jcpval(idum,2,ip)-1
              else          !if not (because quarks already used by other valid string), then redo event to avoid problem in flavor conservation
                if(id.ge.15)then
                  id=-1
                  return
                else
                  call utstop("Virpom:should not happen (2) !&")
                endif
              endif
            else      !if no pair has be found (because quarks already used by other valid string), then redo event to avoid problem in flavor conservation
              if(id.ge.15)then
                id=-1
                return
              else
                call utstop("Virpom:should not happen  (3) !&")
              endif
            endif

c Projectile quark-antiquark pair
          else
            idum=idrafl(iclpro,jcp,iaq,'v',0,iret)      !pick anti-quark
            ntry=0
            do while (jcp(idum,iq).eq.0.and.ntry.lt.100)  !look for the corresponding quark
              ntry=ntry+1
              idum=idrafl(iclpro,jcp,iaq,'v',0,iret)
            enddo
            if(ntry.lt.100)then          !if OK, then remove the pair
              call idsufl3(idum,1,jcp)
              call idsufl3(idum,2,jcp)
              if(jcp(idum,1)-jcpval(idum,1,ip).lt.0)
     &             jcpval(idum,1,ip)=jcpval(idum,1,ip)-1
              if(jcp(idum,2)-jcpval(idum,2,ip).lt.0)
     &             jcpval(idum,2,ip)=jcpval(idum,2,ip)-1
            else                         !if not (because quarks already used by other valid string),then redo event to avoid problem in flavor conservation
              if(id.ge.15)then
                id=-1
                return
              else
                call utstop("Virpom:should not happen (4) !&")
              endif
            endif
          endif

c Target diquark-antidiquark pair
          iaq=nint(1.5+sign(0.5,float(idtarg)))
          iq=3-iaq
          if(idm1pr(n,k).eq.4)then  !diquark
c    first quark
            idum=idrafl(icltar,jct,iaq,'v',0,iret)
            ntry=0
            do while (jct(idum,iq).eq.0.and.ntry.lt.100)
              ntry=ntry+1
              idum=idrafl(icltar,jct,iaq,'v',0,iret)
            enddo
            if(ntry.lt.100)then
              call idsufl3(idum,1,jct)
              call idsufl3(idum,2,jct)
              if(jct(idum,1)-jctval(idum,1,it).lt.0)
     &             jctval(idum,1,it)=jctval(idum,1,it)-1
              if(jct(idum,2)-jctval(idum,2,it).lt.0)
     &             jctval(idum,2,it)=jctval(idum,2,it)-1
c    second quark
              idum=idrafl(icltar,jct,1,'v',0,iret)
              ntry2=0
              do while (jct(idum,2).eq.0.and.ntry2.lt.100)
                ntry2=ntry2+1
                idum=idrafl(icltar,jct,1,'v',0,iret)
              enddo
              if(ntry2.lt.100)then
                call idsufl3(idum,1,jct)
                call idsufl3(idum,2,jct)
                if(jct(idum,1)-jctval(idum,1,it).lt.0)
     &               jctval(idum,1,it)=jctval(idum,1,it)-1
                if(jct(idum,2)-jctval(idum,2,it).lt.0)
     &               jctval(idum,2,it)=jctval(idum,2,it)-1
              else
                if(id.ge.15)then
                  id=-1
                  return
                else
                  call utstop("Virpom:should not happen (5) !&")
                endif
              endif
            else
              if(id.ge.15)then
                id=-1
                return
              else
                call utstop("Virpom:should not happen (6) !&")
              endif
            endif

c Target quark-antiquark pair
          else
            idum=idrafl(icltar,jct,1,'v',0,iret)
            ntry=0
            do while (jct(idum,2).eq.0.and.ntry.lt.100)
              ntry=ntry+1
              idum=idrafl(icltar,jct,1,'v',0,iret)
            enddo
            if(ntry.lt.100)then
              call idsufl3(idum,1,jct)
              call idsufl3(idum,2,jct)
              if(jct(idum,1)-jctval(idum,1,it).lt.0)
     &             jctval(idum,1,it)=jctval(idum,1,it)-1
              if(jct(idum,2)-jctval(idum,2,it).lt.0)
     &             jctval(idum,2,it)=jctval(idum,2,it)-1
            else
              if(id.ge.15)then
                id=-1
                return
              else
                call utstop("Virpom:should not happen (7) !&")
              endif
            endif
          endif

          if(ish.ge.7)write(ifch,*)"out:",jcp,' |',jct
          do j=1,2
            do i=1,nrflav
              jcpref(i,j,ip)=jcp(i,j)
              jctref(i,j,it)=jct(i,j)
            enddo
          enddo

        endif
      endif


      ivpr(n,k)=0
      nbkpr(n,k)=0
      nvpr(n,k)=0
      idpr(n,k)=0
      idfpr(n,k)=0
      xpr(n,k)=0d0
      ypr(n,k)=0d0
      xppr(n,k)=0d0
      xmpr(n,k)=0d0
      idp1pr(n,k)=0
      idp2pr(n,k)=0
      idm1pr(n,k)=0
      idm2pr(n,k)=0
      xm1pr(n,k)=0d0
      xp1pr(n,k)=0d0
      xm2pr(n,k)=0d0
      xp2pr(n,k)=0d0
      xxm1pr(n,k)=0d0
      xym1pr(n,k)=0d0
      xxp1pr(n,k)=0d0
      xyp1pr(n,k)=0d0
      xxm2pr(n,k)=0d0
      xym2pr(n,k)=0d0
      xxp2pr(n,k)=0d0
      xyp2pr(n,k)=0d0

       if(ish.ge.5)write(ifch,*)"remnant out",xpp(ip),xmt(it),itpr(k)

      call utprix('VirPom',ish,ishini,3)

      end

c-----------------------------------------------------------------------
      subroutine StoRe(imod)
c-----------------------------------------------------------------------
c Store Remnant configuration (imod=1) before shuffle  to restore the
c initial configuration (imod=-1) in case of problem.
c-----------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'

      if(imod.eq.1)then

c       initialize projectile

        do i=1,maproj
          iepst(i)=iep(i)
          xppst(i)=xpp(i)
          xmpst(i)=xmp(i)
          xposst(i)=xpos(i)
        enddo

c       initialize target

        do j=1,matarg
          ietst(j)=iet(j)
          xmtst(j)=xmt(j)
          xptst(j)=xpt(j)
          xtosst(j)=xtos(j)
        enddo

      elseif(imod.eq.-1)then

c       restore projectile

        do i=1,maproj
          iep(i)=iepst(i)
          xpp(i)=xppst(i)
          xmp(i)=xmpst(i)
          xpos(i)=xposst(i)
        enddo

c       restore target

        do j=1,matarg
          iet(j)=ietst(j)
          xmt(j)=xmtst(j)
          xpt(j)=xptst(j)
          xtos(j)=xtosst(j)
        enddo

      else

        call utstop('Do not know what to do in StoRe.&')

      endif

      return
      end

c-----------------------------------------------------------------------
      subroutine UpdateFlav(ir,jc,mod)
c-----------------------------------------------------------------------
C Add valence quark to sea quarks in projectile jcpref (mod=10) or target
c jctref (mod=20) for soft string ends (mod=0 reset jcrpref and
c jctref to 0).
c For mod=1 or 2, save jcref into jc.
c For mod=-1 or -2, put jc into jcref.
c-----------------------------------------------------------------------
      include 'epos.inc'
      include 'epos.incems'
      include 'epos.incsem'
      dimension ic(2),jc(nflav,2),jc2(nflav,2)

      if(mod.eq.0)then
        do j=1,2
          do i=1,nrflav
            jcpref(i,j,ir)=0
            jctref(i,j,ir)=0
          enddo
        enddo
      elseif(mod.eq.-1)then
        do j=1,2
          do i=1,nrflav
            jcpref(i,j,ir)=jc(i,j)
          enddo
        enddo
      elseif(mod.eq.-2)then
        do j=1,2
          do i=1,nrflav
            jctref(i,j,ir)=jc(i,j)
          enddo
        enddo
      elseif(mod.eq.1)then
        do j=1,2
          do i=1,nrflav
            jc(i,j)=jcpref(i,j,ir)
          enddo
        enddo
      elseif(mod.eq.2)then
        do j=1,2
          do i=1,nrflav
            jc(i,j)=jctref(i,j,ir)
          enddo
        enddo
      elseif(mod.eq.10)then
        ic(1)=icproj(1,ir)
        ic(2)=icproj(2,ir)
        call iddeco(ic,jc)
        itest=0
        do j=1,2
          do i=1,nrflav
            jcpref(i,j,ir)=jcpref(i,j,ir)+jc(i,j)
          enddo
        enddo

c cancel quark and antiquarks to avoid to much remnant excitation
        do i=1,nrflav

          if(iLHC.eq.1)then

          if(jcpref(i,1,ir).ge.jcpref(i,2,ir))then
            jcpref(i,1,ir)=jcpref(i,1,ir)-jcpref(i,2,ir)
            jcpref(i,2,ir)=0
c update valence quarks (cancel first sea quarks)
            if(jcpref(i,1,ir)-jc(i,1).lt.0)jc(i,1)=jcpref(i,1,ir)
            jc(i,2)=0
          else
            jcpref(i,2,ir)=jcpref(i,2,ir)-jcpref(i,1,ir)
            jcpref(i,1,ir)=0
c update valence quarks (cancel first sea quarks)
            if(jcpref(i,2,ir)-jc(i,2).lt.0)jc(i,2)=jcpref(i,2,ir)
            jc(i,1)=0
          endif

          endif

          do j=1,2
            itest=itest+jcpref(i,j,ir)
            jc2(i,j)=jcpref(i,j,ir)
          enddo
        enddo
        if(itest.eq.0)then !do not leave empty remnant
          idum=idrafl(iclpro,jc2,1,'r',3,iretso)     !create q-qb
          do j=1,2
            do i=1,nrflav
              jcpref(i,j,ir)=jc2(i,j)
            enddo
          enddo
        endif
      if(ish.ge.6)write(ifch,'(a,i3,a,1x,4i3,3x,4i3)')
     & 'jcpref(',ir,') ini:',((jcpref(i,j,ir),i=1,nflavems),j=1,2)
      elseif(mod.eq.20)then
        ic(1)=ictarg(1,ir)
        ic(2)=ictarg(2,ir)
        call iddeco(ic,jc)
        itest=0
        do j=1,2
          do i=1,nrflav
            jctref(i,j,ir)=jctref(i,j,ir)+jc(i,j)
          enddo
        enddo

        do i=1,nrflav

          if(iLHC.eq.1)then

c cancel quark and antiquarks to avoid to much remnant excitation
          if(jctref(i,1,ir).ge.jctref(i,2,ir))then
            jctref(i,1,ir)=jctref(i,1,ir)-jctref(i,2,ir)
            jctref(i,2,ir)=0
c update valence quarks (cancel first sea quarks)
            if(jctref(i,1,ir)-jc(i,1).lt.0)jc(i,1)=jctref(i,1,ir)
            jc(i,2)=0
          else
            jctref(i,2,ir)=jctref(i,2,ir)-jctref(i,1,ir)
            jctref(i,1,ir)=0
c update valence quarks (cancel first sea quarks)
            if(jctref(i,2,ir)-jc(i,2).lt.0)jc(i,2)=jctref(i,2,ir)
            jc(i,1)=0
          endif
          
          endif

          do j=1,2
            itest=itest+jctref(i,j,ir)
            jc2(i,j)=jctref(i,j,ir)
          enddo
        enddo
        if(itest.eq.0)then !do not leave empty remnant
          idum=idrafl(icltar,jc2,1,'r',3,iretso)     !create q-qb
          do j=1,2
            do i=1,nrflav
              jctref(i,j,ir)=jc2(i,j)
            enddo
          enddo
        endif
      if(ish.ge.6)write(ifch,'(a,i3,a,1x,4i3,3x,4i3)')
     & 'jctref(',ir,') ini:',((jctref(i,j,ir),i=1,nflavems),j=1,2)
      else
        stop'mod not recognized in UpdateFlav'
      endif
      end

c-----------------------------------------------------------------------
      subroutine CalcZZ(ir,m)
c-----------------------------------------------------------------------
C Calculates zz for remnant m for proj (ir=1) or target (ir=-1)
c   writes it to zzremn(m, 1 or 2)
c-----------------------------------------------------------------------
      include 'epos.inc'
      include 'epos.incems'
      include 'epos.incpar'
      if(isplit.eq.1)then
        if(ir.eq.1)then
          zz=0.
          if(lproj3(m).ge.1)then
            do l=1,lproj3(m)
              kpair=kproj3(m,l)
              zpar=zparpro(kpair)
              zz=zz+min(zpar,epscrx)
             enddo
          endif
          zzremn(m,1)=zz
       elseif(ir.eq.-1)then
          zz=0
          if(ltarg3(m).ge.1)then
            do l=1,ltarg3(m)
              kpair=ktarg3(m,l)
              zpar=zpartar(kpair)
              zz=zz+min(zpar,epscrx)
            enddo
          endif
          zzremn(m,2)=zz
        else
          stop'CalcZZ: invalid option.          '
        endif
      else
        if(ir.eq.1) zzremn(m,1)=0
        if(ir.eq.-1)zzremn(m,2)=0
      endif
      end

c-----------------------------------------------------------------------
      subroutine WriteZZ(ir,irem)
c-----------------------------------------------------------------------
c Write Z into zpaptl(K) for connected strings
c                 K is the index for the string end
c                 on the corresponding remnant side
c-----------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'
      common/emsptl/nppr(npommx,kollmx),npproj(mamx),nptarg(mamx)
      common/cems5/plc,s
      double precision s,plc

      if(ir.eq.1)then
        jrem=1
      elseif(ir.eq.-1)then
        jrem=2
      else
        jrem=0
        call utstop("Wrong ir in WriteZZ !&")
      endif

      do li=1,lremn(irem,jrem)
        kkk=kremn(irem,li,jrem)
c        ip=iproj(kkk)
c        it=itarg(kkk)
         amtot=0.
         do n=1,nprmx(kkk)
           if(idpr(n,kkk).ne.0)amtot=amtot+sngl(xpr(n,kkk)*s)
         enddo
         amtot=sqrt(amtot)
         do n=1,nprmx(kkk)
          if(idpr(n,kkk).ne.0)then
           npom=nppr(n,kkk)
c              write(ifch,*)'remn',irem,' (',jrem,' )     pom',npom
c     &            ,'    ',zzremn(irem,jrem)
           ie=0
           is1=0
           if(ifrptl(1,npom).gt.0)then
            do is=ifrptl(1,npom),ifrptl(2,npom)
              if(ie.eq.0)is1=is
              if(idptl(is).ne.9)ie=ie+1
              if(ie.eq.2)then
               is2=is
               ie=0
               if(ir.eq. 1)then
c  Z for remnant ip (low if alone and high in nucleus)
                 zpaptl(1,is1)=zzremn(irem,jrem)
c  sum of Z of remnant itt linked to ip (high if connected to many other remn)
                 zpaptl(2,is1)=amtot !float(nprt(kkk)) !float(lproj(ip))
c                 zpaptl(2,is1)=0.
c                 if(lproj(ip).ge.1)then
c                   do l=1,lproj(ip)
c                     kpair=kproj(ip,l)
c                     itt=itarg(kpair)
c                     zpaptl(2,is1)=zpaptl(2,is1)+zzremn(itt,2)
c                   enddo
c                 endif
               endif
               if(ir.eq.-1)then
c  Z for remnant it (low if alone and high in nucleus)
                 zpaptl(1,is2)=zzremn(irem,jrem)
c  sum of Z of remnant ipp linked to it (high if connected to many other remn)
                 zpaptl(2,is2)=float(nprt(kkk)) !float(ltarg(it))
c                 zpaptl(2,is2)=0.
c                 if(ltarg(it).ge.1)then
c                   do l=1,ltarg(it)
c                     kpair=ktarg(it,l)
c                     ipp=iproj(kpair)
c                     zpaptl(2,is2)=zpaptl(2,is2)+zzremn(ipp,1)
c                   enddo
c                 endif
               endif
c               do isi=is1,is2
c                write(ifch,*)' ',isi,idptl(isi),zpaptl(1,isi),zpaptl(2,isi)
c               enddo
              endif
            enddo
           endif
          endif
        enddo
      enddo

      end

c-----------------------------------------------------------------------
      subroutine ProReM(ir,irem,iret)
c-----------------------------------------------------------------------
c propose remnant mass of remnant irem in case of proj (ir=1)
c or target (ir=-1)
c   (-> xmp, xpt)
c iret : input : if iret=10 force to give mass even if no more energy,
c        when input not 10 : output = error if 1
c Energy is taken only from the other side nucleon which are close enough
c to form a pair even if that pair was not used for a collision.
c-----------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'
      include 'epos.incsem'
      double precision rr,xxx,xmin,xmax,msmin,xmmin,xpt2rem,xtest0,xtmp
      double precision at,alp,xi,xii,eps,sx,xmin0,xtest(mamx),fxtest
      parameter(eps=1.d-20)
      common/cemsr5/at(0:1,0:5)
      double precision plc,s,p5sq,aremn,aremnex,xxmax,drangen!,xmdrmax
      common/cems5/plc,s
      integer icrmn(2),jc(nflav,2)
      logical cont,force,drop,excited
      character cremn*4
      dimension k2j(mamx)

      call utpri('ProReM',ish,ishini,5)

      if(iret.eq.10)then
        force=.true.
      else
        iret=0
        force=.false.
      endif
      ntrymx=50
      do j=1,2
        do i=1,nflav
          jc(i,j)=0
        enddo
      enddo

c uncomment the following two lines to force the excitation

ccc      force=.true.   !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
ccc      ntrymx=1       !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

c initial definitions

      ntry=0
      iremo1=0
      jremo=0
c... initialize
      jrem=0.0
      amremn=0.0
      if(ir.eq.1)then
        cremn='targ'
        jrem=1
        jremo=2
        masso=lproj(irem)     !number of target nucleon linked to irem
        do k=1,masso
          k2j(k)=itarg(kproj(irem,k))
          xme(k2j(k))=0.d0
        enddo
        icrmn(1)=icremn(1,irem,jrem)
        if(icrmn(1).eq.999999)then    !more than 9 quark : use jcpref
          do j=1,2
            do i=1,nrflav
              jc(i,j)=jcpref(i,j,irem)
            enddo
          enddo
        else
          icrmn(2)=icremn(2,irem,jrem)
          call iddeco(icrmn,jc)
        endif
        amremn=amproj
           !idx=isign(iabs(idproj)/10*10+1,idproj)
           !call idmass(idx,amremn)
        iremo1=itarg(1)
        msmin=dble(amremn*amremn)
        zz=1.
        if(iez(irem,jrem).eq.3.or.iez(irem,jrem).eq.5)
     &  zz=zz+zzremn(irem,1)*zmsinc
      elseif(ir.eq.-1)then
        cremn='proj'
        jrem=2
        jremo=1
        masso=ltarg(irem)  !number of projectile nucleon linked to irem
        do k=1,masso
          k2j(k)=iproj(ktarg(irem,k))
          xme(k2j(k))=0.d0
        enddo
        icrmn(1)=icremn(1,irem,jrem)
        if(icrmn(1).eq.999999)then    !more than 9 quark : use jctref
          do j=1,2
            do i=1,nrflav
              jc(i,j)=jctref(i,j,irem)
            enddo
          enddo
        else
          icrmn(2)=icremn(2,irem,jrem)
          call iddeco(icrmn,jc)
        endif
        amremn=amtarg
           !idx=isign(iabs(idtarg)/10*10+1,idtarg)
           !call idmass(idx,amremn)
        iremo1=iproj(1)
        msmin=dble(amremn*amremn)
        zz=1.
        if(iez(irem,jrem).eq.3.or.iez(irem,jrem).eq.5)
     &  zz=zz+zzremn(irem,2)*zmsinc
      endif
      drop=.false.
      if(iremn.ge.2.and.(iez(irem,jrem).eq.3.or.iez(irem,jrem).eq.5))
     &   drop=.true.
      excited=.false.
      if(iez(irem,jrem).gt.0.and.iez(irem,jrem).ne.6)
     &   excited=.true.

c for spectators only low mass and few partners, so do not care about energy
      if(iez(irem,jrem).eq.6)force=.true.

c defs

      sx=s*xpz(irem,jrem)
      xpt2rem=xxz(irem,jrem)**2d0+xyz(irem,jrem)**2d0

c  fremnux (+) and not fremnux2 (-) which gives a mass too low in case of gethadron where q and aq do not cancel


      if(excited)then
        aremn=dble(max(amremn,fremnux(jc)))
c       if(iremn.eq.2.and.iez(irem,jrem).eq.3)      !droplet
c     &     aremn=dble(max(amremn,fremnux(jc)))
        if(iremn.ge.2)then
          aremnex=aremn+amemn(idz(irem,jrem),iez(irem,jrem))
c         if(drop)aremnex=aremnex*zz
        else
          aremnex=aremn+amemn(idz(irem,jrem),iez(irem,jrem))
        endif
      elseif(iLHC.eq.1)then !minimum mass for spectators should be as low as possible
        aremn=amremn
        aremnex=dble(max(amremn,fremnux2(jc)))
      else    !minimum mass for spectators should be as low as possible
        aremn=dble(max(amremn,fremnux2(jc)))
        aremnex=aremn
      endif


      if(ish.ge.8)write(ifch,10)ir,irem,masso,icrmn,iez(irem,jrem),force
     &                         ,amremn,fremnux(jc),aremn,aremnex
     &                         ,xpz(irem,jrem),xpt2rem,sx
 10   format('prorem :  ',i3,2i4,2i7,i2,L2,/
     &      ,'    mass :',4g13.5,/
     &      ,' x,pt,sx :',3g13.5)
c ntry

    1 ntry=ntry+1
      if(ntry.gt.ntrymx)then
        if(ish.ge.5)then
          call utmsg('ProReM')
          write(ifch,*)'Remnant mass assignment not possible (ntry)'
     &                 ,ir,irem
          if(force)write(ifch,*)'Ignore p4 conservation'
          call utmsgf
        endif
        if(.not.force)then
          iret=1
          goto 1000
        else
c not enough energy availabe : force last mass and check
          goto 900
        endif
      endif

c check

      if(xpz(irem,jrem).le.0.d0)then
        write(ifch,*)'ProRem ipp',xpz(irem,jrem)
     &                           ,jrem,irem,lremn(irem,jrem)
        do li=1,lremn(irem,jrem)
          kkk=kremn(irem,li,jrem)
          write(ifch,*)'kkk',kkk
        enddo
        call XPrint('ProRem :&')
        call utstop('Big problem in ProRem !&')
      endif

c xtest = xminus-max,  corresponding mostly to a remnant mass 0.2

      xtest0=0.d0
      fxtest=0.4d0*(1d0+drangen(xxx)) !1.d0 !0.3d0
      do k=1,masso
        j=k2j(k)
        cont=.false.
ctp        if(xmz(j,jremo).gt.eps.and.iez(j,jrem).gt.0)then !xmz(,jremo)=xplus
ctp060824        if(xmz(j,jremo).gt.eps.and.iez(j,jrem).ge.0)then !xmz(,jremo)=xplus
c        if(iez(j,jremo).gt.0.or.koll.eq.1)then !xmz(,jremo)=xplus
          if(xmz(j,jremo).gt.eps)then !xmz(,jremo)=xplus
            cont=.true.
            xmmin=xzos(j,jremo)/xmz(j,jremo)
          else
            xmmin=xzos(j,jremo)
          endif
          xtest(j)=xpz(j,jremo)-xmmin !maximal momentum available
!this term is very important for non excited remnants in pp, it changes the xf
! distribution of proton and the multiplicity at low energy. Fxtest should not
! be to close to 0. otherwise it makes a step in xf distribution of p at
! 1-fxtest but if fxtest=1, multiplicity at low energy is too high ...
! but better (and smoother) with exponential decrease).
          if(.not.cont)then
            if(xtest(j).gt.0d0)then
              xtest(j)=min(xtest(j),fxtest/xpz(irem,jrem))
            else
              xtest(j)=min(1.d0,fxtest/xpz(irem,jrem))
            endif
          endif
c        else
c          xtest(j)=0.01d0 !maximal momentum available for non exited state
c        endif
         xtest0=max(xtest0,xtest(j))
c        print *,iep(1),iet(1),iez(irem,jrem),xtest(j),xpz(j,jremo),xmmin
c     & ,xzos(j,jremo),xmz(j,jremo)
      enddo
ctp060824      if(.not.cont)xtest=min(1.d0,0.2d0/xpz(irem,jrem))



c determine xminus

c      xmin0=1.05*(aremn**2d0+xxz(irem,jrem)**2d0+xyz(irem,jrem)**2d0)/sx
c      xmin=1.1*(aremnex**2d0+xxz(irem,jrem)**2d0+xyz(irem,jrem)**2d0)/sx
      xmin0=1.1d0*(aremn**2d0+xpt2rem)/sx
      if(iLHC.eq.1.and.xmin0.ge.1.d0)xmin0=min(xmin0,0.9d0)
      if(ish.ge.1.and.xmin0.ge.1d0)
     &   write(ifch,*)"Warning in ProReM with xmin0 !"
      
      if(iez(irem,jrem).eq.4)then !pion exchange, minim should not change
        xmin=dble(xmindiff)*(aremnex**2d0+xpt2rem)/sx
      else
        xmin=dble(xminremn)*(aremnex**2d0+xpt2rem)/sx
      endif
c      xmax=min(1.d6/s,xtest0)             !to avoid ultra high mass remnants
      xmax=xtest0
c for diffractive remnant, mass should never exceed 5% of the proj or targ energy
c      if(iez(irem,jrem).eq.1)then
c        xmax=min(xmax,max(dble(xminremn),xmin))
c      elseif(iez(irem,jrem).eq.2)then
c        xmax=min(xmax,max(dble(xmindiff),xmin))
c      endif
c      if(iez(irem,jrem).eq.1.or.iez(irem,jrem).eq.3)then
c       xtmp=max(dble(min(1.,xminremn*float(maproj+matarg-1))),xmin)
c     &               *drangen(xmin)
      xtmp=1.d0
      if(excited)then
      if(iez(irem,jrem).eq.2)then
c        xtmp=max(min(1d0,dble(xmindiff)),xmin)!*drangen(xmin)
        xtmp=min(1d0,dble(xmxrem)*dble(masso)
     &                      *drangen(xmin)**0.05)
c        xtmp=dble(xmindiff)
      elseif(iez(irem,jrem).eq.1)then
        xtmp=min(1d0,dble(xmxrem)*dble(masso)
     &                      *drangen(xmin)**0.05)
c        xtmp=dble(xminremn)
      elseif(drop)then     !3 or 5
c       xtmp=max(dble(min(1.,xminremn*float(maproj+matarg-1))),xmin)
c     &               *drangen(xmin)
        xtmp=min(1d0,dble(xmxrem)*zz*dble(masso)
     &                         *drangen(xmin)**0.05)
c        xtmp=dble(xminremn)
      endif
      endif
      xmax=min(xmax,max(xtmp,xmin))
      if(ish.ge.8)write(ifch,*)'ntry',ntry,xmin,xmax,xtmp
     *                               ,xmax*dble(masso),xmin0,excited
      if(koll.eq.1)xmax=min(xmax,xpz(iremo1,jremo))
      xxmax=xmax*dble(masso)-eps
      if(iLHC.eq.1)xxmax=min(1d0-eps,xxmax)      !check energy limit
      if(xmin.ge.xxmax)then
        xmax=xxmax
        xmin=xmin0
        if(xmin0.ge.xmax-eps)then
          if(.not.force)then
            iret=1
          elseif(excited)then
            xmz(irem,jrem)=min(1.-xpz(irem,jrem),
     &                       xmin0+0.5d0*(1d0-xmin0)*drangen(xmin)) !random not to form a peak
          else
            xxx=(aremn**2d0+xpt2rem)/sx
            xmz(irem,jrem)=xxx
c            xmin0=max(0.5d0,(1d0-((amzmn(idz(irem,jremo),jremo)
c     &                   +6d0*drangen(xxx))**2+xpt2rem)/sx))*xxx
            if(iLHC.eq.1)then     !LHC tune (more reasonnable xsi distribution)
c          xmin0=(1d0-xmin**0.3)*xxx
              xmin0=max(0.35d0*(1d0+drangen(xxx))
     &          ,1d0-((amzmn(idz(irem,jremo),jremo)
     &             +engy**drangen(xxx))**2+xpt2rem)/sx)*xxx
            else   !original CR version
              xmin0=max(0.35d0*(1d0+drangen(xxx))
     &          ,1d0-((amzmn(idz(irem,jremo),jremo)
     &             +sqrt(engy)*drangen(xxx)**0.5)**2+xpt2rem)/sx)*xxx
            endif
          endif
          goto 1000
        endif
      elseif(xmin.ge.xmax)then
        xmax=1d0
      endif
      rr=dble(rangen())
      alp=0.
      xxx=0.d0
      if(excited)then
c        xmin=xmin-xpt2rem/sx                     !no pt
c        xmax=xmax-xpt2rem/sx                     !no pt
        alp=at(idz(irem,jrem),iez(irem,jrem))/dble(zz)

        if(dabs(alp-1.d0).lt.eps)then
          xxx=xmax**rr*xmin**(1d0-rr)
        else
          xxx=(rr*xmax**(1d0-alp)+(1d0-rr)*xmin**(1d0-alp))
     &                                             **(1d0/(1d0-alp))
        endif
c        xxx=xxx+xpt2rem/sx                       !no pt
!smooth distribution
        if(iez(irem,jrem).eq.4)xmin=xmin0
        xmin0=xmin+(1d0-exp(-2d0*drangen(xxx)**2))*(xxx-xmin)
      else
        if(masso.eq.1)ntry=ntrymx   !xxx is fixed so 1 try is enough
c        xmin=dble(amremn)**2d0/sx                !no pt
c        xxx=xmin+xpt2rem/sx                      !no pt
        xmin=(dble(aremn)**2d0+xpt2rem)/sx
        xxx=xmin
        if(xmin.gt.xmax+eps)then
          if(ish.ge.6)write(ifch,*)'xmin>xmax for proj not possible (2)'
     &                 ,ir,irem
          if(.not.force)then
            iret=1
          else
            xmz(irem,jrem)=xxx
          endif
          goto 1000
        endif
c to have a nice diffractive peak, do not allow too much fluctuation
c this function is more or less a fit of the diffractive peak
c (pp100, ep-forward (ZEUS), NA49, pipp100, taking into account the 
c contribution of inelastic remnants)
          if(iLHC.eq.1)then     !LHC tune (more reasonnable xsi distribution)
c          xmin0=(1d0-xmin**0.3)*xxx
            xmin0=max(0.35d0*(1d0+drangen(xxx))
     &          ,1d0-((amzmn(idz(irem,jremo),jremo)
     &             +engy**drangen(xxx))**2+xpt2rem)/sx)*xxx
          else   !original CR version
            xmin0=max(0.35d0*(1d0+drangen(xxx))
     &          ,1d0-((amzmn(idz(irem,jremo),jremo)
     &             +sqrt(engy)*drangen(xxx)**0.5)**2+xpt2rem)/sx)*xxx
          endif

c       write(*,*)'->',xmin0/xxx,sx,log10(1d0-xmin0/xxx)
c     &,1d0-((amzmn(idz(irem,jremo),jremo)
c     &+5d0*exp(-0.5d0*drangen(xxx)**2))**2+xpt2rem)/sx
c        xmin0=dble(0.9+0.09*rangen())*xxx
      endif
      if(ish.ge.8)write(ifch,*)'alp',alp,xmin,xxx,xmax,zz
      msmin=xmin*sx
c      msmin=xmin*sx+xpt2rem                      !no pt

c partition xminus between nucleons of the other side

      xii=1d0
      ii=masso
      kk=int(rangen()*float(ii))+1   ! choose ramdomly a nucleon to start

      do while(ii.gt.0)

        iro=k2j(kk)
        cont=iez(iro,jremo).lt.0.or.xme(iro).lt.-0.99d0
        do while(cont)
          kk=kk+1
          if(kk.gt.masso)kk=kk-masso
          iro=k2j(kk)
          ii=ii-1
          if(ii.lt.1)then
            ntry=ntrymx
            goto 1
          endif
          cont=iez(iro,jremo).lt.0.or.xme(iro).lt.-0.99d0
        enddo

        if(ii-1.gt.0)then
         xi=xii*dble(rangen())**(1.d0/dble(ii-1))
        else
         xi=0d0
        endif
        xme(iro)=xxx*(xii-xi)

        xmmin=xzos(iro,jremo)
        if(xmz(iro,jremo).gt.eps)then
          xmmin=xmmin/xmz(iro,jremo)
        elseif(koll.eq.1.and.xtest(iro).gt.eps)then
          xmmin=xmmin/min(xpz(irem,jrem),xtest(iro))
        elseif(xtest(iro).gt.eps)then
          xmmin=xmmin/xtest(iro)
        endif
        if((xpz(iro,jremo)-xme(iro)).lt.xmmin)then
          if(ish.ge.8)write(ifch,*)'     skip ',cremn,' ',ii,masso,ntry
     &                      ,iro,xme(iro),xpz(iro,jremo)-xme(iro),xmmin
          xme(iro)=-1.d0
          if(ii.le.1)goto 1
        else
          xii=xi
          if(ish.ge.8)write(ifch,*)'       ok ',cremn,' ',ii,masso,ntry
     &                      ,iro,xme(iro),xme(iro)/xxx
        endif
        kk=kk+1
        if(kk.gt.masso)kk=kk-masso
        ii=ii-1

      enddo

c check xmz(irem,jrem)

 900  xmz(irem,jrem)=xxx

      p5sq=xpz(irem,jrem)*xmz(irem,jrem)*s
      if(ish.ge.8)write(ifch,*)'final mass',irem,p5sq,msmin
     &,xpz(irem,jrem),xmz(irem,jrem),force
      if(p5sq-msmin.lt.-1d-10)then
        if(ish.ge.5)then
          call utmsg('ProReM')
          write(ifch,*)'Remnant mass assignment not possible (M<Mmin)!'
     &                 ,ir,irem
          if(force)write(ifch,*)'Ignore p4 conservation'
          call utmsgf
        endif
        if(.not.force)then
          iret=1
        elseif(xpz(irem,jrem).gt.0.d0)then
          xmz(irem,jrem)=min(1.-xpz(irem,jrem),
     &                       xmin+0.5d0*(1d0-xmin)*drangen(xmin))   !random not to form a peak
        endif
        goto 1000
      endif

c subtract xme

      do k=1,masso
        iro=k2j(k)
        if(xme(iro).gt.0.d0)then
          xpz(iro,jremo)=xpz(iro,jremo)-xme(iro)  !xpz(,jremo)=xminus
        endif
      enddo

 1000 continue
      if(iret.ne.1)xzos(irem,jrem)=xmin0*xpz(irem,jrem)

      call utprix('ProReM',ish,ishini,5)

      end

c-----------------------------------------------------------------------
      subroutine ProSeTy(k,n)
c-----------------------------------------------------------------------
c creates proposal for string ends, idp., idm.
c updates quark counters
c-----------------------------------------------------------------------
      include 'epos.inc'
      include 'epos.incems'
      include 'epos.incsem'

      common/ems6/ivp0,iap0,idp0,isp0,ivt0,iat0,idt0,ist0
      double precision pes,xfqp,xfqt   !so01
      parameter(eps=1.e-6)
      common/ems9/xfqp(0:9),xfqt(0:9)
      common/emsx3/pes(0:3,0:6)
      integer jcp(nflav,2),jct(nflav,2)
     &       ,jcpi(nflavems,2),jcti(nflavems,2)
      logical go

      if(idpr(n,k).eq.2)stop'no Reggeons any more'

      iret=0
      ip=iproj(k)
      it=itarg(k)
      if(iremn.ge.3)then
        do j=1,2
          do i=1,nrflav
            jcp(i,j)=jcpref(i,j,ip)
            jct(i,j)=jctref(i,j,it)
          enddo
          do i=nrflav+1,nflav
            jcp(i,j)=0
            jct(i,j)=0
          enddo
        enddo
      endif
      
      idp1pr(n,k)=0
      idm1pr(n,k)=0
      idp2pr(n,k)=0
      idm2pr(n,k)=0
      idsppr(n,k)=0
      idstpr(n,k)=0
      pssp=0.
      pvsp=0.
      pvap=0.
      pddp=0.
      psvvp=0.
      paasp=0.
      psst=0.
      pvst=0.
      pvat=0.
      pddt=0.
      psvvt=0.
      paast=0.

      if(iLHC.eq.1)then

c for hard Pomeron, define which string ends are connected to valence quark
c treat gluon has soft string ends (including diquarks but can not be
c a "soft" valence like in soft Pomerons) later
      if(idpr(n,k).eq.3)then
        go=.false.
        if(ivp0.eq.iap0.and.rangen().lt.0.5)go=.true.    !meson
        idsppr(n,k)=5
        if(idhpr(n,k).eq.3.or.idhpr(n,k).eq.1)then
          if(iremn.ge.2)ivp(ip)=ivp(ip)-1
          if(iap0.eq.0.or.go)then !baryon
            idp1pr(n,k)=2
          else                    !antibaryon
            idp2pr(n,k)=2
          endif
        endif
        idstpr(n,k)=5
        if(idhpr(n,k).eq.3.or.idhpr(n,k).eq.2)then
          if(iremn.ge.2)ivt(it)=ivp(it)-1
          if(iat0.eq.0)then     !baryon
            idm1pr(n,k)=2
          else                  !antibaryon
            idm2pr(n,k)=2
          endif
        endif
      endif

      if(idpr(n,k).ne.0)then

c    projectile

       if(idfpr(n,k).eq.1.or.idfpr(n,k).eq.2)then

       ntry=0
       ivpi=ivp(ip)
       idpi=idp(ip)
       idspi=idsppr(n,k)
       if(iremn.eq.3)then
         do j=1,2
           do i=1,nrflav
             jcpi(i,j)=jcp(i,j)
           enddo
         enddo
       endif
  1    ntry=ntry+1
      if(ntry.gt.10)call utstop('something goes wrong in sr ProSeTy&')
       ivp(ip)=ivpi
       idp(ip)=idpi
       idsppr(n,k)=idspi
       if(iremn.eq.3)then
         do j=1,2
           do i=1,nrflav
             jcp(i,j)=jcpi(i,j)
           enddo
         enddo
       endif
       pss=wgtval+wgtsea
       if(pss.gt.0.)then
         pss=wgtsea/pss
       else
         pss=0.
       endif
       if(iremn.ge.2)then
         if(iap0.eq.0)then
           pvs=0.
           if(ivp(ip).ne.0.and.idpr(n,k).ne.3)pvs=1.-pss
           pva=0.
           psvv=0.
           if(idp(ip).ne.0.and.idp2pr(n,k).ne.2)psvv=wgtqqq(iclpro)
           paas=0.
         elseif(ivp0.eq.0)then
           pva=0.
           if(ivp(ip).ne.0.and.idpr(n,k).ne.3)pva=1.-pss
           pvs=0.
           psvv=0.
           paas=0.
           if(idp(ip).ne.0.and.idp1pr(n,k).ne.2)paas=wgtqqq(iclpro)
         else                   !for meson, no soft string with valence quark (we do not know whether the quark or the antiquark will be used by hard string)
           pvs=0.
           pva=0.
c diquark or antidiquark can be created once in meson remnant
           psvv=0.
           paas=0.
           if(1+idp(ip).ne.0)then
             if(idp2pr(n,k).ne.2)psvv=wgtqqq(iclpro)
             if(idp1pr(n,k).ne.2)paas=wgtqqq(iclpro)
           endif
         endif
         pdd=wgtdiq/(1.+float(abs(idp(ip))))
c         if(idpr(n,k).eq.3)then
c           pdd=0.
c           psvv=0.
c           paas=0.
c         endif
       elseif(iremn.ne.0)then
         pvs=0.
         pva=0.
         psvv=0.
         paas=0.
         if(idp2pr(n,k).ne.2)psvv=wgtqqq(iclpro)
         if(idp1pr(n,k).ne.2)paas=wgtqqq(iclpro)
         pdd=wgtdiq/(1.+float(abs(idp(ip))))
       else
         pvs=0.
         pva=0.
         psvv=0.
         paas=0.
         pdd=wgtdiq/(1.+float(abs(idp(ip))))
       endif
       if(idp1pr(n,k).eq.2)then  !with valence quark only 1 SE available
         psd=pdd
         pds=0.
         pdd=0.
       elseif(idp2pr(n,k).eq.2)then  !with valence antiquark only 1 SE available
         pds=pdd
         psd=0.
         pdd=0.
       else
         psd=pdd
         pds=pdd
         pdd=pdd**2
       endif
       su=1.-min(1.,pdd+psd+pds)            !diquark probability
       pss=(1.-min(1.,pvs+pva))*su        !no more valence quark: take from sea
       pvs=pvs*su
       pva=pva*su
       su=1.-min(1.,psvv+paas)      !stopping probability
       pss=pss*su
       pvs=pvs*su
       pva=pva*su
       psd=psd*su
       pds=pds*su
       pdd=pdd*su
       su=pss+pvs+pva+pdd+psd+pds+psvv+paas
       pssp = pss /su
       pvsp = pvs /su
       pvap = pva /su
       psdp = psd /su
       pdsp = pds /su
       pddp = pdd /su
       psvvp= psvv/su
       paasp= paas/su
       r=rangen()
       if(r.gt.(pssp+pvsp+pvap+psdp+pdsp+psvvp+paasp)
     &                               .and.pddp.gt.eps)then
        if(idp1pr(n,k).ne.2)idp1pr(n,k)=4
        if(idp2pr(n,k).ne.2)idp2pr(n,k)=4
        idsppr(n,k)=idsppr(n,k)+4
        if(iremn.ge.2)idp(ip)=idp(ip)+2
        if(iremn.eq.3)then   !add diquark flavor to jcpref for ProSeF later (sea quark)
          idum=idrafl(iclpro,jcp,1,'s',3,iret)
          idum=idrafl(iclpro,jcp,1,'d',3,iret)
          idum=idrafl(iclpro,jcp,1,'s',3,iret)
          idum=idrafl(iclpro,jcp,1,'d',3,iret)
        endif
      elseif(r.gt.(pssp+pvsp+pvap+psdp+psvvp+paasp).and.pdsp.gt.eps)then
        if(idp1pr(n,k).ne.2)idp1pr(n,k)=4
        if(idp2pr(n,k).ne.2)idp2pr(n,k)=1
        idsppr(n,k)=idsppr(n,k)+4
        if(iremn.ge.2)idp(ip)=idp(ip)+1
        if(iremn.eq.3)then   !add diquark flavor to jcpref for ProSeF later (sea quark)
          idum=idrafl(iclpro,jcp,1,'s',3,iret)
          idum=idrafl(iclpro,jcp,1,'d',3,iret)
        endif
       elseif(r.gt.(pssp+pvsp+pvap+psvvp+paasp).and.psdp.gt.eps)then
        if(idp1pr(n,k).ne.2)idp1pr(n,k)=1
        if(idp2pr(n,k).ne.2)idp2pr(n,k)=4
        idsppr(n,k)=idsppr(n,k)+4
        if(iremn.ge.2)idp(ip)=idp(ip)+1
        if(iremn.eq.3)then   !add diquark flavor to jcpref for ProSeF later (sea quark)
          idum=idrafl(iclpro,jcp,1,'s',3,iret)
          idum=idrafl(iclpro,jcp,1,'d',3,iret)
        endif
       elseif(r.gt.(pssp+pvsp+pvap+psvvp).and.paasp.gt.eps)then
        if(idp1pr(n,k).ne.2)idp1pr(n,k)=5
        if(idp2pr(n,k).ne.2)idp2pr(n,k)=1
        idsppr(n,k)=idsppr(n,k)+5
        if(iremn.ge.2)idp(ip)=idp(ip)-1
        if(iremn.eq.3)idum=idrafl(iclpro,jcp,1,'s',3,iret) !add flavor to jcpref for ProSeF later (sea quark) (only a q-aq pair because we replace diquark by q-aq (baryon "decay" or "stopping")
       elseif(r.gt.(pssp+pvsp+pvap+pddp).and.psvvp.gt.eps)then
        if(idp1pr(n,k).ne.2)idp1pr(n,k)=1
        if(idp2pr(n,k).ne.2)idp2pr(n,k)=5
        idsppr(n,k)=idsppr(n,k)+5
        if(iremn.ge.2)idp(ip)=idp(ip)-1
        if(iremn.eq.3)idum=idrafl(iclpro,jcp,1,'s',3,iret) !add flavor to jcpref for ProSeF later (sea quark) (only a q-aq pair because we replace diquark by q-aq (baryon "decay" or "stopping")
       elseif(r.gt.(pssp+pvsp).and.pvap.gt.eps)then
        if(idp1pr(n,k).ne.2)idp1pr(n,k)=1
        if(idp2pr(n,k).ne.2)idp2pr(n,k)=2
        idsppr(n,k)=idsppr(n,k)+2
        if(iremn.ge.2)ivp(ip)=ivp(ip)-1
        if(iremn.eq.3)idum=idrafl(iclpro,jcp,1,'s',3,iret) !add flavor to jcpref for ProSeF later (sea quark)
       elseif(r.gt.pssp.and.pvsp.gt.eps)then
        if(idp1pr(n,k).ne.2)idp1pr(n,k)=2
        if(idp2pr(n,k).ne.2)idp2pr(n,k)=1
        idsppr(n,k)=idsppr(n,k)+2
        if(iremn.ge.2)ivp(ip)=ivp(ip)-1
        if(iremn.eq.3)idum=idrafl(iclpro,jcp,2,'s',3,iret) !add flavor to jcpref for ProSeF later (sea quark)
       elseif(pssp.gt.eps)then
        if(idp1pr(n,k).ne.2)idp1pr(n,k)=1
        if(idp2pr(n,k).ne.2)idp2pr(n,k)=1
        idsppr(n,k)=idsppr(n,k)+1
        if(iremn.eq.3)idum=idrafl(iclpro,jcp,1,'s',3,iret) !add flavor to jcpref for ProSeF later (sea quark)
       else
        goto 1
       endif

       else
        idp1pr(n,k)=1
        idp2pr(n,k)=1
        idsppr(n,k)=0
       endif


c    target

       if(idfpr(n,k).eq.1.or.idfpr(n,k).eq.3)then


       ntry=0
       ivti=ivt(it)
       idti=idt(it)
       idsti=idstpr(n,k)
       if(iremn.eq.3)then
         do j=1,2
           do i=1,nrflav
             jcti(i,j)=jct(i,j)
           enddo
         enddo
       endif
  2    ntry=ntry+1
       if(ntry.gt.10)call utstop('something goes wrong in sr ProSeTy&')
       ivt(it)=ivti
       idt(it)=idti
       idstpr(n,k)=idsti
       if(iremn.eq.3)then
         do j=1,2
           do i=1,nrflav
             jct(i,j)=jcti(i,j)
           enddo
         enddo
       endif
       pss=wgtval+wgtsea
       if(pss.gt.0.)then
         pss=wgtsea/pss
       else
         pss=0.
       endif
       if(iremn.ge.2)then
         if(iat0.eq.0)then
           pvs=0.
           if(ivt(it).ne.0.and.idpr(n,k).ne.3)pvs=1.-pss
           pva=0.
           psvv=0.
           if(idt(it).ne.0.and.idm2pr(n,k).ne.2)psvv=wgtqqq(icltar)
           paas=0.
         elseif(ivt0.eq.0)then
           pva=0.
           if(ivt(it).ne.0.and.idpr(n,k).ne.3)pva=1.-pss
           pvs=0.
           psvv=0.
           paas=0.
           if(idt(it).ne.0.and.idm1pr(n,k).ne.2)paas=wgtqqq(icltar)
         else                   !for meson, no soft string with valence quark (we do not know whether the quark or the antiquark will be used by hard string)
           pvs=0.
           pva=0.
c diquark or antidiquark can be created once in meson remnant
           psvv=0.
           paas=0.
           if(1+idt(it).ne.0)then
             if(idm2pr(n,k).ne.2)psvv=wgtqqq(icltar)
             if(idm1pr(n,k).ne.2)paas=wgtqqq(icltar)
           endif
         endif
         pdd=wgtdiq/(1.+float(abs(idt(it))))
c         if(idpr(n,k).eq.3)then
c           pdd=0.
c           psvv=0.
c           paas=0.
c         endif
       elseif(iremn.ne.0)then
         pvs=0.
         pva=0.
         psvv=0.
         paas=0.
         if(idm2pr(n,k).ne.2)psvv=wgtqqq(icltar)
         if(idm1pr(n,k).ne.2)paas=wgtqqq(icltar)
         pdd=wgtdiq/(1.+float(abs(idt(it))))
       else
         pvs=0.
         pva=0.
         psvv=0.
         paas=0.
         pdd=wgtdiq/(1.+float(abs(idt(it))))
       endif
       if(idm1pr(n,k).eq.2)then  !with valence quark only 1 SE available
         psd=pdd
         pds=0.
         pdd=0.
       elseif(idm2pr(n,k).eq.2)then  !with valence antiquark only 1 SE available
         pds=pdd
         psd=0.
         pdd=0.
       else
         psd=pdd
         pds=pdd
         pdd=pdd**2
       endif
       su=1.-min(1.,pdd+pds+psd)            !diquark probability
       pss=(1.-min(1.,pvs+pva))*su        !no more valence quark: take from sea
       pvs=pvs*su
       pva=pva*su
       su=1.-min(1.,psvv+paas)      !stopping probability
       pss=pss*su
       pvs=pvs*su
       pva=pva*su
       pds=pds*su
       psd=psd*su
       pdd=pdd*su
       su=pss+pvs+pva+pdd+psd+pds+psvv+paas
       psst = pss /su
       pvst = pvs /su
       pvat = pva /su
       psdt = psd /su
       pdst = pds /su
       pddt = pdd /su
       psvvt= psvv/su
       paast= paas/su
       r=rangen()
       if(r.gt.(psst+pvst+pvat+psdt+pdst+psvvt+paast)
     &                               .and.pddt.gt.eps)then
        if(idm1pr(n,k).ne.2)idm1pr(n,k)=4
        if(idm2pr(n,k).ne.2)idm2pr(n,k)=4
        idstpr(n,k)=idstpr(n,k)+4
        if(iremn.ge.2)idt(it)=idt(it)+2
        if(iremn.eq.3)then   !add diquark flavor to jctref for ProSeF later (sea quark)
          idum=idrafl(icltar,jct,1,'s',3,iret)
          idum=idrafl(icltar,jct,1,'d',3,iret)
          idum=idrafl(icltar,jct,1,'s',3,iret)
          idum=idrafl(icltar,jct,1,'d',3,iret)
        endif
      elseif(r.gt.(psst+pvst+pvat+psdt+psvvt+paast).and.pdst.gt.eps)then
        if(idm1pr(n,k).ne.2)idm1pr(n,k)=4
        if(idm2pr(n,k).ne.2)idm2pr(n,k)=1
        idstpr(n,k)=idstpr(n,k)+4
        if(iremn.ge.2)idt(it)=idt(it)+1
        if(iremn.eq.3)then   !add diquark flavor to jctref for ProSeF later (sea quark)
          idum=idrafl(icltar,jct,1,'s',3,iret)
          idum=idrafl(icltar,jct,1,'d',3,iret)
        endif
       elseif(r.gt.(psst+pvst+pvat+psvvt+paast).and.psdt.gt.eps)then
        if(idm1pr(n,k).ne.2)idm1pr(n,k)=1
        if(idm2pr(n,k).ne.2)idm2pr(n,k)=4
        idstpr(n,k)=idstpr(n,k)+4
        if(iremn.ge.2)idt(it)=idt(it)+1
        if(iremn.eq.3)then   !add diquark flavor to jctref for ProSeF later (sea quark)
          idum=idrafl(icltar,jct,1,'s',3,iret)
          idum=idrafl(icltar,jct,1,'d',3,iret)
        endif
       elseif(r.gt.(psst+pvst+pvat+psvvt).and.paast.gt.eps)then
        if(idm1pr(n,k).ne.2)idm1pr(n,k)=5
        if(idm2pr(n,k).ne.2)idm2pr(n,k)=1
        idstpr(n,k)=idstpr(n,k)+5
        if(iremn.ge.2)idt(it)=idt(it)-1
        if(iremn.eq.3)idum=idrafl(icltar,jct,1,'s',3,iret) !add flavor to jcpref for ProSeF later (sea quark) (only a q-aq pair because we replace diquark by q-aq (baryon "decay" or "stopping")
       elseif(r.gt.(psst+pvst+pvat+pddt).and.psvvt.gt.eps)then
        if(idm1pr(n,k).ne.2)idm1pr(n,k)=1
        if(idm2pr(n,k).ne.2)idm2pr(n,k)=5
        idstpr(n,k)=idstpr(n,k)+5
        if(iremn.ge.2)idt(it)=idt(it)-1
        if(iremn.eq.3)idum=idrafl(icltar,jct,1,'s',3,iret) !add flavor to jcpref for ProSeF later (sea quark) (only a q-aq pair because we replace diquark by q-aq (baryon "decay" or "stopping")
       elseif(r.gt.(psst+pvst).and.pvat.gt.eps)then
        if(idm1pr(n,k).ne.2)idm1pr(n,k)=1
        if(idm2pr(n,k).ne.2)idm2pr(n,k)=2
        idstpr(n,k)=idstpr(n,k)+2
        if(iremn.ge.2)ivt(it)=ivt(it)-1
        if(iremn.eq.3)idum=idrafl(icltar,jct,1,'s',3,iret) !add flavor to jctref for ProSeF later (sea quark)
       elseif(r.gt.psst.and.pvst.gt.eps)then
        if(idm1pr(n,k).ne.2)idm1pr(n,k)=2
        if(idm2pr(n,k).ne.2)idm2pr(n,k)=1
        idstpr(n,k)=idstpr(n,k)+2
        if(iremn.ge.2)ivt(it)=ivt(it)-1
        if(iremn.eq.3)idum=idrafl(icltar,jct,2,'s',3,iret) !add flavor to jctref for ProSeF later (sea quark)
       elseif(psst.gt.eps)then
        if(idm1pr(n,k).ne.2)idm1pr(n,k)=1
        if(idm2pr(n,k).ne.2)idm2pr(n,k)=1
        idstpr(n,k)=idstpr(n,k)+1
        if(iremn.eq.3)idum=idrafl(icltar,jct,1,'s',3,iret) !add flavor to jctref for ProSeF later (sea quark)
       else
        goto 2
       endif

       else
        idm1pr(n,k)=1
        idm2pr(n,k)=1
        idstpr(n,k)=0
       endif

      else

        idp1pr(n,k)=0
        idm2pr(n,k)=0
        idp2pr(n,k)=0
        idm1pr(n,k)=0

      endif

      else       !iLHC

      if(idpr(n,k).eq.3)then
       pssp=0.
       pvsp=0.
       pvap=0.
       pddp=0.
       psvvp=0.
       paasp=0.
       psst=0.
       pvst=0.
       pvat=0.
       pddt=0.
       psvvt=0.
       paast=0.
       if(idhpr(n,k).eq.3)then  !so01
        idp1pr(n,k)=2
        idp2pr(n,k)=8
        idm1pr(n,k)=2
        idm2pr(n,k)=8
       elseif(idhpr(n,k).eq.2)then
        idp1pr(n,k)=1
        idp2pr(n,k)=1
        idm1pr(n,k)=2
        idm2pr(n,k)=8
       elseif(idhpr(n,k).eq.1)then
        idp1pr(n,k)=2
        idp2pr(n,k)=8
        idm1pr(n,k)=1
        idm2pr(n,k)=1
       elseif(idhpr(n,k).eq.0)then
        idp1pr(n,k)=1
        idp2pr(n,k)=1
        idm1pr(n,k)=1
        idm2pr(n,k)=1
       else
        call utstop('ProSeTy-idhpr????&')
       endif
       if(iremn.eq.3)then       !add flavor to jcpref and jctref for psahot and ProSeF later (sea quark)
         idum=idrafl(iclpro,jcp,1,'s',3,iret)
         idum=idrafl(icltar,jct,1,'s',3,iret)
       endif


      elseif(idpr(n,k).eq.1)then

c    projectile

       if(idfpr(n,k).eq.1.or.idfpr(n,k).eq.2)then

       ntry=0
       ivpi=ivp(ip)
       idpi=idp(ip)
       if(iremn.eq.3)then
         do j=1,2
           do i=1,nrflav
             jcpi(i,j)=jcp(i,j)
           enddo
         enddo
       endif
 3     ntry=ntry+1
       if(ntry.gt.10)call utstop('something goes wrong in sr ProSeTy&')
       ivp(ip)=ivpi
       idp(ip)=idpi
       if(iremn.eq.3)then
         do j=1,2
           do i=1,nrflav
             jcp(i,j)=jcpi(i,j)
           enddo
         enddo
       endif
       pss=wgtval+wgtsea
       if(pss.gt.0.)then
         pss=wgtsea/pss
       else
         pss=0.
       endif
       if(iremn.ge.2)then
         if(iap0.eq.0)then
           pvs=0.
           if(ivp(ip).ne.0)pvs=1.-pss
           pva=0.
           psvv=0.
           if(idp(ip).ne.0)psvv=wgtqqq(iclpro)
           paas=0.
         elseif(ivp0.eq.0)then
           pva=0.
           if(ivp(ip).ne.0)pva=1.-pss
           pvs=0.
           psvv=0.
           paas=0.
           if(idp(ip).ne.0)paas=wgtqqq(iclpro)
         else                   !for meson, no soft string with valence quark (we do not know whether the quark or the antiquark will be used by hard string)
           pvs=0.
           pva=0.
c diquark or antidiquark can be created once in meson remnant
           psvv=0.
           paas=0.
           if(1+idp(ip).ne.0)then
             psvv=wgtqqq(iclpro)
             paas=wgtqqq(iclpro)
           endif
         endif
         pdd=wgtdiq
       elseif(iremn.ne.0)then
         pvs=0.
         pva=0.
         psvv=wgtqqq(iclpro)
         paas=wgtqqq(iclpro)
         pdd=wgtdiq
       else
         pvs=0.
         pva=0.
         psvv=0.
         paas=0.
         pdd=wgtdiq
       endif
       su=1.-min(1.,pdd)            !diquark probability
       pss=(1.-min(1.,pvs+pva))*su        !no more valence quark: take from sea
       pvs=pvs*su
       pva=pva*su
       su=1.-min(1.,psvv+paas)      !stopping probability
       pdd=pdd*su
       pss=pss*su
       pvs=pvs*su
       pva=pva*su
       su=pss+pvs+pva+pdd+psvv+paas
       pssp = pss /su
       pvsp = pvs /su
       pvap = pva /su
       pddp = pdd /su
       psvvp= psvv/su
       paasp= paas/su
       r=rangen()
       if(r.gt.(pssp+pvsp+pvap+pddp+psvvp).and.paasp.gt.eps)then
        idp1pr(n,k)=5
        idp2pr(n,k)=1
        idsppr(n,k)=6
        if(iremn.ge.2)idp(ip)=idp(ip)-1
        if(iremn.eq.3)idum=idrafl(iclpro,jcp,1,'s',3,iret) !add flavor to jcpref for ProSeF later (sea quark) (only a q-aq pair because we replace diquark by q-aq (baryon "decay" or "stopping")
       elseif(r.gt.(pssp+pvsp+pvap+pddp).and.psvvp.gt.eps)then
        idp1pr(n,k)=1
        idp2pr(n,k)=5
        idsppr(n,k)=5
        if(iremn.ge.2)idp(ip)=idp(ip)-1
        if(iremn.eq.3)idum=idrafl(iclpro,jcp,1,'s',3,iret) !add flavor to jcpref for ProSeF later (sea quark) (only a q-aq pair because we replace diquark by q-aq (baryon "decay" or "stopping")
       elseif(r.gt.(pssp+pvsp+pvap).and.pddp.gt.eps)then
        idp1pr(n,k)=4
        idp2pr(n,k)=4
        idsppr(n,k)=4
        if(iremn.eq.3)then   !add diquark flavor to jcpref for ProSeF later (sea quark)
          idum=idrafl(iclpro,jcp,1,'s',3,iret)
          idum=idrafl(iclpro,jcp,1,'d',3,iret)
        endif
       elseif(r.gt.(pssp+pvsp).and.pvap.gt.eps)then
        idp1pr(n,k)=1
        idp2pr(n,k)=2
        idsppr(n,k)=3
        if(iremn.ge.2)ivp(ip)=ivp(ip)-1
        if(iremn.eq.3)idum=idrafl(iclpro,jcp,1,'s',3,iret) !add flavor to jcpref for ProSeF later (sea quark)
       elseif(r.gt.pssp.and.pvsp.gt.eps)then
        idp1pr(n,k)=2
        idp2pr(n,k)=1
        idsppr(n,k)=2
        if(iremn.ge.2)ivp(ip)=ivp(ip)-1
        if(iremn.eq.3)idum=idrafl(iclpro,jcp,2,'s',3,iret) !add flavor to jcpref for ProSeF later (sea quark)
       elseif(pssp.gt.eps)then
        idp1pr(n,k)=1
        idp2pr(n,k)=1
        idsppr(n,k)=1
        if(iremn.eq.3)idum=idrafl(iclpro,jcp,1,'s',3,iret) !add flavor to jcpref for ProSeF later (sea quark)
       else
        goto 3
       endif

       else
        idp1pr(n,k)=1
        idp2pr(n,k)=1
        idsppr(n,k)=0
       endif


c    target

       if(idfpr(n,k).eq.1.or.idfpr(n,k).eq.3)then


       ntry=0
       ivti=ivt(it)
       idti=idt(it)
       if(iremn.eq.3)then
         do j=1,2
           do i=1,nrflav
             jcti(i,j)=jct(i,j)
           enddo
         enddo
       endif
 4     ntry=ntry+1
       if(ntry.gt.10)call utstop('something goes wrong in sr ProSeTy&')
       ivt(it)=ivti
       idt(it)=idti
       if(iremn.eq.3)then
         do j=1,2
           do i=1,nrflav
             jct(i,j)=jcti(i,j)
           enddo
         enddo
       endif
       pss=wgtval+wgtsea
       if(pss.gt.0.)then
         pss=wgtsea/pss
       else
         pss=0.
       endif
       if(iremn.ge.2)then
         if(iat0.eq.0)then
           pvs=0.
           if(ivt(it).ne.0)pvs=1.-pss
           pva=0.
           psvv=0.
           if(idt(it).ne.0)psvv=wgtqqq(icltar)
           paas=0.
         elseif(ivt0.eq.0)then
           pva=0.
           if(ivt(it).ne.0)pva=1.-pss
           pvs=0.
           psvv=0.
           paas=0.
           if(idt(it).ne.0)paas=wgtqqq(icltar)
         else                   !for meson, no soft string with valence quark (we do not know whether the quark or the antiquark will be used by hard string)
           pvs=0.
           pva=0.
           psvv=0.
           paas=0.
c diquark or antidiquark can be created once in meson remnant
           if(1+idt(it).ne.0)then
             psvv=wgtqqq(icltar)
             paas=wgtqqq(icltar)
           endif
         endif
         pdd=wgtdiq
       elseif(iremn.ne.0)then
         pvs=0.
         pva=0.
         psvv=wgtqqq(icltar)
         paas=wgtqqq(icltar)
         pdd=wgtdiq
       else
         pvs=0.
         pva=0.
         psvv=0.
         paas=0.
         pdd=wgtdiq
       endif
c no more valence quark: take from sea
       su=1.-min(1.,pdd)            !diquark probability
       pss=(1.-min(1.,pvs+pva))*su        !no more valence quark: take from sea
       pvs=pvs*su
       pva=pva*su
       su=1.-min(1.,psvv+paas)      !stopping probability
       pdd=pdd*su
       pss=pss*su
       pvs=pvs*su
       pva=pva*su
       su=pss+pvs+pva+pdd+psvv+paas
       psst = pss /su
       pvst = pvs /su
       pvat = pva /su
       pddt = pdd /su
       psvvt= psvv/su
       paast= paas/su
       r=rangen()
       if(r.gt.(psst+pvst+pvat+pddt+psvvt).and.paast.gt.eps)then
        idm1pr(n,k)=5
        idm2pr(n,k)=1
        idstpr(n,k)=6
        if(iremn.ge.2)idt(it)=idt(it)-1
        if(iremn.eq.3)idum=idrafl(icltar,jct,1,'s',3,iret) !add flavor to jcpref for ProSeF later (sea quark) (only a q-aq pair because we replace diquark by q-aq (baryon "decay" or "stopping")
       elseif(r.gt.(psst+pvst+pvat+pddt).and.psvvt.gt.eps)then
        idm1pr(n,k)=1
        idm2pr(n,k)=5
        idstpr(n,k)=5
        if(iremn.ge.2)idt(it)=idt(it)-1
        if(iremn.eq.3)idum=idrafl(icltar,jct,1,'s',3,iret) !add flavor to jcpref for ProSeF later (sea quark) (only a q-aq pair because we replace diquark by q-aq (baryon "decay" or "stopping")
       elseif(r.gt.(psst+pvst+pvat).and.pddt.gt.eps)then
        idm1pr(n,k)=4
        idm2pr(n,k)=4
        idstpr(n,k)=4
        if(iremn.eq.3)then   !add diquark flavor to jctref for ProSeF later (sea quark)
          idum=idrafl(icltar,jct,1,'s',3,iret)
          idum=idrafl(icltar,jct,1,'d',3,iret)
        endif
       elseif(r.gt.(psst+pvst).and.pvat.gt.eps)then
        idm1pr(n,k)=1
        idm2pr(n,k)=2
        idstpr(n,k)=3
        if(iremn.ge.2)ivt(it)=ivt(it)-1
        if(iremn.eq.3)idum=idrafl(icltar,jct,1,'s',3,iret) !add flavor to jctref for ProSeF later (sea quark)
       elseif(r.gt.psst.and.pvst.gt.eps)then
        idm1pr(n,k)=2
        idm2pr(n,k)=1
        idstpr(n,k)=2
        if(iremn.ge.2)ivt(it)=ivt(it)-1
        if(iremn.eq.3)idum=idrafl(icltar,jct,2,'s',3,iret) !add flavor to jctref for ProSeF later (sea quark)
       elseif(psst.gt.eps)then
        idm1pr(n,k)=1
        idm2pr(n,k)=1
        idstpr(n,k)=1
        if(iremn.eq.3)idum=idrafl(icltar,jct,1,'s',3,iret) !add flavor to jctref for ProSeF later (sea quark)
       else
        goto 4
       endif

       else
        idm1pr(n,k)=1
        idm2pr(n,k)=1
        idstpr(n,k)=0
       endif

      elseif(idpr(n,k).eq.0)then

        idp1pr(n,k)=0
        idm2pr(n,k)=0
        idp2pr(n,k)=0
        idm1pr(n,k)=0

      endif

      endif

        if(ish.ge.6)then
      write(ifch,'(a,2(6(f4.2,1x),2x),$)')'ProSeTy ',
     * pssp,pvsp,pvap,pddp,psvvp,paasp, psst,pvst,pvat,pddt,psvvt,paast
      write(ifch,'(2x,3i3,2x,2(i2,1x,2i2,1x,i2,i3,2x))')idpr(n,k),n,k
     * ,idsppr(n,k),idp1pr(n,k),idp2pr(n,k),ivp(ip),idp(ip)
     * ,idstpr(n,k),idm1pr(n,k),idm2pr(n,k),ivt(it),idt(it)
        endif

      if(iremn.eq.3)then
        do j=1,2
          do i=1,nrflav
            jcpref(i,j,ip)=jcp(i,j)
            jctref(i,j,it)=jct(i,j)
          enddo
        enddo
        if(ish.ge.6)then
          write(ifch,'(a,i3,a,1x,4i3,3x,4i3)')'jcpref(',ip,'):',jcp
          write(ifch,'(a,i3,a,1x,4i3,3x,4i3)')'jctref(',it,'):',jct
        endif
      endif

      return
      end

c-----------------------------------------------------------------------
      subroutine ProSeF(k,n,iret)
c-----------------------------------------------------------------------
c starting from string properties as already determined in EMS,
c one determines string end flavors
c by checking compatibility with remnant masses.
c strings are written to /cems/ and then to /cptl/
c remnant ic is updated (icproj,ictarg)
c------------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'
      include 'epos.incsem'

      double precision plc,s,pstg,pend
      common/cems5/plc,s
      common/cems/pstg(5,2),pend(4,4),idend(4)
      common/emsptl/nppr(npommx,kollmx),npproj(mamx),nptarg(mamx)
      integer icp(2),ict(2),ic(2),icp1(2),icp2(2),icm1(2),icm2(2)
      integer jcp(nflav,2),jct(nflav,2),jcpv(nflav,2),jctv(nflav,2)
      integer jcp1(nflav,2),jcp2(nflav,2),jcm1(nflav,2),jcm2(nflav,2)
      common/col3/ncol,kolpt /cfacmss/facmss /cts/its


c     entry
c     -----

      iret=0

      if(ncol.eq.0)return
      if(abs(itpr(k)).ne.1)return

      ip=iproj(k)
      it=itarg(k)

      if(idpr(n,k).eq.0.or.ivpr(n,k).eq.0)return
      if(idpr(n,k).eq.2)stop'Reggeon'
      if(idpr(n,k).eq.3)return
      call utpri('ProSeF',ish,ishini,5)
      if(ish.ge.5)then
          write(ifch,*)'soft Pomeron'
          write(ifch,*)'k:',k,'  n:',n,'  ip:',ip,'  it:',it
      endif
      np=nppr(n,k)

c         string ends

          pend(1,1)=xxp1pr(n,k)
          pend(2,1)=xyp1pr(n,k)
          pend(3,1)=xp1pr(n,k)*plc/2d0
          pend(4,1)=dsqrt(pend(1,1)**2+pend(2,1)**2+pend(3,1)**2)
          pend(1,2)=xxp2pr(n,k)
          pend(2,2)=xyp2pr(n,k)
          pend(3,2)=xp2pr(n,k)*plc/2d0
          pend(4,2)=dsqrt(pend(1,2)**2+pend(2,2)**2+pend(3,2)**2)
          pend(1,4)=xxm1pr(n,k)
          pend(2,4)=xym1pr(n,k)
          pend(3,4)=-xm1pr(n,k)*plc/2d0
          pend(4,4)=dsqrt(pend(1,4)**2+pend(2,4)**2+pend(3,4)**2)
          pend(1,3)=xxm2pr(n,k)
          pend(2,3)=xym2pr(n,k)
          pend(3,3)=-xm2pr(n,k)*plc/2d0
          pend(4,3)=dsqrt(pend(1,3)**2+pend(2,3)**2+pend(3,3)**2)

c         strings

          pstg(1,1)=xxp1pr(n,k)+xxm2pr(n,k)
          pstg(2,1)=xyp1pr(n,k)+xym2pr(n,k)
          pstg(3,1)=(xp1pr(n,k)-xm2pr(n,k))*plc/2d0
          pstg(4,1)=(xp1pr(n,k)+xm2pr(n,k))*plc/2d0
          pstg(5,1)=dsqrt((pstg(4,1)-pstg(3,1))*(pstg(4,1)+pstg(3,1))
     &                   -pstg(1,1)**2-pstg(2,1)**2)
          pstg(1,2)=xxp2pr(n,k)+xxm1pr(n,k)
          pstg(2,2)=xyp2pr(n,k)+xym1pr(n,k)
          pstg(3,2)=(xp2pr(n,k)-xm1pr(n,k))*plc/2d0
          pstg(4,2)=(xp2pr(n,k)+xm1pr(n,k))*plc/2d0
          pstg(5,2)=dsqrt((pstg(4,2)-pstg(3,2))*(pstg(4,2)+pstg(3,2))
     &                   -pstg(2,2)**2-pstg(1,2)**2)

c         initialize

          ntry=0
  777     ntry=ntry+1
          if(ntry.gt.100)goto1001

          if(iremn.ge.2)then    !uses precalculated flavors
            do i=1,2
              icp(i)=icproj(i,ip)
              ict(i)=ictarg(i,it)
            enddo
            if(iLHC.eq.1)then
              call iddeco(icp,jcpv)
              call iddeco(ict,jctv)
            endif
            do j=1,2
              do i=1,nrflav
                jcp(i,j)=jcpref(i,j,ip)
                jct(i,j)=jctref(i,j,it)
                if(iLHC.eq.0)then
                jcpv(i,j)=jcpval(i,j,ip)
                jctv(i,j)=jctval(i,j,it)
                endif
              enddo
              do i=nrflav+1,nflav
                jcp(i,j)=0
                jct(i,j)=0
                jcpv(i,j)=0
                jctv(i,j)=0
               enddo
            enddo
          else
            do i=1,2
              icp(i)=icproj(i,ip)
              ict(i)=ictarg(i,it)
            enddo
            call iddeco(icp,jcp)
            call iddeco(ict,jct)
            do j=1,2
              do i=1,nflav
                jcpv(i,j)=0
                jctv(i,j)=0
              enddo
            enddo
          endif
          do i=1,2
           icp1(i)=0
           icp2(i)=0
           icm1(i)=0
           icm2(i)=0
           do j=1,nflav
            jcp1(j,i)=0
            jcp2(j,i)=0
            jcm1(j,i)=0
            jcm2(j,i)=0
           enddo
          enddo
          idpj0=idtr2(icp)
          idtg0=idtr2(ict)
          do j=1,4
           idend(j)=0
          enddo

          if(ish.ge.7)then
            write(ifch,'(a,3x,6i3,3x,6i3,i9)')' proj: '
     *     ,jcp,idpj0
            write(ifch,'(a,6i3,3x,6i3)')' proj val:  ',jcpv
          endif
          if(ish.ge.7)then
            write(ifch,'(a,3x,6i3,3x,6i3,i9)')' targ: '
     *    ,jct,idtg0
            write(ifch,'(a,6i3,3x,6i3)')' targ val:  ',jctv
          endif

c         determine string flavors

          call fstrfl(jcp,jct,jcpv,jctv,icp1,icp2,icm1,icm2
     *                ,idp1pr(n,k),idp2pr(n,k),idm1pr(n,k),idm2pr(n,k)
     *                                   ,idsppr(n,k),idstpr(n,k),iret)
          if(iret.ne.0)goto 1002

c         check mass string 1

          ic(1)=icp1(1)+icm2(1)
          ic(2)=icp1(2)+icm2(2)
          if(ic(1).gt.0.or.ic(2).gt.0)then
           am=sngl(pstg(5,1))
           call iddeco(icp1,jcp1)
           call iddeco(icm2,jcm2)
           ammns=utamnx(jcp1,jcm2)
           if(ish.ge.7)write(ifch,'(a,2i7,2e12.3)')
     *           ' string 1 - ic,mass,min.mass:',ic,am,ammns
           if(am.lt.ammns*facmss)then
             goto 777   !avoid virpom
           endif
           if(iLHC.eq.1)then
           idend(1)=idtra(icp1,0,0,0)
           idend(3)=idtra(icm2,0,0,0)
           else
           idend(1)=idtra(icp1,0,0,3)
           idend(3)=idtra(icm2,0,0,3)
           endif
           if(ish.ge.7)write(ifch,'(a,2i6)') ' string 1 - SE-ids:'
     *      ,idend(1),idend(3)
          endif

c         check mass string 2

          ic(1)=icp2(1)+icm1(1)
          ic(2)=icp2(2)+icm1(2)
          if(ic(1).gt.0.or.ic(2).gt.0)then
           am=sngl(pstg(5,2))
           call iddeco(icp2,jcp2)
           call iddeco(icm1,jcm1)
           ammns=utamnx(jcp2,jcm1)
           if(ish.ge.7)write(ifch,'(a,2i7,2e12.3)')
     *           ' string 2 - ic,mass,min.mass:',ic,am,ammns
           if(am.lt.ammns*facmss)then
             goto 777  !avoid virpom
           endif
           if(iLHC.eq.1)then
           idend(2)=idtra(icp2,0,0,0)
           idend(4)=idtra(icm1,0,0,0)
           else
           idend(2)=idtra(icp2,0,0,3)
           idend(4)=idtra(icm1,0,0,3)
           endif
           if(ish.ge.7)write(ifch,'(a,2i6)') ' string 2 - SE-ids:'
     *      ,idend(2),idend(4)
          endif

          if(ish.ge.5)then
          write(ifch,'(a,i10)')' pom:   '
     *    ,idptl(np)
          write(ifch,'(a,2i5)')' str 1: '
     *    ,idend(1),idend(3)
          write(ifch,'(a,2i5)')' str 2: '
     *    ,idend(2),idend(4)
          endif

c         update remnant ic

c determine icp,ict
c Similar process for hard pomeron in epos-rsh !!!!

          if(iremn.ge.2)then    !uses precalculated flavors

            do j=1,2
              do i=1,nrflav
                jcpref(i,j,ip)=jcp(i,j)
                jctref(i,j,it)=jct(i,j)
                if(iLHC.eq.0)then
                jcpval(i,j,ip)=jcpv(i,j)
                jctval(i,j,it)=jctv(i,j)
                endif
              enddo
            enddo
            if(iLHC.eq.1)then
            call idenco(jcpv,icp,iret)
            if(iret.ne.0)goto 1002
            call idenco(jctv,ict,iret)
            if(iret.ne.0)goto 1002
            do i=1,2
              icproj(i,ip)=icp(i)
              ictarg(i,it)=ict(i)
            enddo
            endif
            if(ish.ge.5)then
              write(ifch,'(a,6i3,3x,6i3)')' proj:  ',jcp
              write(ifch,'(a,6i3,3x,6i3)')' proj val:  ',jcpv
              write(ifch,'(a,6i3,3x,6i3)')' targ:  ',jct
              write(ifch,'(a,6i3,3x,6i3)')' targ val:  ',jctv
            endif

          else

            call idenco(jcp,icp,iret)
            if(iret.ne.0)goto 1002
            call idenco(jct,ict,iret)
            if(iret.ne.0)goto 1002
            do i=1,2
              icproj(i,ip)=icp(i)
              ictarg(i,it)=ict(i)
            enddo
            if(ish.ge.5)then
              write(ifch,'(a,2i7,1x,a)')' proj:  '
     *             ,(icp(l),l=1,2)
              write(ifch,'(a,2i7,1x,a)')' targ:  '
     *             ,(ict(l),l=1,2)
            endif

          endif

c         write strings to /cptl/

          its=idp1pr(n,k)+idm2pr(n,k)
          call fstrwr(1,1,3,k,n)
          its=idp2pr(n,k)+idm1pr(n,k)
          call fstrwr(2,2,4,k,n)

c     exit
c     ----

1000  continue
      call utprix('ProSeF',ish,ishini,5)
      return

 1002 jerr(1)=jerr(1)+1         ! > 9 quarks per flavor attempted.
 1001 iret=1
      if(ish.ge.5)write(ifch,'(a)')'Problem in ProSeF ... '
      goto 1000

      end

c-----------------------------------------------------------------------
      subroutine fstrfl(jcp,jct,jcpv,jctv,icp1,icp2,icm1,icm2
     *                         ,idp1,idp2,idm1,idm2,idsp,idst,iret)
c-----------------------------------------------------------------------
c knowing the string end types (idp1,idp2,idm1,idm2)
c               and remnant flavors (icp,ict)
c               and remnant link of the string (idsp and idst)
c   for LHC     (idsp/t=100 with one idp/idm=2 means that the valence quark 
c                to use is define in the corresponding icp/icm
c                (using just 1 to 6 for flavor identification (no diquark)))
c one determines quark flavors of string ends (icp1,icp2,icm1,icm2)
c               and updates remnant flavors (icp,ict)
c iret=0   ok
c iret=1   problem, more than 9 quarks per flavor attempted
c-----------------------------------------------------------------------
      include 'epos.inc'
      include 'epos.incems'
      include 'epos.incsem'
      integer icp1(2),icp2(2),icm1(2),icm2(2)
      integer jcp(nflav,2),jct(nflav,2)
     &       ,jcpi(nflavems,2),jcti(nflavems,2)
      integer iq(2,4),jcpv(nflav,2),jctv(nflav,2)
      character m
c      data neuz/0/proz/0/dtaz/0/
c      save neuz,proz,dtaz

      call utpri('fstrfl',ish,ishini,7)

c     entry
c     -----

      idum=0
      iret=0
      iret1=0
      iret2=0
      iret3=0
      iret4=0

      if(idp1.eq.8)stop'fstrfl: fragm quarks not used any more'
      if(idp2.eq.8)stop'fstrfl: fragm quarks not used any more'
      if(idm1.eq.8)stop'fstrfl: fragm quarks not used any more'
      if(idm2.eq.8)stop'fstrfl: fragm quarks not used any more'

c determine flavors of string ends (u,d,s)

      if(ish.ge.7)then
       write(ifch,'(a,3x,2i3)')' string 1, SE types:',idp1,idm2
       write(ifch,'(a,3x,2i3)')' string 2, SE types:',idp2,idm1
      endif

c empty

      if(idp1.eq.0)then
       iq(1,1)=0
       iq(2,1)=0
      endif
      if(idp2.eq.0)then
       iq(1,2)=0
       iq(2,2)=0
      endif
      if(idm1.eq.0)then
       iq(1,4)=0
       iq(2,4)=0
      endif
      if(idm2.eq.0)then
       iq(1,3)=0
       iq(2,3)=0
      endif
      do j=1,2
        do n=1,nrflav
          jcpi(n,j)=jcp(n,j)
          jcti(n,j)=jct(n,j)
        enddo
      enddo

c Projectile

      if(idsp.eq.0.or.iremn.eq.0)then
c give the same flavor to quark and antiquark not to change remnant flavor

        if(idp1.eq.4)then
c diquarks, code 4
          iq(1,1)=idrafl(iclpro,jcp,1,'d',0,iret)
          iq(2,1)=idrafl(iclpro,jcp,1,'d',0,iret)
          iq(1,2)=iq(1,1)
          iq(2,2)=iq(2,1)
        else
c sea quarks, code 1
          iq(1,1)=idrafl(iclpro,jcp,1,'s',0,iret)
          iq(2,1)=0
          iq(1,2)=iq(1,1)
          iq(2,2)=0
        endif

      elseif(iremn.ge.2)then
c count valence quarks properly

c valence quarks

        if(idp1.eq.2)then

          if(iLHC.eq.1)then
            if(idsp.eq.100)then
              iq(1,1)=icp1(1)   !flavor of hard quark already defined
            else
              iq(1,1)=idrafl(iclpro,jcpv,1,'v',0,idum)
            endif
            if(iq(1,1).gt.0)then !if still exist, update jcp and jcpv
              call idsufl3(iq(1,1),1,jcpv)
            else                ! if not, use jcp directly and sea
              iq(1,1)=idrafl(iclpro,jcp,1,'s',1,idum)
            endif
          else

          iq(1,1)=idrafl(iclpro,jcpv,1,'v',0,idum)
          if(iq(1,1).gt.0)then          !if still exist, update jcp and jcpv
            call idsufl3(iq(1,1),1,jcpv)
            call idsufl3(iq(1,1),1,jcp)
          else                          ! if not, use jcp directly
            iq(1,1)=idrafl(iclpro,jcp,1,'v',1,idum)
          endif

          endif

          iq(2,1)=0
        endif

        if(idp2.eq.2)then

          if(iLHC.eq.1)then
            if(idsp.eq.100)then
              iq(1,2)=icp2(2)   !flavor of hard antiquark already defined
            else
              iq(1,2)=idrafl(iclpro,jcpv,2,'v',0,idum)
            endif
            if(iq(1,2).gt.0)then !if still exist, update jcp and jcpv
              call idsufl3(iq(1,2),2,jcpv)
            else                ! if not, use jcp directly and sea
              iq(1,2)=idrafl(iclpro,jcp,2,'s',1,idum)
            endif
          else

          iq(1,2)=idrafl(iclpro,jcpv,2,'v',0,idum)
          if(iq(1,2).gt.0)then          !if still exist, update jcp and jcpv
            call idsufl3(iq(1,2),2,jcpv)
            call idsufl3(iq(1,2),2,jcp)
          else                          ! if not, use jcp directly
            iq(1,2)=idrafl(iclpro,jcp,2,'v',1,idum)
          endif
          endif
          iq(2,2)=0
        endif

c sea quarks
        m='v'           !iremn=3

        if(idp1.eq.1)then
          if(iremn.eq.2)m='s'
          j=1                    !quark
          i=idrafl(iclpro,jcp,j,m,1,idum)
          iq(1,1)=i
          if(iLHC.eq.0.and.jcp(i,j)-jcpv(i,j).lt.0)jcpv(i,j)=jcpv(i,j)-1
          iq(2,1)=0
        elseif(idp1.ge.4)then
          if(iremn.eq.2)m='d'
          j=2                    !anti-diquark
          i=idrafl(iclpro,jcp,j,m,1,idum)
          iq(1,1)=i
          if(iLHC.eq.0.and.jcp(i,j)-jcpv(i,j).lt.0)jcpv(i,j)=jcpv(i,j)-1
          i=idrafl(iclpro,jcp,j,m,1,idum)
          iq(2,1)=i
          if(iLHC.eq.0.and.jcp(i,j)-jcpv(i,j).lt.0)jcpv(i,j)=jcpv(i,j)-1
        endif
        if(idp2.eq.1)then
          if(iremn.eq.2)m='s'
          j=2                    !antiquark
          i=idrafl(iclpro,jcp,j,m,1,idum)
          iq(1,2)=i
          if(iLHC.eq.0.and.jcp(i,j)-jcpv(i,j).lt.0)jcpv(i,j)=jcpv(i,j)-1
          iq(2,2)=0
        elseif(idp2.ge.4)then
          if(iremn.eq.2)m='d'
          j=1                    !diquark
          i=idrafl(iclpro,jcp,j,m,1,idum)
          iq(1,2)=i
          if(iLHC.eq.0.and.jcp(i,j)-jcpv(i,j).lt.0)jcpv(i,j)=jcpv(i,j)-1
          i=idrafl(iclpro,jcp,j,m,1,idum)
          iq(2,2)=i
          if(iLHC.eq.0.and.jcp(i,j)-jcpv(i,j).lt.0)jcpv(i,j)=jcpv(i,j)-1
        endif

      elseif(iremn.ne.0)then
c free remant content

c valence quarks

        if(idp1.eq.2)then
          if(iLHC.eq.1.and.idsp.eq.100)then
            iq(1,1)=icp1(1)         !flavor of hard quark already defined
          else
            iq(1,1)=idrafl(iclpro,jcp,1,'v',1,iret)
          endif
          iq(2,1)=0
        endif
        if(idp2.eq.2)then
          if(iLHC.eq.1.and.idsp.eq.100)then
            iq(1,2)=icp2(1)     !flavor of hard antiquark already defined
          else
            iq(1,2)=idrafl(iclpro,jcp,2,'v',1,iret)
          endif
          iq(2,2)=0
        endif

c sea quarks

        if(idp1.eq.1)then
          iq(1,1)=idrafl(iclpro,jcp,1,'s',1,iret1)
          iq(2,1)=0
        endif
        if(idp2.eq.1)then
          iq(1,2)=idrafl(iclpro,jcp,2,'s',1,iret2)
          iq(2,2)=0
        endif

c diquarks, code 4

        if(idp1.eq.4.or.idp2.eq.4)then
          iq(1,1)=idrafl(iclpro,jcp,2,'d',1,iret1)
          iq(2,1)=idrafl(iclpro,jcp,2,'d',1,iret1)
          iq(1,2)=idrafl(iclpro,jcp,1,'d',1,iret2)
          iq(2,2)=idrafl(iclpro,jcp,1,'d',1,iret2)
        endif

c diquarks, code 5 (former valence, but actually sea)

        if(idp1.eq.5)then
          iq(1,1)=idrafl(iclpro,jcp,2,'d',1,iret1)
          iq(2,1)=idrafl(iclpro,jcp,2,'d',1,iret1)
        endif
        if(idp2.eq.5)then
          iq(1,2)=idrafl(iclpro,jcp,1,'d',1,iret2)
          iq(2,2)=idrafl(iclpro,jcp,1,'d',1,iret2)
        endif


        if(iret.ne.0)goto 1000



c in case of saturated remnants, use the same flavor for quark and anti-quark
c at string-end
        if(iret1.ne.0.or.iret2.ne.0)then
          do j=1,2
            do n=1,nrflav
              jcp(n,j)=jcpi(n,j)
            enddo
          enddo
          if(idp1.gt.idp2.or.(idp1.eq.idp2.and.rangen().gt.0.5))then
            iq(1,2)=iq(1,1)
            iq(2,2)=iq(2,1)
          else
            iq(1,1)=iq(1,2)
            iq(2,1)=iq(2,2)
          endif
        endif

      endif

c Target

      if(idst.eq.0.or.iremn.eq.0)then
c give the same flavor to quark and antiquark not to change remnant flavor


        if(idm1.eq.4)then
c diquarks, code 4
          iq(1,4)=idrafl(icltar,jct,1,'d',0,iret)
          iq(2,4)=idrafl(icltar,jct,1,'d',0,iret)
          iq(1,3)=iq(1,4)
          iq(2,3)=iq(2,4)
        else
c sea quarks,code 1
          iq(1,4)=idrafl(icltar,jct,1,'s',0,iret)
          iq(2,4)=0
          iq(1,3)=iq(1,4)
          iq(2,3)=0
        endif

      elseif(iremn.ge.2)then
c count valence quarks properly

c valence quarks

        if(idm1.eq.2)then

          if(iLHC.eq.1)then
            if(idst.eq.100)then
              iq(1,4)=icm1(1)   !flavor of hard quark already defined
            else
              iq(1,4)=idrafl(icltar,jctv,1,'v',0,idum)
            endif
            if(iq(1,4).gt.0)then !if still exist, update jct and jctv
              call idsufl3(iq(1,4),1,jctv)
            else                ! if not, use jct directly
              iq(1,4)=idrafl(icltar,jct,1,'s',1,idum)
            endif
          else

          iq(1,4)=idrafl(icltar,jctv,1,'v',0,idum)
          if(iq(1,4).gt.0)then          !if still exist, update jct and jctv
            call idsufl3(iq(1,4),1,jctv)
            call idsufl3(iq(1,4),1,jct)
          else                          ! if not, use jct directly
            iq(1,4)=idrafl(icltar,jct,1,'v',1,idum)
          endif

          endif

          iq(2,4)=0
        endif
        if(idm2.eq.2)then

          if(iLHC.eq.1)then
            if(idst.eq.100)then
              iq(1,3)=icm2(2)   !flavor of hard antiquark already defined
            else
              iq(1,3)=idrafl(icltar,jctv,2,'v',0,idum)
            endif
            if(iq(1,3).gt.0)then !if still exist, update jct and jctv
              call idsufl3(iq(1,3),2,jctv)
            else                ! if not, use jct directly
              iq(1,3)=idrafl(icltar,jct,2,'s',1,idum)
            endif
          else

          iq(1,3)=idrafl(icltar,jctv,2,'v',0,idum)
          if(iq(1,3).gt.0)then          !if still exist, update jct and jctv
            call idsufl3(iq(1,3),2,jctv)
            call idsufl3(iq(1,3),2,jct)
          else                          ! if not, use jct directly
            iq(1,3)=idrafl(icltar,jct,2,'v',1,idum)
          endif
          endif
          iq(2,3)=0
        endif

c sea quarks
        m='v'           !iremn=3

        if(idm1.eq.1)then
          if(iremn.eq.2)m='s'
          j=1                    !quark
          i=idrafl(icltar,jct,j,m,1,idum)
          iq(1,4)=i
          if(iLHC.eq.0.and.jct(i,j)-jctv(i,j).lt.0)jctv(i,j)=jctv(i,j)-1
          iq(2,4)=0
        elseif(idm1.ge.4)then
          if(iremn.eq.2)m='d'
          j=2                   !anti-diquark
          i=idrafl(icltar,jct,j,m,1,idum)
          iq(1,4)=i
          if(iLHC.eq.0.and.jct(i,j)-jctv(i,j).lt.0)jctv(i,j)=jctv(i,j)-1
          i=idrafl(icltar,jct,j,m,1,idum)
          iq(2,4)=i
          if(iLHC.eq.0.and.jct(i,j)-jctv(i,j).lt.0)jctv(i,j)=jctv(i,j)-1
        endif
        if(idm2.eq.1)then
          if(iremn.eq.2)m='s'
          j=2                    !antiquark
          i=idrafl(icltar,jct,j,m,1,idum)
          iq(1,3)=i
          if(iLHC.eq.0.and.jct(i,j)-jctv(i,j).lt.0)jctv(i,j)=jctv(i,j)-1
          iq(2,3)=0
        elseif(idm2.ge.4)then
          if(iremn.eq.2)m='d'
          j=1                    !diquark
          i=idrafl(icltar,jct,j,m,1,idum)
          iq(1,3)=i
          if(iLHC.eq.0.and.jct(i,j)-jctv(i,j).lt.0)jctv(i,j)=jctv(i,j)-1
          i=idrafl(icltar,jct,j,m,1,idum)
          iq(2,3)=i
          if(iLHC.eq.0.and.jct(i,j)-jctv(i,j).lt.0)jctv(i,j)=jctv(i,j)-1
        endif

      elseif(iremn.ne.0)then

c valence quarks

        if(idm1.eq.2)then
          if(iLHC.eq.1.and.idst.eq.100)then
            iq(1,4)=icm1(1)         !flavor of hard quark already defined
          else
            iq(1,4)=idrafl(icltar,jct,1,'v',1,iret)
          endif
          iq(2,4)=0
        endif
        if(idm2.eq.2)then
          if(iLHC.eq.1.and.idst.eq.100)then
            iq(1,3)=icm2(1)         !flavor of hard antiquark already defined
          else
            iq(1,3)=idrafl(icltar,jct,2,'v',1,iret)
          endif
          iq(2,3)=0
        endif

c sea quarks

        if(idm1.eq.1)then
          iq(1,4)=idrafl(icltar,jct,1,'s',1,iret4)
          iq(2,4)=0
        endif
        if(idm2.eq.1)then
          iq(1,3)=idrafl(icltar,jct,2,'s',1,iret3)
          iq(2,3)=0
        endif

c diquarks, code 4

        if(idm1.eq.4.or.idm2.eq.4)then
          iq(1,4)=idrafl(icltar,jct,2,'d',1,iret3)
          iq(2,4)=idrafl(icltar,jct,2,'d',1,iret3)
          iq(1,3)=idrafl(icltar,jct,1,'d',1,iret4)
          iq(2,3)=idrafl(icltar,jct,1,'d',1,iret4)
        endif

c diquarks, code 5 (former valence, but actually sea)

        if(idm1.eq.5)then
          iq(1,4)=idrafl(icltar,jct,2,'d',1,iret4)
          iq(2,4)=idrafl(icltar,jct,2,'d',1,iret4)
        endif
        if(idm2.eq.5)then
          iq(1,3)=idrafl(icltar,jct,1,'d',1,iret3)
          iq(2,3)=idrafl(icltar,jct,1,'d',1,iret3)
        endif


        if(iret.ne.0)goto 1000



c in case of saturated remnants, use the same flavor for quark and anti-quark
c at string-end

        if(iret3.ne.0.or.iret4.ne.0)then
          do j=1,2
            do n=1,nrflav
              jct(n,j)=jcti(n,j)
            enddo
          enddo
          if(idm1.gt.idm2.or.(idm1.eq.idm2.and.rangen().gt.0.5))then
            iq(1,4)=iq(1,3)
            iq(2,4)=iq(2,3)
          else
            iq(1,3)=iq(1,4)
            iq(2,3)=iq(2,4)
          endif
        endif

      endif

      ifla=iq(1,1)
      iflb=iq(2,1)
      iflc=iq(1,3)
      ifld=iq(2,3)
      if(ish.ge.7)write(ifch,'(a,2i5,4x,2i5)')
     *' string 1, string ends:',ifla,iflb,iflc,ifld

      if(ifla.gt.0)then
       if(iflb.eq.0)then
        icp1(1)=10**(6-ifla)
        icp1(2)=0
       else
        icp1(1)=0
        icp1(2)=10**(6-ifla)
        icp1(2)=icp1(2)+10**(6-iflb)
       endif
      endif

      if(iflc.gt.0)then
       if(ifld.eq.0)then
        icm2(1)=0
        icm2(2)=10**(6-iflc)
       else
        icm2(1)=10**(6-iflc)
        icm2(1)=icm2(1)+10**(6-ifld)
        icm2(2)=0
       endif
      endif

      ifla=iq(1,4)
      iflb=iq(2,4)
      iflc=iq(1,2)
      ifld=iq(2,2)
      if(ish.ge.7)write(ifch,'(a,2i5,4x,2i5)')
     *' string 2, string ends:',ifla,iflb,iflc,ifld

      if(ifla.gt.0)then
       if(iflb.eq.0)then
        icm1(1)=10**(6-ifla)
        icm1(2)=0
       else
        icm1(1)=0
        icm1(2)=10**(6-ifla)
        icm1(2)=icm1(2)+10**(6-iflb)
       endif
      endif

      if(iflc.gt.0)then
       if(ifld.eq.0)then
        icp2(1)=0
        icp2(2)=10**(6-iflc)
       else
        icp2(1)=10**(6-iflc)
        icp2(1)=icp2(1)+10**(6-ifld)
        icp2(2)=0
       endif
      endif

      if(ish.ge.7)then
        write(ifch,'(a,2i7,4x,2i7)')
     *  ' SE-forw:',icp1(1),icp1(2),icp2(1),icp2(2)
        write(ifch,'(a,2i7,4x,2i7)')
     *  ' SE-back:',icm1(1),icm1(2),icm2(1),icm2(2)
        write(ifch,'(a,3x,6i3,3x,6i3)')' proj:',jcp
        write(ifch,'(a,3x,6i3,3x,6i3)')' proj val:',jcpv
        write(ifch,'(a,3x,6i3,3x,6i3)')' targ:',jct
        write(ifch,'(a,3x,6i3,3x,6i3)')' targ val:',jctv
      endif

c     exit
c     ----

1000  continue
      call utprix('fstrfl',ish,ishini,7)
      return
      end


cc-----------------------------------------------------------------------
c      subroutine fremfl(icp,ict,iret)
cc-----------------------------------------------------------------------
cc checks projectile and target flavor (icp,ict)
cc in case of reggeon exchange they do not correspond to hadrons.
cc one transfers therefore flavor from one side to the other in order
cc to have hadron flavor.
cc icp and ict are modified correspondingly
cc-----------------------------------------------------------------------
c      include 'epos.inc'
c      integer icp(2),ict(2),jcp(6,2),jct(6,2),kp(4),kt(4)
c
c      call utpri('fremfl',ish,ishini,7)
c
cc     entry
cc     -----
c
c      iret=0
c
c      call iddeco(icp,jcp)
c      call iddeco(ict,jct)
c
c      iakp=0
c      iakt=0
c      ikp=0
c      ikt=0
c      do l=1,4
c       kp(l)=jcp(l,1)-jcp(l,2)
c       kt(l)=jct(l,1)-jct(l,2)
c       iakp=iakp+iabs(kp(l))
c       iakt=iakt+iabs(kt(l))
c       ikp=ikp+kp(l)
c       ikt=ikt+kt(l)
c      enddo
c      if(ish.ge.7)write(ifch,*)'iak_p:',iakp,' ik_p:',ikp
c      if(ish.ge.7)write(ifch,*)'iak_t:',iakt,' ik_t:',ikt
c
c      if(iakp.eq.4)then
c       if(ikp.eq.4.or.ikp.eq.-2)then
c        ifl=idrafl(jcp,1,'v',iret)
c        iqp=2      ! subtract quark
c        iqt=1      ! add quark
c       elseif(ikp.eq.-4.or.ikp.eq.2)then
c        ifl=idrafl(jcp,2,'v',iret)
c        iqp=1      ! subtract antiquark
c        iqt=2      ! add antiquark
c       else
c        call utstop('fremfl&')
c       endif
c      elseif(iakt.eq.4)then
c       if(ikt.eq.4.or.ikt.eq.-2)then
c        ifl=idrafl(jct,1,'v',iret)
c        iqp=1      ! add quark
c        iqt=2      ! subtract quark
c       elseif(ikt.eq.-4.or.ikt.eq.2)then
c        ifl=idrafl(jct,2,'v',iret)
c        iqp=2      ! add antiquark
c        iqt=1      ! subtract antiquark
c       else
c        call utstop('fremfl&')
c       endif
c      elseif(iakp.eq.3)then
c       if(ikp.gt.0)then
c        ifl=idrafl(jcp,1,'v',iret)
c        iqp=2      ! subtract quark
c        iqt=1      ! add quark
c       else
c        ifl=idrafl(jcp,2,'v',iret)
c        iqp=1      ! subtract antiquark
c        iqt=2      ! add antiquark
c       endif
c      elseif(iakt.eq.3)then
c       if(ikt.gt.0)then
c        ifl=idrafl(jct,1,'v',iret)
c        iqp=1      ! add quark
c        iqt=2      ! subtract quark
c       else
c        ifl=idrafl(jct,2,'v',iret)
c        iqp=2      ! add antiquark
c        iqt=1      ! subtract antiquark
c       endif
c      elseif(iakp.eq.2)then
c       if(ikp.gt.0)then
c        ifl=idrafl(jct,1,'v',iret)
c        iqp=1      ! add quark
c        iqt=2      ! subtract quark
c       else
c        ifl=idrafl(jct,2,'v',iret)
c        iqp=2      ! add antiquark
c        iqt=1      ! subtract antiquark
c       endif
c      elseif(iakt.eq.2)then
c       if(ikt.gt.0)then
c        ifl=idrafl(jct,1,'v',iret)
c        iqp=2      ! subtract quark
c        iqt=1      ! add quark
c       else
c        ifl=idrafl(jct,2,'v',iret)
c        iqp=1      ! subtract antiquark
c        iqt=2      ! add antiquark
c       endif
c      elseif(iakp.eq.1)then
c       if(ikp.gt.0)then
c        ifl=idrafl(jcp,2,'v',iret)
c        iqp=2      ! add antiquark
c        iqt=1      ! subtract antiquark
c       else
c        ifl=idrafl(jcp,1,'v',iret)
c        iqp=1      ! add quark
c        iqt=2      ! subtract quark
c       endif
c      elseif(iakt.eq.1)then
c       if(ikt.gt.0)then
c        ifl=idrafl(jct,2,'v',iret)
c        iqp=1      ! subtract antiquark
c        iqt=2      ! add antiquark
c       else
c        ifl=idrafl(jct,1,'v',iret)
c        iqp=2      ! subtract quark
c        iqt=1      ! add quark
c       endif
c      else
c       call utstop('fremfl: error&')
c      endif
c
c      if(ish.ge.7)write(ifch,*)'iq_p:',iqp,' iq_t:',iqt,' if:',ifl
c      call uticpl(icp,ifl,iqp,iret)
c      if(iret.ne.0)goto1000
c      call uticpl(ict,ifl,iqt,iret)
c      if(iret.ne.0)goto1000
c
cc     exit
cc     ----
c
c1000  continue
c      call utprix('fremfl',ish,ishini,7)
c      return
c      end
c
c-----------------------------------------------------------------------
      subroutine fstrwr(j,ii,jj,k,n)
c-----------------------------------------------------------------------
c take pstg(5,j),pend(4,ii),idend(ii),pend(4,jj),idend(jj)  (/cems/)
c and write it to /cptl/
c-----------------------------------------------------------------------
c  j:     string 1 or 2
c  ii,jj: string end (1,2: proj; 3,4: targ)
c  k:     current collision
c  n:     current pomeron
c-----------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'

      double precision pstg,pend,ptt3!,utpcmd
      common/cems/pstg(5,2),pend(4,4),idend(4)
      common/emsptl/nppr(npommx,kollmx),npproj(mamx),nptarg(mamx)
      double precision  pp(4)
      common/cts/its

      call utpri('fstrwr',ish,ishini,7)

      if(idend(ii).ne.0.and.idend(jj).ne.0)then

c string
c        id1=abs(idend(ii))
c        id2=abs(idend(jj))
c        call idmass(id1,am1)
c        call idmass(id2,am2)
c        if(id1.gt.100)then
c          am1=am1+qmass(0)
c        endif
c        if(id2.gt.100)then
c          am2=am2+qmass(0)
c        endif
c        ptt3=utpcmd(pstg(5,j),dble(am1),dble(am2),iret)
c        if(iret.ne.0.or.pstg(5,j)-dble(am1)-dble(am2).le.0d0)then
c          ptt3=0.5d0*pstg(5,j)
c          am1=0.
c          am2=0.
c        endif
        am1=0.
        am2=0.
        ptt3=0.5d0*pstg(5,j)

       call utlob2(1,pstg(1,j),pstg(2,j),pstg(3,j),pstg(4,j),pstg(5,j)
     * ,pend(1,ii),pend(2,ii),pend(3,ii),pend(4,ii),20)
       pp(1)=0d0
       pp(2)=0d0
       pp(3)=ptt3!.5d0*pstg(5,j)
       pp(4)=sqrt(ptt3*ptt3+dble(am1*am1))!.5d0*pstg(5,j)
       call utrot2
     * (-1,pend(1,ii),pend(2,ii),pend(3,ii),pp(1),pp(2),pp(3))
       call utlob2(-1,pstg(1,j),pstg(2,j),pstg(3,j),pstg(4,j),pstg(5,j)
     * ,pp(1),pp(2),pp(3),pp(4),21)

       npom=nppr(n,k)
       if(ifrptl(1,npom).eq.0)ifrptl(1,npom)=nptl+1
       ifrptl(2,npom)=nptl+2
       istptl(npom)=31

       nptl=nptl+1
       pptl(1,nptl)=sngl(pp(1))
       pptl(2,nptl)=sngl(pp(2))
       pptl(3,nptl)=sngl(pp(3))
       pptl(4,nptl)=sngl(pp(4))
       pptl(5,nptl)=am1 !0.
       istptl(nptl)=20
       iorptl(nptl)=npom
       jorptl(nptl)=0
       ifrptl(1,nptl)=0
       ifrptl(2,nptl)=0
       xorptl(1,nptl)=coord(1,k)
       xorptl(2,nptl)=coord(2,k)
       xorptl(3,nptl)=coord(3,k)
       xorptl(4,nptl)=coord(4,k)
       tivptl(1,nptl)=xorptl(4,nptl)
       tivptl(2,nptl)=xorptl(4,nptl)
       idptl(nptl)=idend(ii)
       ityptl(nptl)=ityptl(npom)+j
       itsptl(nptl)=its
       rinptl(nptl)=-9999
       qsqptl(nptl)=pstg(4,j)**2
       zpaptl(1,nptl)=0.
       zpaptl(2,nptl)=0.

       nptl=nptl+1
       do i=1,4
        pptl(i,nptl)=sngl(pstg(i,j))-pptl(i,nptl-1)
       enddo
       pptl(5,nptl)=am2!0.

       istptl(nptl)=20
       iorptl(nptl)=nppr(n,k)
       jorptl(nptl)=0
       ifrptl(1,nptl)=0
       ifrptl(2,nptl)=0
       xorptl(1,nptl)=coord(1,k)
       xorptl(2,nptl)=coord(2,k)
       xorptl(3,nptl)=coord(3,k)
       xorptl(4,nptl)=coord(4,k)
       tivptl(1,nptl)=xorptl(4,nptl)
       tivptl(2,nptl)=xorptl(4,nptl)
       idptl(nptl)=idend(jj)
       ityptl(nptl)=ityptl(npom)+j
       itsptl(nptl)=its
       rinptl(nptl)=-9999
       qsqptl(nptl)=pstg(4,j)**2
       zpaptl(1,nptl)=0.
       zpaptl(2,nptl)=0.

       if(ish.ge.7)then
        write(ifch,100)' kink:',(pptl(l,nptl-1),l=1,4),idptl(nptl-1)
        write(ifch,100)' kink:',(pptl(l,nptl),l=1,4),idptl(nptl)
       endif

      elseif(idend(ii).ne.0.and.idend(jj).eq.0)then

c resonance

       npom=nppr(n,k)
       if(ifrptl(1,npom).eq.0)ifrptl(1,npom)=nptl+1
       ifrptl(2,npom)=nptl+1
       istptl(npom)=31

       nptl=nptl+1
       idptl(nptl)=idend(ii)
       pptl(1,nptl)=sngl(pstg(1,j))
       pptl(2,nptl)=sngl(pstg(2,j))
       pptl(3,nptl)=sngl(pstg(3,j))
       pptl(4,nptl)=sngl(pstg(4,j))
       pptl(5,nptl)=sngl(pstg(5,j))
       istptl(nptl)=0
       iorptl(nptl)=npom
       jorptl(nptl)=0
       ifrptl(1,nptl)=0
       ifrptl(2,nptl)=0
       xorptl(1,nptl)=coord(1,k)
       xorptl(2,nptl)=coord(2,k)
       xorptl(3,nptl)=coord(3,k)
       xorptl(4,nptl)=coord(4,k)
       tivptl(1,nptl)=coord(4,k)
       call idtau(idptl(nptl),pptl(4,nptl),pptl(5,nptl),taugm)
       tivptl(2,nptl)=tivptl(1,nptl)+taugm*(-alog(rangen()))
       ityptl(nptl)=ityptl(npom)+2+j
       itsptl(nptl)=its
       rinptl(nptl)=-9999
       qsqptl(nptl)=0.
       zpaptl(1,nptl)=0.
       zpaptl(2,nptl)=0.

       if(ish.ge.7)then
        write(ifch,100)'  res:',(pptl(l,nptl),l=1,4),idptl(nptl)
       endif
      elseif(idend(ii).eq.0.and.idend(jj).eq.0)then
       goto1000
      else
       call utstop('error in fstrwr&')
      endif

  100 format(a,4e9.3,i5)

1000  continue
      call utprix('fstrwr',ish,ishini,7)
      return
      end

c-----------------------------------------------------------------------
      subroutine ProReF(ir,m,iretxx)
c-----------------------------------------------------------------------
c  proposes flavor for remnant m for proj (ir=1) or target (ir=-1)
c  and writes remnant into /cptl/ as string or hadron
c   ityptl definitions:
c      51  41  ...  rmn drop
c      52  42  ...  rmn str inel
c      53  43  ...  rmn str diff
c      54  44  ...  rmn str inel with split (or after droplet or hadron split)
c      55  45  ...  rmn res
c      56  46  ...  rmn from split without connexion
c      57  47  ...  rmn res active spectators
c      58  48  ...  rmn res from diff
c      59  49  ...  hadron split
c-----------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'
      include 'epos.incsem'

      double precision plc,s   ,ptt1,ptt2,ptt3
      common/cems5/plc,s
      double precision tpro,zpro,ttar,ztar,ttaus,detap,detat,zor,tor
      common/cttaus/tpro,zpro,ttar,ztar,ttaus,detap,detat
      common /cncl/xproj(mamx),yproj(mamx),zproj(mamx)
     *            ,xtarg(mamx),ytarg(mamx),ztarg(mamx)
      double precision amasmin,amasini,xmdrmax,xmdrmin!,utpcmd
      integer icf(2),icb(2)
      integer jcf(nflav,2),jcval(nflav,2)!,jcdummy(nflav,2)
      logical gdrop, ghadr,gproj
      double precision ept(5),ep(4),aa(5),am2t,piq1,piq2,piq3
      common/emsptl/nppr(npommx,kollmx),npproj(mamx),nptarg(mamx)
      common /ems12/iodiba,bidiba  ! defaut iodiba=0. if iodiba=1, study H-Dibaryon
      character c*1,c1*1,c2*1

      call utpri('ProReF',ish,ishini,3)

      iretxx=0

      if(ir.ne.1.and.ir.ne.-1)stop'ProReF: wrong ir'

      irmdropx=irmdrop
 55   idrop=0
      gdrop=.false.
      ghadr=.false.
      iret=0
      dens=0.0765
      do j=1,2
        do i=1,nflav
          jcf(i,j)=0
        enddo
      enddo

      flow=1.
      if(ir.eq.1)then
c        if(kolp(m).le.0)goto1000
        if(iep(m).le.-1)goto1000
        gproj=.true.
        mm=npproj(m)
        iept=iep(m)
        zz=zzremn(m,1)
        iclpt=iclpro
        isopt=isoproj
        if(iremn.ge.2)then         !number of valence quarks still in proj
          if((iept.eq.3.or.iept.eq.5).and.yrmaxi.gt.1.e-5)
     &      flow=1./fradflii**2
          do nnn=1,nrflav
            jcval(nnn,1)=jcpval(nnn,1,m)
            jcval(nnn,2)=jcpval(nnn,2,m)
          enddo
          do nnn=nrflav+1,nflav
            jcval(nnn,1)=0
            jcval(nnn,2)=0
          enddo
        else
          do nnn=1,nflav
            jcval(nnn,1)=0
          enddo
          do nnn=1,nflav
            jcval(nnn,2)=0
          enddo
        endif
      elseif(ir.eq.-1)then
c        if(kolt(m).le.0)goto1000
        if(iet(m).le.-1)goto1000
        gproj=.false.
        mm=nptarg(m)
        iept=iet(m)
        zz=zzremn(m,2)
        iclpt=icltar
        isopt=isotarg
        if(iremn.ge.2)then         !number of valence quarks still in proj
          if((iept.eq.3.or.iept.eq.5).and.yrmaxi.gt.1.e-5)
     &      flow=1./fradflii**2
          do nnn=1,nrflav
            jcval(nnn,1)=jctval(nnn,1,m)
            jcval(nnn,2)=jctval(nnn,2,m)
          enddo
          do nnn=nrflav+1,nflav
            jcval(nnn,1)=0
            jcval(nnn,2)=0
          enddo
        else
          do nnn=1,nflav
            jcval(nnn,1)=0
          enddo
          do nnn=1,nflav
            jcval(nnn,2)=0
          enddo
        endif
      else
        call utstop('ProReF: ir ???&')
      endif
      if(ish.ge.3)
     &write(ifch,*)'remnant particle index:',mm,m,iclpt,isopt

      if(ish.ge.8)call alist('ProRef&',1,nptl)
      antotre=antotre+1.

      mmini=mm
      nptlini=nptl
      minfra=min(minfra,nptlini)   !for trigger condition

      do l=1,5
       ept(l)=dble(pptl(l,mm))
      enddo

      ifrptl(1,mm)=0
      ifrptl(2,mm)=0

c  initialize forward and backward ic (to transform remnant into string)

      if(gproj)then
        icf(1)=icproj(1,m)
        icf(2)=icproj(2,m)
        if(icf(1).eq.999999)then    !more than 9 quark : use jcpref
          do j=1,2
            do i=1,nrflav
              jcf(i,j)=jcpref(i,j,m)
            enddo
          enddo
        else
          call iddeco(icf,jcf)
        endif
      else                     !gtarg
        icf(1)=ictarg(1,m)
        icf(2)=ictarg(2,m)
        if(icf(1).eq.999999)then    !more than 9 quark : use jctref
          do j=1,2
            do i=1,nrflav
              jcf(i,j)=jctref(i,j,m)
            enddo
          enddo
        else
          call iddeco(icf,jcf)
        endif
      endif
      icb(1)=0
      icb(2)=0

      call idquacjc(jcf,nqu,naq)
c use RemoveHadron if too many c quarks
      if(nrflav.gt.3)then
        nqc=jcf(4,1)+jcf(4,2)
        if(nqu.lt.3.and.jcf(4,1).gt.1.or.
     &     naq.lt.3.and.jcf(4,2).gt.1.or.
     &             jcf(4,1)*jcf(4,2).gt.1 )nqc=4
      else
        nqc=0
      endif
      if(iremn.ge.2)then
        ier=0
        ires=0
        id=idtra(icf,ier,ires,0)
        if(ier.eq.0)then
          call idspin(id,ispin,jspin,istra)
        else
          ispin=0
          jspin=0
          istra=0
        endif
      endif

c define masses

      amasmin=dble(fremnux(jcf))**2.d0
      if(ept(5).le.0.d0)then
        ept(5)=dble(fremnux(jcf)*(1.+rangen()))
        if(ish.ge.2)then
          call utmsg('ProReF')
          write(ifch,*)'zero remnant mass -> amasmin'
          call utmsgf
        endif
      endif
      am2t=sqrt(ept(1)**2+ept(2)**2+ept(5)**2)
      if(iLHC.eq.1.and.ept(4).gt.am2t.and.(iept.eq.0.or.iept.eq.6))then
        ept(3)=sign(sqrt((ept(4)+am2t)*(ept(4)-am2t)),ept(3))
      else
        ept(4)=sqrt(ept(3)*ept(3)+ept(2)*ept(2)+ept(1)*ept(1)
     &           +ept(5)*ept(5))
      endif
      am2t=(ept(4)+ept(3))*(ept(4)-ept(3))-(ept(1)**2+ept(2)**2)
      if(ish.ge.2
     &   .and.(am2t.lt.-1d0.or.abs(am2t-ept(5)*ept(5)).gt.ept(5)))then
          write(ifch,*)'Precision problem in ProRef, p:',
     &             (ept(k),k=1,4),ept(5)*ept(5),am2t
      endif

      if(ish.ge.3)then
        if(gproj)then
            write(ifch,'(a,5e11.3,2i7)')' proj:'
     &      ,(sngl(ept(k)) ,k=1,5),(icproj(k,m) ,k=1,2)
        else    !gtarg
           write(ifch,'(a,5e11.3,2i7)')' targ:'
     &      ,(sngl(ept(k)) ,k=1,5),(ictarg(k,m),k=1,2)
         endif
      endif

      amasini=ept(5)*ept(5)

      xmdrmin=dble(fremnux(jcf)+amdrmin)**2
      xmdrmax=dble(fremnux(jcf)+amdrmax)**2


      if(ish.ge.4)write(ifch,*)'remnant masses:',am2t,amasini,amasmin
     &                ,xmdrmin,zz,iept

c.............................exotic ...................................

c      if(amasini.gt.amasmin.and.irmdropx.eq.1)then

c      if(.not.((nqu.eq.3.and.naq.eq.0).or.(nqu.eq.0.and.naq.eq.3)
      if((iept.eq.3.or.iept.eq.5.or.
c     &   (iept.eq.1.and.iremn.eq.3!.and.amasini.le.xmdrmin
c     &    .and.(jcf(4,1)+jcf(4,2).eq.0)).or.
     &   .not.((nqu.eq.3.and.naq.eq.0).or.(nqu.eq.0.and.naq.eq.3)
     &           .or.(nqu.eq.1.and.naq.eq.1))).and.nqc.le.3
     &    .and.amasini.gt.amasmin.and.irmdropx.eq.1)then

c      if((
c     &   .not.((nqu.eq.3.and.naq.eq.0).or.(nqu.eq.0.and.naq.eq.3)
c     &           .or.(nqu.eq.1.and.naq.eq.1)).or.
c     &   (iept.ne.0.and.iept.le.2.and.reminv/ept(5).gt.rangen()))
c     &    .and.amasini.gt.amasmin.and.irmdropx.eq.1)then

         !print*,'-------------------------------------------' !!!
         !print*,jcf
         !print*,icf,sqrt(amasini),sqrt(amasmin),sqrt(xmdrmin)  !!!
         !print*,nqu,naq                                      !!!
c charm not possible in droplet
        if(iremn.ge.2.or.
     &     (amasini.gt.xmdrmin.or.nqc.ne.0))then
          if(iremn.eq.2)then
         call getdropx(ir,iept,m,icf,jcf,jcval,zz,ept,aa
     &                                          ,gdrop,xmdrmax)
          else
         call getdroplet(ir,iept,icf,jcf,zz,ept,aa,gdrop,xmdrmax)
          endif
          !--------------------------------
          !emit a droplet, update the remnant string flavor and 5-momentum
          ! input
          !     ir ......... 1  projectile, -1  target remnant
          !     ept ........ remnant  5-momentum
          !     jcf ........ remnant jc
          ! output
          !     gdrop ...  .true. = successful droplet emission
          !                          jcf, ept ....... droplet  ic and 5-momentum
          !                          icf, a ......... remnant string jc and 5-momentum
          !               .false. = unsuccessful
          !                          jcf, ept .... unchanged,
          !                          emits hadrons instead of droplet
c         !                          considered as droplet jc and 5-momentum
          !-------------------------------------
        endif

c redefine energy and charm quarks in droplet
        amasini=ept(5)*ept(5)
        nqc=jcf(4,1)+jcf(4,2)
c use remove hadrons if droplet too heavy (should not happen) or charm
        if(amasini.gt.1e4.or.nqc.ne.0)goto 500

        !...........droplet
        !also in case of unsuccessful drop emission, then remnant = droplet !
        idrop=1
        nptl=nptl+1
        t=xorptl(4,mm)
        istptl(mm)=41
        ifrptl(1,mm)=nptl
        ifrptl(2,mm)=nptl
        tivptl(2,mm)=t
c            Remnant radius to have eps=dens GeV/fm3
        radptl(nptl)=(3.*sngl(ept(5))/4./pi/dens)**0.3333
        dezptl(nptl)=0.
        do l=1,5
          pptl(l,nptl)=sngl(ept(l))
        enddo
        if(gdrop)then
          idx=0
        else
          if(iLHC.eq.1)then
            idx=idtra(icf,0,0,0)
          else
            idx=idtra(icf,0,0,3)
          endif
        endif
        if(abs(idx).gt.100)then
         amx=sngl(ept(5))
         call idres(idx,amx,idrx,iadjx)
         idx=idrx
        else
         idx=0
        endif
        if(idx.eq.0)then
          istptl(nptl)=10
          call idenct(jcf,idptl(nptl)
     *    ,ibptl(1,nptl),ibptl(2,nptl),ibptl(3,nptl),ibptl(4,nptl))
          if(gproj)then
            ityptl(nptl)=40
          else  !gtarg
            ityptl(nptl)=50
          endif
        else
          istptl(nptl)=0
          idptl(nptl)=idx
          pptl(5,nptl)=amx
          pptl(4,nptl)=sqrt(amx*amx+pptl(1,nptl)*pptl(1,nptl)
     &       +pptl(2,nptl)*pptl(2,nptl)+pptl(3,nptl)*pptl(3,nptl))
          if(gproj)then
            ityptl(nptl)=45
            if(iept.eq.6)ityptl(nptl)=47
          else  !gtarg
            ityptl(nptl)=55
            if(iept.eq.6)ityptl(nptl)=57
          endif
        endif
        iorptl(nptl)=mm
        jorptl(nptl)=0
        ifrptl(1,nptl)=0
        ifrptl(2,nptl)=0
        xorptl(1,nptl)=xorptl(1,mm)
        xorptl(2,nptl)=xorptl(2,mm)
        xorptl(3,nptl)=xorptl(3,mm)
        xorptl(4,nptl)=t
        tivptl(1,nptl)=t
        call idtau(idptl(nptl),pptl(4,nptl),pptl(5,nptl),taugm)
        tivptl(2,nptl)=tivptl(1,nptl)+taugm*(-alog(rangen()))
        do l=1,4
          ibptl(l,nptl)=0
        enddo
        andropl=andropl+1
        if(ish.ge.3)write(ifch,*)'Proref,ept(5),id',ept(5),idptl(nptl)
        !print*,nptl,idptl(nptl),sngl(ept(5)),pptl(5,nptl)  !!!

        !..........remnant update
        if(gdrop)then  !drop emission: new remnant -> ept, icf
          idrop=0
          do l=1,5
            ept(l)=aa(l)
          enddo
          call iddeco(icf,jcf)
          call idquacjc(jcf,nqu,naq)
          if(iret.eq.1)call utstop('Pb in ProRef in strg+drop process&')
          !!!  print*,'new remnant:',icf,ept(5)    !!!
          nptl=nptl+1
          t=xorptl(4,mm)
          ifrptl(2,mm)=nptl
          do l=1,5
            pptl(l,nptl)=sngl(ept(l))
          enddo
          idptl(nptl)=idptl(mm)
          istptl(nptl)=40
          iorptl(nptl)=mm
          jorptl(nptl)=0
          ifrptl(1,nptl)=0
          ifrptl(2,nptl)=0
          xorptl(1,nptl)=xorptl(1,mm)
          xorptl(2,nptl)=xorptl(2,mm)
          xorptl(3,nptl)=xorptl(3,mm)
          xorptl(4,nptl)=t
          tivptl(1,nptl)=t
          tivptl(2,nptl)=ainfin
          if(gproj)then
            ityptl(nptl)=40
          else   !gtarg
            ityptl(nptl)=50
          endif
          do l=1,4
            ibptl(l,nptl)=0
          enddo
        endif

        !........decay mini-droplet......
        mm=nptlini+1
        nptlb=nptl
        if(iabs(idptl(mm)).gt.10**8)then

          iret=0
          if(iorsdf.ne.3.or.pptl(5,mm).gt.100.
     &       .or.amasini.le.amasmin*flow)then      !decay here only if no fusion or large mass or mass too low for flow

          if(ish.ge.3)write(ifch,*)'Decay remnant droplet...'
          if(nptlb.gt.mxptl-10)call utstop('ProRef: mxptl too small&')

          if(ifrade.gt.0.and.ispherio.eq.0)then
            if(ioclude.eq.3.or.dble(pptl(5,mm)).lt.xmdrmin)then
              call hnbaaa(mm,iret)
            else
              call DropletDecay(mm,iret)!Decay remn
              iret=0
            endif
          endif
          if(iret.ne.1.and.nptl.ne.nptlb)then ! ---successful decay---
            istptl(mm)=istptl(mm)+1
            ifrptl(1,mm)=nptlb+1
            ifrptl(2,mm)=nptl
            t=tivptl(2,mm)
            x=xorptl(1,mm)+(t-xorptl(4,mm))*pptl(1,mm)/pptl(4,mm)
            y=xorptl(2,mm)+(t-xorptl(4,mm))*pptl(2,mm)/pptl(4,mm)
            z=xorptl(3,mm)+(t-xorptl(4,mm))*pptl(3,mm)/pptl(4,mm)
            do 21 n=nptlb+1,nptl
              iorptl(n)=mm
              jorptl(n)=0
              istptl(n)=0
              ifrptl(1,n)=0
              ifrptl(2,n)=0
              radius=0.8*sqrt(rangen())
              phi=2*pi*rangen()
              ti=t
              zi=z
              xorptl(1,n)=x + radius*cos(phi)
              xorptl(2,n)=y + radius*sin(phi)
              xorptl(3,n)=zi
              xorptl(4,n)=ti
              iioo=mm
              zor=dble(xorptl(3,iioo))
              tor=dble(xorptl(4,iioo))
c              call idquac(iioo,nq,ndummy1,ndummy2,jcdummy)
              r=rangen()
              tauran=-taurea*alog(r)
              call jtaix(n,tauran,zor,tor,zis,tis)
              tivptl(1,n)=amax1(ti,tis)
              call idtau(idptl(n),pptl(4,n),pptl(5,n),taugm)
              r=rangen()
              tivptl(2,n)=t+taugm*(-alog(r))
              ityptl(n)=ityptl(n)+1
              if(iept.eq.6)ityptl(n)=ityptl(n)+6
              radptl(n)=0.
              dezptl(n)=0.
              itsptl(n)=0
              rinptl(nptl)=-9999
   21       continue
            if(iabs(idptl(nptlb+1)).le.6) then
              call gakli2(0,0)
              if(ish.ge.1)write (ifmt,*)'string from drop:nptlb+1,nptl:'
     *                                 ,nptlb+1,nptl
              istptl(nptlb+1)=1
              do n=nptlb+2,nptl
                istptl(n)=20
                zpaptl(1,n)=0.
                zpaptl(2,n)=0.
              enddo
              call gakfra(0,iret)
              call gakli2(0,0)
            endif
            jerr(4)=jerr(4)+1
          elseif(ifrade.gt.0.and.ispherio.eq.0)then ! Unsuccessful decay
            jerr(5)=jerr(5)+1
            if(ish.ge.4)write(ifch,*)
     *         '***** Unsuccessful remnant cluster decay'
     *             ,' --> do RemoveHadrons instead.'
            mm=mmini
            nptl=nptlini
            irmdropx=0
            goto 55
          endif

          endif
        endif

        if(idrop.eq.1)goto 1000
        !successful drop decay, no additional string, nothing to do

      endif

c...............................................................

 500  mm=mmini
      if(gdrop)mm=nptlini+2
      istptl(mm)=41
      ifrptl(1,mm)=nptl+1

c........................remove hadrons.........................

      if(.not.((nqu.eq.3.and.naq.eq.0).or.(nqu.eq.0.and.naq.eq.3)
     &          .or.(nqu.eq.1.and.naq.eq.1)))then
        if(irmdropx.eq.irmdrop)then
          jerr(6)=jerr(6)+1
             !call utmsg('ProReF')
             !write(ifch,*)'***** condition for droplet treatment: '
             !write(ifch,*)'*****  amasini.gt.amasmin.and.irmdropx.eq.1 = '
             !*           ,amasini.gt.amasmin.and.irmdropx.eq.1
             !write(ifch,*)'***** amasini,amasmin,irmdropx:'
             !*                 ,amasini,amasmin,irmdropx
             !write(ifch,*)'***** nqu,naq:',nqu,naq
             !write(ifch,*)'***** call RemoveHadrons'
             !call utmsgf
        endif
       call RemoveHadrons(gproj,ghadr,m,mm,jcf,jcval,icf,ept,iret)
       if(iret.ne.0)then
         iretxx=1
         goto 1000
       endif
      endif

c........................ determine idr (0=string, else=resonance).......

      if(icf(1).eq.0.and.icf(2).eq.0)then
        id=110
      else
        if(iLHC.eq.1)then
          id=idtra(icf,0,0,0)
        else
          id=idtra(icf,0,0,3)
        endif
      endif
      idr=0
      am=sngl(ept(5))
      call idres(id,am,idr,iadj)
c      if(iabs(mod(idr,10)).le.2.and.idr.ne.0)then
c       id=idr
c      else
c       idr=0
c      endif                                !ckeck on-shell mass (see uti)
      if(iadj.ne.0.and.iept.gt.0.and.ept(5).gt.0.d0
     &     .and.(dabs((ept(4)+ept(3))*(ept(4)-ept(3))
     $           -ept(2)**2-ept(1)**2-dble(am)**2).gt.0.3d0))idr=0

      if(ish.ge.3)then
        write(ifch,'(a,5e11.3)')' updt:',(sngl(ept(k)) ,k=1,5)
        write(ifch,*)'            icf: ',icf,' idr: ',idr,' iept: ',iept
      endif

c      if(iept.eq.3)stop'ProReF: iept=3 ???'

c...........................................string...................
      if(iept.gt.0.and.iept.ne.6.and.idr.eq.0)then

        !... nqu of remainder string

        anstrg0=anstrg0+1
        if(gdrop)anstrg1=anstrg1+1

        call iddeco(icf,jcf)
        nqu=0
        nqv=0
        nav=0
        do l=1,nrflav
          nqu=nqu+jcf(l,1)-jcf(l,2)
          nqv=nqv+jcval(l,1)+jcval(l,2)
          nav=nav+jcval(l,2)
        enddo

c        if(zrminc.lt.0.)stop'ProReF: not supported any more.         '

        !......determine forward momentum ep


        am1=0.
        am2=0.
        ptt1=0d0
        ptt2=0d0
        if(iLHC.eq.1)then
          pt=ranptcut(1.)*ptfraqq
          if(pt.lt.0.5d0*ept(5))then
            phi=2.*pi*rangen()
            ptt1=dble(pt*cos(phi))
            ptt2=dble(pt*sin(phi))
          endif
          ptt3=dble(ir)*sqrt((0.5d0*ept(5))**2-ptt1*ptt1-ptt2*ptt2)
        else
          ptt3=dble(ir)*0.5d0*ept(5)
        endif

        ep(1)=ptt1
        ep(2)=ptt2
        ep(3)=ptt3
cc        ep(4)=0.5d0*ept(5)
        ep(4)=sqrt(ptt3*ptt3+ptt2*ptt2+ptt1*ptt1+dble(am1*am1))

c        if(abs(ept(3)).le.ptsend)then
c          phi=2.*pi*rangen()
c          theta=2.*pi*rangen()
c          ca=cos(theta)
c          sa=sin(theta)
c          call utroa2(dble(phi),dble(ca),dble(sa),0d0,ep(1),ep(2),ep(3))
c        endif
        call utlob2(-1,ept(1),ept(2),ept(3),ept(4),ept(5)
     *     ,ep(1),ep(2),ep(3),ep(4),25)


        xxx=min(1.,sngl(abs(ep(3)/ep(4))))
        qqs=sngl(ept(5)**2)

        !....determine forward and backward flavor icf, icb

        if(iremn.ge.2)then
          xm3val=9.
          xm2val=3.
          xm1val=1.
          ntryx=0
 33       xx1=0.
          xx2=0.
          xx3=0.
          del=1./(1.-alppar)
          if(nqv.eq.3)then
            xx1=min(1.,ranptcut(xm3val))
            xx2=min(1.,ranptcut(xm3val))
            xx3=min(1.,ranptcut(xm3val))
          elseif(nqv.eq.2)then
            xx1=min(1.,ranptcut(xm2val))
            xx2=min(1.,ranptcut(xm2val))
            xx3=rangen()**del
          elseif(nqv.eq.1)then
            xx1=min(1.,ranptcut(xm1val))
            xx2=rangen()**del
            xx3=rangen()**del
          else
            xx1=rangen()**del
            xx2=rangen()**del
            xx3=rangen()**del
          endif
          if(ntryx.lt.1000)then
            if(xx1+xx2+xx3.gt.1)goto 33
          else
            xx1=rangen()
            xx2=rangen()*(1.-xx1)
            xx3=rangen()*(1.-xx1-xx2)
          endif
          xx1=xxx*xx1
          xx2=xxx*xx2
          xx3=xxx*xx3
          piq1=0d0
          piq2=0d0
          piq3=0d0
          if(iept.eq.4)then
            ireminv=0       !no inversion for very low mass diffraction
          else
c inversion needed for inelatic remnant because of cascade (NA49)
            ireminv=1  
          endif
       if(nqu.eq.3)then      !---baryon---
          c="s"
          if(nqv.ge.1)c="v"
          iq1=idraflx(piq1,xx1,qqs,iclpt,jcf,jcval,1,isopt,c)
          c="s"
          if(nqv.ge.2)c="v"
          iq2=idraflx(piq2,xx2,qqs,iclpt,jcf,jcval,1,isopt,c)
          c="s"
          if(nqv.ge.3)c="v"
          iq3=idraflx(piq3,xx3,qqs,iclpt,jcf,jcval,1,isopt,c)
c rescale x to have heavier quark backward (neutron in ZEUS or lambda in NA49 not forward)
c          if(iept.eq.2)then
c            if(isopt.gt.0)then
c              xx1=xx1/float(iq1)
c              xx2=xx2/float(iq2)
c              xx3=xx3/float(iq3)
c            elseif(isopt.lt.0)then
c              if(iq1.lt.3)then
c                xx1=xx1/float(3-iq1)
c              else
c                xx1=xx1/float(iq1)
c              endif
c              if(iq2.lt.3)then
c                xx2=xx2/float(3-iq2)
c              else
c                xx2=xx2/float(iq2)
c              endif
c              if(iq3.lt.3)then
c                xx3=xx3/float(3-iq3)
c              else
c                xx3=xx3/float(iq3)
c              endif
c            endif
c          endif
          call neworderx(xx3,xx2,xx1,iq3,iq2,iq1)
          if(xx2-xx3.gt.reminv*(xx1-xx2))ireminv=0
c put always strange quarks in diquark (for lambda and cascade (NA49))
          if(iq3.ge.3.and.ireminv.eq.0)ireminv=1 !here inversion only in diffraction except for strange particles (lambda and cascade very central)
c if inversion for diffractive and inelastic
c          if(iq1+iq2.lt.6.and.iq3.ge.3.and.ireminv.eq.0)then
c            iqtmp=iq3
c            if(iq2.eq.3.or.(iq1.ne.3.and.rangen().gt.0.5))then
c              iq3=iq1
c              iq1=iqtmp
c            else
c              iq3=iq2
c              iq2=iqtmp
c            endif
c          endif
          if(ireminv.eq.0)then
            call uticpl(icf,iq3,2,iret) ! antiquark
            call uticpl(icb,iq3,1,iret) ! quark
          else
            call uticpl(icf,iq3,2,iret) ! antiquark
            call uticpl(icb,iq3,1,iret) ! quark
            call uticpl(icf,iq2,2,iret) ! antiquark
            call uticpl(icb,iq2,1,iret) ! quark
          endif
        elseif(nqu.eq.-3)then !---antibaryon---
          c="s"
          if(nqv.ge.1)c="v"
          iq1=idraflx(piq1,xx1,qqs,iclpt,jcf,jcval,2,isopt,c)
          c="s"
          if(nqv.ge.2)c="v"
          iq2=idraflx(piq2,xx2,qqs,iclpt,jcf,jcval,2,isopt,c)
          c="s"
          if(nqv.ge.3)c="v"
          iq3=idraflx(piq3,xx3,qqs,iclpt,jcf,jcval,2,isopt,c)
c rescale x to have heavier quark backward (neutron in ZEUS or lambda in NA49 not forward)
c          if(iept.eq.2)then
c            if(isopt.gt.0)then
c              xx1=xx1/float(iq1)
c              xx2=xx2/float(iq2)
c              xx3=xx3/float(iq3)
c            elseif(isopt.lt.0)then
c              if(iq1.lt.3)then
c                xx1=xx1/float(3-iq1)
c              else
c                xx1=xx1/float(iq1)
c              endif
c              if(iq2.lt.3)then
c                xx2=xx2/float(3-iq2)
c              else
c                xx2=xx2/float(iq2)
c              endif
c              if(iq3.lt.3)then
c                xx3=xx3/float(3-iq3)
c              else
c                xx3=xx3/float(iq3)
c              endif
c            endif
c          endif
          call neworderx(xx3,xx2,xx1,iq3,iq2,iq1)
          if(xx2-xx3.gt.reminv*(xx1-xx2))ireminv=0
c put always strange quarks in diquark
          if(iq3.ge.3.and.ireminv.eq.0)ireminv=1
c          if(iq1+iq2.lt.6.and.iq3.ge.3.and.ireminv.eq.0)then
c            iqtmp=iq3
c            if(iq2.eq.3.or.(iq1.ne.3.and.rangen().gt.0.5))then
c              iq3=iq1
c              iq1=iqtmp
c            else
c              iq3=iq2
c              iq2=iqtmp
c            endif
c          endif
          if(ireminv.eq.0)then
            call uticpl(icf,iq3,1,iret) ! quark
            call uticpl(icb,iq3,2,iret) ! antiquark
          else
            call uticpl(icf,iq1,1,iret) ! quark
            call uticpl(icb,iq1,2,iret) ! antiquark
            call uticpl(icf,iq2,1,iret) ! quark
            call uticpl(icb,iq2,2,iret) ! antiquark
          endif
        elseif(nqu.eq.0)then !---meson---
          xx3=0.    !no third quark
          iq3=0
          if(nqv.eq.2)then
            c1="v"
            c2="v"
            j=min(2,1+int(0.5+rangen()))
          elseif(nav.ne.0)then    !valence antiquark
            c1="v"
            c2="s"
            j=2
          elseif(nqv.ne.0)then    !valence quark
            c1="v"
            c2="s"
            j=1
          else                    !only sea quarks
            c1="s"
            c2="s"
            j=min(2,1+int(0.5+rangen()))
          endif
          iq1=idraflx(piq1,xx1,qqs,iclpt,jcf,jcval,j,isopt,c1)
          iq2=idraflx(piq2,xx2,qqs,iclpt,jcf,jcval,3-j,isopt,c2)
          if(xx1.gt.xx2)ireminv=0
         if(ireminv.eq.1)then
            call uticpl(icf,iq1,3-j,iret) ! subtract quark 1 forward
            call uticpl(icb,iq1,j,iret) ! add quark 1 backward
          else
            call uticpl(icf,iq2,j,iret) ! subtract antiquark 2 forward
            call uticpl(icb,iq2,3-j,iret) ! add antiquark 2 backward
          endif
        else
          call utmsg('ProReF')
          write(ifch,*)'***** neither baryon nor antibaryon nor meson.'
          write(ifch,*)'*****  number of net quarks:',nqu
          write(ifmt,*)'ProReF: no hadron; ',nqu,' quarks  --> redo'
          iretxx=1
          goto 1000
        endif
        if(ish.ge.3)write(ifch,'(a,2i3,3(i2,e13.6))')' inversion:',isopt
     &         ,ireminv,iq1,xx1,iq2,xx2,iq3,xx3
        else
        ireminv=0
        if(iept.ne.0)then
          if(rangen().lt.reminv)ireminv=1
        endif
        if(nqu.eq.3)then      !---baryon---
          iq=idrafl(iclpt,jcf,1,'v',1,iret)
          call uticpl(icf,iq,2,iret)       ! antiquark
          call uticpl(icb,iq,1,iret)       ! quark
          if(ireminv.eq.1)then
           iq=idrafl(iclpt,jcf,1,'v',1,iret)
           call uticpl(icf,iq,2,iret)       ! antiquark
           call uticpl(icb,iq,1,iret)       ! quark
          endif
        elseif(nqu.eq.-3)then !---antibaryon---
          iq=idrafl(iclpt,jcf,2,'v',1,iret)
          call uticpl(icf,iq,1,iret)       ! quark
          call uticpl(icb,iq,2,iret)       ! antiquark
          if(ireminv.eq.1)then
           iq=idrafl(iclpt,jcf,2,'v',1,iret)
           call uticpl(icf,iq,1,iret)       ! quark
           call uticpl(icb,iq,2,iret)       ! antiquark
          endif
        elseif(nqu.eq.0)then !---meson---
           iq1=idrafl(iclpt,jcf,1,'v',1,iret)
           iq2=idrafl(iclpt,jcf,2,'v',1,iret)
           if(rangen().gt.0.5)then
             call uticpl(icf,iq1,2,iret) ! subtract quark
             call uticpl(icb,iq1,1,iret) ! add quark
           else
             call uticpl(icf,iq2,1,iret) ! subtract antiquark
             call uticpl(icb,iq2,2,iret) ! add antiquark
           endif
c        elseif(nqu.eq.0)then !---meson---
c          if(iept.ne.1.and.iept.ne.6.and.rangen().lt.0.5)then
c           iq=idrafl(iclpt,jcf,1,'v',1,iret)
c           call uticpl(icf,iq,2,iret)       ! subtract quark
c           call uticpl(icb,iq,1,iret)       ! add quark
c          else
cc put quark in forward direction always for inelastic
c           iq=idrafl(iclpt,jcf,2,'v',1,iret)
c           call uticpl(icf,iq,1,iret)       ! subtract antiquark
c           call uticpl(icb,iq,2,iret)       ! add antiquark
c          endif
        else
          if(ish.ge.1)then
          call utmsg('ProReF')
          write(ifch,*)'***** neither baryon nor antibaryon nor meson.'
          write(ifch,*)'*****  number of net quarks:',nqu
          endif
          write(ifmt,*)'ProReF: no hadron; ',nqu,' quarks  --> redo'
          iretxx=1
          goto1000
        endif
      endif


        !..... forward string end

        nptl=nptl+1
        if(nptl.gt.mxptl)call utstop('ProRef: mxptl too small&')
        pptl(1,nptl)=sngl(ep(1))
        pptl(2,nptl)=sngl(ep(2))
        pptl(3,nptl)=sngl(ep(3))
        pptl(4,nptl)=sngl(ep(4))
        pptl(5,nptl)=am1 !0.
        istptl(nptl)=20
        iorptl(nptl)=mm
        if(.not.gdrop)istptl(mm)=41
        jorptl(nptl)=0
        if(.not.ghadr.and..not.gdrop)ifrptl(1,mm)=nptl
        ifrptl(2,mm)=nptl
        xorptl(1,nptl)=xorptl(1,mm)
        xorptl(2,nptl)=xorptl(2,mm)
        xorptl(3,nptl)=xorptl(3,mm)
        xorptl(4,nptl)=xorptl(4,mm)
        tivptl(1,nptl)=xorptl(4,nptl)
        tivptl(2,nptl)=xorptl(4,nptl)
        if(iLHC.eq.1)then
          idptl(nptl)=idtra(icf,0,0,0)
        else
          idptl(nptl)=idtra(icf,0,0,3)
        endif
        if(gproj)then
          if(iep(m).lt.1)stop'ProReF: iep(m)<1     '
          ityptl(nptl)=41+iep(m)  ! =42 =43 =44 =46 =47
          if(iep(m).eq.4)ityptl(nptl)=42
          if(gdrop.and.iep(m).ne.6)ityptl(nptl)=44
          if(ghadr)ityptl(nptl)=44
        else  !gtarg
          if(iet(m).lt.1)stop'ProReF: iet(m)<1     '
          ityptl(nptl)=51+iet(m)  !=52 =53 =54 =56 =57
          if(iet(m).eq.4)ityptl(nptl)=52
          if(gdrop.and.iet(m).ne.6)ityptl(nptl)=54
          if(ghadr)ityptl(nptl)=54
        endif
        itsptl(nptl)=1
        qsqptl(nptl)=qqs
        rinptl(nptl)=-9999
        !write(6,'(a,i9,$)')'     ',idptl(nptl) !======================
        zpaptl(1,nptl)=zz
        if(gproj)then
          zpaptl(2,nptl)=float(lproj(m))
c          zpaptl(2,nptl)=0.
c          if(lproj(m).ge.1)then
c            do l=1,lproj(m)
c              kpair=kproj(m,l)
c              itt=itarg(kpair)
c              zpaptl(2,nptl)=zpaptl(2,nptl)+zzremn(itt,2)
c            enddo
c          endif
        else  !gtarg
          zpaptl(2,nptl)=float(ltarg(m))
c          zpaptl(2,nptl)=0.
c          if(ltarg(m).ge.1)then
c            do l=1,ltarg(m)
c              kpair=ktarg(m,l)
c              ipp=iproj(kpair)
c              zpaptl(2,nptl)=zpaptl(2,nptl)+zzremn(ipp,1)
c            enddo
c          endif
        endif
        if(ish.ge.3)then
          write(ifch,'(a,5e11.3,$)')' kink:',(pptl(k,nptl),k=1,5)
          write(ifch,*)' id: ',idptl(nptl)
        endif
        !....... backward string end

        nptl=nptl+1
        if(nptl.gt.mxptl)call utstop('ProRef: mxptl too small&')
        pptl2=0.
        do i=1,3
         pptl(i,nptl)=sngl(ept(i)-ep(i))
         pptl2=pptl2+pptl(i,nptl)*pptl(i,nptl)
        enddo
        pptl(5,nptl)=am2 !0.
        pptl2=pptl2+pptl(5,nptl)*pptl(5,nptl)
        pptl(4,nptl)=sqrt(pptl2)
        pptl2=sngl(ept(4)-ep(4))
        if(ish.ge.1.and.abs(pptl2-pptl(4,nptl)).gt.max(0.1,
     &                                         0.1*abs(pptl2)))then
          write(ifmt,*)
     &    'Warning in ProRef: inconsistent backward string end energy !'
     &    ,pptl(4,nptl),pptl2,abs(pptl2-pptl(4,nptl)),am1,am2,ptt3,ep(4)
          if(ish.ge.2)write(ifch,*)
     &    'Warning in ProRef: inconsistent backward string end energy !'
     &    ,(pptl(kkk,nptl),kkk=1,4),pptl2,abs(pptl2-pptl(4,nptl))
        endif
        istptl(nptl)=20
        iorptl(nptl)=mm
        jorptl(nptl)=0
        ifrptl(2,mm)=nptl
        ifrptl(1,nptl)=0
        ifrptl(2,nptl)=0
        xorptl(1,nptl)=xorptl(1,mm)
        xorptl(2,nptl)=xorptl(2,mm)
        xorptl(3,nptl)=xorptl(3,mm)
        xorptl(4,nptl)=xorptl(4,mm)
        tivptl(1,nptl)=xorptl(4,nptl)
        tivptl(2,nptl)=xorptl(4,nptl)
        if(iLHC.eq.1)then
          idptl(nptl)=idtra(icb,0,0,0)
        else
          idptl(nptl)=idtra(icb,0,0,3)
        endif
        if(gproj)then
          ityptl(nptl)=41+iep(m)  ! =42 =43 =47
          if(iep(m).eq.4)ityptl(nptl)=42
          if(gdrop.and.iep(m).ne.6)ityptl(nptl)=44
          if(ghadr)ityptl(nptl)=44
        else  !gtarg
          ityptl(nptl)=51+iet(m)  !=52 =53 =57
          if(iet(m).eq.4)ityptl(nptl)=52
          if(gdrop.and.iet(m).ne.6)ityptl(nptl)=54
          if(ghadr)ityptl(nptl)=54
        endif
        itsptl(nptl)=1
        qsqptl(nptl)=qqs
        rinptl(nptl)=-9999
        !write(6,'(a,i9)')'     ',idptl(nptl)
        zpaptl(1,nptl)=0.
        zpaptl(2,nptl)=1.
        if(ish.ge.3)then
          write(ifch,'(a,5e11.3,$)')' kink:',(pptl(k,nptl),k=1,5)
          write(ifch,*)' id: ',idptl(nptl)
        endif

c............................no string = resonance...................
      else

        anreso0=anreso0+1
        if(gdrop)anreso1=anreso1+1

        nptl=nptl+1
        if(idr.ne.0)id=idr
        if(nptl.gt.mxptl)call utstop('ProRef: mxptl too small&')
        if(iept.eq.0.or.iept.eq.6)call idmass(id,am)
        idptl(nptl)=id
        pptl(1,nptl)=sngl(ept(1))
        pptl(2,nptl)=sngl(ept(2))
        am2t=sqrt(ept(2)*ept(2)+ept(1)*ept(1)+dble(am*am))
        if(iLHC.eq.1.and.ept(4).gt.am2t)then   !conserve value of E on not pz
          pptl(4,nptl)=sngl(ept(4))
          pptl(3,nptl)=sngl(sign(sqrt((ept(4)+am2t)*(ept(4)-am2t))
     &                          ,ept(3)))
        else
          pptl(3,nptl)=sngl(ept(3))
          pptl(4,nptl)=sngl(sqrt(ept(3)*ept(3)+am2t))
        endif
        pptl(5,nptl)=am
        istptl(nptl)=0
        iorptl(nptl)=mm
        if(.not.gdrop)istptl(mm)=41
        jorptl(nptl)=0
        if(.not.ghadr.and..not.gdrop)ifrptl(1,mm)=nptl
        ifrptl(2,mm)=nptl
        ifrptl(1,nptl)=0
        ifrptl(2,nptl)=0
        xorptl(1,nptl)=xorptl(1,mm)
        xorptl(2,nptl)=xorptl(2,mm)
        xorptl(3,nptl)=xorptl(3,mm)
        xorptl(4,nptl)=xorptl(4,mm)
        tivptl(1,nptl)=xorptl(4,nptl)
        call idtau(idptl(nptl),pptl(4,nptl),pptl(5,nptl),taugm)
        tivptl(2,nptl)=tivptl(1,nptl)+taugm*(-alog(rangen()))
        if(gproj)then
          ityptl(nptl)=45
          if(gdrop)then
            ityptl(nptl)=46
          elseif(iept.eq.6)then
            ityptl(nptl)=47
          elseif(iept.eq.2.or.iept.eq.4)then
c          elseif(iept.eq.2)then
            ityptl(nptl)=48
          elseif(ghadr)then
            ityptl(nptl)=49
          else
            mine=0
            mdif=0
            do l=1,lproj(m)
              kp=kproj(m,l)
              if(abs(itpr(kp)).eq.1)mine=1
              if(itpr(kp).eq.2)mdif=1
            enddo
            if(mine.eq.0.and.mdif.eq.1)ityptl(nptl)=48
          endif
        else   !gtarg
          ityptl(nptl)=55
          if(gdrop)then
            ityptl(nptl)=56
          elseif(iept.eq.6)then
            ityptl(nptl)=57
          elseif(iept.eq.2.or.iept.eq.4)then
c          elseif(iept.eq.2)then
            ityptl(nptl)=58
          elseif(ghadr)then
            ityptl(nptl)=59
          else
            mine=0
            mdif=0
            do l=1,lproj(m)
              kp=kproj(m,l)
              if(abs(itpr(kp)).eq.1)mine=1
              if(itpr(kp).eq.2)mdif=1
            enddo
            if(mine.eq.0.and.mdif.eq.1)ityptl(nptl)=58
          endif
        endif
        itsptl(nptl)=0
        qsqptl(nptl)=0.
        rinptl(nptl)=-9999

        if(ish.ge.3)write(ifch,'(a,5e10.3,i7)')' nucl:'
     *         ,(pptl(i,nptl),i=1,5),idptl(nptl)

      endif
c.......................................................................
c      print *,iep(1),iet(1),ityptl(nptl)
 1000 call utprix('ProReF',ish,ishini,3)
ctp060829        if(ityptl(nptl).gt.60)print*,ityptl(nptl)
      return

      end

c-----------------------------------------------------------------------
      subroutine RemoveHadrons(gproj,ghadr,m,mm,jcf,jcv
     &                        ,icf,ept,iret)
c-----------------------------------------------------------------------
      include 'epos.inc'
      include 'epos.incems'
      integer jcf(nflav,2),jcv(nflav,2),icf(2)
      double precision aa(5),ept(5)
      logical ghadr,gproj
      common/ems6/ivp0,iap0,idp0,isp0,ivt0,iat0,idt0,ist0
      common /cncl/xproj(mamx),yproj(mamx),zproj(mamx)
     *            ,xtarg(mamx),ytarg(mamx),ztarg(mamx)

      iret=0

      if(iremn.ge.2)then
        if(gproj)then
          idrf=idp(m)
        else
          idrf=idt(m)
        endif
      else
        if(gproj)then
          idrf=idp0
        else
          idrf=idt0
        endif
      endif
      call idquacjc(jcf,nqu,naq)
      if(nqu.eq.naq.and.(nqu.le.2.or.idrf.eq.0))then
        nmes=nqu
        nmes=nmes-1             !string is aq-q
        nbar=0
      elseif(nqu.gt.naq)then
        nmes=naq
        nbar=(nqu-nmes)/3     !nbar baryons
        if(nmes.eq.0.or.idrf.eq.1)then
          nbar=nbar-1        !string is qq-q
        else
          nmes=nmes-1        !string is aq-q
        endif
      elseif(nqu.lt.naq)then
        nmes=nqu
        nbar=(naq-nmes)/3    !nbar antibaryons
        if(nmes.eq.0.or.idrf.eq.1)then
          nbar=nbar-1        !string is aqaq-aq
        else
          nmes=nmes-1        !string is aq-q
        endif
      else
        nbar=nqu/3
        nmes=nqu-3*nbar
        nbar=nbar+naq/3
        nbar=nbar-1             !string is qq-q or aqaq-aq
      endif
      if(ish.ge.5)
     &       write(ifch,*)'RemoveHadron part (nq,na,nb,nm,dq):'
     &                     ,nqu,naq,nbar,nmes,idrf
      if(nmes+nbar.gt.0)ghadr=.true.
c  remove mesons
       if(nmes.gt.0)then
          do mes=1,nmes
            !write(ifch,*)'remove meson',mes,' / ',nmes
            call gethadron(1,idd,aa,jcf,jcv,ept,gproj,iret)
            if(iret.ne.0)goto 1000
              nptl=nptl+1
              if(nptl.gt.mxptl)
     &             call utstop('RemoveHadrons: mxptl too small&')
              idptl(nptl)=idd
              do i=1,5
                pptl(i,nptl)=sngl(aa(i))
              enddo
              iorptl(nptl)=mm
              jorptl(nptl)=0
              if(mes.eq.1)then
                ifrptl(1,mm)=nptl
                ifrptl(2,mm)=nptl
              else
                ifrptl(2,mm)=nptl
              endif
              ifrptl(1,nptl)=0
              ifrptl(2,nptl)=0
              istptl(nptl)=0
              if(gproj)then
                ityptl(nptl)=49
                xorptl(1,nptl)=xproj(m)
                xorptl(2,nptl)=yproj(m)
                xorptl(3,nptl)=zproj(m)
              else   !gtarg
                ityptl(nptl)=59
                xorptl(1,nptl)=xtarg(m)
                xorptl(2,nptl)=ytarg(m)
                xorptl(3,nptl)=ztarg(m)
              endif
              xorptl(4,nptl)=xorptl(4,mm)
              tivptl(1,nptl)=xorptl(4,nptl)
              call idtau(idptl(nptl),pptl(4,nptl),pptl(5,nptl),taugm)
              tivptl(2,nptl)=tivptl(1,nptl)+taugm*(-alog(rangen()))
              qsqptl(nptl)=0.
c           deleted: after abstracting a meson,
c           check if the NEW remnant is a H-Dibaryon
          enddo
        endif
c remove (anti)baryons
        call idquacjc(jcf,nqu,naq)
        if(nbar.gt.0)then
          do nb=1,nbar
            !write(ifch,*)'remove baryon',nb,' / ',nbar
            prq=float(nqu/3)
            pra=float(naq/3)
            psum=prq+pra
            if(psum.gt.0.)then
              if(rangen()*psum.le.prq)then      !baryon
                call gethadron(2,idd,aa,jcf,jcv,ept,gproj,iret)
                nqu=nqu-3
              else                              !antibaryon
                call gethadron(3,idd,aa,jcf,jcv,ept,gproj,iret)
                naq=naq-3
             endif
            else
              iret=1
            endif
            if(iret.ne.0)goto 1000
              nptl=nptl+1
              if(nptl.gt.mxptl)
     &             call utstop('RemoveHadron: mxptl too small&')
              idptl(nptl)=idd
              do i=1,5
                pptl(i,nptl)=sngl(aa(i))
              enddo
              iorptl(nptl)=mm
              jorptl(nptl)=0
              if(nmes.eq.0.and.nb.eq.1)then
                ifrptl(1,mm)=nptl
                ifrptl(2,mm)=nptl
              else
                ifrptl(2,mm)=nptl
              endif
              ifrptl(1,nptl)=0
              ifrptl(2,nptl)=0
              istptl(nptl)=0
              if(gproj)then
                ityptl(nptl)=49
                xorptl(1,nptl)=xproj(m)
                xorptl(2,nptl)=yproj(m)
                xorptl(3,nptl)=zproj(m)
              else    !gtarg
                ityptl(nptl)=59
                xorptl(1,nptl)=xtarg(m)
                xorptl(2,nptl)=ytarg(m)
                xorptl(3,nptl)=ztarg(m)
              endif
              xorptl(4,nptl)=xorptl(4,mm)
              tivptl(1,nptl)=xorptl(4,nptl)
              call idtau(idptl(nptl),pptl(4,nptl),pptl(5,nptl),taugm)
              tivptl(2,nptl)=tivptl(1,nptl)+taugm*(-alog(rangen()))
              qsqptl(nptl)=0.
c             deleted: after abstracting a (anti)baryon,
c                                  check if the NEW remnant is a H-Dibaryon
          enddo
        endif
        call idenco(jcf,icf,iret)

 1000 return
      end

c------------------------------------------------------------------
         subroutine gethadron(imb,idf,a,jc,jcv,ep,gproj,iret)
c------------------------------------------------------------------
c       goal:  emit a hadron (imb= 1 meson, 2 baryon, 3 antibaryon)
c              update the remnant flavor and 5-momentum
c
c       idf ,a : hadron id and 5-momentum
c       gproj  : T  projectile, F  target remnant
c       jc, ep : remnant flavor and 5-momentum
c       iret   : in case of error, keep correct momentum in remnant
c                and lose the quarks of the (not) emitted hadron
c-----------------------------------------------------------------

        include 'epos.inc'
        include 'epos.incems'
        common/cems5/plc,s
        double precision s,plc
        double precision ep(5),a(5),re(5),p1(5)
        integer jc(nflav,2),jcv(nflav,2),jcini(nflav,2),jcvini(nflav,2)
     &    ,ifh(3),ic(2)
        common /ems12/iodiba,bidiba  ! defaut iodiba=0. if iodiba=1, study H-Dibaryon
        double precision ptm,qcm,u(3),utpcmd,ptt,phi,sxini,sxini0,strmas
     &                  ,ampt2dro,ampt2str,p5sq,amasex,drangen,xmaxrm
        logical gproj

        call utpri('gethad',ish,ishini,5)

        iret=0
        do i=1,5
          a(i)=0.d0
          re(i)=ep(i)
        enddo
        ic(1)=0
        ic(2)=0
        do j=1,2
          do i=1,nflav
            jcini(i,j)=jc(i,j)
            jcvini(i,j)=jcv(i,j)
          enddo
        enddo

        if(iremn.ge.2)then
          if(ish.ge.5)then
            write(ifch,*)'remnant flavor and 5-momentum:',jc
            write(ifch,*)'                              ',jcv
            write(ifch,*)'momentum :',ep,gproj,imb
          endif
          call idquacjc(jcvini,nqv,nav)
        else
          if(ish.ge.5)
     &       write(ifch,*)'remnant flavor and 5-momentum:',jc,ep,gproj
     &                                                          ,imb
          nqv=0
          nav=0
        endif
       !write(*,'(/a,5f8.3)')'p before: ',ep

        if(gproj)then
          iclpt=iclpro
        else
          iclpt=icltar
        endif

c boost remnant in rest frame
         if(ish.ge.6) write (ifch,*) 'on-shell check'
         do k=1,5
           p1(k)=ep(k)
         enddo
         p1(5)=(p1(4)-p1(3))*(p1(4)+p1(3))-p1(2)**2-p1(1)**2
         if(p1(5).gt.0d0.and.abs(p1(5)-ep(5)*ep(5)).lt.ep(5))then
           p1(5)=sqrt(p1(5))
         else
           if(ish.ge.1)write(ifch,*)'Precision problem in gethad, p:',
     &          (p1(k),k=1,5),ep(5)*ep(5)
           p1(5)=0d0
        endif

c       initial limits
        mamos=4
        ptm=p1(5)
        sxini0=ptm*ptm
        idf=0
c redo

        nredo=0
 777    continue
        nredo=nredo+1
        if(nredo.gt.1)then       !restore initial flavors
          ic(1)=0
          ic(2)=0
          do j=1,2
            do i=1,nflav
              jc(i,j)=jcini(i,j)
              jcv(i,j)=jcvini(i,j)
            enddo
          enddo
          if(iremn.ge.2)then
            call idquacjc(jcvini,nqv,nav)
          endif
          if(ish.ge.7)write(ifch,*)'Restore flavor',idf,jc
          idf=0
          if(ptm.eq.0.or.nredo.gt.20)then
            if(ish.ge.4)write(ifch,*)
     &         'Pb with hadron momentum in Gethad !'
            iret=1
          endif
        endif

c  get the id and mass of hadron, the remnant jc is updated
        iret2=0
          if(imb.eq.1)then              ! a meson
            j=1
            if(nqv.gt.0)then
              i=idraflz(jcv,j)
              jc(i,j)=jc(i,j)-1
              nqv=nqv-1
            else
              i=idrafl(iclpt,jc,j,'v',1,iret2)
              if(iLHC.eq.1.and.iret2.ne.0)goto 77
            endif
            ifq=i
            j=2
            if(nav.gt.0)then
              i=idraflz(jcv,j)
              jc(i,j)=jc(i,j)-1
              nav=nav-1
            else
              i=idrafl(iclpt,jc,j,'v',1,iret2)
              if(iLHC.eq.1.and.iret2.ne.0)goto 77
            endif
            ifa=i
c            write(ifch,*)'ici',ifq,ifa,jc,'| ',jcv
            ic(1)=10**(6-ifq)
            ic(2)=10**(6-ifa)
            ier=0
            idf=idtra(ic,ier,idum,0)
            if(ier.ne.0)then
              if(ifq.le.ifa)then
                idf=ifq*100+ifa*10
              else
                idf=-(ifq*10+ifa*100)
              endif
            endif
            call idmass(idf,amss)

          elseif(imb.eq.2)then            ! a baryon
            j=1
            do ik=1,3
              if(nqv.gt.0)then
                i=idraflz(jcv,j)
                jc(i,j)=jc(i,j)-1
                nqv=nqv-1
              else
                i=idrafl(iclpt,jc,j,'v',1,iret2)
              if(iLHC.eq.1.and.iret2.ne.0)goto 77
              endif
              ifh(ik)=i
              ic(j)=ic(j)+10**(6-i)
            enddo
            ier=0
            idf=idtra(ic,ier,idum,0)
            if(ier.ne.0)then
              call neworder(ifh(1),ifh(2),ifh(3))
              idf=ifh(1)*1000+ifh(2)*100+ifh(3)*10
              if(ifh(1).ne.ifh(2).and.ifh(2).ne.ifh(3)
     $             .and.ifh(1).ne.ifh(3))  idf=2130
              if(ifh(1).eq.ifh(2).and.ifh(2).eq.ifh(3))idf=idf+1
            endif
            call idmass(idf,amss)

          elseif(imb.eq.3)then           ! an antibaryon
            j=2
            do ik=1,3
              if(nav.gt.0)then
                i=idraflz(jcv,j)
                jc(i,j)=jc(i,j)-1
                nav=nav-1
              else
                i=idrafl(iclpt,jc,j,'v',1,iret2)
              if(iLHC.eq.1.and.iret2.ne.0)goto 77
              endif
              ifh(ik)=i
              ic(j)=ic(j)+10**(6-i)
            enddo
            ier=0
            idf=idtra(ic,ier,idum,0)
            if(ier.ne.0)then
              call neworder(ifh(1),ifh(2),ifh(3))
              idf=ifh(1)*1000+ifh(2)*100+ifh(3)*10
              if(ifh(1).ne.ifh(2).and.ifh(2).ne.ifh(3)
     $             .and.ifh(1).ne.ifh(3))  idf=2130
              if(ifh(1).eq.ifh(2).and.ifh(2).eq.ifh(3))idf=idf+1
              idf=-idf
            endif
            call idmass(idf,amss)
           else
            call utstop('This imb does not exist in gethad !&')
           endif

   77     if(iret2.ne.0)then
          write(ifmt,*)'warning in gethadron: imb=',imb,'  iclpt:',iclpt
          write(ifmt,*)'   jc: ',jc,'  j: ',j,'   (1=q,2=aq)  --> redo'
          call utmsg('gethad')
          write(ifch,*)'Not enough quark ??? ... redo event !'
          call utmsgf
          iret=1
          goto 1000
          endif

c fix pt
          amasex=dble(amss)
          strmas=dble(utamnz(jc,mamos))

          ptt=dble(ranpt()*alpdro(2))**2         !pt+pl
          if(iret.ne.0)ptt=min(ptt,sxini0)
          if(ptt.gt.sxini0)goto 777
          sxini=sqrt(sxini0-ptt)



          a(5)=amasex
          re(5)=sxini-a(5)
          if(re(5).lt.strmas)then
            call idquacjc(jc,nq,na)
            if(nq+na.le.3)then
              idtmp=idtra(ic,1,idum,0)
              amtmp=0.
              call idmass(idtmp,amtmp)
              if(re(5).lt.amtmp)then
                if(ish.ge.6)write(ifch,*)
     &           'Pb with initial mass in Gethad, retry',idf
     &       ,amasex,re(5),strmas,sxini,ptm,ptt,amtmp,idtmp,ic,iret
                if(iret.eq.0)then
                  goto 777
                else
                  if(ish.ge.6)write(ifch,*)
     &           'Continue with minimal mass for remnant',re(5)
     &                                                   ,amtmp
                  re(5)=amtmp
                endif
              else
                strmas=amtmp
              endif
            endif
          endif

          ampt2dro=amasex**2d0
          ampt2str=strmas**2d0

c two body decay
          iret2=0
          if(iret.eq.1)then
c If energy to small, then produce a new particle adding the needed missing energy (limited energy violation to avoid stop and corrected in utrescl)
            xmaxrm=a(5)*a(5)+re(5)*re(5)
            if(ptm*ptm-xmaxrm.lt.0d0)then
             ptm=1.1d0*sqrt(2.d0*abs(a(5))*abs(re(5))+xmaxrm)
             p1(5)=ptm
             p1(4)=sqrt(p1(3)*p1(3)+p1(2)*p1(2)+p1(1)*p1(1)+p1(5)*p1(5))
            endif
          endif
          if(ish.ge.6)write(ifch,*)'2 body decay',ptm,a(5),re(5),iret
          qcm=utpcmd(ptm,a(5),re(5),iret2)
          if(iret2.ne.0)then
            if(iret.eq.0)then
             goto 777
            else
c              call utstop('Problem with qcm in gethadron !&')
              if(ish.ge.1)then
                call utmsg('gethad')
                write(ifch,*)'Problem with qcm  ... redo event !'
                call utmsgf
              endif
              iret=1
              return
            endif
          endif
          u(3)=2.d0*drangen(qcm)-1.d0
          phi=2.d0*dble(pi)*drangen(u(3))
          u(1)=sqrt(1.d0-u(3)**2)*cos(phi)
          u(2)=sqrt(1.d0-u(3)**2)*sin(phi)
          if(u(3).ge.0d0)then          !send always hadron backward
            do j=1,3
              re(j)=qcm*u(j)
              a(j)=-re(j)
            enddo
          else
            do j=1,3
              a(j)=qcm*u(j)
              re(j)=-a(j)
            enddo
          endif

          re(4)=sqrt(qcm**2+re(5)**2)
          a(4)=sqrt(qcm**2+a(5)**2)

          if(ish.ge.6)write(ifch,*)'boost : ',qcm
     &      ,' and momentum in rest frame : ',re,a


c Fix re of remnant

c boost string in collision frame
        call utlob2(-1,p1(1),p1(2),p1(3),p1(4),p1(5)
     $       ,re(1),re(2),re(3),re(4),81)

         p5sq=(re(4)+re(3))*(re(4)-re(3))-(re(1)*re(1)+re(2)*re(2))
         if(p5sq.ge.ampt2str)then
           re(5)=sqrt(p5sq)
         else
           if(ish.ge.6)then
             write(ifch,*)'Pb with remnant mass -> retry'
             write(ifch,*)'   m^2:',p5sq,'  m_min^2:',ampt2str
             write(ifch,*)'   momentum four vector:',(re(ii),ii=1,4)
           endif
           if(iret.eq.0)then
             goto 777
           else
             if(ish.ge.6)write(ifch,*)
     &            'Finish with minimal mass for remnant',re(5)
           endif
         endif

c Fix a of hadron

c boost hadron in collision frame
        call utlob2(-1,p1(1),p1(2),p1(3),p1(4),p1(5)
     $       ,a(1),a(2),a(3),a(4),82)

         p5sq=(a(4)+a(3))*(a(4)-a(3))-(a(1)**2.d0+a(2)**2.d0)
         if(abs(p5sq-ampt2dro).le.0.1)then
           a(5)=sqrt(p5sq)
         else
           if(ish.ge.6)then
             write(ifch,*)'Pb with hadron mass'
             write(ifch,*)'   m^2:',p5sq,'  m_min^2:',ampt2dro
             write(ifch,*)'   momentum four vector:',(a(ii),ii=1,4)
           endif
           a(4)=sqrt(a(5)*a(5)+a(3)*a(3)+a(2)*a(2)+a(1)*a(1))
           if(ish.ge.6)write(ifch,*)'Fix E with M and P:',(a(ii),ii=1,5)
         endif


c        if(iret.eq.1)then      !If problem with momenta do not update remnant
c
c          if(ish.ge.4)
c     *    write(ifch,*)'no hadron emission in gethad'
c
c        else     !update the 3-momentum and energy of remnant: ep

          if(ish.ge.1.and.abs(ep(4)-re(4)-a(4)).gt.1.d-2*ep(4))then
            write(ifmt,*)'Pb with energy conservation in gethad'
            if(ish.ge.6)then
              write(ifch,*)'Pb with energy conservation :'
              write(ifch,*)'   p1_ini:',ep(1),'  p1:',re(1)+a(1)
              write(ifch,*)'   p2_ini:',ep(2),'  p2:',re(2)+a(2)
              write(ifch,*)'   p3_ini:',ep(3),'  p3:',re(3)+a(3)
              write(ifch,*)'   p4_ini:',ep(4),'  p4:',re(4)+a(4)
            endif
          endif

          do i=1,5
            ep(i)=re(i)
          enddo
          if(ish.ge.5)then
            write(ifch,*)'get hadron with id and 5-momentum:',idf, a
          endif

c        endif

        !do i=1,5
        !  sm(i)=ep(i)+a(i)
        !enddo
        !write(*,'(a,5f8.3,i5)')'p after:  ',sm,iret

c      ghost condition
c         if(abs((a(4)+a(3))*(a(4)-a(3))
c     $           -a(2)**2-a(1)**2-a(5)**2).gt.0.3
c     $      .and.  abs(1.-abs(a(3))/a(4)).gt.0.01)print*,iret,dd

c$$$        if(iodiba.eq.1)then  ! for H-dibaryon study ??????????
c$$$          call idenco(jc,ic,iret)
c$$$          if(ic(1).eq.222000.and.ic(2).eq.0)ep(5)=ep(5)-bidiba
c$$$        endif

        if(ish.ge.5)then
          write(ifch,*)'new remnant flavor and 5-momentum:',jc, ep,iret
        endif
        iret=0
c          write(ifmt,*)'get hadron with id and 5-momentum:',idf, a
c          write(ifmt,*)'new remnant flavor and 5-momentum:',jc, ep

 1000 call utprix('gethad',ish,ishini,5)

      return
      end



c------------------------------------------------------------------
         subroutine getdroplet(ir,iept,ic,jc,z,ep,a,pass,xmdrmax)
c------------------------------------------------------------------
c  emit a droplet, update the remnant string flavor and 5-momentum
c
c input
c       ir ........ 1  projectile, -1  target remnant
c       iept ...... particle excitation
c       ep ........ remnant  5-momentum
c       jc ........ remnant jc
c       z  ........ Z factor from splitting
c output
c       pass ...  .true. = successful droplet emission
c                            jc, ep ....... droplet  ic and 5-momentum
c                            ic, a ........ remnant string jc and 5-momentum
c                 .false. = unsuccessful
c                            jc, ep .... unchanged,
c                            considered as droplet jc and 5-momentum
c-----------------------------------------------------------------

        include 'epos.inc'
        include 'epos.incems'
        double precision ep(5),a(5),p1(5),re(5),eps,amasex,xmdrmax
        double precision xxx,rr,alp,p5sq,xmin,xmax,ampt2str
     &  ,sxini,strmas,xxxmax,xxxmin,ampt2dro,xmdrmaxi
        parameter(eps=1.d-20)
        integer jc(nflav,2),ic(2),icx(2)
        integer jcini(nflav,2),jcfin(nflav,2)
        logical pass
        common/cems5/plc,s
        double precision s,plc,ptm,qcm,u(3),utpcmd,ptt,drangen,phi

        call utpri('getdro',ish,ishini,4)

        iret=0
        iret2=0
        xmdrmaxi=min(50.d0,xmdrmax)
        pass=.true.
        idps=0
        idms=0
        do i=1,nflav
          jcini(i,1)=jc(i,1)
          jcini(i,2)=jc(i,2)
          jcfin(i,1)=0
          jcfin(i,2)=0
        enddo


        call idquacjc(jcini,nqu,naq)

        do i=1,5
          a(i)=0.d0
          re(i)=0.d0
        enddo
        npart=nqu+naq
        nqc=jcini(4,1)+jcini(4,2)

        if(ir.eq.1)then
           iclpt=iclpro
         else
           iclpt=icltar
         endif

         if(ish.ge.5)then
           write(ifch,10)'remnant flavor and 5-momentum:'
     &                    ,jc,ep,nqu,naq,nqc,iept
 10        format(a,/,'jc:',6i3,' |',6i3,/,'ep:',5(e10.3,1x),/,4i4)
         endif

c  get id of string ends, the remnant string jc is updated
         if(iremn.eq.3)then  !  remnant content=string content (droplet empty)

           do i=1,nflav
             jcfin(i,1)=jcini(i,1)
             jcfin(i,2)=jcini(i,2)
             jcini(i,1)=0
             jcini(i,2)=0
           enddo

         else

         if(npart.lt.3.and.ep(5).lt.xmdrmax.and.nqc.eq.0)then !light droplet with few quarks
            pass=.false.
            goto 1000
         elseif(npart.lt.3)then    !few quarks but heavy, add some quarks to extract a q-qbar string (should not exit directly because of the large mass)
           ifq=idrafl(iclpt,jcini,2,'r',3,iret2)
           if(nqu.eq.1.and.naq.eq.1)then
             idps=1
             idms=1
             nqu=2
             naq=2
           else
             call utstop('This should not happen (getdrop) !&')
           endif
         elseif(nqu.eq.2.and.naq.eq.2)then
           idps=1
           idms=1
         elseif(naq.eq.0)then
           idps=5
           idms=1
         elseif(nqu.eq.0)then
           idps=1
           idms=5
         else                 !There is enough q or aq to do qq-q string


           if(jcini(4,1)-jcini(4,2).eq.0)then !if c-cbar

             idps=1
             idms=1

           else

c One chooses the first q or aq

           rrr=rangen()
           npart=nqu+naq
           if(jcini(4,1)+jcini(4,2).ne.0)then !if some charm take it out
             if(jcini(4,1).ne.0)then
               idps=1
               nqu=nqu-1
             else
               idms=1
               naq=naq-1
             endif
           elseif(rrr.gt.float(naq)/float(npart))then
             idps=1
             nqu=nqu-1
           else
             idms=1
             naq=naq-1
           endif

c One chooses the second one

           rrr=rangen()
           npart=nqu+naq
           if(idps.eq.1.and.jcini(4,1).ne.0)then !if some charm take it out
             idps=5
           elseif(idms.eq.1.and.jcini(4,2).ne.0)then !if some charm take it out
             idms=5
           elseif(rrr.gt.float(naq)/float(npart))then
             if(idps.eq.1.and.nqu.ge.2)then
               idps=5
             else
               idps=1
             endif
           else
             if(idms.eq.1.and.naq.ge.2)then
               idms=5
             else
               idms=1
             endif
           endif

c If there is already 2 q or 2 aq as string end, we know that we need
c a third one to complete the string

           if(idps.eq.5)idms=1
           if(idms.eq.5)idps=1
           if(idps.eq.1.and.idms.ne.5)idms=1
           if(idms.eq.1.and.idps.ne.5)idps=1

         endif

         endif

         if(ish.ge.5)then
           write(ifch,*)'remnant string ends :',idps,idms
         endif

          if(idps.ne.5.and.idms.ne.5)then              ! q-aq string
            if(jcini(4,1).eq.1)then
              ifq=idrafl(iclpt,jcini,1,'c',1,iret)
            else
              ifq=idrafl(iclpt,jcini,1,'v',1,iret)
            endif
            if(jcini(4,1).eq.1)then
              ifa=idrafl(iclpt,jcini,2,'c',1,iret)
            else
              ifa=idrafl(iclpt,jcini,2,'v',1,iret)
            endif
            jcfin(ifq,1)=1
            jcfin(ifa,2)=1

          elseif(idps.eq.5)then                       ! qq-q string
            do ik=1,3
              if(jcini(4,1).ne.0)then
                i=idrafl(iclpt,jcini,1,'c',1,iret)
              else
                i=idrafl(iclpt,jcini,1,'v',1,iret)
              endif
              jcfin(i,1)=jcfin(i,1)+1
            enddo

          elseif(idms.eq.5)then                        !aqaq-aq string
            do ik=1,3
              if(jcini(4,2).ne.0)then
                i=idrafl(iclpt,jcini,2,'c',1,iret)
              else
                i=idrafl(iclpt,jcini,2,'v',1,iret)
              endif
              jcfin(i,2)=jcfin(i,2)+1
            enddo
          endif

          endif      !iremn=3

          if(iret.ne.0)call utstop('Not enough quark in getdro ???&')
          if(jcini(4,1)+jcini(4,2).ne.0)
     &         call utstop('There is sitll charm quark in getdro???&')

c string id

         call idenco(jcfin,icx,iret)
         if(iret.eq.1)then
           call utstop('Exotic flavor in getdroplet !&')
         endif


c boost remnant in rest frame
      if(ish.ge.6) write (ifch,*) 'on-shell check'
        do k=1,5
          p1(k)=ep(k)
        enddo
        p1(5)=(p1(4)-p1(3))*(p1(4)+p1(3))-p1(2)**2-p1(1)**2
        if(p1(5).gt.0d0.and.abs(p1(5)-ep(5)*ep(5)).lt.ep(5))then
          p1(5)=sqrt(p1(5))
        else
          if(ish.ge.2)write(ifch,*)'Precision problem in getdro, p:',
     &             (p1(k),k=1,5),ep(5)*ep(5)
          p1(5)=ep(5)
          p1(4)=sqrt(p1(3)*p1(3)+p1(2)*p1(2)+p1(1)*p1(1)+p1(5)*p1(5))
        endif
      if(ish.ge.6) write (ifch,*) 'boost vector:',p1

c limits for momenta

      mamod=4
      mamos=4
      fad=alpdro(1)
      if(iremn.eq.3)fad=fad*(1.+z*zdrinc)
      fad=max(1.5,fad)
      ptm=p1(5)
      amasex=dble(fad*utamnz(jcini,mamod))
      fas=2.
      if(iremn.eq.3)then
        id=idtra(icx,ier,ires,0)
        if(ier.eq.0)then
          call idmass(id,amass)           !minimum is particle mass
          strmas=dble(amass)
        else
          strmas=dble(fas*utamnz(jcfin,mamos))
        endif
      else
        strmas=dble(fas*utamnz(jcfin,mamos))
      endif


c redo

       nredo=0
 777   continue
       nredo=nredo+1
       if(nredo.eq.10)then
          amasex=1.5d0*dble(utamnz(jcini,mamod))
          if(iremn.ne.3)strmas=1.5d0*dble(utamnz(jcfin,mamos))
       elseif(nredo.gt.20)then
          !write(ifch,*)'nredo.gt.20 -> only drop'
         if(ish.ge.4)write(ifch,*)
     &     'Pb with string mass in Getdrop, continue with gethad'
          pass=.false.
         goto 1000
       endif

c fix pt

          sxini=ptm*ptm
          ptt=dble(ranpt()*alpdro(2))**2         !pt
          if(ptt.ge.sxini)goto 777
          sxini=sqrt(sxini-ptt)


          ampt2dro=amasex**2d0
          ampt2str=strmas**2d0
          if(ampt2dro.gt.xmdrmaxi)then
            xmdrmaxi=2d0*ampt2dro
c            write(ifmt,*)'Warning Mmin>Mmax in Getdroplet'
          endif

          xxxmax=min(xmdrmaxi,(sxini-strmas)**2)    !strmas/(strmas+ampt2)
          xxxmin=ampt2dro

          if(xxxmin.gt.xxxmax)then
            !write(ifch,*)'Warning Mmin>sxini -> only drop'
           if(ish.ge.4)write(ifch,*)
     &     'Pb with ampt2 in Getdrop, retry',nredo,ir
     &             ,ampt2dro,ampt2str,xxxmin,xxxmax,sxini,ptt,xmdrmaxi
            goto 777
          endif



c fix mass

            rr=drangen(xxxmax)
            xmax=xxxmax
            xmin=xxxmin
            alp=dble(alpdro(3))
            if(dabs(alp-1.d0).lt.eps)then
              xxx=xmax**rr*xmin**(1d0-rr)
            else
              xxx=(rr*xmax**(1d0-alp)+(1d0-rr)*xmin**(1d0-alp))
     &                                                **(1d0/(1d0-alp))
            endif


c        write(ifch,*)'ini',xmin,xxx,xmax,rr,ampt2dro
c    &                   ,(sxini-sqrt(xxx)),ampt2str,p1(5)



          re(5)=sqrt(xxx)
          a(5)=sxini-re(5)
          if(a(5).lt.strmas)then
            if(ish.ge.6)write(ifch,*)
     &           'Pb with initial mass in Getdrop, retry',ir
     &       ,xmin,xxx,xmax,rr,ampt2dro,ampt2str,a(5)
            goto 777
          endif


c two body decay
          if(ish.ge.6)write(ifch,*)'2 body decay',ptm,re(5),a(5)
          qcm=utpcmd(ptm,re(5),a(5),iret)
          u(3)=0.d0 !2.d0*drangen(qcm)-1.d0
          phi=2.d0*dble(pi)*drangen(u(3))
          u(1)=sqrt(1.d0-u(3)**2)*cos(phi)
          u(2)=sqrt(1.d0-u(3)**2)*sin(phi)
          if(u(3).lt.0d0)then          !send always droplet backward
c          if(u(3).gt.0d0)then          !send always droplet forward     ?????
            do j=1,3
              re(j)=qcm*u(j)
              a(j)=-re(j)
            enddo
          else
            do j=1,3
              a(j)=qcm*u(j)
              re(j)=-a(j)
            enddo
          endif

          re(4)=sqrt(qcm**2+re(5)**2)
          a(4)=sqrt(qcm**2+a(5)**2)

          if(ish.ge.6)write(ifch,*)'momentum in rest frame : ',re,a



c Fix a of string

c boost string in collision frame
        call utlob2(-1,p1(1),p1(2),p1(3),p1(4),p1(5)
     $       ,a(1),a(2),a(3),a(4),71)

         p5sq=(a(4)+a(3))*(a(4)-a(3))-(a(1)**2.d0+a(2)**2.d0)
         if(p5sq.gt.ampt2str)then
           a(5)=sqrt(p5sq)
         else
           if(ish.ge.6)then
             write(ifch,*)'Pb with string mass -> retry'
             write(ifch,*)'   m^2:',p5sq,'  m_min^2:',ampt2str
             write(ifch,*)'   momentum four vector:',(a(ii),ii=1,4)
           endif
           goto 777
         endif

c Fix ep of droplet

c boost droplet in collision frame
        call utlob2(-1,p1(1),p1(2),p1(3),p1(4),p1(5)
     $       ,re(1),re(2),re(3),re(4),72)

         p5sq=(re(4)+re(3))*(re(4)-re(3))-(re(1)*re(1)+re(2)*re(2))
         if(p5sq.gt.ampt2dro)then
           re(5)=sqrt(p5sq)
         else
           if(ish.ge.6)then
             write(ifch,*)'Pb with droplet mass -> retry'
             write(ifch,*)'   m^2:',p5sq,'  m_min^2:',ampt2dro
             write(ifch,*)'   momentum four vector:',(re(ii),ii=1,4)
           endif
           goto 777
         endif


       if(ish.ge.1.and.abs(ep(4)-re(4)-a(4)).gt.1.d-2*ep(4))then
         write(ifmt,*)'Pb with energy conservation in getdro'
         if(ish.ge.6)then
           write(ifch,*)'Pb with energy conservation :'
           write(ifch,*)'   p1_ini:',ep(1),'  p1:',re(1)+a(1)
           write(ifch,*)'   p2_ini:',ep(2),'  p2:',re(2)+a(2)
           write(ifch,*)'   p3_ini:',ep(3),'  p3:',re(3)+a(3)
         endif
       endif

c If OK, save flavors of droplet and string
         do i=1,5
           ep(i)=re(i)
         enddo
         ic(1)=icx(1)
         ic(2)=icx(2)
         do i=1,nflav
           jc(i,1)=jcini(i,1)
           jc(i,2)=jcini(i,2)
         enddo

         if(ish.ge.6)then
           write(ifch,20)'droplet:',jc,ep
           write(ifch,30)'string remnant:',ic,a
         endif
 20      format(a,/,'jc:',6i3,' |',6i3,/,'ep:',5(e10.3,1x))
 30      format(a,/,'ic:',i7,' |',i7,/,'a:',5(e10.3,1x))

 1000    continue
         call utprix('getdro',ish,ishini,4)
         end

c------------------------------------------------------------------
         subroutine getdropx(ir,iept,m,ic,jc,jcv,z,ep,a,pass,xmdrmax)
c------------------------------------------------------------------
c  emit a droplet taken into account momentum fraction without screening,
c  update the remnant string flavor and 5-momentum (to be used with iremn=2)
c
c input
c       ir ........ 1  projectile, -1  target remnant
c       iept ...... particle excitation
c       m  ........ remnant index
c       ep ........ remnant  5-momentum
c       jc ........ remnant jc
c       jcv ....... remnant jc valence quark
c       z  ........ Z factor from splitting
c output
c       pass ...  .true. = successful droplet emission
c                            jc, ep ....... droplet  ic and 5-momentum
c                            ic, a ........ remnant string jc and 5-momentum
c                 .false. = unsuccessful
c                            jc, ep .... unchanged,
c                            considered as droplet jc and 5-momentum
c-----------------------------------------------------------------

        include 'epos.inc'
        include 'epos.incems'
        double precision ep(5),a(5),p1(5),re(5),eps,amasex,xmdrmax
        double precision xxx,rr,alpm,p5sq,xmin,xmax,ampt2str,xmsmax
     &  ,sxini,strmas,xxxmax,xxxmin,ampt2dro,xmdrmaxi,xprmi,xmrmi
     &  ,xprmd,xmrmd,xprms,xmrms,xpti,ypti,xptd,yptd,xpts,ypts,xptt,yptt
        double precision om1xpr,xremd,xrems,freduc
     &       ,atil(ntymi:ntymx),btilp(ntymi:ntymx),btilpp(ntymi:ntymx)
        parameter(eps=1.d-20)
        integer jc(nflav,2),jcv(nflav,2),ic(2),icx(2)
        integer jcvini(nflav,2),jcini(nflav,2)
     &         ,jcfin(nflav,2),jcvfin(nflav,2)
        logical pass
        common/cems5/plc,s
        double precision s,plc,ptm,qcm,u(3),utpcmd,ptt,drangen,phi
        logical strcomp,valqu

        call utpri('getdrx',ish,ishini,4)

        iret=0
        xmdrmaxi=min(50.d0,xmdrmax)
        pass=.true.
        idps=0
        idms=0
        do i=1,nflav
          jcini(i,1)=jc(i,1)
          jcini(i,2)=jc(i,2)
          jcvini(i,1)=jcv(i,1)
          jcvini(i,2)=jcv(i,2)
          jcfin(i,1)=0
          jcfin(i,2)=0
          jcvfin(i,1)=0
          jcvfin(i,2)=0
        enddo


        call idquacjc(jcini,nqu,naq)
        call idquacjc(jcvini,nqv,nav)

        do i=1,5
          a(i)=0.d0
          re(i)=0.d0
        enddo
        nqc=jcini(4,1)+jcini(4,2)

        idrf=0
        if(nqu-naq.ne.0)idrf=1
        if(ir.eq.1)then
           iclpt=iclpro
           if(idrf.eq.0)idrf=idp(m)  !change it only if not 1
           xprmi=xpp(m)
           xmrmi=xmp(m)
           xpti=xxp(m)
           ypti=xyp(m)
           if(lproj3(m).gt.0)then
             nlnk=max(1,nint(z*float(lproj3(m))))
           else
             nlnk=0
           endif
        else
           iclpt=icltar
           if(idrf.eq.0)idrf=idt(m)  !change it only if not 1
           xprmi=xmt(m)
           xmrmi=xpt(m)
           xpti=xxt(m)
           ypti=xyt(m)
           if(ltarg3(m).gt.0)then
             nlnk=max(1,nint(z*float(ltarg3(m))))
           else
             nlnk=0
           endif
         endif

         if(ish.ge.5)then
           write(ifch,10)'remnant flavor and 5-momentum:'
     &                    ,jc,jcv,ep,nqu,naq,nqv,nav,nqc,idrf,iept,nlnk
 10        format(a,/,'jc:',6i3,' |',6i3,/,'jcv:',6i3,' |',6i3,/
     &            ,'ep:',5(e10.3,1x),/,8i4)
         endif

c check formation conditions

         strcomp=.false.
         valqu=.false.    !if true, valence quark will always be in strings : reduce lambda production
         if((nqu.eq.3.and.naq.eq.0).or.(nqu.eq.0.and.naq.eq.3)
     &          .or.(nqu.eq.1.and.naq.eq.1).and.nlnk.ne.0)then 
c not enough quark for the droplet, check mass
           if(iept.ne.5.and.ep(5)*ep(5).lt.xmdrmax.and.nqc.eq.0)then
             pass=.false.       !continue without droplet
             if(ish.ge.4)write(ifch,*)
     &      'Normal remnant in Getdropx, continue only with droplet ...'
             goto 1000
           endif
c create q-aq from sea (but no charm)
           do n=1,nlnk
             idum=idrafl(iclpt,jcini,1,'r',3,iret)
             nqu=nqu+1
             naq=naq+1
           enddo
           strcomp=.true.
           valqu=.false.
         elseif(mod(nqu-naq,3).ne.0)then
           call utstop('This should not happen (getdropx) !&')
         endif

c  get id of string ends, the remnant string jc is updated

c First remove all charm

         if(nqc.ne.0.and.jcini(4,1)-jcini(4,2).eq.0)then !if c-cbar

           if(jcini(4,1).eq.1)then
             idps=1
             idms=1
           else
             call utstop('getdropx can not manage more than c-cb !&')
           endif

         elseif(nqc.ne.0.and.jcini(4,1)*jcini(4,2).ne.0)then

           call utstop('getdropx can not manage c quarks this way !&')

         else


           if(nqc.ne.0)then !if some charm take it out
             if(jcini(4,1).ne.0)then
               if(nqu.lt.3)then
                 idrf=0         !can not use c in antibaryon
               elseif(jcini(4,1).gt.1)then
                 idrf=1         !more than 1 c quark only in baryon
               endif
             elseif(jcini(4,2).ne.0)then
               if(naq.lt.3)then
                 idrf=0         !can not use cb in baryon
               elseif(jcini(4,2).gt.1)then
                 idrf=1         !more than 1 c antiquark only in antibaryon
               endif
             endif
             if(idrf.ne.0.and.jcini(4,1).gt.0.and.jcini(4,1).le.3)then
               idps=5
               idms=1
         elseif(idrf.ne.0.and.jcini(4,2).gt.0.and.jcini(4,2).le.3)then
               idps=1
               idms=5
             elseif(jcini(4,1).gt.1.or.jcini(4,2).gt.1)then
               call utstop('getdropx can not use more than 3 c/cb !&')
             endif
           endif

c take into account number of diquark in final remnant string

           if(idps.eq.0)then

             if(idrf.ne.0)then  !use a diquark
               if(nqu.gt.naq)then        !qq-q
                 idps=5
                 idms=1
               elseif(nqu.lt.naq)then    !qbqb-qb
                 idps=1
                 idms=5
               endif
             else               !q-qb
               idps=1
               idms=1
             endif

           endif

         endif                  !string end type

         if(ish.ge.5)then
           write(ifch,*)'remnant string ends :',idps,idms
         endif

c choose flavor with priority to valence quark (after charm)

          if(idps.ne.5.and.idms.ne.5)then              ! q-aq string
            j=1
            if(jcini(4,j).gt.0)then
              i=4
              jcini(i,j)=jcini(i,j)-1
              if(jcvini(i,j).gt.0)then
                jcvfin(i,j)=jcvfin(i,j)+1
                jcvini(i,j)=jcvini(i,j)-1
                nqv=nqv-1
              endif
            elseif(valqu.and.nqv.gt.0)then
              i=idraflz(jcvini,j)
              jcvfin(i,j)=jcvfin(i,j)+1
              jcini(i,j)=jcini(i,j)-1
              nqv=nqv-1
            else
              i=idrafl(iclpt,jcini,j,'v',1,iret)
              if(jcini(i,j)-jcvini(i,j).lt.0)then
                jcvini(i,j)=jcvini(i,j)-1
                jcvfin(i,j)=jcvfin(i,j)+1
              endif
            endif
            ifq=i
            j=2
            if(jcini(4,j).gt.0)then
              i=4
              jcini(i,j)=jcini(i,j)-1
              if(jcvini(i,j).gt.0)then
                jcvfin(i,j)=jcvfin(i,j)+1
                jcvini(i,j)=jcvini(i,j)-1
                nav=nav-1
              endif
            elseif(valqu.and.nav.gt.0)then
              i=idraflz(jcvini,j)
              jcvfin(i,j)=jcvfin(i,j)+1
              jcini(i,j)=jcini(i,j)-1
              nav=nav-1
            else
              i=idrafl(iclpt,jcini,j,'v',1,iret)
              if(jcini(i,j)-jcvini(i,j).lt.0)then
                jcvini(i,j)=jcvini(i,j)-1
                jcvfin(i,j)=jcvfin(i,j)+1
              endif
            endif
            ifa=i
            jcfin(ifq,1)=1
            jcfin(ifa,2)=1

          elseif(idps.eq.5)then                       ! qq-q string
            j=1
            do ik=1,3
              if(jcini(4,j).ne.0)then
                i=4
                jcini(i,j)=jcini(i,j)-1
                if(jcvini(i,j).gt.0)then
                  jcvfin(i,j)=jcvfin(i,j)+1
                  jcvini(i,j)=jcvini(i,j)-1
                  nqv=nqv-1
                endif
              elseif(valqu.and.nqv.gt.0)then
                i=idraflz(jcvini,j)
                jcvfin(i,j)=jcvfin(i,j)+1
                jcini(i,j)=jcini(i,j)-1
                nqv=nqv-1
              else
                i=idrafl(iclpt,jcini,j,'v',1,iret)
                if(jcini(i,j)-jcvini(i,j).lt.0)then
                  jcvini(i,j)=jcvini(i,j)-1
                  jcvfin(i,j)=jcvfin(i,j)+1
                endif
              endif
              jcfin(i,j)=jcfin(i,j)+1
            enddo

          elseif(idms.eq.5)then                        !aqaq-aq string
            j=2
            do ik=1,3
              if(jcini(4,j).gt.0)then
                i=4
                jcini(i,j)=jcini(i,j)-1
                if(jcvini(i,j).gt.0)then
                  jcvfin(i,j)=jcvfin(i,j)+1
                  jcvini(i,j)=jcvini(i,j)-1
                  nav=nav-1
                endif
              elseif(valqu.and.nav.gt.0)then
                i=idraflz(jcvini,j)
                jcvfin(i,j)=jcvfin(i,j)+1
                jcini(i,j)=jcini(i,j)-1
                nav=nav-1
              else
                i=idrafl(iclpt,jcini,j,'v',1,iret)
                if(jcini(i,j)-jcvini(i,j).lt.0)then
                  jcvini(i,j)=jcvini(i,j)-1
                  jcvfin(i,j)=jcvfin(i,j)+1
                endif
              endif
              jcfin(i,j)=jcfin(i,j)+1
            enddo

          endif

          if(iret.ne.0)call utstop('Not enough quark in getdropx ???&')
          if(jcini(4,1)+jcini(4,2).ne.0)
     &         call utstop('There is sitll charm quark in getdropx???&')

c string id

         call idenco(jcfin,icx,iret)
         if(iret.eq.1)then
           call utstop('Exotic flavor in getdropx !&')
         endif


c boost remnant in rest frame
      if(ish.ge.6) write (ifch,*) 'on-shell check'
        do k=1,5
          p1(k)=ep(k)
        enddo
        p1(5)=(p1(4)-p1(3))*(p1(4)+p1(3))-p1(2)**2-p1(1)**2
        if(p1(5).gt.0d0.and.abs(p1(5)-ep(5)*ep(5)).lt.ep(5))then
          p1(5)=sqrt(p1(5))
        else
          if(ish.ge.2)write(ifch,*)'Precision problem in getdropx, p:',
     &             (p1(k),k=1,5),ep(5)*ep(5)
          p1(5)=ep(5)
          p1(4)=sqrt(p1(3)*p1(3)+p1(2)*p1(2)+p1(1)*p1(1)+p1(5)*p1(5))
        endif

c limits for momenta

      mamod=4
      mamos=4
      fad=alpdro(1)
      fad=max(1.5,fad*(1.+z*zdrinc))
      ptm=p1(5)
      amasex=dble(fad*utamnz(jcini,mamod))
      fas=2.
      strmas=dble(fas*utamnz(jcfin,mamos))
      ampt2dro=amasex**2d0
      ampt2str=strmas**2d0
      if(ampt2dro.gt.xmdrmaxi)then
        xmdrmaxi=2d0*ampt2dro
c       write(ifmt,*)'Warning Mmin>Mmax in Getdropx'
      endif


c redo

       xxxmin=1d0-xprmi
       if(xxxmin.gt.ampt2dro/(s*xmrmi))then
         xmrmd=ampt2dro/(s*xxxmin)
       else
         nlnk=0
       endif
       nredo=-1
       freduc=1d0
 777   continue
       nredo=nredo+1
       if(strcomp.and.nredo.eq.20)then  !after 19 try and remnant compatible with a string
         pass=.false.         !continue without droplet
         if(ish.ge.4)write(ifch,'(a,2i3,4e12.5)')
     &     'Pb with splitting in Getdropx, continue without split ...'
     &     ,nlnk,nvirt,xxxmax,xxxmin,ep(5)**2,xmdrmax
         goto 1000
       elseif(nredo.eq.10.or.nredo.eq.26)then        !reduce minimum mass
          amasex=1.5d0*dble(utamnz(jcini,mamod))
          strmas=1.5d0*dble(utamnz(jcfin,mamos))
          ampt2dro=amasex**2d0
          ampt2str=strmas**2d0
          xmrmd=ampt2dro/(s*xxxmin)
       elseif(nredo.eq.20)then    !after 19 try, use 2 body decay
         xmrmd=1d0+xmrmi
         if(ish.ge.4)write(ifch,*)
     &     'nredo>20, use 2 body decay ...',nvirt,xxxmax,xxxmin
         amasex=dble(fad*utamnz(jcini,mamod))
         strmas=dble(fas*utamnz(jcfin,mamos))
         ampt2dro=amasex**2d0
         ampt2str=strmas**2d0
         if(ish.ge.6) write (ifch,*) 'boost vector:',p1
       elseif(nredo.ge.30)then
          !write(ifch,*)'nredo.gt.20 -> only drop'
         if(ish.ge.4)write(ifch,*)
     &    'Pb with string mass in Getdropx, continue without split ...'
          pass=.false.
         goto 1000
       endif

       if(xmrmd.lt.xmrmi.and.nlnk.gt.0)then        !kinetic compatibility

         xmrms=xmrmi-xmrmd

c fix the virtual number of collision (no screening)
         iscreensave=iscreen
         iscreen=0
         imin=ntymin
         imax=ntymx
         if(iomega.eq.2)imax=1
         spp=sngl(s)
         nvirt=0
         xxxmax=0d0
         xpts=0d0
         ypts=0d0
         if(ir.eq.1)then
           do l=1,lproj3(m)    !use all pairs attached to remnant
             kp=kproj3(m,l)
             nvirt=nvirt+1
             do i=imin,imax
               call Gfunpar(0.,0.,1,i,bk(kp),spp,alp,bet,betp,epsp,epst
     &                                                       ,epss,gamv)
               atil(i)=dble(alp)
               btilp(i)=dble(bet)
               btilpp(i)=dble(betp)
             enddo
             xprms=1d0-xxxmax
c take x from an "unscreened" Pomeron (reduction factor if too high)
             xxx=om1xpr(atil,btilp,btilpp,xprms,xmrmi,ir)*freduc
             ptt=dble(ranptcut(1.)*alpdro(2))
c             ptt=dble(ranptcut(ptsems)*alpdro(2))
             phi=2d0*dble(pi)*drangen(ptt)
             xprms=1d0-xxxmax-xxx
             xptt=xpts+ptt*cos(phi)
             yptt=ypts+ptt*sin(phi)
             xrems=xprms*xmrms*s-(xpti+xptt)**2-(ypti+yptt)**2
             if(xrems.gt.ampt2str)then
               xxxmax=xxxmax+xxx
               xpts=xptt
               ypts=yptt
             endif
           enddo
         else
           do l=1,ltarg3(m)    !use all pairs attached to remnant
             kt=ktarg3(m,l)
             nvirt=nvirt+1
             do i=imin,imax
               call Gfunpar(0.,0.,1,i,bk(kt),spp,alp,bet,betp,epsp,epst
     &                                                       ,epss,gamv)
               atil(i)=dble(alp)
               btilp(i)=dble(bet)
               btilpp(i)=dble(betp)
             enddo
             xprms=1d0-xxxmax
c take x from an "unscreened" Pomeron (reduction factor if too high)
            xxx=om1xpr(atil,btilp,btilpp,xprms,xmrmi,ir)*freduc
             ptt=dble(ranptcut(1.)*alpdro(2))
c             ptt=dble(ranptcut(ptsems)*alpdro(2))
             phi=2d0*dble(pi)*drangen(ptt)
             xprms=1d0-xxxmax-xxx
             xptt=xpts+ptt*cos(phi)
             yptt=ypts+ptt*sin(phi)
             xrems=xprms*xmrms*s-(xpti+xptt)**2-(ypti+yptt)**2
             if(xrems.gt.ampt2str)then
               xxxmax=xxxmax+xxx
               xpts=xptt
               ypts=yptt
             endif
           enddo
         endif
         iscreen=iscreensave

         if(xxxmax.le.xxxmin)goto 777


c check string mass and energy

         xprms=1d0-xxxmax
         xpts=xpti+xpts
         ypts=ypti+ypts
         xrems=xprms*xmrms*s-xpts*xpts-ypts*ypts
         if(xrems.lt.ampt2str)then
           if(ish.ge.4)write(ifch,*)
     &          'Pb with string mass in Getdropx, retry',nredo,ir
     &          ,ampt2str,xrems,xprms,xmrms,xpts,ypts
           goto 777
         endif

c check droplet mass and energy

c Droplet mass should not exceed to much mdrmaxi. Use random to smooth distrib.
         xmsmax=xmdrmaxi*(1.+drangen(xmdrmaxi))
         xprmd=xprmi-xprms
         xptd=xpti-xpts
         yptd=ypti-ypts
         xremd=xprmd*xmrmd*s-xptd*xptd-yptd*yptd
         if(xremd.lt.ampt2dro)then
c Droplet should not have a mass too low.
           if(ish.ge.4)write(ifch,*)
     &          'Pb with drop mass (low) in Getdropx, retry',nredo,ir
     &          ,ampt2dro,xremd,xprmd,xmrmd,xptd,yptd
           goto 777
         elseif(xremd.ge.xmsmax)then
c Droplet should not have a mass too high.
           if(ish.ge.4)write(ifch,*)
     &          'Pb with drop mass (high) in Getdropx, retry',nredo,ir
     &          ,xremd,xmsmax,xprmd,xmrmd,xptd,yptd
           freduc=freduc*0.5d0
           goto 777
         endif


         re(1)=xptd
         re(2)=yptd
         if(ir.eq.1)then
           re(3)=(xprmd-xmrmd)*plc*0.5d0
         else
           re(3)=(xmrmd-xprmd)*plc*0.5d0
         endif
         re(4)=(xprmd+xmrmd)*plc*0.5d0
         re(5)=sqrt(xremd)

         a(1)=xpts
         a(2)=ypts
         if(ir.eq.1)then
           a(3)=(xprms-xmrms)*plc*0.5d0
         else
           a(3)=(xmrms-xprms)*plc*0.5d0
         endif
         a(4)=(xprms+xmrms)*plc*0.5d0
         a(5)=sqrt(xrems)



       else   !if xm to small, use two body decay (should be rare)

         if(ish.ge.6)write (ifch,*)'kinematic limit -> boost vector:',p1

c fix pt

          sxini=ptm*ptm
          ptt=dble(ranpt()*alpdro(2))**2         !pt
          if(ptt.ge.sxini)goto 777
          sxini=sqrt(sxini-ptt)



          xmsmax=xmdrmaxi*(1.+drangen(xmdrmaxi))
          xxxmax=min(xmsmax,(sxini-strmas)**2)    !strmas/(strmas+ampt2)
          xxxmin=ampt2dro

          if(xxxmin.gt.xxxmax)then
            !write(ifch,*)'Warning Mmin>sxini -> only drop'
           if(ish.ge.4)write(ifch,*)
     &     'Pb with ampt2 in Getdropx, retry',nredo,ir
     &             ,ampt2dro,ampt2str,xxxmin,xxxmax,sxini,ptt,xmsmax
            goto 777
          endif



c fix mass

            rr=drangen(xxxmax)
            xmax=xxxmax
            xmin=xxxmin
            alpm=dble(alpdro(3))
            if(dabs(alpm-1.d0).lt.eps)then
              xxx=xmax**rr*xmin**(1d0-rr)
            else
              xxx=(rr*xmax**(1d0-alpm)+(1d0-rr)*xmin**(1d0-alpm))
     &                                                **(1d0/(1d0-alpm))
            endif


c        write(ifch,*)'ini',xmin,xxx,xmax,rr,ampt2dro
c     &                   ,(sxini-sqrt(xxx)),ampt2str,p1(5)



          re(5)=sqrt(xxx)
          a(5)=sxini-re(5)
          if(a(5).lt.strmas)then
            if(ish.ge.6)write(ifch,*)
     &           'Pb with initial mass in Getdropx, retry',ir
     &       ,xmin,xxx,xmax,rr,ampt2dro,ampt2str,a(5)
            goto 777
          endif


c two body decay
          if(ish.ge.6)write(ifch,*)'2 body decay',ptm,re(5),a(5)
          qcm=utpcmd(ptm,re(5),a(5),iret)
          u(3)=2.d0*drangen(qcm)-1.d0
          phi=2.d0*dble(pi)*drangen(u(3))
          u(1)=sqrt(1.d0-u(3)**2)*cos(phi)
          u(2)=sqrt(1.d0-u(3)**2)*sin(phi)
          do j=1,3
            re(j)=qcm*u(j)
            a(j)=-re(j)
          enddo

          re(4)=sqrt(qcm**2+re(5)**2)
          a(4)=sqrt(qcm**2+a(5)**2)

          if(ish.ge.6)write(ifch,*)'momentum in rest frame : ',re,a



c Fix a of string

c boost string in collision frame
        call utlob2(-1,p1(1),p1(2),p1(3),p1(4),p1(5)
     $       ,a(1),a(2),a(3),a(4),73)

         p5sq=(a(4)+a(3))*(a(4)-a(3))-(a(1)**2.d0+a(2)**2.d0)
         if(p5sq.gt.ampt2str)then
           a(5)=sqrt(p5sq)
         else
           if(ish.ge.6)then
             write(ifch,*)'Pb with string mass -> retry'
             write(ifch,*)'   m^2:',p5sq,'  m_min^2:',ampt2str
             write(ifch,*)'   momentum four vector:',(a(ii),ii=1,4)
           endif
           goto 777
         endif

c Fix ep of droplet

c boost droplet in collision frame
        call utlob2(-1,p1(1),p1(2),p1(3),p1(4),p1(5)
     $       ,re(1),re(2),re(3),re(4),74)

         p5sq=(re(4)+re(3))*(re(4)-re(3))-(re(1)*re(1)+re(2)*re(2))
         if(p5sq.gt.ampt2dro)then
           re(5)=sqrt(p5sq)
         else
           if(ish.ge.6)then
             write(ifch,*)'Pb with droplet mass -> retry'
             write(ifch,*)'   m^2:',p5sq,'  m_min^2:',ampt2dro
             write(ifch,*)'   momentum four vector:',(re(ii),ii=1,4)
           endif
           goto 777
         endif

       endif     !test of xm


       if(ish.ge.1.and.abs(ep(4)-re(4)-a(4)).gt.1.e-2*ep(4))then
         write(ifmt,*)'Pb with energy conservation in getdropx'
         if(ish.ge.6)then
           write(ifch,*)'Pb with energy conservation :'
           write(ifch,*)'   p1_ini:',ep(1),'  p1:',re(1)+a(1)
           write(ifch,*)'   p2_ini:',ep(2),'  p2:',re(2)+a(2)
           write(ifch,*)'   p3_ini:',ep(3),'  p3:',re(3)+a(3)
         endif
       endif

c If OK, save flavors of droplet and string
         do i=1,5
           ep(i)=re(i)
         enddo
         ic(1)=icx(1)
         ic(2)=icx(2)
         do i=1,nflav
           jc(i,1)=jcini(i,1)
           jc(i,2)=jcini(i,2)
           jcv(i,1)=jcvfin(i,1)
           jcv(i,2)=jcvfin(i,2)
         enddo

         if(ish.ge.6)then
           write(ifch,20)'droplet:',jc,ep
           write(ifch,30)'string remnant:',ic,a
           write(ifch,'(a)')'valence:'
           write(ifch,'(6i3)')jcv
         endif
 20      format(a,/,'jc:',6i3,' |',6i3,/,'ep:',5(e10.3,1x))
 30      format(a,/,'ic:',i7,' |',i7,/,'a:',5(e10.3,1x))

 1000    continue
         call utprix('getdrx',ish,ishini,4)
         end

c-----------------------------------------------------
       subroutine neworder(n1, n2, n3)
c-----------------------------------------------------
c make 3 integers ordered like 1 2 3
c------------------------------------------------------
            if(n2.lt.n1)then
              ifb=n2
              n2=n1
              n1=ifb
            endif
            if(n3.lt.n1)then
              ifb=n3
              n3=n2
              n2=n1
              n1=ifb
            elseif(n3.lt.n2)then
              ifb=n3
              n3=n2
              n2=ifb
            endif
         end

c-----------------------------------------------------
       subroutine neworderx(x1,x2,x3,i1,i2,i3)
c-----------------------------------------------------
c make 3 reals ordered like 1 2 3
c------------------------------------------------------
            if(x2.lt.x1)then
              xfb=x2
              x2=x1
              x1=xfb
              ifb=i2
              i2=i1
              i1=ifb
            endif
            if(x3.lt.x1)then
              xfb=x3
              x3=x2
              x2=x1
              x1=xfb
              ifb=i3
              i3=i2
              i2=i1
              i1=ifb
            elseif(x3.lt.x2)then
              xfb=x3
              x3=x2
              x2=xfb
              ifb=i3
              i3=i2
              i2=ifb
            endif
         end

c-----------------------------------------------------------------------
      function idtr2(ic)
c-----------------------------------------------------------------------
c transforms ic to id such that only hadrons have nonzero id
c-----------------------------------------------------------------------
      parameter (nidt=30)
      integer idt(3,nidt),ic(2)
      data idt/
     * 100000,100000, 110   ,100000,010000, 120   ,010000,010000, 220
     *,100000,001000, 130   ,010000,001000, 230   ,001000,001000, 330
     *,100000,000100, 140   ,010000,000100, 240   ,001000,000100, 340
     *,000100,000100, 440
     *,300000,000000,1111   ,210000,000000,1120   ,120000,000000,1220
     *,030000,000000,2221   ,201000,000000,1130   ,111000,000000,1230
     *,021000,000000,2230   ,102000,000000,1330   ,012000,000000,2330
     *,003000,000000,3331   ,200100,000000,1140   ,110100,000000,1240
     *,020100,000000,2240   ,101100,000000,1340   ,011100,000000,2340
     *,002100,000000,3340   ,100200,000000,1440   ,010200,000000,2440
     *,001200,000000,3440   ,000300,000000,4441/

      idtr2=0
      if(ic(1).eq.0.and.ic(2).eq.0)then
       if(rangen().ge.0.5)then
        idtr2=110
        ic(1)=100000
        ic(2)=100000
       else
        idtr2=220
        ic(1)=10000
        ic(2)=10000
       endif
       return
      endif
      do 1 i=1,nidt
       if(ic(2).eq.idt(1,i).and.ic(1).eq.idt(2,i))idtr2=-idt(3,i)
       if(ic(1).eq.idt(1,i).and.ic(2).eq.idt(2,i))idtr2=idt(3,i)
1     continue
      return
      end

c----------------------------------------------------------------------
      subroutine emsini(e,idpji,idtgi)
c----------------------------------------------------------------------
c  energy-momentum sharing initializations
c----------------------------------------------------------------------
      include 'epos.inc'
      include 'epos.incems'
      include 'epos.incsem'
      common/cemsr5/at(0:1,0:5)
      common/cems5/plc,s
      common/cems10/a(0:ntypmx),b(0:ntypmx),d(0:ntypmx)
      common/ems6/ivp0,iap0,idp0,isp0,ivt0,iat0,idt0,ist0
      double precision d,a,b,plc,s,amd,dcel,xvpr,xdm,at,xdm2
      common/ems3/dcel,ad
      common/cems13/xvpr(0:3)


c parameter test

      if(nflavems.lt.nrflav)
     &   call utstop("nflavems<nrflav : change it in epos-ems !&")


c abreviations

      plc=dble(e)
      s=plc**2
      amd=0.5d0   !dble(delrex) !(large enough in case of strangeness in string end


c alpha (0=0, 1=s, 2=v, 4=d, 8=f)

      a(0)=0d0
      a(1)=dble(alpsea)
      a(2)=dble(alpval)
      a(3)= 0.0d0
      a(4)=dble(alpdiq)
      a(5)=dble(a(4))
      a(6)= 0.0d0
      a(7)= 0.0d0
      a(8)=dble(a(2))
      a(9)= 0.0d0

c beta (0=0, 1=s, 2=v, 4=d, 8=f)

      b(0)=0.0d0
      b(1)=dble(-alpqua)
      b(2)=dble(-alpqua)
      b(3)=0.0d0
      b(4)=0.0d0
      b(5)=0.0d0
      b(6)=0.0d0
      b(7)=0.0d0
      b(8)=dble(-alpqua)
      b(9)=0.0d0


c alpha_trailing and beta_trailing (0=meson, 1=baryon;
c                                   0=no excit, 1=nondiffr, 2=diffr,
c                                   3=nondiffr split, 5=diffr split)

      at(0,0)=0.0d0
      at(0,1)=dble(alpndi)
      at(0,2)=dble(alpdi)
      at(0,3)=dble(alpdro(3))
      at(0,4)=10d0
      at(0,5)=dble(alpdro(3))
      at(1,0)=0.0d0
      at(1,1)=dble(alpndi)
      at(1,2)=dble(alpdi)
      at(1,3)=dble(alpdro(3))
      at(1,4)=10d0
      at(1,5)=dble(alpdro(3))

c minimal string masses ( i+j, each one: 0=0, 1=s, 2=v, 4=d, 5=d, 8=f)

      ammn(0)=0d0
      ammn(1)=0d0
      ammn(2)=dble(ammsqq)+amd
      ammn(3)=dble(ammsqq)
      ammn(4)=dble(ammsqq)
      ammn(5)=dble(ammsqd)+amd
      ammn(6)=dble(ammsqd)+amd
      ammn(7)=0d0
      ammn(8)=dble(ammsdd)+amd
      ammn(9)=dble(ammsqd)+amd
      ammn(10)=dble(ammsqd)+amd
      ammn(12)=dble(ammsqd)+amd
      ammn(16)=0.14d0

c minimal pomeron masses (0=soft or gg, 1=qg, 2=gq, 3=qq)

      amprmn(0)=ammsqq
      amprmn(1)=dsqrt(4d0*dble(q2min))
      amprmn(2)=amprmn(1)
      amprmn(3)=amprmn(1)

c cutoff for virtual pomeron (0=0, 1=soft Pom, 2=regge, 3=hard)

c      xvpr(0)=0d0
c      xvpr(1)=dble(cumpom**2)/s
c      xvpr(2)=dble(cumpom**2)/s
c      xvpr(3)=0.0d0**2/s

c minimal remnant masses (0=meson, 1=baryon)

      idpj=idpji
      xdm=0.35d0                  !<pt>
      call idmass(idpj,ampj)
      if(iabs(idpj).gt.1000)then
       ampmn(0)=0.14d0+xdm
       ampmn(1)=dble(ampj)+xdm
      else
       ampmn(0)=dble(ampj)+xdm
       ampmn(1)=0.94d0+xdm
      endif
      idtg=idtgi
      if(idtg.eq.0)idtg=1120
      call idmass(idtg,amtg)
      if(iabs(idtg).gt.1000)then
       amtmn(0)=0.14d0+xdm
       amtmn(1)=dble(amtg)+xdm
      else
       amtmn(0)=dble(amtg)+xdm
       amtmn(1)=0.94d0+xdm
      endif

c minimal excitation masses (0=meson, 1=baryon
c                            0=no excit, 1=nondiffr, 2=diffr,
c                                   6=nondiffr but no pomeron)

      xdm2=0.35d0
      amemn(0,0)=0.d0
      amemn(1,0)=0.d0
      amemn(0,4)=0.d0
      amemn(1,4)=0.d0
      amemn(0,6)=0.d0
      amemn(1,6)=0.d0

      amemn(0,1)=xdm2!+dble(delrex)
      amemn(0,2)=xdm2!+dble(delrex)
      amemn(0,3)=xdm2!+dble(delrex)
      amemn(0,5)=xdm2+dble(delrex) !remnant excited without connexion (split)

      amemn(1,1)=xdm2!+dble(delrex)
      amemn(1,2)=xdm2!+dble(delrex)
      amemn(1,3)=xdm2!+dble(delrex)
      amemn(1,5)=xdm2+dble(delrex) !remnant excited without connexion (split)

c maximal excitation masses (0=no excit, 1=nondiffr, 2=diffr)

      amemx(0)=2d0*xdm
      amemx(1)=plc
      amemx(2)=plc

      if(idpj.gt.1000)then     ! baryon

c initial quark configuration
       ivp0=3
       iap0=0
       idp0=1
       isp0=1

c no val quark for exotic projectile
       if(iremn.ge.2.and.(idpj.ne.1120.and.idpj.ne.1220))ivp0=0

      elseif(idpj.lt.-1000)then     ! antibaryon

c initial quark configuration
       ivp0=0
       iap0=3
       idp0=1
       isp0=1

c no val quark for exotic projectile
       if(iremn.ge.2.and.(idpj.ne.-1120.and.idpj.ne.-1220))iap0=0

      else      ! meson

c initial quark configuration
       ivp0=1
       iap0=1
       idp0=0
       if(iclpro.eq.1)then
         isp0=0
       else
         isp0=1
       endif

c no val quark for exotic projectile
       if(iremn.ge.2.and.(mod(abs(idpj/100),10).gt.4
     &                 .or.mod(abs(idpj/10),10).gt.4
     &    .or.mod(abs(idpj/100),10)/mod(abs(idpj/10),10).eq.1))then
         ivp0=0
         iap0=0
       endif
      endif

      if(idtg.gt.1000)then    ! baryon

c initial quark configuration
       ivt0=3
       iat0=0
       idt0=1
       ist0=0

c no val quark for exotic target
       if(iremn.ge.2.and.(idtg.ne.1120.and.idtg.ne.1220))ivt0=0

      elseif(idtg.lt.-1000)then   ! antibaryon

c initial quark configuration
       ivt0=0
       iat0=3
       idt0=1
       ist0=0

c no val quark for exotic target
       if(iremn.ge.2.and.(idtg.ne.-1120.and.idtg.ne.-1220))iat0=0

      else       ! meson

c initial quark configuration
       ivt0=1
       iat0=1
       if(icltar.eq.1)then
         idt0=0
       else
         idt0=1
       endif
       ist0=0

c no val quark for exotic target
       if(iremn.ge.2.and.(mod(abs(idtg/100),10).gt.4
     &                 .or.mod(abs(idtg/10),10).gt.4
     &    .or.mod(abs(idtg/100),10)/mod(abs(idtg/10),10).eq.1))then
         ivt0=0
         iat0=0
       endif

      endif


c eikonal parameters

       dcel=dble(chad(iclpro)*chad(icltar))

c counters

       antot=0.
       ansh=0.
       ansf=0.
       antotf=0.
       anshf=0.
       ansff=0.
       pp4max=0.
       pp4ini=0.
       andropl=0.
       anstrg0=0.
       anstrg1=0.
       anreso0=0.
       anreso1=0.
       anghadr=0.
       antotre=0.
       anintdiff=0.
       anintsdif=0.
       anintine=0.

      return
      end

c-----------------------------------------------------------------------
      subroutine emsigr
c-----------------------------------------------------------------------
c initialize grid
c-----------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'

      common/emsptl/nppr(npommx,kollmx),npproj(mamx),nptarg(mamx)

      call utpri('emsigr',ish,ishini,5)

      do k=1,koll  !----k-loop---->

c determine length of k-th line of grid

       o=max(1.e-5,min(sngl(om1intc(k)),float(npommx)))!if GFF used for propo
        if(ish.ge.7)write(ifch,*)'emsigr:k,o',k,o
       n=0
       if(o.le.50)then
         p=1./(exp(o)-1)
       else
         p=0.
       endif
10     n=n+1
       p=p*o/n
        if(ish.ge.7)write(ifch,*)'emsigr:n,p',n,p
       if((p.gt.1e-4.or.n.lt.int(o)).and.n.lt.npommx
     *.and.n.lt.nprmax)goto 10

       if(ish.ge.5)write(ifch,*)'emsigr:nmax,b',n,bk(k)

       npr(0,k)=n
       nprmx(k)=n
       nprt(k)=0
       do i=1,3
        npr(i,k)=0
       enddo


c initial value for interaction type

       itpr(k)=0

c initial value for nuclear splitting

       do ir=1,2
         knucnt(ir,k)=0
         do ncon=1,mamx
           npnuc(ncon,ir,k)=0
           irnuc(ncon,ir,k)=0
           xxnuc(ncon,ir,k)=0d0
         enddo
       enddo

c initialize grid


       do n=1,nprmx(k)
        idpr(n,k)=0
        idfpr(n,k)=0
        ivpr(n,k)=1
        nppr(n,k)=0
        nbkpr(n,k)=0
        nvpr(n,k)=0
        idsppr(n,k)=0
        idstpr(n,k)=0
        idrpr(n,k)=0
        idhpr(n,k)=0
        bhpr(n,k)=0.
        xpr(n,k)=0d0
        ypr(n,k)=0d0
        xppr(n,k)=0d0
        xmpr(n,k)=0d0
        xp1pr(n,k)=0d0
        xp2pr(n,k)=0d0
        xm1pr(n,k)=0d0
        xm2pr(n,k)=0d0
        xp1pr(n,k)=0d0
        xp2pr(n,k)=0d0
        xm1pr(n,k)=0d0
        xm2pr(n,k)=0d0
        idp1pr(n,k)=0
        idp2pr(n,k)=0
        idm1pr(n,k)=0
        idm2pr(n,k)=0
        xxp1pr(n,k)=0d0
        xyp1pr(n,k)=0d0
        xxp2pr(n,k)=0d0
        xyp2pr(n,k)=0d0
        xxm1pr(n,k)=0d0
        xym1pr(n,k)=0d0
        xxm2pr(n,k)=0d0
        xym2pr(n,k)=0d0
       enddo

      enddo !  <----k-loop-----

      call utprix('emsigr',ish,ishini,5)
      return
      end

c-----------------------------------------------------------------------
      subroutine emsipt
c-----------------------------------------------------------------------
c initialize projectile and target
c-----------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'

      common/emsptl/nppr(npommx,kollmx),npproj(mamx),nptarg(mamx)
      common/cems5/plc,s
      common/ems3/dcel,ad
      common/ems6/ivp0,iap0,idp0,isp0,ivt0,iat0,idt0,ist0
      common /cncl/xproj(mamx),yproj(mamx),zproj(mamx)
     *            ,xtarg(mamx),ytarg(mamx),ztarg(mamx)

      double precision dcel,s,plc

c initialize projectile

      do i=1,maproj
       idp(i)=idp0
       ivp(i)=ivp0+iap0
       iap(i)=iap0
       isp(i)=isp0
       iep(i)=-1
       ifp(i)=0
       kolp(i)=0
       npp(i)=0
       npproj(i)=0
       xxp(i)=0d0
       xyp(i)=0d0
       xpmn(i)=(amemn(idp(i),0)+ampmn(isp(i)))**2/s
       xpmx(i)=dmin1(1d0,(amemx(0)+ampmn(isp(i)))**2/s)
       xpos(i)=0.9d0*(amemx(0)+ampmn(isp(i)))**2/s
       xppmx(i)=0.5d0/(1d0+1d0/dble(maproj)**0.3d0)!1d0-dsqrt(xpmn(i))/maproj
       xmpmx(i)=0.5d0/(1d0+1d0/dble(matarg)**0.3d0)!1d0-dsqrt(xpmn(i))/matarg
       xmpmn(i)=xpmn(i)/xppmx(i)
       xppmn(i)=xpmn(i)/xmpmx(i)
       xpp(i)=1d0
       xmp(i)=0d0
       xppst(i)=0.d0
       xmpst(i)=0.d0
       xposst(i)=0.d0
      enddo

c initialize target

      do j=1,matarg
       idt(j)=idt0
       ivt(j)=ivt0+iat0
       iat(j)=iat0
       ist(j)=ist0
       iet(j)=-1
       ift(j)=0
       kolt(j)=0
       npt(j)=0
       nptarg(j)=0
       xxt(j)=0d0
       xyt(j)=0d0
       xtmn(j)=(amemn(idt(j),0)+amtmn(ist(j)))**2/s
       xtmx(j)=dmin1(1d0,(amemx(0)+amtmn(ist(j)))**2/s)
       xtos(j)=0.9d0*(amemx(0)+amtmn(ist(j)))**2/s
       xmtmx(j)=0.5d0/(1d0+1d0/dble(matarg)**0.3d0)!1d0-dsqrt(xtmn(j))/matarg
       xptmx(j)=0.5d0/(1d0+1d0/dble(maproj)**0.3d0)!1d0-dsqrt(xtmn(j))/maproj
       xptmn(j)=xtmn(j)/xmtmx(j)
       xmtmn(j)=xtmn(j)/xptmx(j)
       xmt(j)=1d0
       xpt(j)=0d0
       xmtst(j)=0.d0
       xptst(j)=0.d0
       xtosst(j)=0.d0
      enddo

      return
      end


c-----------------------------------------------------------------------
      subroutine emszz
c-----------------------------------------------------------------------
c     completes /cptl/ for nucleons, checks for no interaction
c     writes   /cevt/
c-----------------------------------------------------------------------
      include 'epos.inc'
      include 'epos.incems'
      common/nucl3/phi,bimp
      common/col3/ncol,kolpt
      integer kolpz(mamx),koltz(mamx)

      call utpri('emszz ',ish,ishini,6)

c     write /cptl/
c     ------------

      if(iokoll.eq.1)then   ! precisely matarg collisions

c nothing to do
        ntg=0
        npj=0
        ncoli=0

      else

c determine ncol

       ncolx=ncol
       ncol=0
       ncoli=0
       do 8 k=1,koll
       if(ish.ge.7)write(ifch,*)'k,itpr,ncol,ncolx',k,itpr(k),ncol,ncolx
        if(itpr(k).eq.0)goto 8
        if(abs(itpr(k)).eq.1)ncoli=ncoli+1
          ncol=ncol+1
          if(itpr(k).ne.3)then          !empty pair, remnant not modified
            i=iproj(k)
            j=itarg(k)
            istptl(i)=1
            iorptl(i)=-1
            tivptl(2,i)=coord(4,k)
            istptl(maproj+j)=1
            iorptl(maproj+j)=-1
            tivptl(2,maproj+j)=coord(4,k)
          endif
8      continue
       if(ncolx.ne.ncol)write(6,*)'ncolx,ncol:', ncolx,ncol
       if(ncolx.ne.ncol)call utstop('********ncolx.ne.ncol********&')
       if(ncol.eq.0)goto1001

c determine npj, ntg

       do ip=1,maproj
        kolpz(ip)=0
       enddo
       do it=1,matarg
        koltz(it)=0
       enddo
      do k=1,koll
       if(itpr(k).ne.0.and.itpr(k).ne.3)then
        ip=iproj(k)
        it=itarg(k)
        kolpz(ip)=kolpz(ip)+1
        koltz(it)=koltz(it)+1
       endif
      enddo
      npj=0
      do ip=1,maproj
       if(kolpz(ip).gt.0.or.iep(ip).ge.3)npj=npj+1
      enddo
      ntg=0
      do it=1,matarg
       if(koltz(it).gt.0.or.iet(it).ge.3)ntg=ntg+1
      enddo
c     write(6,*)'npj,ntg,npj+ntg:',npj,ntg,npj+ntg

       endif

c     write /cevt/
c     ------------

      nevt=1
      bimevt=bimp
      phievt=phi
      kolevt=ncol
      koievt=ncoli
      kohevt=0      !not yet defined
      npjevt=npj
      ntgevt=ntg
      pmxevt=pnll
      egyevt=engy
      !print*,' ===== ',kolevt,koievt' ====='

c     exit
c     ----

      if(ish.ge.7)then
      do n=1,nptl
      write(ifch,115)iorptl(n),jorptl(n),n,istptl(n)
     *,tivptl(1,n),tivptl(2,n)
      enddo
  115 format(1x,'/cptl/',2i6,2i10,2(e10.3,1x))
      endif

1000  continue
      call utprix('emszz ',ish,ishini,6)
      return

1001  continue
      if(ish.ge.3)then
      write(ifch,*)
      write(ifch,*)'   ***** no interaction!!!'
      write(ifch,*)'   ***** ncol=0 detected in emszz'
      write(ifch,*)
      endif
      goto 1000

      end

c-----------------------------------------------------------------------
      subroutine ProCop(i,ii)
c-----------------------------------------------------------------------
c Propose Coordinates of remnants from active projectile nucleons
c-----------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'
      include 'epos.incsem'

      double precision xmptmp,aproj
      common/cems5/plc,s
      common/emsptl/nppr(npommx,kollmx),npproj(mamx),nptarg(mamx)
      integer icrmn(2),jc(nflav,2),icini(2)
      double precision s,plc

      nptl=nptl+1
      npproj(i)=nptl
      idptl(nptl)=idptl(ii)*100+99  !100*10**idp(i)+iep(i)
      istptl(nptl)=40
      ityptl(nptl)=40
      iorptl(nptl)=ii
      jorptl(nptl)=0
      ifrptl(1,nptl)=0
      ifrptl(2,nptl)=0
      do j=1,2
        do k=1,nflav
          jc(k,j)=0
        enddo
      enddo

      istptl(ii)=1

c     determine kolz

      if(lproj(i).gt.1)then
        zmax=-ainfin
        kolz=0
        do l=1,lproj(i)
          k=kproj(i,l)
          z=coord(3,k)
          if(itpr(k).ne.0.and.z.gt.zmax)then
            zmax=z
            kolz=k
          endif
        enddo
      else
        kolz=1
      endif
c      if(kolz.eq.0)call utstop(' kolz=0 (proj)&')
      if(kolz.eq.0)then
        t=0.
      else
        t=coord(4,kolz)
      endif

      xorptl(1,nptl)=xorptl(1,ii)
      xorptl(2,nptl)=xorptl(2,ii)
      xorptl(3,nptl)=xorptl(3,ii)
      xorptl(4,nptl)=t
      tivptl(1,nptl)=t
      tivptl(2,nptl)=t
      naq=0
      nqu=0

      if(iremn.ge.2)then   !update icproj
        idp(i)=min(1,abs(idp(i)))
        k=1
        nqu=0
        do n=1,nrflav
          jc(n,k)=jcpref(n,k,i)
          nqu=nqu+jc(n,k)
        enddo
        k=2
        naq=0
        do n=1,nrflav
          jc(n,k)=jcpref(n,k,i)
          naq=naq+jc(n,k)
        enddo
        isum=nqu+naq
        call idenco(jc,icrmn,iret)
        if(iret.eq.0.and.(isum.le.3.or.iremn.ne.3))then
          icproj(1,i)=icrmn(1)
          icproj(2,i)=icrmn(2)
        elseif(iremn.eq.3)then
      write(ifch,*)'Problem in projectile flavor :',i,' ->',jc,' :',isum
          call utstop('Procop: Problem in projectile flavor !&')
        else     !for iremn=2 and large number of quark define icproj=999999
          icproj(1,i)=999999
          icproj(2,i)=999999
        endif
      endif

      icrmn(1)=icproj(1,i)
      icrmn(2)=icproj(2,i)

      if(iremn.ge.1)then      !excited remnant ?
        call idtr4(idptl(ii),icini)
        if(ish.ge.5)write(ifch,*)'Procop icini proj',i,icini,' ->',icrmn
        if((icrmn(1)-icini(1))+(icrmn(2)-icini(2)).ne.0)then
          if(iep(i).eq.6)then
            write(ifch,'(a,d25.15)')
     &'Flavor problem in proj for pseudo-inelastic collision !',seedc
          elseif(iep(i).eq.0)then
            iep(i)=1
          endif
        endif

        if(iremn.eq.2)then
          if(.not.((nqu.eq.3.and.naq.eq.0).or.(nqu.eq.0.and.naq.eq.3)
     &       .or.(nqu.eq.1.and.naq.eq.1)))iep(i)=3

        endif
      endif

      if(ish.ge.5)write(ifch,'(a,i3,a,i3,a,i2)')
     &            'Procop part ',ii,', iep(',i,'): ',iep(i)

      if(iremn.le.1)call iddeco(icrmn,jc)
      if(iep(i).ge.1.and.iep(i).ne.6)then
        aproj=dble(max(amproj,fremnux(jc)))
      else
        aproj=dble(max(amproj,fremnux2(jc)))
      endif
c      aprojex=max(ampmn(isp(i))+amemn(idp(i),iep(i))
c     &           ,dble(fremnux(jc)))
      xmptmp=(aproj**2+xxp(i)*xxp(i)+xyp(i)*xyp(i))
     &       /(xpp(i)*s)
      xpos(i)=xpp(i)*xmptmp
      if(ish.ge.5)write(ifch,*)'Procop mass : ',aproj,xpos(i)*s
      if(xmptmp.gt.1.d0)then
        xmptmp=0.d0
      if(ish.ge.1)write(ifmt,*)'Warning in ProCop, Remnant mass too low'
      endif

      pptl(1,nptl)=sngl(xxp(i))
      pptl(2,nptl)=sngl(xyp(i))
      pptl(3,nptl)=sngl((xpp(i)-xmptmp)*plc/2d0)
      pptl(4,nptl)=sngl((xpp(i)+xmptmp)*plc/2d0)
      pptl(5,nptl)=aproj

c      write(ifmt,*)'ProCop',i,nptl

      return

      end

c-----------------------------------------------------------------------
      subroutine ProCot(j,jj)
c-----------------------------------------------------------------------
c Propose Coordinates of remnants from active targets nucleons
c-----------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'
      include 'epos.incsem'

      double precision xpttmp,atarg
      common/cems5/plc,s
      common/emsptl/nppr(npommx,kollmx),npproj(mamx),nptarg(mamx)
      integer icrmn(2),jc(nflav,2),icini(2)
      double precision s,plc

      nptl=nptl+1
      nptarg(j)=nptl

      idptl(nptl)=idptl(jj)*100+99    !100*10**idt(j)+iet(j)
      istptl(nptl)=40
      ityptl(nptl)=50
      iorptl(nptl)=jj
      jorptl(nptl)=0
      ifrptl(1,nptl)=0
      ifrptl(2,nptl)=0
      do k=1,2
        do i=1,nflav
          jc(i,k)=0
        enddo
      enddo

      istptl(jj)=1

c     determine kolz

      if(ltarg(j).gt.1)then
        zmin=ainfin
        kolz=0
        do l=1,ltarg(j)
          k=ktarg(j,l)
          z=coord(3,k)
          if(itpr(k).ne.0.and.z.lt.zmin)then
            zmin=z
            kolz=k
          endif
        enddo
      else
        kolz=1
      endif
c      if(kolz.eq.0)call utstop(' kolz=0 (targ)&')
      if(kolz.eq.0)then
        t=0.
      else
        t=coord(4,kolz)
      endif

      xorptl(1,nptl)=xorptl(1,jj)
      xorptl(2,nptl)=xorptl(2,jj)
      xorptl(3,nptl)=xorptl(3,jj)
      xorptl(4,nptl)=t
      tivptl(1,nptl)=t
      tivptl(2,nptl)=t
      naq=0
      nqu=0

      if(iremn.ge.2)then   !update ictarg
        idt(j)=min(1,abs(idt(j)))
        k=1
        nqu=0
        do n=1,nrflav
          jc(n,k)=jctref(n,k,j)
          nqu=nqu+jc(n,k)
        enddo
        k=2
        naq=0
        do n=1,nrflav
          jc(n,k)=jctref(n,k,j)
          naq=naq+jc(n,k)
        enddo
        isum=nqu+naq
        call idenco(jc,icrmn,iret)
        if(iret.eq.0.and.(isum.le.3.or.iremn.ne.3))then
          ictarg(1,j)=icrmn(1)
          ictarg(2,j)=icrmn(2)
        elseif(iremn.eq.3)then
      write(ifch,*)'Problem in projectile flavor :',j,' ->',jc,' :',isum
          call utstop('Procot: Problem in target flavor !&')
        else     !for iremn=2 and large number of quark define ictarg=999999
          ictarg(1,j)=999999
          ictarg(2,j)=999999
        endif
      endif

      icrmn(1)=ictarg(1,j)
      icrmn(2)=ictarg(2,j)

      if(iremn.ge.1)then      !excited remnant ?
        call idtr4(idptl(jj),icini)
        if(ish.ge.5)write(ifch,*)'Procot icini targ',j,icini,' ->',icrmn
        if((icrmn(1)-icini(1))+(icrmn(2)-icini(2)).ne.0)then
          if(iet(j).eq.6)then
            write(ifch,'(a,d25.15)')
     &'Flavor problem in targ for pseudo-inelastic collision !',seedc
          elseif(iet(j).eq.0)then
            iet(j)=1
          endif
        endif

        if(iremn.eq.2)then
          if(.not.((nqu.eq.3.and.naq.eq.0).or.(nqu.eq.0.and.naq.eq.3)
     &       .or.(nqu.eq.1.and.naq.eq.1)))iet(j)=3

        endif
      endif
      if(ish.ge.5)write(ifch,'(a,i3,a,i3,a,i2)')
     &            'Procot part ',jj,', iet(',j,'): ',iet(j)



      if(iremn.le.1)call iddeco(icrmn,jc)
      if(iet(j).ge.1.and.iet(j).ne.6)then
        atarg=dble(max(amtarg,fremnux(jc)))
      else
        atarg=dble(max(amtarg,fremnux2(jc)))
      endif
c      atargex=max(amtmn(ist(j))+amemn(idt(j),iet(j))
c     &           ,dble(fremnux(jc)))
      xpttmp=(atarg**2+xxt(j)*xxt(j)+xyt(j)*xyt(j))
     &       /(xmt(j)*s)
      xtos(j)=xpttmp*xmt(j)
      if(ish.ge.5)write(ifch,*)'Procot mass : ',atarg,xtos(j)*s
      if(xpttmp.gt.1.d0)then
        xpttmp=0.d0
      if(ish.ge.1)write(ifch,*)'Warning in ProCot, Remnant mass too low'
      endif

      pptl(1,nptl)=sngl(xxt(j))
      pptl(2,nptl)=sngl(xyt(j))
      pptl(3,nptl)=sngl((xpttmp-xmt(j))*plc/2d0)
      pptl(4,nptl)=sngl((xpttmp+xmt(j))*plc/2d0)
      pptl(5,nptl)=atarg

c      write(ifmt,*)'ProCot',j,nptl

      return
      end

c-----------------------------------------------------------------------
      subroutine emswrp(i,ii)
c-----------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'

      double precision p5sq
      common/cems5/plc,s
      common/emsptl/nppr(npommx,kollmx),npproj(mamx),nptarg(mamx)
      double precision s,plc
      parameter(eps=1.e-5)

      if(npproj(i).eq.0)then
        write(*,*)'emswrp i ii',i,ii
        call utstop('emswrp with npproj=0 should never happen !&')

c        t=xorptl(4,kolp(i))
c        istptl(ii)=1
c        iorptl(ii)=-1
c        tivptl(2,ii)=t
c        nptl=nptl+1
c        npproj(i)=nptl
c        idptl(nptl)=idptl(ii)*100+99 !100*10**idp(i)+iep(i)
c        istptl(nptl)=40
c        ityptl(nptl)=40
c        iorptl(nptl)=ii
c        jorptl(nptl)=kolp(i)
c        ifrptl(1,nptl)=0
c        ifrptl(2,nptl)=0
c        xorptl(1,nptl)=xorptl(1,ii)
c        xorptl(2,nptl)=xorptl(2,ii)
c        xorptl(3,nptl)=xorptl(3,ii)
c        xorptl(4,nptl)=t
c        tivptl(1,nptl)=t
c        tivptl(2,nptl)=t
        mm=nptl
c        kolp(i)=1
      else
        mm=npproj(i)
      endif
      if(iLHC.eq.1.and.(iep(i).eq.0.or.iep(i).eq.6))
     &xmp(i)=min(1d0-xpp(i),xmp(i))
      pptl(1,mm)=sngl(xxp(i))
      pptl(2,mm)=sngl(xyp(i))
      pptl(3,mm)=sngl((xpp(i)-xmp(i))*plc/2d0)
      pptl(4,mm)=sngl((xpp(i)+xmp(i))*plc/2d0)
      if(pptl(4,mm).lt.-eps)call utstop('E pro<0 !&')
      p5sq=xpp(i)*xmp(i)*s-xxp(i)*xxp(i)-xyp(i)*xyp(i)
      if(p5sq.gt.1.d-10)then
        pptl(5,mm)=sngl(sqrt(p5sq))
      elseif(iep(i).eq.0)then
        pptl(5,mm)=pptl(5,ii)
      else
        if(ish.ge.2)then
          write(ifch,*)'problem with mass for projectile, '
     &         ,'continue with zero mass'
          write(ifch,*)i,mm,xxp(i),xyp(i),xpp(i),xmp(i),p5sq
        endif
        pptl(5,mm)=0.
      endif

      do l=1,4
       ibptl(l,mm)=0
      enddo

      return

      end

c-----------------------------------------------------------------------
      subroutine emswrt(j,jj)
c-----------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'

      double precision p5sq
      common/cems5/plc,s
      common/emsptl/nppr(npommx,kollmx),npproj(mamx),nptarg(mamx)
      double precision s,plc
      parameter(eps=1.e-5)

      if(nptarg(j).eq.0)then

        write(*,*)'emswrt j jj',j,jj
        call utstop('emswrt with nptarg=0 should never happen !&')

c        t=xorptl(4,kolt(j))
c        istptl(jj)=1
c        iorptl(jj)=-1
c        tivptl(2,jj)=t
c        nptl=nptl+1
c        nptarg(j)=nptl
c        idptl(nptl)=idptl(jj)*100+99 !100*10**idp(i)+iep(i)
c        istptl(nptl)=40
c        ityptl(nptl)=50
c        iorptl(nptl)=jj
c        jorptl(nptl)=kolt(j)
c        ifrptl(1,nptl)=0
c        ifrptl(2,nptl)=0
c        xorptl(1,nptl)=xorptl(1,jj)
c        xorptl(2,nptl)=xorptl(2,jj)
c        xorptl(3,nptl)=xorptl(3,jj)
c        xorptl(4,nptl)=t
c        tivptl(1,nptl)=t
c        tivptl(2,nptl)=t
c... initialize
        mm=nptl
c        kolt(j)=1
      else
        mm=nptarg(j)
      endif
      if(iLHC.eq.1.and.(iet(j).eq.0.or.iet(j).eq.6))
     &xpt(j)=min(1d0-xmt(j),xpt(j))
      pptl(1,mm)=sngl(xxt(j))
      pptl(2,mm)=sngl(xyt(j))
      pptl(3,mm)=sngl((xpt(j)-xmt(j))*plc/2d0)
      pptl(4,mm)=sngl((xpt(j)+xmt(j))*plc/2d0)
      if(pptl(4,mm).lt.-eps)call utstop('E targ<0 !&')
      p5sq=xpt(j)*xmt(j)*s-xxt(j)*xxt(j)-xyt(j)*xyt(j)
      if(p5sq.gt.1.d-10)then
        pptl(5,mm)=sngl(sqrt(p5sq))
      elseif(iet(j).eq.0)then
        pptl(5,mm)=pptl(5,jj)
      else
        if(ish.ge.2)then
          write(ifch,*)'problem with mass for target, '
     &            ,'continue with zero mass'
          write(ifch,*)j,mm,xxt(j),xyt(j),xpt(j),xmt(j),p5sq
        endif
        pptl(5,mm)=0.
      endif

      do l=1,4
       ibptl(l,mm)=0
      enddo

      return
      end

c-----------------------------------------------------------------------
      subroutine emswrpom(k,i,j)
c-----------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'

      common/cems5/plc,s
      common/emsptl/nppr(npommx,kollmx),npproj(mamx),nptarg(mamx)
      double precision s,px,py,plc

      do 30 n=1,nprmx(k)
       if(idpr(n,k).eq.0.or.ivpr(n,k).eq.0)goto30
       nptl=nptl+1
       nppr(n,k)=nptl
       px=xxp1pr(n,k)+xxp2pr(n,k)+xxm1pr(n,k)+xxm2pr(n,k)
       py=xyp1pr(n,k)+xyp2pr(n,k)+xym1pr(n,k)+xym2pr(n,k)
       pptl(1,nptl)=sngl(px)
       pptl(2,nptl)=sngl(py)
       pptl(3,nptl)=sngl(dsqrt(xpr(n,k))*dsinh(ypr(n,k))*plc)
       pptl(4,nptl)=sngl(dsqrt(xpr(n,k))*dcosh(ypr(n,k))*plc)
       pptl(5,nptl)=sngl(dsqrt(xpr(n,k)*s-px*px-py*py))
   !    print*,pptl(5,nptl)/plc
       idptl(nptl)=idpr(n,k)*10000
     &     +idp1pr(n,k)*1000
     &     +idp2pr(n,k)*100
     &     +idm1pr(n,k)*10
     &     +idm2pr(n,k)
       idptl(nptl)=idptl(nptl)*100+99
       istptl(nptl)=30
       iorptl(nptl)=i
       jorptl(nptl)=j
       ifrptl(1,nptl)=0
       ifrptl(2,nptl)=0
       xorptl(1,nptl)=coord(1,k)
       xorptl(2,nptl)=coord(2,k)
       xorptl(3,nptl)=coord(3,k)
       xorptl(4,nptl)=coord(4,k)
       tivptl(1,nptl)=coord(4,k)
       tivptl(2,nptl)=coord(4,k)
       if(idpr(n,k).eq.1)then
        ityptl(nptl)=20
        if(itpr(k).gt.0)ityptl(nptl)=25
       elseif(idpr(n,k).eq.3)then
        ityptl(nptl)=30
        if(itpr(k).gt.0)ityptl(nptl)=35
       else
        call utstop('emswrpom: unknown id&')
       endif
       do l = 1,4
        ibptl(l,nptl)=0
       enddo
30    continue

      return
      end

c-----------------------------------------------------------------------
      subroutine emsfrag(iret)
c-----------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'
      double precision pfrx(mamxx),pfry(mamxx),pfrz(mamxx),xmean,ymean
     &                ,zmean,spec
      integer          ityp(mamxx)

      iret=0

c Projectile fragment(s)

      irest = maproj*100+abs(laproj)
      inew=0
      idrest=0
      mapro=maproj
      xmean=0d0
      ymean=0d0
      zmean=0d0
      spec=0d0
      amrest=0.
      imin=maproj
      imax=1

      do is=1,maproj
        
        if(istptl(is).eq.0)then
          if ( iorptl(is) .eq. 0 ) then
            if(infragm.eq.0)then   !keep free nucleons
c  copy spectators at the end of the list (necessary for hepmc interface)
              nptl=nptl+1
              if(nptl.gt.mxptl)then
                iret=1
                goto 1000
              endif
              call utrepl(nptl,is)
              istptl(is)=1
              ifrptl(1,is)=nptl
              ifrptl(2,is)=nptl
              istptl(nptl)=0
              iorptl(nptl)=is
            else
c  compose projectile spectators to remaining nucleus
              spec=spec+1d0
              tivptl(2,is)=0d0
              xmean=xmean+xorptl(1,is)
              ymean=ymean+xorptl(2,is)
              zmean=0d0
              amrest=amrest+pptl(5,is)
              imin=min(imin,is)
              imax=max(imax,is)
              istptl(is)=1
              ifrptl(1,is)=nptl+1
              ifrptl(2,is)=nptl+1
              idrest = is
              id=idptl(is)
              if     ( id .eq. 1120 ) then
                inew  = inew + 101
                irest = irest - 101
              elseif ( id .eq. 1220 ) then
                inew  = inew + 100
                irest = irest - 100
              endif
            endif
          endif
        elseif( iorptl(is) .le. 0  .and.  istptl(is) .eq. 1 ) then
          if( iorptl(is) .eq. 0 )jorptl(is)=1
          mapro=mapro-1
        endif
        
      enddo

      if(inew.eq.0)goto 100

      xmean=xmean/spec
      ymean=ymean/spec
      zmean=zmean/spec
      nptla=nptl
c prepare intermediate particle to produce nuclear fragment
      nptl=nptl+1
      if(nptl.gt.mxptl)then
        iret=1
        goto 1000
      endif

      if( inew .eq. 100 .or. inew .eq. 101 ) then
c  remaining nucleus is single neutron or proton
        call utrepl(nptl,idrest)
        ifrptl(1,idrest)=nptl
        ifrptl(2,idrest)=nptl
        istptl(nptl)=0
        iorptl(nptl)=idrest
        goto 100

      else

c intermediate particles for father/mother relationship
        idptl(nptl)=800000000+inew
        ea = float(inew/100)*pptl(4,idrest)
c  momenta squared
        ptm = sqrt(max(0.,(ea-amrest)*(ea+amrest)))
        istptl(nptl)=51
        pptl(1,nptl)=0.
        pptl(2,nptl)=0.
        pptl(3,nptl)=ptm
        pptl(4,nptl)=sqrt(pptl(1,nptl)**2+pptl(2,nptl)**2
     *                     +pptl(3,nptl)**2+amrest**2)
        pptl(5,nptl)=amrest         !mass
        ityptl(nptl)=40
        iorptl(nptl)=imax
        jorptl(nptl)=imax
        ifrptl(1,nptl)=nptl+1
        ifrptl(2,nptl)=0
        xorptl(1,nptl)=0d0
        xorptl(2,nptl)=0d0
        xorptl(3,nptl)=0d0
        xorptl(4,nptl)=0d0
        tivptl(1,nptl)=0d0
        tivptl(2,nptl)=0d0

        if ( infragm .ge. 2 ) then
c  remaining nucleus is evaporating nucleons and alpha particles
         jfin  = 0
         call epovapor( mapro,inew,jfin,ityp,pfrx,pfry,pfrz )
         if ( jfin .eq. 0 )then   !something failed
           iret=1
           goto 1000
         endif
c loop to treat the remnants of the desintegrated fragment
         do  135  j = 1, jfin
          if(ityp(j).lt.0.)then
            idnucl=-ityp(j)
            inucl= idnucl/100
            if(idnucl.eq.402)then   !helium (alpha)
              idnucl=19
            elseif(idnucl.eq.301)then   !tritium
              idnucl=18
            elseif(idnucl.eq.201)then   !deuterium
              idnucl=17
            else
              iprot= mod(idnucl,100)
              idnucl=1000000000+iprot*10000+inucl*10 !PDG code for nucleus
            endif
          else
            inucl=1
            idnucl=ityp(j)
          endif
          ea = float(inucl)*pptl(4,idrest)
c  momenta squared
          call idmass(idnucl,am)
          ptm = ( ea - am ) * ( ea + am )
          pt2 = sngl( pfrx(j)**2 + pfry(j)**2 )
          if(ish.ge.6)write(ifch,*) 'pro fragment: j,id,ea,ptm,pt2=',
     *                                       j,idnucl,ea,ptm,pt2
          if ( pt2 + pfrz(j)**2 .ge. ptm ) then
            if (ish.ge.2) write(ifch,*) 'emsfrag: pt reject particle',j
            nnn=0
            is=0
            do while (is.lt.maproj.and.nnn.lt.inucl)
              is=is+1
              if(istptl(is).eq.1
     &             .and.jorptl(is).eq.0.and.iorptl(is).eq.0)then
                nnn=nnn+1
c  copy spectators at the end of the list (necessary for hepmc interface)
                nptl=nptl+1
                if(nptl.gt.mxptl)then
                  iret=1
                  goto 1000
                endif
                call utrepl(nptl,is)
                jorptl(is)=1
                ifrptl(1,is)=nptl
                ifrptl(2,is)=nptl
                istptl(nptl)=0
                iorptl(nptl)=is
              endif
            enddo
            goto 135
          else
            plong = sqrt(ptm-pt2)
          endif
          nptl=nptl+1
          if(nptl.gt.mxptl)then
            iret=1
            goto 1000
          endif
          istptl(nptl)=0
          pptl(1,nptl)=sngl(pfrx(j))
          pptl(2,nptl)=sngl(pfry(j))
          pptl(3,nptl)=plong+sngl(pfrz(j))   !OK if plong >> pfrz
          pptl(4,nptl)=sqrt(pptl(1,nptl)**2+pptl(2,nptl)**2
     *                     +pptl(3,nptl)**2+am**2)
          pptl(5,nptl)=am    !mass
          ityptl(nptl)=0
          iorptl(nptl)=nptla+1
          jorptl(nptl)=0
          ifrptl(1,nptl)=0
          ifrptl(2,nptl)=0
          xorptl(1,nptl)=xmean
          xorptl(2,nptl)=ymean
          xorptl(3,nptl)=zmean
          xorptl(4,nptl)=zmean
          tivptl(1,nptl)=zmean
          tivptl(2,nptl)=tivptl(2,idrest)
          idptl(nptl)=idnucl
 135    continue

        elseif ( infragm .eq. 1 ) then
c  remaining nucleus is one fragment
          nptl=nptl+1
          if(nptl.gt.mxptl)then
            iret=1
            goto 1000
          endif
          istptl(nptl)=0
          pptl(1,nptl)=0.d0
          pptl(2,nptl)=0.d0
          pptl(4,nptl)=0.d0
          inucl=0
          do is=1,maproj
            if(iorptl(is).eq.0.and.jorptl(is).eq.0)then
              inucl=inucl+1
              pptl(4,nptl)=pptl(4,nptl)+dble(pptl(4,is))
            endif
          enddo
          if(inucl.ne.inew/100)call utstop('Pb in emsfrag !&')
          idnucl=1000000000+mod(inew,100)*10000+(inew/100)*10
          call idmass(idnucl,am)
          pptl(5,nptl)=am    !mass
          ptot=(pptl(4,nptl)+am)*(pptl(4,nptl)-am)
          pptl(3,nptl)=sqrt(ptot)
          ityptl(nptl)=0
          istptl(nptl)=0
          iorptl(nptl)=nptla+1
          jorptl(nptl)=0
          ifrptl(1,nptl)=0
          ifrptl(2,nptl)=0
          xorptl(1,nptl)=xmean
          xorptl(2,nptl)=ymean
          xorptl(3,nptl)=zmean
          xorptl(4,nptl)=zmean
          tivptl(1,nptl)=zmean
          tivptl(2,nptl)=tivptl(2,idrest)
          idptl(nptl)=idnucl
        endif
        ifrptl(2,nptla+1)=nptl
        if(ifrptl(1,nptla+1).gt.ifrptl(2,nptla+1))then
          ifrptl(1,nptla+1)=0
          ifrptl(2,nptla+1)=0
        endif
      endif

      do is=nptla+1,nptl
          if(ish.ge.5)write(ifch,'(a,i5,a,i10,a,4(e10.4,1x),f6.3)')
     $       ' Projectile fragments ',is,' id :',idptl(is)
     $  , ' momentum :',(pptl(k,is),k=1,5)
      enddo

 100  continue

c Target fragment(s)

      irest = matarg*100+abs(latarg)
      inew=0
      matar=matarg
      xmean=0d0
      ymean=0d0
      zmean=0d0
      spec=0d0
      amrest=0.
      imin=maproj+matarg
      imax=maproj+1

      do is=maproj+1,maproj+matarg
        
        if(istptl(is).eq.0)then
          if ( iorptl(is) .eq. 0 ) then
            if(infragm.eq.0)then   !keep free nucleons
c  copy spectators at the end of the list (necessary for hepmc interface)
              nptl=nptl+1
              if(nptl.gt.mxptl)then
                iret=1
                goto 1000
              endif
              call utrepl(nptl,is)
              istptl(is)=1
              ifrptl(1,is)=nptl
              ifrptl(2,is)=nptl
              istptl(nptl)=0
              iorptl(nptl)=is
            else
c  compose projectile spectators to remaining nucleus
              spec=spec+1d0
              tivptl(2,is)=0d0
              xmean=xmean+xorptl(1,is)
              ymean=ymean+xorptl(2,is)
              zmean=0d0
              amrest=amrest+pptl(5,is)
              imin=min(imin,is)
              imax=max(imax,is)
              istptl(is)=1
              ifrptl(1,is)=nptl+1
              ifrptl(2,is)=nptl+1
              idrest = is
              id=idptl(is)
              if     ( id .eq. 1120 ) then
                inew  = inew + 101
                irest = irest - 101
              elseif ( id .eq. 1220 ) then
                inew  = inew + 100
                irest = irest - 100
              endif
            endif
          endif
          
        elseif( iorptl(is) .le. 0  .and.  istptl(is) .eq. 1 ) then
          if( iorptl(is) .eq. 0 ) jorptl(is)=1
          matar=matar-1
        endif
        
      enddo

      if(inew.eq.0)goto 1000

      xmean=xmean/spec
      ymean=ymean/spec
      zmean=zmean/spec
      nptla=nptl
c prepare intermediate particle to produce nuclear fragment
      nptl=nptl+1
      if(nptl.gt.mxptl)then
        iret=1
        goto 1000
      endif

      if( inew .eq. 100 .or. inew .eq. 101 ) then
c  remaining nucleus is single neutron or proton
        call utrepl(nptl,idrest)
        ifrptl(1,idrest)=nptl
        ifrptl(2,idrest)=nptl
        istptl(nptl)=0
        iorptl(nptl)=idrest
        goto 1000

      else

c intermediate particles for father/mother relationship
        idptl(nptl)=800000000+inew
        ea = float(inew/100)*pptl(4,idrest)
c  momenta squared
        ptm = sqrt(max(0.,(ea-amrest)*(ea+amrest)))
        istptl(nptl)=51
        pptl(1,nptl)=0.
        pptl(2,nptl)=0.
        pptl(3,nptl)=-ptm
        pptl(4,nptl)=sqrt(pptl(1,nptl)**2+pptl(2,nptl)**2
     *                     +pptl(3,nptl)**2+amrest**2)
        pptl(5,nptl)=amrest         !mass
        ityptl(nptl)=50
        iorptl(nptl)=imax
        jorptl(nptl)=imax
        ifrptl(1,nptl)=nptl+1
        ifrptl(2,nptl)=0
        xorptl(1,nptl)=0d0
        xorptl(2,nptl)=0d0
        xorptl(3,nptl)=0d0
        xorptl(4,nptl)=0d0
        tivptl(1,nptl)=0d0
        tivptl(2,nptl)=0d0

        if ( infragm .ge. 2 ) then
c  remaining nucleus is evaporating nucleons and alpha particles
         jfin  = 0
         call epovapor( matar,inew,jfin,ityp,pfrx,pfry,pfrz )
         if ( jfin .eq. 0 )then   !something failed
           iret=1
           goto 1000
         endif
c loop to treat the remnants of the desintegrated fragment
         do  235  j = 1, jfin
          if(ityp(j).lt.0.)then
            idnucl=-ityp(j)
            inucl= idnucl/100
            if(idnucl.eq.402)then   !helium (alpha)
              idnucl=19
            elseif(idnucl.eq.301)then   !tritium
              idnucl=18
            elseif(idnucl.eq.201)then   !deuterium
              idnucl=17
            else
              iprot= mod(idnucl,100)
              idnucl=1000000000+iprot*10000+inucl*10 !PDG code for nucleus
            endif
          else
            inucl=1
            idnucl=ityp(j)
          endif
          ea = float(inucl)*pptl(4,idrest)
c  momenta squared
          call idmass(idnucl,am)
          ptm = ( ea - dble(am) ) * ( ea + dble(am) )
          pt2 = sngl( pfrx(j)**2 + pfry(j)**2 )
          if(ish.ge.6)write(ifch,*) 'tar fragment: j,id,ea,ptm,pt2=',
     *                                       j,idnucl,ea,ptm,pt2
          if ( pt2 + pfrz(j)**2 .ge. ptm ) then
            if (ish.ge.2) write(ifch,*) 'emsfrag: pt reject particle',j
            nnn=0
            is=maproj
            do while (is.lt.maproj+matarg.and.nnn.lt.inucl)
              is=is+1
              if(istptl(is).eq.1
     &             .and.jorptl(is).eq.0.and.iorptl(is).eq.0)then
                nnn=nnn+1
c  copy spectators at the end of the list (necessary for hepmc interface)
                nptl=nptl+1
                if(nptl.gt.mxptl)then
                  iret=1
                  goto 1000
                endif
                call utrepl(nptl,is)
                jorptl(is)=1
                ifrptl(1,is)=nptl
                ifrptl(2,is)=nptl
                istptl(nptl)=0
                iorptl(nptl)=is
              endif
            enddo
            goto 235
          else
            plong=-sqrt(ptm-pt2)
          endif
          nptl=nptl+1
          if(nptl.gt.mxptl)then
            iret=1
            goto 1000
          endif
          istptl(nptl)=0
          pptl(1,nptl)=sngl(pfrx(j))
          pptl(2,nptl)=sngl(pfry(j))
          pptl(3,nptl)=plong+sngl(pfrz(j))   !OK if plong >> pfrz
          pptl(4,nptl)=sqrt(pptl(1,nptl)**2+pptl(2,nptl)**2
     *                     +pptl(3,nptl)**2+am**2)
          pptl(5,nptl)=am    !mass
          ityptl(nptl)=0
          iorptl(nptl)=nptla+1
          jorptl(nptl)=0
          ifrptl(1,nptl)=0
          ifrptl(2,nptl)=0
          xorptl(1,nptl)=xmean
          xorptl(2,nptl)=ymean
          xorptl(3,nptl)=zmean
          xorptl(4,nptl)=zmean
          tivptl(1,nptl)=zmean
          tivptl(2,nptl)=tivptl(2,idrest)
          idptl(nptl)=idnucl
 235    continue

        elseif ( infragm .eq. 1 ) then
c  remaining nucleus is one fragment
          nptl=nptl+1
          if(nptl.gt.mxptl)then
            iret=1
            goto 1000
          endif
          istptl(nptl)=0
          pptl(1,nptl)=0.d0
          pptl(2,nptl)=0.d0
          pptl(4,nptl)=0.d0
          inucl=0
          do is=maproj+1,maproj+matarg
            if(iorptl(is).eq.0.and.jorptl(is).eq.0)then
              inucl=inucl+1
              pptl(4,nptl)=pptl(4,nptl)+dble(pptl(4,is))
            endif
          enddo
          if(inucl.ne.inew/100)call utstop('Pb in emsfrag !&')
          idnucl=1000000000+mod(inew,100)*10000+(inew/100)*10
          call idmass(idnucl,am)
          pptl(5,nptl)=am    !mass
          ptot=(pptl(4,nptl)+am)*(pptl(4,nptl)-am)
          pptl(3,nptl)=sqrt(ptot)
          ityptl(nptl)=0
          istptl(nptl)=0
          iorptl(nptl)=nptla+1
          jorptl(nptl)=0
          ifrptl(1,nptl)=0
          ifrptl(2,nptl)=0
          xorptl(1,nptl)=xmean
          xorptl(2,nptl)=ymean
          xorptl(3,nptl)=zmean
          xorptl(4,nptl)=zmean
          tivptl(1,nptl)=zmean
          tivptl(2,nptl)=tivptl(2,idrest)
          idptl(nptl)=idnucl
        endif
        ifrptl(2,nptla+1)=nptl
        if(ifrptl(1,nptla+1).gt.ifrptl(2,nptla+1))then
          ifrptl(1,nptla+1)=0
          ifrptl(2,nptla+1)=0
        endif
      endif

      do is=nptla+1,nptl
          if(ish.ge.5)write(ifch,'(a,i5,a,i10,a,4(e10.4,1x),f6.3)')
     $       ' Target fragments ',is,' id :',idptl(is)
     $  , ' momentum :',(pptl(k,is),k=1,5)
      enddo


 1000 continue


      end

cc--------------------------------------------------------------------------
c      subroutine reaction(idpj,idtg,ireac)
cc--------------------------------------------------------------------------
cc returns reaction code ireac
cc--------------------------------------------------------------------------
c      iap=iabs(idpj/10)
c      iat=iabs(idtg/10)
c      isp=idpj/10/iap
c      ist=idtg/10/iat
c      call idchrg(idpj,cp)
c      call idchrg(idtg,ct)
c      ac=abs(cp+ct)
c      if(iap.gt.100)then
c       if(iat.gt.100)then
c        if(isp.eq.1)then
c         if(ist.eq.1)then
c          ireac=1
c         else
c          ireac=6
c         endif
c        else
c         if(ist.eq.1)then
c          ireac=6
c         else
c          ireac=1
c         endif
c        endif
c       elseif(iat.eq.11.or.iat.eq.12.or.iat.eq.22)then
c        if(ac.ge.2.)then
c         ireac=2
c        else
c         ireac=3
c        endif
c       else
c        if(ac.ge.2.)then
c         ireac=4
c        else
c         ireac=5
c        endif
c       endif
c      elseif(iap.eq.11.or.iap.eq.12.or.iap.eq.22)then
c       if(iat.gt.100)then
c        if(ac.ge.2.)then
c         ireac=2
c        else
c         ireac=3
c        endif
c       elseif(iat.eq.11.or.iat.eq.12.or.iat.eq.22)then
c        ireac=7
c       else
c        ireac=8
c       endif
c      else
c       if(iat.gt.100)then
c        if(ac.ge.2.)then
c         ireac=4
c        else
c         ireac=5
c        endif
c       elseif(iat.eq.11.or.iat.eq.12.or.iat.eq.22)then
c        ireac=8
c       else
c        ireac=9
c       endif
c      endif
c
c      end
c
c-----------------------------------------------------------------------
      subroutine xEmsI1(iii,kc,omlog)
c-----------------------------------------------------------------------
c plot omlog vs iter
c plot  nr of pomerons vs iter
c plot number of collisions vs iter
c-----------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'
      include 'epos.incsem'

      parameter(nbin=100)
      common/cmc/ot(0:nbin),zz(0:nbin),i(0:nbin)
     *,yt1,yt2,kx(0:nbin)
      parameter(nbim=100)
      common/cmc1/xp(0:nbim),xt(0:nbim),x(0:nbim),o(0:nbim)
     *,y1,y2,car
      character car*5
      double precision xp,xt,x,omlog,om1intbc
      character ce*8
      double precision plc,s,seedp
      common/cems5/plc,s

c      if(iemsi2.eq.0)call utstop('ERROR in XemsI1: iemsi2 = 0&')

       if(iii.eq.1)then

      o(kc)=sngl(omlog)
      nptk=0
      kollx=0
      do ko=1,koll
      nptk=nptk+nprt(ko)
c      if(itpr(ko).gt.0)then
      if(nprt(ko).gt.0)then
       kollx=kollx+1
      endif
      enddo
      zz(kc)=nptk
      kx(kc)=kollx

        elseif(iii.eq.2)then

      call ranfgt(seedp)
      sum=0
      kollx=0
      sumg=0
      kollg=0
      kollini=koll
      koll=1
      do ko=1,kollini
ctp060829       ip=iproj(ko)
ctp060829       it=itarg(ko)
       om1i=sngl(om1intbc(bk(ko)))
ctp060829         wk=1.
ctp060829         wp=0.
ctp060829         wt=0.
       om1g=sngl(om1intbc(bk(ko)))
       sum=sum+om1i
       sumg=sumg+om1g
       if(rangen().lt.1.-exp(-om1i))then
        kollx=kollx+1
       endif
       if(rangen().lt.1.-exp(-om1g))then
        kollg=kollg+1
       endif
      enddo
      koll=kollini
      call ranfst(seedp)

      x1=0
      x2=nbin
      write(ce,'(f8.2)')sngl(plc)

      write(ifhi,'(a)')       '!##################################'
      write(ifhi,'(a,i3)')    '!   log omega       for event ',nrevt+1
      write(ifhi,'(a)')       '!##################################'
      write(ifhi,'(a,i1)')    'openhisto name omega-',nrevt+1
      write(ifhi,'(a)')       'htyp lin'
      write(ifhi,'(a)')       'xmod lin ymod lin'
      write(ifhi,'(a,2e11.3)')'xrange',x1,x2
      write(ifhi,'(a)')       'yrange auto auto '
      write(ifhi,'(a)')    'text 0 0 "xaxis iteration"'
      write(ifhi,'(a)')    'text 0 0 "yaxis ln[W]"'
      write(ifhi,'(a,a)')  'text 0.5 0.90 "E ='//ce//'"'
      write(ifhi,'(a)')       'array 2'
         do k=0,nbim
      write(ifhi,'(2e11.3)')float(k),o(k)
         enddo
      write(ifhi,'(a)')    '  endarray'
      write(ifhi,'(a)')    'closehisto plot 0'

      write(ifhi,'(a)')   '!##################################'
      write(ifhi,'(a,i3)')'! nr of coll`s  for event ',nrevt+1
      write(ifhi,'(a)')   '!##################################'
      write(ifhi,'(a,i1)')    'openhisto name coll-',nrevt+1
      write(ifhi,'(a)')       'htyp lin'
      write(ifhi,'(a)')       'xmod lin ymod lin'
      write(ifhi,'(a,2e11.3)')'xrange',x1,x2
      write(ifhi,'(a)')    'text 0 0 "xaxis iteration"'
      write(ifhi,'(a)')    'text 0 0 "yaxis nr of collisions"'
      write(ifhi,'(a)')       'array 2'
         do k=0,nbin
      write(ifhi,'(2e11.3)')float(k),float(kx(k))
         enddo
      write(ifhi,'(a)')    '  endarray'
      write(ifhi,'(a)')    'closehisto plot 0-'
      write(ifhi,'(a)')       'openhisto'
      write(ifhi,'(a)')       'htyp lin'
      write(ifhi,'(a)')       'array 2'
         do k=0,nbin
      write(ifhi,'(2e11.3)')float(k),float(kollx)
         enddo
      write(ifhi,'(a)')    '  endarray'
      write(ifhi,'(a)')    'closehisto plot 0-'
      write(ifhi,'(a)')       'openhisto'
      write(ifhi,'(a)')       'htyp lin'
      write(ifhi,'(a)')       'array 2'
         do k=0,nbin
      write(ifhi,'(2e11.3)')float(k),float(kollg)
         enddo
      write(ifhi,'(a)')    '  endarray'
      write(ifhi,'(a)')    'closehisto plot 0'

      write(ifhi,'(a)')   '!##################################'
      write(ifhi,'(a,i3)')'! nr of pom`s  for event ',nrevt+1
      write(ifhi,'(a)')   '!##################################'
      write(ifhi,'(a,i1)')    'openhisto name pom-',nrevt+1
      write(ifhi,'(a)')       'htyp lin'
      write(ifhi,'(a)')       'xmod lin ymod lin'
      write(ifhi,'(a,2e11.3)')'xrange',x1,x2
      write(ifhi,'(a)')    'text 0 0 "xaxis iteration"'
      write(ifhi,'(a)')    'text 0 0 "yaxis nr of Pomerons"'
      write(ifhi,'(a)')       'array 2'
         do k=0,nbin
      write(ifhi,'(2e11.3)')float(k),zz(k)
         enddo
      write(ifhi,'(a)')    '  endarray'
      if(sum.lt.4*zz(nbin))then
      write(ifhi,'(a)')    'closehisto plot 0-'
      write(ifhi,'(a)')       'openhisto'
      write(ifhi,'(a)')       'htyp lin'
      write(ifhi,'(a)')       'array 2'
         do k=0,nbin
      write(ifhi,'(2e11.3)')float(k),sum
         enddo
      write(ifhi,'(a)')    '  endarray'
      write(ifhi,'(a)')    'closehisto plot 0-'
      write(ifhi,'(a)')       'openhisto'
      write(ifhi,'(a)')       'htyp lin'
      write(ifhi,'(a)')       'array 2'
         do k=0,nbin
      write(ifhi,'(2e11.3)')float(k),sumg
         enddo
      write(ifhi,'(a)')    '  endarray'
      endif
      write(ifhi,'(a)')    'closehisto plot 0'

        endif

      return
      end

c-----------------------------------------------------------------------
      subroutine xEmsI2(iii,kc)
c-----------------------------------------------------------------------
c plot quanities vs iter
c   plot 1: <x> for Pomeron vs iter
c   plot 2: <x> for projectile vs iter
c   plot 3: <x> for target vs iter
c arguments:
c   iii:   modus (1,2)
c   kc:    iteration step
c   omega: config probability
c-----------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'

      parameter(nbim=100)
      common/cmc1/xp(0:nbim),xt(0:nbim),x(0:nbim),o(0:nbim)
     *,y1,y2,car
      character car*5
      double precision xp,xt,x,xpo,xpj,xtg
      common/cemsi2/xpo,xpj,xtg

        if(iii.eq.1)then

      npom=0
      xpo=0
      do k=1,koll
c       ip=iproj(k)
c       it=itarg(k)
       if(nprmx(k).gt.0)then
        do n=1,nprmx(k)
         if(idpr(n,k).gt.0.and.ivpr(n,k).gt.0)then
          xpo=xpo+xpr(n,k)
          npom=npom+1
         endif
        enddo
       endif
      enddo
      if(npom.gt.0)xpo=xpo/npom

      npk=0
      xpj=0d0
      do i=1,maproj
       if(xpp(i).lt.0.999)then
        xpj=xpj+xpp(i)!*xmp(i)
        npk=npk+1
       endif
      enddo
      if(npk.gt.0)xpj=xpj/dble(npk)

      ntk=0
      xtg=0d0
      do j=1,matarg
       if(xmt(j).lt.0.999)then
        xtg=xtg+xmt(j)!*xpt(j)
        ntk=ntk+1
       endif
      enddo
      if(ntk.gt.0)xtg=xtg/dble(ntk)

      x(kc)=xpo
      xp(kc)=xpj
      xt(kc)=xtg

        elseif(iii.eq.2)then

      x1=0
      x2=nbim

      write(ifhi,'(a)')       '!##################################'
      write(ifhi,'(a,i3)')    '!   average x  Pom   for event ',nrevt+1
      write(ifhi,'(a)')       '!##################################'
      write(ifhi,'(a,i1)')    'openhisto name avxPom-',nrevt+1
      write(ifhi,'(a)')       'htyp lin'
      write(ifhi,'(a)')       'xmod lin ymod lin'
      write(ifhi,'(a,2e11.3)')'xrange',x1,x2
      write(ifhi,'(a)')    'text 0 0 "xaxis iteration"'
      write(ifhi,'(a)')    'text 0 0 "yaxis average x Pomeron"'
      write(ifhi,'(a)')       'array 2'
         do k=0,nbim
      write(ifhi,'(2e11.3)')float(k),x(k)
         enddo
      write(ifhi,'(a)')    '  endarray'
      write(ifhi,'(a)')    'closehisto plot 0'

      write(ifhi,'(a)')       '!##################################'
      write(ifhi,'(a,i3)')    '!   average x proj   for event ',nrevt+1
      write(ifhi,'(a)')       '!##################################'
      write(ifhi,'(a,i1)')    'openhisto name avxProj-',nrevt+1
      write(ifhi,'(a)')       'htyp lin'
      write(ifhi,'(a)')       'xmod lin ymod lin'
      write(ifhi,'(a,2e11.3)')'xrange',x1,x2
      write(ifhi,'(a)')    'text 0 0 "xaxis iteration"'
      write(ifhi,'(a)')    'text 0 0 "yaxis average x proj"'
      write(ifhi,'(a)')       'array 2'
         do k=0,nbim
      write(ifhi,'(2e11.3)')float(k),xp(k)
         enddo
      write(ifhi,'(a)')    '  endarray'
      write(ifhi,'(a)')    'closehisto plot 0'

      write(ifhi,'(a)')       '!##################################'
      write(ifhi,'(a,i3)')    '!   average x targ   for event ',nrevt+1
      write(ifhi,'(a)')       '!##################################'
      write(ifhi,'(a,i1)')    'openhisto name avxTarg-',nrevt+1
      write(ifhi,'(a)')       'htyp lin'
      write(ifhi,'(a)')       'xmod lin ymod lin'
      write(ifhi,'(a,2e11.3)')'xrange',x1,x2
      write(ifhi,'(a)')    'text 0 0 "xaxis iteration"'
      write(ifhi,'(a)')    'text 0 0 "yaxis average x targ"'
      write(ifhi,'(a)')       'array 2'
         do k=0,nbim
      write(ifhi,'(2e11.3)')float(k),xt(k)
         enddo
      write(ifhi,'(a)')    '  endarray'
      write(ifhi,'(a)')    'closehisto plot 0'
        endif

      return
      end

c-----------------------------------------------------------------------
      subroutine xEmsRx(iii,id,xp,xm)
c-----------------------------------------------------------------------
c plot  x+, x-, x, y distribution of remnants
c-----------------------------------------------------------------------

      include 'epos.inc'

      parameter(nbix=50,nbiy=50,nid=2)
      common/cxp/nxp(nid),nxm(nid),nx(nid),ny(nid)
     *,wxp(nbix,nid),wxm(nbix,nid),wx(nbix,nid),wy(nbiy,nid)
     *,xpu,xpo,xmu,xmo,xu,xo,yu,yo,dy

      if(iemsrx.eq.0)call utstop('ERROR in XemsRx: iemsrx = 0&')

        if(iii.eq.0)then

      xpu=10/engy**2
      xpo=1
      xmu=10/engy**2
      xmo=1
      xu=10/engy**2
      xo=1
      yu=-alog(engy**2)
      yo=alog(engy**2)
      dy=(yo-yu)/nbiy
      do j=1,nid
       nxp(j)=0
       nxm(j)=0
       nx(j)=0
       do i=1,nbix
        wxp(i,j)=0
        wxm(i,j)=0
        wx(i,j)=0
       enddo
       ny(j)=0
       do i=1,nbiy
        wy(i,j)=0
       enddo
      enddo

        elseif(iii.eq.1)then

      i=0
      if(xp.lt.xpu)goto1
      i=1+int(alog(xp/xpu)/alog(xpo/xpu)*nbix)
      if(i.gt.nbix)goto1
      if(i.lt.1)goto1
      wxp(i,id)=wxp(i,id)+1
      nxp(id)=nxp(id)+1
1     continue

      if(xm.lt.xmu)goto2
      i=1+int(alog(xm/xmu)/alog(xmo/xmu)*nbix)
      if(i.gt.nbix)goto2
      if(i.lt.1)goto2
      wxm(i,id)=wxm(i,id)+1
      nxm(id)=nxm(id)+1
2     continue

      x=xp*xm
      if(x.lt.xu)goto3
      i=1+int(alog(x/xu)/alog(xo/xu)*nbix)
      if(i.gt.nbix)goto3
      if(i.lt.1)goto3
      wx(i,id)=wx(i,id)+1
      nx(id)=nx(id)+1
3     continue

      if(xm.le.0.)goto4
      if(xp.le.0.)goto4
      y=0.5*alog(xp/xm)
      if(y.lt.yu)goto4
      i=int((y-yu)/dy)+1
      if(i.gt.nbiy)goto4
      if(i.lt.1)goto4
      wy(i,id)=wy(i,id)+1
      ny(id)=ny(id)+1
4     continue

        elseif(iii.eq.2)then

      do j=1,nid
      if(j.eq.1)then
        iclrem=iclpro
      elseif(j.eq.2)then
        iclrem=icltar
      else
        iclrem=0
      endif
      write(ifhi,'(a)')      '!----------------------------------'
      write(ifhi,'(a)')      '!   remnant xp distribution      '
      write(ifhi,'(a)')      '!----------------------------------'
      write(ifhi,'(a,i1)')    'openhisto name xpRemnant-',j
      write(ifhi,'(a)')       'htyp lin'
      write(ifhi,'(a)')       'xmod log ymod log'
      write(ifhi,'(a,2e11.3)')'xrange',xpu,xpo
      write(ifhi,'(a)')    'text 0 0 "xaxis remnant x+"'
      write(ifhi,'(a)')    'text 0 0 "yaxis P(x+)"'
      write(ifhi,'(a)')       'array 2'
         do i=1,nbix
      x=xpu*(xpo/xpu)**((i-0.5)/nbix)
      dx=xpu*(xpo/xpu)**(1.*i/nbix)*(1.-(xpo/xpu)**(-1./nbix))
      if(nxp(j).ne.0)write(ifhi,'(2e11.3)')x,wxp(i,j)/dx/nxp(j)
      if(nxp(j).eq.0)write(ifhi,'(2e11.3)')x,0.
         enddo
      write(ifhi,'(a)')    '  endarray'
      write(ifhi,'(a)')    'closehisto plot 0-'
      write(ifhi,'(a)')       'openhisto'
      write(ifhi,'(a)')       'htyp lin'
      write(ifhi,'(a)')       'array 2'
         do i=1,nbix
      x=xu*(xo/xu)**((i-0.5)/nbix)
      write(ifhi,'(2e11.3)')x,x**alplea(iclrem)*(1+alplea(iclrem))
         enddo
      write(ifhi,'(a)')    '  endarray'
      write(ifhi,'(a)')    'closehisto plot 0'

      write(ifhi,'(a)')      '!----------------------------------'
      write(ifhi,'(a)')      '!   remnant xm distribution      '
      write(ifhi,'(a)')      '!----------------------------------'
      write(ifhi,'(a,i1)')    'openhisto name xmRemnant-',j
      write(ifhi,'(a)')       'htyp lin'
      write(ifhi,'(a)')       'xmod log ymod log'
      write(ifhi,'(a,2e11.3)')'xrange',xmu,xmo
      write(ifhi,'(a)')    'text 0 0 "xaxis remnant x-"'
      write(ifhi,'(a)')    'text 0 0 "yaxis P(x-)"'
      write(ifhi,'(a)')       'array 2'
         do i=1,nbix
      x=xmu*(xmo/xmu)**((i-0.5)/nbix)
      dx=xmu*(xmo/xmu)**(1.*i/nbix)*(1.-(xmo/xmu)**(-1./nbix))
      if(nxm(j).ne.0)write(ifhi,'(2e11.3)')x,wxm(i,j)/dx/nxm(j)
      if(nxm(j).eq.0)write(ifhi,'(2e11.3)')x,0.
         enddo
      write(ifhi,'(a)')    '  endarray'
      write(ifhi,'(a)')    'closehisto plot 0'

      write(ifhi,'(a)')      '!----------------------------------'
      write(ifhi,'(a)')      '!   remnant x distribution      '
      write(ifhi,'(a)')      '!----------------------------------'
      write(ifhi,'(a,i1)')    'openhisto name xRemnant-',j
      write(ifhi,'(a)')       'htyp lin'
      write(ifhi,'(a)')       'xmod log ymod log'
      write(ifhi,'(a,2e11.3)')'xrange',xu,xo
      write(ifhi,'(a)')    'text 0 0 "xaxis remnant x"'
      write(ifhi,'(a)')    'text 0 0 "yaxis P(x)"'
      write(ifhi,'(a)')       'array 2'
         do i=1,nbix
      x=xu*(xo/xu)**((i-0.5)/nbix)
      dx=xu*(xo/xu)**(1.*i/nbix)*(1.-(xo/xu)**(-1./nbix))
      if(nx(j).ne.0)write(ifhi,'(2e11.3)')x,wx(i,j)/dx/nx(j)
      if(nx(j).eq.0)write(ifhi,'(2e11.3)')x,0.
         enddo
      write(ifhi,'(a)')    '  endarray'
      write(ifhi,'(a)')    'closehisto plot 0'

      write(ifhi,'(a)')      '!----------------------------------'
      write(ifhi,'(a)')      '!   remnant y distribution      '
      write(ifhi,'(a)')      '!----------------------------------'
      write(ifhi,'(a,i1)')    'openhisto name yRemnant-',j
      write(ifhi,'(a)')       'htyp lin'
      write(ifhi,'(a)')       'xmod lin ymod log'
      write(ifhi,'(a,2e11.3)')'xrange',yu,yo
      write(ifhi,'(a)')    'text 0 0 "xaxis remnant y"'
      write(ifhi,'(a)')    'text 0 0 "yaxis P(y)"'
      write(ifhi,'(a)')       'array 2'
         do i=1,nbix
      y=yu+dy/2.+(i-1)*dy
      if(ny(j).ne.0)write(ifhi,'(2e11.3)')y,wy(i,j)/dy/ny(j)
      if(ny(j).eq.0)write(ifhi,'(2e11.3)')y,0.
         enddo
      write(ifhi,'(a)')    '  endarray'
      write(ifhi,'(a)')    'closehisto plot 0'

      enddo

        endif

      return
      end

c-----------------------------------------------------------------------
      subroutine xEmsPm(iii,ko,nmci,nmcmx)
c-----------------------------------------------------------------------
c m (pomeron number) distribution for different b-bins.
c arguments:
c   iii:  modus (0,1,2)
c   ko:   pair number (1 - AB)
c   nmc:  number of pomerons
c   nmcmx: number max of pomerons
c-----------------------------------------------------------------------
      include 'epos.inc'
      include 'epos.incems'
      common/geom/rmproj,rmtarg,bmax,bkmx
      parameter(nbin=200)
      parameter(nbib=32)
      common/cn/wn(0:nbin,nbib),wnmc(0:nbin,nbib),npmx(nbib),nn(nbib)
     &         ,nn2(nbib),dn(nbib)
      common/cb1/db,b1,b2,bb(nbib),nbibx
      double precision plc,s,om1intbc
      character ce*8,cb*4
      common/cems5/plc,s
      common/cemspm/sumb(nbib)

      if(iemspm.eq.0)call utstop('ERROR in XemsPm: iemspm = 0&')

        if(iii.eq.0)then

      do k=1,nbib
       nn(k)=0
       nn2(k)=0
       sumb(k)=0
       do i=0,nbin
        wnmc(i,k)=0
       enddo
      enddo
      nbibx=6
      b1=0
      b2=2
      db=(b2-b1)/nbibx


        elseif(iii.eq.1)then

      k=int((bk(ko)-b1)/db)+1
c      nmc=nmci
      if(k.gt.nbibx)k=nbibx
      if(k.lt.1)k=1
      dn(k)=max(1.,float(nmcmx)/float(nbin))
      nmc=nint(float(nmci)/dn(k)+0.499999)
      if(nmc.gt.nbin)nmc=nbin
      if(nmc.lt.0)return
      nn(k)=nn(k)+1
      wnmc(nmc,k)=wnmc(nmc,k)+1./dn(k)
      sumb(k)=sumb(k)+bk(ko)


        elseif(iii.eq.2)then

      kollini=koll
      koll=1         !to have screening for pp

      do 1 k=1,nbibx

       bb(k)=b1+(k-0.5)*db
       if(maproj.eq.1.and.matarg.eq.1.and.bmaxim.eq.0.)bb(k)=b1
       om1i=sngl(om1intbc(bb(k)))
       wntmp=0.
       do 10 i=0,nbin
         wn(i,k)=0.
         if(wntmp.gt.1e5)goto 10
         do j=i,i+int(dn(k))-1
           if(j.eq.0)then
             wntmp=exp(-om1i)
           else
             wntmp=wntmp*om1i/j
           endif
           wn(i,k)=wn(i,k)+wntmp/dn(k)
         enddo
         if(wn(i,k).gt.0.000001*(1.-exp(-om1i)))npmx(k)=i
 10    continue

      write(ifhi,'(a)')   '!##################################'
      write(ifhi,'(a)')   '! distr of Pomeron number vs b'
      write(ifhi,'(a)')   '!##################################'
      write(ce,'(f8.2)')sngl(plc)
      write(cb,'(f4.2)')bb(k)
      if(nn(k).gt.0)then
      write(ifhi,'(a,i1)')    'openhisto name mPom-',k
      write(ifhi,'(a)')       'htyp lru'
      write(ifhi,'(a)')       'xmod lin ymod log'
      write(ifhi,'(a,2e11.3)')'xrange',0.,float(npmx(k))*dn(k)
      write(ifhi,'(a)')    'text 0 0 "xaxis number m of Pomerons"'
      write(ifhi,'(a)')    'text 0 0 "yaxis prob(m)"'
      if(k.eq.1)
     *write(ifhi,'(a,a)')     'text 0.5 0.90 "E ='//ce//'"'
      write(ifhi,'(a,a)')     'text 0.5 0.80 "b ='//cb//'"'
      write(ifhi,'(a)')       'array 2'
         do i=0,nbin
      write(ifhi,'(2e11.3)')float(i)*dn(k),wnmc(i,k)/max(1,nn(k))
         enddo
      write(ifhi,'(a)')    '  endarray'
      write(ifhi,'(a)')    'closehisto plot 0-'
      endif

      write(ifhi,'(a)')   '!##################################'
      write(ifhi,'(a)')   '! distr of Pomeron number vs b'
      write(ifhi,'(a)')   '!   traditional approach'
      write(ifhi,'(a)')   '!##################################'
      write(ifhi,'(a,i1)')    'openhisto name mPomTradi-',k
      write(ifhi,'(a)')       'htyp lba'
      write(ifhi,'(a)')       'xmod lin ymod log'
      write(ifhi,'(a,2e11.3)')'xrange',0.,float(npmx(k))*dn(k)
      write(ifhi,'(a)')       'array 2'
         do i=0,nbin
      write(ifhi,'(2e11.3)')float(i)*dn(k),wn(i,k)
         enddo
      write(ifhi,'(a)')    '  endarray'
      write(ifhi,'(a)')    'closehisto plot 0'

 1    continue

      koll=kollini

      endif

      return
      end

c-----------------------------------------------------------------------
      subroutine xEmsB(iii,jjj,ko)
c-----------------------------------------------------------------------
c b distribution at different stages
c arguments:
c   iii:  modus (0,1,2)
c   jjj:  stage or type of interaction
c     just after Metropolis:
c           1 ... all
c           2 ... interaction
c     after defining diffraction:
c           3 ... nothing
c           4 ... cut
c           5 ... diffr
c           6 ... cut + diffr cut
c   ko:   pair number (1 - AB)
c-----------------------------------------------------------------------
      include 'epos.inc'
      include 'epos.incems'
      include 'epos.incsem'
      parameter(njjj=6)
      parameter(nbib=32)
      common/cxemsb1/w(0:njjj,nbib),nn(njjj)
      common/cxemsb2/db,b1,b2
      common/cxemsb3/njjj1
      double precision PhiExact,om1intbi,PhiExpo!,PhiUnit
      common/geom/rmproj,rmtarg,bmax,bkmx
      dimension uua2(nbib),uuo2(nbib),uu3(nbib)

      if(iemsb.eq.0)call utstop('ERROR in XemsB: iemsB = 0&')

        if(iii.eq.0)then

      do k=1,nbib
       do j=0,njjj
        w(j,k)=0
       enddo
      enddo
      do j=1,njjj
       nn(j)=0
      enddo
      njjj1=0

        elseif(iii.eq.1)then

      b1=0
      b2=bkmx*1.2
      db=(b2-b1)/nbib
      k=int((bk(ko)-b1)/db)+1
      if(k.gt.nbib)return
      if(k.lt.1)return
      w(jjj,k)=w(jjj,k)+1
      nn(jjj)=nn(jjj)+1
      if(jjj.eq.1)njjj1=1

        elseif(iii.eq.2)then

      if(njjj1.ne.1)call utstop
     &('xEmsB must be called also with jjj=1&')
      ymax=0
      kollini=koll
      koll=1
      do k=1,nbib
       x=b1+(k-0.5)*db
       y=w(1,k)/nn(1)/(pi*((x+0.5*db)**2-(x-0.5*db)**2))
       ymax=max(ymax,y)
      enddo
      fk=bkmx**2*pi
      ymax=1.4

      do 1 j=1,njjj
       if(nn(j).eq.0)goto1

      write(ifhi,'(a)')   '!##################################'
      write(ifhi,'(a)')   '! b distr exact theory '
      write(ifhi,'(a)')   '!##################################'
         if(j.ge.2.and.j.le.6)then
      write(ifhi,'(a,i1,a)')  'openhisto name b',j,'Exact'
      write(ifhi,'(a)')       'htyp lba xmod lin ymod lin'
      write(ifhi,'(a)')    'text 0 0 "xaxis impact parameter b"'
      write(ifhi,'(a)')    'text 0 0 "yaxis P(b)"'
      write(ifhi,'(a)')       'array 2'
         do k=1,nbib
      b=b1+(k-0.5)*db
      if(j.eq.2)then
        uuo2(k)=sngl(PhiExpo(0.,0.,1.,1.d0,1.d0,engy**2,b))
        uua2(k)=min(uuo2(k),max(0.,
     &          sngl(Phiexact(0.,0.,1.,1.d0,1.d0,engy**2,b))))
        uu3(k)=sngl(min(50d0,exp(om1intbi(b,2)/dble(r2hads(iclpro)
     &                                             +r2hads(icltar)))))
      endif
      if(j.eq.2)y=(1.-uua2(k))
      if(j.eq.3)y=uua2(k)
      if(j.eq.4.or.j.eq.6)y=(1.-uua2(k)*uu3(k))
      if(j.eq.5)y=uua2(k)*(uu3(k)-1.)
      write(ifhi,'(2e11.3)')b,y
         enddo
      write(ifhi,'(a)')    '  endarray'
      write(ifhi,'(a)')    'closehisto plot 0-'
         endif
      write(ifhi,'(a)')   '!##################################'
      write(ifhi,'(a)')   '! b distr unitarized theory '
      write(ifhi,'(a)')   '!##################################'
      write(ifhi,'(a,i1,a)')  'openhisto name b',j,'Unit'
      write(ifhi,'(a)')       'htyp lbf xmod lin ymod lin'
      write(ifhi,'(a)')    'text 0 0 "xaxis impact parameter b"'
      write(ifhi,'(a)')    'text 0 0 "yaxis P(b)"'
      write(ifhi,'(a)')       'array 2'
         do k=1,nbib
      b=b1+(k-0.5)*db
      if(j.eq.1)y=1
      if(j.eq.2)y=(1.-uuo2(k))
      if(j.eq.3)y=uuo2(k)
      if(j.eq.4.or.j.eq.6)y=(1.-uuo2(k)*uu3(k))
      if(j.eq.5)y=uuo2(k)*(uu3(k)-1.)
      write(ifhi,'(2e11.3)')b,y
         enddo
      write(ifhi,'(a)')    '  endarray'
      write(ifhi,'(a)')    'closehisto plot 0-'
      write(ifhi,'(a)')   '!##################################'
      write(ifhi,'(a)')   '! b distr for cross section '
      write(ifhi,'(a)')   '!##################################'
      write(ifhi,'(a,i1,a)')  'openhisto name b',j,'Unit'
      write(ifhi,'(a)')       'htyp lge xmod lin ymod lin'
      write(ifhi,'(a)')    'text 0 0 "xaxis impact parameter b"'
      write(ifhi,'(a)')    'text 0 0 "yaxis P(b)"'
      write(ifhi,'(a)')       'array 2'
         do k=1,nbib
      b=b1+(k-0.5)*db
      if(j.eq.1)y=1
      if(j.eq.2)y=(1.-(uuo2(k)+uua2(k))*0.5)
      if(j.eq.3)y=(uuo2(k)+uua2(k))*0.5
      if(j.eq.4.or.j.eq.6)y=(1.-(uuo2(k)+uua2(k))*0.5*uu3(k))
      if(j.eq.5)y=(uuo2(k)+uua2(k))*0.5*(uu3(k)-1.)
      write(ifhi,'(2e11.3)')b,y
         enddo
      write(ifhi,'(a)')    '  endarray'
      write(ifhi,'(a)')    'closehisto plot 0-'
      write(ifhi,'(a)')   '!##################################'
      write(ifhi,'(a)')   '! b distribution simulation'
      write(ifhi,'(a)')   '!##################################'
      write(ifhi,'(a,i1,a)')  'openhisto name b',j,'Simu'
      write(ifhi,'(a)')       'htyp lrf xmod lin ymod lin'
      write(ifhi,'(a,2e11.3)')'xrange',0.0,b2
      write(ifhi,'(a,2e11.3)')'yrange',0.,ymax
      write(ifhi,'(a)')    'text 0 0 "xaxis impact parameter b"'
      write(ifhi,'(a)')    'text 0 0 "yaxis P(b)"'
      if(j.eq.1)write(ifhi,'(a)')'text 0.1 0.35 "after Metropolis"'
      if(j.eq.1)write(ifhi,'(a)')'text 0.2 0.20 "all "'
      if(j.eq.2)write(ifhi,'(a)')'text 0.3 0.85 "after Metropolis"'
      if(j.eq.2)write(ifhi,'(a)')'text 0.5 0.70 "interaction "'
      if(j.eq.3)write(ifhi,'(a)')'text 0.3 0.85 "nothing"'
      if(j.eq.4)write(ifhi,'(a)')'text 0.3 0.85 "cut"'
      if(j.eq.5)write(ifhi,'(a)')'text 0.3 0.85 "diffr"'
      if(j.eq.6)write(ifhi,'(a)')'text 0.3 0.85 "cut + diffr cut"'
      write(ifhi,'(a)')       'array 2'
         do k=1,nbib
      x=b1+(k-0.5)*db
      if(j.eq.1)y=fk*w(j,k)/nn(1)/(pi*((x+0.5*db)**2-(x-0.5*db)**2))
      if(j.ne.1)y=0.
      if(j.ne.1.and.w(1,k).ne.0.)y=w(j,k)/w(1,k)
      if(nn(j).gt.0)write(ifhi,'(2e11.3)')x,y
         enddo
      write(ifhi,'(a)')    '  endarray'
      write(ifhi,'(a)')    'closehisto plot 0'

   1  continue

      koll=kollini

      endif

      return
      end

c-----------------------------------------------------------------------
      subroutine xEmsBg(iii,jjj,ko)
c-----------------------------------------------------------------------
c b distribution at different stages for different group
c arguments:
c   iii:  modus (0,1,2,3)
c   jjj:  group of interaction (1,2 ... ,7)
c   ko:   pair number (1 - AB)
c-----------------------------------------------------------------------
      include 'epos.inc'
      include 'epos.incems'
      parameter(njjj=7)
      parameter(nbib=16)
      common/cxemsb4/wg(-1:njjj,nbib),nng(nbib),uug(nbib),kollx
      common/cxemsb5/dbg,b1g,b2g
      common/cxemsb6/njjj0
      double precision seedp,PhiExpo!,PhiExact
      common/geom/rmproj,rmtarg,bmax,bkmx

      if(iemsbg.eq.0)call utstop('ERROR in XemsBg: iemsbg = 0&')

        if(iii.eq.0)then

      do k=1,nbib
       nng(k)=0
       do j=-1,njjj
        wg(j,k)=0
       enddo
      enddo
      njjj0=0
      kollx=0

        elseif(iii.eq.1)then

      b1g=0
      b2g=bkmx*1.2
      dbg=(b2g-b1g)/nbib
      k=int((bk(ko)-b1g)/dbg)+1
      if(k.gt.nbib)return
      if(k.lt.1)return
      if(jjj.eq.-1.or.jjj.eq.0)then
        wg(jjj,k)=wg(jjj,k)+1
      else
        wg(jjj,k)=wg(jjj,k)+1
        nng(k)=nng(k)+1
      endif
      if(jjj.eq.0)njjj0=1

        elseif(iii.eq.3)then

          call ranfgt(seedp)
          do k=1,koll
            om1i=sngl(om1intc(k))
            if(rangen().lt.1.-exp(-om1i))then
c            om1i=sngl(PhiExpo(0.,0.,1.,1.d0,1.d0,engy*engy,bk(k)))
c            if(rangen().lt.1.-om1i)then
              kollx=kollx+1
            endif
          enddo
          call ranfst(seedp)

        elseif(iii.eq.2)then

      if(njjj0.ne.1)call utstop
     &('xEmsBg must be called also with jjj=0&')
      ymax=1.4
      kollini=koll
      koll=1

      wtot=1.
      if(matarg+maproj.gt.2)then
      wtot=0.
      do k=1,nbib
       wtot=wtot+wg(-1,k)
      enddo
      if(kollx.gt.0)wtot=wtot/float(kollx)
      endif

      do 1 j=1,njjj

      write(ifhi,'(a)')   '!##################################'
      write(ifhi,'(a)')   '! b distribution simulation'
      write(ifhi,'(a)')   '!##################################'
      write(ifhi,'(a,i1,a)')  'openhisto name bg',j,'Simu'
      write(ifhi,'(a)')       'htyp lin xmod lin ymod lin'
      write(ifhi,'(a,2e11.3)')'xrange',0.,b2g
      write(ifhi,'(a,2e11.3)')'yrange',0.,ymax
      write(ifhi,'(a)')    'text 0 0 "xaxis impact parameter b"'
      write(ifhi,'(a)')    'text 0 0 "yaxis P(b)"'
      if(wtot.gt.0.d0)
     &write(ifhi,'(a,f7.4,a)')    'text 0.5 0.8 "alpha=',1./wtot,'"'
      write(ifhi,'(a)')       'array 2'
         do k=1,nbib
      b=b1g+(k-0.5)*dbg
      y=0.
      if(nng(k).ne.0.and.wg(0,k).ne.0)
     &              y=wg(j,k)/float(nng(k))*wg(-1,k)/wg(0,k)!/wtot
c      if(wg(0,k).ne.0..and.nng(k).ne.0)y=wg(j,k)/nng(k)*wg(-1,k)/wg(0,k)
c!???????????? better normalization ? probability to have an interaction
c in epos compared to eikonal probability, instead of normalized by the
c probability of a collision for a pair (the number collision/number
c active pair).
      uug(k)=uug(k)+y
      write(ifhi,'(2e11.3)')b,y
         enddo
      write(ifhi,'(a)')    '  endarray'
      write(ifhi,'(a)')    'closehisto plot 0-'
   1  continue
      write(ifhi,'(a)')   '!##################################'
      write(ifhi,'(a)')   '! b distr tot simul theory '
      write(ifhi,'(a)')   '!##################################'
      write(ifhi,'(a)')  'openhisto name btotSimu'
      write(ifhi,'(a)')       'htyp pfc xmod lin ymod lin'
      write(ifhi,'(a)')    'text 0 0 "xaxis impact parameter b"'
      write(ifhi,'(a)')    'text 0 0 "yaxis P(b)"'
      write(ifhi,'(a)')       'array 2'
         do k=1,nbib
      b=b1g+(k-0.5)*dbg
      write(ifhi,'(2e11.3)')b,uug(k)
         enddo
      write(ifhi,'(a)')    '  endarray'
      write(ifhi,'(a)')    'closehisto plot 0-'
      write(ifhi,'(a)')   '!##################################'
      write(ifhi,'(a)')   '! b distr unitarized theory '
      write(ifhi,'(a)')   '!##################################'
      write(ifhi,'(a,i1,a)')  'openhisto name bg',j,'Unit'
      write(ifhi,'(a)')       'htyp lba xmod lin ymod lin'
      write(ifhi,'(a)')    'text 0 0 "xaxis impact parameter b"'
      write(ifhi,'(a)')    'text 0 0 "yaxis P(b)"'
      write(ifhi,'(a)')       'array 2'
         do k=1,nbib
      b=b1g+(k-0.5)*dbg
c      a1=Phiexact(0.,0.,1.,1.d0,1.d0,engy**2,b)
       a1=sngl(PhiExpo(0.,0.,1.,1.d0,1.d0,engy**2,b))
      y=(1.-a1)
      write(ifhi,'(2e11.3)')b,y
         enddo
      write(ifhi,'(a)')    '  endarray'
      write(ifhi,'(a)')    'closehisto plot 0'

      koll=kollini

      endif

      return
      end

c-----------------------------------------------------------------------
      subroutine xEmsPx(iii,xmc,ymc,npos)
c-----------------------------------------------------------------------
c plot  x-distribution and y-distribution of Pomerons
c-----------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incems'
      common/geom/rmproj,rmtarg,bmax,bkmx

      parameter(nbix=30,nbib=51)
      common/cx/x(2,nbix),dx(2,nbix),wxmc(2,nbix),wxmcI(2,nbix)
     * ,xl(2,nbix),dxl(2,nbix),wxp(2,nbix),wxm(2,nbix),wxpI(2,nbix)
     *,wxmI(2,nbix),wxpY(2,nbix),wxmY(2,nbix),wxmcY(2,nbix)
      parameter(nbiy=50)
      common/cy/y(nbiy),wymc(nbiy),wymcY(nbiy),wymcI(nbiy),nyp,nym
      double precision PomIncXExact,PomIncPExact,PomIncMExact,dcel
      double precision PomIncXIExact,PomIncPIExact,PomIncMIExact
      common/ems3/dcel,ad
      common/cemspx/xu,xo,yu,yo,dy,xlu,xlo,bb,nn,db,mm,nm,nt
      character mod*5, imod*5, txtxm*6

      nposi=5

      if(iemspx.eq.0)call utstop('ERROR in XemsPx: iemspx = 0&')

      if(iii.eq.0)then

       xu=0.1/engy**2
       xo=1.
       xlu=0.01/engy
       xlo=1.
       yu=-alog(engy**2)
       yo=alog(engy**2)
       dy=(yo-yu)/nbiy
        do i=1,nbix
        x(1,i)=xu*(xo/xu)**((i-0.5)/nbix)
        x(2,i)=xu+(xo-xu)*((i-0.5)/nbix)
        dx(1,i)=xu*(xo/xu)**(1.*i/nbix)*(1.-(xo/xu)**(-1./nbix))
        dx(2,i)=(xo-xu)/nbix
        wxmc(1,i)=0.
        wxmc(2,i)=0.
        wxmcI(1,i)=0.
        wxmcI(2,i)=0.
        wxmcY(1,i)=0.
        wxmcY(2,i)=0.
       enddo
       do i=1,nbix
        xl(1,i)=xlu*(xlo/xlu)**((i-0.5)/nbix)
        xl(2,i)=xlu+(xlo-xlu)*((i-0.5)/nbix)
        dxl(1,i)=xlu*(xlo/xlu)**(1.*i/nbix)*(1.-(xlo/xlu)**(-1./nbix))
        dxl(2,i)=(xlo-xlu)/nbix
        wxp(1,i)=0.
        wxp(2,i)=0.
        wxm(1,i)=0.
        wxm(2,i)=0.
        wxpI(1,i)=0.
        wxpI(2,i)=0.
        wxmI(1,i)=0.
        wxmI(2,i)=0.
        wxpY(1,i)=0.
        wxpY(2,i)=0.
        wxmY(1,i)=0.
        wxmY(2,i)=0.
       enddo
       do i=1,nbiy
        y(i)=yu+dy/2.+float(i-1)*dy
        wymc(i)=0.
        wymcI(i)=0.
        wymcY(i)=0.
       enddo
       mm=0
       nt=0
       nyp=0
       nym=0
       db=bkmx*2./float(nbib-1)

      elseif(iii.eq.1)then

       xp=sqrt(xmc)*exp(ymc)
       xm=sqrt(xmc)*exp(-ymc)
       mm=mm+1

       if(xmc.lt.xu)goto11
       i=1+int(alog(xmc/xu)/alog(xo/xu)*nbix)
       if(i.gt.nbix)goto1
       if(i.lt.1)goto1
       wxmc(1,i)=wxmc(1,i)+1.
       if(npos.eq.1)    wxmcI(1,i)=wxmcI(1,i)+1.
       if(npos.eq.nposi)wxmcY(1,i)=wxmcY(1,i)+1.
1      continue
       i=1+int((xmc-xu)/(xo-xu)*nbix)
       if(i.gt.nbix)goto11
       if(i.lt.1)goto11
       wxmc(2,i)=wxmc(2,i)+1.
       if(npos.eq.1)    wxmcI(2,i)=wxmcI(2,i)+1.
       if(npos.eq.nposi)wxmcY(2,i)=wxmcY(2,i)+1.
11     continue

       if(xp.lt.xlu)goto12
       i=1+int(alog(xp/xlu)/alog(xlo/xlu)*nbix)
       if(i.gt.nbix)goto2
       if(i.lt.1)goto2
       wxp(1,i)=wxp(1,i)+1.
       if(npos.eq.1)    wxpI(1,i)=wxpI(1,i)+1.
       if(npos.eq.nposi)wxpY(1,i)=wxpY(1,i)+1.
2      continue
       i=1+int((xp-xlu)/(xlo-xlu)*nbix)
       if(i.gt.nbix)goto12
       if(i.lt.1)goto12
       wxp(2,i)=wxp(2,i)+1.
       if(npos.eq.1)    wxpI(2,i)=wxpI(2,i)+1.
       if(npos.eq.nposi)wxpY(2,i)=wxpY(2,i)+1.
12     continue

       if(xm.lt.xlu)goto13
       i=1+int(alog(xm/xlu)/alog(xlo/xlu)*nbix)
       if(i.gt.nbix)goto3
       if(i.lt.1)goto3
       wxm(1,i)=wxm(1,i)+1.
       if(npos.eq.1)    wxmI(1,i)=wxmI(1,i)+1.
       if(npos.eq.nposi)wxmY(1,i)=wxmY(1,i)+1.
3      continue
       i=1+int((xm-xlu)/(xlo-xlu)*nbix)
       if(i.gt.nbix)goto13
       if(i.lt.1)goto13
       wxm(2,i)=wxm(2,i)+1.
       if(npos.eq.1)    wxmI(2,i)=wxmI(2,i)+1.
       if(npos.eq.nposi)wxmY(2,i)=wxmY(2,i)+1.
13     continue

       if(ymc.lt.yu)return
       i=int((ymc-yu)/dy)+1
       if(i.gt.nbiy)return
       if(i.lt.1)return
       wymc(i)=wymc(i)+1
       if(npos.eq.1)    wymcI(i)=wymcI(i)+1
       if(npos.eq.nposi)wymcY(i)=wymcY(i)+1
       if(ymc.gt.0)nyp=nyp+1
       if(ymc.lt.0)nym=nym+1

      elseif(iii.eq.2)then

       if(maproj.eq.1.and.matarg.eq.1.and.bminim.eq.bmaxim)then
        mmmm=1
        bb=bmaxim
        ff=float(nrevt)/float(ntevt)
        imod='   dn'
       elseif(maproj.eq.1.and.matarg.eq.1)then
        mmmm=3
        ff=1.
        imod='   dn'
       elseif(bminim.lt.0.001.and.bmaxim.gt.20)then
        mmmm=2
        area=pi*(rmproj+rmtarg)**2
        ff=area*float(nrevt)/float(ntevt)/(maproj*matarg)/sigine*10
        imod='   dn'
       else
        write(ifmt,*)'xEmsPx ignored'
        return
       endif
       kollini=koll
       koll=1

       kk1=nint(xpar1)
       kk2=nint(xpar2)

       do kk=kk1,kk2

       if(kk.eq.1)mod=' log '
       if(kk.eq.2)mod=' lin '

       write(ifhi,'(a)')       '!----------------------------------'
       write(ifhi,'(a)')       '!   Pomeron x distribution    '//mod
       write(ifhi,'(a)')       '!----------------------------------'

       write(ifhi,'(a)')  'openhisto name xPomSimuL'//mod(3:4)
       write(ifhi,'(a)')  'htyp lru xmod'//mod//'ymod log'
       write(ifhi,'(a,2e11.3)')'xrange',xu,xo
       write(ifhi,'(a)')    'text 0 0 "xaxis x?PE!"'
       write(ifhi,'(a)') 'text 0 0 "yaxis'//imod//'?Pom! / dx?PE!"'
       if(kk.eq.1)write(ifhi,'(a,f5.2,a)')'text 0.1 0.3 "f=',ff,'"'
       if(kk.eq.2)write(ifhi,'(a,f5.2,a)')'text 0.1 0.1 "f=',ff,'"'
       write(ifhi,'(a)')       'array 2'
       s1=0
       do i=1,nbix
       u=x(kk,i)
       z=ff*wxmc(kk,i)/dx(kk,i)/nrevt
       s1=s1+z*dx(kk,i)
        write(ifhi,'(2e11.3)')u,z
       enddo
       write(ifhi,'(a)')    '  endarray'
       write(ifhi,'(a)')    'closehisto plot 0-'

       write(ifhi,'(a)')       'openhisto name xPomUnitL'//mod(3:4)
       write(ifhi,'(a)')  'htyp lba xmod'//mod//'ymod log'
       write(ifhi,'(a,2e11.3)')'xrange',xu,xo
       write(ifhi,'(a)')    'text 0 0 "xaxis x?PE!"'
       write(ifhi,'(a)') 'text 0 0 "yaxis'//imod//'?Pom! / dx?PE!"'
       write(ifhi,'(a)')       'array 2'
       s2=0
       do i=1,nbix
        u=x(kk,i)
        if(mmmm.eq.1)z=sngl(PomIncXExact(dble(u),bb))
        if(mmmm.eq.2)z=sngl(PomIncXIExact(dble(u)))/sigine*10
        if(mmmm.eq.3)z=sngl(PomIncXIExact(dble(u)))/sigine*10
        s2=s2+dx(kk,i)*z
        write(ifhi,'(2e11.3)')u,z
       enddo
       write(ifhi,'(a)')    '  endarray'
       write(ifhi,'(a,f5.3,a,f5.3,a)')
     *                       'text .1 .85 "I= ',s1,' (',s2,')"'
       write(ifhi,'(a)')    'closehisto plot 0'

       write(ifhi,'(a)')           '!--------------------------------'
       write(ifhi,'(a)')           '!   Pomeron y distribution   '//mod
       write(ifhi,'(a)')           '!--------------------------------'

       write(ifhi,'(a)')       'openhisto name yPomSimuL'//mod(3:4)
       write(ifhi,'(a)')       'htyp lru xmod lin ymod'//mod
       write(ifhi,'(a,2e11.3)')'xrange',yu,yo
       write(ifhi,'(a)')    'text 0 0 "xaxis y?PE!"'
       write(ifhi,'(a)') 'text 0 0 "yaxis'//imod//'?Pom!/dy?PE!"'
       write(ifhi,'(a,f5.2,a)')'text 0.1 0.7 "f=',ff,'"'
       write(ifhi,'(a)')       'array 2'
       s1=0
       do i=1,nbiy
       u=y(i)
       z=ff*wymc(i)/dy/nrevt
       s1=s1+z*dy
        write(ifhi,'(2e11.3)')u,z
       enddo
       write(ifhi,'(a)')    '  endarray'
       write(ifhi,'(a)')    'closehisto plot 0'

       write(ifhi,'(a)')       '!----------------------------------'
       write(ifhi,'(a)')       '!   Pomeron x+ distribution    '//mod
       write(ifhi,'(a)')       '!----------------------------------'

       write(ifhi,'(a)')   'openhisto name xpPomSimuL'//mod(3:4)
       write(ifhi,'(a)')   'htyp lru xmod'//mod//'ymod log'
       write(ifhi,'(a,2e11.3)')'xrange',xlu,xlo
       write(ifhi,'(a)')    'text 0 0 "xaxis x+?PE!"'
       write(ifhi,'(a)') 'text 0 0 "yaxis'//imod//'?Pom! / dx+?PE!"'
       if(kk.eq.1)write(ifhi,'(a,f5.2,a)')'text 0.1 0.3 "f=',ff,'"'
       if(kk.eq.2)write(ifhi,'(a,f5.2,a)')'text 0.1 0.1 "f=',ff,'"'
       write(ifhi,'(a)')       'array 2'
       s1=0
       do i=1,nbix
       u=xl(kk,i)
       z=ff*wxp(kk,i)/dxl(kk,i)/nrevt
       s1=s1+z*dxl(kk,i)
        write(ifhi,'(2e11.3)')u,z
       enddo
       write(ifhi,'(a)')    '  endarray'
       write(ifhi,'(a)')    'closehisto plot 0-'

       write(ifhi,'(a)')       'openhisto name xpPomUnitL'//mod(3:4)
       write(ifhi,'(a)')   'htyp lba xmod'//mod//'ymod log'
       write(ifhi,'(a,2e11.3)')'xrange',xlu,xlo
       write(ifhi,'(a)')    'text 0 0 "xaxis x+?PE!"'
       write(ifhi,'(a)') 'text 0 0 "yaxis'//imod//'?Pom! / dx+?PE!"'
       write(ifhi,'(a)')       'array 2'
       s2=0
       do i=1,nbix
        u=xl(kk,i)
        if(mmmm.eq.1)z=sngl(PomIncPExact(dble(u),bb))
        if(mmmm.eq.2)z=sngl(PomIncPIExact(dble(u)))/sigine*10
        if(mmmm.eq.3)z=sngl(PomIncPIExact(dble(u)))/sigine*10
        s2=s2+dxl(kk,i)*z
        write(ifhi,'(2e11.3)')u,z
       enddo
       write(ifhi,'(a)')    '  endarray'
       write(ifhi,'(a,f5.3,a,f5.3,a)')
     *                       'text .1 .85 "I= ',s1,' (',s2,')"'
       write(ifhi,'(a)')    'closehisto plot 0'

       write(ifhi,'(a)')       '!----------------------------------'
       write(ifhi,'(a)')       '!   x-?PE! distribution    '//mod
       write(ifhi,'(a)')       '!----------------------------------'

       write(ifhi,'(a)')   'openhisto name xmPomSimuL'//mod(3:4)
       write(ifhi,'(a)')   'htyp lru xmod'//mod//'ymod log'
       write(ifhi,'(a,2e11.3)')'xrange',xlu,xlo
       write(ifhi,'(a)')    'text 0 0 "xaxis x-?PE!"'
       write(ifhi,'(a)') 'text 0 0 "yaxis'//imod//'?Pom! / dx-?PE!"'
       if(kk.eq.1)write(ifhi,'(a,f5.2,a)')'text 0.1 0.3 "f=',ff,'"'
       if(kk.eq.2)write(ifhi,'(a,f5.2,a)')'text 0.1 0.1 "f=',ff,'"'
       write(ifhi,'(a)')       'array 2'
       s1=0
       do i=1,nbix
       u=xl(kk,i)
       z=ff*wxm(kk,i)/dxl(kk,i)/nrevt
       s1=s1+z*dxl(kk,i)
        write(ifhi,'(2e11.3)')u,z
       enddo
       write(ifhi,'(a)')    '  endarray'
       write(ifhi,'(a)')    'closehisto plot 0-'

       write(ifhi,'(a)')       'openhisto name xmPomUnitL'//mod(3:4)
       write(ifhi,'(a)')   'htyp lba xmod'//mod//'ymod log'
       write(ifhi,'(a,2e11.3)')'xrange',xlu,xlo
       write(ifhi,'(a)')    'text 0 0 "xaxis x-?PE!"'
       write(ifhi,'(a)') 'text 0 0 "yaxis'//imod//'?Pom! / dx-"'
       write(ifhi,'(a)')       'array 2'
       s2=0
       do i=1,nbix
        u=xl(kk,i)
        if(mmmm.eq.1)z=sngl(PomIncMExact(dble(u),bb))
        if(mmmm.eq.2)z=sngl(PomIncMIExact(dble(u))/sigine*10)
        if(mmmm.eq.3)z=sngl(PomIncMIExact(dble(u))/sigine*10)
        s2=s2+dxl(kk,i)*z
        write(ifhi,'(2e11.3)')u,z
       enddo
       write(ifhi,'(a)')    '  endarray'
       write(ifhi,'(a,f5.3,a,f5.3,a)')
     *                       'text .1 .85 "I= ',s1,' (',s2,')"'
       write(ifhi,'(a)')    'closehisto plot 0'

  !................................................................

       xm=-1. !xm integration
       txtxm='xm int'
       do jjb=0,3
       b=jjb*0.5
       do jj=0,2

       write(ifhi,'(a)')       '!----------------------------------'
       write(ifhi,'(a,3i1)')   '!   ffom11    '//mod,jjb,jj
       write(ifhi,'(a)')       '!----------------------------------'

       write(ifhi,'(a,2i1)')'openhisto name ffom11L'//mod(3:4),jjb,jj+8
       write(ifhi,'(a)')    'htyp lin xmod'//mod//'ymod log'
       write(ifhi,'(a,2e11.3)')'xrange ',xlu,xlo
       write(ifhi,'(a)')'txt "xaxis  x+?PE!"'
       write(ifhi,'(a)')'txt "yaxis dn?Pom! / dx+?PE! "'
       write(ifhi,'(a)')'text 0.05 0.1  "fit and exact, all contrib."'
       if(jjb.lt.3)write(ifhi,'(a,f4.1,3a)')
     *             'txt "title ffom11   b =',b,'   ',txtxm,'"'
       if(jjb.ge.3)write(ifhi,'(3a)')
     *             'txt "title ffom11   b aver   ',txtxm,'"'
       write(ifhi,'(a)')       'array 2'
       do i=1,nbix
       u=xl(kk,i)
       if(jjb.lt.3.and.jj.eq.0)z= ffom11(u,xm,b,-1,-1)
       if(jjb.lt.3.and.jj.eq.1)z= ffom11(u,xm,b,0,5)
       if(jjb.lt.3.and.jj.eq.2)z= ffom11(u,xm,b,0,4)
       if(jjb.eq.3.and.jj.eq.0)z=ffom11a(u,xm,-1,-1)
       if(jjb.eq.3.and.jj.eq.1)z=ffom11a(u,xm,0,5)
       if(jjb.eq.3.and.jj.eq.2)z=ffom11a(u,xm,0,4)
        write(ifhi,'(2e11.3)')u,z
       enddo
       write(ifhi,'(a)')    '  endarray'
       if(jj.le.1)write(ifhi,'(a)')    'closehisto plot 0-'
       if(jj.eq.2)write(ifhi,'(a)')    'closehisto plot 0'

       enddo
       enddo

       do jjb=0,3
       b=jjb*0.5
       do jjj=1,6
       jj=jjj
       if(jjj.eq.6)jj=0

       write(ifhi,'(a)')       '!----------------------------------'
       write(ifhi,'(a,3i1)')   '!   ffom11    '//mod,jjb,jj
       write(ifhi,'(a)')       '!----------------------------------'

       write(ifhi,'(a,3i1)')'openhisto name om1ffL'//mod(3:4),jjb,jj
       if(jj.ne.0)write(ifhi,'(a)')    'htyp lin xmod'//mod//'ymod log'
       if(jj.eq.0)write(ifhi,'(a)')    'htyp lro xmod'//mod//'ymod log'
       write(ifhi,'(a,2e11.3)')'xrange ',xlu,xlo
       if(jj.eq.1)then
       write(ifhi,'(a)') 'txt "xaxis  x+?PE!"'
       write(ifhi,'(a)') 'txt "yaxis  dn?Pom! / dx+?PE!  "'
       if(kk.eq.2)then
        write(ifhi,'(a)') 'text 0.1 0.2  "soft sea-sea"'
        write(ifhi,'(a)') 'text 0.1 0.1  "val-sea sea-val val-val"'
       else
        write(ifhi,'(a)') 'text 0.05 0.8  "soft"'
        write(ifhi,'(a)') 'text 0.05 0.7  "diff"'
        write(ifhi,'(a)') 'text 0.05 0.6  "sea-sea"'
        write(ifhi,'(a)') 'text 0.05 0.5  "val-sea"'
        write(ifhi,'(a)') 'text 0.05 0.4  "sea-val"'
        write(ifhi,'(a)') 'text 0.05 0.3  "val-val"'
      endif
       if(jjb.lt.3)write(ifhi,'(a,f4.1,3a)')
     *             'txt "title ffom11   b =',b,'  ',txtxm,'"'
       if(jjb.ge.3)write(ifhi,'(3a)')
     *             'txt "title ffom11   b aver  ',txtxm,'"'
       endif
       write(ifhi,'(a)')       'array 2'
       do i=1,nbix
       u=xl(kk,i)
       if(jjb.lt.3)z= ffom11(u,xm,b,jj,jj)
       if(jjb.eq.3)z=ffom11a(u,xm,jj,jj)
       write(ifhi,'(2e11.3)')u,z
       enddo
       write(ifhi,'(a)')    '  endarray'
       if(jjj.ne.6)write(ifhi,'(a)')    'closehisto plot 0-'
       if(jjj.eq.6)write(ifhi,'(a)')    'closehisto plot 0'

       enddo
       enddo

      enddo

      koll=kollini
      endif

      return
      end

c-----------------------------------------------------------------------
      subroutine xEmsP2(iii,jaa,jex,xpd,xmd,xpb,xmb,pt1,pt2)
c-----------------------------------------------------------------------
c plot  x+ distributions of Pomeron ends (PE) (xpd)
c          and Pomeron's in Born (IB) partons (xpb),
c     and pt dist of Pomeron's out Born (OB) partons
c       integrated over x- bins (xmd,xmb)
c  iii=0: initialize
c  ii=1: fill arrays
c  iii>=2: make histogram
c           (2 - Pomeron end PE, 3 - in Born IB, 4 - out Born OB)
c  jaa: type of semihard Pomeron
c         0= sea-sea diff, 
c         1= sea-sea, 2= val=sea, 3= sea-val, 4= val-val
c         5= all  for iii=2
c  jex: emission type
c         1= no emission, 2= proj emis, 3= targ emis, 4= both sides
c         5= all  for iii=2
c-----------------------------------------------------------------------

      include 'epos.inc'
      include 'epos.incsem'
      include 'epos.incems'
      common/geom/rmproj,rmtarg,bmax,bkmx
      parameter(nbixp=25,nbixm=5,nbipt=20)
      common/cxb/xlp(2,nbixp),dxlp(2,nbixp)
     *          ,xlm(2,nbixm),dxlm(2,nbixm)
     *          ,wxb(2,0:4,4,nbixp,nbixm)
     *          ,wxe(2,0:4,4,nbixp,nbixm)
      common/cptb/ptu,pto,ptob(nbipt),wptob(0:4,4,nbipt)
      common/cemspbx/xlub1,xlub2,xlob
ctp060829      character imod*5

      if(iemspbx.eq.0)call utstop('ERROR in xEmsP2: iemspbx = 0&')

      if(iii.eq.0)then

       xlub1=0.01/engy
       xlub2=0.
       xlob=1.
       do i=1,nbixp
        xlp(1,i)=xlub1*(xlob/xlub1)**((i-0.5)/nbixp)
        xlp(2,i)=xlub2+(xlob-xlub2)*((i-0.5)/nbixp)
        dxlp(1,i)=xlub1*(xlob/xlub1)**(1.*i/nbixp)
     *             *(1.-(xlob/xlub1)**(-1./nbixp))
        dxlp(2,i)=(xlob-xlub2)/nbixp
       enddo
       do i=1,nbixm
        xlm(1,i)=xlub1*(xlob/xlub1)**((i-0.5)/nbixm)
        xlm(2,i)=xlub2+(xlob-xlub2)*((i-0.5)/nbixm)
        dxlm(1,i)=xlub1*(xlob/xlub1)**(1.*i/nbixm)
     *             *(1.-(xlob/xlub1)**(-1./nbixm))
        dxlm(2,i)=(xlob-xlub2)/nbixm
       enddo
       do i=1,nbixp
       do j=1,nbixm
       do jaai=0,4
       do jexi=1,4
        wxb(1,jaai,jexi,i,j)=0.
        wxb(2,jaai,jexi,i,j)=0.
        wxe(1,jaai,jexi,i,j)=0.
        wxe(2,jaai,jexi,i,j)=0.
       enddo
       enddo
       enddo
       enddo
       ptu=2
       pto=20
       do i=1,nbipt
       ptob(i)=ptu+(pto-ptu)*(i-0.5)/nbipt
       do jaai=0,4
       do jexi=1,4
       wptob(jaai,jexi,i)=0
       enddo
       enddo
       enddo

      elseif(iii.eq.1)then

       xp=xpb
       xm=xmb
       if(xp.lt.xlub1)goto2
       if(xm.lt.xlub1)goto2
       i=1+int(alog(xp/xlub1)/alog(xlob/xlub1)*nbixp)
       if(i.gt.nbixp)goto2
       if(i.lt.1)goto2
       j=1+int(alog(xm/xlub1)/alog(xlob/xlub1)*nbixm)
       if(j.gt.nbixm)goto2
       if(j.lt.1)goto2
       wxb(1,jaa,jex,i,j)=wxb(1,jaa,jex,i,j)+1.
2      continue

       if(xp.lt.xlub2)goto12
       if(xm.lt.xlub2)goto12
       i=1+int((xp-xlub2)/(xlob-xlub2)*nbixp)
       if(i.gt.nbixp)goto12
       if(i.lt.1)goto12
       j=1+int((xm-xlub2)/(xlob-xlub2)*nbixm)
       if(j.gt.nbixm)goto12
       if(j.lt.1)goto12
       wxb(2,jaa,jex,i,j)=wxb(2,jaa,jex,i,j)+1.
12     continue

       xp=xpd
       xm=xmd
       if(xp.lt.xlub1)goto22
       if(xm.lt.xlub1)goto22
       i=1+int(alog(xp/xlub1)/alog(xlob/xlub1)*nbixp)
       if(i.gt.nbixp)goto22
       if(i.lt.1)goto22
       j=1+int(alog(xm/xlub1)/alog(xlob/xlub1)*nbixm)
       if(j.gt.nbixm)goto22
       if(j.lt.1)goto22
       wxe(1,jaa,jex,i,j)=wxe(1,jaa,jex,i,j)+1.
  22   continue

       if(xp.lt.xlub2)goto32
       if(xm.lt.xlub2)goto32
       i=1+int((xp-xlub2)/(xlob-xlub2)*nbixp)
       if(i.gt.nbixp)goto32
       if(i.lt.1)goto32
       j=1+int((xm-xlub2)/(xlob-xlub2)*nbixm)
       if(j.gt.nbixm)goto32
       if(j.lt.1)goto32
       wxe(2,jaa,jex,i,j)=wxe(2,jaa,jex,i,j)+1.
  32   continue

       do m=1,2
       if(m.eq.1)pt=pt1
       if(m.eq.2)pt=pt2
       i=1+int((pt-ptu)/(pto-ptu)*nbipt)
       if(i.lt.1)goto42
       if(i.gt.nbipt)goto42
       wptob(jaa,jex,i)=wptob(jaa,jex,i)+1
   42  continue
       enddo

      elseif(iii.ge.2)then

       if(maproj.eq.1.and.matarg.eq.1.and.bminim.eq.bmaxim)then
ctp060829        mmmm=1
ctp060829        bb=bmaxim
        ff=float(nrevt)/float(ntevt)
ctp060829        imod='   dn'
       elseif(maproj.eq.1.and.matarg.eq.1)then
ctp060829        mmmm=3
        ff=1.
ctp060829        imod='   dn'
       elseif(bminim.lt.0.001.and.bmaxim.gt.20)then
ctp060829        mmmm=2
        area=pi*(rmproj+rmtarg)**2
        ff=area*float(nrevt)/float(ntevt)/(maproj*matarg)/sigine*10
ctp060829        imod='   dn'
       else
        write(ifmt,*)'xEmsP2 ignored'
        return
       endif

       j1=1  !nint(xpar1)   !first xminus bin
       j2=5  !nint(xpar2)   !last xminus bin
       if(iii.eq.4)j2=1
       kkk=2 !nint(xpar3)   !1 (log binning) 2 (lin binning)
       if(kkk.eq.1)then
ctp060829         xmi1=xlub1*(xlob/xlub1)**((j1-1.)/nbixm)
ctp060829         xmi2=xlub1*(xlob/xlub1)**((j2-0.)/nbixm)
         xlub=xlub1
       elseif(kkk.eq.2)then
ctp060829         xmi1=xlub2+(xlob-xlub2)*((j1-1.)/nbixm)
ctp060829         xmi2=xlub2+(xlob-xlub2)*((j2-0.)/nbixm)
         xlub=xlub2
       endif

       jaa1=jaa
       jaa2=jaa
       jex1=jex
       jex2=jex
       if(jaa.eq.5)then
       jaa1=0
       jaa2=4
       endif
       if(jex.eq.5)then
       jex1=1
       jex2=4
       endif

       if(jex.eq.1)then
        je1=0
        je2=0
       elseif(jex.eq.2)then
        je1=1
        je2=0
       elseif(jex.eq.3)then
        je1=0
        je2=1
       elseif(jex.eq.4)then
        je1=1
        je2=1
       elseif(jex.eq.5)then
        je1=2
        je2=2
       endif

       if(iii.eq.2)then

        write(ifhi,'(a)')       '!----------------------------------'
        write(ifhi,'(a,3i1)')   '!   PE    ',jaa,jex
        write(ifhi,'(a)')       '!----------------------------------'

        sum=ffom12aii(max(1,jaa),je1,je2)
        write(ifhi,'(a,2i1)')'openhisto name ffom12a',jaa,jex
        write(ifhi,'(a)')'htyp lin xmod lin ymod log'
        write(ifhi,'(a,2e11.3)')'xrange ',xlub,xlob
        write(ifhi,'(a)')    'txt "xaxis  x+?PE!"'
        write(ifhi,'(a)')    'txt "yaxis dn?semi! / dx+?PE!    "'
       write(ifhi,'(a,2i1,a)')'txt "title ffom12a + MC   (',jaa,jex,')"'
        write(ifhi,'(a)')    'array 2'
        do i=1,nbixp
         u=xlp(kkk,i)
         z=ffom12ai(u,max(1,jaa1),jaa2,je1,je2)
         write(ifhi,'(2e11.3)')u,z
        enddo
        write(ifhi,'(a)')    '  endarray'
        if(jex.eq.5)then
          write(ifhi,'(a)')    'closehisto plot 0-'
          write(ifhi,'(a,2i1)')'openhisto name ffom11',jaa,jex
          write(ifhi,'(a)')'htyp lba'
          write(ifhi,'(a)')'text 0.05 0.5 "+ ffom11a "'
          write(ifhi,'(a)')'array 2'
          do i=1,nbixp
           u=xlp(kkk,i)
           z=ffom11a(u,-1.,max(1,jaa1),jaa2)
           write(ifhi,'(2e11.3)')u,z
          enddo
          write(ifhi,'(a)')    '  endarray'
        endif

       elseif(iii.eq.3)then

        write(ifhi,'(a)')       '!----------------------------------'
        write(ifhi,'(a,3i1)')   '!   IB    ',jaa,jex
        write(ifhi,'(a)')       '!----------------------------------'

    !.......total integral
        s2min=4*q2min
        zmin=s2min/engy**2
        zmax=1
        xpmin0 = 0.01/engy
        xpmax=1
        ig1=3
        ig2=3
        r1=0
        do i1=1,ig1
        do m1=1,2
          z=zmin*(zmax/zmin)**(.5+tgss(ig1,i1)*(m1-1.5))
          xpmin=max(z,xpmin0)
          r2=0
          if(xpmin.lt.xpmax)then
          do i2=1,ig2
          do m2=1,2
            xp=xpmin*(xpmax/xpmin)**(.5+tgss(ig2,i2)*(m2-1.5))
            xm=z/xp
            r2=r2+wgss(ig2,i2)*ffsigiut(xp,xm,max(1,jaa),je1,je2)
          enddo
          enddo
          endif
          r2=r2*0.5*log(xpmax/xpmin)
          r1=r1+wgss(ig1,i1)*r2*z
        enddo
        enddo
        r1=r1*0.5*log(zmax/zmin)
        res=  r1 * factk * .0390  /sigine*10
        sum=res
   !.......plot
        xx2min = 0.01/engy     !max(xpar1,0.01/engy)
        xx2max = 1             !xpar2
        xx1min = 0.01/engy     !max(xpar3,0.01/engy)
        xx1max = 1             !xpar4
        nbins  = 10            !nint(xpar5)

        write(ifhi,'(a,2i1)') 'openhisto xrange 0 1 name ffsig',jaa,jex
        write(ifhi,'(a)') 'yrange auto auto htyp lin xmod lin ymod log'
        write(ifhi,'(a)') 'txt "xaxis x+?IB!         "              '
        write(ifhi,'(a)') 'txt "yaxis dn?semi! / dx+?IB!  "'
        write(ifhi,'(a,2i1,a)')'txt "title ffsig + MC   (',jaa,jex,')"'
        write(ifhi,'(a)') 'array 2'
        del=(xx1max-xx1min)/nbins
        do ii=1,nbins
          xx1=xx1min+(ii-0.5)*del
          ig2=3
          r2=0
          do i2=1,ig2
          do m2=1,2
            xx2=xx2min*(xx2max/xx2min)**(.5+tgss(ig2,i2)*(m2-1.5))
            r2=r2+wgss(ig2,i2)*ffsigiut(xx1,xx2,max(1,jaa),je1,je2)*xx2
          enddo
          enddo
          sig=r2*0.5*log(xx2max/xx2min)
          sig   = sig * factk * .0390   /sigine*10
          write(ifhi,'(2e12.4)')xx1,sig
        enddo
        write(ifhi,'(a)')  '  endarray'

       elseif(iii.eq.4)then

        write(ifhi,'(a)')       '!----------------------------------'
        write(ifhi,'(a,3i1)')   '!   OB    ',jaa,jex
        write(ifhi,'(a)')       '!----------------------------------'

      !...... integral
        y2     = 10
        ptmin  = 2
        ptmax  = 6
        sum=0
        ig=2
        do i=1,ig
        do m=1,2
              pt=ptmin*(ptmax/ptmin)**(.5+tgss(ig,i)*(m-1.5))
          sig=ffsigi(pt**2,y2)
          sig   =sig    * factk * .0390 /sigine*10  * 2   ! 2 partons!
              sum=sum+wgss(ig,i)*sig*pt
        enddo
        enddo
        sum=sum*0.5*log(ptmax/ptmin)
      !...... pt distr
        y2     = 10
        ptmin  = 2
        ptmax  = 20
        nbins  = 18
        sx=engy**2
        do jj=3,1,-1
        write(ifhi,'(a,i1)')'openhisto name jet',jj
        write(ifhi,'(a)')'xrange 0 20 xmod lin ymod log '
        write(ifhi,'(a)') 'txt "xaxis pt?OB!         "           '
        write(ifhi,'(a)') 'txt "yaxis dn?ptn! / dpt?OB!  "'
        if(jj.eq.1)write(ifhi,'(a)')'htyp lro'
        if(jj.eq.2)write(ifhi,'(a)')'htyp lgo'
        if(jj.eq.3)write(ifhi,'(a)')'htyp lyo'
        write(ifhi,'(a,f7.2,a)')  'text 0.05 0.1 "1/f=',1./ff,'"'
        write(ifhi,'(a)')'array 2'
        delpt=(ptmax-ptmin)/nbins
        do i=1,nbins
          pt=ptmin+(i-0.5)*delpt
          sig=1
          if(jj.eq.1)then
            sig=ffsigi(pt**2,y2)      ! our stuff
          elseif(jj.eq.2)then
            if(engy.ge.10.)sig=psjvrg1(pt**2,sx,y2) ! grv
          elseif(jj.eq.3)then
            if(engy.ge.10.)sig=psjwo1(pt**2,sx,y2)   !duke-owens
          endif
          sig   =sig    * factk * .0390 /sigine*10 * 2
          write(ifhi,'(2e12.4)')pt,sig
        enddo
        write(ifhi,'(a)')       '  endarray'
        if(jj.ne.1)write(ifhi,'(a)')       'closehisto'
        if(jj.ne.1)write(ifhi,'(a)')  'plot 0-'
        enddo

       endif

       x=0.1+(min(3,iii)-2)*0.30
       y=0.2+(min(3,iii)-2)*0.55
       if(engy.gt.100.)then
       write(ifhi,'(a,2f5.2,a,f6.3,a)')'text',x,y,' "   form ',sum,'"'
       else
       write(ifhi,'(a,2f5.2,a,f6.5,a)')'text',x,y,' "   form ',sum,'"'
       endif
       write(ifhi,'(a)')  'closehisto plot 0-'

       write(ifhi,'(a)') "!-----------------------------"
       write(ifhi,'(a)') "! MC   "
       write(ifhi,'(a)') "!-----------------------------"

       if(iii.eq.2)
     *  write(ifhi,'(a,i1,i1)')'openhisto name dndxPE',jaa,jex
       if(iii.eq.3)
     *  write(ifhi,'(a,i1,i1)')'openhisto name dndxIB',jaa,jex
       if(iii.eq.4)
     *  write(ifhi,'(a,i1,i1)')'openhisto name dndptOB',jaa,jex
       write(ifhi,'(a)')     'htyp prs'
       write(ifhi,'(a)')     'array 2'
       sum=0
       imax=nbixp
       if(iii.eq.4)imax=nbipt
       do i=1,imax
        u=xlp(kkk,i)
        if(iii.eq.4)u=ptob(i)
        z=0
        do j=j1,j2
        do jaai=jaa1,jaa2
        do jexi=jex1,jex2
         if(iii.eq.2)z=z+wxe(kkk,jaai,jexi,i,j)
         if(iii.eq.3)z=z+wxb(kkk,jaai,jexi,i,j)
         if(iii.eq.4)z=z+wptob(jaai,jexi,i)
        enddo
        enddo
        enddo
        del=dxlp(kkk,i)
        if(iii.eq.4)del=(pto-ptu)/nbipt
        z=z/del*ff/nrevt
        write(ifhi,'(2e11.3)')u,z
        sum=sum+z*del
       enddo
       write(ifhi,'(a)')    '  endarray'
       x=0.1+(min(3,iii)-2)*0.30
       y=0.1+(min(3,iii)-2)*0.55
       if(engy.gt.100)then
       write(ifhi,'(a,2f5.2,a,f6.3,a)')'text',x,y,' "   simu ',sum,'"'
       else
       write(ifhi,'(a,2f5.2,a,f6.5,a)')'text',x,y,' "   simu ',sum,'"'
       endif
       write(ifhi,'(a)')    'closehisto'

      endif

      return
      end

c-----------------------------------------------------------------------
      subroutine xEmsSe(iii,xmc,ptmc,ih,iqq)
c-----------------------------------------------------------------------
c     iqq = 1 : String End mass and rapidity
c     iqq = 2 : String mass and rapidity
c-----------------------------------------------------------------------

      include 'epos.inc'

      parameter(nbix=50)
      common/cxpar/nx(2),x(nbix),wxmc(nbix,2),xmn,xmx,xu,xo
      parameter(nbiy=40)
      common/cypar/ny(2),y(nbiy),wymc(nbiy,2),ymin,ymax,dy,yu,yo

      s=engy**2

      if(iii.eq.0)then

       nx(iqq)=0
       xu=0.1/engy**2
       xo=1.
       do i=1,nbix
         x(i)=xu*(xo/xu)**((i-0.5)/nbix)
         wxmc(i,iqq)=0
       enddo
       yo=alog(s)
       yu=-yo
       dy=(yo-yu)/nbiy
       ny(iqq)=0
       do i=1,nbiy
         y(i)=yu+dy/2.+(i-1)*dy
         wymc(i,iqq)=0
       enddo

      elseif(iii.eq.1)then

       if(xmc.lt.xu)return
       if(ptmc.eq.0.)return
       ymc=0.
       if(iqq.eq.1)ymc=0.5*alog(xmc*s/ptmc)*ih
       if(iqq.eq.2)ymc=0.5*alog(xmc/ptmc)
       i=1+int(alog(xmc/xu)/alog(xo/xu)*nbix)
       if(i.gt.nbix)goto1
       if(i.lt.1)goto1
       wxmc(i,iqq)=wxmc(i,iqq)+1
       nx(iqq)=nx(iqq)+1
1      continue
       if(ymc.lt.yu)return
       i=int((ymc-yu)/dy)+1
       if(i.gt.nbiy)return
       if(i.lt.1)return
       wymc(i,iqq)=wymc(i,iqq)+1
       ny(iqq)=ny(iqq)+1

      elseif(iii.eq.2)then

       write(ifhi,'(a)')        '!--------------------------------'
       write(ifhi,'(a)')        '!   string end x distr       '
       write(ifhi,'(a)')        '!--------------------------------'
        write(ifhi,'(a)')       'openhisto'
        write(ifhi,'(a)')       'htyp lin'
        write(ifhi,'(a)')       'xmod log ymod log'
        write(ifhi,'(a,2e11.3)')'xrange',xu,xo
        if(iqq.eq.1)write(ifhi,'(a)')    'text 0 0 "xaxis string end x"'
        if(iqq.eq.2)write(ifhi,'(a)')    'text 0 0 "xaxis string x"'
        write(ifhi,'(a)')    'text 0 0 "yaxis P(x)"'
        write(ifhi,'(a)')       'array 2'
        do i=1,nbix
         dx=xu*(xo/xu)**(1.*i/nbix)*(1.-(xo/xu)**(-1./nbix))
         if(nx(iqq).gt.0)
     *   write(ifhi,'(2e11.3)')x(i),wxmc(i,iqq)/dx/nx(iqq)
        enddo
        write(ifhi,'(a)')    '  endarray'
        write(ifhi,'(a)')    'closehisto plot 0'
        write(ifhi,'(a)')       'openhisto'
        write(ifhi,'(a)')       'htyp lin'
        write(ifhi,'(a)')       'xmod lin ymod lin'
        write(ifhi,'(a,2e11.3)')'xrange',yu,yo
        if(iqq.eq.1)write(ifhi,'(a)')    'text 0 0 "xaxis string end y"'
        if(iqq.eq.2)write(ifhi,'(a)')    'text 0 0 "xaxis string y"'
        write(ifhi,'(a)')    'text 0 0 "yaxis P(y)"'
        write(ifhi,'(a)')       'array 2'
        do i=1,nbiy
         if(ny(iqq).gt.0)
     *   write(ifhi,'(2e11.3)')y(i),wymc(i,iqq)/dy/ny(iqq)
        enddo
        write(ifhi,'(a)')    '  endarray'
        write(ifhi,'(a)')    'closehisto plot 0'
      endif

      return
      end

c-----------------------------------------------------------------------
      subroutine xEmsDr(iii,xpmc,xmmc,ie)
c-----------------------------------------------------------------------

      include 'epos.inc'

      parameter(nbix=50,nie=4)
      common/cxpardr/nxp(nie),nxm(nie),x(nbix),wxpmc(nbix,nie)
     &      ,wxmmc(nbix,nie),xmn,xmx,xu,xo,wxmc(nbix,nie),nx(nie)
      parameter(nbiy=40)
      common/cypardr/ny(nie),y(nbiy),wymc(nbiy,nie),ymin,ymax,dy,yu,yo

      s=engy**2

      if(iii.eq.0)then

       do ni=1,nie
         nxp(ni)=0
         nxm(ni)=0
         nx(ni)=0
       enddo
       xu=0.1/engy**2
       xo=1.
       do i=1,nbix
         x(i)=xu*(xo/xu)**((i-0.5)/nbix)
         do ni=1,nie
           wxpmc(i,ni)=0
           wxmmc(i,ni)=0
           wxmc(i,ni)=0
         enddo
       enddo
       yo=alog(s)
       yu=-yo
       dy=(yo-yu)/nbiy
       do ni=1,nie
         ny(ni)=0
       enddo
       do i=1,nbiy
         y(i)=yu+dy/2.+(i-1)*dy
         do ni=1,nie
           wymc(i,ni)=0
         enddo
       enddo

      elseif(iii.eq.1)then

       if(ie.lt.1.or.ie.gt.nie)return

       if(xpmc.lt.xu)return
       i=1+int(alog(xpmc/xu)/alog(xo/xu)*nbix)
       if(i.gt.nbix)goto1
       if(i.lt.1)goto1
       wxpmc(i,ie)=wxpmc(i,ie)+1
       nxp(ie)=nxp(ie)+1
       if(xmmc.lt.xu)return
       i=1+int(alog(xmmc/xu)/alog(xo/xu)*nbix)
       if(i.gt.nbix)goto1
       if(i.lt.1)goto1
       wxmmc(i,ie)=wxmmc(i,ie)+1
       nxm(ie)=nxm(ie)+1
1      continue
       if(xmmc.ge.xu)then
         ymc=0.5*alog(xpmc/xmmc)
       else
         return
       endif
       if(ymc.lt.yu)return
       i=int((ymc-yu)/dy)+1
       if(i.gt.nbiy)return
       if(i.lt.1)return
       wymc(i,ie)=wymc(i,ie)+1
       ny(ie)=ny(ie)+1

       xmc=xpmc*xmmc
       if(xmc.lt.xu)return
       i=1+int(alog(xmc/xu)/alog(xo/xu)*nbix)
       if(i.gt.nbix)return
       if(i.lt.1)return
       wxmc(i,ie)=wxmc(i,ie)+1
       nx(ie)=nx(ie)+1

      elseif(iii.eq.2)then

        do ii=1,nie

       if(ii.eq.1)write(ifhi,'(a)')'!-----  projectile droplet  ----'
       if(ii.eq.2)write(ifhi,'(a)')'!-----    target droplet    ----'
       if(ii.eq.3)write(ifhi,'(a)')'!-----  projectile string end  ----'
       if(ii.eq.4)write(ifhi,'(a)')'!-----    target string end    ----'
        write(ifhi,'(a)')       '!--------------------------------'
        write(ifhi,'(a)')       '!   droplet/string x+ distr       '
        write(ifhi,'(a)')       '!--------------------------------'
        write(ifhi,'(a)')       'openhisto'
        write(ifhi,'(a)')       'htyp lru'
        write(ifhi,'(a)')       'xmod log ymod log'
        write(ifhi,'(a,2e11.3)')'xrange',xu,xo
        if(ii.eq.1.or.ii.eq.2)
     *  write(ifhi,'(a)')    'text 0 0 "xaxis droplet x+"'
        if(ii.eq.3.or.ii.eq.4)
     *  write(ifhi,'(a)')    'text 0 0 "xaxis string end x+"'
        write(ifhi,'(a)')    'text 0 0 "yaxis P(x)"'
        write(ifhi,'(a)')       'array 2'
        do i=1,nbix
         dx=xu*(xo/xu)**(1.*i/nbix)*(1.-(xo/xu)**(-1./nbix))
         if(nxp(ii).gt.0)
     *   write(ifhi,'(2e11.3)')x(i),wxpmc(i,ii)/dx/nxp(ii)
        enddo
        write(ifhi,'(a)')    '  endarray'
        write(ifhi,'(a)')    'closehisto plot 0-'
        write(ifhi,'(a)')       '!--------------------------------'
        write(ifhi,'(a)')       '!   droplet/string x- distr       '
        write(ifhi,'(a)')       '!--------------------------------'
        write(ifhi,'(a)')       'openhisto'
        write(ifhi,'(a)')       'htyp lba'
        write(ifhi,'(a)')       'xmod log ymod log'
        write(ifhi,'(a,2e11.3)')'xrange',xu,xo
        if(ii.eq.1.or.ii.eq.2)
     *  write(ifhi,'(a)')    'text 0 0 "xaxis droplet x-"'
        if(ii.eq.3.or.ii.eq.4)
     *  write(ifhi,'(a)')    'text 0 0 "xaxis string end x-"'
        write(ifhi,'(a)')    'text 0 0 "yaxis P(x)"'
        write(ifhi,'(a)')       'array 2'
        do i=1,nbix
         dx=xu*(xo/xu)**(1.*i/nbix)*(1.-(xo/xu)**(-1./nbix))
         if(nxm(ii).gt.0)
     *   write(ifhi,'(2e11.3)')x(i),wxmmc(i,ii)/dx/nxm(ii)
        enddo
        write(ifhi,'(a)')    '  endarray'
        write(ifhi,'(a)')    'closehisto plot 0'
        write(ifhi,'(a)')       '!--------------------------------'
        write(ifhi,'(a)')       '!   droplet/string y distr       '
        write(ifhi,'(a)')       '!--------------------------------'
        write(ifhi,'(a)')       'openhisto'
        write(ifhi,'(a)')       'htyp lin'
        write(ifhi,'(a)')       'xmod lin ymod lin'
        write(ifhi,'(a,2e11.3)')'xrange',yu,yo
        if(ii.eq.1.or.ii.eq.2)
     *  write(ifhi,'(a)')    'text 0 0 "xaxis droplet y"'
        if(ii.eq.3.or.ii.eq.4)
     *  write(ifhi,'(a)')    'text 0 0 "xaxis string end y"'
        write(ifhi,'(a)')    'text 0 0 "yaxis P(y)"'
        write(ifhi,'(a)')       'array 2'
        do i=1,nbiy
         if(ny(ii).gt.0)
     *   write(ifhi,'(2e11.3)')y(i),wymc(i,ii)/dy/ny(ii)
        enddo
        write(ifhi,'(a)')    '  endarray'
        write(ifhi,'(a)')    'closehisto plot 0'

      enddo

        write(ifhi,'(a)')       '!--------------------------------'
        write(ifhi,'(a)')       '!   droplet/string mass distr       '
        write(ifhi,'(a)')       '!--------------------------------'
      do ii=1,nie


        if(ii.eq.2.or.ii.eq.4)write(ifhi,'(a)')    'closehisto plot 0-'
        if(ii.eq.3)write(ifhi,'(a)')    'closehisto plot 0'
        write(ifhi,'(a)')       'openhisto'
        if(ii.eq.1.or.ii.eq.3)write(ifhi,'(a)')       'htyp lru'
        if(ii.eq.2.or.ii.eq.4)write(ifhi,'(a)')       'htyp lba'
        write(ifhi,'(a)')       'xmod log ymod log'
        write(ifhi,'(a,2e11.3)')'xrange',sqrt(xu*s),sqrt(s*xo)
        if(ii.eq.1.or.ii.eq.2)
     *  write(ifhi,'(a)')    'text 0 0 "xaxis droplet mass (GeV)"'
        if(ii.eq.4.or.ii.eq.3)
     *  write(ifhi,'(a)')    'text 0 0 "xaxis string end mass (GeV)"'
        write(ifhi,'(a)')    'text 0 0 "yaxis P(x)"'
        write(ifhi,'(a)')       'array 2'
        do i=1,nbix
         dx=xu*(xo/xu)**(1.*i/nbix)*(1.-(xo/xu)**(-1./nbix))
         if(nx(ii).gt.0)
     *   write(ifhi,'(2e11.3)')sqrt(x(i)*s),wxmc(i,ii)/dx/nx(ii)
        enddo
        write(ifhi,'(a)')    '  endarray'
      enddo
       write(ifhi,'(a)')    'closehisto plot 0'

      endif

      return
      end

cc--------------------------------------------------------------------------
c      subroutine xtype(k,n,i1,i2,text)
cc--------------------------------------------------------------------------
c
c      include 'epos.inc'
c      include 'epos.incems'
c      parameter(itext=40)
c      character  text*40
c
c      imax=itext+1
c      do i=itext,1,-1
c      if(text(i:i).eq.'&')imax=i
c      enddo
c
c      ip=iproj(k)
c      it=itarg(k)
c
c      if(i1.eq.1)then
c         write(ifch,*)
c         write(ifch,*)('-',ll=1,27)
c         write(ifch,*)'  '//text(1:imax-1)
c         write(ifch,*)('-',ll=1,27)
c      endif
c
c      if(i2.eq.1)then
c         write(ifch,*)
c         write(ifch,*)'k:',k,'   n:',n,'   ip:',ip,'   it:',it
c         write(ifch,*)'bk:',bk(k)
c         if(n.ne.0)write(ifch,*)'idpr:',idpr(n,k)
c         write(ifch,*)'iep:',iep(ip),'   iet:',iet(it)
c         write(ifch,*)'idp:',idp(ip),'   idt:',idt(it)
c      endif
c
c      end
c
c------------------------------------------------------------------------
      subroutine XPrint(text)
c------------------------------------------------------------------------
      include 'epos.inc'
      include 'epos.incems'
      double precision xpptot,xmptot,xpttot,xmttot
c      parameter(itext=15)
      character  text*(*)
      imax=index(text,'&')
      if(imax.gt.1)write(ifch,'(1x,a)')text(1:imax-1)

      write(ifch,'(a)')
     *' k:     itpr:   npr0: npr1: nprmx:   Pomeron id lattice:'
      do k=1,koll
       write(ifch,'(1x,i6,1x,i4,4x,i4,2x,i4,3x,i4,a3,$)')
     *              k,itpr(k),npr(0,k),npr(1,k),nprmx(k),'   '
       do n=1,nprmx(k)
        write(ifch,'(i2,$)')idpr(n,k)
       enddo
       write(ifch,*)' '
      enddo

      xpptot=0d0
      xmptot=0d0
      xpttot=0d0
      xmttot=0d0
      write(ifch,'(a)')' Pomeron xy lattice:'
      do k=1,koll
       do n=1,nprmx(k)
       xpptot=xpptot+xppr(n,k)
       xmttot=xmttot+xmpr(n,k)
        write(ifch,'(i6,1x,i2,1x,d10.3,1x,d10.3,3x,$)')
     *                  k,n,xpr(n,k),ypr(n,k)
       enddo
       write(ifch,*)' '
      enddo

      write(ifch,'(a)')' projectile remnants x+,x-,px,py,x,iep:'
      do ip=1,maproj
       xpptot=xpptot+xpp(ip)
       xmptot=xmptot+xmp(ip)
       write(ifch,'(i3,2x,5d12.3,i3)')ip,xpp(ip),xmp(ip),xxp(ip),xyp(ip)
     *                             ,xpos(ip),iep(ip)
      enddo

      write(ifch,'(a)')' target remnants x-,x+,px,py,x,iet:'
      do it=1,matarg
       xpttot=xpttot+xpt(it)
       xmttot=xmttot+xmt(it)
       write(ifch,'(i3,2x,5d12.3,i3)')it,xmt(it),xpt(it),xxt(it),xyt(it)
     *                             ,xtos(it),iet(it)
      enddo

      write(ifch,*)' remnant balance x+,x-:'
     &,(xpptot+xpttot)/dble(maproj)
     &,(xmptot+xmttot)/dble(matarg)
      end


c-------------------------------------------------------------------------
      subroutine xfom
c-------------------------------------------------------------------------
      include 'epos.inc'
      double precision fom,x
      write(ifhi,'(a)')     '!##################################'
      write(ifhi,'(a,i3)')  '!   fom     '
      write(ifhi,'(a)')     '!##################################'
      b=0.
      do i=1,6
        z=0.2*exp(0.8*i)
        xi=0.01+0.16*float(i-1)
        write(ifhi,'(a,i1)') 'openhisto name fom',i
        write(ifhi,'(a)')    'htyp lin xmod lin ymod log'
        write(ifhi,'(a)')    'xrange 0 1'
        write(ifhi,'(a)')    'yrange 0.1 1000 '
        write(ifhi,'(a)')    'text 0 0 "xaxis x "'
        write(ifhi,'(a)')    'text 0 0 "yaxis fom"'
        if(z.lt.10.)
     &   write(ifhi,'(a,f4.2,a,f4.1,a)')'text ',xi,' 0.9 "',z,'"'
        if(z.ge.10.)
     &   write(ifhi,'(a,f4.2,a,f4.0,a)')'text ',xi,' 0.9 "',z,'"'
        write(ifhi,'(a)')    'array 2'
        do n=1,99
          x=dble(n)*0.01d0
          write(ifhi,'(2e11.3)')x,fom(z,x,b)
        enddo
        write(ifhi,'(a)')    '  endarray'
        write(ifhi,'(a)')    '  closehisto '
        if(i.lt.6)write(ifhi,'(a)')    'plot 0-'
        if(i.eq.6)write(ifhi,'(a)')    'plot 0'
      enddo
      end


c-----------------------------------------------------------------------
      subroutine xbDens(jjj)
c-----------------------------------------------------------------------
c plots b distribution for all pairs
c----------------------------------------------------------------
      include 'epos.inc'
      include 'epos.incems'
      common/geom/rmproj,rmtarg,bmax,bkmx

      if(jjj.eq.1)then
c prepare plot for xbDens
      if(ixbDens.eq.1)then
        iii=1     !proj
        Nnucla=0
        do ip=1,maproj
          if(lproj(ip).ne.0)then
            Nnucla=Nnucla+1
            do l=1,lproj(ip)
C...fix compilation warning             
C              k=kproj(ip,l)
C              b=bk(k)
C              i=1+int(b/bkmx*float(mxnucl))
              i=1+int(bk(kproj(ip,l))/bkmx*float(mxnucl)) 
              if(i.le.mxnucl)bnucl(i,iii)=bnucl(i,iii)+1.
            enddo
          endif
          if(lproj3(ip).ne.0)then
            do l=1,lproj3(ip)
C...fix compilation warning
C              k=kproj3(ip,l)
C              b=bk(k)
C              i=1+int(b/bkmx*float(mxnucl))
              i=1+int(bk(kproj3(ip,l))/bkmx*float(mxnucl))
              if(i.le.mxnucl)bnucl(i,iii+2)=bnucl(i,iii+2)+1.
            enddo
          endif
        enddo
        xbtot(iii)=xbtot(iii)+float(Nnucla)
        iii=2     !targ
        Nnucla=0
        do it=1,matarg
          if(ltarg(it).ne.0)then
            Nnucla=Nnucla+1
            do l=1,ltarg(it)
              k=ktarg(it,l)
              b=bk(k)
              i=1+int(b/bkmx*float(mxnucl))
              if(i.le.mxnucl)bnucl(i,iii)=bnucl(i,iii)+1.
            enddo
          endif
          if(ltarg3(it).ne.0)then
            do l=1,ltarg3(it)
              k=ktarg3(it,l)
              b=bk(k)
              i=1+int(b/bkmx*float(mxnucl))
              if(i.le.mxnucl)bnucl(i,iii+2)=bnucl(i,iii+2)+1.
            enddo
          endif
        enddo
        xbtot(iii)=xbtot(iii)+float(Nnucla)
      endif

      else

      if(xbtot(1).gt.0.)then
        xbtot(3)=xbtot(1)
        xbtot(4)=xbtot(2)
        write(ifhi,'(a)')       'openhisto'
        write(ifhi,'(a)')       'htyp lin name bdens'
        write(ifhi,'(a)')       '- txt "xaxis b (fm)" '
        write(ifhi,'(a)')       '+ txt "yaxis P(b) proj " '
        write(ifhi,'(a)')       '+ txt "yaxis P(b) targ " '
        write(ifhi,'(a)')       '+ txt "yaxis P(b) scr proj " '
        write(ifhi,'(a)')       '+ txt "yaxis P(b) scr targ " '
        write(ifhi,'(a)')       'array 5'
        db=bkmx/float(mxnucl)
        do j=1,mxnucl
          b=(j-0.5)*db
          d=pi*((b+db)**2-b**2)
          write(ifhi,'(2e12.4)') b,(bnucl(j,iii)/xbtot(iii)/d,iii=1,4)
        enddo
        write(ifhi,'(a)')       '  endarray'
        write(ifhi,'(a)')       'closehisto'
        write(ifhi,'(a)')       'plot bdens+1- plot bdens+2-'
        write(ifhi,'(a)')       'plot bdens+3- plot bdens+4 '
      endif

      endif

      end