Esempio n. 1
0
    def save(self, filename=None, verbose=False, **inkwargs):
        ''' Save the current canvas contents to [filename] '''
        kwargs = {
            'png': True,
            'pdf': True,
            'dotc': False,
            'dotroot': False,
            'json': False
        }
        kwargs.update(self.defaults['save'] if 'save' in self.defaults else {})
        kwargs.update(inkwargs)
        #
        if filename is None:
            filename = self.canvas.name

        #self.pad.Draw()
        self.canvas.Update()
        if not os.path.exists(self.outputdir):
            os.makedirs(self.outputdir)
        if verbose:
            print 'saving ' + os.path.join(self.outputdir, filename) + '.png'
        if kwargs['png']:
            self.canvas.SaveAs(os.path.join(self.outputdir, filename) + '.png')
        if kwargs['pdf']:
            self.canvas.SaveAs(os.path.join(self.outputdir, filename) + '.pdf')
        if kwargs['dotc']:
            self.canvas.SaveAs(os.path.join(self.outputdir, filename) + '.C')
        if kwargs['json']:
            jdict = {}
            for obj in self.keep:
                if isinstance(obj, ROOT.TH1):
                    jdict[obj.GetTitle()] = [
                        obj.GetBinContent(1),
                        obj.GetBinError(1)
                    ]
                if isinstance(obj, ROOT.THStack):
                    jdict['hist_stack'] = {}
                    for i in obj.GetHists():
                        jdict['hist_stack'][i.GetTitle()] = [
                            i.GetBinContent(1),
                            i.GetBinError(1)
                        ]
            with open(os.path.join(self.outputdir, filename) + '.json',
                      'w') as jout:
                jout.write(prettyjson.dumps(jdict))
        if kwargs['dotroot']:
            logging.error('This functionality still has to be implemented '
                          'properly, due to the awsome ROOT "features"')
            rfile = os.path.join(self.outputdir, filename) + '.root'
            with io.root_open(rfile, 'recreate') as tfile:
                self.canvas.Write()
                for obj in self.keep:
                    if isinstance(obj, plotting.HistStack):
                        for hist in obj.hists:
                            hist.Write()
                    obj.Write()

        self.reset()
Esempio n. 2
0
  def save(self, filename=None, verbose=False, **inkwargs):
      ''' Save the current canvas contents to [filename] '''
      kwargs = {
          'png' : True, 'pdf' : True, 'dotc' : False, 
          'dotroot' : False, 'json' : False
          }
      kwargs.update(
          self.defaults['save'] if 'save' in self.defaults else {}
          )
      kwargs.update(
          inkwargs
          )
      #
      if filename is None:
        filename = self.canvas.name
      
      #self.pad.Draw()
      self.canvas.Update()
      if not os.path.exists(self.outputdir):
          os.makedirs(self.outputdir)
      if verbose:
          print 'saving '+os.path.join(self.outputdir, filename) + '.png'
      if kwargs['png']: self.canvas.SaveAs(os.path.join(self.outputdir, filename) + '.png')
      if kwargs['pdf']: self.canvas.SaveAs(os.path.join(self.outputdir, filename) + '.pdf')
      if kwargs['dotc']:
          self.canvas.SaveAs(os.path.join(self.outputdir, filename) + '.C')
      if kwargs['json']:
          jdict = {}
          for obj in self.keep:
              if isinstance(obj, ROOT.TH1):
                  jdict[obj.GetTitle()] = [obj.GetBinContent(1), obj.GetBinError(1)] 
              if isinstance(obj, ROOT.THStack):
                  jdict['hist_stack'] = {}
                  for i in obj.GetHists():
                      jdict['hist_stack'][i.GetTitle()] = [i.GetBinContent(1), i.GetBinError(1)]
          with open(os.path.join(self.outputdir, filename) + '.json', 'w') as jout:
              jout.write(prettyjson.dumps(jdict))
      if kwargs['dotroot']:
          logging.error(
              'This functionality still has to be implemented '
              'properly, due to the awsome ROOT "features"')
          rfile = os.path.join(self.outputdir, filename) + '.root'
          with io.root_open(rfile, 'recreate') as tfile:
              self.canvas.Write()
              for obj in self.keep:
                  if isinstance(obj, plotting.HistStack):
                      for hist in obj.hists:
                          hist.Write()
                  obj.Write()

      self.reset()
