diff --git a/calibrationSuite/argumentParser.py b/calibrationSuite/argumentParser.py index b687157..245c0bb 100644 --- a/calibrationSuite/argumentParser.py +++ b/calibrationSuite/argumentParser.py @@ -31,8 +31,10 @@ def __init__(self): self.parser.add_argument("-p", "--path", type=str, help="the base path to the output directory") self.parser.add_argument("-n", "--nModules", type=int, help="nModules") self.parser.add_argument("--analyzedModules", type=str, help="of the form '[0,2,5]'") - self.parser.add_argument("--regionSlice", type=str, help="of the form '[0,8,0,704,0,768]' - six ints for 3d detectors") - + self.parser.add_argument( + "--regionSlice", type=str, help="of the form '[0,8,0,704,0,768]' - six ints for 3d detectors" + ) + self.parser.add_argument( "--mode", type=str, help="detector mode (1d, 2d, ...?" ) ## might be discoverable otherwise diff --git a/calibrationSuite/basicSuiteScript.py b/calibrationSuite/basicSuiteScript.py index 7362816..e61e26d 100755 --- a/calibrationSuite/basicSuiteScript.py +++ b/calibrationSuite/basicSuiteScript.py @@ -95,17 +95,17 @@ def getRawData(self, evt, gainBitsMasked=True, negativeGain=False): frames = self.plainGetRawData(evt) if frames is None: return None - + nZero = frames.size - np.count_nonzero(frames) try: dz = self.nZero - nZero if dz != 0: - print("found %d new zero pixels, expected %d, setting frame to None" %(dz, self.nZero)) + print("found %d new zero pixels, expected %d, setting frame to None" % (dz, self.nZero)) return None - except: + except Exception: self.nZero = nZero - print("Starting with %d zero pixels, will require exactly that many for this run" %(nZero)) - + print("Starting with %d zero pixels, will require exactly that many for this run" % (nZero)) + if False and self.special: ## turned off for a tiny bit of speed if "thirteenBits" in self.special: frames = frames & 0xFFFE diff --git a/calibrationSuite/detectorInfo.py b/calibrationSuite/detectorInfo.py index d6e55e9..7fffac7 100644 --- a/calibrationSuite/detectorInfo.py +++ b/calibrationSuite/detectorInfo.py @@ -39,17 +39,25 @@ def __init__(self, detType, detSubtype="1d", detVersion=0): self.cameraType = None self.dimension = 3 ## suite attempts not to know self.autoRanging = True - - knownTypes = ["epixhr", "epixm", "epix100", "Epix100a", - "jungfrau", "Jungfrau",## cxic00121 has no alias in run 88 - "epix10k", "Epix10ka", "archon"] + + knownTypes = [ + "epixhr", + "epixm", + "epix100", + "Epix100a", + "jungfrau", + "Jungfrau", ## cxic00121 has no alias in run 88 + "epix10k", + "Epix10ka", + "archon", + ] if detType not in knownTypes: raise Exception("type %s not in known types %s" % (detType, str(knownTypes))) self.epix10kCameraTypes = {1: "Epix10ka", 4: "Epix10kaQuad", 16: "Epix10ka2M"} self.jungfrauCameraTypes = {1: "Jungfrau0.5", 2: "Jungfrau1M", 8: "Jungfrau4M"} - def setupDetector(self): ## needs nModules to be set + def setupDetector(self): ## needs nModules to be set if self.detectorType == "epixhr": self.setup_epixhr() elif self.detectorType == "epixm": @@ -58,11 +66,11 @@ def setupDetector(self): ## needs nModules to be set self.setup_epix100() elif self.detectorType == "archon": self.setup_rixsCCD() - elif 'jungfrau' in self.detectorType.lower(): + elif "jungfrau" in self.detectorType.lower(): self.setup_jungfrau() - elif 'epix10k' in self.detectorType.lower(): + elif "epix10k" in self.detectorType.lower(): self.setup_epix10k() - + def setNModules(self, n): self.nModules = n @@ -99,9 +107,9 @@ def setup_epixM(self): self.preferredCommonMode = "rowCommonMode" ## guess self.clusterShape = [3, 3] self.gainMode = None ## may want to know about default, softHigh, softLow - if self.detectorVersion<1: + if self.detectorVersion < 1: self.negativeGain = True - print("N.b: using negative gain for version %d" %(self.detectorVersion)) + print("N.b: using negative gain for version %d" % (self.detectorVersion)) self.aduPerKeV = 666 self.seedCut = 2 self.neighborCut = 0.25 ## ditto @@ -121,10 +129,10 @@ def setup_epix100(self): # need to still implement getGainMode() # self.gainMode = self.getGainMode() self.preferredCommonMode = "regionCommonMode" - self.aduPerKeV = 18. ## approximate + self.aduPerKeV = 18.0 ## approximate self.clusterShape = [3, 3] self.seedCut = 3 - self.neighborCut = 0.5 + self.neighborCut = 0.5 def setup_jungfrau(self): self.cameraType = self.jungfrauCameraTypes[self.nModules] @@ -141,9 +149,9 @@ def setup_jungfrau(self): # self.gainMode = self.getGainMode() self.preferredCommonMode = "regionCommonMode" self.clusterShape = [3, 3] - self.aduPerKeV = 41 ## g0 only of course... + self.aduPerKeV = 41 ## g0 only of course... self.seedCut = 3 - self.neighborCut = 0.5 + self.neighborCut = 0.5 def setup_epix10k(self): self.cameraType = self.epix10kCameraTypes[self.nModules] @@ -158,13 +166,13 @@ def setup_epix10k(self): # self.gainMode = self.getGainMode() self.preferredCommonMode = "colCommonMode" self.clusterShape = [3, 3] - self.aduPerKeV = 16 ## high gain; 5.5 for medium + self.aduPerKeV = 16 ## high gain; 5.5 for medium self.seedCut = 3 - self.neighborCut = 0.5 + self.neighborCut = 0.5 def setup_rixsCCD(self): print("rixsCCD mode:", self.detectorSubtype) - self.cameraType = "rixsCCD" ##+ mode ## psana should support mode + self.cameraType = "rixsCCD" ##+ mode ## psana should support mode self.autoRanging = False self.nTestPixelsPerBank = 36 self.nBanks = 16 @@ -178,5 +186,3 @@ def setup_rixsCCD(self): self.nRows = 1200 self.clusterShape = [3, 5] ## maybe self.g0cut = 1 << 16 - - diff --git a/calibrationSuite/fitFunctions.py b/calibrationSuite/fitFunctions.py index 668e84d..7a805b9 100644 --- a/calibrationSuite/fitFunctions.py +++ b/calibrationSuite/fitFunctions.py @@ -16,7 +16,8 @@ logger = logging.getLogger(__name__) -sqrt2pi = np.sqrt(2*np.pi) +sqrt2pi = np.sqrt(2 * np.pi) + def linear(x, a, b): return a * x + b diff --git a/calibrationSuite/psana1Base.py b/calibrationSuite/psana1Base.py index 9b0db4a..1fbcf71 100755 --- a/calibrationSuite/psana1Base.py +++ b/calibrationSuite/psana1Base.py @@ -44,10 +44,12 @@ def setupPsana(self): self.run = self.runRange[0] self.ds = self.get_ds() -## self.det = psana.Detector("%s.0:%s.%d" % (self.location, self.detType, self.camera), self.ds.env()) - self.det = psana.Detector("%s.0:%s.%d" % (self.location, self.detectorInfo.cameraType, self.camera), self.ds.env()) + ## self.det = psana.Detector("%s.0:%s.%d" % (self.location, self.detType, self.camera), self.ds.env()) + self.det = psana.Detector( + "%s.0:%s.%d" % (self.location, self.detectorInfo.cameraType, self.camera), self.ds.env() + ) ## this is to distinguish between epix10ka form factors, etc. - + self.evrs = None try: self.wave8 = psana.Detector(self.fluxSource, self.ds.env()) @@ -65,13 +67,13 @@ def get_ds(self, run=None): ##return psana.DataSource("exp=%s:run=%d:smd" % (self.exp, run)) return psana.MPIDataSource("exp=%s:run=%d:smd" % (self.exp, run)) - def get_smalldata(self, **kwargs):##, gather_interval=100): + def get_smalldata(self, **kwargs): ##, gather_interval=100): try: - return self.ds.small_data(filename=filename, gather_interval=gather_interval) - except: + return self.ds.small_data(filename=filename, gather_interval=gather_interval) # noqa: F821 + except Exception: print("can't make smalldata - is datasource defined?") return None - + def getEvt(self, run=None): oldDs = self.ds if run is not None: @@ -87,7 +89,7 @@ def getEvt(self, run=None): def getEventCodes(self, evt): ## do something smarter if ever needed return [] - + def getFlux(self, evt): try: fluxes = self.wave8.get(evt).peakA() diff --git a/calibrationSuite/psana2Base.py b/calibrationSuite/psana2Base.py index 1d2b9ae..3bd5720 100755 --- a/calibrationSuite/psana2Base.py +++ b/calibrationSuite/psana2Base.py @@ -115,10 +115,10 @@ def get_ds(self, run=None): ) ##, dir=tmpDir) return ds - def get_smalldata(self, filename):##, gather_interval=100): + def get_smalldata(self, filename): ##, gather_interval=100): try: - return self.ds.smalldata(filename=filename)##, gather_interval=gather_interval) - except: + return self.ds.smalldata(filename=filename) ##, gather_interval=gather_interval) + except Exception: print("can't make smalldata - is datasource defined?") return None @@ -255,14 +255,14 @@ def getTimestamp(self, evt): return evt.timestamp def getUniqueid(self): - return getattr(self.det, 'raw')._uniqueid + return getattr(self.det, "raw")._uniqueid def getPedestal(self, evt, gainMode): ## assumes a dimension for gainmode if self.detectorInfo.autoRanging: return self.det.calibconst["pedestals"][0][gainMode] return self.det.calibconst["pedestals"][0] - + def getPingPongParity(self, frameRegion): evensEvenRowsOddsOddRows = frameRegion[::2, ::2] + frameRegion[1::2, 1::2] oddsEvenRowsEvensOddRows = frameRegion[1::2, ::2] + frameRegion[::2, 1::2] diff --git a/calibrationSuite/psanaCommon.py b/calibrationSuite/psanaCommon.py index 125eef3..add38e7 100755 --- a/calibrationSuite/psanaCommon.py +++ b/calibrationSuite/psanaCommon.py @@ -32,8 +32,8 @@ def __init__(self, analysisType="scan"): print("in psanaCommon") logger.info("in psanaCommon") - self.analysisType = analysisType ## fix below - + self.analysisType = analysisType ## fix below + self.args = ArgumentParser().parse_args() logger.info("parsed cmdline args: " + str(self.args)) @@ -59,6 +59,7 @@ def __init__(self, analysisType="scan"): self.setupFromHashOrCmd() self.setupOutputDirString(analysisType) self.setupConfigHash() + #### Start of setup related functions #### def loadExperimentHashFromConfig(self): @@ -113,20 +114,17 @@ def setupFromExperimentHash(self): try: detVersion = self.experimentHash["detectorVersion"] - except: + except Exception: detVersion = 0 try: self.detectorInfo = DetectorInfo( self.experimentHash["detectorType"], detSubtype=self.experimentHash["detectorSubtype"], - detVersion=detVersion + detVersion=detVersion, ) except Exception: - self.detectorInfo = DetectorInfo( - self.experimentHash["detectorType"], - detVersion=detVersion - ) + self.detectorInfo = DetectorInfo(self.experimentHash["detectorType"], detVersion=detVersion) self.exp = self.experimentHash.get("exp", None) @@ -134,7 +132,6 @@ def setupFromExperimentHash(self): self.singlePixels = self.experimentHash.get("singlePixels", None) - self.regionSlice = self.experimentHash.get("regionSlice", None) ## some code moved to setupFromHashOrCmd self.analyzedModules = self.experimentHash.get("analyzedModules", None) @@ -257,20 +254,28 @@ def setupFromCmdlineArgs(self): else: self.detType = self.args.detType jungfrau = epix10k = False - if 'epix10k' in self.detType.lower(): + if "epix10k" in self.detType.lower(): epix10k = True - elif 'jungfrau' in self.detType.lower(): + elif "jungfrau" in self.detType.lower(): jungfrau = True ## could allow just epix10k or jungfrau + n modules... if epix10k or jungfrau: if self.args.nModules is not None: raise RuntimeError("should not specify exact detector type and n modules") if epix10k: - nModules = [k for k in self.detectorInfo.epix10kCameraTypes.keys() if self.detectorInfo.epix10kCameraTypes[k]==self.detType] + nModules = [ + k + for k in self.detectorInfo.epix10kCameraTypes.keys() + if self.detectorInfo.epix10kCameraTypes[k] == self.detType + ] if jungfrau: - nModules = [k for k in self.detectorInfo.jungfrauCameraTypes.keys() if self.detectorInfo.jungfrauCameraTypes[k]==self.detType] + nModules = [ + k + for k in self.detectorInfo.jungfrauCameraTypes.keys() + if self.detectorInfo.jungfrauCameraTypes[k] == self.detType + ] if nModules == []: - raise RuntimeError("could not determine n modules from detector type %s" %(self.detType)) + raise RuntimeError("could not determine n modules from detector type %s" % (self.detType)) self.detectorInfo.setNModules(nModules[0]) self.detectorInfo.setupDetector() @@ -287,7 +292,7 @@ def setupFromCmdlineArgs(self): regionSliceArray = eval(self.args.regionSlice) if len(regionSliceArray) != 6: raise RuntimeError("expect 6 elements in region slice") - a,b,c,d,e,f = regionSliceArray + a, b, c, d, e, f = regionSliceArray self.regionSlice = np.s_[a:b, c:d, e:f] self.g0cut = self.detectorInfo.g0cut @@ -344,7 +349,7 @@ def setupFromHashOrCmd(self): if self.regionSlice is not None: ## n.b. expects 3d slice definition regardless for consistency if self.detectorInfo.dimension == 3: - offset = 1 + self.offset = 1 self.sliceCoordinates = [ [self.regionSlice[1].start, self.regionSlice[1].stop], [self.regionSlice[2].start, self.regionSlice[2].stop], @@ -352,7 +357,7 @@ def setupFromHashOrCmd(self): sc = self.sliceCoordinates self.sliceEdges = [sc[0][1] - sc[0][0], sc[1][1] - sc[1][0]] ##print(self.regionSlice, sc, self.sliceEdges) - if self.detectorInfo.dimension == 2: ## remap to be 2d + if self.detectorInfo.dimension == 2: ## remap to be 2d self.regionSlice = self.regionSlice[1:3] print("remapping regionSlice to be 2d") @@ -401,13 +406,14 @@ def setupOutputDirString(self, analysisType): print("output dir: " + self.outputDir) logger.info("output dir: " + self.outputDir) - def setupConfigHash(self): ## info to write to h5 to help processing - self.configHash = {"sliceCoordinates":self.sliceCoordinates, - "analyzedModules": self.analyzedModules, - "modules": self.detectorInfo.nModules, - "rows": self.detectorInfo.nRows, - "cols": self.detectorInfo.nCols - } + self.configHash = { + "sliceCoordinates": self.sliceCoordinates, + "analyzedModules": self.analyzedModules, + "modules": self.detectorInfo.nModules, + "rows": self.detectorInfo.nRows, + "cols": self.detectorInfo.nCols, + } + #### End of setup related functions #### diff --git a/config_files/asc_epixMSuiteConfig.py b/config_files/asc_epixMSuiteConfig.py index 570193c..4f24e11 100755 --- a/config_files/asc_epixMSuiteConfig.py +++ b/config_files/asc_epixMSuiteConfig.py @@ -24,10 +24,10 @@ experimentHash = { "detectorType": "epixm", - "detectorVersion":1,## new firmware + "detectorVersion": 1, ## new firmware "exp": "ascdaq18", # "exp": "rixx1003721", - "location": "DetLab",## I guess + "location": "DetLab", ## I guess "ignoreEventCodeCheck": True, "analyzedModules": [0, 1, 2, 3], "seedCut": 40, ## pure guess diff --git a/config_files/epix100SuiteConfig.py b/config_files/epix100SuiteConfig.py index 85bbe46..17a766c 100755 --- a/config_files/epix100SuiteConfig.py +++ b/config_files/epix100SuiteConfig.py @@ -40,7 +40,7 @@ # 'ROIs':['roiFromSwitched_e557_rmfxx1005021'] # 'ROIs':['allHRasicPixels', 'goodboxROI']#'roiAbove7k_raw_r123'] # "ROIs": ["../data/XavierV4_2", "../data/OffXavierV4_2"], - #], - ##"regionSlice": np.s_[0:1, 0:704:, 0:768] - "regionSlice": np.s_[0:1, 0:704:, 0:768] + # ], + ##"regionSlice": np.s_[0:1, 0:704:, 0:768] + "regionSlice": np.s_[0:1, 0:704:, 0:768], } diff --git a/config_files/epix10kQuadSuiteConfig.py b/config_files/epix10kQuadSuiteConfig.py index e39fd9b..b911312 100755 --- a/config_files/epix10kQuadSuiteConfig.py +++ b/config_files/epix10kQuadSuiteConfig.py @@ -41,7 +41,7 @@ # 'ROIs':['roiFromSwitched_e557_rmfxx1005021'] # 'ROIs':['allHRasicPixels', 'goodboxROI']#'roiAbove7k_raw_r123'] # "ROIs": ["../data/XavierV4_2", "../data/OffXavierV4_2"], - #], - ##"regionSlice": np.s_[0:1, 0:704:, 0:768] - "regionSlice": np.s_[0:4, 0:352:, 0:384] + # ], + ##"regionSlice": np.s_[0:1, 0:704:, 0:768] + "regionSlice": np.s_[0:4, 0:352:, 0:384], } diff --git a/config_files/epixMSuiteConfig.py b/config_files/epixMSuiteConfig.py index ad054e6..ecd5ea7 100755 --- a/config_files/epixMSuiteConfig.py +++ b/config_files/epixMSuiteConfig.py @@ -13,7 +13,7 @@ # experimentHash = {'exp':'rixc00121', 'location':'RixEndstation', singlePixelArray = [] ##for i in [0, 2, 3]:##range(0, 3): -for i in [2, 3]:##range(0, 3): +for i in [2, 3]: ##range(0, 3): singlePixelArray.append([i, 55, 10]) singlePixelArray.append([i, 120, 10]) singlePixelArray.append([i, 55, 200]) @@ -23,16 +23,16 @@ experimentHash = { "detectorType": "epixm", - "detectorVersion":1,## new firmware + "detectorVersion": 1, ## new firmware "exp": "rixx1005922", "location": "RixEndstation", "analyzedModules": [2, 3], "seedCut": 40, ## pure guess "neighborCut": 10, ##pure guess "fluxSource": "MfxDg1BmMon", - #"fluxSource": "MfxDg2BmMon", - "fluxChannels": [15],## or 11 if we see saturation - #"fluxSign": 1, ## for dg2 + # "fluxSource": "MfxDg2BmMon", + "fluxChannels": [15], ## or 11 if we see saturation + # "fluxSign": 1, ## for dg2 "fluxSign": -1, "singlePixels": singlePixelArray, # 'ROIs':['module0', 'module2', 'module4', 'module6', 'module10','module12', 'module14'] diff --git a/config_files/jfSuiteConfig.py b/config_files/jfSuiteConfig.py index 1ab4d9d..bdb8b31 100755 --- a/config_files/jfSuiteConfig.py +++ b/config_files/jfSuiteConfig.py @@ -10,7 +10,7 @@ import numpy as np singlePixelArray = [] -for i in [3]:##range(0, 8): +for i in [3]: ##range(0, 8): singlePixelArray.append([i, 10, 10]) singlePixelArray.append([i, 10, 100]) singlePixelArray.append([i, 100, 10]) @@ -25,7 +25,7 @@ "exp": "cxic00121", "location": "CxiDs1", "ignoreEventCodeCheck": True, - "analyzedModules": [0], ##range(0:8), + "analyzedModules": [0], ##range(0:8), "seedCut": 100, ## pure guess "neighborCut": 20, ##pure guess # "fluxSource": "MfxDg1BmMon", @@ -36,8 +36,8 @@ "singlePixels": singlePixelArray, # 'ROIs':['module0', 'module2', 'module4', 'module6', 'module10','module12', 'module14'] # "ROIs": ["../data/XavierV4_2", "../data/OffXavierV4_2"], - #], + # ], ##"regionSlice": np.s_[0:8, 0:512:, 0:1024] ## whole camera ##"regionSlice": np.s_[0:1, 0:512:, 0:1024] - "regionSlice": np.s_[0:1, 0:129:, 0:129] + "regionSlice": np.s_[0:1, 0:129:, 0:129], } diff --git a/suite_scripts/AnalyzeH5.py b/suite_scripts/AnalyzeH5.py index a398c27..66213b7 100644 --- a/suite_scripts/AnalyzeH5.py +++ b/suite_scripts/AnalyzeH5.py @@ -23,7 +23,7 @@ # log to file named .log currFileName = os.path.basename(__file__) -ls.setupScriptLogging(currFileName[:-3] + ".log", logging.INFO) # change to logging.INFO for full logging output +ls.setupScriptLogging(currFileName[:-3] + ".log", logging.INFO) # change to logging.INFO for full logging output # for logging from current file logger = logging.getLogger(__name__) @@ -41,7 +41,7 @@ def __init__(self): self.label = args.label self.camera = 0 self.seedCut = args.seedCut - self.isTestRun = args.special is not None and 'testRun' in args.special + self.isTestRun = args.special is not None and "testRun" in args.special def getFiles(self): fileNames = self.files.split(",") @@ -51,20 +51,19 @@ def getFiles(self): self.h5Files.append(h5py.File(f)) def identifyAnalysis(self): - for key in ["analysis", "sliceCoordinates", "modules", "rows", "cols"]: if key not in self.h5Files[0]: print("h5 file missing metadata for key: '" + key + "'\nexiting...") - exit(1) # eventually try get this data from cmdline args?? or maybe have default vals to try with? + exit(1) # eventually try get this data from cmdline args?? or maybe have default vals to try with? - encoding = 'utf-8' + encoding = "utf-8" # note: [()] is h5py way to access key's data # handle how some different machines create h5 differently try: self.analysis = self.h5Files[0]["analysis"][()][0].decode(encoding) - except: + except Exception: try: self.analysis = self.h5Files[0]["analysis"][()].decode(encoding) except Exception as e: @@ -85,7 +84,6 @@ def identifyAnalysis(self): print("detRows: ", self.detRows) print("detCols: ", self.detCols) - def sliceToDetector(self, sliceRow, sliceCol): return sliceRow + self.sliceCoordinates[0][0], sliceCol + self.sliceCoordinates[1][0] @@ -95,14 +93,14 @@ def getRowsColsFromSliceCoordinates(self): offset = 1 self.rowStart = self.sliceCoordinates[offset][0] self.rowStop = self.sliceCoordinates[offset][1] - self.colStart = self.sliceCoordinates[offset+1][0] - self.colStop = self.sliceCoordinates[offset+1][1] - rows = self.rowStop-self.rowStart - cols = self.colStop-self.colStart - print("analyzing %d rows, %d cols" %(rows, cols)) - + self.colStart = self.sliceCoordinates[offset + 1][0] + self.colStop = self.sliceCoordinates[offset + 1][1] + rows = self.rowStop - self.rowStart + cols = self.colStop - self.colStart + print("analyzing %d rows, %d cols" % (rows, cols)) + return rows, cols - + def analyze(self): if self.analysis == "cluster": self.clusterAnalysis() @@ -124,7 +122,7 @@ def clusterAnalysis(self): pass self.nBins = 200 ## for epixM with a lot of 2 photon events... - + if self.seedCut is None: self.lowEnergyCut = 4 ## fix - should be 0.5 photons or something else: @@ -173,18 +171,17 @@ def analyzeSimpleClusters(self, clusters): ax = plt.subplot() energy = clusters[:, 0] ##.flatten() - maximumModule = int(clusters[:, 1].max()) analyzedModules = np.unique(clusters[:, 1]).astype("int") print("analyzing modules", analyzedModules) rows, cols = self.getRowsColsFromSliceCoordinates() -## ##cols = self.sliceEdges[1] -## ## doesn't exist in h5 yet so calculate dumbly instead -## rows = int(clusters[:, 2].max()) + 1 -## cols = int(clusters[:, 3].max()) + 1 + ## ##cols = self.sliceEdges[1] + ## ## doesn't exist in h5 yet so calculate dumbly instead + ## rows = int(clusters[:, 2].max()) + 1 + ## cols = int(clusters[:, 3].max()) + 1 print("appear to have a slice with %d rows, %d cols" % (rows, cols)) -## self.sliceCoordinates = [[0, rows], [0, cols]] ## temp - get from h5 -## self.sliceEdges = [rows, cols] + ## self.sliceCoordinates = [[0, rows], [0, cols]] ## temp - get from h5 + ## self.sliceEdges = [rows, cols] print("mean energy above 0:" + str(energy[energy > 0].mean())) logger.info("mean energy above 0:" + str(energy[energy > 0].mean())) @@ -204,6 +201,7 @@ def analyzeSimpleClusters(self, clusters): plt.close() # verbose = False + ##maximumModule = int(clusters[:, 1].max()) ##fitInfo = np.zeros((maximumModule + 1, rows, cols, 5)) ## mean, std, area, mu, sigma fitInfo = np.zeros((self.detModules, self.detRows, self.detCols, 5)) ## mean, std, area, mu, sigma smallSquareClusters = ancillaryMethods.getSmallSquareClusters(clusters, nPixelCut=3) @@ -218,7 +216,7 @@ def analyzeSimpleClusters(self, clusters): for j in range(self.colStart, self.colStop): ##detRow, detCol = self.sliceToDetector(i, j) - detRow, detCol = i, j ## mostly for clarity + detRow, detCol = i, j ## mostly for clarity currGoodClusters = ancillaryMethods.getMatchedClusters(rowModClusters, "column", j) if len(currGoodClusters) < 5: print("too few clusters in slice pixel %d, %d, %d: %d" % (m, i, j, len(currGoodClusters))) diff --git a/suite_scripts/CalcNoiseAndMean.py b/suite_scripts/CalcNoiseAndMean.py index c3717ac..c7e303c 100755 --- a/suite_scripts/CalcNoiseAndMean.py +++ b/suite_scripts/CalcNoiseAndMean.py @@ -44,6 +44,7 @@ outer_max = 1 inner_count = 0 inner_max = 50 + nonZeroAsicArray = 0 for nstep, step in enumerate(stepGen): # for testing @@ -53,11 +54,11 @@ for nevt, evt in enumerate(step.events()): if nevt >= bss.maxNevents: - print("have reached %d events" %(bss.maxNevents)) + print("have reached %d events" % (bss.maxNevents)) break if evt is None: continue - + # for testing inner_count += 1 if isTestRun and inner_count > inner_max: @@ -119,12 +120,12 @@ ## temp for Alex: if False and not (bss.detObj and bss.detObj == "calib"): - ##if True and not (bss.detObj and bss.detObj == "calib"): - nonZeroAsics = [1*np.any(frames[i]) for i in range(frames.shape[0])] + ##if True and not (bss.detObj and bss.detObj == "calib"): + nonZeroAsics = [1 * np.any(frames[i]) for i in range(frames.shape[0])] try: ##print(nonZeroAsics) nonZeroAsicArray += nonZeroAsics - except: + except Exception: nonZeroAsicArray = np.array(nonZeroAsics) for i, p in enumerate(bss.singlePixels): @@ -184,6 +185,6 @@ bss.dumpEventCodeStatistics() ## temp for Alex: if False: - ##if True: + ##if True: np.save("nonZeroAsicAccounting.npy", np.array(nonZeroAsicArray)) print("non-zero asic accounting:", nonZeroAsicArray) diff --git a/suite_scripts/EventScanParallelSlice.py b/suite_scripts/EventScanParallelSlice.py index 9c810aa..b9204f1 100644 --- a/suite_scripts/EventScanParallelSlice.py +++ b/suite_scripts/EventScanParallelSlice.py @@ -203,9 +203,9 @@ def analyze_h5(self, dataFile, label): sys.exit(0) esp.setupPsana() - if esp.psanaType == 1:## move to psana1Base asap + if esp.psanaType == 1: ## move to psana1Base asap from psana import EventId - + try: ##skip_283_check = "skip283" in esp.special skip_283_check = "fakeBeamCode" in esp.special @@ -222,7 +222,7 @@ def analyze_h5(self, dataFile, label): size = 666 h5FileName = "%s/%s_c%d_r%d_%s_n%d.h5" % (esp.outputDir, esp.className, esp.camera, esp.run, esp.label, size) - if esp.psanaType==1: + if esp.psanaType == 1: smd = esp.ds.small_data(filename=h5FileName, gather_interval=100) else: smd = esp.ds.smalldata(filename=h5FileName) @@ -234,9 +234,9 @@ def analyze_h5(self, dataFile, label): bitSliceSum = None try: evtGen = esp.myrun.events() - except: + except Exception: evtGen = esp.ds.events() - + for nevt, evt in enumerate(evtGen): if evt is None: continue @@ -305,9 +305,9 @@ def analyze_h5(self, dataFile, label): ##parityTest = esp.getPingPongParity(frames[0][144:224, 0:80]) ##print(frames[tuple(esp.singlePixels[0])], parityTest) - if esp.psanaType==1: + if esp.psanaType == 1: t = evt.get(EventId).time() - timestamp = t[0]+t[1]/1000000000. + timestamp = t[0] + t[1] / 1000000000.0 pulseId = 0 else: timestamp = evt.datetime().timestamp() @@ -321,7 +321,7 @@ def analyze_h5(self, dataFile, label): if esp.ROIs != []: smdDict["rois"] = np.array([roiMeans[i][-1] for i in range(len(esp.ROIs))]) - if esp.psanaType==1: + if esp.psanaType == 1: smd.event(**smdDict) else: smd.event(evt, **smdDict) @@ -344,10 +344,9 @@ def analyze_h5(self, dataFile, label): logger.info("Wrote file: " + npyFileName) ##esp.plotData(roiMeans, pixelValues, eventNumbers, None, "foo") - - if (esp.psanaType==1 or smd.summary) and esp.fakePedestal is None: + if (esp.psanaType == 1 or smd.summary) and esp.fakePedestal is None: allSum = smd.sum(bitSliceSum) - if esp.psanaType==1: + if esp.psanaType == 1: smd.save({"summedBitSlice": allSum}) else: smd.save_summary({"summedBitSlice": allSum}) diff --git a/suite_scripts/SimpleClustersParallelSlice.py b/suite_scripts/SimpleClustersParallelSlice.py index 3e1eeb3..4087dee 100644 --- a/suite_scripts/SimpleClustersParallelSlice.py +++ b/suite_scripts/SimpleClustersParallelSlice.py @@ -135,11 +135,11 @@ def analyze_h5(self, dataFile, label): sic.setupPsana() sic.configHash["analysis"] = "cluster" - - print("analyzed modules:", sic.analyzedModules) ## move this to psana setup + + print("analyzed modules:", sic.analyzedModules) ## move this to psana setup size = 666 - filename="%s/%s_%s_c%d_r%d_n%d.h5" % (sic.outputDir, sic.className, sic.label, sic.camera, sic.run, size) - if sic.psanaType==1: + filename = "%s/%s_%s_c%d_r%d_n%d.h5" % (sic.outputDir, sic.className, sic.label, sic.camera, sic.run, size) + if sic.psanaType == 1: smd = sic.ds.small_data(filename=filename, gather_interval=100) else: smd = sic.get_smalldata(filename=filename) @@ -175,10 +175,10 @@ def analyze_h5(self, dataFile, label): else: try: evtGen = sic.myrun.events() - except: + except Exception: ##if sic.psanaType == 1: ## fix in base class asap evtGen = sic.ds.events() - + pedestal = None nComplaints = 0 try: @@ -210,11 +210,11 @@ def analyze_h5(self, dataFile, label): zeroLowGain = False if sic.special and "zeroLowGain" in sic.special: zeroLowGain = True - + useSlice = False if sic.special is not None and "slice" in sic.special: useSlice = True - + hSum = None for nevt, evt in enumerate(evtGen): if evt is None: @@ -271,10 +271,10 @@ def analyze_h5(self, dataFile, label): ## temp fix for 2d case (epix100, rixsCCD) if sic.detectorInfo.dimension == 2: frames = np.array([frames]) - - try: ## added for psana1 - should fix in base class + + try: ## added for psana1 - should fix in base class flux = sic.flux - except: + except Exception: flux = None if sic.useFlux and flux is None: continue @@ -297,7 +297,7 @@ def analyze_h5(self, dataFile, label): if nClusters == maxClusters: continue if useSlice: - if sic.detectorInfo.dimension == 2: ## figure out how to kill if + if sic.detectorInfo.dimension == 2: ## figure out how to kill if bc = BuildClusters(frames[module][sic.regionSlice], seedCut, neighborCut) elif sic.detectorInfo.dimension == 3: bc = BuildClusters(frames[sic.regionSlice][module], seedCut, neighborCut) @@ -324,22 +324,22 @@ def analyze_h5(self, dataFile, label): ## had continue here break - if nevt%1000 == 0: - print("event %d, found %d clusters" %(nevt, nClusters)) - - if sic.psanaType==1: + if nevt % 1000 == 0: + print("event %d, found %d clusters" % (nevt, nClusters)) + + if sic.psanaType == 1: smd.event(clusterData=clusterArray) else: smd.event(evt, clusterData=clusterArray) sic.nGoodEvents += 1 if sic.nGoodEvents == sic.maxNevents: - print("have reached max n events %d, quitting" %(sic.maxNevents)) + print("have reached max n events %d, quitting" % (sic.maxNevents)) break - + if sic.nGoodEvents % 1000 == 0: print("n good events analyzed: %d, clusters this event: %d" % (sic.nGoodEvents, nClusters)) - + if sic.detectorInfo.dimension == 3: f = frames[sic.regionSlice] elif sic.detectorInfo.dimension == 2: @@ -356,11 +356,11 @@ def analyze_h5(self, dataFile, label): ## np.save("%s/eventNumbers_c%d_r%d_%s.npy" %(sic.outputDir, sic.camera, sic.run, sic.exp), np.array(eventNumbers)) ## sic.plotData(roiMeans, pixelValues, eventNumbers, "foo") - if sic.psanaType==1 or smd.summary: + if sic.psanaType == 1 or smd.summary: ## guess at desired psana1 behavior - no smd.summary there ## maybe check smd.rank == 0? sumhSum = smd.sum(hSum) - if sic.psanaType==1: + if sic.psanaType == 1: smd.save({"energyHistogram": sumhSum}) smd.save(sic.configHash) else: diff --git a/suite_scripts/makeGainCorrection.py b/suite_scripts/makeGainCorrection.py index de87978..f0f4777 100644 --- a/suite_scripts/makeGainCorrection.py +++ b/suite_scripts/makeGainCorrection.py @@ -7,18 +7,18 @@ ## may be copied, modified, propagated, or distributed except according to ## the terms contained in the LICENSE.txt file. ############################################################################## -import sys import numpy as np -from calibrationSuite.basicSuiteScript import BasicSuiteScript from calibrationSuite import ancillaryMethods +from calibrationSuite.basicSuiteScript import BasicSuiteScript class MakeGainCorrection(BasicSuiteScript): def __init__(self): super().__init__() ##self) + if __name__ == "__main__": mgc = MakeGainCorrection() mgc.setupPsana() @@ -31,7 +31,7 @@ def __init__(self): ## pedestal should be the same shape as a the numpy file analyzed data = np.load(mgc.file) - print("found %d NaN elements in %s" %(np.isnan(data).sum(), mgc.file)) + print("found %d NaN elements in %s" % (np.isnan(data).sum(), mgc.file)) ## handle 2d case if mgc.detectorInfo.dimension == 2: @@ -44,24 +44,26 @@ def __init__(self): regionMedian = np.median(data[m][np.isfinite(data[m])]) data[m][np.isnan(data[m])] = regionMedian rms = data[m].std() - data[m] = data[m].clip(regionMedian-3*rms, regionMedian+3*rms) - print("have clipped module %d, rms was %0.2f, now %0.2f" - %(m, rms, data[m].std())) + data[m] = data[m].clip(regionMedian - 3 * rms, regionMedian + 3 * rms) + print("have clipped module %d, rms was %0.2f, now %0.2f" % (m, rms, data[m].std())) - correction = measuredAduPerKeV = data/mgc.photonEnergy*mgc.detectorInfo.aduPerKeV + correction = measuredAduPerKeV = data / mgc.photonEnergy * mgc.detectorInfo.aduPerKeV if mgc.detectorInfo.detectorType == "Epix100a": ## psana wants keV/adu in this one case - correction = 1./measuredAduPerKeV + correction = 1.0 / measuredAduPerKeV fileName = mgc.file.split(".npy")[0] + "_cleanedCorrection.npy" np.save(fileName, correction) - + print("n.b. for autoranging detectors this needs to be applied carefully") import matplotlib.pyplot as plt + pedMean = pedestal.mean() pedRms = pedestal.std() - x, y, err = ancillaryMethods.makeProfile(pedestal.clip(pedMean-5*pedRms,pedMean+4*pedRms).flatten(), correction.flatten(), 50)##, spread=True)##, myStatistic="median") + x, y, err = ancillaryMethods.makeProfile( + pedestal.clip(pedMean - 5 * pedRms, pedMean + 4 * pedRms).flatten(), correction.flatten(), 50 + ) ##, spread=True)##, myStatistic="median") plt.errorbar(x, y, err) plt.xlabel("pedestal") plt.ylabel("gain correction") diff --git a/suite_scripts/simplePhotonCounter.py b/suite_scripts/simplePhotonCounter.py index 4eecb4b..c488305 100755 --- a/suite_scripts/simplePhotonCounter.py +++ b/suite_scripts/simplePhotonCounter.py @@ -106,7 +106,7 @@ print("total photons in detector using cut %0.2f is %0.3f" % (spc.photonCut, (thresholded).sum())) logger.info("total photons in detector using cut %0.2f is %0.3f" % (spc.photonCut, (thresholded).sum())) - if True:##False: + if True: ##False: spectrumFileName = "%s/%s_%s_r%d_c%d_%s_spectrum.npy" % ( spc.outputDir, scriptType, @@ -121,10 +121,11 @@ tImage = spc.getImage(evt, thresholded) np.save(imageFileName, tImage) import matplotlib.pyplot as plt + p90 = np.percentile(tImage, 90) print("clipping image at 90% of max") plt.imshow(tImage.clip(0, p90)) plt.colorbar() plt.savefig(imageFileName.replace("npy", "png")) - + spc.dumpEventCodeStatistics() diff --git a/suite_scripts/simpleTestScript.py b/suite_scripts/simpleTestScript.py index 9c7d64b..5967c8e 100644 --- a/suite_scripts/simpleTestScript.py +++ b/suite_scripts/simpleTestScript.py @@ -1,18 +1,18 @@ -from psana import * import sys +from psana import * # noqa: F403 + ##N.b. - this is psana2 -exp = 'rixc00122' +exp = "rixc00122" run = eval(sys.argv[1]) -ds = DataSource(exp=exp,run=run, - intg_det='archon', max_events=666666) +ds = DataSource(exp=exp, run=run, intg_det="archon", max_events=666666) # noqa: F405 smd = ds.smalldata(filename="foo.h5") print(help(smd.event)) myrun = next(ds.runs()) -det = myrun.Detector('archon') +det = myrun.Detector("archon") nGood = 0 nBad = 0 @@ -20,10 +20,12 @@ ##print(dir(evt)) ##print(help(evt)) if det.raw.raw(evt) is None: - if nBad<3: print("event %d is None" %(nevt)) + if nBad < 3: + print("event %d is None" % (nevt)) nBad += 1 else: - if nGood<3: print("good event") + if nGood < 3: + print("good event") nGood += 1 diff --git a/suite_scripts/slurmSubModulesAndRegions.py b/suite_scripts/slurmSubModulesAndRegions.py index cc6e0fc..c9385cc 100644 --- a/suite_scripts/slurmSubModulesAndRegions.py +++ b/suite_scripts/slurmSubModulesAndRegions.py @@ -1,8 +1,10 @@ -from subprocess import call import time + from calibrationSuite.detectorInfo import DetectorInfo + ##import sys + def calculateBlock(i, step, maxStep, analysisType): if i == 0: start = 0 @@ -10,37 +12,38 @@ def calculateBlock(i, step, maxStep, analysisType): else: start = step * i stop = start + step - if analysisType=='cluster': - start -= 1 ## handle fact I don't fit edges - if i