Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:33:09

0001 // name of analyzer
0002 static const char *directory = "analyzeBJetVertex";
0003 
0004 static const char *algos[] = { "distSig", "mass", 0 };
0005 
0006 static const char *algoNames[] = {
0007     "flight distance significance",
0008     "vertex mass",
0009     0
0010 };
0011 
0012 static const char *flavours[] = { "b", "c", "udsg", 0 };
0013 
0014 void patBJetVertex_efficiencies()
0015 {
0016     // define proper canvas style
0017     setNiceStyle();
0018     gStyle->SetOptStat(0);
0019 
0020     // open file
0021     TFile* file = new TFile("analyzePatBJetVertex.root");
0022 
0023     unsigned int j = 0;
0024     for(const char **algo = algos; *algo; algo++, j++) {
0025         TLegend *legend[3] = { 0, 0, 0 };
0026 
0027         // draw canvas with efficiencies
0028         TCanvas *canv;
0029         canv = new TCanvas(*algo, Form("%s efficiencies", algoNames[j]), 800, 300);
0030         canv->Divide(3, 1);
0031 
0032         TH1 *total = (TH1*)file->Get(Form("%s/flavours", directory));
0033         TH1 *effVsCutB = 0;
0034         unsigned int i = 0;
0035         for(const char **flavour = flavours; *flavour; flavour++, i++) {
0036             TH1 *h = (TH1*)file->Get(Form("%s/%s_%s", directory, *algo, *flavour));
0037             TH1 *discrShape = (TH1*)h->Clone(Form("%s_discrShape", h->GetName()));
0038             discrShape->Scale(1.0 / discrShape->Integral());
0039             discrShape->SetMaximum(discrShape->GetMaximum() * 5);
0040             TH1 *effVsCut = computeEffVsCut(h, total->GetBinContent(4 - i));
0041             TH1 *effVsBEff = 0;
0042 
0043             if (flavour == flavours)    // b-jets
0044                 effVsCutB = effVsCut;
0045             else
0046                 effVsBEff = computeEffVsBEff(effVsCut, effVsCutB);
0047 
0048             discrShape->SetTitle("discriminator shape");
0049             effVsCut->SetTitle("efficiency versus discriminator cut");
0050             if (effVsBEff)
0051                 effVsBEff->SetTitle("mistag versus b efficiency");
0052 
0053             setHistStyle(discrShape);
0054             setHistStyle(effVsCut);
0055             setHistStyle(effVsBEff);
0056 
0057             canv->cd(1);
0058             gPad->SetLogy(1);
0059             gPad->SetGridy(1);
0060             discrShape->SetLineColor(i + 1);
0061             discrShape->SetMarkerColor(i + 1);
0062             discrShape->Draw(i > 0 ? "same" : "");
0063             if (!legend[0])
0064                 legend[0] = new TLegend(0.5, 0.7, 0.78, 0.88);
0065             legend[0]->AddEntry(discrShape, *flavour);
0066 
0067             canv->cd(2);
0068             gPad->SetLogy(1);
0069             gPad->SetGridy(1);
0070             effVsCut->SetLineColor(i + 1);
0071             effVsCut->SetMarkerColor(i + 1);
0072             effVsCut->Draw(i > 0 ? "same" : "");
0073             if (!legend[1])
0074                 legend[1] = new TLegend(0.3, 0.4, 0.58, 0.58);
0075             legend[1]->AddEntry(effVsCut, *flavour);
0076 
0077             if (!effVsBEff)
0078                 continue;
0079             canv->cd(3);
0080             gPad->SetLogy(1);
0081             gPad->SetGridx(1);
0082             gPad->SetGridy(1);
0083             effVsBEff->SetLineColor(i + 1);
0084             effVsBEff->SetMarkerColor(i + 1);
0085             effVsBEff->Draw(i > 1 ? "same" : "");
0086             if (!legend[2])
0087                 legend[2] = new TLegend(0.12, 0.7, 0.40, 0.88);
0088             legend[2]->AddEntry(effVsBEff, *flavour);
0089         }
0090 
0091         canv->cd(1);
0092         legend[0]->Draw();
0093 
0094         canv->cd(2);
0095         legend[1]->Draw();
0096 
0097         canv->cd(3);
0098         legend[2]->Draw();
0099     }
0100 }
0101 
0102 TH1 *computeEffVsCut(TH1 *discrShape, double total)
0103 {
0104     TH1 *h = discrShape->Clone(Form("%s_effVsCut", discrShape->GetName()));
0105     h->Sumw2();
0106     h->SetMaximum(1.5);
0107     h->SetMinimum(1e-3);
0108 
0109     unsigned int n = h->GetNbinsX();
0110     for(unsigned int bin = 1; bin <= n; bin++) {
0111         double efficiency = h->Integral(bin, n + 1) / total;
0112         double error = sqrt(efficiency * (1 - efficiency) / total);
0113         h->SetBinContent(bin, efficiency);
0114         h->SetBinError(bin, error);
0115     }
0116 
0117     return h;
0118 }
0119 
0120 TH1 *computeEffVsBEff(TH1 *effVsCut, TH1 *effVsCutB)
0121 {
0122     TH1 *h = new TH1F(Form("%s_effVsBEff", effVsCut->GetName()), "effVsBEff",
0123                       100, 0, 1);
0124     h->SetMaximum(1.5);
0125     h->SetMinimum(1e-3);
0126 
0127     unsigned int n = effVsCut->GetNbinsX();
0128     for(unsigned int bin = 1; bin <= n; bin++) {
0129         double eff = effVsCut->GetBinContent(bin);
0130         double error = effVsCut->GetBinError(bin);
0131         double effB = effVsCutB->GetBinContent(bin);
0132 
0133         h->SetBinContent(h->FindBin(effB), eff);
0134         h->SetBinError(h->FindBin(effB), error);
0135         // FIXME: The error in effB is not propagated
0136     }
0137 
0138     return h;
0139 }
0140 
0141 void setAxisStyle(TH1 *hist) {
0142     // --------------------------------------------------
0143     // define proper axsis style for a given histogram
0144     // --------------------------------------------------
0145     hist->GetXaxis()->SetTitleSize( 0.06);
0146     hist->GetXaxis()->SetTitleColor( 1);
0147     hist->GetXaxis()->SetTitleOffset( 0.8);
0148     hist->GetXaxis()->SetTitleFont( 62);
0149     hist->GetXaxis()->SetLabelSize( 0.05);
0150     hist->GetXaxis()->SetLabelFont( 62);
0151     hist->GetXaxis()->CenterTitle();
0152     hist->GetXaxis()->SetNdivisions( 505);
0153 
0154     hist->GetYaxis()->SetTitleSize( 0.07);
0155     hist->GetYaxis()->SetTitleColor( 1);
0156     hist->GetYaxis()->SetTitleOffset( 0.5);
0157     hist->GetYaxis()->SetTitleFont( 62);
0158     hist->GetYaxis()->SetLabelSize( 0.05);
0159     hist->GetYaxis()->SetLabelFont( 62);
0160 }
0161 
0162 void setHistStyle(TH1 *hist)
0163 {
0164     if (!hist)
0165         return;
0166 
0167     // --------------------------------------------------
0168     // define proper histogram style
0169     // --------------------------------------------------
0170     setAxisStyle(hist);
0171     hist->GetXaxis()->SetTitle(hist->GetTitle());
0172     hist->SetTitle();
0173     hist->SetLineColor(4.);
0174     hist->SetLineWidth(2.);
0175     hist->SetMarkerSize(0.75);
0176     hist->SetMarkerColor(4.);
0177     hist->SetMarkerStyle(20.);
0178 }
0179 
0180 void setNiceStyle() 
0181 {
0182     gROOT->SetStyle("Plain");
0183 
0184     // --------------------------------------------------
0185     // define proper canvas style
0186     // --------------------------------------------------
0187     TStyle *MyStyle = new TStyle ("MyStyle", "My style for nicer plots");
0188     
0189     Float_t xoff = MyStyle->GetLabelOffset("X"),
0190             yoff = MyStyle->GetLabelOffset("Y"),
0191             zoff = MyStyle->GetLabelOffset("Z");
0192 
0193     MyStyle->SetCanvasBorderMode ( 0 );
0194     MyStyle->SetPadBorderMode    ( 0 );
0195     MyStyle->SetPadColor         ( 0 );
0196     MyStyle->SetCanvasColor      ( 0 );
0197     MyStyle->SetTitleColor       ( 0 );
0198     MyStyle->SetStatColor        ( 0 );
0199     MyStyle->SetTitleBorderSize  ( 0 );
0200     MyStyle->SetTitleFillColor   ( 0 );
0201     MyStyle->SetTitleH        ( 0.07 );
0202     MyStyle->SetTitleW        ( 1.00 );
0203     MyStyle->SetTitleFont     (  132 );
0204 
0205     MyStyle->SetLabelOffset (1.5*xoff, "X");
0206     MyStyle->SetLabelOffset (1.5*yoff, "Y");
0207     MyStyle->SetLabelOffset (1.5*zoff, "Z");
0208 
0209     MyStyle->SetTitleOffset (0.9,      "X");
0210     MyStyle->SetTitleOffset (0.9,      "Y");
0211     MyStyle->SetTitleOffset (0.9,      "Z");
0212 
0213     MyStyle->SetTitleSize   (0.045,    "X");
0214     MyStyle->SetTitleSize   (0.045,    "Y");
0215     MyStyle->SetTitleSize   (0.045,    "Z");
0216 
0217     MyStyle->SetLabelFont   (132,      "X");
0218     MyStyle->SetLabelFont   (132,      "Y");
0219     MyStyle->SetLabelFont   (132,      "Z");
0220 
0221     MyStyle->SetPalette(1);
0222 
0223     MyStyle->cd();
0224 }