{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "**Example of global fit of single group, multirun suite**\n",
    "\n",
    "MAKE SURE YOU RENEWED ACCESS TO DATABASE!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/roberto.derenzi/git/mujpy\n"
     ]
    }
   ],
   "source": [
    "%matplotlib tk\n",
    "from os import getcwd\n",
    "startuppath = getcwd() # before changing to git mujpy (if mujpy installed this is not needed)\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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "******************* SUITE *********************\n",
      "/afs/psi.ch/project/bulkmusr/data/gps/d2019/tdc/deltat_tdc_gps_2507.bin loaded\n",
      "Run 2507: 1212 15% 180.4 5.0K 200mT\n",
      "/afs/psi.ch/project/bulkmusr/data/gps/d2019/tdc/deltat_tdc_gps_2508.bin loaded\n",
      "Run 2508: 1212 15% 180.4 12.0K 200mT\n",
      "/afs/psi.ch/project/bulkmusr/data/gps/d2019/tdc/deltat_tdc_gps_2509.bin loaded\n",
      "Run 2509: 1212 15% 180.4 20.0K 200mT\n",
      "/afs/psi.ch/project/bulkmusr/data/gps/d2019/tdc/deltat_tdc_gps_2510.bin loaded\n",
      "Run 2510: 1212 15% 180.4 30.0K 200mT\n",
      "/afs/psi.ch/project/bulkmusr/data/gps/d2019/tdc/deltat_tdc_gps_2511.bin loaded\n",
      "Run 2511: 1212 15% 180.4 40.0K 200mT\n",
      "/afs/psi.ch/project/bulkmusr/data/gps/d2019/tdc/deltat_tdc_gps_2512.bin loaded\n",
      "Run 2512: 1212 15% 180.4 50.0K 200mT\n",
      "/afs/psi.ch/project/bulkmusr/data/gps/d2019/tdc/deltat_tdc_gps_2513.bin loaded\n",
      "Run 2513: 1212 15% 180.4 60.0K 200mT\n",
      "/afs/psi.ch/project/bulkmusr/data/gps/d2019/tdc/deltat_tdc_gps_2514.bin loaded\n",
      "Run 2514: 1212 15% 180.4 70.0K 200mT\n",
      "/afs/psi.ch/project/bulkmusr/data/gps/d2019/tdc/deltat_tdc_gps_2515.bin loaded\n",
      "Run 2515: 1212 15% 180.4 75.0K 200mT\n",
      "/afs/psi.ch/project/bulkmusr/data/gps/d2019/tdc/deltat_tdc_gps_2516.bin loaded\n",
      "Run 2516: 1212 15% 180.4 80.0K 200mT\n",
      "/afs/psi.ch/project/bulkmusr/data/gps/d2019/tdc/deltat_tdc_gps_2517.bin loaded\n",
      "Run 2517: 1212 15% 180.4 85.0K 200mT\n"
     ]
    }
   ],
   "source": [
    "datafile = '/afs/psi.ch/project/bulkmusr/data/gps/d2019/tdc/deltat_tdc_gps_2507.bin'\n",
    "runlist = '2507:2517' # suite of runs\n",
    "offset = '20'\n",
    "grp_calib = [{'forward':'3', 'backward':'4', 'alpha':1.029}]\n",
    "the_suite = suite(datafile, runlist , grp_calib , offset, startuppath) # load the data\n",
    "version = '1'\n",
    "logpath = startuppath+'/fit/' # where the manually built json is \n",
    "# this json contains fit range, ..., and parameters defined by dictionaries arranged into\n",
    "#      user parameters, global\n",
    "#      user parameters, local (replicated for each run)\n",
    "#      model parameters, local (\"flag\":\"~\") (>>)\n",
    "#      model parameters, defined by a function of user parameters (\"flag\":\"=\"))\n",
    "# open an example and modify it, \n",
    "dashboard = 'mgmgmg.2507-17.3-4.1_g.json' # filename\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": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "migrad converged in 133.80810952186584 s, ... now redo without limits\n",
      "Without limits in 14.086013317108154 s\n",
      "migrad in 14.086013317108154 s, hesse in 14.329673051834106 s\n",
      " ________________________________________________________________________________________________________________________\n",
      "| Runs 2507-2517: 1212 15% 180.4 5.0K 200mT  Global fit 15/11/2022 16:05:24 on group: 3 - 4   α = 1.029                  |\n",
      "| χᵣ² = 1.020(0.994,1.006) ,    on [7.83ns, 7.8µs, 1.56ns/bin]                                                           |\n",
      "| A0 = 0.2199(5)        Asc = 0.207(1)        B0 = 200.06(1)        φ0 = 48.5(2)          σ0 = 0.0(1)                    |\n",
      "|........................................................................................................................|\n",
      "| x0 = 0.49(9)          mgB_s10 = 198.4(1)    σ_s10 = 3.46(9)       mgB_s20 = 200.26(4)   σ_s20 = 1.16(6)                |\n",
      "| x1 = 0.26(9)          mgB_s11 = 198.6(2)    σ_s11 = 3.17(9)       mgB_s21 = 200.44(3)   σ_s21 = 1.10(5)                |\n",
      "| x2 = 0.30(9)          mgB_s12 = 198.7(1)    σ_s12 = 2.75(6)       mgB_s22 = 200.56(3)   σ_s22 = 1.04(5)                |\n",
      "| x3 = 0.26(8)          mgB_s13 = 198.8(1)    σ_s13 = 2.48(5)       mgB_s23 = 200.53(3)   σ_s23 = 0.89(4)                |\n",
      "| x4 = -0.28(7)         mgB_s14 = 200.55(2)   σ_s14 = 0.78(3)       mgB_s24 = 198.9(1)    σ_s24 = 2.27(4)                |\n",
      "| x5 = -0.05(7)         mgB_s15 = 200.50(2)   σ_s15 = 0.74(3)       mgB_s25 = 198.7(1)    σ_s25 = 1.97(4)                |\n",
      "| x6 = 0.08(7)          mgB_s16 = 199.05(8)   σ_s16 = 1.61(3)       mgB_s26 = 200.37(1)   σ_s26 = 0.56(2)                |\n",
      "| x7 = 0.15(6)          mgB_s17 = 200.192(9)  σ_s17 = 0.38(1)       mgB_s27 = 199.31(6)   σ_s27 = 1.14(3)                |\n",
      "| x8 = -0.29(7)         mgB_s18 = 199.48(5)   σ_s18 = 0.96(3)       mgB_s28 = 200.110(7)  σ_s28 = 0.28(1)                |\n",
      "| x9 = -0.6(1)          mgB_s19 = 199.72(4)   σ_s19 = 0.68(3)       mgB_s29 = 200.016(5)  σ_s29 = 0.19(1)                |\n",
      "| x10 = -2.3(6)         mgB_s110 = 200.0(1)   σ_s110 = 0.8(1)       mgB_s210 = 199.970(4) σ_s210 = 0.14(1)               |\n",
      "|________________________________________________________________________________________________________________________|\n",
      "Global fit of runs 2507-2517: values and errors saved in mgmgmg.g.1_g.3-4.csv\n",
      "Best fit saved in /home/roberto.derenzi/mujpy/YSrMo/YSrMo-15/fit/mgmgmg.2507-2507.3-4.1_g_fit.json \n"
     ]
    }
   ],
   "source": [
    "if 'datafile' in the_suite.__dict__:\n",
    "    the_fit = mufit(the_suite,logpath+dashboard) # "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Multirun fit animation: toggle pause/resume by clicking on the plot\n"
     ]
    }
   ],
   "source": [
    "if 'datafile' in the_suite.__dict__:\n",
    "    fit_plot= mufitplot('0,500,4,20000,40',the_fit) # try '0,1000,4,24000,100'"
   ]
  },
  {
   "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
}
