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
#include "DQMServices/Core/interface/QTest.h"
#include "DQMServices/Core/interface/DQMStore.h"
#include "Math/ProbFuncMathCore.h"
#include "TMath.h"
#include <cmath>
#include <iostream>
#include <sstream>

using namespace std;

const float QCriterion::ERROR_PROB_THRESHOLD = 0.50;
const float QCriterion::WARNING_PROB_THRESHOLD = 0.90;

// initialize values
void QCriterion::init() {
  errorProb_ = ERROR_PROB_THRESHOLD;
  warningProb_ = WARNING_PROB_THRESHOLD;
  setAlgoName("NO_ALGORITHM");
  status_ = dqm::qstatus::DID_NOT_RUN;
  message_ = "NO_MESSAGE";
  verbose_ = 0;  // 0 = silent, 1 = algorithmic failures, 2 = info
}

float QCriterion::runTest(const MonitorElement* /* me */) { return 0.; }
//===================================================//
//================ QUALITY TESTS ====================//
//==================================================//

//----------------------------------------------------//
//--------------- ContentsXRange ---------------------//
//----------------------------------------------------//
float ContentsXRange::runTest(const MonitorElement* me) {
  badChannels_.clear();

  if (!me)
    return -1;
  if (!me->getRootObject())
    return -1;
  TH1* h = nullptr;

  if (verbose_ > 1)
    std::cout << "QTest:" << getAlgoName() << "::runTest called on " << me->getFullname() << "\n";
  // -- TH1F
  if (me->kind() == MonitorElement::Kind::TH1F) {
    h = me->getTH1F();
  }
  // -- TH1S
  else if (me->kind() == MonitorElement::Kind::TH1S) {
    h = me->getTH1S();
  }
  // -- TH1D
  else if (me->kind() == MonitorElement::Kind::TH1D) {
    h = me->getTH1D();
  } else {
    if (verbose_ > 0)
      std::cout << "QTest:ContentsXRange"
                << " ME " << me->getFullname() << " does not contain TH1F/TH1S/TH1D, exiting\n";
    return -1;
  }

  if (!rangeInitialized_) {
    if (h->GetXaxis())
      setAllowedXRange(h->GetXaxis()->GetXmin(), h->GetXaxis()->GetXmax());
    else
      return -1;
  }
  int ncx = h->GetXaxis()->GetNbins();
  // use underflow bin
  int first = 0;  // 1
  // use overflow bin
  int last = ncx + 1;  // ncx
  // all entries
  double sum = 0;
  // entries outside X-range
  double fail = 0;
  int bin;
  for (bin = first; bin <= last; ++bin) {
    double contents = h->GetBinContent(bin);
    double x = h->GetBinCenter(bin);
    sum += contents;
    if (x < xmin_ || x > xmax_)
      fail += contents;
  }

  if (sum == 0)
    return 1;
  // return fraction of entries within allowed X-range
  return (sum - fail) / sum;
}

//-----------------------------------------------------//
//--------------- ContentsYRange ---------------------//
//----------------------------------------------------//
float ContentsYRange::runTest(const MonitorElement* me) {
  badChannels_.clear();

  if (!me)
    return -1;
  if (!me->getRootObject())
    return -1;
  TH1* h = nullptr;

  if (verbose_ > 1)
    std::cout << "QTest:" << getAlgoName() << "::runTest called on " << me->getFullname() << "\n";

  if (me->kind() == MonitorElement::Kind::TH1F) {
    h = me->getTH1F();  //access Test histo
  } else if (me->kind() == MonitorElement::Kind::TH1S) {
    h = me->getTH1S();  //access Test histo
  } else if (me->kind() == MonitorElement::Kind::TH1D) {
    h = me->getTH1D();  //access Test histo
  } else {
    if (verbose_ > 0)
      std::cout << "QTest:ContentsYRange"
                << " ME " << me->getFullname() << " does not contain TH1F/TH1S/TH1D, exiting\n";
    return -1;
  }

  if (!rangeInitialized_ || !h->GetXaxis())
    return 1;  // all bins are accepted if no initialization
  int ncx = h->GetXaxis()->GetNbins();
  // do NOT use underflow bin
  int first = 1;
  // do NOT use overflow bin
  int last = ncx;
  // bins outside Y-range
  int fail = 0;
  int bin;

  if (useEmptyBins_)  ///Standard test !
  {
    for (bin = first; bin <= last; ++bin) {
      double contents = h->GetBinContent(bin);
      bool failure = false;
      failure = (contents < ymin_ || contents > ymax_);  // allowed y-range: [ymin_, ymax_]
      if (failure) {
        DQMChannel chan(bin, 0, 0, contents, h->GetBinError(bin));
        badChannels_.push_back(chan);
        ++fail;
      }
    }
    // return fraction of bins that passed test
    return 1. * (ncx - fail) / ncx;
  } else  ///AS quality test !!!
  {
    for (bin = first; bin <= last; ++bin) {
      double contents = h->GetBinContent(bin);
      bool failure = false;
      if (contents)
        failure = (contents < ymin_ || contents > ymax_);  // allowed y-range: [ymin_, ymax_]
      if (failure)
        ++fail;
    }
    // return fraction of bins that passed test
    return 1. * (ncx - fail) / ncx;
  }  ///end of AS quality tests
}

