From b1c158da901f19cce4af1340727389ae2dbdf615 Mon Sep 17 00:00:00 2001 From: Steinn Ymir Agustsson Date: Tue, 24 Oct 2023 22:03:50 +0200 Subject: [PATCH] energy calibration notebook and config --- tutorial/5 - hextof workflow.ipynb | 367 +++++++++++++++++++++++++++-- tutorial/hextof_config.yaml | 135 +++++++++++ 2 files changed, 486 insertions(+), 16 deletions(-) create mode 100644 tutorial/hextof_config.yaml diff --git a/tutorial/5 - hextof workflow.ipynb b/tutorial/5 - hextof workflow.ipynb index 35f27197..f8f1316f 100644 --- a/tutorial/5 - hextof workflow.ipynb +++ b/tutorial/5 - hextof workflow.ipynb @@ -14,8 +14,16 @@ "\n", "%matplotlib inline\n", "# %matplotlib ipympl\n", - "import matplotlib.pyplot as plt\n", - "\n" + "import matplotlib.pyplot as plt" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%matplotlib widget" ] }, { @@ -32,7 +40,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# with energy calibration" + "# Loading Data" ] }, { @@ -54,13 +62,86 @@ "sp.align_dld_sectors()" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Energy Calibration" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## using lmfit" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "axes = ['sampleBias', 'dldTimeSteps']\n", + "bins = [6, 500]\n", + "ranges = [[28,33], [4000, 4800]]\n", + "res = sp.compute(bins=bins, axes=axes, ranges=ranges)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sp.load_bias_series(binned_data=res)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ranges=(4250, 4500)\n", + "ref_id=3\n", + "sp.find_bias_peaks(ranges=ranges, ref_id=ref_id)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ref_id=3\n", + "ref_energy=0\n", + "sp.calibrate_energy_axis(\n", + " ref_id=ref_id, \n", + " ref_energy=ref_energy,\n", + " method=\"lmfit\",\n", + " energy_scale='kinetic',\n", + " d={'value':1e9, 'min': 1e8, 'max': 1e10},\n", + " E0={'value':0, 'min': -100, 'max': 100,'vary':True},\n", + " t0={'value':225, 'min': -100, 'max': 250, 'vary':True},\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sp.append_energy_axis()" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "sp.append_energy_axis()\n", "sp.append_tof_ns_axis()" ] }, @@ -73,6 +154,29 @@ "sp.dataframe[['dldTime','dldTimeSteps','energy','dldSectorID']].head()" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "axes = ['sampleBias', 'energy']\n", + "bins = [5, 500]\n", + "ranges = [[28,33], [-10,10]]\n", + "res = sp.compute(bins=bins, axes=axes, ranges=ranges)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "plt.figure()\n", + "res.mean('sampleBias').plot.line(x='energy',linewidth=3);\n", + "res.plot.line(x='energy',linewidth=1,alpha=.5,label='all');\n" + ] + }, { "cell_type": "code", "execution_count": null, @@ -80,9 +184,9 @@ "outputs": [], "source": [ "sp.apply_energy_offset(\n", - " constant=31.6, \n", + " constant=-31.5, \n", " columns=['sampleBias'],\n", - " signs=[-1],\n", + " signs=[+1],\n", ")" ] }, @@ -94,7 +198,58 @@ "source": [ "axes = ['sampleBias', 'energy']\n", "bins = [5, 500]\n", - "ranges = [[28,33], [-1,5]]\n", + "ranges = [[28,33], [-3,2]]\n", + "res_fit = sp.compute(bins=bins, axes=axes, ranges=ranges)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "plt.figure()\n", + "ax = plt.subplot(111)\n", + "res_fit.energy.attrs['unit'] = 'eV'\n", + "res_fit.mean('sampleBias').plot.line(x='energy',linewidth=3, ax=ax);\n", + "res_fit.plot.line(x='energy',linewidth=1,alpha=.5,label='all',ax=ax);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## with poly fit" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sp = SedProcessor(runs=[44797], config=config_file, collect_metadata=False)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sp.add_jitter()\n", + "sp.align_dld_sectors()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "axes = ['sampleBias', 'dldTimeSteps']\n", + "bins = [6, 500]\n", + "ranges = [[28,33], [4000, 4800]]\n", "res = sp.compute(bins=bins, axes=axes, ranges=ranges)" ] }, @@ -104,7 +259,55 @@ "metadata": {}, "outputs": [], "source": [ - "%matplotlib widget" + "sp.load_bias_series(binned_data=res)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ranges=(4250, 4500)\n", + "ref_id=3\n", + "sp.find_bias_peaks(ranges=ranges, ref_id=ref_id)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ref_id=3\n", + "ref_energy=-0.3\n", + "sp.calibrate_energy_axis(\n", + " ref_id=ref_id, \n", + " ref_energy=-0.3, \n", + " method=\"lstsq\",\n", + " order=2,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sp.append_energy_axis()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "axes = ['sampleBias', 'energy']\n", + "bins = [5, 500]\n", + "ranges = [[28,33], [-10,10]]\n", + "res = sp.compute(bins=bins, axes=axes, ranges=ranges)" ] }, { @@ -124,7 +327,11 @@ "metadata": {}, "outputs": [], "source": [ - "sp.dataframe['binding_energy'] = -sp.dataframe['energy']" + "sp.apply_energy_offset(\n", + " constant=-31.5, \n", + " columns=['sampleBias'],\n", + " signs=[+1],\n", + ")" ] }, { @@ -133,10 +340,10 @@ "metadata": {}, "outputs": [], "source": [ - "axes = ['sampleBias', 'binding_energy']\n", + "axes = ['sampleBias', 'energy']\n", "bins = [5, 500]\n", - "ranges = [[28,33], [-5,1]]\n", - "res = sp.compute(bins=bins, axes=axes, ranges=ranges)" + "ranges = [[28,33], [-3,2]]\n", + "res_poly = sp.compute(bins=bins, axes=axes, ranges=ranges)" ] }, { @@ -147,9 +354,19 @@ "source": [ "plt.figure()\n", "ax = plt.subplot(111)\n", - "res.binding_energy.attrs['unit'] = 'eV'\n", - "res.mean('sampleBias').plot.line(x='binding_energy',linewidth=3, ax=ax);\n", - "res.plot.line(x='binding_energy',linewidth=1,alpha=.5,label='all',ax=ax);" + "res_poly.energy.attrs['unit'] = 'eV'\n", + "res_poly.mean('sampleBias').plot.line(x='energy',linewidth=3, ax=ax);\n", + "res_poly.plot.line(x='energy',linewidth=1,alpha=.5,label='all',ax=ax);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# From manual values\n", + "these are obviously not the right values, I think I got them from an other calibraiton file.\n", + "They are here to show how the calibration works.\n", + "Also, I noticed the parameter energy_scale from the config has no effect..." ] }, { @@ -157,7 +374,125 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "sp = SedProcessor(runs=[44797], config=config_file, collect_metadata=False)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sp.add_jitter()\n", + "sp.align_dld_sectors()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sp.append_energy_axis()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sp.apply_energy_offset(\n", + " constant=+31.5, \n", + " columns=['sampleBias'],\n", + " signs=[-1],\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "axes = ['sampleBias', 'energy']\n", + "bins = [5, 500]\n", + "ranges = [[28,33], [-10,5]]\n", + "res_config = sp.compute(bins=bins, axes=axes, ranges=ranges)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "plt.figure()\n", + "ax = plt.subplot(111)\n", + "res_config.energy.attrs['unit'] = 'eV'\n", + "res_config.mean('sampleBias').plot.line(x='energy',linewidth=3, ax=ax);\n", + "res_config.plot.line(x='energy',linewidth=1,alpha=.5,label='all',ax=ax);" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sp.dataframe['energy'] = - sp.dataframe['energy']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "axes = ['sampleBias', 'energy']\n", + "bins = [5, 500]\n", + "ranges = [[28,33], [-3,2]]\n", + "res_config = sp.compute(bins=bins, axes=axes, ranges=ranges)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "plt.figure()\n", + "ax = plt.subplot(111)\n", + "res_config.energy.attrs['unit'] = 'eV'\n", + "res_config.mean('sampleBias').plot.line(x='energy',linewidth=3, ax=ax);\n", + "res_config.plot.line(x='energy',linewidth=1,alpha=.5,label='all',ax=ax);" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# compare the two methods\n", + "fig, ax = plt.subplots(1,3, figsize=(10,4), layout='constrained')\n", + "res_poly.energy.attrs['unit'] = 'eV'\n", + "res_poly.mean('sampleBias').plot.line(x='energy',linewidth=3, ax=ax[0], label='all');\n", + "res_poly.plot.line(x='energy',linewidth=1,alpha=.5,ax=ax[0]);\n", + "ax[0].set_title('poly')\n", + "res_fit.energy.attrs['unit'] = 'eV'\n", + "res_fit.mean('sampleBias').plot.line(x='energy',linewidth=3, ax=ax[1], label='all');\n", + "res_fit.plot.line(x='energy',linewidth=1,alpha=.5,ax=ax[1]);\n", + "ax[1].set_title('fit')\n", + "res_config.energy.attrs['unit'] = 'eV'\n", + "res_config.mean('sampleBias').plot.line(x='energy',linewidth=3, ax=ax[2]);\n", + "res_config.plot.line(x='energy',linewidth=1,alpha=.5,label='all',ax=ax[2]);\n", + "ax[2].set_title('config')\n", + "ax[0].set_xlim(-2, 1.5)\n", + "ax[1].set_xlim(-2, 1.5)\n", + "ax[2].set_xlim(-2, 1.5)" + ] } ], "metadata": { diff --git a/tutorial/hextof_config.yaml b/tutorial/hextof_config.yaml new file mode 100644 index 00000000..d9a4178b --- /dev/null +++ b/tutorial/hextof_config.yaml @@ -0,0 +1,135 @@ +core: + loader: flash + beamtime_id: 11013410 + year: 2023 + beamline: pg2 + instrument: hextof + paths: + data_raw_dir: "/asap3/flash/gpfs/pg2/2023/data/11019101/raw/hdf/offline/fl1user3" + # change this to a local directory where you want to store the parquet files + data_parquet_dir: "/home/agustsss/temp/sed_parquet" + +binning: + num_cores: 10 + +dataframe: + ubid_offset: 5 + daq: fl1user3 + forward_fill_iterations: 2 + split_sector_id_from_dld_time: True + sector_id_reserved_bits: 3 + x_column: dldPosX + corrected_x_column: "X" + kx_column: "kx" + y_column: dldPosY + corrected_y_column: "Y" + ky_column: "ky" + tof_column: dldTimeSteps + tof_ns_column: dldTime + corrected_tof_column: "tm" + bias_column: "sampleBias" + tof_binwidth: 0.020576131995767355 + tof_binning: 3 # with 3, 8 bins per step 2**3 + sector_id_column: dldSectorID + sector_delays: [0., 0., 0., 0., 0., 0., 0., 0.] + jitter_cols: ["dldPosX", "dldPosY", "dldTimeSteps"] + + units: + dldPosX: 'step' + dldPosY: 'step' + dldTimeSteps: 'step' + tof_voltage: 'V' + extractorVoltage: 'V' + extractorCurrent: 'A' + cryoTemperature: 'K' + sampleTemperature: 'K' + dldTime: 'ns' + # delay: 'ps' + timeStamp: 's' + # energy: 'eV' + # E: 'eV' + kx: '1/A' + ky: '1/A' + + channels: + timeStamp: + format: per_train + group_name: "/uncategorised/FLASH.DIAG/TIMINGINFO/TIME1.BUNCH_FIRST_INDEX.1/" + pulseId: + format: per_electron + group_name: "/uncategorised/FLASH.EXP/HEXTOF.DAQ/DLD1/" + slice: 2 + dldPosX: + format: per_electron + group_name: "/uncategorised/FLASH.EXP/HEXTOF.DAQ/DLD1/" + slice: 1 + dldPosY: + format: per_electron + group_name: "/uncategorised/FLASH.EXP/HEXTOF.DAQ/DLD1/" + slice: 0 + dldTimeSteps: + format: per_electron + group_name: "/uncategorised/FLASH.EXP/HEXTOF.DAQ/DLD1/" + slice: 3 + dldAux: + format: per_pulse + group_name : "/uncategorised/FLASH.EXP/HEXTOF.DAQ/DLD1/" + slice: 4 + dldAuxChannels: + sampleBias: 0 + tofVoltage: 1 + extractorVoltage: 2 + extractorCurrent: 3 + cryoTemperature: 4 + sampleTemperature: 5 + crystalVoltage: 6 + dldTimeBinSize: 15 + pulserSignAdc: # ADC containing the pulser sign (1: value approx. 35000, 0: 33000) + format: per_pulse + group_name: "/FL1/Experiment/PG/SIS8300 100MHz ADC/CH6/TD/" + #slice: 0 + monochromatorPhotonEnergy: + format: per_train + group_name: "/FL1/Beamlines/PG/Monochromator/monochromator photon energy/" + gmdBda: # The GMDs can not be read yet... + format: per_train + group_name: "/FL1/Photon Diagnostic/GMD/Average energy/energy BDA/" + # slice: ":" + #gmdTunnel: # The GMDs can not be read yet... + # format: per_pulse + # group_name: "/FL1/Photon Diagnostic/GMD/Pulse resolved energy/energy tunnel/" + # slice: ":" + bam: # Here we use the DBC2 BAM as the "normal" one is broken. + format: per_pulse + group_name: "/uncategorised/FLASH.SDIAG/BAM.DAQ/FL0.DBC2.ARRIVAL_TIME.ABSOLUTE.SA1.COMP/" + delayStage: + format: per_train + group_name: "/zraw/FLASH.SYNC/LASER.LOCK.EXP/F1.PG.OSC/FMC0.MD22.1.ENCODER_POSITION.RD/dGroup/" + + stream_name_prefixes: + pbd: "GMD_DATA_gmd_data" + pbd2: "FL2PhotDiag_pbd2_gmd_data" + fl1user1: "FLASH1_USER1_stream_2" + fl1user2: "FLASH1_USER2_stream_2" + fl1user3: "FLASH1_USER3_stream_2" + fl2user1: "FLASH2_USER1_stream_2" + fl2user2: "FLASH2_USER2_stream_2" + + beamtime_dir: + pg2: "/asap3/flash/gpfs/pg2/" + hextof: "/asap3/fs-flash-o/gpfs/hextof/" + wespe: "/asap3/fs-flash-o/gpfs/wespe/" + +energy: + calibration: + offset: 4150.0 + coeffs: [-2.01882455e-05, 1.94714008e-01] + Tmat: [[ 1.01040e+06, 1.20000e+02], + [ 7.18675e+05, 8.50000e+01], + [ 3.82275e+05, 4.50000e+01], + [-3.86325e+05, -4.50000e+01]] + bvec: [ 3., 2., 1., -1.] + energy_scale: kinetic + axis: -463.3385531514501 + E0: -463.3385531514501 + refid: 3