Beispiel #1
0
def main():

    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("-i", "--inputfile", dest="inputfile")
    parser.add_option("-N", "--multiplicity", dest="N", type="int", default=3)
    parser.add_option("-x", "--exclusive", action="store_true",\
          dest="isExclusive", default=False)
    parser.add_option("-l", "--label", dest="label", type="string", default="")
    (options, args) = parser.parse_args()

    N = options.N
    isExclusive = options.isExclusive
    label_text = options.label

    if isExclusive and not (N == 2 or N == 3):
        parser.error("Exclusive plot only for N =2 or 3")

    import configurations as config
    from ROOT import TFile, TCanvas, THStack, TLegend, TPaveText, gStyle
    from ModelParser import ModelKey

    gStyle.SetPadTopMargin(0.05)
    gStyle.SetPadRightMargin(0.05)

    suffix = ""
    if not isExclusive:
        suffix = "up"

    sm_files = []
    for model in config.sm_models:
        f = TFile("%s/%s.root" % (config.sm_dir, model), "READ")
        sm_files.append(f)

    bh_weights = []
    bh_files = []
    from BHXsec import BHXsec
    xsec = BHXsec()
    for model in config.bh_showcase:
        f = TFile("%s/%s.root" % (config.bh_dir, model), "READ")
        bh_files.append(f)
        h = f.Get("plotsNoCut/ST")
        nEvents = h.GetEntries()
        bh_weights.append(
            xsec.get(model) / nEvents * config.integrated_luminosity)

    c = TCanvas("ST_Mul%d%s" % (N, suffix), "ST_Mul%d%s" % (N, suffix), 500,
                500)
    hs = THStack()

    infile = TFile(options.inputfile, "READ")
    hBkg = infile.Get("Background_N%d%s" % (N, suffix))
    gBkg = infile.Get("BackgroundGraph_N%d%s" % (N, suffix))
    hData = infile.Get("Data_N%d%s" % (N, suffix))
    hBkg = infile.Get("Background_N%d%s" % (N, suffix))
    hBkg.SetMarkerSize(0)
    hBkg_ = hBkg.Clone("BkgLine")
    hBkg.SetFillColor(33)
    hBkg.SetLineColor(33)
    hBkg_.SetLineWidth(3)
    hBkg_.SetLineColor(862)
    hs.Add(hBkg, "e3")

    legend = TLegend(0.3326613, 0.6419492, 0.9294355, 0.9216102)
    legend.SetTextSize(0.02966102)
    legend.SetTextFont(42)
    legend.SetFillColor(0)
    legend.SetLineColor(0)
    if isExclusive:
        legend.SetHeader("N = %d" % N)
    else:
        legend.SetHeader("N #geq %d" % N)
    legend.AddEntry(hData, "Data", "p")
    legend.AddEntry(hBkg_, "Background", "l")
    legend.AddEntry(hBkg, "Uncertainty", "f")

    legend_sm = TLegend(0.6471774, 0.7669492, 0.8508065, 0.8771186)
    legend_sm.SetTextSize(0.02966102)
    legend_sm.SetTextFont(42)
    legend_sm.SetFillColor(0)
    legend_sm.SetLineColor(0)

    for i, f in enumerate(bh_files):
        h = f.Get("plotsN%d%s/ST" % (N, suffix))
        h.Rebin(config.rebin)
        h.Scale(bh_weights[i])

        # Add background
        for ibin in range(h.GetNbinsX()):
            h.SetBinContent(ibin+1,\
                  h.GetBinContent(ibin+1)\
                  + hBkg.GetBinContent(ibin+1))

            h.SetLineWidth(2)
            h.SetLineStyle(i + 2)

        hs.Add(h, "hist")
        model = ModelKey(config.bh_showcase[i])
        bh_legend = "M_{D} = %.1f TeV, M_{BH}^{ min} = %.1f TeV, n = %d" % (\
              model.parameter["MD"],
              model.parameter["M"],
              model.parameter["n"])

        legend.AddEntry(h, bh_legend, "l")

    if isExclusive:
        for i, f in enumerate(sm_files):
            h = f.Get("plotsN%d%s/ST" % (N, suffix))
            h.Rebin(config.rebin)
            h.Scale(config.integrated_luminosity)
            h.SetFillColor(config.sm_colors[i])
            h.SetLineColor(config.sm_colors[i])
            hs.Add(h, "hist")
            legend_sm.AddEntry(h, config.sm_models[i], "f")

    #hs.Add(hData, "e")

    hs.Draw("nostack")
    c.SetLogy(1)
    hs.GetXaxis().SetTitle("S_{T} (GeV)")
    hs.GetYaxis().SetTitle(hData.GetYaxis().GetTitle())
    hs.GetYaxis().SetTitleOffset(1.2)

    ibin = 0
    if isExclusive:
        hs.GetXaxis().SetRangeUser(config.fit_range[0], config.maxST)
        ibin = hData.FindBin(config.fit_range[0])
    else:
        hs.GetXaxis().SetRangeUser(config.norm_range[0], config.maxST)
        ibin = hData.FindBin(config.norm_range[0])
    from Styles import formatUncertainty
    formatUncertainty(gBkg)
    gBkg.Draw("LX")
    hData.Draw("esame")

    hs.SetMinimum(5e-2)
    if isExclusive:
        hs.SetMaximum(hData.GetBinContent(ibin) * 20)
    else:
        #hs.SetMaximum(4e4)
        hs.SetMaximum(hData.GetBinContent(ibin) * 20)

    legend.Draw("plain")
    if isExclusive:
        legend_sm.Draw("plain")

    if isExclusive:
        cmslabel = TPaveText(0.5544355, 0.5127119, 0.8991935, 0.6292373,
                             "brNDC")
    else:
        cmslabel = TPaveText(0.1955645, 0.1631356, 0.5403226, 0.279661,
                             "brNDC")
    cmslabel.AddText(config.cmsTitle)
    cmslabel.AddText(config.cmsSubtitle)
    cmslabel.SetFillColor(0)
    cmslabel.Draw("plain")

    label = TPaveText(0.6891129, 0.8644068, 0.9435484, 0.9258475, "brNDC")
    label.SetFillColor(0)
    label.SetTextSize(0.0529661)
    label.AddText(label_text)
    label.Draw("plain")

    c.Update()

    raw_input("Press Enter to continue...")
def main():

   from optparse import OptionParser
   parser = OptionParser()
   parser.add_option("-i", "--inputfile", dest="inputfile")
   parser.add_option("-N", "--multiplicity", dest="N", type="int", default=3)
   parser.add_option("-x", "--exclusive", action="store_true",\
         dest="isExclusive", default=False)
   parser.add_option("-l", "--label", dest="label", type="string", default="")
   (options, args) = parser.parse_args()

   N = options.N
   isExclusive = options.isExclusive
   label_text = options.label

   if isExclusive and not (N == 2 or N == 3):
      parser.error("Exclusive plot only for N =2 or 3")

   import configurations as config
   from ROOT import TFile, TCanvas, THStack, TLegend, TPaveText, TH1F, gStyle
   from ModelParser import ModelKey

   gStyle.SetPadTopMargin(0.05)
   gStyle.SetPadRightMargin(0.05)
   gStyle.SetOptStat(0000000)

   suffix = ""
   if not isExclusive:
      suffix = "up"

   sm_files = []
   for model in config.sm_models:
      f = TFile("%s/%s.root" % (config.sm_dir, model), "READ")
      sm_files.append(f)

   bh_weights = []
   bh_files = []
   from BHXsec import BHXsec
   xsec = BHXsec()
   for model in config.bh_showcase:
      f = TFile("%s/%s.root" % (config.bh_dir, model), "READ")
      bh_files.append(f)
      h = f.Get("plotsNoCut/ST")
      nEvents= h.GetEntries()
      bh_weights.append(xsec.get(model) / nEvents * config.integrated_luminosity)

   c = TCanvas("ST_Mul%d%s" % (N, suffix),
         "ST_Mul%d%s" % (N, suffix), 500, 500)
   hs = THStack()
   hs1 = THStack()

   infile = TFile(options.inputfile, "READ")
   hBkg = infile.Get("Background_N%d%s" % (N, suffix))
   gBkg = infile.Get("BackgroundGraph_N%d%s" % (N, suffix))
   hData = infile.Get("Data_N%d%s" % (N, suffix))
   hBkg = infile.Get("Background_N%d%s" % (N, suffix))
   hBkg.SetMarkerSize(0)
   hBkg_ = hBkg.Clone("BkgLine")
   hBkg.SetFillColor(33)
   hBkg.SetLineColor(33)
   hBkg_.SetLineWidth(3)
   hBkg_.SetLineColor(862)
   hs.Add(hBkg, "e3")
   
   hData_N2 = infile.Get("Data_N2")
   hBkg_N2 = infile.Get("Background_N2")

   ratio_data = infile.Get("Data_N%d%s" % (N, suffix))
   ratio_data.Sumw2()
   ratio_data.Divide(hData_N2,hBkg_N2)

   ref_N2 = infile.Get("ReferenceTemplateN2_0")
   ref_N3 = infile.Get("ReferenceTemplateN3_0")
   ratio_fits = infile.Get("ReferenceTemplateN2_0")
   ratio_fits.Sumw2()
   ratio_fits.Divide(ref_N2,ref_N3)
 
   legend = TLegend(0.3026613,0.6919492,0.6094355,0.8816102)
   legend.SetTextSize(0.041); #was 0.02966102
   legend.SetTextFont(42);
   legend.SetFillColor(0)
   legend.SetLineColor(0)
   if isExclusive:
      legend.SetHeader("Multiplicity, N = %d" % N)
   else:
      legend.SetHeader("Multiplicity, N #geq %d" % N)
   legend.AddEntry(ratio_data, "Data/Background", "p")
   legend.AddEntry(ratio_fits, "Fit-0 (N=2)/Fit-0 (N=3)", "l")

   legend_sm = TLegend(0.6271774,0.6769492,0.8308065,0.8171186)
   legend_sm.SetTextSize(0.041);
   legend_sm.SetTextFont(42);
   legend_sm.SetFillColor(0)
   legend_sm.SetLineColor(0)

   for i, f in enumerate(bh_files):
      h = f.Get("plotsN%d%s/ST" % (N, suffix))
      h.Rebin(config.rebin)
      h.Scale(bh_weights[i])

      # Add background
      for ibin in range(h.GetNbinsX()):
         h.SetBinContent(ibin+1,\
               h.GetBinContent(ibin+1)\
               + hBkg.GetBinContent(ibin+1))

         h.SetLineWidth(2)
         h.SetLineStyle(i+2)

      hs.Add(h, "hist")
      model = ModelKey(config.bh_showcase[i])
      bh_legend = "M_{D} = %.1f TeV, M_{BH}^{ min} = %.1f TeV, n = %d" % (\
            model.parameter["MD"],
            model.parameter["M"],
            model.parameter["n"])

      #legend.AddEntry(h, bh_legend, "l")

   if isExclusive:
      for i, f in enumerate(sm_files):
         h = f.Get("plotsN%d%s/ST" % (N, suffix))
         h.Rebin(config.rebin)
         h.Scale(config.integrated_luminosity)
         h.SetFillColor(config.sm_colors[i])
         h.SetLineColor(config.sm_colors[i])
         hs1.Add(h, "hist")
         #legend_sm.AddEntry(h, config.sm_models[i], "f")
   
   #hs.Add(hData, "e")  
   #TH1F *ratio_data 
    
   ratio_fits.SetLineColor(4)
   ratio_fits.SetMarkerStyle(0)
   ratio_fits.SetLineWidth(2)
   
   #hs1.SetMinimum(1e-1)   
   ratio_fits.Draw("linee1")
   ratio_data.Draw("same")   
   #hs.Draw("samenostack")  
   #c.SetLogy(1)
   ratio_fits.GetXaxis().SetTitle("S_{T} (GeV)")
   ratio_fits.GetYaxis().SetTitle("Arbitrary Units")
   ratio_fits.GetYaxis().SetTitleOffset(1.1)

   ratio_fits.GetYaxis().SetTitleSize(0.045)
   ratio_fits.GetYaxis().SetLabelSize(0.045)
   ratio_fits.GetXaxis().SetTitleSize(0.045)
   ratio_fits.GetXaxis().SetLabelSize(0.045)
   ratio_fits.GetXaxis().Draw() 
   ibin = 0
   if isExclusive:
      ratio_fits.GetXaxis().SetRangeUser(config.fit_range[0], config.fit_range[1]+330)
      ibin = hData.FindBin(config.fit_range[0])
   else:
      ratio_fits.GetXaxis().SetRangeUser(config.norm_range[0], config.norm_range[1]+330)
      ibin = hData.FindBin(config.norm_range[0])
   from Styles import formatUncertainty
   formatUncertainty(gBkg)
   #gBkg.Draw("LX")
   #hData.GetXaxis().SetNdivisions(510)
   
   #hData.Draw("esame")
   ratio_fits.SetTitle("")
   ratio_fits.SetMinimum(0)
   ratio_fits.SetMaximum(3)
   
   legend.Draw("plain")
   #if isExclusive:
   #   legend_sm.Draw("plain")

   if isExclusive:
      cmslabel =TPaveText(0.45,0.90,0.60,0.93,"brNDC");
   else:
      cmslabel = TPaveText(0.45,0.90,0.60,0.93,"brNDC")
   cmslabel.AddText(config.cmsTitle)
   #cmslabel.AddText(config.cmsSubtitle)
   cmslabel.SetFillColor(0)
   cmslabel.SetTextSize(0.041)
   cmslabel.Draw("plain")

   label = TPaveText(0.8891129,0.8644068,0.9435484,0.9258475,"brNDC")
   label.SetFillColor(0)
   label.SetTextSize(0.0529661);
   label.AddText(label_text);
   label.Draw("plain")
   
   if isExclusive:
     c.Print("ST_Residuals_Mul%d.pdf" % N)
     c.Print("ST_Residuals_Mul%d.png" % N)
   else:
     c.Print("ST_Mul%dup.pdf" % N)
     c.Print("ST_Mul%dup.png" % N)    
   c.Update()

   raw_input("Press Enter to continue...")
