HiggsTemplateCrossSections

Macros

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
#ifndef TRUTHRIVETTOOLS_HIGGSTEMPLATECROSSSECTIONS_CC
#define TRUTHRIVETTOOLS_HIGGSTEMPLATECROSSSECTIONS_CC

// -*- C++ -*-
#include "Rivet/Analysis.hh"
#include "Rivet/Particle.hh"
#include "Rivet/Projections/FastJets.hh"

// Definition of the StatusCode and Category enums
//#include "HiggsTemplateCrossSections.h"
#include "SimDataFormats/HTXS/interface/HiggsTemplateCrossSections.h"  //

#include <atomic>

namespace Rivet {

  /// @class HiggsTemplateCrossSections
  /// @brief  Rivet routine for classifying MC events according to the Higgs template cross section categories
  /// @author Jim Lacey (DESY) <james.lacey@cern.ch,jlacey@desy.de>
  /// @author Dag Gillberg (Carleton University) <dag.gillberg@cern.ch>
  class HiggsTemplateCrossSections : public Analysis {
  public:
    // Constructor
    HiggsTemplateCrossSections() : Analysis("HiggsTemplateCrossSections"), m_HiggsProdMode(HTXS::UNKNOWN) {}

  public:
    /// @name Utility methods
    /// Methods to identify the Higgs boson and
    /// associated vector boson and to build jets
    /// @{

    /// follow a "propagating" particle and return its last instance
    Particle getLastInstance(Particle ptcl) {
      if (ptcl.genParticle()->end_vertex()) {
        if (!hasChild(ptcl.genParticle(), ptcl.pid()))
          return ptcl;
        else
          return getLastInstance(ptcl.children()[0]);
      }
      return ptcl;
    }

    /// @brief Whether particle p originate from any of the ptcls
    bool originateFrom(const Particle &p, const Particles &ptcls) {
      const ConstGenVertexPtr prodVtx = p.genParticle()->production_vertex();
      if (prodVtx == nullptr)
        return false;
      // for each ancestor, check if it matches any of the input particles
      for (const auto &ancestor : HepMCUtils::particles(prodVtx, Relatives::ANCESTORS)) {
        for (const auto &part : ptcls)
          if (ancestor == part.genParticle())
            return true;
      }
      // if we get here, no ancetor matched any input particle
      return false;
    }

    /// @brief Whether particle p originates from p2
    bool originateFrom(const Particle &p, const Particle &p2) {
      Particles ptcls = {p2};
      return originateFrom(p, ptcls);
    }

    /// @brief Checks whether the input particle has a child with a given PDGID
    bool hasChild(const ConstGenParticlePtr &ptcl, int pdgID) {
      for (const auto &child : Particle(*ptcl).children()) {
        if (child.pid() == pdgID) {
          return true;
        }
      }
      return false;
    }

    /// @brief Checks whether the input particle has a parent with a given PDGID
    bool hasParent(const ConstGenParticlePtr &ptcl, int pdgID) {
      for (const auto &parent : HepMCUtils::particles(ptcl->production_vertex(), Relatives::PARENTS))
        if (parent->pdg_id() == pdgID)
          return true;
      return false;
    }

    /// @brief Return true is particle decays to quarks
    bool quarkDecay(const Particle &p) {
      for (const auto &child : p.children()) {
        if (PID::isQuark(child.pid())) {
          return true;
        }
      }
      return false;
    }

    /// @brief Return true if particle decays to charged leptons.
    bool ChLeptonDecay(const Particle &p) {
      for (const auto &child : p.children()) {
        if (PID::isChargedLepton(child.pid())) {
          return true;
        }
      }
      return false;
    }

    /// @brief Returns the classification object with the error code set.
    ///        Prints an warning message, and keeps track of number of errors
    HiggsClassification error(HiggsClassification &cat,
                              HTXS::ErrorCode err,
                              std::string msg = "",
                              int NmaxWarnings = 20) {
      // Set the error, and keep statistics
      cat.errorCode = err;
      ++m_errorCount[err];

      // Print warning message to the screen/log
      static std::atomic<int> Nwarnings{0};
      if (!msg.empty() && ++Nwarnings < NmaxWarnings)
        MSG_WARNING(msg);

      return cat;
    }
    /// @}

