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
// -*- C++ -*-
//
// Package:   EcalCosmicsHists
// Class:     EcalCosmicsHists
//
/**\class EcalCosmicsHists EcalCosmicsHists.cc

 Description: <one line class summary>

 Implementation:
     <Notes on implementation>
*/

#include "CaloOnlineTools/EcalTools/plugins/EcalCosmicsHists.h"
#include "DataFormats/DetId/interface/DetId.h"
#include "Geometry/CaloTopology/interface/CaloTopology.h"
#include "Geometry/Records/interface/CaloTopologyRecord.h"

#include "DataFormats/EgammaReco/interface/BasicCluster.h"
#include "DataFormats/EgammaReco/interface/BasicClusterFwd.h"
#include "DataFormats/CaloRecHit/interface/CaloClusterFwd.h"

#include "DataFormats/HcalRecHit/interface/HcalRecHitCollections.h"
#include "DataFormats/HcalDetId/interface/HcalSubdetector.h"

#include "DataFormats/L1GlobalTrigger/interface/L1GlobalTriggerReadoutSetupFwd.h"
#include "DataFormats/L1GlobalTrigger/interface/L1GlobalTriggerReadoutSetup.h"

using namespace cms;
using namespace edm;
using namespace std;

//
// constants, enums and typedefs
//

//
// static data member definitions
//

//
// constructors and destructor
//
EcalCosmicsHists::EcalCosmicsHists(const edm::ParameterSet& iConfig)
    : ecalRawDataColl_(iConfig.getParameter<edm::InputTag>("ecalRawDataColl")),
      ecalRecHitCollectionEB_(iConfig.getParameter<edm::InputTag>("ecalRecHitCollectionEB")),
      ecalRecHitCollectionEE_(iConfig.getParameter<edm::InputTag>("ecalRecHitCollectionEE")),
      barrelClusterCollection_(iConfig.getParameter<edm::InputTag>("barrelClusterCollection")),
      endcapClusterCollection_(iConfig.getParameter<edm::InputTag>("endcapClusterCollection")),
      l1GTReadoutRecTag_(iConfig.getUntrackedParameter<std::string>("L1GlobalReadoutRecord", "gtDigis")),
      l1GMTReadoutRecTag_(iConfig.getUntrackedParameter<std::string>("L1GlobalMuonReadoutRecord", "gtDigis")),
      barrelClusterToken_(consumes<reco::SuperClusterCollection>(barrelClusterCollection_)),
      endcapClusterToken_(consumes<reco::SuperClusterCollection>(endcapClusterCollection_)),
      ebRecHitToken_(consumes<EcalRecHitCollection>(ecalRecHitCollectionEB_)),
      eeRecHitToken_(consumes<EcalRecHitCollection>(ecalRecHitCollectionEE_)),
      ecalRawDataToken_(consumes<EcalRawDataCollection>(ecalRawDataColl_)),
      tracksToken_(consumes<reco::TrackCollection>(edm::InputTag("cosmicMuons"))),
      tracksBarrelToken_(consumes<reco::TrackCollection>(edm::InputTag("cosmicMuonsBarrelOnly"))),
      hbheRecHitToken_(consumes<HBHERecHitCollection>(edm::InputTag("hbhereco"))),
      hfRecHitToken_(consumes<HFRecHitCollection>(edm::InputTag("hfreco"))),
      hoRecHitToken_(consumes<HORecHitCollection>(edm::InputTag("horeco"))),
      l1MuGMTToken_(consumes<L1MuGMTReadoutCollection>(l1GMTReadoutRecTag_)),
      l1GTReadoutToken_(consumes<L1GlobalTriggerReadoutRecord>(l1GTReadoutRecTag_)),
      gtRecordToken_(consumes<L1GlobalTriggerReadoutRecord>(edm::InputTag("gtDigis"))),
      ecalADCToGeVConstantToken_(esConsumes()),
      l1MenuToken_(esConsumes()),
      ecalMappingToken_(esConsumes<edm::Transition::BeginRun>()),
      runNum_(-1),
      histRangeMax_(iConfig.getUntrackedParameter<double>("histogramMaxRange", 1.8)),
      histRangeMin_(iConfig.getUntrackedParameter<double>("histogramMinRange", 0.0)),
      minTimingAmpEB_(iConfig.getUntrackedParameter<double>("MinTimingAmpEB", 0.100)),
      minTimingAmpEE_(iConfig.getUntrackedParameter<double>("MinTimingAmpEE", 0.100)),
      minRecHitAmpEB_(iConfig.getUntrackedParameter<double>("MinRecHitAmpEB", 0.027)),
      minRecHitAmpEE_(iConfig.getUntrackedParameter<double>("MinRecHitAmpEE", 0.18)),
      minHighEnergy_(iConfig.getUntrackedParameter<double>("MinHighEnergy", 2.0)),
      fileName_(iConfig.getUntrackedParameter<std::string>("fileName", std::string("ecalCosmicHists"))),
      runInFileName_(iConfig.getUntrackedParameter<bool>("runInFileName", true)),
      startTime_(iConfig.getUntrackedParameter<double>("TimeStampStart", 1215107133.)),
      runTimeLength_(iConfig.getUntrackedParameter<double>("TimeStampLength", 3.)),
      numTimingBins_(iConfig.getUntrackedParameter<int>("TimeStampBins", 1800)) {
  naiveEvtNum_ = 0;
  cosmicCounter_ = 0;
  cosmicCounterEB_ = 0;
  cosmicCounterEEM_ = 0;
  cosmicCounterEEP_ = 0;
  cosmicCounterTopBottom_ = 0;

  // TrackAssociator parameters
  edm::ParameterSet trkParameters = iConfig.getParameter<edm::ParameterSet>("TrackAssociatorParameters");
  edm::ConsumesCollector iC = consumesCollector();
  trackParameters_.loadParameters(trkParameters, iC);
  trackAssociator_.useDefaultPropagator();

  string title1 = "Seed Energy for All Feds; Seed Energy (GeV)";
  string name1 = "SeedEnergyAllFEDs";
  int numBins = 200;  //(int)round(histRangeMax_-histRangeMin_)+1;
  allFedsHist_ = new TH1F(name1.c_str(), title1.c_str(), numBins, histRangeMin_, histRangeMax_);

  fedMap_ = new EcalFedMap();
}

EcalCosmicsHists::~EcalCosmicsHists() {}

//
// member functions
//