Beispiel #3
0
def main():

   from optparse import OptionParser
   parser = OptionParser()
   parser.add_option("-i", "--inputfile", dest="inputfile")
   parser.add_option("-N", "--multiplicity", dest="N", type="int", default=3)
   parser.add_option("-x", "--exclusive", action="store_true",\
         dest="isExclusive", default=False)
   parser.add_option("-l", "--label", dest="label", type="string", default="")
   (options, args) = parser.parse_args()

   N = options.N
   isExclusive = options.isExclusive
   label_text = options.label

   if isExclusive and not (N == 2 or N == 3):
      parser.error("Exclusive plot only for N =2 or 3")

   import configurations as config
   from ROOT import TFile, TCanvas, THStack, TLegend, TPaveText, gStyle
   from ModelParser import ModelKey

   gStyle.SetPadTopMargin(0.05)
   gStyle.SetPadRightMargin(0.05)
   gStyle.SetErrorX(0.)

   suffix = ""
   if not isExclusive:
      suffix = "up"

   sm_files = []
   for model in config.sm_models:
      f = TFile("%s/%s.root" % (config.sm_dir, model), "READ")
      sm_files.append(f)

   bh_weights = []
   bh_files = []
   from BHXsec import BHXsec
   xsec = BHXsec()
   for model in config.bh_showcase:
      f = TFile("%s/%s.root" % (config.bh_dir, model), "READ")
      bh_files.append(f)
      h = f.Get("plotsNoCut/ST")
      nEvents= h.GetEntries()
      bh_weights.append(xsec.get(model) / nEvents * config.integrated_luminosity)

   c = TCanvas("ST_Mul%d%s" % (N, suffix),
         "ST_Mul%d%s" % (N, suffix), 500, 500)
   hs = THStack()
   hs1 = THStack()

   infile = TFile(options.inputfile, "READ")
   hBkg = infile.Get("Background_N%d%s" % (N, suffix))
   gBkg = infile.Get("BackgroundGraph_N%d%s" % (N, suffix))
   hData = infile.Get("Data_N%d%s" % (N, suffix))
   hBkg = infile.Get("Background_N%d%s" % (N, suffix))
   hBkg.SetMarkerSize(0)
   hBkg_ = hBkg.Clone("BkgLine")
   hBkg.SetFillColor(33)
   hBkg.SetLineColor(33)
   hBkg_.SetLineWidth(3)
   hBkg_.SetLineColor(862)
   hs.Add(hBkg, "e3")

   #legend = TLegend(0.2826613,0.4819492,0.6094355,0.9416102) # - only for N >= 2 and 3
   legend = TLegend(0.3026613,0.5519492,0.6094355,0.9416102) # was 0.4919...
   legend.SetTextSize(0.041); #was 0.02966102
   legend.SetTextFont(42);
   legend.SetFillColor(0)
   legend.SetLineColor(0)
   if isExclusive:
      legend.SetHeader("Multiplicity N = %d" % N)
   else:
      legend.SetHeader("Multiplicity N #geq %d" % N)
   legend.AddEntry(hData, "Data", "lep")
   legend.AddEntry(hBkg_, "Background", "l")
   legend.AddEntry(hBkg, "Uncertainty", "f")

   legend_sm = TLegend(0.6271774,0.7369492,0.8308065,0.8771186)
   legend_sm.SetTextSize(0.041);
   legend_sm.SetTextFont(42);
   legend_sm.SetFillColor(0)
   legend_sm.SetLineColor(0)

   for i, f in enumerate(bh_files):
      h = f.Get("plotsN%d%s/ST" % (N, suffix))
      h.Rebin(config.rebin)
      h.Scale(bh_weights[i])

      # Add background
      for ibin in range(h.GetNbinsX()):
         h.SetBinContent(ibin+1,\
               h.GetBinContent(ibin+1)\
               + hBkg.GetBinContent(ibin+1))

         h.SetLineWidth(3)
         #h.SetLineColor(i+2)
         h.SetLineStyle(i+2)

         if i == 0:
            h.SetLineColor(814)
         if i == 1:
            h.SetLineStyle(5)
            h.SetLineColor(899)
         if i == 2:
            h.SetLineStyle(9)
            h.SetLineColor(4)
         if i == 3:
            h.SetLineStyle(3)
            h.SetLineColor(614)

      hs.Add(h, "hist")
      model = ModelKey(config.bh_showcase[i])
      bh_legend = "M_{D} = %.1f TeV, M_{BH}^{ min} = %.1f TeV, n = %d" % (\
            model.parameter["MD"],
            model.parameter["M"],
            model.parameter["n"])
      if i == 3:
         bh_legend = "M_{D} = 3.0 TeV, M_{QBH}^{ min} = 4.0 TeV, n = 4"    

      legend.AddEntry(h, bh_legend, "l")

#      qbh_legend = "M_{D} = 4.0 TeV, M_{QBH}^{ min} = 5.0 TeV, n = 5"

#      legend.AddEntry(h, qbh_legend, "l")

   if isExclusive:
      for i, f in enumerate(sm_files):
         h = f.Get("plotsN%d%s/ST" % (N, suffix))
         h.Rebin(config.rebin)
         h.Scale(config.integrated_luminosity)
         h.SetFillColor(config.sm_colors[i])
         h.SetLineColor(config.sm_colors[i])
         hs1.Add(h, "hist")
         legend_sm.AddEntry(h, config.sm_models[i], "f")
   
   #hs.Add(hData, "e")   
   
   hs.Draw("nostack")
   hs1.Draw("same")  
   c.SetLogy(1)
   hs.GetXaxis().SetTitle("S_{T} (GeV)")
   hs.GetYaxis().SetTitle(hData.GetYaxis().GetTitle())
   hs.GetYaxis().SetTitleOffset(1.25)

   hs.GetYaxis().SetTitleSize(0.04)
   hs.GetYaxis().SetLabelSize(0.04)
   hs.GetXaxis().SetTitleSize(0.04)
   hs.GetXaxis().SetLabelSize(0.04)
    
   ibin = 0
   #if isExclusive:
   #   hs.GetXaxis().SetRangeUser(config.fit_range[0], config.maxST)
   #   ibin = hData.FindBin(config.fit_range[0])
   #else:
   #   hs.GetXaxis().SetRangeUser(config.norm_range[0], config.maxST)
   #   ibin = hData.FindBin(config.norm_range[0])
   
   if isExclusive:
      hs.GetXaxis().SetRangeUser(1800, config.maxST)
      ibin = hData.FindBin(1800)
   else:
      hs.GetXaxis().SetRangeUser(config.norm_range[0], config.maxST)
      ibin = hData.FindBin(config.norm_range[0])

   from Styles import formatUncertainty
   formatUncertainty(gBkg)
   gBkg.Draw("LX")
   hData.Draw("sameex0")

   hs.SetMinimum(5e-1)
   if isExclusive:
      hs.SetMaximum(hData.GetBinContent(ibin) * 40)
   else:
      #hs.SetMaximum(4e4)
      hs.SetMaximum(hData.GetBinContent(ibin) * 20) # or 1e7 for N>=3 and use 4 models

   legend.Draw("plain")
   if isExclusive:
      legend_sm.Draw("plain")

   if isExclusive:
      cmslabel =TPaveText(0.45,0.96,0.60,0.99,"brNDC");
   else:
      cmslabel = TPaveText(0.45,0.96,0.60,0.99,"brNDC")
   cmslabel.AddText(config.cmsTitle)
   #cmslabel.AddText(config.cmsSubtitle)
   cmslabel.SetFillColor(0)
   cmslabel.SetTextSize(0.041)
   cmslabel.Draw("plain")

   label = TPaveText(0.8891129,0.8644068,0.9435484,0.9258475,"brNDC")
   label.SetFillColor(0)
   label.SetTextSize(0.0529661);
   label.AddText(label_text);
   label.Draw("plain")

   #block1 =TPaveText(0.333,0.84,0.354,0.86,"brNDC"); # for N>=2 and >=3 only
   block1 =TPaveText(0.351,0.85,0.37,0.87,"brNDC");
   block1.SetFillColor(0)
   block1.Draw("plain")

   #block2 =TPaveText(0.295,0.84,0.315,0.86,"brNDC"); # for N>=2 and >=3 only
   block2 =TPaveText(0.314,0.85,0.332,0.87,"brNDC");
   block2.SetFillColor(0)
   block2.Draw("plain")
   
   if isExclusive:
     c.Print("ST_Mul%d.pdf" % N)
     c.Print("ST_Mul%d.png" % N)
   else:
     c.Print("ST_Mul%dup.pdf" % N)
     c.Print("ST_Mul%dup.png" % N)    
   c.Update()

   raw_input("Press Enter to continue...")
