Blame view

Modules/Spectrum.py 4.93 KB
374f3d52   Thomas Fitoussi   add Weight distri...
1
from numpy import histogram, log10, shape, logspace, sqrt
8ffbee93   Thomas Fitoussi   Analytic expressi...
2
3
from matplotlib.pyplot import figure, show, ylim, setp
from matplotlib import gridspec
7030f150   Thomas Fitoussi   Full reorganisati...
4
from os.path import isfile
8011cc96   Thomas Fitoussi   Add profile readi...
5
from Read import ReadEnergy, ReadWeight, ReadElmag, ReadNbLeptonsProd, resultDirectory, ElmagFile
374f3d52   Thomas Fitoussi   add Weight distri...
6
7
8
9
from Read import ReadNphot_source, ReadGeneration
from Analytic import Analytic_flux, Ethreshold_gg

def spectrum(fileId,gen=-1):
f4246390   Thomas Fitoussi   Improve angular d...
10
11
12
13
14
   '''
      Compute flux versus energy
      Input: directory name, generation (optional, by default: all generation)
      Output: energy, flux
   '''
374f3d52   Thomas Fitoussi   add Weight distri...
15
16
17
18
19
20
21
22
23
24
   energy = ReadEnergy(fileId)
   weight = ReadWeight(fileId)

   if gen != -1:
      generation = ReadGeneration(fileId)
      energy=energy[generation==gen]
      weight=weight[generation==gen]
      print "file", fileId, "gen=", gen,"->", shape(energy)[0], "events" 
   else:
      print "file", fileId, "->", shape(energy)[0], "events" 
7030f150   Thomas Fitoussi   Full reorganisati...
25

7030f150   Thomas Fitoussi   Full reorganisati...
26
27
28
29
30
31
32
   nbBins=100
   energy =log10(energy)
   flux,ener=histogram(energy,nbBins,weights=weight)
   ener = 10**ener
   enercenter=(ener[1:nbBins+1]+ener[0:nbBins])/2
   binSize=ener[1:nbBins+1]-ener[0:nbBins]
   flux = enercenter**2 * flux/binSize 
374f3d52   Thomas Fitoussi   add Weight distri...
33
34
   maxflux = ReadNphot_source(fileId)
   flux /= maxflux
7030f150   Thomas Fitoussi   Full reorganisati...
35
36
   return enercenter,flux

374f3d52   Thomas Fitoussi   add Weight distri...
37
###########################################################################################
f4246390   Thomas Fitoussi   Improve angular d...
38
39
40
41
42
43
44
45
def drawSpectrum(files,gen=-1,PlotElmag=False,PlotAnalytic=False):
   '''
      Plot flux versus energy
      Input: list of directories
      Input (optional, default=all): selection on the generation
      Input (optional, default=False): Plot Elmag spectrum if exists, plot analytic expression
      Output: graph spectrum normalize to 1 source photon
   '''
374f3d52   Thomas Fitoussi   add Weight distri...
46
47
48
49
   fig = figure()
   ax = fig.add_subplot(111)

   for fileId in files:
f4246390   Thomas Fitoussi   Improve angular d...
50
      energy,flux = spectrum(fileId,gen)
374f3d52   Thomas Fitoussi   add Weight distri...
51
52
53
54
55
56
57
58
59
60
61
      if PlotElmag:
         maxflux = max(flux)*(.8)
      p=ax.plot(energy,flux,drawstyle='steps-mid',label=fileId)

      if PlotElmag & isfile(resultDirectory+fileId+ElmagFile):
         energy, flux = ReadElmag(fileId)
         maxflux = max(flux)
         flux /= maxflux
         ax.plot(energy,flux,"--",color=p[0].get_color(),label="Elmag - "+fileId)
            
   if PlotAnalytic:
f4246390   Thomas Fitoussi   Improve angular d...
62
63
      minEner=min(energy)
      alpha=flux[energy==minEner]/sqrt(minEner)
374f3d52   Thomas Fitoussi   add Weight distri...
64
      ax.plot(energy,alpha*sqrt(energy),'--m',linewidth=2)
f4246390   Thomas Fitoussi   Improve angular d...
65
      ax.plot(energy,energy**(0)*max(flux)*0.95,'--m',linewidth=2)
374f3d52   Thomas Fitoussi   add Weight distri...
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
      ax.axvline(x=Ethreshold_gg(), ymin=0., ymax = 1., color='r', linewidth=2)

   ax.set_xscale('log')
   ax.set_yscale('log')
   ax.grid(b=True,which='major')
   ax.legend(loc="best")
   if PlotElmag:
      ax.set_ylabel("$E^2 \\frac{dN}{dE}$ [normalized]")
   else:
      ax.set_ylabel("$E^2 \\frac{dN}{dE}$ [GeV / photon]")
   ax.set_xlabel("energy [GeV]")

   show()