// ------------ method called to for each event  ------------
void EcalCosmicsHists::analyze(edm::Event const& iEvent, edm::EventSetup const& iSetup) {
  bool hasEndcapClusters = true;
  int ievt = iEvent.id().event();

  edm::Handle<reco::SuperClusterCollection> bscHandle;
  edm::Handle<reco::SuperClusterCollection> escHandle;

  naiveEvtNum_++;

  //LogDebug("EcalCosmicsHists")<< "  My Event: " << naiveEvtNum_ << " " << iEvent.id().run() << " " << iEvent.id().event() << " " << iEvent.time().value();
  //LogDebug("EcalCosmicsHists")<< "Timestamp: " << iEvent.id().run() << " " << iEvent.id().event() << " " << iEvent.time().value();

  // check DB payload
  const auto& agc = iSetup.getData(ecalADCToGeVConstantToken_);
  if (naiveEvtNum_ <= 1) {
    LogWarning("EcalCosmicsHists") << "Global EB ADC->GeV scale: " << agc.getEBValue() << " GeV/ADC count";
    LogWarning("EcalCosmicsHists") << "Global EE ADC->GeV scale: " << agc.getEEValue() << " GeV/ADC count";
  }

  //===================TIMESTAMP INFORMTION=================================
  // Code added to get the time in seconds
  unsigned int timeStampLow = (0xFFFFFFFF & iEvent.time().value());
  unsigned int timeStampHigh = (iEvent.time().value() >> 32);
  double rawEventTime = (double)(timeStampHigh) + ((double)(timeStampLow) / 1000000.);
  double eventTime = rawEventTime - startTime_;  //Notice the subtraction of the "starttime"
  //std::cout << "Event Time " << eventTime << " High " <<timeStampHigh<< " low"<<timeStampLow <<" value " <<iEvent.time().value() << std::endl;
  //========================================================================

  iEvent.getByToken(barrelClusterToken_, bscHandle);
  if (!(bscHandle.isValid())) {
    LogWarning("EcalCosmicsHists") << barrelClusterCollection_ << " not available";
    return;
  }
  LogDebug("EcalCosmicsHists") << "event " << ievt;

  iEvent.getByToken(endcapClusterToken_, escHandle);
  if (!(escHandle.isValid())) {
    LogWarning("EcalCosmicsHists") << endcapClusterCollection_ << " not available";
    hasEndcapClusters = false;
    //return;
  }

  Handle<EcalRecHitCollection> hits;
  iEvent.getByToken(ebRecHitToken_, hits);
  if (!(hits.isValid())) {
    LogWarning("EcalCosmicsHists") << ecalRecHitCollectionEB_ << " not available";
    return;
  }
  Handle<EcalRecHitCollection> hitsEE;
  iEvent.getByToken(eeRecHitToken_, hitsEE);
  if (!(hitsEE.isValid())) {
    LogWarning("EcalCosmicsHists") << ecalRecHitCollectionEE_ << " not available";
    return;
  }

  Handle<EcalRawDataCollection> DCCHeaders;
  iEvent.getByToken(ecalRawDataToken_, DCCHeaders);
  if (!DCCHeaders.isValid())
    LogWarning("EcalCosmicsHists") << "DCC headers not available";

  //make the bx histos right here
  //TODO: Right now we are filling histos for errors...
  int orbit = -100;
  int bx = -100;
  int runType = -100;

  for (EcalRawDataCollection::const_iterator headerItr = DCCHeaders->begin(); headerItr != DCCHeaders->end();
       ++headerItr) {
    headerItr->getEventSettings();
    int myorbit = headerItr->getOrbit();
    int mybx = headerItr->getBX();
    int myRunType = headerItr->getRunType();
    int FEDid = headerItr->fedId();
    TH2F* dccRuntypeHist = FEDsAndDCCRuntypeVsBxHists_[FEDid];
    if (dccRuntypeHist == nullptr) {
      initHists(FEDid);
      dccRuntypeHist = FEDsAndDCCRuntypeVsBxHists_[FEDid];
    }
    dccRuntypeHist->Fill(mybx, myRunType);

    if (bx == -100) {
      bx = mybx;
    } else if (bx != mybx) {
      LogWarning("EcalCosmicsHists") << "This header has a conflicting bx OTHERS were " << bx << " here " << mybx;
      dccBXErrorByFEDHist_->Fill(headerItr->fedId());
      if (bx != -100) {
        dccErrorVsBxHist_->Fill(bx, 0);
      }
    }

    if (runType == -100) {
      runType = myRunType;
    } else if (runType != myRunType) {
      LogWarning("EcalCosmicsHists") << "This header has a conflicting runType OTHERS were " << bx << " here " << mybx;
      dccRuntypeErrorByFEDHist_->Fill(headerItr->fedId());
      if (bx != -100)
        dccErrorVsBxHist_->Fill(bx, 2);
    }

    if (orbit == -100) {
      orbit = myorbit;
    } else if (orbit != myorbit) {
      LogWarning("EcalCosmicsHists") << "This header has a conflicting orbit; OTHERS were " << orbit << " here "
                                     << myorbit;
      dccOrbitErrorByFEDHist_->Fill(headerItr->fedId());
      if (bx != -100)
        dccErrorVsBxHist_->Fill(bx, 1);
    }
  }
  dccEventVsBxHist_->Fill(bx, runType);
  dccRuntypeHist_->Fill(runType);

  std::vector<bool> l1Triggers = determineTriggers(iEvent, iSetup);
  bool isEcalL1 = l1Triggers[4];
  bool isHCALL1 = l1Triggers[3];
  bool isRPCL1 = l1Triggers[2];
  bool isCSCL1 = l1Triggers[1];
  bool isDTL1 = l1Triggers[0];

  if (runNum_ == -1) {
    runNum_ = iEvent.id().run();
  }

  int numberOfCosmics = 0;
  int numberOfCosmicsEB = 0;
  int numberOfCosmicsEEP = 0;
  int numberOfCosmicsEEM = 0;
  int numberOfCosmicsTop = 0;
  int numberOfCosmicsBottom = 0;
  int numberOfHighEClusters = 0;
  //int eventnum = iEvent.id().event();
  std::vector<EBDetId> seeds;

  //++++++++++++++++++BEGIN LOOP OVER EB SUPERCLUSTERS+++++++++++++++++++++++++//

  const reco::SuperClusterCollection* clusterCollection_p = bscHandle.product();
  for (reco::SuperClusterCollection::const_iterator clus = clusterCollection_p->begin();
       clus != clusterCollection_p->end();
       ++clus) {
    double energy = clus->energy();
    double phi = clus->phi();
    double eta = clus->eta();
    double time = -1000.0;
    double ampli = 0.;
    double secondMin = 0.;
    double secondTime = -1000.;
    int numXtalsinCluster = 0;

    EBDetId maxDet;
    EBDetId secDet;

    numberofBCinSC_->Fill(clus->clustersSize());
    numberofBCinSCphi_->Fill(phi, clus->clustersSize());

    for (reco::CaloCluster_iterator bclus = (clus->clustersBegin()); bclus != (clus->clustersEnd()); ++bclus) {
      double cphi = (*bclus)->phi();
      double ceta = (*bclus)->eta();
      TrueBCOccupancy_->Fill(cphi, ceta);
      TrueBCOccupancyCoarse_->Fill(cphi, ceta);
    }

    std::vector<std::pair<DetId, float> > clusterDetIds = clus->hitsAndFractions();  //get these from the cluster
    for (std::vector<std::pair<DetId, float> >::const_iterator detitr = clusterDetIds.begin();
         detitr != clusterDetIds.end();
         ++detitr) {
      //Here I use the "find" on a digi collection... I have been warned...
      if ((*detitr).first.det() != DetId::Ecal) {
        std::cout << " det is " << (*detitr).first.det() << std::endl;
        continue;
      }
      if ((*detitr).first.subdetId() != EcalBarrel) {
        std::cout << " subdet is " << (*detitr).first.subdetId() << std::endl;
        continue;
      }
      EcalRecHitCollection::const_iterator thishit = hits->find((*detitr).first);
      if (thishit == hits->end())
        continue;
      //The checking above should no longer be needed...... as only those in the cluster would already have rechits..

      EcalRecHit myhit = (*thishit);

      double thisamp = myhit.energy();
      if (thisamp > minRecHitAmpEB_) {
        numXtalsinCluster++;
      }
      if (thisamp > secondMin) {
        secondMin = thisamp;
        secondTime = myhit.time();
        secDet = (EBDetId)(*detitr).first;
      }
      if (secondMin > ampli) {
        std::swap(ampli, secondMin);
        std::swap(time, secondTime);
        std::swap(maxDet, secDet);
      }
    }

    double fullnumXtalsinCluster = clusterDetIds.size();

    float E2 = ampli + secondMin;
    EcalElectronicsId elecId = ecalElectronicsMap_->getElectronicsId((EBDetId)maxDet);
    int FEDid = 600 + elecId.dccId();

    numberOfCosmics++;
    numberOfCosmicsEB++;

    //Set some more values
    seeds.push_back(maxDet);
    int ieta = maxDet.ieta();
    int iphi = maxDet.iphi();
    int ietaSM = maxDet.ietaSM();
    int iphiSM = maxDet.iphiSM();

    int LM = ecalElectronicsMap_->getLMNumber(maxDet);  //FIX ME

    // top and bottom clusters
    if (iphi > 0 && iphi < 180) {
      numberOfCosmicsTop++;
    } else {
      numberOfCosmicsBottom++;
    }

    // fill the proper hist
    TH1F* uRecHist = FEDsAndHists_[FEDid];
    TH1F* E2uRecHist = FEDsAndE2Hists_[FEDid];
    TH1F* energyuRecHist = FEDsAndenergyHists_[FEDid];
    TH1F* timingHist = FEDsAndTimingHists_[FEDid];
    TH1F* freqHist = FEDsAndFrequencyHists_[FEDid];
    TH1F* iphiProfileHist = FEDsAndiPhiProfileHists_[FEDid];
    TH1F* ietaProfileHist = FEDsAndiEtaProfileHists_[FEDid];
    TH2F* timingHistVsFreq = FEDsAndTimingVsFreqHists_[FEDid];
    TH2F* timingHistVsAmp = FEDsAndTimingVsAmpHists_[FEDid];
    TH2F* E2vsE1uRecHist = FEDsAndE2vsE1Hists_[FEDid];
    TH2F* energyvsE1uRecHist = FEDsAndenergyvsE1Hists_[FEDid];
    TH2F* occupHist = FEDsAndOccupancyHists_[FEDid];
    TH2F* timingHistVsPhi = FEDsAndTimingVsPhiHists_[FEDid];
    TH2F* timingHistVsModule = FEDsAndTimingVsModuleHists_[FEDid];

    if (uRecHist == nullptr) {
      initHists(FEDid);
      uRecHist = FEDsAndHists_[FEDid];
      E2uRecHist = FEDsAndE2Hists_[FEDid];
      energyuRecHist = FEDsAndenergyHists_[FEDid];
      timingHist = FEDsAndTimingHists_[FEDid];
      freqHist = FEDsAndFrequencyHists_[FEDid];
      timingHistVsFreq = FEDsAndTimingVsFreqHists_[FEDid];
      timingHistVsAmp = FEDsAndTimingVsAmpHists_[FEDid];
      iphiProfileHist = FEDsAndiPhiProfileHists_[FEDid];
      ietaProfileHist = FEDsAndiEtaProfileHists_[FEDid];
      E2vsE1uRecHist = FEDsAndE2vsE1Hists_[FEDid];
      energyvsE1uRecHist = FEDsAndenergyvsE1Hists_[FEDid];
      occupHist = FEDsAndOccupancyHists_[FEDid];
      timingHistVsPhi = FEDsAndTimingVsPhiHists_[FEDid];
      timingHistVsModule = FEDsAndTimingVsModuleHists_[FEDid];
    }

    uRecHist->Fill(ampli);
    E2uRecHist->Fill(E2);
    E2vsE1uRecHist->Fill(ampli, E2);
    energyuRecHist->Fill(energy);
    energyvsE1uRecHist->Fill(ampli, energy);
    allFedsHist_->Fill(ampli);
    allFedsE2Hist_->Fill(E2);
    allFedsenergyHist_->Fill(energy);
    allFedsenergyHighHist_->Fill(energy);
    allFedsE2vsE1Hist_->Fill(ampli, E2);
    allFedsenergyvsE1Hist_->Fill(ampli, energy);
    freqHist->Fill(naiveEvtNum_);
    iphiProfileHist->Fill(iphi);
    ietaProfileHist->Fill(ieta);

    allFedsFrequencyHist_->Fill(naiveEvtNum_);
    allFedsiPhiProfileHist_->Fill(iphi);
    allFedsiEtaProfileHist_->Fill(ieta);
    allOccupancy_->Fill(iphi, ieta);
    TrueOccupancy_->Fill(phi, eta);
    allOccupancyCoarse_->Fill(iphi, ieta);
    TrueOccupancyCoarse_->Fill(phi, eta);
    allFedsNumXtalsInClusterHist_->Fill(numXtalsinCluster);
    NumXtalsInClusterHist_->Fill(fullnumXtalsinCluster);
    numxtalsVsEnergy_->Fill(energy, numXtalsinCluster);
    numxtalsVsHighEnergy_->Fill(energy, numXtalsinCluster);

    //Fill the hists for the time stamp information
    allFedsFreqTimeVsPhiHist_->Fill(iphi, eventTime);
    allFedsFreqTimeVsPhiTTHist_->Fill(iphi, eventTime);
    allFedsFreqTimeVsEtaHist_->Fill(eventTime, ieta);
    allFedsFreqTimeVsEtaTTHist_->Fill(eventTime, ieta);
    //end time stamp hists

    occupHist->Fill(ietaSM, iphiSM);
    if (fullnumXtalsinCluster == 1) {
      allOccupancySingleXtal_->Fill(iphi, ieta);
      energySingleXtalHist_->Fill(energy);
    }

    // Exclusive trigger plots

    if (isEcalL1 && !isDTL1 && !isRPCL1 && !isCSCL1 && !isHCALL1) {
      allOccupancyExclusiveECAL_->Fill(iphi, ieta);
      allOccupancyCoarseExclusiveECAL_->Fill(iphi, ieta);
      if (ampli > minTimingAmpEB_) {
        allFedsTimingHistECAL_->Fill(time);
        allFedsTimingPhiEtaHistECAL_->Fill(iphi, ieta, time);
        allFedsTimingTTHistECAL_->Fill(iphi, ieta, time);
        allFedsTimingLMHistECAL_->Fill(LM, time);
      }
      triggerExclusiveHist_->Fill(0);
    }

    if (!isEcalL1 && !isDTL1 && !isRPCL1 && !isCSCL1 && isHCALL1) {
      allOccupancyExclusiveHCAL_->Fill(iphi, ieta);
      allOccupancyCoarseExclusiveHCAL_->Fill(iphi, ieta);
      if (ampli > minTimingAmpEB_) {
        allFedsTimingHistHCAL_->Fill(time);
        allFedsTimingPhiEtaHistHCAL_->Fill(iphi, ieta, time);
        allFedsTimingTTHistHCAL_->Fill(iphi, ieta, time);
        allFedsTimingLMHistHCAL_->Fill(LM, time);
      }
      triggerExclusiveHist_->Fill(1);
    }

    if (!isEcalL1 && isDTL1 && !isRPCL1 && !isCSCL1 && !isHCALL1) {
      allOccupancyExclusiveDT_->Fill(iphi, ieta);
      allOccupancyCoarseExclusiveDT_->Fill(iphi, ieta);
      if (ampli > minTimingAmpEB_) {
        allFedsTimingHistDT_->Fill(time);
        allFedsTimingPhiEtaHistDT_->Fill(iphi, ieta, time);
        allFedsTimingTTHistDT_->Fill(iphi, ieta, time);
        allFedsTimingLMHistDT_->Fill(LM, time);
      }
      triggerExclusiveHist_->Fill(2);
    }

    if (!isEcalL1 && !isDTL1 && isRPCL1 && !isCSCL1 && !isHCALL1) {
      allOccupancyExclusiveRPC_->Fill(iphi, ieta);
      allOccupancyCoarseExclusiveRPC_->Fill(iphi, ieta);
      if (ampli > minTimingAmpEB_) {
        allFedsTimingHistRPC_->Fill(time);
        allFedsTimingPhiEtaHistRPC_->Fill(iphi, ieta, time);
        allFedsTimingTTHistRPC_->Fill(iphi, ieta, time);
        allFedsTimingLMHistRPC_->Fill(LM, time);
      }
      triggerExclusiveHist_->Fill(3);
    }

    if (!isEcalL1 && !isDTL1 && !isRPCL1 && isCSCL1 && !isHCALL1) {
      allOccupancyExclusiveCSC_->Fill(iphi, ieta);
      allOccupancyCoarseExclusiveCSC_->Fill(iphi, ieta);
      if (ampli > minTimingAmpEB_) {
        allFedsTimingHistCSC_->Fill(time);
        allFedsTimingPhiEtaHistCSC_->Fill(iphi, ieta, time);
        allFedsTimingTTHistCSC_->Fill(iphi, ieta, time);
        allFedsTimingLMHistCSC_->Fill(LM, time);
      }
      triggerExclusiveHist_->Fill(4);
    }

    // Inclusive trigger plots

    if (isEcalL1) {
      triggerHist_->Fill(0);
      allOccupancyECAL_->Fill(iphi, ieta);
      allOccupancyCoarseECAL_->Fill(iphi, ieta);
    }
    if (isHCALL1) {
      triggerHist_->Fill(1);
      allOccupancyHCAL_->Fill(iphi, ieta);
      allOccupancyCoarseHCAL_->Fill(iphi, ieta);
    }
    if (isDTL1) {
      triggerHist_->Fill(2);
      allOccupancyDT_->Fill(iphi, ieta);
      allOccupancyCoarseDT_->Fill(iphi, ieta);
    }
    if (isRPCL1) {
      triggerHist_->Fill(3);
      allOccupancyRPC_->Fill(iphi, ieta);
      allOccupancyCoarseRPC_->Fill(iphi, ieta);
    }
    if (isCSCL1) {
      triggerHist_->Fill(4);
      allOccupancyCSC_->Fill(iphi, ieta);
      allOccupancyCoarseCSC_->Fill(iphi, ieta);
    }

    // Fill histo for Ecal+muon coincidence
    if (isEcalL1 && (isCSCL1 || isRPCL1 || isDTL1) && !isHCALL1)
      allFedsTimingHistEcalMuon_->Fill(time);

    if (ampli > minTimingAmpEB_) {
      timingHist->Fill(time);
      timingHistVsFreq->Fill(time, naiveEvtNum_);
      timingHistVsAmp->Fill(time, ampli);
      allFedsTimingHist_->Fill(time);
      allFedsTimingVsAmpHist_->Fill(time, ampli);
      allFedsTimingVsFreqHist_->Fill(time, naiveEvtNum_);
      timingHistVsPhi->Fill(time, iphiSM);
      timingHistVsModule->Fill(time, ietaSM);
      allFedsTimingPhiHist_->Fill(iphi, time);
      allFedsTimingPhiEtaHist_->Fill(iphi, ieta, time);
      allFedsTimingTTHist_->Fill(iphi, ieta, time);
      allFedsTimingLMHist_->Fill(LM, time);
      if (FEDid >= 610 && FEDid <= 627)
        allFedsTimingPhiEbmHist_->Fill(iphi, time);
      if (FEDid >= 628 && FEDid <= 645)
        allFedsTimingPhiEbpHist_->Fill(iphi, time);

      if (FEDid >= 610 && FEDid <= 627)
        allFedsTimingEbmHist_->Fill(time);
      if (FEDid >= 628 && FEDid <= 645)
        allFedsTimingEbpHist_->Fill(time);
      if (FEDid >= 613 && FEDid <= 616)
        allFedsTimingEbmTopHist_->Fill(time);
      if (FEDid >= 631 && FEDid <= 634)
        allFedsTimingEbpTopHist_->Fill(time);
      if (FEDid >= 622 && FEDid <= 625)
        allFedsTimingEbmBottomHist_->Fill(time);
      if (FEDid >= 640 && FEDid <= 643)
        allFedsTimingEbpBottomHist_->Fill(time);
    }

    // *** High Energy Clusters Analysis ** //

    if (energy > minHighEnergy_) {
      LogInfo("EcalCosmicsHists") << "High energy event " << iEvent.id().run() << " : " << iEvent.id().event() << " "
                                  << naiveEvtNum_ << " : " << energy << " " << numXtalsinCluster << " : " << iphi << " "
                                  << ieta << " : " << isEcalL1 << isHCALL1 << isDTL1 << isRPCL1 << isCSCL1;

      numberOfHighEClusters++;
      allOccupancyHighEnergy_->Fill(iphi, ieta);
      allOccupancyHighEnergyCoarse_->Fill(iphi, ieta);
      allFedsOccupancyHighEnergyHist_->Fill(iphi, ieta, energy);
      allFedsenergyOnlyHighHist_->Fill(energy);

      HighEnergy_2GeV_occuCoarse->Fill(iphi, ieta);
      HighEnergy_2GeV_occu3D->Fill(iphi, ieta, energy);

      HighEnergy_NumXtal->Fill(fullnumXtalsinCluster);
      HighEnergy_NumXtalFedId->Fill(FEDid, fullnumXtalsinCluster);
      HighEnergy_NumXtaliphi->Fill(iphi, fullnumXtalsinCluster);
      HighEnergy_energy3D->Fill(iphi, ieta, energy);
      HighEnergy_energyNumXtal->Fill(fullnumXtalsinCluster, energy);

      if (energy > 100.0) {
        LogInfo("EcalCosmicsHists") << "Very high energy event " << iEvent.id().run() << " : " << iEvent.id().event()
                                    << " " << naiveEvtNum_ << " : " << energy << " " << numXtalsinCluster << " : "
                                    << iphi << " " << ieta << " : " << isEcalL1 << isHCALL1 << isDTL1 << isRPCL1
                                    << isCSCL1;

        HighEnergy_100GeV_occuCoarse->Fill(iphi, ieta);
        HighEnergy_100GeV_occu3D->Fill(iphi, ieta, energy);
      }
    }

    // *** end of High Energy Clusters analysis *** //

  }  //++++++++++++++++++END LOOP OVER EB SUPERCLUSTERS+++++++++++++++++++++++//

  //+++++++++++++++++++LOOP OVER ENDCAP EE CLUSTERS++++++++++++++++++++//

  if (hasEndcapClusters) {
    clusterCollection_p = escHandle.product();
    for (reco::SuperClusterCollection::const_iterator clus = clusterCollection_p->begin();
         clus != clusterCollection_p->end();
         ++clus) {
      double energy = clus->energy();
      //double phi    = clus->phi();
      //double eta    = clus->eta();
      double time = -1000.0;
      double ampli = 0.;
      double secondMin = 0.;
      double secondTime = -1000.;
      int clusSize = clus->clustersSize();
      int numXtalsinCluster = 0;

      EEDetId maxDet;
      EEDetId secDet;
      //LogInfo("EcalCosmicsHists") << "Here is what we initialized the maxDet to: " << maxDet;

      //      for (reco::basicCluster_iterator bclus = (clus->clustersBegin()); bclus != (clus->clustersEnd()); ++bclus) {
      // 	//double cphi = (*bclus)->phi();
      // 	//double ceta = (*bclus)->eta();
      //         //TODO: extend histos to EE
      //         //TrueBCOccupancy_->Fill(cphi,ceta);
      //         //TrueBCOccupancyCoarse_->Fill(cphi,ceta);
      //       }

      std::vector<std::pair<DetId, float> > clusterDetIds = clus->hitsAndFractions();  //get these from the cluster
      for (std::vector<std::pair<DetId, float> >::const_iterator detitr = clusterDetIds.begin();
           detitr != clusterDetIds.end();
           ++detitr) {
        //LogInfo("EcalCosmicsHists") << " Here is the DetId inside the cluster: " << (EEDetId)(*detitr);
        //Here I use the "find" on a digi collection... I have been warned...

        if ((*detitr).first.det() != DetId::Ecal) {
          LogError("EcalCosmicsHists") << " det is " << (*detitr).first.det();
          continue;
        }
        if ((*detitr).first.subdetId() != EcalEndcap) {
          LogError("EcalCosmicsHists") << " subdet is " << (*detitr).first.subdetId();
          continue;
        }

        EcalRecHitCollection::const_iterator thishit = hitsEE->find((*detitr).first);

        if (thishit == hitsEE->end()) {
          LogInfo("EcalCosmicsHists") << " WARNING: EEDetId not found in the RecHit collection!";
          continue;
        }
        // The checking above should no longer be needed......
        // as only those in the cluster would already have rechits..

        EcalRecHit myhit = (*thishit);

        //LogInfo("EcalCosmicsHists") << " Found hit for DetId: " << (EEDetId)(*detitr);
        double thisamp = myhit.energy();
        if (thisamp > minRecHitAmpEE_) {
          numXtalsinCluster++;
        }
        if (thisamp > secondMin) {
          secondMin = thisamp;
          secondTime = myhit.time();
          secDet = (EEDetId)(*detitr).first;
        }
        if (secondMin > ampli) {
          std::swap(ampli, secondMin);
          std::swap(time, secondTime);
          std::swap(maxDet, secDet);
        }

        //LogInfo("EcalCosmicsHists") << "maxDetId is now: " << (EEDetId)(maxDet);
      }

      double fullnumXtalsinCluster = clusterDetIds.size();

      float E2 = ampli + secondMin;

      ecalElectronicsMap_->getElectronicsId((EEDetId)maxDet);
      //int FEDid = 600+elecId.dccId();

      //Set some more values
      //TODO: need to fix the seeds vector to be DetId or have another one for EE
      //seeds.push_back(maxDet);

      int ix = maxDet.ix();
      int iy = maxDet.iy();
      int iz = maxDet.zside();

      //      LogWarning("EcalCosmicsHists") << "EE cluster (x,y,z) : ( "
      //				     << ix << " , " << iy << " , " << iz
      //				     << " ) " << std::endl;

      if (!EEDetId::validDetId(ix, iy, iz)) {
        LogWarning("EcalCosmicsHists") << "INVALID EE DetId !!!";
        return;
      }

      numberOfCosmics++;
      if (iz < 0) {
        numberOfCosmicsEEM++;
      } else {
        numberOfCosmicsEEP++;
      }

      //int LM = ecalElectronicsMap_->getLMNumber(maxDet) ;//FIX ME

      //TODO: extend histos to EE
      //TH1F* uRecHist = FEDsAndHists_[FEDid];
      //TH1F* E2uRecHist = FEDsAndE2Hists_[FEDid];
      //TH1F* energyuRecHist = FEDsAndenergyHists_[FEDid];
      //TH1F* timingHist = FEDsAndTimingHists_[FEDid];
      //TH1F* freqHist = FEDsAndFrequencyHists_[FEDid];
      //TH1F* iphiProfileHist = FEDsAndiPhiProfileHists_[FEDid];
      //TH1F* ietaProfileHist = FEDsAndiEtaProfileHists_[FEDid];
      //TH2F* timingHistVsFreq = FEDsAndTimingVsFreqHists_[FEDid];
      //TH2F* timingHistVsAmp = FEDsAndTimingVsAmpHists_[FEDid];
      //TH2F* E2vsE1uRecHist = FEDsAndE2vsE1Hists_[FEDid];
      //TH2F* energyvsE1uRecHist = FEDsAndenergyvsE1Hists_[FEDid];
      //TH1F* numXtalInClusterHist = FEDsAndNumXtalsInClusterHists_[FEDid];
      //TH2F* occupHist = FEDsAndOccupancyHists_[FEDid];
      //TH2F* timingHistVsPhi = FEDsAndTimingVsPhiHists_[FEDid];
      //TH2F* timingHistVsModule = FEDsAndTimingVsModuleHists_[FEDid];
      //if(uRecHist==0)
      //{
      //  initHists(FEDid);
      //  uRecHist = FEDsAndHists_[FEDid];
      //  E2uRecHist = FEDsAndE2Hists_[FEDid];
      //  energyuRecHist = FEDsAndenergyHists_[FEDid];
      //  timingHist = FEDsAndTimingHists_[FEDid];
      //  freqHist = FEDsAndFrequencyHists_[FEDid];
      //  timingHistVsFreq = FEDsAndTimingVsFreqHists_[FEDid];
      //  timingHistVsAmp = FEDsAndTimingVsAmpHists_[FEDid];
      //  iphiProfileHist = FEDsAndiPhiProfileHists_[FEDid];
      //  ietaProfileHist = FEDsAndiEtaProfileHists_[FEDid];
      //  E2vsE1uRecHist = FEDsAndE2vsE1Hists_[FEDid];
      //  energyvsE1uRecHist = FEDsAndenergyvsE1Hists_[FEDid];
      //  numXtalInClusterHist = FEDsAndNumXtalsInClusterHists_[FEDid];
      //  occupHist = FEDsAndOccupancyHists_[FEDid];
      //  timingHistVsPhi = FEDsAndTimingVsPhiHists_[FEDid];
      //  timingHistVsModule = FEDsAndTimingVsModuleHists_[FEDid];
      //}
      //uRecHist->Fill(ampli);
      //E2uRecHist->Fill(E2);
      //E2vsE1uRecHist->Fill(ampli,E2);
      //energyuRecHist->Fill(energy);
      //energyvsE1uRecHist->Fill(ampli,energy);
      //allFedsHist_->Fill(ampli);

      if (iz < 0) {
        EEM_FedsSeedEnergyHist_->Fill(ampli);
        EEM_FedsenergyHist_->Fill(energy);
        EEM_FedsenergyHighHist_->Fill(energy);
        EEM_FedsE2Hist_->Fill(E2);
        EEM_FedsE2vsE1Hist_->Fill(ampli, E2);
        EEM_FedsenergyvsE1Hist_->Fill(ampli, energy);
        EEM_AllOccupancyCoarse_->Fill(ix - 0.5, iy - 0.5);
        EEM_AllOccupancy_->Fill(ix - 0.5, iy - 0.5);

        EEM_FedsNumXtalsInClusterHist_->Fill(numXtalsinCluster);
        EEM_NumXtalsInClusterHist_->Fill(fullnumXtalsinCluster);
        EEM_numxtalsVsEnergy_->Fill(energy, numXtalsinCluster);
        EEM_numxtalsVsHighEnergy_->Fill(energy, numXtalsinCluster);
        EEM_numberofBCinSC_->Fill(clusSize);

        if (fullnumXtalsinCluster == 1) {
          EEM_OccupancySingleXtal_->Fill(ix - 0.5, iy - 0.5);
          EEM_energySingleXtalHist_->Fill(energy);
        }

        if (ampli > minTimingAmpEE_) {
          EEM_FedsTimingHist_->Fill(time);
          EEM_FedsTimingVsAmpHist_->Fill(time, ampli);
          EEM_FedsTimingTTHist_->Fill(ix - 0.5, iy - 0.5, time);
        }

        // Exclusive trigger plots

        if (isEcalL1 && !isDTL1 && !isRPCL1 && !isCSCL1 && !isHCALL1) {
          EEM_OccupancyExclusiveECAL_->Fill(ix - 0.5, iy - 0.5);
          EEM_OccupancyCoarseExclusiveECAL_->Fill(ix - 0.5, iy - 0.5);
          if (ampli > minTimingAmpEE_) {
            EEM_FedsTimingHistECAL_->Fill(time);
            EEM_FedsTimingTTHistECAL_->Fill(ix - 0.5, iy - 0.5, time);
          }
          EEM_triggerExclusiveHist_->Fill(0);
        }

        if (!isEcalL1 && !isDTL1 && !isRPCL1 && !isCSCL1 && isHCALL1) {
          EEM_OccupancyExclusiveHCAL_->Fill(ix - 0.5, iy - 0.5);
          EEM_OccupancyCoarseExclusiveHCAL_->Fill(ix - 0.5, iy - 0.5);
          if (ampli > minTimingAmpEE_) {
            EEM_FedsTimingHistHCAL_->Fill(time);
            EEM_FedsTimingTTHistHCAL_->Fill(ix - 0.5, iy - 0.5, time);
          }
          EEM_triggerExclusiveHist_->Fill(1);
        }

        if (!isEcalL1 && isDTL1 && !isRPCL1 && !isCSCL1 && !isHCALL1) {
          EEM_OccupancyExclusiveDT_->Fill(ix - 0.5, iy - 0.5);
          EEM_OccupancyCoarseExclusiveDT_->Fill(ix - 0.5, iy - 0.5);
          if (ampli > minTimingAmpEE_) {
            EEM_FedsTimingHistDT_->Fill(time);
            EEM_FedsTimingTTHistDT_->Fill(ix - 0.5, iy - 0.5, time);
          }
          EEM_triggerExclusiveHist_->Fill(2);
        }

        if (!isEcalL1 && !isDTL1 && isRPCL1 && !isCSCL1 && !isHCALL1) {
          EEM_OccupancyExclusiveRPC_->Fill(ix - 0.5, iy - 0.5);
          EEM_OccupancyCoarseExclusiveRPC_->Fill(ix - 0.5, iy - 0.5);
          if (ampli > minTimingAmpEE_) {
            EEM_FedsTimingHistRPC_->Fill(time);
            EEM_FedsTimingTTHistRPC_->Fill(ix - 0.5, iy - 0.5, time);
          }
          EEM_triggerExclusiveHist_->Fill(3);
        }

        if (!isEcalL1 && !isDTL1 && !isRPCL1 && isCSCL1 && !isHCALL1) {
          EEM_OccupancyExclusiveCSC_->Fill(ix - 0.5, iy - 0.5);
          EEM_OccupancyCoarseExclusiveCSC_->Fill(ix - 0.5, iy - 0.5);
          if (ampli > minTimingAmpEE_) {
            EEM_FedsTimingHistCSC_->Fill(time);
            EEM_FedsTimingTTHistCSC_->Fill(ix - 0.5, iy - 0.5, time);
          }
          EEM_triggerExclusiveHist_->Fill(4);
        }

        // Inclusive trigger plots

        if (isEcalL1) {
          EEM_triggerHist_->Fill(0);
          EEM_OccupancyECAL_->Fill(ix - 0.5, iy - 0.5);
          EEM_OccupancyCoarseECAL_->Fill(ix - 0.5, iy - 0.5);
        }
        if (isHCALL1) {
          EEM_triggerHist_->Fill(1);
          EEM_OccupancyHCAL_->Fill(ix - 0.5, iy - 0.5);
          EEM_OccupancyCoarseHCAL_->Fill(ix - 0.5, iy - 0.5);
        }
        if (isDTL1) {
          EEM_triggerHist_->Fill(2);
          EEM_OccupancyDT_->Fill(ix - 0.5, iy - 0.5);
          EEM_OccupancyCoarseDT_->Fill(ix - 0.5, iy - 0.5);
        }
        if (isRPCL1) {
          EEM_triggerHist_->Fill(3);
          EEM_OccupancyRPC_->Fill(ix - 0.5, iy - 0.5);
          EEM_OccupancyCoarseRPC_->Fill(ix - 0.5, iy - 0.5);
        }
        if (isCSCL1) {
          EEM_triggerHist_->Fill(4);
          EEM_OccupancyCSC_->Fill(ix - 0.5, iy - 0.5);
          EEM_OccupancyCoarseCSC_->Fill(ix - 0.5, iy - 0.5);
        }

      } else {
        EEP_FedsSeedEnergyHist_->Fill(ampli);
        EEP_FedsenergyHist_->Fill(energy);
        EEP_FedsenergyHighHist_->Fill(energy);
        EEP_FedsE2Hist_->Fill(E2);
        EEP_FedsE2vsE1Hist_->Fill(ampli, E2);
        EEP_FedsenergyvsE1Hist_->Fill(ampli, energy);
        EEP_AllOccupancyCoarse_->Fill(ix - 0.5, iy - 0.5);
        EEP_AllOccupancy_->Fill(ix - 0.5, iy - 0.5);

        EEP_FedsNumXtalsInClusterHist_->Fill(numXtalsinCluster);
        EEP_NumXtalsInClusterHist_->Fill(fullnumXtalsinCluster);
        EEP_numxtalsVsEnergy_->Fill(energy, numXtalsinCluster);
        EEP_numxtalsVsHighEnergy_->Fill(energy, numXtalsinCluster);
        EEP_numberofBCinSC_->Fill(clusSize);

        if (fullnumXtalsinCluster == 1) {
          EEP_OccupancySingleXtal_->Fill(ix - 0.5, iy - 0.5);
          EEP_energySingleXtalHist_->Fill(energy);
        }

        if (ampli > minTimingAmpEE_) {
          EEP_FedsTimingHist_->Fill(time);
          EEP_FedsTimingVsAmpHist_->Fill(time, ampli);
          EEP_FedsTimingTTHist_->Fill(ix - 0.5, iy - 0.5, time);
        }

        // Exclusive trigger plots

        if (isEcalL1 && !isDTL1 && !isRPCL1 && !isCSCL1 && !isHCALL1) {
          EEP_OccupancyExclusiveECAL_->Fill(ix - 0.5, iy - 0.5);
          EEP_OccupancyCoarseExclusiveECAL_->Fill(ix - 0.5, iy - 0.5);
          if (ampli > minTimingAmpEE_) {
            EEP_FedsTimingHistECAL_->Fill(time);
            EEP_FedsTimingTTHistECAL_->Fill(ix - 0.5, iy - 0.5, time);
          }
          EEP_triggerExclusiveHist_->Fill(0);
        }

        if (!isEcalL1 && !isDTL1 && !isRPCL1 && !isCSCL1 && isHCALL1) {
          EEP_OccupancyExclusiveHCAL_->Fill(ix - 0.5, iy - 0.5);
          EEP_OccupancyCoarseExclusiveHCAL_->Fill(ix - 0.5, iy - 0.5);
          if (ampli > minTimingAmpEE_) {
            EEP_FedsTimingHistHCAL_->Fill(time);
            EEP_FedsTimingTTHistHCAL_->Fill(ix - 0.5, iy - 0.5, time);
          }
          EEP_triggerExclusiveHist_->Fill(1);
        }

        if (!isEcalL1 && isDTL1 && !isRPCL1 && !isCSCL1 && !isHCALL1) {
          EEP_OccupancyExclusiveDT_->Fill(ix - 0.5, iy - 0.5);
          EEP_OccupancyCoarseExclusiveDT_->Fill(ix - 0.5, iy - 0.5);
          if (ampli > minTimingAmpEE_) {
            EEP_FedsTimingHistDT_->Fill(time);
            EEP_FedsTimingTTHistDT_->Fill(ix - 0.5, iy - 0.5, time);
          }
          EEP_triggerExclusiveHist_->Fill(2);
        }

        if (!isEcalL1 && !isDTL1 && isRPCL1 && !isCSCL1 && !isHCALL1) {
          EEP_OccupancyExclusiveRPC_->Fill(ix - 0.5, iy - 0.5);
          EEP_OccupancyCoarseExclusiveRPC_->Fill(ix - 0.5, iy - 0.5);
          if (ampli > minTimingAmpEE_) {
            EEP_FedsTimingHistRPC_->Fill(time);
            EEP_FedsTimingTTHistRPC_->Fill(ix - 0.5, iy - 0.5, time);
          }
          EEP_triggerExclusiveHist_->Fill(3);
        }

        if (!isEcalL1 && !isDTL1 && !isRPCL1 && isCSCL1 && !isHCALL1) {
          EEP_OccupancyExclusiveCSC_->Fill(ix - 0.5, iy - 0.5);
          EEP_OccupancyCoarseExclusiveCSC_->Fill(ix - 0.5, iy - 0.5);
          if (ampli > minTimingAmpEE_) {
            EEP_FedsTimingHistCSC_->Fill(time);
            EEP_FedsTimingTTHistCSC_->Fill(ix - 0.5, iy - 0.5, time);
          }
          EEP_triggerExclusiveHist_->Fill(4);
        }

        // Inclusive trigger plots

        if (isEcalL1) {
          EEP_triggerHist_->Fill(0);
          EEP_OccupancyECAL_->Fill(ix - 0.5, iy - 0.5);
          EEP_OccupancyCoarseECAL_->Fill(ix - 0.5, iy - 0.5);
        }
        if (isHCALL1) {
          EEP_triggerHist_->Fill(1);
          EEP_OccupancyHCAL_->Fill(ix - 0.5, iy - 0.5);
          EEP_OccupancyCoarseHCAL_->Fill(ix - 0.5, iy - 0.5);
        }
        if (isDTL1) {
          EEP_triggerHist_->Fill(2);
          EEP_OccupancyDT_->Fill(ix - 0.5, iy - 0.5);
          EEP_OccupancyCoarseDT_->Fill(ix - 0.5, iy - 0.5);
        }
        if (isRPCL1) {
          EEP_triggerHist_->Fill(3);
          EEP_OccupancyRPC_->Fill(ix - 0.5, iy - 0.5);
          EEP_OccupancyCoarseRPC_->Fill(ix - 0.5, iy - 0.5);
        }
        if (isCSCL1) {
          EEP_triggerHist_->Fill(4);
          EEP_OccupancyCSC_->Fill(ix - 0.5, iy - 0.5);
          EEP_OccupancyCoarseCSC_->Fill(ix - 0.5, iy - 0.5);
        }
      }

      // *** High Energy Clusters Analysis ** //

      if (energy > minHighEnergy_) {
        LogInfo("EcalCosmicsHists") << "High energy event in EE " << iEvent.id().run() << " : " << iEvent.id().event()
                                    << " " << naiveEvtNum_ << " : " << energy << " " << numXtalsinCluster << " : " << ix
                                    << " " << iy << " : " << isEcalL1 << isHCALL1 << isDTL1 << isRPCL1 << isCSCL1;

        //  numberOfHighEClusters++;
        //  allOccupancyHighEnergy_->Fill(iphi, ieta);
        //  allOccupancyHighEnergyCoarse_->Fill(iphi, ieta);
        //  allFedsOccupancyHighEnergyHist_->Fill(iphi,ieta,energy);
        if (iz < 0) {
          EEM_FedsenergyOnlyHighHist_->Fill(energy);
          EEM_OccupancyHighEnergy_->Fill(ix - 0.5, iy - 0.5);
          EEM_OccupancyHighEnergyCoarse_->Fill(ix - 0.5, iy - 0.5);
        } else {
          EEP_FedsenergyOnlyHighHist_->Fill(energy);
        }
        //  HighEnergy_2GeV_occuCoarse->Fill(iphi,ieta);
        //  HighEnergy_2GeV_occu3D->Fill(iphi,ieta,energy);

        //  HighEnergy_NumXtal->Fill(fullnumXtalsinCluster);
        //  HighEnergy_NumXtalFedId->Fill(FEDid,fullnumXtalsinCluster);
        //  HighEnergy_NumXtaliphi->Fill(iphi,fullnumXtalsinCluster);
        //  HighEnergy_energy3D->Fill(iphi,ieta,energy);
        //  HighEnergy_energyNumXtal->Fill(fullnumXtalsinCluster,energy);

        if (energy > 100.0) {
          LogInfo("EcalCosmicsHists") << "Very high energy event in EE " << iEvent.id().run() << " : "
                                      << iEvent.id().event() << " " << naiveEvtNum_ << " : " << energy << " "
                                      << numXtalsinCluster << " : " << ix << " " << iy << " : " << isEcalL1 << isHCALL1
                                      << isDTL1 << isRPCL1 << isCSCL1;
          //    HighEnergy_100GeV_occuCoarse->Fill(iphi,ieta);
          //    HighEnergy_100GeV_occu3D->Fill(iphi,ieta,energy);
        }
      }

      // *** end of High Energy Clusters analysis *** //

    }  //++++++++++++++++++END LOOP OVER EE SUPERCLUSTERS+++++++++++++++++++++++++++++++++++
  }

  HighEnergy_numClusHighEn->Fill(numberOfHighEClusters);
  HighEnergy_ratioClusters->Fill((double(numberOfHighEClusters)) / (double(numberOfCosmics)));

  numberofCosmicsHist_->Fill(numberOfCosmics);
  EEP_numberofCosmicsHist_->Fill(numberOfCosmicsEEP);
  EEM_numberofCosmicsHist_->Fill(numberOfCosmicsEEM);
  numberofCosmicsHistEB_->Fill(numberOfCosmicsEB);

  if (numberOfCosmics > 0) {
    cosmicCounter_++;
    numberofGoodEvtFreq_->Fill(naiveEvtNum_);
    allFedsFreqTimeHist_->Fill(eventTime);
    //This line will work in 21X!!
    //std::cout << " Orbit " <<   iEvent.orbitNumber() << " BX " << iEvent.bunchCrossing()<< std::endl;
    //std::cout << " BX " << iEvent.experimentType() << std::endl;
  }

  if (numberOfCosmicsEB > 0)
    cosmicCounterEB_++;
  if (numberOfCosmicsEEP > 0)
    cosmicCounterEEP_++;
  if (numberOfCosmicsEEM > 0)
    cosmicCounterEEM_++;

  if (numberOfCosmicsTop && numberOfCosmicsBottom) {
    cosmicCounterTopBottom_++;
    numberofCosmicsTopBottomHist_->Fill(numberOfCosmicsTop + numberOfCosmicsBottom);
  }

  // *** TrackAssociator *** //

  // get reco tracks
  edm::Handle<reco::TrackCollection> recoTracks;
  iEvent.getByToken(tracksToken_, recoTracks);

  if (recoTracks.isValid()) {
    std::map<int, std::vector<DetId> > trackDetIdMap;
    int tracks = 0;
    for (reco::TrackCollection::const_iterator recoTrack = recoTracks->begin(); recoTrack != recoTracks->end();
         ++recoTrack) {
      if (fabs(recoTrack->d0()) > 70 || fabs(recoTrack->dz()) > 70)
        continue;
      if (recoTrack->numberOfValidHits() < 20)
        continue;

      //if (recoTrack->pt() < 2) continue; // skip low Pt tracks

      TrackDetMatchInfo info = trackAssociator_.associate(iEvent, iSetup, *recoTrack, trackParameters_);

      //       edm::LogVerbatim("TrackAssociator") << "\n-------------------------------------------------------\n Track (pt,eta,phi): " <<
      //    recoTrack->pt() << " , " << recoTrack->eta() << " , " << recoTrack->phi() ;
      //       edm::LogVerbatim("TrackAssociator") << "Ecal energy in crossed crystals based on RecHits: " <<
      //    info.crossedEnergy(TrackDetMatchInfo::EcalRecHits);
      //       edm::LogVerbatim("TrackAssociator") << "Ecal energy in 3x3 crystals based on RecHits: " <<
      //    info.nXnEnergy(TrackDetMatchInfo::EcalRecHits, 1);
      //       edm::LogVerbatim("TrackAssociator") << "Hcal energy in crossed towers based on RecHits: " <<
      //    info.crossedEnergy(TrackDetMatchInfo::HcalRecHits);
      //       edm::LogVerbatim("TrackAssociator") << "Hcal energy in 3x3 towers based on RecHits: " <<
      //    info.nXnEnergy(TrackDetMatchInfo::HcalRecHits, 1);
      //       edm::LogVerbatim("TrackAssociator") << "Number of muon segment matches: " << info.numberOfSegments();

      //       std::cout << "\n-------------------------------------------------------\n Track (pt,eta,phi): " <<
      //    recoTrack->pt() << " , " << recoTrack->eta() << " , " << recoTrack->phi() << std::endl;
      //       std::cout << "Ecal energy in crossed crystals based on RecHits: " <<
      //    info.crossedEnergy(TrackDetMatchInfo::EcalRecHits) << std::endl;
      //       std::cout << "Ecal energy in 3x3 crystals based on RecHits: " <<
      //    info.nXnEnergy(TrackDetMatchInfo::EcalRecHits, 1) << std::endl;
      //       std::cout << "Hcal energy in crossed towers based on RecHits: " <<
      //    info.crossedEnergy(TrackDetMatchInfo::HcalRecHits) << std::endl;
      //       std::cout << "Hcal energy in 3x3 towers based on RecHits: " <<
      //    info.nXnEnergy(TrackDetMatchInfo::HcalRecHits, 1) << std::endl;

      for (unsigned int i = 0; i < info.crossedEcalIds.size(); i++) {
        // only checks for barrel
        if (info.crossedEcalIds[i].det() == DetId::Ecal && info.crossedEcalIds[i].subdetId() == 1) {
          EBDetId ebDetId(info.crossedEcalIds[i]);
          trackAssoc_muonsEcal_->Fill(ebDetId.iphi(), ebDetId.ieta());
          //std::cout << "Crossed iphi: " << ebDetId.iphi()
          //    << " ieta: " << ebDetId.ieta() << " : nCross " << info.crossedEcalIds.size() << std::endl;

          EcalRecHitCollection::const_iterator thishit = hits->find(ebDetId);
          if (thishit == hits->end())
            continue;

          //EcalRecHit myhit = (*thishit);
          //double thisamp = myhit.energy();
          //std::cout << " Crossed energy: " << thisamp << " : nCross " << info.crossedEcalIds.size() << std::endl;
        }
      }

      //edm::LogVerbatim("TrackAssociator") << " crossedEcalIds size: " << info.crossedEcalIds.size()
      //				  << " crossedEcalRecHits size: " << info.crossedEcalRecHits.size();
      numberofCrossedEcalIdsHist_->Fill(info.crossedEcalIds.size());
      tracks++;
      if (!info.crossedEcalIds.empty())
        trackDetIdMap.insert(std::pair<int, std::vector<DetId> >(tracks, info.crossedEcalIds));
    }

    // Now to match recoTracks with cosmic clusters

    int numAssocTracks = 0;
    int numAssocClusters = 0;
    edm::LogVerbatim("TrackAssociator") << "Matching cosmic clusters to tracks...";
    int numSeeds = seeds.size();
    int numTracks = trackDetIdMap.size();
    while (!seeds.empty() && !trackDetIdMap.empty()) {
      double bestDr = 1000;
      double bestDPhi = 1000;
      double bestDEta = 1000;
      double bestEtaTrack = 1000;
      double bestEtaSeed = 1000;
      double bestPhiTrack = 1000;
      double bestPhiSeed = 1000;
      EBDetId bestTrackDet;
      EBDetId bestSeed;
      int bestTrack = -1;
      std::map<EBDetId, EBDetId> trackDetIdToSeedMap;

      //edm::LogVerbatim("TrackAssociator") << "NumTracks:" << trackDetIdMap.size() << " numClusters:" << seeds.size();

      for (std::vector<EBDetId>::const_iterator seedItr = seeds.begin(); seedItr != seeds.end(); ++seedItr) {
        for (std::map<int, std::vector<DetId> >::const_iterator mapItr = trackDetIdMap.begin();
             mapItr != trackDetIdMap.end();
             ++mapItr) {
          for (unsigned int i = 0; i < mapItr->second.size(); i++) {
            // only checks for barrel
            if (mapItr->second[i].det() == DetId::Ecal && mapItr->second[i].subdetId() == 1) {
              EBDetId ebDet = (mapItr->second[i]);
              double seedEta = seedItr->ieta();
              double deta = ebDet.ieta() - seedEta;
              if (seedEta * ebDet.ieta() < 0)
                deta > 0 ? (deta = deta - 1.) : (deta = deta + 1.);
              double dR;
              double dphi = ebDet.iphi() - seedItr->iphi();
              if (abs(dphi) > 180)
                dphi > 0 ? (dphi = 360 - dphi) : (dphi = -360 - dphi);
              dR = sqrt(deta * deta + dphi * dphi);
              if (dR < bestDr) {
                bestDr = dR;
                bestDPhi = dphi;
                bestDEta = deta;
                bestTrackDet = mapItr->second[i];
                bestTrack = mapItr->first;
                bestSeed = (*seedItr);
                bestEtaTrack = ebDet.ieta();
                bestEtaSeed = seedEta;
                bestPhiTrack = ebDet.iphi();
                bestPhiSeed = seedItr->iphi();
              }
            }
          }
        }
      }
      if (bestDr < 1000) {
        //edm::LogVerbatim("TrackAssociator") << "Best deltaR from matched DetId's to cluster:" << bestDr;
        deltaRHist_->Fill(bestDr);
        deltaPhiHist_->Fill(bestDPhi);
        deltaEtaHist_->Fill(bestDEta);
        deltaEtaDeltaPhiHist_->Fill(bestDEta, bestDPhi);
        seedTrackEtaHist_->Fill(bestEtaSeed, bestEtaTrack);
        seedTrackPhiHist_->Fill(bestPhiSeed, bestPhiTrack);
        seeds.erase(find(seeds.begin(), seeds.end(), bestSeed));
        trackDetIdMap.erase(trackDetIdMap.find(bestTrack));
        trackDetIdToSeedMap[bestTrackDet] = bestSeed;
        numAssocTracks++;
        numAssocClusters++;

        // for high energy analysis
        if (bestDPhi < 1.5 && bestDEta < 1.8) {
          //edm::LogVerbatim("TrackAssociator") << "Best seed ieta and iphi: ("
          //					<< bestSeed.ieta() << ", " << bestSeed.iphi() << ") ";
          //check if the bestSeed is a High Energy one
          EcalRecHitCollection::const_iterator Ecalhit = hits->find(bestSeed);
          if (Ecalhit == hits->end()) {
            continue;
          }
          double EcalhitEnergy = Ecalhit->energy();
          //edm::LogVerbatim("TrackAssociator") << "Best seed energy: " << EcalhitEnergy ;
          HighEnergy_bestSeed->Fill(EcalhitEnergy);
          HighEnergy_bestSeedOccupancy->Fill(bestSeed.iphi(), bestSeed.ieta());
        }
      } else {
        edm::LogVerbatim("TrackAssociator") << "could not match cluster seed to track." << bestDr;
        break;  // no match found
      }
    }
    if (numSeeds > 0 && numTracks > 0) {
      ratioAssocClustersHist_->AddBinContent(1, numAssocClusters);
      ratioAssocClustersHist_->AddBinContent(2, numSeeds);
    }
    if (numTracks > 0) {
      ratioAssocTracksHist_->AddBinContent(1, numAssocTracks);
      ratioAssocTracksHist_->AddBinContent(2, numTracks);
      numberofCosmicsWTrackHist_->Fill(numSeeds);
    }
  } else {
    LogWarning("EcalCosmicsHists") << "!!! No TrackAssociator recoTracks !!!";
  }

  // Study on Tracks for High Energy events
  edm::Handle<reco::TrackCollection> recoTracksBarrel;
  iEvent.getByToken(tracksBarrelToken_, recoTracksBarrel);
  if (!recoTracksBarrel.isValid()) {
    edm::LogWarning("EcalCosmicsHists") << "RecoTracksBarrel not valid!! ";
  } else {
    HighEnergy_numRecoTrackBarrel->Fill(recoTracksBarrel->size());
    for (reco::TrackCollection::const_iterator recoTrack = recoTracksBarrel->begin();
         recoTrack != recoTracksBarrel->end();
         ++recoTrack) {
      //edm::LogWarning("EcalCosmicsHists") << "Track (pt,eta,phi): " << recoTrack->pt() << " , " << recoTrack->eta() << " , " << recoTrack->phi() ;
      //edm::LogWarning("EcalCosmicsHists") << "Track innermost hit: " << recoTrack->innerPosition().phi() ;
    }

    for (reco::SuperClusterCollection::const_iterator clus = clusterCollection_p->begin();
         clus != clusterCollection_p->end();
         ++clus) {
      double energy = clus->energy();
      double phi = clus->phi();
      double eta = clus->eta();

      if (recoTracksBarrel->empty())
        HighEnergy_0tracks_occu3D->Fill(phi, eta, energy);
      if (recoTracksBarrel->size() == 1)
        HighEnergy_1tracks_occu3D->Fill(phi, eta, energy);
      if (recoTracksBarrel->size() == 2)
        HighEnergy_2tracks_occu3D->Fill(phi, eta, energy);

      std::vector<std::pair<DetId, float> > clusterDetIds = clus->hitsAndFractions();  //get these from the cluster
      for (std::vector<std::pair<DetId, float> >::const_iterator detitr = clusterDetIds.begin();
           detitr != clusterDetIds.end();
           ++detitr) {
        if ((*detitr).first.det() != DetId::Ecal) {
          continue;
        }
        if ((*detitr).first.subdetId() != EcalBarrel) {
          continue;
        }
        EcalRecHitCollection::const_iterator thishit = hits->find((*detitr).first);
        if (thishit == hits->end()) {
          continue;
        }

        double rechitenergy = thishit->energy();
        int ieta = ((EBDetId)(*detitr).first).ieta();
        int iphi = ((EBDetId)(*detitr).first).iphi();
        if (rechitenergy > minRecHitAmpEB_) {
          if (recoTracksBarrel->empty())
            HighEnergy_0tracks_occu3DXtal->Fill(iphi, ieta, rechitenergy);
          if (recoTracksBarrel->size() == 1)
            HighEnergy_1tracks_occu3DXtal->Fill(iphi, ieta, rechitenergy);
          if (recoTracksBarrel->size() == 2)
            HighEnergy_2tracks_occu3DXtal->Fill(iphi, ieta, rechitenergy);

          if (rechitenergy > 10)
            edm::LogWarning("EcalCosmicsHists") << "!!!!! Crystal with energy " << rechitenergy << " at (ieta,iphi) ("
                                                << ieta << " ," << iphi << "); Id: " << (*detitr).first.det();
        }
      }
    }

    // look at angle between 2 recoTracks
    if (recoTracksBarrel->size() == 2) {
      reco::TrackCollection::const_iterator Track1 = recoTracksBarrel->begin();
      reco::TrackCollection::const_iterator Track2 = (recoTracksBarrel->begin()) + 1;
      float angle = (acos(sin(Track1->theta()) * cos(Track1->phi()) * sin(Track2->theta()) * cos(Track2->phi()) +
                          sin(Track1->theta()) * sin(Track1->phi()) * sin(Track2->theta()) * sin(Track2->phi()) +
                          cos(Track1->theta()) * cos(Track2->theta()))) /
                    0.017453292519943;
      //edm::LogWarning("EcalCosmicsHists") << "Tracks angle: " << angle;
      HighEnergy_TracksAngle->Fill(angle);
      if ((Track1->innerPosition().phi()) > 0 && (Track2->innerPosition().phi()) < 0) {
        //edm::LogWarning("EcalCosmicsHists") << "Top-bottom tracks";
        HighEnergy_TracksAngleTopBottom->Fill(angle);
      }
    }
  }

  // *** end of TrackAssociator code *** //

  // *** HCAL RecHits code *** //

  //hcal rechits
  edm::Handle<HBHERecHitCollection> hbhe;
  iEvent.getByToken(hbheRecHitToken_, hbhe);

  edm::Handle<HFRecHitCollection> hfrh;
  iEvent.getByToken(hfRecHitToken_, hfrh);

  edm::Handle<HORecHitCollection> horh;
  iEvent.getByToken(hoRecHitToken_, horh);

  if (hbhe.isValid()) {
    const HBHERecHitCollection hbheHit = *(hbhe.product());
    for (HBHERecHitCollection::const_iterator hhit = hbheHit.begin(); hhit != hbheHit.end(); hhit++) {
      //      if (hhit->energy() > 0.6){
      hcalEnergy_HBHE_->Fill(hhit->energy());
      //      }
    }
  } else {
    LogWarning("EcalCosmicHists") << " HBHE RecHits **NOT** VALID!! ";
  }

  if (hfrh.isValid()) {
    const HFRecHitCollection hfHit = *(hfrh.product());
    for (HFRecHitCollection::const_iterator hhit = hfHit.begin(); hhit != hfHit.end(); hhit++) {
      hcalEnergy_HF_->Fill(hhit->energy());
    }
  } else {
    LogWarning("EcalCosmicHists") << " HF RecHits **NOT** VALID!! ";
  }

  if (horh.isValid()) {
    const HORecHitCollection hoHit = *(horh.product());
    for (HORecHitCollection::const_iterator hhit = hoHit.begin(); hhit != hoHit.end(); hhit++) {
      //     if (hhit->energy() > 0.6){
      hcalEnergy_HO_->Fill(hhit->energy());
      //     }
    }
  } else {
    LogWarning("EcalCosmicHists") << " HO RecHits **NOT** VALID!! ";
  }

  // *** end of HCAL code *** //
}