def main():
   from optparse import OptionParser
   parser = OptionParser()
   parser.add_option("-i", "--inputfile", dest="inputfile")
   parser.add_option("-o", "--ouputfile", dest="outputfile")
   parser.add_option("-b", "--batch", action="store_true",\
         dest="isBatch", default=False)
   parser.add_option("--normalization", nargs=2,\
         type="float", dest="norm_range")
   parser.add_option("--fit", nargs=2,\
         type="float", dest="fit_range")
   (options, args) = parser.parse_args()

   isSaveOutput = options.outputfile is not None

   if not (options.inputfile):
      parser.error("Please specify inputfiles.")

   import configurations as config
   
   integrated_luminosity = config.integrated_luminosity
   
   if options.fit_range:
      fit_range = options.fit_range
      norm_range = (fit_range[1] - 200., fit_range[1])      
   else:
      fit_range = config.fit_range
      norm_range = config.norm_range

   # Override normalization range from input
   if options.norm_range:
      norm_range = options.norm_range


   from Styles import formatST, formatTemplate, formatUncertainty
   from ROOT import TFile, TF1, TH1D, TMath, TCanvas, TLegend,\
         TGraphAsymmErrors, TVectorD, gStyle 

   gStyle.SetPadTopMargin(0.05)
   gStyle.SetPadRightMargin(0.05)
   #gStyle.SetOptFit(2222222)
   #gStyle.SetStatX(0.95)
   #gStyle.SetStatY(0.95)
   gStyle.SetOptStat(0000000)
   gStyle.SetOptFit(0000000)
   from ROOT import TFile, TCanvas, TMultiGraph, TLegend, TPaveText

   #input file name
   infile = TFile(options.inputfile, "READ")

   from HistoStore import HistoStore
   store = HistoStore()
   canvas = HistoStore()

   print "Fit range: %d - %d GeV" % fit_range
   print "Normalization range: %d - %d GeV" % norm_range
   print "Integrated luminosity: %d inv. pb" % integrated_luminosity

   # Fit
   for N in config.exclusive_multiplicities:
      hST = infile.Get("plots%dJets/ST" %  N)
      hST.GetXaxis().SetNdivisions(510)
      if not options.isBatch:
         c = TCanvas("TemplateN%d" % N, 
               "TemplateN%d" % N, 500, 500)
         canvas.book(c)
         formatST(hST)
	 hST.SetTitle("")
         hST.Draw("e")
         hST.GetXaxis().SetRangeUser(fit_range[0], config.maxST)
         hST.GetYaxis().SetRangeUser(1e-2, 2e4)
	 hST.GetYaxis().SetTitleOffset(1.25)
	 hST.GetYaxis().SetTitleSize(0.04)
         hST.GetYaxis().SetLabelSize(0.04)
         hST.GetXaxis().SetTitleSize(0.04)
         hST.GetXaxis().SetLabelSize(0.04)
         c.SetLogy(1)

      for i,formula in enumerate(config.templates):
         print "formula %d" % (i)
         if N == 2:
            f = TF1("templateN%d_%d" % (N, i), formula, 0, 10000)
         elif N == 3:
            f = store.get("templateN2_%d" % i).Clone("templateN%d_%d" % (N, i))
	 if i < 3:   
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "QN0", "", 1800, 2800)
            hST.Fit(f, "N0", "", 1800, 2800)
         elif i == 0:
            hST.Fit(f, "Q0", "", fit_range[0], fit_range[1])
         elif i > 2:
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])	  

         formatTemplate(f, N, i)
         store.book(f)

         if not options.isBatch:
            f.Draw("same")

         hTemplate = hST.Clone("histoTemplateN%d_%d" % (N,i))
         hTemplate.Reset()
         hTemplate.Eval(f)
         formatTemplate(hTemplate, N, i)
         store.book(hTemplate)

         if i == 0:
            hRef = hTemplate.Clone("ReferenceTemplateN%d_0" % N)
            store.book(hRef)

         # Print Chi-squre/Ndof
         print "N = %d, Chi^2/Ndof = %0.2f/%d" %\
               (N, f.GetChisquare(), f.GetNDF())
      if not options.isBatch:
         c.Update()
	 c.Print("TemplateN%d.pdf" % N)
	 c.Print("TemplateN%d.png" % N)

   # Calculate scale/error
   from OptimizationTools import OptimizeScale
   for histoN, templateN in [[2,3]]:
      hST = store.get("ReferenceTemplateN%d_0" % histoN)
      hTemplate  = store.get("ReferenceTemplateN%d_0" % templateN)

      hlnL, scale, err = OptimizeScale(hST, hTemplate, norm_range)
      hlnL.SetName("LogLikelihood_%dto%d" % (templateN, histoN))
      store.book(hlnL)

      for i in range(len(config.templates)):
         hTemplate  = store.get("histoTemplateN%d_%d" % (templateN, i))
         hTemplate_ = hTemplate.Clone("histoTemplateN%d_%d__RescaledToN%d"
               % (templateN, i, histoN))
         hTemplate_.Scale(scale)
         store.book(hTemplate_)

   # Shape Uncertainty
   hBkgTemplate  = store.get("histoTemplateN2_0")
   hBkgTemplate.Rebin(config.rebin)
   nbins = hBkgTemplate.GetNbinsX()
   vST = TVectorD(nbins)
   vBkg = TVectorD(nbins)
   vexl = TVectorD(nbins)
   vexh = TVectorD(nbins)
   shape_el = TVectorD(nbins)
   shape_eh = TVectorD(nbins)
   rel_shape_el = TVectorD(nbins)
   rel_shape_eh = TVectorD(nbins)
   for i in range(nbins):
      vST[i] = hBkgTemplate.GetBinCenter(i+1)
      if (vST[i] < config.com):
         vBkg[i] = hBkgTemplate.GetBinContent(i+1)
      else:
         vBkg[i] = 0.0
      vexl[i] = 0.0
      vexh[i] = 0.0
      shape_el[i] = 0.0
      shape_eh[i] = 0.0
      rel_shape_el[i] = 0.0
      rel_shape_eh[i] = 0.0

   for i in range(len(config.templates)):
      for label in ["histoTemplateN2_%d", "histoTemplateN3_%d__RescaledToN2"]:
         if label % i == "histoTemplateN2_0":
            continue
         h = store.get(label % i)
         h.Rebin(config.rebin)
         for ibin in range(nbins):
            diff = h.GetBinContent(ibin+1) - vBkg[ibin]
            if diff > 0 and diff > shape_eh[ibin]:
               shape_eh[ibin] = diff
            elif diff < 0 and abs(diff) > shape_el[ibin]:
               shape_el[ibin] = abs(diff)

   # Relative Shape Uncertaincy
   for i in range(nbins):
      if vBkg[i] > 0:
         #rel_shape_el[i] = rel_shape_el[i] / vBkg[i]
         #hape_eh[i] = rel_shape_eh[i] / vBkg[i]
         max_err = max(shape_el[i], shape_eh[i])
         shape_el[i] = max_err
         shape_eh[i] = max_err
         rel_shape_el[i] = max_err /vBkg[i]
         rel_shape_eh[i] = max_err /vBkg[i]
      else:
         rel_shape_el[i] = 0.0
         rel_shape_eh[i] = 0.0
      #print vST[i], vBkg[i], rel_shape_el[i], rel_shape_eh[i]
   gShapeUncertainty = TGraphAsymmErrors(vST, vBkg,
         vexl, vexh, shape_el, shape_eh)
   gShapeUncertainty.SetName("Shape_Uncertainty")
   formatUncertainty(gShapeUncertainty)
   store.book(gShapeUncertainty)

   gRelShapeUncertainty = TGraphAsymmErrors(vST, vexl,
         vexl, vexh, rel_shape_el, rel_shape_eh)
   gRelShapeUncertainty.SetName("Relative_Shape_Uncertainty")
   formatUncertainty(gRelShapeUncertainty)
   store.book(gRelShapeUncertainty)

   # Generate Backgrouds
   for N in config.label_for_data:
      hST = infile.Get("plotsN%s/ST" % N)
      rel_scale_err2 = 0.0
      scale_factor = 1.0
      for Nref in config.label_for_ref:
         if N == Nref:
            continue

         template = store.get("ReferenceTemplateN%s_0" % Nref)

         hlnL, scale, err = OptimizeScale(hST, template, norm_range)
         hlnL.SetName("LogLikelihood_%sto%s" % (Nref, N))
         store.book(hlnL)

         if Nref == "2":
            scale_factor = scale
         rel_scale_err2 += err/scale * err/scale

         print "%s/%s %.3f $\pm$ %.3f" % (N, Nref, scale, err)

      vy = TVectorD(nbins)
      veyh = TVectorD(nbins)
      veyl = TVectorD(nbins)
      for i in range(nbins):
         vy[i] = vBkg[i] * scale_factor
         veyh[i] = vy[i] * TMath.Sqrt(rel_scale_err2 
               + rel_shape_eh[i]*rel_shape_eh[i])
         veyl[i] = vy[i] * TMath.Sqrt(rel_scale_err2 
               + rel_shape_el[i]*rel_shape_el[i])

      print "Scaling uncertainty (%s): %.2f" %\
            (N, TMath.sqrt(rel_scale_err2) * 100.0)

      gBkg = TGraphAsymmErrors(vST, vy, vexl, vexh, veyl, veyh)
      gBkg.SetName("BackgroundGraph_N%s" % N)
      formatUncertainty(gBkg)
      store.book(gBkg)

      hST.Rebin(config.rebin)
      hST.SetName("Data_N%s" % N)
      formatST(hST)
      store.book(hST)

      hBkg = hST.Clone("Background_N%s" % N)
      hBkg.Reset()
      store.book(hBkg)

      for i in range(nbins):
         ibin = hBkg.FindBin(vST[i])
         hBkg.SetBinContent(ibin, vy[i])
         hBkg.SetBinError(ibin, max(veyh[i], vexl[i]))

      from OptimizationTools import Integral
      hIntBkg = hBkg.Clone("IntegralBackground_N%s" % N)
      Integral(hIntBkg)
      store.book(hIntBkg)

      hIntData = hST.Clone("IntegralData_N%s" % N)
      Integral(hIntData)
      store.book(hIntData)

      
   # Plot Shape Uncertainty
   if not options.isBatch:
      legend_shape = TLegend(0.4244355,0.4241525,0.9395968,0.8652542)
      legend_shape.SetTextFont(42)
      legend_shape.SetFillColor(0)
      legend_shape.SetLineColor(0)
      legend_shape.SetTextSize(0.036)
      c = TCanvas("ShapeUncertaintyN2", "ShapeUncertaintyN2", 500, 500)
      canvas.book(c)
      gShapeUncertainty.Draw("AC3")
      gShapeUncertainty.GetXaxis().SetNdivisions(510)
      gShapeUncertainty.GetXaxis().SetRangeUser(fit_range[0], config.maxST)
      gShapeUncertainty.GetYaxis().SetRangeUser(5e-2, 5e6)

      legend_shape.AddEntry(store.get("Data_N2"), "Data (N = 2)", "p")
      legend_shape.AddEntry(gShapeUncertainty, "Shape Uncertainty", "f")
      for i in range(len(config.templates)):
         for label in ["histoTemplateN2_%d", "histoTemplateN3_%d__RescaledToN2"]:
            h = store.get(label % i)
            h.GetXaxis().SetRangeUser(fit_range[0], config.maxST)
            h.Draw("histcsame")
	    h.GetXaxis().SetNdivisions(510)
            if label == "histoTemplateN2_%d":
               N = 2
            else:
               N = 3
            legend_shape.AddEntry(h, "Parameterization %d (N = %d)" % (i, N), "l")
      store.get("Data_N2").Draw("esame")
      cmslabel = TPaveText(0.45,0.90,0.60,0.93,"brNDC")
      cmslabel.AddText(config.cmsTitle)
      #cmslabel.AddText(config.cmsSubtitle)
      cmslabel.SetFillColor(0)
      cmslabel.SetTextSize(0.041)
      cmslabel.Draw("plain")
      c.SetLogy(1)
      legend_shape.Draw("plain")
      c.Update()
      c.Print("ShapeUncertaintyN2.pdf")
      c.Print("ShapeUncertaintyN2.png")

   if isSaveOutput:
      store.saveAs(options.outputfile)

   if not options.isBatch:
      raw_input("Press Enter to continue...")
