Beispiel #1
0
def project(input_file, output_file, binnings):
    if not os.path.isfile(input_file):
        raise RuntimeError('No such file: %s' % input_file)
    root_file = ROOT.TFile.Open(input_file, 'read')
    if not root_file:
        print('Unable to read file %s' % input_file)
        return False
    events = root_file.Get('Events')
    assert (events)

    histograms = []
    for branch_name, binning_array in binnings.items():
        binning = array.array('f', binning_array)
        histogram = ROOT.TH1F(branch_name, branch_name,
                              len(binning) - 1, binning)
        assert (histogram)
        events.Project(branch_name, branch_name)
        histograms.append(histogram)

    out_file = ROOT.TFile.Open(output_file, 'recreate')
    out_file.cd()
    for histogram in histograms:
        histogram.Write()

    out_file.Close()
    root_file.Close()
    return True
Beispiel #2
0
def plot(input_files, output_files, title, legend):
    handle_lhe = Handle('LHEEventProduct')
    label_lhe = ('externalLHEProducer')

    bins = array.array('f', [1. * i for i in range(0, 201)])
    h = ROOT.TH1F(legend, legend, len(bins) - 1, bins)

    for input_file in input_files:
        events = Events(input_file)

        for event in events:
            event.getByLabel(label_lhe, handle_lhe)
            lhe = handle_lhe.product()
            invmass = []

            for status, pdg, mom in zip(lhe.hepeup().ISTUP,
                                        lhe.hepeup().IDUP,
                                        lhe.hepeup().PUP):
                if status == 1 and abs(pdg) in [11, 13, 15]:
                    l = ROOT.TLorentzVector(mom.x[0], mom.x[1], mom.x[2],
                                            mom.x[3])
                    invmass.append(l)

            if len(invmass) == 2:
                h.Fill((invmass[0] + invmass[1]).M())
            else:
                raise RuntimeError(
                    'Did not find exactly 2 but %d LHE leptons in %s' %
                    (len(invmass), input_file))

    c = ROOT.TCanvas('c', 'c')
    c.SetLogy()
    c.SetGrid()
    h.SetTitle(title)
    h.GetXaxis().SetTitle('m_ll [GeV]')
    h.GetYaxis().SetTitle('# events')
    h.Draw()
    for output_file in output_files:
        c.SaveAs(output_file)
    del h
    del c
Beispiel #3
0
      histogram_name_base = 'ttH_{}'.format(ptbin_name)

    histogram_names = [ key.GetName() for key in htxs_dir.GetListOfKeys() if key.GetName().endswith(fitvar) ]
    if not histogram_names:
      raise RuntimeError(
        "Unable to find any histograms ending with '%s' in directory %s/%s of file %s" % \
        (fitvar, evt_dirname, htxs_dirname, input_hadd2_fn)
      )

    for histogram_name in histogram_names:
      histogram_name_new = histogram_name_base
      if histogram_name != fitvar:
        sysname = histogram_name[:-fitvar_len_plus_1]
        if not sysname.endswith(('Down', 'Up')):
          raise RuntimeError("Invalid systematics in histogram name %s" % histogram_name)
        histogram_name_new += '_{}'.format(sysname)
      assert(histogram_name_new not in histograms)
      histograms[histogram_name_new] = htxs_dir.Get(histogram_name)

  output.cd()

  for histogram_name in histograms:
    histogram = ROOT.TH1F()
    histograms[histogram_name].Copy(histogram)
    histogram.SetName(histogram_name)
    histogram.SetTitle(histogram_name)
    histogram.Write()

  output.Close()
  input_hadd2.Close()