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
//////////////////////////////////////////////////////////
// L3 iterative procedure 
// for IsoTrack calibration
//
// CalibTree class contains ROOT-tree
// generated with IsoTrackCalibration plugin
//
// version 5.0   September 2016
// modified detID for CMSSW_8_X !!!!!!!!!!!!!!!!!!!!!
// M. Chadeeva
//////////////////////////////////////////////////////////

#include <TSystem.h>
#include <TStyle.h>
#include <TCanvas.h>
#include <TROOT.h>
#include <TChain.h>
#include <TFile.h>
#include <TTree.h>
#include <TH1.h>
#include <TGraph.h>
#include <TGraphErrors.h>
//#include <TProfile.h>
#include <TLegend.h>
#include <TString.h>
#include <TF1.h>

#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <map>
#include <utility>

//**********************************************************
// Constants
//**********************************************************
const unsigned int MAXNUM_SUBDET = 100;
const int FIRST_IETA_TR = 15;
const int FIRST_IETA_HE = 18;
const int FIRST_IETA_FWD_1 = 25;
const int FIRST_IETA_FWD_2 = 26;
const unsigned int N_DEPTHS = 3;
/*
// old detID format for CMSSW_7_X 
const unsigned int PHI_MASK       = 0x7F;
const unsigned int ETA_OFFSET     = 7;
const unsigned int ETA_MASK       = 0x3F;
const unsigned int ZSIDE_MASK     = 0x2000;
const unsigned int DEPTH_OFFSET   = 14;
const unsigned int DEPTH_MASK     = 0x1F;
const unsigned int DEPTH_SET      = 0x1C000;
unsigned int MASK(0xFF80); //merge phi 
*/
// new detID format for CMSSW_8_X
const unsigned int PHI_MASK       = 0x3FF;
const unsigned int ETA_OFFSET     = 10;
const unsigned int ETA_MASK       = 0x1FF;
const unsigned int ZSIDE_MASK     = 0x80000;
const unsigned int DEPTH_OFFSET   = 20;
const unsigned int DEPTH_MASK     = 0xF;
const unsigned int DEPTH_SET      = 0xF00000;

//--------------------------------------------
// merging depths

const unsigned int MERGE_PHI_AND_DEPTHS = 1;
const unsigned int MASK(0xFFC00); //merge phi and depth
/*
const unsigned int MERGE_PHI_AND_DEPTHS = 0;
const unsigned int MASK(0xFFFC00); //merge phi
*/
//-------------------------------------------

// individual ieta rings
const unsigned int MASK2(0); // no second mask
const int N_ETA_RINGS_PER_BIN = 1;
/*
// twin (even+odd) ieta rings
const unsigned int MASK2(0x80);
const int N_ETA_RINGS_PER_BIN = 2;

// 4-fold ieta rings
const unsigned int MASK2(0x180);
const int N_ETA_RINGS_PER_BIN = 4;
*/
const int MAX_ONESIDE_ETA_RINGS = 30;
const int HALF_NUM_ETA_BINS =
  (MAX_ONESIDE_ETA_RINGS + 1*(N_ETA_RINGS_PER_BIN>1))/N_ETA_RINGS_PER_BIN;
const int NUM_ETA_BINS = 2*HALF_NUM_ETA_BINS + 1;

const int MIN_N_TRACKS_PER_CELL = 50;
const int MIN_N_ENTRIES_FOR_FIT = 150;
const double MAX_REL_UNC_FACTOR = 0.2;

const bool APPLY_CORRECTION_FOR_PU = true;
const bool SINGLE_REFERENCE_RESPONSE = false;
/*
//--- base correction for PU as of 2015 MC studies
const char *l3prefix1 = "base";
const double DELTA_CUT = 0.0; 
const double LINEAR_COR_COEF[5] = { -0.375, -0.375, -0.375, -0.375, -0.375 };
const double SQUARE_COR_COEF[5] = { -0.450, -0.450, -0.450, -0.450, -0.450 };
//const double UPPER_LIMIT_DELTA_PU_COR = 2.0;
*/
//--- optimized correction for PU as of 2016 MC studies
// tuned to get MPV after correction
// at the level of that of single pion response w/o PU and w/o correction
const char *l3prefix1 = "opt";
const double DELTA_CUT = 0.02; 
const double LINEAR_COR_COEF[5] = { -0.35, -0.35, -0.35, -0.35, -0.45 };
const double SQUARE_COR_COEF[5] = { -0.65, -0.65, -0.65, -0.30, -0.10 };
//const double UPPER_LIMIT_DELTA_PU_COR = 1.5;

const double UPPER_LIMIT_RESPONSE_BEFORE_COR = 3.0;
const double FLEX_SEL_FIRST_CONST = 20.0;  // 20.(for exp); or 16*2
const double FLEX_SEL_SECOND_CONST = 18.0; // 18.(for exp);

const double MIN_RESPONSE_HIST = 0.0;
const double MAX_RESPONSE_HIST = UPPER_LIMIT_RESPONSE_BEFORE_COR;
const int NBIN_RESPONSE_HIST = 480;
const int NBIN_RESPONSE_HIST_IND = 120;
const double FIT_RMS_INTERVAL = 1.5;
const double RESOLUTION_HCAL = 0.3;
const double LOW_RESPONSE = 0.5;
const double HIGH_RESPONSE = 1.5;

//std::cout.precision(3);

//**********************************************************
// Header with CalibTree class definition
//**********************************************************

//////////////////////////////////////////////////////////
// Header with CalibTree class
// for L3 iterative procedure
// implemented in L3_IsoTrackCalibration.C
// 
// version 2.0 January 2016
// M. Chadeeva
//////////////////////////////////////////////////////////

#include <TSystem.h>
#include <TStyle.h>
#include <TCanvas.h>
#include <TROOT.h>
#include <TChain.h>
#include <TFile.h>
#include <TTree.h>
#include <TH1.h>
#include <TGraph.h>
#include <TGraphErrors.h>
#include <TProfile.h>
#include <TLegend.h>
#include <TString.h>
#include <TF1.h>

#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <map>
#include <utility>


//**********************************************************
// Class with TTree containing parameters of selected events
//**********************************************************
class CalibTree {
public :
  TChain          *fChain;   //!pointer to the analyzed TTree
  //TChain          *inChain;   //!pointer to the analyzed TChain
  Int_t           fCurrent; //!current Tree number in a TChain

  // Declaration of leaf types
  Int_t           t_Run;
  Int_t           t_Event;
  Int_t           t_nVtx;
  Int_t           t_nTrk;
  Double_t        t_EventWeight;
  Double_t        t_p;
  Double_t        t_pt;
  Int_t           t_ieta;
  Double_t        t_phi;
  Double_t        t_eMipDR;
  Double_t        t_eHcal;
  Double_t        t_eHcal10;
  Double_t        t_eHcal30;
  Double_t        t_hmaxNearP;
  Bool_t          t_selectTk;
  Bool_t          t_qltyMissFlag;
  Bool_t          t_qltyPVFlag;
/*
  Double_t        t_l1pt;
  Double_t        t_l1eta;
  Double_t        t_l1phi;
  Double_t        t_l3pt;
  Double_t        t_l3eta;
  Double_t        t_l3phi;
*/
  Double_t        t_mindR1;
  Double_t        t_mindR2;
  std::vector<unsigned int> *t_DetIds;
  //std::vector<unsigned int> *t_DetIds1;
  //std::vector<unsigned int> *t_DetIds3;
  std::vector<double>  *t_HitEnergies;
  //std::vector<double>  *t_HitEnergies1;
  //std::vector<double>  *t_HitEnergies3;
  
  // List of branches
  TBranch        *b_t_Run;   //!
  TBranch        *b_t_Event;   //!
  TBranch        *b_t_nVtx;
  TBranch        *b_t_nTrk;
  TBranch        *b_t_EventWeight;   //!
  TBranch        *b_t_p;   //!
  TBranch        *b_t_pt;   //!
  TBranch        *b_t_ieta;   //!
  TBranch        *b_t_phi;   //!
  TBranch        *b_t_eMipDR;   //!
  TBranch        *b_t_eHcal;   //!
  TBranch        *b_t_eHcal10;   //!
  TBranch        *b_t_eHcal30;   //!
  TBranch        *b_t_hmaxNearP;   //!
  TBranch        *b_t_selectTk;   //!
  TBranch        *b_t_qltyMissFlag;   //!
  TBranch        *b_t_qltyPVFlag;   //!
/*
  TBranch        *b_t_l1pt;   //!
  TBranch        *b_t_l1eta;   //!
  TBranch        *b_t_l1phi;   //!
  TBranch        *b_t_l3pt;   //!
  TBranch        *b_t_l3eta;   //!
  TBranch        *b_t_l3phi;   //!
*/
  TBranch        *b_t_mindR1;   //!
  TBranch        *b_t_mindR2;   //!
  TBranch        *b_t_DetIds;   //!
  //TBranch        *b_t_DetIds1;   //!
  //TBranch        *b_t_DetIds3;   //!
  TBranch        *b_t_HitEnergies;   //!
  //TBranch        *b_t_HitEnergies1;   //!
  //TBranch        *b_t_HitEnergies3;   //!

  //--- constructor & destructor
  //CalibTree(TTree *tree=0);
  CalibTree(TChain *tree,
	    double min_enrHcal, double min_pt,
	    double lim_mipEcal, double lim_charIso,
	    double min_trackMom, double max_trackMom);
  virtual ~CalibTree();
  
  //--- functions
  virtual Int_t      GetEntry(Long64_t entry);
  virtual Long64_t   LoadTree(Long64_t entry);
  //virtual void     Init(TTree *tree);
  virtual void       Init(TChain *tree);
  virtual Bool_t     Notify();
  virtual Int_t      firstLoop(unsigned, bool, unsigned);
  virtual Double_t   loopForIteration(unsigned, unsigned, unsigned);
  virtual Double_t   lastLoop(unsigned, unsigned, bool, unsigned);
  Bool_t             goodTrack(int);
  Bool_t             getFactorsFromFile(std::string, unsigned);
  unsigned int       saveFactorsInFile(std::string);
  Bool_t             openOutputRootFile(std::string);

  //--- variables for iterations
  Double_t referenceResponse;
  Double_t referenceResponseHB;
  Double_t referenceResponseTR;
  Double_t referenceResponseHE;
  Double_t maxZtestFromWeights;
  Double_t maxSys2StatRatio;
  int maxNumOfTracksForIeta;
  std::map<unsigned int, double> factors;
  std::map<unsigned int, double> uncFromWeights;
  std::map<unsigned int, double> uncFromDeviation;
  std::map<unsigned int, int> subDetector_trk;
  std::map<unsigned int, int> subDetector_final;
  std::map<unsigned int, int> nTrks;
  std::map<unsigned int, int> nSubdetInEvent;
  std::map<unsigned int, int> nPhiMergedInEvent;
  std::map<unsigned int, double> sumOfResponse;
  std::map<unsigned int, double> sumOfResponseSquared;

  //--- variables for selection
  double minEnrHcal;
  double minTrackPt;
  double minTrackMom;
  double maxTrackMom;
  double limMipEcal;
  double limCharIso;
  double constForFlexSel;
  