Beispiel #5
0
def main():

   from optparse import OptionParser
   parser = OptionParser()
   parser.add_option("-i", "--inputfile", dest="inputfile")
   parser.add_option("-N", "--multiplicity", dest="N", type="int", default=3)
   parser.add_option("-x", "--exclusive", action="store_true",\
         dest="isExclusive", default=False)
   parser.add_option("-l", "--label", dest="label", type="string", default="")
   (options, args) = parser.parse_args()

   N = options.N
   isExclusive = options.isExclusive
   label_text = options.label

   if isExclusive and not (N == 2 or N == 3):
      parser.error("Exclusive plot only for N =2 or 3")

   import configurations as config
   from ROOT import TFile, TCanvas, THStack, TLegend, TPaveText, gStyle
   from ModelParser import ModelKey

   gStyle.SetPadTopMargin(0.05)
   gStyle.SetPadRightMargin(0.05)

   suffix = ""
   if not isExclusive:
      suffix = "up"

   sm_files = []
   for model in config.sm_models:
      f = TFile("%s/%s.root" % (config.sm_dir, model), "READ")
      sm_files.append(f)

   bh_weights = []
   bh_files = []
   from BHXsec import BHXsec
   xsec = BHXsec()
   for model in config.bh_showcase:
      f = TFile("%s/%s.root" % (config.bh_dir, model), "READ")
      bh_files.append(f)
      h = f.Get("plotsNoCut/ST")
      nEvents= h.GetEntries()
      bh_weights.append(xsec.get(model) / nEvents * config.integrated_luminosity)

   c = TCanvas("ST_Mul%d%s" % (N, suffix),
         "ST_Mul%d%s" % (N, suffix), 500, 500)
   hs = THStack()

   infile = TFile(options.inputfile, "READ")
   hBkg = infile.Get("Background_N%d%s" % (N, suffix))
   gBkg = infile.Get("BackgroundGraph_N%d%s" % (N, suffix))
   hData = infile.Get("Data_N%d%s" % (N, suffix))
   hBkg = infile.Get("Background_N%d%s" % (N, suffix))
   hBkg.SetMarkerSize(0)
   hBkg_ = hBkg.Clone("BkgLine")
   hBkg.SetFillColor(33)
   hBkg.SetLineColor(33)
   hBkg_.SetLineWidth(3)
   hBkg_.SetLineColor(862)
   hs.Add(hBkg, "e3")

   legend = TLegend(0.3326613,0.6419492,0.9294355,0.9216102)
   legend.SetTextSize(0.02966102);
   legend.SetTextFont(42);
   legend.SetFillColor(0)
   legend.SetLineColor(0)
   if isExclusive:
      legend.SetHeader("N = %d" % N)
   else:
      legend.SetHeader("N #geq %d" % N)
   legend.AddEntry(hData, "Data", "p")
   legend.AddEntry(hBkg_, "Background", "l")
   legend.AddEntry(hBkg, "Uncertainty", "f")

   legend_sm = TLegend(0.6471774,0.7669492,0.8508065,0.8771186)
   legend_sm.SetTextSize(0.02966102);
   legend_sm.SetTextFont(42);
   legend_sm.SetFillColor(0)
   legend_sm.SetLineColor(0)

   for i, f in enumerate(bh_files):
      h = f.Get("plotsN%d%s/ST" % (N, suffix))
      h.Rebin(config.rebin)
      h.Scale(bh_weights[i])

      # Add background
      for ibin in range(h.GetNbinsX()):
         h.SetBinContent(ibin+1,\
               h.GetBinContent(ibin+1)\
               + hBkg.GetBinContent(ibin+1))

         h.SetLineWidth(2)
         h.SetLineStyle(i+2)

      hs.Add(h, "hist")
      model = ModelKey(config.bh_showcase[i])
      bh_legend = "M_{D} = %.1f TeV, M_{BH}^{ min} = %.1f TeV, n = %d" % (\
            model.parameter["MD"],
            model.parameter["M"],
            model.parameter["n"])

      legend.AddEntry(h, bh_legend, "l")

   if isExclusive:
      for i, f in enumerate(sm_files):
         h = f.Get("plotsN%d%s/ST" % (N, suffix))
         h.Rebin(config.rebin)
         h.Scale(config.integrated_luminosity)
         h.SetFillColor(config.sm_colors[i])
         h.SetLineColor(config.sm_colors[i])
         hs.Add(h, "hist")
         legend_sm.AddEntry(h, config.sm_models[i], "f")
   
   #hs.Add(hData, "e")   

   hs.Draw("nostack")   
   c.SetLogy(1)
   hs.GetXaxis().SetTitle("S_{T} (GeV)")
   hs.GetYaxis().SetTitle(hData.GetYaxis().GetTitle())
   hs.GetYaxis().SetTitleOffset(1.2)

   
   ibin = 0
   if isExclusive:
      hs.GetXaxis().SetRangeUser(config.fit_range[0], config.maxST)
      ibin = hData.FindBin(config.fit_range[0])
   else:
      hs.GetXaxis().SetRangeUser(config.norm_range[0], config.maxST)
      ibin = hData.FindBin(config.norm_range[0])
   from Styles import formatUncertainty
   formatUncertainty(gBkg)
   gBkg.Draw("LX")
   hData.Draw("esame")

   hs.SetMinimum(5e-2)
   if isExclusive:
      hs.SetMaximum(hData.GetBinContent(ibin) * 20)
   else:
      #hs.SetMaximum(4e4)
      hs.SetMaximum(hData.GetBinContent(ibin) * 20)

   legend.Draw("plain")
   if isExclusive:
      legend_sm.Draw("plain")

   if isExclusive:
      cmslabel =TPaveText(0.5544355,0.5127119,0.8991935,0.6292373,"brNDC");
   else:
      cmslabel = TPaveText(0.1955645,0.1631356,0.5403226,0.279661,"brNDC")
   cmslabel.AddText(config.cmsTitle)
   cmslabel.AddText(config.cmsSubtitle)
   cmslabel.SetFillColor(0)
   cmslabel.Draw("plain")

   label = TPaveText(0.6891129,0.8644068,0.9435484,0.9258475,"brNDC")
   label.SetFillColor(0)
   label.SetTextSize(0.0529661);
   label.AddText(label_text);
   label.Draw("plain")

   c.Update()

   raw_input("Press Enter to continue...")