    /// @brief Main classificaion method.
    HiggsClassification classifyEvent(const Event &event, const HTXS::HiggsProdMode prodMode) {
      if (m_HiggsProdMode == HTXS::UNKNOWN)
        m_HiggsProdMode = prodMode;

      // the classification object
      HiggsClassification cat;
      cat.prodMode = prodMode;
      cat.errorCode = HTXS::UNDEFINED;
      cat.stage0_cat = HTXS::Stage0::UNKNOWN;
      cat.stage1_cat_pTjet25GeV = HTXS::Stage1::UNKNOWN;
      cat.stage1_cat_pTjet30GeV = HTXS::Stage1::UNKNOWN;
      cat.stage1_1_cat_pTjet25GeV = HTXS::Stage1_1::UNKNOWN;
      cat.stage1_1_cat_pTjet30GeV = HTXS::Stage1_1::UNKNOWN;
      cat.stage1_1_fine_cat_pTjet25GeV = HTXS::Stage1_1_Fine::UNKNOWN;
      cat.stage1_1_fine_cat_pTjet30GeV = HTXS::Stage1_1_Fine::UNKNOWN;
      cat.stage1_2_cat_pTjet25GeV = HTXS::Stage1_2::UNKNOWN;
      cat.stage1_2_cat_pTjet30GeV = HTXS::Stage1_2::UNKNOWN;
      cat.stage1_2_fine_cat_pTjet25GeV = HTXS::Stage1_2_Fine::UNKNOWN;
      cat.stage1_2_fine_cat_pTjet30GeV = HTXS::Stage1_2_Fine::UNKNOWN;

      if (prodMode == HTXS::UNKNOWN)
        return error(cat,
                     HTXS::PRODMODE_DEFINED,
                     "Unkown Higgs production mechanism. Cannot classify event."
                     " Classification for all events will most likely fail.");

      /*****
       * Step 1. 
       *  Idenfify the Higgs boson and the hard scatter vertex
       *  There should be only one of each.
       */

      ConstGenVertexPtr HSvtx = nullptr;
      int Nhiggs = 0;
      for (const ConstGenParticlePtr &ptcl : HepMCUtils::particles(event.genEvent())) {
        // a) Reject all non-Higgs particles
        if (!PID::isHiggs(ptcl->pdg_id()))
          continue;
        // b) select only the final Higgs boson copy, prior to decay
        if (ptcl->end_vertex() && !hasChild(ptcl, PID::HIGGS)) {
          cat.higgs = Particle(ptcl);
          ++Nhiggs;
        }
        // c) HepMC3 does not provide signal_proces_vertex anymore
        //    set hard-scatter vertex based on first Higgs boson
        if (HSvtx == nullptr && ptcl->production_vertex() && !hasParent(ptcl, PID::HIGGS))
          HSvtx = ptcl->production_vertex();
      }

      // Make sure things are in order so far
      if (Nhiggs != 1)
        return error(cat,
                     HTXS::HIGGS_IDENTIFICATION,
                     "Current event has " + std::to_string(Nhiggs) + " Higgs bosons. There must be only one.");
      if (cat.higgs.children().size() < 2)
        return error(cat, HTXS::HIGGS_DECAY_IDENTIFICATION, "Could not identify Higgs boson decay products.");

      if (HSvtx == nullptr)
        return error(cat, HTXS::HS_VTX_IDENTIFICATION, "Cannot find hard-scatter vertex of current event.");

      /*****
       * Step 2. 
       *   Identify associated vector bosons
       */

      // Find associated vector bosons
      bool is_uncatdV = false;
      Particles uncatV_decays;
      FourMomentum uncatV_p4(0, 0, 0, 0);
      FourVector uncatV_v4(0, 0, 0, 0);
      int nWs = 0, nZs = 0;
      if (isVH(prodMode)) {
        for (const auto &ptcl : HepMCUtils::particles(HSvtx, Relatives::CHILDREN)) {
          if (PID::isW(ptcl->pdg_id())) {
            ++nWs;
            cat.V = Particle(ptcl);
          }
          if (PID::isZ(ptcl->pdg_id())) {
            ++nZs;
            cat.V = Particle(ptcl);
          }
        }
        if (nWs + nZs > 0)
          cat.V = getLastInstance(cat.V);
        else {
          for (const auto &ptcl : HepMCUtils::particles(HSvtx, Relatives::CHILDREN)) {
            if (!PID::isHiggs(ptcl->pdg_id())) {
              uncatV_decays += Particle(ptcl);
              uncatV_p4 += Particle(ptcl).momentum();
              // uncatV_v4 += Particle(ptcl).origin();
            }
          }
          // is_uncatdV = true; cat.V = Particle(24,uncatV_p4,uncatV_v4);
          is_uncatdV = true;
          cat.V = Particle(24, uncatV_p4);
        }
      }

      if (!is_uncatdV) {
        if (isVH(prodMode) && !cat.V.genParticle()->end_vertex())
          return error(cat, HTXS::VH_DECAY_IDENTIFICATION, "Vector boson does not decay!");

        if (isVH(prodMode) && cat.V.children().size() < 2)
          return error(cat, HTXS::VH_DECAY_IDENTIFICATION, "Vector boson does not decay!");

        if ((prodMode == HTXS::WH && (nZs > 0 || nWs != 1)) ||
            ((prodMode == HTXS::QQ2ZH || prodMode == HTXS::GG2ZH) && (nZs != 1 || nWs > 0)))
          return error(cat,
                       HTXS::VH_IDENTIFICATION,
                       "Found " + std::to_string(nWs) + " W-bosons and " + std::to_string(nZs) +
                           " Z-bosons. Inconsitent with VH expectation.");
      }

      // Find and store the W-bosons from ttH->WbWbH
      Particles Ws;
      if (prodMode == HTXS::TTH || prodMode == HTXS::TH) {
        // loop over particles produced in hard-scatter vertex
        for (const auto &ptcl : HepMCUtils::particles(HSvtx, Relatives::CHILDREN)) {
          if (!PID::isTop(ptcl->pdg_id()))
            continue;
          Particle top = getLastInstance(Particle(ptcl));
          if (top.genParticle()->end_vertex())
            for (const auto &child : top.children())
              if (PID::isW(child.pid()))
                Ws += getLastInstance(child);
        }
      }

      // Make sure result make sense
      if ((prodMode == HTXS::TTH && Ws.size() < 2) || (prodMode == HTXS::TH && Ws.empty()))
        return error(cat, HTXS::TOP_W_IDENTIFICATION, "Failed to identify W-boson(s) from t-decay!");

      /*****
       * Step 3.
       *   Build jets
       *   Make sure all stable particles are present
       */

      // Create a list of the vector bosons that decay leptonically
      // Either the vector boson produced in association with the Higgs boson,
      // or the ones produced from decays of top quarks produced with the Higgs
      Particles leptonicVs;
      if (!is_uncatdV) {
        if (isVH(prodMode) && !quarkDecay(cat.V))
          leptonicVs += cat.V;
      } else
        leptonicVs = uncatV_decays;
      for (const auto &W : Ws)
        if (W.genParticle()->end_vertex() && !quarkDecay(W))
          leptonicVs += W;

      // Obtain all stable, final-state particles
      const Particles FS = apply<FinalState>(event, "FS").particles();
      Particles hadrons;
      FourMomentum sum(0, 0, 0, 0), vSum(0, 0, 0, 0), hSum(0, 0, 0, 0);
      for (const Particle &p : FS) {
        // Add up the four momenta of all stable particles as a cross check
        sum += p.momentum();
        // ignore particles from the Higgs boson
        if (originateFrom(p, cat.higgs)) {
          hSum += p.momentum();
          continue;
        }
        // Cross-check the V decay products for VH
        if (isVH(prodMode) && !is_uncatdV && originateFrom(p, Ws))
          vSum += p.momentum();
        // ignore final state particles from leptonic V decays
        if (!leptonicVs.empty() && originateFrom(p, leptonicVs))
          continue;
        // All particles reaching here are considered hadrons and will be used to build jets
        hadrons += p;
      }

      cat.p4decay_higgs = hSum;
      cat.p4decay_V = vSum;

      FinalState fps_temp;
      FastJets jets(fps_temp, JetAlg::ANTIKT, 0.4);
      jets.calc(hadrons);

      cat.jets25 = jets.jetsByPt(Cuts::pT > 25.0);
      cat.jets30 = jets.jetsByPt(Cuts::pT > 30.0);

      // Temporary fix: add variables to perform STXS 1.3 classification with nanoAOD on-the-fly
      // Vector-boson pt for VH production modes
      if (isVH(prodMode)) {
        cat.V_pt = cat.V.pt();
      } else {
        cat.V_pt = -999;
      }
      // Dijet variables using jets30 collection
      if (cat.jets30.size() >= 2) {
        cat.Mjj = (cat.jets30[0].mom() + cat.jets30[1].mom()).mass();
        cat.ptHjj = (cat.jets30[0].mom() + cat.jets30[1].mom() + cat.higgs.momentum()).pt();
        cat.dPhijj = cat.jets30[0].mom().phi() - cat.jets30[1].mom().phi();
        // Return phi angle in the interval [-PI,PI)
        if (cat.dPhijj >= Rivet::pi)
          cat.dPhijj -= 2 * Rivet::pi;
        else if (cat.dPhijj < -1 * Rivet::pi)
          cat.dPhijj += 2 * Rivet::pi;
      } else {
        cat.Mjj = -999;
        cat.ptHjj = -999;
        cat.dPhijj = -999;
      }

      // check that four mometum sum of all stable particles satisfies momentum consevation
      /*
      if ( sum.pt()>0.1 )
	return error(cat,HTXS::MOMENTUM_CONSERVATION,"Four vector sum does not amount to pT=0, m=E=sqrt(s), but pT="+
		     std::to_string(sum.pt())+" GeV and m = "+std::to_string(sum.mass())+" GeV");
*/
      // check if V-boson was not included in the event record but decay particles were
      // EFT contact interaction: return UNKNOWN for category but set all event/particle kinematics
      if (is_uncatdV)
        return error(cat, HTXS::VH_IDENTIFICATION, "Failed to identify associated V-boson!");

      /*****
       * Step 4.
       *   Classify and save output
       */

      // Apply the categorization categorization
      cat.isZ2vvDecay = false;
      if ((prodMode == HTXS::GG2ZH || prodMode == HTXS::QQ2ZH) && !quarkDecay(cat.V) && !ChLeptonDecay(cat.V))
        cat.isZ2vvDecay = true;
      cat.stage0_cat = getStage0Category(prodMode, cat.higgs, cat.V);
      cat.stage1_cat_pTjet25GeV = getStage1Category(prodMode, cat.higgs, cat.jets25, cat.V);
      cat.stage1_cat_pTjet30GeV = getStage1Category(prodMode, cat.higgs, cat.jets30, cat.V);
      cat.stage1_1_cat_pTjet25GeV = getStage1_1_Category(prodMode, cat.higgs, cat.jets25, cat.V);
      cat.stage1_1_cat_pTjet30GeV = getStage1_1_Category(prodMode, cat.higgs, cat.jets30, cat.V);
      cat.stage1_1_fine_cat_pTjet25GeV = getStage1_1_Fine_Category(prodMode, cat.higgs, cat.jets25, cat.V);
      cat.stage1_1_fine_cat_pTjet30GeV = getStage1_1_Fine_Category(prodMode, cat.higgs, cat.jets30, cat.V);
      cat.stage1_2_cat_pTjet25GeV = getStage1_2_Category(prodMode, cat.higgs, cat.jets25, cat.V);
      cat.stage1_2_cat_pTjet30GeV = getStage1_2_Category(prodMode, cat.higgs, cat.jets30, cat.V);
      cat.stage1_2_fine_cat_pTjet25GeV = getStage1_2_Fine_Category(prodMode, cat.higgs, cat.jets25, cat.V);
      cat.stage1_2_fine_cat_pTjet30GeV = getStage1_2_Fine_Category(prodMode, cat.higgs, cat.jets30, cat.V);

      cat.errorCode = HTXS::SUCCESS;
      ++m_errorCount[HTXS::SUCCESS];
      ++m_sumevents;

      return cat;
    }

