def defineStyle(self):

        import tdrStyle as tdrStyle
        tdrStyle.setTDRStyle()

        ROOT.TGaxis.SetExponentOffset(-0.08, 0.00, "y")
#!/usr/bin/env python

import numpy as np

import ROOT
from ROOT import TCanvas, TPad, TFile, TPaveText, TLegend
from ROOT import gBenchmark, gStyle, gROOT, TStyle
from ROOT import TH1D, TF1, TGraphErrors, TMultiGraph

from math import sqrt

from array import array

import tdrStyle
tdrStyle.setTDRStyle()

import CMS_lumi

#change the CMS_lumi variables (see CMS_lumi.py)
CMS_lumi.lumi_13TeV = '35.9 fb^{-1}'
CMS_lumi.writeExtraText = 1
#CMS_lumi.extraText = 'Preliminary'
CMS_lumi.extraText = ''

iPos = 0

gStyle.SetOptFit(0)


def k_value(Num, Den):
    if Num == 0 or Den == 0:
Beispiel #3
0
#! /usr/bin/python

import ROOT
import tdrStyle
from array import array
from selectionCuts import *

ROOT.gROOT.LoadMacro("PlotBase.cc+")
ROOT.gROOT.LoadMacro("PlotHists.cc+")
ROOT.gROOT.LoadMacro("PlotResolution.cc+")

tdrStyle.setTDRStyle()

ROOT.gROOT.SetBatch(True)

sampledir = "/afs/cern.ch/work/d/dabercro/public/Winter15/flatTreesSkimmedV7/"
goodRuns = "/afs/cern.ch/work/d/dabercro/public/Winter15/flatTreesSkimmedV7/"

MuonFile = ROOT.TFile(goodRuns + "monojet_SingleMuon.root")
SingleElecFile = ROOT.TFile(goodRuns + "monojet_SingleElectron.root")
SinglePhoFile = ROOT.TFile(goodRuns + "monojet_SinglePhoton.root")
DYFile = ROOT.TFile(sampledir + "monojet_DYJetsToLL_M-50.root")
GJetsFile = ROOT.TFile(sampledir + "monojet_GJets.root")

MuonTree = MuonFile.Get("events")
SingleElecTree = SingleElecFile.Get("events")
SinglePhoTree = SinglePhoFile.Get("events")
DYTree = DYFile.Get("events")
GJetsTree = GJetsFile.Get("events")

plotter = ROOT.PlotResolution()
Beispiel #4
0
sys.argv = grootargs

doFit = opt.DOFIT
doPlots = opt.DOPLOTS

if (not os.path.exists("plots") and doPlots):
    os.system("mkdir plots")

from ROOT import *
from LoadData_Unc import *

RooMsgService.instance().setGlobalKillBelow(RooFit.WARNING)

if (opt.DOPLOTS and os.path.isfile('tdrStyle.py')):
    from tdrStyle import setTDRStyle
    setTDRStyle()

Histos = {}
acceptance = {}
qcdUncert = {}
pdfUncert = {}


def getunc(channel, List, m4l_bins, m4l_low, m4l_high, obs_reco, obs_gen,
           obs_bins, genbin):

    obs_gen_low = obs_bins[genbin]
    obs_gen_high = obs_bins[genbin + 1]

    obs_gen_lowest = obs_bins[0]
    obs_gen_highest = obs_bins[len(obs_bins) - 1]
