Blame view

Generation and weight.ipynb 5.28 KB
627ed999   Thomas Fitoussi   1. Reorganized fi...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from numpy import shape, arange\n",
    "from matplotlib.pyplot import figure, show, hist\n",
    "from matplotlib import rcParams\n",
    "from read import ReadGeneration\n",
    "\n",
    "label_size = 20\n",
    "rcParams['xtick.labelsize'] = label_size \n",
    "rcParams['ytick.labelsize'] = label_size \n",
    "\n",
    "from numpy import shape, arange, log10, histogram\n",
    "from matplotlib.pyplot import figure, show, hist\n",
    "from read import ReadGeneration, ReadWeight, ReadEnergy\n",
    "\n",
    "import matplotlib as mpl\n",
    "label_size = 20\n",
    "mpl.rcParams['xtick.labelsize'] = label_size \n",
    "mpl.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",
    "\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()\n"
   ]
  }
 ],
 "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
}