  //--- variables for plotting
  TFile *foutRootFile;
  TH1F* e2p_init;
  TH1F* e2pHB_init;
  TH1F* e2pTR_init;
  TH1F* e2pHE_init;
  TH1F* e2p_last;
  TH1F* e2pHB_last;
  TH1F* e2pTR_last;
  TH1F* e2pHE_last;
  
  TH1F* ieta_lefttail;
  TH1F* ieta_righttail;
  /*
  TProfile* deltaVSieta;
  TProfile* deltaNorm;
  TProfile* eHcalDeltaHB;
  TProfile* eHcalDeltaTR;
  TProfile* eHcalDeltaHE;
  TProfile* eHcalDeltaHEfwd;
  TProfile* frespDeltaHB;
  TProfile* frespDeltaTR;
  TProfile* frespDeltaHE;
  TProfile* frespDeltaHEfwd;
  */
};


//**********************************************************
// Description of function to run iteration
//**********************************************************
unsigned int runIterations(const char *inFileDir = ".", 
			   const char *inFileNamePrefix = "outputFromAnalyzer",
			   const int firstInputFileEnum = 0,
			   const int lastInputFileEnum = 1,
			   const unsigned maxNumberOfIterations = 10,
			   //const char *l3prefix0 = "cf",
			   const double minHcalEnergy = 10.0,
			   const double minPt = 7.0,
			   const double limitForChargeIsolation = 2.0,
			   const double minTrackMomentum = 40.0,
			   const double maxTrackMomentum = 60.0,
			   const double limitForMipInEcal = 1.0,
			   const bool shiftResponse = 1,
			   const unsigned int subSample = 2,
			   const bool isCrosscheck = false,
			   const char *inTxtFilePrefix = "test",
			   const char *treeDirName = "IsoTrackCalibration", 
			   const char *treeName = "CalibTree",
			   unsigned int Debug = 0)
{
  // Debug:  0-no debugging; 1-short debug; >1 - number of events to be shown in detail
  // subSample: extract factors from odd (0), even(1) or all(2) events
  // limitForChargeIsolation:  <0 - flex. sel.
  // and corr. for PU
  
  if ( isCrosscheck )
    std::cout << "Test with previously extracted factors..." << std::endl;
  else
    std::cout << "Extracting factors using L3 algorithm and isolated tracks..." << std::endl;

  char l3prefix0[10] = "ref1";
  if ( !SINGLE_REFERENCE_RESPONSE ) sprintf(l3prefix0,"ref3");
  char l3prefix2[20] = "_noCor";
  if ( APPLY_CORRECTION_FOR_PU ) sprintf(l3prefix2,"_%s%02d",
					 l3prefix1, int(100*DELTA_CUT)
					 );				
  char l3prefix3[10] = "_mean";
  if ( shiftResponse ) sprintf(l3prefix3,"_mpv");
  char l3prefix4[8] = "_3dep";
  if ( MERGE_PHI_AND_DEPTHS ) sprintf(l3prefix4,"_merged");
  char l3prefix[40];
  sprintf(l3prefix,"%s%s%s%s", l3prefix0, l3prefix2, l3prefix3, l3prefix4);

  char isoPrefix[10] = "hard";
  if ( limitForChargeIsolation < 0 ) sprintf(isoPrefix, "flex");
    
  char fnameInput[120];
  char fnameOutRoot[120];
  char fnameOutTxt[120] = "dummy";
  char fnameInTxt[120]  = "dummy";
  char tname[100];
  
  TGraph *g_converge1 = new TGraph(maxNumberOfIterations);
  TGraph *g_converge2 = new TGraph(maxNumberOfIterations);
  TGraph *g_converge3 = new TGraph(maxNumberOfIterations);

  sprintf(tname, "%s/%s", treeDirName, treeName );
  TChain tree(tname);

  //--- combine tree from several enumerated files with the same prefix
  //    or one file w/o number (firstInputFileEnum = lastInputFileEnum < 0 )

  for ( int ik = firstInputFileEnum; ik <= lastInputFileEnum; ik++ ) {
    if ( ik < 0 ) 
      sprintf(fnameInput, "%s/%s.root", inFileDir, inFileNamePrefix);
    else if (ik < 10 )
      sprintf(fnameInput, "%s/%s_%1d.root", inFileDir, inFileNamePrefix, ik);
    else if (ik < 100 )
      sprintf(fnameInput, "%s/%s_%2d.root", inFileDir, inFileNamePrefix, ik);
    else if (ik < 1000 )
      sprintf(fnameInput, "%s/%s_%3d.root", inFileDir, inFileNamePrefix, ik);
    else
      sprintf(fnameInput, "%s/%s_%4d.root", inFileDir, inFileNamePrefix, ik);

    if ( !gSystem->Which("./", fnameInput ) ) { // check file availability
      std::cout << "File " << fnameInput << " doesn't exist." << std::endl;
    }
    else {
      tree.Add(fnameInput);
      std::cout << "Add tree from " << fnameInput 
	        << "   total number of entries (tracks): "
		<< tree.GetEntries() << std::endl;
    }
  }
  if ( tree.GetEntries() == 0 ) {
    std:: cout << "Tree is empty." << std::endl;
    return -2;
  }

  //--- Initialize tree
  CalibTree t(&tree,
	      minHcalEnergy, minPt,
	      limitForMipInEcal, limitForChargeIsolation,
	      minTrackMomentum, maxTrackMomentum);
  
  //--- Define files
  if ( isCrosscheck ) {
    sprintf(fnameInTxt, "%s_%s%02d-%02d-%02d_p%02d-%02d_pt%02d_eh%02d_ee%1d_step%1d.txt",
	    inTxtFilePrefix,
	    isoPrefix, int(t.limCharIso),
	    int(abs(FLEX_SEL_FIRST_CONST)),
	    int(abs(FLEX_SEL_SECOND_CONST)),
	    int(minTrackMomentum), int(maxTrackMomentum), int(minPt),
	    int(minHcalEnergy), int(limitForMipInEcal),
	    N_ETA_RINGS_PER_BIN
	    );
    sprintf(fnameOutRoot,
	    "test_%1d_%s_by_%s_%s%02d-%02d-%02d_p%02d-%02d_pt%02d_eh%02d_ee%1d_step%1d.root",
	    subSample,
	    inFileNamePrefix,
	    inTxtFilePrefix,
	    isoPrefix, int(t.limCharIso),
	    int(abs(FLEX_SEL_FIRST_CONST)),
	    int(abs(FLEX_SEL_SECOND_CONST)),
	    int(minTrackMomentum), int(maxTrackMomentum), int(minPt),
	    int(minHcalEnergy), int(limitForMipInEcal),
	    N_ETA_RINGS_PER_BIN
	    );
  }
  else {    
    sprintf(fnameOutTxt,
	    "%s_%1d_%s_i%02d_%s%02d-%02d-%02d_p%02d-%02d_pt%02d_eh%02d_ee%1d_step%1d.txt",
	    l3prefix,
	    subSample,
	    inFileNamePrefix,
	    maxNumberOfIterations,
	    isoPrefix, int(t.limCharIso),
	    int(abs(FLEX_SEL_FIRST_CONST)),
	    int(abs(FLEX_SEL_SECOND_CONST)),
	    int(minTrackMomentum), int(maxTrackMomentum), int(minPt),
	    int(minHcalEnergy), int(limitForMipInEcal),
	    N_ETA_RINGS_PER_BIN
	    );
    sprintf(fnameOutRoot,
	    "%s_%1d_%s_i%02d_%s%02d-%02d-%02d_p%02d-%02d_pt%02d_eh%02d_ee%1d_step%1d.root",
	    l3prefix,
	    subSample,
	    inFileNamePrefix,
	    maxNumberOfIterations,
	    isoPrefix, int(t.limCharIso),
	    int(abs(FLEX_SEL_FIRST_CONST)),
	    int(abs(FLEX_SEL_SECOND_CONST)),
	    int(minTrackMomentum), int(maxTrackMomentum), int(minPt),
	    int(minHcalEnergy), int(limitForMipInEcal),
	    N_ETA_RINGS_PER_BIN
	    );
  }  
  if ( !t.openOutputRootFile(fnameOutRoot) ) {
    std::cout << "Problems with booking output file " << fnameOutRoot << std::endl;
    return -1;
  }
  std::cout << "Correction for PU: ";
  if ( APPLY_CORRECTION_FOR_PU ) {
    std::cout << " applied for delta > " << DELTA_CUT << std::endl;
    std::cout << " for HB: " << LINEAR_COR_COEF[0]
	      << " ; " << SQUARE_COR_COEF[0]
	      << std::endl;
    std::cout << " for TR: " << LINEAR_COR_COEF[1]
	      << " ; " << SQUARE_COR_COEF[1]
	      << std::endl;
    std::cout << " for HE(<=24): " << LINEAR_COR_COEF[2]
	      << " ; " << SQUARE_COR_COEF[2]
	      << std::endl;
    std::cout << " for ieta=25: " << LINEAR_COR_COEF[3]
	      << " ; " << SQUARE_COR_COEF[3]
	      << std::endl;
    std::cout << " for ieta=26: " << LINEAR_COR_COEF[4]
	      << " ; " << SQUARE_COR_COEF[4]
	      << std::endl;
  }
  else
    std::cout << " no " << std::endl;
    
  /*
  std::cout << "Constant coefficient from charge isolation: "
	    << t.constForFlexSel << std::endl; 
  */

  unsigned int numOfSavedFactors(0);
  int nEventsWithGoodTrack(0);
  double MPVfromLastFit(0);
  
  if ( isCrosscheck ) {
    // open txt file and fill map with factors
    if ( t.getFactorsFromFile(fnameInTxt, Debug) ) {
      nEventsWithGoodTrack = t.firstLoop(subSample, false, Debug);
      std::cout << "Number of events with good track = "
		<< nEventsWithGoodTrack << std::endl;
      MPVfromLastFit = t.lastLoop(subSample, maxNumberOfIterations, true, Debug);
      std::cout << "Finish testing " << t.factors.size() << " factors from file "
		<< fnameInTxt << std::endl;
      std::cout << "MPV from fit after last iteration = "
		<< MPVfromLastFit << std::endl;
      std::cout << "Test plots saved in " << fnameOutRoot << std::endl;
    }
    else {
      std::cout << "File " << fnameInTxt << " doesn't exist." << std::endl;
    }
  }
  else {
    //--- Prepare initial histograms and count good track
    nEventsWithGoodTrack = t.firstLoop(subSample, shiftResponse, Debug);
    std::cout << "Number of events with good track = "
	      << nEventsWithGoodTrack << std::endl;
    //--- Iterate
    for ( unsigned int k = 0; k < maxNumberOfIterations; ++k ) {
      g_converge1->SetPoint( k, k+1, t.loopForIteration(subSample, k+1, Debug) );
      g_converge2->SetPoint( k, k+1, t.maxZtestFromWeights );
      g_converge3->SetPoint( k, k+1, t.maxSys2StatRatio );
    }
    //--- Finish
    MPVfromLastFit = t.lastLoop(subSample, maxNumberOfIterations, false, Debug);
    numOfSavedFactors = t.saveFactorsInFile(fnameOutTxt);

    sprintf(tname,"Mean deviation for subdetectors with Ntrack>%d",
	    MIN_N_TRACKS_PER_CELL);
    g_converge1->SetTitle(tname);
    g_converge1->GetXaxis()->SetTitle("iteration");
    t.foutRootFile->WriteTObject(g_converge1, "g_cvgD");
    sprintf(tname,"Max abs(Z-test) for factors");
    g_converge2->SetTitle(tname);
    g_converge2->GetXaxis()->SetTitle("iteration");
    t.foutRootFile->WriteTObject(g_converge2, "g_cvgW");
    sprintf(tname,"Max ratio of syst. to stat. uncertainty");
    g_converge3->SetTitle(tname);
    g_converge3->GetXaxis()->SetTitle("iteration");
    t.foutRootFile->WriteTObject(g_converge3, "g_cvgR");
    
    std::cout << "Finish adjusting factors after "
	      << maxNumberOfIterations << " iterations" << std::endl;
    std::cout << "MPV from fit after last iteration = "
	      << MPVfromLastFit << std::endl;
    std::cout << "Table with " << numOfSavedFactors << " factors"
	      << " with more than " << MIN_N_TRACKS_PER_CELL << " tracks/subdetector"
	      << " (from " << t.factors.size() << " available)"
	      << " is written in file " << fnameOutTxt << std::endl;
    std::cout << "Plots saved in " << fnameOutRoot << std::endl;
  }

  return numOfSavedFactors;
}