    /// @name Categorization methods
    /// Methods to assign the truth category based
    /// on the identified Higgs boson and associated
    /// vector bosons and/or reconstructed jets
    /// @{

    /// @brief Return bin index of x given the provided bin edges. 0=first bin, -1=underflow bin.
    int getBin(double x, std::vector<double> bins) {
      for (size_t i = 1; i < bins.size(); ++i)
        if (x < bins[i])
          return i - 1;
      return bins.size() - 1;
    }

    /// @brief VBF topolog selection
    /// 0 = fail loose selction: m_jj > 400 GeV and Dy_jj > 2.8
    /// 1 pass loose, but fail additional cut pT(Hjj)<25. 2 pass tight selection
    int vbfTopology(const Jets &jets, const Particle &higgs) {
      if (jets.size() < 2)
        return 0;
      const FourMomentum &j1 = jets[0].momentum(), &j2 = jets[1].momentum();
      bool VBFtopo = (j1 + j2).mass() > 400.0 && std::abs(j1.rapidity() - j2.rapidity()) > 2.8;
      return VBFtopo ? (j1 + j2 + higgs.momentum()).pt() < 25 ? 2 : 1 : 0;
    }

    /// @brief VBF topology selection Stage1.1 and Stage1.2
    /// 0 = fail loose selection: m_jj > 350 GeV
    /// 1 pass loose, but fail additional cut pT(Hjj)<25. 2 pass pT(Hjj)>25 selection
    /// 3 pass tight (m_jj>700 GeV), but fail additional cut pT(Hjj)<25. 4 pass pT(Hjj)>25 selection
    int vbfTopology_Stage1_X(const Jets &jets, const Particle &higgs) {
      if (jets.size() < 2)
        return 0;
      const FourMomentum &j1 = jets[0].momentum(), &j2 = jets[1].momentum();
      double mjj = (j1 + j2).mass();
      if (mjj > 350 && mjj <= 700)
        return (j1 + j2 + higgs.momentum()).pt() < 25 ? 1 : 2;
      else if (mjj > 700)
        return (j1 + j2 + higgs.momentum()).pt() < 25 ? 3 : 4;
      else
        return 0;
    }

    /// @brief VBF topology selection for Stage1.1 and Stage 1.2 Fine
    /// 0 = fail loose selection: m_jj > 350 GeV
    /// 1 pass loose, but fail additional cut pT(Hjj)<25. 2 pass pT(Hjj)>25 selection
    /// 3 pass 700<m_jj<1000 GeV, but fail additional cut pT(Hjj)<25. 4 pass pT(Hjj)>25 selection
    /// 5 pass 1000<m_jj<1500 GeV, but fail additional cut pT(Hjj)<25. 6 pass pT(Hjj)>25 selection
    /// 7 pass m_jj>1500 GeV, but fail additional cut pT(Hjj)<25. 8 pass pT(Hjj)>25 selection
    int vbfTopology_Stage1_X_Fine(const Jets &jets, const Particle &higgs) {
      if (jets.size() < 2)
        return 0;
      const FourMomentum &j1 = jets[0].momentum(), &j2 = jets[1].momentum();
      double mjj = (j1 + j2).mass();
      if (mjj > 350 && mjj <= 700)
        return (j1 + j2 + higgs.momentum()).pt() < 25 ? 1 : 2;
      else if (mjj > 700 && mjj <= 1000)
        return (j1 + j2 + higgs.momentum()).pt() < 25 ? 3 : 4;
      else if (mjj > 1000 && mjj <= 1500)
        return (j1 + j2 + higgs.momentum()).pt() < 25 ? 5 : 6;
      else if (mjj > 1500)
        return (j1 + j2 + higgs.momentum()).pt() < 25 ? 7 : 8;
      else
        return 0;
    }

    /// @brief Whether the Higgs is produced in association with a vector boson (VH)
    bool isVH(HTXS::HiggsProdMode p) { return p == HTXS::WH || p == HTXS::QQ2ZH || p == HTXS::GG2ZH; }

    /// @brief Stage-0 HTXS categorization
    HTXS::Stage0::Category getStage0Category(const HTXS::HiggsProdMode prodMode,
                                             const Particle &higgs,
                                             const Particle &V) {
      using namespace HTXS::Stage0;
      int ctrlHiggs = std::abs(higgs.rapidity()) < 2.5;
      // Special cases first, qq→Hqq
      if ((prodMode == HTXS::WH || prodMode == HTXS::QQ2ZH) && quarkDecay(V)) {
        return ctrlHiggs ? VH2HQQ : VH2HQQ_FWDH;
      } else if (prodMode == HTXS::GG2ZH && quarkDecay(V)) {
        return Category(HTXS::GGF * 10 + ctrlHiggs);
      }
      // General case after
      return Category(prodMode * 10 + ctrlHiggs);
    }

