15 from __future__
import print_function
18 from CoolLumiUtilities.LumiBlobConversion
import bConvertList
64 for i
in range(last+1, n+1):
71 self.
nPar = calibPayload[
'NumOfParameters']
72 self.
fType = calibPayload[
'Function']
73 self.
muToLumi = calibPayload[
'MuToLumi']
74 blob = calibPayload[
'Parameters']
79 print(
'LumiCalibrator.setCalibration for %s found %d parameters: ' % (self.
fType, self.
nPar))
85 polyPos = algname.find(
"_PolyCorr")
86 currPos = algname.find(
"_CurrCorr")
88 if polyPos > 0
and currPos > 0:
89 algname = algname[:-18]
90 elif polyPos > 0
or currPos > 0:
91 algname = algname[:-9]
94 print(
'LumiCalibrator.setCalibration - Found poly: %d curr: %d' % (polyPos, currPos))
99 print(
'LumiCalibrator.setCalibration - reading polynomial from', self.
parVec[-4:-1])
116 print(
'LumiCalibrator.setCalibration - found %d parameters in %s' % (npar, algname))
117 print(
'LumiCalibrator.setCalibration - reading current vers %d from position %d' % (self.
currVersion, npar))
135 elif currPos > polyPos:
143 print(
'LumiCalibrator.setCalibration - found %d parameters in %s' % (npar, algname))
150 print(
'LumiCalibrator.setCalibration - reading current vers %d from position %d' % (self.
currVersion, npar))
170 print(
'LumiCalibrator.setCalibration - reading polynomial from list' , self.
parVec[-4:])
184 if currPayload
is None:
188 self.
currentA = currPayload[
'CurrentSideA']
189 self.
currentC = currPayload[
'CurrentSideC']
198 if self.
fType ==
'Logarithm':
201 elif self.
fType ==
'LookupTable_EventAND_Lin':
213 print(
'LumiCalibrator.calibrate(%f) - non-physical value!' % rawLumi)
217 elif (rawLumi == 0.):
221 calibstr = self.
fType
223 if calibstr.find(
'Polynomial') == 0:
227 elif calibstr.find(
'Logarithm') == 0:
231 elif calibstr.find(
'HitLogarithm') == 0:
235 elif calibstr.find(
'LookupTable_EventAND_Lin') == 0:
239 except Exception
as e:
242 print(
'LumiCalibrator.calibLookupTable(%f) - Error: %s' % (rawLumi, e))
244 elif calibstr.find(
'LookupTable_EventAND_Log') == 0:
248 except Exception
as e:
251 print(
'LumiCalibrator.calibLookupTableLog(%f) - Error: %s' % (rawLumi, e))
253 elif calibstr.find(
'LookupTable_EventANDFull_Log') == 0:
257 except Exception
as e:
260 print(
'LumiCalibrator.calibLookupTableFullLog(%f) - Error: %s' % (rawLumi, e))
262 elif calibstr.find(
'LookupTablePoisson_Lin') == 0:
266 except Exception
as e:
269 print(
'LumiCalibrator.calibLookupTablePoisson(%f) - Error: %s' % (rawLumi, e))
271 elif calibstr.find(
'LookupTableZeroPoisson_Lin') == 0:
275 except Exception
as e:
278 print(
'LumiCalibrator.calibLookupTableZeroPoisson(%f) - Error: %s' % (rawLumi, e))
281 print(
'LumiCalibrator.calibrate(%f) - Unknown calibration type %s!' % (rawLumi, self.
fType))
289 polyPos = calibstr.find(
"_PolyCorr")
290 currPos = calibstr.find(
"_CurrCorr")
293 if polyPos == -1
and currPos == -1:
296 elif polyPos == -1
and currPos > 0:
299 elif polyPos > 0
and currPos == -1:
302 elif polyPos < currPos:
306 elif currPos < polyPos:
311 print(
'LumiCalibrator.calibrate() - I am so confused: %s' % calibstr)
330 print(
'LumiCalibrator.currentCorrection() - unknown current type: %d' % self.
currType)
339 print(
'LumiCalibrator.currentCorrection() - invalid LUCID currents found - A: %f C: %f' % (self.
currentA, self.
currentC))
370 print(
'LumiCalibrator.currentCorrection() - unknown calibration version %d' % self.
currVersion)
373 print(
'LumiCalibrator.currentCorrection() - version %d -> currentA = %f, currentC = %f, correction = %f' % (self.
currVersion, self.
currentA, self.
currentC, correction))
390 print(
'LumiCalibrator.polyCorrection() - unknown calibration version %d' % self.
polyVersion)
401 for i
in range(nrange):
404 if rawLumi < rmax
and rawLumi >= rmin:
406 a.append(self.
parVec[i+4])
407 a.append(self.
parVec[i+5])
408 a.append(self.
parVec[i+6])
409 a.append(self.
parVec[i+7])
410 a.append(self.
parVec[i+8])
412 cal += a[k]*
pow(rawLumi, k)
418 print(
'LumiCalibrator.calibPolynomial(%f) - Value out of range' % rawLumi)
432 cal = -invEff*math.log(1.-rawLumi)
439 print(
'LumiCalibrator.calibLogarithm(%f) - Unphysical input!' % rawLumi)
450 maxRawLumiperBX = self.
parVec[3]
455 if rawLumi > maxRawLumiperBX:
457 print(
'LumiCalibrator.calibHitLogarithm(%f) - input greater than max range %f!' % (rawLumi, maxRawLumiperBX))
461 cal = -invEff*math.log(1.-rawLumi/channels)/(1-offset)
466 print(
'LumiCalibrator.calibHitLogarithm(%f) - Unphysical input!' % rawLumi)
479 print(
'LumiCalibrator.calibLookupTable(%f) - Unphysical input to LUT!' % rawLumi)
499 if (rawLumi < 0.)
or (rawLumi >= 1.):
501 print(
'LumiCalibrator.calibLookupTableLog(%f) - Unphysical input to LUT!' % rawLumi)
529 y = 1 - 2 * math.exp(-a * mu) + math.exp(-b * mu)
530 dy = 2 * a * math.exp(-a * mu) - b * math.exp(-b * mu)
532 munew = mu - (y-rawPerBX)/dy
538 if math.fabs(munew-mu)/munew < 1.e-5:
543 print(
'LumiCalibrator.calibLookupTable(', rawPerBX,
') - did not converge (Vincent method)!')
552 rbxl=self.
rpbx(sr,muvl)
553 rbxu=self.
rpbx(sr,muvu)
554 rbxm=self.
rpbx(sr,muvm)
560 if rawPerBX < rbxl
or rawPerBX > rbxu:
561 print(
'LumiCalibrator.calibLookupTable(', rawPerBX,
') - raw lumi value outside of LUT range', rbxl,
'to', rbxu,
'!')
566 if rbxl<rawPerBX
and rbxm>rawPerBX:
575 rbxm = self.
rpbx(sr, muvm)
577 if (muvu-muvl)/muvl < 1e-5:
582 print(
'LumiCalibrator.calibLookupTable(', rawPerBX,
') - did not converge (Mika method)!')
586 return 1 - 2*math.exp(-(1+sr)*muvis/2) + math.exp(-sr*muvis)
595 if (rawLumi < 0.)
or (rawLumi >= 1.):
597 print(
'LumiCalibrator.calibLookupTableFullLog(%f) - Unphysical input to LUT!' % rawLumi)
628 y = 1 - math.exp(-ra * mu) - math.exp(-rc * mu) + math.exp(-b * mu)
629 dy = ra * math.exp(-ra * mu) + rc * math.exp(-rc * mu) - b * math.exp(-b * mu)
631 munew = mu - (y-rawPerBX)/dy
637 if math.fabs(munew-mu)/munew < 1.e-5:
642 print(
'LumiCalibrator.calibLookupTable(', rawPerBX,
') - did not converge (Vincent method)!')
662 if rawPerBX < rbxl
or rawPerBX > rbxu:
663 print(
'LumiCalibrator.calibLookupTable(', rawPerBX,
') - raw lumi value outside of LUT range', rbxl,
'to', rbxu,
'!')
668 if rbxl<rawPerBX
and rbxm>rawPerBX:
677 rbxm = self.
rpbxFull(ra, rc, sr, muvm)
679 if (muvu-muvl)/muvl < 1e-5:
684 print(
'LumiCalibrator.calibLookupTable(', rawPerBX,
') - did not converge (Mika method)!')
688 return 1 - math.exp(-ra*muvis) - math.exp(-rc*muvis) + math.exp(-sr*muvis)
694 maxRawLumiperBX = self.
parVec[1]
705 if rawLumi > maxRawLumiperBX:
706 print(
'LumiCalibrator.calibLookupTablePoisson(%f) - input greater than max range %f!' % (rawLumi, maxRawLumiperBX))
721 for j
in range(nRefs):
731 munew = mu - (y-rawLumi/math.exp(-mu))/dy
736 print(
'LumiCalibrator.calibLookupTablePoisson(%f) - failed to converge (went negative)!'% (rawLumi))
739 if math.fabs(munew-mu)/munew < 1.e-5:
741 return cal/(1-offset)
744 print(
'LumiCalibrator.calibLookupTablePoisson(%f) - failed to converge (trials)!'% (rawLumi))
745 return cal/(1-offset)
751 maxRawLumiperBX = self.
parVec[1]
762 if rawLumi > maxRawLumiperBX:
763 print(
'LumiCalibrator.calibLookupTableZeroPoisson(%f) - input greater than max range %f!' % (rawLumi, maxRawLumiperBX))
777 for j
in range(nRefs):
787 munew = mu - (y-zRate/math.exp(-mu))/dy
791 print(
'LumiCalibrator.calibLookupTableZeroPoisson(%f) - failed to converge (negative)!'% (rawLumi))
795 if math.fabs(munew-mu)/munew < 1.e-5:
797 return cal/(1-offset)
799 print(
'LumiCalibrator.calibLookupTableZeroPoisson(%f) - failed to converge (trials)!'% (rawLumi))
800 return cal/(1-offset)