Skip to content

Commit

Permalink
Merge pull request #5 from kit-cn-cms/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
leareuter authored Nov 9, 2018
2 parents c89e1f4 + 52b9501 commit 7f4c5d5
Show file tree
Hide file tree
Showing 14 changed files with 1,854 additions and 272 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
# datacardMaker
repo for object-oriented datacard maker

Writes Datacards

Current structure:
- src/datacardMaker.py: Wrapper to automatically create data cards from category/process/systematic objects
- src/categoryObject.py: Object for categories that contains processes
- src/processObject.py: Object for process (e.g. ttH_hbb), contains uncertainties corresponding to the process
- src/systematicObject.py: object for nuisance parameters (e.g. bgnorm_ttbarPlusBBbar)
- base/fileHandler.py: fileHandler class manages all access to files required for the analysis
- base/identificationLogic: identificationLogic class is meant to handle all logic concerning
the key handling for the different categories required in datacards
- base/valueConventions.py: checks if value is allowed for systematic
7 changes: 7 additions & 0 deletions base/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# datacardMaker
repo for object-oriented datacard maker

- base/fileHandler.py: fileHandler class manages all access to files required for the analysis
- base/identificationLogic: identificationLogic class is meant to handle all logic concerning
the key handling for the different categories required in datacards
- base/valueConventions.py: checks if value is allowed for systematic
114 changes: 114 additions & 0 deletions base/fileHandler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
from ROOT import TFile, TH1
from os import path as opath

class fileHandler(object):
"""
The fileHandler class manages all access to files required for the analysis
TODO: Improve memory handling by cleverly closing files
"""
_debug = 200

def init_variables(self):
self._filepath = "/path/to/file"
self._file = None

def __init__(self):
if self._debug >= 10:
print "Initializing fileHandler"
self.init_variables()
if opath.exists(self._filepath):
self._file = self.load_file(filepath)

def __del__(self):
self.close_file()

def is_file_open(self):
if self._debug >= 99:
print "DEBUG: checking for open file"
if not self._file is None and isinstance(self._file, TFile):
return self._file.IsOpen()
if self._debug >= 99:
print "DEBUG: file is not a TFile!"
return False

def close_file(self):
if self._debug >= 99:
print "DEBUG: Closing file if it's open"
if self.is_file_open():
self._file.Close()

def open_file(self, path):
if self._debug >= 99:
print "opening file '%s'" % path
if opath.exists(path):
f = TFile.Open(path)
if self.is_intact_file(f):
self._file = f
self._filepath = path
else:
if self._debug >= 3:
print "Could not open file '%s'" % path
else:
if self._debug >= 3:
print "File '%s' does not exist!" % path
return None

@property
def filepath(self):
return self._filepath
@filepath.setter
def filepath(self, path):
if self._debug >= 20:
print "trying to set file path to", path

self.close_file()
self.open_file(path)

def is_intact_file(self, f):

if f and isinstance(f, TFile):
if f.IsOpen():
if not f.IsZombie():
if not f.TestBit(TFile.kRecovered):
return True
else:
if self._debug >= 99:
print "ERROR: file '%s' is recovered!" % f.GetName()
else:
if self._debug >= 99:
print "ERROR: file '%s' is zombie!" % f.GetName()
else:
if self._debug >= 99:
print "ERROR: file '%s' is not open" % f.GetName()
return False

def load_histogram(self, histname):
if self._debug >= 99:
print "DEBUG: entering 'fileHandler.load_histogram'"
if self._file:
if self._debug >= 10:
print "DEBUG: loading histogram '%s' from '%s'" % (histname, file)
h = self._file.Get(histname)
if isinstance(h, TH1):
return h
else:
print ("WARNING: histogram '%s' does not exist in '%s'"
% (histname, file))
else:
print "ERROR: In fileHandler - file is not set!"
return None

def histogram_exists(self, histname):
h = self.load_histogram(histname = histname)
if self._debug >= 99:
print "found histogram at", h
if h:
return True
return False

def get_integral(self, histname):
h = self.load_histogram(histname = histname)
if h:
return h.Integral()
else:
return -1
Loading

0 comments on commit 7f4c5d5

Please sign in to comment.