diff --git a/calibrationSuite/commonPsanaBase.py b/calibrationSuite/commonPsanaBase.py deleted file mode 100644 index db12ba6..0000000 --- a/calibrationSuite/commonPsanaBase.py +++ /dev/null @@ -1,319 +0,0 @@ -import os -import sys -import logging -import importlib.util -from psana import * -from calibrationSuite.argumentParser import ArgumentParser -## standard -from mpi4py import MPI -import argparse -import numpy as np -import matplotlib.pyplot as plt -from matplotlib.ticker import AutoMinorLocator -import sys -import h5py -from scipy.optimize import curve_fit ## here? -from calibrationSuite.fitFunctions import * -from calibrationSuite.ancillaryMethods import * -from calibrationSuite.argumentParser import ArgumentParser -from calibrationSuite.detectorInfo import DetectorInfo -import os - -logger = logging.getLogger(__name__) - -class CommonPsanaBase(object): - def __init__(self, analysisType="scan"): - - self.comm = MPI.COMM_WORLD - self.rank = self.comm.Get_rank() - self.size = self.comm.Get_size() - - print("in commonPsanaBase") - logger.info("in commonPsanaBase") - - commandUsed = sys.executable + " " + " ".join(sys.argv) - logger.info("Ran with cmd: " + commandUsed) - - self.camera = 0 - self.gainModes = {"FH": 0, "FM": 1, "FL": 2, "AHL-H": 3, "AML-M": 4, "AHL-L": 5, "AML-L": 6} - self.ePix10k_cameraTypes = {1: "Epix10ka", 4: "Epix10kaQuad", 16: "Epix10ka2M"} - self.g0cut = 1 << 14 ## 2023 - self.gainBitsMask = self.g0cut - 1 - - ## for non-120 Hz running - self.nRunCodeEvents = 0 - self.nDaqCodeEvents = 0 - self.nBeamCodeEvents = 0 - self.runCode = 280 - self.daqCode = 281 - self.beamCode = 283 ## per Matt - ##self.beamCode = 281 ## don't see 283... - self.fakeBeamCode = False - - self.ds = None - self.det = None ## do we need multiple dets in an array? or self.secondDet? - - ##self.outputDir = '/sdf/data/lcls/ds/rix/rixx1003721/results/%s/' %(analysisType) - self.outputDir = "../%s/" % (analysisType) - logging.info("output dir: " + self.outputDir) - - self.args = ArgumentParser().parse_args() - logger.info("parsed cmdline args: " + str(self.args)) - - # if the SUITE_CONFIG env var is set use that, otherwise if the cmd line arg is set use that - # if neither are set, use the default 'suiteConfig.py' file - defaultConfigFileName = "suiteConfig.py" - secondaryConfigFileName = defaultConfigFileName if self.args.configFile is None else self.args.configFile - # secondaryConfigFileName is returned if env var not set - configFileName = os.environ.get("SUITE_CONFIG", secondaryConfigFileName) - config = self.importConfigFile(configFileName) - if config is None: - print("\ncould not find or read config file: " + configFileName) - print("please set SUITE_CONFIG env-var or use the '-cf' cmd-line arg to specify a valid config file") - print("exiting...") - sys.exit(1) - self.experimentHash = config.experimentHash - - self.detectorInfo = DetectorInfo(self.experimentHash['detectorType']) - - self.location = self.experimentHash.get("location", None) - self.exp = self.experimentHash.get("exp", None) - print (self.exp) - - self.ROIfileNames = self.experimentHash.get("ROIs", None) - if not self.ROIfileNames: - print("had trouble finding" + str(self.ROIfileNames)) - logger.info("had trouble finding" + str(self.ROIfileNames)) - self.ROIs = [] - for f in self.ROIfileNames: - self.ROIs.append(np.load(f + ".npy")) - self.ROI = None - if len(self.ROIs[0]): - self.ROI = self.ROIs[0] - - self.singlePixels = self.experimentHash.get("singlePixels", None) - - self.sliceEdges = None - self.sliceCoordinates = None - self.regionSlice = self.experimentHash.get("regionSlice", None) - if self.regionSlice is not None: - self.sliceCoordinates = [ - [self.regionSlice[0].start, self.regionSlice[0].stop], - [self.regionSlice[1].start, self.regionSlice[1].stop], - ] - sc = self.sliceCoordinates - self.sliceEdges = [sc[0][1] - sc[0][0], sc[1][1] - sc[1][0]] - - self.fluxSource = self.experimentHash.get("fluxSource", None) - self.fluxChannels = self.experimentHash.get("fluxChannels", range(8, 16) ) - self.fluxSign = self.experimentHash.get("fluxSign", 1) - - self.setValuesFromArgs() - - def setValuesFromArgs(self): - - ## for standalone analysis - self.file = self.args.files - self.label = self.args.label - - ## analyzing xtcs - if self.args.run is not None: - self.run = self.args.run - if self.args.camera is not None: - self.camera = self.args.camera - if self.args.exp is not None: - self.exp = self.args.exp - if self.args.location is not None: - self.location = self.args.location - self.maxNevents = self.args.maxNevents - self.skipNevents = self.args.skipNevents - self.path = self.args.path - - # if set, output folders will be relative to OUTPUT_ROOT - # if not, they will be relative to the current script file - self.outputDir = os.getenv("OUTPUT_ROOT", "") + self.outputDir - # check if outputDir exists, if does not create it and tell user - if not os.path.exists(self.outputDir): - print("could not find output dir: " + self.outputDir) - logger.info("could not find output dir: " + self.outputDir) - print("please create this dir, exiting...") - logger.info("please create this dir, exiting...") - exit(1) - # the following doesnt work with mpi parallelism (other thread could make dir b4 curr thread) - #print("so creating dir: " + self.outputDir) - #logger.info("creating dir: " + self.outputDir) - #os.makedirs(self.outputDir) - # give dir read, write, execute permissions - #os.chmod(self.outputDir, 0o777) - - self.detObj = self.args.detObj - self.threshold = eval(self.args.threshold) if self.args.threshold is not None else None - self.runRange = eval(self.args.runRange) if self.args.runRange is not None else None - self.fluxCut = self.args.fluxCut - - self.fivePedestalRun = self.args.fivePedestalRun ## in case needed - self.fakePedestal = self.args.fakePedestal ## in case needed] - self.fakePedestalFrame = np.load(self.fakePedestal) if self.fakePedestal is not None else None ##cast to uint32??? - - self.g0PedFile = self.args.g0PedFile - self.g0Ped = np.array([np.load(self.args.g0PedFile)]) if self.args.g0PedFile is not None else None - - self.g1PedFile = self.args.g1PedFile - self.g1Ped = np.array([np.load(self.args.g1PedFile)]) if self.args.g1PedFile is not None else None - - self.g0GainFile = self.args.g0GainFile - self.g0Gain = np.load(self.g0GainFile) if self.args.g0GainFile is not None else None - - self.g1GainFile = self.args.g1GainFile - self.g1Gain = np.load(self.g1GainFile) if self.args.g1GainFile is not None else None - - self.offsetFile = self.args.offsetFile - self.offset = np.load(self.offsetFile) if self.args.offsetFile is not None else None - - self.detType = None - if self.args.detType == "": - ## assume epix10k for now - if self.args.nModules is not None: - self.detectorInfo.setNModules(self.args.nModules) - self.detType = self.detectorInfo.getCameraType() - else: - self.detType = self.args.detType - - self.special = self.args.special - - def importConfigFile(self, file_path): - if not os.path.exists(file_path): - print(f"The file '{file_path}' does not exist") - return None - spec = importlib.util.spec_from_file_location("config", file_path) - config_module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(config_module) - return config_module - - def get_config(self): - self.config = self.ds.env().configStore() - - def getImage(self, evt, data=None): - return self.raw.image(evt, data) - - def getFivePedestalRunInfo(self): - ## could do load_txt but would require full path so - if self.det is None: - self.setupPsana() - - evt = self.getEvt(self.fivePedestalRun) - self.fpGains = self.det.gain(evt) - self.fpPedestals = self.det.pedestals(evt) - self.fpStatus = self.det.status(evt) ## does this work? - self.fpRMS = self.det.rms(evt) ## does this work? - - def sortArrayByList(a, data): - return [x for _, x in sorted(zip(a, data), key=lambda pair: pair[0])] - - def setROI(self, roiFile=None, roi=None): - """Call with both file name and roi to save roi to file and use, - just name to load, - just roi to set for current job""" - if roiFile is not None: - if roi is None: - self.ROIfile = roiFile - self.ROI = np.load(roiFile) - return - else: - np.save(roiFile, roi) - self.ROI = roi - - def sliceToDetector(self, sliceRow, sliceCol):## cp from AnalyzeH5: import? - return sliceRow + self.sliceCoordinates[0][0], sliceCol + self.sliceCoordinates[1][0] - - def noCommonModeCorrection(self, frame): - return frame - - def regionCommonModeCorrection(self, frame, region, arbitraryCut=1000): - ## this takes a 2d frame - ## cut keeps photons in common mode - e.g. set to <<1 photon - - regionCM = np.median(frame[region][frame[region] < arbitraryCut]) - return frame - regionCM - - def rowCommonModeCorrection(self, frame, arbitraryCut=1000): - ## this takes a 2d frame - ## cut keeps photons in common mode - e.g. set to <<1 photon - - ##rand = np.random.random() - for r in range(self.detectorInfo.nRows): - colOffset = 0 - ##for b in range(0, self.detNbanks): - for b in range(0, 2): - try: - rowCM = np.median( - frame[r, colOffset : colOffset + self.detColsPerBank][ - frame[r, colOffset : colOffset + self.detColsPerBank] < arbitraryCut - ] - ) - ##if r == 280 and rand > 0.999: - ##print(b, frame[r, colOffset:colOffset + self.detColsPerBank], rowCM, rowCM 0.999: - ##print(frame[r, colOffset:colOffset + self.detColsPerBank], np.median(frame[r, colOffset:colOffset + self.detColsPerBank])) - except: - rowCM = -666 - print("rowCM problem") - logger.error("rowCM problem") - print(frame[r, colOffset : colOffset + self.detColsPerBank]) - colOffset += self.detColsPerBank - return frame - - def colCommonModeCorrection(self, frame, arbitraryCut=1000): - ## this takes a 2d frame - ## cut keeps photons in common mode - e.g. set to <<1 photon - - ##rand = np.random.random() - for c in range(self.detCols): - rowOffset = 0 - for b in range(0, self.detNbanksCol): - ##for b in range(0, 2): - try: - colCM = np.median( - frame[rowOffset : rowOffset + self.detectorInfo.nRowsPerBank, c][ - frame[rowOffset : rowOffset + self.detectorInfo.nRowsPerBank, c] < arbitraryCut - ] - ) - ##if r == 280 and rand > 0.999: - ##print(b, frame[r, colOffset:colOffset + self.detColsPerBank], rowCM, rowCM 0.999: - ##print(frame[r, colOffset:colOffset + self.detColsPerBank], np.median(frame[r, colOffset:colOffset + self.detColsPerBank])) - except: - colCM = -666 - print("colCM problem") - logger.error("colCM problem") - print(frame[rowOffset : rowOffset + self.detectorInfo.nRowsPerBank], c) - rowOffset += self.detectorInfo.nRowsPerBank - return frame - - def isBeamEvent(self, evt): - ec = self.getEventCodes(evt) - ##print(ec[280], ec[281], ec[282], ec[283], ec[284], ec[285] ) - if ec[self.runCode]: - self.nRunCodeEvents += 1 - if ec[self.daqCode]: - self.nDaqCodeEvents += 1 - if self.fakeBeamCode: - return True - if ec[self.beamCode]: - self.nBeamCodeEvents += 1 - return True - return False - - def dumpEventCodeStatistics(self): - print( - "have counted %d run triggers, %d DAQ triggers, %d beam events" - % (self.nRunCodeEvents, self.nDaqCodeEvents, self.nBeamCodeEvents) - ) - logger.info( - "have counted %d run triggers, %d DAQ triggers, %d beam events" - % (self.nRunCodeEvents, self.nDaqCodeEvents, self.nBeamCodeEvents) - ) \ No newline at end of file diff --git a/calibrationSuite/psana1.py b/calibrationSuite/psana1.py new file mode 100644 index 0000000..a15f65d --- /dev/null +++ b/calibrationSuite/psana1.py @@ -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 diff --git a/calibrationSuite/psana1Base.py b/calibrationSuite/psana1Base.py index 7e9bd98..e69de29 100755 --- a/calibrationSuite/psana1Base.py +++ b/calibrationSuite/psana1Base.py @@ -1,163 +0,0 @@ -############################################################################## -## This file is part of 'SLAC Beamtime Calibration Suite'. -## It is subject to the license terms in the LICENSE.txt file found in the -## top-level directory of this distribution and at: -## https://confluence.slac.stanford.edu/display/ppareg/LICENSE.html. -## No part of 'SLAC Beamtime Calibration Suite', including this file, -## may be copied, modified, propagated, or distributed except according to -## the terms contained in the LICENSE.txt file. -############################################################################## -from psana import * -from calibrationSuite.commonPsanaBase import * -from PSCalib.NDArrIO import load_txt -import logging -logger = logging.getLogger(__name__) - - -class PsanaBase(CommonPsanaBase): - def __init__(self, analysisType="scan"): - super().__init__(analysisType) - self.psanaType = 1 - print("in psana1Base") - logger.info("in psana1Base") - - - def get_ds(self, run=None): - if run is None: - run = self.run - return DataSource("exp=%s:run=%d:smd" % (self.exp, run)) - - def setupPsana(self): - logger.info("have built basic script class, exp %s run %d" % (self.exp, self.run)) - - if self.runRange is None: - self.ds = self.get_ds(self.run) - else: - self.run = self.runRange[0] - self.ds = self.get_ds() - - self.det = Detector("%s.0:%s.%d" % (self.location, self.detType, self.camera), self.ds.env()) - self.evrs = None - try: - self.wave8 = Detector(self.fluxSource, self.ds.env()) - except: - self.wave8 = None - self.config = None - try: - self.controlData = Detector("ControlData") - except: - self.controlData = None - - - def getEvt(self, run=None): - oldDs = self.ds - if run is not None: - self.ds = self.get_ds(run) - try: ## or just yield evt I think - evt = next(self.ds.events()) - except StopIteration: - self.ds = oldDs - return None - self.ds = oldDs - return evt - - def getEvtFromRunsTooSmartForMyOwnGood(self): - for r in self.runRange: - self.run = r - self.ds = self.get_ds() - try: - evt = next(self.ds.events()) - yield evt - except: - continue - - def getEvtFromRuns(self): - try: ## can't get yield to work - evt = next(self.ds.events()) - return evt - except StopIteration: - i = self.runRange.index(self.run) - try: - self.run = self.runRange[i + 1] - print("switching to run %d" % (self.run)) - logger.info("switching to run %d" % (self.run)) - self.ds = self.get_ds(self.run) - except: - print("have run out of new runs") - logger.exception("have run out of new runs") - return None - ##print("get event from new run") - evt = next(self.ds.events()) - return evt - - def getFlux(self, evt): - try: - fluxes = self.wave8.get(evt).peakA() - if fluxes is None: - print("No flux found") ## if self.verbose? - logger.error("No flux found") - return None - f = fluxes[self.fluxChannels].mean() * self.fluxSign - try: - if f < self.fluxCut: - return None - except: - pass - except: - return None - return f - - def get_evrs(self): - if self.config is None: - self.get_config() - - self.evrs = [] - for key in list(self.config.keys()): - if key.type() == EvrData.ConfigV7: - self.evrs.append(key.src()) - - def isKicked(self, evt): - try: - evr = evt.get(EvrData.DataV4, self.evrs[0]) - except: - self.get_evrs() - evr = evt.get(EvrData.DataV4, self.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 getScanValue(self, foo): - return self.controlData().pvControls()[0].value() - - def getStepGen(self): - return self.ds.steps() - - def getRawData(self, evt, gainBitsMasked=True): - frames = self.det.raw(evt) - if frames is None: - return None - if gainBitsMasked: - return frames & 0x3FFF - return frames - - def getCalibData(self, evt): - return self.det.calib(evt) - - -if __name__ == "__main__": - bSS = BasicSuiteScript() - print("have built a BasicSuiteScript") - bSS.setupPsana() - evt = bSS.getEvt() - print(dir(evt)) diff --git a/calibrationSuite/psana2.py b/calibrationSuite/psana2.py new file mode 100644 index 0000000..e641576 --- /dev/null +++ b/calibrationSuite/psana2.py @@ -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 + diff --git a/calibrationSuite/psana2Base.py b/calibrationSuite/psana2Base.py deleted file mode 100755 index 3a00009..0000000 --- a/calibrationSuite/psana2Base.py +++ /dev/null @@ -1,290 +0,0 @@ -############################################################################## -## This file is part of 'SLAC Beamtime Calibration Suite'. -## It is subject to the license terms in the LICENSE.txt file found in the -## top-level directory of this distribution and at: -## https://confluence.slac.stanford.edu/display/ppareg/LICENSE.html. -## No part of 'SLAC Beamtime Calibration Suite', including this file, -## may be copied, modified, propagated, or distributed except according to -## the terms contained in the LICENSE.txt file. -############################################################################## -from psana import * -from calibrationSuite.commonPsanaBase import * -import logging -## for parallelism -import os -##from PSCalib.NDArrIO import load_txt - -os.environ["PS_SMD_N_EVENTS"] = "50" -os.environ["PS_SRV_NODES"] = "1" -## psana2 only - - -logger = logging.getLogger(__name__) - - -class PsanaBase(CommonPsanaBase): - def __init__(self, analysisType="scan"): - super().__init__(analysisType) - self.psanaType = 2 - print("in psana2Base") - logger.info("in psana2Base") - - self.allowed_timestamp_mismatch = 1000 - - ##self.setupPsana() - - def get_ds(self, run=None): - if run is None: - run = self.run - return DataSource(exp=self.exp, run=run, intg_det=self.experimentHash['detectorType'], max_events=self.maxNevents) - - def setupPsana(self): - ##print("have built basic script class, exp %s run %d" %(self.exp, self.run)) - if self.runRange is None: - print('a') - self.ds = self.get_ds(self.run) - else: - print('b') - self.run = self.runRange[0] - self.ds = self.get_ds() - - self.myrun = next(self.ds.runs()) - try: - self.step_value = self.myrun.Detector("step_value") - self.step_docstring = self.myrun.Detector("step_docstring") - except: - self.step_value = self.step_docstring = None - - ## self.det = Detector('%s.0:%s.%d' %(self.location, self.detType, self.camera), self.ds.env()) - ## make this less dumb to accomodate epixM etc. - ## use a dict etc. - self.det = self.myrun.Detector(self.experimentHash['detectorType']) - if self.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? - - self.timing = self.myrun.Detector("timing") - self.desiredCodes = {"120Hz": 272, "4kHz": 273, "5kHz": 274} - - try: - self.mfxDg1 = self.myrun.Detector("MfxDg1BmMon") - except: - self.mfxDg1 = None - print("No flux source found") ## if self.verbose? - logger.exception("No flux source found") - try: - self.mfxDg2 = self.myrun.Detector("MfxDg2BmMon") - except: - self.mfxDg2 = None - ## fix hardcoding in the fullness of time - self.detEvts = 0 - self.flux = None - - self.evrs = None - try: - self.wave8 = Detector(self.fluxSource, self.ds.env()) - except: - self.wave8 = None - self.config = None - try: - self.controlData = Detector("ControlData") - except: - self.controlData = None - - ## if self.mfxDg1 is None: - - def getEvtOld(self, run=None): - oldDs = self.ds - if run is not None: - self.ds = self.get_ds(run) - try: ## or just yield evt I think - evt = next(self.ds.events()) - except StopIteration: - self.ds = oldDs - return None - self.ds = oldDs - return evt - - def getNextEvtFromGen(self, gen): - ## this is needed to get flux information out of phase with detector - ## information in mixed lcls1/2 mode - for nevt, evt in enumerate(gen): - try: - self.flux = self._getFlux(evt) - except: - pass - if self.det.raw.raw(evt) is None: - continue - self.detEvts += 1 - ## should check for beam code here to be smarter - return self.detEvts, evt - - def matchedDetEvt(self): - self.fluxTS = 0 - for nevt, evt in enumerate(self.myrun.events()): - ec = self.getEventCodes(evt) - if ec[137]: - self.flux = self._getFlux(evt) ## fix this - self.fluxTS = self.getTimestamp(evt) - continue - elif ec[281]: - self.framesTS = self.getTimestamp(evt) - if self.framesTS - self.fluxTS > self.allowed_timestamp_mismatch: - continue - yield evt - else: - continue - - def getEvtFromRunsTooSmartForMyOwnGood(self): - for r in self.runRange: - self.run = r - self.ds = self.get_ds() - try: - evt = next(self.ds.events()) - yield evt - except: - continue - - def getEvtFromRuns(self): - try: ## can't get yield to work - evt = next(self.ds.events()) - return(evt) - except StopIteration: - i = self.runRange.index(self.run) - try: - self.run = self.runRange[i + 1] - print("switching to run %d" % (self.run)) - logger.info("switching to run %d" % (self.run)) - self.ds = self.get_ds(self.run) - except: - print("have run out of new runs") - logger.exception("have run out of new runs") - return None - ##print("get event from new run") - evt = next(self.ds.events()) - return evt - - def getAllFluxes(self, evt): - if evt is None: - return None - try: - return self.mfxDg1.raw.peakAmplitude(evt) - except: - return None - - def _getFlux(self, evt): - if self.mfxDg1 is None: - return None - - ## f = self.mfxDg1.raw.peakAmplitude(evt)[self.fluxChannels].mean()*self.fluxSign - try: - f = self.mfxDg1.raw.peakAmplitude(evt)[self.fluxChannels].mean() * self.fluxSign - ##print(f) - except Exception as e: - # print(e) - return None - try: - if f < self.fluxCut: - return None - except: - pass - return f - - def getFlux(self, evt): - ##return 1 - return self.flux - - def get_evrs(self): - if self.config is None: - self.get_config() - - self.evrs = [] - for key in list(self.config.keys()): - if key.type() == EvrData.ConfigV7: - self.evrs.append(key.src()) - - def getEventCodes(self, evt): - return self.timing.raw.eventcodes(evt) - - def getPulseId(self, evt): - return self.timing.raw.pulseId(evt) - - def isKicked(self, evt): - allcodes = self.getEventCodes(evt) - ##print(allcodes) - return allcodes[self.desiredCodes["120Hz"]] - - - def getRunGen(self): - return self.ds.runs() - - def getEvt(self): - try: - evt = next(self.myrun.events()) - ## dumb to do the below everywhere, should best not call this method - ##try: - ## self.flux = self._getFlux(evt) - ##except: - ## pass - - except StopIteration: - return None - return evt - - def getScanValue(self, step, useStringInfo=False): - ##print(self.step_value(step),self.step_docstring(step),useStringInfo) - if useStringInfo: - payload = self.step_docstring(step) - ##print(payload) - sv = eval(payload.split()[-1][:-1]) - print("step", int(self.step_value(step)), sv) - logger.info("step" + str(int(self.step_value(step))) + str(sv)) - return sv - return self.step_value(step) - - def getRawData(self, evt, gainBitsMasked=True): - frames = self.det.raw.raw(evt) - if frames is None: - return None - if self.special: - if 'thirteenBits' in self.special: - frames = (frames & 0xfffe) - ##print("13bits") - elif 'twelveBits' in self.special: - frames = (frames & 0xfffc) - ##print("12bits") - elif 'elevenBits' in self.special: - frames = (frames & 0xfff8) - ##print("11bits") - elif 'tenBits' in self.special: - frames = (frames & 0xfff0) - ##print("10bits") - if gainBitsMasked: - return frames & self.gainBitsMask - return frames - - def getCalibData(self, evt): - frames = self.det.raw.calib(evt) - return frames - - def getStepGen(self): - return self.myrun.steps() - - def getTimestamp(self, evt): - return evt.timestamp - - def getPingPongParity(self, frameRegion): - evensEvenRowsOddsOddRows = frameRegion[::2, ::2] + frameRegion[1::2, 1::2] - oddsEvenRowsEvensOddRows = frameRegion[1::2, ::2] + frameRegion[::2, 1::2] - delta = evensEvenRowsOddsOddRows.mean() - oddsEvenRowsEvensOddRows.mean() - ##print("delta:", delta) - return delta > 0 - - -if __name__ == "__main__": - bSS = BasicSuiteScript() - print("have built a BasicSuiteScript") - bSS.setupPsana() - evt = bSS.getEvt() - print(dir(evt)) diff --git a/suite_scripts/TimeScanParallelSlice.py b/suite_scripts/TimeScanParallelSlice.py index 7111c0f..76c6954 100755 --- a/suite_scripts/TimeScanParallelSlice.py +++ b/suite_scripts/TimeScanParallelSlice.py @@ -7,9 +7,7 @@ ## may be copied, modified, propagated, or distributed except according to ## the terms contained in the LICENSE.txt file. ############################################################################## -from calibrationSuite.psana2Base import PsanaBase -#from calibrationSuite.psana1Base import PsanaBase - +from calibrationSuite.psanaBase import PsanaBase import logging import os import sys