def __init__(self):
        """Initialize whatever is needed"""
        self.my_logger = Logger()
        #self.log = Logger().getLogger(self.__class__.__name__, 10)
        self.log = self.my_logger.getLogger(self.__class__.__name__, 10)
        #}
        self.DEBUG = self.my_logger.is_debug()
        self.pp = pprint.PrettyPrinter(indent=4)

        #initialize RooFit
        gSystem.Load("libHiggsAnalysisCombinedLimit.so")
        self.output_filename = 'worskapce_with_embedded_toys.root'
예제 #2
0
  def __init__(self,name = "plotters_base_functionality" ):
        self.log = Logger().getLogger(self.__class__.__name__, 10)
        self.name = name
        #ROOT.gSystem.AddIncludePath("-I$ROOFITSYS/include/");
        #ROOT.gROOT.ProcessLine(".L tdrstyle.cc")
        #from ROOT import setTDRStyle
        #ROOT.setTDRStyle(True)

        #ROOT.gStyle.SetPalette(1)
        #ROOT.gStyle.SetOptStat(0)
        self.copy_to_web_dir = False
        self.webdir = ""
        self.save_extensions = ['png','pdf','eps']
def prepare_asimov_toy_datasets_for_sync():
    #parseOptions()
    DEBUG = False
    if opt.verbosity != 10:
        os.environ['PYTHON_LOGGER_VERBOSITY'] = str(opt.verbosity)
    if opt.verbosity >= 4:
        DEBUG = True

    #1) RooDataSet with all processes for one sqrts and final state
    #- have RooCategory for sqrts and final state
    #chan  = [1_7, 2_7, 3_7, 1_8,2_8,3_8]
    #cat.defineType(chan[j],j); j=0..6
    #cat.setLabel(chan[j]);
    #2) finally append all toys to same RooDataSet

    log = Logger().getLogger("prepare_toy_datasets_for_sync", 10)

    toy_manager = ToyDataSetManager()
    #toy_manager.set_workspace_path(opt.ws_path)
    #path_to_tree, tree_variables, weight = "1", dataset_name = "my_dataset"):
    #shapes *        ch1_ch1  hzz4l_4muS_7TeV.input.root w:$PROCESS
    #shapes *        ch1_ch2  hzz4l_4eS_7TeV.input.root w:$PROCESS
    #shapes *        ch1_ch3  hzz4l_2e2muS_7TeV.input.root w:$PROCESS
    #shapes *        ch2_ch1  hzz4l_4muS_8TeV.input.root w:$PROCESS
    #shapes *        ch2_ch2  hzz4l_4eS_8TeV.input.root w:$PROCESS
    #shapes *        ch2_ch3  hzz4l_2e2muS_8TeV.input.root w:$PROCESS

    model = "trees_SM"
    #model = "trees_Mix_fa2"
    #model = "trees_Mix_fa3"
    toys_dir = "/afs/cern.ch/user/r/roko/wd_datacards/CreateFullSimToys/{0}".format(
        model)

    #Christoys:
    #toys_dir = "/afs/cern.ch/work/c/chmartin/public/ForEmbedded/Final_v2/"

    #chan_path_dict = {
    #'ch1_ch1' :  "{0}/Toys_CJLSTntuple_7TeV_4mu_*.root/SelectedTree".format(toys_dir),
    #'ch1_ch2' :  "{0}/Toys_CJLSTntuple_7TeV_4e_*.root/SelectedTree".format(toys_dir),
    #'ch1_ch3' :  "{0}/Toys_CJLSTntuple_7TeV_2e2mu_*.root/SelectedTree".format(toys_dir),
    #'ch2_ch1' :  "{0}/Toys_CJLSTntuple_8TeV_4mu_*.root/SelectedTree".format(toys_dir),
    #'ch2_ch2' :  "{0}/Toys_CJLSTntuple_8TeV_4e_*.root/SelectedTree".format(toys_dir),
    #'ch2_ch3' :  "{0}/Toys_CJLSTntuple_8TeV_2e2mu_*.root/SelectedTree".format(toys_dir)
    #}

    chan_path_dict = {
        'ch1_ch1': "{0}/DTree_7TeV_4mu_*.root/selectedEvents".format(toys_dir),
        'ch1_ch2': "{0}/DTree_7TeV_4e_*.root/selectedEvents".format(toys_dir),
        'ch1_ch3':
        "{0}/DTree_7TeV_2e2mu_*.root/selectedEvents".format(toys_dir),
        'ch2_ch1': "{0}/DTree_8TeV_4mu_*.root/selectedEvents".format(toys_dir),
        'ch2_ch2': "{0}/DTree_8TeV_4e_*.root/selectedEvents".format(toys_dir),
        'ch2_ch3':
        "{0}/DTree_8TeV_2e2mu_*.root/selectedEvents".format(toys_dir)
    }

    pp = pprint.PrettyPrinter(indent=4)
    #if DEBUG:
    #log.debug('Initial dictionary:')
    #pp.pprint(chan_path_dict)
    channel_name = sorted(chan_path_dict.keys())
    cat = RooCategory("CMS_channel", "CMS_channel")
    for cat_idx, cat_name in enumerate(channel_name):
        cat.defineType(cat_name, cat_idx)
        cat.setLabel(cat_name)
    if DEBUG:
        log.debug('Category : {0}'.format(cat_idx))
        cat.Print('v')

    my_vars = []
    if ("fa3" in model) and ("fa2" in model):
        my_vars = [
            'D_bkg', 'D_0m', 'D_cp', 'D_0hp', 'D_int', 'Weight', 'mass4l'
        ]
    elif "fa3" in model:
        my_vars = ['D_bkg', 'D_0m', 'D_cp', 'Weight', 'mass4l']
    elif "fa2" in model:
        my_vars = ['D_bkg', 'D_0hp', 'D_int', 'Weight', 'mass4l']
    else:
        my_vars = [
            'D_bkg', 'D_0m', 'D_cp', 'D_0hp', 'D_int', 'Weight', 'mass4l'
        ]

    my_rrv = dict()
    my_rrv['D_bkg'] = RooRealVar('D_bkg', 'D_bkg', 0, 1.)

    my_rrv['D_0m'] = RooRealVar('D_0m', 'D_0m', 0, 1.)
    my_rrv['D_cp'] = RooRealVar('D_cp', 'D_cp', -0.5, 0.5)

    my_rrv['D_0hp'] = RooRealVar('D_0hp', 'D_0hp', 0, 1.)
    my_rrv['D_int'] = RooRealVar('D_int', 'D_int', -0.2, 1)

    my_rrv['mass4l'] = RooRealVar('mass4l', 'mass4l', 100, 1000.)
    my_rrv['Weight'] = RooRealVar('Weight', 'Weight', 1.)

    my_rrv['D_bkg'].setBins(5)
    #my_rrv['D_bkg'].setBins(50)
    my_rrv['D_0m'].setBins(50)
    my_rrv['D_cp'].setBins(50)
    my_rrv['D_0hp'].setBins(50)
    my_rrv['D_int'].setBins(50)

    my_arg_set = RooArgSet()
    for var_name in my_vars:
        #TODO implement check that branch exist
        #my_rrv[var_name] = RooRealVar(var_name,var_name,-999999999,999999999)
        my_arg_set.add(my_rrv[var_name])
    my_arg_set.add(cat)
    if DEBUG:
        log.debug('RooArgSet is now:')
        my_arg_set.Print('v')

    if os.path.exists(opt.output_filename):
        log.debug("Removing file: {0}".format(opt.output_filename))
        os.remove(opt.output_filename)

    import lib.RootHelpers.RootHelperBase as rhb
    root_helper = rhb.RootHelperBase()

    my_arg_set = RooArgSet()
    for var_name in my_vars:
        #TODO implement check that branch exist
        #my_rrv[var_name] = RooRealVar(var_name,var_name,-999999999,999999999)
        my_arg_set.add(my_rrv[var_name])
    my_arg_set.add(cat)
    if DEBUG:
        log.debug('RooArgSet is now:')
        my_arg_set.Print('v')

    #import asimov dataset
    for cat_idx, cat_name in enumerate(channel_name):
        my_path_to_tree = chan_path_dict[cat_name]
        log.debug("my_path_to_tree: {0} ".format(my_path_to_tree))

        the_dataset = toy_manager.get_dataset_from_tree(
            path_to_tree=my_path_to_tree,
            tree_variables=my_vars,
            weight="(mass4l<140.6&&mass4l>105.6)",
            dataset_name="toys/toy_asimov_v0_{0}".format(cat_idx),
            basket=False,
            weight_var_name="Weight")

        the_dataset_with_cat = RooDataSet(
            "toys/toy_asimov_{0}".format(cat_idx),
            "toys/toy_asimov_{0}".format(cat_idx), my_arg_set,
            RooFit.Index(cat), RooFit.Import(cat_name, the_dataset),
            RooFit.WeightVar("Weight"))
        log.debug('RooDataSet the_dataset_with_cat contains {0} events'.format(
            the_dataset_with_cat.sumEntries()))
        the_dataset_with_cat.Print()
        if cat_idx == 0:
            combined_dataset = the_dataset_with_cat
        else:
            combined_dataset.append(the_dataset_with_cat)

    log.debug('RooDataSet combined_dataset contains {0} events'.format(
        combined_dataset.sumEntries()))
    combined_dataset.Print()
    combined_dataset.Print("v")
    #combined_dataset.SetNameTitle("toys/embedded_asimov","toys/embedded_asimov")
    #toy_manager.add_to_basket(combined_dataset)
    toy_manager.add_to_basket(combined_dataset,
                              new_name="toys/embedded_asimov",
                              new_title="toys/embedded_asimov")
    toy_manager.dump_datasets_to_file(opt.output_filename,
                                      'UPDATE')  #this one can receive both