//-----------------------------------------------------//
//------------------ DeadChannel ---------------------//
//----------------------------------------------------//
float DeadChannel::runTest(const MonitorElement* me) {
  badChannels_.clear();
  if (!me)
    return -1;
  if (!me->getRootObject())
    return -1;
  TH1* h1 = nullptr;
  TH2* h2 = nullptr;  //initialize histogram pointers

  if (verbose_ > 1)
    std::cout << "QTest:" << getAlgoName() << "::runTest called on " << me->getFullname() << "\n";
  //TH1F
  if (me->kind() == MonitorElement::Kind::TH1F) {
    h1 = me->getTH1F();  //access Test histo
  }
  //TH1S
  else if (me->kind() == MonitorElement::Kind::TH1S) {
    h1 = me->getTH1S();  //access Test histo
  }
  //TH1D
  else if (me->kind() == MonitorElement::Kind::TH1D) {
    h1 = me->getTH1D();  //access Test histo
  }
  //-- TH2F
  else if (me->kind() == MonitorElement::Kind::TH2F) {
    h2 = me->getTH2F();  // access Test histo
  }
  //-- TH2S
  else if (me->kind() == MonitorElement::Kind::TH2S) {
    h2 = me->getTH2S();  // access Test histo
  }
  //-- TH2D
  else if (me->kind() == MonitorElement::Kind::TH2D) {
    h2 = me->getTH2D();  // access Test histo
  } else {
    if (verbose_ > 0)
      std::cout << "QTest:DeadChannel"
                << " ME " << me->getFullname() << " does not contain TH1F/TH1S/TH1D/TH2F/TH2S/TH2D, exiting\n";
    return -1;
  }

  int fail = 0;  // number of failed channels

  //--------- do the quality test for 1D histo ---------------//
  if (h1 != nullptr) {
    if (!rangeInitialized_ || !h1->GetXaxis())
      return 1;  // all bins are accepted if no initialization
    int ncx = h1->GetXaxis()->GetNbins();
    int first = 1;
    int last = ncx;
    int bin;

    /// loop over all channels
    for (bin = first; bin <= last; ++bin) {
      double contents = h1->GetBinContent(bin);
      bool failure = false;
      failure = contents <= ymin_;  // dead channel: equal to or less than ymin_
      if (failure) {
        DQMChannel chan(bin, 0, 0, contents, h1->GetBinError(bin));
        badChannels_.push_back(chan);
        ++fail;
      }
    }
    //return fraction of alive channels
    return 1. * (ncx - fail) / ncx;
  }
  //----------------------------------------------------------//

  //--------- do the quality test for 2D -------------------//
  else if (h2 != nullptr) {
    int ncx = h2->GetXaxis()->GetNbins();  // get X bins
    int ncy = h2->GetYaxis()->GetNbins();  // get Y bins

    /// loop over all bins
    for (int cx = 1; cx <= ncx; ++cx) {
      for (int cy = 1; cy <= ncy; ++cy) {
        double contents = h2->GetBinContent(h2->GetBin(cx, cy));
        bool failure = false;
        failure = contents <= ymin_;  // dead channel: equal to or less than ymin_
        if (failure) {
          DQMChannel chan(cx, cy, 0, contents, h2->GetBinError(h2->GetBin(cx, cy)));
          badChannels_.push_back(chan);
          ++fail;
        }
      }
    }
    //return fraction of alive channels
    return 1. * (ncx * ncy - fail) / (ncx * ncy);
  } else {
    if (verbose_ > 0)
      std::cout << "QTest:DeadChannel"
                << " TH1/TH2F are NULL, exiting\n";
    return -1;
  }
}

//-----------------------------------------------------//
//----------------  NoisyChannel ---------------------//
//----------------------------------------------------//
// run the test (result: fraction of channels not appearing noisy or "hot")
// [0, 1] or <0 for failure
float NoisyChannel::runTest(const MonitorElement* me) {
  badChannels_.clear();
  if (!me)
    return -1;
  if (!me->getRootObject())
    return -1;
  TH1* h = nullptr;   //initialize histogram pointer
  TH2* h2 = nullptr;  //initialize histogram pointer

  if (verbose_ > 1)
    std::cout << "QTest:" << getAlgoName() << "::runTest called on " << me->getFullname() << "\n";

  int nbins = 0;
  int nbinsX = 0, nbinsY = 0;
  //-- TH1F
  if (me->kind() == MonitorElement::Kind::TH1F) {
    nbins = me->getTH1F()->GetXaxis()->GetNbins();
    h = me->getTH1F();  // access Test histo
  }
  //-- TH1S
  else if (me->kind() == MonitorElement::Kind::TH1S) {
    nbins = me->getTH1S()->GetXaxis()->GetNbins();
    h = me->getTH1S();  // access Test histo
  }
  //-- TH1D
  else if (me->kind() == MonitorElement::Kind::TH1D) {
    nbins = me->getTH1D()->GetXaxis()->GetNbins();
    h = me->getTH1D();  // access Test histo
  }
  //-- TH2
  else if (me->kind() == MonitorElement::Kind::TH2F) {
    nbinsX = me->getTH2F()->GetXaxis()->GetNbins();
    nbinsY = me->getTH2F()->GetYaxis()->GetNbins();
    h2 = me->getTH2F();  // access Test histo
  }
  //-- TH2
  else if (me->kind() == MonitorElement::Kind::TH2S) {
    nbinsX = me->getTH2S()->GetXaxis()->GetNbins();
    nbinsY = me->getTH2S()->GetYaxis()->GetNbins();
    h2 = me->getTH2S();  // access Test histo
  }
  //-- TH2
  else if (me->kind() == MonitorElement::Kind::TH2D) {
    nbinsX = me->getTH2F()->GetXaxis()->GetNbins();
    nbinsY = me->getTH2F()->GetYaxis()->GetNbins();
    h2 = me->getTH2D();  // access Test histo
  } else {
    if (verbose_ > 0)
      std::cout << "QTest:NoisyChannel"
                << " ME " << me->getFullname() << " does not contain TH1F/TH1S/TH1D or TH2F/TH2S/TH2D, exiting\n";
    return -1;
  }

  //--  QUALITY TEST itself

  // do NOT use underflow bin
  int first = 1;
  // do NOT use overflow bin
  int last = nbins;
  int lastX = nbinsX, lastY = nbinsY;
  // bins outside Y-range
  int fail = 0;
  int bin;
  int binX, binY;
  if (h != nullptr) {
    if (!rangeInitialized_ || !h->GetXaxis()) {
      return 1;  // all channels are accepted if tolerance has not been set
    }
    for (bin = first; bin <= last; ++bin) {
      double contents = h->GetBinContent(bin);
      double average = getAverage(bin, h);
      bool failure = false;
      if (average != 0)
        failure = (((contents - average) / std::abs(average)) > tolerance_);

      if (failure) {
        ++fail;
        DQMChannel chan(bin, 0, 0, contents, h->GetBinError(bin));
        badChannels_.push_back(chan);
      }
    }

    // return fraction of bins that passed test
    return 1. * (nbins - fail) / nbins;
  } else if (h2 != nullptr) {
    for (binY = first; binY <= lastY; ++binY) {
      for (binX = first; binX <= lastX; ++binX) {
        double contents = h2->GetBinContent(binX, binY);
        double average = getAverage2D(binX, binY, h2);
        bool failure = false;
        if (average != 0)
          failure = (((contents - average) / std::abs(average)) > tolerance_);
        if (failure) {
          ++fail;
          DQMChannel chan(binX, 0, 0, contents, h2->GetBinError(binX));
          badChannels_.push_back(chan);
        }
      }  //end x loop
    }  //end y loop
    // return fraction of bins that passed test
    return 1. * ((nbinsX * nbinsY) - fail) / (nbinsX * nbinsY);
  }  //end nullptr conditional
  else {
    if (verbose_ > 0)
      std::cout << "QTest:NoisyChannel"
                << " TH1/TH2F are NULL, exiting\n";
    return -1;
  }
}