Beispiel #5
0
def systematicFits():
    setTDRStyle();



  # Choose the jet multiplicity
    jet_num = "2b_";
    jet_num_temp = "ge2j";
  
    metBin = ["1","2","3","4","5"]

    templ = "_central";


    #differential histo
    xbins = [1,25,45,70,100,150] 
    #histograms for comaprison
    #diff generators	
    theor  = TH1D("theor", "", 5, xbins);  
    nlo =  TH1D("nlo", "", 5, xbins);
    powheg =  TH1D("powheg", "", 5, xbins);
    #syst samples
    sysup = TH1D("sysup", "", 5, xbins); 
    sysdown = TH1D("sysdown", "", 5, xbins); 
    mup = TH1D("mup", "", 5, xbins); 
    mdown = TH1D("mdown", "", 5, xbins); 
    #measured
    measured  = TH1D("meas", "", 5, xbins);  #muon
    ele  = TH1D("ele", "", 5, xbins);     
    comb  = TH1D("comb", "", 5, xbins); 
   #compare histo
    nominal  = TH1D("nominal", "", 5, xbins);
  
    #theroy onn ttbar only due to stats (need to also change the string for w/z)
    syst = ["_qup","_qdown","_mup","_mdown"]
   
    #syst[8] = {"_jup","_jdown","_METup","_METdown","_pup","_pdown","_bup","_bdown"};

        
    for sysErr in range(0,4):
        print syst[sysErr]

   #asymerrors
        n = 5;
        x = [0]*n;   
        y = [0]*n;
        exl = [0]*n;
        exh = [0]*n;

        theorNum = 0;	
        nloNum = 0;
        powhegNum = 0;
	
        #syst
        upNum = 0;  
        downNum = 0; 
        mupNum = 0;  
        mdownNum = 0; 

        theorXsect = 157.5;
        totXsect = 0;
        for met in range(0,5):
  #for systematics
            f_central = TFile.Open("PFhistosForFitting_met"+metBin[met]+templ+".root");  
            f_all = TFile.Open("PFhistosForFitting_metall"+templ+".root");
            f_templates  = TFile.Open("QCDetaData.root");

            signal = f_central.Get("metEta_h_"+jet_num+"signal");
            wjets = f_central.Get("metEta_h_"+jet_num+"wjets");
            zjets = f_central.Get("metEta_h_"+jet_num+"zjets");
            SinglT = f_central.Get("metEta_h_"+jet_num+"SinglT");
            ttbar = f_central.Get("metEta_h_"+jet_num+"ttbar");
  
            QCD = f_central.Get("metEta_h_"+jet_num+"QCD");
            tt_Z = f_central.Get("metEta_h_"+jet_num+"tt_Z");
            tt_W = f_central.Get("metEta_h_"+jet_num+"tt_W");
            DATA = f_central.Get("metEta_h_"+jet_num+"data");
            QCData = f_templates.Get("metEta_h_"+jet_num_temp);

            ttbarAll = f_all.Get("metEta_h_"+jet_num+"ttbar");
 
            tt_sys = f_central.Get("metEta_h_"+jet_num+"tt"+syst[sysErr]);  
            #wjets_sys = f_central.Get("metEta_h_"+jet_num+"wjets"+syst[sysErr]);
            #zjets_sys = f_central.Get("metEta_h_"+jet_num+"zjets"+syst[sysErr]);
  
            #for other gens
            ttbar_nlo = f_central.Get("metEta_h_"+jet_num+"nlo");   
            ttbar_powheg = f_central.Get("metEta_h_"+jet_num+"powheg");
            ttbar_nloAll = f_all.Get("metEta_h_"+jet_num+"nlo");
            ttbar_powhegAll = f_all.Get("metEta_h_"+jet_num+"powheg");
 
            #for sys up/down
  
            ttbar_sys = f_central.Get("metEta_h_"+jet_num+"tt_qup");   
            ttbar_sysdown = f_central.Get("metEta_h_"+jet_num+"tt_qdown");
            ttbarUpAll = f_all.Get("metEta_h_"+jet_num+"tt_qup");
            ttbarDownAll = f_all.Get("metEta_h_"+jet_num+"tt_qdown");
  
            ttbar_mup = f_central.Get("metEta_h_"+jet_num+"tt_mup");
            ttbar_mdown = f_central.Get("metEta_h_"+jet_num+"tt_mdown");
            ttbarUpAllMat = f_all.Get("metEta_h_"+jet_num+"tt_mup");
            ttbarDownAllMat = f_all.Get("metEta_h_"+jet_num+"tt_mdown");
  
            #Choose rebin factor...
            rebinF = 8;

            ttbar_sys.Rebin(rebinF);

            #tt_sys.Rebin(rebinF);
            #wjets_sys.Rebin(rebinF);
            #zjets_sys.Rebin(rebinF);
  
            ttbar_sysdown.Rebin(rebinF);  
            ttbar_mup.Rebin(rebinF);
            ttbar_mdown.Rebin(rebinF);

            ttbar_nlo.Rebin(rebinF);
            ttbar_powheg.Rebin(rebinF);
 

            signal.Rebin(rebinF);
            wjets.Rebin(rebinF);
            zjets.Rebin(rebinF);
            QCD.Rebin(rebinF);
            SinglT.Rebin(rebinF);
            ttbar.Rebin(rebinF);
            tt_Z.Rebin(rebinF);
            tt_W.Rebin(rebinF);
            DATA.Rebin(rebinF);
            QCData.Rebin(rebinF/2);


            NttbarUp = ttbar_sys.Integral();
            NtotPassUp = ttbarUpAll.Integral();    
            NttbarDown = ttbar_sysdown.Integral();
            NtotPassDown = ttbarDownAll.Integral();      
            NttbarUpMat = ttbar_mup.Integral();
            NtotPassUpMat = ttbarUpAllMat.Integral();    
            NttbarDownMat = ttbar_mdown.Integral();
            NtotPassDownMat = ttbarDownAllMat.Integral();

            NttbarNLO = ttbar_nlo.Integral();
            NtotPassNLO = ttbar_nloAll.Integral();    
            NttbarPOW = ttbar_powheg.Integral();
            NtotPassPOW = ttbar_powhegAll.Integral();
 
            Nsignal = signal.Integral();
            Nwjets = wjets.Integral();
            Nzjets = zjets.Integral();
            NQCD = QCD.Integral();
            NSinglT = SinglT.Integral();
            Nttbar = ttbar.Integral();
            NtotPass = ttbarAll.Integral();


            Ntt_Z =tt_Z.Integral();
            Ntt_W =tt_W.Integral();
            tot =   Nttbar+NSinglT+NQCD+Nzjets+Nwjets+Ntt_Z+Ntt_W;
            Ndata = DATA.Integral();
            wstring = "wjets";
            zstring = "zjets";
            qcdstring = "";


            # Read in the data and templates.
            getDataFromHis(f_central, jet_num, nbins); # Get data histrogram form central selection
            getSignalFromHis(f_central, jet_num, syst[sysErr], nbins, ttbar_Vec, ttbar_err_Vec);# Get ttbar, single-top histrograms form MC with entral selection 

            getTemFromHis(f_central, wstring, jet_num, nbins, wjets_Vec, wjets_err_Vec);                 
            getTemFromHis(f_central, zstring, jet_num, nbins, zjets_Vec, zjets_err_Vec);
            getTemFromHis(f_templates, qcdstring, jet_num_temp, nbins, qcd_Vec, qcd_err_Vec);

            f_central.Close();
            f_templates.Close(); 
 
  # Initialize minuit, set initial values etc. of parameters.
            npar = 4;              # the number of parameters
            minuit = TMinuit(npar);
            minuit.SetFCN(fcn);
  
  #minuit.SetPrintLevel(1);
            minuit.SetPrintLevel(-1);
            minuit.SetErrorDef(1.);
 
            fraction= NSinglT/Nsignal;
  
            ierflg = 0;
            parName = ["ttbar+single-top", "wjets", "zjets", "qcd"] #background parameters
            par = [Nsignal, Nwjets, Nzjets, NQCD]               #using the MC estimation as the start values 1fb

            for i=0 in range(0,npar):
                minuit.mnparm(i, parName[i], par[i], 10., 0, Ntotal, ierflg);

            #the following is copied from Fabian's fitting code to improve minimum, but you can comment it, it won't affect the fitting results.
            # 1 standard
            # 2 try to improve minimum (slower)
            arglist = [0]*10;
            arglist[0]=1;
            minuit.mnexcm("SET STR",arglist,1,ierflg);
            minuit.Migrad();
  
            outpar = [0]*npar
            err = [0]*npar;
    
            for i=0 in range(0,npar):
                minuit.GetParameter(i,outpar[i],err[i]);
  
            xs_fit = (outpar[0]-NSinglT)/(NtotPass/theorXsect);  #=out-Nsing/lumi*Accept  Nsing should probs be xsect*Accept... 1fb
            xs_fitup = (outpar[0]+err[0]-NSinglT)/(NtotPass/theorXsect);  #=out-Nsing/lumi*Accept  Nsing should probs be xsect*Accept... 1fb
            xs_fitdown = (outpar[0]-err[0]-NSinglT)/(NtotPass/theorXsect);  #=out-Nsing/lumi*Accept  Nsing should probs be xsect*Accept... 1fb
  
            #madgraph
            theorNum = (Nttbar)/(NtotPass/theorXsect);
            nloNum = (NttbarNLO)/(NtotPassNLO/theorXsect);
            powhegNum = (NttbarPOW)/(NtotPassPOW/theorXsect);

            #systematics  
            upNum = (NttbarUp)/(NtotPassUp/theorXsect);
            downNum = (NttbarDown)/(NtotPassDown/theorXsect);  

            mupNum = (NttbarUpMat)/(NtotPassUpMat/theorXsect);
            mdownNum = (NttbarDownMat)/(NtotPassDownMat/theorXsect); 

  
  
            totXsect += xs_fit;

            data_Vec = []
            ttbar_Vec = []
            wjets_Vec = []
            zjets_Vec = []
            qcd_Vec = []

            ttbar_err_Vec = []
            wjets_err_Vec = []
            zjets_err_Vec = []
            qcd_err_Vec = []


            width = measured.GetBinWidth(met+1);

            measured.SetBinContent(met+1,xs_fit/width);
            measured.SetBinError(met+1,(xs_fitup-xs_fit)/width);     

            nlo.SetBinContent(met+1,nloNum/157.5/width);
            powheg.SetBinContent(met+1,powhegNum/157.5/width);
            theor.SetBinContent(met+1, (theorNum/157.5/width)); 

            sysup.SetBinContent(met+1, (upNum/157.5/width)); 
            sysdown.SetBinContent(met+1, (downNum/157.5/width)); 
            mup.SetBinContent(met+1, (mupNum/157.5/width)); 
            mdown.SetBinContent(met+1, (mdownNum/157.5/width));


            x[met] = measured.GetBinCenter(met+1);
            y[met] = xs_fit/width;
            #exl[met] = measured.GetBinWidth(met+1)/2.;
            #exh[met] = measured.GetBinWidth(met+1)/2.;
            exl[met] = 0.;
            exh[met] = 0.;