def drawSpectrum_normaLepton(files):
f4246390   Thomas Fitoussi   Improve angular d...
81
82
83
84
85
   '''
      Plot flux versus energy compare with analytic expression
      Input: list of directories
      Output: graph spectrum normalize to 1 lepton generated
   '''
7030f150   Thomas Fitoussi   Full reorganisati...
86
   fig = figure()
8ffbee93   Thomas Fitoussi   Analytic expressi...
87
88
89
90
91
   gs = gridspec.GridSpec(2, 1, height_ratios=[4,1]) 
   fig.subplots_adjust(hspace=0.001)
   ax1 = fig.add_subplot(gs[0])
   ax2 = fig.add_subplot(gs[1],sharex=ax1)

7030f150   Thomas Fitoussi   Full reorganisati...
92
93
   for fileId in files:
      energy,flux = spectrum(fileId)
8011cc96   Thomas Fitoussi   Add profile readi...
94
      maxflux = ReadNbLeptonsProd(fileId)
7030f150   Thomas Fitoussi   Full reorganisati...
95
      flux /= maxflux
8ffbee93   Thomas Fitoussi   Analytic expressi...
96
97
98
99
100
101
102
      p=ax1.plot(energy,flux,drawstyle='steps-mid',label=fileId)

      yfit = Analytic_flux(energy)
      ax1.plot(energy,yfit,'--m',linewidth=2)

      error=flux/yfit-1
      ax2.plot(energy,error,'+',color=p[0].get_color())
7030f150   Thomas Fitoussi   Full reorganisati...
103

8ffbee93   Thomas Fitoussi   Analytic expressi...
104
105
106
107
   ax1.set_xscale('log')
   ax1.set_yscale('log')
   ax1.grid(b=True,which='major')
   ax1.legend(loc="best")
374f3d52   Thomas Fitoussi   add Weight distri...
108
   ax1.set_ylabel("$E^2 \\frac{dN}{dE}$ [GeV] / lepton")
8ffbee93   Thomas Fitoussi   Analytic expressi...
109
110
111
112
113
114
115
116
   ax2.set_xscale('log')
   ax2.grid(b=True,which='major')
   ax2.set_xlabel("energy [GeV]")
   ax2.set_ylabel("error")
   step=0.2*(max(error)-min(error))
   ylim(min(error)-step, max(error)+step)
   xticklabels = ax1.get_xticklabels()
   setp(xticklabels, visible=False)
7030f150   Thomas Fitoussi   Full reorganisati...
117
118

   show()
374f3d52   Thomas Fitoussi   add Weight distri...
119
120
121

########################################################################################
def drawSpectrumGen(fileId):
f4246390   Thomas Fitoussi   Improve angular d...
122
123
124
125
126
   '''
      Plot flux versus energy generation by generation
      Input: directory name
      Output: graph spectrum normalize to 1 photon source
   '''
374f3d52   Thomas Fitoussi   add Weight distri...
127
128
129
130
131
132
133
134
135
   fig = figure()
   ax = fig.add_subplot(111)

   for gen in list(set(ReadGeneration(fileId))):
      energy,flux = spectrum(fileId,gen)
      p=ax.plot(energy,flux,drawstyle='steps-mid',label="gen="+str(int(gen)))

   energy,flux = spectrum(fileId)
   p=ax.plot(energy,flux,drawstyle='steps-mid',color='k',label="gen = all")
374f3d52   Thomas Fitoussi   add Weight distri...
136

f4246390   Thomas Fitoussi   Improve angular d...
137
138
   minEner=min(energy)
   alpha=flux[energy==minEner]/sqrt(minEner)
374f3d52   Thomas Fitoussi   add Weight distri...
139
140
141
142
143
144
145
146
147
148
149
150
151
   ax.plot(energy,sqrt(energy)*alpha,'--m',linewidth=2)
   ax.plot(energy,energy**(0)*max(flux),'--m',linewidth=2)
   ax.axvline(x=Ethreshold_gg(), ymin=0., ymax = 1., color='r', linewidth=2)

   ax.set_xscale('log')
   ax.set_yscale('log')
   ax.grid(b=True,which='major')
   ax.legend(loc="best")
   ax.set_title(fileId)
   ax.set_ylabel("$E^2 \\frac{dN}{dE}$ [GeV / photon]")
   ax.set_xlabel("energy [GeV]")

   show()