Beispiel #6
0
def main():
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("-i", "--inputfile", dest="inputfile")
    parser.add_option("-o", "--ouputfile", dest="outputfile")
    parser.add_option("-b", "--batch", action="store_true",\
          dest="isBatch", default=False)
    parser.add_option("--normalization", nargs=2,\
          type="float", dest="norm_range")
    parser.add_option("--fit", nargs=2,\
          type="float", dest="fit_range")
    (options, args) = parser.parse_args()

    isSaveOutput = options.outputfile is not None

    if not (options.inputfile):
        parser.error("Please specify inputfiles.")

    import configurations as config
    if options.fit_range:
        fit_range = options.fit_range
        norm_range = (fit_range[1] - 200., fit_range[1])
    else:
        fit_range = config.fit_range
        norm_range = config.norm_range

    # Override normalization range from input
    if options.norm_range:
        norm_range = options.norm_range

    from Styles import formatST, formatTemplate, formatUncertainty
    from ROOT import TFile, TF1, TH1D, TMath, TCanvas, TLegend,\
          TGraphAsymmErrors, TVectorD, gStyle

    gStyle.SetPadTopMargin(0.05)
    gStyle.SetPadRightMargin(0.05)
    #gStyle.SetOptFit(1111111)

    #input file name
    infile = TFile(options.inputfile, "READ")

    from HistoStore import HistoStore
    store = HistoStore()
    canvas = HistoStore()

    print "Fit range: %d - %d GeV" % fit_range
    print "Normalization range: %d - %d GeV" % norm_range

    # Fit
    for N in config.exclusive_multiplicities:
        hST = infile.Get("plots%dJets/ST" % N)
        if not options.isBatch:
            c = TCanvas("TemplateN%d" % N, "TemplateN%d" % N, 500, 500)
            canvas.book(c)
            formatST(hST)
            hST.Draw("e")
            hST.GetXaxis().SetRangeUser(fit_range[0], config.maxST)
            hST.GetYaxis().SetRangeUser(1e-2, 2e4)
            hST.GetYaxis().SetTitleOffset(1.2)
            c.SetLogy(1)

        for i, formula in enumerate(config.templates):
            if N == 2:
                f = TF1("templateN%d_%d" % (N, i), formula, 0, 10000)
            elif N == 3:
                f = store.get("templateN2_%d" % i).Clone("templateN%d_%d" %
                                                         (N, i))
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            hST.Fit(f, "QN0", "", fit_range[0], fit_range[1])
            if i == 0:
                hST.Fit(f, "Q0", "", fit_range[0], fit_range[1])

            formatTemplate(f, N, i)
            store.book(f)

            if not options.isBatch:
                f.Draw("same")

            hTemplate = hST.Clone("histoTemplateN%d_%d" % (N, i))
            hTemplate.Reset()
            hTemplate.Eval(f)
            formatTemplate(hTemplate, N, i)
            store.book(hTemplate)

            if i == 0:
                hRef = hTemplate.Clone("ReferenceTemplateN%d_0" % N)
                store.book(hRef)

            # Print Chi-squre/Ndof
            print "N = %d, Chi^2/Ndof = %0.2f/%d" %\
                  (N, f.GetChisquare(), f.GetNDF())
        if not options.isBatch:
            c.Update()

    # Calculate scale/error
    from OptimizationTools import OptimizeScale
    for histoN, templateN in [[2, 3]]:
        hST = store.get("ReferenceTemplateN%d_0" % histoN)
        hTemplate = store.get("ReferenceTemplateN%d_0" % templateN)

        hlnL, scale, err = OptimizeScale(hST, hTemplate, norm_range)
        hlnL.SetName("LogLikelihood_%dto%d" % (templateN, histoN))
        store.book(hlnL)

        for i in range(len(config.templates)):
            hTemplate = store.get("histoTemplateN%d_%d" % (templateN, i))
            hTemplate_ = hTemplate.Clone("histoTemplateN%d_%d__RescaledToN%d" %
                                         (templateN, i, histoN))
            hTemplate_.Scale(scale)
            store.book(hTemplate_)

    # Shape Uncertainty
    hBkgTemplate = store.get("histoTemplateN2_0")
    hBkgTemplate.Rebin(config.rebin)
    nbins = hBkgTemplate.GetNbinsX()
    vST = TVectorD(nbins)
    vBkg = TVectorD(nbins)
    vexl = TVectorD(nbins)
    vexh = TVectorD(nbins)
    shape_el = TVectorD(nbins)
    shape_eh = TVectorD(nbins)
    rel_shape_el = TVectorD(nbins)
    rel_shape_eh = TVectorD(nbins)
    for i in range(nbins):
        vST[i] = hBkgTemplate.GetBinCenter(i + 1)
        if (vST[i] < config.com):
            vBkg[i] = hBkgTemplate.GetBinContent(i + 1)
        else:
            vBkg[i] = 0.0
        vexl[i] = 0.0
        vexh[i] = 0.0
        shape_el[i] = 0.0
        shape_eh[i] = 0.0
        rel_shape_el[i] = 0.0
        rel_shape_eh[i] = 0.0

    for i in range(len(config.templates)):
        for label in [
                "histoTemplateN2_%d", "histoTemplateN3_%d__RescaledToN2"
        ]:
            if label % i == "histoTemplateN2_0":
                continue
            h = store.get(label % i)
            h.Rebin(config.rebin)
            for ibin in range(nbins):
                diff = h.GetBinContent(ibin + 1) - vBkg[ibin]
                if diff > 0 and diff > shape_eh[ibin]:
                    shape_eh[ibin] = diff
                elif diff < 0 and abs(diff) > shape_el[ibin]:
                    shape_el[ibin] = abs(diff)

    # Relative Shape Uncertaincy
    for i in range(nbins):
        if vBkg[i] > 0:
            #rel_shape_el[i] = rel_shape_el[i] / vBkg[i]
            #hape_eh[i] = rel_shape_eh[i] / vBkg[i]
            max_err = max(shape_el[i], shape_eh[i])
            shape_el[i] = max_err
            shape_eh[i] = max_err
            rel_shape_el[i] = max_err / vBkg[i]
            rel_shape_eh[i] = max_err / vBkg[i]
        else:
            rel_shape_el[i] = 0.0
            rel_shape_eh[i] = 0.0
        #print vST[i], vBkg[i], rel_shape_el[i], rel_shape_eh[i]
    gShapeUncertainty = TGraphAsymmErrors(vST, vBkg, vexl, vexh, shape_el,
                                          shape_eh)
    gShapeUncertainty.SetName("Shape_Uncertainty")
    formatUncertainty(gShapeUncertainty)
    store.book(gShapeUncertainty)

    gRelShapeUncertainty = TGraphAsymmErrors(vST, vexl, vexl, vexh,
                                             rel_shape_el, rel_shape_eh)
    gRelShapeUncertainty.SetName("Relative_Shape_Uncertainty")
    formatUncertainty(gRelShapeUncertainty)
    store.book(gRelShapeUncertainty)

    # Generate Backgrouds
    for N in config.label_for_data:
        hST = infile.Get("plotsN%s/ST" % N)
        rel_scale_err2 = 0.0
        scale_factor = 1.0
        for Nref in config.label_for_ref:
            if N == Nref:
                continue

            template = store.get("ReferenceTemplateN%s_0" % Nref)

            hlnL, scale, err = OptimizeScale(hST, template, norm_range)
            hlnL.SetName("LogLikelihood_%sto%s" % (Nref, N))
            store.book(hlnL)

            if Nref == "2":
                scale_factor = scale
            rel_scale_err2 += err / scale * err / scale

            print "%s/%s %.3f +/- %.3f" % (N, Nref, scale, err)

        vy = TVectorD(nbins)
        veyh = TVectorD(nbins)
        veyl = TVectorD(nbins)
        for i in range(nbins):
            vy[i] = vBkg[i] * scale_factor
            veyh[i] = vy[i] * TMath.Sqrt(rel_scale_err2 +
                                         rel_shape_eh[i] * rel_shape_eh[i])
            veyl[i] = vy[i] * TMath.Sqrt(rel_scale_err2 +
                                         rel_shape_el[i] * rel_shape_el[i])

        print "Scaling uncertainty (%s): %.2f" %\
              (N, TMath.sqrt(rel_scale_err2) * 100.0)

        gBkg = TGraphAsymmErrors(vST, vy, vexl, vexh, veyl, veyh)
        gBkg.SetName("BackgroundGraph_N%s" % N)
        formatUncertainty(gBkg)
        store.book(gBkg)

        hST.Rebin(config.rebin)
        hST.SetName("Data_N%s" % N)
        formatST(hST)
        store.book(hST)

        hBkg = hST.Clone("Background_N%s" % N)
        hBkg.Reset()
        store.book(hBkg)

        for i in range(nbins):
            ibin = hBkg.FindBin(vST[i])
            hBkg.SetBinContent(ibin, vy[i])
            hBkg.SetBinError(ibin, max(veyh[i], vexl[i]))

        from OptimizationTools import Integral
        hIntBkg = hBkg.Clone("IntegralBackground_N%s" % N)
        Integral(hIntBkg)
        store.book(hIntBkg)

        hIntData = hST.Clone("IntegralData_N%s" % N)
        Integral(hIntData)
        store.book(hIntData)

    # Plot Shape Uncertainty
    if not options.isBatch:
        legend_shape = TLegend(0.5544355, 0.5741525, 0.9395968, 0.9152542)
        legend_shape.SetTextFont(42)
        legend_shape.SetFillColor(0)
        legend_shape.SetLineColor(0)
        c = TCanvas("ShapeUncertaintyN2", "ShapeUncertaintyN2", 500, 500)
        canvas.book(c)
        gShapeUncertainty.Draw("AC3")
        gShapeUncertainty.GetXaxis().SetRangeUser(fit_range[0], config.maxST)
        gShapeUncertainty.GetYaxis().SetRangeUser(5e-2, 1.2e6)
        legend_shape.AddEntry(store.get("Data_N2"), "Data (N = 2)", "p")
        legend_shape.AddEntry(gShapeUncertainty, "Shape Uncertainty", "f")
        for i in range(len(config.templates)):
            for label in [
                    "histoTemplateN2_%d", "histoTemplateN3_%d__RescaledToN2"
            ]:
                h = store.get(label % i)
                h.GetXaxis().SetRangeUser(fit_range[0], config.maxST)
                h.Draw("histcsame")
                if label == "histoTemplateN2_%d":
                    N = 2
                else:
                    N = 3
                legend_shape.AddEntry(h,
                                      "Parametrization %d (N = %d)" % (i, N),
                                      "l")
        store.get("Data_N2").Draw("esame")
        c.SetLogy(1)
        legend_shape.Draw("plain")
        c.Update()

    if isSaveOutput:
        store.saveAs(options.outputfile)

    if not options.isBatch:
        raw_input("Press Enter to continue...")
def main():

    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("-i", "--inputfile", dest="inputfile")
    parser.add_option("-N", "--multiplicity", dest="N", type="int", default=3)
    parser.add_option("-x", "--exclusive", action="store_true",\
          dest="isExclusive", default=False)
    parser.add_option("-l", "--label", dest="label", type="string", default="")
    (options, args) = parser.parse_args()

    N = options.N
    isExclusive = options.isExclusive
    label_text = options.label

    if isExclusive and not (N == 2 or N == 3):
        parser.error("Exclusive plot only for N =2 or 3")

    import configurations as config
    from ROOT import TFile, TCanvas, THStack, TLegend, TPaveText, TH1F, gStyle
    from ModelParser import ModelKey

    gStyle.SetPadTopMargin(0.05)
    gStyle.SetPadRightMargin(0.05)
    gStyle.SetOptStat(0000000)

    suffix = ""
    if not isExclusive:
        suffix = "up"

    sm_files = []
    for model in config.sm_models:
        f = TFile("%s/%s.root" % (config.sm_dir, model), "READ")
        sm_files.append(f)

    bh_weights = []
    bh_files = []
    from BHXsec import BHXsec
    xsec = BHXsec()
    for model in config.bh_showcase:
        f = TFile("%s/%s.root" % (config.bh_dir, model), "READ")
        bh_files.append(f)
        h = f.Get("plotsNoCut/ST")
        nEvents = h.GetEntries()
        bh_weights.append(
            xsec.get(model) / nEvents * config.integrated_luminosity)

    c = TCanvas("ST_Mul%d%s" % (N, suffix), "ST_Mul%d%s" % (N, suffix), 500,
                500)
    hs = THStack()
    hs1 = THStack()

    infile = TFile(options.inputfile, "READ")
    hBkg = infile.Get("Background_N%d%s" % (N, suffix))
    gBkg = infile.Get("BackgroundGraph_N%d%s" % (N, suffix))
    hData = infile.Get("Data_N%d%s" % (N, suffix))
    hBkg = infile.Get("Background_N%d%s" % (N, suffix))
    hBkg.SetMarkerSize(0)
    hBkg_ = hBkg.Clone("BkgLine")
    hBkg.SetFillColor(33)
    hBkg.SetLineColor(33)
    hBkg_.SetLineWidth(3)
    hBkg_.SetLineColor(862)
    hs.Add(hBkg, "e3")

    hData_N2 = infile.Get("Data_N2")
    hBkg_N2 = infile.Get("Background_N2")

    ratio_data = infile.Get("Data_N%d%s" % (N, suffix))
    ratio_data.Sumw2()
    ratio_data.Divide(hData_N2, hBkg_N2)

    ref_N2 = infile.Get("ReferenceTemplateN2_0")
    ref_N3 = infile.Get("ReferenceTemplateN3_0")
    ratio_fits = infile.Get("ReferenceTemplateN2_0")
    ratio_fits.Sumw2()
    ratio_fits.Divide(ref_N2, ref_N3)

    legend = TLegend(0.3026613, 0.6919492, 0.6094355, 0.8816102)
    legend.SetTextSize(0.041)
    #was 0.02966102
    legend.SetTextFont(42)
    legend.SetFillColor(0)
    legend.SetLineColor(0)
    if isExclusive:
        legend.SetHeader("Multiplicity, N = %d" % N)
    else:
        legend.SetHeader("Multiplicity, N #geq %d" % N)
    legend.AddEntry(ratio_data, "Data/Background", "p")
    legend.AddEntry(ratio_fits, "Fit-0 (N=2)/Fit-0 (N=3)", "l")

    legend_sm = TLegend(0.6271774, 0.6769492, 0.8308065, 0.8171186)
    legend_sm.SetTextSize(0.041)
    legend_sm.SetTextFont(42)
    legend_sm.SetFillColor(0)
    legend_sm.SetLineColor(0)

    for i, f in enumerate(bh_files):
        h = f.Get("plotsN%d%s/ST" % (N, suffix))
        h.Rebin(config.rebin)
        h.Scale(bh_weights[i])

        # Add background
        for ibin in range(h.GetNbinsX()):
            h.SetBinContent(ibin+1,\
                  h.GetBinContent(ibin+1)\
                  + hBkg.GetBinContent(ibin+1))

            h.SetLineWidth(2)
            h.SetLineStyle(i + 2)

        hs.Add(h, "hist")
        model = ModelKey(config.bh_showcase[i])
        bh_legend = "M_{D} = %.1f TeV, M_{BH}^{ min} = %.1f TeV, n = %d" % (\
              model.parameter["MD"],
              model.parameter["M"],
              model.parameter["n"])

        #legend.AddEntry(h, bh_legend, "l")

    if isExclusive:
        for i, f in enumerate(sm_files):
            h = f.Get("plotsN%d%s/ST" % (N, suffix))
            h.Rebin(config.rebin)
            h.Scale(config.integrated_luminosity)
            h.SetFillColor(config.sm_colors[i])
            h.SetLineColor(config.sm_colors[i])
            hs1.Add(h, "hist")
            #legend_sm.AddEntry(h, config.sm_models[i], "f")

    #hs.Add(hData, "e")
    #TH1F *ratio_data

    ratio_fits.SetLineColor(4)
    ratio_fits.SetMarkerStyle(0)
    ratio_fits.SetLineWidth(2)

    #hs1.SetMinimum(1e-1)
    ratio_fits.Draw("linee1")
    ratio_data.Draw("same")
    #hs.Draw("samenostack")
    #c.SetLogy(1)
    ratio_fits.GetXaxis().SetTitle("S_{T} (GeV)")
    ratio_fits.GetYaxis().SetTitle("Arbitrary Units")
    ratio_fits.GetYaxis().SetTitleOffset(1.1)

    ratio_fits.GetYaxis().SetTitleSize(0.045)
    ratio_fits.GetYaxis().SetLabelSize(0.045)
    ratio_fits.GetXaxis().SetTitleSize(0.045)
    ratio_fits.GetXaxis().SetLabelSize(0.045)
    ratio_fits.GetXaxis().Draw()
    ibin = 0
    if isExclusive:
        ratio_fits.GetXaxis().SetRangeUser(config.fit_range[0],
                                           config.fit_range[1] + 330)
        ibin = hData.FindBin(config.fit_range[0])
    else:
        ratio_fits.GetXaxis().SetRangeUser(config.norm_range[0],
                                           config.norm_range[1] + 330)
        ibin = hData.FindBin(config.norm_range[0])
    from Styles import formatUncertainty
    formatUncertainty(gBkg)
    #gBkg.Draw("LX")
    #hData.GetXaxis().SetNdivisions(510)

    #hData.Draw("esame")
    ratio_fits.SetTitle("")
    ratio_fits.SetMinimum(0)
    ratio_fits.SetMaximum(3)

    legend.Draw("plain")
    #if isExclusive:
    #   legend_sm.Draw("plain")

    if isExclusive:
        cmslabel = TPaveText(0.45, 0.90, 0.60, 0.93, "brNDC")
    else:
        cmslabel = TPaveText(0.45, 0.90, 0.60, 0.93, "brNDC")
    cmslabel.AddText(config.cmsTitle)
    #cmslabel.AddText(config.cmsSubtitle)
    cmslabel.SetFillColor(0)
    cmslabel.SetTextSize(0.041)
    cmslabel.Draw("plain")

    label = TPaveText(0.8891129, 0.8644068, 0.9435484, 0.9258475, "brNDC")
    label.SetFillColor(0)
    label.SetTextSize(0.0529661)
    label.AddText(label_text)
    label.Draw("plain")

    if isExclusive:
        c.Print("ST_Residuals_Mul%d.pdf" % N)
        c.Print("ST_Residuals_Mul%d.png" % N)
    else:
        c.Print("ST_Mul%dup.pdf" % N)
        c.Print("ST_Mul%dup.png" % N)
    c.Update()

    raw_input("Press Enter to continue...")
