Generation and weight.ipynb 6.53 KB
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "cannot import name ReadEnergy",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-ad74c4a45462>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mmatplotlib\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpyplot\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mfigure\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mshow\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mhist\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mmatplotlib\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mrcParams\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mmodules\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mReadGeneration\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mReadEnergy\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[0mlabel_size\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m20\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mImportError\u001b[0m: cannot import name ReadEnergy"
     ]
    }
   ],
   "source": [
    "from numpy import shape, arange, log10, histogram\n",
    "from matplotlib.pyplot import figure, show, hist\n",
    "from matplotlib import rcParams\n",
    "from modules.read import ReadGeneration, ReadEnergy\n",
    "\n",
    "label_size = 20\n",
    "rcParams['xtick.labelsize'] = label_size \n",
    "rcParams['ytick.labelsize'] = label_size \n",
    "\n",
    "def drawWeightHisto(fileId):\n",
    "   '''\n",
    "      Plot particles'weights histogram, generation by generation\n",
    "      Input: directory name\n",
    "      Output: particles'weights histogram\n",
    "   '''\n",
    "   fig = figure(figsize=(12,9))\n",
    "   ax = fig.add_subplot(111)\n",
    "   nbBins =100 \n",
    "\n",
    "   weight = ReadWeight(fileId)\n",
    "   generation = ReadGeneration(fileId)\n",
    "   print \"file\", fileId, \"->\", shape(generation)[0], \"events\" \n",
    "\n",
    "   for gen in list(set(generation)):\n",
    "      hist(log10(weight[generation==gen]),nbBins,log=1,range=[0,7],alpha=.5,\n",
    "            label=\"gen=\"+str(int(gen)))\n",
    "\n",
    "   ax.legend(loc='best',fontsize=label_size)\n",
    "   ax.set_xlabel(\"Weight [log]\",fontsize=label_size)\n",
    "   ax.set_ylabel(\"Number of events\",fontsize=label_size)\n",
    "\n",
    "   show()\n",
    "\n",
    "#######################################################################################\n",
    "def weightVsEnergy(fileId,gen=-1):\n",
    "   '''\n",
    "      Compute particles'weight versus energy\n",
    "      Input: directory name\n",
    "      Input (optional): generation (default = all)\n",
    "   '''\n",
    "   energy = ReadEnergy(fileId)\n",
    "   weight = ReadWeight(fileId)\n",
    "   nbBins = 100 \n",
    "\n",
    "   if gen != -1:\n",
    "      generation = ReadGeneration(fileId)\n",
    "      energy=energy[generation==gen]\n",
    "      weight=weight[generation==gen]\n",
    "      nbBins =40 \n",
    "\n",
    "   weight,ener=histogram(log10(energy),nbBins,weights=log10(weight))\n",
    "   nb,ener=histogram(log10(energy),nbBins)\n",
    "   ener=10**ener\n",
    "   enercenter=(ener[1:nbBins+1]+ener[0:nbBins])/2\n",
    "   weight /= nb\n",
    "\n",
    "   return enercenter, weight\n",
    "\n",
    "def drawWeightVsEnergy(fileId):\n",
    "   '''\n",
    "      Plot particles'weight versus energy, generation by generation\n",
    "      Input: directory name\n",
    "      Output: graph of particles'weight versus energy\n",
    "   '''\n",
    "   fig = figure(figsize=(12,9))\n",
    "   ax = fig.add_subplot(111)\n",
    "\n",
    "   generation = ReadGeneration(fileId)\n",
    "   print \"file\", fileId, \"->\", shape(generation)[0], \"events\" \n",
    "\n",
    "   for gen in list(set(generation)):\n",
    "      energy,weight = weightVsEnergy(fileId,gen)\n",
    "      ax.plot(energy,weight,\".\",label=\"gen=\"+str(int(gen)))\n",
    "\n",
    "   energy,weight = weightVsEnergy(fileId)\n",
    "   ax.plot(energy,weight,\"k\",label=\"gen=all\")\n",
    "\n",
    "   ax.set_xscale('log')\n",
    "   ax.grid(b=True,which='major')\n",
    "   ax.legend(loc=\"best\",fontsize=label_size)\n",
    "   ax.set_title(fileId)\n",
    "   ax.set_xlabel(\"energy [GeV]\",fontsize=label_size)\n",
    "   ax.set_ylabel(\"mean weight [log]\",fontsize=label_size)\n",
    "\n",
    "   show()\n",
    "\n",
    "def drawGeneration(files,plot=\"\"):\n",
    "   '''\n",
    "      Plot histogram of particles'generations\n",
    "      Input: list of directories\n",
    "      Output: histogram of generations\n",
    "   '''\n",
    "   fig = figure(figsize=(12,9))\n",
    "   ax = fig.add_subplot(111)\n",
    "   for fileId in files:\n",
    "      if plot==\"generation\":\n",
    "         i=1\n",
    "         for powerlaw_index in [1,1.5,2,2.5]: \n",
    "            generation, ratio = ReadGeneration(fileId,[0,i])\n",
    "            ax.plot(generation,ratio,drawstyle='steps-mid',label=\"p=%.1f\"%powerlaw_index)\n",
    "            i+=1\n",
    "      else:\n",
    "         generation, ratio = ReadGeneration(fileId,[0,3])\n",
    "         ax.plot(generation,ratio,drawstyle='steps-mid',label=fileId)\n",
    "\n",
    "   xtick=arange(9)\n",
    "   ax.legend(loc='best',fontsize=label_size)\n",
    "   ax.set_yscale('log')\n",
    "   ax.set_xlabel(\"Generation\",fontsize=label_size)\n",
    "   ax.set_ylabel(\"Ratio of events [%]\",fontsize=label_size)\n",
    "\n",
    "   show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "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.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}