//**********************************************************
// CalibTree constructor
//**********************************************************
//CalibTree::CalibTree(TTree *tree) : fChain(0) {
CalibTree::CalibTree(TChain *tree,
		     double min_enrHcal,
		     double min_pt,
		     double lim_mipEcal,
		     double lim_charIso,
		     double min_trackMom,
		     double max_trackMom )
{ //: fChain(0) {
  // if parameter tree is not specified (or zero), connect the file
  // used to generate this class and read the Tree.
  if (tree == 0) {
    TFile *f = (TFile*)gROOT->GetListOfFiles()->FindObject("output.root");
    if (!f || !f->IsOpen()) {
      f = new TFile("output.root");
    }
    TDirectory * dir = (TDirectory*)f->Get("IsoTrackCalibration");
    dir->GetObject("CalibTree",tree);
  }
  Init(tree);

  referenceResponse = 1;
  maxNumOfTracksForIeta = 0;
  // initialization of maps
  factors.clear();
  uncFromWeights.clear();
  uncFromDeviation.clear();
  subDetector_trk.clear();
  subDetector_final.clear();
  nTrks.clear();
  nSubdetInEvent.clear();
  nPhiMergedInEvent.clear();
  sumOfResponse.clear();
  sumOfResponseSquared.clear();
  
  // selection
  minEnrHcal = min_enrHcal;
  minTrackPt = min_pt;
  minTrackMom = min_trackMom;
  maxTrackMom = max_trackMom;
  limMipEcal = lim_mipEcal;
  limCharIso = abs(lim_charIso);
  if ( lim_charIso < 0 ) 
    constForFlexSel = log(FLEX_SEL_FIRST_CONST/limCharIso)/FLEX_SEL_SECOND_CONST;
  else constForFlexSel = 0;
}

//**********************************************************
// CalibTree destructor
//**********************************************************
CalibTree::~CalibTree() {

    foutRootFile->cd();
    foutRootFile->Write();
    foutRootFile->Close();

  if (!fChain) return;
  delete fChain->GetCurrentFile();
}

//**********************************************************
// Get entry function
//**********************************************************
Int_t CalibTree::GetEntry(Long64_t entry) {
  // Read contents of entry.
  if (!fChain) return 0;
  return fChain->GetEntry(entry);
}

//**********************************************************
// Load tree function
//**********************************************************
Long64_t CalibTree::LoadTree(Long64_t entry) {
  // Set the environment to read one entry
  if (!fChain) return -5;
  Long64_t centry = fChain->LoadTree(entry);
  if (centry < 0) return centry;
  if (fChain->GetTreeNumber() != fCurrent) {
    fCurrent = fChain->GetTreeNumber();
    Notify();
  }
  return centry;
}

//**********************************************************
// Initialisation of TTree
//**********************************************************
void CalibTree::Init(TChain *tree) {
  // Set object pointer
  t_DetIds = 0;
  //t_DetIds1 = 0;
  //t_DetIds3 = 0;
  t_HitEnergies = 0;
  //t_HitEnergies1 = 0;
  //t_HitEnergies3 = 0;
  // Set branch addresses and branch pointers
  if (!tree) return;
  fChain = tree;
  fCurrent = -1;
  fChain->SetMakeClass(1);
  
  fChain->SetBranchAddress("t_Run", &t_Run, &b_t_Run);
  fChain->SetBranchAddress("t_Event", &t_Event, &b_t_Event);
  fChain->SetBranchAddress("t_nVtx", &t_nVtx, &b_t_nVtx);
  fChain->SetBranchAddress("t_nTrk", &t_nTrk, &b_t_nTrk);
  fChain->SetBranchAddress("t_EventWeight", &t_EventWeight, &b_t_EventWeight);
  fChain->SetBranchAddress("t_p", &t_p, &b_t_p);
  fChain->SetBranchAddress("t_pt", &t_pt, &b_t_pt);
  fChain->SetBranchAddress("t_ieta", &t_ieta, &b_t_ieta);
  fChain->SetBranchAddress("t_phi", &t_phi, &b_t_phi);
  fChain->SetBranchAddress("t_eMipDR", &t_eMipDR, &b_t_eMipDR);
  fChain->SetBranchAddress("t_eHcal", &t_eHcal, &b_t_eHcal);
  fChain->SetBranchAddress("t_eHcal10", &t_eHcal10, &b_t_eHcal10);
  fChain->SetBranchAddress("t_eHcal30", &t_eHcal30, &b_t_eHcal30);
  fChain->SetBranchAddress("t_hmaxNearP", &t_hmaxNearP, &b_t_hmaxNearP);
  fChain->SetBranchAddress("t_selectTk", &t_selectTk, &b_t_selectTk);
  fChain->SetBranchAddress("t_qltyMissFlag", &t_qltyMissFlag, &b_t_qltyMissFlag);
  fChain->SetBranchAddress("t_qltyPVFlag", &t_qltyPVFlag, &b_t_qltyPVFlag);
/*
  fChain->SetBranchAddress("t_l1pt", &t_l1pt, &b_t_l1pt);
  fChain->SetBranchAddress("t_l1eta", &t_l1eta, &b_t_l1eta);
  fChain->SetBranchAddress("t_l1phi", &t_l1phi, &b_t_l1phi);
  fChain->SetBranchAddress("t_l3pt", &t_l3pt, &b_t_l3pt);
  fChain->SetBranchAddress("t_l3eta", &t_l3eta, &b_t_l3eta);
  fChain->SetBranchAddress("t_l3phi", &t_l3phi, &b_t_l3phi);
*/
  fChain->SetBranchAddress("t_mindR1", &t_mindR1, &b_t_mindR1);
  fChain->SetBranchAddress("t_mindR2", &t_mindR2, &b_t_mindR2);
  fChain->SetBranchAddress("t_DetIds", &t_DetIds, &b_t_DetIds);
  //fChain->SetBranchAddress("t_DetIds1", &t_DetIds1, &b_t_DetIds1);
  //fChain->SetBranchAddress("t_DetIds3", &t_DetIds3, &b_t_DetIds3);
  fChain->SetBranchAddress("t_HitEnergies", &t_HitEnergies, &b_t_HitEnergies);
  //fChain->SetBranchAddress("t_HitEnergies1", &t_HitEnergies1, &b_t_HitEnergies1);
  //fChain->SetBranchAddress("t_HitEnergies3", &t_HitEnergies3, &b_t_HitEnergies3);
  Notify();
}

//**********************************************************
// Notification when opening new file
//**********************************************************
Bool_t CalibTree::Notify() {
  // The Notify() function is called when a new file is opened. This
  // can be either for a new TTree in a TChain or when when a new TTree
  // is started when using PROOF. It is normally not necessary to make changes
  // to the generated code, but the routine can be extended by the
  // user if needed. The return value is currently not used.
  
  return kTRUE;
}
//**********************************************************
// Open file and book histograms
//**********************************************************
bool CalibTree::openOutputRootFile(std::string fname)
{
  bool decision = false;
  
  foutRootFile = new TFile(fname.c_str(), "RECREATE");
  if ( foutRootFile != NULL ) decision = true;  
  foutRootFile->cd();

  return decision;
}

