{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "46367320",
   "metadata": {},
   "source": [
    "# sqlfilldb\n",
    "Fill run catalog with nfiles entries\n",
    "\n",
    "Based on sqlfill.C by Sergey Linev\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**Author:** Juan Fernando Jaramillo Botero  \n",
    "<i><small>This notebook tutorial was automatically generated with <a href= \"https://github.com/root-project/root/blob/master/documentation/doxygen/converttonotebook.py\">ROOTBOOK-izer</a> from the macro found in the ROOT repository  on Tuesday, May 19, 2026 at 08:15 PM.</small></i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a636e932",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from ROOT import TSQLServer, TSQLResult, TStopwatch, gRandom\n",
    "\n",
    "\n",
    "nfiles = 1000\n",
    "\n",
    "ins = \"INSERT INTO runcatalog VALUES ('%s', %d,\" \\\n",
    "      \" %d, %d, %d, %10.2f, '%s', '%s', '1997-01-15 20:16:28',\" \\\n",
    "      \" '1999-01-15 20:16:28', '%s', '%s')\"\n",
    "\n",
    "tag = evt = 0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef7a3219",
   "metadata": {},
   "source": [
    "open connection to MySQL server on localhost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "466398fa",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "db = TSQLServer.Connect(\"mysql://localhost/test\", \"nobody\", \"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a0f7e19",
   "metadata": {},
   "source": [
    "first clean table of old entries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8e8d1cc",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "res = db.Query(\"DELETE FROM runcatalog\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8fd7350f",
   "metadata": {},
   "source": [
    "start timer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84f20e11",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "timer = TStopwatch()\n",
    "timer.Start()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0fe47778",
   "metadata": {},
   "source": [
    "fill run catalog"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c0f2232",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for i in range(nfiles):\n",
    "    dataset = \"testrun_%d\" % i\n",
    "    rawfile = \"/v1/data/lead/test/run_%d.root\" % i\n",
    "    tag = int(gRandom.Rndm() * 10.)\n",
    "    sql = ins % (dataset, i, evt, evt + 10000, tag, 25.5, \"test\", \"lead\",\n",
    "                 rawfile, \"test run dummy data\")\n",
    "    evt += 10000\n",
    "    res = db.Query(sql)\n",
    "    # print(\"%s\" % sql)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f8e7a43",
   "metadata": {},
   "source": [
    "stop timer and print results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e4bfe2bd",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "timer.Stop()\n",
    "rtime = timer.RealTime()\n",
    "ctime = timer.CpuTime()\n",
    "\n",
    "print(\"\")\n",
    "print(\"%d files in run catalog\" % nfiles)\n",
    "print(\"RealTime=%f seconds, CpuTime=%f seconds\" % (rtime, ctime))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}