// get average for bin under consideration
// (see description of method setNumNeighbors)
double NoisyChannel::getAverage(int bin, const TH1* h) const {
  int first = 1;                        // Do NOT use underflow bin
  int ncx = h->GetXaxis()->GetNbins();  // Do NOT use overflow bin
  double sum = 0;
  int bin_start, bin_end;
  int add_right = 0;
  int add_left = 0;

  bin_start = bin - numNeighbors_;  // First bin in integral
  bin_end = bin + numNeighbors_;    // Last bin in integral

  if (bin_start < first) {          // If neighbors take you outside of histogram range shift integral right
    add_right = first - bin_start;  // How much to shift remembering we are not using underflow
    bin_start = first;              // Remember to reset the starting bin
    bin_end += add_right;
    if (bin_end > ncx)
      bin_end = ncx;  // If the test would be larger than histogram just sum histogram without overflow
  }

  if (bin_end > ncx) {  // Now we make sure doesn't run off right edge of histogram
    add_left = bin_end - ncx;
    bin_end = ncx;
    bin_start -= add_left;
    if (bin_start < first)
      bin_start = first;  // If the test would be larger than histogram just sum histogram without underflow
  }

  sum += h->Integral(bin_start, bin_end);
  sum -= h->GetBinContent(bin);

  int dimension = 2 * numNeighbors_ + 1;
  if (dimension > h->GetNbinsX())
    dimension = h->GetNbinsX();

  return sum / (dimension - 1);
}

double NoisyChannel::getAverage2D(int binX, int binY, const TH2* h2) const {
  /// Do NOT use underflow or overflow bins
  int firstX = 1;
  int firstY = 1;
  double sum = 0;
  int ncx = h2->GetXaxis()->GetNbins();
  int ncy = h2->GetYaxis()->GetNbins();

  int neighborsX, neighborsY;  // Convert unsigned input to int so we can use comparators
  neighborsX = numNeighbors_;
  neighborsY = numNeighbors_;
  int bin_startX, bin_endX;
  int add_rightX = 0;  // Start shifts at 0
  int add_leftX = 0;
  int bin_startY, bin_endY;
  int add_topY = 0;
  int add_downY = 0;

  bin_startX = binX - neighborsX;  // First bin in X
  bin_endX = binX + neighborsX;    // Last bin in X

  if (bin_startX < firstX) {           // If neighbors take you outside of histogram range shift integral right
    add_rightX = firstX - bin_startX;  // How much to shift remembering we are no using underflow
    bin_startX = firstX;               // Remember to reset the starting bin
    bin_endX += add_rightX;
    if (bin_endX > ncx)
      bin_endX = ncx;
  }

  if (bin_endX > ncx) {  // Now we make sure doesn't run off right edge of histogram
    add_leftX = bin_endX - ncx;
    bin_endX = ncx;
    bin_startX -= add_leftX;
    if (bin_startX < firstX)
      bin_startX = firstX;  // If the test would be larger than histogram just sum histogram without underflow
  }

  bin_startY = binY - neighborsY;  // First bin in Y
  bin_endY = binY + neighborsY;    // Last bin in Y

  if (bin_startY < firstY) {         // If neighbors take you outside of histogram range shift integral up
    add_topY = firstY - bin_startY;  // How much to shift remembering we are no using underflow
    bin_startY = firstY;             // Remember to reset the starting bin
    bin_endY += add_topY;
    if (bin_endY > ncy)
      bin_endY = ncy;
  }

  if (bin_endY > ncy) {  // Now we make sure doesn't run off top edge of histogram
    add_downY = bin_endY - ncy;
    bin_endY = ncy;
    bin_startY -= add_downY;
    if (bin_startY < firstY)
      bin_startY = firstY;  // If the test would be larger than histogram just sum histogram without underflow
  }

  sum += h2->Integral(bin_startX, bin_endX, bin_startY, bin_endY);
  sum -= h2->GetBinContent(binX, binY);

  int dimensionX = 2 * neighborsX + 1;
  int dimensionY = 2 * neighborsY + 1;

  if (dimensionX > h2->GetNbinsX())
    dimensionX = h2->GetNbinsX();
  if (dimensionY > h2->GetNbinsY())
    dimensionY = h2->GetNbinsY();

  return sum / (dimensionX * dimensionY - 1);  // Average is sum over the # of bins used

}  // End getAverage2D

//-----------------------------------------------------//
//-----Content Sigma (Emma Yeager and Chad Freer)------//
//----------------------------------------------------//
// run the test (result: fraction of channels with sigma that is not noisy or hot)

