if sys.argv[i] == "--forPaper": Preliminary = ""
        if sys.argv[i] == "--printPlots": printPlots = True
        if sys.argv[i].find("--fit-region=") != -1:
            frLabelString = sys.argv[i].replace("--fit-region=","")
            # frLabels = frLabelString.split(",")
            frLabels = [frLabelString,]
        if sys.argv[i].find("-MC=") != -1:
            Preliminary = "Simulation"
            datasetName = sys.argv[i].replace("-MC=","")
        if sys.argv[i].find("-Label=") != -1:
            Label = sys.argv[i].replace("-Label=","")
        if sys.argv[i].find("-Lumi=") != -1: Lumi = float(sys.argv[i].replace("-Lumi=",""))
        if sys.argv[i].find("-Energy=") != -1: Energy = float(sys.argv[i].replace("-Energy=",""))

    from RazorBox import getBinning
    MRbins    = getBinning(Box, "MR"   , "Btag")
    Rsqbins   = getBinning(Box, "Rsq"  , "Btag")
    nBtagbins = getBinning(Box, "nBtag", "Btag")
    print MRbins
    print Rsqbins
    print nBtagbins


    x = array("d",MRbins)
    y = array("d",Rsqbins)
    z = array("d",nBtagbins)

    # file with bkg predictions from toys
    fileIn = rt.TFile.Open(fileName)
    myTree = fileIn.Get("myTree")