    /// @brief Stage-1 categorization
    HTXS::Stage1::Category getStage1Category(const HTXS::HiggsProdMode prodMode,
                                             const Particle &higgs,
                                             const Jets &jets,
                                             const Particle &V) {
      using namespace HTXS::Stage1;
      int Njets = jets.size(), ctrlHiggs = std::abs(higgs.rapidity()) < 2.5, fwdHiggs = !ctrlHiggs;
      double pTj1 = !jets.empty() ? jets[0].momentum().pt() : 0;
      int vbfTopo = vbfTopology(jets, higgs);

      // 1. GGF Stage 1 categories
      //    Following YR4 write-up: XXXXX
      if (prodMode == HTXS::GGF || (prodMode == HTXS::GG2ZH && quarkDecay(V))) {
        if (fwdHiggs)
          return GG2H_FWDH;
        if (Njets == 0)
          return GG2H_0J;
        else if (Njets == 1)
          return Category(GG2H_1J_PTH_0_60 + getBin(higgs.pt(), {0, 60, 120, 200}));
        else if (Njets >= 2) {
          // events with pT_H>200 get priority over VBF cuts
          if (higgs.pt() <= 200) {
            if (vbfTopo == 2)
              return GG2H_VBFTOPO_JET3VETO;
            else if (vbfTopo == 1)
              return GG2H_VBFTOPO_JET3;
          }
          // Njets >= 2jets without VBF topology
          return Category(GG2H_GE2J_PTH_0_60 + getBin(higgs.pt(), {0, 60, 120, 200}));
        }
      }
      // 2. Electroweak qq->Hqq Stage 1 categories
      else if (prodMode == HTXS::VBF || (isVH(prodMode) && quarkDecay(V))) {
        if (std::abs(higgs.rapidity()) > 2.5)
          return QQ2HQQ_FWDH;
        if (pTj1 > 200)
          return QQ2HQQ_PTJET1_GT200;
        if (vbfTopo == 2)
          return QQ2HQQ_VBFTOPO_JET3VETO;
        if (vbfTopo == 1)
          return QQ2HQQ_VBFTOPO_JET3;
        double mjj = jets.size() > 1 ? (jets[0].mom() + jets[1].mom()).mass() : 0;
        if (60 < mjj && mjj < 120)
          return QQ2HQQ_VH2JET;
        return QQ2HQQ_REST;
      }
      // 3. WH->Hlv categories
      else if (prodMode == HTXS::WH) {
        if (fwdHiggs)
          return QQ2HLNU_FWDH;
        else if (V.pt() < 150)
          return QQ2HLNU_PTV_0_150;
        else if (V.pt() > 250)
          return QQ2HLNU_PTV_GT250;
        // 150 < pTV/GeV < 250
        return jets.empty() ? QQ2HLNU_PTV_150_250_0J : QQ2HLNU_PTV_150_250_GE1J;
      }
      // 4. qq->ZH->llH categories
      else if (prodMode == HTXS::QQ2ZH) {
        if (fwdHiggs)
          return QQ2HLL_FWDH;
        else if (V.pt() < 150)
          return QQ2HLL_PTV_0_150;
        else if (V.pt() > 250)
          return QQ2HLL_PTV_GT250;
        // 150 < pTV/GeV < 250
        return jets.empty() ? QQ2HLL_PTV_150_250_0J : QQ2HLL_PTV_150_250_GE1J;
      }
      // 5. gg->ZH->llH categories
      else if (prodMode == HTXS::GG2ZH) {
        if (fwdHiggs)
          return GG2HLL_FWDH;
        if (V.pt() < 150)
          return GG2HLL_PTV_0_150;
        else if (jets.empty())
          return GG2HLL_PTV_GT150_0J;
        return GG2HLL_PTV_GT150_GE1J;
      }
      // 6.ttH,bbH,tH categories
      else if (prodMode == HTXS::TTH)
        return Category(TTH_FWDH + ctrlHiggs);
      else if (prodMode == HTXS::BBH)
        return Category(BBH_FWDH + ctrlHiggs);
      else if (prodMode == HTXS::TH)
        return Category(TH_FWDH + ctrlHiggs);
      return UNKNOWN;
    }

    /// @brief Stage-1.1 categorization
    HTXS::Stage1_1::Category getStage1_1_Category(const HTXS::HiggsProdMode prodMode,
                                                  const Particle &higgs,
                                                  const Jets &jets,
                                                  const Particle &V) {
      using namespace HTXS::Stage1_1;
      int Njets = jets.size(), ctrlHiggs = std::abs(higgs.rapidity()) < 2.5, fwdHiggs = !ctrlHiggs;
      int vbfTopo = vbfTopology_Stage1_X(jets, higgs);

      // 1. GGF Stage 1 categories
      //    Following YR4 write-up: XXXXX
      if (prodMode == HTXS::GGF || (prodMode == HTXS::GG2ZH && quarkDecay(V))) {
        if (fwdHiggs)
          return GG2H_FWDH;
        if (higgs.pt() > 200)
          return GG2H_PTH_GT200;
        if (Njets == 0)
          return higgs.pt() < 10 ? GG2H_0J_PTH_0_10 : GG2H_0J_PTH_GT10;
        if (Njets == 1)
          return Category(GG2H_1J_PTH_0_60 + getBin(higgs.pt(), {0, 60, 120, 200}));
        if (Njets > 1) {
          //VBF topology
          if (vbfTopo)
            return Category(GG2H_MJJ_350_700_PTHJJ_0_25 + vbfTopo - 1);
          //Njets >= 2jets without VBF topology (mjj<350)
          return Category(GG2H_GE2J_MJJ_0_350_PTH_0_60 + getBin(higgs.pt(), {0, 60, 120, 200}));
        }
      }

      // 2. Electroweak qq->Hqq Stage 1.1 categories
      else if (prodMode == HTXS::VBF || (isVH(prodMode) && quarkDecay(V))) {
        if (std::abs(higgs.rapidity()) > 2.5)
          return QQ2HQQ_FWDH;
        int Njets = jets.size();
        if (Njets == 0)
          return QQ2HQQ_0J;
        else if (Njets == 1)
          return QQ2HQQ_1J;
        else if (Njets >= 2) {
          double mjj = (jets[0].mom() + jets[1].mom()).mass();
          if (mjj < 60)
            return QQ2HQQ_MJJ_0_60;
          else if (60 < mjj && mjj < 120)
            return QQ2HQQ_MJJ_60_120;
          else if (120 < mjj && mjj < 350)
            return QQ2HQQ_MJJ_120_350;
          else if (mjj > 350) {
            if (higgs.pt() > 200)
              return QQ2HQQ_MJJ_GT350_PTH_GT200;
            if (vbfTopo)
              return Category(QQ2HQQ_MJJ_GT350_PTH_GT200 + vbfTopo);
          }
        }
      }
      // 3. WH->Hlv categories
      else if (prodMode == HTXS::WH) {
        if (fwdHiggs)
          return QQ2HLNU_FWDH;
        else if (V.pt() < 75)
          return QQ2HLNU_PTV_0_75;
        else if (V.pt() < 150)
          return QQ2HLNU_PTV_75_150;
        else if (V.pt() > 250)
          return QQ2HLNU_PTV_GT250;
        // 150 < pTV/GeV < 250
        return jets.empty() ? QQ2HLNU_PTV_150_250_0J : QQ2HLNU_PTV_150_250_GE1J;
      }
      // 4. qq->ZH->llH categories
      else if (prodMode == HTXS::QQ2ZH) {
        if (fwdHiggs)
          return QQ2HLL_FWDH;
        else if (V.pt() < 75)
          return QQ2HLL_PTV_0_75;
        else if (V.pt() < 150)
          return QQ2HLL_PTV_75_150;
        else if (V.pt() > 250)
          return QQ2HLL_PTV_GT250;
        // 150 < pTV/GeV < 250
        return jets.empty() ? QQ2HLL_PTV_150_250_0J : QQ2HLL_PTV_150_250_GE1J;
      }
      // 5. gg->ZH->llH categories
      else if (prodMode == HTXS::GG2ZH) {
        if (fwdHiggs)
          return GG2HLL_FWDH;
        else if (V.pt() < 75)
          return GG2HLL_PTV_0_75;
        else if (V.pt() < 150)
          return GG2HLL_PTV_75_150;
        else if (V.pt() > 250)
          return GG2HLL_PTV_GT250;
        return jets.empty() ? GG2HLL_PTV_150_250_0J : GG2HLL_PTV_150_250_GE1J;
      }
      // 6.ttH,bbH,tH categories
      else if (prodMode == HTXS::TTH)
        return Category(TTH_FWDH + ctrlHiggs);
      else if (prodMode == HTXS::BBH)
        return Category(BBH_FWDH + ctrlHiggs);
      else if (prodMode == HTXS::TH)
        return Category(TH_FWDH + ctrlHiggs);
      return UNKNOWN;
    }

    /// @brief Stage-1_1 categorization
    HTXS::Stage1_1_Fine::Category getStage1_1_Fine_Category(const HTXS::HiggsProdMode prodMode,
                                                            const Particle &higgs,
                                                            const Jets &jets,
                                                            const Particle &V) {
      using namespace HTXS::Stage1_1_Fine;
      int Njets = jets.size(), ctrlHiggs = std::abs(higgs.rapidity()) < 2.5, fwdHiggs = !ctrlHiggs;
      int vbfTopo = vbfTopology_Stage1_X_Fine(jets, higgs);

      // 1. GGF Stage 1.1 categories
      //    Following YR4 write-up: XXXXX
      if (prodMode == HTXS::GGF || (prodMode == HTXS::GG2ZH && quarkDecay(V))) {
        if (fwdHiggs)
          return GG2H_FWDH;
        if (higgs.pt() > 200)
          return GG2H_PTH_GT200;
        if (Njets == 0)
          return higgs.pt() < 10 ? GG2H_0J_PTH_0_10 : GG2H_0J_PTH_GT10;
        if (Njets == 1)
          return Category(GG2H_1J_PTH_0_60 + getBin(higgs.pt(), {0, 60, 120, 200}));
        if (Njets > 1) {
          //double mjj = (jets[0].mom()+jets[1].mom()).mass();
          double pTHjj = (jets[0].momentum() + jets[1].momentum() + higgs.momentum()).pt();
          //VBF topology
          if (vbfTopo)
            return Category(GG2H_MJJ_350_700_PTHJJ_0_25 + vbfTopo - 1);
          //Njets >= 2jets without VBF topology (mjj<350)
          if (pTHjj < 25)
            return Category(GG2H_GE2J_MJJ_0_350_PTH_0_60_PTHJJ_0_25 + getBin(higgs.pt(), {0, 60, 120, 200}));
          else
            return Category(GG2H_GE2J_MJJ_0_350_PTH_0_60_PTHJJ_GT25 + getBin(higgs.pt(), {0, 60, 120, 200}));
        }
      }

      // 2. Electroweak qq->Hqq Stage 1.1 categories
      else if (prodMode == HTXS::VBF || (isVH(prodMode) && quarkDecay(V))) {
        if (std::abs(higgs.rapidity()) > 2.5)
          return QQ2HQQ_FWDH;
        int Njets = jets.size();
        if (Njets == 0)
          return QQ2HQQ_0J;
        else if (Njets == 1)
          return QQ2HQQ_1J;
        else if (Njets >= 2) {
          double mjj = (jets[0].mom() + jets[1].mom()).mass();
          double pTHjj = (jets[0].momentum() + jets[1].momentum() + higgs.momentum()).pt();
          if (mjj < 350) {
            if (pTHjj < 25)
              return Category(QQ2HQQ_MJJ_0_60_PTHJJ_0_25 + getBin(mjj, {0, 60, 120, 350}));
            else
              return Category(QQ2HQQ_MJJ_0_60_PTHJJ_GT25 + getBin(mjj, {0, 60, 120, 350}));
          } else {  //mjj>350 GeV
            if (higgs.pt() < 200) {
              return Category(QQ2HQQ_MJJ_350_700_PTHJJ_0_25 + vbfTopo - 1);
            } else {
              return Category(QQ2HQQ_PTH_GT200_MJJ_350_700_PTHJJ_0_25 + vbfTopo - 1);
            }
          }
        }
      }
      // 3. WH->Hlv categories
      else if (prodMode == HTXS::WH) {
        if (fwdHiggs)
          return QQ2HLNU_FWDH;
        int Njets = jets.size();
        if (Njets == 0)
          return Category(QQ2HLNU_PTV_0_75_0J + getBin(V.pt(), {0, 75, 150, 250, 400}));
        if (Njets == 1)
          return Category(QQ2HLNU_PTV_0_75_1J + getBin(V.pt(), {0, 75, 150, 250, 400}));
        return Category(QQ2HLNU_PTV_0_75_GE2J + getBin(V.pt(), {0, 75, 150, 250, 400}));
      }
      // 4. qq->ZH->llH categories
      else if (prodMode == HTXS::QQ2ZH) {
        if (fwdHiggs)
          return QQ2HLL_FWDH;
        int Njets = jets.size();
        if (Njets == 0)
          return Category(QQ2HLL_PTV_0_75_0J + getBin(V.pt(), {0, 75, 150, 250, 400}));
        if (Njets == 1)
          return Category(QQ2HLL_PTV_0_75_1J + getBin(V.pt(), {0, 75, 150, 250, 400}));
        return Category(QQ2HLL_PTV_0_75_GE2J + getBin(V.pt(), {0, 75, 150, 250, 400}));
      }
      // 5. gg->ZH->llH categories
      else if (prodMode == HTXS::GG2ZH) {
        if (fwdHiggs)
          return GG2HLL_FWDH;
        int Njets = jets.size();
        if (Njets == 0)
          return Category(GG2HLL_PTV_0_75_0J + getBin(V.pt(), {0, 75, 150, 250, 400}));
        if (Njets == 1)
          return Category(GG2HLL_PTV_0_75_1J + getBin(V.pt(), {0, 75, 150, 250, 400}));
        return Category(GG2HLL_PTV_0_75_GE2J + getBin(V.pt(), {0, 75, 150, 250, 400}));
      }
      // 6.ttH,bbH,tH categories
      else if (prodMode == HTXS::TTH)
        return Category(TTH_FWDH + ctrlHiggs);
      else if (prodMode == HTXS::BBH)
        return Category(BBH_FWDH + ctrlHiggs);
      else if (prodMode == HTXS::TH)
        return Category(TH_FWDH + ctrlHiggs);
      return UNKNOWN;
    }

