{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "**Example of multigroup group calibration, global:**\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 = '2241' # first run first\n",
    "modelname = 'almgml'\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 = 'calib.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_2241.bin loaded\n",
      " MnBi2Te4 pellet 30.0K 700mT\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": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model defined by dashboard /home/roberto.derenzi/git/mujpy/log/almgml.2241.2-1_3-4.gg_1.json\n",
      "mufit _initialise_fit debug: dash [{'name': 'al', 'label': '', 'pardicts': [{'name': 'α', 'value': 1.13, 'flag': '~', 'function': '', 'function_multi': ['p[0]', 'p[1]'], 'error': 0.001, 'limits': [0, None]}]}, {'name': 'mg', 'label': 'T_fast', 'pardicts': [{'name': 'A', 'value': 0.09, 'flag': '=', 'function': '', 'function_multi': ['p[2]*p[4]', 'p[3]*p[4]'], 'error_propagate_multi': ['sqrt((p[2]*e[4])**2+(e[2]*p[4])**2)', 'sqrt((p[3]*e[4])**2+(e[3]*p[4])**2)'], 'error': 0.01, 'limits': [None, None]}, {'name': 'B', 'value': 700.0, 'flag': '=', 'function': 'p[5]', 'error': 0.1, 'limits': [None, None]}, {'name': 'φ', 'value': 0.0, 'flag': '=', 'function': '', 'function_multi': ['p[6]', 'p[7]'], 'error': 1.0, 'limits': [None, None]}, {'name': 'σ', 'value': 1.0, 'flag': '=', 'function': 'p[8]', 'error': 0.01, 'limits': [None, None]}]}, {'name': 'ml', 'label': 'T_slow', 'pardicts': [{'name': 'A', 'value': 0.09, 'flag': '=', 'function': '', 'function_multi': ['p[2]*(1-p[4])', 'p[3]*(1-p[4])'], 'error_propagate_multi': ['sqrt((p[2]*e[4])**2+(e[2]*(1-p[4]))**2)', 'sqrt((p[3]*e[4])**2+(e[3]*(1-p[4]))**2)'], 'error': 0.01, 'limits': [None, None]}, {'name': 'B', 'value': 700.0, 'flag': '=', 'function': 'p[9]', 'error': 0.1, 'limits': [None, None]}, {'name': 'φ', 'value': 0.0, 'flag': '=', 'function': '', 'function_multi': ['p[6]', 'p[7]'], 'error': 1.0, 'limits': [None, None]}, {'name': 'λ', 'value': 0.1, 'flag': '=', 'function': 'p[10]', 'error': 0.01, 'limits': [None, None]}]}]\n",
      "aux debug: par α21 limits [0, None]\n",
      "aux debug: par α34 limits [None, None]\n",
      "aux debug: par A21 limits [0, None]\n",
      "aux debug: par A34 limits [None, None]\n",
      "aux debug: par f limits [0, 1]\n",
      "aux debug: par B_f limits [None, None]\n",
      "aux debug: par φ21 limits [None, None]\n",
      "aux debug: par φ34 limits [None, None]\n",
      "aux debug: par σ_f limits [None, None]\n",
      "aux debug: par B_s limits [None, None]\n",
      "aux debug: par λ_s limits [None, None]\n",
      " ________________________________________________________________________________________________\n",
      "| Run 2241: MnBi2Te4 pellet 30.0K 700mT    Global fit of 09/01/2022 15:36:34              |\n",
      "| χᵣ² = 6.156(0.987,1.013) ,    on [3.88ns, 4.7µs, 0.39ns/bin]                                   |\n",
      "|----------------------------------------------------------------- on group: 2 - 1   α = 0.898   |\n",
      "| al: α_ = 0.898(1)\n",
      "| mg: A_T_fast = 0.00201(4) B_T_fast = 699.9(1) φ_T_fast = -65(2) σ_T_fast = 4.2(2)\n",
      "| ml: A_T_slow = 0.001(1) B_T_slow = 689.5(3) φ_T_slow = -65(2) λ_T_slow = 5.1(3)\n",
      "|----------------------------------------------------------------- on group: 3 - 4   α = 1.043   |\n",
      "| al: α_ = 1.0430(6)\n",
      "| mg: A_T_fast = 0.15695(9) B_T_fast = 699.9(1) φ_T_fast = 168.9(8) σ_T_fast = 4.2(2)\n",
      "| ml: A_T_slow = 0.090(3) B_T_slow = 689.5(3) φ_T_slow = 168.9(8) λ_T_slow = 5.1(3)\n",
      "|________________________________________________________________________________________________|\n",
      "mufit prepare_csv debug, k =0 run=2241\n",
      "aux initialize_csv debug: nrun 2241\n",
      "Best fit saved in /home/roberto.derenzi/git/mujpy/log/almgml.2241.2-1_3-4.gg_1_fit.json .  Log added to almgml.gg_1.2-1_3-4.csv\n",
      "mufitplot debug: Really finished\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": [
      "mufitplot __init__ debug: inside\n",
      "mufitplot init__ debug: the_fit.dashboard =\n",
      "{'version': 'gg_1', 'fit_range': '0,24000,2', 'offset': 20, 'userpardicts_guess': [{'name': 'α21', 'value': 1.13, 'flag': '~', 'error': 0.001, 'limits': [0, None]}, {'name': 'α34', 'value': 1.14, 'flag': '~', 'error': 0.001, 'limits': [None, None]}, {'name': 'A21', 'value': 0.25, 'flag': '~', 'error': 0.001, 'limits': [0, None]}, {'name': 'A34', 'value': 0.26, 'flag': '~', 'error': 0.001, 'limits': [None, None]}, {'name': 'f', 'value': 0.5, 'flag': '~', 'error': 0.001, 'limits': [0, 1]}, {'name': 'B_f', 'value': 700.0, 'flag': '~', 'error': 0.1, 'limits': [None, None]}, {'name': 'φ21', 'value': 0, 'flag': '~', 'error': 0.2, 'limits': [None, None]}, {'name': 'φ34', 'value': 90, 'flag': '~', 'error': 0.2, 'limits': [None, None]}, {'name': 'σ_f', 'value': 1, 'flag': '~', 'error': 0.05, 'limits': [None, None]}, {'name': 'B_s', 'value': 700.0, 'flag': '~', 'error': 0.1, 'limits': [None, None]}, {'name': 'λ_s', 'value': 0.1, 'flag': '~', 'error': 0.01, 'limits': [None, None]}], 'model_guess': [{'name': 'al', 'label': '', 'pardicts': [{'name': 'α', 'value': 1.13, 'flag': '~', 'function': '', 'function_multi': ['p[0]', 'p[1]'], 'error': 0.001, 'limits': [0, None]}]}, {'name': 'mg', 'label': 'T_fast', 'pardicts': [{'name': 'A', 'value': 0.09, 'flag': '=', 'function': '', 'function_multi': ['p[2]*p[4]', 'p[3]*p[4]'], 'error_propagate_multi': ['sqrt((p[2]*e[4])**2+(e[2]*p[4])**2)', 'sqrt((p[3]*e[4])**2+(e[3]*p[4])**2)'], 'error': 0.01, 'limits': [None, None]}, {'name': 'B', 'value': 700.0, 'flag': '=', 'function': 'p[5]', 'error': 0.1, 'limits': [None, None]}, {'name': 'φ', 'value': 0.0, 'flag': '=', 'function': '', 'function_multi': ['p[6]', 'p[7]'], 'error': 1.0, 'limits': [None, None]}, {'name': 'σ', 'value': 1.0, 'flag': '=', 'function': 'p[8]', 'error': 0.01, 'limits': [None, None]}]}, {'name': 'ml', 'label': 'T_slow', 'pardicts': [{'name': 'A', 'value': 0.09, 'flag': '=', 'function': '', 'function_multi': ['p[2]*(1-p[4])', 'p[3]*(1-p[4])'], 'error_propagate_multi': ['sqrt((p[2]*e[4])**2+(e[2]*(1-p[4]))**2)', 'sqrt((p[3]*e[4])**2+(e[3]*(1-p[4]))**2)'], 'error': 0.01, 'limits': [None, None]}, {'name': 'B', 'value': 700.0, 'flag': '=', 'function': 'p[9]', 'error': 0.1, 'limits': [None, None]}, {'name': 'φ', 'value': 0.0, 'flag': '=', 'function': '', 'function_multi': ['p[6]', 'p[7]'], 'error': 1.0, 'limits': [None, None]}, {'name': 'λ', 'value': 0.1, 'flag': '=', 'function': 'p[10]', 'error': 0.01, 'limits': [None, None]}]}], 'userpardicts_result': [{'name': 'α21', 'value': 0.8975691530251988, 'std': 0.001243001567486357}, {'name': 'α34', 'value': 1.0430080471770808, 'std': 0.000566390736323407}, {'name': 'A21', 'value': 0.003166096381408412, 'std': 0.0011037743340067685}, {'name': 'A34', 'value': 0.24718708436317716, 'std': 0.0026446076007577038}, {'name': 'f', 'value': 0.6349371489677325, 'std': 0.03291742443803386}, {'name': 'B_f', 'value': 699.9370353482483, 'std': 0.11329181258481243}, {'name': 'φ21', 'value': -64.70337408868075, 'std': 19.800578145002333}, {'name': 'φ34', 'value': 168.875880510036, 'std': 0.7801282801206139}, {'name': 'σ_f', 'value': 4.2255648607950205, 'std': 0.17283737694322898}, {'name': 'B_s', 'value': 689.5100632454282, 'std': 0.29317975486927744}, {'name': 'λ_s', 'value': 5.11928021906451, 'std': 0.3426836868194451}], 'chi2': 6.155755669371747}\n",
      "Multigroup fit animation: toggle pause/resume by clicking on the plot\n",
      "plot_run mufitplot debug: single chi is True\n"
     ]
    }
   ],
   "source": [
    "fit_plot= mufitplot('0,5000,5',the_fit)#,rotating_frame_frequencyMHz=87)#,guess=True) # '0,1000,4,24000,100' # two range plot"
   ]
  },
  {
   "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
}