def goodPlot(varname, Box, outFolder, Label, Energy, Lumi, hMRTOTcopy, hMRTOT, hMRTTj1b, hMRTTj2b, hMRVpj, hMRData, hMRSignal, c1, pad1, pad2, fit3D, btagOpt):
    rt.gStyle.SetOptStat(0000)
    rt.gStyle.SetOptTitle(0)

    #c1.SetLeftMargin(0.15)
    #c1.SetRightMargin(0.05)
    #c1.SetTopMargin(0.05)
    #c1.SetBottomMargin(0.05)

    pad1.Range(-213.4588,-0.3237935,4222.803,5.412602);
    pad2.Range(-213.4588,-2.206896,4222.803,3.241379);

    pad1.SetLeftMargin(0.15)
    pad2.SetLeftMargin(0.15)
    pad1.SetRightMargin(0.05)
    pad2.SetRightMargin(0.05)
    pad1.SetTopMargin(0.05)
    pad2.SetTopMargin(0.)
    pad1.SetBottomMargin(0.)
    #pad2.SetTopMargin(0.04)
    #pad1.SetBottomMargin(0.004)
    pad2.SetBottomMargin(0.47)

    pad1.Draw()
    pad1.cd()
    rt.gPad.SetLogy()

    # from RazorBox import getBinning
    MRbins    = getBinning(Box, "MR"   , "Btag")
    Rsqbins   = getBinning(Box, "Rsq"  , "Btag")
    nBtagbins = getBinning(Box, "nBtag", "Btag")

    binMap = {"MR":MRbins,"RSQ":Rsqbins,"BTAG":nBtagbins}
    print binMap
    print binMap[varname]
    print binMap[varname][0]
    # MR PLOT
    hMRData.SetLineWidth(2)
    hMRTOTcopy.SetFillStyle(1001)
    hMRTOTcopy.SetFillColor(rt.kBlue-10)
    hMRTOTcopy.SetLineColor(rt.kBlue)
    hMRTOTcopy.SetLineWidth(2)
    hMRTOTcopy.GetXaxis().SetTitle("")
    hMRTOTcopy.GetXaxis().SetLabelOffset(0.16)
    hMRTOTcopy.GetXaxis().SetLabelSize(0.06)
    hMRTOTcopy.GetYaxis().SetLabelSize(0.06)
    hMRTOTcopy.GetXaxis().SetTitleSize(0.06)
    hMRTOTcopy.GetYaxis().SetTitleSize(0.08)
    hMRTOTcopy.GetXaxis().SetTitleOffset(0.8)
    hMRTOTcopy.GetYaxis().SetTitleOffset(0.7)
    hMRTOTcopy.GetXaxis().SetTicks("+-")
    #hMRTOTcopy.GetXaxis().SetRange(1,FindLastBin(hMRTOTcopy))
    hMRTOTcopy.GetXaxis().SetRange(1,hMRTOTcopy.GetNbinsX())
    hMRTOTcopy.GetYaxis().SetTitle("Events")
    hMRTOTmax = 1.e6
    if hMRTOTcopy.GetMaximum() < 1.e5 and hMRTOTcopy.GetMaximum() >= 1.e4:
        hMRTOTmax = 1.e5
    if hMRTOTcopy.GetMaximum() < 1.e4 and hMRTOTcopy.GetMaximum() >= 1.e3:
        hMRTOTmax = 1.e4
    elif hMRTOTcopy.GetMaximum() < 1.e3 and hMRTOTcopy.GetMaximum() >= 1.e2:
        hMRTOTmax = 1.e3
    elif hMRTOTcopy.GetMaximum() < 1.e2 and hMRTOTcopy.GetMaximum() >= 1.e1:
        hMRTOTmax = 1.e2

    if varname == "MR":
        hMRTOTmax = 4.5E3
        hMRTOTmax = 2*2287.49060835
        print ""
        print "MAXIMUM = ", hMRTOTcopy.GetMaximum()
        print ""
        hMRTOTcopy.SetMaximum(hMRTOTmax)
        hMRTOTcopy.SetMinimum(0.05)
    elif varname == "RSQ":
        hMRTOTmax = 1.2e4
        hMRTOTmax = 5*2421.97127178
        print ""
        print "MAXIMUM = ", hMRTOTcopy.GetMaximum()
        print ""
        hMRTOTcopy.SetMaximum(hMRTOTmax)
        hMRTOTcopy.SetMinimum(0.5)
    elif varname == "BTAG":
        hMRTOTcopy.SetMaximum(hMRTOTmax)
        hMRTOTcopy.SetMinimum(0.5)
    #if hMRTOTcopy.GetBinContent(hMRTOTcopy.GetNbinsX())>=10.: hMRTOTcopy.SetMinimum(5.)
    #if hMRTOTcopy.GetBinContent(hMRTOTcopy.GetNbinsX())>=100.: hMRTOTcopy.SetMinimum(50.)
    hMRTOTcopy.Draw("e2")

    # TTj1b is shown only if it has some entry
    showTTj1b = hMRTTj1b != None
    if showTTj1b:
        if  hMRTTj1b.Integral()<=1: showTTj1b = False
    # TTj2b is shown only if it has some entry
    showTTj2b = hMRTTj2b != None
    if showTTj2b:
        if  hMRTTj2b.Integral()<=1: showTTj2b = False
    # Vpj is shown only if it has some entry
    showVpj = hMRVpj != None
    if showVpj:
        if  hMRVpj.Integral()<=1: showVpj = False
    showSignal = hMRSignal !=None

    if showVpj:
        hMRVpj.SetFillStyle(0)
        col1 = rt.gROOT.GetColor(rt.kGreen-7)
        # col1.SetAlpha(1.0)
        hMRVpj.SetLineColor(rt.kGreen-3)
        hMRVpj.SetLineWidth(2)
        if varname == "BTAG":
            hMRVpj.Add(hMRTTj1b)
            # col1.SetAlpha(1.0)
            hMRVpj.SetFillStyle(1001)
            hMRVpj.SetFillColor(rt.kGreen-7)
        hMRVpj.Draw("histsame")
    if showTTj2b:
        hMRTTj2b.SetFillStyle(0)
        col1 = rt.gROOT.GetColor(rt.kRed-4)
        # col1.SetAlpha(1.0)
        hMRTTj2b.SetLineColor(rt.kRed)
        hMRTTj2b.SetLineWidth(2)
        if varname == "BTAG":
            # col1.SetAlpha(1.0)
            hMRTTj2b.SetFillStyle(1001)
            hMRTTj2b.SetFillColor(rt.kRed-4)
        hMRTTj2b.Draw("histsame")
    if showTTj1b:
        hMRTTj1b.SetFillStyle(0)
        col2 = rt.gROOT.GetColor(rt.kViolet-4)
        # col2.SetAlpha(1.0)
        hMRTTj1b.SetLineColor(rt.kViolet)
        hMRTTj1b.SetLineWidth(2)
        if varname == "BTAG":
            # col2.SetAlpha(1.0)
            hMRTTj1b.SetFillStyle(1001)
            hMRTTj1b.SetFillColor(rt.kViolet-4)
        hMRTTj1b.Draw("histsame")
    if varname =="BTAG":
        hMRTOTcopy.SetFillStyle(1001)
        hMRTOTcopy.SetFillColor(rt.kBlue-10)
        hMRTOTcopy.SetLineColor(rt.kBlue)
        hMRTOTcopy.Draw("e2same")
    hMRData.SetLineColor(rt.kBlack)
    hMRData.SetMarkerStyle(20)
    hMRData.SetMarkerColor(rt.kBlack)
    hMRData.Draw("pesame")
    hMRTOT.SetLineWidth(2)
    hMRTOT.SetFillStyle(0)
    hMRTOT.DrawCopy("histsame")

    if showSignal:
        c4 = rt.gROOT.GetColor(rt.kGray+2)
        # c4.SetAlpha(1.0)
        hMRSignal.SetLineColor(rt.kBlack)
        hMRSignal.SetFillColor(rt.kGray+2)
        hMRSignal.SetLineStyle(2)
        hMRSignal.SetFillStyle(3005)
        hMRSignal.SetLineWidth(2)
        hMRSignal.Draw("histfsame")

    if showTTj2b and showTTj1b and showVpj and showSignal:
        leg = rt.TLegend(0.7,0.5,0.93,0.93)
    elif (showTTj2b and showTTj1b and showVpj) or (showTTj2b and showTTj1b and showSignal):
        leg = rt.TLegend(0.7,0.55,0.93,0.93)
    elif (showTTj2b and showTTj1b) or (showTTj2b and showSignal) or (showTTj1b and showSignal):
        leg = rt.TLegend(0.7,0.65,0.93,0.93)
    else:
        leg = rt.TLegend(0.7,0.72,0.93,0.93)
    leg.SetFillColor(0)
    leg.SetTextFont(42)
    leg.SetLineColor(0)

    if noBtag:
        btagLabel = ", no b-tag"
    else:
        if btagOpt==0:
            btagLabel = ", #geq 1 b-tag"
        elif btagOpt==1:
            btagLabel = ", 1 b-tag"
        elif btagOpt==23:
            btagLabel = ", #geq2 b-tag"
        elif btagOpt==2:
            btagLabel = ", 2 b-tag"
        elif btagOpt==3:
            btagLabel = ", #geq3 b-tag"

    if datasetName=="TTJets" or datasetName=="WJets" or datasetName=="DYJetsToLL" or datasetName=="ZJetsToNuNu" or  datasetName=="SMCocktail":
        if btagOpt==0:
            leg.AddEntry(hMRData,"Simulated Data","lep")
        else:
            leg.AddEntry(hMRData,"Simulated Data%s"%(btagLabel),"lep")

    else:
        if btagOpt==0:
            leg.AddEntry(hMRData,"Data","lep")
        else:
            leg.AddEntry(hMRData,"Data%s"%(btagLabel),"lep")

    leg.AddEntry(hMRTOTcopy,"Bkgd%s"%(btagLabel))
    #leg.AddEntry(hMRTOTcopy,"Total Bkgd")
    if showTTj1b and showTTj2b:
        if varname=="BTAG":
            if showVpj:
                leg.AddEntry(hMRTTj1b,"1 b-tag, t#bar{t}+jets","f")
                leg.AddEntry(hMRVpj,"1 b-tag, V+jets","f")
            else:
                leg.AddEntry(hMRTTj1b,"1 b-tag","f")
            leg.AddEntry(hMRTTj2b,"#geq 2 b-tag","f")
        else:
            if showVpj:
                leg.AddEntry(hMRTTj1b,"1 b-tag, t#bar{t}+jets","l")
                leg.AddEntry(hMRVpj,"1 b-tag, V+jets","l")
            else:
                leg.AddEntry(hMRTTj1b,"1 b-tag","l")
            leg.AddEntry(hMRTTj2b,"#geq 2 b-tag","l")

    #if showSignal:
    #    leg.AddEntry(hMRSignal,"Signal","fl")
    leg.Draw("same")

    # plot labels
    if Box in ["EleMultiJet","MuMultiJet"]:
        pt = rt.TPaveText(0.17,0.67,0.7,0.93,"ndc")
    else:
        pt = rt.TPaveText(0.25,0.67,0.7,0.93,"ndc")
    pt.SetBorderSize(0)
    pt.SetTextSize(0.05)
    pt.SetFillColor(0)
    pt.SetFillStyle(0)
    pt.SetLineColor(0)
    pt.SetTextAlign(21)
    pt.SetTextFont(42)
    pt.SetTextSize(0.062)
    text = pt.AddText("CMS %s #sqrt{s} = %i TeV" %(Preliminary,int(Energy)))
    if datasetName=="TTJets":
        text = pt.AddText("t#bar{t}+jets %s Box %s" %(Box,btagLabel))
    elif datasetName=="WJets":
        text = pt.AddText("W+jets %s Box %s" %(Box,btagLabel))
    elif datasetName=="SMCocktail":
        text = pt.AddText("Total SM %s Box %s" %(Box,btagLabel))
    elif datasetName=="ZJetsToNuNu":
        text = pt.AddText("Z(#nu#nu)+jets %s Box %s" %(Box,btagLabel))
    elif datasetName=="DYJetsToLL":
        text = pt.AddText("Z(ll)+jets")
    else:
        if Box=="Jet2b":
            text = pt.AddText("Razor 2b-Jet Box #int L = %3.1f fb^{-1}" %(Lumi))
        else:
            text = pt.AddText("Razor %s Box #int L = %3.1f fb^{-1}" %(Box,Lumi))
    pt.Draw()
    pad1.Draw()

    c1.Update()

    c1.cd()

    pad2.Draw()
    pad2.cd()
    rt.gPad.SetLogy(0)
    hMRData.Sumw2()
    hMRTOTcopy.Sumw2()
    hMRDataDivide = hMRData.Clone(hMRData.GetName()+"Divide")
    hMRDataDivide.Sumw2()

    hMRTOTclone = hMRTOT.Clone(hMRTOTcopy.GetName()+"Divide")
    hMRTOTcopyclone = hMRTOTcopy.Clone(hMRTOTcopy.GetName()+"Divide")
    hMRTOTcopyclone.GetYaxis().SetLabelSize(0.18)
    hMRTOTcopyclone.SetTitle("")
    hMRTOTcopyclone.SetMaximum(3.5)
    hMRTOTcopyclone.SetMinimum(0.)
    if varname=="BTAG": hMRTOTcopyclone.GetXaxis().SetLabelSize(0.32)
    else: hMRTOTcopyclone.GetXaxis().SetLabelSize(0.22)
    hMRTOTcopyclone.GetXaxis().SetTitleSize(0.22)

    for i in range(1, hMRData.GetNbinsX()+1):
        tmpVal = hMRTOTcopyclone.GetBinContent(i)
        if tmpVal != -0.:
            hMRDataDivide.SetBinContent(i, hMRDataDivide.GetBinContent(i)/tmpVal)
            hMRDataDivide.SetBinError(i, hMRDataDivide.GetBinError(i)/tmpVal)
            hMRTOTcopyclone.SetBinContent(i, hMRTOTcopyclone.GetBinContent(i)/tmpVal)
            hMRTOTcopyclone.SetBinError(i, hMRTOTcopyclone.GetBinError(i)/tmpVal)
            hMRTOTclone.SetBinContent(i, hMRTOTclone.GetBinContent(i)/tmpVal)
            hMRTOTclone.SetBinError(i, hMRTOTclone.GetBinError(i)/tmpVal)

    hMRTOTcopyclone.GetXaxis().SetTitleOffset(0.97)
    hMRTOTcopyclone.GetXaxis().SetLabelOffset(0.02)
    if varname == "MR":
        hMRTOTcopyclone.GetXaxis().SetTitle("M_{R} [GeV]")
    if varname == "RSQ":
        hMRTOTcopyclone.GetXaxis().SetTitle("R^{2}")
    if varname == "BTAG":
        hMRTOTcopyclone.GetXaxis().SetTitle("n_{b-tag}")

    hMRTOTcopyclone.GetYaxis().SetNdivisions(504,rt.kTRUE)
    hMRTOTcopyclone.GetYaxis().SetTitleOffset(0.2)
    hMRTOTcopyclone.GetYaxis().SetTitleSize(0.22)
    hMRTOTcopyclone.GetYaxis().SetTitle("Data/Bkgd")
    hMRTOTcopyclone.GetXaxis().SetTicks("+")
    hMRTOTcopyclone.GetXaxis().SetTickLength(0.07)
    hMRTOTcopyclone.SetMarkerColor(rt.kBlue-10)
    hMRTOTcopyclone.Draw("e2")
    hMRDataDivide.Draw('pesame')
    hMRTOTcopyclone.Draw("axissame")

    pad2.Update()
    pad1.cd()
    pad1.Update()
    pad1.Draw()
    c1.cd()

    if fit3D and btagOpt>0:
        c1.Print("%s/%s_%ib_%s.pdf" %(outFolder,varname,btagOpt,Label))
        c1.Print("%s/%s_%ib_%s.C" %(outFolder,varname,btagOpt,Label))
    else:
        c1.Print("%s/%s_%s.pdf" %(outFolder,varname,Label))
        c1.Print("%s/%s_%s.C" %(outFolder,varname,Label))
    signalRegion = options.signalRegion
    expected_a_priori = options.expected_a_priori

    ##other param
    other_parameters = cfg.getVariables(box, "other_parameters")
    temp = rt.RooWorkspace("temp")
    for parameters in other_parameters:
        temp.factory(parameters)
    lumi = temp.var("lumi_value").getVal()
    lumi_uncert = temp.var("lumi_uncert").getVal()
    trigger_uncert = temp.var("trigger_uncert").getVal()
    lepton_uncert = temp.var("lepton_uncert").getVal()

    ##binning
    from RazorBox import getBinning
    x = array('d', getBinning(box, "MR"   , "Btag"))
    y = array('d', getBinning(box, "Rsq"  , "Btag"))
    z = array('d', getBinning(box, "nBtag", "Btag"))
    ## nMaxBins = 162
    ## nBins = nMaxBins
    nBins = (len(x)-1)*(len(y)-1)*(len(z)-1)

    ##convert 3D pdf into 1D
    #prepare 1D histo to dump the 3D pdf
    th1x = rt.RooRealVar("th1x","th1x",0,0,nBins)
    th1xBins = array('d',range(0,nBins+1))
    th1xRooBins = rt.RooBinning(nBins, th1xBins, "uniform")
    th1x.setBinning(th1xRooBins)
    #import 1D histo into container ws
    w = rt.RooWorkspace("w%s"%box)
    RootTools.Utils.importToWS(w,th1x)