    /// @brief Stage-1.2 categorization
    HTXS::Stage1_2::Category getStage1_2_Category(const HTXS::HiggsProdMode prodMode,
                                                  const Particle &higgs,
                                                  const Jets &jets,
                                                  const Particle &V) {
      using namespace HTXS::Stage1_2;
      int Njets = jets.size(), ctrlHiggs = std::abs(higgs.rapidity()) < 2.5, fwdHiggs = !ctrlHiggs;
      int vbfTopo = vbfTopology_Stage1_X(jets, higgs);

      // 1. GGF Stage 1 categories
      //    Following YR4 write-up: XXXXX
      if (prodMode == HTXS::GGF || (prodMode == HTXS::GG2ZH && quarkDecay(V))) {
        if (fwdHiggs)
          return GG2H_FWDH;
        if (higgs.pt() > 200)
          return Category(GG2H_PTH_200_300 + getBin(higgs.pt(), {200, 300, 450, 650}));
        if (Njets == 0)
          return higgs.pt() < 10 ? GG2H_0J_PTH_0_10 : GG2H_0J_PTH_GT10;
        if (Njets == 1)
          return Category(GG2H_1J_PTH_0_60 + getBin(higgs.pt(), {0, 60, 120, 200}));
        if (Njets > 1) {
          //VBF topology
          if (vbfTopo)
            return Category(GG2H_GE2J_MJJ_350_700_PTH_0_200_PTHJJ_0_25 + vbfTopo - 1);
          //Njets >= 2jets without VBF topology (mjj<350)
          return Category(GG2H_GE2J_MJJ_0_350_PTH_0_60 + getBin(higgs.pt(), {0, 60, 120, 200}));
        }
      }

      // 2. Electroweak qq->Hqq Stage 1.2 categories
      else if (prodMode == HTXS::VBF || (isVH(prodMode) && quarkDecay(V))) {
        if (std::abs(higgs.rapidity()) > 2.5)
          return QQ2HQQ_FWDH;
        int Njets = jets.size();
        if (Njets == 0)
          return QQ2HQQ_0J;
        else if (Njets == 1)
          return QQ2HQQ_1J;
        else if (Njets >= 2) {
          double mjj = (jets[0].mom() + jets[1].mom()).mass();
          if (mjj < 60)
            return QQ2HQQ_GE2J_MJJ_0_60;
          else if (60 < mjj && mjj < 120)
            return QQ2HQQ_GE2J_MJJ_60_120;
          else if (120 < mjj && mjj < 350)
            return QQ2HQQ_GE2J_MJJ_120_350;
          else if (mjj > 350) {
            if (higgs.pt() > 200)
              return QQ2HQQ_GE2J_MJJ_GT350_PTH_GT200;
            if (vbfTopo)
              return Category(QQ2HQQ_GE2J_MJJ_GT350_PTH_GT200 + vbfTopo);
          }
        }
      }
      // 3. WH->Hlv categories
      else if (prodMode == HTXS::WH) {
        if (fwdHiggs)
          return QQ2HLNU_FWDH;
        else if (V.pt() < 75)
          return QQ2HLNU_PTV_0_75;
        else if (V.pt() < 150)
          return QQ2HLNU_PTV_75_150;
        else if (V.pt() > 250)
          return QQ2HLNU_PTV_GT250;
        // 150 < pTV/GeV < 250
        return jets.empty() ? QQ2HLNU_PTV_150_250_0J : QQ2HLNU_PTV_150_250_GE1J;
      }
      // 4. qq->ZH->llH categories
      else if (prodMode == HTXS::QQ2ZH) {
        if (fwdHiggs)
          return QQ2HLL_FWDH;
        else if (V.pt() < 75)
          return QQ2HLL_PTV_0_75;
        else if (V.pt() < 150)
          return QQ2HLL_PTV_75_150;
        else if (V.pt() > 250)
          return QQ2HLL_PTV_GT250;
        // 150 < pTV/GeV < 250
        return jets.empty() ? QQ2HLL_PTV_150_250_0J : QQ2HLL_PTV_150_250_GE1J;
      }
      // 5. gg->ZH->llH categories
      else if (prodMode == HTXS::GG2ZH) {
        if (fwdHiggs)
          return GG2HLL_FWDH;
        else if (V.pt() < 75)
          return GG2HLL_PTV_0_75;
        else if (V.pt() < 150)
          return GG2HLL_PTV_75_150;
        else if (V.pt() > 250)
          return GG2HLL_PTV_GT250;
        return jets.empty() ? GG2HLL_PTV_150_250_0J : GG2HLL_PTV_150_250_GE1J;
      }
      // 6.ttH,bbH,tH categories
      else if (prodMode == HTXS::TTH) {
        if (fwdHiggs)
          return TTH_FWDH;
        else
          return Category(TTH_PTH_0_60 + getBin(higgs.pt(), {0, 60, 120, 200, 300}));
      } else if (prodMode == HTXS::BBH)
        return Category(BBH_FWDH + ctrlHiggs);
      else if (prodMode == HTXS::TH)
        return Category(TH_FWDH + ctrlHiggs);
      return UNKNOWN;
    }

