int gammaJetHistogram(const TString configFile, const TString inputFile, const TString outputFile, const int nJobs, const int jobNum) {
    TH1::SetDefaultSumw2();

    std::cout << "running gammaJetHistogram()" << std::endl;
    std::cout << "configFile  = " << configFile.Data() << std::endl;
    std::cout << "inputFile   = " << inputFile.Data() << std::endl;
    std::cout << "outputFile  = " << outputFile.Data() << std::endl;

    InputConfiguration configInput = InputConfigurationParser::Parse(configFile.Data());
    CutConfiguration configCuts = CutConfigurationParser::Parse(configFile.Data());

    // input configuration
    if (!configInput.isValid) {
        std::cout << "Invalid input configuration" << std::endl;
        return 1;
    } else if (!configCuts.isValid) {
        std::cout << "Invalid cut configuration" << std::endl;
        return 1;
    }

    // input configuration
    // verbose about input configuration
    std::cout << "Input Configuration :" << std::endl;

    const int collision = configInput.proc[INPUT::kHISTOGRAM].i[INPUT::k_collisionType];
    const char* collisionName = getCollisionTypeName((COLL::TYPE)collision).c_str();
    std::cout << "collision = " << collisionName << std::endl;

    const bool isMC = collisionIsMC((COLL::TYPE)collision);
    const bool isHI = collisionIsHI((COLL::TYPE)collision);

    // observable bins
    std::vector<float> bins_pt[2];          // array of vectors for eta bins, each array element is a vector.
    std::vector<int>   bins_hiBin[2];       // array of vectors for hiBin bins, each array element is a vector.

    bins_pt[0] = ConfigurationParser::ParseListFloat(configCuts.proc[CUTS::kHISTOGRAM].obj[CUTS::kPHOTON].s[CUTS::PHO::k_bins_pt_gt]);
    bins_pt[1] = ConfigurationParser::ParseListFloat(configCuts.proc[CUTS::kHISTOGRAM].obj[CUTS::kPHOTON].s[CUTS::PHO::k_bins_pt_lt]);
    bins_hiBin[0] = ConfigurationParser::ParseListInteger(configCuts.proc[CUTS::kHISTOGRAM].obj[CUTS::kEVENT].s[CUTS::EVT::k_bins_hiBin_gt]);
    bins_hiBin[1] = ConfigurationParser::ParseListInteger(configCuts.proc[CUTS::kHISTOGRAM].obj[CUTS::kEVENT].s[CUTS::EVT::k_bins_hiBin_lt]);

    const int nSmearBins = configCuts.proc[CUTS::kSKIM].obj[CUTS::kJET].i[CUTS::JET::k_nSmearBins];

    // event cuts/weights
    // photon cuts
    const std::string trigger = configCuts.proc[CUTS::kHISTOGRAM].obj[CUTS::kPHOTON].s[CUTS::PHO::k_trigger_gammaJet].c_str();
    const float cut_phoHoverE = configCuts.proc[CUTS::kHISTOGRAM].obj[CUTS::kPHOTON].f[CUTS::PHO::k_phoHoverE];
    const float cut_phoSigmaIEtaIEta = configCuts.proc[CUTS::kHISTOGRAM].obj[CUTS::kPHOTON].f[CUTS::PHO::k_phoSigmaIEtaIEta];
    const float cut_sumIso = configCuts.proc[CUTS::kHISTOGRAM].obj[CUTS::kPHOTON].f[CUTS::PHO::k_sumIso];

    // jet cuts
    const std::string jetCollection = configCuts.proc[CUTS::kHISTOGRAM].obj[CUTS::kJET].s[CUTS::JET::k_jetCollection];
    const float cut_jetpt = configCuts.proc[CUTS::kHISTOGRAM].obj[CUTS::kJET].f[CUTS::JET::k_pt];
    const float cut_jeteta = configCuts.proc[CUTS::kHISTOGRAM].obj[CUTS::kJET].f[CUTS::JET::k_eta];
    const int   cut_jetID = configCuts.proc[CUTS::kHISTOGRAM].obj[CUTS::kJET].i[CUTS::JET::k_jetID];

    // gammaJet cuts
    const float cut_awayRange = configCuts.proc[CUTS::kHISTOGRAM].obj[CUTS::kGAMMAJET].f[CUTS::GJT::k_awayRange] * TMath::Pi();
    const float cut_dR = configCuts.proc[CUTS::kHISTOGRAM].obj[CUTS::kGAMMAJET].f[CUTS::GJT::k_dR];

    // process cuts
    const bool doPhotonEnergyScaleSystematics = configCuts.proc[CUTS::kHISTOGRAM].obj[CUTS::kPHOTON].i[CUTS::PHO::k_doPhotonEnergyScaleSystematics];
    const bool doElectronRejection = configCuts.proc[CUTS::kHISTOGRAM].obj[CUTS::kPHOTON].i[CUTS::PHO::k_doElectronRejection];
    const bool doPhotonIsolationSys = configCuts.proc[CUTS::kHISTOGRAM].obj[CUTS::kPHOTON].i[CUTS::PHO::k_doPhotonIsolationSys];
    const bool useCorrectedSumIso = configCuts.proc[CUTS::kHISTOGRAM].obj[CUTS::kPHOTON].i[CUTS::PHO::k_useCorrectedSumIso];

    const float smearingResJet = configCuts.proc[CUTS::kSKIM].obj[CUTS::kJET].f[CUTS::JET::k_smearingRes];
    const bool doSmearingRes = (smearingResJet > 0);
    const int sysUncFactor = 100;
    TRandom3 rand(12345);
    
    const int dphi_check = configCuts.proc[CUTS::kHISTOGRAM].obj[CUTS::kEVENT].i[CUTS::EVT::k_dphi_check];

    const int nBins_pt = bins_pt[0].size();         // assume <myvector>[0] and <myvector>[1] have the same size.
    const int nBins_hiBin = bins_hiBin[0].size();     // assume <myvector>[0] and <myvector>[1] have the same size.
    // verbose about cut configuration
    std::cout << "Cut Configuration :" << std::endl;
    std::cout << "nBins_pt = " << nBins_pt << std::endl;
    for (int i=0; i<nBins_pt; ++i)
        std::cout << Form("bins_pt[%d] = [%.1f, %.1f)", i, bins_pt[0][i], bins_pt[1][i]) << std::endl;
    std::cout << "nBins_hiBin = " << nBins_hiBin << std::endl;
    for (int i=0; i<nBins_hiBin; ++i)
        std::cout << Form("bins_hiBin[%d] = [%d, %d)", i, bins_hiBin[0][i], bins_hiBin[1][i]) << std::endl;

    std::cout << "trigger    = " << trigger.c_str() << std::endl;

    std::cout << "cut_phoHoverE             = " << cut_phoHoverE << std::endl;
    std::cout << "cut_phoSigmaIEtaIEta      = " << cut_phoSigmaIEtaIEta << std::endl;
    std::cout << "cut_sumIso                = " << cut_sumIso << std::endl;
    std::cout << "useCorrectedSumIso        = " << useCorrectedSumIso << std::endl;

    std::cout << "jetCollection             = " << jetCollection.c_str() << std::endl;
    std::cout << "cut_jetpt                 = " << cut_jetpt << std::endl;
    std::cout << "cut_jeteta                = " << cut_jeteta << std::endl;
    std::cout << "cut_jetID                 = " << cut_jetID << std::endl;

    std::cout << "cut_awayRange             = " << cut_awayRange << std::endl;
    std::cout << "cut_dR                    = " << cut_dR << std::endl;

    std::cout << "doPhotonEnergyScaleSystematics = " << doPhotonEnergyScaleSystematics << std::endl;
    std::cout << "doSmearingRes = " << doSmearingRes << std::endl;
    std::cout << "smearingResJet = " << smearingResJet << std::endl;

    /// Input Bookkeeping block ///
    TFile* input = TFile::Open(inputFile);

    TTree* tHlt = (TTree*)input->Get("hltTree");
    tHlt->SetBranchStatus("*", 0);
    tHlt->SetBranchStatus("HLT_HISinglePhoton40_Eta1p5_v1", 1);
    tHlt->SetBranchStatus("HLT_HISinglePhoton40_Eta1p5_v2", 1);
    tHlt->SetBranchStatus("HLT_HISinglePhoton50_Eta3p1_v1", 1);
    tHlt->SetBranchStatus("HLT_HISinglePhoton50_Eta3p1_v2", 1);
    tHlt->SetBranchStatus("HLT_HISinglePhoton40_Eta1p5ForPPRef_v1", 1);

    TTree* tPho = (TTree*)input->Get("EventTree");    // photons
    tPho->SetBranchStatus("*", 0);
    tPho->SetBranchStatus("phoEt", 1);
    tPho->SetBranchStatus("phoEtCorrected", 1);
    tPho->SetBranchStatus("phoEtCorrected_sys", 1);
    tPho->SetBranchStatus("phoEta", 1);
    tPho->SetBranchStatus("phoPhi", 1);
    tPho->SetBranchStatus("phoSigmaIEtaIEta_2012", 1);
    tPho->SetBranchStatus("pho_ecalClusterIsoR4", 1);
    tPho->SetBranchStatus("pho_hcalRechitIsoR4", 1);
    tPho->SetBranchStatus("pho_trackIsoR4PtCut20", 1);
    tPho->SetBranchStatus("pho_sumIsoCorrected", 1);
    tPho->SetBranchStatus("phoHoverE", 1);
    tPho->SetBranchStatus("phoE3x3", 1);
    tPho->SetBranchStatus("phoE5x5", 1);
    tPho->SetBranchStatus("phoE1x5", 1);
    tPho->SetBranchStatus("phoE2x5", 1);
    if (doElectronRejection) {
        tPho->SetBranchStatus("nEle", 1);
        tPho->SetBranchStatus("elePt", 1);
        tPho->SetBranchStatus("eleEta", 1);
        tPho->SetBranchStatus("elePhi", 1);
        tPho->SetBranchStatus("eleEoverP", 1);
    }
    if (isMC && doPhotonIsolationSys) {
        tPho->SetBranchStatus("nMC", 1);
        tPho->SetBranchStatus("mcPID", 1);
        tPho->SetBranchStatus("mcEta", 1);
        tPho->SetBranchStatus("mcPhi", 1);
        tPho->SetBranchStatus("mcCalIsoDR03", 1);
        tPho->SetBranchStatus("mcCalIsoDR04", 1);
        tPho->SetBranchStatus("pho_genMatchedIndex", 1);
    }

    TTree* tJet = (TTree*)input->Get(jetCollection.c_str());
    if (!tJet) {
        std::cout << "following jet collection is not found in the input file : " << jetCollection.c_str() << std::endl;
        std::cout << "exiting" << std::endl;
        return 1;
    }

    tJet->SetBranchStatus("*", 0);
    tJet->SetBranchStatus("nref", 1);
    tJet->SetBranchStatus("jtpt", 1);
    tJet->SetBranchStatus("jtpt_smeared", 1);
    // tJet->SetBranchStatus("jteta", 1);

    TTree* tHiEvt = (TTree*)input->Get("HiEvt");       // HiEvt tree will be placed in PP forest as well.

    ggHiNtuplizer pho;
    pho.setupTreeForReading(tPho);

    Jets jet;
    jet.setupTreeForReading(tJet);

    hiEvt evt;
    evt.setupTreeForReading(tHiEvt);

    Int_t HLT_HISinglePhoton40_Eta1p5_v1, HLT_HISinglePhoton40_Eta1p5_v2;
    Int_t HLT_HISinglePhoton50_Eta3p1_v1, HLT_HISinglePhoton50_Eta3p1_v2;
    Int_t HLT_HISinglePhoton40_Eta1p5ForPPRef_v1;
    tHlt->SetBranchAddress("HLT_HISinglePhoton40_Eta1p5_v1", &HLT_HISinglePhoton40_Eta1p5_v1);
    tHlt->SetBranchAddress("HLT_HISinglePhoton40_Eta1p5_v2", &HLT_HISinglePhoton40_Eta1p5_v2);
    tHlt->SetBranchAddress("HLT_HISinglePhoton50_Eta3p1_v1", &HLT_HISinglePhoton50_Eta3p1_v1);
    tHlt->SetBranchAddress("HLT_HISinglePhoton50_Eta3p1_v2", &HLT_HISinglePhoton50_Eta3p1_v2);
    tHlt->SetBranchAddress("HLT_HISinglePhoton40_Eta1p5ForPPRef_v1", &HLT_HISinglePhoton40_Eta1p5ForPPRef_v1);

    TTree* gammaJetTree[nSmearBins+1];
    gammaJetTree[0] = (TTree*)input->Get(Form("gamma_%s", jetCollection.c_str()));
    for (int i=0; i<nSmearBins; i++)
        gammaJetTree[i+1] = (TTree*)input->Get(Form("gamma_%s_smearBin%i", jetCollection.c_str(), i));

    GammaJet gammaJet[nSmearBins+1];
    for (int i=0; i<nSmearBins+1; i++)
        gammaJet[i].setupGammaJetTree(gammaJetTree[i]);

    TTree* jetTreeMB = 0;
    TTree* gammaJetTreeMB = 0;
    // check the existence of HI specific trees in "gammaJetSkim.root" file
    bool hasJetsMB = false;
    bool hasGammaJetMB = false;
    if (isHI) {
        input->GetObject(Form("%sMB", jetCollection.c_str()), jetTreeMB);
        input->GetObject(Form("gamma_%sMB", jetCollection.c_str()), gammaJetTreeMB);

        if (jetTreeMB)
            hasJetsMB = true;
        if (gammaJetTreeMB)
            hasGammaJetMB = true;
    }

    Jets jetMB;
    GammaJet gammaJetMB;
    if (hasJetsMB && hasGammaJetMB) {
        jetTreeMB->SetBranchStatus("*", 0);
        jetTreeMB->SetBranchStatus("jtpt", 1);
        jetTreeMB->SetBranchStatus("nref", 1);
        // jetTreeMB->SetBranchStatus("jteta", 1);

        jetMB.setupTreeForReading(jetTreeMB);
        gammaJetMB.setupGammaJetTree(gammaJetTreeMB);
    }

    // smearing set up block
    // ONLY used for JER systematic
    // WARNING make super sure this matches the values in gammaJetSkim.C !!
    // otherwise JER systematic will be off
    jetCorrector resolutionJetSmear[6];

    // smear 0-10 %
    std::vector<double> CSN_HI_cent0010 = {0.06, 1.23, 8.38};
    std::vector<double> CSN_phi_HI_cent0010 = {-3.18781/10000000, 0.125911, 2.23898};
    // smear 10-30 %
    std::vector<double> CSN_HI_cent1030 = {0.06, 1.23, 5.88};
    std::vector<double> CSN_phi_HI_cent1030 = {1.14344/100000, 0.179847, 1.56128};
    // smear 30-50 %
    std::vector<double> CSN_HI_cent3050 = {0.06, 1.23, 3.24};
    std::vector<double> CSN_phi_HI_cent3050 = {0.0145775, 0.1222, 1.21751};
    // smear 50-100 %
    std::vector<double> CSN_HI_cent50100 = {0.06, 1.23, 0};
    std::vector<double> CSN_phi_HI_cent50100 = {-0.0073078, 0.168879, 0.798885};

    // 0-30 and 30-100 are necessary only for smeared pp JER computation
    // smear 0-30 %
    std::vector<double> CSN_HI_cent0030 = {0.06, 1.23, 7.38};
    std::vector<double> CSN_phi_HI_cent0030 = {-1.303/1000000, 0.1651, 1.864};
    // smear 30-100 %
    std::vector<double> CSN_HI_cent30100 = {0.06, 1.23, 2.1};
    std::vector<double> CSN_phi_HI_cent30100 = {-2.013/100000000, 0.1646, 1.04};


    for (int i=0; i<6; ++i) {
        resolutionJetSmear[i].rand = rand;

        switch (i) {
        case 0: //0-10
            resolutionJetSmear[i].CSN_HI = CSN_HI_cent0010;
            resolutionJetSmear[i].CSN_phi_HI = CSN_phi_HI_cent0010;
            break;
        case 1: //10-30
            resolutionJetSmear[i].CSN_HI = CSN_HI_cent1030;
            resolutionJetSmear[i].CSN_phi_HI = CSN_phi_HI_cent1030;
            break;
        case 2: //30-50
            resolutionJetSmear[i].CSN_HI = CSN_HI_cent3050;
            resolutionJetSmear[i].CSN_phi_HI = CSN_phi_HI_cent3050;
            break;
        case 3: //50-100
            resolutionJetSmear[i].CSN_HI = CSN_HI_cent50100;
            resolutionJetSmear[i].CSN_phi_HI = CSN_phi_HI_cent50100;
            break;
        case 4: // 0-30, for smeared pp only
            resolutionJetSmear[i].CSN_HI = CSN_HI_cent0030;
            resolutionJetSmear[i].CSN_phi_HI = CSN_phi_HI_cent0030;
            break;
        case 5: // 30-100, for smeared pp only
            resolutionJetSmear[i].CSN_HI = CSN_HI_cent30100;
            resolutionJetSmear[i].CSN_phi_HI = CSN_phi_HI_cent30100;
            break;
        }
    }

    /// End Input Bookkeeping block //

    TFile* output = TFile::Open(outputFile, "RECREATE");
    // histograms will be put under a directory whose name is the type of the collision
    if (!output->GetKey(collisionName))
        output->mkdir(collisionName, Form("input file is %s", inputFile.Data()));
    output->cd(collisionName);
    std::cout << "histograms will be put under directory : " << collisionName << std::endl;

    TTree *configTree = setupConfigurationTreeForWriting(configCuts);

    int nCorrHist = correlationHistNames.size();
    correlationHist corrHists[nCorrHist][nBins_pt][nBins_hiBin];

    // prepare histogram names for xjg, abs(dphi) and jet pt
    for (int iHist=0; iHist<nCorrHist; ++iHist) {
        for (int i=0; i<nBins_pt; ++i) {
            for (int j=0; j<nBins_hiBin; ++j) {
                corrHists[iHist][i][j].name = Form("%s_ptBin%d_hiBin%d", correlationHistNames[iHist].c_str(), i, j);

                for (int iCorr = 0; iCorr < CORR::kN_CORRFNC; ++iCorr) {
                    for (int jCorr = 0; jCorr < CORR::kN_CORRFNC; ++jCorr) {
                        std::string subHistName = Form("%s_ptBin%d_hiBin%d_%s_%s", correlationHistNames[iHist].c_str(), i, j,
                                                       CORR::CORR_PHO_LABELS[iCorr].c_str(), CORR::CORR_JET_LABELS[jCorr].c_str());
                        corrHists[iHist][i][j].h1D_name[iCorr][jCorr] = subHistName.c_str();
                        corrHists[iHist][i][j].h1D[iCorr][jCorr] = new TH1D(Form("h1D_%s", subHistName.c_str()), "",
                                nBinsx[iHist], xlow[iHist], xup[iHist]);

                        corrHists[iHist][i][j].h1D_titleX[iCorr][jCorr] = correlationHistTitleX[iHist].c_str();
                        corrHists[iHist][i][j].h1D_titleY_final_norm[iCorr][jCorr] = correlationHistTitleY_final_normalized[iHist].c_str();
                    }
                }
            }
        }
    }

    // histograms to store the number of photon events, not photon-Jet event
    // those histograms have a single bin whose content is the number of photon events
    // they are just a tool to store number.
    TH1D* h_nPho[nBins_pt][nBins_hiBin][2];
    for (int i=0; i<nBins_pt; ++i) {
        for (int j=0; j<nBins_hiBin; ++j) {
            std::string histNamePhoRAW = Form("h_nPho_ptBin%d_hiBin%d_%s", i, j, CORR::CORR_PHO_LABELS[CORR::kRAW].c_str());
            h_nPho[i][j][CORR::kRAW] = new TH1D(histNamePhoRAW.c_str(), "", 1, 0, 1);

            std::string histNamePhoBKG = Form("h_nPho_ptBin%d_hiBin%d_%s", i, j, CORR::CORR_PHO_LABELS[CORR::kBKG].c_str());
            h_nPho[i][j][CORR::kBKG] = new TH1D(histNamePhoBKG.c_str(), "", 1, 0, 1);
        }
    }

    // selection for jet regions
    // jet from bkg region are already separated from raw region.
    // no additional selection for jets. just use different trees.
    std::cout << "####################" << std::endl;
    std::cout << "gammaJetTree->GetEntries() = " << gammaJetTree[0]->GetEntries() << std::endl;
    if (trigger.compare("") != 0 && !isMC)
        std::cout << "gammaJetTree->GetEntries(trigger==1) = " << tHlt->GetEntries(Form("%s == 1", trigger.c_str())) << std::endl;
    else
        std::cout << "gammaJetTree->GetEntries(trigger==1) is skipped because either no trigger is specified or the data is coming from MC." << std::endl;
    std::cout << "####################" << std::endl;

    long long nentries = gammaJetTree[0]->GetEntries();
    long long firstEntry = 0;
    long long lastEntry = nentries;
    std::cout << "Total Entries: " << nentries << std::endl;

    if (nJobs != -1) {
        if (jobNum >= nJobs) {
            std::cout << "jobNum > nJobs, invalid configuration, aborting" << std::endl;
            return 1;
        }

        firstEntry = floor(nentries/nJobs)*jobNum;
        lastEntry = floor(nentries/nJobs)*(jobNum+1);
        if (jobNum == nJobs-1)
            lastEntry = nentries;

        std::cout << "For this job " << jobNum << std::endl;
        std::cout << "First Entry: " << firstEntry << std::endl;
        std::cout << "Final Entry: " << lastEntry << std::endl;
    }

    for (long long jentry = firstEntry; jentry < lastEntry; jentry++) {
        if (jentry % 2000 == 0 && nJobs == -1)
            printf("current entry = %lli out of %lli : %3f%%\n", jentry, nentries, (double)jentry/nentries*100);

        tHlt->GetEntry(jentry);
        // event selection
        if (!isMC && !HLT_HISinglePhoton40_Eta1p5_v1)
            continue;
        if (isHI && isMC && !HLT_HISinglePhoton40_Eta1p5_v2)
            continue;
        if (!isHI && isMC && !HLT_HISinglePhoton40_Eta1p5ForPPRef_v1)
            continue;

        tPho->GetEntry(jentry);
        gammaJetTree[0]->GetEntry(jentry);

        // eta cut moved to skim step
        // if (TMath::Abs((*pho.phoEta)[gammaJet[0].phoIdx]) > 1.44) continue;

        // noise cut
        if (((*pho.phoE3x3)[gammaJet[0].phoIdx]/(*pho.phoE5x5)[gammaJet[0].phoIdx] > 2./3.-0.03 &&
             (*pho.phoE3x3)[gammaJet[0].phoIdx]/(*pho.phoE5x5)[gammaJet[0].phoIdx] < 2./3.+0.03) &&
            ((*pho.phoE1x5)[gammaJet[0].phoIdx]/(*pho.phoE5x5)[gammaJet[0].phoIdx] > 1./3.-0.03 &&
             (*pho.phoE1x5)[gammaJet[0].phoIdx]/(*pho.phoE5x5)[gammaJet[0].phoIdx] < 1./3.+0.03) &&
            ((*pho.phoE2x5)[gammaJet[0].phoIdx]/(*pho.phoE5x5)[gammaJet[0].phoIdx] > 2./3.-0.03 &&
             (*pho.phoE2x5)[gammaJet[0].phoIdx]/(*pho.phoE5x5)[gammaJet[0].phoIdx] < 2./3.+0.03)) continue;

        // isolation cut
        if (useCorrectedSumIso) {
            if ((*pho.pho_sumIsoCorrected)[gammaJet[0].phoIdx] > cut_sumIso) {
                continue;
            }
        } else if (((*pho.pho_ecalClusterIsoR4)[gammaJet[0].phoIdx] +
                    (*pho.pho_hcalRechitIsoR4)[gammaJet[0].phoIdx] +
                    (*pho.pho_trackIsoR4PtCut20)[gammaJet[0].phoIdx]) > cut_sumIso) {
            continue;
        }

        if ((*pho.phoHoverE)[gammaJet[0].phoIdx] > 0.1)
            continue;

        bool isSignalPho = ((*pho.phoSigmaIEtaIEta_2012)[gammaJet[0].phoIdx] < cut_phoSigmaIEtaIEta);
        bool isBkgPho = ((*pho.phoSigmaIEtaIEta_2012)[gammaJet[0].phoIdx] > 0.011 &&
                         (*pho.phoSigmaIEtaIEta_2012)[gammaJet[0].phoIdx] < 0.017);

        if (!(isSignalPho || isBkgPho))
            continue;

        int phoType = isSignalPho ? CORR::kRAW : CORR::kBKG;

        // reco electron rejection part
        bool isEle = false;
        if (doElectronRejection) {
            float eleEpTemp = 100.0;
            for (int ie=0; ie<pho.nEle; ++ie) {
                if ((*pho.elePt)[ie] < 10)
                    continue;
                if (abs((*pho.eleEta)[ie] - (*pho.phoEta)[gammaJet[0].phoIdx]) > 0.03) // deta
                    continue;
                if (abs(getDPHI((*pho.elePhi)[ie], (*pho.phoPhi)[gammaJet[0].phoIdx])) > 0.03) // dphi
                    continue;
                if (eleEpTemp < pho.eleEoverP->at(ie))
                    continue;

                isEle = true;
                break;
            }
        }
        if (isEle)
            continue;

        // photon isolation systematic part (gen matching & genIso condition)
        if (isMC && doPhotonIsolationSys) {
            if (!(((*pho.pho_genMatchedIndex)[gammaJet[0].phoIdx]!=-1) && (pho.mcCalIsoDR04->at((*pho.pho_genMatchedIndex)[gammaJet[0].phoIdx])<5.0))) {
                // std::cout << "failedGenIso : mcCalIso = " << (pho.mcCalIsoDR04->at((*pho.pho_genMatchedIndex)[gammaJet[0].phoIdx]))<< std::endl;
                continue;
            }
        }

        tJet->GetEntry(jentry);
        tHiEvt->GetEntry(jentry);

        float weight = isMC ? evt.weight : 1;
        float phoEt = doPhotonEnergyScaleSystematics ? (*pho.phoEtCorrected_sys)[gammaJet[0].phoIdx] : (*pho.phoEtCorrected)[gammaJet[0].phoIdx];

        // handle nEntriesPho separate from jet loop
        for (int i=0; i<nBins_pt; ++i) {
            if (phoEt <  bins_pt[0][i] || phoEt >= bins_pt[1][i])
                continue;
            for (int j=0; j<nBins_hiBin; ++j) {
                if (isHI && (evt.hiBin <  bins_hiBin[0][j] || evt.hiBin >= bins_hiBin[1][j]))
                    continue;
                for (int iHist = 0; iHist < nCorrHist; iHist++)
                    corrHists[iHist][i][j].nEntriesPho[phoType][CORR::kRAW] += weight;
            }
        }

        if(doSmearingRes)
            weight /= sysUncFactor;

        for (int j=0; j<nBins_hiBin; ++j) {
            int smearBin = 0;
            if (isHI) {
                if (evt.hiBin <  bins_hiBin[0][j] || evt.hiBin >= bins_hiBin[1][j])
                    continue;
            } else {
                if (j) {
                    // change smearBin to select the smeared jtpt and jtphi corresponding to the centrality bin
                    // 0 is unsmeared jtpt
                    smearBin = j;
                }

                if (smearBin)
                    gammaJetTree[smearBin]->GetEntry(jentry);
            }

            for (int ijet = 0; ijet < jet.nref; ijet++) {
                int multiplyJets = 1;
                if(doSmearingRes)
                    multiplyJets = sysUncFactor;
                for(int iResSmear = 0; iResSmear < multiplyJets; iResSmear++){
                    float jetpt = (*jet.jtpt_smeared)[smearBin][ijet];
                    float smearFactor = 1;
                    if(doSmearingRes) {
                        //smearFactor = rand.Gaus(1,smearingResJet);
                        float SF = 1 + smearingResJet;
                        int resolutionBin = 0;
                        if(isHI) {
                            resolutionBin = getResolutionBin(evt.hiBin);
                        } else {
                            resolutionBin = getResolutionBinPP(smearBin);
                        }
                        float initialResolution = resolutionJetSmear[resolutionBin].getResolutionHI(jetpt);
                        smearFactor = rand.Gaus(1, SF * initialResolution * sqrt(SF*SF - 1));
                        jetpt *= smearFactor;
                    }
                    // jet cuts
                    // jteta cut moved to skim
                    // if (TMath::Abs(jet.jteta[ijet]) > cut_jeteta) continue;
                    if ((*gammaJet[0].jetID)[ijet] < cut_jetID)
                        continue;
                    if (jetpt < cut_jetpt)
                        continue;
                    if ((*gammaJet[smearBin].dR)[ijet] < cut_dR)
                        continue;

                    for (int i=0; i<nBins_pt; ++i) {
                        if (phoEt <  bins_pt[0][i] || phoEt >= bins_pt[1][i])
                            continue;

                        // fill histograms
                        // dphi = 1
                        corrHists[1][i][j].h1D[phoType][CORR::kRAW]->Fill(TMath::Abs((*gammaJet[smearBin].dphi)[ijet]), weight);
                        corrHists[1][i][j].nEntries[phoType][CORR::kRAW] += weight;
                        
                        //apply dphi cuts now
                        if (TMath::Abs((*gammaJet[smearBin].dphi)[ijet]) <= cut_awayRange)
                            continue;
                        // xjg = 0
                        // jtpt = 2
                        float xjg = doPhotonEnergyScaleSystematics ? (*gammaJet[smearBin].xjgCorrected_sys)[ijet] : (*gammaJet[smearBin].xjgCorrected)[ijet];
                        if(doSmearingRes){
                            xjg *= smearFactor;
                        }
                        corrHists[0][i][j].h1D[phoType][CORR::kRAW]->Fill(xjg, weight);
                        corrHists[0][i][j].nEntries[phoType][CORR::kRAW] += weight;
                        corrHists[2][i][j].h1D[phoType][CORR::kRAW]->Fill(jetpt, weight);
                        corrHists[2][i][j].nEntries[phoType][CORR::kRAW] += weight;
                    }
                }
            }
        }

        if (dphi_check && isHI) {
            for (int ijet = 0; ijet < jet.nref; ijet++) {
                for (int j=0; j<nBins_hiBin; ++j) {
                    if (evt.hiBin <  bins_hiBin[0][j] || evt.hiBin >= bins_hiBin[1][j])
                        continue;
                    if (jet.jtpt[ijet] < cut_jetpt)
                        continue;
                    if ((*gammaJet[0].dR)[ijet] < cut_dR)
                        continue;

                    for (int i=0; i<nBins_pt; ++i) {
                        if ((*pho.phoEtCorrected)[gammaJet[0].phoIdx] <  bins_pt[0][i] ||
                            (*pho.phoEtCorrected)[gammaJet[0].phoIdx] >= bins_pt[1][i]) continue;

                        float lower_sideband = 1;
                        float upper_sideband = TMath::Pi()/2;
                        float sideband_width = upper_sideband - lower_sideband;

                        // select dphi sideband region
                        if (TMath::Abs((*gammaJet[0].dphi)[ijet]) > upper_sideband || TMath::Abs((*gammaJet[0].dphi)[ijet]) < lower_sideband)
                            continue;

                        corrHists[1][i][j].h1D[phoType][CORR::kBKG]->Fill((TMath::Abs((*gammaJet[0].dphi)[ijet])-lower_sideband)*(TMath::Pi()/sideband_width), weight*40*(TMath::Pi()/sideband_width));
                        corrHists[1][i][j].nEntries[phoType][CORR::kBKG] += weight*40*(TMath::Pi()/sideband_width);

                        corrHists[0][i][j].h1D[phoType][CORR::kBKG]->Fill((*gammaJet[0].xjgCorrected)[ijet], weight*40*(TMath::Pi()/8/sideband_width));
                        corrHists[0][i][j].nEntries[phoType][CORR::kBKG] += weight*40*(TMath::Pi()/8/sideband_width);
                        corrHists[2][i][j].h1D[phoType][CORR::kBKG]->Fill(jet.jtpt[ijet], weight*40*(TMath::Pi()/8/sideband_width));
                        corrHists[2][i][j].nEntries[phoType][CORR::kBKG] += weight*40*(TMath::Pi()/8/sideband_width);
                    }
                }
            }
        } else {
            if (hasJetsMB && hasGammaJetMB) {
                jetTreeMB->GetEntry(jentry);
                gammaJetTreeMB->GetEntry(jentry);

                for (int ijet = 0; ijet < jetMB.nref; ijet++) {
                    int multiplyJets = 1;
                    if(doSmearingRes)
                        multiplyJets = sysUncFactor;
                    for(int iResSmear = 0; iResSmear < multiplyJets; iResSmear++){
                        float jetpt = jetMB.jtpt[ijet];
                        float smearFactor = 1;
                        if(doSmearingRes) {
                            //smearFactor = rand.Gaus(1,smearingResJet);
                            float SF = 1 + smearingResJet;
                            int resolutionBin = 0;
                            resolutionBin = getResolutionBin(evt.hiBin);
                            float initialResolution = resolutionJetSmear[resolutionBin].getResolutionHI(jetpt);
                            smearFactor = rand.Gaus(1, SF * initialResolution * sqrt(SF*SF - 1));
                            jetpt *= smearFactor;
                        }

                        // jet cuts
                        if ((*gammaJetMB.dR)[ijet] < cut_dR)
                            continue;
                        // jteta cut moved to skim
                        // if (TMath::Abs(jetMB.jteta[ijet]) > cut_jeteta) continue;
                        if (jetpt < cut_jetpt)
                            continue;
                        if ((*gammaJetMB.jetID)[ijet] < cut_jetID)
                            continue;

                        for (int i=0; i<nBins_pt; ++i) {
                            if ((*pho.phoEtCorrected)[gammaJetMB.phoIdx] <  bins_pt[0][i] ||
                                (*pho.phoEtCorrected)[gammaJetMB.phoIdx] >= bins_pt[1][i]) continue;
                            for (int j=0; j<nBins_hiBin; ++j) {
                                if (evt.hiBin <  bins_hiBin[0][j] || evt.hiBin >= bins_hiBin[1][j])
                                    continue;

                                // fill histograms
                                // dphi = 1
                                corrHists[1][i][j].h1D[phoType][CORR::kBKG]->Fill(TMath::Abs((*gammaJetMB.dphi)[ijet]), weight);
                                corrHists[1][i][j].nEntries[phoType][CORR::kBKG] += weight;
                                // apply dphi cuts now
                                if (TMath::Abs((*gammaJetMB.dphi)[ijet]) <= cut_awayRange)
                                    continue;
                                // xjg = 0
                                // jtpt = 2
                                float xjg = doPhotonEnergyScaleSystematics ? (*gammaJetMB.xjgCorrected_sys)[ijet] : (*gammaJetMB.xjgCorrected)[ijet];
                                if(doSmearingRes){
                                    xjg *= smearFactor;
                                }
                                corrHists[0][i][j].h1D[phoType][CORR::kBKG]->Fill(xjg, weight);
                                corrHists[0][i][j].nEntries[phoType][CORR::kBKG] += weight;
                                corrHists[2][i][j].h1D[phoType][CORR::kBKG]->Fill(jetpt, weight);
                                corrHists[2][i][j].nEntries[phoType][CORR::kBKG] += weight;
                            }
                        }
                    }
                }
            }
        }
    }

    /// Histogram arithmetic (no reading I/O)
    for (int iHist = 0; iHist < nCorrHist; iHist++) {
        for (int i=0; i<nBins_pt; ++i) {
            for (int j=0; j<nBins_hiBin; ++j) {
                corrHists[iHist][i][j].nEntriesPho[CORR::kRAW][CORR::kBKG] = corrHists[iHist][i][j].nEntriesPho[CORR::kRAW][CORR::kRAW];
                corrHists[iHist][i][j].nEntriesPho[CORR::kBKG][CORR::kBKG] = corrHists[iHist][i][j].nEntriesPho[CORR::kBKG][CORR::kRAW];

                if (h_nPho[i][j][CORR::kRAW]->GetBinContent(1) == 0) {
                    h_nPho[i][j][CORR::kRAW]->SetBinContent(1, corrHists[iHist][i][j].nEntriesPho[CORR::kRAW][CORR::kRAW]);
                    h_nPho[i][j][CORR::kRAW]->Write("", TObject::kOverwrite);
                }

                if (h_nPho[i][j][CORR::kBKG]->GetBinContent(1) == 0) {
                    h_nPho[i][j][CORR::kBKG]->SetBinContent(1, corrHists[iHist][i][j].nEntriesPho[CORR::kBKG][CORR::kRAW]);
                    h_nPho[i][j][CORR::kBKG]->Write("", TObject::kOverwrite);
                }

                std::cout << "nEntries[CORR::kRAW][CORR::kRAW] = " << corrHists[iHist][i][j].nEntries[CORR::kRAW][CORR::kRAW] << std::endl;
                std::cout << "nEntries[CORR::kBKG][CORR::kRAW] = " << corrHists[iHist][i][j].nEntries[CORR::kBKG][CORR::kRAW] << std::endl;
                std::cout << "nEntriesPho[CORR::kRAW][CORR::kRAW] = " << corrHists[iHist][i][j].nEntriesPho[CORR::kRAW][CORR::kRAW] << std::endl;
                std::cout << "nEntriesPho[CORR::kBKG][CORR::kRAW] = " << corrHists[iHist][i][j].nEntriesPho[CORR::kBKG][CORR::kRAW] << std::endl;

                std::string histoTitle;
                if (bins_pt[1][i] == 9999 && bins_hiBin[0][j] <= 0 && bins_hiBin[1][j] >= 200)
                    histoTitle = Form("%s , p^{#gamma}_{T} > %.0f GeV/c", collisionName , bins_pt[0][i]);
                else if (bins_hiBin[0][j] <= 0 && bins_hiBin[1][j] >= 200)
                    histoTitle = Form("%s , %.0f < p^{#gamma}_{T} < %.0f GeV/c", collisionName , bins_pt[0][i], bins_pt[1][i]);
                else if (bins_pt[1][i] == 9999)
                    histoTitle = Form("%s , p^{#gamma}_{T} > %.0f GeV/c, %d-%d %% ", collisionName , bins_pt[0][i], bins_hiBin[0][j]/2, bins_hiBin[1][j]/2);
                else
                    histoTitle = Form("%s , %.0f < p^{#gamma}_{T} < %.0f GeV/c, %d-%d %% ", collisionName , bins_pt[0][i], bins_pt[1][i], bins_hiBin[0][j]/2, bins_hiBin[1][j]/2);

                // histograms for RAW and BKG regions
                for (int iCorr = 0; iCorr < CORR::kN_CORRFNC-1; ++iCorr) {
                    for (int jCorr = 0; jCorr < CORR::kN_CORRFNC-1; ++jCorr) {
                        if (jCorr == CORR::kBKG && !isHI) continue;      // no jet background for non-HI

                        std::string titleX = corrHists[iHist][i][j].h1D_titleX[iCorr][jCorr].c_str();
                        corrHists[iHist][i][j].h1D[iCorr][jCorr]->SetTitle(Form("%s;%s;%s", histoTitle.c_str(), titleX.c_str(), "Entries"));
                        corrHists[iHist][i][j].h1D[iCorr][jCorr]->SetMarkerStyle(kFullCircle);
                        corrHists[iHist][i][j].h1D[iCorr][jCorr]->SetMarkerColor(kBlack);

                        // histogram name excluding the "h1D" prefix
                        std::string tmpH1D_name = corrHists[iHist][i][j].h1D_name[iCorr][jCorr].c_str();
                        std::string tmpHistName = corrHists[iHist][i][j].h1D[iCorr][jCorr]->GetName();

                        corrHists[iHist][i][j].h1D[iCorr][jCorr]->Write("", TObject::kOverwrite);
                    }
                }
            }
        }
    }

    configTree->Write("", TObject::kOverwrite);

    output->Write("", TObject::kOverwrite);
    input->Close();
    output->Close();

    return 0;
}
void photonRaaSkim(const TString configFile, const TString inputFile, const TString outputFile, COLL::TYPE colli)
{
    std::cout<<"running photonRaaSkim()"<<std::endl;
    std::cout<<"configFile  = "<< configFile.Data() <<std::endl;
    std::cout<<"inputFile   = "<< inputFile.Data() <<std::endl;
    std::cout<<"outputFile  = "<< outputFile.Data() <<std::endl;

    InputConfiguration configInput = InputConfigurationParser::Parse(configFile.Data());
    CutConfiguration configCuts = CutConfigurationParser::Parse(configFile.Data());

    // input configuration
/*    int collisionType;
    if (configInput.isValid) {
        collisionType = configInput.proc[INPUT::kSKIM].i[INPUT::k_collisionType];
    }
    else {
        collisionType = COLL::kPP;
    }
*/
    // verbose about input configuration
    int collisionType = colli;
    std::cout<<"Input Configuration :"<<std::endl;
    std::cout << "collisionType = " << collisionType << std::endl;
    const char* collisionName =  getCollisionTypeName((COLL::TYPE)collisionType).c_str();
    std::cout << "collision = " << collisionName << std::endl;

    // cut configuration
    float cut_vz;
    int cut_pcollisionEventSelection;
    int cut_pPAprimaryVertexFilter;
    int cut_pBeamScrapingFilter;

    float cutPhoEt;
    float cutPhoEta;

    if (configCuts.isValid) {
        cut_vz = configCuts.proc[CUTS::kSKIM].obj[CUTS::kEVENT].f[CUTS::EVT::k_vz];
        cut_pcollisionEventSelection = configCuts.proc[CUTS::kSKIM].obj[CUTS::kEVENT].i[CUTS::EVT::k_pcollisionEventSelection];
        cut_pPAprimaryVertexFilter = configCuts.proc[CUTS::kSKIM].obj[CUTS::kEVENT].i[CUTS::EVT::k_pPAprimaryVertexFilter];
        cut_pBeamScrapingFilter = configCuts.proc[CUTS::kSKIM].obj[CUTS::kEVENT].i[CUTS::EVT::k_pBeamScrapingFilter];

        cutPhoEt = configCuts.proc[CUTS::kSKIM].obj[CUTS::kPHOTON].f[CUTS::PHO::k_et];
        cutPhoEta = configCuts.proc[CUTS::kSKIM].obj[CUTS::kPHOTON].f[CUTS::PHO::k_eta];

    }
    else {
        cut_vz = 15;
        cut_pcollisionEventSelection = 1;
        cut_pPAprimaryVertexFilter = 1;
        cut_pBeamScrapingFilter = 1;

        cutPhoEt = 15;
        cutPhoEta = 1.44;
    }

    bool isMC = collisionIsMC((COLL::TYPE)collisionType);
    bool isHI = collisionIsHI((COLL::TYPE)collisionType);
    bool isPP = collisionIsPP((COLL::TYPE)collisionType);

    // verbose about cut configuration
    std::cout<<"Cut Configuration :"<<std::endl;
    std::cout<<"cut_vz = "<< cut_vz <<std::endl;
    if (isHI) {
        std::cout<<"cut_pcollisionEventSelection = "<< cut_pcollisionEventSelection <<std::endl;
    }
    else {   // PP
        std::cout<<"cut_pPAprimaryVertexFilter = "<< cut_pPAprimaryVertexFilter <<std::endl;
        std::cout<<"cut_pBeamScrapingFilter = "<< cut_pBeamScrapingFilter <<std::endl;
    }

    std::cout<<"cutPhoEt  = "<<cutPhoEt<<std::endl;
    std::cout<<"cutPhoEta = "<<cutPhoEta<<std::endl;

    std::vector<std::string> inputFiles = InputConfigurationParser::ParseFiles(inputFile.Data());
    std::cout<<"input ROOT files : num = "<<inputFiles.size()<< std::endl;
    std::cout<<"#####"<< std::endl;
    for (std::vector<std::string>::iterator it = inputFiles.begin() ; it != inputFiles.end(); ++it) {
        std::cout<<(*it).c_str()<< std::endl;
    }
    std::cout<<"##### END #####"<< std::endl;

    TChain* treeHLT   = new TChain("hltanalysis/HltTree");
    TChain* treeggHiNtuplizer  = new TChain("ggHiNtuplizer/EventTree");
    TChain* treeHiEvt = new TChain("hiEvtAnalyzer/HiTree");
    TChain* treeSkim  = new TChain("skimanalysis/HltTree");
    TChain* treeHiForestInfo = new TChain("HiForest/HiForestInfo");
    TChain* treeGen=0;
    if(isMC) treeGen  = new TChain("HiGenParticleAna/hi");

    // pthatWeight Calculation block! 
    int nPthat = 5;
    float pthatCut[nPthat+1];
    const char* lowestPthatFileName="";
    int nfiles = 0;
    for (std::vector<std::string>::iterator it = inputFiles.begin() ; it != inputFiles.end(); ++it) {
        treeHLT->Add((*it).c_str());
        treeggHiNtuplizer->Add((*it).c_str());
        treeHiEvt->Add((*it).c_str());
        treeSkim->Add((*it).c_str());
        treeHiForestInfo->Add((*it).c_str());
        if(isMC) treeGen->Add((*it).c_str());
        if(isMC && (nfiles==0)) { 
            lowestPthatFileName = (*it).c_str(); 
            TString str(lowestPthatFileName);
            cout << "lowestPthatFileName = " << lowestPthatFileName << endl;
            if(str.Contains("15")) {
                float temp[] = {15,30,50,80,120,9999};          
                for(int j=0;j<nPthat+1;j++){
                    pthatCut[j] = temp[j];
                }
            } else if(str.Contains("30")) {
                float temp[] = {30,50,80,120,170,9999};          
                for(int j=0;j<nPthat+1;j++){
                    pthatCut[j] = temp[j];
                }
            }
        }
        nfiles++;
    }

    float tmpWeight[nPthat];
    if(isMC) {
        for(int j=0; j<nPthat ; j++){
            tmpWeight[j] = xSecCal(lowestPthatFileName,treeHiEvt, pthatCut[j], pthatCut[j+1]);
            cout << collisionName << ", pthatWeight of " << pthatCut[j] << " to " << pthatCut[j+1] << " = " << tmpWeight[j] << endl;
        }
    }
/*
    HiForestInfoController hfic(treeHiForestInfo);
    std::cout<<"### HiForestInfo Tree ###"<< std::endl;
    hfic.printHiForestInfo();
    std::cout<<"###"<< std::endl;
*/
    treeHLT->SetBranchStatus("*",0);     // disable all branches
    treeHLT->SetBranchStatus("HLT_HI*SinglePhoton*Eta*",1);     // enable photon branches
    treeHLT->SetBranchStatus("HLT_HI*DoublePhoton*Eta*",1);     // enable photon branches

    float vz;
    Int_t hiBin;
    UInt_t run, lumis;
    ULong64_t event;
    float pthat, pthatWeight;
    treeHiEvt->SetBranchAddress("vz",&vz);
    treeHiEvt->SetBranchAddress("hiBin",&hiBin);
    treeHiEvt->SetBranchAddress("run", &run);
    treeHiEvt->SetBranchAddress("evt", &event);
    treeHiEvt->SetBranchAddress("lumi", &lumis);
    if(isMC) {
        //treeHiEvt->Branch("pthatWeight", &pthatWeight, "pthatWeight/F");
        treeHiEvt->SetBranchAddress("pthat", &pthat);
    }   

    // specify explicitly which branches to store, do not use wildcard
    treeSkim->SetBranchStatus("*",0);

    Int_t pcollisionEventSelection;  // this filter is used for HI.
    if (isHI) {
        treeSkim->SetBranchStatus("pcollisionEventSelection",1);
        if (treeSkim->GetBranch("pcollisionEventSelection")) {
            treeSkim->SetBranchAddress("pcollisionEventSelection",&pcollisionEventSelection);
        }
        else {   // overwrite to default
            pcollisionEventSelection = 1;
            std::cout<<"could not get branch : pcollisionEventSelection"<<std::endl;
            std::cout<<"set to default value : pcollisionEventSelection = "<<pcollisionEventSelection<<std::endl;
        }
    }
    else {
        pcollisionEventSelection = 0;    // default value if the collision is not HI, will not be used anyway.
    }
    Int_t pPAprimaryVertexFilter;    // this filter is used for PP.
    if (isPP) {
        treeSkim->SetBranchStatus("pPAprimaryVertexFilter",1);
        if (treeSkim->GetBranch("pPAprimaryVertexFilter")) {
            treeSkim->SetBranchAddress("pPAprimaryVertexFilter",&pPAprimaryVertexFilter);
        }
        else {   // overwrite to default
            pPAprimaryVertexFilter = 1;
            std::cout<<"could not get branch : pPAprimaryVertexFilter"<<std::endl;
            std::cout<<"set to default value : pPAprimaryVertexFilter = "<<pPAprimaryVertexFilter<<std::endl;
        }
    }
    else {
        pPAprimaryVertexFilter = 0;      // default value if the collision is not PP, will not be used anyway.
    }
    Int_t pBeamScrapingFilter;   // this filter is used for PP.
    if (isPP) {
        treeSkim->SetBranchStatus("pBeamScrapingFilter",1);
        if (treeSkim->GetBranch("pBeamScrapingFilter")) {
            treeSkim->SetBranchAddress("pBeamScrapingFilter",&pBeamScrapingFilter);
        }
        else {   // overwrite to default
            pBeamScrapingFilter = 1;
            std::cout<<"could not get branch : pBeamScrapingFilter"<<std::endl;
            std::cout<<"set to default value : pBeamScrapingFilter = "<<pBeamScrapingFilter<<std::endl;
        }
    }
    else {
        pBeamScrapingFilter = 0;     // default value if the collision is not PP, will not be used anyway.
    }

    // objects for z-jet correlations
    ggHiNtuplizer ggHi;
    ggHi.setupTreeForReading(treeggHiNtuplizer);    // treeggHiNtuplizer is input
    treeggHiNtuplizer->SetBranchStatus("*",0);
    treeggHiNtuplizer->SetBranchStatus("run",1);
    treeggHiNtuplizer->SetBranchStatus("event",1);
    treeggHiNtuplizer->SetBranchStatus("lumis",1);
    treeggHiNtuplizer->SetBranchStatus("nPho",1);
    treeggHiNtuplizer->SetBranchStatus("pho*",1);
    treeggHiNtuplizer->SetBranchStatus("pf*",1);
    //treeggHiNtuplizer->SetBranchStatus("tower*",1);
    if(isMC) treeggHiNtuplizer->SetBranchStatus("mc*",1);



    TFile* output = TFile::Open(outputFile,"RECREATE");
    TTree* configTree = setupConfigurationTreeForWriting(configCuts);

    // output tree variables
    TTree *outputTreeHLT=0, *outputTreeggHiNtuplizer=0, 
          *outputTreeHiEvt=0, *outputTreeSkim=0, *outputTreeHiForestInfo=0, *outputTreeGen=0;

    // output tree variables
    outputTreeHLT    = treeHLT->CloneTree(0);
    outputTreeggHiNtuplizer = treeggHiNtuplizer->CloneTree(0);
    outputTreeHiEvt = treeHiEvt->CloneTree(0);
    outputTreeSkim   = treeSkim->CloneTree(0);
    outputTreeHiForestInfo = treeHiForestInfo->CloneTree(0);
    if(isMC) outputTreeGen = treeGen ->CloneTree(0);

    outputTreeSkim->SetName("skimTree");
    outputTreeHLT->SetMaxTreeSize(MAXTREESIZE);
    outputTreeggHiNtuplizer->SetMaxTreeSize(MAXTREESIZE);
    outputTreeHiEvt->SetMaxTreeSize(MAXTREESIZE);
    outputTreeSkim->SetMaxTreeSize(MAXTREESIZE);
    outputTreeHiForestInfo->SetMaxTreeSize(MAXTREESIZE);
    if(isMC) outputTreeGen->SetMaxTreeSize(MAXTREESIZE);

    if(isMC) outputTreeHiEvt->Branch("pthatWeight", &pthatWeight, "pthatWeight/F");
  

    /////// Event Matching for DATA ///////
    EventMatcher* em = new EventMatcher();
    Long64_t duplicateEntries = 0;
    Long64_t entriesPassedEventSelection =0;
    Long64_t entriesAnalyzed =0;
    Long64_t entriesSpikeRejected=0;
    Long64_t entries = treeggHiNtuplizer->GetEntries();
    std::cout << "entries = " << entries << std::endl;
    std::cout<< "Loop : ggHiNtuplizer/EventTree" <<std::endl;
    //for (Long64_t j_entry=10000; j_entry<10100; ++j_entry)
    for (Long64_t j_entry=0; j_entry<entries; ++j_entry)
    {
        if (j_entry % 2000 == 0)  {
            std::cout << "current entry = " <<j_entry<<" out of "<<entries<<" : "<<std::setprecision(2)<<(double)j_entry/entries*100<<" %"<<std::endl;
        }

        treeHLT->GetEntry(j_entry);
        treeggHiNtuplizer->GetEntry(j_entry);
        treeSkim->GetEntry(j_entry);
        treeHiEvt->GetEntry(j_entry);
        treeHiForestInfo->GetEntry(j_entry);
        if(isMC) treeGen->GetEntry(j_entry);

        bool eventAdded = em->addEvent(run,lumis,event,j_entry);
        //std::cout << run << " " << lumis << " " << event << " " << j_entry << std::endl;
        if(!eventAdded) // this event is duplicate, skip this one.
        {
            duplicateEntries++;
            continue;
        }

        if(isMC) {
            if((pthat>=pthatCut[0]) && (pthat<pthatCut[1])) pthatWeight = tmpWeight[0];
            else if((pthat>=pthatCut[1]) && (pthat<pthatCut[2])) pthatWeight = tmpWeight[1];
            else if((pthat>=pthatCut[2]) && (pthat<pthatCut[3])) pthatWeight = tmpWeight[2];
            else if((pthat>=pthatCut[3]) && (pthat<pthatCut[4])) pthatWeight = tmpWeight[3];
            else if((pthat>=pthatCut[4]) && (pthat<pthatCut[5])) pthatWeight = tmpWeight[4];
            else continue;
        }
        // event selection
        if (!(TMath::Abs(vz) < cut_vz))  continue;
        if (isHI) {
            if ((pcollisionEventSelection < cut_pcollisionEventSelection))  continue;
        }
        else {
            if (pPAprimaryVertexFilter < cut_pPAprimaryVertexFilter || pBeamScrapingFilter < cut_pBeamScrapingFilter)  continue;
        }
        entriesPassedEventSelection++;

        // photon block
        // find leading photon
        int phoIdx = -1;     // index of the leading photon
        double maxPhoEt = -1;
        for(int i=0; i<ggHi.nPho; ++i)
        {
            bool failedEtCut  = (ggHi.phoEt->at(i) < cutPhoEt) ;
            bool failedEtaCut = (TMath::Abs(ggHi.phoEta->at(i)) > cutPhoEta) ;
            bool failedSpikeRejection;
            bool failedHotSpotRejection;
            //if (isHI) {
            failedSpikeRejection =( (ggHi.phoEta->at(i)<1.44) && 
                    (ggHi.phoSigmaIEtaIEta->at(i) < 0.002 ||
                     ggHi.pho_swissCrx->at(i)     > 0.9   ||
                     TMath::Abs(ggHi.pho_seedTime->at(i)) > 3) );
            // }
            // else {
            //     failedSpikeRejection = (ggHi.phoSigmaIEtaIEta->at(i) < 0.002);
            // }

            failedHotSpotRejection = (
                (ggHi.phoE3x3->at(i)/ggHi.phoE5x5->at(i) > 2./3.-0.03 && ggHi.phoE3x3->at(i)/ggHi.phoE5x5->at(i) < 2./3.+0.03) &&
                (ggHi.phoE1x5->at(i)/ggHi.phoE5x5->at(i) > 1./3.-0.03 && ggHi.phoE1x5->at(i)/ggHi.phoE5x5->at(i) < 1./3.+0.03) &&
                (ggHi.phoE2x5->at(i)/ggHi.phoE5x5->at(i) > 2./3.-0.03 && ggHi.phoE2x5->at(i)/ggHi.phoE5x5->at(i) < 2./3.+0.03) );
            
            bool failedHoverE = (ggHi.phoHoverE->at(i) > 0.2);      // <0.1 cut is applied after corrections
            //               bool failedEnergyRatio = ((float)ggHi.phoSCRawE->at(i)/ggHi.phoE->at(i) < 0.5);

            if (failedEtCut)          continue;
            if (failedEtaCut)         continue;
            if (failedSpikeRejection) continue;
            if (failedHotSpotRejection) {entriesSpikeRejected++; continue;}
            if (failedHoverE)         continue;
            //               if (failedEnergyRatio)    continue;    // actually applied after corrections

            if (ggHi.phoEt->at(i) > maxPhoEt)
            {
                maxPhoEt = ggHi.phoEt->at(i);
                phoIdx = i;
            }
        }
        if (phoIdx == -1) continue;
        entriesAnalyzed++;

        outputTreeHLT->Fill();
        outputTreeggHiNtuplizer->Fill();
        outputTreeHiEvt->Fill();
        outputTreeSkim->Fill();
        outputTreeHiForestInfo->Fill();
        if(isMC) outputTreeGen->Fill();
    }// event loop closed here

    std::cout<<  "Loop ENDED : ggHiNtuplizer/EventTree" <<std::endl;
    std::cout << "entries            = " << entries << std::endl;
    std::cout << "duplicateEntries   = " << duplicateEntries << std::endl;
    std::cout << "entriesPassedEventSelection   = " << entriesPassedEventSelection << std::endl;
    std::cout << "entriesAnalyzed               = " << entriesAnalyzed << std::endl;
    std::cout << "entriesSpikeRejected          = " << entriesSpikeRejected << std::endl;
    std::cout << "outputTreeHLT->GetEntries()   = " << outputTreeHLT->GetEntries() << std::endl;
    std::cout << "outputTreeggHiNtuplizer->GetEntries()   = " << outputTreeggHiNtuplizer->GetEntries() << std::endl;
    std::cout << "outputTreeSkim->GetEntries()  = " << outputTreeSkim->GetEntries() << std::endl;
    std::cout << "outputTreeHiEvt->GetEntries() = " << outputTreeHiEvt->GetEntries() << std::endl;
    std::cout << "outputTreeHiForestInfo->GetEntries() = " << outputTreeHiForestInfo->GetEntries() << std::endl;
    if(isMC) std::cout << "outputTreeGen->GetEntries() = " << outputTreeGen->GetEntries() << std::endl;


    output->cd();
    configTree->Write("",TObject::kOverwrite);
    output->Write("",TObject::kOverwrite);
    output->Close();
}