{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Example of A21 single run double group global fit:**\n",
    "\n",
    "MAKE SURE YOU RENEWED ACCESS TO DATABASE!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/roberto.derenzi/git/mujpy\n"
     ]
    }
   ],
   "source": [
    "%matplotlib tk\n",
    "%cd /home/roberto.derenzi/git/mujpy/\n",
    "from mujpy.musuite import suite\n",
    "import json, re\n",
    "from os.path import isfile\n",
    "from mujpy.mufit import mufit\n",
    "from mujpy.mufitplot import mufitplot\n",
    "jsonsuffix = '.json'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "logpath = '/home/roberto.derenzi/git/mujpy/log/'\n",
    "datafile = '/home/roberto.derenzi/musrfit/MBT/gps/run_05_21/data/deltat_tdc_gps_0822.bin'\n",
    "runlist = '822' # first run first\n",
    "modelname = 'mgml'\n",
    "version = 'gg_1'\n",
    "grp_calib = [{'forward':'2', 'backward':'1', 'alpha':1.13},{'forward':'3', 'backward':'4', 'alpha':1.13}]\n",
    "groupcalibfile = '2-1_3-4.calib'\n",
    "inputsuitefile = 'input.suite'\n",
    "dashboard = modelname+'.'+re.search(r'\\d+', runlist).group()+'.'+groupcalibfile[:groupcalibfile.index('.')]+'.'+version+jsonsuffix\n",
    "if not isfile(logpath+dashboard): \n",
    "    print('Model definition dashboard file {} does not exist. Make one.'.format(logpath+dashboard))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "******************* SUITE *********************\n",
      "Run /home/roberto.derenzi/musrfit/MBT/gps/run_05_21/data/deltat_tdc_gps_0822.bin loaded\n",
      " MnBi6Te10 pellet 30.0K 10mT\n",
      "Succesfully completed prompt Minuit fit, check nt0, dt0 \n",
      "****************END OF SUITE*****************\n"
     ]
    }
   ],
   "source": [
    "#  can add 'scan':'T' or 'B' for orderinng csv for increasing T, B, otherwise increasing nrun\n",
    "input_suite = {'console':'print',\n",
    "                   'datafile':datafile,\n",
    "                   'logpath':logpath,\n",
    "                   'runlist':runlist,\n",
    "                   'groups calibration':groupcalibfile,\n",
    "                   'offset':20\n",
    "                  }  # 'console':logging, for output in Log Console, 'console':print, for output in notebook\n",
    "with open(logpath+inputsuitefile,\"w\") as f:\n",
    "    json.dump(input_suite,f)\n",
    "\n",
    "with open(logpath+groupcalibfile,\"w\") as f:\n",
    "    json.dump(grp_calib,f)\n",
    "    \n",
    "the_suite = suite(logpath+inputsuitefile,mplot=False) # the_suite implements the class suite according to input.suite"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model defined by dashboard /home/roberto.derenzi/git/mujpy/log/mgml.822.2-1_3-4.gg_1.json\n",
      "suite asymmetry_single debug: run 822, kgroup 0 raw experr zeros 24126\n",
      " ________________________________________________________________________________________________\n",
      "| Run 822: MnBi6Te10 pellet 30.0K 10mT    Global fit of 08/01/2022 11:21:32              |\n",
      "| χᵣ² = 1.036(0.986,1.014) ,    on [7.82ns, 9.4µs, 1.95ns/bin]                                   |\n",
      "|----------------------------------------------------------------- on group: 2 - 1   α = 1.130   |\n",
      "| mg: A_T_fast = 0.10537(9) B_T_fast = 10.30(9) φ_T_fast = -58.2(7) σ_T_fast = 0.9(1)\n",
      "| ml: A_T_slow = 0.158(3) B_T_slow = 10.017(4) φ_T_slow = -58.2(7) λ_T_slow = 0.04(1)\n",
      "|----------------------------------------------------------------- on group: 3 - 4   α = 1.130   |\n",
      "| mg: A_T_fast = 0.09684(9) B_T_fast = 10.30(9) φ_T_fast = 33.6(5) σ_T_fast = 0.9(1)\n",
      "| ml: A_T_slow = 0.166(3) B_T_slow = 10.017(4) φ_T_slow = 33.6(5) λ_T_slow = 0.04(1)\n",
      "|________________________________________________________________________________________________|\n",
      "Best fit saved in /home/roberto.derenzi/git/mujpy/log/mgml.822.2-1_3-4.gg_1_fit.json .  Log added to mgml.gg_1.2-1_3-4.csv\n"
     ]
    }
   ],
   "source": [
    "print('Model defined by dashboard {}'.format(logpath+dashboard))\n",
    "the_fit = mufit(the_suite,logpath+dashboard)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Multigroup fit animation: toggle pause/resume by clicking on the plot\n",
      "suite asymmetry_single debug: run 822, kgroup 0 raw experr zeros 24126\n",
      "Exiting mufitplot without a plot\n"
     ]
    }
   ],
   "source": [
    "fit_plot= mufitplot('0,2000,10,15000,40',the_fit)#,guess=True) # '0,1000,4,24000,100' # two range plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "25600"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(the_suite._the_runs_[0][0].get_histo_vector(0,1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "25178"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "the_suite.histoLength"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