def prepare_toy_datasets_for_sync():
    #parseOptions()
    DEBUG = False
    if opt.verbosity != 10:
        os.environ['PYTHON_LOGGER_VERBOSITY'] = str(opt.verbosity)
    if opt.verbosity >= 4:
        DEBUG = True

    #1) RooDataSet with all processes for one sqrts and final state
    #- have RooCategory for sqrts and final state
    #chan  = [1_7, 2_7, 3_7, 1_8,2_8,3_8]
    #cat.defineType(chan[j],j); j=0..6
    #cat.setLabel(chan[j]);
    #2) finally append all toys to same RooDataSet

    log = Logger().getLogger("prepare_toy_datasets_for_sync", 10)

    toy_manager = ToyDataSetManager()
    #toy_manager.set_workspace_path(opt.ws_path)
    #path_to_tree, tree_variables, weight = "1", dataset_name = "my_dataset"):
    #shapes *        ch1_ch1  hzz4l_4muS_7TeV.input.root w:$PROCESS
    #shapes *        ch1_ch2  hzz4l_4eS_7TeV.input.root w:$PROCESS
    #shapes *        ch1_ch3  hzz4l_2e2muS_7TeV.input.root w:$PROCESS
    #shapes *        ch2_ch1  hzz4l_4muS_8TeV.input.root w:$PROCESS
    #shapes *        ch2_ch2  hzz4l_4eS_8TeV.input.root w:$PROCESS
    #shapes *        ch2_ch3  hzz4l_2e2muS_8TeV.input.root w:$PROCESS

    #model = "trees_v3/bkg_only"
    #model = "trees_v3/bkg_only/zjets"
    #model = "trees_v3/bkg_only/qqZZ"
    #model = "trees_v3/bkg_only/ggZZ"

    model = "trees_v3/SM"
    #model = "trees_v3/Mixed_fa3"
    #model = "trees_v3/Mixed_fa2"

    #model = "trees_v2/SM"
    #model = "trees_v2/Mixed_fa3"
    #model = "trees_v2/Mixed_fa2"

    #model = "trees_v0/SM"
    #model = "trees_v0/Mixed_fa3"
    #model = "trees_v0/Mixed_fa2"

    toys_dir = "/afs/cern.ch/work/r/roko/Stat/CMSSW_611_JCP/src/HZZ4L_Combination/CombinationPy/CreateDatacards/CMSdata/SYNC/{0}".format(
        model)

    #Christoys:
    #toys_dir = "/afs/cern.ch/work/c/chmartin/public/ForEmbedded/Final_v2/"

    chan_path_dict = {
        'ch1_ch1':
        "{0}/Toys_CJLSTntuple_7TeV_4mu_*.root/ToyEvents".format(toys_dir),
        'ch1_ch2':
        "{0}/Toys_CJLSTntuple_7TeV_4e_*.root/ToyEvents".format(toys_dir),
        'ch1_ch3':
        "{0}/Toys_CJLSTntuple_7TeV_2e2mu_*.root/ToyEvents".format(toys_dir),
        'ch2_ch1':
        "{0}/Toys_CJLSTntuple_8TeV_4mu_*.root/ToyEvents".format(toys_dir),
        'ch2_ch2':
        "{0}/Toys_CJLSTntuple_8TeV_4e_*.root/ToyEvents".format(toys_dir),
        'ch2_ch3':
        "{0}/Toys_CJLSTntuple_8TeV_2e2mu_*.root/ToyEvents".format(toys_dir)
    }
    pp = pprint.PrettyPrinter(indent=4)
    #if DEBUG:
    #log.debug('Initial dictionary:')
    #pp.pprint(chan_path_dict)
    channel_name = sorted(chan_path_dict.keys())
    cat = RooCategory("CMS_channel", "CMS_channel")
    for cat_idx, cat_name in enumerate(channel_name):
        cat.defineType(cat_name, cat_idx)
        cat.setLabel(cat_name)
    if DEBUG:
        log.debug('Category : {0}'.format(cat_idx))
        cat.Print('v')

    my_vars = []
    if ("fa3" in model) and ("fa2" in model):
        my_vars = [
            'D_bkg', 'D_0m', 'D_cp', 'D_0hp', 'D_int', 'Weight', 'ZZMass'
        ]
    elif "fa3" in model:
        my_vars = ['D_bkg', 'D_0m', 'D_cp', 'Weight', 'ZZMass']
    elif "fa2" in model:
        my_vars = ['D_bkg', 'D_0hp', 'D_int', 'Weight', 'ZZMass']
    else:
        my_vars = [
            'D_bkg', 'D_0m', 'D_cp', 'D_0hp', 'D_int', 'Weight', 'ZZMass'
        ]

    my_rrv = dict()
    my_rrv['D_bkg'] = RooRealVar('D_bkg', 'D_bkg', 0, 1.)

    my_rrv['D_0m'] = RooRealVar('D_0m', 'D_0m', 0, 1.)
    my_rrv['D_cp'] = RooRealVar('D_cp', 'D_cp', -0.5, 0.5)

    my_rrv['D_0hp'] = RooRealVar('D_0hp', 'D_0hp', 0, 1.)
    my_rrv['D_int'] = RooRealVar('D_int', 'D_int', -0.2, 1)

    my_rrv['ZZMass'] = RooRealVar('ZZMass', 'ZZMass', 100, 1000.)
    my_rrv['Weight'] = RooRealVar('Weight', 'Weight', 1.)

    #my_rrv['D_bkg'].setBins(2)
    my_rrv['D_bkg'].setBins(50)
    my_rrv['D_0m'].setBins(50)
    my_rrv['D_cp'].setBins(50)
    my_rrv['D_0hp'].setBins(50)
    my_rrv['D_int'].setBins(50)

    my_arg_set = RooArgSet()
    for var_name in my_vars:
        #TODO implement check that branch exist
        #my_rrv[var_name] = RooRealVar(var_name,var_name,-999999999,999999999)
        my_arg_set.add(my_rrv[var_name])
    my_arg_set.add(cat)
    if DEBUG:
        log.debug('RooArgSet is now:')
        my_arg_set.Print('v')

    if os.path.exists(opt.output_filename):
        log.debug("Removing file: {0}".format(opt.output_filename))
        os.remove(opt.output_filename)

    ###run on all toys
    #for idx in range(0,1000):  #run on toy numbers
    #log.debug('Running on toy number : {0}'.format(idx))

    ##run on each channel (sqrts+finalstate)
    #for cat_idx, cat_name in enumerate( channel_name ):
    #my_path_to_tree = chan_path_dict[cat_name]
    #log.debug("my_path_to_tree:  {0}".format(my_path_to_tree ))

    #the_dataset = toy_manager.get_dataset_from_tree(path_to_tree = my_path_to_tree, tree_variables=my_vars,
    #weight="ToyNumber=={0} && (ZZMass>105.6 && ZZMass<140.6)".format(idx),
    #dataset_name="toys/toy_{0}_{1}".format(idx,cat_idx), basket = False)
    #the_dataset_with_cat = RooDataSet(the_dataset.GetName(),the_dataset.GetName(), my_arg_set, RooFit.Index(cat),RooFit.Import(cat_name,the_dataset))

    #log.debug('RooDataSet the_dataset_with_cat contains {0} events'.format(the_dataset_with_cat.sumEntries()))
    #if cat_idx==0:
    #combined_dataset = the_dataset_with_cat
    #else:
    #combined_dataset.append(the_dataset_with_cat)

    #log.debug('RooDataSet combined_dataset contains {0} events'.format(combined_dataset.sumEntries()))
    #combined_dataset.Print("v")
    #toy_manager.add_to_basket(combined_dataset, new_name = "toys/comb_{0}".format(idx), new_title = "toys/comb_{0}".format(idx))
    #toy_manager.dump_datasets_to_file(opt.output_filename,'UPDATE')  #this one can receive both

    import lib.RootHelpers.RootHelperBase as rhb
    root_helper = rhb.RootHelperBase()

    my_arg_set = RooArgSet()
    for var_name in my_vars:
        #TODO implement check that branch exist
        #my_rrv[var_name] = RooRealVar(var_name,var_name,-999999999,999999999)
        my_arg_set.add(my_rrv[var_name])
    my_arg_set.add(cat)
    if DEBUG:
        log.debug('RooArgSet is now:')
        my_arg_set.Print('v')

    #import asimov dataset
    for cat_idx, cat_name in enumerate(channel_name):
        my_path_to_tree = chan_path_dict[cat_name]
        log.debug("my_path_to_tree: {0} ".format(my_path_to_tree))

        the_dataset = toy_manager.get_dataset_from_tree(
            path_to_tree=my_path_to_tree,
            tree_variables=my_vars,
            weight="(ZZMass<140.6&&ZZMass>105.6)",
            dataset_name="toys/toy_asimov_v0_{0}".format(cat_idx),
            basket=False,
            weight_var_name="Weight")
        #mass_column = RooFormulaVar("CMS_zz4l_mass","CMS_zz4l_mass", "ZZMass", RooArgList(my_rrv['ZZMass']))
        #the_dataset.addColumn(mass_column)
        #my_arg_set.add(mass_column)

        #my_tree = root_helper.get_TTree(my_path_to_tree, cut = "(ZZMass<140.6&&ZZMass>105.6)")
        #log.debug('Selected tree contains {0} events'.format(my_tree.GetEntries()))
        #the_dataset  =  RooDataSet("toys/toy_asimov_v0_{0}".format(cat_idx), "toys/toy_asimov_v0_{0}".format(cat_idx), my_tree, my_arg_set, "", "Weight")

        the_dataset_with_cat = RooDataSet(
            "toys/toy_asimov_{0}".format(cat_idx),
            "toys/toy_asimov_{0}".format(cat_idx), my_arg_set,
            RooFit.Index(cat), RooFit.Import(cat_name, the_dataset),
            RooFit.WeightVar("Weight"))

        #the_dataset_with_cat = RooDataSet(the_dataset.GetName(),the_dataset.GetName(), my_arg_set, RooFit.Index(cat),RooFit.Import(cat_name,the_dataset))
        log.debug('RooDataSet the_dataset_with_cat contains {0} events'.format(
            the_dataset_with_cat.sumEntries()))
        the_dataset_with_cat.Print()
        if cat_idx == 0:
            combined_dataset = the_dataset_with_cat
        else:
            combined_dataset.append(the_dataset_with_cat)

    log.debug('RooDataSet combined_dataset contains {0} events'.format(
        combined_dataset.sumEntries()))
    combined_dataset.Print()
    combined_dataset.Print("v")
    toy_manager.add_to_basket(combined_dataset,
                              new_name="toys/embedded_asimov",
                              new_title="toys/embedded_asimov")
    toy_manager.dump_datasets_to_file(opt.output_filename,
                                      'UPDATE')  #this one can receive both
예제 #5
0
 def __init__(self):
     self.log = Logger().getLogger(self.__class__.__name__, 10)
     self.pp = pprint.PrettyPrinter(indent=4)
     self.DEBUG = True