Skip to content

Instantly share code, notes, and snippets.

@lukasheinrich
Created April 23, 2018 13:21
Show Gist options
  • Save lukasheinrich/8fc62031f380573428eda03f8db4fa58 to your computer and use it in GitHub Desktop.
Save lukasheinrich/8fc62031f380573428eda03f8db4fa58 to your computer and use it in GitHub Desktop.
diff --git a/pyhf/readxml.py b/pyhf/readxml.py
index 9a3a27f..9ec8bd5 100644
--- a/pyhf/readxml.py
+++ b/pyhf/readxml.py
@@ -1,5 +1,6 @@
import os
import xml.etree.ElementTree as ET
+import numpy as np
def import_root_histogram(rootdir, filename, path, name):
import uproot
@@ -9,24 +10,28 @@ def import_root_histogram(rootdir, filename, path, name):
path = path.strip('/')
f = uproot.open(os.path.join(rootdir, filename))
try:
- return f[name].numpy[0]
+ h = f[name]
+ return h.numpy[0].tolist(), np.sqrt(h.fSumw2[1:-1]).tolist()
except KeyError:
pass
try:
- return f[os.path.join(path, name)].numpy[0]
+ h = f[os.path.join(path, name)]
+ return h.numpy[0].tolist(), np.sqrt(h.fSumw2[1:-1]).tolist()
except KeyError:
pass
raise KeyError('Both {0:s} and {1:s} were tried and not found in {2:s}'.format(name, os.path.join(path, name), os.path.join(rootdir, filename)))
-def process_sample(sample,rootdir,inputfile, histopath):
+def process_sample(sample,rootdir,inputfile, histopath, channelname):
if 'InputFile' in sample.attrib:
inputfile = sample.attrib.get('InputFile')
if 'HistoPath' in sample.attrib:
histopath = sample.attrib.get('HistoPath')
histoname = sample.attrib['HistoName']
+ data,err = import_root_histogram(rootdir, inputfile, histopath, histoname)
+
modifiers = []
for modtag in sample.iter():
if modtag.tag == 'OverallSys':
@@ -43,9 +48,19 @@ def process_sample(sample,rootdir,inputfile, histopath):
'data': None
})
+ if modtag.tag == 'StatError' and modtag.attrib['Activate'] == 'True':
+ if modtag.attrib['HistoName'] == '':
+ modifiers.append({
+ 'name': 'staterror_{}'.format(channelname),
+ 'type': 'staterror',
+ 'data': err
+ })
+ else:
+ raise NotImplementedError
+
return {
'name': sample.attrib['Name'],
- 'data': import_root_histogram(rootdir, inputfile, histopath, histoname).tolist(),
+ 'data': data,
'modifiers': modifiers
}
@@ -56,7 +71,8 @@ def process_data(sample,rootdir,inputfile, histopath):
histopath = sample.attrib.get('HistoPath')
histoname = sample.attrib['HistoName']
- return import_root_histogram(rootdir, inputfile, histopath, histoname)
+ data,_ = import_root_histogram(rootdir, inputfile, histopath, histoname)
+ return data
def process_channel(channelxml,rootdir):
channel = channelxml.getroot()
@@ -69,7 +85,8 @@ def process_channel(channelxml,rootdir):
data = channel.findall('Data')[0]
- return channel.attrib['Name'], process_data(data, rootdir, inputfile, histopath), [process_sample(x, rootdir, inputfile, histopath) for x in samples]
+ channelname = channel.attrib['Name']
+ return channelname, process_data(data, rootdir, inputfile, histopath), [process_sample(x, rootdir, inputfile, histopath, channelname) for x in samples]
def parse(configfile,rootdir):
toplvl = ET.parse(configfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment