-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminuitAverage.py
169 lines (147 loc) · 5.94 KB
/
minuitAverage.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Subclass of clsqAverage to implement least squares averaging
# using minuit
# 5/2012 S Kluth
from AverageTools.clsqAverage import FitAverage
from AverageTools.minuitSolver import minuitSolver
from numpy import matrix
from math import log
class minuitAverage( FitAverage ):
def __init__( self, filename, llognormal=False ):
FitAverage.__init__( self, filename, llognormal )
return
# Used by base class to create the least squares solver
# and run by base class ctor:
def _createSolver( self, gm, parindexmaps, errorkeys,
systerrormatrix, data,
extrapars, extraparerrors, upar,
upnames, mpnames, extraparnames ):
dataparser= self._getDataparser()
covoptions= dataparser.getCovoption()
covm= dataparser.getTotalReducedCovariance()
invm= covm.getI()
ndata= len( data )
npar= len( upar )
self.__npar= npar
nextrapar= len( extrapars )
uparv= matrix( npar*[ 0.0 ] )
uparv.shape= (npar,1)
datav= matrix( data )
datav.shape= (ndata,1)
self.__data= datav
# The minuit fcn with chi^2 with constraint terms
# for correlated systematics
def fcn( n, grad, fval, par, ipar ):
for ipar in range( npar ):
uparv[ipar]= par[ipar]
umpar= gm*uparv
for ival in range( ndata ):
for ierr in parindexmaps.keys():
covopt= covoptions[errorkeys[ierr]]
indexmap= parindexmaps[ierr]
if ival in indexmap.keys():
parindex= indexmap[ival] + npar
term= par[parindex]*systerrormatrix[ierr][ival]
if "r" in covopt:
# umpar[ival]*= 1.0+term/self.__data[ival]
umpar[ival]/= ( 1.0 + term/self.__data[ival] )
else:
umpar[ival]-= term
delta= self.__data - umpar
chisq= delta.getT()*invm*delta
for ipar in range( npar, npar+nextrapar ):
chisq+= par[ipar]**2
# Assign chisq to ctypes variable in new pyroot:
fval.value= chisq
return
# Prepare and create the minuit solver:
pars= upar + extrapars
parerrors= upar + extraparerrors
parnames= upnames + extraparnames
ndof= ndata - npar
solver= minuitSolver( fcn, pars, parerrors, parnames, ndof )
return solver
# Needed for calculation of weights by derivatives of
# solution w.r.t. inputs in base class
def _getSolverData( self ):
return self.__data
def _getAverage( self ):
uparv= FitAverage._getAverage( self )
return uparv[:self.__npar]
class minuitBluecowAverage( FitAverage ):
def __init__( self, filename ):
FitAverage.__init__( self, filename )
return
# Used by base class to create the least squares solver
# and run by base class ctor:
def _createSolver( self, gm, parindexmaps, errorkeys,
systerrormatrix, data,
extrapars, extraparerrors, upar,
upnames, mpnames, extraparnames ):
dataparser= self._getDataparser()
covoptions= dataparser.getCovoption()
covm= dataparser.getTotalReducedCovariance()
invm= covm.getI()
ndata= len( data )
npar= len( upar )
self.__npar= npar
nextrapar= len( extrapars )
uparv= matrix( npar*[ 0.0 ] )
uparv.shape= (npar,1)
datav= matrix( data )
datav.shape= (ndata,1)
self.__data= datav
rvalues= dataparser.getRvalues()
for name in extraparnames:
if name in rvalues:
continue
else:
for key in rvalues.keys():
if key in name:
rvalues[name]= rvalues[key]
continue
#print extraparnames
#print rvalues
# The minuit fcn with chi^2 with constraint terms
# for correlated systematics
def fcn( n, grad, fval, par, ipar ):
for ipar in range( npar ):
uparv[ipar]= par[ipar]
umpar= gm*uparv
for ival in range( ndata ):
for ierr in parindexmaps.keys():
covopt= covoptions[errorkeys[ierr]]
indexmap= parindexmaps[ierr]
if ival in indexmap.keys():
parindex= indexmap[ival] + npar
term= par[parindex]*systerrormatrix[ierr][ival]
if "r" in covopt:
# umpar[ival]*= 1.0+term/self.__data[ival]
umpar[ival]/= ( 1.0 + term/self.__data[ival] )
else:
umpar[ival]-= term
delta= self.__data - umpar
chisq= delta.getT()*invm*delta
for ipar in range( npar, npar+nextrapar ):
errorKey= extraparnames[ipar-npar]
if errorKey in rvalues:
rvalue= rvalues[errorKey]
tworsq= 2.0*rvalue**2
chisq+= (1.0+1.0/tworsq)*log(1.0+tworsq*par[ipar]**2)
else:
chisq+= par[ipar]**2
fval[0]= chisq
return
# Prepare and create the minuit solver:
pars= upar + extrapars
parerrors= upar + extraparerrors
parnames= upnames + extraparnames
ndof= ndata - npar
solver= minuitSolver( fcn, pars, parerrors, parnames, ndof )
return solver
# Needed for calculation of weights by derivatives of
# solution w.r.t. inputs in base class
def _getSolverData( self ):
return self.__data
def _getAverage( self ):
uparv= FitAverage._getAverage( self )
return uparv[:self.__npar]