Beispiel #8
0
def main():

   from optparse import OptionParser
   parser = OptionParser()
   parser.add_option("-i", "--inputfile", dest="inputfile")
   parser.add_option("-N", "--multiplicity", dest="N", type="int", default=3)
   parser.add_option("-x", "--exclusive", action="store_true",\
         dest="isExclusive", default=False)
   parser.add_option("-l", "--label", dest="label", type="string", default="")
   parser.add_option("-z", "--zeyneplabel",action="store_true",dest="zeynep",default=True)
   (options, args) = parser.parse_args()

   N = options.N
   isExclusive = options.isExclusive
   label_text = options.label

   zeynep = options.zeynep

   if isExclusive and not (N == 2 or N == 3):
      parser.error("Exclusive plot only for N =2 or 3")

   import configurations as config
   from ROOT import TFile, TCanvas, THStack, TLegend, TPaveText, gStyle, TPad, TH1F, TGraphAsymmErrors, TMath
   from ModelParser import ModelKey

   gStyle.SetPadTopMargin(0.05)
   gStyle.SetPadRightMargin(0.05)
   gStyle.SetPadBottomMargin(0.20)

   gStyle.SetErrorX(0.)

   suffix = ""
   if not isExclusive:
      suffix = "up"

   sm_files = []
   for model in config.sm_models:
      f = TFile("%s/%s.root" % (config.sm_dir, model), "READ")
      sm_files.append(f)

   bh_weights = []
   bh_files = []
   from BHXsec import BHXsec
   xsec = BHXsec()
   for model in config.bh_showcase:
      f = TFile("%s/%s.root" % (config.bh_dir, model), "READ")
      bh_files.append(f)
      h = f.Get("plotsNoCut/ST")
      nEvents= h.GetEntries()
      bh_weights.append(xsec.get(model) / nEvents * config.integrated_luminosity)

   c = TCanvas("ST_Mul%d%s" % (N, suffix),
         "ST_Mul%d%s" % (N, suffix), 500, 600)
   hs = THStack()
   hs1 = THStack()

   infile = TFile(options.inputfile, "READ")
   hBkg = infile.Get("Background_N%d%s" % (N, suffix))
   #hnewBkg = infile.Get("histoTemplateN3_0")
   hnewBkg = infile.Get("ReferenceTemplateN3_0")
   gBkg = infile.Get("BackgroundGraph_N%d%s" % (N, suffix))
   hData = infile.Get("Data_N%d%s" % (N, suffix))
   hBkg = infile.Get("Background_N%d%s" % (N, suffix))
   hBkg.SetMarkerSize(0)
   hBkg_ = hBkg.Clone("BkgLine")
   hBkg.SetFillColor(33)
   hBkg.SetLineColor(33)
   hBkg_.SetLineWidth(3)
   hBkg_.SetLineColor(862)

   
   hs.Add(hBkg, "e3")
   hnewBkg.SetLineWidth(3)
   hnewBkg.Scale(10*3.407)
   hs.Add(hnewBkg,"l")


   
   legend = TLegend(0.2826613,0.4819492,0.6094355,0.9416102) # - only for N >= 2 and 3
   #legend = TLegend(0.3026613,0.5519492,0.6094355,0.9416102) # was 0.4919...zeynep
   
   #legend = TLegend(0.3526613,0.5519492,0.6094355,0.9416102) # was 0.4919...

   #legend.SetTextSize(0.041); #was 0.02966102
   legend.SetTextSize(0.037);
   legend.SetTextFont(42);
   legend.SetFillColor(0)
   legend.SetLineColor(0)
   if isExclusive:
      legend.SetHeader("Multiplicity N = %d" % N)
   else:
      legend.SetHeader("Multiplicity N #geq %d" % N)
   legend.AddEntry(hData, "Data", "lep")
   #legend.AddEntry(hnewBkg, "N=3 Fit Rescaled","l")
   legend.AddEntry(hBkg_, "Background", "l")
   legend.AddEntry(hBkg, "Uncertainty", "f")

   legend_sm = TLegend(0.6471774,0.7069492,0.8508065,0.8471186)
#   legend_sm = TLegend(0.6271774,0.7369492,0.8308065,0.8771186)
#   legend_sm.SetTextSize(0.037);
   legend_sm.SetTextSize(0.037);

   legend_sm.SetTextFont(42);
   legend_sm.SetFillColor(0)
   legend_sm.SetLineColor(0)

   for i, f in enumerate(bh_files):
      h = f.Get("plotsN%d%s/ST" % (N, suffix))
      h.Rebin(config.rebin)
      h.Scale(bh_weights[i])

      # Add background
      for ibin in range(h.GetNbinsX()):
         h.SetBinContent(ibin+1,\
               h.GetBinContent(ibin+1)\
               + hBkg.GetBinContent(ibin+1))

         h.SetLineWidth(3)
         #h.SetLineColor(i+2)
         h.SetLineStyle(i+2)
         
         #if i == 0:
            #h.SetLineColor(814)
         if i == 0:
            h.SetLineStyle(5)
            h.SetLineColor(899)
         if i == 1:
            h.SetLineStyle(9)
            h.SetLineColor(4)
         if i == 2:
            h.SetLineStyle(3)
            h.SetLineColor(614)

      hs.Add(h, "hist")
      model = ModelKey(config.bh_showcase[i])
      bh_legend = "M_{D} = %.1f TeV, M_{BH}^{ min} = %.1f TeV, n = %d" % (\
            model.parameter["MD"],
            model.parameter["M"],
            model.parameter["n"])
      if i == 3:
         bh_legend = "M_{D} = 3.0 TeV, M_{QBH}^{ min} = 4.0 TeV, n = 4"    

      legend.AddEntry(h, bh_legend, "l")

#      qbh_legend = "M_{D} = 4.0 TeV, M_{QBH}^{ min} = 5.0 TeV, n = 5"

