Skip to content
Snippets Groups Projects
Commit 48f8d849 authored by Karim Ahmed's avatar Karim Ahmed
Browse files

merge notebooks and update notebooks.py

parent 73ef8b0d
No related branches found
No related tags found
1 merge request!841[JUNGFRAU][FF] Feat: new notebook for producing gain constants.
%% Cell type:markdown id: tags:
# G0 map from single photon spectra
Author: European XFEL Detector Group, Version: 1.0
Converts single photon flat fields into a G0 map
%% Cell type:code id: tags:
``` python
in_folder = "/gpfs/exfel/exp/HED/202231/p900297/raw"
out_folder = "/gpfs/exfel/data/scratch/ahmedk/test/FFDATA/HED/p900297/gain_fit"
runs = [26] # can be a list of runs
dir_date_iso = "" ## string to save the date of the creation of the run
# to be used when injecting constants (third separate nb)
sensor_size = [512, 1024] # size of the array in the 'row' and 'col' dimensions
chunk_size = 10 # n of trains per chunk
block_size = [128, 64] # dimension of the chunks in 'row' and 'col'
mod_n = 1 # module number
karabo_id = "HED_IA1_JF500K1" # karabo prefix of Jungfrau devices
da_name = f"JNGFR{mod_n:02d}"
da_control = "JNGFRCTRL00"
ctrl_source_template = "{}/DET/CONTROL" # template for control source name (filled with karabo_id_control)
karabo_id_control = "" # if control is on a different ID, set to empty string if it is the same a karabo-id
bias_voltage = 180.0 # bias voltage - should be derived from CONTROL file
integration_time = 10.0 # integration time - should be derived from CONTROL file.
memory_cells = 1 # number of memory cells - should be derived from CONTROL file
sc_start = 15 # storage cell start value - should be derived from CONTROL file
gain_mode = 0 # number of memory cells - Set to -1 to derive from CONTROL file.
_fit_func = "CHARGE_SHARING" ## which function will be used to fit the histogram
_h_range = (200.0, 350.0) # range of the histogram in x-axis units
rebin = 1
# parameters for the peak finder
n_sigma = 20.0 # n of sigma abov pedestal threshold
ratio = 0.99 # ratio of the next peak amplitude in the peak_finder
```
%% Cell type:code id: tags:
``` python
import multiprocessing
import time
from functools import partial
from logging import warning
from pathlib import Path
import numpy as np
from extra_data import RunDirectory
from h5py import File as h5file
from cal_tools.jungfrau import jungfrau_ff
from cal_tools.jungfraulib import JungfrauCtrl
```
%% Cell type:code id: tags:
``` python
out_folder = Path(out_folder)
out_folder.mkdir(parents=True, exist_ok=True)
in_folder = Path(in_folder)
if karabo_id_control == "":
karabo_id_control = karabo_id
```
%% Cell type:markdown id: tags:
### Opening log file
%% Cell type:code id: tags:
``` python
h_spectra = None
edges = None
noise_map = None
ctrl_src = ctrl_source_template.format(karabo_id_control)
run_dc = RunDirectory(in_folder / f"r{runs[0]:04d}")
ctrl_data = JungfrauCtrl(run_dc, ctrl_src)
if memory_cells < 0:
memory_cells, sc_start = ctrl_data.get_memory_cells()
mem_cells_name = "single cell" if memory_cells == 1 else "burst"
print(f"Run is in {mem_cells_name} mode.\nStorage cell start: {sc_start:02d}")
else:
mem_cells_name = "single cell" if memory_cells == 1 else "burst"
print(
f"Run is in manually set to {mem_cells_name} mode. With {memory_cells} memory cells"
)
mode = "Single"
if memory_cells > 1:
mode = "Burst"
file_h_name = (
f"R{runs[0]:04d}_Gain_{mode}_Spectra_{da_name}_Histo.h5" # histogram file name
)
begin_stuff = time.localtime()
i_cut = file_h_name.find("_Histo")
fout_temp = file_h_name[:i_cut]
fout_temp += f"_{_fit_func}_Fit"
print(f"block_size: {block_size}")
if integration_time < 0:
integration_time = ctrl_data.get_integration_time()
if bias_voltage < 0:
bias_voltage = ctrl_data.get_bias_voltage()
# if gain_setting < 0:
# gain_setting = ctrl_data.get_gain_setting()
if gain_mode < 0:
gain_mode = ctrl_data.get_gain_mode()
print(f"Integration time is {integration_time} us")
# print(f"Gain setting is {gain_setting} (run settings: {ctrl_data.run_settings})")
print(f"Gain mode is {gain_mode} ({ctrl_data.run_mode})")
print(f"Bias voltage is {bias_voltage} V")
print(f"Number of memory cells are {memory_cells}")
```
%% Cell type:markdown id: tags:
## Opening Histo File
%% Cell type:code id: tags:
``` python
# transposition here is just to make saved histo compatible with my other notebooks
with h5file(out_folder / file_h_name, "r") as f:
print(f"opening histo in file {file_h_name}")
if "histos" in f.keys():
h_spectra = np.transpose(np.array(f["histos"]))
print(f"histogram found: {h_spectra.shape}")
else:
raise AttributeError("No histo in file!")
edges = np.array(f["edges"])
x = (edges[1:] + edges[:-1]) / 2.0
if "noise_map" in f.keys():
noise_map = np.array(f["noise_map"])
print(f"noise map found: {noise_map.shape}")
else:
warning("noise map not found!")
print("Reading control data from histogram files.")
if "integration_time" in f.attrs.keys():
integration_time = np.float32(f.attrs["integration_time"])
else:
print(f"integration_time is not found! using default value{integration_time}")
if "bias_voltage" in f.attrs.keys():
bias_voltage = np.float32(f.attrs["bias_voltage"])
else:
warning(f"bias_voltage not found! using default value: {bias_voltage}")
if "creation_time" in f.attrs.keys():
dir_date_iso = str(f.attrs["creation_time"])
if len(dir_date_iso) == 0:
warning("Dir date is empty string")
else:
print("dir_date not found!")
```
%% Cell type:markdown id: tags:
## Fitting histograms
%% Cell type:code id: tags:
``` python
chunks = jungfrau_ff.chunk_Multi([h_spectra], block_size)
pool = multiprocessing.Pool()
st = time.perf_counter()
partial_fit = partial(
jungfrau_ff.fit_histogram,
x,
_fit_func,
n_sigma,
rebin,
ratio,
noise_map,
)
print("starting spectra fit")
r_maps = pool.map(partial_fit, chunks)
print("r_maps calculation", time.perf_counter() - st)
g0_map = np.zeros((memory_cells, sensor_size[0], sensor_size[1]), dtype=np.float32)
sigma_map = np.zeros((memory_cells, sensor_size[0], sensor_size[1]), dtype=np.float32)
chi2ndf_map = np.zeros((memory_cells, sensor_size[0], sensor_size[1]), dtype=np.float32)
alpha_map = np.zeros((memory_cells, sensor_size[0], sensor_size[1]), dtype=np.float32)
for i, r in enumerate(r_maps):
g0_chk, sigma_chk, chi2ndf_chk, alpha_chk = r
n_blocks_col = int(g0_map.shape[-1] / block_size[1])
irow = int(np.floor(i / n_blocks_col)) * block_size[0]
icol = i % n_blocks_col * block_size[1]
g0_map[..., irow : irow + block_size[0], icol : icol + block_size[1]] = g0_chk
sigma_map[..., irow : irow + block_size[0], icol : icol + block_size[1]] = sigma_chk
chi2ndf_map[
..., irow : irow + block_size[0], icol : icol + block_size[1]
] = chi2ndf_chk
alpha_map[..., irow : irow + block_size[0], icol : icol + block_size[1]] = alpha_chk
print("loading r_maps calculation results", time.perf_counter() - st)
pool.close()
print("... done")
```
%% Cell type:markdown id: tags:
## Final steps
### Saving fit results
%% Cell type:code id: tags:
``` python
fout_path = f"{out_folder}/{fout_temp}.h5"
with h5file(fout_path, "w") as fout:
print("saving noise map ...")
##trasposition is to make it compatible with my other nb
dset_noi = fout.create_dataset("noise_map", data=np.transpose(noise_map))
print("saving fit results ...")
dset_chi2 = fout.create_dataset("chi2map", data=np.transpose(chi2ndf_map))
dset_gmap_fit = fout.create_dataset("gainMap_fit", data=np.transpose(g0_map))
dset_std = fout.create_dataset("sigmamap", data=np.transpose(sigma_map))
dset_alpha = fout.create_dataset("alphamap", data=np.transpose(alpha_map))
fout.attrs["memory_cells"] = memory_cells # TODO: Why memory cells are not saved here. What about the other conditions??
fout.attrs["integration_time"] = integration_time
fout.attrs["bias_voltage"] = bias_voltage
fout.attrs["dir_date_iso"] = dir_date_iso
fout.attrs["karabo_id"] = karabo_id
fout.attrs["da_name"] = da_name
print("closing")
```
......@@ -206,30 +206,15 @@ notebooks = {
"FF_HISTS": {
"notebook":
"notebooks/Jungfrau/gainCal_JF_Create_Spectra_Histos.ipynb",
"dep_notebooks": [
"notebooks/Jungfrau/gainCal_JF_Fit_Spectra_Histos.ipynb"],
"concurrency": {"parameter": None,
"default concurrency": None,
"concurrency": {"parameter": "karabo_da",
"default concurrency": list(range(8)),
"cluster cores": 4},
},
"FF": {
"notebook":
"notebooks/Jungfrau/create_gain_map.ipynb",
"dep_notebooks": [
"notebooks/Jungfrau/gainCal_JF_Fit_sendDB_NBC.ipynb"],
"concurrency": {"parameter": None,
"default concurrency": None,
"cluster cores": 4},
},
"FF_ALL": {
"notebook":
"notebooks/Jungfrau/gainCal_JF_Create_Spectra_Histos.ipynb",
"dep_notebooks": [
"notebooks/Jungfrau/gainCal_JF_Fit_Spectra_Histos.ipynb",
"notebooks/Jungfrau/create_gain_map.ipynb",
"notebooks/Jungfrau/gainCal_JF_Fit_sendDB_NBC.ipynb",],
"concurrency": {"parameter": None,
"default concurrency": None,
"concurrency": {"parameter": "karabo_da",
"default concurrency": list(range(8)),
"cluster cores": 4},
},
},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment