-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.py
148 lines (132 loc) · 4.29 KB
/
Utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import array
def getFrom1DHistogram( histo ):
"""
Get histogram content as list.
Excluding under-flow and over-flow bins
"""
data=[]
for bin in range(histo.GetXaxis().GetFirst(),histo.GetXaxis().GetLast()+1):
binctr = histo.GetXaxis().GetBinCenter(bin)
bincon = histo.GetBinContent(bin)
if ( bincon > 0 ):
data+=( [ binctr for e in xrange(0,int(bincon)) ] )
data.sort()
return array.array('d',data)
def getFromTree( branch ):
"""
Get data from Branch input as array of doubles
"""
return array.array('d',branch())
def draw( obj , linecol , opt="" , limits=None , title=None, logx=False):
"""
Draw an histogram of object obj, with specified line color.
obj can be a histogram or a dataset.
"""
if "TH1" in obj.__class__.__name__:
obj.SetLineColor( linecol )
obj.Draw(opt)
hh = obj
else:
from ROOT import TH1F
if logx:
bins = array.array('d')
ratio = limits[2]/limits[1]
for i in xrange(limits[0]):
binlo = limits[1]*pow(ratio, float(i)/float(limits[0]))
bins.append( binlo )
bins.append(limits[2])
hh=TH1F( title , title , limits[0], bins )
else:
hh=TH1F( title , title , limits[0],limits[1],limits[2])
[ hh.Fill( e ) for e in obj ]
hh.SetLineColor( linecol )
hh.DrawCopy(opt)
return hh
def makeResiduals( h1 , h2 ):
"""
Create residuals histogram
Copyied from TH1::Chi2TestX for UU 1D distributions
"""
hres = h1.Clone()
hres.SetName( h1.GetName() + "-Residuals" )
hres.SetTitle( "Residuals" )
hres.Reset()
sum1 = 0
sum2 = 0
for bin in range( h1.GetXaxis().GetFirst() , h1.GetXaxis().GetLast() ):
sum1 += h1.GetBinContent( bin )
sum2 += h2.GetBinContent( bin )
sum = sum1+sum2
from math import sqrt
for bin in range( h1.GetXaxis().GetFirst() , h1.GetXaxis().GetLast() ):
bin1 = h1.GetBinContent( bin )
bin2 = h2.GetBinContent( bin )
binsum= bin1+bin2
if binsum > 0:
nexp1 = binsum*sum1/sum
res = (bin1-nexp1)/sqrt(nexp1)
#Habermann correction for residuals
correc = (1.-sum1/sum)*(1.-binsum/sum)
if correc != 0 :
res /= sqrt(correc)
else:
print("NOT APPLYING Habermann correction for residuals %s"%correc)
hres.SetBinContent( bin, res )
return hres
def makeROOTgof( data1, data2):
"""
Utility function, creates a ROOT's GoFTest class instance
"""
from ROOT import Math
return Math.GoFTest( len(data1) , data1, len(data2), data2)
import logging
class logger:
""" logging class
this is a simple interface to log messages
"""
def __init__(self):
self.FORMAT = '$(asctime)s %(name)s %(levelname)s: %(message)s'
self.LEVEL = logging.WARNING
try:
logging.basicConfig(level=self.LEVEL, foramt = self.FORMAT)
except TypeError: #< 2.5
logging.basicConfig()
l=logging.getLogger()
l.setLevel(self.LEVEL)
def getLogger(self,name):
return logging.getLogger('StatTest.'+name)
class DataType:
BINNED1D = 1
UNBINNED = 2
class Error(Exception):
def __init__(self,msg):
Exception.__init__(self,"StatTest",msg)
self.message=msg
self.logger = logger().getLogger('StatTest')
self.logger.critical(str(self.message))
class WrongDataType(Error):
def __init__(self):
Error.__init__(self,"DataType not recognized")
class BaseClass(Error):
def __init__(self):
Error.__init__(self,"Use derived class")
class NotYet(Error):
def __init__(self):
Error.__init__(self,"Not yet implemented")
#import Interface
#import Tests
def SplitLine(line) :
line = line.lstrip()
wordst = line.split("\t")
# words = []
# for word in wordst :
# if word != "" :
# words.append(word)
wordst = line.split(" ")
words = []
for word in wordst :
wordst2 = word.split("\t")
for word2 in wordst2 :
if word2 != "" :
words.append(word2)
return words