Blame view

Modules/Weight.py 2.23 KB
374f3d52   Thomas Fitoussi   add Weight distri...
1
2
3
4
5
6
from numpy import shape, arange, log10, histogram
from matplotlib.pyplot import figure, show, hist
from Read import ReadGeneration, ReadWeight, ReadEnergy


def drawWeightHisto(fileId):
f4246390   Thomas Fitoussi   Improve angular d...
7
8
9
10
11
   '''
      Plot particles'weights histogram, generation by generation
      Input: directory name
      Output: particles'weights histogram
   '''
374f3d52   Thomas Fitoussi   add Weight distri...
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
   fig = figure()
   ax = fig.add_subplot(111)
   nbBins =100 

   weight = ReadWeight(fileId)
   generation = ReadGeneration(fileId)
   print "file", fileId, "->", shape(generation)[0], "events" 

   for gen in list(set(generation)):
      hist(log10(weight[generation==gen]),nbBins,log=1,range=[0,7],alpha=.5,
            label="gen="+str(int(gen)))

   ax.legend(loc='best')
   ax.set_xlabel("Weight [log]")
   ax.set_ylabel("Number of events")

   show()

#######################################################################################
def weightVsEnergy(fileId,gen=-1):
f4246390   Thomas Fitoussi   Improve angular d...
32
33
34
35
36
   '''
      Compute particles'weight versus energy
      Input: directory name
      Input (optional): generation (default = all)
   '''
374f3d52   Thomas Fitoussi   add Weight distri...
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
   energy = ReadEnergy(fileId)
   weight = ReadWeight(fileId)
   nbBins = 100 

   if gen != -1:
      generation = ReadGeneration(fileId)
      energy=energy[generation==gen]
      weight=weight[generation==gen]
      nbBins =40 

   weight,ener=histogram(log10(energy),nbBins,weights=log10(weight))
   nb,ener=histogram(log10(energy),nbBins)
   ener=10**ener
   enercenter=(ener[1:nbBins+1]+ener[0:nbBins])/2
   weight /= nb

   return enercenter, weight

def drawWeightVsEnergy(fileId):
f4246390   Thomas Fitoussi   Improve angular d...
56
57
58
59
60
   '''
      Plot particles'weight versus energy, generation by generation
      Input: directory name
      Output: graph of particles'weight versus energy
   '''
374f3d52   Thomas Fitoussi   add Weight distri...
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
   fig = figure()
   ax = fig.add_subplot(111)

   generation = ReadGeneration(fileId)
   print "file", fileId, "->", shape(generation)[0], "events" 

   for gen in list(set(generation)):
      energy,weight = weightVsEnergy(fileId,gen)
      ax.plot(energy,weight,".",label="gen="+str(int(gen)))

   energy,weight = weightVsEnergy(fileId)
   ax.plot(energy,weight,"k",label="gen=all")

   ax.set_xscale('log')
   ax.grid(b=True,which='major')
   ax.legend(loc="best")
   ax.set_title(fileId)
   ax.set_xlabel("energy [GeV]")
   ax.set_ylabel("mean weight [log]")

   show()