float ContentSigma::runTest(const MonitorElement* me) {
  badChannels_.clear();
  if (!me)
    return -1;
  if (!me->getRootObject())
    return -1;
  TH1* h = nullptr;  //initialize histogram pointer

  if (verbose_ > 1)
    std::cout << "QTest:" << getAlgoName() << "::runTest called on " << me->getFullname() << "\n";

  unsigned nbinsX;
  unsigned nbinsY;

  //-- TH1F
  if (me->kind() == MonitorElement::Kind::TH1F) {
    nbinsX = me->getTH1F()->GetXaxis()->GetNbins();
    nbinsY = me->getTH1F()->GetYaxis()->GetNbins();
    h = me->getTH1F();  // access Test histo
  }
  //-- TH1S
  else if (me->kind() == MonitorElement::Kind::TH1S) {
    nbinsX = me->getTH1S()->GetXaxis()->GetNbins();
    nbinsY = me->getTH1S()->GetYaxis()->GetNbins();
    h = me->getTH1S();  // access Test histo
  }
  //-- TH1D
  else if (me->kind() == MonitorElement::Kind::TH1D) {
    nbinsX = me->getTH1D()->GetXaxis()->GetNbins();
    nbinsY = me->getTH1D()->GetYaxis()->GetNbins();
    h = me->getTH1D();  // access Test histo
  }
  //-- TH2
  else if (me->kind() == MonitorElement::Kind::TH2F) {
    nbinsX = me->getTH2F()->GetXaxis()->GetNbins();
    nbinsY = me->getTH2F()->GetYaxis()->GetNbins();
    h = me->getTH2F();  // access Test histo
  }
  //-- TH2
  else if (me->kind() == MonitorElement::Kind::TH2S) {
    nbinsX = me->getTH2S()->GetXaxis()->GetNbins();
    nbinsY = me->getTH2S()->GetYaxis()->GetNbins();
    h = me->getTH2S();  // access Test histo
  }
  //-- TH2
  else if (me->kind() == MonitorElement::Kind::TH2D) {
    nbinsX = me->getTH2D()->GetXaxis()->GetNbins();
    nbinsY = me->getTH2D()->GetYaxis()->GetNbins();
    h = me->getTH2D();  // access Test histo
  } else {
    if (verbose_ > 0)
      std::cout << "QTest:ContentSigma"
                << " ME " << me->getFullname() << " does not contain TH1F/TH1S/TH1D or TH2F/TH2S/TH2D, exiting\n";
    return -1;
  }

  //--  QUALITY TEST itself

  if (!rangeInitialized_ || !h->GetXaxis())
    return 1;  // all channels are accepted if tolerance has not been set

  int fail = 0;       // initialize bin failure count
  unsigned xMin = 1;  //initialize minimums and maximums with expected values
  unsigned yMin = 1;
  unsigned xMax = nbinsX;
  unsigned yMax = nbinsY;
  unsigned XBlocks = numXblocks_;  //Initialize xml inputs blocks and neighbors
  unsigned YBlocks = numYblocks_;
  unsigned neighborsX = numNeighborsX_;
  unsigned neighborsY = numNeighborsY_;
  unsigned Xbinnum = 1;
  unsigned Ybinnum = 1;
  unsigned XWidth = 0;
  unsigned YWidth = 0;

  if (neighborsX == 999) {
    neighborsX = 0;
  }
  if (neighborsY == 999) {
    neighborsY = 0;
  }

  //give users option for automatic mininum and maximum selection by inputting 0 to any of the parameters
  // check that user's parameters are completely in agreement with histogram
  // for instance, if inputted xMax is out of range xMin will automatically be ignored
  if (xMin_ != 0 && xMax_ != 0) {
    if ((xMax_ <= nbinsX) && (xMin_ <= xMax_)) {  // rescale area of histogram being analyzed
      nbinsX = xMax_ - xMin_ + 1;
      xMax = xMax_;  // do NOT use overflow bin
      xMin = xMin_;  // do NOT use underflow bin
    }
  }
  //give users option for automatic mininum and maximum selection by inputting 0 to any of the parameters
  if (yMin_ != 0 && yMax_ != 0) {
    if ((yMax_ <= nbinsY) && (yMin_ <= yMax_)) {
      nbinsY = yMax_ - yMin_ + 1;
      yMax = yMax_;
      yMin = yMin_;
    }
  }

  if (neighborsX * 2 >= nbinsX) {  //make sure neighbor check does not overlap with bin under consideration
    if (nbinsX % 2 == 0) {
      neighborsX = nbinsX / 2 - 1;  //set neighbors for no overlap
    } else {
      neighborsX = (nbinsX - 1) / 2;
    }
  }

  if (neighborsY * 2 >= nbinsY) {
    if (nbinsY % 2 == 0) {
      neighborsY = nbinsY / 2 - 1;
    } else {
      neighborsY = (nbinsY - 1) / 2;
    }
  }

  if (XBlocks == 999) {  //Setting 999 prevents blocks and does quality tests by bins only
    XBlocks = nbinsX;
  }
  if (YBlocks == 999) {
    YBlocks = nbinsY;
  }

  Xbinnum = nbinsX / XBlocks;
  Ybinnum = nbinsY / YBlocks;
  for (unsigned groupx = 0; groupx < XBlocks; ++groupx) {  //Test over all the blocks
    for (unsigned groupy = 0; groupy < YBlocks; ++groupy) {
      double blocksum = 0;
      for (unsigned binx = 0; binx < Xbinnum; ++binx) {  //Sum the contents of the block in question
        for (unsigned biny = 0; biny < Ybinnum; ++biny) {
          if (groupx * Xbinnum + xMin + binx <= xMax && groupy * Ybinnum + yMin + biny <= yMax) {
            blocksum += abs(h->GetBinContent(groupx * Xbinnum + xMin + binx, groupy * Ybinnum + yMin + biny));
          }
        }
      }

      double sum = getNeighborSum(groupx, groupy, XBlocks, YBlocks, neighborsX, neighborsY, h);
      sum -= blocksum;  //remove center block to test

      if (neighborsX > groupx) {  //Find correct average at the edges
        XWidth = neighborsX + groupx + 1;
      } else if (neighborsX > (XBlocks - (groupx + 1))) {
        XWidth = (XBlocks - groupx) + neighborsX;
      } else {
        XWidth = 2 * neighborsX + 1;
      }
      if (neighborsY > groupy) {
        YWidth = neighborsY + groupy + 1;
      } else if (neighborsY > (YBlocks - (groupy + 1))) {
        YWidth = (YBlocks - groupy) + neighborsY;
      } else {
        YWidth = 2 * neighborsY + 1;
      }

      double average = sum / (XWidth * YWidth - 1);
      double sigma = getNeighborSigma(average, groupx, groupy, XBlocks, YBlocks, neighborsX, neighborsY, h);
      //get rid of block being tested just like we did with the average
      sigma -= (average - blocksum) * (average - blocksum);
      double sigma_2 = sqrt(sigma) / sqrt(XWidth * YWidth - 2);  //N-1 where N=XWidth*YWidth - 1
      double sigma_real = sigma_2 / (XWidth * YWidth - 1);
      //double avg_uncrt = average*sqrt(sum)/sum;//Obsolete now(Chad Freer)
      double avg_uncrt = 3 * sigma_real;

      double probNoisy = ROOT::Math::poisson_cdf_c(blocksum - 1, average + avg_uncrt);
      double probDead = ROOT::Math::poisson_cdf(blocksum, average - avg_uncrt);
      double thresholdNoisy = ROOT::Math::normal_cdf_c(toleranceNoisy_);
      double thresholdDead = ROOT::Math::normal_cdf(-1 * toleranceDead_);

      int failureNoisy = 0;
      int failureDead = 0;

      if (average != 0) {
        if (noisy_ && dead_) {
          if (blocksum > average) {
            failureNoisy = probNoisy < thresholdNoisy;
          } else {
            failureDead = probDead < thresholdDead;
          }
        } else if (noisy_) {
          if (blocksum > average) {
            failureNoisy = probNoisy < thresholdNoisy;
          }
        } else if (dead_) {
          if (blocksum < average) {
            failureDead = probDead < thresholdDead;
          }
        } else {
          std::cout << "No test type selected!\n";
        }
        //Following lines useful for debugging using verbose (Chad Freer)
        //string histName = h->GetName();
        //if (histName == "emtfTrackBX") {
        //   std::printf("Chad says: %i XBlocks, %i XBlocks, %f Blocksum, %f Average", XBlocks,YBlocks,blocksum,average);}
      }

      if (failureNoisy || failureDead) {
        ++fail;
        //DQMChannel chan(groupx*Xbinnum+xMin+binx, 0, 0, blocksum, h->GetBinError(groupx*Xbinnum+xMin+binx));
        //badChannels_.push_back(chan);
      }
    }
  }
  return 1. * ((XBlocks * YBlocks) - fail) / (XBlocks * YBlocks);
}

