Exemplo n.º 1
0
    def __init__(self,m_cfg_parser):
        """Make the Data/MC plots."""
        self.m_cfg_parser = m_cfg_parser

        ## -- INIT VARIABLES THAT CAN BE USED IN INHERITED CLASSES -- ##
        ##    ++ These don't require the config file

        ## -- Details for making plots
        self.notforpre    = info.noPrePlot()       # variables that can't be plotted in pre-selection
        self.GeV          = 1000.

        ## -- Initialize uncertainties
        self.unc          = {'stat':{'up':[],'dn':[]},\
                             'syst':{'up':[],'dn':[]},\
                             'total':{'up':[],'dn':[]}}  # recording uncertainties
        self.resid_unc    = {'stat':{'up':[],'dn':[]},\
                             'syst':{'up':[],'dn':[]},\
                             'total':{'up':[],'dn':[]}}  # recording residual uncertainties

        ##  -- Plotting details
        self.timeStamp    = strftime("%d%b%Y",localtime())
        self.draw_ax1_unc = True   # draw uncertainty on the prediction in the main plot
        self.label_size   = 20
        self.atlas_size   = self.label_size+2
        self.leg_txtsize  = self.label_size-4

        ## -- Sample details
        self.physics_samples = info.physicsSamples()
        self.background      = self.physics_samples['backgrounds']

        self.plot_keys       = datamc_dicts.text_dicts() 
Exemplo n.º 2
0
    def book_histos(self,sample_name,nuis_p):
        """
        Initialize histograms for saving systematics information.
        
        @param sample_name   The name of the sample (ttbar, wjets, etc.)
        @param np            The current nuisance parameter of interest
        """
        var_binnings    = datamc_dicts.text_dicts()['bins']
        self.histograms = {sample_name:{nuis_p:{}}}

        for var in self.p_varList:
            binning = var_binnings[var]
            nbins   = len(binning)-1
            h_name  = 'n_{0}_{1}_{2}'.format(sample_name,nuis_p,var)
            h_title = 't_{0}_{1}_{2}'.format(sample_name,nuis_p,var)
            self.histograms[sample_name][nuis_p][var] = ROOT.TH1D(h_name,h_title,nbins,binning)

        return
Exemplo n.º 3
0
def addData(DataMC, r2j_tree, varlist, cfgParser):
    """
    Function that opens a ROOT file, and saves relevant data 
    from the root file in the DataMC_Type objects.
    
    @param DataMC       object that contains the variable information (values, weights)
    @param r2j_tree     ROOT TTree
    @param varlist      list of variables to save
    @param cfgParser    ConfigParser object of settings
    """
    logging.getLogger('share/datamc.log')
    loggingLEVEL = logging.getLogger().getEffectiveLevel() # DEBUG, INFO, ERROR, etc.
    logging.info("")
    logging.info(" -- In addData.py")
    logging.info("  ------------  ")
    logging.info("  Initializing the config file.")

    plot_keys = datamc_dicts.text_dicts() 
    GeV       = 1000.

    logging.info("  Initializing root file and tree ")
    r2j_file = r2j_tree.GetDirectory().GetName().split('/')[-1] # may need this later

    ## -- Configuration -- ##
    p_lep_name       = cfgParser.get('datamc','lepton')       # ex. muel (both muon & electron)
    p_nEvents        = int(cfgParser.get('datamc','nEvents')) # ex. -1
    p_event_selector = cfgParser.get('datamc','eventsel')     # ex. custom file for applying extra event selection
    p_custom_vars    = cfgParser.get('datamc','custom_vars')  # ex. custom file for making variables not in the tree
    p_btag_wkpt      = cfgParser.get('datamc','btag_wkpt')    # ex. 77 (for 77%)
    ## ------------------- ##


    ## -- Load the custom selection script
    if p_event_selector:
        import pyMiniSL.selectionBase as pyEventSel
        evtSel = pyEventSel.SelectionBase(cfgParser,_cutsfile=p_event_selector)
        evtSel.initialize('miniSL')              # setup some of the configurations
        evtSel.execute(r2j_tree,r2j_file)


    ## -- Sort out the loop over entries
    total_entries = r2j_tree.GetEntries()
    if p_nEvents < 0 or p_nEvents > total_entries:
        nEntries = total_entries
    else:
        nEntries  = p_nEvents

    ## -- Actually doing the event loop here
    logging.info("  Begin the event loop ")
    logging.info("  Specified entries: {0}; setting: {1}".format(p_nEvents,nEntries))
    
    ## -- Event Loop
    entry = 0
    while entry < nEntries:

        if not entry%1000 and not p_event_selector:
            # only print if we aren't going to print the entry number elsewhere
            print "       >> Entry {0}".format(entry)

        r2j_tree.GetEntry(entry)

        ## -- custom selection the user may add (e.g., b-tagging)
        passed_selection = True
        if p_event_selector: # only call this function if needed
            passed_selection = evtSel.event_loop(entry)['result']

        if not passed_selection:
            logging.info("  Entry: {0}; ".format(entry))
            logging.info("  Failed custom event selection.")
            entry+=1
            continue

        ## Check the lepton! (syntax to avoid if/else)
        lepton_name = ['mu','el'][(r2j_tree.ejets)]
        logging.info("  Lepton type: {0}".format(lepton_name))

        if p_lep_name!='muel' and p_lep_name!=lepton_name:
            logging.info("  Wrong lepton flavor: want = {0}; got = {1}".format(p_lep_name,lepton_name))
            entry+=1
            continue

        ## Custom variables that aren't branches
        logging.info("  Loading custom variables.")
        custom_vars = custom_variables(r2j_tree,p_custom_vars,None)

        ## Loop over the desired variables
        logging.info("  Looping through variable list ")
        for var in varlist:

            try:
                val = custom_vars[var]
            except KeyError:
                val = getattr(r2j_tree,var)

            ## if not choosing (sub-)leading, then we can plot the attributes of
            ## each object in the event (e.g., all jet pT)
            if "vector" in str(type(val)):
                val = list(val)  

            ## If there are no values in the event, then there's no need to save the SF
            if type(val)==list and not val:
                logging.debug(" Variable {0} is a list and it's empty -- do not continue (issues with SF for no objects)!".format(var))
                logging.debug("  > This probably happened because there aren't any fatjets in this event")
                continue

            ## Get the event weight for scaling MC
            scaleFactor = 1.
            if not 'data' in DataMC.name.lower():
                event_weight  = vlq.compute_weight(r2j_tree,p_btag_wkpt)
                scaleFactor  *= event_weight

            ## Scale by GeV for variables with that unit
            if plot_keys['variables'][var]['gev']:
                val_type = str(type(val))
                if "vector" in val_type or "list" in val_type:
                    val = [i/GeV for i in val]
                else:
                    val = val/GeV

            ## saving the data to plot later
            logging.info("  Exporting data to lists")
            logging.info("  Value  = {0}".format(val))
            logging.info("  Weight = {0}".format(scaleFactor))
            DataMC.varVals[var].append(val)  
            DataMC.scaleFactors[var].append(scaleFactor)
            DataMC.lepNames[var].append(lepton_name)

        entry+=1

    logging.info("  End addData.py")

    return DataMC