doFit = opt.DOFIT
doPlots = opt.DOPLOTS

if (not os.path.exists("plots") and doPlots):
    os.system("mkdir plots")

from ROOT import *
from LoadData_dsperka import *
LoadData(opt.SOURCEDIR)
save = ""

RooMsgService.instance().setGlobalKillBelow(RooFit.WARNING)    

if (opt.DOPLOTS and os.path.isfile('tdrStyle.py')):
    from tdrStyle import setTDRStyle
    setTDRStyle()

Histos = {}
wrongfrac = {}
dwrongfrac = {}
binfrac_wrongfrac = {}
dbinfrac_wrongfrac = {}
outfrac = {}
doutfrac = {}
binfrac_outfrac = {}
dbinfrac_outfrac = {}
outinratio = {}
doutinratio = {}
CB_mean_post = {}
CB_sigma_post = {}
CB_dmean_post = {}
Beispiel #7
0
def CompareExpLimitsEachYear(dirname_prefix, tag=''):
    import tdrStyle as tdrStyle
    tdrStyle.setTDRStyle()

    tcanvas = ROOT.TCanvas('tcanvas' + tag, 'distro' + tag, 800, 600)
    tcanvas.cd()
    tcanvas.SetLogy()
    tcanvas.SetGrid()
    dic_limit = GetLimit(dirname_prefix + '2016' + tag, 'exp.txt')
    tgr_2016exp = GetLimitGraph(dic_limit)
    dic_2016exp = copy.deepcopy(dic_limit)

    dic_limit = GetLimit(dirname_prefix + '2017' + tag, 'exp.txt')
    tgr_2017exp = GetLimitGraph(dic_limit)
    dic_2017exp = copy.deepcopy(dic_limit)

    dic_limit = GetLimit(dirname_prefix + '2018' + tag, 'exp.txt')
    tgr_2018exp = GetLimitGraph(dic_limit)
    dic_2018exp = copy.deepcopy(dic_limit)

    dic_limit = GetLimit(dirname_prefix + '_201620172018' + tag, 'exp.txt')
    tgr_201620172018exp = GetLimitGraph(dic_limit)
    dic_201620172018exp = copy.deepcopy(dic_limit)

    dic_limit = GetggHWWXsec()
    tgr_theory = GetLimitGraph(dic_limit)
    dic_theory = copy.deepcopy(dic_limit)

    dic_3yr = copy.deepcopy(dic_2016exp)
    dic_3yr.update(dic_2017exp)
    dic_3yr.update(dic_2018exp)
    dic_3yr.update(dic_201620172018exp)
    dic_3yr.update(dic_theory)
    miny, maxy = GetMinMaxFromDic(dic_3yr)

    tgr_2016exp.SetLineStyle(2)
    tgr_2017exp.SetLineStyle(2)
    tgr_2018exp.SetLineStyle(2)
    tgr_201620172018exp.SetLineStyle(2)

    tgr_2016exp.SetLineColor(ROOT.kBlue)
    tgr_2017exp.SetLineColor(ROOT.kGreen)
    tgr_2018exp.SetLineColor(ROOT.kOrange)
    tgr_201620172018exp.SetLineColor(ROOT.kBlack)
    tgr_theory.SetLineColor(ROOT.kRed)

    masses = [
        200, 210, 230, 250, 270, 300, 350, 400, 450, 500, 550, 600, 650, 700,
        750, 800, 850, 900, 1000, 1500, 2000, 2500, 3000, 4000, 5000
    ]
    Nmass = len(masses)
    frame = ROOT.TH2F("frame", "", Nmass, min(masses), max(masses), 100,
                      miny * 0.1, maxy * 10)
    frame.SetYTitle("Limit 95% CL_{s} on #sigma_{X#rightarrowWW} [pb]")
    frame.GetYaxis().SetTitleSize(0.05)
    frame.GetYaxis().SetLabelSize(0.03)
    frame.SetXTitle("m_{X} (GeV)")
    frame.GetXaxis().SetTitleSize(0.035)
    frame.GetXaxis().SetLabelSize(0.03)
    frame.GetXaxis().SetTitleOffset(1.5)
    frame.Draw()

    tgr_2016exp.Draw('l same')
    tgr_2017exp.Draw('l same')
    tgr_2018exp.Draw('l same')
    tgr_201620172018exp.Draw('l same')
    tgr_theory.Draw('l same')

    import CMS_lumi as CMS_lumi
    CMS_lumi.lumi_13TeV = '137 fb^{-1}'
    CMS_lumi.writeExtraText = 1
    CMS_lumi.extraText = "Preliminary"
    CMS_lumi.relPosX = 0.12
    CMS_lumi.lumi_sqrtS = '13 TeV'
    iPeriod = 4
    iPos = 0
    CMS_lumi.CMS_lumi(tcanvas, iPeriod, iPos)
    #x1 y1 x2 y2
    leg = ROOT.TLegend(0.5, 0.75, 0.95, 0.94)
    leg.SetFillColor(0)
    leg.SetBorderSize(1)
    leg.SetTextFont(8)
    leg.SetTextSize(20)
    if not tag == '': tag = ' ' + tag
    leg.AddEntry(tgr_2016exp, "Expected Limit 2016" + tag, "l")
    leg.AddEntry(tgr_2017exp, "Expected Limit 2017" + tag, "l")
    leg.AddEntry(tgr_2018exp, "Expected Limit 2018" + tag, "l")
    leg.AddEntry(tgr_201620172018exp, "3yrs combined" + tag, "l")
    leg.AddEntry(tgr_theory, "SM-like scenario, f_{vbf}=0", 'l')

    leg.Draw("same")
    tag = tag.replace(' ', '_')
    tcanvas.SaveAs('compare' + tag + '.png')