示例#1
0
def main():

    gROOT.SetBatch(1)

    parser = ArgumentParser()
    parser.add_argument('--nepoch',
                        type=int,
                        default=10,
                        help='number of epochs to run the training for')
    parser.add_argument('--TrainingFile', default="", help='path to training')
    #parser.add_argument('--initepoch', type=int, default=1,help='starting epoch when using an existing training')
    parser.add_argument(
        '--infile',
        default=
        "/user/smoortga/Analysis/2017/ttcc_Analysis/CMSSW_8_0_25/src/ttcc/matching/FullTrainingSampleWithFlippedAndWeights/TTTo2L2Nu_TuneCP5_PSweights_13TeV-powheg-pythia8.root",
        help='path to the training file')
    parser.add_argument('--tag',
                        default=time.strftime("%a%d%b%Y_%Hh%Mm%Ss"),
                        help='name of output directory')
    parser.add_argument('--skipEvery',
                        type=int,
                        default=1,
                        help='ignore one entry every')
    parser.add_argument('--verbose',
                        type=int,
                        default=1,
                        help='verbosity of training')
    args = parser.parse_args()

    from keras import backend as K
    #K.set_session(K.tf.Session(config=K.tf.ConfigProto(intra_op_parallelism_threads = 4, inter_op_parallelism_threads = 4)))

    nb_classes = 3

    print "******** READING INPUT FILES ************"

    f_ = TFile(args.infile)
    correct_tree = f_.Get("tree_correct")
    flipped_tree = f_.Get("tree_flipped")
    wrong_tree = f_.Get("tree_wrong")

    # branchnames = [i.GetName() for i in correct_tree.GetListOfBranches()]
    #     branchnames = sorted(branchnames)
    #     for i in branchnames:
    #         print '"'+i+'",'
    #     sys.exit(1)

    variables = [
        # "CSVv2_addlead",
        #     "CSVv2_addsublead",
        #     "CSVv2_antitopb",
        #     "CSVv2_topb",
        "DeepCSVBDiscr_addlead",
        "DeepCSVBDiscr_addsublead",
        "DeepCSVBDiscr_antitopb",
        "DeepCSVBDiscr_topb",
        "DeepCSVCvsB_addlead",
        "DeepCSVCvsB_addsublead",
        "DeepCSVCvsB_antitopb",
        "DeepCSVCvsB_topb",
        "DeepCSVCvsL_addlead",
        "DeepCSVCvsL_addsublead",
        "DeepCSVCvsL_antitopb",
        "DeepCSVCvsL_topb",
        "DeltaR_adds",
        "DeltaR_antitopb_lepneg",
        "DeltaR_topb_leppos",
        "Eta_addlead",
        "Eta_addsublead",
        "Eta_antitopb",
        "Eta_topb",
        # "Phi_addlead",
        #     "Phi_addsublead",
        #     "Phi_antitopb",
        #     "Phi_topb",
        "minv_adds",
        "minv_antitopb_lepneg",
        "minv_topb_leppos",
        "pT_addlead",
        "pT_addsublead",
        "pT_antitopb",
        "pT_topb"
    ]

    if not os.path.isdir(os.getcwd() + "/" + args.tag):
        os.mkdir(os.getcwd() + "/" + args.tag)
    pickle.dump(variables,
                open(os.getcwd() + "/" + args.tag + "/variables.pkl", 'wb'))

    X_sig = rootnp.tree2array(correct_tree, variables, step=args.skipEvery)
    X_sig = rootnp.rec2array(X_sig)
    w_sig = rootnp.tree2array(correct_tree, "weight", step=args.skipEvery)
    wbtag_sig = rootnp.tree2array(correct_tree,
                                  "btag_weight",
                                  step=args.skipEvery)
    X_flip = rootnp.tree2array(flipped_tree, variables, step=args.skipEvery)
    X_flip = rootnp.rec2array(X_flip)
    w_flip = rootnp.tree2array(flipped_tree, "weight", step=args.skipEvery)
    wbtag_flip = rootnp.tree2array(flipped_tree,
                                   "btag_weight",
                                   step=args.skipEvery)
    X_bkg = rootnp.tree2array(wrong_tree, variables, step=args.skipEvery)
    X_bkg = rootnp.rec2array(X_bkg)
    w_bkg = rootnp.tree2array(wrong_tree, "weight", step=args.skipEvery)
    wbtag_bkg = rootnp.tree2array(wrong_tree,
                                  "btag_weight",
                                  step=args.skipEvery)

    max_len = min(len(X_sig), len(X_bkg), len(X_flip))
    X_sig = X_sig[0:max_len]
    X_flip = X_flip[0:max_len]
    X_bkg = X_bkg[0:max_len]
    w_sig = w_sig[0:max_len]
    w_flip = w_flip[0:max_len]
    w_bkg = w_bkg[0:max_len]
    wbtag_sig = wbtag_sig[0:max_len]
    wbtag_flip = wbtag_flip[0:max_len]
    wbtag_bkg = wbtag_bkg[0:max_len]

    X = np.concatenate((X_sig, X_flip, X_bkg))
    #X = [i for i in X_tmp if not np.isnan(X_tmp).any()]
    w = np.concatenate((w_sig, w_flip, w_bkg))
    wbtag = np.concatenate((wbtag_sig, wbtag_flip, wbtag_bkg))
    wtotal = np.asarray([i * j for i, j in zip(w, wbtag)])
    y = np.concatenate(
        (np.ones(len(X_sig)), np.full(len(X_flip), 2),
         np.zeros(len(X_bkg))))  # correct = 1, flipped = 2, wrong = 0
    Y = np_utils.to_categorical(y.astype(int), nb_classes)

    print "******** SCALING INPUTS ************"

    scaler = StandardScaler()
    scaler.fit(X)
    if not os.path.isdir(os.getcwd() + "/" + args.tag):
        os.mkdir(os.getcwd() + "/" + args.tag)
    print "storing output in %s" % (os.getcwd() + "/" + args.tag)
    pickle.dump(scaler, open(os.getcwd() + "/" + args.tag + "/scaler.pkl",
                             'wb'))
    X = scaler.transform(X)

    X_train, X_test, y_train, y_test, Y_train, Y_test, w_train, w_test, wbtag_train, wbtag_test, wtotal_train, wtotal_test = train_test_split(
        X, y, Y, w, wbtag, wtotal, test_size=0.2)

    print "%i training correct events, %i training flipped events and %i training background events" % (
        len(X_train[y_train == 1]), len(
            X_train[y_train == 2]), len(X_train[y_train == 0]))

    print "******** BUILDING/TRAINING MODEL ************"
    if args.TrainingFile == "":
        model = make_model(X_train.shape[1:], nb_classes, nb_epoch=args.nepoch)
        print model.summary()

        if args.nepoch > 0:
            batch_size = 128
            if not os.path.isdir(os.getcwd() + "/" + args.tag):
                os.mkdir(os.getcwd() + "/" + args.tag)
            first_phase_nepoch = int(args.nepoch / 2.)
            second_phase_nepoch = args.nepoch - first_phase_nepoch
            train_history_phase1 = model.fit(
                X_train,
                Y_train,
                batch_size=batch_size,
                nb_epoch=first_phase_nepoch,
                validation_data=(X_test, Y_test),
                callbacks=[
                    ModelCheckpoint(os.getcwd() + "/" + args.tag +
                                    "/model_checkpoint_save.hdf5")
                ],
                shuffle=True,
                verbose=args.verbose,
                sample_weight=wbtag_train)
            pickle.dump(
                train_history_phase1.history,
                open(os.getcwd() + "/" + args.tag + "/loss_and_acc_phase1.pkl",
                     'wb'))
            train_history_phase2 = model.fit(
                X_train,
                Y_train,
                batch_size=batch_size,
                nb_epoch=second_phase_nepoch,
                validation_data=(X_test, Y_test),
                callbacks=[
                    ModelCheckpoint(os.getcwd() + "/" + args.tag +
                                    "/model_checkpoint_save.hdf5")
                ],
                shuffle=True,
                verbose=args.verbose,
                sample_weight=wtotal_train)
            pickle.dump(
                train_history_phase2.history,
                open(os.getcwd() + "/" + args.tag + "/loss_and_acc_phase2.pkl",
                     'wb'))

            #pickle.dump(train_history.history,open(os.getcwd() + "/"+args.tag+"/loss_and_acc.pkl",'wb'))
            drawTrainingCurve(
                os.getcwd() + "/" + args.tag + "/loss_and_acc_phase1.pkl",
                os.getcwd() + "/" + args.tag + "/training_curve_phase1.pdf")
            drawTrainingCurve(
                os.getcwd() + "/" + args.tag + "/loss_and_acc_phase2.pkl",
                os.getcwd() + "/" + args.tag + "/training_curve_phase2.pdf")

    else:
        model = load_model(args.TrainingFile)
        print model.summary()

        if args.nepoch > 0:
            batch_size = 128
            if not os.path.isdir(os.getcwd() + "/" + args.tag):
                os.mkdir(os.getcwd() + "/" + args.tag)
            train_history_contd2 = model.fit(
                X_train,
                Y_train,
                batch_size=batch_size,
                nb_epoch=args.nepoch,
                validation_data=(X_test, Y_test),
                callbacks=[
                    ModelCheckpoint(os.getcwd() + "/" + args.tag +
                                    "/model_checkpoint_save.hdf5")
                ],
                shuffle=True,
                verbose=args.verbose,
                sample_weight=wtotal_train)

            pickle.dump(
                train_history_contd2.history,
                open(os.getcwd() + "/" + args.tag + "/loss_and_acc_contd2.pkl",
                     'wb'))
            drawTrainingCurve(
                os.getcwd() + "/" + args.tag + "/loss_and_acc_contd2.pkl",
                os.getcwd() + "/" + args.tag + "/training_curve_contd2.pdf")

    # print X_test[0], X_test[0].shape, type(X_test[0])


#     print X_test, X_test.shape, type(X_test)
#     sys.exit(1)

# Validation
    discr_buffer = model.predict(X_test)
    discr_bkg = discr_buffer[:, 0]
    discr_sig1 = discr_buffer[:, 1]
    discr_sig2 = discr_buffer[:, 2]
    discr = [
        max(i / (i + k), j / (j + k))
        for i, j, k in zip(discr_sig1, discr_sig2, discr_bkg)
    ]
    correct_discr = [i for idx, i in enumerate(discr) if y_test[idx] == 1]
    flipped_discr = [i for idx, i in enumerate(discr) if y_test[idx] == 2]
    wrong_discr = [i for idx, i in enumerate(discr) if y_test[idx] == 0]
    discr_buffer_train = model.predict(X_train)
    discr_train_bkg = discr_buffer_train[:, 0]
    discr_train_sig1 = discr_buffer_train[:, 1]
    discr_train_sig2 = discr_buffer_train[:, 2]
    discr_train = [
        max(i / (i + k), j / (j + k))
        for i, j, k in zip(discr_train_sig1, discr_train_sig2, discr_train_bkg)
    ]
    correct_discr_train = [
        i for idx, i in enumerate(discr_train) if y_train[idx] == 1
    ]
    flipped_discr_train = [
        i for idx, i in enumerate(discr_train) if y_train[idx] == 2
    ]
    wrong_discr_train = [
        i for idx, i in enumerate(discr_train) if y_train[idx] == 0
    ]
    fpr, tpr, thres = roc_curve(
        np.concatenate((np.ones(len(correct_discr) + len(flipped_discr)),
                        np.zeros(len(wrong_discr)))),
        np.concatenate((correct_discr, flipped_discr, wrong_discr)))
    AUC = 1 - roc_auc_score(
        np.concatenate((np.ones(len(correct_discr) + len(flipped_discr)),
                        np.zeros(len(wrong_discr)))),
        np.concatenate((correct_discr, flipped_discr, wrong_discr)))
    makeROC(fpr, tpr, thres, AUC,
            os.getcwd() + "/" + args.tag + "/ROC_curve.pdf",
            "correct or flipped", "wrong")
    makeDiscr(
        {
            "correct train": correct_discr_train,
            "flipped train": flipped_discr_train,
            "wrong train": wrong_discr_train
        }, {
            "correct test": correct_discr,
            "flipped test": flipped_discr,
            "wrong test": wrong_discr
        },
        os.getcwd() + "/" + args.tag + "/Discriminator.pdf",
        "top-matching NN output")
示例#2
0
from ROOT import ROOT, gROOT, gStyle, gRandom, TSystemDirectory
from ROOT import TFile, TChain, TTree, TCut, TH1F, TH2F, THStack, TGraph
from ROOT import TStyle, TCanvas, TPad
from ROOT import TLegend, TLatex, TText, TLine

import optparse
usage = "usage: %prog [options]"
parser = optparse.OptionParser(usage)
parser.add_option("-b",
                  "--bash",
                  action="store_true",
                  default=False,
                  dest="bash")
#parser.add_option("-v", "--variable", action="store", type="string", dest="variable", default="")
(options, args) = parser.parse_args()
if options.bash: gROOT.SetBatch(True)
gStyle.SetOptStat(0)

NTUPLEDIR = "/nfs/dust/cms/user/lbenato/RecoStudies_ntuples_v5/"
PLOTDIR = "/afs/desy.de/user/l/lbenato/LongLivedReconstruction/CMSSW_8_0_26_patch1/src/Analyzer/LongLivedReco/macro/plots/"

