Blame view

Analysis.py 5.56 KB
7030f150   Thomas Fitoussi   Full reorganisati...
1
2
3
#!/bin/python

from sys import argv
631c85a9   Thomas Fitoussi   Ad generation com...
4
from numpy import append, savetxt, shape, array, newaxis, zeros, arange
074c1729   Thomas Fitoussi   Analysis.py used ...
5
from Modules.Read import ReadEnergy, ReadTime, ReadExtraFile, ReadProfile
49a0fe94   Thomas Fitoussi   Add direct printi...
6
from Modules.Read import ReadMomentumAngle, ReadPositionAngle, resultDirectory
65135318   Thomas Fitoussi   Reset modules
7
from Modules.Spectrum import spectrum
28843f23   Thomas Fitoussi   Put radial distri...
8
9
from Modules.Map import computeMap, isotrop
from Modules.Angle import angle_vs_energy
074c1729   Thomas Fitoussi   Analysis.py used ...
10
from Modules.Timing import timing
49a0fe94   Thomas Fitoussi   Add direct printi...
11
from Modules.Constants import degre
7030f150   Thomas Fitoussi   Full reorganisati...
12

28843f23   Thomas Fitoussi   Put radial distri...
13
14
def InProgress(i,Nmax):
   print "  ", (i*100)/Nmax, "% done"
7030f150   Thomas Fitoussi   Full reorganisati...
15

6633967c   Thomas Fitoussi   Correction on the...
16
17
18
if shape(argv)[0] < 2: 
   print "not enough arguments (at least 1)"
   exit()
8ffbee93   Thomas Fitoussi   Analytic expressi...
19

28843f23   Thomas Fitoussi   Put radial distri...
20
#=============================================================================#
6633967c   Thomas Fitoussi   Correction on the...
21
PowerSpectrum=[1,1.5,2,2.5] 
7030f150   Thomas Fitoussi   Full reorganisati...
22

074c1729   Thomas Fitoussi   Analysis.py used ...
23
for fileId in argv[1:]:
28843f23   Thomas Fitoussi   Put radial distri...
24
25
26
   print "#=============================================================================#"
   print "#    Analysis of", fileId
   print "#=============================================================================#"
074c1729   Thomas Fitoussi   Analysis.py used ...
27
   # read files
28843f23   Thomas Fitoussi   Put radial distri...
28
   print "# 1. Reading data" 
074c1729   Thomas Fitoussi   Analysis.py used ...
29
30
31
32
33
   time = ReadTime(fileId)
   energy = ReadEnergy(fileId)
   weightini, generation, theta_arrival, Esource = ReadExtraFile(fileId,[2,3,4,5])
   nbPhotonsEmitted=ReadProfile(fileId,[3]) 

631c85a9   Thomas Fitoussi   Ad generation com...
34
35
   Gen_contrib = []
   Gen_cont = zeros((int(max(generation))+1))
074c1729   Thomas Fitoussi   Analysis.py used ...
36
37
   Source = []
   Spectrum = []
074c1729   Thomas Fitoussi   Analysis.py used ...
38
39
40
   Angle_Energy = []
   Timing = []

28843f23   Thomas Fitoussi   Put radial distri...
41
   print "# 2. Computing powerlaw spectrum" 
6633967c   Thomas Fitoussi   Correction on the...
42
   for powerlaw_index in PowerSpectrum:
074c1729   Thomas Fitoussi   Analysis.py used ...
43
      # apply source spectrum
49a0fe94   Thomas Fitoussi   Add direct printi...
44
      weight_source = (Esource/min(Esource))**(1-powerlaw_index)
6633967c   Thomas Fitoussi   Correction on the...
45
46
      weight = weightini* weight_source

074c1729   Thomas Fitoussi   Analysis.py used ...
47
      #=============================================================================#
631c85a9   Thomas Fitoussi   Ad generation com...
48
49
50
51
52
53
54
55
56
57
58
      # GENERATION CONTRIBUTION
      #=============================================================================#
      NbTotEvents = sum(weight)
      if Gen_contrib==[]:
         Gen_contrib = arange(0,max(generation)+1,1)[:,newaxis]

      for gen in list(set(generation)):
         Gen_cont[int(gen)] = sum(weight[generation==gen])/NbTotEvents *100
      Gen_contrib = append(Gen_contrib,Gen_cont[:,newaxis],axis=1)

      #=============================================================================#
074c1729   Thomas Fitoussi   Analysis.py used ...
59
60
      #  SPECTRUM (SOURCE AND MEASURED)
      #=============================================================================#
28843f23   Thomas Fitoussi   Put radial distri...
61
      nbBins = 50
074c1729   Thomas Fitoussi   Analysis.py used ...
62
63
      # draw source spectrum
      Es=array(list(set(Esource)))