#      legend.AddEntry(h, qbh_legend, "l")
  
   #if isExclusive:
   zeynep = True
   if zeynep:
      for i, f in enumerate(sm_files):
         h = f.Get("plotsN%d%s/ST" % (N, suffix))
         h.Rebin(config.rebin)
         h.Scale(config.integrated_luminosity)
         h.SetFillColor(config.sm_colors[i])
         h.SetLineColor(config.sm_colors[i])
         hs1.Add(h, "hist")
         legend_sm.AddEntry(h, config.sm_models[i], "f")
   
   #hs.Add(hData, "e")   

   
   hs.Draw("nostack")
   hs1.Draw("same")

   c.SetLogy(1)
   hs.GetXaxis().SetTitle("S_{T} (GeV)")
   hs.GetYaxis().SetTitle(hData.GetYaxis().GetTitle())
   hs.GetYaxis().SetTitleOffset(1.25)

   hs.GetYaxis().SetTitleSize(0.04)
   hs.GetYaxis().SetLabelSize(0.04)
   hs.GetXaxis().SetTitleSize(0.01)
   hs.GetXaxis().SetLabelSize(0.01)
    
   ibin = 0
   #if isExclusive:
   #   hs.GetXaxis().SetRangeUser(config.fit_range[0], config.maxST)
   #   ibin = hData.FindBin(config.fit_range[0])
   #else:
   #   hs.GetXaxis().SetRangeUser(config.norm_range[0], config.maxST)
   #   ibin = hData.FindBin(config.norm_range[0])
   
   if isExclusive:
      hs.GetXaxis().SetRangeUser(1800, config.maxST)
      ibin = hData.FindBin(1800)
   else:
      hs.GetXaxis().SetRangeUser(config.norm_range[0], config.maxST)
      ibin = hData.FindBin(config.norm_range[0])

   from Styles import formatUncertainty
   formatUncertainty(gBkg)
   gBkg.Draw("LX")
   hData.Draw("sameex0")

   hs.SetMinimum(5e-1)
   if isExclusive:
      hs.SetMaximum(1e7)
 #     hs.SetMaximum(hData.GetBinContent(ibin) * 40)
   else:
      #hs.SetMaximum(1e8)
      hs.SetMaximum(hData.GetBinContent(ibin) * 20) # or 1e7 for N>=3 and use 4 models

   legend.Draw("plain")
   #if isExclusive:
   if zeynep:
      legend_sm.Draw("plain")

   if isExclusive:
      cmslabel =TPaveText(0.45,0.96,0.60,0.99,"brNDC");
   else:
      cmslabel = TPaveText(0.45,0.96,0.60,0.99,"brNDC")
   cmslabel.AddText(config.cmsTitle)
   #cmslabel.AddText(config.cmsSubtitle)
   cmslabel.SetFillColor(0)
   cmslabel.SetTextSize(0.041)
   cmslabel.Draw("plain")

   label = TPaveText(0.8891129,0.8644068,0.9435484,0.9258475,"brNDC")
   label.SetFillColor(0)
   #label.SetTextSize(0.0529661);
   label.SetTextSize(0.0529661);
   label.AddText(label_text);
   label.Draw("plain")

   c.RedrawAxis()


      #Divide
   
   ibin = hData.FindBin(config.norm_range[0])
   #print ibin

   fbin = hData.FindBin(config.maxST)
   #print fbin

   hData.Sumw2()
   hBkg_.Sumw2()

   Pull = TH1F("","",fbin-ibin+1,(ibin-1)*100,fbin*100)
   Pull2 = TH1F("","",fbin-ibin+1,(ibin-1)*100,fbin*100)

   Ratio = hData.Clone()
   Ratio.Add(hBkg_,-1)
   #Ratio.Divide(hBkg_) 

   Band = TGraphAsymmErrors(fbin-ibin+1)
   
   for i in range(ibin-1,fbin+1):
      i += 1
      if hData.GetBinContent(i) != 0:
         value = hData.GetBinContent(i) + (hBkg_.GetBinError(i)*hBkg_.GetBinError(i))
         #print Ratio.GetBinError(i),  value**(0.5)
         #print i-19,i,(i)*100, hData.GetBinContent(i) , hBkg_.GetBinContent(i),hData.GetBinContent(i) - hBkg_.GetBinContent(i)
         Pull.SetBinContent(i-19,(hData.GetBinContent(i) - hBkg_.GetBinContent(i))/ Ratio.GetBinError(i))
         #print Ratio.GetBinError(i), abs(Ratio.GetBinContent(i))*0.05
         #Pull.SetBinContent(i-19,(hData.GetBinContent(i) - hBkg_.GetBinContent(i))/ Ratio.GetBinError(i))
         #Pull.SetBinContent(i-19,(hData.GetBinContent(i) / hBkg_.GetBinContent(i)))
         Pull.SetBinError(i-19,Ratio.GetBinError(i))
         #Pull.SetBinError(i-19,hData.GetBinError(i)/gBkg.GetErrorY(i))
         if (hBkg_.GetBinContent(i)*0.05 >  hBkg_.GetBinError(i)):
            #print "bin error too small changing the error to: ", hBkg_.GetBinContent(i)*0.05
            Pull2.SetBinContent(i-19,(hnewBkg.GetBinContent(i) - hBkg_.GetBinContent(i))/ (hBkg_.GetBinContent(i)*0.05))
         else:
            Pull2.SetBinContent(i-19,(hnewBkg.GetBinContent(i) - hBkg_.GetBinContent(i))/ hBkg_.GetBinError(i))
         #print hBkg_.GetBinError(i), hBkg_.GetBinContent(i)*0.05
         #print i, " Pull2: ", Pull2.GetBinContent(i-19), "hnewBkg.GetBinContent(i-1): " , hnewBkg.GetBinContent(i-1), "hBkg_.GetBinContent(i): ", hBkg_.GetBinContent(i)
      else:
         Pull.SetBinContent(i-19,0)
         Pull2.SetBinContent(i-19,(hnewBkg.GetBinContent(i-1) - hBkg_.GetBinContent(i))/ hBkg_.GetBinError(i))

                  
      Band.SetPoint(i,hBkg_.GetBinCenter(i),1.0)
      #print hBkg_.GetBinContent(i), hBkg_.GetBinError(i)
      up   = abs ( 1.- ((hBkg_.GetBinContent(i) + hBkg_.GetBinError(i)) / hBkg_.GetBinContent(i)))
      down = abs ( 1.- ((hBkg_.GetBinContent(i) - hBkg_.GetBinError(i)) / hBkg_.GetBinContent(i)))
      Band.SetPointError(i,0.,0.,down,up)

      
 
   #Band.Print()
   pad = TPad("pad", "pad", 0.0, 0.0, 1.0, 1.0)

   pad.SetTopMargin(0.799999)
   pad.SetRightMargin(0.05)
   pad.SetBottomMargin(0.09)

   pad.SetFillColor(0)
   #pad.SetGridy(1)
   pad.SetFillStyle(0)
   pad.Draw("same")
   pad.cd(0)

   Ratio.SetMarkerStyle(20)
   Pull.SetMarkerStyle(20)
   Pull.SetLineColor(1)
   Pull.SetMarkerSize(0.9)
   #Pull.SetMaximum(3)
   #Pull.SetMinimum(-1)

   Pull.SetMaximum(2.5)
   Pull.SetMinimum(-2.5)
   Pull.GetYaxis().SetNdivisions(5,1);

   Pull.GetXaxis().SetTitle('S_{T} (GeV)')
   Pull.GetXaxis().SetLabelSize(0.04)
   
   Pull.GetYaxis().SetTitleOffset(1.05)
   Pull.GetYaxis().SetLabelSize(0.02)
   Pull.GetYaxis().SetTitleSize(0.025)
   Pull.GetYaxis().CenterTitle(1)


   Pull.GetYaxis().SetTitle('#sigma(Data-Bkg)')

   Pull.SetFillColor(2)
   Pull.Draw("HIST")
   Pull2.SetLineColor(862)
   Pull2.SetLineWidth(3)
   Pull2.SetLineStyle(2)
   #Pull2.Draw("SAME")
   formatUncertainty(Band)
   Band.SetFillStyle(3001)
   
  # Band.Draw("le3")
  # Pull.Draw("ex0same")
   pad.RedrawAxis()

   gStyle.SetOptStat(0)
   

   #block1 =TPaveText(0.370,0.84,0.351,0.86,"brNDC"); # for N>=2 and >=3 only
   #block1 =TPaveText(0.361,0.85,0.377,0.87,"brNDC");
   #block1 =TPaveText(0.333,0.88,0.354,0.85,"brNDC") #for n = 2 only


   block1 =TPaveText(0.331,0.82,0.357,0.85,"brNDC"); #FOR n=3 only
   block1.SetFillColor(0)
   block1.Draw("plain")

   #block2 =TPaveText(0.305,0.84,0.333,0.86,"brNDC"); # for N>=2 and >=3 only
   #block2 =TPaveText(0.395,0.85,0.41,0.87,"brNDC");
   #block2 =TPaveText(0.296,0.88,0.316,0.85,"brNDC"); for n =2 only

   block2 =TPaveText(0.295,0.82,0.317,0.85,"brNDC");  #FOR n=3 only
   block2.SetFillColor(0)
   block2.Draw("plain")
   
   if isExclusive:
     c.Print("ST_Mul%d.pdf" % N)
     c.Print("ST_Mul%d.png" % N)
   else:
     c.Print("ST_Mul%dup.pdf" % N)
     c.Print("ST_Mul%dup.png" % N)    
   c.Update()

   raw_input("Press Enter to continue...")
Beispiel #9
0
def main():

    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("-i", "--inputfile", dest="inputfile")
    parser.add_option("-N", "--multiplicity", dest="N", type="int", default=3)
    parser.add_option("-x", "--exclusive", action="store_true",\
          dest="isExclusive", default=False)
    parser.add_option("-l", "--label", dest="label", type="string", default="")
    parser.add_option("-z",
                      "--zeyneplabel",
                      action="store_true",
                      dest="zeynep",
                      default=True)
    (options, args) = parser.parse_args()

    N = options.N
    isExclusive = options.isExclusive
    label_text = options.label

    zeynep = options.zeynep

    if isExclusive and not (N == 2 or N == 3):
        parser.error("Exclusive plot only for N =2 or 3")

    import configurations as config
    from ROOT import TFile, TCanvas, THStack, TLegend, TPaveText, gStyle, TPad, TH1F, TGraphAsymmErrors, TMath
    from ModelParser import ModelKey

    gStyle.SetPadTopMargin(0.05)
    gStyle.SetPadRightMargin(0.05)
    gStyle.SetPadBottomMargin(0.20)

    gStyle.SetErrorX(0.)

    suffix = ""
    if not isExclusive:
        suffix = "up"

    sm_files = []
    for model in config.sm_models:
        f = TFile("%s/%s.root" % (config.sm_dir, model), "READ")
        sm_files.append(f)

    bh_weights = []
    bh_files = []
    from BHXsec import BHXsec
    xsec = BHXsec()
    for model in config.bh_showcase:
        f = TFile("%s/%s.root" % (config.bh_dir, model), "READ")
        bh_files.append(f)
        h = f.Get("plotsNoCut/ST")
        nEvents = h.GetEntries()
        bh_weights.append(
            xsec.get(model) / nEvents * config.integrated_luminosity)

    c = TCanvas("ST_Mul%d%s" % (N, suffix), "ST_Mul%d%s" % (N, suffix), 500,
                600)
    hs = THStack()
    hs1 = THStack()

    infile = TFile(options.inputfile, "READ")
    hBkg = infile.Get("Background_N%d%s" % (N, suffix))
    #hnewBkg = infile.Get("histoTemplateN3_0")
    hnewBkg = infile.Get("ReferenceTemplateN3_0")
    gBkg = infile.Get("BackgroundGraph_N%d%s" % (N, suffix))
    hData = infile.Get("Data_N%d%s" % (N, suffix))
    hBkg = infile.Get("Background_N%d%s" % (N, suffix))
    hBkg.SetMarkerSize(0)
    hBkg_ = hBkg.Clone("BkgLine")
    hBkg.SetFillColor(33)
    hBkg.SetLineColor(33)
    hBkg_.SetLineWidth(3)
    hBkg_.SetLineColor(862)

    hs.Add(hBkg, "e3")
    hnewBkg.SetLineWidth(3)
    hnewBkg.Scale(10 * 3.407)
    hs.Add(hnewBkg, "l")

    legend = TLegend(0.2826613, 0.4819492, 0.6094355,
                     0.9416102)  # - only for N >= 2 and 3
    #legend = TLegend(0.3026613,0.5519492,0.6094355,0.9416102) # was 0.4919...zeynep

    #legend = TLegend(0.3526613,0.5519492,0.6094355,0.9416102) # was 0.4919...

    #legend.SetTextSize(0.041); #was 0.02966102
    legend.SetTextSize(0.037)
    legend.SetTextFont(42)
    legend.SetFillColor(0)
    legend.SetLineColor(0)
    if isExclusive:
        legend.SetHeader("Multiplicity N = %d" % N)
    else:
        legend.SetHeader("Multiplicity N #geq %d" % N)
    legend.AddEntry(hData, "Data", "lep")
    #legend.AddEntry(hnewBkg, "N=3 Fit Rescaled","l")
    legend.AddEntry(hBkg_, "Background", "l")
    legend.AddEntry(hBkg, "Uncertainty", "f")

    legend_sm = TLegend(0.6471774, 0.7069492, 0.8508065, 0.8471186)
    #   legend_sm = TLegend(0.6271774,0.7369492,0.8308065,0.8771186)
    #   legend_sm.SetTextSize(0.037);
    legend_sm.SetTextSize(0.037)

    legend_sm.SetTextFont(42)
    legend_sm.SetFillColor(0)
    legend_sm.SetLineColor(0)

    for i, f in enumerate(bh_files):
        h = f.Get("plotsN%d%s/ST" % (N, suffix))
        h.Rebin(config.rebin)
        h.Scale(bh_weights[i])

        # Add background
        for ibin in range(h.GetNbinsX()):
            h.SetBinContent(ibin+1,\
                  h.GetBinContent(ibin+1)\
                  + hBkg.GetBinContent(ibin+1))

            h.SetLineWidth(3)
            #h.SetLineColor(i+2)
            h.SetLineStyle(i + 2)

            #if i == 0:
            #h.SetLineColor(814)
            if i == 0:
                h.SetLineStyle(5)
                h.SetLineColor(899)
            if i == 1:
                h.SetLineStyle(9)
                h.SetLineColor(4)
            if i == 2:
                h.SetLineStyle(3)
                h.SetLineColor(614)

        hs.Add(h, "hist")
        model = ModelKey(config.bh_showcase[i])
        bh_legend = "M_{D} = %.1f TeV, M_{BH}^{ min} = %.1f TeV, n = %d" % (\
              model.parameter["MD"],
              model.parameter["M"],
              model.parameter["n"])
        if i == 3:
            bh_legend = "M_{D} = 3.0 TeV, M_{QBH}^{ min} = 4.0 TeV, n = 4"

        legend.AddEntry(h, bh_legend, "l")