Exemplo n.º 4
0
    def init_data(self,f_ind):
        # self.p_variables
        self.data = {}

        self.use_hists = False
        self.use_json  = True
        
        if self.p_plot_type == 'python':
            if self.p_plot1d == 'efficiency':
                self.use_hists = True
            else:
                self.use_json  = True
        elif self.p_plot_type == 'root':
            self.use_hists = True
        else:
            print " UNKNOWN PLOT TYPE. EXITING "
            import sys
            sys.exit(1)


        # putting for loop inside this if statement because I don't want to check it every time
        if self.use_hists:
            import pyDataMC.datamc_dicts as datamc_dicts
            self.outfiles = [ROOT.TFile(of+'.root',"recreate") for of in self.outputfilenames]
            plot_keys     = datamc_dicts.text_dicts()
            n_bins        = 1000
            for eff_c,var in zip(self.eff_conditions,self.p_variables):
                eff_c = re.search(r'\d+',eff_c).group()
                # check for 2d histogram
                if ',' in var:
                    vars       = var.split(',')
                    ttree_vars = []
                    bins       = []
                    for var in vars:
                        ttree_name = self.variablename2ttreename(var)
                        bins.append(array('d',plot_keys['variables'][ttree_name]['bins']))
                    self.data[var] = {'mu':ROOT.TH2D(var+'_mu_name',var+'_mu_title', n_bins,bins[0],n_bins,bins[1] ),\
                                      'el':ROOT.TH2D(var+'_el_name',var+'_el_title', n_bins,bins[0],n_bins,bins[1] )}
                # else it's a 1d histogram
                else:
                    try:
                        bins = np.asarray(plot_keys['variables'][self.p_eff_x_vars[0]]['bins'])
                    except KeyError:
                        v_name     = var.split('_')
                        ttree_name = self.obj2ttree[v_name[0]]+'_'+self.key2attr[v_name[1]]
                        bins       = np.asarray(plot_keys['variables'][ttree_name]['bins'])
                    if self.p_plot1d == 'efficiency':
                        self.data[var+eff_c] = {'el':
                                         {'total':ROOT.TH1D(var+'_el_name_t_'+eff_c,var+'_el_title_t_'+eff_c, n_bins,bins[0],bins[-1] ),\
                                          'good': ROOT.TH1D(var+'_el_name_g_'+eff_c,var+'_el_title_g_'+eff_c, n_bins,bins[0],bins[-1] ),\
                                          'mis':  ROOT.TH1D(var+'_el_name_m_'+eff_c,var+'_el_title_m_'+eff_c, n_bins,bins[0],bins[-1] )},\
                                          'mu':
                                          {'total':ROOT.TH1D(var+'_mu_name_t_'+eff_c,var+'_mu_title_t_'+eff_c, n_bins,bins[0],bins[-1] ),\
                                          'good':  ROOT.TH1D(var+'_mu_name_g_'+eff_c,var+'_mu_title_g_'+eff_c, n_bins,bins[0],bins[-1] ),\
                                          'mis':   ROOT.TH1D(var+'_mu_name_m_'+eff_c,var+'_mu_title_m_'+eff_c, n_bins,bins[0],bins[-1] )}}
                    else:
                        self.data[var] = {'el':ROOT.TH1D(var+'_el_name',var+'_el_title', n_bins,bins ),\
                                          'mu':ROOT.TH1D(var+'_mu_name',var+'_mu_title', n_bins,bins )}
        elif self.use_json:
            self.save_event      = self.save_json
            self.outputfilenames = self.outputfilenames[f_ind]+'.json'
            for var in self.p_variables:
                self.data[var] = {'el':{'value':[],'weight':[]},\
                                  'mu':{'value':[],'weight':[]}} # can support 2d (use [a,b])
        else:
            print " NEED JSON OR ROOT OUTPUT. EXITING "
            sys.exit(1)

        return