{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "**Example of double group sequential fit with optional fft:**\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 = '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",
    "dashfit = modelname+'.'+re.search(r'\\d+', runlist).group()+'.'+groupcalibfile[:groupcalibfile.index('.')]+'.'+version+'_fit'+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.1.json\n",
      " _____________________________________________________________________________________\n",
      "| Run 822: MnBi6Te10 pellet 30.0K 10mT     on group: 2 - 1   α = 1.130    |\n",
      "| χᵣ² = 1.042(0.980,1.020), fit on [7.82ns, 9.4µs, 1.95ns/bin], at 03/01/2022 12:54:03|\n",
      "|-------------------------------------------------------------------------------------|\n",
      "| mg: A_T_fast = 0.063(8) B_T_fast = 10.016(7) φ_T_fast = -55.9(7) σ_T_fast = 1.5(2)\n",
      "| ml: A_T_slow = 0.179(9) B_T_slow = 10.016(7) φ_T_slow = -55.9(7) λ_T_slow = 0.05(1)\n",
      "|_____________________________________________________________________________________|\n",
      " _____________________________________________________________________________________\n",
      "| Run 822: MnBi6Te10 pellet 30.0K 10mT     on group: 3 - 4   α = 1.130    |\n",
      "| χᵣ² = 1.037(0.980,1.020), fit on [7.82ns, 9.4µs, 1.95ns/bin], at 03/01/2022 12:54:03|\n",
      "|-------------------------------------------------------------------------------------|\n",
      "| mg: A_T_fast = 0.057(4) B_T_fast = 10.019(4) φ_T_fast = 35.1(3) σ_T_fast = -1.7(2)\n",
      "| ml: A_T_slow = 0.199(5) B_T_slow = 10.019(4) φ_T_slow = 35.1(3) λ_T_slow = 0.078(8)\n",
      "|_____________________________________________________________________________________|\n",
      ".  Log added to mgml.1.2-1.csv\n",
      ".  Log added to mgml.1.3-4.csv\n",
      "Best fit of group 2-1 saved in /home/roberto.derenzi/git/mujpy/log/mgml.822.2-1.1_fit.json \n",
      "Best fit of group 3-4 saved in /home/roberto.derenzi/git/mujpy/log/mgml.822.3-4.1_fit.json \n"
     ]
    }
   ],
   "source": [
    "print('Model defined by dashboard {}'.format(logpath+dashboard))\n",
    "the_fit = mufit(the_suite,logpath+dashboard)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Multigroup fit animation: toggle pause/resume by clicking on the plot\n",
      "Multigroup fft animation: toggle pause/resume by clicking on the plot\n",
      "Autophase 0: Optimization terminated successfully.\n",
      "Autophase 1: Optimization terminated successfully.\n"
     ]
    }
   ],
   "source": [
    "fit_plot= mufitplot('0,2000,10,20000,40',the_fit,fft_range=(0.,5.,0.1),real=True)#,guess=True) # '0,1000,4,24000,100' # two range plot"
   ]
  }
 ],
 "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
}
