예제 #1
0
void TreeFinalizerC_ZJet::finalize() {



    TString dataset_tstr(dataset_);

    gROOT->cd();

    std::string outfileName;

    if( DEBUG_ ) outfileName = "provaZJet_"+dataset_;
    else {
        if(dataset_!="") outfileName = "ZJet_"+dataset_;
        else outfileName = "ZJet";
    }

    outfileName = outfileName;


    if( nBlocks_ >1 ) {
        char blockText[100];
        sprintf( blockText, "_%d", iBlock_ );
        std::string iBlockString(blockText);
        outfileName = outfileName + iBlockString;
    }
    outfileName += ".root";


    TFile* outFile = new TFile(outfileName.c_str(), "RECREATE");
    outFile->cd();

    TTree* tree_passedEvents = new TTree("tree_passedEvents", "Unbinned data for statistical treatment");



    TH1D* h1_cutflow_50100 = new TH1D("cutflow_50100", "", 6, 0, 6);
    h1_cutflow_50100->Sumw2();


    TH1F* h1_nvertex = new TH1F("nvertex", "", 21, -0.5, 20.5);
    h1_nvertex->Sumw2();
    TH1F* h1_nvertex_PUW = new TH1F("nvertex_PUW", "", 21, -0.5, 20.5);
    h1_nvertex_PUW->Sumw2();

    TH1D* h1_quarkFraction_3050 = new TH1D("quarkFraction_3050", "", 1, 0., 1.);
    h1_quarkFraction_3050->Sumw2();
    TH1D* h1_quarkFraction_5080 = new TH1D("quarkFraction_5080", "", 1, 0., 1.);
    h1_quarkFraction_5080->Sumw2();
    TH1D* h1_quarkFraction_80120 = new TH1D("quarkFraction_80120", "", 1, 0., 1.);
    h1_quarkFraction_80120->Sumw2();

    TH1D* h1_quarkFraction_antibtag_3050 = new TH1D("quarkFraction_antibtag_3050", "", 1, 0., 1.);
    h1_quarkFraction_antibtag_3050->Sumw2();
    TH1D* h1_quarkFraction_antibtag_5080 = new TH1D("quarkFraction_antibtag_5080", "", 1, 0., 1.);
    h1_quarkFraction_antibtag_5080->Sumw2();
    TH1D* h1_quarkFraction_antibtag_80120 = new TH1D("quarkFraction_antibtag_80120", "", 1, 0., 1.);
    h1_quarkFraction_antibtag_80120->Sumw2();

    TH1D* h1_ptJetReco = new TH1D("ptJetReco", "", 100, 0., 300);
    h1_ptJetReco->Sumw2();
    TH1D* h1_pt2ndJetReco = new TH1D("pt2ndJetReco", "", 100, 5., 400);
    h1_pt2ndJetReco->Sumw2();

    TH1D* h1_ptDJetReco_3050 = new TH1D("ptDJetReco_3050", "", 50, 0., 1.0001);
    h1_ptDJetReco_3050->Sumw2();
    TH1D* h1_nChargedJetReco_3050 = new TH1D("nChargedJetReco_3050", "", 51, -0.5, 50.5);
    h1_nChargedJetReco_3050->Sumw2();
    TH1D* h1_nNeutralJetReco_3050 = new TH1D("nNeutralJetReco_3050", "", 51, -0.5, 50.5);
    h1_nNeutralJetReco_3050->Sumw2();
    TH1D* h1_QGLikelihoodJetReco_3050 = new TH1D("QGLikelihoodJetReco_3050", "", 50, 0., 1.0001);
    h1_QGLikelihoodJetReco_3050->Sumw2();

    TH1D* h1_ptDJetReco_antibtag_3050 = new TH1D("ptDJetReco_antibtag_3050", "", 50, 0., 1.0001);
    h1_ptDJetReco_antibtag_3050->Sumw2();
    TH1D* h1_nChargedJetReco_antibtag_3050 = new TH1D("nChargedJetReco_antibtag_3050", "", 51, -0.5, 50.5);
    h1_nChargedJetReco_antibtag_3050->Sumw2();
    TH1D* h1_nNeutralJetReco_antibtag_3050 = new TH1D("nNeutralJetReco_antibtag_3050", "", 51, -0.5, 50.5);
    h1_nNeutralJetReco_antibtag_3050->Sumw2();
    TH1D* h1_QGLikelihoodJetReco_antibtag_3050 = new TH1D("QGLikelihoodJetReco_antibtag_3050", "", 50, 0., 1.0001);
    h1_QGLikelihoodJetReco_antibtag_3050->Sumw2();
    TH1D* h1_QGLikelihoodJetReco_antibtag_gluon_3050 = new TH1D("QGLikelihoodJetReco_antibtag_gluon_3050", "", 50, 0., 1.0001);
    h1_QGLikelihoodJetReco_antibtag_gluon_3050->Sumw2();
    TH1D* h1_QGLikelihoodJetReco_antibtag_quark_3050 = new TH1D("QGLikelihoodJetReco_antibtag_quark_3050", "", 50, 0., 1.0001);
    h1_QGLikelihoodJetReco_antibtag_quark_3050->Sumw2();

    TH1D* h1_ptDJetReco_5080 = new TH1D("ptDJetReco_5080", "", 50, 0., 1.0001);
    h1_ptDJetReco_5080->Sumw2();
    TH1D* h1_nChargedJetReco_5080 = new TH1D("nChargedJetReco_5080", "", 51, -0.5, 50.5);
    h1_nChargedJetReco_5080->Sumw2();
    TH1D* h1_nNeutralJetReco_5080 = new TH1D("nNeutralJetReco_5080", "", 51, -0.5, 50.5);
    h1_nNeutralJetReco_5080->Sumw2();
    TH1D* h1_QGLikelihoodJetReco_5080 = new TH1D("QGLikelihoodJetReco_5080", "", 50, 0., 1.0001);
    h1_QGLikelihoodJetReco_5080->Sumw2();

    TH1D* h1_ptDJetReco_antibtag_5080 = new TH1D("ptDJetReco_antibtag_5080", "", 50, 0., 1.0001);
    h1_ptDJetReco_antibtag_5080->Sumw2();
    TH1D* h1_nChargedJetReco_antibtag_5080 = new TH1D("nChargedJetReco_antibtag_5080", "", 51, -0.5, 50.5);
    h1_nChargedJetReco_antibtag_5080->Sumw2();
    TH1D* h1_nNeutralJetReco_antibtag_5080 = new TH1D("nNeutralJetReco_antibtag_5080", "", 51, -0.5, 50.5);
    h1_nNeutralJetReco_antibtag_5080->Sumw2();
    TH1D* h1_QGLikelihoodJetReco_antibtag_5080 = new TH1D("QGLikelihoodJetReco_antibtag_5080", "", 50, 0., 1.0001);
    h1_QGLikelihoodJetReco_antibtag_5080->Sumw2();
    TH1D* h1_QGLikelihoodJetReco_antibtag_gluon_5080 = new TH1D("QGLikelihoodJetReco_antibtag_gluon_5080", "", 50, 0., 1.0001);
    h1_QGLikelihoodJetReco_antibtag_gluon_5080->Sumw2();
    TH1D* h1_QGLikelihoodJetReco_antibtag_quark_5080 = new TH1D("QGLikelihoodJetReco_antibtag_quark_5080", "", 50, 0., 1.0001);
    h1_QGLikelihoodJetReco_antibtag_quark_5080->Sumw2();

    TH1D* h1_ptDJetReco_80120 = new TH1D("ptDJetReco_80120", "", 50, 0., 1.0001);
    h1_ptDJetReco_80120->Sumw2();
    TH1D* h1_nChargedJetReco_80120 = new TH1D("nChargedJetReco_80120", "", 51, -0.5, 50.5);
    h1_nChargedJetReco_80120->Sumw2();
    TH1D* h1_nNeutralJetReco_80120 = new TH1D("nNeutralJetReco_80120", "", 51, -0.5, 50.5);
    h1_nNeutralJetReco_80120->Sumw2();
    TH1D* h1_QGLikelihoodJetReco_80120 = new TH1D("QGLikelihoodJetReco_80120", "", 50, 0., 1.0001);
    h1_QGLikelihoodJetReco_80120->Sumw2();

    TH1D* h1_ptDJetReco_antibtag_80120 = new TH1D("ptDJetReco_antibtag_80120", "", 50, 0., 1.0001);
    h1_ptDJetReco_antibtag_80120->Sumw2();
    TH1D* h1_nChargedJetReco_antibtag_80120 = new TH1D("nChargedJetReco_antibtag_80120", "", 51, -0.5, 50.5);
    h1_nChargedJetReco_antibtag_80120->Sumw2();
    TH1D* h1_nNeutralJetReco_antibtag_80120 = new TH1D("nNeutralJetReco_antibtag_80120", "", 51, -0.5, 50.5);
    h1_nNeutralJetReco_antibtag_80120->Sumw2();
    TH1D* h1_QGLikelihoodJetReco_antibtag_80120 = new TH1D("QGLikelihoodJetReco_antibtag_80120", "", 50, 0., 1.0001);
    h1_QGLikelihoodJetReco_antibtag_80120->Sumw2();
    TH1D* h1_QGLikelihoodJetReco_antibtag_gluon_80120 = new TH1D("QGLikelihoodJetReco_antibtag_gluon_80120", "", 50, 0., 1.0001);
    h1_QGLikelihoodJetReco_antibtag_gluon_80120->Sumw2();
    TH1D* h1_QGLikelihoodJetReco_antibtag_quark_80120 = new TH1D("QGLikelihoodJetReco_antibtag_quark_80120", "", 50, 0., 1.0001);
    h1_QGLikelihoodJetReco_antibtag_quark_80120->Sumw2();


    Double_t ptBinning[4];
    ptBinning[0] = 30.;
    ptBinning[1] = 50.;
    ptBinning[2] = 80.;
    ptBinning[3] = 120.;

    TH1D* h1_nEvents_passed = new TH1D("nEvents_passed", "", 3, ptBinning);
    h1_nEvents_passed->Sumw2();
    TH1D* h1_nEvents_passed_quark = new TH1D("nEvents_passed_quark", "", 3, ptBinning);
    h1_nEvents_passed_quark->Sumw2();


    TH1D* h1_ptZ = new TH1D("ptZ", "", 500, 0., 500.);
    h1_ptZ->Sumw2();
    TH1D* h1_mZ = new TH1D("mZ", "", 100, 50., 150.);
    h1_mZ->Sumw2();
    TH1D* h1_etaZ = new TH1D("etaZ", "", 15, -1.3, 1.3);
    h1_etaZ->Sumw2();
    TH1D* h1_phiZ = new TH1D("phiZ", "", 15, -3.1416, 3.1416);
    h1_phiZ->Sumw2();





    Int_t run;
    tree_->SetBranchAddress("run", &run);
    Int_t LS;
    tree_->SetBranchAddress("LS", &LS);
    Int_t event;
    tree_->SetBranchAddress("event", &event);
    Int_t nvertex;
    tree_->SetBranchAddress("nvertex", &nvertex);
    Float_t eventWeight;
    tree_->SetBranchAddress("eventWeight", &eventWeight);
    Float_t eventWeight_genjets;

    Float_t eMet;
    Float_t phiMet;
    tree_->SetBranchAddress("epfMet", &eMet);
    tree_->SetBranchAddress("phipfMet", &phiMet);


    Float_t ptHat;
    tree_->SetBranchAddress("ptHat", &ptHat);

    Int_t nPU;
    tree_->SetBranchAddress("nPU", &nPU);
    Int_t PUReWeight;
    tree_->SetBranchAddress("PUReWeight", &PUReWeight);
    Float_t rhoPF;
    tree_->SetBranchAddress("rhoPF", &rhoPF);
    Float_t rhoJetPF;
    tree_->SetBranchAddress("rhoJetPF", &rhoJetPF);

    Float_t eLeptZ1;
    tree_->SetBranchAddress("eLeptZ1", &eLeptZ1);
    Float_t ptLeptZ1;
    tree_->SetBranchAddress("ptLeptZ1", &ptLeptZ1);
    Float_t etaLeptZ1;
    tree_->SetBranchAddress("etaLeptZ1", &etaLeptZ1);
    Float_t phiLeptZ1;
    tree_->SetBranchAddress("phiLeptZ1", &phiLeptZ1);

    Float_t eLeptZ2;
    tree_->SetBranchAddress("eLeptZ2", &eLeptZ2);
    Float_t ptLeptZ2;
    tree_->SetBranchAddress("ptLeptZ2", &ptLeptZ2);
    Float_t etaLeptZ2;
    tree_->SetBranchAddress("etaLeptZ2", &etaLeptZ2);
    Float_t phiLeptZ2;
    tree_->SetBranchAddress("phiLeptZ2", &phiLeptZ2);



    Bool_t matchedToMC;
    tree_->SetBranchAddress("matchedToMC", &matchedToMC);

    Int_t nJets;
    tree_->SetBranchAddress("nJets", &nJets);
    Float_t eJet[50];
    tree_->SetBranchAddress("eJet", eJet);
    Float_t ptJet[50];
    tree_->SetBranchAddress("ptJet", ptJet);
    Float_t etaJet[50];
    tree_->SetBranchAddress("etaJet", etaJet);
    Float_t phiJet[50];
    tree_->SetBranchAddress("phiJet", phiJet);
    Int_t nChargedJet[50];
    tree_->SetBranchAddress("nChargedJet", nChargedJet);
    Int_t nNeutralJet[50];
    tree_->SetBranchAddress("nNeutralJet", nNeutralJet);
    Float_t eChargedHadronsJet[50];
    tree_->SetBranchAddress("eChargedHadronsJet", eChargedHadronsJet);
    Float_t eNeutralHadronsJet[50];
    tree_->SetBranchAddress("eNeutralHadronsJet", eNeutralHadronsJet);
    Float_t ePhotonsJet[50];
    tree_->SetBranchAddress("ePhotonsJet", ePhotonsJet);
    Float_t eHFHadronsJet[50];
    tree_->SetBranchAddress("eHFHadronsJet", eHFHadronsJet);
    Float_t eHFEMJet[50];
    tree_->SetBranchAddress("eHFEMJet", eHFEMJet);
    Float_t ptDJet[50];
    tree_->SetBranchAddress("ptDJet", ptDJet);
    Float_t rmsCandJet[50];
    tree_->SetBranchAddress("rmsCandJet", rmsCandJet);
    Float_t betaStarJet[50];
    tree_->SetBranchAddress("betaStarJet", betaStarJet);
    Float_t QGLikelihoodJet[50];
    tree_->SetBranchAddress("QGLikelihoodJet", QGLikelihoodJet);
    Float_t trackCountingHighEffBJetTagsJet[50];
    tree_->SetBranchAddress("trackCountingHighEffBJetTagsJet", trackCountingHighEffBJetTagsJet);
    Float_t combinedSecondaryVertexBJetTagJet[50];
    tree_->SetBranchAddress("combinedSecondaryVertexBJetTagJet", combinedSecondaryVertexBJetTagJet);
    Int_t pdgIdPartJet[50];
    tree_->SetBranchAddress("pdgIdPartJet", pdgIdPartJet);
    Float_t ptPartJet[50];
    tree_->SetBranchAddress("ptPartJet", ptPartJet);
    Float_t etaPartJet[50];
    tree_->SetBranchAddress("etaPartJet", etaPartJet);
    Float_t phiPartJet[50];
    tree_->SetBranchAddress("phiPartJet", phiPartJet);
    Float_t ptGenJet[50];
    tree_->SetBranchAddress("ptGenJet", ptGenJet);
    Float_t etaGenJet[50];
    tree_->SetBranchAddress("etaGenJet", etaGenJet);
    Float_t phiGenJet[50];
    tree_->SetBranchAddress("phiGenJet", phiGenJet);


    Float_t axis1Jet[50];
    tree_->SetBranchAddress("axis1Jet",axis1Jet);
    Float_t axis2Jet[50];
    tree_->SetBranchAddress("axis2Jet",axis2Jet);
    Float_t pullJet[50];
    tree_->SetBranchAddress("pullJet",pullJet);
    Float_t tanaJet[50];
    tree_->SetBranchAddress("tanaJet",tanaJet);
    Float_t ptD_QCJet[50];
    tree_->SetBranchAddress("ptD_QCJet",ptD_QCJet);
    Float_t rmsCand_QCJet[50];
    tree_->SetBranchAddress("rmsCand_QCJet",rmsCand_QCJet);
    Float_t axis1_QCJet[50];
    tree_->SetBranchAddress("axis1_QCJet",axis1_QCJet);
    Float_t axis2_QCJet[50];
    tree_->SetBranchAddress("axis2_QCJet",axis2_QCJet);
    Float_t pull_QCJet[50];
    tree_->SetBranchAddress("pull_QCJet",pull_QCJet);
    Float_t tana_QCJet[50];
    tree_->SetBranchAddress("tana_QCJet",tana_QCJet);
    Int_t   nPFCand_QC_ptCutJet[50];
    tree_->SetBranchAddress("nPFCand_QC_ptCutJet",nPFCand_QC_ptCutJet);
    //Float_t nChg_ptCutJet[50];	tree_->SetBranchAddress("nChg_ptCutJet",nChg_ptCutJet);
    //Float_t nChg_QCJet[50];		tree_->SetBranchAddress("nChg_QCJet",nChg_QCJet);
    //Float_t nChg_ptCut_QCJet[50];	tree_->SetBranchAddress("nChg_ptCut_QCJet",nChg_ptCut_QCJet);
    //Float_t nNeutral_ptCutJet[50];	tree_->SetBranchAddress("nNeutral_ptCutJet",nNeutral_ptCutJet);
    Float_t RchgJet[50];
    tree_->SetBranchAddress("RchgJet",RchgJet);
    Float_t RneutralJet[50];
    tree_->SetBranchAddress("RneutralJet",RneutralJet);
    Float_t RJet[50];
    tree_->SetBranchAddress("RJet",RJet);
    Float_t Rchg_QCJet[50];
    tree_->SetBranchAddress("Rchg_QCJet",Rchg_QCJet);







    Bool_t secondJetOK;
    Bool_t btagged;
    Bool_t matchedToGenJet;
    Float_t deltaPhi_jet;
    Float_t eventWeight_noPU;
    Float_t ptZ, etaZ, mZ;
    Float_t QGlikelihood, QGlikelihood2012;

    Float_t ptJet0_t;
    Float_t etaJet0_t;
    Int_t nChargedJet0_t;
    Int_t nNeutralJet0_t;
    Float_t ptDJet0_t;
    Float_t ptD_QCJet0_t;
    Float_t axis1_QCJet0_t;
    Float_t axis2_QCJet0_t;
    Int_t nPFCand_QC_ptCutJet0_t;
    Int_t pdgIdPartJet_t;
    Float_t betaStarJet0_t;

    Float_t ptJet1_t;
    Float_t etaJet1_t;



    tree_passedEvents->Branch( "run", &run, "run/I" );
    tree_passedEvents->Branch( "LS", &LS, "LS/I" );
    tree_passedEvents->Branch( "nPU", &nPU, "nPU/I" );
    tree_passedEvents->Branch( "PUReWeight", &PUReWeight, "PUReWeight/F" );
    tree_passedEvents->Branch( "event", &event, "event/I" );
    tree_passedEvents->Branch( "eventWeight", &eventWeight, "eventWeight/F");
    tree_passedEvents->Branch( "eventWeight_noPU", &eventWeight_noPU, "eventWeight_noPU/F");
    tree_passedEvents->Branch( "nvertex", &nvertex, "nvertex/I");
    tree_passedEvents->Branch( "rhoPF", &rhoPF, "rhoPF/F");
    tree_passedEvents->Branch( "rhoJetPF", &rhoJetPF, "rhoJetPF/F");
    tree_passedEvents->Branch( "secondJetOK", &secondJetOK, "secondJetOK/O");
    tree_passedEvents->Branch( "btagged", &btagged, "btagged/O");
    tree_passedEvents->Branch( "mZ", &mZ, "mZ/F");
    tree_passedEvents->Branch( "ptZ", &ptZ, "ptZ/F");
    tree_passedEvents->Branch( "etaZ", &etaZ, "etaZ/F");
    tree_passedEvents->Branch( "ptJet0", &ptJet0_t, "ptJet0_t/F");
    tree_passedEvents->Branch( "etaJet0", &etaJet0_t, "etaJet0_t/F");
    tree_passedEvents->Branch( "ptJet1", &ptJet1_t, "ptJet1_t/F");
    tree_passedEvents->Branch( "etaJet1", &etaJet1_t, "etaJet1_t/F");
    tree_passedEvents->Branch( "nChargedJet0", &nChargedJet0_t, "nChargedJet0_t/I");
    tree_passedEvents->Branch( "nNeutralJet0", &nNeutralJet0_t, "nNeutralJet0_t/I");
    tree_passedEvents->Branch( "ptDJet0", &ptDJet0_t, "ptDJet0_t/F");
    //tree_passedEvents->Branch( "rmsCandJet0", &rmsCandJet0_t, "rmsCandJet[0]/F");
    //tree_passedEvents->Branch( "betaStarJet0", &betaStarJet0_t, "betaStarJet[0]/F");
    tree_passedEvents->Branch( "QGLikelihoodJet0", &QGlikelihood, "QGlikelihood/F");
    tree_passedEvents->Branch( "QGLikelihood2012Jet0", &QGlikelihood2012, "QGlikelihood2012/F");
    tree_passedEvents->Branch( "pdgIdPartJet0", &pdgIdPartJet_t, "pdgIdPart_t/I");
    tree_passedEvents->Branch( "deltaPhi_jet", &deltaPhi_jet, "deltaPhi_jet/F");

    //tree_passedEvents->Branch( "axis1Jet0"		, &axis1Jet[0]			, "axis1Jet[0]/F");
    //tree_passedEvents->Branch( "axis2Jet0"		, &axis2Jet[0]			, "axis2Jet[0]/F");
    //tree_passedEvents->Branch( "pullJet0"			, &pullJet[0]			, "pullJet[0]/F");
    //tree_passedEvents->Branch( "tanaJet0"			, &tanaJet[0]			, "tanaJet[0]/F");

    tree_passedEvents->Branch( "ptD_QCJet0"		, &ptD_QCJet0_t		, "ptD_QCJet0_t/F");
    //tree_passedEvents->Branch( "rmsCand_QCJet0"		, &rmsCand_QCJet[0]		, "rmsCand_QCJet[0]/F");
    tree_passedEvents->Branch( "axis1_QCJet0"		, &axis1_QCJet0_t		, "axis1_QCJet0_t/F");
    tree_passedEvents->Branch( "axis2_QCJet0"		, &axis2_QCJet0_t		, "axis2_QCJet0_t/F");
    //tree_passedEvents->Branch( "pull_QCJet0"		, &pull_QCJet[0]		, "pull_QCJet[0]/F");
    //tree_passedEvents->Branch( "tana_QCJet0"		, &tana_QCJet[0]		, "tana_QCJet[0]/F");

    tree_passedEvents->Branch( "nPFCand_QC_ptCutJet"		, &nPFCand_QC_ptCutJet0_t		, "nPFCand_QC_ptCutJet0_t/I");
    //tree_passedEvents->Branch( "nChg_ptCutJet0"		, &nChg_ptCutJet[0]		, "nChg_ptCutJet[0]/I");
    //tree_passedEvents->Branch( "nChg_QCJet0"		, &nChg_QCJet[0]		, "nChg_QCJet[0]/I");
    //tree_passedEvents->Branch( "nChg_ptCut_QCJet0"	, &nChg_ptCut_QCJet[0]	 	, "nChg_ptCut_QCJet[0]/I");
    //tree_passedEvents->Branch( "nNeutral_ptCutJet0"	, &nNeutral_ptCutJet[0]	, "nNeutral_ptCutJet[0]/I");

    //tree_passedEvents->Branch( "RchgJet0"			, &RchgJet[0]			, "RchgJet[0]/F");
    //tree_passedEvents->Branch( "RneutralJet0"		, &RneutralJet[0]		, "RneutralJet[0]/F");
    //tree_passedEvents->Branch( "RJet0"			, &RJet[0]			, "RJet[0]/F");
    //tree_passedEvents->Branch( "Rchg_QCJet0"		, &Rchg_QCJet[0]		, "Rchg_QCJet[0]/F");

    tree_passedEvents->Branch( "betaStarJet0"		, &betaStarJet0_t		, "betaStarJet0_t/F");



    QGLikelihoodCalculator* qglikeli = new QGLikelihoodCalculator("/afs/cern.ch/work/p/pandolf/public/QG/QG_QCD_Pt-15to3000_TuneZ2_Flat_8TeV_pythia6_Summer12_DR53X-PU_S10_START53_V7A-v1.root");






    TRandom3* rand = new TRandom3(13);



    int nEntries = tree_->GetEntries();
//nEntries = 100000;

    std::map< int, std::map<int, std::vector<int> > > run_lumi_ev_map;


    int blockSize = TMath::Floor( (float)nEntries/nBlocks_ );
    int iEventMin = iBlock_*blockSize;
    int iEventMax = (iBlock_+1)*blockSize;
    if( iEventMax>nEntries ) iEventMax = nEntries;

    std::cout << "-> Running on events: " << iEventMin << " - " << iEventMax << std::endl;

    for(int iEntry=iEventMin; iEntry<iEventMax; ++iEntry) {

        if( ((iEntry-iEventMin) % 100000)==0 ) std::cout << "Entry: " << (iEntry-iEventMin) << " /" << blockSize << std::endl;

        tree_->GetEntry(iEntry);


        if( eventWeight <= 0. ) eventWeight = 1.;

        int icut = 0;
        h1_cutflow_50100->Fill( icut++, eventWeight );
        h1_nvertex->Fill( nvertex, eventWeight);

        bool isMC = run<5;

        if( isMC ) {

            // PU reweighting:
            eventWeight_noPU = eventWeight;

        } else { //it's data: remove duplicate events (if any):

            std::map<int, std::map<int, std::vector<int> > >::iterator it;

            it = run_lumi_ev_map.find(run);


            if( it==run_lumi_ev_map.end() ) {

                std::vector<int> events;
                events.push_back(event);
                std::map<int, std::vector<int> > lumi_ev_map;
                lumi_ev_map.insert( std::pair<int,std::vector<int> >(LS, events));
                run_lumi_ev_map.insert( std::pair<int, std::map<int, std::vector<int> > > (run, lumi_ev_map) );

            } else { //run exists, look for LS


                std::map<int, std::vector<int> >::iterator it_LS;
                it_LS = it->second.find( LS );

                if( it_LS==(it->second.end())  ) {

                    std::vector<int> events;
                    events.push_back(event);
                    it->second.insert( std::pair<int, std::vector<int> > (LS, events) );

                } else { //LS exists, look for event

                    std::vector<int>::iterator ev;
                    for( ev=it_LS->second.begin(); ev!=it_LS->second.end(); ++ev )
                        if( *ev==event ) break;


                    if( ev==it_LS->second.end() ) {

                        it_LS->second.push_back(event);

                    } else {

                        std::cout << "DISCARDING DUPLICATE EVENT!! Run: " << run << " LS: " << LS << " event: " << event << std::endl;

                        continue;

                    }
                }
            }

        } //if is mc




        h1_nvertex_PUW->Fill( nvertex, eventWeight);


        TLorentzVector lept1, lept2;
        lept1.SetPtEtaPhiE( ptLeptZ1, etaLeptZ1, phiLeptZ1, eLeptZ1 );
        lept2.SetPtEtaPhiE( ptLeptZ2, etaLeptZ2, phiLeptZ2, eLeptZ2 );

        TLorentzVector Z = lept1 + lept2;

        ptZ = Z.Pt();
        mZ = Z.M();
        etaZ = Z.Eta();

        h1_mZ->Fill( Z.M(), eventWeight );

        if( Z.M()<70. || Z.M()>110. ) continue;

        if( nJets==0 ) continue;
        if( ptJet[0] < 20. ) continue;


        // jet id:
        TLorentzVector jet;
        jet.SetPtEtaPhiE( ptJet[0], etaJet[0], phiJet[0], eJet[0] );

        if( fabs(jet.Eta())<2.4 && nChargedJet[0]==0 ) continue;
        if( (nNeutralJet[0]+nChargedJet[0])==1 ) continue;
        if( (ePhotonsJet[0]+eHFEMJet[0])/jet.E()>0.99 ) continue;
        if( (eNeutralHadronsJet[0])/jet.E()>0.99 ) continue;


        pdgIdPartJet_t = 0;

        if( isMC ) {

            // check if matched to parton/genjet
            TLorentzVector part;
            part.SetPtEtaPhiE( ptPartJet[0], etaPartJet[0], phiPartJet[0], ptPartJet[0] );

            TLorentzVector genJet;
            genJet.SetPtEtaPhiE( ptGenJet[0], etaGenJet[0], phiGenJet[0], ptGenJet[0] );

            float deltaR_jet_part = jet.DeltaR(part);

            if( deltaR_jet_part<0.3 ) {

                pdgIdPartJet_t = pdgIdPartJet[0];

            } else {

                float deltaR_jet_genjet = jet.DeltaR(genJet);

                if( deltaR_jet_genjet < 0.3 ) { //undefined
                    pdgIdPartJet_t = -999;
                } else {
                    pdgIdPartJet_t = 0;  //PU
                }

            } // else (if not matched to parton)

        } // if is MC



        //leading jet and Z back2back in transverse plane
        bool back2back = true;
        deltaPhi_jet = fabs(delta_phi(Z.Phi(), phiJet[0]));
        Float_t pi = TMath::Pi();
        float deltaPhiThreshold = 1.;
        if( fabs(deltaPhi_jet) < (pi - deltaPhiThreshold) ) back2back = false; //loose back to back for now


        // cut away b-jets:
        //if( trackCountingHighEffBJetTagsJetReco>1.7 ) continue;
        btagged = combinedSecondaryVertexBJetTagJet[0]>0.244;
        if( btagged ) continue;

        if( nJets>1 )
            secondJetOK = ( ptJet[1] < secondJetThreshold_*Z.Pt() || ptJet[1] < 10. );
        else
            secondJetOK = true;


        ptJet0_t = jet.Pt();
        etaJet0_t = jet.Eta();
        nChargedJet0_t = nChargedJet[0];
        nNeutralJet0_t = nNeutralJet[0];
        ptDJet0_t = ptDJet[0];
        ptD_QCJet0_t = ptD_QCJet[0];
        axis1_QCJet0_t = axis1_QCJet[0];
        axis2_QCJet0_t = axis2_QCJet[0];
        betaStarJet0_t= betaStarJet[0];
        nPFCand_QC_ptCutJet0_t = nPFCand_QC_ptCutJet[0];

        ptJet1_t  = (nJets>0) ? ptJet[1] : 0.;
        etaJet1_t = (nJets>0) ? etaJet[1] : 10.;




        QGlikelihood = qglikeli->computeQGLikelihoodPU( ptJet[0], rhoPF, nChargedJet[0], nNeutralJet[0], ptDJet[0], -1. );
        QGlikelihood2012 = qglikeli->computeQGLikelihood2012( ptJet[0], etaJet[0], rhoPF, nPFCand_QC_ptCutJet[0], ptD_QCJet[0], axis2_QCJet[0] );


        tree_passedEvents->Fill();

    }


    outFile->cd();

    tree_passedEvents->Write();

    h1_cutflow_50100->Write();

    h1_nvertex->Write();
    h1_nvertex_PUW->Write();

    h1_ptZ->Write();
    h1_mZ->Write();
    h1_phiZ->Write();
    h1_etaZ->Write();
    h1_ptJetReco->Write();
    h1_pt2ndJetReco->Write();

    h1_nEvents_passed_quark->Write();
    h1_nEvents_passed->Write();




    outFile->Close();


}
예제 #2
0
void Analyzer::Loop(TChain *t,int type){ //type|=4 : compute lmin,lmax; type|=1 data type |=2 mc

		
		treeVarInt["nvtx"] = -999; t->SetBranchAddress("nvtx",&treeVarInt["nvtx"]);
		treeVar["rho"] = -999; t->SetBranchAddress("rho",&treeVar["rho"]);
		Float_t jetPt[4]; t->SetBranchAddress("jetPt",&jetPt);
		Float_t jetEnergy[4]; t->SetBranchAddress("jetEnergy",&jetEnergy);
		Float_t jetBtag[4]; t->SetBranchAddress("jetBtag",&jetBtag);
		Float_t jetBeta[4]; t->SetBranchAddress("jetBeta",&jetBeta);
		Float_t jetEta[4]; t->SetBranchAddress("jetEta",&jetEta);
		Float_t jetPhi[4]; t->SetBranchAddress("jetPhi",&jetPhi);
		Float_t jetAxis_QC[2][4]; t->SetBranchAddress("jetAxis_QC",&jetAxis_QC);
		Float_t jetAxis[2][4]; t->SetBranchAddress("jetAxis",&jetAxis);
		Float_t jetPtD[4]; t->SetBranchAddress("jetPtD",&jetPtD);
		Float_t jetPtD_QC[4]; t->SetBranchAddress("jetPtD_QC",&jetPtD_QC);
		Int_t jetChgPart_ptcut[4]; t->SetBranchAddress("jetChgPart_ptcut",&jetChgPart_ptcut);
		Int_t jetChgPart_QC[4]; t->SetBranchAddress("jetChgPart_QC",&jetChgPart_QC);
		Int_t jetNeutralPart_ptcut[4]; t->SetBranchAddress("jetNeutralPart_ptcut",&jetNeutralPart_ptcut);
		vector<int> *partonId=0;if(type >1)t->SetBranchAddress("partonId",&partonId);
		vector<int> *partonSt=0;if(type >1)t->SetBranchAddress("partonSt",&partonSt);
		vector<float> *partonPt=0;if(type >1)t->SetBranchAddress("partonPt",&partonPt);
		vector<float> *partonEta=0;if(type >1)t->SetBranchAddress("partonEta",&partonEta);
		vector<float> *partonPhi=0;if(type >1)t->SetBranchAddress("partonPhi",&partonPhi);
		vector<float> *partonE=0;if(type >1)t->SetBranchAddress("partonE",&partonE);
		
		vector<bool> *triggerResult=0;if(type ==1)t->SetBranchAddress("triggerResult",&triggerResult);
		
		
		if(type&4) {lmin=1.0;lmax=0;} //reset lmin-lmax
		if(type&1) {delete h_data; CreateHisto(1);}
		if(type&10) {delete h_mc; CreateHisto(2);} //8+2
		if(type&32) {varAll.clear();} //reset varAll

		for(int i=0;i<t->GetEntries() ;i++)
			{
			t->GetEntry(i);
			treeVar["ptJet0"]=jetPt[0];
			treeVar["etaJet0"]=jetEta[0];
			treeVar["rhoPF"]=treeVar["rho"];
			
			//fprintf(stderr,"A: Pt: %f<%f<%f - Eta: %f<%f<%f: Rho: %f<%f<%f\n",PtMin,treeVar["ptJet0"],PtMax,EtaMin,treeVar["etaJet0"],EtaMax,RhoMin,treeVar["rhoPF"],RhoMax);
			if((treeVar["ptJet0"]<PtMin)||(treeVar["ptJet0"]>PtMax)||(fabs(treeVar["etaJet0"])<EtaMin)||(fabs(treeVar["etaJet0"])>EtaMax)|| (treeVar["rhoPF"]<RhoMin)||(treeVar["rhoPF"]>RhoMax))continue;
			//fprintf(stderr,"-B\n");
			//selection
			double muJet_dphi=deltaPhi(jetPhi[0],jetPhi[1]);
			if(fabs(muJet_dphi)<2.5) continue;
			//fprintf(stderr,"--C\n");
			if( ! (2.0 *jetPt[2]/ (jetPt[0]+jetPt[1])<.3) )continue; 
			//fprintf(stderr,"---D\n");
			if( jetBtag[0] >0.244)continue;
			//fprintf(stderr,"----E\n");
			//trigger --only on data
			if( type==1 && !( triggerResult != NULL && triggerResult->size()>1 && triggerResult->at(1) )) continue;
			
			//parton Matching
			double dR_min=999;
			int pos_min=999;
			//int part_min=5;
			
			//fprintf(stderr,"_______not NULL: %ld = %ld = %ld\n",partonPt,partonEta,partonPhi);
			if(type>1){ //only on MC
			for(int iPart=0;iPart<partonPt->size();iPart++)
				{
				double dR_ipart= deltaR(partonEta->at(iPart),partonPhi->at(iPart),jetEta[0],jetPhi[0]);
				if(dR_ipart< dR_min){dR_min=dR_ipart;pos_min=iPart;}
				}
			}
			if(dR_min<.3){
				//fprintf(stderr,"_______%f<%f\n",pos_min,partonId->size());
				treeVarInt["pdgIdPartJet0"]=partonId->at(pos_min);
				} else treeVarInt["pdgIdPartJet0"]=0;
			
			//fprintf(stderr,"_______E2:pos_min=%d dR=%f\n",pos_min,dR_min);
			map<TString,float> variables_MLP;	
			map<TString,float> variables_corr_MLP;	
			//map<TString,float> variables_QGL;	
			
//			variables_QGL["axis1"]= jetAxis_QC[0][0];
//			variables_QGL["axis2"]= jetAxis_QC[1][0];
//			variables_QGL["ptd"] = jetPtD_QC[0];
//			variables_QGL["mult"] = jetChgPart_QC[0]+jetNeutralPart_ptcut[0];
//			variables_QGL["pt"] = jetPt[0];
//			variables_QGL["eta"] = jetEta[0];
//			variables_QGL["rho"] = rho;
	
			//fprintf(stderr,"_______E3:nvtx: %d\n",treeVarInt["nvtx"]);
			if(fabs(jetEta[0])<2.5 && (jetBeta[0]<(1.0 -0.2*TMath::Log(treeVarInt["nvtx"]-0.67)))) continue;	
			//fprintf(stderr,"-----F\n");
			float sub_data=0.0;
			if(fabs(jetEta[0])>2.5 && type==1)sub_data=1.0;
			//Variables as general variables
			treeVar["mult"]=float(jetChgPart_QC[0]+jetNeutralPart_ptcut[0])-sub_data;
			treeVar["axis1"]=jetAxis_QC[0][0];
			treeVar["axis2"]=jetAxis_QC[1][0];
			treeVar["ptD"]=jetPtD_QC[0];
			//Discriminators - only if needed -  save time
			if(varName=="QGLHisto")treeVar["QGLHisto"] = qgl->computeQGLikelihood2012(jetPt[0],jetEta[0],treeVar["rho"],jetChgPart_QC[0]+jetNeutralPart_ptcut[0]-sub_data,jetPtD_QC[0],jetAxis_QC[1][0]);
			if(varName=="QGLMLP"){	
				variables_MLP["axis1"]=jetAxis_QC[0][0];
				variables_MLP["axis2"]=jetAxis_QC[1][0];
				variables_MLP["ptD"]=jetPtD_QC[0];
				variables_MLP["mult"]=jetChgPart_QC[0];
				
				variables_MLP["pt"]=jetPt[0];
				variables_MLP["eta"]=jetEta[0];
				variables_MLP["rho"]=treeVar["rho"];
				
				if(fabs(jetEta[0])>2.5){
					variables_MLP["axis1"]=jetAxis[0][0];
					variables_MLP["axis2"]=jetAxis[1][0];
					variables_MLP["ptD"]=jetPtD[0];
					variables_MLP["mult"]=jetChgPart_QC[0]+jetNeutralPart_ptcut[0]-sub_data;
					
					}
				
				variables_corr_MLP["axis1"] = variables_MLP["axis1"];
				variables_corr_MLP["axis2"] = variables_MLP["axis2"];
				variables_corr_MLP["ptD"] = variables_MLP["ptD"];
				variables_corr_MLP["mult"] = variables_MLP["mult"];
			
				//variables_corr_MLP=qgmlp->TEST(variables_MLP,variables_corr_MLP);
				
				treeVar["QGLMLP"]=qgmlp->QGvalue(variables_MLP);
			}
			//treeVar["pdgIdPartJet0"];
			//---------------------------
		
			//fprintf(stderr,"------G\n");
			if(type&1){
				//printf("passed selection - type 1 --> %.3f - %.3f\n",treeVar[varName],treeVar[varName+"Fwd"]);
				string var=varName;
			//	if(EtaMin>2.5)var+="Fwd"; //only in data fwd
				alpha=1; beta=0;
					//int bin=puw->FindBin(treeVar["rho"]);
					//int bin2=ptetaw->FindBin(jetPt[0],fabs(jetEta[0]) );
					//float weight=puw->GetBinContent(bin) *  ptetaw->GetBinContent(bin2);
					treeVar["eventWeight"]=1.; //data
					treeVar["PUReWeight"]=1;
				FillHisto(h_data,var);
				}	
			if(type&2){
				//mc
			//fprintf(stderr,"________notNull:%ld %ld %f\n",puw,ptetaw,treeVar["rho"]);
					int bin = puw->FindBin(treeVar["rho"]);
					int bin2 = ptetaw->FindBin(jetPt[0],fabs(jetEta[0]) );
					float weight=puw->GetBinContent(bin) *  ptetaw->GetBinContent(bin2);
			//fprintf(stderr,"________bin:%d,%d, w=%f\n",bin,bin2,weight);
					treeVar["eventWeight"]=weight;treeVar["PUReWeight"]=1;
				FillHisto(h_mc,varName);
				}
			if(type&4){
				string var=varName;
				if(lmin>treeVar[var]) lmin=treeVar[var];
				if(lmax<treeVar[var]) lmax=treeVar[var];
				}
			if(type&8){
				alpha=1;beta=0;
				if( treeVarInt["pdgIdPartJet0"] ==21) {alpha=a_g;beta=b_g;}
				if( fabs(treeVarInt["pdgIdPartJet0"]) < 5) {alpha=a_q;beta=b_q;}
				if( fabs(treeVarInt["pdgIdPartJet0"])== 0) {alpha=1;beta=0;}

				FillHisto(h_mc,varName);
				}
			if(type&16){
				for(int j=0;j<int(alphaFast.size());j++)
					{
					alpha=alphaFast[j];
					beta=betaFast[j];
					FillHisto(h_mcFast[j],varName);
					}
				}
			if(type&32){ 
					int bin=puw->FindBin(treeVar["rho"]);
					int bin2=ptetaw->FindBin(jetPt[0],fabs(jetEta[0]) );
					float weight=puw->GetBinContent(bin) *  ptetaw->GetBinContent(bin2);
					varAll.push_back(TRIO(treeVarInt["pdgIdPartJet0"],treeVar[varName],weight)); //w=-1 default
				}
			}
}
void Ntp1Finalizer_ComputeQGLikelihood::finalize() {

  if( outFile_==0 ) this->createOutputFile();



  const int nBins = 18;
  Double_t ptBins[nBins+1];
  fitTools::getBins_int( nBins, ptBins, 20., 1000. );
  ptBins[nBins] = 3500.;


  std::vector<TH1D*> vh1_QGLikelihood_gluon;
  std::vector<TH1D*> vh1_QGLikelihood_quark;

  std::vector<TH1D*> vh1_QGLikelihood_norms_gluon;
  std::vector<TH1D*> vh1_QGLikelihood_norms_quark;

  std::vector<TH1D*> vh1_QGLikelihood_noptD_gluon;
  std::vector<TH1D*> vh1_QGLikelihood_noptD_quark;

  std::vector<TH1D*> vh1_QGLikelihood_norms_noptD_gluon;
  std::vector<TH1D*> vh1_QGLikelihood_norms_noptD_quark;

  std::vector<TH1D*> vh1_QGLikelihood_onlyNch_gluon;
  std::vector<TH1D*> vh1_QGLikelihood_onlyNch_quark;

  std::vector<TH1D*> vh1_rmsCand_cutOnQGLikelihood_norms_gluon;
  std::vector<TH1D*> vh1_rmsCand_cutOnQGLikelihood_norms_quark;

  std::vector<TH1D*> vh1_QGLikelihood_eta35_gluon;
  std::vector<TH1D*> vh1_QGLikelihood_eta35_quark;


  for( unsigned iBin=0; iBin<nBins; iBin++ ) {

    float ptMin = ptBins[iBin];
    float ptMax = ptBins[iBin+1];

    char histoname[500];
    
    sprintf( histoname, "QGLikelihood_gluon_pt%.0f_%.0f", ptMin, ptMax);
    TH1D* h1_QGLikelihood_gluon_new = new TH1D(histoname, "", 50, 0., 1.);
    h1_QGLikelihood_gluon_new->Sumw2();
    vh1_QGLikelihood_gluon.push_back(h1_QGLikelihood_gluon_new);

    sprintf( histoname, "QGLikelihood_quark_pt%.0f_%.0f", ptMin, ptMax);
    TH1D* h1_QGLikelihood_quark_new = new TH1D(histoname, "", 50, 0., 1.);
    h1_QGLikelihood_quark_new->Sumw2();
    vh1_QGLikelihood_quark.push_back(h1_QGLikelihood_quark_new);


    sprintf( histoname, "QGLikelihood_norms_gluon_pt%.0f_%.0f", ptMin, ptMax);
    TH1D* h1_QGLikelihood_norms_gluon_new = new TH1D(histoname, "", 50, 0., 1.);
    h1_QGLikelihood_norms_gluon_new->Sumw2();
    vh1_QGLikelihood_norms_gluon.push_back(h1_QGLikelihood_norms_gluon_new);

    sprintf( histoname, "QGLikelihood_norms_quark_pt%.0f_%.0f", ptMin, ptMax);
    TH1D* h1_QGLikelihood_norms_quark_new = new TH1D(histoname, "", 50, 0., 1.);
    h1_QGLikelihood_norms_quark_new->Sumw2();
    vh1_QGLikelihood_norms_quark.push_back(h1_QGLikelihood_norms_quark_new);


    sprintf( histoname, "QGLikelihood_noptD_gluon_pt%.0f_%.0f", ptMin, ptMax);
    TH1D* h1_QGLikelihood_noptD_gluon_new = new TH1D(histoname, "", 50, 0., 1.);
    h1_QGLikelihood_noptD_gluon_new->Sumw2();
    vh1_QGLikelihood_noptD_gluon.push_back(h1_QGLikelihood_noptD_gluon_new);

    sprintf( histoname, "QGLikelihood_noptD_quark_pt%.0f_%.0f", ptMin, ptMax);
    TH1D* h1_QGLikelihood_noptD_quark_new = new TH1D(histoname, "", 50, 0., 1.);
    h1_QGLikelihood_noptD_quark_new->Sumw2();
    vh1_QGLikelihood_noptD_quark.push_back(h1_QGLikelihood_noptD_quark_new);


    sprintf( histoname, "QGLikelihood_norms_noptD_gluon_pt%.0f_%.0f", ptMin, ptMax);
    TH1D* h1_QGLikelihood_norms_noptD_gluon_new = new TH1D(histoname, "", 50, 0., 1.);
    h1_QGLikelihood_norms_noptD_gluon_new->Sumw2();
    vh1_QGLikelihood_norms_noptD_gluon.push_back(h1_QGLikelihood_norms_noptD_gluon_new);

    sprintf( histoname, "QGLikelihood_norms_noptD_quark_pt%.0f_%.0f", ptMin, ptMax);
    TH1D* h1_QGLikelihood_norms_noptD_quark_new = new TH1D(histoname, "", 50, 0., 1.);
    h1_QGLikelihood_norms_noptD_quark_new->Sumw2();
    vh1_QGLikelihood_norms_noptD_quark.push_back(h1_QGLikelihood_norms_noptD_quark_new);


    sprintf( histoname, "QGLikelihood_onlyNch_gluon_pt%.0f_%.0f", ptMin, ptMax);
    TH1D* h1_QGLikelihood_onlyNch_gluon_new = new TH1D(histoname, "", 50, 0., 1.);
    h1_QGLikelihood_onlyNch_gluon_new->Sumw2();
    vh1_QGLikelihood_onlyNch_gluon.push_back(h1_QGLikelihood_onlyNch_gluon_new);

    sprintf( histoname, "QGLikelihood_onlyNch_quark_pt%.0f_%.0f", ptMin, ptMax);
    TH1D* h1_QGLikelihood_onlyNch_quark_new = new TH1D(histoname, "", 50, 0., 1.);
    h1_QGLikelihood_onlyNch_quark_new->Sumw2();
    vh1_QGLikelihood_onlyNch_quark.push_back(h1_QGLikelihood_onlyNch_quark_new);


    sprintf( histoname, "rmsCand_cutOnQGLikelihood_norms_gluon_pt%.0f_%.0f", ptMin, ptMax);
    TH1D* h1_rmsCand_cutOnQGLikelihood_norms_gluon_new = new TH1D(histoname, "", 100, 0., 0.1 );
    h1_rmsCand_cutOnQGLikelihood_norms_gluon_new->Sumw2();
    vh1_rmsCand_cutOnQGLikelihood_norms_gluon.push_back(h1_rmsCand_cutOnQGLikelihood_norms_gluon_new);

    sprintf( histoname, "rmsCand_cutOnQGLikelihood_norms_quark_pt%.0f_%.0f", ptMin, ptMax);
    TH1D* h1_rmsCand_cutOnQGLikelihood_norms_quark_new = new TH1D(histoname, "", 100, 0., 0.1 );
    h1_rmsCand_cutOnQGLikelihood_norms_quark_new->Sumw2();
    vh1_rmsCand_cutOnQGLikelihood_norms_quark.push_back(h1_rmsCand_cutOnQGLikelihood_norms_quark_new);

    sprintf( histoname, "QGLikelihood_eta35_gluon_pt%.0f_%.0f", ptMin, ptMax);
    TH1D* h1_QGLikelihood_eta35_gluon_new = new TH1D(histoname, "", 50, 0., 1.);
    h1_QGLikelihood_eta35_gluon_new->Sumw2();
    vh1_QGLikelihood_eta35_gluon.push_back(h1_QGLikelihood_eta35_gluon_new);

    sprintf( histoname, "QGLikelihood_eta35_quark_pt%.0f_%.0f", ptMin, ptMax);
    TH1D* h1_QGLikelihood_eta35_quark_new = new TH1D(histoname, "", 50, 0., 1.);
    h1_QGLikelihood_eta35_quark_new->Sumw2();
    vh1_QGLikelihood_eta35_quark.push_back(h1_QGLikelihood_eta35_quark_new);



  } //for bins


  Int_t run;
  tree_->SetBranchAddress("run", &run);
  Float_t eventWeight;
  tree_->SetBranchAddress("eventWeight", &eventWeight);
  Float_t rhoPF;
  tree_->SetBranchAddress("rhoPF", &rhoPF);

  Float_t ptHat;
  tree_->SetBranchAddress("ptHat", &ptHat);

  Int_t nJet;
  tree_->SetBranchAddress("nJet", &nJet);
  Float_t eJet[20];
  tree_->SetBranchAddress("eJet", eJet);
  Float_t ptJet[20];
  tree_->SetBranchAddress("ptJet", ptJet);
  Float_t etaJet[20];
  tree_->SetBranchAddress("etaJet", etaJet);
  Float_t phiJet[20];
  tree_->SetBranchAddress("phiJet", phiJet);
  Int_t nChargedJet[20];
  tree_->SetBranchAddress("nChargedJet", nChargedJet);
  Int_t nNeutralJet[20];
  tree_->SetBranchAddress("nNeutralJet", nNeutralJet);
  Float_t rmsCandJet[20];
  tree_->SetBranchAddress("rmsCandJet", rmsCandJet);
  Float_t ptDJet[20];
  tree_->SetBranchAddress("ptDJet", ptDJet);

  Int_t nPart;
  tree_->SetBranchAddress("nPart", &nPart);
  Float_t ePart[20];
  tree_->SetBranchAddress("ePart", ePart);
  Float_t ptPart[20];
  tree_->SetBranchAddress("ptPart", ptPart);
  Float_t etaPart[20];
  tree_->SetBranchAddress("etaPart", etaPart);
  Float_t phiPart[20];
  tree_->SetBranchAddress("phiPart", phiPart);
  Int_t pdgIdPart[20];
  tree_->SetBranchAddress("pdgIdPart", pdgIdPart);


  //QGLikelihoodCalculator *qglikeli = new QGLikelihoodCalculator("QG_QCD_Pt_15to3000_TuneZ2_Flat_7TeV_pythia6_Fall10.root", nBins);
  //QGLikelihoodCalculator *qglikeli = new QGLikelihoodCalculator("QG_QCD_Pt_15to3000_TuneZ2_Flat_7TeV_pythia6_Spring11-PU_S1_START311_V1G1-v1.root", nBins);
  QGLikelihoodCalculator *qglikeli = new QGLikelihoodCalculator("QG_QCD_Pt-15to3000_TuneZ2_Flat_7TeV_pythia6_Summer11-PU_S3_START42_V11-v2.root", nBins);


  int nEntries = tree_->GetEntries();
  //nEntries = 10000;

  for(int iEntry=0; iEntry<nEntries; ++iEntry) {
  //for(int iEntry=0; iEntry<500000; ++iEntry) {

    if( (iEntry % 100000)==0 ) std::cout << "Entry: " << iEntry << " /" << nEntries << std::endl;

    tree_->GetEntry(iEntry);

    if( eventWeight <= 0. ) eventWeight = 1.;


    //for( unsigned iJet=0; iJet<nJet; ++iJet ) {
    for( unsigned iJet=0; (iJet<nJet && iJet<3); ++iJet ) { //only 3 leading jets considered

      TLorentzVector thisJet;
      thisJet.SetPtEtaPhiE( ptJet[iJet], etaJet[iJet], phiJet[iJet], eJet[iJet]);

      float deltaRmin=999.;
      int partFlavor=-1;
      TLorentzVector foundPart;

      for( unsigned iPart=0; iPart<nPart; iPart++ ) {

        TLorentzVector thisPart;
        thisPart.SetPtEtaPhiE( ptPart[iPart], etaPart[iPart], phiPart[iPart], ePart[iPart]);

        float thisDeltaR = thisJet.DeltaR(thisPart);

        if( thisDeltaR < deltaRmin ) {
          deltaRmin = thisDeltaR;
          partFlavor = pdgIdPart[iPart];
          foundPart = thisPart;
        }

      } //for partons

      if( deltaRmin > 0.5 ) continue;
      //if( deltaRmin > 0.5 ) partFlavor=21; //lets try this


      int thisBin=-1;
      if( thisJet.Pt() > ptBins[nBins] ) {
        thisBin = nBins-1;
      } else {
        for( unsigned int iBin=0; iBin<nBins; ++iBin ) {
          if( thisJet.Pt()>ptBins[iBin] && thisJet.Pt()<ptBins[iBin+1] ) {
            thisBin = iBin;
            break;
          }
        }
      }

      if( thisBin==-1 ) continue;

    //float QGLikelihood = qglikeli->computeQGLikelihood( thisJet.Pt(), nChargedJet[iJet], nNeutralJet[iJet], ptDJet[iJet], rmsCandJet[iJet] );
    //float QGLikelihood_norms = qglikeli->computeQGLikelihood( thisJet.Pt(), nChargedJet[iJet], nNeutralJet[iJet], ptDJet[iJet], -1. );
    //float QGLikelihood_noptD = qglikeli->computeQGLikelihood( thisJet.Pt(), nChargedJet[iJet], nNeutralJet[iJet], -1., rmsCandJet[iJet] );
    //float QGLikelihood_norms_noptD = qglikeli->computeQGLikelihood( thisJet.Pt(), nChargedJet[iJet], nNeutralJet[iJet], -1., -1. );
    //float QGLikelihood_onlyNch = qglikeli->computeQGLikelihood( thisJet.Pt(), nChargedJet[iJet], -1., -1., -1. );


      // change to new var: -ln rmsCand:
      rmsCandJet[iJet] = -log(rmsCandJet[iJet]);

      if( fabs(thisJet.Eta())<2. ) {

        float QGLikelihood = qglikeli->computeQGLikelihoodPU( thisJet.Pt(), rhoPF, nChargedJet[iJet], nNeutralJet[iJet], ptDJet[iJet], rmsCandJet[iJet] );
        float QGLikelihood_norms = qglikeli->computeQGLikelihoodPU( thisJet.Pt(), rhoPF, nChargedJet[iJet], nNeutralJet[iJet], ptDJet[iJet], -1. );
    
        float QGLikelihood_noptD = -1.;
        float QGLikelihood_norms_noptD = -1.;
        float QGLikelihood_onlyNch = -1.;
    
        //float QGLikelihood_noptD = qglikeli->computeQGLikelihoodPU( thisJet.Pt(), rhoPF, nChargedJet[iJet], nNeutralJet[iJet], -1., rmsCandJet[iJet] );
        //float QGLikelihood_norms_noptD = qglikeli->computeQGLikelihoodPU( thisJet.Pt(), rhoPF, nChargedJet[iJet], nNeutralJet[iJet], -1., -1. );
        //float QGLikelihood_onlyNch = qglikeli->computeQGLikelihoodPU( thisJet.Pt(), rhoPF, nChargedJet[iJet], -1., -1., -1. );
    
        if( abs(partFlavor)< 4 ) { //light quark
          
          vh1_QGLikelihood_quark[thisBin]->Fill( QGLikelihood, eventWeight );
          vh1_QGLikelihood_norms_quark[thisBin]->Fill( QGLikelihood_norms, eventWeight );
          vh1_QGLikelihood_noptD_quark[thisBin]->Fill( QGLikelihood_noptD, eventWeight );
          vh1_QGLikelihood_norms_noptD_quark[thisBin]->Fill( QGLikelihood_norms_noptD, eventWeight );
          if( QGLikelihood_norms<0.9 ) vh1_rmsCand_cutOnQGLikelihood_norms_quark[thisBin]->Fill( rmsCandJet[iJet], eventWeight );
          vh1_QGLikelihood_onlyNch_quark[thisBin]->Fill( QGLikelihood_onlyNch, eventWeight );
    
        } else if( partFlavor==21 ) { //gluon
    
          vh1_QGLikelihood_gluon[thisBin]->Fill( QGLikelihood, eventWeight );
          vh1_QGLikelihood_norms_gluon[thisBin]->Fill( QGLikelihood_norms, eventWeight );
          vh1_QGLikelihood_noptD_gluon[thisBin]->Fill( QGLikelihood_noptD, eventWeight );
          vh1_QGLikelihood_norms_noptD_gluon[thisBin]->Fill( QGLikelihood_norms_noptD, eventWeight );
          if( QGLikelihood_norms<0.9 ) vh1_rmsCand_cutOnQGLikelihood_norms_gluon[thisBin]->Fill( rmsCandJet[iJet], eventWeight );
          vh1_QGLikelihood_onlyNch_gluon[thisBin]->Fill( QGLikelihood_onlyNch, eventWeight );
    
        }

      } else if( fabs(thisJet.Eta())>3. && fabs(thisJet.Eta())<5. ) {

        float QGLikelihood = qglikeli->computeQGLikelihoodFwd( thisJet.Pt(), rhoPF, ptDJet[iJet], rmsCandJet[iJet] );
    
        if( abs(partFlavor)< 4 ) { //light quark
          
          vh1_QGLikelihood_eta35_quark[thisBin]->Fill( QGLikelihood, eventWeight );
    
        } else if( partFlavor==21 ) { //gluon
    
          vh1_QGLikelihood_eta35_gluon[thisBin]->Fill( QGLikelihood, eventWeight );

        } //if gluon

      } //if eta
    
    } // for jets


  } //for entries


  std::vector<TGraphErrors*> vgr_eff_vs_rej;
  std::vector<TGraphErrors*> vgr_eff_vs_rej_norms;
  std::vector<TGraphErrors*> vgr_eff_vs_rej_noptD;
  std::vector<TGraphErrors*> vgr_eff_vs_rej_norms_noptD;
  std::vector<TGraphErrors*> vgr_eff_vs_rej_onlyNch;

  for( unsigned iBin=0; iBin<nBins; ++iBin ) {

    float ptMin = ptBins[iBin];
    float ptMax = ptBins[iBin+1];

    char graphName[200];
    sprintf( graphName, "eff_vs_rej_pt%.0f_%.0f", ptMin, ptMax );
    TGraphErrors* newGraph = new TGraphErrors(0);
    newGraph->SetName(graphName);
    vgr_eff_vs_rej.push_back(newGraph);

    sprintf( graphName, "eff_vs_rej_norms_pt%.0f_%.0f", ptMin, ptMax );
    TGraphErrors* newGraph_norms = new TGraphErrors(0);
    newGraph_norms->SetName(graphName);
    vgr_eff_vs_rej_norms.push_back(newGraph_norms);

    sprintf( graphName, "eff_vs_rej_noptD_pt%.0f_%.0f", ptMin, ptMax );
    TGraphErrors* newGraph_noptD = new TGraphErrors(0);
    newGraph_noptD->SetName(graphName);
    vgr_eff_vs_rej_noptD.push_back(newGraph_noptD);

    sprintf( graphName, "eff_vs_rej_norms_noptD_pt%.0f_%.0f", ptMin, ptMax );
    TGraphErrors* newGraph_norms_noptD = new TGraphErrors(0);
    newGraph_norms_noptD->SetName(graphName);
    vgr_eff_vs_rej_norms_noptD.push_back(newGraph_norms_noptD);

    sprintf( graphName, "eff_vs_rej_onlyNch_pt%.0f_%.0f", ptMin, ptMax );
    TGraphErrors* newGraph_onlyNch = new TGraphErrors(0);
    newGraph_onlyNch->SetName(graphName);
    vgr_eff_vs_rej_onlyNch.push_back(newGraph_onlyNch);

  }


  // compute eff-rej curves:
  for( unsigned iBin=0; iBin<nBins; ++iBin ) {

    for( unsigned iHistoBin=1; iHistoBin<vh1_QGLikelihood_quark[iBin]->GetNbinsX(); ++iHistoBin ) {

      float eff_q = vh1_QGLikelihood_quark[iBin]->Integral(iHistoBin, vh1_QGLikelihood_quark[iBin]->GetNbinsX())/vh1_QGLikelihood_quark[iBin]->Integral(1, vh1_QGLikelihood_quark[iBin]->GetNbinsX());
      float eff_g = vh1_QGLikelihood_gluon[iBin]->Integral(iHistoBin, vh1_QGLikelihood_gluon[iBin]->GetNbinsX())/vh1_QGLikelihood_gluon[iBin]->Integral(1, vh1_QGLikelihood_gluon[iBin]->GetNbinsX());

      float eff_q_norms = vh1_QGLikelihood_norms_quark[iBin]->Integral(iHistoBin, vh1_QGLikelihood_norms_quark[iBin]->GetNbinsX())/vh1_QGLikelihood_norms_quark[iBin]->Integral(1, vh1_QGLikelihood_norms_quark[iBin]->GetNbinsX());
      float eff_g_norms = vh1_QGLikelihood_norms_gluon[iBin]->Integral(iHistoBin, vh1_QGLikelihood_norms_gluon[iBin]->GetNbinsX())/vh1_QGLikelihood_norms_gluon[iBin]->Integral(1, vh1_QGLikelihood_norms_gluon[iBin]->GetNbinsX());

      float eff_q_noptD = vh1_QGLikelihood_noptD_quark[iBin]->Integral(iHistoBin, vh1_QGLikelihood_noptD_quark[iBin]->GetNbinsX())/vh1_QGLikelihood_noptD_quark[iBin]->Integral(1, vh1_QGLikelihood_noptD_quark[iBin]->GetNbinsX());
      float eff_g_noptD = vh1_QGLikelihood_noptD_gluon[iBin]->Integral(iHistoBin, vh1_QGLikelihood_noptD_gluon[iBin]->GetNbinsX())/vh1_QGLikelihood_noptD_gluon[iBin]->Integral(1, vh1_QGLikelihood_noptD_gluon[iBin]->GetNbinsX());

      float eff_q_norms_noptD = vh1_QGLikelihood_norms_noptD_quark[iBin]->Integral(iHistoBin, vh1_QGLikelihood_norms_noptD_quark[iBin]->GetNbinsX())/vh1_QGLikelihood_norms_noptD_quark[iBin]->Integral(1, vh1_QGLikelihood_norms_noptD_quark[iBin]->GetNbinsX());
      float eff_g_norms_noptD = vh1_QGLikelihood_norms_noptD_gluon[iBin]->Integral(iHistoBin, vh1_QGLikelihood_norms_noptD_gluon[iBin]->GetNbinsX())/vh1_QGLikelihood_norms_noptD_gluon[iBin]->Integral(1, vh1_QGLikelihood_norms_noptD_gluon[iBin]->GetNbinsX());

      float eff_q_onlyNch = vh1_QGLikelihood_onlyNch_quark[iBin]->Integral(iHistoBin, vh1_QGLikelihood_onlyNch_quark[iBin]->GetNbinsX())/vh1_QGLikelihood_onlyNch_quark[iBin]->Integral(1, vh1_QGLikelihood_onlyNch_quark[iBin]->GetNbinsX());
      float eff_g_onlyNch = vh1_QGLikelihood_onlyNch_gluon[iBin]->Integral(iHistoBin, vh1_QGLikelihood_onlyNch_gluon[iBin]->GetNbinsX())/vh1_QGLikelihood_onlyNch_gluon[iBin]->Integral(1, vh1_QGLikelihood_onlyNch_gluon[iBin]->GetNbinsX());

      vgr_eff_vs_rej[iBin]->SetPoint( vgr_eff_vs_rej[iBin]->GetN(), eff_q, 1.-eff_g ); 
      vgr_eff_vs_rej_norms[iBin]->SetPoint( vgr_eff_vs_rej_norms[iBin]->GetN(), eff_q_norms, 1.-eff_g_norms ); 
      vgr_eff_vs_rej_noptD[iBin]->SetPoint( vgr_eff_vs_rej_noptD[iBin]->GetN(), eff_q_noptD, 1.-eff_g_noptD ); 
      vgr_eff_vs_rej_norms_noptD[iBin]->SetPoint( vgr_eff_vs_rej_norms_noptD[iBin]->GetN(), eff_q_norms_noptD, 1.-eff_g_norms_noptD ); 
      vgr_eff_vs_rej_onlyNch[iBin]->SetPoint( vgr_eff_vs_rej_onlyNch[iBin]->GetN(), eff_q_onlyNch, 1.-eff_g_onlyNch ); 

    }

  }


  outFile_->cd();


  for( unsigned iBin=0; iBin<nBins; ++iBin ) {

    vh1_QGLikelihood_gluon[iBin]->Write();
    vh1_QGLikelihood_quark[iBin]->Write();

    vh1_QGLikelihood_norms_gluon[iBin]->Write();
    vh1_QGLikelihood_norms_quark[iBin]->Write();

    vh1_QGLikelihood_noptD_gluon[iBin]->Write();
    vh1_QGLikelihood_noptD_quark[iBin]->Write();

    vh1_QGLikelihood_norms_noptD_gluon[iBin]->Write();
    vh1_QGLikelihood_norms_noptD_quark[iBin]->Write();

    vh1_QGLikelihood_onlyNch_gluon[iBin]->Write();
    vh1_QGLikelihood_onlyNch_quark[iBin]->Write();

    vh1_QGLikelihood_eta35_gluon[iBin]->Write();
    vh1_QGLikelihood_eta35_quark[iBin]->Write();

    vh1_rmsCand_cutOnQGLikelihood_norms_gluon[iBin]->Write();
    vh1_rmsCand_cutOnQGLikelihood_norms_quark[iBin]->Write();

    vgr_eff_vs_rej[iBin]->Write();
    vgr_eff_vs_rej_norms[iBin]->Write();
    vgr_eff_vs_rej_noptD[iBin]->Write();
    vgr_eff_vs_rej_norms_noptD[iBin]->Write();
    vgr_eff_vs_rej_onlyNch[iBin]->Write();

  }

  outFile_->Close();

}