//Gets the sum of the bins surrounding the block to be tested (Chad Freer)
double ContentSigma::getNeighborSum(unsigned groupx,
                                    unsigned groupy,
                                    unsigned Xblocks,
                                    unsigned Yblocks,
                                    unsigned neighborsX,
                                    unsigned neighborsY,
                                    const TH1* h) const {
  double sum = 0;
  unsigned nbinsX = h->GetXaxis()->GetNbins();
  unsigned nbinsY = h->GetYaxis()->GetNbins();
  unsigned xMin = 1;
  unsigned yMin = 1;
  unsigned xMax = nbinsX;
  unsigned yMax = nbinsY;
  unsigned Xbinnum = 1;
  unsigned Ybinnum = 1;

  //give users option for automatic mininum and maximum selection by inputting 0 to any of the parameters
  // check that user's parameters are completely in agreement with histogram
  // for instance, if inputted xMax is out of range xMin will automatically be ignored
  if (xMin_ != 0 && xMax_ != 0) {
    if ((xMax_ <= nbinsX) && (xMin_ <= xMax_)) {
      nbinsX = xMax_ - xMin_ + 1;
      xMax = xMax_;  // do NOT use overflow bin
      xMin = xMin_;  // do NOT use underflow bin
    }
  }
  if (yMin_ != 0 && yMax_ != 0) {
    if ((yMax_ <= nbinsY) && (yMin_ <= yMax_)) {
      nbinsY = yMax_ - yMin_ + 1;
      yMax = yMax_;
      yMin = yMin_;
    }
  }

  if (Xblocks == 999) {  //Check to see if blocks should be ignored
    Xblocks = nbinsX;
  }
  if (Yblocks == 999) {
    Yblocks = nbinsY;
  }

  Xbinnum = nbinsX / Xblocks;
  Ybinnum = nbinsY / Yblocks;

  unsigned xLow, xHi, yLow, yHi;  //Define the neighbor blocks edges to be summed
  if (groupx > neighborsX) {
    xLow = (groupx - neighborsX) * Xbinnum + xMin;
    if (xLow < xMin) {
      xLow = xMin;  //If the neigbor block would go outside the histogram edge, set it the edge
    }
  } else {
    xLow = xMin;
  }
  xHi = (groupx + 1 + neighborsX) * Xbinnum + xMin;
  if (xHi > xMax) {
    xHi = xMax;
  }
  if (groupy > neighborsY) {
    yLow = (groupy - neighborsY) * Ybinnum + yMin;
    if (yLow < yMin) {
      yLow = yMin;
    }
  } else {
    yLow = yMin;
  }
  yHi = (groupy + 1 + neighborsY) * Ybinnum + yMin;
  if (yHi > yMax) {
    yHi = yMax;
  }

  for (unsigned xbin = xLow; xbin <= xHi; ++xbin) {  //now sum over all the bins
    for (unsigned ybin = yLow; ybin <= yHi; ++ybin) {
      sum += h->GetBinContent(xbin, ybin);
    }
  }
  return sum;
}

//Similar to algorithm  above but returns a version of standard deviation. Additional operations to return real standard deviation used above (Chad Freer)
double ContentSigma::getNeighborSigma(double average,
                                      unsigned groupx,
                                      unsigned groupy,
                                      unsigned Xblocks,
                                      unsigned Yblocks,
                                      unsigned neighborsX,
                                      unsigned neighborsY,
                                      const TH1* h) const {
  double sigma = 0;
  unsigned nbinsX = h->GetXaxis()->GetNbins();
  unsigned nbinsY = h->GetYaxis()->GetNbins();
  unsigned xMin = 1;
  unsigned yMin = 1;
  unsigned xMax = nbinsX;
  unsigned yMax = nbinsY;
  unsigned Xbinnum = 1;
  unsigned Ybinnum = 1;
  double block_sum;

  if (xMin_ != 0 && xMax_ != 0) {
    if ((xMax_ <= nbinsX) && (xMin_ <= xMax_)) {
      nbinsX = xMax_ - xMin_ + 1;
      xMax = xMax_;
      xMin = xMin_;
    }
  }
  if (yMin_ != 0 && yMax_ != 0) {
    if ((yMax_ <= nbinsY) && (yMin_ <= yMax_)) {
      nbinsY = yMax_ - yMin_ + 1;
      yMax = yMax_;
      yMin = yMin_;
    }
  }
  if (Xblocks == 999) {
    Xblocks = nbinsX;
  }
  if (Yblocks == 999) {
    Yblocks = nbinsY;
  }

  Xbinnum = nbinsX / Xblocks;
  Ybinnum = nbinsY / Yblocks;

  unsigned xLow, xHi, yLow, yHi;
  for (unsigned x_block_count = 0; x_block_count <= 2 * neighborsX; ++x_block_count) {
    for (unsigned y_block_count = 0; y_block_count <= 2 * neighborsY; ++y_block_count) {
      //Sum over blocks. Need to find standard deviation of average of blocksums. Set up low and hi values similar to sum but for blocks now.
      if (groupx + x_block_count > neighborsX) {
        xLow = (groupx + x_block_count - neighborsX) * Xbinnum + xMin;
        if (xLow < xMin) {
          xLow = xMin;
        }
      } else {
        xLow = xMin;
      }
      xHi = xLow + Xbinnum;
      if (xHi > xMax) {
        xHi = xMax;
      }
      if (groupy + y_block_count > neighborsY) {
        yLow = (groupy + y_block_count - neighborsY) * Ybinnum + yMin;
        if (yLow < yMin) {
          yLow = yMin;
        }
      } else {
        yLow = yMin;
      }
      yHi = yLow + Ybinnum;
      if (yHi > yMax) {
        yHi = yMax;
      }
      block_sum = 0;
      for (unsigned x_block_bin = xLow; x_block_bin <= xHi; ++x_block_bin) {
        for (unsigned y_block_bin = yLow; y_block_bin <= yHi; ++y_block_bin) {
          block_sum += h->GetBinContent(x_block_bin, y_block_bin);
        }
      }
      sigma += (average - block_sum) * (average - block_sum);  //will sqrt and divide by sqrt(N-1) outside of function
    }
  }
  return sigma;
}