//**********************************************************
// Initial loop over events in the tree
//**********************************************************
Int_t CalibTree::firstLoop(unsigned int subsample,
			   bool shiftResp,
			   unsigned int debug)
{
  char name[100];
  unsigned int ndebug(0);
  double maxRespForGoodTrack(0);
  double minRespForGoodTrack(1000);
  int nRespOverHistLimit(0);
  int ntrk_ieta[NUM_ETA_BINS];
  for ( int j = 0; j < NUM_ETA_BINS; j++ ) {
    ntrk_ieta[j] = 0;
  }
  
  char scorr[80] = "correction for PU";
  char sxlabel[80] ="(E^{cor}_{hcal} + E_{ecal})/p_{track}"; 
  if ( !APPLY_CORRECTION_FOR_PU ) {
    sprintf(scorr,"no correction for PU");
    sprintf(sxlabel,"(E_{hcal} + E_{ecal})/p_{track}");
  }
  
  TF1* f1 = new TF1("f1","gaus", MIN_RESPONSE_HIST, MAX_RESPONSE_HIST);

  //--------- initialize histograms for response -----------------------------------------
  sprintf(name,"Initial HB+HE: %s", scorr);
  e2p_init = new TH1F("e2p_init", name,
		      NBIN_RESPONSE_HIST, MIN_RESPONSE_HIST, MAX_RESPONSE_HIST);
  e2p_init->Sumw2();
  e2p_init->GetXaxis()->SetTitle(sxlabel);
  
  sprintf(name,"Initial HB: %s", scorr);
  e2pHB_init = new TH1F("e2pHB_init", name,
			NBIN_RESPONSE_HIST/2, MIN_RESPONSE_HIST, MAX_RESPONSE_HIST);
  e2pHB_init->Sumw2();
  e2pHB_init->GetXaxis()->SetTitle(sxlabel);

  sprintf(name,"Initial TR: %s", scorr);
  e2pTR_init = new TH1F("e2pTR_init", name,
			NBIN_RESPONSE_HIST/10, MIN_RESPONSE_HIST, MAX_RESPONSE_HIST);
  e2pTR_init->Sumw2();
  e2pTR_init->GetXaxis()->SetTitle(sxlabel);

  sprintf(name,"Initial HE: %s", scorr);
  e2pHE_init = new TH1F("e2pHE_init", name,
			NBIN_RESPONSE_HIST/2, MIN_RESPONSE_HIST, MAX_RESPONSE_HIST);
  e2pHE_init->Sumw2();
  e2pHE_init->GetXaxis()->SetTitle(sxlabel);

  sprintf(name,"Response < %3.1f", LOW_RESPONSE);
  ieta_lefttail = new TH1F("ieta_lefttail", name,
			   2*MAX_ONESIDE_ETA_RINGS,
			   -MAX_ONESIDE_ETA_RINGS, MAX_ONESIDE_ETA_RINGS); 
  ieta_lefttail->GetXaxis()->SetTitle("i#eta");

  sprintf(name,"Response > %3.1f", HIGH_RESPONSE);
  ieta_righttail = new TH1F("ieta_righttail", name,
			   2*MAX_ONESIDE_ETA_RINGS,
			   -MAX_ONESIDE_ETA_RINGS, MAX_ONESIDE_ETA_RINGS); 			    
  ieta_righttail->GetXaxis()->SetTitle("i#eta");
  
//--- initialize chain ----------------------------------------
  if (fChain == 0) return 0;
  Long64_t nentries = fChain->GetEntriesFast();
  Long64_t nb = 0;
  
  int nSelectedEvents(0);

  if ( debug > 0 ) { 
    std::cout << "---------- First loop -------------------------- " << std::endl;
  }
// ----------------------- loop over events -------------------------------------  
  for (Long64_t jentry=0; jentry<nentries; jentry++) {
    Long64_t ientry = LoadTree(jentry);
    if ( ientry < 0 || ndebug > debug ) break;   
    nb = fChain->GetEntry(jentry);   //nbytes += nb;
    
    if ( (jentry%2 == subsample) ) continue;   // only odd or even events
 
// --------------- selection of good track --------------------    

    if ( !goodTrack(t_ieta) ) continue;
    
    if ( debug > 1 ) {
      ndebug++;
      std::cout << "***Entry (Track) Number : " << ientry << "(" << jentry << ")"
		<< " p/eHCal/eMipDR/nDets : " << t_p << "/" << t_eHcal 
		<< "/" << t_eMipDR << "/" << (*t_DetIds).size() 
		<< std::endl;
    }
    
    double eTotal(0.0);
    double eTotalWithEcal(0.0);
      
    // ---- loop over active subdetectors in the event for total energy ---
    unsigned int nDets = (*t_DetIds).size();
    for (unsigned int idet = 0; idet < nDets; idet++) { 
      eTotal += (*t_HitEnergies)[idet];
    }
    eTotalWithEcal = eTotal + t_eMipDR;    

// --- Correction for PU  --------
    double eTotalCor(eTotal);
    double eTotalWithEcalCor(eTotalWithEcal);
    //double e10(0.0);
    //double e30(0.0);
    double correctionForPU(1.0);
    double de2p(0.0);
    int abs_t_ieta = abs(t_ieta);
    
    if ( APPLY_CORRECTION_FOR_PU ) { 
      /*
      for (unsigned int idet1 = 0; idet1 < (*t_DetIds1).size(); idet1++) { 
	e10 += (*t_HitEnergies1)[idet1];
      }
      for (unsigned int idet3 = 0; idet3 < (*t_DetIds3).size(); idet3++) { 
	e30 += (*t_HitEnergies3)[idet3];
      }
      
      de2p = (e30 - e10)/t_p;
      */
      de2p = (t_eHcal30 - t_eHcal10)/t_p;

      if ( de2p > DELTA_CUT ) {
	int icor = int(abs_t_ieta >= FIRST_IETA_TR) + int(abs_t_ieta >= FIRST_IETA_HE)
	  + int(abs_t_ieta >= FIRST_IETA_FWD_1) + int(abs_t_ieta >= FIRST_IETA_FWD_2);
	correctionForPU = (1 + LINEAR_COR_COEF[icor]*(t_eHcal/t_p)*de2p
			   *(1 + SQUARE_COR_COEF[icor]*de2p));
      }
    }    

    // check for possibility to correct for PU
    if ( correctionForPU <= 0 || correctionForPU > 1 ) continue;
    nSelectedEvents++;

    eTotalCor = eTotal*correctionForPU;
    eTotalWithEcalCor = eTotalCor + t_eMipDR;

    double response = eTotalWithEcalCor/t_p;
    std::map<unsigned int, bool> sameSubdet;
    sameSubdet.clear();
    double resp2 = response*response;

    for (unsigned int idet = 0; idet < nDets; idet++) { 
      unsigned int detId = ( (*t_DetIds)[idet] & MASK ) | MASK2 ;

      if ( debug > 1 ) {
	unsigned int detId0 = ( (*t_DetIds)[idet] & MASK ) ;
	std::cout << "jentry/idet/detId :: ieta/z/depth ::: "
		  << std::dec
		  << jentry << " / "
		  << ((*t_DetIds)[idet]) << " / "
		  << detId0 << "(" << detId << ")" << " :: "
		  << ((detId0>>ETA_OFFSET) & ETA_MASK)
		  << "(" << ((detId>>ETA_OFFSET) & ETA_MASK) << ")" << " / "
		  << ((detId0&ZSIDE_MASK) ? 1 : -1)
		  << "(" << ((detId&ZSIDE_MASK) ? 1 : -1) << ")" << " / "
		  << ((detId0>>DEPTH_OFFSET)&DEPTH_MASK)
		  << "(" << ((detId>>DEPTH_OFFSET)&DEPTH_MASK) << ")"
		  << std::endl;
      }
      if (nPhiMergedInEvent.find(detId) != nPhiMergedInEvent.end()) 
	nPhiMergedInEvent[detId]++;
      else 
	nPhiMergedInEvent.insert(std::pair<unsigned int,int>(detId, 1));
		
      if (nTrks.find(detId) != nTrks.end()) {
	if ( sameSubdet.find(detId) == sameSubdet.end() ) {
	  nTrks[detId]++;
	  nSubdetInEvent[detId] += nDets;
	  sumOfResponse[detId] += response;
	  sumOfResponseSquared[detId] += resp2;
	  sameSubdet.insert(std::pair<unsigned int,bool>(detId, true));
	}
      }
      else {
	nTrks.insert(std::pair<unsigned int,int>(detId, 1));
	nSubdetInEvent.insert(std::pair<unsigned int,int>(detId, nDets));
	sumOfResponse.insert(std::pair<unsigned int,double>(detId,response));
	sumOfResponseSquared.insert(std::pair<unsigned int,double>(detId,resp2));
	sameSubdet.insert(std::pair<unsigned int,bool>(detId, true));
	subDetector_trk.insert(std::pair<unsigned int,
			       int>( detId,((*t_DetIds)[idet] &0xe000000) / 0x2000000 ));
      }
      
    }

// --- Fill initial histograms ---------------------------      
    e2p_init->Fill(response ,1.0);

    if ( abs_t_ieta < FIRST_IETA_TR )
      e2pHB_init->Fill(response ,1.0);
    else if ( abs_t_ieta < FIRST_IETA_HE )
      e2pTR_init->Fill(response ,1.0);
    else
      e2pHE_init->Fill(response ,1.0);

    if ( debug > 1 ) {
      std::cout << "***Entry : " << ientry
		<< " ***ieta/p/Ecal/nDet : "
		<< t_ieta << "/" << t_p
		<< "/" << t_eMipDR << "/" << (*t_DetIds).size() 
		<< " ***Etot/E10/E30/Ecor/cPU : " << t_eHcal
		<< "/" << t_eHcal10 << "/" << t_eHcal30
		<< "/" << eTotalCor << "/" << correctionForPU
		<< "(" << de2p << ")"
		<< std::endl;
    }
    if ( response > maxRespForGoodTrack  )
      maxRespForGoodTrack = response;
    if ( response < minRespForGoodTrack )
      minRespForGoodTrack = response;
    if ( response > MAX_RESPONSE_HIST )
      nRespOverHistLimit++;

    if ( response < LOW_RESPONSE ) ieta_lefttail->Fill(t_ieta);
    if ( response > HIGH_RESPONSE ) ieta_righttail->Fill(t_ieta);

    int jj = HALF_NUM_ETA_BINS + int(t_ieta/N_ETA_RINGS_PER_BIN) + (t_ieta>0);
    ntrk_ieta[jj]++;
    
  } // ------------------- end of loop over events -------------------------------------

  for ( int j = 0; j < NUM_ETA_BINS; j++ ) {
    if ( maxNumOfTracksForIeta < ntrk_ieta[j] ) maxNumOfTracksForIeta = ntrk_ieta[j];
  }

  double jeta[N_DEPTHS][MAXNUM_SUBDET];
  double nTrk[N_DEPTHS][MAXNUM_SUBDET];
  double nSub[N_DEPTHS][MAXNUM_SUBDET];
  double nPhi[N_DEPTHS][MAXNUM_SUBDET];
  double rms[N_DEPTHS][MAXNUM_SUBDET];
  unsigned int kdep[N_DEPTHS];
  for ( unsigned ik = 0; ik < N_DEPTHS; ik++ ) { kdep[ik] = 0; }

  // fill number of tracks
  std::map <unsigned int,int>::iterator nTrksItr = nTrks.begin();
  for (nTrksItr = nTrks.begin(); nTrksItr != nTrks.end(); nTrksItr++ ) {
    unsigned int detId = nTrksItr->first;
    int depth= ((detId>>DEPTH_OFFSET) & DEPTH_MASK) + int(MERGE_PHI_AND_DEPTHS);
    int zside= (detId&ZSIDE_MASK) ? 1 : -1;
    unsigned int kcur = kdep[depth-1];
    
    jeta[depth-1][kcur] = int((detId>>ETA_OFFSET) & ETA_MASK)*zside;
    nTrk[depth-1][kcur] = nTrksItr->second;
    nSub[depth-1][kcur] = double(nSubdetInEvent[detId])/double(nTrksItr->second);
    nPhi[depth-1][kcur] = double(nPhiMergedInEvent[detId])/double(nTrksItr->second);
    if ( nTrk[depth-1][kcur] > 1 ) 
      rms[depth-1][kcur] = sqrt((sumOfResponseSquared[detId] -
				 pow(sumOfResponse[detId],2)/nTrk[depth-1][kcur])
				/(nTrk[depth-1][kcur] - 1));
    else rms[depth-1][kcur] = RESOLUTION_HCAL;
    kdep[depth-1]++;
  }
  for ( unsigned ik = 0; ik < N_DEPTHS; ik++ ) {
    double x[MAXNUM_SUBDET];
    double ytrk[MAXNUM_SUBDET], ysub[MAXNUM_SUBDET];
    double yphi[MAXNUM_SUBDET], yrms[MAXNUM_SUBDET];
    for ( unsigned im = 0; im < MAXNUM_SUBDET; im++ ) {
      x[im] = jeta[ik][im];
      ytrk[im] = nTrk[ik][im];
      ysub[im] = nSub[ik][im];
      yphi[im] = nPhi[ik][im];
      yrms[im] = rms[ik][im];
    }
    TGraph*  g_ntrk = new TGraph(kdep[ik], x, ytrk);
    sprintf(name, "Number of tracks for depth %1d", ik+1);
    g_ntrk->SetTitle(name);
    sprintf(name, "nTrk_depth%1d", ik+1);
    foutRootFile->WriteTObject(g_ntrk, name);

    TGraph*  g_nsub = new TGraph(kdep[ik], x, ysub);
    sprintf(name, "Mean number of active subdetectors, depth %1d", ik+1);
    g_nsub->SetTitle(name);
    sprintf(name, "nSub_depth%1d", ik+1);
    foutRootFile->WriteTObject(g_nsub, name);

    TGraph*  g_nphi = new TGraph(kdep[ik], x, yphi);
    sprintf(name, "Mean number of phi-merged subdetectors, depth %1d", ik+1);
    g_nphi->SetTitle(name);
    sprintf(name, "nPhi_depth%1d", ik+1);
    foutRootFile->WriteTObject(g_nphi, name);

    TGraph*  g_rms = new TGraph(kdep[ik], x, yrms);
    sprintf(name, "RMS of samples, depth %1d", ik+1);
    g_rms->SetTitle(name);
    sprintf(name, "rms_depth%1d", ik+1);
    foutRootFile->WriteTObject(g_rms, name);
  }

  //--- estimate ratio mean/MPV
  double xl = e2p_init->GetMean() - FIT_RMS_INTERVAL*e2p_init->GetRMS();
  double xr = e2p_init->GetMean() + FIT_RMS_INTERVAL*e2p_init->GetRMS();
  e2p_init->Fit("f1","QN", "R", xl, xr);
  xl = f1->GetParameter(1) - FIT_RMS_INTERVAL*f1->GetParameter(2);
  xr = f1->GetParameter(1) + FIT_RMS_INTERVAL*f1->GetParameter(2);
  e2p_init->Fit("f1","QN", "R", xl, xr);

  if ( shiftResp && (f1->GetParameter(1) != 0) ) {
    referenceResponse = e2p_init->GetMean()/f1->GetParameter(1);
    std::cout << "Use reference response=<mean from sample>/<mpv from fit>:"
	      << e2p_init->GetMean() << "/" << f1->GetParameter(1)
	      << " = " << referenceResponse //<< std::endl
	      << " (chi2ndf = " << f1->GetChisquare()/f1->GetNDF() << ")"
	      << std::endl;
  }
  else {
    referenceResponse = 1;
    std::cout << "Use reference response = 1" << std::endl
	      << "<mean from sample>/<mpv from fit> = "
	      << e2p_init->GetMean()/f1->GetParameter(1)
	      << "  (chi2ndf = " << f1->GetChisquare()/f1->GetNDF() << ")"
	      << std::endl;
  }
  //---- for HB
  xl = e2pHB_init->GetMean() - FIT_RMS_INTERVAL*e2pHB_init->GetRMS();
  xr = e2pHB_init->GetMean() + FIT_RMS_INTERVAL*e2pHB_init->GetRMS();
  e2pHB_init->Fit("f1","QN", "R", xl, xr);
  xl = f1->GetParameter(1) - FIT_RMS_INTERVAL*f1->GetParameter(2);
  xr = f1->GetParameter(1) + FIT_RMS_INTERVAL*f1->GetParameter(2);
  e2pHB_init->Fit("f1","QN", "R", xl, xr);

  if ( shiftResp && (f1->GetParameter(1) != 0) ) {
    referenceResponseHB = e2pHB_init->GetMean()/f1->GetParameter(1);
    std::cout << "In HB <mean from sample>/<mpv from fit> = "
	      << e2pHB_init->GetMean() << "/" << f1->GetParameter(1)
	      << " = " << referenceResponseHB //<< std::endl
	      << " (chi2ndf = " << f1->GetChisquare()/f1->GetNDF() << ")"
	      << std::endl;
  }
  else {
    referenceResponseHB = 1;
    std::cout << "Use reference response in HB = 1" << std::endl
	      << "<mean from sample>/<mpv from fit> = "
	      << e2pHB_init->GetMean()/f1->GetParameter(1)
	      << "  (chi2ndf = " << f1->GetChisquare()/f1->GetNDF() << ")"
	      << std::endl;
  }
  //---- for TR
  xl = e2pTR_init->GetMean() - FIT_RMS_INTERVAL*e2pTR_init->GetRMS();
  xr = e2pTR_init->GetMean() + FIT_RMS_INTERVAL*e2pTR_init->GetRMS();
  e2pTR_init->Fit("f1","QN", "R", xl, xr);
  xl = f1->GetParameter(1) - FIT_RMS_INTERVAL*f1->GetParameter(2);
  xr = f1->GetParameter(1) + FIT_RMS_INTERVAL*f1->GetParameter(2);
  e2pTR_init->Fit("f1","QN", "R", xl, xr);

  if ( shiftResp && (f1->GetParameter(1) != 0) ) {
    referenceResponseTR = e2pTR_init->GetMean()/f1->GetParameter(1);
    std::cout << "In TR <mean from sample>/<mpv from fit> = "
	      << e2pTR_init->GetMean() << "/" << f1->GetParameter(1)
	      << " = " << referenceResponseTR //<< std::endl
	      << " (chi2ndf = " << f1->GetChisquare()/f1->GetNDF() << ")"
	      << std::endl;
  }
  else {
    referenceResponseTR = 1;
    std::cout << "Use reference response in TR = 1" << std::endl
	      << "<mean from sample>/<mpv from fit> = "
	      << e2pTR_init->GetMean()/f1->GetParameter(1)
	      << "  (chi2ndf = " << f1->GetChisquare()/f1->GetNDF() << ")"
	      << std::endl;
  }
  //---- for HE
  xl = e2pHE_init->GetMean() - FIT_RMS_INTERVAL*e2pHE_init->GetRMS();
  xr = e2pHE_init->GetMean() + FIT_RMS_INTERVAL*e2pHE_init->GetRMS();
  e2pHE_init->Fit("f1","QN", "R", xl, xr);
  xl = f1->GetParameter(1) - FIT_RMS_INTERVAL*f1->GetParameter(2);
  xr = f1->GetParameter(1) + FIT_RMS_INTERVAL*f1->GetParameter(2);
  e2pHE_init->Fit("f1","QN", "R", xl, xr);

  if ( shiftResp && (f1->GetParameter(1) != 0) ) {
    referenceResponseHE = e2pHE_init->GetMean()/f1->GetParameter(1);
    std::cout << "In HE <mean from sample>/<mpv from fit> = "
	      << e2pHE_init->GetMean() << "/" << f1->GetParameter(1)
	      << " = " << referenceResponseHE //<< std::endl
	      << " (chi2ndf = " << f1->GetChisquare()/f1->GetNDF() << ")"
	      << std::endl;
  }
  else {
    referenceResponseHE = 1;
    std::cout << "Use reference response in HE = 1" << std::endl
	      << "<mean from sample>/<mpv from fit> = "
	      << e2pHE_init->GetMean()/f1->GetParameter(1)
	      << "  (chi2ndf = " << f1->GetChisquare()/f1->GetNDF() << ")"
	      << std::endl;
  }

  //----- print additional info
  std::cout << "Maximal response for good tracks = " 
	    << maxRespForGoodTrack << std::endl
	    << nRespOverHistLimit
	    << " events with response > " << MAX_RESPONSE_HIST
	    << "(hist limit for mean estimate)"
	    << std::endl;
  std::cout << "Minimal response for good tracks = " 
	    << minRespForGoodTrack
	    << std::endl;
  std::cout << "Maximum number of selected tracks per ieta bin = " 
	    << maxNumOfTracksForIeta
	    << std::endl;
  std::cout << "Number of selected tracks in HB = "
	    << e2pHB_init->GetEntries()
	    << std::endl;
  std::cout << "Number of selected tracks in TR = "
	    << e2pTR_init->GetEntries()
	    << std::endl;
  std::cout << "Number of selected tracks in HE = "
	    << e2pHE_init->GetEntries()
	    << std::endl;

  /*
  xl = e2pHB_init->GetMean() - FIT_RMS_INTERVAL*e2pHB_init->GetRMS();
  xr = e2pHB_init->GetMean() + FIT_RMS_INTERVAL*e2pHB_init->GetRMS();
  e2pHB_init->Fit("f1","QN", "R", xl, xr);

  xl = e2pHE_init->GetMean() - FIT_RMS_INTERVAL*e2pHE_init->GetRMS();
  xr = e2pHE_init->GetMean() + FIT_RMS_INTERVAL*e2pHE_init->GetRMS();
  e2pHE_init->Fit("f1","QN", "R", xl, xr);
  */
  return nSelectedEvents;
}