    /// @brief Stage-1.2 Fine categorization
    HTXS::Stage1_2_Fine::Category getStage1_2_Fine_Category(const HTXS::HiggsProdMode prodMode,
                                                            const Particle &higgs,
                                                            const Jets &jets,
                                                            const Particle &V) {
      using namespace HTXS::Stage1_2_Fine;
      int Njets = jets.size(), ctrlHiggs = std::abs(higgs.rapidity()) < 2.5, fwdHiggs = !ctrlHiggs;
      int vbfTopo = vbfTopology_Stage1_X_Fine(jets, higgs);

      // 1. GGF Stage 1.2 categories
      //    Following YR4 write-up: XXXXX
      if (prodMode == HTXS::GGF || (prodMode == HTXS::GG2ZH && quarkDecay(V))) {
        if (fwdHiggs)
          return GG2H_FWDH;
        if (higgs.pt() > 200) {
          if (Njets > 0) {
            double pTHj = (jets[0].momentum() + higgs.momentum()).pt();
            if (pTHj / higgs.pt() > 0.15)
              return Category(GG2H_PTH_200_300_PTHJoverPTH_GT15 + getBin(higgs.pt(), {200, 300, 450, 650}));
            else
              return Category(GG2H_PTH_200_300_PTHJoverPTH_0_15 + getBin(higgs.pt(), {200, 300, 450, 650}));
          } else
            return Category(GG2H_PTH_200_300_PTHJoverPTH_0_15 + getBin(higgs.pt(), {200, 300, 450, 650}));
        }
        if (Njets == 0)
          return higgs.pt() < 10 ? GG2H_0J_PTH_0_10 : GG2H_0J_PTH_GT10;
        if (Njets == 1)
          return Category(GG2H_1J_PTH_0_60 + getBin(higgs.pt(), {0, 60, 120, 200}));
        if (Njets > 1) {
          //double mjj = (jets[0].mom()+jets[1].mom()).mass();
          double pTHjj = (jets[0].momentum() + jets[1].momentum() + higgs.momentum()).pt();
          //VBF topology
          if (vbfTopo)
            return Category(GG2H_GE2J_MJJ_350_700_PTH_0_200_PTHJJ_0_25 + vbfTopo - 1);
          //Njets >= 2jets without VBF topology (mjj<350)
          if (pTHjj < 25)
            return Category(GG2H_GE2J_MJJ_0_350_PTH_0_60_PTHJJ_0_25 + getBin(higgs.pt(), {0, 60, 120, 200}));
          else
            return Category(GG2H_GE2J_MJJ_0_350_PTH_0_60_PTHJJ_GT25 + getBin(higgs.pt(), {0, 60, 120, 200}));
        }
      }

      // 2. Electroweak qq->Hqq Stage 1.2 categories
      else if (prodMode == HTXS::VBF || (isVH(prodMode) && quarkDecay(V))) {
        if (std::abs(higgs.rapidity()) > 2.5)
          return QQ2HQQ_FWDH;
        int Njets = jets.size();
        if (Njets == 0)
          return QQ2HQQ_0J;
        else if (Njets == 1)
          return QQ2HQQ_1J;
        else if (Njets >= 2) {
          double mjj = (jets[0].mom() + jets[1].mom()).mass();
          double pTHjj = (jets[0].momentum() + jets[1].momentum() + higgs.momentum()).pt();
          if (mjj < 350) {
            if (pTHjj < 25)
              return Category(QQ2HQQ_GE2J_MJJ_0_60_PTHJJ_0_25 + getBin(mjj, {0, 60, 120, 350}));
            else
              return Category(QQ2HQQ_GE2J_MJJ_0_60_PTHJJ_GT25 + getBin(mjj, {0, 60, 120, 350}));
          } else {  //mjj>350 GeV
            if (higgs.pt() < 200) {
              return Category(QQ2HQQ_GE2J_MJJ_350_700_PTH_0_200_PTHJJ_0_25 + vbfTopo - 1);
            } else {
              return Category(QQ2HQQ_GE2J_MJJ_350_700_PTH_GT200_PTHJJ_0_25 + vbfTopo - 1);
            }
          }
        }
      }
      // 3. WH->Hlv categories
      else if (prodMode == HTXS::WH) {
        if (fwdHiggs)
          return QQ2HLNU_FWDH;
        int Njets = jets.size();
        if (Njets == 0)
          return Category(QQ2HLNU_PTV_0_75_0J + getBin(V.pt(), {0, 75, 150, 250, 400}));
        if (Njets == 1)
          return Category(QQ2HLNU_PTV_0_75_1J + getBin(V.pt(), {0, 75, 150, 250, 400}));
        return Category(QQ2HLNU_PTV_0_75_GE2J + getBin(V.pt(), {0, 75, 150, 250, 400}));
      }
      // 4. qq->ZH->llH categories
      else if (prodMode == HTXS::QQ2ZH) {
        if (fwdHiggs)
          return QQ2HLL_FWDH;
        int Njets = jets.size();
        if (Njets == 0)
          return Category(QQ2HLL_PTV_0_75_0J + getBin(V.pt(), {0, 75, 150, 250, 400}));
        if (Njets == 1)
          return Category(QQ2HLL_PTV_0_75_1J + getBin(V.pt(), {0, 75, 150, 250, 400}));
        return Category(QQ2HLL_PTV_0_75_GE2J + getBin(V.pt(), {0, 75, 150, 250, 400}));
      }
      // 5. gg->ZH->llH categories
      else if (prodMode == HTXS::GG2ZH) {
        if (fwdHiggs)
          return GG2HLL_FWDH;
        int Njets = jets.size();
        if (Njets == 0)
          return Category(GG2HLL_PTV_0_75_0J + getBin(V.pt(), {0, 75, 150, 250, 400}));
        if (Njets == 1)
          return Category(GG2HLL_PTV_0_75_1J + getBin(V.pt(), {0, 75, 150, 250, 400}));
        return Category(GG2HLL_PTV_0_75_GE2J + getBin(V.pt(), {0, 75, 150, 250, 400}));
      }
      // 6.ttH,bbH,tH categories
      else if (prodMode == HTXS::TTH) {
        if (fwdHiggs)
          return TTH_FWDH;
        else
          return Category(TTH_PTH_0_60 + getBin(higgs.pt(), {0, 60, 120, 200, 300, 450}));
      } else if (prodMode == HTXS::BBH)
        return Category(BBH_FWDH + ctrlHiggs);
      else if (prodMode == HTXS::TH)
        return Category(TH_FWDH + ctrlHiggs);
      return UNKNOWN;
    }

    /// @}

    /// @name Default Rivet analysis methods and steering methods
    /// @{

    /// @brief Sets the Higgs production mode
    void setHiggsProdMode(HTXS::HiggsProdMode prodMode) { m_HiggsProdMode = prodMode; }

    /// @brief default Rivet Analysis::init method
    /// Booking of histograms, initializing Rivet projection
    /// Extracts Higgs production mode from shell variable if not set manually using setHiggsProdMode
    void init() override {
      printf("==============================================================\n");
      printf("========     HiggsTemplateCrossSections Initialization     =========\n");
      printf("==============================================================\n");
      // check that the production mode has been set
      // if running in standalone Rivet the production mode is set through an env variable
      if (m_HiggsProdMode == HTXS::UNKNOWN) {
        char *pm_env = std::getenv("HIGGSPRODMODE");
        string pm(pm_env == nullptr ? "" : pm_env);
        if (pm == "GGF")
          m_HiggsProdMode = HTXS::GGF;
        else if (pm == "VBF")
          m_HiggsProdMode = HTXS::VBF;
        else if (pm == "WH")
          m_HiggsProdMode = HTXS::WH;
        else if (pm == "ZH")
          m_HiggsProdMode = HTXS::QQ2ZH;
        else if (pm == "QQ2ZH")
          m_HiggsProdMode = HTXS::QQ2ZH;
        else if (pm == "GG2ZH")
          m_HiggsProdMode = HTXS::GG2ZH;
        else if (pm == "TTH")
          m_HiggsProdMode = HTXS::TTH;
        else if (pm == "BBH")
          m_HiggsProdMode = HTXS::BBH;
        else if (pm == "TH")
          m_HiggsProdMode = HTXS::TH;
        else {
          MSG_WARNING("No HIGGSPRODMODE shell variable found. Needed when running Rivet stand-alone.");
        }
      }

      // Projections for final state particles
      const FinalState FS;
      declare(FS, "FS");

      // initialize the histograms with for each of the stages
      initializeHistos();
      m_sumw = 0.0;
      m_sumevents = 0;
      printf("==============================================================\n");
      printf("========             Higgs prod mode %d              =========\n", m_HiggsProdMode);
      printf("========          Sucessful Initialization           =========\n");
      printf("==============================================================\n");
    }