//-----------------------------------------------------------//
//----------------  ContentsWithinExpected ---------------------//
//-----------------------------------------------------------//
// run the test (result: fraction of channels that passed test);
// [0, 1] or <0 for failure
float ContentsWithinExpected::runTest(const MonitorElement* me) {
  badChannels_.clear();
  if (!me)
    return -1;
  if (!me->getRootObject())
    return -1;
  TH1* h = nullptr;  //initialize histogram pointer

  if (verbose_ > 1)
    std::cout << "QTest:" << getAlgoName() << "::runTest called on " << me->getFullname() << "\n";

  int ncx;
  int ncy;

  if (useEmptyBins_) {
    //-- TH2
    if (me->kind() == MonitorElement::Kind::TH2F) {
      ncx = me->getTH2F()->GetXaxis()->GetNbins();
      ncy = me->getTH2F()->GetYaxis()->GetNbins();
      h = me->getTH2F();  // access Test histo
    }
    //-- TH2S
    else if (me->kind() == MonitorElement::Kind::TH2S) {
      ncx = me->getTH2S()->GetXaxis()->GetNbins();
      ncy = me->getTH2S()->GetYaxis()->GetNbins();
      h = me->getTH2S();  // access Test histo
    }
    //-- TH2D
    else if (me->kind() == MonitorElement::Kind::TH2D) {
      ncx = me->getTH2D()->GetXaxis()->GetNbins();
      ncy = me->getTH2D()->GetYaxis()->GetNbins();
      h = me->getTH2D();  // access Test histo
    }
    //-- TProfile
    else if (me->kind() == MonitorElement::Kind::TPROFILE) {
      ncx = me->getTProfile()->GetXaxis()->GetNbins();
      ncy = 1;
      h = me->getTProfile();  // access Test histo
    }
    //-- TProfile2D
    else if (me->kind() == MonitorElement::Kind::TPROFILE2D) {
      ncx = me->getTProfile2D()->GetXaxis()->GetNbins();
      ncy = me->getTProfile2D()->GetYaxis()->GetNbins();
      h = me->getTProfile2D();  // access Test histo
    } else {
      if (verbose_ > 0)
        std::cout << "QTest:ContentsWithinExpected"
                  << " ME does not contain TH2F/TH2S/TH2D/TPROFILE/TPROFILE2D, exiting\n";
      return -1;
    }

    int nsum = 0;
    double sum = 0.0;
    double average = 0.0;

    if (checkMeanTolerance_) {  // calculate average value of all bin contents

      for (int cx = 1; cx <= ncx; ++cx) {
        for (int cy = 1; cy <= ncy; ++cy) {
          if (me->kind() == MonitorElement::Kind::TH2F) {
            sum += h->GetBinContent(h->GetBin(cx, cy));
            ++nsum;
          } else if (me->kind() == MonitorElement::Kind::TH2S) {
            sum += h->GetBinContent(h->GetBin(cx, cy));
            ++nsum;
          } else if (me->kind() == MonitorElement::Kind::TH2D) {
            sum += h->GetBinContent(h->GetBin(cx, cy));
            ++nsum;
          } else if (me->kind() == MonitorElement::Kind::TPROFILE) {
            if (me->getTProfile()->GetBinEntries(h->GetBin(cx)) >= minEntries_ / (ncx)) {
              sum += h->GetBinContent(h->GetBin(cx));
              ++nsum;
            }
          } else if (me->kind() == MonitorElement::Kind::TPROFILE2D) {
            if (me->getTProfile2D()->GetBinEntries(h->GetBin(cx, cy)) >= minEntries_ / (ncx * ncy)) {
              sum += h->GetBinContent(h->GetBin(cx, cy));
              ++nsum;
            }
          }
        }
      }

      if (nsum > 0)
        average = sum / nsum;

    }  // calculate average value of all bin contents

    int fail = 0;

    for (int cx = 1; cx <= ncx; ++cx) {
      for (int cy = 1; cy <= ncy; ++cy) {
        bool failMean = false;
        bool failRMS = false;
        bool failMeanTolerance = false;

        if (me->kind() == MonitorElement::Kind::TPROFILE &&
            me->getTProfile()->GetBinEntries(h->GetBin(cx)) < minEntries_ / (ncx))
          continue;

        if (me->kind() == MonitorElement::Kind::TPROFILE2D &&
            me->getTProfile2D()->GetBinEntries(h->GetBin(cx, cy)) < minEntries_ / (ncx * ncy))
          continue;

        if (checkMean_) {
          double mean = h->GetBinContent(h->GetBin(cx, cy));
          failMean = (mean < minMean_ || mean > maxMean_);
        }

        if (checkRMS_) {
          double rms = h->GetBinError(h->GetBin(cx, cy));
          failRMS = (rms < minRMS_ || rms > maxRMS_);
        }

        if (checkMeanTolerance_) {
          double mean = h->GetBinContent(h->GetBin(cx, cy));
          failMeanTolerance = (std::abs(mean - average) > toleranceMean_ * std::abs(average));
        }

        if (failMean || failRMS || failMeanTolerance) {
          if (me->kind() == MonitorElement::Kind::TH2F) {
            DQMChannel chan(cx, cy, 0, h->GetBinContent(h->GetBin(cx, cy)), h->GetBinError(h->GetBin(cx, cy)));
            badChannels_.push_back(chan);
          } else if (me->kind() == MonitorElement::Kind::TH2S) {
            DQMChannel chan(cx, cy, 0, h->GetBinContent(h->GetBin(cx, cy)), h->GetBinError(h->GetBin(cx, cy)));
            badChannels_.push_back(chan);
          } else if (me->kind() == MonitorElement::Kind::TH2D) {
            DQMChannel chan(cx, cy, 0, h->GetBinContent(h->GetBin(cx, cy)), h->GetBinError(h->GetBin(cx, cy)));
            badChannels_.push_back(chan);
          } else if (me->kind() == MonitorElement::Kind::TPROFILE) {
            DQMChannel chan(
                cx, cy, int(me->getTProfile()->GetBinEntries(h->GetBin(cx))), 0, h->GetBinError(h->GetBin(cx)));
            badChannels_.push_back(chan);
          } else if (me->kind() == MonitorElement::Kind::TPROFILE2D) {
            DQMChannel chan(cx,
                            cy,
                            int(me->getTProfile2D()->GetBinEntries(h->GetBin(cx, cy))),
                            h->GetBinContent(h->GetBin(cx, cy)),
                            h->GetBinError(h->GetBin(cx, cy)));
            badChannels_.push_back(chan);
          }
          ++fail;
        }
      }
    }
    return 1. * (ncx * ncy - fail) / (ncx * ncy);
  }  /// end of normal Test

  else  /// AS quality test !!!
  {
    if (me->kind() == MonitorElement::Kind::TH2F) {
      ncx = me->getTH2F()->GetXaxis()->GetNbins();
      ncy = me->getTH2F()->GetYaxis()->GetNbins();
      h = me->getTH2F();  // access Test histo
    } else if (me->kind() == MonitorElement::Kind::TH2S) {
      ncx = me->getTH2S()->GetXaxis()->GetNbins();
      ncy = me->getTH2S()->GetYaxis()->GetNbins();
      h = me->getTH2S();  // access Test histo
    } else if (me->kind() == MonitorElement::Kind::TH2D) {
      ncx = me->getTH2D()->GetXaxis()->GetNbins();
      ncy = me->getTH2D()->GetYaxis()->GetNbins();
      h = me->getTH2D();  // access Test histo
    } else {
      if (verbose_ > 0)
        std::cout << "QTest:ContentsWithinExpected AS"
                  << " ME does not contain TH2F/TH2S/TH2D, exiting\n";
      return -1;
    }

    // if (!rangeInitialized_) return 0; // all accepted if no initialization
    int fail = 0;
    for (int cx = 1; cx <= ncx; ++cx) {
      for (int cy = 1; cy <= ncy; ++cy) {
        bool failure = false;
        double Content = h->GetBinContent(h->GetBin(cx, cy));
        if (Content)
          failure = (Content < minMean_ || Content > maxMean_);
        if (failure)
          ++fail;
      }
    }
    return 1. * (ncx * ncy - fail) / (ncx * ncy);
  }  /// end of AS quality test
}
/// set expected value for mean
void ContentsWithinExpected::setMeanRange(double xmin, double xmax) {
  if (xmax < xmin)
    if (verbose_ > 0)
      std::cout << "QTest:ContentsWitinExpected"
                << " Illogical range: (" << xmin << ", " << xmax << "\n";
  minMean_ = xmin;
  maxMean_ = xmax;
  checkMean_ = true;
}