std::vector<bool> EcalCosmicsHists::determineTriggers(const edm::Event& iEvent, const edm::EventSetup& eventSetup) {
  std::vector<bool> l1Triggers;  //DT,CSC,RPC,HCAL,ECAL
                                 //0 , 1 , 2 , 3  , 4
  l1Triggers.reserve(5);
  for (int i = 0; i < 5; i++)
    l1Triggers.push_back(false);

  // get the GMTReadoutCollection
  Handle<L1MuGMTReadoutCollection> gmtrc_handle;
  iEvent.getByToken(l1MuGMTToken_, gmtrc_handle);
  L1MuGMTReadoutCollection const* gmtrc = gmtrc_handle.product();
  if (!(gmtrc_handle.isValid())) {
    LogWarning("EcalCosmicsHists") << "l1MuGMTReadoutCollection"
                                   << " not available";
    return l1Triggers;
  }
  // get hold of L1GlobalReadoutRecord
  Handle<L1GlobalTriggerReadoutRecord> L1GTRR;
  iEvent.getByToken(l1GTReadoutToken_, L1GTRR);

  //Ecal
  const auto& menu = eventSetup.getData(l1MenuToken_);
  edm::Handle<L1GlobalTriggerReadoutRecord> gtRecord;
  iEvent.getByToken(gtRecordToken_, gtRecord);
  // Get dWord after masking disabled bits
  const DecisionWord dWord = gtRecord->decisionWord();

  bool l1SingleEG1 = menu.gtAlgorithmResult("L1_SingleEG1", dWord);
  bool l1SingleEG5 = menu.gtAlgorithmResult("L1_SingleEG5", dWord);
  bool l1SingleEG8 = menu.gtAlgorithmResult("L1_SingleEG8", dWord);
  bool l1SingleEG10 = menu.gtAlgorithmResult("L1_SingleEG10", dWord);
  bool l1SingleEG12 = menu.gtAlgorithmResult("L1_SingleEG12", dWord);
  bool l1SingleEG15 = menu.gtAlgorithmResult("L1_SingleEG15", dWord);
  bool l1SingleEG20 = menu.gtAlgorithmResult("L1_SingleEG20", dWord);
  bool l1SingleEG25 = menu.gtAlgorithmResult("L1_SingleEG25", dWord);
  bool l1DoubleNoIsoEGBTBtight = menu.gtAlgorithmResult("L1_DoubleNoIsoEG_BTB_tight", dWord);
  bool l1DoubleNoIsoEGBTBloose = menu.gtAlgorithmResult("L1_DoubleNoIsoEG_BTB_loose ", dWord);
  bool l1DoubleNoIsoEGTopBottom = menu.gtAlgorithmResult("L1_DoubleNoIsoEGTopBottom", dWord);
  bool l1DoubleNoIsoEGTopBottomCen = menu.gtAlgorithmResult("L1_DoubleNoIsoEGTopBottomCen", dWord);
  bool l1DoubleNoIsoEGTopBottomCen2 = menu.gtAlgorithmResult("L1_DoubleNoIsoEGTopBottomCen2", dWord);
  bool l1DoubleNoIsoEGTopBottomCenVert = menu.gtAlgorithmResult("L1_DoubleNoIsoEGTopBottomCenVert", dWord);

  l1Triggers[4] = l1SingleEG1 || l1SingleEG5 || l1SingleEG8 || l1SingleEG10 || l1SingleEG12 || l1SingleEG15 ||
                  l1SingleEG20 || l1SingleEG25 || l1DoubleNoIsoEGBTBtight || l1DoubleNoIsoEGBTBloose ||
                  l1DoubleNoIsoEGTopBottom || l1DoubleNoIsoEGTopBottomCen || l1DoubleNoIsoEGTopBottomCen2 ||
                  l1DoubleNoIsoEGTopBottomCenVert;

  std::vector<L1MuGMTReadoutRecord> gmt_records = gmtrc->getRecords();
  std::vector<L1MuGMTReadoutRecord>::const_iterator igmtrr;
  for (igmtrr = gmt_records.begin(); igmtrr != gmt_records.end(); igmtrr++) {
    std::vector<L1MuRegionalCand>::const_iterator iter1;
    std::vector<L1MuRegionalCand> rmc;

    //DT triggers
    int idt = 0;
    rmc = igmtrr->getDTBXCands();
    for (iter1 = rmc.begin(); iter1 != rmc.end(); iter1++) {
      if (!(*iter1).empty()) {
        idt++;
      }
    }
    //if(idt>0) std::cout << "Found " << idt << " valid DT candidates in bx wrt. L1A = "
    //  << igmtrr->getBxInEvent() << std::endl;
    if (igmtrr->getBxInEvent() == 0 && idt > 0)
      l1Triggers[0] = true;

    //RPC triggers
    int irpcb = 0;
    rmc = igmtrr->getBrlRPCCands();
    for (iter1 = rmc.begin(); iter1 != rmc.end(); iter1++) {
      if (!(*iter1).empty()) {
        irpcb++;
      }
    }
    //if(irpcb>0) std::cout << "Found " << irpcb << " valid RPC candidates in bx wrt. L1A = "
    //  << igmtrr->getBxInEvent() << std::endl;
    if (igmtrr->getBxInEvent() == 0 && irpcb > 0)
      l1Triggers[2] = true;

    //CSC Triggers
    int icsc = 0;
    rmc = igmtrr->getCSCCands();
    for (iter1 = rmc.begin(); iter1 != rmc.end(); iter1++) {
      if (!(*iter1).empty()) {
        icsc++;
      }
    }
    //if(icsc>0) std::cout << "Found " << icsc << " valid CSC candidates in bx wrt. L1A = "
    //  << igmtrr->getBxInEvent() << std::endl;
    if (igmtrr->getBxInEvent() == 0 && icsc > 0)
      l1Triggers[1] = true;
  }

  L1GlobalTriggerReadoutRecord const* gtrr = L1GTRR.product();

  for (int ibx = -1; ibx <= 1; ibx++) {
    bool hcal_top = false;
    bool hcal_bot = false;
    const L1GtPsbWord psb = gtrr->gtPsbWord(0xbb0d, ibx);
    std::vector<int> valid_phi;
    if ((psb.aData(4) & 0x3f) >= 1) {
      valid_phi.push_back((psb.aData(4) >> 10) & 0x1f);
    }
    if ((psb.bData(4) & 0x3f) >= 1) {
      valid_phi.push_back((psb.bData(4) >> 10) & 0x1f);
    }
    if ((psb.aData(5) & 0x3f) >= 1) {
      valid_phi.push_back((psb.aData(5) >> 10) & 0x1f);
    }
    if ((psb.bData(5) & 0x3f) >= 1) {
      valid_phi.push_back((psb.bData(5) >> 10) & 0x1f);
    }
    std::vector<int>::const_iterator iphi;
    for (iphi = valid_phi.begin(); iphi != valid_phi.end(); iphi++) {
      //std::cout << "Found HCAL mip with phi=" << *iphi << " in bx wrt. L1A = " << ibx << std::endl;
      if (*iphi < 9)
        hcal_top = true;
      if (*iphi > 8)
        hcal_bot = true;
    }
    if (ibx == 0 && hcal_top && hcal_bot)
      l1Triggers[3] = true;
  }

  edm::LogInfo("EcalCosmicsHists") << "**** Trigger SourceSource ****";
  if (l1Triggers[0])
    edm::LogInfo("EcalCosmicsHists") << "DT";
  if (l1Triggers[2])
    edm::LogInfo("EcalCosmicsHists") << "RPC";
  if (l1Triggers[1])
    edm::LogInfo("EcalCosmicsHists") << "CSC";
  if (l1Triggers[3])
    edm::LogInfo("EcalCosmicsHists") << "HCAL";
  if (l1Triggers[4])
    edm::LogInfo("EcalCosmicsHists") << "ECAL";
  edm::LogInfo("EcalCosmicsHists") << "************************";

  return l1Triggers;
}