//**********************************************************
// Loop over events in the tree for current iteration
//**********************************************************
Double_t CalibTree::loopForIteration(unsigned int subsample,
				     unsigned int nIter,
				     unsigned int debug )
{
  char name[500];
  double meanDeviation = 0;
  unsigned int ndebug(0);
    
  TF1* f1 = new TF1("f1","gaus", MIN_RESPONSE_HIST, MAX_RESPONSE_HIST);
  TH1F* e2p[NUM_ETA_BINS];

  int n_ieta_bins = 2*2.5*pow(maxNumOfTracksForIeta,1/3.0);
  for ( int i = 0; i < NUM_ETA_BINS; i++ ) {
    sprintf(name,"e2p[%02d]", i);
    e2p[i] = new TH1F(name, "",
		      n_ieta_bins, //NBIN_RESPONSE_HIST_IND,
		      MIN_RESPONSE_HIST, MAX_RESPONSE_HIST);
    e2p[i]->Sumw2();
  }

  std::map<unsigned int, std::pair<double,double> > sumsForFactorCorrection;
  std::map<unsigned int, double> sumOfWeightsSquared;

  if ( debug > 0 ) {
    std::cout.precision(3);
    std::cout << "-------------------------------------------- nIter = "
	      << nIter << std::endl;
  }
//--- initialize chain ----------------------------------------
  if (fChain == 0) return 0;
  Long64_t nentries = fChain->GetEntriesFast();
  Long64_t nb = 0;
  
// ----------------------- loop over events -------------------------------------  
  for (Long64_t jentry=0; jentry<nentries; jentry++) {
    Long64_t ientry = LoadTree(jentry);
    if ( ientry < 0 || ndebug > debug ) break;   
    nb = fChain->GetEntry(jentry);   //nbytes += nb;
    
    if ( (jentry%2 == subsample) ) continue;   // only odd or even events

    // --------------- selection of good track --------------------
    
    if ( !goodTrack(t_ieta) ) continue;

    if ( debug > 1 ) {
      ndebug++;
      std::cout << "***Entry (Track) Number : " << ientry 
		<< " p/eHCal/eMipDR/nDets : " << t_p << "/" << t_eHcal 
		<< "/" << t_eMipDR << "/" << (*t_DetIds).size() 
		<< std::endl;
    }
    
    double eTotal(0.0);
    double eTotalWithEcal(0.0);
      
    // ---- first loop over active subdetectors in the event for total energy ---

    for (unsigned int idet = 0; idet < (*t_DetIds).size(); idet++) { 
      double hitEnergy(0);	
      unsigned int detId = ( (*t_DetIds)[idet] & MASK ) | MASK2 ;
	
      if (factors.find(detId) != factors.end()) 
	hitEnergy = factors[detId] * (*t_HitEnergies)[idet];
      else 
	hitEnergy = (*t_HitEnergies)[idet];

      eTotal += hitEnergy;
    }

    eTotalWithEcal = eTotal + t_eMipDR;    

// --- Correction for PU   --------      
    double eTotalCor(eTotal);
    double eTotalWithEcalCor(eTotalWithEcal);
    //double e10(0.0);
    //double e30(0.0);
    double correctionForPU(1.0);

    if ( APPLY_CORRECTION_FOR_PU ) {
      /*
      for (unsigned int idet1 = 0; idet1 < (*t_DetIds1).size(); idet1++) { 
	double hitEnergy(0);
	unsigned int detId1 = ( (*t_DetIds1)[idet1] & MASK ) | MASK2;
	
	if (factors.find(detId1) != factors.end()) 
	  hitEnergy = factors[detId1] * (*t_HitEnergies1)[idet1];
	else 
	  hitEnergy = (*t_HitEnergies1)[idet1];
	
	e10 += hitEnergy;
      }
      for (unsigned int idet3 = 0; idet3 < (*t_DetIds3).size(); idet3++) { 
	double hitEnergy(0);
	unsigned int detId3 = ( (*t_DetIds3)[idet3] & MASK ) | MASK2;
	
	if (factors.find(detId3) != factors.end()) 
	  hitEnergy = factors[detId3] * (*t_HitEnergies3)[idet3];
	else
	  hitEnergy = (*t_HitEnergies3)[idet3];
	
	e30 += hitEnergy;
      }
      double de2p = (e30 - e10)/t_p;
      */
      double de2p = (t_eHcal30 - t_eHcal10)/t_p;
      if ( de2p > DELTA_CUT ) {
	int abs_t_ieta = abs(t_ieta);
	int icor = int(abs_t_ieta >= FIRST_IETA_TR) + int(abs_t_ieta >= FIRST_IETA_HE)
	  + int(abs_t_ieta >= FIRST_IETA_FWD_1) + int(abs_t_ieta >= FIRST_IETA_FWD_2);
	correctionForPU = (1 + LINEAR_COR_COEF[icor]*(t_eHcal/t_p)*de2p
			   *(1 + SQUARE_COR_COEF[icor]*de2p));
      }
    }    

    // check for possibility to correct for PU
    if ( correctionForPU <= 0 || correctionForPU > 1 ) continue;

    eTotalCor = eTotal*correctionForPU;
    eTotalWithEcalCor = eTotalCor + t_eMipDR;
     
    int jeta = HALF_NUM_ETA_BINS + int(t_ieta/N_ETA_RINGS_PER_BIN) + (t_ieta>0);
    e2p[jeta]->Fill(eTotalWithEcalCor/t_p ,1.0);
      
// ---- second loop over active subdetectors in the event  -----------------
      
    double response = eTotalWithEcalCor/t_p; // - referenceResponse;
    
    for (unsigned int idet = 0; idet < (*t_DetIds).size(); idet++) {
      double hitEnergy(0);
      unsigned int detId = ( (*t_DetIds)[idet] & MASK ) | MASK2 ;
		 
      if (factors.find(detId) != factors.end())
	hitEnergy = factors[detId] * (*t_HitEnergies)[idet];
      else
	hitEnergy = (*t_HitEnergies)[idet];

      double cellWeight = hitEnergy/eTotal;   
      //double trackWeight = (cellWeight * t_p) / eTotalWithEcalCor; // old method
      double trackWeight = cellWeight*response;   // new method
      double cellweight2 = cellWeight*cellWeight;
      
      if( sumsForFactorCorrection.find(detId) != sumsForFactorCorrection.end() ) {
	cellWeight  += sumsForFactorCorrection[detId].first;
	trackWeight += sumsForFactorCorrection[detId].second;
	sumsForFactorCorrection[detId] = std::pair<double,double>(cellWeight,trackWeight);
	sumOfWeightsSquared[detId] += cellweight2;
      }
      else {
	sumsForFactorCorrection.insert(std::pair<unsigned int,
				       std::pair<double,double> >(detId,
								  std::pair<double,double>(cellWeight,
											   trackWeight)));
	sumOfWeightsSquared.insert(std::pair<unsigned int,double>(detId, cellweight2));
     }
	
      if ( debug > 1 ) { //|| hitEnergy < -0.5) {
	double f = 1;
	int zside= (detId&ZSIDE_MASK) ? 1 : -1;
	if (factors.find(detId) != factors.end()) f = factors[detId];
	std::cout << jentry << "::: "
		  << " Ncells: " << (*t_DetIds).size()
		  << " !! detId(ieta)/e/f : " 
	    //    << std::hex << (*t_DetIds)[idet] << ":"
		  << detId << "(" << int((detId>>ETA_OFFSET) & ETA_MASK)*zside << ")"
		  << "/" << hitEnergy
		  << "/" << f
		  << " ||| cellW/trW : " << cellWeight << " / " << trackWeight
		  << " ||| E/Ecor/p : " << eTotal
		  << " / " << eTotalCor
		  << " / " << t_p
		  << " || e10/e30/cF : " << t_eHcal10
		  << " / " << t_eHcal30
		  << " / " << correctionForPU
		  << std::endl;
      }
    }  // --------------- end of second loop over cells ----------
  } // ------------------- end of loop over events -------------------------------------

//----- Graphs to be saved in root file ----------------
  if ( debug > 0 ) {
    std::cout << "Fit and calculate means..." << std::endl;
    std::cout << "Number of plots (ieta bins) = " << NUM_ETA_BINS << std::endl;
  }
  TGraph *g_chi = new TGraph(NUM_ETA_BINS);  
  TGraphErrors* g_e2pFit = new TGraphErrors(NUM_ETA_BINS);
  TGraphErrors* g_e2pMean = new TGraphErrors(NUM_ETA_BINS);
  TGraph *g_nhistentries = new TGraph(NUM_ETA_BINS);
  
  int ipoint(0);
  for ( int i = 0; i < NUM_ETA_BINS; i++ ) {
    int ieta = (i - HALF_NUM_ETA_BINS - (i>HALF_NUM_ETA_BINS))*N_ETA_RINGS_PER_BIN;
    if ( N_ETA_RINGS_PER_BIN > 1 ) {
      ieta = (i > HALF_NUM_ETA_BINS) ? ieta+1 : ieta-1;
    }
    int nhistentries = e2p[i]->GetEntries();
    /*
      if ( debug > 0 ) {
	std::cout << "i / entries / ieta :::"
		  << i
		  << " / " << nhistentries
		  << " / " << ieta
		  << std::endl;
      }
    */
     if ( nIter == 1 ) {
       g_nhistentries->SetPoint(i, ieta, nhistentries);
     }

    if ( nhistentries < 1 ) continue;
    else {
      g_e2pMean->SetPoint(ipoint, ieta, e2p[i]->GetMean());
      g_e2pMean->SetPointError(ipoint, 0, e2p[i]->GetMeanError());

      if ( nhistentries > MIN_N_ENTRIES_FOR_FIT ) {
	int nrebin = n_ieta_bins/(2*2.5*pow(nhistentries,1/3.0));
	if ( nrebin > 2 ) e2p[i]->Rebin(nrebin);
	
	double xl = e2p[i]->GetMean() - FIT_RMS_INTERVAL*e2p[i]->GetRMS();
	double xr = e2p[i]->GetMean() + FIT_RMS_INTERVAL*e2p[i]->GetRMS();
	e2p[i]->Fit("f1","QN", "R", xl, xr);
	xl = f1->GetParameter(1) - FIT_RMS_INTERVAL*f1->GetParameter(2);
	xr = f1->GetParameter(1) + FIT_RMS_INTERVAL*f1->GetParameter(2);
	e2p[i]->Fit("f1","QN", "R", xl, xr);
	g_e2pFit->SetPoint(ipoint, ieta, f1->GetParameter(1));
	g_e2pFit->SetPointError(ipoint, 0, f1->GetParError(1));
	g_chi->SetPoint(ipoint, ieta, f1->GetChisquare()/f1->GetNDF());
      }
      else {
	g_e2pFit->SetPoint(ipoint, ieta, e2p[i]->GetMean());
	g_e2pFit->SetPointError(ipoint, 0, e2p[i]->GetMeanError());
	g_chi->SetPoint(ipoint, ieta, 0);
      }
      ipoint++;
    }
  }
  // fill number of tracks per ieta
  if ( nIter == 1 ) {
      sprintf(name, "Number of selected tracks");
      g_nhistentries->SetTitle(name);
      g_nhistentries->GetXaxis()->SetTitle("i#eta");
      sprintf(name, "selTrks");
      foutRootFile->WriteTObject(g_nhistentries, name);   
  }

  for ( int k = ipoint; k < NUM_ETA_BINS; k++ ) {
    g_e2pFit->RemovePoint(ipoint);
    g_e2pMean->RemovePoint(ipoint);
  }
  sprintf(name, "Response from fit, iteration %2d", nIter);
  g_e2pFit->SetTitle(name);
  g_e2pFit->GetXaxis()->SetTitle("i#eta");
  sprintf(name, "respFit_%d", nIter);
  foutRootFile->WriteTObject(g_e2pFit, name);

  sprintf(name, "Mean response, iteration %2d", nIter);
  g_e2pMean->SetTitle(name);
  g_e2pMean->GetXaxis()->SetTitle("i#eta");
  sprintf(name, "respMean_%d", nIter);
  foutRootFile->WriteTObject(g_e2pMean, name);

  sprintf(name, "Chi2/NDF, iteration %2d", nIter);
  g_chi->SetTitle(name);
  g_chi->GetXaxis()->SetTitle("i#eta");
  sprintf(name, "chi2ndf_%d", nIter);
  foutRootFile->WriteTObject(g_chi, name);

// --- convergence criteria and correction factors -----------------------------------

  double MeanConvergenceDelta(0),  MaxRelDeviationWeights(0), MaxRatioUncertainties(0);
  double dets[MAXNUM_SUBDET];
  double ztest[MAXNUM_SUBDET], sys2statRatio[MAXNUM_SUBDET];

  if ( debug > 0 ) std::cout << "Calculate correction factors..." << std::endl;

  unsigned int kount(0), mkount(0);
  unsigned int maxKountW(0), maxKountR(0);

  double fac[N_DEPTHS][MAXNUM_SUBDET];
  double dfac[N_DEPTHS][MAXNUM_SUBDET];
  double ieta[N_DEPTHS][MAXNUM_SUBDET];
  double dieta[N_DEPTHS][MAXNUM_SUBDET];

  unsigned int kdep[N_DEPTHS];
  for ( unsigned ik = 0; ik < N_DEPTHS; ik++ ) { kdep[ik] = 0; }

//-------------- loop over all cells ---------------------------------
  std::map <unsigned int,
	    std::pair<double,double> >::iterator sumsForFactorCorrectionItr
    = sumsForFactorCorrection.begin();
  for (; sumsForFactorCorrectionItr != sumsForFactorCorrection.end();
       sumsForFactorCorrectionItr++) {

    unsigned int detId = sumsForFactorCorrectionItr->first;
    double sumOfWeights = (sumsForFactorCorrectionItr->second).first;
    int nSubDetTracks(0);
    double subdetRMS(RESOLUTION_HCAL);
    if ( nTrks.find(detId) != nTrks.end() ) {
      nSubDetTracks = nTrks[detId];
      if ( nSubDetTracks > 1 ) 
	subdetRMS = sqrt((sumOfResponseSquared[detId] 
			  - pow(sumOfResponse[detId],2)/double(nSubDetTracks))
			 /double(nSubDetTracks - 1));
    }
    else {
      std::cout << "!!!!!!! No tracks for subdetector " << detId << std::endl;
      continue;
    }
    double NcellMean = double(nSubdetInEvent[detId])/double(nSubDetTracks);

    double ratioWeights(1);
    if ( abs(sumOfWeights) > 0 )
      ratioWeights = sqrt(sumOfWeightsSquared[detId])/sumOfWeights;
    double correctionRMS = subdetRMS*ratioWeights*sqrt(NcellMean);
    
    double absErrorW(0);
    double absErrorWprevious(0);
    double factorPrevious(1);
    double factorCorrection(1);

    int zside = (detId&ZSIDE_MASK) ? 1 : -1;
    int depth = ((detId>>DEPTH_OFFSET)&DEPTH_MASK) + int(MERGE_PHI_AND_DEPTHS);
    unsigned int kcur = kdep[depth-1];
    
    ieta[depth-1][kcur] = int((detId>>ETA_OFFSET) & ETA_MASK)*zside;
    dieta[depth-1][kcur] = 0;
    
    double refR = referenceResponse;
    if ( !SINGLE_REFERENCE_RESPONSE ) {
      if ( abs(ieta[depth-1][kcur]) < FIRST_IETA_TR )
	refR = referenceResponseHB;
      else if ( abs(ieta[depth-1][kcur]) < FIRST_IETA_HE )
	refR = referenceResponseTR;
      else
	refR = referenceResponseHE;
    }
	
    //--- old expression ---------------
    /*
    factorCorrection = (sumsForFactorCorrectionItr->second).second
                     / (sumsForFactorCorrectionItr->second).first;
    */
    //--- new expression --------------
    if ( abs(sumOfWeights) > 0 )  
      factorCorrection = 1 + refR
	- (sumsForFactorCorrectionItr->second).second / sumOfWeights;
    //---------------------------------
    if ( correctionRMS/factorCorrection > MAX_REL_UNC_FACTOR ||
	 nSubDetTracks < MIN_N_TRACKS_PER_CELL ) {
      correctionRMS = sqrt(pow(correctionRMS,2) + pow((factorCorrection - 1),2));
      factorCorrection = 1;
    }
    
    if( nSubDetTracks > MIN_N_TRACKS_PER_CELL ) {
      if (factorCorrection > 1) MeanConvergenceDelta += (1 - 1/factorCorrection);
      else                      MeanConvergenceDelta += (1 - factorCorrection);
      mkount++;
    }

              
    if (factors.find(detId) != factors.end()) {
      factorPrevious = factors[detId];
      factors[detId] *= factorCorrection;
      absErrorWprevious = uncFromWeights[detId];
      absErrorW = factorPrevious*correctionRMS;
      uncFromWeights[detId] = absErrorW;
      uncFromDeviation[detId] = factorPrevious*abs(factorCorrection - 1);
    }
    else {
      factorPrevious = 1;
      factors.insert(std::pair<unsigned int, double>(detId, factorCorrection));
      subDetector_final.insert(std::pair<unsigned int, double>(detId,
							       subDetector_trk[detId]));
      absErrorW = correctionRMS;
      absErrorWprevious = 0;
      uncFromWeights.insert(std::pair<unsigned int, double>(detId, absErrorW));
      uncFromDeviation.insert(std::pair<unsigned int, double>(detId,
							      abs(factorCorrection - 1)));
    }

    if ( debug > 0 ) {
      //if ( ieta[depth-1][kcur] == 27 && depth == 2 ) {
      std::cout.precision(3);
      std::cout << detId // << " (" << mkount << ")"
	        << " *** ieta/depth | rw | cw | tw | fCor | nTrk | Ncell | C |::: "
	        << ieta[depth-1][kcur] << "/" << depth << " | "
		<< ratioWeights << " | "
	        << sumOfWeights << " | "
	        << (sumsForFactorCorrectionItr->second).second << " | "
	        << factorCorrection << " | "
		<< nSubDetTracks << " | "
		<< NcellMean << " | "
		<< correctionRMS << " |"
	        << std::endl;
    }

    dets[kount] = detId;

    fac[depth-1][kcur] = factors[detId];
    dfac[depth-1][kcur] =
      sqrt(pow(uncFromWeights[detId],2) + pow(uncFromDeviation[detId],2));
    
    sys2statRatio[kount] = abs(factorPrevious*(factorCorrection - 1))/absErrorW;
    if ( sys2statRatio[kount] > MaxRatioUncertainties ) {
      MaxRatioUncertainties = sys2statRatio[kount];
      maxKountR = kount;
    }
    ztest[kount] = factorPrevious*(factorCorrection - 1)
      /sqrt(pow(absErrorWprevious,2) + pow(absErrorW,2));
    if ( abs(ztest[kount]) > MaxRelDeviationWeights ) {
      MaxRelDeviationWeights = abs(ztest[kount]);
      maxKountW = kount;
    } 
    kount++;
    kdep[depth-1]++;
  }

//---- write current plots -----------------------------
  if ( debug > 0 ) std::cout << "Write graphs..." << std::endl;

  for ( unsigned ik = 0; ik < N_DEPTHS; ik++ ) {
    double x[MAXNUM_SUBDET], dx[MAXNUM_SUBDET], y[MAXNUM_SUBDET], dy[MAXNUM_SUBDET];
    for ( unsigned im = 0; im < MAXNUM_SUBDET; im++ ) {
      x[im] = ieta[ik][im];
      dx[im] = dieta[ik][im];
      y[im] = fac[ik][im];
      dy[im] = dfac[ik][im];
    }
    TGraphErrors*  g_fac = new TGraphErrors(kdep[ik], x, y, dx, dy);
    sprintf(name, "Extracted correction factors, depth %1d", ik+1);
    g_fac->SetTitle(name);
    g_fac->GetXaxis()->SetTitle("i#eta");
    sprintf(name, "Cfacs_depth%1d_%02d", ik+1, nIter);
    foutRootFile->WriteTObject(g_fac, name);
  }

  TGraph  *g_ztest, *g_sys2stat;

  g_ztest = new TGraph(kount, dets, ztest); 
  sprintf(name, "Z-test (unc. from weights) vs detId for iter %d", nIter);
  g_ztest->SetTitle(name);
  sprintf(name, "Ztest_detId_%02d", nIter);
  foutRootFile->WriteTObject(g_ztest, name);

  g_sys2stat = new TGraph(kount, dets, sys2statRatio); 
  sprintf(name, "Ratio of syst. to stat. unc. vs detId for iter %d", nIter);
  g_sys2stat->SetTitle(name);
  sprintf(name, "Sys2stat_detId_%02d", nIter);
  foutRootFile->WriteTObject(g_sys2stat, name);

  std::cout << "----------Iteration " << nIter << "--------------------" << std::endl;
  maxZtestFromWeights = MaxRelDeviationWeights; 
  std::cout << "Max abs(Z-test) with stat errors from weights = "
	    << maxZtestFromWeights << " for subdetector " << maxKountW << std::endl;
  maxSys2StatRatio = MaxRatioUncertainties; 
  std::cout << "Max ratio of syst.(f_cur - f_prev) to stat. uncertainty = "
	    << maxSys2StatRatio << " for subdetector " << maxKountR << std::endl;

  meanDeviation = (mkount > 0) ? (MeanConvergenceDelta/mkount) : 0;
  std::cout << "Mean absolute deviation from previous iteration = " << meanDeviation
	    << " for " << mkount
	    << " from " << kount << " DetIds" << std::endl;

//--- delete hists ---------------------------
  for ( int i = 0; i < NUM_ETA_BINS; i++ ) {
    delete e2p[i];
  }

  return meanDeviation;
}
//**********************************************************
// Last loop over events in the tree
//**********************************************************
Double_t CalibTree::lastLoop(unsigned int subsample,
			     unsigned int maxIter,
			     bool isTest,
			     unsigned int debug)
{
  char name[100];
  unsigned int ndebug(0);
  
  char stest[80] = "test";
  if ( !isTest )
    sprintf(stest,"after %2d iterations", maxIter);
  char scorr[80] = "correction for PU";
  char sxlabel[80] ="(E^{cor}_{hcal} + E_{ecal})/p_{track}"; 
  if ( !APPLY_CORRECTION_FOR_PU ) {
    sprintf(scorr,"no correction for PU");
    sprintf(sxlabel,"(E_{hcal} + E_{ecal})/p_{track}");
  } 
    
  TF1* f1 = new TF1("f1","gaus", MIN_RESPONSE_HIST, MAX_RESPONSE_HIST);

  sprintf(name,"HB+HE: %s, %s", stest, scorr);
  e2p_last = new TH1F("e2p_last", name,
		      NBIN_RESPONSE_HIST, MIN_RESPONSE_HIST, MAX_RESPONSE_HIST);
  e2p_last->Sumw2();
  e2p_last->GetXaxis()->SetTitle(sxlabel);
  
  sprintf(name,"HB: %s, %s", stest, scorr);
  e2pHB_last = new TH1F("e2pHB_last", name,
			NBIN_RESPONSE_HIST/2, MIN_RESPONSE_HIST, MAX_RESPONSE_HIST);
  e2pHB_last->Sumw2();
  e2pHB_last->GetXaxis()->SetTitle(sxlabel);

  sprintf(name,"Initial TR: %s", scorr);
  e2pTR_last = new TH1F("e2pTR_last", name,
			NBIN_RESPONSE_HIST/10, MIN_RESPONSE_HIST, MAX_RESPONSE_HIST);
  e2pTR_last->Sumw2();
  e2pTR_last->GetXaxis()->SetTitle(sxlabel);
  
  sprintf(name,"HE: %s, %s", stest, scorr);
  e2pHE_last = new TH1F("e2pHE_last", name,
			NBIN_RESPONSE_HIST/2, MIN_RESPONSE_HIST, MAX_RESPONSE_HIST);
  e2pHE_last->Sumw2();
  e2pHE_last->GetXaxis()->SetTitle(sxlabel);
 
//--- initialize chain ----------------------------------------
  if (fChain == 0) return 0;
  Long64_t nentries = fChain->GetEntriesFast();
  Long64_t nb = 0;
  
  int nSelectedEvents(0);

  if ( debug > 0 ) { 
    std::cout << "------------- Last loop after " << maxIter << " iterations"
	      << std::endl;
  }
// ----------------------- loop over events -------------------------------------  
  for (Long64_t jentry=0; jentry<nentries; jentry++) {
    Long64_t ientry = LoadTree(jentry);
    if ( ientry < 0 || ndebug > debug ) break;   
    nb = fChain->GetEntry(jentry);   //nbytes += nb;
    
    if ( (jentry%2 == subsample) ) continue;   // only odd or even events

    // --------------- selection of good track --------------------
    
    if ( !goodTrack(t_ieta) ) continue;

    nSelectedEvents++;
    
    if ( debug > 1 ) {
      ndebug++;
      std::cout << "***Entry (Track) Number : " << ientry 
		<< " p/eHCal/eMipDR/nDets : " << t_p << "/" << t_eHcal 
		<< "/" << t_eMipDR << "/" << (*t_DetIds).size() 
		<< std::endl;
    }
    
    double eTotal(0.0);
    double eTotalWithEcal(0.0);
      
    // ---- loop over active cells in the event for total energy ---

    for (unsigned int idet = 0; idet < (*t_DetIds).size(); idet++) { 
      double hitEnergy(0);	
      unsigned int detId = ( (*t_DetIds)[idet] & MASK ) | MASK2 ;
	
      if (factors.find(detId) != factors.end()) 
	hitEnergy = factors[detId] * (*t_HitEnergies)[idet];
      else 
	hitEnergy = (*t_HitEnergies)[idet];

      eTotal += hitEnergy;
    }

    eTotalWithEcal = eTotal + t_eMipDR;    

// --- Correction for PU   --------      
    double eTotalCor(eTotal);
    double eTotalWithEcalCor(eTotalWithEcal);
    //double e10(0.0);
    //double e30(0.0);
    double correctionForPU(1.0);
    int abs_t_ieta = abs(t_ieta);

    if ( APPLY_CORRECTION_FOR_PU ) { 
      /* 
      for (unsigned int idet1 = 0; idet1 < (*t_DetIds1).size(); idet1++) { 
	double hitEnergy(0);
	unsigned int detId1 = ( (*t_DetIds1)[idet1] & MASK ) | MASK2;
	
	if (factors.find(detId1) != factors.end()) 
	  hitEnergy = factors[detId1] * (*t_HitEnergies1)[idet1];
	else 
	  hitEnergy = (*t_HitEnergies1)[idet1];
	
	e10 += hitEnergy;
      }
      for (unsigned int idet3 = 0; idet3 < (*t_DetIds3).size(); idet3++) { 
	double hitEnergy(0);
	unsigned int detId3 = ( (*t_DetIds3)[idet3] & MASK ) | MASK2;
	
	if (factors.find(detId3) != factors.end()) 
	  hitEnergy = factors[detId3] * (*t_HitEnergies3)[idet3];
	else
	  hitEnergy = (*t_HitEnergies3)[idet3];
	
	e30 += hitEnergy;
      }
      double de2p = (e30 - e10)/t_p;
      */
      
      double de2p = (t_eHcal30 - t_eHcal10)/t_p;
      if ( de2p > DELTA_CUT ) {
	int icor = int(abs_t_ieta >= FIRST_IETA_TR) + int(abs_t_ieta >= FIRST_IETA_HE)
	  + int(abs_t_ieta >= FIRST_IETA_FWD_1) + int(abs_t_ieta >= FIRST_IETA_FWD_2);
	correctionForPU = (1 + LINEAR_COR_COEF[icor]*(t_eHcal/t_p)*de2p
			   *(1 + SQUARE_COR_COEF[icor]*de2p));
      }
    }      
    // check for possibility to correct for PU
    if ( correctionForPU <= 0 || correctionForPU > 1 ) continue;

    eTotalCor = eTotal*correctionForPU;
    eTotalWithEcalCor = eTotalCor + t_eMipDR;
      
    e2p_last->Fill(eTotalWithEcalCor/t_p ,1.0);

    if ( abs_t_ieta < FIRST_IETA_TR )
      e2pHB_last->Fill(eTotalWithEcalCor/t_p ,1.0);
    else if ( abs_t_ieta < FIRST_IETA_HE )
      e2pTR_last->Fill(eTotalWithEcalCor/t_p ,1.0);
    else
      e2pHE_last->Fill(eTotalWithEcalCor/t_p ,1.0);
      
  } // ------------------- end of loop over events -------------------------------------

  if ( isTest ) {
    double fac[N_DEPTHS][MAXNUM_SUBDET]; 
    double dfac[N_DEPTHS][MAXNUM_SUBDET];
    double ieta[N_DEPTHS][MAXNUM_SUBDET];
    double dieta[N_DEPTHS][MAXNUM_SUBDET];
    unsigned int kdep[N_DEPTHS];
    for ( unsigned ik = 0; ik < N_DEPTHS; ik++ ) { kdep[ik] = 0; }
  
    std::map<unsigned int, double>::iterator factorsItr = factors.begin();
    for (factorsItr=factors.begin(); factorsItr != factors.end(); factorsItr++){

      unsigned int detId = factorsItr->first;
      int zside = (detId&ZSIDE_MASK) ? 1 : -1;
      int depth = ((detId>>DEPTH_OFFSET)&DEPTH_MASK) + int(MERGE_PHI_AND_DEPTHS);

      unsigned int kcur = kdep[depth-1];
      ieta[depth-1][kcur] = int((detId>>ETA_OFFSET) & ETA_MASK)*zside;
      dieta[depth-1][kcur] = 0;
      fac[depth-1][kcur] = factorsItr->second;
      dfac[depth-1][kcur] = 0;
      kdep[depth-1]++;
    }
    for ( unsigned ik = 0; ik < N_DEPTHS; ik++ ) {
      double x[MAXNUM_SUBDET], dx[MAXNUM_SUBDET], y[MAXNUM_SUBDET], dy[MAXNUM_SUBDET];
      for ( unsigned im = 0; im < MAXNUM_SUBDET; im++ ) {
	x[im] = ieta[ik][im];
	dx[im] = dieta[ik][im];
	y[im] = fac[ik][im];
	dy[im] = dfac[ik][im];
      }
      TGraphErrors*  g_fac = new TGraphErrors(kdep[ik], x, y, dx, dy);
      sprintf(name, "Applied correction factors, depth %1d", ik+1);
      g_fac->SetTitle(name);
      g_fac->GetXaxis()->SetTitle("i#eta");
      sprintf(name, "Cfacs_depth%1d", ik+1);
      foutRootFile->WriteTObject(g_fac, name);
    }
  }
  //--- fit response distributions ---------------------------------

  double xl = e2p_last->GetMean() - FIT_RMS_INTERVAL*e2p_last->GetRMS();
  double xr = e2p_last->GetMean() + FIT_RMS_INTERVAL*e2p_last->GetRMS();
  e2p_last->Fit("f1","QN", "R", xl, xr);
  xl = f1->GetParameter(1) - FIT_RMS_INTERVAL*f1->GetParameter(2);
  xr = f1->GetParameter(1) + FIT_RMS_INTERVAL*f1->GetParameter(2);
  e2p_last->Fit("f1","QN", "R", xl, xr);

  double fitMPV = f1->GetParameter(1);
  /*
  xl = e2pHB_last->GetMean() - FIT_RMS_INTERVAL*e2pHB_last->GetRMS();
  xr = e2pHB_last->GetMean() + FIT_RMS_INTERVAL*e2pHB_last->GetRMS();
  e2pHB_last->Fit("f1","QN", "R", xl, xr);

  xl = e2pHE_last->GetMean() - FIT_RMS_INTERVAL*e2pHE_last->GetRMS();
  xr = e2pHE_last->GetMean() + FIT_RMS_INTERVAL*e2pHE_last->GetRMS();
  e2pHE_last->Fit("f1","QN", "R", xl, xr);
  */
  return fitMPV;
}