samples = {
    'ZH_MS-40_ctauS-0': {
        'file':
        'ZH_HToSSTobbbb_ZToLL_MH-125_MS-40_ctauS-0_TuneCUETP8M1_13TeV-powheg-pythia8',
        'leg_name': 'm_{#pi} = 40 GeV, c#tau = 0 mm',
        'type': 'reconstruction',
    },
    'ZH_MS-40_ctauS-0p05': {
        'file':
        'ZH_HToSSTobbbb_ZToLL_MH-125_MS-40_ctauS-0p05_TuneCUETP8M1_13TeV-powheg-pythia8',
示例#3
0
                  dest='year',
                  default='2016')
parser.add_option("-c",
                  "--category",
                  action="store",
                  type="string",
                  dest="category",
                  default="")
parser.add_option("-b",
                  "--btagging",
                  action="store",
                  type="string",
                  dest="btagging",
                  default="loose")
(options, args) = parser.parse_args()
gROOT.SetBatch(True)  #suppress immediate graphic output

########## SETTINGS ##########

BTAGGING = options.btagging
CARDDIR = "datacards/MANtag_study/" + BTAGGING + "/"
YEAR = options.year
ISMC = True
PLOTDIR = "plots/MANtag_study/datacards/"
LUMI = {'2016': 35920., '2017': 41530., '2018': 59740., 'run2': 137190.}
if YEAR not in ['2016', '2017', '2018', 'run2']:
    print "unknown year:", YEAR
    sys.exit()

if BTAGGING not in ['tight', 'medium', 'loose', 'semimedium']:
    print "unknown btagging requirement:", BTAGGING
示例#4
0
# ##-- Data / MC Analysis
# python NtupleAnalysis.py --DataMC --dataFolder Data --mcFolder Backgrounds --signalFolder Signal --VarBatch mass --CutsType Loose --Lumi 41.5 --Tags HHWWggTag_0 --verbose  --noQCD --testFeatures
#########################################################################################################################################################################################################

##-- Import python modules
from python.Options import *
from python.NtupleAnalysisTools import *
from ROOT import gPad, TAxis, TTree, TChain, TLine, TGraph, TMultiGraph, TFile, TCanvas, gROOT, TH2F, TH1F, kPink, kGreen, kCyan, TLegend, kRed, kOrange, kBlack, TLegend, gStyle, TObjArray, kBlue, TGraphErrors
from array import array
import os

##-- Define flags and variables based on user input
args = GetOptions()

if __name__ == '__main__':
    gROOT.SetBatch(1)  # Do not output upon draw statement
    if (args.Efficiency):
        ol = '/eos/user/a/atishelm/www/HHWWgg/NtupleAnalysis/cutFlow/'
    elif (args.DataMC):
        if (args.testFeatures):
            ol = '/eos/user/a/atishelm/www/HHWWgg/NtupleAnalysis/DataMC_testFeatures/'
        else:
            ol = '/eos/user/a/atishelm/www/HHWWgg/NtupleAnalysis/DataMC_v7/'
    nTupleDirec = '/afs/cern.ch/work/a/atishelm/public/ForJosh/2017_DataMC_ntuples_moreVars/'

    if (args.Efficiency):
        print "Performing cut flow efficiency analysis"
        if (args.ratio):
            nMassPoints = len(args.massPoints.split(','))
            ratio_x_vals = []
            for i in range(5):
示例#5
0
# python plotHists.py -r <region> -p <POI> -o <plotOpen> -d<dir> -n<NORM> -c<category> -s<SPLIT>
# python plotHists.py -r 2lss -p Bin2l -n 1 -b 1 -c SubCat2l -s inclusive
# 9 March 2019   Binghuan Li
# ***
#

import sys, os, subprocess
import optparse
import distutils.util
import math
import ROOT
from ROOT import TCanvas, TColor, TGaxis, TH1F, TPad, TString, TFile, TH1, THStack, gROOT, TStyle, TAttFill, TLegend, TGraphAsymmErrors
from ROOT import kBlack, kBlue, kRed, kCyan, kViolet, kGreen, kOrange, kGray, kPink, kTRUE
from ROOT import gROOT

gROOT.SetBatch(kTRUE)

usage = 'usage: %prog [options]'
parser = optparse.OptionParser(usage)
parser.add_option('-r',
                  '--region',
                  dest='region',
                  help='region to plot: 2lss or ttWctrl',
                  default='2lss',
                  type='string')
parser.add_option('-p',
                  '--parameter',
                  dest='POI',
                  help='parameter of interest',
                  default='Bin2l',
                  type='string')
示例#6
0
def studysignificance(part_label, pt_min, pt_max, sig_set, bkg_set, names,
                      target_var, suffix, plot_dir):
    gROOT.SetBatch(True)
    gROOT.ProcessLine("gErrorIgnoreLevel = 2000;")

    common_dict, pt_specific_dict = getOptimizationParameters(
        part_label, pt_min, pt_max)
    plot_FONLL(common_dict, part_label, suffix, plot_dir)
    sig_before_sel = calc_sig_Dmeson(common_dict, pt_specific_dict, pt_min,
                                     pt_max)
    mass = common_dict['mass']
    sigma = pt_specific_dict['sigma']
    sig_region = [mass - 3 * sigma, mass + 3 * sigma]
    mass_min, mass_max = getmasscut(part_label)
    mass_cuts = [mass_min, mass_max]

    fig_eff = plt.figure(figsize=(20, 15))
    plt.xlabel('Probability', fontsize=20)
    plt.ylabel('Efficiency', fontsize=20)
    plt.title("Efficiency Signal", fontsize=20)

    fig_signif = plt.figure(figsize=(20, 15))
    plt.xlabel('Probability', fontsize=20)
    plt.ylabel('Significance (A.U.)', fontsize=20)
    plt.title("Significance vs probability ", fontsize=20)

    num_steps = 101

    for name in names:
        mask_sig = sig_set[target_var].values == 1
        eff_array, x_axis = calc_efficiency(sig_set[mask_sig], name, num_steps)
        plt.figure(fig_eff.number)
        plt.plot(x_axis,
                 eff_array,
                 alpha=0.3,
                 label='%s' % name,
                 linewidth=4.0)

        sig_array = [eff * sig_before_sel for eff in eff_array]
        mask_bkg = bkg_set['cand_type_ML'].values == 0
        bkg_array, x_axis_bkg = calc_bkg(bkg_set[mask_bkg], name, num_steps,
                                         mass_cuts,
                                         common_dict['mass_fit_lim'],
                                         pt_specific_dict['bin_width'],
                                         sig_region)

        signif_array = calc_signif(sig_array, bkg_array)
        plt.figure(fig_signif.number)
        plt.plot(x_axis,
                 signif_array,
                 alpha=0.3,
                 label='%s' % name,
                 linewidth=4.0)

    plt.figure(fig_eff.number)
    plt.legend(loc="lower left", prop={'size': 18})
    plt.savefig(plot_dir + '/Efficiency%sSignal.png' % suffix)

    plt.figure(fig_signif.number)
    plt.legend(loc="lower left", prop={'size': 18})
    plt.savefig(plot_dir + '/Significance%s.png' % suffix)

    return
示例#7
0
def main():
    """
    Main function
    """

    parser = argparse.ArgumentParser(description="Arguments to pass")
    parser.add_argument(
        "configfile_name",
        metavar="text",
        default="config_Dplus_pp5TeV.yml",
        help="input yaml config file",
    )
    parser.add_argument("--batch",
                        action="store_true",
                        default=False,
                        help="suppress video output")
    args = parser.parse_args()

    if args.batch:
        gROOT.SetBatch(True)

    # load info from config file
    with open(args.configfile_name, "r") as yml_configfile:
        cfg = yaml.safe_load(yml_configfile)
    frac_method = cfg["fraction"]

    histos, norm = load_inputs(cfg)

    # consistency check of bins
    ptlims = {}
    for histo in ["rawyields", "acceffp", "acceffnp"]:
        ptlims[histo] = get_hist_binlimits(histos[histo])
        if (histo != "rawyields"
                and not np.equal(ptlims[histo], ptlims["rawyields"]).all()):
            print("\033[91mERROR: histo binning not consistent. Exit\033[0m")
            sys.exit(10)

    # compute cross section
    axistit_cross = "d#sigma/d#it{p}_{T} (pb GeV^{-1} #it{c})"
    axistit_cross_times_br = "d#sigma/d#it{p}_{T} #times BR (pb GeV^{-1} #it{c})"
    axistit_pt = "#it{p}_{T} (GeV/#it{c})"
    axistit_fprompt = "#if{f}_{prompt}"
    gfraction = TGraphAsymmErrors(0)
    gfraction.SetNameTitle("gfraction", f";{axistit_pt};{axistit_fprompt}")
    hptspectrum = TH1F(
        "hptspectrum",
        f";{axistit_pt};{axistit_cross}",
        len(ptlims["rawyields"]) - 1,
        ptlims["rawyields"],
    )
    hptspectrum_wo_br = TH1F(
        "hptspectrum_wo_br",
        f";{axistit_pt};{axistit_cross_times_br}",
        len(ptlims["rawyields"]) - 1,
        ptlims["rawyields"],
    )

    for i_pt, (ptmin, ptmax) in enumerate(
            zip(ptlims["rawyields"][:-1], ptlims["rawyields"][1:])):
        pt_cent = (ptmax + ptmin) / 2
        pt_delta = ptmax - ptmin
        rawy = histos["rawyields"].GetBinContent(i_pt + 1)
        rawy_unc = histos["rawyields"].GetBinError(i_pt + 1)
        eff_times_acc_prompt = histos["acceffp"].GetBinContent(i_pt + 1)
        eff_times_acc_nonprompt = histos["acceffnp"].GetBinContent(i_pt + 1)
        ptmin_fonll = (
            histos["FONLL"]["nonprompt"]["central"].GetXaxis().FindBin(ptmin *
                                                                       1.0001))
        ptmax_fonll = (
            histos["FONLL"]["nonprompt"]["central"].GetXaxis().FindBin(ptmax *
                                                                       0.9999))
        crosssec_nonprompt_fonll = [
            histos["FONLL"]["nonprompt"][pred].Integral(
                ptmin_fonll, ptmax_fonll, "width") / (ptmax - ptmin)
            for pred in histos["FONLL"]["nonprompt"]
        ]

        # compute prompt fraction
        if frac_method == "Nb":
            frac = compute_fraction_nb(  # BR already included in FONLL prediction
                rawy,
                eff_times_acc_prompt,
                eff_times_acc_nonprompt,
                crosssec_nonprompt_fonll,
                pt_delta,
                1.0,
                1.0,
                norm["events"],
                norm["sigmaMB"],
            )
        elif frac_method == "fc":
            crosssec_prompt_fonll = [
                histos["FONLL"]["prompt"][pred].Integral(
                    ptmin_fonll, ptmax_fonll, "width") / (ptmax - ptmin)
                for pred in histos["FONLL"]["prompt"]
            ]
            frac = compute_fraction_fc(
                eff_times_acc_prompt,
                eff_times_acc_nonprompt,
                crosssec_prompt_fonll,
                crosssec_nonprompt_fonll,
            )

        # compute cross section times BR
        crosssec, crosssec_unc = compute_crosssection(
            rawy,
            rawy_unc,
            frac[0],
            eff_times_acc_prompt,
            ptmax - ptmin,
            1.0,
            norm["sigmaMB"],
            norm["events"],
            1.0,
            frac_method,
        )

        hptspectrum.SetBinContent(i_pt + 1, crosssec / norm["BR"])
        hptspectrum.SetBinError(i_pt + 1, crosssec_unc / norm["BR"])
        hptspectrum_wo_br.SetBinContent(i_pt + 1, crosssec)
        hptspectrum_wo_br.SetBinError(i_pt + 1, crosssec_unc)
        gfraction.SetPoint(i_pt, pt_cent, frac[0])
        gfraction.SetPointError(i_pt, pt_delta / 2, pt_delta / 2,
                                frac[0] - frac[1], frac[2] - frac[0])

    # create plots
    style_hist = StyleObject1D()
    style_hist.markercolor = kAzure + 4
    style_hist.markerstyle = kFullCircle
    style_hist.markersize = 1
    style_hist.linecolor = kAzure + 4
    style_hist.linewidth = 2
    style_hist.draw_options = "P"

    fig_crosssec = ROOTFigure(1,
                              1,
                              column_margin=(0.14, 0.035),
                              row_margin=(0.1, 0.035),
                              size=(600, 800))
    fig_crosssec.axes(label_size=0.025, title_size=0.030)
    fig_crosssec.axes("x", title=axistit_pt, title_offset=1.5)
    fig_crosssec.axes("y", title=axistit_cross_times_br, title_offset=1.8)
    fig_crosssec.define_plot(0, 0, y_log=True)
    fig_crosssec.add_object(hptspectrum_wo_br, style=style_hist)
    fig_crosssec.create()

    output_dir = cfg["output"]["directory"]
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    fig_crosssec.save(
        os.path.join(output_dir, f'{cfg["output"]["filename"]}.pdf'))

    # save output file
    output_file = TFile(
        os.path.join(output_dir, f'{cfg["output"]["filename"]}.root'),
        "recreate")
    hptspectrum.Write()
    hptspectrum_wo_br.Write()
    gfraction.Write()
    for hist in histos:
        if isinstance(histos[hist], TH1):
            histos[hist].Write()
        else:
            for flav in histos[hist]:
                for pred in histos[hist][flav]:
                    histos[hist][flav][pred].Write()
    output_file.Close()
from drawLambda import *
from variables import variable
from selections_SSLep import *
from sampleslist import *

import color as col

gROOT.Macro('functions.C')

import optparse
usage = "usage: %prog [options]"
parser = optparse.OptionParser(usage)
parser.add_option("-b", "--bash", action="store_true", default=False, dest="runBash")
(options, args) = parser.parse_args()
if options.runBash: gROOT.SetBatch(True)

########## SETTINGS ##########
stats=False
if not stats:
    gStyle.SetOptStat(0)
else:
    gStyle.SetOptStat(1111)
LUMI        = 35800. #140000 #35800. # in pb-1
RATIO       = 4 # 0: No ratio plot; !=0: ratio between the top and bottom pads
#NTUPLEDIR   = '/Users/shoh/Projects/CMS/PhD/Analysis/SSL/signal-dev/'
NTUPLEDIR   = '/Users/shoh/Projects/CMS/PhD/Analysis/SSL/dataset-v19-VH/' 

back = ["WJetsToLNu_HT"]
#back = []
signals         = ['Wp', 'Wm'] #, 'WHWWp', 'WHWWm']
def do_entire_analysis(data_config: dict, data_param: dict, data_param_overwrite: dict, # pylint: disable=too-many-locals, too-many-statements, too-many-branches
                       data_model: dict, run_param: dict, clean: bool):

    # Disable any graphical stuff. No TCanvases opened and shown by default
    gROOT.SetBatch(True)

    logger = get_logger()
    logger.info("Do analysis chain")

    # If we are here we are interested in the very first key in the parameters database
    case = list(data_param.keys())[0]

    # Update database accordingly if needed
    update_config(data_param, data_config, data_param_overwrite)

    dodownloadalice = data_config["download"]["alice"]["activate"]
    doconversionmc = data_config["conversion"]["mc"]["activate"]
    doconversiondata = data_config["conversion"]["data"]["activate"]
    domergingmc = data_config["merging"]["mc"]["activate"]
    domergingdata = data_config["merging"]["data"]["activate"]
    doskimmingmc = data_config["skimming"]["mc"]["activate"]
    doskimmingdata = data_config["skimming"]["data"]["activate"]
    domergingperiodsmc = data_config["mergingperiods"]["mc"]["activate"]
    domergingperiodsdata = data_config["mergingperiods"]["data"]["activate"]
    doml = data_config["ml_study"]["activate"]
    docorrelation = data_config["ml_study"]['docorrelation']
    dotraining = data_config["ml_study"]['dotraining']
    dotesting = data_config["ml_study"]['dotesting']
    doapplytodatamc = data_config["ml_study"]['doapplytodatamc']
    docrossvalidation = data_config["ml_study"]['docrossvalidation']
    dolearningcurve = data_config["ml_study"]['dolearningcurve']
    doroc = data_config["ml_study"]['doroc']
    doroctraintest = data_config["ml_study"]['doroctraintest']
    doboundary = data_config["ml_study"]['doboundary']
    doimportance = data_config["ml_study"]['doimportance']
    dogridsearch = data_config["ml_study"]['dogridsearch']
    dobayesianopt = data_config["ml_study"]['dobayesianopt']
    doefficiencyml = data_config["ml_study"]['doefficiency']
    dosignifopt = data_config["ml_study"]['dosignifopt']
    doscancuts = data_config["ml_study"]["doscancuts"]
    doplotdistr = data_config["ml_study"]["doplotdistr"]
    doapplydata = data_config["mlapplication"]["data"]["doapply"]
    doapplymc = data_config["mlapplication"]["mc"]["doapply"]
    domergeapplydata = data_config["mlapplication"]["data"]["domergeapply"]
    domergeapplymc = data_config["mlapplication"]["mc"]["domergeapply"]
    docontinueapplydata = data_config["mlapplication"]["data"]["docontinueafterstop"]
    docontinueapplymc = data_config["mlapplication"]["mc"]["docontinueafterstop"]
    dohistomassmc = data_config["analysis"]["mc"]["histomass"]
    dohistomassdata = data_config["analysis"]["data"]["histomass"]
    doefficiency = data_config["analysis"]["mc"]["efficiency"]
    doresponse = data_config["analysis"]["mc"]["response"]
    dofeeddown = data_config["analysis"]["mc"]["feeddown"]
    dounfolding = data_config["analysis"]["mc"]["dounfolding"]
    dojetsystematics = data_config["analysis"]["data"]["dojetsystematics"]
    dofit = data_config["analysis"]["dofit"]
    doeff = data_config["analysis"]["doeff"]
    docross = data_config["analysis"]["docross"]
    doplotsval = data_config["analysis"]["doplotsval"]
    doplots = data_config["analysis"]["doplots"]
    dosyst = data_config["analysis"]["dosyst"]
    dosystprob = data_config["systematics"]["cutvar"]["activate"]
    do_syst_prob_mass = data_config["systematics"]["cutvar"]["probvariationmass"]
    do_syst_prob_eff = data_config["systematics"]["cutvar"]["probvariationeff"]
    do_syst_prob_fit = data_config["systematics"]["cutvar"]["probvariationfit"]
    do_syst_prob_cross = data_config["systematics"]["cutvar"]["probvariationcross"]
    dosystptshape = data_config["systematics"]["mcptshape"]["activate"]
    doanaperperiod = data_config["analysis"]["doperperiod"]
    typean = data_config["analysis"]["type"]

    dojetstudies = data_config["analysis"]["dojetstudies"]

    dirpklmc = data_param[case]["multi"]["mc"]["pkl"]
    dirpklevtcounter_allmc = data_param[case]["multi"]["mc"]["pkl_evtcounter_all"]
    dirpklskmc = data_param[case]["multi"]["mc"]["pkl_skimmed"]
    dirpklmlmc = data_param[case]["multi"]["mc"]["pkl_skimmed_merge_for_ml"]
    dirpklmltotmc = data_param[case]["multi"]["mc"]["pkl_skimmed_merge_for_ml_all"]
    dirpkldata = data_param[case]["multi"]["data"]["pkl"]
    dirpklevtcounter_alldata = data_param[case]["multi"]["data"]["pkl_evtcounter_all"]
    dirpklskdata = data_param[case]["multi"]["data"]["pkl_skimmed"]
    dirpklmldata = data_param[case]["multi"]["data"]["pkl_skimmed_merge_for_ml"]
    dirpklmltotdata = data_param[case]["multi"]["data"]["pkl_skimmed_merge_for_ml_all"]
    dirpklskdecmc = data_param[case]["mlapplication"]["mc"]["pkl_skimmed_dec"]
    dirpklskdec_mergedmc = data_param[case]["mlapplication"]["mc"]["pkl_skimmed_decmerged"]
    dirpklskdecdata = data_param[case]["mlapplication"]["data"]["pkl_skimmed_dec"]
    dirpklskdec_mergeddata = data_param[case]["mlapplication"]["data"]["pkl_skimmed_decmerged"]

    dirresultsdata = data_param[case]["analysis"][typean]["data"]["results"]
    dirresultsmc = data_param[case]["analysis"][typean]["mc"]["results"]
    dirresultsdatatot = data_param[case]["analysis"][typean]["data"]["resultsallp"]
    dirresultsmctot = data_param[case]["analysis"][typean]["mc"]["resultsallp"]

    binminarray = data_param[case]["ml"]["binmin"]
    binmaxarray = data_param[case]["ml"]["binmax"]
    raahp = data_param[case]["ml"]["opt"]["raahp"]
    mltype = data_param[case]["ml"]["mltype"]
    training_vars = data_param[case]["variables"]["var_training"]

    mlout = data_param[case]["ml"]["mlout"]
    mlplot = data_param[case]["ml"]["mlplot"]

    proc_type = data_param[case]["analysis"][typean]["proc_type"]

    #creating folder if not present
    counter = 0
    if doconversionmc is True:
        counter = counter + checkdirlist(dirpklmc)

    if doconversiondata is True:
        counter = counter + checkdirlist(dirpkldata)

    if doskimmingmc is True:
        checkdirlist(dirpklskmc)
        counter = counter + checkdir(dirpklevtcounter_allmc)

    if doskimmingdata is True:
        counter = counter + checkdirlist(dirpklskdata)
        counter = counter + checkdir(dirpklevtcounter_alldata)

    if domergingmc is True:
        counter = counter + checkdirlist(dirpklmlmc)

    if domergingdata is True:
        counter = counter + checkdirlist(dirpklmldata)

    if domergingperiodsmc is True:
        counter = counter + checkdir(dirpklmltotmc)

    if domergingperiodsdata is True:
        counter = counter + checkdir(dirpklmltotdata)

    if doml is True:
        counter = counter + checkdir(mlout)
        counter = counter + checkdir(mlplot)

    if docontinueapplymc is False:
        if doapplymc is True:
            counter = counter + checkdirlist(dirpklskdecmc)

        if domergeapplymc is True:
            counter = counter + checkdirlist(dirpklskdec_mergedmc)

    if docontinueapplydata is False:
        if doapplydata is True:
            counter = counter + checkdirlist(dirpklskdecdata)

        if domergeapplydata is True:
            counter = counter + checkdirlist(dirpklskdec_mergeddata)

    if dohistomassmc is True:
        counter = counter + checkdirlist(dirresultsmc)
        counter = counter + checkdir(dirresultsmctot)

    if dohistomassdata is True:
        counter = counter + checkdirlist(dirresultsdata)
        counter = counter + checkdir(dirresultsdatatot)

    if counter < 0:
        sys.exit()
    # check and create directories

    if doconversionmc is True:
        checkmakedirlist(dirpklmc)

    if doconversiondata is True:
        checkmakedirlist(dirpkldata)

    if doskimmingmc is True:
        checkmakedirlist(dirpklskmc)
        checkmakedir(dirpklevtcounter_allmc)

    if doskimmingdata is True:
        checkmakedirlist(dirpklskdata)
        checkmakedir(dirpklevtcounter_alldata)

    if domergingmc is True:
        checkmakedirlist(dirpklmlmc)

    if domergingdata is True:
        checkmakedirlist(dirpklmldata)

    if domergingperiodsmc is True:
        checkmakedir(dirpklmltotmc)

    if domergingperiodsdata is True:
        checkmakedir(dirpklmltotdata)

    if doml is True:
        checkmakedir(mlout)
        checkmakedir(mlplot)

    if docontinueapplymc is False:
        if doapplymc is True:
            checkmakedirlist(dirpklskdecmc)

        if domergeapplymc is True:
            checkmakedirlist(dirpklskdec_mergedmc)

    if docontinueapplydata is False:
        if doapplydata is True:
            checkmakedirlist(dirpklskdecdata)

        if domergeapplydata is True:
            checkmakedirlist(dirpklskdec_mergeddata)

    if dohistomassmc is True:
        checkmakedirlist(dirresultsmc)
        checkmakedir(dirresultsmctot)

    if dohistomassdata is True:
        checkmakedirlist(dirresultsdata)
        checkmakedir(dirresultsdatatot)

    proc_class = Processer
    ana_class = Analyzer
    syst_class = Systematics
    if proc_type == "Dhadrons":
        print("Using new feature for Dhadrons")
        proc_class = ProcesserDhadrons
        ana_class = AnalyzerDhadrons
    if proc_type == "Dhadrons_mult":
        print("Using new feature for Dhadrons_mult")
        proc_class = ProcesserDhadrons_mult
        ana_class = AnalyzerDhadrons_mult
    if proc_type == "Dhadrons_jet":
        print("Using new feature for Dhadrons_jet")
        proc_class = ProcesserDhadrons_jet
        ana_class = AnalyzerJet

    mymultiprocessmc = MultiProcesser(case, proc_class, data_param[case], typean, run_param, "mc")
    mymultiprocessdata = MultiProcesser(case, proc_class, data_param[case], typean, run_param,\
                                        "data")
    ana_mgr = AnalyzerManager(ana_class, data_param[case], case, typean, doanaperperiod)
    # Has to be done always period-by-period
    syst_mgr = AnalyzerManager(syst_class, data_param[case], case, typean, True, run_param)

    #perform the analysis flow
    if dodownloadalice == 1:
        subprocess.call("../cplusutilities/Download.sh")

    if doconversionmc == 1:
        mymultiprocessmc.multi_unpack_allperiods()

    if doconversiondata == 1:
        mymultiprocessdata.multi_unpack_allperiods()

    if doskimmingmc == 1:
        mymultiprocessmc.multi_skim_allperiods()

    if doskimmingdata == 1:
        mymultiprocessdata.multi_skim_allperiods()

    if domergingmc == 1:
        mymultiprocessmc.multi_mergeml_allperiods()

    if domergingdata == 1:
        mymultiprocessdata.multi_mergeml_allperiods()

    if domergingperiodsmc == 1:
        mymultiprocessmc.multi_mergeml_allinone()

    if domergingperiodsdata == 1:
        mymultiprocessdata.multi_mergeml_allinone()

    if doml is True:
        index = 0
        for binmin, binmax in zip(binminarray, binmaxarray):
            myopt = Optimiser(data_param[case], case, typean,
                              data_model[mltype], binmin, binmax,
                              raahp[index], training_vars[index])
            if docorrelation is True:
                myopt.do_corr()
            if dotraining is True:
                myopt.do_train()
            if dotesting is True:
                myopt.do_test()
            if doapplytodatamc is True:
                myopt.do_apply()
            if docrossvalidation is True:
                myopt.do_crossval()
            if dolearningcurve is True:
                myopt.do_learningcurve()
            if doroc is True:
                myopt.do_roc()
            if doroctraintest is True:
                myopt.do_roc_train_test()
            if doplotdistr is True:
                myopt.do_plot_model_pred()
            if doimportance is True:
                myopt.do_importance()
            if dogridsearch is True:
                myopt.do_grid()
            if dobayesianopt is True:
                myopt.do_bayesian_opt()
            if doboundary is True:
                myopt.do_boundary()
            if doefficiencyml is True:
                myopt.do_efficiency()
            if dosignifopt is True:
                myopt.do_significance()
            if doscancuts is True:
                myopt.do_scancuts()
            index = index + 1

    if doapplydata is True:
        mymultiprocessdata.multi_apply_allperiods()
    if doapplymc is True:
        mymultiprocessmc.multi_apply_allperiods()
    if domergeapplydata is True:
        mymultiprocessdata.multi_mergeapply_allperiods()
    if domergeapplymc is True:
        mymultiprocessmc.multi_mergeapply_allperiods()
    if dohistomassmc is True:
        mymultiprocessmc.multi_histomass()
    if dohistomassdata is True:
        # After-burner in case of a mult analysis to obtain "correctionsweight.root"
        # for merged-period data
        # pylint: disable=fixme
        # FIXME Can only be run here because result directories are constructed when histomass
        #       is run. If this step was independent, histomass would always complain that the
        #       result directory already exists.
        mymultiprocessdata.multi_histomass()
    if doefficiency is True:
        mymultiprocessmc.multi_efficiency()
    if doresponse is True:
        mymultiprocessmc.multi_response()

    # Collect all desired analysis steps
    analyze_steps = []
    if dofit is True:
        analyze_steps.append("fit")
    if dosyst is True:
        analyze_steps.append("yield_syst")
    if doeff is True:
        analyze_steps.append("efficiency")
    if dojetstudies is True:
        if dofit is False:
            analyze_steps.append("fit")
        if doeff is False:
            analyze_steps.append("efficiency")
        analyze_steps.append("side_band_sub")
    if dofeeddown is True:
        analyze_steps.append("feeddown")
    if dounfolding is True:
        analyze_steps.append("unfolding")
        analyze_steps.append("unfolding_closure")
    if dojetsystematics is True:
        analyze_steps.append("jetsystematics")
    if docross is True:
        analyze_steps.append("makenormyields")
    if doplots is True:
        analyze_steps.append("plotternormyields")
    if doplotsval is True:
        analyze_steps.append("plottervalidation")

    # Now do the analysis
    ana_mgr.analyze(*analyze_steps)

    ml_syst_steps = []
    if dosystprob is True:
        if do_syst_prob_mass:
            ml_syst_steps.append("ml_cutvar_mass")
        if do_syst_prob_eff:
            ml_syst_steps.append("ml_cutvar_eff")
        if do_syst_prob_fit:
            ml_syst_steps.append("ml_cutvar_fit")
        if do_syst_prob_cross:
            ml_syst_steps.append("ml_cutvar_cross")
    if dosystptshape is True:
        ml_syst_steps.append("mcptshape")
    syst_mgr.analyze(*ml_syst_steps)

    # Delete per-period results.
    if clean:
        print("Cleaning")
        if doanaperperiod:
            print("Per-period analysis enabled. Skipping.")
        else:
            if not delete_dirlist(dirresultsmc + dirresultsdata):
                print("Error: Failed to complete cleaning.")

    print("Done")
示例#10
0
#!/usr/bin/env python

import math, os, sys
from DisappTrks.BackgroundEstimation.bkgdEstimate import LeptonBkgdEstimate
from DisappTrks.BackgroundEstimation.fakeEstimateTest import FakeTrackBkgdEstimate
from DisappTrks.StandardAnalysis.plotUtilities import *
from DisappTrks.StandardAnalysis.IntegratedLuminosity_cff import *
from ROOT import gROOT, TCanvas, TFile, TGraphErrors

gROOT.SetBatch () # I am Groot.

dirs = getUser()
canvas = TCanvas("c1", "c1",800,800)
setCanvasStyle(canvas)

background = "all"
if len (sys.argv) > 1:
    background = sys.argv[1]
background = background.upper ()

# '' will gives you Dataset_2016.root for the whole year
#runPeriods = ['B', 'C', 'D', 'E', 'F', 'G', 'H']
runPeriods = ['BC', 'DEFGH', '']

nEstFake = []
nEstElectron = []
nEstMuon = []
nEstTau = []

nEstFakeVsNHits = {}
nEstFakeVsNHitsZtoMuMu = {}
示例#11
0
def main():
    gROOT.SetBatch(True)

    suffix = argv[1]

    histos = ["BB", "BE"]
    labels = [
        "dimuon_2016", "dielectron_2016", "dimuon_2017", "dielectron_2017",
        "dimuon_2018", "dielectron_2018"
    ]
    #~ channels = ["cito2mu","cito2e"]
    suffixesMu = ["nominal", "scaledown", "smeared", "muonid"]
    suffixesEle = ["nominal", "scaledown", "scaleup", "pileup", "piledown"]
    css = ["inc", "cspos", "csneg"]
    #~ suffixes = ["smeared"]
    lambdas = [10, 16, 22, 28, 34, 40, 46]
    interferences = ["Con", "Des"]
    # ~ hels = ["LL","LR","RR"]
    # ~ interferences = ["Con"]
    hels = ["LL", "LR", "RL", "RR"]

    #~ massBins = [1200,1400,1600,1800,2000,2200,2400,2600,2800,3000,3200,3400]
    massBins = [
        1200, 1400, 1600, 1800, 2000, 2200, 2400, 2600, 2800, 3000, 3200, 3400
    ]
    signalYields = {}

    #~ names = ["default","resolution","scale","ID"]

    for label in labels:
        print(label)
        # ~ if "dimuon" in label:
        # ~ suffixes = suffixesMu
        # ~ else:
        # ~ suffixes = suffixesEle
        # ~ for suffix in suffixes:
        if "dimuon" in label and not suffix in suffixesMu:
            continue
        if "dielectron" in label and not suffix in suffixesEle:
            continue
        for cs in css:
            for histo in histos:
                for hel in hels:
                    for interference in interferences:
                        model = interference + hel
                        if "dimuon" in label:
                            name = "CIto2mu"
                        else:
                            name = "CIto2e"
                        print name
                        if "2016" in label:
                            fitFile = TFile(
                                "%s_%s_%s_%s_parametrization_fixdes_fixinf_limitp0_limitp1_limitp2_2016.root"
                                % (name, suffix, histo.lower(), cs), "READ")
                        elif "2018" in label:
                            fitFile = TFile(
                                "%s_%s_%s_%s_parametrization_fixdes_fixinf_limitp0_limitp1_limitp2_2018.root"
                                % (name, suffix, histo.lower(), cs), "READ")
                        else:
                            fitFile = TFile(
                                "%s_%s_%s_%s_parametrization_fixdes_fixinf_limitp0_limitp1_limitp2.root"
                                % (name, suffix, histo.lower(), cs), "READ")
                        for l in lambdas:
                            if "dimuon" in label:
                                name = "CITo2Mu_Lam%dTeV%s" % (l, model)
                            else:
                                name = "CITo2E_Lam%dTeV%s" % (l, model)
                            if not cs == "inc":
                                name += "_%s" % cs
                            signalYields["%s_%s_%s" %
                                         (name, label, histo)] = {}
                            for index, massBin in enumerate(massBins):
                                function = fitFile.Get("fn_m%d_%s" %
                                                       (massBin, model))
                                fitR = fitFile.Get("fitR_m%d_%s" %
                                                   (massBin, model))
                                pars = fitR.GetParams()
                                errs = fitR.Errors()
                                function.SetParameter(0, pars[0])
                                function.SetParameter(1, pars[1])
                                function.SetParameter(2, pars[2])
                                function.SetParError(0, errs[0])
                                function.SetParError(1, errs[1])
                                function.SetParError(2, errs[2])
                                functionUnc = fitFile.Get("fn_unc_m%d_%s" %
                                                          (massBin, model))
                                uncert = ((functionUnc.Eval(l) /
                                           function.Eval(l))**2 +
                                          (functionUnc.Eval(100000) /
                                           function.Eval(100000)))**0.5
                                signalYields["%s_%s_%s" %
                                             (name, label,
                                              histo)][str(massBin)] = [
                                                  (function.Eval(l) -
                                                   function.Eval(100000)),
                                                  uncert
                                              ]
                    if "dimuon" in label:
                        name = "CITo2Mu_Lam%dTeV%s" % (l, hel)
                        nameCon = "CITo2Mu_Lam%dTeV%s" % (l, "Con" + hel)
                        nameDes = "CITo2Mu_Lam%dTeV%s" % (l, "Des" + hel)
                    else:
                        name = "CITo2E_Lam%dTeV%s" % (l, hel)
                        nameCon = "CITo2E_Lam%dTeV%s" % (l, "Con" + hel)
                        nameDes = "CITo2E_Lam%dTeV%s" % (l, "Des" + hel)
                    signalYields["%s_%s_%s" % (name, label, histo)] = {}
                    for index, massBin in enumerate(massBins):
                        signalYields["%s_%s_%s" % (name, label, histo)][str(
                            massBin)] = [
                                (signalYields["%s_%s_%s" %
                                              (nameCon, label,
                                               histo)][str(massBin)][0] +
                                 signalYields["%s_%s_%s" %
                                              (nameDes, label,
                                               histo)][str(massBin)][0]) / 2,
                                ((signalYields["%s_%s_%s" %
                                               (nameCon, label,
                                                histo)][str(massBin)][1]**2 +
                                  signalYields["%s_%s_%s" %
                                               (nameDes, label, histo)][str(
                                                   massBin)][1]**2))**0.5
                            ]

        if "dimuon" in label:
            fileName = "CIsignalYieldsSingleBin"
        else:
            fileName = "CIsignalYieldsSingleBinEle"

        if suffix == "nominal":
            otherSuffix = "default"
        elif suffix == "scaledown":
            otherSuffix = "scaleDown"
        elif suffix == "scaleup":
            otherSuffix = "scaleUp"
        elif suffix == "smeared":
            otherSuffix = "resolution"
        elif suffix == "muonid":
            otherSuffix = "ID"
        elif suffix == "pileup":
            otherSuffix = "pileup"
        elif suffix == "piledown":
            otherSuffix = "piledown"
        else:
            print(suffix)
        outFilePkl = open("%s_%s.pkl" % (fileName, otherSuffix), "wb")
        pickle.dump(signalYields, outFilePkl, protocol=2)
        outFilePkl.close()
示例#12
0
	def makeMultiPlot(self):
		from ROOT import gROOT, gStyle, TFile, TTree, gDirectory
		from ROOT import TGraphErrors, TCanvas, TLegend, TH2D, TLatex, TPave
		from operator import itemgetter

		rangey=(self.args.miny, self.args.maxy)
		rangex=(self.args.minx, self.args.maxx)

		## Build caselist
		caselist = []
		for filename in self.filelist:
			caselist.append(path.dirname(filename).split('/')[-1])

		gROOT.SetBatch()
		gStyle.SetOptStat(0)

		oname = self.args.outputName
		if oname.endswith('.pdf') or oname.endswith('.png'): oname = oname[:-4]
		if len(oname)==0: ## default
			oname = 'plot_' + reduce(lambda x,y:x+'_'+y, caselist)

		canv = TCanvas(oname, "Plot", 0, 0, 1024, 768)
		canv.cd()
		if not self.args.nologx: canv.SetLogx()
		if self.args.logy: canv.SetLogy()
		canv.SetGridx()
		canv.SetGridy()
		## Cosmetics
		# canv.DrawFrame(rangex[0], rangey[0], rangex[1], rangey[1])
		axes = TH2D('axes', 'A', 100, rangex[0], rangex[1], 100, rangey[0], rangey[1])
		title = args.title if len(args.title) else 'Throughput vs. Fragment Size'
		titleX = args.titleX if len(args.titleX) else 'Fragment Size (bytes)'
		titleY = args.titleY if len(args.titleY) else 'Av. Throughput per RU (MB/s)'
		axes.GetYaxis().SetTitle(titleY)
		axes.GetYaxis().SetTitleOffset(1.4)
		axes.GetXaxis().SetTitleOffset(1.2)
		axes.SetTitle(title)
		axes.GetXaxis().SetTitle(titleX)
		axes.GetXaxis().SetMoreLogLabels()
		axes.GetXaxis().SetNoExponent()
		axes.Draw()

		tl = TLatex()
		tl.SetTextFont(42)
		tl.SetNDC(1)
		if len(self.args.tag) > 0:
			width = 0.12+0.020*len(self.args.tag)
			if width > 0.9: width=0.899
			pave = TPave(0.12, 0.80, width, 0.899, 0, 'NDC')
			pave.SetFillStyle(1001)
			pave.SetFillColor(0)
			pave.Draw()
		if len(self.args.subtag) > 0:
			width2 = 0.12+0.015*len(self.args.subtag)
			if width2 > 0.9: width2=0.899
			pave2 = TPave(0.12, 0.75, width2, 0.899, 0, 'NDC')
			pave2.SetFillStyle(1001)
			pave2.SetFillColor(0)
			pave2.Draw()
		if len(self.args.tag) > 0:
			tl.SetTextSize(0.05)
			tl.DrawLatex(0.14, 0.83, self.args.tag)
		if len(self.args.subtag) > 0:
			tl.SetTextSize(0.035)
			tl.DrawLatex(0.145, 0.77, self.args.subtag)

		graphs = []
		configs = set()
		for filename,case in zip(self.filelist,caselist):
			try:
				graphs.append(self.getGraph(filename))

				config, builder, protocol, rms = extractConfig(filename)
				nstreams, nrus, nbus, strperfrl = getConfig(config)

				if strperfrl == 0:
					nrus = 1 ## ignore number of RUs if MSIO
					configs.add(nrus)
				else:
					configs.add(nstreams//nrus) ## care only about Nstreams per RU
			except AttributeError:
				print "#### Couldn't get graph for ", case, "in file", filename
				return

		if not self.args.hideDate:
			datepave = drawDate(self.filelist[0])
			datepave.Draw()

		# if self.args.daq1:
		# 	daq1_graph = getDAQ1Graph()

		configs = sorted(configs)
		nlegentries = len(self.filelist)
		# nlegentries = len(caselist) if not self.args.daq1 else len(caselist) + 1
		legendpos = (0.44, 0.13, 0.899, 0.20+nlegentries*0.05)
		maxentrylength = 0.04
		if self.args.legend:
			maxentrylength = max(len(x) for x in self.args.legends)
			legxlength = min(max(maxentrylength*0.013, 0.18),0.6)
		if strperfrl == 0:
			legendpos = (0.13, 0.73, 0.13+legxlength, 0.73-nlegentries*0.045)
		# if self.args.legendPos == 'TL':
		# 	legendpos = (0.12, 0.82-nlegentries*0.05, 0.579, 0.898)
		# 	# legendpos = (0.12, 0.71-nlegentries*0.05, 0.579, 0.78)
		leg = TLegend(legendpos[0], legendpos[1], legendpos[2], legendpos[3])
		leg.SetFillStyle(1001)
		leg.SetFillColor(0)
		leg.SetTextFont(42)
		leg.SetTextSize(0.033)
		leg.SetBorderSize(0)

		colors  = [1,2,3,4,51,95,65,39,32]
		markers = [20,21,22,23,34,33,29,24,25,26]

		if (len(self.args.legends) > 0 and
			len(self.args.legends) != len(self.filelist)):
			print "Legends doesn't match with filelist, falling back to default"

		for n,graph in enumerate(graphs):
			graph.SetLineColor(colors[n])
			graph.SetMarkerColor(colors[n])
			graph.SetMarkerStyle(markers[n])

			 ## Custom legends
			if (len(self.args.legends) == len(self.filelist) and
			    len(self.args.legends)>0):
				leg.AddEntry(graph, self.args.legends[n], 'P')
			else: ## Default
				leg.AddEntry(graph, caselist[n], 'P')
			# if self.args.daq1:
			# 	leg.AddEntry(daq1_graph, 'DAQ1 (2011)', 'P')
			graph.Draw("PL")

		# if self.args.daq1:
		# 	daq1_graph.Draw("PL")

		if not self.args.noRateLine:
			for n,streams_per_ru in enumerate(configs):
				func = getRateGraph(streams_per_ru, frag=True,
					                rate=self.args.rate, xmax=rangex[1])
				func.SetLineColor(colors[n])
				func.SetLineWidth(1)
				func.DrawCopy("same")
				if not strperfrl == 0:
					leg.AddEntry(func, '%.0f kHz (%d streams)'%
						                 (self.args.rate, streams_per_ru), 'l')
				else:
					leg.AddEntry(func, '%.0f kHz'% (self.args.rate), 'l')

		leg.Draw()

		for graph in graphs:
			graph.Draw("PL")

		canv.Print(oname + '.pdf')
		canv.Print(oname + '.png')
示例#13
0
    cls = method.im_class
    return _unpickle_method, (func_name, obj, cls)

def _unpickle_method(func_name, obj, cls):
    for cls in cls.mro():
        try:
            func = cls.__dict__[func_name]
        except KeyError:
            pass
        else:
            break
    return func.__get__(obj, cls)

pickle(MethodType, _pickle_method, _unpickle_method)

gr.SetBatch(True) # NEEDS TO BE SET FOR MULTIPROCESSING OF plot.Draw()
Cut = namedtuple('Cut', ['name', 'cut'])

# int_lumi = 41000.0 # pb #### FIXME 
int_lumi = 41000.0 * (30564478/19122658) # [pb]; adapt to the amount of events done for the nonprompt analysis
#int_lumi = 80000.0 # pb #### FIXME 


## RICCARDO
# cuts.append(Cut('ttjetsloose', 'nbj>1'))
# cuts.append(Cut('zmmloose' , 'l1_pt>5  & l2_pt>5  & l1_q!=l2_q & l1_id_t & l2_id_t & l1_reliso05<0.2 & l2_reliso05<0.2 & abs(l1_dz)<0.2 & abs(l2_dz)<0.2 & abs(l1_dxy)<0.045 & abs(l2_dxy)<0.045 & nbj==0 & pass_e_veto & pass_m_veto'))
#     cuts.append(Cut('zmmhighpt', 'l1_pt>15  & l2_pt>15  & l1_q!=l2_q & l1_id_t & l2_id_t & l1_reliso05<0.2 & l2_reliso05<0.2 & abs(l1_dz)<0.2 & abs(l2_dz)<0.2 & abs(l1_dxy)<0.045 & abs(l2_dxy)<0.045 & nbj==0 & pass_e_veto & pass_m_veto'))
# cuts.append(Cut('zmm'      , 'l1_pt>10 & l2_pt>10 & l1_q!=l2_q & !l0_eid_mva_iso_loose & l0_reliso05>0.15 & l1_id_t & l2_id_t & l1_reliso05<0.2 & l2_reliso05<0.2 & abs(l1_dz)<0.2 & abs(l2_dz)<0.2 & abs(l1_dxy)<0.045 & abs(l2_dxy)<0.045 & nbj==0 & pass_e_veto & pass_m_veto'))

# cuts.append(Cut('inclusive'    , 'l0_pt>30 & l1_pt>4 & l2_pt>4 & l1_q != l2_q & l0_eid_mva_iso_loose & l0_reliso05<0.15'))
# cuts.append(Cut('inclusive'    , 'l0_pt>30 & l1_pt>4 & l2_pt>4 & l1_q != l2_q & l0_eid_mva_iso_loose & l0_reliso05<0.15 & l1_id_m & l2_id_m & l1_reliso05<0.2 & l2_reliso05<0.2'))
示例#14
0
def main(yamlFile):

    print("")
    gStyle.SetOptStat(0)

    globalCfg = readYamlFile(yamlFile)
    #  print(globalCfg)

    for cfgIterator in globalCfg:
        cfg = globalCfg[cfgIterator]
        print('\n[INFO]\tProcess plot: %s' % (cfgIterator))

        if ("silentMode" in cfg):
            gROOT.SetBatch(cfg['silentMode'])
            cfg.pop('silentMode')

        #********************************************************************************
        # create objects to draw
        #********************************************************************************
        classHelper = helperClass()
        hists, funcs = classHelper.getProcessedHists(cfg)
        leg = classHelper.getLegend(hists, cfg)
        canvas = classHelper.getCanvas(cfg)
        labels = classHelper.getTextLabels(hists, cfg)
        labels += classHelper.getLatexLabels(hists, cfg)
        funcs += classHelper.getTF1s(cfg)

        print("# of labels to be drawn: %d" % (len(labels)))

        #********************************************************************************
        # Draw Everything
        #********************************************************************************
        if ("drawOption" not in cfg):
            cfg['drawOption'] = ""
        if (("sameDrawOption" not in cfg) and ("drawOption" in cfg)):
            cfg['sameDrawOption'] = cfg['drawOption']

        hists[0].Draw(cfg['drawOption'])
        for i in range(1, len(hists)):
            hists[i].Draw(cfg['sameDrawOption'] + "same")
        cfg.pop('drawOption')
        cfg.pop('sameDrawOption')

        if leg:
            leg.Draw("same")

        for iLabel in labels:
            iLabel.Draw("same")

        for iFunc in funcs:
            iFunc.Draw("same")

        #********************************************************************************
        # Save plots
        #********************************************************************************
        print("")
        if not os.path.exists("pictures/"):
            os.makedirs("pictures/")
        outHistName = cfgIterator
        if ("histNamePrefix" in cfg):
            outHistName = cfg['histNamePrefix'] + outHistName
            cfg.pop('histNamePrefix')
        if ("savePictDir" in cfg):
            pythonFileDir = os.path.dirname(os.path.realpath(__file__))
            outHistName = cfg['savePictDir'] + '/' + outHistName
            if not os.path.exists("pictures/" + cfg['savePictDir']):
                os.makedirs("pictures/" + cfg['savePictDir'])
            cfg.pop('savePictDir')
        canvas.SaveAs("pictures/" + outHistName + ".png")
        canvas.SaveAs("pictures/" + outHistName + ".pdf")
        #  canvas.SaveAs("pictures/"+outHistName+".eps")
        #  canvas.SaveAs("pictures/"+outHistName+".C")

        #********************************************************************************
        # Check for typos in yaml files
        #********************************************************************************
        if (len(cfg) != 0):
            print("\nNot used attributes:")
            print(cfg)
            print("")
            raise notUsedAttribute(
                "there are not used attributes in the yml file. See above!")
示例#15
0
                    default='.',
                    type=str,
                    help='directory to save the plot')
parser.add_argument('--cut-height',
                    dest='hcut',
                    default=0,
                    type=float,
                    help='cut on the minimum peak height')
args = parser.parse_args()

if args.stype not in signal_types:
    print('signal type must be in {}'.format(list(signal_types)))
    exit(-1)

# batch mode
gROOT.SetBatch(args.batch)

# recover paths
for attr in ['root_file', 'json_file', 'snap', 'out_dir']:
    setattr(args, attr, os.path.join(owd, getattr(args, attr)))

os.makedirs(args.out_dir, exist_ok=True)

# root dataframe
rdf = ROOT.RDataFrame('EvTree', args.root_file)

# read json
json_data = dict()
if os.path.exists(args.json_file):
    with open(args.json_file, 'r') as f:
        json_data.update(json.load(f))
#!/usr/bin/env python

import math, os, sys
from DisappTrks.BackgroundEstimation.bkgdEstimate import LeptonBkgdEstimate
from DisappTrks.BackgroundEstimation.fakeEstimateTest import FakeTrackBkgdEstimate
from DisappTrks.StandardAnalysis.plotUtilities import *
from DisappTrks.StandardAnalysis.IntegratedLuminosity_cff import *
from ROOT import gROOT, TCanvas, TFile, TGraphErrors

gROOT.SetBatch()  # I too am Groot.

dirs = getUser()
canvas = TCanvas("c1", "c1", 800, 800)
setCanvasStyle(canvas)

background = "all"
if len(sys.argv) > 1:
    background = sys.argv[1]
background = background.upper()

runPeriods = ['D']

nEstFake = []
nEstElectron = []
nEstMuon = []
nEstTau = []

nEstFakeVsNHits = {}
nEstFakeVsNHitsZtoMuMu = {}

stdout = sys.stdout
示例#17
0
def main():
    #gStyle.SetOptStat(0)
    gROOT.SetBatch(True)
    gStyle.SetPalette(kTemperatureMap)

    fIn=TFile.Open(FLAGS.noPUFile)
    data=fIn.Get('data')

    #Calibrate everything!
    calibration = Calibration(FLAGS.mingenen, FLAGS.mingeneta, FLAGS.maxgeneta,
                              FLAGS.plotLabel, FLAGS.samples, FLAGS.mask, FLAGS.outpath)
    calibration.L0L1Calibration(FLAGS.noPUFile)

    with open('calib_'+FLAGS.samples+"_"+str(FLAGS.mask)+'_nopu.pck','w') as cachefile:
        pickle.dump(calibration.calib, cachefile, pickle.HIGHEST_PROTOCOL)

    histos=OrderedDict()
    etabins = 12
    etacuts = FLAGS.etacuts

    hn = ['res_complete_before{}',          'res_complete_after{}', 
          'res_incomplete_before_left{}',   'res_incomplete_after_left{}',
          'res_incomplete_before_right{}',  'res_incomplete_after_right{}',
          'en{}_per_layer_signal',          'en{}_per_layer_bckg1',
          'en{}_per_layer_bckg2',           'en{}_per_layer_bckg3',
          'noise{}_per_layer_signal',       'noise{}_per_layer_bckg1',
          'noise{}_per_layer_bckg2',        'noise{}_per_layer_bckg3']
    for ireg in range(1,NREG+1):
        bins = Carray('d', np.arange(-1.05, .8, 0.01))
        strings = ';#Delta E/E_{gen};PDF'
        for ih in range(6):
            histos[hn[ih].format(ireg)] = TH1F(hn[ih].format(ireg), strings,
                                           len(bins)-1, bins)
        bins = Carray('d', np.arange(0.5,29,1.))
        strings = ';Layer;E_{reco} / E_{gen}'
        for ih in range(6,14):
            histos[hn[ih].format(ireg)] = TProfile(hn[ih].format(ireg), strings,
                                                   len(bins)-1, bins)

    for h in histos:
        histos[h].Sumw2()
        histos[h].SetMarkerStyle(20)
        histos[h].SetDirectory(0)


    for i in range(0,data.GetEntriesFast()):
        data.GetEntry(i)
        genen    = getattr(data,'genen')        
        geneta   = abs(getattr(data,'geneta'))
        genphi   = getattr(data,'genphi')

        sc = (FLAGS.maxgeneta, FLAGS.mingeneta)
        singlecut = ( geneta < sc[0] if FLAGS.samples == 'inner' 
                      else geneta > sc[1] )
        insidecut = ( geneta < FLAGS.etacuts[-2] if FLAGS.samples == 'inner' 
                      else geneta > FLAGS.etacuts[1] )

        for ireg in range(1,NREG+1):
            recen = getattr(data,'en_sr{}_ROI'.format(ireg))
            avgnoise = getattr(data,'noise_sr3_ROI')*A[ireg-1]/A[2]

            #Calibration factors. f2 is used for PU.
            f1, f2 = 1., 0.
            if 'L0' in calib:
                f1 /= (calib['L0'][ireg].Eval(geneta)+1.0)
                if 'L1' in calib:
                    f1 /= (calib['L1'][ireg].Eval(f1*recen)+1.0)
                    if 'L2' in calib and ireg in calib['L2']:
                        f2 = calib['L2'][ireg].Eval(avgnoise)
            recen = f1*recen - f2 
            recen_shift = recen / .49
            deltaE = recen/genen-1.
            deltaE_shift = recen_shift/genen-1.

            #differentiate complete from incomplete showers
            bool_sig = ( (FLAGS.samples == 'inner' and geneta < FLAGS.maxgeneta) or 
                         (FLAGS.samples == 'outer' and geneta >= FLAGS.mingeneta) )
            bools_bckg = []
            for ic in range(len(etacuts)-1):
                bools_bckg.append((FLAGS.samples=='inner' 
                                   and geneta >= etacuts[ic] 
                                   and geneta < etacuts[ic+1])
                                  or (FLAGS.samples == 'outer' 
                                      and geneta < etacuts[-(ic+1)] 
                                      and geneta >= etacuts[-(ic+2)]) )
            check_bools = int(bool_sig)
            for ic in range(len(etacuts)-1):
                check_bools += int(bools_bckg[ic])
            assert check_bools == 1

            ###Calculate and calibrate the energy per layer###
            recen_corr = 0 
            for il in range(1,NLAYERS+1):
                #if: 'signal-like': complete showers
                #elif: 'background-like': incomplete showers
                if FLAGS.apply_weights:
                    if FLAGS.samples == 'inner':
                        weight_limit = il > boundaries[ireg-1] 
                    else:
                        weight_limit = il < boundaries[ireg-1] 

                b = histos[hn[6].format(ireg)].FindBin(il)
                if bool_sig:
                    v = f1*getattr(data,'en_sr{}_layer{}'.format(ireg,il)) - f2
                    histos[hn[6].format(ireg)].Fill(b,v/recen)
                    if FLAGS.apply_weights:
                        recen_corr += v
                    v = (f1*getattr(data,'noise_sr3_layer{}'.format(il))
                         *A[ireg-1]/A[2] - f2)
                    histos[hn[10].format(ireg)].Fill(b,v/recen)
                else:
                    lshift = [.9, .85, .7] #layer shift
                    for w in range(len(etacuts)-1):
                        if bools_bckg[w]:      
                            v = f1*getattr(data,'en_sr{}_layer{}'.format(ireg,il)) - f2
                            histos[hn[7+w].format(ireg)].Fill(b*lshift[w],v/recen)
                            if ( FLAGS.apply_weights and 
                                 weights[ireg-1][w][il-1]!=0 and
                                 weight_limit):
                                recen_corr += v/weights[ireg-1][w][int(round((il-1)*lshift[w],0))]
                                #weight_graphs[ireg][il].SetBit(weight_graphs[ireg][il].klsSortedX)
                                #weight_graphs[ireg][il].Eval(geneta, spline=0, 'S')
                            v = (f1*getattr(data,'noise_sr3_layer{}'.format(il))
                                 *A[ireg-1]/A[2] - f2)
                            histos[hn[11+w].format(ireg)].Fill(b,v/recen)
                
    #end of tree loop    
    fIn.Close()

    else:
示例#18
0
def setDefault(paintformat="4.2f"):
    gROOT.SetBatch(True)
    gStyle.SetOptStat(0)
    tdr.setTDRStyle()
    gStyle.SetPaintTextFormat(paintformat)
    gROOT.ProcessLine("gErrorIgnoreLevel = 1001;")
示例#19
0
    def do_significance(self):
        self.logger.info("Doing significance optimization")
        gROOT.SetBatch(True)
        gROOT.ProcessLine("gErrorIgnoreLevel = kWarning;")
        #first extract the number of data events in the ml sample
        self.df_evt_data = pickle.load(openfile(self.f_evt_data, 'rb'))
        if self.p_dofullevtmerge is True:
            self.df_evttotsample_data = pickle.load(
                openfile(self.f_evttotsample_data, 'rb'))
        else:
            self.logger.warning(
                "The total merged event dataframe was not merged for space limits"
            )
            self.df_evttotsample_data = pickle.load(
                openfile(self.f_evt_data, 'rb'))
        #and the total number of events
        self.p_nevttot = len(self.df_evttotsample_data)
        self.p_nevtml = len(self.df_evt_data)
        self.logger.debug("Number of data events used for ML: %d",
                          self.p_nevtml)
        self.logger.debug("Total number of data events: %d", self.p_nevttot)
        #calculate acceptance correction. we use in this case all
        #the signal from the mc sample, without limiting to the n. signal
        #events used for training
        denacc = len(self.df_mcgen[self.df_mcgen["ismcprompt"] == 1])
        numacc = len(self.df_mc[self.df_mc["ismcprompt"] == 1])
        acc, acc_err = calc_eff(numacc, denacc)
        self.logger.debug("Acceptance: %.3e +/- %.3e", acc, acc_err)
        #calculation of the expected fonll signals
        ptmin = self.p_binmin
        ptmax = self.p_binmax
        delta_pt = ptmax - ptmin
        if self.is_fonll_from_root:
            df_fonll = TFile.Open(self.f_fonll)
            df_fonll_Lc = df_fonll.Get(self.p_fonllparticle + "pred_" +
                                       self.p_fonllband)
            prod_cross = df_fonll_Lc.Integral(
                ptmin * 20, ptmax * 20) * self.p_fragf * 1e-12 / delta_pt
            signal_yield = 2. * prod_cross * delta_pt * acc * self.p_taa \
                           / (self.p_sigmamb * self.p_fprompt)
            #now we plot the fonll expectation
            cFONLL = TCanvas('cFONLL', 'The FONLL expectation')
            df_fonll_Lc.GetXaxis().SetRangeUser(0, 16)
            df_fonll_Lc.Draw("")
            cFONLL.SaveAs("%s/FONLL_curve_%s.png" %
                          (self.dirmlplot, self.s_suffix))
        else:
            df_fonll = pd.read_csv(self.f_fonll)
            df_fonll_in_pt = df_fonll.query(
                '(pt >= @ptmin) and (pt < @ptmax)')[self.p_fonllband]
            prod_cross = df_fonll_in_pt.sum() * self.p_fragf * 1e-12 / delta_pt
            signal_yield = 2. * prod_cross * delta_pt * self.p_br * acc * self.p_taa \
                           / (self.p_sigmamb * self.p_fprompt)
            #now we plot the fonll expectation
            plt.figure(figsize=(20, 15))
            plt.subplot(111)
            plt.plot(df_fonll['pt'],
                     df_fonll[self.p_fonllband] * self.p_fragf,
                     linewidth=4.0)
            plt.xlabel('P_t [GeV/c]', fontsize=20)
            plt.ylabel('Cross Section [pb/GeV]', fontsize=20)
            plt.title("FONLL cross section " + self.p_case, fontsize=20)
            plt.semilogy()
            plt.savefig(f'{self.dirmlplot}/FONLL_curve_{self.s_suffix}.png')

        self.logger.debug("Expected signal yield: %.3e", signal_yield)
        signal_yield = self.p_raahp * signal_yield
        self.logger.debug("Expected signal yield x RAA hp: %.3e", signal_yield)

        df_data_sideband = self.df_data.query(self.s_selbkgml)
        df_data_sideband = shuffle(df_data_sideband,
                                   random_state=self.rnd_shuffle)
        df_data_sideband = df_data_sideband.tail(
            round(len(df_data_sideband) * self.p_bkgfracopt))
        hmass = TH1F('hmass', '', self.p_num_bins, self.p_mass_fit_lim[0],
                     self.p_mass_fit_lim[1])
        df_mc_signal = self.df_mc[self.df_mc["ismcsignal"] == 1]
        mass_array = df_mc_signal['inv_mass'].values
        for mass_value in np.nditer(mass_array):
            hmass.Fill(mass_value)

        gaus_fit = TF1("gaus_fit", "gaus", self.p_mass_fit_lim[0],
                       self.p_mass_fit_lim[1])
        gaus_fit.SetParameters(0, hmass.Integral())
        gaus_fit.SetParameters(1, self.p_mass)
        gaus_fit.SetParameters(2, 0.02)
        self.logger.debug("To fit the signal a gaussian function is used")
        fitsucc = hmass.Fit("gaus_fit", "RQ")

        if int(fitsucc) != 0:
            self.logger.warning("Problem in signal peak fit")
            sigma = 0.

        sigma = gaus_fit.GetParameter(2)
        self.logger.debug("Mean of the gaussian: %.3e",
                          gaus_fit.GetParameter(1))
        self.logger.debug("Sigma of the gaussian: %.3e", sigma)
        sig_region = [self.p_mass - 3 * sigma, self.p_mass + 3 * sigma]
        fig_signif_pevt = plt.figure(figsize=(20, 15))
        plt.xlabel('Threshold', fontsize=20)
        plt.ylabel(r'Significance Per Event ($3 \sigma$)', fontsize=20)
        plt.title("Significance Per Event vs Threshold", fontsize=20)
        plt.xticks(fontsize=18)
        plt.yticks(fontsize=18)
        fig_signif = plt.figure(figsize=(20, 15))
        plt.xlabel('Threshold', fontsize=20)
        plt.ylabel(r'Significance ($3 \sigma$)', fontsize=20)
        plt.title("Significance vs Threshold", fontsize=20)
        plt.xticks(fontsize=18)
        plt.yticks(fontsize=18)

        df_sig = self.df_mltest[self.df_mltest["ismcprompt"] == 1]

        for name in self.p_classname:
            eff_array, eff_err_array, x_axis = calc_sigeff_steps(
                self.p_nstepsign, df_sig, name)
            bkg_array, bkg_err_array, _ = calc_bkg(
                df_data_sideband, name, self.p_nstepsign, self.p_mass_fit_lim,
                self.p_bkg_func, self.p_bin_width, sig_region, self.p_savefit,
                self.dirmlplot, [self.p_binmin, self.p_binmax])
            sig_array = [eff * signal_yield for eff in eff_array]
            sig_err_array = [
                eff_err * signal_yield for eff_err in eff_err_array
            ]
            bkg_array = [
                bkg / (self.p_bkgfracopt * self.p_nevtml) for bkg in bkg_array
            ]
            bkg_err_array = [bkg_err / (self.p_bkgfracopt * self.p_nevtml) \
                             for bkg_err in bkg_err_array]
            signif_array, signif_err_array = calc_signif(
                sig_array, sig_err_array, bkg_array, bkg_err_array)
            plt.figure(fig_signif_pevt.number)
            plt.errorbar(x_axis,
                         signif_array,
                         yerr=signif_err_array,
                         label=f'{name}',
                         elinewidth=2.5,
                         linewidth=5.0)
            signif_array_ml = [
                sig * sqrt(self.p_nevtml) for sig in signif_array
            ]
            signif_err_array_ml = [
                sig_err * sqrt(self.p_nevtml) for sig_err in signif_err_array
            ]
            plt.figure(fig_signif.number)
            plt.errorbar(x_axis,
                         signif_array_ml,
                         yerr=signif_err_array_ml,
                         label=f'{name}_ML_dataset',
                         elinewidth=2.5,
                         linewidth=5.0)
            signif_array_tot = [
                sig * sqrt(self.p_nevttot) for sig in signif_array
            ]
            signif_err_array_tot = [
                sig_err * sqrt(self.p_nevttot) for sig_err in signif_err_array
            ]
            plt.figure(fig_signif.number)
            plt.errorbar(x_axis,
                         signif_array_tot,
                         yerr=signif_err_array_tot,
                         label=f'{name}_Tot',
                         elinewidth=2.5,
                         linewidth=5.0)
            plt.figure(fig_signif_pevt.number)
            plt.legend(loc="upper left", prop={'size': 30})
            plt.savefig(
                f'{self.dirmlplot}/Significance_PerEvent_{self.s_suffix}.png')
            plt.figure(fig_signif.number)
            plt.legend(loc="upper left", prop={'size': 30})
            plt.savefig(f'{self.dirmlplot}/Significance_{self.s_suffix}.png')
            with open(f'{self.dirmlplot}/Significance_{self.s_suffix}.pickle',
                      'wb') as out:
                pickle.dump(fig_signif, out)
示例#20
0
    def do_significance(self):
        if self.step_done("significance"):
            return

        self.do_apply()
        self.do_test()

        self.logger.info("Doing significance optimization")
        gROOT.SetBatch(True)
        gROOT.ProcessLine("gErrorIgnoreLevel = kWarning;")
        #first extract the number of data events in the ml sample
        # This might need a revisit, for now just extract the numbers from the ML merged
        # event count (aka from a YAML since the actual events are not needed)
        # Before the ML count was always taken from the ML merged event df while the total
        # number was taken from the event counter. But the latter is basically not used
        # anymore for a long time cause "dofullevtmerge" is mostly "false" in the DBs
        #and the total number of events
        count_dict = parse_yaml(self.f_evt_count_ml)
        self.p_nevttot = count_dict["evtorig"]
        self.p_nevtml = count_dict["evt"]
        self.logger.debug("Number of data events used for ML: %d",
                          self.p_nevtml)
        self.logger.debug("Total number of data events: %d", self.p_nevttot)
        #calculate acceptance correction. we use in this case all
        #the signal from the mc sample, without limiting to the n. signal
        #events used for training
        denacc = len(self.df_mcgen[self.df_mcgen["ismcprompt"] == 1])
        numacc = len(self.df_mc[self.df_mc["ismcprompt"] == 1])
        acc, acc_err = calc_eff(numacc, denacc)
        self.logger.debug("Acceptance: %.3e +/- %.3e", acc, acc_err)
        #calculation of the expected fonll signals
        delta_pt = self.p_binmax - self.p_binmin
        if self.is_fonll_from_root:
            df_fonll = TFile.Open(self.f_fonll)
            df_fonll_Lc = df_fonll.Get(self.p_fonllparticle + "_" +
                                       self.p_fonllband)
            bin_min = df_fonll_Lc.FindBin(self.p_binmin)
            bin_max = df_fonll_Lc.FindBin(self.p_binmax)
            prod_cross = df_fonll_Lc.Integral(
                bin_min, bin_max) * self.p_fragf * 1e-12 / delta_pt
            signal_yield = 2. * prod_cross * delta_pt * acc * self.p_taa * self.p_br \
                           / (self.p_sigmamb * self.p_fprompt)
            #now we plot the fonll expectation
            cFONLL = TCanvas('cFONLL', 'The FONLL expectation')
            df_fonll_Lc.GetXaxis().SetRangeUser(0, 16)
            df_fonll_Lc.Draw("")
            cFONLL.SaveAs("%s/FONLL_curve_%s.png" %
                          (self.dirmlplot, self.s_suffix))
        else:
            df_fonll = pd.read_csv(self.f_fonll)
            df_fonll_in_pt = \
                    df_fonll.query('(pt >= @self.p_binmin) and (pt < @self.p_binmax)')\
                    [self.p_fonllband]
            prod_cross = df_fonll_in_pt.sum() * self.p_fragf * 1e-12 / delta_pt
            signal_yield = 2. * prod_cross * delta_pt * self.p_br * acc * self.p_taa \
                           / (self.p_sigmamb * self.p_fprompt)
            #now we plot the fonll expectation
            fig = plt.figure(figsize=(20, 15))
            plt.subplot(111)
            plt.plot(df_fonll['pt'],
                     df_fonll[self.p_fonllband] * self.p_fragf,
                     linewidth=4.0)
            plt.xlabel('P_t [GeV/c]', fontsize=20)
            plt.ylabel('Cross Section [pb/GeV]', fontsize=20)
            plt.title("FONLL cross section " + self.p_case, fontsize=20)
            plt.semilogy()
            plt.savefig(f'{self.dirmlplot}/FONLL_curve_{self.s_suffix}.png')
            plt.close(fig)

        self.logger.debug("Expected signal yield: %.3e", signal_yield)
        signal_yield = self.p_raahp * signal_yield
        self.logger.debug("Expected signal yield x RAA hp: %.3e", signal_yield)

        df_data_sideband = self.df_data.query(self.s_selbkgml)
        df_data_sideband = shuffle(df_data_sideband,
                                   random_state=self.rnd_shuffle)
        df_data_sideband = df_data_sideband.tail(
            round(len(df_data_sideband) * self.p_bkgfracopt))
        hmass = TH1F('hmass', '', self.p_num_bins, self.p_mass_fit_lim[0],
                     self.p_mass_fit_lim[1])
        df_mc_signal = self.df_mc[self.df_mc["ismcsignal"] == 1]
        mass_array = df_mc_signal[self.v_invmass].values
        for mass_value in np.nditer(mass_array):
            hmass.Fill(mass_value)

        gaus_fit = TF1("gaus_fit", "gaus", self.p_mass_fit_lim[0],
                       self.p_mass_fit_lim[1])
        gaus_fit.SetParameters(0, hmass.Integral())
        gaus_fit.SetParameters(1, self.p_mass)
        gaus_fit.SetParameters(2, 0.02)
        self.logger.debug("To fit the signal a gaussian function is used")
        fitsucc = hmass.Fit("gaus_fit", "RQ")

        if int(fitsucc) != 0:
            self.logger.warning("Problem in signal peak fit")
            sigma = 0.

        sigma = gaus_fit.GetParameter(2)
        self.logger.debug("Mean of the gaussian: %.3e",
                          gaus_fit.GetParameter(1))
        self.logger.debug("Sigma of the gaussian: %.3e", sigma)
        sig_region = [self.p_mass - 3 * sigma, self.p_mass + 3 * sigma]
        fig_signif_pevt = plt.figure(figsize=(20, 15))
        plt.xlabel('Threshold', fontsize=20)
        plt.ylabel(r'Significance Per Event ($3 \sigma$)', fontsize=20)
        #plt.title("Significance Per Event vs Threshold", fontsize=20)
        plt.xticks(fontsize=18)
        plt.yticks(fontsize=18)
        fig_signif = plt.figure(figsize=(20, 15))
        plt.xlabel('Threshold', fontsize=20)
        plt.ylabel(r'Significance ($3 \sigma$)', fontsize=20)
        #plt.title("Significance vs Threshold", fontsize=20)
        plt.xticks(fontsize=18)
        plt.yticks(fontsize=18)

        df_sig = self.df_mltest[self.df_mltest["ismcprompt"] == 1]

        for name in self.p_classname:
            eff_array, eff_err_array, x_axis = calc_sigeff_steps(
                self.p_nstepsign, df_sig, name)
            bkg_array, bkg_err_array, _ = calc_bkg(
                df_data_sideband, name, self.p_nstepsign, self.p_mass_fit_lim,
                self.p_bkg_func, self.p_bin_width, sig_region, self.p_savefit,
                self.dirmlplot, [self.p_binmin, self.p_binmax], self.v_invmass)
            sig_array = [eff * signal_yield for eff in eff_array]
            sig_err_array = [
                eff_err * signal_yield for eff_err in eff_err_array
            ]
            bkg_array = [
                bkg / (self.p_bkgfracopt * self.p_nevtml) for bkg in bkg_array
            ]
            bkg_err_array = [bkg_err / (self.p_bkgfracopt * self.p_nevtml) \
                             for bkg_err in bkg_err_array]
            signif_array, signif_err_array = calc_signif(
                sig_array, sig_err_array, bkg_array, bkg_err_array)
            plt.figure(fig_signif_pevt.number)
            plt.errorbar(x_axis,
                         signif_array,
                         yerr=signif_err_array,
                         label=f'{name}',
                         elinewidth=2.5,
                         linewidth=5.0)
            signif_array_ml = [
                sig * sqrt(self.p_nevtml) for sig in signif_array
            ]
            signif_err_array_ml = [
                sig_err * sqrt(self.p_nevtml) for sig_err in signif_err_array
            ]
            plt.figure(fig_signif.number)
            plt.errorbar(x_axis,
                         signif_array_ml,
                         yerr=signif_err_array_ml,
                         label=f'{name}_ML_dataset',
                         elinewidth=2.5,
                         linewidth=5.0)
            plt.text(
                0.7,
                0.95,
                f" ${self.p_binmin} < p_\\mathrm{{T}}/(\\mathrm{{GeV}}/c) < {self.p_binmax}$",
                verticalalignment="center",
                transform=fig_signif.gca().transAxes,
                fontsize=30)
            #signif_array_tot = [sig * sqrt(self.p_nevttot) for sig in signif_array]
            #signif_err_array_tot = [sig_err * sqrt(self.p_nevttot) for sig_err in signif_err_array]
            #plt.figure(fig_signif.number)
            #plt.errorbar(x_axis, signif_array_tot, yerr=signif_err_array_tot,
            #             label=f'{name}_Tot', elinewidth=2.5, linewidth=5.0)
            plt.figure(fig_signif_pevt.number)
            plt.legend(loc="upper left", prop={'size': 30})
            plt.savefig(
                f'{self.dirmlplot}/Significance_PerEvent_{self.s_suffix}.png')
            plt.figure(fig_signif.number)
            plt.legend(loc="upper left", prop={'size': 30})
            mpl.rcParams.update({"text.usetex": True})
            plt.savefig(f'{self.dirmlplot}/Significance_{self.s_suffix}.png')
            mpl.rcParams.update({"text.usetex": False})

            with open(f'{self.dirmlplot}/Significance_{self.s_suffix}.pickle',
                      'wb') as out:
                pickle.dump(fig_signif, out)

            plt.close(fig_signif_pevt)
            plt.close(fig_signif)
示例#21
0
    def checkROOT(self):
        # Checking if ROOT is present
        logging.info("Checking ROOT libraries ...")

        # Trying to call root-config
        rootdirs = commands.getstatusoutput('root-config --libdir --incdir')
        if rootdirs[0] > 0:
            logging.error('ROOT module called "root-config" is not detected.\n'\
            +'Two explanations :n'\
            +' - ROOT is not installed. You can download it '\
            +'from http://root.cern.ch\n'\
            +' - ROOT binary folder must be placed in the '\
                          +'global environment variable $PATH')
            return False

        # Extracting ROOT library and header path
        root_tmp = rootdirs[1].split()
        self.includes.append(root_tmp[1])
        self.libs.append(root_tmp[0])
        os.environ['LD_LIBRARY_PATH'] = os.environ['LD_LIBRARY_PATH'] + \
                              ":" + root_tmp[0]
        os.environ['DYLD_LIBRARY_PATH'] = os.environ['DYLD_LIBRARY_PATH'] + \
                              ":" + root_tmp[0]
        os.environ['LIBRARY_PATH'] = os.environ['LIBRARY_PATH'] + \
                              ":" + root_tmp[0]
        os.environ['CPLUS_INCLUDE_PATH'] = os.environ['CPLUS_INCLUDE_PATH'] + \
                              ":" + root_tmp[1]

        # Adding ROOT library path to Python path
        sys.path.append(root_tmp[0])

        # Looking for libPyROOT.so
        find = False
        for item in self.libs:
            files = glob.glob(item + "/libPyROOT.so")
            if len(files) != 0:
                self.configLinux.libraries['PyROOT'] = files[0] + ":" + str(
                    os.stat(files[0]).st_mtime)
                find = True
                break
        if not find:
            logging.error(
                "ROOT library called 'libPyROOT.so' is not found. Please check that ROOT is properly installed."
            )
            return False

        # Looking for ROOT.py
        find = False
        for item in self.libs:
            files = glob.glob(item + "/ROOT.py")
            if len(files) != 0:
                self.configLinux.libraries['ROOT'] = files[0] + ":" + str(
                    os.stat(files[0]).st_mtime)
                find = True
                break
        if not find:
            logging.error(
                "ROOT file called 'ROOT.py' is not found. Please check that ROOT is properly installed."
            )
            return False

        # Looking for TH1F.h
        find = False
        for item in self.includes:
            files = glob.glob(item + "/TH1F.h")
            if len(files) != 0:
                self.configLinux.headers['ROOT'] = files[0] + ":" + str(
                    os.stat(files[0]).st_mtime)
                find = True
                break
        if not find:
            logging.error("ROOT headers are not found. " +\
          "Please check that ROOT is properly installed.")
            return False

        # Loading ROOT library
        logging.info("Loading ROOT libraries ...")
        try:
            from ROOT import gROOT
        except:
            logging.error("'root-config --libdir' indicates a wrong path for ROOT"\
                   +" libraries. Please specify the ROOT library path"\
            +" into the environnement variable $PYTHONPATH")
            return False

        # Setting ROOT batch mode
        if not self.script:
            from ROOT import TApplication
            from ROOT import gApplication
            TApplication.NeedGraphicsLibs()
            gApplication.InitializeGraphics()
        gROOT.SetBatch(True)

        # Checking ROOT release
        RootVersion = str(gROOT.GetVersionInt())
        if len(RootVersion) < 3:
            logging.error('Bad release of ROOT : '+gROOT.GetVersion()+\
                                 '. MadAnalysis5 needs ROOT 5.27 or higher.\n Please upgrade your version of ROOT.')
            return False

        RootVersionA = int(RootVersion[0])
        RootVersionB = int(RootVersion[1] + RootVersion[2])
        if RootVersionA != 5 or RootVersionB < 27:
            logging.error('Bad release of ROOT : '+gROOT.GetVersion()+\
                                 '. MadAnalysis5 needs ROOT 5.27 or higher.\n Please upgrade your version of ROOT.')
            return False

        self.configLinux.root_version = RootVersion
        return True
def main():

    parser = OptionParser(usage='usage: %prog [options] ARGs',
                          description='S/B plot macro')
    parser.add_option('-c', '--channel', dest='channel', default='mt et tt em', action='store',
                      help='channels to be considered for the plot. Default : mt et tt em ee mm mt_soft vhtt')

    parser.add_option('-p', '--period', dest='period', default='7TeV 8TeV', action='store',
                      help='period used for the S/B plots. Default : 7TeV 8TeV')

    parser.add_option('-g', '--category', dest='category', default='0jet 1jet vbf', action='store',
                      help='categories used for the S/B plots. Default : 0jet 1jet vbf')

    parser.add_option('-m', '--mode', dest='mode', default='Mtt', action='store',
                      help='Plot mode. You can choose Mtt, SOB, SigMtt Default : Mtt')

    parser.add_option("-b", "--batch", dest='batch', action="store_true", default=True,
                      help='Set Batch mode. Default : False')

    parser.add_option("-u", "--unblind", dest='unblind', action="store_true", default=False,
                      help='Use this option when unblinding. Default : False')

    (options, args) = parser.parse_args()


    if(options.batch==True):
        gROOT.SetBatch(True)

    init = ConfigParser.SafeConfigParser()
    init.read('./config.ini')

    dict = {}
    list_all = []


    for ichn in options.channel.split():

        list = []
        
        for icat in options.category.split():
            for iperiod in options.period.split():
                list.extend(init.get(ichn, icat+'_'+iperiod).split())

        dict[ichn] = list
        list_all.extend(list)

    print
    print fmt % ('Total # of files', ':', len(list_all))

#    categoryname = 'All_category'
    categoryname = ''
    if options.category != '0jet 1jet vbf':
        categoryname = options.category

    # Individual channel
    for ichn in options.channel.split():
        pname = ichn + '_SM'

#        sobCombine(pname, dict[ichn], init.get('naming', 'caption'), init.get('naming',ichn), categoryname, 1, init.get('muvalue',ichn), options.mode)
        sobCombine(pname, dict[ichn], init.get('naming', 'caption'), init.get('naming',ichn), categoryname, 1, init.get('muvalue',ichn), options.mode, options.unblind)


    # Combine
    sobCombine('All_SM', list_all, init.get('naming', 'caption'), init.get('naming','all'), categoryname, 1, init.get('muvalue','all'), options.mode, options.unblind)
def main():

    gROOT.SetBatch(1)
    SetAtlasStyle()

    channels = ["ejets", "mujets"]
    #channels = ["mujets"]

    variables = [
        "Top1_lhood", "Top2_lhood", "SystemMass", "SystemPt", "SystemRapidity"
    ]
    #variables = ["Top1_lhood"]

    for j in range(len(variables)):
        variable = variables[j]
        print j, variable

        can = TCanvas(variable, variable, 0, 0, 800, 600)
        can.SetMargin(0.2, 0.05, 0.15, 0.03)

        label = ''
        regnumber = ''

        if variable is 'Top1_lhood':
            label = 'Leptonic top p^{t}_{T}'
            regnumber = "reg4"
        elif variable is 'Top2_lhood':
            label = 'Hadronic top p^{t}_{T}'
            regnumber = "reg4"
        elif variable is 'SystemMass':
            label = 't#bar{t} System mass'
            regnumber = "reg3"
        elif variable is 'SystemPt':
            label = 't#bar{t} System p_{T}'
            regnumber = "reg3"
        elif variable is 'SystemRapidity':
            label = 't#bar{t} System rapidity'
            regnumber = "reg4"

        legend = TLegend(0.6, 0.95, 0.95, 0.75, label)
        legend.SetFillStyle(0)
        legend.SetBorderSize(0)

        ratioPlots = []

        for i in range(len(channels)):
            channel = channels[i]

            print i, channel

            postfix = ''
            labelChannel = ''
            if channel is 'ejets':
                postfix = 'el'
                labelChannel = 'e + jets'
            elif channel is 'mujets':
                postfix = 'mu'
                labelChannel = '#mu + jets'
            elif channel is 'combined':
                postfix = 'co'

            filename = "../data/data_Unfolded_14_02_2013/Alpgen/nominal_Tag1_" + variable + "_" + postfix + "_toy5000_svd_" + regnumber + ".root"
            fileDi = TFile(filename)
            if not fileDi.IsOpen():
                print 'ERROR opening ', filename

            m_histo = fileDi.Get("unfolding/toys/nominal/" + postfix +
                                 "/Tag1_" + variable + "/SVD/" + regnumber +
                                 "/toy5000/H_" + postfix + "_Tag1_" +
                                 variable + "_SVD_" + regnumber +
                                 "_toy5000_Regularization")
            m_histo.SetName("diFactor" + variable + postfix)
            nbins = m_histo.GetNbinsX() - 1

            diFactor = m_histo.Clone()
            diFactor.SetDirectory(0)
            diFactor.SetNdivisions(508)
            diFactor.GetXaxis().SetRangeUser(1, nbins + 1)

            for bin in range(nbins):
                bindiFactor = m_histo.GetBinContent(bin + 1)
                print bin, bindiFactor
                diFactor.SetBinContent(bin + 2, bindiFactor)

            diFactor.SetTitle("")
            diFactor.SetMarkerColor(kBlack + i)
            diFactor.SetMarkerStyle(20 + i)
            diFactor.SetLineColor(kBlack + i)
            diFactor.GetYaxis().SetRangeUser(0.005, 400)
            diFactor.GetYaxis().SetTitle("|d_{i}|")
            diFactor.GetXaxis().SetTitle("bin number")

            ratioPlots.append(diFactor)

            legend.AddEntry(diFactor, labelChannel, "p")

            if i > 0:
                diFactor.Draw('histosame')
            else:
                diFactor.Draw("histo")

        can.cd()
        horizontal = TF1("line", "pol1", 0, 2800)
        horizontal.SetParameters(1, 0)
        horizontal.SetLineColor(kBlack)
        horizontal.SetLineStyle(2)
        horizontal.Draw('same')

        legend.Draw('same')
        can.SetLogy()
        can.SaveAs("regularizationValue_" + variable + ".eps")
示例#24
0
def drawMultiGraph(mg, title, lt, rt, fname, ymin, ymax, ptmax, log, bl=True):

    #myStyle()
    gROOT.SetBatch(True)
    canvas = ROOT.TCanvas('bla', 'bla', 600, 600)
    canvas.SetLogy(log)
    canvas.SetTicks(1, 1)
    canvas.SetLeftMargin(0.14)
    canvas.SetRightMargin(0.08)
    ROOT.gStyle.SetOptStat(0000000)

    mg.Draw("AL")
    '''mg.GetYaxis().SetLabelFont(132)
    mg.GetYaxis().SetTitleFont(132)
    mg.GetYaxis().SetLabelOffset(0.02)
    mg.GetYaxis().CenterTitle()
    mg.GetYaxis().SetNdivisions(505)
    mg.GetXaxis().SetNdivisions(505)
    mg.GetYaxis().SetTitleOffset(1.8)

    mg.GetXaxis().SetTitleFont(132)
    mg.GetXaxis().SetLabelFont(132)
    mg.GetXaxis().SetLabelOffset(0.02)
    mg.GetXaxis().SetTitleOffset(1.5)
    mg.GetXaxis().SetTitleSize(0.06)
    mg.GetYaxis().SetTitleSize(0.048)
    mg.GetXaxis().SetLabelSize(0.06)
    mg.GetYaxis().SetLabelSize(0.06)'''

    mg.GetXaxis().SetTitleSize(0.035)
    mg.GetYaxis().SetTitleSize(0.035)

    mg.GetXaxis().SetRangeUser(50., ptmax)

    mg.GetYaxis().SetTitleOffset(1.75)
    mg.GetXaxis().SetTitleOffset(1.35)

    mg.SetMinimum(ymin)
    mg.SetMaximum(ymax)

    if log: ROOT.gPad.SetLogy()

    if bl:
        leg = canvas.BuildLegend(0.55, 0.70, 0.88, 0.88)
        leg.SetFillColor(0)
        leg.SetFillStyle(0)
        leg.SetLineColor(0)
        leg.Draw()

    Text = ROOT.TLatex()

    Text.SetNDC()
    Text.SetTextAlign(31)
    Text.SetTextSize(0.04)

    text = '#it{' + lt + '}'

    Text.DrawLatex(0.90, 0.92, text)

    rt = re.split(",", rt)
    text = '#bf{#it{' + rt[0] + '}}'

    Text.SetTextAlign(22)
    Text.SetNDC(ROOT.kTRUE)
    Text.SetTextSize(0.04)
    Text.DrawLatex(0.30, 0.83, text)

    Text.SetTextAlign(22)
    text = '#bf{#it{' + rt[1] + '}}'
    Text.SetTextSize(0.036)
    Text.DrawLatex(0.30, 0.76, text)
    #Text.DrawLatex(0.18, 0.78, rt[1])

    if len(rt) > 2:
        text = '#it{#bf{' + rt[2] + '}}'
        if 'ell' in text:
            text = text.replace('#', '\\')
        Text.SetTextSize(0.05)
        Text.DrawLatex(0.69, 0.27, text)

    if len(rt) > 3:
        text = '#it{#bf{' + rt[3] + '}}'
        if 'ell' in text:
            text = text.replace('#', '\\')
        Text.SetTextSize(0.04)
        Text.DrawLatex(0.71, 0.20, text)

    canvas.RedrawAxis()
    canvas.Update()
    canvas.GetFrame().SetBorderSize(12)
    canvas.Modified()
    canvas.Update()

    pdir = os.path.dirname(fname)
    name = os.path.basename(fname)
    name = title + '_' + name

    filename = pdir + '/' + name

    if not os.path.exists(pdir):
        os.makedirs(pdir)
    canvas.Print('{}.png'.format(filename), 'png')
    canvas.Print('{}.pdf'.format(filename), 'pdf')
示例#25
0
def plotDataMC(mainConfig, dilepton):
    import gc
    gc.enable()

    from ROOT import TCanvas, TPad, TH1F, TH1I, THStack, TLegend, TMath, gROOT, TFile, TH2F
    import ratios
    from defs import Backgrounds
    from defs import Backgrounds2011
    from defs import Signals
    from defs import defineMyColors
    from defs import myColors
    from defs import Region
    from defs import Regions
    from defs import Plot
    from corrections import triggerEffs, rSFOFTrig
    from setTDRStyle import setTDRStyle
    gROOT.SetBatch(True)
    from helpers import *
    import math

    ### file to normalize signal MC
    signalDenominatorFile = TFile(
        "../SignalScan/T6bbllsleptonDenominatorHisto4.root")
    signalDenominatorHisto = TH2F(signalDenominatorFile.Get("massScan"))

    if mainConfig.forPAS:
        hCanvas = TCanvas("hCanvas", "Distribution", 600, 800)
    else:
        hCanvas = TCanvas("hCanvas", "Distribution", 800, 800)
    if mainConfig.plotRatio:
        plotPad = ROOT.TPad("plotPad", "plotPad", 0, 0.3, 1, 1)
        ratioPad = ROOT.TPad("ratioPad", "ratioPad", 0, 0., 1, 0.3)
        setTDRStyle()
        plotPad.UseCurrentStyle()
        ratioPad.UseCurrentStyle()
        plotPad.Draw()
        ratioPad.Draw()
        plotPad.cd()
    else:
        plotPad = ROOT.TPad("plotPad", "plotPad", 0, 0, 1, 1)
        setTDRStyle()
        plotPad.UseCurrentStyle()
        plotPad.Draw()
        plotPad.cd()

    colors = createMyColors()

    eventCounts = totalNumberOfGeneratedEvents(mainConfig.dataSetPath)
    processes = []
    for background in mainConfig.backgrounds:
        processes.append(Process(getattr(Backgrounds, background),
                                 eventCounts))

    ### get the signal if any is to be plotted
    signalEventCounts = {}
    signals = []
    signalNameLabel = ""
    for signal in mainConfig.signals:
        m_b = int(signal.split("_")[2])
        m_n_2 = int(signal.split("_")[4])
        signalEventCounts[signal] = signalDenominatorHisto.GetBinContent(
            signalDenominatorHisto.GetXaxis().FindBin(m_b),
            signalDenominatorHisto.GetYaxis().FindBin(m_n_2))
        signals.append(Process(getattr(Signals, signal), signalEventCounts))
        if signalNameLabel == "":
            signalNameLabel = "Signal"
        signalNameLabel += "_m_b_%s_m_n_%s" % (signal.split("_")[2],
                                               signal.split("_")[4])

    #~ legend = TLegend(0.45, 0.6, 0.925, 0.925)
    legend = TLegend(0.55, 0.6, 0.925, 0.925)
    legend.SetFillStyle(0)
    legend.SetBorderSize(0)
    legend.SetTextFont(42)
    #~ legend.SetNColumns(2)
    legendEta = TLegend(0.15, 0.75, 0.7, 0.9)
    legendEta.SetFillStyle(0)
    legendEta.SetBorderSize(0)
    legendEta.SetTextFont(42)

    latex = ROOT.TLatex()
    latex.SetTextFont(42)
    latex.SetTextAlign(31)
    latex.SetTextSize(0.04)
    latex.SetNDC(True)
    latexCMS = ROOT.TLatex()
    latexCMS.SetTextFont(61)
    latexCMS.SetTextSize(0.06)
    latexCMS.SetNDC(True)
    latexCMSExtra = ROOT.TLatex()
    latexCMSExtra.SetTextFont(52)
    latexCMSExtra.SetTextSize(0.045)
    latexCMSExtra.SetNDC(True)
    legendHists = []

    legendHistData = ROOT.TH1F()
    if mainConfig.plotData:
        legend.AddEntry(legendHistData, "Data", "pe")
        legendEta.AddEntry(legendHistData, "Data", "pe")

    ### loop over the background processes, add them in iverse order to the legend
    if mainConfig.plotMC:
        for process in reversed(processes):
            temphist = ROOT.TH1F()
            temphist.SetFillColor(process.theColor)
            ### Do not add a seperate legend entry for DY to tautau if new legend scheme is used
            if not process.label == "DY+jets non resonant":
                legendHists.append(temphist.Clone)
                legend.AddEntry(temphist, process.label, "f")
                legendEta.AddEntry(temphist, process.label, "f")
    if mainConfig.plotRatio:
        temphist = ROOT.TH1F()
        temphist.SetFillColor(myColors["MyGreen"])
        legendHists.append(temphist.Clone)
        #~ legend.AddEntry(temphist,"Syst. uncert.","f")
        temphist2 = ROOT.TH1F()
        temphist2.SetFillColor(myColors["DarkRed"], )
        temphist2.SetFillStyle(3002)
        legendHists.append(temphist2.Clone)

    if mainConfig.plotSignal:
        for Signal in signals:
            temphist = ROOT.TH1F()
            temphist.SetFillColor(Signal.theColor)
            temphist.SetLineColor(Signal.theLineColor)
            legendHists.append(temphist.Clone)
            legend.AddEntry(temphist, Signal.label, "l")
            legendEta.AddEntry(temphist, Signal.label, "l")

    nEvents = -1

    ROOT.gStyle.SetOptStat(0)

    intlumi = ROOT.TLatex()
    intlumi.SetTextAlign(12)
    intlumi.SetTextSize(0.03)
    intlumi.SetNDC(True)
    intlumi2 = ROOT.TLatex()
    intlumi2.SetTextAlign(12)
    intlumi2.SetTextSize(0.07)
    intlumi2.SetNDC(True)
    scalelabel = ROOT.TLatex()
    scalelabel.SetTextAlign(12)
    scalelabel.SetTextSize(0.03)
    scalelabel.SetNDC(True)
    metDiffLabel = ROOT.TLatex()
    metDiffLabel.SetTextAlign(12)
    metDiffLabel.SetTextSize(0.03)
    metDiffLabel.SetNDC(True)
    chi2Label = ROOT.TLatex()
    chi2Label.SetTextAlign(12)
    chi2Label.SetTextSize(0.03)
    chi2Label.SetNDC(True)
    hCanvas.SetLogy()

    treeEE = readTrees(mainConfig.dataSetPath, "EE")
    treeMuMu = readTrees(mainConfig.dataSetPath, "MuMu")
    treeEMu = readTrees(mainConfig.dataSetPath, "EMu")

    mainConfig.plot.addDilepton(dilepton)

    plotPad.cd()
    plotPad.SetLogy(0)
    logScale = mainConfig.plot.log
    #~ logScale = True
    if mainConfig.plot.variable == "met" or mainConfig.plot.variable == "type1Met" or mainConfig.plot.variable == "tcMet" or mainConfig.plot.variable == "caloMet" or mainConfig.plot.variable == "mht":
        logScale = True

    if logScale == True:
        plotPad.SetLogy()

    scaleTree1 = 1.0
    scaleTree2 = 1.0
    if mainConfig.plot.tree1 == "EE":
        tree1 = treeEE
        if mainConfig.useDataTrigEff:
            scaleTree1 = getattr(triggerEffs, mainConfig.etaRegion).effEE.val
            #~ scaleTree1 = getattr(triggerEffs,mainConfig.etaRegion).effEE.val**2
            print "ee trigger Eff:"
            print scaleTree1
        #~ elif mainConfig.useTriggerEmulation:
        #~ scaleTree1 = getattr(triggerEffs,mainConfig.etaRegion).effEE.val/getattr(triggerEffs,mainConfig.etaRegion).effEE.valMC
    elif mainConfig.plot.tree1 == "MuMu":
        tree1 = treeMuMu
        if mainConfig.useDataTrigEff:
            scaleTree1 = getattr(triggerEffs, mainConfig.etaRegion).effMM.val
            #~ scaleTree1 = getattr(triggerEffs,mainConfig.etaRegion).effMM.val**2
            print "mumu trigger Eff:"
            print scaleTree1
        #~ elif mainConfig.useTriggerEmulation:
        #~ scaleTree1 = getattr(triggerEffs,mainConfig.etaRegion).effMM.val/getattr(triggerEffs,mainConfig.etaRegion).effMM.valMC
    elif mainConfig.plot.tree1 == "EMu":
        tree1 = treeEMu
        if mainConfig.useDataTrigEff:
            scaleTree1 = getattr(triggerEffs, mainConfig.etaRegion).effEM.val
            print "emu trigger Eff:"
            print scaleTree1
        #~ elif mainConfig.useTriggerEmulation:
        #~ scaleTree1 = getattr(triggerEffs,mainConfig.etaRegion).effEM.val/getattr(triggerEffs,mainConfig.etaRegion).effEM.valMC
    else:
        print "Unknown Dilepton combination! %s not created!" % (
            mainConfig.plot.filename, )
        return

    if mainConfig.plot.tree2 != "None":
        if mainConfig.plot.tree2 == "EE":
            tree2 = treeEE
            if mainConfig.useDataTrigEff:
                #~ scaleTree2 = mainConfig.selection.trigEffs.effEE.val
                scaleTree2 = mainConfig.selection.trigEffs.effEE.val**2
                print "ee trigger Eff:"
                print scaleTree2
            #~ elif mainConfig.useTriggerEmulation:
            #~ scaleTree2 = getattr(triggerEffs,mainConfig.etaRegion).effEE.val/getattr(triggerEffs,mainConfig.etaRegion).effEE.valMC
        elif mainConfig.plot.tree2 == "MuMu":
            tree2 = treeMuMu
            if mainConfig.useDataTrigEff:
                #~ scaleTree2 = mainConfig.selection.trigEffs.effMM.val
                scaleTree2 = mainConfig.selection.trigEffs.effMM.val**2
                print "mumu trigger Eff:"
                print scaleTree2
            #~ elif mainConfig.useTriggerEmulation:
            #~ scaleTree2 = getattr(triggerEffs,mainConfig.etaRegion).effMM.val/getattr(triggerEffs,mainConfig.etaRegion).effMM.valMC

        elif mainConfig.plot.tree2 == "EMu":
            tree2 = treeEMu
            if mainConfig.useDataTrigEff:
                scaleTree2 = mainConfig.selection.trigEffs.effEM.val
                print "emu trigger Eff:"
                print scaleTree2
            #~ elif mainConfig.useTriggerEmulation:
            #~ scaleTree2 = getattr(triggerEffs,mainConfig.etaRegion).effEM.val/getattr(triggerEffs,mainConfig.etaRegion).effEM.valMC
        else:
            print "Unknown Dilepton combination! %s not created!" % (
                mainConfig.plot.filename, )
            return
    else:
        tree2 = "None"

    if mainConfig.normalizeToData:
        pickleName = mainConfig.plot.filename % (
            "_normalizedToData_" + mainConfig.runRange.label + "_" + dilepton)
    elif mainConfig.useTriggerEmulation:
        pickleName = mainConfig.plot.filename % (
            "_TriggerEmulation_" + mainConfig.runRange.label + "_" + dilepton)
    elif mainConfig.DontScaleTrig:
        pickleName = mainConfig.plot.filename % (
            "_NoTriggerScaling_" + mainConfig.runRange.label + "_" + dilepton)
    elif mainConfig.useDataTrigEff:
        pickleName = mainConfig.plot.filename % (
            "_dataTriggerEffs_" + mainConfig.runRange.label + "_" + dilepton)
    else:
        pickleName = mainConfig.plot.filename % (
            "_" + mainConfig.runRange.label + "_" + dilepton)

    counts = {}
    import pickle
    print mainConfig.plot.cuts
    if mainConfig.plotData:
        datahist = getDataHist(
            mainConfig.plot,
            tree1,
            tree2,
            normalizeToBinWidth=mainConfig.normalizeToBinWidth)
        counts["Data"] = {
            "val": datahist.Integral(0,
                                     datahist.GetNbinsX() + 1),
            "err": math.sqrt(datahist.Integral(0,
                                               datahist.GetNbinsX() + 1))
        }
        print datahist.GetEntries()

    if mainConfig.plotMC:
        #~ stack = TheStack(processes,mainConfig.runRange.lumi,mainConfig.plot,tree1,tree2,1.0,scaleTree1,scaleTree2,saveIntegrals=True,counts=counts,doTopReweighting=mainConfig.doTopReweighting,theoUncert=mainConfig.theoUncert,doPUWeights=mainConfig.doPUWeights,normalizeToBinWidth=mainConfig.normalizeToBinWidth)
        stack = TheStack(processes,
                         mainConfig.runRange.lumi,
                         mainConfig.plot,
                         tree1,
                         tree2,
                         1.0,
                         scaleTree1,
                         scaleTree2,
                         saveIntegrals=True,
                         counts=counts,
                         doTopReweighting=mainConfig.doTopReweighting,
                         doPUWeights=mainConfig.doPUWeights,
                         normalizeToBinWidth=mainConfig.normalizeToBinWidth,
                         useTriggerEmulation=mainConfig.useTriggerEmulation)

        ### get the number of MC events and the uncertainty
        errIntMC = ROOT.Double()
        intMC = stack.theHistogram.IntegralAndError(
            0,
            stack.theHistogram.GetNbinsX() + 1, errIntMC)

        val = float(intMC)
        err = float(errIntMC)

        counts["Total Background"] = {"val": val, "err": err}

        ### scale the stack if MC is to be normalzed to data
        if mainConfig.normalizeToData:
            scalefac = datahist.Integral(
                datahist.FindBin(mainConfig.plot.firstBin),
                datahist.FindBin(
                    mainConfig.plot.lastBin)) / stack.theHistogram.Integral(
                        stack.theHistogram.FindBin(mainConfig.plot.firstBin),
                        stack.theHistogram.FindBin(mainConfig.plot.lastBin))

            drawStack = TheStack(
                processes,
                mainConfig.runRange.lumi,
                mainConfig.plot,
                tree1,
                tree2,
                1.0,
                scalefac * scaleTree1,
                scalefac * scaleTree2,
                doPUWeights=mainConfig.doPUWeights,
                normalizeToBinWidth=mainConfig.normalizeToBinWidth,
                useTriggerEmulation=mainConfig.useTriggerEmulation)
            stackJESUp = TheStack(
                processes,
                mainConfig.runRange.lumi,
                mainConfig.plot,
                tree1,
                tree2,
                0.955,
                scalefac * scaleTree1,
                scalefac * scaleTree2,
                doPUWeights=mainConfig.doPUWeights,
                normalizeToBinWidth=mainConfig.normalizeToBinWidth,
                useTriggerEmulation=mainConfig.useTriggerEmulation)
            stackJESDown = TheStack(
                processes,
                mainConfig.runRange.lumi,
                mainConfig.plot,
                tree1,
                tree2,
                1.045,
                scalefac * scaleTree1,
                scalefac * scaleTree2,
                doPUWeights=mainConfig.doPUWeights,
                normalizeToBinWidth=mainConfig.normalizeToBinWidth,
                useTriggerEmulation=mainConfig.useTriggerEmulation)

        else:
            drawStack = stack

            if mainConfig.plotSyst:

                mainConfig.plot.cuts = mainConfig.plot.cuts.replace(
                    "met ", "metJESUp ")
                mainConfig.plot.cuts = mainConfig.plot.cuts.replace(
                    " ht", " htJESUp")
                mainConfig.plot.cuts = mainConfig.plot.cuts.replace(
                    " nJets ", " nShiftedJetsJESUp ")
                mainConfig.plot.cuts = mainConfig.plot.cuts.replace(
                    "(nJets ", "(nShiftedJetsJESUp ")
                stackJESUp = TheStack(
                    processes,
                    mainConfig.runRange.lumi,
                    mainConfig.plot,
                    tree1,
                    tree2,
                    1.0,
                    scaleTree1,
                    scaleTree2,
                    JESUp=True,
                    saveIntegrals=True,
                    counts=counts,
                    doPUWeights=mainConfig.doPUWeights,
                    normalizeToBinWidth=mainConfig.normalizeToBinWidth,
                    useTriggerEmulation=mainConfig.useTriggerEmulation)
                mainConfig.plot.cuts = mainConfig.plot.cuts.replace(
                    "metJESUp", "metJESDown")
                mainConfig.plot.cuts = mainConfig.plot.cuts.replace(
                    "htJESUp", "htJESDown")
                mainConfig.plot.cuts = mainConfig.plot.cuts.replace(
                    "nShiftedJetsJESUp", "nShiftedJetsJESDown")
                stackJESDown = TheStack(
                    processes,
                    mainConfig.runRange.lumi,
                    mainConfig.plot,
                    tree1,
                    tree2,
                    1.0,
                    scaleTree1,
                    scaleTree2,
                    JESDown=True,
                    saveIntegrals=True,
                    counts=counts,
                    doPUWeights=mainConfig.doPUWeights,
                    normalizeToBinWidth=mainConfig.normalizeToBinWidth,
                    useTriggerEmulation=mainConfig.useTriggerEmulation)
                mainConfig.plot.cuts = mainConfig.plot.cuts.replace(
                    "metJESDown", "met")
                mainConfig.plot.cuts = mainConfig.plot.cuts.replace(
                    "htJESDown", "ht")
                mainConfig.plot.cuts = mainConfig.plot.cuts.replace(
                    "nShiftedJetsJESDown", "nJets")
                mainConfig.plot.cuts = mainConfig.plot.cuts.replace(
                    "*(", "Up*(")
                stackPileUpUp = TheStack(
                    processes,
                    mainConfig.runRange.lumi,
                    mainConfig.plot,
                    tree1,
                    tree2,
                    1.0,
                    scaleTree1,
                    scaleTree2,
                    saveIntegrals=True,
                    PileUpUp=True,
                    counts=counts,
                    doPUWeights=mainConfig.doPUWeights,
                    normalizeToBinWidth=mainConfig.normalizeToBinWidth,
                    useTriggerEmulation=mainConfig.useTriggerEmulation)
                mainConfig.plot.cuts = mainConfig.plot.cuts.replace(
                    "Up*(", "Down*(")
                stackPileUpDown = TheStack(
                    processes,
                    mainConfig.runRange.lumi,
                    mainConfig.plot,
                    tree1,
                    tree2,
                    1.0,
                    scaleTree1,
                    scaleTree2,
                    saveIntegrals=True,
                    PileUpDown=True,
                    counts=counts,
                    doPUWeights=mainConfig.doPUWeights,
                    normalizeToBinWidth=mainConfig.normalizeToBinWidth,
                    useTriggerEmulation=mainConfig.useTriggerEmulation)
                mainConfig.plot.cuts = mainConfig.plot.cuts.replace(
                    "Down*(", "*(")

                errIntMC = ROOT.Double()
                intMCPileUpUp = stackPileUpUp.theHistogram.IntegralAndError(
                    0,
                    stack.theHistogram.GetNbinsX() + 1, errIntMC)
                errIntMC = ROOT.Double()
                intMCPileUpDown = stackPileUpDown.theHistogram.IntegralAndError(
                    0,
                    stack.theHistogram.GetNbinsX() + 1, errIntMC)

                valPileUpUp = float(intMCPileUpUp)
                valPileUpDown = float(intMCPileUpDown)
                pileUpUp = abs(counts["Total Background"]["val"] - valPileUpUp)
                pileUpDown = abs(counts["Total Background"]["val"] -
                                 valPileUpDown)
                counts["Total Background"]["pileUpDown"] = pileUpDown
                counts["Total Background"]["pileUpUp"] = pileUpUp

                if mainConfig.doTopReweighting:
                    stackReweightDown = TheStack(
                        processes,
                        mainConfig.runRange.lumi,
                        mainConfig.plot,
                        tree1,
                        tree2,
                        1.0,
                        scaleTree1,
                        scaleTree2,
                        TopWeightDown=True,
                        saveIntegrals=True,
                        counts=counts,
                        doPUWeights=mainConfig.doPUWeights,
                        normalizeToBinWidth=mainConfig.normalizeToBinWidth,
                        useTriggerEmulation=mainConfig.useTriggerEmulation)
                    stackReweightUp = TheStack(
                        processes,
                        mainConfig.runRange.lumi,
                        mainConfig.plot,
                        tree1,
                        tree2,
                        1.0,
                        scaleTree1,
                        scaleTree2,
                        TopWeightUp=True,
                        saveIntegrals=True,
                        counts=counts,
                        doPUWeights=mainConfig.doPUWeights,
                        normalizeToBinWidth=mainConfig.normalizeToBinWidth,
                        useTriggerEmulation=mainConfig.useTriggerEmulation)

                    errIntMC = ROOT.Double()
                    intMCTopWeightUp = stackReweightUp.theHistogram.IntegralAndError(
                        0,
                        stack.theHistogram.GetNbinsX() + 1, errIntMC)
                    errIntMC = ROOT.Double()
                    intMCTopWeightDown = stackReweightDown.theHistogram.IntegralAndError(
                        0,
                        stack.theHistogram.GetNbinsX() + 1, errIntMC)

                    valTopWeightUp = float(intMCTopWeightUp)
                    valTopWeightDown = float(intMCTopWeightDown)
                    topWeightUp = abs(counts["Total Background"]["val"] -
                                      valTopWeightUp)
                    topWeightDown = abs(counts["Total Background"]["val"] -
                                        valTopWeightDown)
                    counts["Total Background"]["topWeightDown"] = topWeightDown
                    counts["Total Background"]["topWeightUp"] = topWeightUp

                errIntMC = ROOT.Double()
                intMCJESUp = stackJESUp.theHistogram.IntegralAndError(
                    0,
                    stack.theHistogram.GetNbinsX() + 1, errIntMC)
                errIntMC = ROOT.Double()
                intMCJESDown = stackJESDown.theHistogram.IntegralAndError(
                    0,
                    stack.theHistogram.GetNbinsX() + 1, errIntMC)

                valJESUp = float(intMCJESUp)
                valJESDown = float(intMCJESDown)
                jesUp = abs(counts["Total Background"]["val"] - valJESUp)
                jesDown = abs(counts["Total Background"]["val"] - valJESDown)
                counts["Total Background"]["jesDown"] = jesDown
                counts["Total Background"]["jesUp"] = jesUp

        xSec = abs(
            stack.theHistogramXsecUp.Integral(
                0,
                stack.theHistogram.GetNbinsX() + 1) -
            counts["Total Background"]["val"])
        counts["Total Background"]["xSec"] = xSec
        #~ theoUncert = abs(stack.theHistogramTheoUp.Integral(0,stack.theHistogram.GetNbinsX()+1)-counts["Total Background"]["val"])
        #~ counts["Total Background"]["theo"]=theoUncert

    outFilePkl = open("shelves/%s.pkl" % (pickleName), "w")
    pickle.dump(counts, outFilePkl)
    outFilePkl.close()

    if mainConfig.plotSignal:
        signalhists = []
        signalLabels = []
        ymaxSignal = 0
        for Signal in signals:
            signalhist = Signal.createCombinedHistogram(
                mainConfig.runRange.lumi, mainConfig.plot, tree1, tree2)
            signalhist.SetLineWidth(2)
            if mainConfig.stackSignal:
                signalhist.Add(drawStack.theHistogram)
            signalhist.SetMinimum(0.1)
            signalhists.append(signalhist)
            signalLabels.append(Signal.label)
            tmpYmaxSignal = signalhist.GetBinContent(
                signalhist.GetMaximumBin())
            if tmpYmaxSignal > ymaxSignal:
                ymaxSignal = tmpYmaxSignal

    ### set the maximum of the y-axis
    yMax = 0
    yMin = 0
    if mainConfig.plotData:
        yMax = datahist.GetBinContent(datahist.GetMaximumBin())
    if (mainConfig.plotMC and yMax < drawStack.theHistogram.GetBinContent(
            drawStack.theHistogram.GetMaximumBin())):
        yMax = drawStack.theHistogram.GetBinContent(
            drawStack.theHistogram.GetMaximumBin())
    if (mainConfig.plotSignal and yMax < ymaxSignal):
        yMax = ymaxSignal
    if mainConfig.plot.yMax == 0:
        if logScale:
            yMax = yMax * 200
            if datahist.GetBinContent(datahist.GetMinimumBin()) > 0:
                yMin = datahist.GetBinContent(datahist.GetMinimumBin()) / 5.
            else:
                yMin = 0.1
                #~ yMin = 0.5
                #~ yMin = 5
        else:
            yMax = yMax * 1.5

    else:
        yMax = plot.yMax

    #~ yMax = 1200
    #~ yMax = 130

    plotPad.DrawFrame(
        mainConfig.plot.firstBin, yMin, mainConfig.plot.lastBin, yMax,
        "; %s ; %s" % (mainConfig.plot.xaxis, mainConfig.plot.yaxis))

    ### draw the stack
    if mainConfig.plotMC:
        drawStack.theStack.Draw("samehist")
        lineHistogram = drawStack.theHistogram
        lineHistogram.Draw("same l")

    ### Draw signal
    if mainConfig.plotSignal:
        for signalhist in signalhists:
            signalhist.Draw("samehist")

    dileptonLabel = ""
    if dilepton == "SF":
        dileptonLabel = "ee + #mu#mu"
    if dilepton == "OF":
        dileptonLabel = "e#mu"
    if dilepton == "EE":
        dileptonLabel = "ee"
    if dilepton == "MuMu":
        dileptonLabel = "#mu#mu"

    if mainConfig.plotData:
        #~ datahist.SetMinimum(0.1)
        datahist.Draw("samep")

    if mainConfig.normalizeToData:
        scalelabel.DrawLatex(0.6, 0.4,
                             "Background scaled by %.2f" % (scalefac))

    if mainConfig.plot.variable == "eta1" or mainConfig.plot.variable == "eta2":
        legendEta.SetNColumns(2)
        legendEta.Draw()
        intlumi.DrawLatex(
            0.2, 0.7, "#splitline{" + mainConfig.plot.label + " " +
            dileptonLabel + "}{#splitline{" + mainConfig.plot.label2 + "}{" +
            mainConfig.plot.label3 + "}}")
    else:
        legend.Draw()
        intlumi.DrawLatex(
            0.4, 0.55,
            "#splitline{%s}{%s}" % (mainConfig.plot.label2, dileptonLabel))

    latex.DrawLatex(0.95, 0.96,
                    "%s fb^{-1} (13 TeV)" % (mainConfig.runRange.printval, ))
    yLabelPos = 0.85
    #~ yLabelPos = 0.88
    cmsExtra = ""
    if mainConfig.personalWork:
        cmsExtra = "Private Work"
        if not mainConfig.plotData:
            cmsExtra = "#splitline{Private Work}{Simulation}"
            yLabelPos = 0.82
            #~ yLabelPos = 0.85
    elif not mainConfig.plotData:
        cmsExtra = "Simulation"
    elif mainConfig.preliminary:
        cmsExtra = "Preliminary"
    elif mainConfig.forTWIKI:
        cmsExtra = "Unpublished"
    if mainConfig.forPAS:
        latexCMS.DrawLatex(0.15, 0.955, "CMS")
        latexCMSExtra.DrawLatex(0.26, 0.955, "%s" % (cmsExtra))

    else:
        latexCMS.DrawLatex(0.19, 0.89, "CMS")
        latexCMSExtra.DrawLatex(0.19, yLabelPos, "%s" % (cmsExtra))

    if mainConfig.plotRatio:
        ratioPad.cd()
        ratioGraphs = ratios.RatioGraph(datahist,
                                        drawStack.theHistogram,
                                        xMin=mainConfig.plot.firstBin,
                                        xMax=mainConfig.plot.lastBin,
                                        title="Data / MC",
                                        yMin=0.0,
                                        yMax=2,
                                        ndivisions=10,
                                        color=ROOT.kBlack,
                                        adaptiveBinning=0.25)
        if mainConfig.plotSyst:
            ratioGraphs.addErrorByHistograms("Pileup",
                                             stackPileUpUp.theHistogram,
                                             stackPileUpDown.theHistogram,
                                             color=myColors["MyGreen"])
            ratioGraphs.addErrorByHistograms("JES",
                                             stackJESUp.theHistogram,
                                             stackJESDown.theHistogram,
                                             color=myColors["MyGreen"])
            if mainConfig.doTopReweighting:
                ratioGraphs.addErrorByHistograms(
                    "TopWeight",
                    stackReweightUp.theHistogram,
                    stackReweightDown.theHistogram,
                    color=myColors["MyGreen"])
            ratioGraphs.addErrorBySize("Effs",
                                       getattr(rSFOFTrig,
                                               mainConfig.etaRegion).err,
                                       color=myColors["MyGreen"],
                                       add=True)
            ratioGraphs.addErrorByHistograms("Xsecs",
                                             drawStack.theHistogramXsecUp,
                                             drawStack.theHistogramXsecDown,
                                             color=myColors["MyGreen"],
                                             add=True)
            #~ ratioGraphs.addErrorByHistograms( "Theo", drawStack.theHistogramTheoUp, drawStack.theHistogramTheoDown,color=myColors["MyGreen"],add=True)
        ratioGraphs.draw(ROOT.gPad, True, False, True, chi2Pos=0.8)
        if mainConfig.plotSignal and mainConfig.stackSignal:
            signalRatios = []

            legendRatio = TLegend(0.175, 0.725, 0.65, 0.95)
            legendRatio.SetFillStyle(0)
            legendRatio.SetBorderSize(0)
            legendRatio.SetTextFont(42)
            backgroundHist = ROOT.TH1F()
            legendRatio.AddEntry(backgroundHist, "Data / background", "pe")
            temphist = ROOT.TH1F()
            temphist.SetFillColor(myColors["MyGreen"])
            if mainConfig.plotSyst:
                legendRatio.AddEntry(temphist, "Syst. uncert.", "f")
                legendRatio.SetNColumns(2)
            for index, signalhist in enumerate(signalhists):
                signalRatios.append(
                    ratios.RatioGraph(datahist,
                                      signalhist,
                                      xMin=mainConfig.plot.firstBin,
                                      xMax=mainConfig.plot.lastBin,
                                      title="Data / MC",
                                      yMin=0.0,
                                      yMax=2,
                                      ndivisions=10,
                                      color=signalhist.GetLineColor(),
                                      adaptiveBinning=0.25))
                signalRatios[index].draw(ROOT.gPad,
                                         False,
                                         False,
                                         True,
                                         chi2Pos=0.7 - index * 0.1)
                signalhist.SetMarkerColor(signalhist.GetLineColor())
                legendRatio.AddEntry(
                    signalhist,
                    "Data / Background + Signal (%s)" % signalLabels[index],
                    "p")
            legendRatio.Draw("same")

    ROOT.gPad.RedrawAxis()
    plotPad.RedrawAxis()
    if mainConfig.plotRatio:

        ratioPad.RedrawAxis()

    nameModifier = mainConfig.runRange.label + "_" + dilepton
    if mainConfig.doTopReweighting:
        nameModifier += "_TopReweighted"
    if mainConfig.plotData == False:
        nameModifier += "_MCOnly"
    if mainConfig.plotMC == False:
        nameModifier += "_NoBkgMC"

    if mainConfig.plotSignal:
        nameModifier += "_" + signalNameLabel
        if mainConfig.stackSignal:
            nameModifier += "_stackedSignal"

    if mainConfig.normalizeToData:
        hCanvas.Print(
            "fig/DataMC/" + mainConfig.plot.filename %
            ("_scaled_" + nameModifier), )
    elif mainConfig.useTriggerEmulation:
        hCanvas.Print(
            "fig/DataMC/" + mainConfig.plot.filename %
            ("_TriggerEmulation_" + nameModifier), )
    elif mainConfig.DontScaleTrig:
        hCanvas.Print(
            "fig/DataMC/" + mainConfig.plot.filename %
            ("_NoTriggerScaling_" + nameModifier), )
    else:
        hCanvas.Print(
            "fig/DataMC/" + mainConfig.plot.filename % ("_" + nameModifier), )
示例#26
0
def main():
    ## print "hello"
    gROOT.SetBatch(1)
    gStyle.SetPaintTextFormat('1.1f')
    output = TFile('diffratio.root', 'recreate')
    c1 = TCanvas('c1', 'c1', 1600, 1200)
    h_MhpassProfile = []
    h_MhfailProfile = []
    h_MhdifProfile = []
    h_MZppassProfile = []
    h_MZpfailProfile = []
    h_MZpdifProfile = []
    for i in range(4):
        h_MhpassProfile.append(
            TH1F('h_MhpassProfile_' + str(i), 'h_MhpassProfile_' + str(i), 25,
                 100, 150))
        h_MhfailProfile.append(
            TH1F('h_MhfailProfile_' + str(i), 'h_MhfailProfile_' + str(i), 25,
                 100, 150))
        h_MhdifProfile.append(
            TH1F('h_MhdifProfile_' + str(i), 'h_MhdifProfile_' + str(i), 25,
                 100, 150))
        h_MhpassProfile[i].Sumw2()
        h_MhfailProfile[i].Sumw2()
        h_MZppassProfile.append(
            TH1F('h_MZppassProfile_' + str(i), 'h_MZppassProfile_' + str(i),
                 20, 500, 2500))
        h_MZpfailProfile.append(
            TH1F('h_MZpfailProfile_' + str(i), 'h_MZpfailProfile_' + str(i),
                 20, 500, 2500))
        h_MZpdifProfile.append(
            TH1F('h_MZpdifProfile_' + str(i), 'h_MZpdifProfile_' + str(i), 20,
                 500, 2500))
        h_MZppassProfile[i].Sumw2()
        h_MZpfailProfile[i].Sumw2()
    h_pass = TH2F('h_MhMZp_pass', 'h_MhMZp_pass', 25, 100, 150, 20, 500, 2500)
    h_pass.Sumw2()
    h_alphabet = TH2F('h_MhMZp_alphabet', 'h_MhMZp_alphabet', 25, 100, 150, 20,
                      500, 2500)
    h_alphabet.Sumw2()
    h_dif_alphabet = TH2F('h_dif_alphabet', 'h_dif_alphabet', 25, 100, 150, 20,
                          500, 2500)
    h_dif_alphabet.Sumw2()
    h_SB = TH2F('h_MA0hPt_SB', 'h_MA0hPt_SB', 20, 200, 1000, 25, 0,
                500)  ## MA0 vs hPt
    h_SB.Sumw2()
    h_bk = TH2F('h_MA0hPt_bk', 'h_MA0hPt_bk', 20, 200, 1000, 25, 0, 500)
    h_bk.Sumw2()
    h_dif = TH2F('h_dif', 'h_dif', 20, 200, 1000, 25, 0, 500)
    h_dif.Sumw2()
    h_SB_MhPt = TH2F('h_SB_MhPt', 'h_SB_MhPt', 25, 100, 150, 25, 0, 500)
    h_SB_MhPt.Sumw2()
    h_bk_MhPt = TH2F('h_bk_MhPt', 'h_bk_MhPt', 25, 100, 150, 25, 0, 500)
    h_bk_MhPt.Sumw2()
    h_dif_MhPt = TH2F('h_dif_MhPt', 'h_dif_MhPt', 25, 100, 150, 25, 0, 500)
    h_dif_MhPt.Sumw2()
    h_SB_MhMA0 = TH2F('h_SB_MhMA0', 'h_SB_MhMA0', 25, 100, 150, 20, 200, 1000)
    h_SB_MhMA0.Sumw2()
    h_bk_MhMA0 = TH2F('h_bk_MhMA0', 'h_bk_MhMA0', 25, 100, 150, 20, 200, 1000)
    h_bk_MhMA0.Sumw2()
    h_dif_MhMA0 = TH2F('h_dif_MhMA0', 'h_dif_MhMA0', 25, 100, 150, 20, 200,
                       1000)
    h_dif_MhMA0.Sumw2()
    h_SB_Mh = TH1F('h_SB_Mh', 'h_SB_Mh', 25, 100, 150)
    h_SB_Mh.Sumw2()
    h_bk_Mh = TH1F('h_bk_Mh', 'h_bk_Mh', 25, 100, 150)
    h_bk_Mh.Sumw2()
    h_SB_MA0 = TH1F('h_SB_MA0', 'h_SB_MA0', 40, 200, 1000)
    h_SB_MA0.Sumw2()
    h_bk_MA0 = TH1F('h_bk_MA0', 'h_bk_MA0', 40, 200, 1000)
    h_bk_MA0.Sumw2()
    h_SB_MZp = TH1F('h_SB_MZp', 'h_SB_MZp', 20, 500, 2500)
    h_SB_MZp.Sumw2()
    h_bk_MZp = TH1F('h_bk_MZp', 'h_bk_MZp', 20, 500, 2500)
    h_bk_MZp.Sumw2()
    h_SB_hPt = TH1F('h_SB_hPt', 'h_SB_hPt', 50, 0, 500)
    h_SB_hPt.Sumw2()
    h_bk_hPt = TH1F('h_bk_hPt', 'h_bk_hPt', 50, 0, 500)
    h_bk_hPt.Sumw2()
    h_dif_MA0 = TH1F('h_dif_MA0', 'h_dif_MA0', 40, 200, 1000)
    h_dif_MA0.Sumw2()
    h_dif_Mh = TH1F('h_dif_Mh', 'h_dif_Mh', 25, 100, 150)
    h_dif_Mh.Sumw2()
    h_dif_MZp = TH1F('h_dif_MZp', 'h_dif_MZp', 20, 500, 2500)
    h_dif_MZp.Sumw2()
    h_dif_hPt = TH1F('h_dif_hPt', 'h_dif_hPt', 50, 0, 500)
    h_dif_hPt.Sumw2()
    f = TFile('../data/ALLBTagCSV-Run2016_tree.root')
    myTree = f.tree
    # print myTree.GetEntries()
    for en in range(myTree.GetEntries()):
        if (not en % 10000): print en, 'of ', myTree.GetEntries()
        myTree.GetEntry(en)
        # print en, myTree.Mh, myTree.MZp
        #print en, myTree.isTag, myTree.isAntiTag
        if (en >= 1000 and len(sys.argv) >= 2): break
        ## skip signal
        np = int((myTree.MZp - 500) // 500)
        if (myTree.Mh > 136): nq = 3
        elif (myTree.Mh < 114): nq = 0
        if (myTree.isTag and myTree.Mh < 136 and myTree.Mh > 114): continue
        elif (myTree.isTag):
            h_SB.Fill(myTree.MZp, myTree.hPt)
            h_pass.Fill(myTree.Mh, myTree.MZp)
            h_SB_MhPt.Fill(myTree.Mh, myTree.hPt)
            h_SB_MhMA0.Fill(myTree.Mh, myTree.MA0)
            h_SB_MA0.Fill(myTree.MA0)
            h_SB_hPt.Fill(myTree.hPt)
            h_SB_Mh.Fill(myTree.Mh)
            h_SB_MZp.Fill(myTree.MZp)
            if (np < 4 and np >= 0): h_MhpassProfile[np].Fill(myTree.Mh)
            if (nq < 4 and nq >= 0): h_MZppassProfile[nq].Fill(myTree.MZp)
        elif (myTree.isAntiTag):
            weight = R_pass_fail(MhTrans(myTree.Mh), ZpTrans(myTree.MZp))
            if (weight < 0): print 'error', en, weight
            h_bk.Fill(myTree.MZp, myTree.hPt, weight)
            h_alphabet.Fill(myTree.Mh, myTree.MZp, weight)
            h_bk_MhPt.Fill(myTree.Mh, myTree.hPt, weight)
            h_bk_MhMA0.Fill(myTree.Mh, myTree.MA0, weight)
            h_bk_MA0.Fill(myTree.MA0, weight)
            h_bk_Mh.Fill(myTree.Mh, weight)
            h_bk_MZp.Fill(myTree.MZp, weight)
            h_bk_hPt.Fill(myTree.hPt, weight)
            if (np < 4 and np >= 0):
                h_MhfailProfile[np].Fill(myTree.Mh, weight)
            if (nq < 4 and nq >= 0):
                h_MZpfailProfile[nq].Fill(myTree.MZp, weight)

    h_dif_alphabet.Divide(h_alphabet, h_pass)
    h_dif.Divide(h_bk, h_SB)
    h_dif_MA0.Divide(h_bk_MA0, h_SB_MA0)
    h_dif_Mh.Divide(h_bk_Mh, h_SB_Mh)
    h_dif_MZp.Divide(h_bk_MZp, h_SB_MZp)
    h_dif_hPt.Divide(h_bk_hPt, h_SB_hPt)
    h_dif_MhPt.Divide(h_bk_MhPt, h_SB_MhPt)
    h_dif_MhMA0.Divide(h_bk_MhMA0, h_SB_MhMA0)
    for i in range(4):
        h_MhdifProfile[i].Divide(h_MhpassProfile[i], h_MhfailProfile[i])
    for i in range(4):
        h_MZpdifProfile[i].Divide(h_MZppassProfile[i], h_MZpfailProfile[i])
    pdfName = 'diff.pdf'
    c1.Print(pdfName + '[')
    ## gPad.SetLogz()
    h_dif_alphabet.SetMaximum(5.)
    h_dif_alphabet.SetMinimum(0.1)
    h_dif_alphabet.GetZaxis().SetRangeUser(0.1, 10)
    h_dif_alphabet.Draw('colz text')
    c1.Print(pdfName)
    h_dif_alphabet.Draw('surf2z')
    c1.Print(pdfName)
    h_dif.Draw('colz text')
    c1.Print(pdfName)
    h_dif.Draw('surf2z')
    c1.Print(pdfName)
    h_dif_MhPt.Draw('colz text')
    c1.Print(pdfName)
    h_dif_MhPt.Draw('surf2z')
    c1.Print(pdfName)
    h_dif_MhMA0.Draw('colz text')
    c1.Print(pdfName)
    h_dif_MhMA0.Draw('surf2z')
    c1.Print(pdfName)
    gPad.SetLogz(0)
    h_dif_Mh.Draw('e')
    c1.Print(pdfName)
    h_SB_MA0.Draw('e')
    c1.Print(pdfName)
    h_bk_MA0.Draw('e')
    c1.Print(pdfName)
    h_dif_MA0.Draw('e')
    c1.Print(pdfName)
    h_SB_MZp.Draw('e')
    c1.Print(pdfName)
    h_bk_MZp.Draw('e')
    c1.Print(pdfName)
    h_dif_MZp.Draw('e')
    c1.Print(pdfName)
    h_SB_hPt.Draw('e')
    c1.Print(pdfName)
    h_bk_hPt.Draw('e')
    c1.Print(pdfName)
    h_dif_hPt.Draw('e')
    c1.Print(pdfName)
    for i in range(4):
        h_MhdifProfile[i].Draw('e')
        c1.Print(pdfName)

    for i in range(4):
        h_MZpdifProfile[i].Draw('e')
        c1.Print(pdfName)
    output.Write()

    c1.Print(pdfName + ']')
    print "event", h_bk_Mh.Integral(), h_SB_Mh.Integral()
    print "Mh  event ratio: ", h_bk_Mh.Integral() / h_SB_Mh.Integral()
def getObject(filename, key):

    _f = r.TFile(filename)
    _h = _f.Get(key)
    _hcopy = copy.deepcopy(_h)
    _f.Close()

    return _hcopy



if __name__ == "__main__":


    gROOT.ProcessLine('.L ' + WORKPATH + 'include/tdrstyle.C')
    gROOT.SetBatch(1)
    print('WORKPATH: ' + WORKPATH)
    r.setTDRStyle()

    ###########################
    ####   Parser object   ####
    ###########################
    parser = optparse.OptionParser(usage='usage: %prog [opts] FilenameWithSamples', version='%prog 1.0')
    parser.add_option('-t', '--tag', action='store', type=str, dest='tag', default='', help='Output tag')
    (opts, args) = parser.parse_args()



    #####################################
    ####   Construct TEfficiencies   ####
    #####################################
示例#28
0
def drawMultiGraph(mg,
                   title,
                   lt,
                   rt,
                   fname,
                   ymin,
                   ymax,
                   xmin,
                   xmax,
                   log,
                   bl=True):

    #myStyle()
    gROOT.SetBatch(True)
    canvas = ROOT.TCanvas('bla', 'bla', 600, 600)
    canvas.SetLogy(log)
    canvas.SetTicks(1, 1)
    canvas.SetLeftMargin(0.14)
    canvas.SetRightMargin(0.08)
    ROOT.gStyle.SetOptStat(0000000)

    mg.Draw("a3")

    mg.GetXaxis().SetTitleSize(0.035)
    mg.GetYaxis().SetTitleSize(0.035)

    mg.GetXaxis().SetRangeUser(xmin, xmax)

    mg.GetYaxis().SetTitleOffset(1.75)
    mg.GetXaxis().SetTitleOffset(1.35)

    mg.SetMinimum(ymin)
    mg.SetMaximum(ymax)

    if log: ROOT.gPad.SetLogy()

    if bl:
        leg = canvas.BuildLegend(0.55, 0.65, 0.88, 0.86)
        leg.SetFillColor(0)
        leg.SetFillStyle(0)
        leg.SetLineColor(0)
        leg.Draw()

    Text = ROOT.TLatex()

    Text.SetNDC()
    Text.SetTextAlign(31)
    Text.SetTextSize(0.04)

    text = '#it{' + lt + '}'

    Text.DrawLatex(0.90, 0.92, text)

    text = '#bf{#it{' + rt + '}}'

    Text.SetTextAlign(22)
    Text.SetNDC(ROOT.kTRUE)
    Text.SetTextSize(0.04)
    Text.DrawLatex(0.35, 0.78, text)

    canvas.RedrawAxis()
    canvas.Update()
    canvas.GetFrame().SetBorderSize(12)
    canvas.Modified()
    canvas.Update()

    canvas.Print('{}.pdf'.format(fname), 'pdf')
    canvas.Print('{}.png'.format(fname), 'png')
示例#29
0
import argparse
import ROOT
ROOT.PyConfig.IgnoreCommandLineOptions = True

from ROOT import TCanvas, TPad, TH1F, TH1I, THStack, TLegend, TMath, gROOT, TGaxis
import ratios
from setTDRStyle import setTDRStyle
gROOT.SetBatch(True)
from helpers import *
from defs import getPlot, Backgrounds, Backgrounds2016, Backgrounds2018, Signals, Signals2016, Signals2016ADD, Data, Data2016, Data2018, path, plotList, zScale, zScale2016, zScale2018
import math
import os
from copy import copy
import numpy as np
import root_numpy


# Muon sys uncertainty (%)
# as a function of mass
def getMuErr(mass, chann, norm=False):
    lumi = 0.0
    znorm = 0.0
    pileup = 0.0  # we don't use pileup 0.046
    dybkg = 0.0  # 0.07
    #pdf = 0.01*(0.433+0.003291*mass-2.159e-6*mass**2+9.044e-10*mass**3-1.807e-13*mass**4+1.51e-17*mass**5)
    pdf = 0.0

    # muons only next
    if chann: mutrig = 0.003
    else: mutrig = 0.007
    resolution = 0.01
示例#30
0
def main():
    # usage description
    usage = "Example: ./scripts/plotSignificance.py -l logs -f qq --massrange 1200 6000 100"

    # input parameters
    parser = ArgumentParser(description='Script that plots significance for specified mass points',epilog=usage)

    parser.add_argument("-M", "--method", dest="method",
                        choices=['MaxLikelihoodFit'],
                        default='MaxLikelihoodFit',
                        help="Method to calculate upper limits",
                        metavar="METHOD")

    results_group = parser.add_mutually_exclusive_group(required=True)
    results_group.add_argument("-l", "--logs_path", dest="logs_path",
                               help="Path to log files",
                               metavar="LOGS_PATH")
    results_group.add_argument("-r", "--results_file", dest="results_file",
                               help="Path to a file containing results",
                               metavar="RESULTS_FILE")

    parser.add_argument("-f", "--final_state", dest="final_state", required=True,
                        help="Final state (e.g. qq, qg, gg)",
                        metavar="FINAL_STATE")

    parser.add_argument("--postfix", dest="postfix", default='', help="Postfix for the output plot name (default: %(default)s)")

    parser.add_argument("--fileFormat", dest="fileFormat", default='pdf', help="Format of the output plot (default: %(default)s)")

    parser.add_argument("--extraText", dest="extraText", default='Simulation Preliminary', help="Extra text on the plot (default: %(default)s)")

    parser.add_argument("--lumi_sqrtS", dest="lumi_sqrtS", default='1 fb^{-1} (13 TeV)', help="Integrated luminosity and center-of-mass energy (default: %(default)s)")

    parser.add_argument("--printResults", dest="printResults", default=False, action="store_true", help="Print results to the screen")

    mass_group = parser.add_mutually_exclusive_group(required=True)
    mass_group.add_argument("--mass",
                            type=int,
                            nargs = '*',
                            default = 1000,
                            help="Mass can be specified as a single value or a whitespace separated list (default: %(default)i)"
                            )
    mass_group.add_argument("--massrange",
                            type=int,
                            nargs = 3,
                            help="Define a range of masses to be produced. Format: min max step",
                            metavar = ('MIN', 'MAX', 'STEP')
                            )
    mass_group.add_argument("--masslist",
                            help = "List containing mass information"
                            )

    args = parser.parse_args()

    # mass points for which resonance shapes will be produced
    input_masses = []

    if args.massrange != None:
        MIN, MAX, STEP = args.massrange
        input_masses = range(MIN, MAX+STEP, STEP)
    elif args.masslist != None:
        # A mass list was provided
        print "Will create mass list according to", args.masslist
        masslist = __import__(args.masslist.replace(".py",""))
        input_masses = masslist.masses
    else:
        input_masses = args.mass
    # sort masses
    input_masses.sort()

    # arrays holding results
    masses = array('d')
    sig = array('d')
    sig_ex = array('d')
    sig_eyl = array('d')
    sig_eyh = array('d')

    if args.logs_path != None:

        logs_path = os.path.join(os.getcwd(),args.logs_path)

        for mass in input_masses:

            print ">> Reading results for %s resonance with m = %i GeV..."%(args.final_state, int(mass))

            masses.append(mass)

            logName = 'signal_xs_%s_%s_m%i.log'%(args.method,args.final_state,int(mass))

            log_file = open(os.path.join(logs_path,logName),'r')

            # read the log file
            for line in log_file:
                if re.search("^Best fit r:", line):
                  sig.append(float(line.split()[3]))
                  sig_eyl.append(float(line.split()[4].split('/')[0].lstrip('-')))
                  sig_eyh.append(float(line.split()[4].split('/')[1].lstrip('+')))

            sig_ex.append(0.)

            if len(masses) != len(sig):
                print "** WARNING: ** Fit failed for m =", int(mass), "GeV. Setting signal cross section to 0."
                sig.append(0.)
                sig_eyl.append(0.)
                sig_eyh.append(0.)
    else:
        print ">> Importing results..."

        sys.path.insert(0, os.path.dirname(args.results_file))

        results = __import__(os.path.basename(args.results_file).replace(".py",""))

        all_masses = np.array(results.masses)
        indices = []

        # search for indices of input_masses
        for mass in input_masses:
            where = np.where(all_masses==mass)[0]
            if len(where) == 0:
                print "** WARNING: ** Cannot find results for m =", int(mass), "GeV in the provided results file. Skipping this mass point."
            indices.extend( where )

        # sort indices
        indices.sort()

        for i in indices:
            masses.append( results.masses[i] )
            sig.append( results.sig[i] )
            sig_ex.append( results.sig_ex[i] )
            sig_eyl.append( results.sig_eyl[i] )
            sig_eyh.append( results.sig_eyh[i] )


    if args.printResults:
        print "masses =", masses.tolist()
        print "sig =", sig.tolist()
        print "sig_ex =", sig_ex.tolist()
        print "sig_eyl =", sig_eyl.tolist()
        print "sig_eyh =", sig_eyh.tolist()

    # create final arrays
    sig_pos = array('d')
    sig_exl = array('d')
    sig_exh = array('d')

    # fill final arrays
    for i in range(0,len(masses)):
        sig_pos.append(sig[i] if sig[i]>0. else 0.)
        sig_exl.append(sig_ex[i])
        sig_exh.append(sig_ex[i])
        sig_eyl.append(sig_eyl[i] if sig[i]>0. else 0.)
        sig_eyh.append(sig_eyh[i])

    # import ROOT stuff
    from ROOT import kTRUE, kFALSE, gROOT, gStyle, gPad, TGraphAsymmErrors, TCanvas, TLegend
    from ROOT import kGreen, kYellow, kWhite, kRed, kBlue

    gROOT.SetBatch(kTRUE);
    gStyle.SetOptStat(0)
    gStyle.SetOptTitle(0)
    gStyle.SetTitleFont(42, "XYZ")
    gStyle.SetTitleSize(0.06, "XYZ")
    gStyle.SetLabelFont(42, "XYZ")
    gStyle.SetLabelSize(0.05, "XYZ")
    gStyle.SetCanvasBorderMode(0)
    gStyle.SetFrameBorderMode(0)
    gStyle.SetCanvasColor(kWhite)
    gStyle.SetPadTickX(1)
    gStyle.SetPadTickY(1)
    gStyle.SetPadLeftMargin(0.15)
    gStyle.SetPadRightMargin(0.05)
    gStyle.SetPadTopMargin(0.05)
    gStyle.SetPadBottomMargin(0.15)
    gROOT.ForceStyle()

    graph_sig = TGraphAsymmErrors(len(masses),masses,sig_pos,sig_exl,sig_exh,sig_eyl,sig_eyh)
    graph_sig.GetXaxis().SetTitle("%s resonance mass [GeV]"%(args.final_state))
    graph_sig.GetYaxis().SetTitle("Signal cross section [pb]")
    graph_sig.GetYaxis().SetTitleOffset(1.2)
    graph_sig.GetYaxis().SetRangeUser(1e-4,2e2)
    graph_sig.SetMarkerStyle(20)
    graph_sig.SetMarkerColor(1)
    graph_sig.SetLineWidth(2)
    graph_sig.SetLineStyle(1)
    graph_sig.SetLineColor(1)

    c = TCanvas("c", "",800,800)
    c.cd()

    graph_sig.Draw("AP")

    # draw the lumi text on the canvas
    CMS_lumi.extraText = args.extraText
    CMS_lumi.lumi_sqrtS = args.lumi_sqrtS # used with iPeriod = 0 (free form)
    iPos = 11
    iPeriod = 0

    CMS_lumi.CMS_lumi(c, iPeriod, iPos)

    gPad.RedrawAxis()

    c.SetLogy()
    c.SetGridx()
    c.SetGridy()
    fileName = 'signal_xs_%s_%s.%s'%(args.method,args.final_state + ( ('_' + args.postfix) if args.postfix != '' else '' ), args.fileFormat.lower())
    c.SaveAs(fileName)
    print "Plot saved to '%s'"%(fileName)