// insert the hist map into the map keyed by FED number
void EcalCosmicsHists::initHists(int FED) {
  using namespace std;

  string FEDid = intToString(FED);
  string title1 = "Energy of Seed Crystal ";
  title1.append(fedMap_->getSliceFromFed(FED));
  title1.append(";Seed Energy (GeV);Number of Cosmics");
  string name1 = "SeedEnergyFED";
  name1.append(intToString(FED));
  int numBins = 200;  //(int)round(histRangeMax_-histRangeMin_)+1;
  TH1F* hist = new TH1F(name1.c_str(), title1.c_str(), numBins, histRangeMin_, histRangeMax_);
  FEDsAndHists_[FED] = hist;
  FEDsAndHists_[FED]->SetDirectory(nullptr);

  TH1F* E2hist = new TH1F(Form("E2_FED_%d", FED), Form("E2_FED_%d", FED), numBins, histRangeMin_, histRangeMax_);
  FEDsAndE2Hists_[FED] = E2hist;
  FEDsAndE2Hists_[FED]->SetDirectory(nullptr);

  TH1F* energyhist =
      new TH1F(Form("Energy_FED_%d", FED), Form("Energy_FED_%d", FED), numBins, histRangeMin_, histRangeMax_);
  FEDsAndenergyHists_[FED] = energyhist;
  FEDsAndenergyHists_[FED]->SetDirectory(nullptr);

  TH2F* E2vsE1hist = new TH2F(Form("E2vsE1_FED_%d", FED),
                              Form("E2vsE1_FED_%d", FED),
                              numBins,
                              histRangeMin_,
                              histRangeMax_,
                              numBins,
                              histRangeMin_,
                              histRangeMax_);
  FEDsAndE2vsE1Hists_[FED] = E2vsE1hist;
  FEDsAndE2vsE1Hists_[FED]->SetDirectory(nullptr);

  TH2F* energyvsE1hist = new TH2F(Form("EnergyvsE1_FED_%d", FED),
                                  Form("EnergyvsE1_FED_%d", FED),
                                  numBins,
                                  histRangeMin_,
                                  histRangeMax_,
                                  numBins,
                                  histRangeMin_,
                                  histRangeMax_);
  FEDsAndenergyvsE1Hists_[FED] = energyvsE1hist;
  FEDsAndenergyvsE1Hists_[FED]->SetDirectory(nullptr);

  title1 = "Time for ";
  title1.append(fedMap_->getSliceFromFed(FED));
  title1.append(";Relative Time (1 clock = 25ns);Events");
  name1 = "TimeFED";
  name1.append(intToString(FED));
  TH1F* timingHist = new TH1F(name1.c_str(), title1.c_str(), 78, -7, 7);
  FEDsAndTimingHists_[FED] = timingHist;
  FEDsAndTimingHists_[FED]->SetDirectory(nullptr);

  TH1F* freqHist =
      new TH1F(Form("Frequency_FED_%d", FED), Form("Frequency for FED %d;Event Number", FED), 100, 0., 100000);
  FEDsAndFrequencyHists_[FED] = freqHist;
  FEDsAndFrequencyHists_[FED]->SetDirectory(nullptr);

  TH1F* iphiProfileHist =
      new TH1F(Form("iPhi_Profile_FED_%d", FED), Form("iPhi Profile for FED %d", FED), 360, 1., 361);
  FEDsAndiPhiProfileHists_[FED] = iphiProfileHist;
  FEDsAndiPhiProfileHists_[FED]->SetDirectory(nullptr);

  TH1F* ietaProfileHist =
      new TH1F(Form("iEta_Profile_FED_%d", FED), Form("iEta Profile for FED %d", FED), 172, -86, 86);
  FEDsAndiEtaProfileHists_[FED] = ietaProfileHist;
  FEDsAndiEtaProfileHists_[FED]->SetDirectory(nullptr);

  TH2F* timingHistVsFreq =
      new TH2F(Form("timeVsFreqFED_%d", FED), Form("time Vs Freq FED %d", FED), 78, -7, 7, 100, 0., 100000);
  FEDsAndTimingVsFreqHists_[FED] = timingHistVsFreq;
  FEDsAndTimingVsFreqHists_[FED]->SetDirectory(nullptr);

  TH2F* timingHistVsAmp = new TH2F(
      Form("timeVsAmpFED_%d", FED), Form("time Vs Amp FED %d", FED), 78, -7, 7, numBins, histRangeMin_, histRangeMax_);
  FEDsAndTimingVsAmpHists_[FED] = timingHistVsAmp;
  FEDsAndTimingVsAmpHists_[FED]->SetDirectory(nullptr);

  TH1F* numXtalInClusterHist = new TH1F(Form("NumXtalsInCluster_FED_%d", FED),
                                        Form("Num active Xtals In Cluster for FED %d;Num Active Xtals", FED),
                                        25,
                                        0,
                                        25);
  FEDsAndNumXtalsInClusterHists_[FED] = numXtalInClusterHist;
  FEDsAndNumXtalsInClusterHists_[FED]->SetDirectory(nullptr);

  TH2F* OccupHist = new TH2F(Form("occupFED_%d", FED), Form("Occupancy FED %d;i#eta;i#phi", FED), 85, 1, 86, 20, 1, 21);
  FEDsAndOccupancyHists_[FED] = OccupHist;
  FEDsAndOccupancyHists_[FED]->SetDirectory(nullptr);

  TH2F* timingHistVsPhi = new TH2F(Form("timeVsPhiFED_%d", FED),
                                   Form("time Vs Phi FED %d;Relative Time (1 clock = 25ns);i#phi", FED),
                                   78,
                                   -7,
                                   7,
                                   20,
                                   1,
                                   21);
  FEDsAndTimingVsPhiHists_[FED] = timingHistVsPhi;
  FEDsAndTimingVsPhiHists_[FED]->SetDirectory(nullptr);

  TH2F* timingHistVsModule = new TH2F(Form("timeVsModuleFED_%d", FED),
                                      Form("time Vs Module FED %d;Relative Time (1 clock = 25ns);i#eta", FED),
                                      78,
                                      -7,
                                      7,
                                      4,
                                      1,
                                      86);
  FEDsAndTimingVsModuleHists_[FED] = timingHistVsModule;
  FEDsAndTimingVsModuleHists_[FED]->SetDirectory(nullptr);

  TH2F* dccRuntypeVsBxFED =
      new TH2F(Form("DCCRuntypeVsBxFED_%d", FED), Form("DCC Runtype vs. BX FED %d", FED), 3600, 0, 3600, 24, 0, 24);
  FEDsAndDCCRuntypeVsBxHists_[FED] = dccRuntypeVsBxFED;
  FEDsAndDCCRuntypeVsBxHists_[FED]->SetDirectory(nullptr);
}

