Skip to content
Fast_analysis.ipynb 480 KiB
Newer Older
Alice Donini's avatar
Alice Donini committed
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e7797698-4ef1-459b-aba6-70f5a19700fe",
   "metadata": {},
   "source": [
    "# Fast theta2 plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5c70feea-56cc-47e0-ac6f-1a1badb72b6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import astropy.units as u\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.style as style\n",
    "from matplotlib.offsetbox import AnchoredText\n",
    "\n",
    "from lstchain.io.io import dl2_params_lstcam_key\n",
    "from lstchain.reco.utils import (\n",
    "    compute_theta2,\n",
    "    extract_source_position,\n",
    "    clip_alt,\n",
    "    get_effective_time,\n",
    "    radec_to_camera,\n",
    ")\n",
    "from ctapipe.containers import EventType\n",
    "from ctapipe.coordinates import CameraFrame\n",
    "from astropy.coordinates import (\n",
    "    ICRS,\n",
    "    Galactic,\n",
    "    FK4,\n",
    "    FK5,\n",
    "    AltAz,\n",
    "    SkyCoord,\n",
    "    EarthLocation,\n",
    ")\n",
    "from astropy.time import Time\n",
    "from gammapy.stats import WStatCountsStatistic\n",
    "\n",
    "style.use('tableau-colorblind10')\n",
    "plt.rcParams['font.size'] = 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "13478fbb-ce71-40a7-ade3-ba24fd7bf3a8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lstchain: 0.9.2\n"
     ]
    }
   ],
   "source": [
    "import lstchain\n",
    "print(\"lstchain:\", lstchain.__version__)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee6d5a07-31ac-4037-a344-d4d41bc1f301",
   "metadata": {
    "tags": []
   },
   "source": [
    "## This function gets the source position in camera coordinates for any source"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c64aebd0-c499-45aa-b093-5457eab3110e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def extract_source_position_from_coord(\n",
    "    data, coord, equivalent_focal_length=28 * u.m\n",
    "):\n",
    "    \"\"\"\n",
    "    Extract source position from data\n",
    "    Parameters:\n",
    "    -----------\n",
    "    pandas.DataFrame data: input data\n",
    "    str observed_source_name: Name of the observed source\n",
    "    astropy.units.m equivalent_focal_length: Equivalent focal length of a telescope\n",
    "    Returns:\n",
    "    --------\n",
    "    2D array of coordinates of the source in form [(x),(y)] in astropy.units.m\n",
    "    \"\"\"\n",
    "\n",
    "    obstime = pd.to_datetime(data[\"dragon_time\"], unit=\"s\")\n",
    "    pointing_alt = u.Quantity(data[\"alt_tel\"], u.rad, copy=False)\n",
    "    pointing_az = u.Quantity(data[\"az_tel\"], u.rad, copy=False)\n",
    "    source_pos_camera = radec_to_camera(\n",
    "        coord,\n",
    "        obstime,\n",
    "        pointing_alt,\n",
    "        pointing_az,\n",
    "        focal=equivalent_focal_length,\n",
    "    )\n",
    "    source_position = [source_pos_camera.x, source_pos_camera.y]\n",
    "    return source_position"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04be8310-f669-4d4b-9bc3-3d28ce3557b6",
   "metadata": {},
   "source": [
    "## Choose input files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "d86c8d23-6cdd-4203-8b74-080d5729581b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 0 ns, sys: 17 µs, total: 17 µs\n",
      "Wall time: 32.2 µs\n"
     ]
    }
   ],
   "source": [
    "%time\n",
    "data=pd.DataFrame()\n",
    "base_dir = '/fefs/aswg/workspace/alice.donini/Analysis/data/DL2/OT081/03_04/'\n",
    "runs=['7247', '7248', '7249', '7250', '7268', '7269', '7270', '7271'] # You can concatenate several runs\n",
    "for run in runs:\n",
    "    input_file = base_dir + 'dl2_LST-1.Run0' + run + '.h5'\n",
    "    data = pd.concat([data, pd.read_hdf(input_file, key=dl2_params_lstcam_key)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "6d3c7a4c-7962-4996-9fa1-63031759e082",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>obs_id</th>\n",
       "      <th>event_id</th>\n",
       "      <th>intensity</th>\n",
       "      <th>log_intensity</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>r</th>\n",
       "      <th>phi</th>\n",
       "      <th>length</th>\n",
       "      <th>length_uncertainty</th>\n",
       "      <th>...</th>\n",
       "      <th>reco_disp_dx</th>\n",
       "      <th>reco_disp_dy</th>\n",
       "      <th>reco_src_x</th>\n",
       "      <th>reco_src_y</th>\n",
       "      <th>signed_time_gradient</th>\n",
       "      <th>signed_skewness</th>\n",
       "      <th>reco_alt</th>\n",
       "      <th>reco_az</th>\n",
       "      <th>reco_type</th>\n",
       "      <th>gammaness</th>\n",
       "    </tr>\n",
       "  </thead>\n",
Loading full blame...