Esempio n. 3
0
parser.add_argument(
    '--category',
    default='*',
    help='category to be used for drawing correlation matrix (POSIX regex)')
parser.add_argument('--pickEvery',
                    type=int,
                    default=10,
                    help='pick one event every to draw the correlation matrix')
parser.add_argument('--batch', action='store_true', help='batch mode')

args = parser.parse_args()

args_dict = deepcopy(args.__dict__)
current_file = os.path.abspath(inspect.getfile(inspect.currentframe()))

watermark = TObjString(prettyjson.dumps(args_dict))
codeset = open(current_file).read(
)  #zlib.compress(open(current_file).read()) compressing does not work well with root...
codemark = TObjString(codeset)

#
# 	CORRELATION MATRIX
#

scripts_dir = os.path.join(os.environ['CTRAIN'], 'scripts')
fname_regex = re.compile(
    '[a-zA-Z_0-9]+_(?P<category>[a-zA-Z]+)_(?P<flavor>[A-Z]+)\.root')
qcd_txt_path = os.path.join(scripts_dir, 'data/flat_trees/qcd_flat.list')
input_files = [i.strip() for i in open(qcd_txt_path)]
if args.category != '*':
    input_files = [
Esempio n. 4
0
    #splot, All_tp0_category_classifications[i,valid_disc0_indices], All_tp1_category_classifications[i,valid_disc1_indices]  = plot_data(lda, vars_dist, class_type, class_type_pred, cat_type_list[i][0][3], cat_type_list[i][0][4], cat_type_list[i][1][4], fig_index=i+1)
    #set_trace()

    plot_lda_cov(lda, splot)
    plt.axis('tight')
    #plt.axis([0, 1000, -10, 30])

    plt.ylabel(ytitle)
    #plt.xlabel('$t_{h}$ $p_{T}$ [GeV]')

plt.xlabel('$t_{h}$ $p_{T}$ [GeV]')
#plt.show()

#category_classifications(tp0_category_classifications, tp1_category_classifications)
#category_classifications(All_tp0_category_classifications, All_tp1_category_classifications)

#print 'ttJetsM1000_THadPt_vs_%s_%s_%s_Comparison.png' % (discname, cat_type_list[0][0][3], cat_type_list[1][0][3])
#set_trace()
outname = 'ttJetsM1000_THadPt_vs_%s_%s_%s_Comparison_split' % (
    discname, cat_type_list[0][0][3], cat_type_list[1][0][3])
out_dir = '/home/jdulemba/ROOT/ttbar_reco_3J/plots/Split'
fig.savefig('%s/%s.png' % (out_dir, outname))
#pt.print_table(rows, filename='%s.txt' % outname)

with open('%s/%s.json' % (out_dir, outname), 'w') as f:
    f.write(prettyjson.dumps(json))

print '   %s/%s.png and\n   %s/%s.json created.' % (out_dir, outname, out_dir,
                                                    outname)
#set_trace()
   log.setLevel(rootpy.log.DEBUG)

input_files = [i.strip() for i in open('../data_trees/inputs/%s.list' % args.sample)]

fname_regex = re.compile('[a-zA-Z_0-9\/]*\/?[a-zA-Z_0-9]+_(?P<category>[a-zA-Z]+)_(?P<flavor>[A-Z]+)\.root')
yields = {}

for flavor in flavors:
   yields[flavor] = {}
   for category in sv_categories:
      yields[flavor][category] = {}

for fname in input_files:
   log.info('processing file %s' % fname)
   with io.root_open(fname) as tfile:
      match = fname_regex.match(fname)
      if not match:
         raise ValueError("Could not match the regex to the file %s" % fname)
      flavor = match.group('flavor')
      full_category = match.group('category')
      category = [i for i in sv_categories if i in full_category][0]
      tree = tfile.Get('ttree')
      for cut in itercuts():
         nentries = float(tree.GetEntries(cut))
         if cut not in yields[flavor][category]:
            yields[flavor][category][cut] = 0
         yields[flavor][category][cut] += nentries

with open('../data_trees/%s_yields.json'  % args.sample, 'w') as out:
   out.write(prettyjson.dumps(yields))
import localsettings as site
import features


parser = ArgumentParser()
parser.add_argument('--flavour', default='C', help='the flavour fto be used for drawing the correlation matrix')
parser.add_argument('--category', default='*', help='category to be used for drawing correlation matrix (POSIX regex)')
parser.add_argument('--pickEvery', type=int, default=10, help='pick one event every to draw the correlation matrix')
parser.add_argument('--batch', action='store_true', help='batch mode')

args = parser.parse_args()

args_dict = deepcopy(args.__dict__)
current_file = os.path.abspath(inspect.getfile(inspect.currentframe()))

watermark = TObjString(prettyjson.dumps(args_dict))
codeset = open(current_file).read() #zlib.compress(open(current_file).read()) compressing does not work well with root...
codemark = TObjString(
   codeset
)


#
# 	CORRELATION MATRIX
#



scripts_dir = os.path.join(os.environ['CTRAIN'], 'scripts')
fname_regex = re.compile('[a-zA-Z_0-9]+_(?P<category>[a-zA-Z]+)_(?P<flavor>[A-Z]+)\.root')
qcd_txt_path= os.path.join(scripts_dir, 'data/flat_trees/qcd_flat.list')
Esempio n. 7
0
if args.input:
   jconf = prettyjson.loads(open(args.input[-1]).read())
   file_names = [tuple(i) for i in jconf['file_names']]
   output = jconf['output']
   graph_path = jconf['graph_path']

#dump configuration in json for bookkeeping
jconf = {
   'file_names' : file_names,
   'output' : output,
   'graph_path' : graph_path,
}

jout = output.split('.')[0]
with open('%s.json' % jout, 'w') as out:
   out.write(prettyjson.dumps(jconf))

canvas = plt.Canvas(800, 800)
canvas.SetLogy();
canvas.SetGridx();
canvas.SetGridy();

max_txt_len = max(len(i) for _, i in file_names)
legend = plt.Legend(
   len(file_names), 
   leftmargin=0.18+(30-max_txt_len)*0.016,
   rightmargin=0.005, 
   topmargin=0.60-0.057*(len(file_names)-3), 
   entrysep=0, 
   margin=0.1+0.006*(30-max_txt_len)
)
for imin, imax in zip(mins, maxes):
   offsets.append(-1*imin)
   delta = imax-imin
   scales.append(100./delta if delta else 1.)

for row in X:
   for idx in range(len(row)):
      row[idx] = scales[idx]*(row[idx]+offsets[idx])
      if row[idx] < 0: set_trace()
log.info('... ranking ...')
chi2_ranking  = SelectKBest(chi2, 'all')
chi2_ranking.fit(X, y)
log.info('... done')

anova_scores = zip(variables, anova_ranking.scores_)
anova_scores.sort(key=lambda x: x[1], reverse=True)

chi2_scores = zip(variables, chi2_ranking.scores_)
chi2_scores.sort(key=lambda x: x[1], reverse=True)

scores = [
   {'method' : 'ANOVA', 'scores' : anova_scores}, 
   {'method' : 'chi2', 'scores' : anova_scores}
   ]

if '.json' in args.out:
   with open(args.out, 'w') as out:
      out.write(prettyjson.dumps(scores))
else:
   pickle.dump(scores, open(args.out, "wb"))
Esempio n. 9
0
   log.setLevel(rootpy.log.DEBUG)

input_files = [i.strip() for i in open('data/inputs/%s.list' % args.sample)]

fname_regex = re.compile('[a-zA-Z_0-9\/]*\/?[a-zA-Z_0-9]+_(?P<category>[a-zA-Z]+)_(?P<flavor>[A-Z]+)\.root')
yields = {}

for flavor in flavors:
   yields[flavor] = {}
   for category in sv_categories:
      yields[flavor][category] = {}

for fname in input_files:
   log.info('processing file %s' % fname)
   with io.root_open(fname) as tfile:
      match = fname_regex.match(fname)
      if not match:
         raise ValueError("Could not match the regex to the file %s" % fname)
      flavor = match.group('flavor')
      full_category = match.group('category')
      category = [i for i in sv_categories if i in full_category][0]
      tree = tfile.Get(full_category)
      for cut in itercuts():
         nentries = float(tree.GetEntries(cut))
         if cut not in yields[flavor][category]:
            yields[flavor][category][cut] = 0
         yields[flavor][category][cut] += nentries

with open('data/%s_yields.json'  % args.sample, 'w') as out:
   out.write(prettyjson.dumps(yields))
Esempio n. 10
0
## many, many checks!
for flavour in flavours:
   assert(is_flat(bin_yields(final_qcd[flavour])))
   for bin in bins:
      bias_qcds = [final_qcd[flavour][i][bin] for i in categories]
      ttjs = [ttj_yields[flavour][i][bin] for i in categories]
      assert(ratios_are_ok(bias_qcds, ttjs))

##
##  STORE IN H2D and jsons
##

with open('data/qcd_bin_weights.json', 'w') as out:
   out.write(
      prettyjson.dumps(bin_weight)
      )   

with io.root_open('data/qcd_weights.root', 'w') as out:
   fweights = ROOT.TObjString(prettyjson.dumps(flav_weights))
   out.WriteTObject(fweights, 'flavour_weights')
   for flavour in flavours:
      fdir = out.mkdir(flavour)
      for category in categories:
         cdir = fdir.mkdir(category)
         h2d_category = Hist2D(binning.pt_bins, binning.eta_bins) #category bias weights
         h2d_bin      = Hist2D(binning.pt_bins, binning.eta_bins) #pt/eta bin weights        
         for cat_bin, bin_bin in zip(h2d_category, h2d_bin):
            cat_cut = binning.cut_from_bin(cat_bin)
            bin_cut = binning.cut_from_bin(bin_bin)
            assert(cat_cut == bin_cut)
Esempio n. 11
0
    scales.append(100. / delta if delta else 1.)

for row in X:
    for idx in range(len(row)):
        row[idx] = scales[idx] * (row[idx] + offsets[idx])
        if row[idx] < 0: set_trace()
log.info('... ranking ...')
chi2_ranking = SelectKBest(chi2, 'all')
chi2_ranking.fit(X, y)
log.info('... done')

anova_scores = zip(variables, anova_ranking.scores_)
anova_scores.sort(key=lambda x: x[1], reverse=True)

chi2_scores = zip(variables, chi2_ranking.scores_)
chi2_scores.sort(key=lambda x: x[1], reverse=True)

scores = [{
    'method': 'ANOVA',
    'scores': anova_scores
}, {
    'method': 'chi2',
    'scores': anova_scores
}]

if '.json' in args.out:
    with open(args.out, 'w') as out:
        out.write(prettyjson.dumps(scores))
else:
    pickle.dump(scores, open(args.out, "wb"))
Esempio n. 12
0
if args.input:
    jconf = prettyjson.loads(open(args.input[-1]).read())
    file_names = [tuple(i) for i in jconf['file_names']]
    output = jconf['output']
    graph_path = jconf['graph_path']

#dump configuration in json for bookkeeping
jconf = {
    'file_names': file_names,
    'output': output,
    'graph_path': graph_path,
}

jout = output.split('.')[0]
with open('%s.json' % jout, 'w') as out:
    out.write(prettyjson.dumps(jconf))

canvas = plt.Canvas(800, 800)
canvas.SetLogy()
canvas.SetGridx()
canvas.SetGridy()

max_txt_len = max(len(i) for _, i in file_names)
legend = plt.Legend(len(file_names),
                    leftmargin=0.18 + (30 - max_txt_len) * 0.016,
                    rightmargin=0.005,
                    topmargin=0.60 - 0.057 * (len(file_names) - 3),
                    entrysep=0,
                    margin=0.1 + 0.006 * (30 - max_txt_len))
legend.SetTextSize(legend.GetTextSize() * 0.8)
legend.SetFillColor(0)