// ------------ method called once each job just before starting event loop  ------------
void EcalCosmicsHists::beginRun(edm::Run const&, edm::EventSetup const& eventSetup) {
  ecalElectronicsMap_ = &eventSetup.getData(ecalMappingToken_);

  //Here I will init some of the specific histograms
  int numBins = 200;  //(int)round(histRangeMax_-histRangeMin_)+1;

  //=============Special Bins for TT and Modules borders=============================
  double ttEtaBins[36] = {-85, -80, -75, -70, -65, -60, -55, -50, -45, -40, -35, -30, -25, -20, -15, -10, -5, 0,
                          1,   6,   11,  16,  21,  26,  31,  36,  41,  46,  51,  56,  61,  66,  71,  76,  81, 86};
  double modEtaBins[10] = {-85, -65, -45, -25, 0, 1, 26, 46, 66, 86};
  double ttPhiBins[73];
  double modPhiBins[19];
  double timingBins[79];
  double highEBins[11];
  for (int i = 0; i < 79; ++i) {
    timingBins[i] = -7. + double(i) * 14. / 78.;
    if (i < 73) {
      ttPhiBins[i] = 1 + 5 * i;
      if (i < 19) {
        modPhiBins[i] = 1 + 20 * i;
        if (i < 11) {
          highEBins[i] = 10. + double(i) * 20.;
        }
      }
    }
  }
  //=============END Special Bins for TT and Modules borders===========================

  //====================Frequency for Event timing information=========================
  double timingEndInSeconds = runTimeLength_ * 3600.;
  double timingBinWidth = timingEndInSeconds / double(numTimingBins_);
  //====================END Frequency for Event timing information=====================

  allFedsenergyHist_ =
      new TH1F("energy_AllClusters", "energy_AllClusters;Cluster Energy (GeV)", numBins, histRangeMin_, histRangeMax_);
  allFedsenergyHighHist_ =
      new TH1F("energyHigh_AllClusters", "energyHigh_AllClusters;Cluster Energy (GeV)", numBins, histRangeMin_, 200.0);
  allFedsenergyOnlyHighHist_ = new TH1F("energyHigh_HighEnergyClusters",
                                        "energy of High Energy Clusters;Cluster Energy (GeV)",
                                        numBins,
                                        histRangeMin_,
                                        200.0);
  allFedsE2Hist_ = new TH1F(
      "E2_AllClusters", "E2_AllClusters;Seed+highest neighbor energy (GeV)", numBins, histRangeMin_, histRangeMax_);
  allFedsE2vsE1Hist_ = new TH2F("E2vsE1_AllClusters",
                                "E2vsE1_AllClusters;Seed Energy (GeV);Seed+highest neighbor energy (GeV)",
                                numBins,
                                histRangeMin_,
                                histRangeMax_,
                                numBins,
                                histRangeMin_,
                                histRangeMax_);
  allFedsenergyvsE1Hist_ = new TH2F("energyvsE1_AllClusters",
                                    "energyvsE1_AllClusters;Seed Energy (GeV);Energy(GeV)",
                                    numBins,
                                    histRangeMin_,
                                    histRangeMax_,
                                    numBins,
                                    histRangeMin_,
                                    histRangeMax_);
  allFedsTimingHist_ = new TH1F("timeForAllFeds", "timeForAllFeds;Relative Time (1 clock = 25ns)", 78, -7, 7);
  allFedsTimingVsFreqHist_ = new TH2F("timeVsFreqAllEvent",
                                      "time Vs Freq All events;Relative Time (1 clock = 25ns);Event Number",
                                      78,
                                      -7,
                                      7,
                                      2000,
                                      0.,
                                      200000);
  allFedsTimingVsAmpHist_ = new TH2F("timeVsAmpAllEvents",
                                     "time Vs Amp All Events;Relative Time (1 clock = 25ns);Amplitude (GeV)",
                                     78,
                                     -7,
                                     7,
                                     numBins,
                                     histRangeMin_,
                                     histRangeMax_);
  allFedsFrequencyHist_ = new TH1F("FrequencyAllEvent", "Frequency for All events;Event Number", 2000, 0., 200000);

  //--------Special Hists for times stamp info----------------------------------------------
  allFedsFreqTimeHist_ = new TH1F("FrequencyAllEventsInTime",
                                  Form("Time of Cosmic Events; Time (s);Passing Event rate/%5g s", timingBinWidth),
                                  numTimingBins_,
                                  0.,
                                  timingEndInSeconds);
  allFedsFreqTimeVsPhiHist_ =
      new TH2F("FrequencyAllEventsInTimeVsPhi",
               Form("Time of Cosmic Events vs iPhi; iPhi;Time (s)/%5g s", timingBinWidth * 360.),
               360,
               1.,
               361.,
               numTimingBins_ / 360,
               0.,
               timingEndInSeconds);
  allFedsFreqTimeVsPhiTTHist_ =
      new TH2F("FrequencyAllEventsInTimeVsTTPhi",
               Form("Time of Cosmic Events vs iPhi (TT bins); iPhi;Time (s)/%5g s", timingBinWidth * 72.),
               72,
               1.,
               361.,
               numTimingBins_ / 72,
               0.,
               timingEndInSeconds);
  allFedsFreqTimeVsEtaHist_ =
      new TH2F("FrequencyAllEventsInTimeVsEta",
               Form("Time of Cosmic Events vs iEta; Time (s)/%5g s; iEta", timingBinWidth * 172.),
               numTimingBins_ / 172,
               0.,
               timingEndInSeconds,
               172,
               -86.,
               86.);
  allFedsFreqTimeVsEtaTTHist_ =
      new TH2F("FrequencyAllEventsInTimeVsTTEta",
               Form("Time of Cosmic Events vs Eta (TT bins);Time (s)/%5g s; iEta", timingBinWidth * 35.),
               numTimingBins_ / 35,
               0.,
               timingEndInSeconds,
               35,
               ttEtaBins);
  //--------END Special Hists for times stamp info------------------------------------------

  allFedsiPhiProfileHist_ = new TH1F("iPhiProfileAllEvents", "iPhi Profile all events;i#phi", 360, 1., 361.);
  allFedsiEtaProfileHist_ = new TH1F("iEtaProfileAllEvents", "iEta Profile all events;i#eta", 172, -86, 86);

  allOccupancy_ = new TH2F("OccupancyAllEvents", "Occupancy all events;i#phi;i#eta", 360, 1., 361., 172, -86, 86);
  TrueOccupancy_ =
      new TH2F("TrueOccupancyAllEvents", "True Occupancy all events;#phi;#eta", 360, -3.14159, 3.14159, 172, -1.5, 1.5);
  allOccupancyCoarse_ =
      new TH2F("OccupancyAllEventsCoarse", "Occupancy all events Coarse;i#phi;i#eta", 360 / 5, 1, 361., 35, ttEtaBins);
  TrueOccupancyCoarse_ = new TH2F("TrueOccupancyAllEventsCoarse",
                                  "True Occupancy all events Coarse;#phi;#eta",
                                  360 / 5,
                                  -3.14159,
                                  3.14159,
                                  34,
                                  -1.5,
                                  1.5);

  // single xtal cluster occupancy
  allOccupancySingleXtal_ =
      new TH2F("OccupancySingleXtal", "Occupancy single xtal clusters;i#phi;i#eta", 360, 1., 361., 172, -86, 86);
  energySingleXtalHist_ = new TH1F(
      "energy_SingleXtalClusters", "Energy single xtal clusters;Cluster Energy (GeV)", numBins, histRangeMin_, 200.0);

  allFedsTimingPhiHist_ = new TH2F("timePhiAllFEDs",
                                   "time vs Phi for all FEDs (TT binning);i#phi;Relative Time (1 clock = 25ns)",
                                   72,
                                   1,
                                   361,
                                   78,
                                   -7,
                                   7);
  allFedsTimingPhiEbpHist_ = new TH2F("timePhiEBP",
                                      "time vs Phi for FEDs in EB+ (TT binning) ;i#phi;Relative Time (1 clock = 25ns)",
                                      72,
                                      1,
                                      361,
                                      78,
                                      -7,
                                      7);
  allFedsTimingPhiEbmHist_ = new TH2F("timePhiEBM",
                                      "time vs Phi for FEDs in EB- (TT binning);i#phi;Relative Time (1 clock = 25ns)",
                                      72,
                                      1,
                                      361,
                                      78,
                                      -7,
                                      7);
  allFedsTimingPhiEtaHist_ =
      new TH3F("timePhiEtaAllFEDs",
               "(Phi,Eta,time) for all FEDs (SM,M binning);i#phi;i#eta;Relative Time (1 clock = 25ns)",
               18,
               modPhiBins,
               9,
               modEtaBins,
               78,
               timingBins);
  allFedsTimingTTHist_ =
      new TH3F("timeTTAllFEDs",
               "(Phi,Eta,time) for all FEDs (SM,TT binning);i#phi;i#eta;Relative Time (1 clock = 25ns)",
               72,
               ttPhiBins,
               35,
               ttEtaBins,
               78,
               timingBins);
  allFedsTimingLMHist_ = new TH2F(
      "timeLMAllFEDs", "(LM,time) for all FEDs (SM,LM binning);LM;Relative Time (1 clock = 25ns)", 92, 1, 92, 78, -7, 7);

  allFedsTimingEbpHist_ = new TH1F("timeEBP", "time for FEDs in EB+;Relative Time (1 clock = 25ns)", 78, -7, 7);
  allFedsTimingEbmHist_ = new TH1F("timeEBM", "time for FEDs in EB-;Relative Time (1 clock = 25ns)", 78, -7, 7);
  allFedsTimingEbpTopHist_ =
      new TH1F("timeEBPTop", "time for FEDs in EB+ Top;Relative Time (1 clock = 25ns)", 78, -7, 7);
  allFedsTimingEbmTopHist_ =
      new TH1F("timeEBMTop", "time for FEDs in EB- Top;Relative Time (1 clock = 25ns)", 78, -7, 7);
  allFedsTimingEbpBottomHist_ =
      new TH1F("timeEBPBottom", "time for FEDs in EB+ Bottom;Relative Time (1 clock = 25ns)", 78, -7, 7);
  allFedsTimingEbmBottomHist_ =
      new TH1F("timeEBMBottom", "time for FEDs in EB- Bottom;Relative Time (1 clock = 25ns)", 78, -7, 7);

  numberofCosmicsHist_ =
      new TH1F("numberofCosmicsPerEvent", "Number of cosmics per event;Number of Cosmics", 30, 0, 30);
  numberofCosmicsHistEB_ =
      new TH1F("numberofCosmicsPerEvent_EB", "Number of cosmics per event EB;Number of Cosmics", 30, 0, 30);

  numberofCosmicsWTrackHist_ =
      new TH1F("numberofCosmicsWTrackPerEvent", "Number of cosmics with track per event", 30, 0, 30);
  numberofCosmicsTopBottomHist_ = new TH1F(
      "numberofCosmicsTopBottomPerEvent", "Number of top bottom cosmics per event;Number of Cosmics", 30, 0, 30);
  numberofGoodEvtFreq_ = new TH1F("frequencyOfGoodEvents",
                                  "Number of events with cosmic vs Event;Event Number;Number of Good Events/100 Events",
                                  2000,
                                  0,
                                  200000);

  numberofCrossedEcalIdsHist_ = new TH1F("numberofCrossedEcalCosmicsPerEvent",
                                         "Number of crossed ECAL cosmics per event;Number of Crossed Cosmics",
                                         10,
                                         0,
                                         10);

  allOccupancyExclusiveECAL_ = new TH2F("OccupancyAllEvents_ExclusiveECAL",
                                        "Occupancy all events Exclusive ECAL ;i#phi;i#eta",
                                        360,
                                        1.,
                                        361.,
                                        172,
                                        -86,
                                        86);
  allOccupancyCoarseExclusiveECAL_ = new TH2F("OccupancyAllEventsCoarse_ExclusiveECAL",
                                              "Occupancy all events Coarse Exclusive ECAL;i#phi;i#eta",
                                              360 / 5,
                                              ttPhiBins,
                                              35,
                                              ttEtaBins);
  allOccupancyECAL_ =
      new TH2F("OccupancyAllEvents_ECAL", "Occupancy all events ECAL;i#phi;i#eta", 360, 1., 361., 172, -86, 86);
  allOccupancyCoarseECAL_ = new TH2F("OccupancyAllEventsCoarse_ECAL",
                                     "Occupancy all events Coarse ECAL;i#phi;i#eta",
                                     360 / 5,
                                     ttPhiBins,
                                     35,
                                     ttEtaBins);
  allFedsTimingHistECAL_ =
      new TH1F("timeForAllFeds_ECAL", "timeForAllFeds ECAL;Relative Time (1 clock = 25ns)", 78, -7, 7);
  allFedsTimingPhiEtaHistECAL_ =
      new TH3F("timePhiEtaAllFEDs_ECAL",
               "ECAL (Phi,Eta,time) for all FEDs (SM,M binning);i#phi;i#eta;Relative Time (1 clock = 25ns)",
               18,
               modPhiBins,
               9,
               modEtaBins,
               78,
               timingBins);
  allFedsTimingTTHistECAL_ =
      new TH3F("timeTTAllFEDs_ECAL",
               "ECAL (Phi,Eta,time) for all FEDs (SM,TT binning);i#phi;i#eta;Relative Time (1 clock = 25ns)",
               72,
               ttPhiBins,
               35,
               ttEtaBins,
               78,
               timingBins);
  allFedsTimingLMHistECAL_ = new TH2F("timeLMAllFEDs_ECAL",
                                      "ECAL (LM,time) for all FEDs (SM,LM binning);LM;Relative Time (1 clock = 25ns)",
                                      92,
                                      1,
                                      92,
                                      78,
                                      -7,
                                      7);

  allOccupancyExclusiveDT_ = new TH2F(
      "OccupancyAllEvents_ExclusiveDT", "Occupancy all events Exclusive DT;i#phi;i#eta", 360, 1., 361., 172, -86, 86);
  allOccupancyCoarseExclusiveDT_ = new TH2F("OccupancyAllEventsCoarse_ExclusiveDT",
                                            "Occupancy all events Coarse Exclusive DT;i#phi;i#eta",
                                            360 / 5,
                                            1,
                                            361.,
                                            35,
                                            ttEtaBins);
  allOccupancyDT_ =
      new TH2F("OccupancyAllEvents_DT", "Occupancy all events DT;i#phi;i#eta", 360, 1., 361., 172, -86, 86);
  allOccupancyCoarseDT_ = new TH2F(
      "OccupancyAllEventsCoarse_DT", "Occupancy all events Coarse DT;i#phi;i#eta", 360 / 5, 1, 361., 35, ttEtaBins);
  allFedsTimingHistDT_ = new TH1F("timeForAllFeds_DT", "timeForAllFeds DT;Relative Time (1 clock = 25ns)", 78, -7, 7);
  allFedsTimingPhiEtaHistDT_ =
      new TH3F("timePhiEtaAllFEDs_DT",
               "DT (Phi,Eta,time) for all FEDs (SM,M binning);i#phi;i#eta;Relative Time (1 clock = 25ns)",
               18,
               modPhiBins,
               9,
               modEtaBins,
               78,
               timingBins);
  allFedsTimingTTHistDT_ =
      new TH3F("timeTTAllFEDs_DT",
               "DT (Phi,Eta,time) for all FEDs (SM,TT binning);i#phi;i#eta;Relative Time (1 clock = 25ns)",
               72,
               ttPhiBins,
               35,
               ttEtaBins,
               78,
               timingBins);
  allFedsTimingLMHistDT_ = new TH2F("timeLMAllFEDs_DT",
                                    "DT (LM,time) for all FEDs (SM,LM binning);LM;Relative Time (1 clock = 25ns)",
                                    92,
                                    1,
                                    92,
                                    78,
                                    -7,
                                    7);

  allOccupancyExclusiveRPC_ = new TH2F(
      "OccupancyAllEvents_ExclusiveRPC", "Occupancy all events Exclusive RPC;i#phi;i#eta", 360, 1., 361., 172, -86, 86);
  allOccupancyCoarseExclusiveRPC_ = new TH2F("OccupancyAllEventsCoarse_ExclusiveRPC",
                                             "Occupancy all events Coarse Exclusive RPC;i#phi;i#eta",
                                             360 / 5,
                                             1,
                                             361.,
                                             35,
                                             ttEtaBins);
  allOccupancyRPC_ =
      new TH2F("OccupancyAllEvents_RPC", "Occupancy all events RPC;i#phi;i#eta", 360, 1., 361., 172, -86, 86);
  allOccupancyCoarseRPC_ = new TH2F(
      "OccupancyAllEventsCoarse_RPC", "Occupancy all events Coarse RPC;i#phi;i#eta", 360 / 5, 1, 361., 35, ttEtaBins);
  allFedsTimingHistRPC_ =
      new TH1F("timeForAllFeds_RPC", "timeForAllFeds RPC;Relative Time (1 clock = 25ns)", 78, -7, 7);
  allFedsTimingPhiEtaHistRPC_ =
      new TH3F("timePhiEtaAllFEDs_RPC",
               "RPC (Phi,Eta,time) for all FEDs (SM,M binning);i#phi;i#eta;Relative Time (1 clock = 25ns)",
               18,
               modPhiBins,
               9,
               modEtaBins,
               78,
               timingBins);
  allFedsTimingTTHistRPC_ =
      new TH3F("timeTTAllFEDs_RPC",
               "RPC (Phi,Eta,time) for all FEDs (SM,TT binning);i#phi;i#eta;Relative Time (1 clock = 25ns)",
               72,
               ttPhiBins,
               35,
               ttEtaBins,
               78,
               timingBins);
  allFedsTimingLMHistRPC_ = new TH2F("timeLMAllFEDs_RPC",
                                     "RPC (LM,time) for all FEDs (SM,LM binning);LM;Relative Time (1 clock = 25ns)",
                                     92,
                                     1,
                                     92,
                                     78,
                                     -7,
                                     7);

  allOccupancyExclusiveCSC_ = new TH2F(
      "OccupancyAllEvents_ExclusiveCSC", "Occupancy all events Exclusive CSC;i#phi;i#eta", 360, 1., 361., 172, -86, 86);
  allOccupancyCoarseExclusiveCSC_ = new TH2F("OccupancyAllEventsCoarse_ExclusiveCSC",
                                             "Occupancy all events Coarse Exclusive CSC;i#phi;i#eta",
                                             360 / 5,
                                             1,
                                             361.,
                                             35,
                                             ttEtaBins);
  allOccupancyCSC_ =
      new TH2F("OccupancyAllEvents_CSC", "Occupancy all events CSC;i#phi;i#eta", 360, 1., 361., 172, -86, 86);
  allOccupancyCoarseCSC_ = new TH2F(
      "OccupancyAllEventsCoarse_CSC", "Occupancy all events Coarse CSC;i#phi;i#eta", 360 / 5, 1, 361., 35, ttEtaBins);
  allFedsTimingHistCSC_ =
      new TH1F("timeForAllFeds_CSC", "timeForAllFeds CSC;Relative Time (1 clock = 25ns)", 78, -7, 7);
  allFedsTimingPhiEtaHistCSC_ =
      new TH3F("timePhiEtaAllFEDs_CSC",
               "CSC (Phi,Eta,time) for all FEDs (SM,M binning);i#phi;i#eta;Relative Time (1 clock = 25ns)",
               18,
               modPhiBins,
               9,
               modEtaBins,
               78,
               timingBins);
  allFedsTimingTTHistCSC_ =
      new TH3F("timeTTAllFEDs_CSC",
               "CSC (Phi,Eta,time) for all FEDs (SM,TT binning);i#phi;i#eta;Relative Time (1 clock = 25ns)",
               72,
               ttPhiBins,
               35,
               ttEtaBins,
               78,
               timingBins);
  allFedsTimingLMHistCSC_ = new TH2F("timeLMAllFEDs_CSC",
                                     "CSC (LM,time) for all FEDs (SM,LM binning);LM;Relative Time (1 clock = 25ns)",
                                     92,
                                     1,
                                     62,
                                     78,
                                     -7,
                                     7);

  allOccupancyExclusiveHCAL_ = new TH2F("OccupancyAllEvents_ExclusiveHCAL",
                                        "Occupancy all events Exclusive HCAL;i#phi;i#eta",
                                        360,
                                        1.,
                                        361.,
                                        172,
                                        -86,
                                        86);
  allOccupancyCoarseExclusiveHCAL_ = new TH2F("OccupancyAllEventsCoarse_ExclusiveHCAL",
                                              "Occupancy all events Coarse Exclusive HCAL;i#phi;i#eta",
                                              360 / 5,
                                              1,
                                              361.,
                                              35,
                                              ttEtaBins);
  allOccupancyHCAL_ =
      new TH2F("OccupancyAllEvents_HCAL", "Occupancy all events HCAL;i#phi;i#eta", 360, 1., 361., 172, -86, 86);
  allOccupancyCoarseHCAL_ = new TH2F(
      "OccupancyAllEventsCoarse_HCAL", "Occupancy all events Coarse HCAL;i#phi;i#eta", 360 / 5, 1, 361., 35, ttEtaBins);
  allFedsTimingHistHCAL_ =
      new TH1F("timeForAllFeds_HCAL", "timeForAllFeds HCAL;Relative Time (1 clock = 25ns)", 78, -7, 7);
  allFedsTimingPhiEtaHistHCAL_ =
      new TH3F("timePhiEtaAllFEDs_HCAL",
               "HCAL (Phi,Eta,time) for all FEDs (SM,M binning);i#phi;i#eta;Relative Time (1 clock = 25ns)",
               18,
               modPhiBins,
               9,
               modEtaBins,
               78,
               timingBins);
  allFedsTimingTTHistHCAL_ =
      new TH3F("timeTTAllFEDs_HCAL",
               "HCAL (Phi,Eta,time) for all FEDs (SM,TT binning);i#phi;i#eta;Relative Time (1 clock = 25ns)",
               72,
               ttPhiBins,
               35,
               ttEtaBins,
               78,
               timingBins);
  allFedsTimingLMHistHCAL_ = new TH2F("timeLMAllFEDs_HCAL",
                                      "HCAL (LM,time) for all FEDs (SM,LM binning);LM;Relative Time (1 clock = 25ns)",
                                      92,
                                      1,
                                      92,
                                      78,
                                      -7,
                                      7);

  TrueBCOccupancy_ = new TH2F(
      "BCTrueOccupancyAllEvents", "True SB Occupancy all events;#phi;#eta", 360, -3.14159, 3.14159, 172, -1.5, 1.5);
  TrueBCOccupancyCoarse_ = new TH2F("BCTrueOccupancyAllEventsCoarse",
                                    "True BC Occupancy all events Coarse;#phi;#eta",
                                    360 / 5,
                                    -3.14159,
                                    3.14159,
                                    34,
                                    -1.5,
                                    1.5);

  numberofBCinSC_ =
      new TH1F("numberofBCinSC", "Number of Basic Clusters in Super Cluster;Num Basic Clusters", 20, 0, 20);  //SC
  numberofBCinSCphi_ = new TH2F("numberofBCinSCphi",
                                "Number of Basic Clusters in Super Cluster;phi;Num Basic Clusters",
                                360 / 5,
                                -3.14159,
                                3.14159,
                                20,
                                0,
                                20);  //SC

  allFedsTimingHistEcalMuon_ =
      new TH1F("timeForAllFeds_EcalMuon", "timeForAllFeds Ecal+Muon;Relative Time (1 clock = 25ns)", 78, -7, 7);

  triggerHist_ = new TH1F("triggerHist", "Trigger Number", 5, 0, 5);
  triggerHist_->GetXaxis()->SetBinLabel(1, "ECAL");
  triggerHist_->GetXaxis()->SetBinLabel(2, "HCAL");
  triggerHist_->GetXaxis()->SetBinLabel(3, "DT");
  triggerHist_->GetXaxis()->SetBinLabel(4, "RPC");
  triggerHist_->GetXaxis()->SetBinLabel(5, "CSC");

  triggerExclusiveHist_ = new TH1F("triggerExclusiveHist", "Trigger Number (Mutually Exclusive)", 5, 0, 5);
  triggerExclusiveHist_->GetXaxis()->SetBinLabel(1, "ECAL");
  triggerExclusiveHist_->GetXaxis()->SetBinLabel(2, "HCAL");
  triggerExclusiveHist_->GetXaxis()->SetBinLabel(3, "DT");
  triggerExclusiveHist_->GetXaxis()->SetBinLabel(4, "RPC");
  triggerExclusiveHist_->GetXaxis()->SetBinLabel(5, "CSC");

  runNumberHist_ = new TH1F("runNumberHist", "Run Number", 1, 0, 1);

  deltaRHist_ = new TH1F("deltaRHist", "deltaR", 500, -0.5, 499.5);
  deltaEtaHist_ = new TH1F("deltaIEtaHist", "deltaIEta", 170, -85.5, 84.5);
  deltaPhiHist_ = new TH1F("deltaIPhiHist", "deltaIPhi", 720, -360.5, 359.5);
  ratioAssocTracksHist_ = new TH1F("ratioAssocTracks", "num assoc. tracks/tracks through Ecal", 11, 0, 1.1);
  ratioAssocClustersHist_ = new TH1F("ratioAssocClusters", "num assoc. clusters/total clusters", 11, 0, 1.1);
  trackAssoc_muonsEcal_ = new TH2F(
      "trackAssoc_muonsEcal", "Map of muon hits in Ecal", 360, 1., 361., 172, -86, 86);  //360, 0 , 360, 170,-85 ,85);
  deltaEtaDeltaPhiHist_ =
      new TH2F("deltaEtaDeltaPhi", "Delta ieta vs. delta iphi", 170, -85.5, 84.5, 720, -360.5, 359.5);
  seedTrackEtaHist_ = new TH2F("seedTrackEta", "track ieta vs. seed ieta", 170, -85.5, 84.5, 170, -85.5, 84.5);
  seedTrackPhiHist_ = new TH2F("seedTrackPhi", "track iphi vs. seed iphi", 720, -360.5, 359.5, 720, -360.5, 359.5);

  dccEventVsBxHist_ = new TH2F("dccEventVsBx", "DCC Runtype vs. bunch crossing", 3600, 0, 3600, 24, 0, 24);
  dccBXErrorByFEDHist_ = new TH1F("dccBXErrorByFED", "Incorrect BX number by FED", 54, 601, 655);
  dccOrbitErrorByFEDHist_ = new TH1F("dccOrbitErrorByFED", "Incorrect orbit number by FED", 54, 601, 655);
  dccRuntypeErrorByFEDHist_ = new TH1F("dccRuntypeErrorByFED", "Incorrect DCC Runtype by FED", 54, 601, 655);
  dccRuntypeHist_ = new TH1F("dccRuntype", "DCC Runtype frequency", 24, 0, 24);
  dccErrorVsBxHist_ = new TH2F("dccErrorVsBX", "DCC Errors vs. BX", 3600, 0, 3600, 3, 0, 3);

  hcalEnergy_HBHE_ = new TH1F("hcalEnergy_HBHE", "RecHit Energy HBHE", 440, -10, 100);
  hcalEnergy_HF_ = new TH1F("hcalEnergy_HF", "RecHit Energy HF", 440, -10, 100);
  hcalEnergy_HO_ = new TH1F("hcalEnergy_HO", "RecHit Energy HO", 440, -10, 100);
  hcalHEHBecalEB_ =
      new TH2F("hcalHEHBecalEB", "HCAL HBHE RecHit energy vs ECAL EB energy", numBins, histRangeMin_, 300.0, 40, -5, 5);

  NumXtalsInClusterHist_ = new TH1F("NumXtalsInClusterAllHist", "Number of Xtals in Cluster;NumXtals", 150, 0, 150);
  numxtalsVsEnergy_ = new TH2F("NumXtalsVsEnergy",
                               "Number of Xtals in Cluster vs Energy;Energy (GeV);Number of Xtals in Cluster",
                               numBins,
                               histRangeMin_,
                               histRangeMax_,
                               150,
                               0,
                               150);
  numxtalsVsHighEnergy_ = new TH2F("NumXtalsVsHighEnergy",
                                   "Number of Xtals in Cluster vs Energy;Energy (GeV);Number of Xtals in Cluster",
                                   numBins,
                                   histRangeMin_,
                                   200.,
                                   150,
                                   0,
                                   150);

  // high energy analysis
  allOccupancyHighEnergy_ =
      new TH2F("OccupancyHighEnergyEvents", "Occupancy high energy events;i#phi;i#eta", 360, 1., 361., 172, -86, 86);
  allOccupancyHighEnergyCoarse_ = new TH2F("OccupancyHighEnergyEventsCoarse",
                                           "Occupancy high energy events Coarse;i#phi;i#eta",
                                           72,
                                           ttPhiBins,
                                           35,
                                           ttEtaBins);
  allFedsOccupancyHighEnergyHist_ = new TH3F("OccupancyHighEnergyEvents3D",
                                             "(Phi,Eta,energy) for all high energy events;i#phi;i#eta;energy (GeV)",
                                             18,
                                             modPhiBins,
                                             9,
                                             modEtaBins,
                                             10,
                                             highEBins);
  allFedsNumXtalsInClusterHist_ =
      new TH1F("NumActiveXtalsInClusterAllHist", "Number of active Xtals in Cluster;NumXtals", 100, 0, 100);

  HighEnergy_NumXtal = new TH1F("HighEnergy_NumXtal", "Num crystals in high E clusters;num crystals", 150, 0, 150);
  HighEnergy_NumXtalFedId = new TH2F(
      "HighEnergy_NumXtalFedId", "Num crystals in cluster vs FedId;FedId;num crystals", 36, 610., 645., 150, 0, 150);
  HighEnergy_NumXtaliphi = new TH2F(
      "HighEnergy_NumXtaliphi", "Num crystals in cluster vs iphi;i#phi;num crystals", 360, 1., 361., 150, 0, 150);
  HighEnergy_energy3D = new TH3F("HighEnergy_energy3D",
                                 "(Phi,Eta,energy) for all high energy events;i#phi;i#eta;energy (GeV)",
                                 72,
                                 ttPhiBins,
                                 35,
                                 ttEtaBins,
                                 10,
                                 highEBins);

  HighEnergy_energyNumXtal = new TH2F("HighEnergy_energyNumXtal",
                                      "Energy in cluster vs Num crystals in cluster;num crystals;energy",
                                      150,
                                      0,
                                      150,
                                      200,
                                      0.,
                                      200.);

  HighEnergy_bestSeed = new TH1F("HighEnergy_bestSeedEnergy", "BestSeed Energy from TrackAss", 200, 0., 200.);
  HighEnergy_bestSeedOccupancy = new TH2F(
      "HighEnergy_bestSeedOccupancy", "Occupancy HighEn events from TrackAss;i#phi;i#eta", 360, 1., 361., 172, -86, 86);

  HighEnergy_numClusHighEn = new TH1F("HighEnergy_numClusHighEn", "Num High Energy Clusters", 7, 0, 7);
  HighEnergy_ratioClusters =
      new TH1F("HighEnergy_ratioClusters", "Num High Energy Clusters/Num tot Clusters", 100, 0., 1.1);

  HighEnergy_numRecoTrackBarrel = new TH1F("HighEnergy_numRecoTracksBarrel", "Num BarrelRecoTracks", 10, 0, 10);
  HighEnergy_TracksAngle = new TH1F("HighEnergy_TracksAngle", "Angle between tracks", 720, 0., 180.);
  HighEnergy_TracksAngleTopBottom =
      new TH1F("HighEnergy_TopBottomTracksAngle", "Angle between top-bottom tracks", 720, 0., 180.);

  HighEnergy_2GeV_occuCoarse = new TH2F("HighEnergy_occu2GeV_Coarse",
                                        "Occupancy high energy events with more than 2 GeV;i#phi;i#eta",
                                        72,
                                        ttPhiBins,
                                        35,
                                        ttEtaBins);
  HighEnergy_2GeV_occu3D = new TH3F("HighEnergy_2GeV_energy3D",
                                    "(iphi,ieta,energy) for all high energy events w > 10 GeV;i#phi;i#eta;energy (GeV)",
                                    72,
                                    ttPhiBins,
                                    35,
                                    ttEtaBins,
                                    10,
                                    highEBins);
  HighEnergy_100GeV_occuCoarse = new TH2F("HighEnergy_occu100GeV_Coarse",
                                          "Occupancy high energy events with more than 100 GeV;i#phi;i#eta",
                                          72,
                                          ttPhiBins,
                                          35,
                                          ttEtaBins);
  HighEnergy_100GeV_occu3D =
      new TH3F("HighEnergy_100GeV_energy3D",
               "(iphi,ieta,energy) for all high energy events more than 100 GeV;i#phi;i#eta;energy (GeV)",
               72,
               ttPhiBins,
               35,
               ttEtaBins,
               10,
               highEBins);
  HighEnergy_0tracks_occu3D = new TH3F("HighEnergy_0Tracks_energy3D",
                                       "(iphi,ieta,energy) for all events with 0 tracks;i#phi;i#eta;energy (GeV)",
                                       72,
                                       ttPhiBins,
                                       35,
                                       ttEtaBins,
                                       10,
                                       highEBins);
  HighEnergy_1tracks_occu3D = new TH3F("HighEnergy_1Tracks_energy3D",
                                       "(iphi,ieta,energy) for all events with 1 tracks;i#phi;i#eta;energy (GeV)",
                                       72,
                                       ttPhiBins,
                                       35,
                                       ttEtaBins,
                                       10,
                                       highEBins);
  HighEnergy_2tracks_occu3D = new TH3F("HighEnergy_2Tracks_energy3D",
                                       "(iphi,ieta,energy) for all events with 2 tracks;i#phi;i#eta;energy (GeV)",
                                       72,
                                       ttPhiBins,
                                       35,
                                       ttEtaBins,
                                       10,
                                       highEBins);
  HighEnergy_0tracks_occu3DXtal = new TH3F("HighEnergy_0Tracks_energy3DXtal",
                                           "(iphi,ieta,energy) for all events with 0 tracks;i#phi;i#eta;energy (GeV)",
                                           360,
                                           1.,
                                           361.,
                                           172,
                                           -86,
                                           86,
                                           200,
                                           0.,
                                           200.);
  HighEnergy_1tracks_occu3DXtal = new TH3F("HighEnergy_1Tracks_energy3DXtal",
                                           "(iphi,ieta,energy) for all events with 1 tracks;i#phi;i#eta;energy (GeV)",
                                           360,
                                           1.,
                                           361.,
                                           172,
                                           -86,
                                           86,
                                           200,
                                           0.,
                                           200.);
  HighEnergy_2tracks_occu3DXtal = new TH3F("HighEnergy_2Tracks_energy3DXtal",
                                           "(iphi,ieta,energy) for all events with 2 tracks;i#phi;i#eta;energy (GeV)",
                                           360,
                                           1.,
                                           361.,
                                           172,
                                           -86,
                                           86,
                                           200,
                                           0.,
                                           200.);

  //EE histograms

  // EE-
  EEM_FedsSeedEnergyHist_ =
      new TH1F("SeedEnergyAllFEDs", "Seed Energy for EEM Feds; Seed Energy (GeV)", 200, histRangeMin_, 10.0);

  EEM_AllOccupancyCoarse_ =
      new TH2F("OccupancyAllEventsCoarse", "Occupancy all events Coarse EEM;ix;iy", 20, 0, 100, 20, 0, 100);
  EEM_AllOccupancy_ = new TH2F("OccupancyAllEvents", "Occupancy all events EEM;ix;iy", 100, 0, 100, 100, 0, 100);
  EEM_FedsenergyHist_ =
      new TH1F("energy_AllClusters", "energy_AllClusters_EEM;Cluster Energy (GeV)", numBins, histRangeMin_, 10.0);
  EEM_FedsenergyHighHist_ = new TH1F(
      "energyHigh_AllClusters", "energyHigh_AllClusters in EEM;Cluster Energy (GeV)", numBins, histRangeMin_, 200.0);
  EEM_FedsenergyOnlyHighHist_ = new TH1F("energyHigh_HighEnergyClusters",
                                         "energy of High Energy Clusters in EEM;Cluster Energy (GeV)",
                                         numBins,
                                         histRangeMin_,
                                         200.0);
  EEM_FedsE2Hist_ =
      new TH1F("E2_AllClusters", "E2_AllClusters_EEM;Seed+highest neighbor energy (GeV)", numBins, histRangeMin_, 10.0);
  EEM_FedsE2vsE1Hist_ = new TH2F("E2vsE1_AllClusters",
                                 "E2vsE1_AllClusters_EEM;Seed Energy (GeV);Seed+highest neighbor energy (GeV)",
                                 numBins,
                                 histRangeMin_,
                                 10.0,
                                 numBins,
                                 histRangeMin_,
                                 10.0);
  EEM_FedsenergyvsE1Hist_ = new TH2F("energyvsE1_AllClusters",
                                     "energyvsE1_AllClusters_EEM;Seed Energy (GeV);Energy(GeV)",
                                     numBins,
                                     histRangeMin_,
                                     10.0,
                                     numBins,
                                     histRangeMin_,
                                     10.0);
  EEM_FedsTimingHist_ = new TH1F("timeForAllFeds", "timeForAllFeds_EEM;Relative Time (1 clock = 25ns)", 78, -7, 7);
  EEM_numberofCosmicsHist_ =
      new TH1F("numberofCosmicsPerEvent", "Number of cosmics per event EEM;Number of Cosmics", 30, 0, 30);

  EEM_FedsTimingVsAmpHist_ = new TH2F("timeVsAmpAllEvents",
                                      "time Vs Amp All Events EEM;Relative Time (1 clock = 25ns);Amplitude (GeV)",
                                      78,
                                      -7,
                                      7,
                                      numBins,
                                      histRangeMin_,
                                      10.0);
  EEM_FedsTimingTTHist_ = new TH3F("timeTTAllFEDs",
                                   "(ix,iy,time) for all FEDs (SM,TT binning) EEM;ix;iy;Relative Time (1 clock = 25ns)",
                                   20,
                                   0,
                                   100,
                                   20,
                                   0,
                                   100,
                                   78,
                                   -7,
                                   7);

  EEM_OccupancySingleXtal_ =
      new TH2F("OccupancySingleXtal", "Occupancy single xtal clusters EEM;ix;iy", 100, 0, 100, 100, 0, 100);
  EEM_energySingleXtalHist_ = new TH1F("energy_SingleXtalClusters",
                                       "Energy single xtal clusters EEM;Cluster Energy (GeV)",
                                       numBins,
                                       histRangeMin_,
                                       200.0);

  EEM_OccupancyExclusiveECAL_ = new TH2F(
      "OccupancyAllEvents_ExclusiveECAL", "Occupancy all events Exclusive ECAL  EEM;ix;iy", 100, 0, 100, 100, 0, 100);
  EEM_OccupancyCoarseExclusiveECAL_ = new TH2F("OccupancyAllEventsCoarse_ExclusiveECAL",
                                               "Occupancy all events Coarse Exclusive ECAL EEM;ix;iy",
                                               20,
                                               0,
                                               100,
                                               20,
                                               0,
                                               100);
  EEM_OccupancyECAL_ =
      new TH2F("OccupancyAllEvents_ECAL", "Occupancy all events ECAL EEM;ix;iy", 100, 0, 100, 100, 0, 100);
  EEM_OccupancyCoarseECAL_ =
      new TH2F("OccupancyAllEventsCoarse_ECAL", "Occupancy all events Coarse ECAL EEM;ix;iy", 20, 0, 100, 20, 0, 100);
  EEM_FedsTimingHistECAL_ =
      new TH1F("timeForAllFeds_ECAL", "timeForAllFeds ECAL EEM;Relative Time (1 clock = 25ns)", 78, -7, 7);
  EEM_FedsTimingTTHistECAL_ =
      new TH3F("timeTTAllFEDs_ECAL",
               "(ix,iy,time) for all FEDs (SM,TT binning) ECAL EEM;ix;iy;Relative Time (1 clock = 25ns)",
               20,
               0,
               100,
               20,
               0,
               100,
               78,
               -7,
               7);

  EEM_OccupancyExclusiveDT_ = new TH2F(
      "OccupancyAllEvents_ExclusiveDT", "Occupancy all events Exclusive DT EEM;ix;iy", 100, 0, 100, 100, 0, 100);
  EEM_OccupancyCoarseExclusiveDT_ = new TH2F("OccupancyAllEventsCoarse_ExclusiveDT",
                                             "Occupancy all events Coarse Exclusive DT EEM;ix;iy",
                                             20,
                                             0,
                                             100,
                                             20,
                                             0,
                                             100);
  EEM_OccupancyDT_ = new TH2F("OccupancyAllEvents_DT", "Occupancy all events DT EEM;ix;iy", 100, 0, 100, 100, 0, 100);
  EEM_OccupancyCoarseDT_ =
      new TH2F("OccupancyAllEventsCoarse_DT", "Occupancy all events Coarse DT EEM;ix;iy", 20, 0, 100, 20, 0, 100);
  EEM_FedsTimingHistDT_ =
      new TH1F("timeForAllFeds_DT", "timeForAllFeds DT EEM;Relative Time (1 clock = 25ns)", 78, -7, 7);
  EEM_FedsTimingTTHistDT_ =
      new TH3F("timeTTAllFEDs_DT",
               "(ix,iy,time) for all FEDs (SM,TT binning) DT EEM;ix;iy;Relative Time (1 clock = 25ns)",
               20,
               0,
               100,
               20,
               0,
               100,
               78,
               -7,
               7);

  EEM_OccupancyExclusiveRPC_ = new TH2F(
      "OccupancyAllEvents_ExclusiveRPC", "Occupancy all events Exclusive RPC EEM;ix;iy", 100, 0, 100, 100, 0, 100);
  EEM_OccupancyCoarseExclusiveRPC_ = new TH2F("OccupancyAllEventsCoarse_ExclusiveRPC",
                                              "Occupancy all events Coarse Exclusive RPC EEM;ix;iy",
                                              20,
                                              0,
                                              100,
                                              20,
                                              0,
                                              100);
  EEM_OccupancyRPC_ =
      new TH2F("OccupancyAllEvents_RPC", "Occupancy all events RPC EEM;ix;iy", 100, 0, 100, 100, 0, 100);
  EEM_OccupancyCoarseRPC_ =
      new TH2F("OccupancyAllEventsCoarse_RPC", "Occupancy all events Coarse RPC EEM;ix;iy", 20, 0, 100, 20, 0, 100);
  EEM_FedsTimingHistRPC_ =
      new TH1F("timeForAllFeds_RPC", "timeForAllFeds RPC EEM;Relative Time (1 clock = 25ns)", 78, -7, 7);
  EEM_FedsTimingTTHistRPC_ =
      new TH3F("timeTTAllFEDs_RPC",
               "(ix,iy,time) for all FEDs (SM,TT binning) RPC EEM;ix;iy;Relative Time (1 clock = 25ns)",
               20,
               0,
               100,
               20,
               0,
               100,
               78,
               -7,
               7);

  EEM_OccupancyExclusiveCSC_ = new TH2F(
      "OccupancyAllEvents_ExclusiveCSC", "Occupancy all events Exclusive CSC EEM;ix;iy", 100, 0, 100, 100, 0, 100);
  EEM_OccupancyCoarseExclusiveCSC_ = new TH2F("OccupancyAllEventsCoarse_ExclusiveCSC",
                                              "Occupancy all events Coarse Exclusive CSC EEM;ix;iy",
                                              20,
                                              0,
                                              100,
                                              20,
                                              0,
                                              100);
  EEM_OccupancyCSC_ =
      new TH2F("OccupancyAllEvents_CSC", "Occupancy all events CSC EEM;ix;iy", 100, 0, 100, 100, 0, 100);
  EEM_OccupancyCoarseCSC_ =
      new TH2F("OccupancyAllEventsCoarse_CSC", "Occupancy all events Coarse CSC EEM;ix;iy", 20, 0, 100, 20, 0, 100);
  EEM_FedsTimingHistCSC_ =
      new TH1F("timeForAllFeds_CSC", "timeForAllFeds CSC EEM;Relative Time (1 clock = 25ns)", 78, -7, 7);
  EEM_FedsTimingTTHistCSC_ =
      new TH3F("timeTTAllFEDs_CSC",
               "(ix,iy,time) for all FEDs (SM,TT binning) CSC EEM;ix;iy;Relative Time (1 clock = 25ns)",
               20,
               0,
               100,
               20,
               0,
               100,
               78,
               -7,
               7);

  EEM_OccupancyExclusiveHCAL_ = new TH2F(
      "OccupancyAllEvents_ExclusiveHCAL", "Occupancy all events Exclusive HCAL EEM;ix;iy", 100, 0, 100, 100, 0, 100);
  EEM_OccupancyCoarseExclusiveHCAL_ = new TH2F("OccupancyAllEventsCoarse_ExclusiveHCAL",
                                               "Occupancy all events Coarse Exclusive HCAL EEM;ix;iy",
                                               20,
                                               0,
                                               100,
                                               20,
                                               0,
                                               100);
  EEM_OccupancyHCAL_ =
      new TH2F("OccupancyAllEvents_HCAL", "Occupancy all events HCAL EEM;ix;iy", 100, 0, 100, 100, 0, 100);
  EEM_OccupancyCoarseHCAL_ =
      new TH2F("OccupancyAllEventsCoarse_HCAL", "Occupancy all events Coarse HCAL EEM;ix;iy", 20, 0, 100, 20, 0, 100);
  EEM_FedsTimingHistHCAL_ =
      new TH1F("timeForAllFeds_HCAL", "timeForAllFeds HCAL EEM;Relative Time (1 clock = 25ns)", 78, -7, 7);
  EEM_FedsTimingTTHistHCAL_ =
      new TH3F("timeTTAllFEDs_HCAL",
               "(ix,iy,time) for all FEDs (SM,TT binning) HCAL EEM;ix;iy;Relative Time (1 clock = 25ns)",
               20,
               0,
               100,
               20,
               0,
               100,
               78,
               -7,
               7);

  EEM_numberofBCinSC_ =
      new TH1F("numberofBCinSC", "Number of Basic Clusters in Super Cluster EEM;Num Basic Clusters", 20, 0, 20);

  EEM_triggerHist_ = new TH1F("triggerHist", "Trigger Number EEM", 5, 0, 5);
  EEM_triggerHist_->GetXaxis()->SetBinLabel(1, "ECAL");
  EEM_triggerHist_->GetXaxis()->SetBinLabel(2, "HCAL");
  EEM_triggerHist_->GetXaxis()->SetBinLabel(3, "DT");
  EEM_triggerHist_->GetXaxis()->SetBinLabel(4, "RPC");
  EEM_triggerHist_->GetXaxis()->SetBinLabel(5, "CSC");

  EEM_triggerExclusiveHist_ = new TH1F("triggerExclusiveHist", "Trigger Number (Mutually Exclusive) EEM", 5, 0, 5);
  triggerExclusiveHist_->GetXaxis()->SetBinLabel(1, "ECAL");
  EEM_triggerExclusiveHist_->GetXaxis()->SetBinLabel(2, "HCAL");
  EEM_triggerExclusiveHist_->GetXaxis()->SetBinLabel(3, "DT");
  EEM_triggerExclusiveHist_->GetXaxis()->SetBinLabel(4, "RPC");
  EEM_triggerExclusiveHist_->GetXaxis()->SetBinLabel(5, "CSC");

  EEM_NumXtalsInClusterHist_ =
      new TH1F("NumXtalsInClusterAllHist", "Number of Xtals in Cluster EEM;NumXtals", 150, 0, 150);
  EEM_numxtalsVsEnergy_ = new TH2F("NumXtalsVsEnergy",
                                   "Number of Xtals in Cluster vs Energy EEM;Energy (GeV);Number of Xtals in Cluster",
                                   numBins,
                                   histRangeMin_,
                                   10.0,
                                   150,
                                   0,
                                   150);
  EEM_numxtalsVsHighEnergy_ =
      new TH2F("NumXtalsVsHighEnergy",
               "Number of Xtals in Cluster vs Energy EEM;Energy (GeV);Number of Xtals in Cluster",
               numBins,
               histRangeMin_,
               200.,
               150,
               0,
               150);

  EEM_OccupancyHighEnergy_ =
      new TH2F("OccupancyHighEnergyEvents", "Occupancy high energy events EEM;ix;iy", 100, 0, 100, 100, 0, 100);
  EEM_OccupancyHighEnergyCoarse_ = new TH2F(
      "OccupancyHighEnergyEventsCoarse", "Occupancy high energy events Coarse EEM;ix;iy", 20, 0, 100, 20, 0, 100);

  EEM_FedsNumXtalsInClusterHist_ =
      new TH1F("NumActiveXtalsInClusterAllHist", "Number of active Xtals in Cluster EEM;NumXtals", 100, 0, 100);

  // EE+
  EEP_FedsSeedEnergyHist_ =
      new TH1F("SeedEnergyAllFEDs", "Seed Energy for EEP Feds; Seed Energy (GeV)", 200, histRangeMin_, 10.0);

  EEP_AllOccupancyCoarse_ =
      new TH2F("OccupancyAllEventsCoarse", "Occupancy all events Coarse EEP;ix;iy", 20, 0, 100, 20, 0, 100);
  EEP_AllOccupancy_ = new TH2F("OccupancyAllEvents", "Occupancy all events EEP;ix;iy", 100, 0, 100, 100, 0, 100);
  EEP_FedsenergyHist_ =
      new TH1F("energy_AllClusters", "energy_AllClusters_EEP;Cluster Energy (GeV)", numBins, histRangeMin_, 10.0);
  EEP_FedsenergyHighHist_ = new TH1F(
      "energyHigh_AllClusters", "energyHigh_AllClusters in EEP;Cluster Energy (GeV)", numBins, histRangeMin_, 200.0);
  EEP_FedsenergyOnlyHighHist_ = new TH1F("energyHigh_HighEnergyClusters",
                                         "energy of High Energy Clusters in EEP;Cluster Energy (GeV)",
                                         numBins,
                                         histRangeMin_,
                                         200.0);
  EEP_FedsE2Hist_ =
      new TH1F("E2_AllClusters", "E2_AllClusters_EEP;Seed+highest neighbor energy (GeV)", numBins, histRangeMin_, 10.0);
  EEP_FedsE2vsE1Hist_ = new TH2F("E2vsE1_AllClusters",
                                 "E2vsE1_AllClusters_EEP;Seed Energy (GeV);Seed+highest neighbor energy (GeV)",
                                 numBins,
                                 histRangeMin_,
                                 10.0,
                                 numBins,
                                 histRangeMin_,
                                 10.0);
  EEP_FedsenergyvsE1Hist_ = new TH2F("energyvsE1_AllClusters",
                                     "energyvsE1_AllClusters_EEP;Seed Energy (GeV);Energy(GeV)",
                                     numBins,
                                     histRangeMin_,
                                     10.0,
                                     numBins,
                                     histRangeMin_,
                                     10.0);
  EEP_FedsTimingHist_ = new TH1F("timeForAllFeds", "timeForAllFeds_EEP;Relative Time (1 clock = 25ns)", 78, -7, 7);
  EEP_numberofCosmicsHist_ =
      new TH1F("numberofCosmicsPerEvent", "Number of cosmics per event EEP;Number of Cosmics", 30, 0, 30);

  EEP_FedsTimingVsAmpHist_ = new TH2F("timeVsAmpAllEvents",
                                      "time Vs Amp All Events EEP;Relative Time (1 clock = 25ns);Amplitude (GeV)",
                                      78,
                                      -7,
                                      7,
                                      numBins,
                                      histRangeMin_,
                                      10.0);
  EEP_FedsTimingTTHist_ = new TH3F("timeTTAllFEDs",
                                   "(ix,iy,time) for all FEDs (SM,TT binning) EEP;ix;iy;Relative Time (1 clock = 25ns)",
                                   20,
                                   0,
                                   100,
                                   20,
                                   0,
                                   100,
                                   78,
                                   -7,
                                   7);

  EEP_OccupancySingleXtal_ =
      new TH2F("OccupancySingleXtal", "Occupancy single xtal clusters EEP;ix;iy", 100, 0, 100, 100, 0, 100);
  EEP_energySingleXtalHist_ = new TH1F("energy_SingleXtalClusters",
                                       "Energy single xtal clusters EEP;Cluster Energy (GeV)",
                                       numBins,
                                       histRangeMin_,
                                       200.0);

  EEP_OccupancyExclusiveECAL_ = new TH2F(
      "OccupancyAllEvents_ExclusiveECAL", "Occupancy all events Exclusive ECAL  EEP;ix;iy", 100, 0, 100, 100, 0, 100);
  EEP_OccupancyCoarseExclusiveECAL_ = new TH2F("OccupancyAllEventsCoarse_ExclusiveECAL",
                                               "Occupancy all events Coarse Exclusive ECAL EEP;ix;iy",
                                               20,
                                               0,
                                               100,
                                               20,
                                               0,
                                               100);
  EEP_OccupancyECAL_ =
      new TH2F("OccupancyAllEvents_ECAL", "Occupancy all events ECAL EEP;ix;iy", 100, 0, 100, 100, 0, 100);
  EEP_OccupancyCoarseECAL_ =
      new TH2F("OccupancyAllEventsCoarse_ECAL", "Occupancy all events Coarse ECAL EEP;ix;iy", 20, 0, 100, 20, 0, 100);
  EEP_FedsTimingHistECAL_ =
      new TH1F("timeForAllFeds_ECAL", "timeForAllFeds ECAL EEP;Relative Time (1 clock = 25ns)", 78, -7, 7);
  EEP_FedsTimingTTHistECAL_ =
      new TH3F("timeTTAllFEDs_ECAL",
               "(ix,iy,time) for all FEDs (SM,TT binning) ECAL EEP;ix;iy;Relative Time (1 clock = 25ns)",
               20,
               0,
               100,
               20,
               0,
               100,
               78,
               -7,
               7);

  EEP_OccupancyExclusiveDT_ = new TH2F(
      "OccupancyAllEvents_ExclusiveDT", "Occupancy all events Exclusive DT EEP;ix;iy", 100, 0, 100, 100, 0, 100);
  EEP_OccupancyCoarseExclusiveDT_ = new TH2F("OccupancyAllEventsCoarse_ExclusiveDT",
                                             "Occupancy all events Coarse Exclusive DT EEP;ix;iy",
                                             20,
                                             0,
                                             100,
                                             20,
                                             0,
                                             100);
  EEP_OccupancyDT_ = new TH2F("OccupancyAllEvents_DT", "Occupancy all events DT EEP;ix;iy", 100, 0, 100, 100, 0, 100);
  EEP_OccupancyCoarseDT_ =
      new TH2F("OccupancyAllEventsCoarse_DT", "Occupancy all events Coarse DT EEP;ix;iy", 20, 0, 100, 20, 0, 100);
  EEP_FedsTimingHistDT_ =
      new TH1F("timeForAllFeds_DT", "timeForAllFeds DT EEP;Relative Time (1 clock = 25ns)", 78, -7, 7);
  EEP_FedsTimingTTHistDT_ =
      new TH3F("timeTTAllFEDs_DT",
               "(ix,iy,time) for all FEDs (SM,TT binning) DT EEP;ix;iy;Relative Time (1 clock = 25ns)",
               20,
               0,
               100,
               20,
               0,
               100,
               78,
               -7,
               7);

  EEP_OccupancyExclusiveRPC_ = new TH2F(
      "OccupancyAllEvents_ExclusiveRPC", "Occupancy all events Exclusive RPC EEP;ix;iy", 100, 0, 100, 100, 0, 100);
  EEP_OccupancyCoarseExclusiveRPC_ = new TH2F("OccupancyAllEventsCoarse_ExclusiveRPC",
                                              "Occupancy all events Coarse Exclusive RPC EEP;ix;iy",
                                              20,
                                              0,
                                              100,
                                              20,
                                              0,
                                              100);
  EEP_OccupancyRPC_ =
      new TH2F("OccupancyAllEvents_RPC", "Occupancy all events RPC EEP;ix;iy", 100, 0, 100, 100, 0, 100);
  EEP_OccupancyCoarseRPC_ =
      new TH2F("OccupancyAllEventsCoarse_RPC", "Occupancy all events Coarse RPC EEP;ix;iy", 20, 0, 100, 20, 0, 100);
  EEP_FedsTimingHistRPC_ =
      new TH1F("timeForAllFeds_RPC", "timeForAllFeds RPC EEP;Relative Time (1 clock = 25ns)", 78, -7, 7);
  EEP_FedsTimingTTHistRPC_ =
      new TH3F("timeTTAllFEDs_RPC",
               "(ix,iy,time) for all FEDs (SM,TT binning) RPC EEP;ix;iy;Relative Time (1 clock = 25ns)",
               20,
               0,
               100,
               20,
               0,
               100,
               78,
               -7,
               7);

  EEP_OccupancyExclusiveCSC_ = new TH2F(
      "OccupancyAllEvents_ExclusiveCSC", "Occupancy all events Exclusive CSC EEP;ix;iy", 100, 0, 100, 100, 0, 100);
  EEP_OccupancyCoarseExclusiveCSC_ = new TH2F("OccupancyAllEventsCoarse_ExclusiveCSC",
                                              "Occupancy all events Coarse Exclusive CSC EEP;ix;iy",
                                              20,
                                              0,
                                              100,
                                              20,
                                              0,
                                              100);
  EEP_OccupancyCSC_ =
      new TH2F("OccupancyAllEvents_CSC", "Occupancy all events CSC EEP;ix;iy", 100, 0, 100, 100, 0, 100);
  EEP_OccupancyCoarseCSC_ =
      new TH2F("OccupancyAllEventsCoarse_CSC", "Occupancy all events Coarse CSC EEP;ix;iy", 20, 0, 100, 20, 0, 100);
  EEP_FedsTimingHistCSC_ =
      new TH1F("timeForAllFeds_CSC", "timeForAllFeds CSC EEP;Relative Time (1 clock = 25ns)", 78, -7, 7);
  EEP_FedsTimingTTHistCSC_ =
      new TH3F("timeTTAllFEDs_CSC",
               "(ix,iy,time) for all FEDs (SM,TT binning) CSC EEP;ix;iy;Relative Time (1 clock = 25ns)",
               20,
               0,
               100,
               20,
               0,
               100,
               78,
               -7,
               7);

  EEP_OccupancyExclusiveHCAL_ = new TH2F(
      "OccupancyAllEvents_ExclusiveHCAL", "Occupancy all events Exclusive HCAL EEP;ix;iy", 100, 0, 100, 100, 0, 100);
  EEP_OccupancyCoarseExclusiveHCAL_ = new TH2F("OccupancyAllEventsCoarse_ExclusiveHCAL",
                                               "Occupancy all events Coarse Exclusive HCAL EEP;ix;iy",
                                               20,
                                               0,
                                               100,
                                               20,
                                               0,
                                               100);
  EEP_OccupancyHCAL_ =
      new TH2F("OccupancyAllEvents_HCAL", "Occupancy all events HCAL EEP;ix;iy", 100, 0, 100, 100, 0, 100);
  EEP_OccupancyCoarseHCAL_ =
      new TH2F("OccupancyAllEventsCoarse_HCAL", "Occupancy all events Coarse HCAL EEP;ix;iy", 20, 0, 100, 20, 0, 100);
  EEP_FedsTimingHistHCAL_ =
      new TH1F("timeForAllFeds_HCAL", "timeForAllFeds HCAL EEP;Relative Time (1 clock = 25ns)", 78, -7, 7);
  EEP_FedsTimingTTHistHCAL_ =
      new TH3F("timeTTAllFEDs_HCAL",
               "(ix,iy,time) for all FEDs (SM,TT binning) HCAL EEP;ix;iy;Relative Time (1 clock = 25ns)",
               20,
               0,
               100,
               20,
               0,
               100,
               78,
               -7,
               7);

  EEP_numberofBCinSC_ =
      new TH1F("numberofBCinSC", "Number of Basic Clusters in Super Cluster EEP;Num Basic Clusters", 20, 0, 20);

  EEP_triggerHist_ = new TH1F("triggerHist", "Trigger Number EEP", 5, 0, 5);
  EEP_triggerHist_->GetXaxis()->SetBinLabel(1, "ECAL");
  EEP_triggerHist_->GetXaxis()->SetBinLabel(2, "HCAL");
  EEP_triggerHist_->GetXaxis()->SetBinLabel(3, "DT");
  EEP_triggerHist_->GetXaxis()->SetBinLabel(4, "RPC");
  EEP_triggerHist_->GetXaxis()->SetBinLabel(5, "CSC");

  EEP_triggerExclusiveHist_ = new TH1F("triggerExclusiveHist", "Trigger Number (Mutually Exclusive) EEP", 5, 0, 5);
  EEP_triggerExclusiveHist_->GetXaxis()->SetBinLabel(1, "ECAL");
  EEP_triggerExclusiveHist_->GetXaxis()->SetBinLabel(2, "HCAL");
  EEP_triggerExclusiveHist_->GetXaxis()->SetBinLabel(3, "DT");
  EEP_triggerExclusiveHist_->GetXaxis()->SetBinLabel(4, "RPC");
  EEP_triggerExclusiveHist_->GetXaxis()->SetBinLabel(5, "CSC");

  EEP_NumXtalsInClusterHist_ =
      new TH1F("NumXtalsInClusterAllHist", "Number of Xtals in Cluster EEP;NumXtals", 150, 0, 150);
  EEP_numxtalsVsEnergy_ = new TH2F("NumXtalsVsEnergy",
                                   "Number of Xtals in Cluster vs Energy EEP;Energy (GeV);Number of Xtals in Cluster",
                                   numBins,
                                   histRangeMin_,
                                   10.0,
                                   150,
                                   0,
                                   150);
  EEP_numxtalsVsHighEnergy_ =
      new TH2F("NumXtalsVsHighEnergy",
               "Number of Xtals in Cluster vs Energy EEP;Energy (GeV);Number of Xtals in Cluster",
               numBins,
               histRangeMin_,
               200.,
               150,
               0,
               150);

  EEP_OccupancyHighEnergy_ =
      new TH2F("OccupancyHighEnergyEvents", "Occupancy high energy events EEP;ix;iy", 100, 0, 100, 100, 0, 100);
  EEP_OccupancyHighEnergyCoarse_ = new TH2F(
      "OccupancyHighEnergyEventsCoarse", "Occupancy high energy events Coarse EEP;ix;iy", 20, 0, 100, 20, 0, 100);

  EEP_FedsNumXtalsInClusterHist_ =
      new TH1F("NumActiveXtalsInClusterAllHist", "Number of active Xtals in Cluster EEP;NumXtals", 100, 0, 100);
}

