Skip to content

Commit

Permalink
ENH: refactor psana*Base clases
Browse files Browse the repository at this point in the history
Idea is to use psanaBase for handling 1 vs 2 differences,
and move all shared vars and funcs into suiteBase
  • Loading branch information
nstelter-slac committed Apr 4, 2024
1 parent 1e8c412 commit b02e721
Show file tree
Hide file tree
Showing 7 changed files with 467 additions and 489 deletions.
81 changes: 81 additions & 0 deletions calibrationSuite/psana1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from calibrationSuite.psanaBase import *
import logging
logger = logging.getLogger(__name__)

def setupPsana1(baseObj):

if baseObj.runRange is None:
baseObj.ds = baseObj.get_ds(baseObj.run)
else:
baseObj.run = baseObj.runRange[0]
baseObj.ds = baseObj.get_ds()

baseObj.det = Detector("%s.0:%s.%d" % (baseObj.location, baseObj.detType, baseObj.camera), baseObj.ds.env())
baseObj.evrs = None
try:
baseObj.wave8 = Detector(baseObj.fluxSource, baseObj.ds.env())
except:
baseObj.wave8 = None
baseObj.config = None
try:
baseObj.controlData = Detector("ControlData")
except:
baseObj.controlData = None

def getFluxPsana1(baseObj, evt):
try:
fluxes = baseObj.wave8.get(evt).peakA()
if fluxes is None:
print("No flux found") ## if baseObj.verbose?
logger.error("No flux found")
return None
f = fluxes[baseObj.fluxChannels].mean() * baseObj.fluxSign
try:
if f < baseObj.fluxCut:
return None
except:
pass
except:
return None
return f

def isKickedPsana1(baseObj, evt):
try:
evr = evt.get(EvrData.DataV4, baseObj.evrs[0])
except:
baseObj.get_evrs()
evr = evt.get(EvrData.DataV4, baseObj.evrs[0])

## kicked = False
## try:
## for ec in evr.fifoEvents():
## if ec.eventCode() == 162:
## return True
kicked = True
try:
for ec in evr.fifoEvents():
if ec.eventCode() == 137:
kicked = False
except:
pass
return kicked

def getEvtPsana1(baseObj, run=None):
oldDs = baseObj.ds
if run is not None:
baseObj.ds = baseObj.get_ds(run)
try: ## or just yield evt I think
evt = next(baseObj.ds.events())
except StopIteration:
baseObj.ds = oldDs
return None
baseObj.ds = oldDs
return evt

def getRawDataPsana1(baseObj, evt, gainBitsMasked=True):
frames = baseObj.det.raw(evt)
if frames is None:
return None
if gainBitsMasked:
return frames & 0x3FFF
return frames
163 changes: 0 additions & 163 deletions calibrationSuite/psana1Base.py

This file was deleted.

89 changes: 89 additions & 0 deletions calibrationSuite/psana2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from calibrationSuite.psanaBase import *
logger = logging.getLogger(__name__)

def setupPsana2(baseObj):

if baseObj.runRange is None:
baseObj.ds = baseObj.get_ds(baseObj.run)
else:
baseObj.run = baseObj.runRange[0]
baseObj.ds = baseObj.get_ds()

baseObj.myrun = next(baseObj.ds.runs())
try:
baseObj.step_value = baseObj.myrun.Detector("step_value")
baseObj.step_docstring = baseObj.myrun.Detector("step_docstring")
except:
baseObj.step_value = baseObj.step_docstring = None

## baseObj.det = Detector('%s.0:%s.%d' %(baseObj.location, baseObj.detType, baseObj.camera), baseObj.ds.env())
## make this less dumb to accomodate epixM etc.
## use a dict etc.
baseObj.det = baseObj.myrun.Detector(baseObj.experimentHash['detectorType'])
if baseObj.det is None:
print("no det object for epixhr, what? Pretend it's ok.")
##raise Exception
## could set to None and reset with first frame I guess, or does the det object know?

baseObj.timing = baseObj.myrun.Detector("timing")
baseObj.desiredCodes = {"120Hz": 272, "4kHz": 273, "5kHz": 274}

try:
baseObj.mfxDg1 = baseObj.myrun.Detector("MfxDg1BmMon")
except:
baseObj.mfxDg1 = None
print("No flux source found") ## if baseObj.verbose?
logger.exception("No flux source found")
try:
baseObj.mfxDg2 = baseObj.myrun.Detector("MfxDg2BmMon")
except:
baseObj.mfxDg2 = None
## fix hardcoding in the fullness of time
baseObj.detEvts = 0
baseObj.flux = None

baseObj.evrs = None
try:
baseObj.wave8 = Detector(baseObj.fluxSource, baseObj.ds.env())
except:
baseObj.wave8 = None
baseObj.config = None
try:
baseObj.controlData = Detector("ControlData")
except:
baseObj.controlData = None

def getEvtPsana2(baseObj):
try:
evt = next(baseObj.myrun.events())
## dumb to do the below everywhere, should best not call this method
##try:
## baseObj.flux = baseObj._getFlux(evt)
##except:
## pass

except StopIteration:
return None
return evt

def getRawDataPsana2(baseObj, evt, gainBitsMasked=True):
frames = baseObj.det.raw.raw(evt)
if frames is None:
return None
if baseObj.special:
if 'thirteenBits' in baseObj.special:
frames = (frames & 0xfffe)
##print("13bits")
elif 'twelveBits' in baseObj.special:
frames = (frames & 0xfffc)
##print("12bits")
elif 'elevenBits' in baseObj.special:
frames = (frames & 0xfff8)
##print("11bits")
elif 'tenBits' in baseObj.special:
frames = (frames & 0xfff0)
##print("10bits")
if gainBitsMasked:
return frames & baseObj.gainBitsMask
return frames

Loading

0 comments on commit b02e721

Please sign in to comment.