Problem with TEfficiency when using TH1F with negative weights!

Dear Experts,

I am trying to plot kaon efficiency and fake rate plots , along side kaon roc curve. Now I am using sPlot technique to separate signal and background. Now my sWeights go from around -1.2 to 1.0.
So, i only want to take positive values and set the values < 0 as “0.01”(not zero, as TEffieciency gives an error).

I tried using the following example to set the bin content to 0.01 to force the x-axis to take the value as 0.01.

#include <vector>

void kaon_eff_fr_svd_mc_zero()
{
    TChain *chain = new TChain("tree");
    chain->Add("/home/g_vikas_raj/Downloads/root/SVD/dedxcalibration/validation/Dstar_validation_MC.root"); 
    
    double DST_D0_K_P, DST_D0_K_kaonID_ALL, DST_D0_K_pionID_ALL, DST_D0_K_kaon_pion_ALL, DST_D0_K_pion_kaon_ALL,DST_D0_K_kaonID_SVD, DST_D0_K_pionID_SVD, DST_D0_K_kaon_pion_SVD, DST_D0_K_pion_kaon_SVD,
    DST_D0_K_kaonID_WTSVD, DST_D0_K_pionID_WTSVD, DST_D0_K_kaon_pion_WTSVD, DST_D0_K_pion_kaon_WTSVD;
    
    double nsig_sw;

    chain->SetBranchAddress("DST_D0_K_P",&DST_D0_K_P);
    chain->SetBranchAddress("DST_D0_K_kaonID_ALL",&DST_D0_K_kaonID_ALL);
    chain->SetBranchAddress("DST_D0_K_pionID_ALL",&DST_D0_K_pionID_ALL);
    chain->SetBranchAddress("DST_D0_K_kaon_pion_ALL",&DST_D0_K_kaon_pion_ALL);
    chain->SetBranchAddress("DST_D0_K_pion_kaon_ALL",&DST_D0_K_pion_kaon_ALL);
    chain->SetBranchAddress("DST_D0_K_kaonID_SVD", &DST_D0_K_kaonID_SVD);
    chain->SetBranchAddress("DST_D0_K_pionID_SVD", &DST_D0_K_pionID_SVD);
    chain->SetBranchAddress("DST_D0_K_kaon_pion_SVD",&DST_D0_K_kaon_pion_SVD);
    chain->SetBranchAddress("DST_D0_K_pion_kaon_SVD",&DST_D0_K_pion_kaon_SVD);
    chain->SetBranchAddress("DST_D0_K_kaonID_WTSVD", &DST_D0_K_kaonID_WTSVD);
    chain->SetBranchAddress("DST_D0_K_pionID_WTSVD", &DST_D0_K_pionID_WTSVD);
    chain->SetBranchAddress("DST_D0_K_kaon_pion_WTSVD",&DST_D0_K_kaon_pion_WTSVD);
    chain->SetBranchAddress("DST_D0_K_pion_kaon_WTSVD",&DST_D0_K_pion_kaon_WTSVD);
    
    chain->SetBranchAddress("nsig_sw", &nsig_sw);

    TH1F *kaon_hist_all = new TH1F("kaon_hist_all", "", 30, 0.1, 1.6);
    kaon_hist_all->GetXaxis()->SetTitle("p (GeV)");
    kaon_hist_all->GetYaxis()->SetTitle("K efficiency/ #pi fake rate");

    TH1F *kaon_hist_passed_kaon = new TH1F("kaon_hist_passed_kaon", "", 30, 0.1, 1.6);
    kaon_hist_passed_kaon->GetXaxis()->SetTitle("p (GeV)");
    kaon_hist_passed_kaon->GetYaxis()->SetTitle("K efficiency/ #pi fake rate");

    TH1F *kaon_hist_passed_pion = new TH1F("kaon_hist_passed_pion", "", 30, 0.1, 1.6);
    kaon_hist_passed_pion->GetXaxis()->SetTitle("p (GeV)");
    kaon_hist_passed_pion->GetYaxis()->SetTitle("K efficiency/ #pi fake rate");
    
    // kaon efficiency and pion fake rate
    for (int i = 0; i < chain->GetEntries(); i++) {   
    chain->GetEntry(i);
    if (nsig_sw >= 0) {
        kaon_hist_all->Fill(DST_D0_K_P, nsig_sw);
        if (DST_D0_K_kaon_pion_SVD > 0.5) {
            kaon_hist_passed_kaon->Fill(DST_D0_K_P, nsig_sw);
        }
        if (DST_D0_K_pion_kaon_SVD > 0.5) {
            kaon_hist_passed_pion->Fill(DST_D0_K_P, nsig_sw);
        }
    } else { // For nsig_sw < 0, fill the histogram with a specific value (0.01)
        int binNumber = kaon_hist_all->FindBin(DST_D0_K_P); // Find the bin corresponding to DST_D0_K_P
        kaon_hist_all->SetBinContent(binNumber, 0.01);
    }
}
    
    TEfficiency *kaon_efficiency1 = new TEfficiency(*kaon_hist_passed_kaon, *kaon_hist_all); // kaon efficiency 
    TEfficiency *pion_fr1 = new TEfficiency(*kaon_hist_passed_pion, *kaon_hist_all);        // pion fake rate
    
    kaon_efficiency1->SetStatisticOption(TEfficiency::kFNormal);
    pion_fr1->SetStatisticOption(TEfficiency::kFNormal);

    TLegend *legend1 = new TLegend(0.4, 0.4, 0.6, 0.6);
    legend1->SetBorderSize(0); 
    legend1->AddEntry(kaon_efficiency1, "Kaon Efficiency", "p");
    legend1->AddEntry(pion_fr1, "Pion Fake Rate", "p");

    kaon_efficiency1->SetMarkerColor(kBlue);
    kaon_efficiency1->SetMarkerSize(1.5);
    kaon_efficiency1->SetMarkerStyle(22);
    pion_fr1->SetMarkerColor(kRed);
    pion_fr1->SetMarkerSize(1.5);
    pion_fr1->SetMarkerStyle(23);

    TCanvas *c1 = new TCanvas();
    c1->cd();
    kaon_efficiency1->Draw();
    pion_fr1->Draw("same");
    legend1->Draw();

    c1->Update();
    kaon_efficiency1->GetPaintedGraph()->GetYaxis()->SetRangeUser(0.0, 1.0);
    pion_fr1->GetPaintedGraph()->GetYaxis()->SetRangeUser(0.0, 1.0);
 
    c1->SaveAs("kaon_efficiency_and_pion_fake_rate_all_mc_correct_scaled_zero.png");

    delete kaon_efficiency1;
    delete pion_fr1;
    delete legend1;
    delete c1;
}

But, I am getting the following error

.x kaon_eff_fr_svd_mc_zero.C

Info in <TROOT::TEfficiency::CheckEntries>: Histograms are not consistent: passed bin content > total bin content
Error in <TROOT::TEfficiency::CheckConsistency>: passed TEfficiency objects do not have consistent bin contents
Error in <TEfficiency::TEfficiency(const TH1&,const TH1&)>: histograms are not consistent -> results are useless
Warning in <TEfficiency::TEfficiency(const TH1&,const TH1&)>: using two empty TH1D('h1','h1',10,0,10)

Info in <TCanvas::Print>: file kaon_efficiency_and_pion_fake_rate_all_mc_correct_scaled_zero.png has been created

Now, I am not sure how to handle these negative weights so as to fill them with 0.01.

Thanks in advance!

Hi,

Adding @moneta in the loop.

Cheers,
D

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.