Skip to content

Commit

Permalink
Merge pull request #138 from slaclab/development
Browse files Browse the repository at this point in the history
Merge Code From Beamtime October 1st 2024
  • Loading branch information
nstelter-slac authored Oct 4, 2024
2 parents 7b9c525 + 17122cb commit 0fcfaaf
Show file tree
Hide file tree
Showing 25 changed files with 420 additions and 228 deletions.
9 changes: 7 additions & 2 deletions calibrationSuite/argumentParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -60,6 +62,9 @@ def __init__(self):
self.parser.add_argument("--fluxCutMax", type=float, help="maximum flux to be included in analysis")
self.parser.add_argument("--seedCut", help="seed cut for clustering")
self.parser.add_argument("--photonEnergy", type=float, help="photon energy")
self.parser.add_argument("--aduPerKeV", type=float, help="ADU per keV")
self.parser.add_argument("--gainMode", type=int, help="gain mode (int)")

self.parser.add_argument(
"--special",
type=str,
Expand Down
23 changes: 16 additions & 7 deletions calibrationSuite/basicSuiteScript.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,22 @@ 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))

try:
self.dumpEpixMHeaderInfo(evt)
except:
pass

if False and self.special: ## turned off for a tiny bit of speed
if "thirteenBits" in self.special:
frames = frames & 0xFFFE
Expand Down Expand Up @@ -231,9 +236,9 @@ def rowCommonModeCorrection(self, frame, arbitraryCut=1000):
frame[r, colOffset : colOffset + self.detectorInfo.nColsPerBank] < arbitraryCut
]
)
frame[r, colOffset : colOffset + self.detectorInfo.nColsPerBank] -= rowCM
if not np.isnan(rowCM): ## no pixels found under cut
frame[r, colOffset : colOffset + self.detectorInfo.nColsPerBank] -= rowCM
except Exception:
rowCM = -666
print("rowCM problem")
logger.error("rowCM problem")
print(frame[r, colOffset : colOffset + self.detectorInfo.nColsPerBank])
Expand All @@ -245,6 +250,7 @@ def colCommonModeCorrection(self, frame, arbitraryCut=1000):
## cut keeps photons in common mode - e.g. set to <<1 photon

##rand = np.random.random()

for c in range(self.detectorInfo.nCols):
rowOffset = 0
for b in range(0, self.detectorInfo.nBanksRow):
Expand All @@ -254,9 +260,12 @@ def colCommonModeCorrection(self, frame, arbitraryCut=1000):
frame[rowOffset : rowOffset + self.detectorInfo.nRowsPerBank, c] < arbitraryCut
]
)
frame[rowOffset : rowOffset + self.detectorInfo.nRowsPerBank, c] -= colCM
if not np.isnan(colCM): ## if no pixels < cut we get nan
if False:
if c<100:
self.commonModeVals.append(colCM)
frame[rowOffset : rowOffset + self.detectorInfo.nRowsPerBank, c] -= colCM
except Exception:
colCM = -666
print("colCM problem")
logger.error("colCM problem")
print(frame[rowOffset : rowOffset + self.detectorInfo.nRowsPerBank], c)
Expand Down
44 changes: 25 additions & 19 deletions calibrationSuite/detectorInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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
Expand All @@ -178,5 +186,3 @@ def setup_rixsCCD(self):
self.nRows = 1200
self.clusterShape = [3, 5] ## maybe
self.g0cut = 1 << 16


3 changes: 2 additions & 1 deletion calibrationSuite/fitFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 11 additions & 9 deletions calibrationSuite/psana1Base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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:
Expand All @@ -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()
Expand Down Expand Up @@ -156,5 +158,5 @@ def getImage(self, evt, data=None):

def getPedestal(self, evt, gainmode):
if self.detectorInfo.autoRanging:
return self.det.pedestal(evt)[gainmode]
return self.det.pedestals(evt)[gainmode]
return self.det.pedestals(evt)
17 changes: 12 additions & 5 deletions calibrationSuite/psana2Base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -255,17 +255,24 @@ 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]
delta = evensEvenRowsOddsOddRows.mean() - oddsEvenRowsEvensOddRows.mean()
##print("delta:", delta)
return delta > 0

def dumpEpixMHeaderInfo(self, evt):
print(f'frameNo: {self.det.raw.frameNo(evt)},',
f'asicNo: {self.det.raw.asicNo(evt)},',
f'autoFillMask: {[hex(a) for a in self.det.raw.autoFillMask(evt)]},',
f'fixedMask: {self.det.raw.fixedMask(evt)}')

Loading

0 comments on commit 0fcfaaf

Please sign in to comment.