{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "**Example of single group calibration:**\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 = 'almgml'\n",
    "version = '1'\n",
    "grp_calib = [{'forward':'2,3', 'backward':'1,4', 'alpha':1.13}]\n",
    "groupcalibfile = '2_3-1_4.calib'\n",
    "inputsuitefile = 'calib.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",
      "File /afs/psi.ch/project/bulkmusr/data/gps/d2021/tdc/deltat_tdc_gps_0822.bin not found\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",
    "grp_calib = [{'forward':'2,3', 'backward':'1,4', 'alpha':1.13}]\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/almgml.822.2_3-1_4.1.json\n",
      "******* no data in musuite\n",
      "* check access to database\n",
      "     mufit stops here\n"
     ]
    }
   ],
   "source": [
    "print('Model defined by dashboard {}'.format(logpath+dashboard))\n",
    "the_fit = mufit(the_suite,logpath+dashboard)# 'log/almgml.822.2_3-1_4.1.json')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sorry, no access to data... quitting mufitplot\n"
     ]
    }
   ],
   "source": [
    "fit_plot= mufitplot('0,2000,10,20000,40',the_suite,logpath+dashfit)#,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
}