49a0fe94   Thomas Fitoussi   Add direct printi...
64
      Ws= (Es/min(Es))**(1-powerlaw_index)
6633967c   Thomas Fitoussi   Correction on the...
65
      Es,Fs = spectrum(Es,Ws,nbBins=nbBins)
074c1729   Thomas Fitoussi   Analysis.py used ...
66
67
68
69
70
71
72
73
      Es=Es[:,newaxis]
      Fs=Fs[:,newaxis]
      if Source==[]:
         Source = Es
         Source = append(Source,Fs,axis=1)
      else: 
         Source = append(Source,Fs,axis=1)

6633967c   Thomas Fitoussi   Correction on the...
74
75
      # primary gamma-rays contribution and full spectrum
      ener,flux,flux_0 = spectrum(energy,weight,generation,nbBins)
074c1729   Thomas Fitoussi   Analysis.py used ...
76
77
78
79
80
81
82
83
84
85
86
87
      ener=ener[:,newaxis]
      flux=flux[:,newaxis]
      flux_0=flux_0[:,newaxis]
      if Spectrum==[]:
         Spectrum = ener
         Spectrum = append(Spectrum,flux,axis=1)
         Spectrum = append(Spectrum,flux_0,axis=1)
      else:
         Spectrum = append(Spectrum,flux,axis=1)
         Spectrum = append(Spectrum,flux_0,axis=1)

      #=============================================================================#
074c1729   Thomas Fitoussi   Analysis.py used ...
88
89
90
91
92
93
94
95
96
97
98
99
      #  TIME DISTRIBUTION AND TIME DELAY VERSUS ANGLE
      #=============================================================================#
      nbBins = 100
      delta_t,dNdt = timing(time,weight,nbBins)
      delta_t=delta_t[:,newaxis]
      dNdt=dNdt[:,newaxis]
      if Timing==[]:
         Timing = delta_t
         Timing = append(Timing,dNdt,axis=1)
      else:
         Timing = append(Timing,dNdt,axis=1)

28843f23   Thomas Fitoussi   Put radial distri...
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
      InProgress(PowerSpectrum.index(powerlaw_index)+1,shape(PowerSpectrum)[0])

   #=============================================================================#
   #  IMAGING, RADIAL DISTRIBUTION AND ANGLE VERSUS ENERGY
   #=============================================================================#
   print "# 3. compute images and radial distribution" 
   powerlaw_index = 2
   Elim = 1e-1 # GeV
   thetalim = 20 # degre
   nbBins = 100
   Radial = []

   thetaDir,phiDir = ReadMomentumAngle(fileId)*degre
   thetaPos,phiPos = ReadPositionAngle(fileId)*degre
   theta = thetaDir - thetaPos
   phi = phiDir -phiPos

   # apply source spectrum
   weight_source = (Esource/min(Esource))**(1-powerlaw_index)
   weight = weightini* weight_source
6633967c   Thomas Fitoussi   Correction on the...
120

28843f23   Thomas Fitoussi   Put radial distri...
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
   # apply selection
   cond=(energy>Elim) #& (abs(theta)<thetalim) & (abs(phi)<thetalim)
   theta=theta[cond]
   phi=phi[cond]
   weight=weight[cond]
   energy=energy[cond]
   theta2,dndtheta2  = computeMap(theta,phi,weight,energy,fileId,nbBins,
         source=isotrop(),borne=[thetalim,thetalim])
   ener,angle = angle_vs_energy(theta,energy,weight,nbBins)
   theta2=theta2[:,newaxis]
   dndtheta2=dndtheta2[:,newaxis]
   ener=ener[:,newaxis]
   angle=angle[:,newaxis]

   if Radial==[]:
      Radial = theta2
      Radial = append(Radial,dndtheta2,axis=1)
      Angle_Energy = ener
      Angle_Energy = append(Angle_Energy,angle,axis=1)
   else:
      Radial = append(Radial,dndtheta2,axis=1)
      Angle_Energy = append(Angle_Energy,angle,axis=1)

   #=============================================================================#
   print "# 4. writing files" 
49a0fe94   Thomas Fitoussi   Add direct printi...
146
147
148
149
150
151
   savetxt(resultDirectory+fileId+"/Generation.txt",Gen_contrib)
   savetxt(resultDirectory+fileId+"/Spectrum.txt",Spectrum)
   savetxt(resultDirectory+fileId+"/Source_spectrum.txt",Source)
   savetxt(resultDirectory+fileId+"/Angle_vs_Energy.txt",Angle_Energy)
   savetxt(resultDirectory+fileId+"/Radial_distribution.txt",Radial)
   savetxt(resultDirectory+fileId+"/Timing.txt",Timing)
28843f23   Thomas Fitoussi   Put radial distri...
152
   print "#=============================================================================#"