{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Fitting a real point source with particle weights\n", "\n", "_Weights used:_\n", "* Spatial\n", "* Particle\n", "* Spectral\n", "\n", "This jupyter notebook fits for the phase-averaged polarization of the point source GX 301-2, observed with observation ID 01002601, with particle weights. The previous notebook described the fit without particle weights. Particle weights were created two notebooks ago with the `leakagelib_bkg` script." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[93m>>> PyXSPEC is not installed, you will no be able to use it.\u001b[0m\n" ] } ], "source": [ "import leakagelib" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We start the fit in the same manner as the last notebook:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ ">>> Reading (in memory) /opt/homebrew/anaconda3/lib/python3.12/site-packages/ixpeobssim/caldb/ixpe/xrt/bcf/vign/ixpe_d2_obssim20240101_vign_v013.fits...\n", "WARNING: FITSFixedWarning: RADECSYS= 'ICRS ' / Celestial coordinate system \n", "the RADECSYS keyword is deprecated, use RADESYSa. [astropy.wcs.wcs]\n", "WARNING: FITSFixedWarning: 'datfix' made the change 'Set DATEREF to '2017-01-01T00:01:09.184' from MJDREF.\n", "Set MJD-OBS to 59789.109204 from DATE-OBS.\n", "Set MJD-END to 59794.248844 from DATE-END'. [astropy.wcs.wcs]\n", ">>> Reading (in memory) /opt/homebrew/anaconda3/lib/python3.12/site-packages/ixpeobssim/caldb/ixpe/xrt/bcf/vign/ixpe_d1_obssim20240101_vign_v013.fits...\n", ">>> Reading (in memory) /opt/homebrew/anaconda3/lib/python3.12/site-packages/ixpeobssim/caldb/ixpe/xrt/bcf/vign/ixpe_d3_obssim20240101_vign_v013.fits...\n", "59535 events were cut for being outside the region of interest.\n" ] } ], "source": [ "datas = leakagelib.IXPEData.load_all_detectors_with_path(\"data\", \"01002601\", event_dir=\"event_l2_weight\")\n", "\n", "for data in datas:\n", " data.iterative_centroid_center()\n", " data.retain(data.evt_energies > 2)\n", " data.retain(data.evt_energies < 8)\n", "\n", "settings = leakagelib.FitSettings(datas)\n", "settings.apply_circular_roi(280)\n", "\n", "settings.add_point_source() # Named \"src\" by default\n", "settings.fix_flux(\"src\", 1)\n", "settings.set_initial_qu(\"src\", (0.5,0))\n", "\n", "settings.add_background() # Named \"bkg\" by default\n", "settings.set_initial_qu(\"bkg\", (0, 0)) # Assume an unpolarized background\n", "settings.set_initial_flux(\"bkg\", 0.1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Adding particle weights is done simply by including a particle source. " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "settings.add_particle_background() # Named \"pbkg\" by default\n", "settings.set_initial_flux(\"pbkg\", 0.1)\n", "settings.set_initial_qu(\"pbkg\", (0, 0))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook doesn't use spectral weights, but if we had, LeakageLib would automatically use a power-law spectrum with Gamma=1.6 for particles, unmodified by the ARF or RMF. If you wish to overwrite this change, you can set the particle spectrum of the `pbkg` source yourself.\n", "\n", "Rerunning the fit removes the warning stating that the particle weights are not being used." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "FitResult:\n", "\tq (src) = 0.0021 +/- 0.0056\n", "\tu (src) = -0.0043 +/- 0.0056\n", "\tq (bkg) = -0.0037 +/- 0.0336\n", "\tu (bkg) = 0.0048 +/- 0.0336\n", "\tq (pbkg) = 0.0192 +/- 0.0087\n", "\tu (pbkg) = 0.0094 +/- 0.0086\n", "\tf (bkg) = 0.0650 +/- 0.0006\n", "\tf (pbkg) = 0.0773 +/- 0.0005\n", "\n", "Polarization:\n", "\tPD (src): 0.0048 +/- 0.0056\n", "\tPA (src): -31.9532 deg +/- 33.5076\n", "\tPD (bkg): 0.0060 +/- 0.0336\n", "\tPA (bkg): 63.8590 deg +/- 159.9714\n", "\tPD (pbkg): 0.0214 +/- 0.0086\n", "\tPA (pbkg): 13.0802 deg +/- 11.5500\n", "Likelihood 1434098.7507091842, dof 447933\n", "Optimization terminated successfully." ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fitter = leakagelib.Fitter(settings)\n", "result = fitter.fit()\n", "result" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Based on the best-fit flux of the photon and particle backgrounds, it appears slightly more than half of background events were particles, which demonstrates the importance of particle treatment.\n", "\n", "You might notice that the particles were marginally \"polarized\", with a PD of 1.6 +/- 0.9%. This isn't a physical polarization; the results simply indicate that the tracks have slight angular dependence.\n", "\n", "In reality, IXPE's photon and particle backgrounds differ between detectors both in flux and polarization. It would be best to fit independent background sources to each detector. This is done in leakagelib by supplying the `det` argument in `add_background` or `add_particle_background`. E.g.\n", "\n", "```\n", "settings.add_background(\"bkg1\", det=(1,))\n", "settings.add_background(\"bkg2\", det=(2,))\n", "settings.add_background(\"bkg3\", det=(3,))\n", "\n", "settings.add_particle_background(\"pbkg1\", det=(1,))\n", "settings.add_particle_background(\"pbkg2\", det=(2,))\n", "settings.add_particle_background(\"pbkg3\", det=(3,))\n", "```" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 2 }