/// set expected value for mean
void ContentsWithinExpected::setRMSRange(double xmin, double xmax) {
  if (xmax < xmin)
    if (verbose_ > 0)
      std::cout << "QTest:ContentsWitinExpected"
                << " Illogical range: (" << xmin << ", " << xmax << "\n";
  minRMS_ = xmin;
  maxRMS_ = xmax;
  checkRMS_ = true;
}

//----------------------------------------------------------------//
//--------------------  MeanWithinExpected  ---------------------//
//---------------------------------------------------------------//
// run the test;
//   (a) if useRange is called: 1 if mean within allowed range, 0 otherwise
//   (b) is useRMS or useSigma is called: result is the probability
//   Prob(chi^2, ndof=1) that the mean of histogram will be deviated by more than
//   +/- delta from <expected_mean>, where delta = mean - <expected_mean>, and
//   chi^2 = (delta/sigma)^2. sigma is the RMS of the histogram ("useRMS") or
//   <expected_sigma> ("useSigma")
//   e.g. for delta = 1, Prob = 31.7%
//  for delta = 2, Prob = 4.55%
//   (returns result in [0, 1] or <0 for failure)
float MeanWithinExpected::runTest(const MonitorElement* me) {
  if (!me)
    return -1;
  if (!me->getRootObject())
    return -1;
  TH1* h = nullptr;

  if (verbose_ > 1)
    std::cout << "QTest:" << getAlgoName() << "::runTest called on " << me->getFullname() << "\n";

  if (minEntries_ != 0 && me->getEntries() < minEntries_)
    return -1;

  if (me->kind() == MonitorElement::Kind::TH1F) {
    h = me->getTH1F();  //access Test histo
  } else if (me->kind() == MonitorElement::Kind::TH1S) {
    h = me->getTH1S();  //access Test histo
  } else if (me->kind() == MonitorElement::Kind::TH1D) {
    h = me->getTH1D();  //access Test histo
  } else {
    if (verbose_ > 0)
      std::cout << "QTest:MeanWithinExpected"
                << " ME " << me->getFullname() << " does not contain TH1F/TH1S/TH1D, exiting\n";
    return -1;
  }

  if (useRange_) {
    double mean = h->GetMean();
    if (mean <= xmax_ && mean >= xmin_)
      return 1;
    else
      return 0;
  } else if (useSigma_) {
    if (sigma_ != 0.) {
      double chi = (h->GetMean() - expMean_) / sigma_;
      return TMath::Prob(chi * chi, 1);
    } else {
      if (verbose_ > 0)
        std::cout << "QTest:MeanWithinExpected"
                  << " Error, sigma_ is zero, exiting\n";
      return 0;
    }
  } else if (useRMS_) {
    if (h->GetRMS() != 0.) {
      double chi = (h->GetMean() - expMean_) / h->GetRMS();
      return TMath::Prob(chi * chi, 1);
    } else {
      if (verbose_ > 0)
        std::cout << "QTest:MeanWithinExpected"
                  << " Error, RMS is zero, exiting\n";
      return 0;
    }
  } else {
    if (verbose_ > 0)
      std::cout << "QTest:MeanWithinExpected"
                << " Error, neither Range, nor Sigma, nor RMS, exiting\n";
    return -1;
  }
}

void MeanWithinExpected::useRange(double xmin, double xmax) {
  useRange_ = true;
  useSigma_ = useRMS_ = false;
  xmin_ = xmin;
  xmax_ = xmax;
  if (xmin_ > xmax_)
    if (verbose_ > 0)
      std::cout << "QTest:MeanWithinExpected"
                << " Illogical range: (" << xmin_ << ", " << xmax_ << "\n";
}
void MeanWithinExpected::useSigma(double expectedSigma) {
  useSigma_ = true;
  useRMS_ = useRange_ = false;
  sigma_ = expectedSigma;
  if (sigma_ == 0)
    if (verbose_ > 0)
      std::cout << "QTest:MeanWithinExpected"
                << " Expected sigma = " << sigma_ << "\n";
}

void MeanWithinExpected::useRMS() {
  useRMS_ = true;
  useSigma_ = useRange_ = false;
}

//----------------------------------------------------------------//
//------------------------  CompareToMedian  ---------------------------//
//----------------------------------------------------------------//
/* 
Test for TProfile2D
For each x bin, the median value is calculated and then each value is compared with the median.
This procedure is repeated for each x-bin of the 2D profile
The parameters used for this comparison are:
MinRel and MaxRel to identify outliers wrt the median value
An absolute value (MinAbs, MaxAbs) on the median is used to identify a full region out of specification 
*/
float CompareToMedian::runTest(const MonitorElement* me) {
  int32_t nbins = 0, failed = 0;
  badChannels_.clear();

  if (!me)
    return -1;
  if (!me->getRootObject())
    return -1;
  TH1* h = nullptr;

  if (verbose_ > 1) {
    std::cout << "QTest:" << getAlgoName() << "::runTest called on " << me->getFullname() << "\n";
    std::cout << "\tMin = " << _min << "; Max = " << _max << "\n";
    std::cout << "\tMinMedian = " << _minMed << "; MaxMedian = " << _maxMed << "\n";
    std::cout << "\tUseEmptyBins = " << (_emptyBins ? "Yes" : "No") << "\n";
  }

  if (me->kind() == MonitorElement::Kind::TPROFILE2D) {
    h = me->getTProfile2D();  // access Test histo
  } else {
    if (verbose_ > 0)
      std::cout << "QTest:ContentsWithinExpected"
                << " ME does not contain TPROFILE2D, exiting\n";
    return -1;
  }

  nBinsX = h->GetNbinsX();
  nBinsY = h->GetNbinsY();
  int entries = 0;
  float median = 0.0;

  //Median calculated with partially sorted vector
  for (int binX = 1; binX <= nBinsX; binX++) {
    reset();
    // Fill vector
    for (int binY = 1; binY <= nBinsY; binY++) {
      int bin = h->GetBin(binX, binY);
      auto content = (double)h->GetBinContent(bin);
      if (content == 0 && !_emptyBins)
        continue;
      binValues.push_back(content);
      entries = me->getTProfile2D()->GetBinEntries(bin);
    }
    if (binValues.empty())
      continue;
    nbins += binValues.size();

    //calculate median
    if (!binValues.empty()) {
      int medPos = (int)binValues.size() / 2;
      nth_element(binValues.begin(), binValues.begin() + medPos, binValues.end());
      median = *(binValues.begin() + medPos);
    }

    // if median == 0, use the absolute cut
    if (median == 0) {
      if (verbose_ > 0) {
        std::cout << "QTest: Median is 0; the fixed cuts: [" << _minMed << "; " << _maxMed << "]  are used\n";
      }
      for (int binY = 1; binY <= nBinsY; binY++) {
        int bin = h->GetBin(binX, binY);
        auto content = (double)h->GetBinContent(bin);
        entries = me->getTProfile2D()->GetBinEntries(bin);
        if (entries == 0)
          continue;
        if (content > _maxMed || content < _minMed) {
          DQMChannel chan(binX, binY, 0, content, h->GetBinError(bin));
          badChannels_.push_back(chan);
          failed++;
        }
      }
      continue;
    }

    //Cut on stat: will mask rings with no enought of statistics
    if (median * entries < _statCut)
      continue;

    // If median is off the absolute cuts, declare everything bad (if bin has non zero entries)
    if (median > _maxMed || median < _minMed) {
      for (int binY = 1; binY <= nBinsY; binY++) {
        int bin = h->GetBin(binX, binY);
        auto content = (double)h->GetBinContent(bin);
        entries = me->getTProfile2D()->GetBinEntries(bin);
        if (entries == 0)
          continue;
        DQMChannel chan(binX, binY, 0, content / median, h->GetBinError(bin));
        badChannels_.push_back(chan);
        failed++;
      }
      continue;
    }

    // Test itself
    float minCut = median * _min;
    float maxCut = median * _max;
    for (int binY = 1; binY <= nBinsY; binY++) {
      int bin = h->GetBin(binX, binY);
      auto content = (double)h->GetBinContent(bin);
      entries = me->getTProfile2D()->GetBinEntries(bin);
      if (entries == 0)
        continue;
      if (content > maxCut || content < minCut) {
        DQMChannel chan(binX, binY, 0, content / median, h->GetBinError(bin));
        badChannels_.push_back(chan);
        failed++;
      }
    }
  }

  if (nbins == 0) {
    if (verbose_ > 0)
      std::cout << "QTest:CompareToMedian: Histogram is empty" << std::endl;
    return 1.;
  }
  return 1 - (float)failed / nbins;
}
//----------------------------------------------------------------//
//------------------------  CompareLastFilledBin -----------------//
//----------------------------------------------------------------//
/* 
Test for TH1F and TH2F
For the last filled bin the value is compared with specified upper and lower limits. If 
it is outside the limit the test failed test result is returned
The parameters used for this comparison are:
MinRel and MaxRel to check identify outliers wrt the median value
*/
float CompareLastFilledBin::runTest(const MonitorElement* me) {
  if (!me)
    return -1;
  if (!me->getRootObject())
    return -1;
  TH1* h1 = nullptr;
  TH2* h2 = nullptr;
  if (verbose_ > 1) {
    std::cout << "QTest:" << getAlgoName() << "::runTest called on " << me->getFullname() << "\n";
    std::cout << "\tMin = " << _min << "; Max = " << _max << "\n";
  }
  if (me->kind() == MonitorElement::Kind::TH1F) {
    h1 = me->getTH1F();  // access Test histo
  } else if (me->kind() == MonitorElement::Kind::TH2F) {
    h2 = me->getTH2F();  // access Test histo
  } else {
    if (verbose_ > 0)
      std::cout << "QTest:ContentsWithinExpected"
                << " ME does not contain TH1F or TH2F, exiting\n";
    return -1;
  }
  int lastBinX = 0;
  int lastBinY = 0;
  float lastBinVal;

  //--------- do the quality test for 1D histo ---------------//
  if (h1 != nullptr) {
    lastBinX = h1->FindLastBinAbove(_average, 1);
    lastBinVal = h1->GetBinContent(lastBinX);
    if (h1->GetEntries() == 0 || lastBinVal < 0)
      return 1;
  } else if (h2 != nullptr) {
    lastBinX = h2->FindLastBinAbove(_average, 1);
    lastBinY = h2->FindLastBinAbove(_average, 2);
    if (h2->GetEntries() == 0 || lastBinX < 0 || lastBinY < 0)
      return 1;
    lastBinVal = h2->GetBinContent(h2->GetBin(lastBinX, lastBinY));
  } else {
    if (verbose_ > 0)
      std::cout << "QTest:" << getAlgoName() << " Histogram does not exist" << std::endl;
    return 1;
  }
  if (verbose_ > 0)
    std::cout << "Min and Max values " << _min << " " << _max << " Av value " << _average << " lastBinX " << lastBinX
              << " lastBinY " << lastBinY << " lastBinVal " << lastBinVal << std::endl;
  if (lastBinVal > _min && lastBinVal <= _max)
    return 1;
  else
    return 0;
}
//----------------------------------------------------//
//--------------- CheckVariance ---------------------//
//----------------------------------------------------//
float CheckVariance::runTest(const MonitorElement* me) {
  badChannels_.clear();

  if (!me)
    return -1;
  if (!me->getRootObject())
    return -1;
  TH1* h = nullptr;

  if (verbose_ > 1)
    std::cout << "QTest:" << getAlgoName() << "::runTest called on " << me->getFullname() << "\n";
  // -- TH1F
  if (me->kind() == MonitorElement::Kind::TH1F) {
    h = me->getTH1F();
  }
  // -- TH1D
  else if (me->kind() == MonitorElement::Kind::TH1D) {
    h = me->getTH1D();
  } else if (me->kind() == MonitorElement::Kind::TPROFILE) {
    h = me->getTProfile();  // access Test histo
  } else {
    if (verbose_ > 0)
      std::cout << "QTest:CheckVariance"
                << " ME " << me->getFullname() << " does not contain TH1F/TH1D/TPROFILE, exiting\n";
    return -1;
  }

  int ncx = h->GetXaxis()->GetNbins();

  double sum = 0;
  double sum2 = 0;
  for (int bin = 1; bin <= ncx; ++bin) {
    double contents = h->GetBinContent(bin);
    sum += contents;
  }
  if (sum == 0)
    return -1;
  double avg = sum / ncx;

  for (int bin = 1; bin <= ncx; ++bin) {
    double contents = h->GetBinContent(bin);
    sum2 += (contents - avg) * (contents - avg);
  }

  double Variance = TMath::Sqrt(sum2 / ncx);
  return Variance;
}