    // Perform the per-event analysis
    void analyze(const Event &event) override {
      // get the classification
      HiggsClassification cat = classifyEvent(event, m_HiggsProdMode);

      // Fill histograms: categorization --> linerize the categories
      const double weight = 1.0;
      m_sumw += weight;

      int F = cat.stage0_cat % 10, P = cat.stage1_cat_pTjet30GeV / 100;
      hist_stage0->fill(cat.stage0_cat / 10 * 2 + F, weight);

      // Stage 1 enum offsets for each production mode: GGF=12, VBF=6, WH= 5, QQ2ZH=5, GG2ZH=4, TTH=2, BBH=2, TH=2
      vector<int> offset({0, 1, 13, 19, 24, 29, 33, 35, 37, 39});
      int off = offset[P];
      hist_stage1_pTjet25->fill(cat.stage1_cat_pTjet25GeV % 100 + off, weight);
      hist_stage1_pTjet30->fill(cat.stage1_cat_pTjet30GeV % 100 + off, weight);

      // Stage 1_2 enum offsets for each production mode: GGF=17, VBF=11, WH= 6, QQ2ZH=6, GG2ZH=6, TTH=6, BBH=2, TH=2
      static vector<int> offset1_2({0, 1, 18, 29, 35, 41, 47, 53, 55, 57});
      int off1_2 = offset1_2[P];
      // Stage 1_2 Fine enum offsets for each production mode: GGF=28, VBF=25, WH= 16, QQ2ZH=16, GG2ZH=16, TTH=7, BBH=2, TH=2
      static vector<int> offset1_2f({0, 1, 29, 54, 70, 86, 102, 109, 111, 113});
      int off1_2f = offset1_2f[P];
      hist_stage1_2_pTjet25->fill(cat.stage1_2_cat_pTjet25GeV % 100 + off1_2, weight);
      hist_stage1_2_pTjet30->fill(cat.stage1_2_cat_pTjet30GeV % 100 + off1_2, weight);
      hist_stage1_2_fine_pTjet25->fill(cat.stage1_2_fine_cat_pTjet25GeV % 100 + off1_2f, weight);
      hist_stage1_2_fine_pTjet30->fill(cat.stage1_2_fine_cat_pTjet30GeV % 100 + off1_2f, weight);

      // Fill histograms: variables used in the categorization
      hist_pT_Higgs->fill(cat.higgs.pT(), weight);
      hist_y_Higgs->fill(cat.higgs.rapidity(), weight);
      hist_pT_V->fill(cat.V.pT(), weight);

      hist_Njets25->fill(cat.jets25.size(), weight);
      hist_Njets30->fill(cat.jets30.size(), weight);

      hist_isZ2vv->fill(cat.isZ2vvDecay, weight);

      // Jet variables. Use jet collection with pT threshold at 30 GeV
      if (!cat.jets30.empty())
        hist_pT_jet1->fill(cat.jets30[0].pt(), weight);
      if (cat.jets30.size() >= 2) {
        const FourMomentum &j1 = cat.jets30[0].momentum(), &j2 = cat.jets30[1].momentum();
        hist_deltay_jj->fill(std::abs(j1.rapidity() - j2.rapidity()), weight);
        hist_dijet_mass->fill((j1 + j2).mass(), weight);
        hist_pT_Hjj->fill((j1 + j2 + cat.higgs.momentum()).pt(), weight);
      }
    }

    void printClassificationSummary() {
      MSG_INFO(" ====================================================== ");
      MSG_INFO("      Higgs Template X-Sec Categorization Tool          ");
      MSG_INFO("                Status Code Summary                     ");
      MSG_INFO(" ====================================================== ");
      bool allSuccess = (m_sumevents == m_errorCount[HTXS::SUCCESS]);
      if (allSuccess)
        MSG_INFO("     >>>> All " << m_errorCount[HTXS::SUCCESS] << " events successfully categorized!");
      else {
        MSG_INFO("     >>>> " << m_errorCount[HTXS::SUCCESS] << " events successfully categorized");
        MSG_INFO("     >>>> --> the following errors occured:");
        MSG_INFO("     >>>> " << m_errorCount[HTXS::PRODMODE_DEFINED] << " had an undefined Higgs production mode.");
        MSG_INFO("     >>>> " << m_errorCount[HTXS::MOMENTUM_CONSERVATION] << " failed momentum conservation.");
        MSG_INFO("     >>>> " << m_errorCount[HTXS::HIGGS_IDENTIFICATION]
                              << " failed to identify a valid Higgs boson.");
        MSG_INFO("     >>>> " << m_errorCount[HTXS::HS_VTX_IDENTIFICATION]
                              << " failed to identify the hard scatter vertex.");
        MSG_INFO("     >>>> " << m_errorCount[HTXS::VH_IDENTIFICATION] << " VH: to identify a valid V-boson.");
        MSG_INFO("     >>>> " << m_errorCount[HTXS::TOP_W_IDENTIFICATION]
                              << " failed to identify valid Ws from top decay.");
      }
      MSG_INFO(" ====================================================== ");
      MSG_INFO(" ====================================================== ");
    }

    void finalize() override {
      printClassificationSummary();
      double sf = m_sumw > 0 ? 1.0 / m_sumw : 1.0;
      scale({hist_stage0,
             hist_stage1_pTjet25,
             hist_stage1_pTjet30,
             hist_stage1_2_pTjet25,
             hist_stage1_2_pTjet30,
             hist_stage1_2_fine_pTjet25,
             hist_stage1_2_fine_pTjet30,
             hist_Njets25,
             hist_Njets30,
             hist_pT_Higgs,
             hist_y_Higgs,
             hist_pT_V,
             hist_pT_jet1,
             hist_deltay_jj,
             hist_dijet_mass,
             hist_pT_Hjj},
            sf);
    }

    /*
     *  initialize histograms
     */

    void initializeHistos() {
      book(hist_stage0, "HTXS_stage0", 20, 0, 20);
      book(hist_stage1_pTjet25, "HTXS_stage1_pTjet25", 40, 0, 40);
      book(hist_stage1_pTjet30, "HTXS_stage1_pTjet30", 40, 0, 40);
      book(hist_stage1_2_pTjet25, "HTXS_stage1_2_pTjet25", 57, 0, 57);
      book(hist_stage1_2_pTjet30, "HTXS_stage1_2_pTjet30", 57, 0, 57);
      book(hist_stage1_2_fine_pTjet25, "HTXS_stage1_2_fine_pTjet25", 113, 0, 113);
      book(hist_stage1_2_fine_pTjet30, "HTXS_stage1_2_fine_pTjet30", 113, 0, 113);
      book(hist_pT_Higgs, "pT_Higgs", 80, 0, 400);
      book(hist_y_Higgs, "y_Higgs", 80, -4, 4);
      book(hist_pT_V, "pT_V", 80, 0, 400);
      book(hist_pT_jet1, "pT_jet1", 80, 0, 400);
      book(hist_deltay_jj, "deltay_jj", 50, 0, 10);
      book(hist_dijet_mass, "m_jj", 50, 0, 2000);
      book(hist_pT_Hjj, "pT_Hjj", 50, 0, 250);
      book(hist_Njets25, "Njets25", 10, 0, 10);
      book(hist_Njets30, "Njets30", 10, 0, 10);
      book(hist_isZ2vv, "isZ2vv", 2, 0, 2);
    }
    /// @}

    /*
     *    initialize private members used in the classification procedure
     */

  private:
    double m_sumw;
    size_t m_sumevents;
    HTXS::HiggsProdMode m_HiggsProdMode;
    std::map<HTXS::ErrorCode, size_t> m_errorCount;
    Histo1DPtr hist_stage0;
    Histo1DPtr hist_stage1_pTjet25, hist_stage1_pTjet30;
    Histo1DPtr hist_stage1_2_pTjet25, hist_stage1_2_pTjet30;
    Histo1DPtr hist_stage1_2_fine_pTjet25, hist_stage1_2_fine_pTjet30;
    Histo1DPtr hist_pT_Higgs, hist_y_Higgs;
    Histo1DPtr hist_pT_V, hist_pT_jet1;
    Histo1DPtr hist_deltay_jj, hist_dijet_mass, hist_pT_Hjj;
    Histo1DPtr hist_Njets25, hist_Njets30;
    Histo1DPtr hist_isZ2vv;
  };

  // the PLUGIN only needs to be decleared when running standalone Rivet
  // and causes compilation / linking issues if included in Athena / RootCore
  //check for Rivet environment variable RIVET_ANALYSIS_PATH
#ifdef RIVET_ANALYSIS_PATH
  // The hook for the plugin system
  DECLARE_RIVET_PLUGIN(HiggsTemplateCrossSections);
#endif

}  // namespace Rivet

#endif