#      qbh_legend = "M_{D} = 4.0 TeV, M_{QBH}^{ min} = 5.0 TeV, n = 5"

#      legend.AddEntry(h, qbh_legend, "l")

#if isExclusive:
    zeynep = True
    if zeynep:
        for i, f in enumerate(sm_files):
            h = f.Get("plotsN%d%s/ST" % (N, suffix))
            h.Rebin(config.rebin)
            h.Scale(config.integrated_luminosity)
            h.SetFillColor(config.sm_colors[i])
            h.SetLineColor(config.sm_colors[i])
            hs1.Add(h, "hist")
            legend_sm.AddEntry(h, config.sm_models[i], "f")

    #hs.Add(hData, "e")

    hs.Draw("nostack")
    hs1.Draw("same")

    c.SetLogy(1)
    hs.GetXaxis().SetTitle("S_{T} (GeV)")
    hs.GetYaxis().SetTitle(hData.GetYaxis().GetTitle())
    hs.GetYaxis().SetTitleOffset(1.25)

    hs.GetYaxis().SetTitleSize(0.04)
    hs.GetYaxis().SetLabelSize(0.04)
    hs.GetXaxis().SetTitleSize(0.01)
    hs.GetXaxis().SetLabelSize(0.01)

    ibin = 0
    #if isExclusive:
    #   hs.GetXaxis().SetRangeUser(config.fit_range[0], config.maxST)
    #   ibin = hData.FindBin(config.fit_range[0])
    #else:
    #   hs.GetXaxis().SetRangeUser(config.norm_range[0], config.maxST)
    #   ibin = hData.FindBin(config.norm_range[0])

    if isExclusive:
        hs.GetXaxis().SetRangeUser(1800, config.maxST)
        ibin = hData.FindBin(1800)
    else:
        hs.GetXaxis().SetRangeUser(config.norm_range[0], config.maxST)
        ibin = hData.FindBin(config.norm_range[0])

    from Styles import formatUncertainty
    formatUncertainty(gBkg)
    gBkg.Draw("LX")
    hData.Draw("sameex0")

    hs.SetMinimum(5e-1)
    if isExclusive:
        hs.SetMaximum(1e7)

#     hs.SetMaximum(hData.GetBinContent(ibin) * 40)
    else:
        #hs.SetMaximum(1e8)
        hs.SetMaximum(hData.GetBinContent(ibin) *
                      20)  # or 1e7 for N>=3 and use 4 models

    legend.Draw("plain")
    #if isExclusive:
    if zeynep:
        legend_sm.Draw("plain")

    if isExclusive:
        cmslabel = TPaveText(0.45, 0.96, 0.60, 0.99, "brNDC")
    else:
        cmslabel = TPaveText(0.45, 0.96, 0.60, 0.99, "brNDC")
    cmslabel.AddText(config.cmsTitle)
    #cmslabel.AddText(config.cmsSubtitle)
    cmslabel.SetFillColor(0)
    cmslabel.SetTextSize(0.041)
    cmslabel.Draw("plain")

    label = TPaveText(0.8891129, 0.8644068, 0.9435484, 0.9258475, "brNDC")
    label.SetFillColor(0)
    #label.SetTextSize(0.0529661);
    label.SetTextSize(0.0529661)
    label.AddText(label_text)
    label.Draw("plain")

    c.RedrawAxis()

    #Divide

    ibin = hData.FindBin(config.norm_range[0])
    #print ibin

    fbin = hData.FindBin(config.maxST)
    #print fbin

    hData.Sumw2()
    hBkg_.Sumw2()

    Pull = TH1F("", "", fbin - ibin + 1, (ibin - 1) * 100, fbin * 100)
    Pull2 = TH1F("", "", fbin - ibin + 1, (ibin - 1) * 100, fbin * 100)

    Ratio = hData.Clone()
    Ratio.Add(hBkg_, -1)
    #Ratio.Divide(hBkg_)

    Band = TGraphAsymmErrors(fbin - ibin + 1)

    for i in range(ibin - 1, fbin + 1):
        i += 1
        if hData.GetBinContent(i) != 0:
            value = hData.GetBinContent(i) + (hBkg_.GetBinError(i) *
                                              hBkg_.GetBinError(i))
            #print Ratio.GetBinError(i),  value**(0.5)
            #print i-19,i,(i)*100, hData.GetBinContent(i) , hBkg_.GetBinContent(i),hData.GetBinContent(i) - hBkg_.GetBinContent(i)
            Pull.SetBinContent(
                i - 19, (hData.GetBinContent(i) - hBkg_.GetBinContent(i)) /
                Ratio.GetBinError(i))
            #print Ratio.GetBinError(i), abs(Ratio.GetBinContent(i))*0.05
            #Pull.SetBinContent(i-19,(hData.GetBinContent(i) - hBkg_.GetBinContent(i))/ Ratio.GetBinError(i))
            #Pull.SetBinContent(i-19,(hData.GetBinContent(i) / hBkg_.GetBinContent(i)))
            Pull.SetBinError(i - 19, Ratio.GetBinError(i))
            #Pull.SetBinError(i-19,hData.GetBinError(i)/gBkg.GetErrorY(i))
            if (hBkg_.GetBinContent(i) * 0.05 > hBkg_.GetBinError(i)):
                #print "bin error too small changing the error to: ", hBkg_.GetBinContent(i)*0.05
                Pull2.SetBinContent(
                    i - 19,
                    (hnewBkg.GetBinContent(i) - hBkg_.GetBinContent(i)) /
                    (hBkg_.GetBinContent(i) * 0.05))
            else:
                Pull2.SetBinContent(
                    i - 19,
                    (hnewBkg.GetBinContent(i) - hBkg_.GetBinContent(i)) /
                    hBkg_.GetBinError(i))
            #print hBkg_.GetBinError(i), hBkg_.GetBinContent(i)*0.05
            #print i, " Pull2: ", Pull2.GetBinContent(i-19), "hnewBkg.GetBinContent(i-1): " , hnewBkg.GetBinContent(i-1), "hBkg_.GetBinContent(i): ", hBkg_.GetBinContent(i)
        else:
            Pull.SetBinContent(i - 19, 0)
            Pull2.SetBinContent(
                i - 19,
                (hnewBkg.GetBinContent(i - 1) - hBkg_.GetBinContent(i)) /
                hBkg_.GetBinError(i))

        Band.SetPoint(i, hBkg_.GetBinCenter(i), 1.0)
        #print hBkg_.GetBinContent(i), hBkg_.GetBinError(i)
        up = abs(1. - ((hBkg_.GetBinContent(i) + hBkg_.GetBinError(i)) /
                       hBkg_.GetBinContent(i)))
        down = abs(1. - ((hBkg_.GetBinContent(i) - hBkg_.GetBinError(i)) /
                         hBkg_.GetBinContent(i)))
        Band.SetPointError(i, 0., 0., down, up)

    #Band.Print()
    pad = TPad("pad", "pad", 0.0, 0.0, 1.0, 1.0)

    pad.SetTopMargin(0.799999)
    pad.SetRightMargin(0.05)
    pad.SetBottomMargin(0.09)

    pad.SetFillColor(0)
    #pad.SetGridy(1)
    pad.SetFillStyle(0)
    pad.Draw("same")
    pad.cd(0)

    Ratio.SetMarkerStyle(20)
    Pull.SetMarkerStyle(20)
    Pull.SetLineColor(1)
    Pull.SetMarkerSize(0.9)
    #Pull.SetMaximum(3)
    #Pull.SetMinimum(-1)

    Pull.SetMaximum(2.5)
    Pull.SetMinimum(-2.5)
    Pull.GetYaxis().SetNdivisions(5, 1)

    Pull.GetXaxis().SetTitle('S_{T} (GeV)')
    Pull.GetXaxis().SetLabelSize(0.04)

    Pull.GetYaxis().SetTitleOffset(1.05)
    Pull.GetYaxis().SetLabelSize(0.02)
    Pull.GetYaxis().SetTitleSize(0.025)
    Pull.GetYaxis().CenterTitle(1)

    Pull.GetYaxis().SetTitle('#sigma(Data-Bkg)')

    Pull.SetFillColor(2)
    Pull.Draw("HIST")
    Pull2.SetLineColor(862)
    Pull2.SetLineWidth(3)
    Pull2.SetLineStyle(2)
    #Pull2.Draw("SAME")
    formatUncertainty(Band)
    Band.SetFillStyle(3001)

    # Band.Draw("le3")
    # Pull.Draw("ex0same")
    pad.RedrawAxis()

    gStyle.SetOptStat(0)

    #block1 =TPaveText(0.370,0.84,0.351,0.86,"brNDC"); # for N>=2 and >=3 only
    #block1 =TPaveText(0.361,0.85,0.377,0.87,"brNDC");
    #block1 =TPaveText(0.333,0.88,0.354,0.85,"brNDC") #for n = 2 only

    block1 = TPaveText(0.331, 0.82, 0.357, 0.85, "brNDC")
    #FOR n=3 only
    block1.SetFillColor(0)
    block1.Draw("plain")

    #block2 =TPaveText(0.305,0.84,0.333,0.86,"brNDC"); # for N>=2 and >=3 only
    #block2 =TPaveText(0.395,0.85,0.41,0.87,"brNDC");
    #block2 =TPaveText(0.296,0.88,0.316,0.85,"brNDC"); for n =2 only

    block2 = TPaveText(0.295, 0.82, 0.317, 0.85, "brNDC")
    #FOR n=3 only
    block2.SetFillColor(0)
    block2.Draw("plain")

    if isExclusive:
        c.Print("ST_Mul%d.pdf" % N)
        c.Print("ST_Mul%d.png" % N)
    else:
        c.Print("ST_Mul%dup.pdf" % N)
        c.Print("ST_Mul%dup.png" % N)
    c.Update()

    raw_input("Press Enter to continue...")