//**********************************************************
// Isolated track selection
//**********************************************************
Bool_t CalibTree::goodTrack(int ieta) 
{

  double maxCharIso = limCharIso*exp(abs(ieta)*constForFlexSel);

  bool ok = (    (t_selectTk)
	      && (t_qltyMissFlag)
	      && (t_hmaxNearP < maxCharIso)
	      && (t_eMipDR < limMipEcal) 
	      && (t_p > minTrackMom) && (t_p < maxTrackMom)
	      && (t_pt >= minTrackPt)               // constraint on track pt
	      && (t_eHcal >= minEnrHcal)            // constraint on Hcal energy
	      && (t_eHcal/t_p < UPPER_LIMIT_RESPONSE_BEFORE_COR)
		 // reject events with too big cluster energy
		 //&& ((t_eHcal30 - t_eHcal10)/t_p < UPPER_LIMIT_DELTA_PU_COR)
		 // reject events with too high PU in the ring around cluster
	     );
  return ok;
}
//**********************************************************
// Save txt file with calculated factors
//**********************************************************
unsigned int CalibTree::saveFactorsInFile(std::string txtFileName)
{
  char sprnt[100];
  
  FILE* foutTxtFile = fopen(txtFileName.c_str(),"w+");
  fprintf(foutTxtFile,
	  "%1s%16s%16s%16s%9s%11s\n","#", "eta", "depth", "det", "value", "DetId");

  std::cout << "New factors:" << std::endl;
  std::map<unsigned int, double>::iterator factorsItr = factors.begin();
  unsigned int indx(0);
  unsigned int isave(0);
  
  for (factorsItr=factors.begin(); factorsItr != factors.end(); factorsItr++, indx++){
    unsigned int detId = factorsItr->first;
    int ieta = (detId>>ETA_OFFSET) & ETA_MASK;
    int zside= (detId&ZSIDE_MASK) ? 1 : -1;
    int depth= ((detId>>DEPTH_OFFSET)&DEPTH_MASK) + int(MERGE_PHI_AND_DEPTHS);

    double erWeight = 100*uncFromWeights[detId]/factorsItr->second;
    double erDev = 100*uncFromDeviation[detId]/factorsItr->second;
    double erTotal = 100*sqrt(pow(uncFromWeights[detId],2)
			  + pow(uncFromDeviation[detId],2))/factorsItr->second;
    
    if ( N_ETA_RINGS_PER_BIN < 2 ) { 
      sprintf(sprnt,
	      "DetId[%3d] %x (%3d,%1d)  %6.4f  : %6d  [%8.3f%% + %8.3f%% = %8.3f%%]",
	      indx, detId, ieta*zside, depth,
	      factorsItr->second, nTrks[detId],
	      erWeight, erDev, erTotal);
      std::cout << sprnt << std::endl;
    }
    else {
      int ieta_min = ieta - (N_ETA_RINGS_PER_BIN - 1);
      sprintf(sprnt,
	      "DetId[%3d] %x (%3d:%3d,%1d)  %6.4f  : %6d  [%8.3f%% + %8.3f%% = %8.3f%%]",
	      indx, detId, ieta_min*zside, ieta*zside, depth,
	      factorsItr->second, nTrks[detId],
	      erWeight, erDev, erTotal);
      std::cout << sprnt << std::endl;
    }
	    /*
    std::cout << "DetId[" << indx << "] " << std::hex  << (detId) << std::dec 
	      << "(" << ieta*zside << "," << depth << ") ( nTrks:" 
	      << nTrks[detId] << ") : " << factorsItr->second
	      << ""
	      << std::endl;
	    */
    
    const char* subDetector[2] = {"HB","HE"};
    if ( nTrks[detId] < MIN_N_TRACKS_PER_CELL ) continue;
    isave++;
    fprintf(foutTxtFile, "%17i%16i%16s%9.5f%11X\n", 
	    ieta*zside, depth, subDetector[subDetector_final[detId]-1],
	    factorsItr->second, detId);
  }
  fclose(foutTxtFile);
  foutTxtFile = NULL;
  return isave;
}
//**********************************************************
// Get factors from txt file
//**********************************************************
Bool_t CalibTree::getFactorsFromFile(std::string txtFileName,
				     unsigned int dbg)
{

  if ( !gSystem->Which("./", txtFileName.c_str() ) ) return false;

  FILE* finTxtFile = fopen(txtFileName.c_str(),"r");
  int flag;

  char header[80]; 
  for ( unsigned int i = 0; i < 6; i++ ) { 
    flag = fscanf(finTxtFile, "%7s", header);
  }

  int eta;
  int depth;
  char det[2]; 
  double cellFactor;
  unsigned int detId;
  unsigned int nReadFactors(0);
  
  while ( fscanf(finTxtFile, "%3d", &eta) != EOF )
    {
      flag = fscanf(finTxtFile, "%2d", &depth);
      flag = fscanf(finTxtFile, "%10s", det);
      flag = fscanf(finTxtFile, "%lf", &cellFactor);
      flag = fscanf(finTxtFile, "%x", &detId);
      factors.insert( std::pair<unsigned int, double>(detId, cellFactor) );
      nReadFactors++;
      if ( dbg > 0 ) 
	std::cout << "  " << std::dec << cellFactor
		  << "  " << std::hex << detId << std::endl; 
    }

  std::cout << std::dec << nReadFactors << " factors read from file "
	    << txtFileName
	    << std::endl;
  
  return true;
}
//**********************************************************