void EcalCosmicsHists::endRun(edm::Run const&, edm::EventSetup const& eventSetup) {}

// ------------ method called once each job just after ending the event loop  ------------
void EcalCosmicsHists::endJob() {
  using namespace std;
  if (runInFileName_) {
    fileName_ += "-" + intToString(runNum_) + ".graph.root";
  } else {
    fileName_ += ".root";
  }

  TFile root_file_(fileName_.c_str(), "RECREATE");

  for (map<int, TH1F*>::const_iterator itr = FEDsAndHists_.begin(); itr != FEDsAndHists_.end(); ++itr) {
    string dir = fedMap_->getSliceFromFed(itr->first);
    TDirectory* FEDdir = gDirectory->mkdir(dir.c_str());
    FEDdir->cd();

    TH1F* hist = itr->second;
    if (hist != nullptr)
      hist->Write();
    else {
      cerr << "EcalCosmicsHists: Error: This shouldn't happen!" << endl;
    }
    // Write out timing hist
    hist = FEDsAndTimingHists_[itr->first];
    if (hist != nullptr)
      hist->Write();
    else {
      cerr << "EcalCosmicsHists: Error: This shouldn't happen!" << endl;
    }

    hist = FEDsAndFrequencyHists_[itr->first];
    hist->Write();

    hist = FEDsAndiPhiProfileHists_[itr->first];
    hist->Write();

    hist = FEDsAndiEtaProfileHists_[itr->first];
    hist->Write();

    hist = FEDsAndE2Hists_[itr->first];
    hist->Write();

    hist = FEDsAndenergyHists_[itr->first];
    hist->Write();

    hist = FEDsAndNumXtalsInClusterHists_[itr->first];
    hist->Write();

    TH2F* hist2 = FEDsAndTimingVsAmpHists_[itr->first];
    hist2->Write();

    hist2 = FEDsAndTimingVsFreqHists_[itr->first];
    hist2->Write();

    hist2 = FEDsAndE2vsE1Hists_[itr->first];
    hist2->Write();

    hist2 = FEDsAndenergyvsE1Hists_[itr->first];
    hist2->Write();

    hist2 = FEDsAndOccupancyHists_[itr->first];
    hist2->Write();

    hist2 = FEDsAndTimingVsPhiHists_[itr->first];
    hist2->Write();

    hist2 = FEDsAndTimingVsModuleHists_[itr->first];
    hist2->Write();

    //2d hist
    map<int, TH2F*>::const_iterator itr2d;
    itr2d = FEDsAndDCCRuntypeVsBxHists_.find(itr->first);
    if (itr2d != FEDsAndDCCRuntypeVsBxHists_.end()) {
      TH2F* hist2 = itr2d->second;
      hist2->GetYaxis()->SetBinLabel(1, "COSMIC");
      hist2->GetYaxis()->SetBinLabel(2, "BEAMH4");
      hist2->GetYaxis()->SetBinLabel(3, "BEAMH2");
      hist2->GetYaxis()->SetBinLabel(4, "MTCC");
      hist2->GetYaxis()->SetBinLabel(5, "LASER_STD");
      hist2->GetYaxis()->SetBinLabel(6, "LASER_POWER_SCAN");
      hist2->GetYaxis()->SetBinLabel(7, "LASER_DELAY_SCAN");
      hist2->GetYaxis()->SetBinLabel(8, "TESTPULSE_SCAN_MEM");
      hist2->GetYaxis()->SetBinLabel(9, "TESTPULSE_MGPA");
      hist2->GetYaxis()->SetBinLabel(10, "PEDESTAL_STD");
      hist2->GetYaxis()->SetBinLabel(11, "PEDESTAL_OFFSET_SCAN");
      hist2->GetYaxis()->SetBinLabel(12, "PEDESTAL_25NS_SCAN");
      hist2->GetYaxis()->SetBinLabel(13, "LED_STD");
      hist2->GetYaxis()->SetBinLabel(14, "PHYSICS_GLOBAL");
      hist2->GetYaxis()->SetBinLabel(15, "COSMICS_GLOBAL");
      hist2->GetYaxis()->SetBinLabel(16, "HALO_GLOBAL");
      hist2->GetYaxis()->SetBinLabel(17, "LASER_GAP");
      hist2->GetYaxis()->SetBinLabel(18, "TESTPULSE_GAP");
      hist2->GetYaxis()->SetBinLabel(19, "PEDESTAL_GAP");
      hist2->GetYaxis()->SetBinLabel(20, "LED_GAP");
      hist2->GetYaxis()->SetBinLabel(21, "PHYSICS_LOCAL");
      hist2->GetYaxis()->SetBinLabel(22, "COSMICS_LOCAL");
      hist2->GetYaxis()->SetBinLabel(23, "HALO_LOCAL");
      hist2->GetYaxis()->SetBinLabel(24, "CALIB_LOCAL");
      hist2->Write();
    }

    root_file_.cd();
  }
  allFedsHist_->Write();
  allFedsE2Hist_->Write();
  allFedsenergyHist_->Write();
  allFedsenergyHighHist_->Write();
  allFedsenergyOnlyHighHist_->Write();
  allFedsE2vsE1Hist_->Write();
  allFedsenergyvsE1Hist_->Write();
  allFedsTimingHist_->Write();
  allFedsTimingVsAmpHist_->Write();
  allFedsFrequencyHist_->Write();
  allFedsTimingVsFreqHist_->Write();
  allFedsiEtaProfileHist_->Write();
  allFedsiPhiProfileHist_->Write();
  allOccupancy_->Write();
  TrueOccupancy_->Write();
  allOccupancyCoarse_->Write();
  TrueOccupancyCoarse_->Write();
  allOccupancyHighEnergy_->Write();
  allOccupancyHighEnergyCoarse_->Write();
  allOccupancySingleXtal_->Write();
  energySingleXtalHist_->Write();
  allFedsNumXtalsInClusterHist_->Write();
  allFedsTimingPhiHist_->Write();
  allFedsTimingPhiEbpHist_->Write();
  allFedsTimingPhiEbmHist_->Write();
  allFedsTimingEbpHist_->Write();
  allFedsTimingEbmHist_->Write();
  allFedsTimingEbpTopHist_->Write();
  allFedsTimingEbmTopHist_->Write();
  allFedsTimingEbpBottomHist_->Write();
  allFedsTimingEbmBottomHist_->Write();
  allFedsTimingPhiEtaHist_->Write();
  allFedsTimingTTHist_->Write();
  allFedsTimingLMHist_->Write();
  allFedsOccupancyHighEnergyHist_->Write();

  numberofBCinSC_->Write();         //SC
  numberofBCinSCphi_->Write();      //SC
  TrueBCOccupancyCoarse_->Write();  //BC
  TrueBCOccupancy_->Write();        //BC

  numxtalsVsEnergy_->Write();
  numxtalsVsHighEnergy_->Write();

  allOccupancyExclusiveECAL_->Write();
  allOccupancyCoarseExclusiveECAL_->Write();
  allOccupancyECAL_->Write();
  allOccupancyCoarseECAL_->Write();
  allFedsTimingPhiEtaHistECAL_->Write();
  allFedsTimingHistECAL_->Write();
  allFedsTimingTTHistECAL_->Write();
  allFedsTimingLMHistECAL_->Write();

  allOccupancyExclusiveHCAL_->Write();
  allOccupancyCoarseExclusiveHCAL_->Write();
  allOccupancyHCAL_->Write();
  allOccupancyCoarseHCAL_->Write();
  allFedsTimingPhiEtaHistHCAL_->Write();
  allFedsTimingHistHCAL_->Write();
  allFedsTimingTTHistHCAL_->Write();
  allFedsTimingLMHistHCAL_->Write();

  allOccupancyExclusiveDT_->Write();
  allOccupancyCoarseExclusiveDT_->Write();
  allOccupancyDT_->Write();
  allOccupancyCoarseDT_->Write();
  allFedsTimingPhiEtaHistDT_->Write();
  allFedsTimingHistDT_->Write();
  allFedsTimingTTHistDT_->Write();
  allFedsTimingLMHistDT_->Write();

  allOccupancyExclusiveRPC_->Write();
  allOccupancyCoarseExclusiveRPC_->Write();
  allOccupancyRPC_->Write();
  allOccupancyCoarseRPC_->Write();
  allFedsTimingPhiEtaHistRPC_->Write();
  allFedsTimingHistRPC_->Write();
  allFedsTimingTTHistRPC_->Write();
  allFedsTimingLMHistRPC_->Write();

  allOccupancyExclusiveCSC_->Write();
  allOccupancyCoarseExclusiveCSC_->Write();
  allOccupancyCSC_->Write();
  allOccupancyCoarseCSC_->Write();
  allFedsTimingPhiEtaHistCSC_->Write();
  allFedsTimingHistCSC_->Write();
  allFedsTimingTTHistCSC_->Write();
  allFedsTimingLMHistCSC_->Write();

  allFedsTimingHistEcalMuon_->Write();

  //EE
  TDirectory* EEMinusDir = gDirectory->mkdir("EEMinus");
  EEMinusDir->cd();
  EEM_FedsSeedEnergyHist_->Write();
  EEM_AllOccupancyCoarse_->Write();
  EEM_AllOccupancy_->Write();
  EEM_FedsenergyHist_->Write();
  EEM_FedsenergyHighHist_->Write();
  EEM_FedsenergyOnlyHighHist_->Write();
  EEM_FedsE2Hist_->Write();
  EEM_FedsE2vsE1Hist_->Write();
  EEM_FedsenergyvsE1Hist_->Write();
  EEM_FedsTimingHist_->Write();
  EEM_numberofCosmicsHist_->Write();
  EEM_FedsTimingVsAmpHist_->Write();
  EEM_FedsTimingTTHist_->Write();
  EEM_OccupancySingleXtal_->Write();
  EEM_energySingleXtalHist_->Write();
  EEM_OccupancyExclusiveECAL_->Write();
  EEM_OccupancyCoarseExclusiveECAL_->Write();
  EEM_OccupancyECAL_->Write();
  EEM_OccupancyCoarseECAL_->Write();
  EEM_FedsTimingHistECAL_->Write();
  EEM_FedsTimingTTHistECAL_->Write();
  EEM_OccupancyExclusiveDT_->Write();
  EEM_OccupancyCoarseExclusiveDT_->Write();
  EEM_OccupancyDT_->Write();
  EEM_OccupancyCoarseDT_->Write();
  EEM_FedsTimingHistDT_->Write();
  EEM_FedsTimingTTHistDT_->Write();
  EEM_OccupancyExclusiveRPC_->Write();
  EEM_OccupancyCoarseExclusiveRPC_->Write();
  EEM_OccupancyRPC_->Write();
  EEM_OccupancyCoarseRPC_->Write();
  EEM_FedsTimingHistRPC_->Write();
  EEM_FedsTimingTTHistRPC_->Write();
  EEM_OccupancyExclusiveCSC_->Write();
  EEM_OccupancyCoarseExclusiveCSC_->Write();
  EEM_OccupancyCSC_->Write();
  EEM_OccupancyCoarseCSC_->Write();
  EEM_FedsTimingHistCSC_->Write();
  EEM_FedsTimingTTHistCSC_->Write();
  EEM_OccupancyExclusiveHCAL_->Write();
  EEM_OccupancyCoarseExclusiveHCAL_->Write();
  EEM_OccupancyHCAL_->Write();
  EEM_OccupancyCoarseHCAL_->Write();
  EEM_FedsTimingHistHCAL_->Write();
  EEM_FedsTimingTTHistHCAL_->Write();
  EEM_numberofBCinSC_->Write();
  EEM_triggerHist_->Write();
  EEM_triggerExclusiveHist_->Write();
  EEM_NumXtalsInClusterHist_->Write();
  EEM_numxtalsVsEnergy_->Write();
  EEM_numxtalsVsHighEnergy_->Write();
  EEM_OccupancyHighEnergy_->Write();
  EEM_OccupancyHighEnergyCoarse_->Write();
  EEM_FedsNumXtalsInClusterHist_->Write();
  root_file_.cd();

  TDirectory* EEPlusDir = gDirectory->mkdir("EEPlus");
  EEPlusDir->cd();
  EEP_FedsSeedEnergyHist_->Write();
  EEP_AllOccupancyCoarse_->Write();
  EEP_AllOccupancy_->Write();
  EEP_FedsenergyHist_->Write();
  EEP_FedsenergyHighHist_->Write();
  EEP_FedsenergyOnlyHighHist_->Write();
  EEP_FedsE2Hist_->Write();
  EEP_FedsE2vsE1Hist_->Write();
  EEP_FedsenergyvsE1Hist_->Write();
  EEP_FedsTimingHist_->Write();
  EEP_numberofCosmicsHist_->Write();
  EEP_FedsTimingVsAmpHist_->Write();
  EEP_FedsTimingTTHist_->Write();
  EEP_OccupancySingleXtal_->Write();
  EEP_energySingleXtalHist_->Write();
  EEP_OccupancyExclusiveECAL_->Write();
  EEP_OccupancyCoarseExclusiveECAL_->Write();
  EEP_OccupancyECAL_->Write();
  EEP_OccupancyCoarseECAL_->Write();
  EEP_FedsTimingHistECAL_->Write();
  EEP_FedsTimingTTHistECAL_->Write();
  EEP_OccupancyExclusiveDT_->Write();
  EEP_OccupancyCoarseExclusiveDT_->Write();
  EEP_OccupancyDT_->Write();
  EEP_OccupancyCoarseDT_->Write();
  EEP_FedsTimingHistDT_->Write();
  EEP_FedsTimingTTHistDT_->Write();
  EEP_OccupancyExclusiveRPC_->Write();
  EEP_OccupancyCoarseExclusiveRPC_->Write();
  EEP_OccupancyRPC_->Write();
  EEP_OccupancyCoarseRPC_->Write();
  EEP_FedsTimingHistRPC_->Write();
  EEP_FedsTimingTTHistRPC_->Write();
  EEP_OccupancyExclusiveCSC_->Write();
  EEP_OccupancyCoarseExclusiveCSC_->Write();
  EEP_OccupancyCSC_->Write();
  EEP_OccupancyCoarseCSC_->Write();
  EEP_FedsTimingHistCSC_->Write();
  EEP_FedsTimingTTHistCSC_->Write();
  EEP_OccupancyExclusiveHCAL_->Write();
  EEP_OccupancyCoarseExclusiveHCAL_->Write();
  EEP_OccupancyHCAL_->Write();
  EEP_OccupancyCoarseHCAL_->Write();
  EEP_FedsTimingHistHCAL_->Write();
  EEP_FedsTimingTTHistHCAL_->Write();
  EEP_numberofBCinSC_->Write();
  EEP_triggerHist_->Write();
  EEP_triggerExclusiveHist_->Write();
  EEP_NumXtalsInClusterHist_->Write();
  EEP_numxtalsVsEnergy_->Write();
  EEP_numxtalsVsHighEnergy_->Write();
  EEP_OccupancyHighEnergy_->Write();
  EEP_OccupancyHighEnergyCoarse_->Write();
  EEP_FedsNumXtalsInClusterHist_->Write();
  root_file_.cd();

  triggerHist_->Write();
  triggerExclusiveHist_->Write();

  NumXtalsInClusterHist_->Write();

  numberofCosmicsHist_->Write();
  numberofCosmicsHistEB_->Write();

  numberofCosmicsWTrackHist_->Write();
  numberofCosmicsTopBottomHist_->Write();
  numberofGoodEvtFreq_->Write();
  numberofCrossedEcalIdsHist_->Write();

  runNumberHist_->SetBinContent(1, runNum_);
  runNumberHist_->Write();

  deltaRHist_->Write();
  deltaEtaHist_->Write();
  deltaPhiHist_->Write();
  ratioAssocClustersHist_->Write();
  ratioAssocTracksHist_->Write();
  deltaEtaDeltaPhiHist_->Write();
  seedTrackPhiHist_->Write();
  seedTrackEtaHist_->Write();
  dccEventVsBxHist_->Write();
  dccOrbitErrorByFEDHist_->Write();
  dccBXErrorByFEDHist_->Write();
  dccRuntypeErrorByFEDHist_->Write();
  dccErrorVsBxHist_->Write();
  dccRuntypeHist_->Write();

  trackAssoc_muonsEcal_->Write();

  hcalEnergy_HBHE_->Write();
  hcalEnergy_HF_->Write();
  hcalEnergy_HO_->Write();
  hcalHEHBecalEB_->Write();

  TDirectory* highEnergyDir = gDirectory->mkdir("HighEnergy");
  highEnergyDir->cd();
  HighEnergy_NumXtal->Write();
  HighEnergy_NumXtalFedId->Write();
  HighEnergy_NumXtaliphi->Write();
  HighEnergy_energy3D->Write();
  HighEnergy_energyNumXtal->Write();
  HighEnergy_bestSeed->Write();
  HighEnergy_bestSeedOccupancy->Write();
  HighEnergy_numClusHighEn->Write();
  HighEnergy_ratioClusters->Write();
  HighEnergy_numRecoTrackBarrel->Write();
  HighEnergy_TracksAngle->Write();
  HighEnergy_TracksAngleTopBottom->Write();
  HighEnergy_2GeV_occuCoarse->Write();
  HighEnergy_2GeV_occu3D->Write();
  HighEnergy_100GeV_occuCoarse->Write();
  HighEnergy_100GeV_occu3D->Write();
  HighEnergy_0tracks_occu3D->Write();
  HighEnergy_1tracks_occu3D->Write();
  HighEnergy_2tracks_occu3D->Write();
  HighEnergy_0tracks_occu3DXtal->Write();
  HighEnergy_1tracks_occu3DXtal->Write();
  HighEnergy_2tracks_occu3DXtal->Write();

  root_file_.cd();

  TDirectory* TimeStampdir = gDirectory->mkdir("EventTiming");
  TimeStampdir->cd();
  allFedsFreqTimeHist_->Write();
  allFedsFreqTimeVsPhiHist_->Write();
  allFedsFreqTimeVsPhiTTHist_->Write();
  allFedsFreqTimeVsEtaHist_->Write();
  allFedsFreqTimeVsEtaTTHist_->Write();

  root_file_.cd();

  root_file_.Close();

  LogWarning("EcalCosmicsHists") << "---> Number of cosmic events: " << cosmicCounter_ << " in " << naiveEvtNum_
                                 << " events.";
  LogWarning("EcalCosmicsHists") << "---> Number of EB cosmic events: " << cosmicCounterEB_ << " in " << naiveEvtNum_
                                 << " events.";
  LogWarning("EcalCosmicsHists") << "---> Number of EE- cosmic events: " << cosmicCounterEEM_ << " in " << naiveEvtNum_
                                 << " events.";
  LogWarning("EcalCosmicsHists") << "---> Number of EE+ cosmic events: " << cosmicCounterEEP_ << " in " << naiveEvtNum_
                                 << " events.";

  //  LogWarning("EcalCosmicsHists") << "---> Number of top+bottom cosmic events: " << cosmicCounterTopBottom_ << " in " << cosmicCounter_ << " cosmics in " << naiveEvtNum_ << " events.";
}

std::string EcalCosmicsHists::intToString(int num) {
  using namespace std;
  ostringstream myStream;
  myStream << num << flush;
  return (myStream.str());  //returns the string form of the stringstream object
}