def get_best_style(sample): best_pattern = '' for pattern, style_dict in data_styles.iteritems(): log.debug("Checking pattern: %s against %s", pattern, sample) if fnmatch.fnmatch(sample, pattern): log.debug("-> it matches!") if len(pattern) > len(best_pattern): best_pattern = pattern log.info("Found new best style for %s: %s", sample, pattern) if best_pattern: return data_styles[best_pattern] else: return None
def data_views(files, lumifiles): ''' Builds views of files. [files] gives an iterator of .root files with histograms to build. [lumifiles] gives the correspond list of .lumisum files which contain the effective integrated luminosity of the samples. The lumi to normalize to is taken as the sum of the data file int. lumis. ''' files = list(files) log.info("Creating views from %i files", len(files)) # Map sample_name => root file histo_files = dict((extract_sample(x), io.open(x)) for x in files) # Map sample_name => lumi file lumi_files = dict((extract_sample(x), read_lumi(x)) for x in lumifiles) # Identify data files datafiles = set([name for name in histo_files.keys() if 'data' in name]) log.info("Found the following data samples:") log.info(" ".join(datafiles)) datalumi = 0 for x in datafiles: if x not in lumi_files: raise KeyError( "Can't find a lumi file for %s - I have these ones: " % x + repr(lumi_files.keys())) datalumi += lumi_files[x] log.info("-> total int. lumi = %0.0fpb-1", datalumi) # Figure out the dataset for each file, and the int lumi. # Key = dataset name # Value = {intlumi, rootpy file, weight, weighted view} output = {} for sample in histo_files.keys(): raw_file = histo_files[sample] intlumi = lumi_files[sample] log.info("Building sample: %s => int lumi: %0.f pb-1", sample, intlumi) weight = 1 if intlumi: weight = datalumi/intlumi if 'data' in sample: weight = 1 log.debug("Weight: %0.2f", weight) view = views.ScaleView(raw_file, weight) unweighted_view = raw_file # Find the longest (i.e. most specific) matching style pattern best_pattern = '' for pattern, style_dict in data_styles.iteritems(): log.debug("Checking pattern: %s against %s", pattern, sample) if fnmatch.fnmatch(sample, pattern): log.debug("-> it matches!") if len(pattern) > len(best_pattern): best_pattern = pattern log.info("Found new best style for %s: %s", sample, pattern) if best_pattern: style_dict = data_styles[best_pattern] log.info("Found style for %s - applying Style View", sample) # Set style and title # title = the name of the sample, rootpy Legend uses this. nicename = copy.copy(style_dict['name']) view = views.TitleView( views.StyleView(view, **style_dict), nicename ) unweighted_view = views.TitleView( views.StyleView(unweighted_view, **style_dict), nicename ) output[sample] = { 'intlumi': intlumi, 'file' : raw_file, 'weight' : weight, 'view' : view, 'unweighted_view' : unweighted_view } # Merge the data into just 'data' log.info("Merging data together") output['data'] = { 'intlumi' : datalumi, 'weight' : 1, 'view' : views.SumView(*[output[x]['view'] for x in datafiles]), 'unweighted_view' : views.SumView(*[output[x]['unweighted_view'] for x in datafiles]), } return output