Analysis.py 6.37 KB
#!/bin/python

from sys import argv
from numpy import append, savetxt, shape, array, newaxis, zeros, arange
from Modules.Read import ReadEnergy, ReadTime, ReadExtraFile, ReadProfile
from Modules.Read import ReadMomentumAngle, ReadPositionAngle, resultDirectory
from Modules.Spectrum import spectrum
from Modules.Map import computeMap
from Modules.Angle import angle_vs_energy, radial
from Modules.Timing import timing
from Modules.Constants import degre

if shape(argv)[0] < 2: 
   print "not enough arguments (at least 1)"
   exit()

powerlaw_index=2 

for fileId in argv[1:]:
   print "#===============================================#"
   print "# Analysis of", fileId
   print "#===============================================#"
   # read files
   print "   > Reading data" 
   time = ReadTime(fileId)
   energy = ReadEnergy(fileId)
   weightini, generation, theta_arrival, Esource, dir_source = ReadExtraFile(fileId,[2,3,4,5,6])
   #weightini, generation, theta_arrival, Esource = ReadExtraFile(fileId,[2,3,4,5])
   n_phot, n_lept = ReadProfile(fileId,[3,5]) 
   ratio = n_phot#/n_lept
   weightini /= ratio
   theta_arrival*= degre

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

   powerlaw_index = 2

   #=============================================================================#
   #  NO SELECTION
   #=============================================================================#
   print "   > No selection of events ..." 
   #  SPECTRUM (SOURCE) =========================================================#
   weight_source = (Esource/min(Esource))**(1-powerlaw_index)
   weight = weightini* weight_source
   nbBins = 100
   Es=array(list(set(Esource)))
   Ws= (Es/min(Es))**(1-powerlaw_index)  / ratio
   Es,Fs = spectrum(Es,Ws,nbBins=nbBins)
   Es=Es[:,newaxis]
   Fs=Fs[:,newaxis]
   Source = Es
   Source = append(Source,Fs,axis=1)

   #  IMAGING ===================================================================#
   print "     ... Computing image"
   nbBins = 50
   thetalim = 20
   computeMap(theta,phi,weight,energy,fileId,nbBins,borne=[thetalim,thetalim])

   #  SPECTRUM (MEASURED) =======================================================#
   print "     ... Computing spectrum"
   nbBins = 100
   ener,flux = spectrum(energy,weight,nbBins)
   ener=ener[:,newaxis]
   flux=flux[:,newaxis]
   Spectrum = ener
   Spectrum = append(Spectrum,flux,axis=1)

   #  ANGLE VERSUS ENERGY =======================================================#
   print "     ... Computing arrival angle versus energy"
   nbBins = 100
   ener,angle = angle_vs_energy(theta_arrival,energy,weight,nbBins)
   ener=ener[:,newaxis]
   angle=angle[:,newaxis]
   Angle_Energy = ener
   Angle_Energy = append(Angle_Energy,angle,axis=1)

   #  RADIAL ====================================================================#
   print "     ... Computing radial distribution"
   nbBins = 100
   theta2,dndtheta = radial(theta,weight,nbBins)
   theta2=theta2[:,newaxis]
   dndtheta=dndtheta[:,newaxis]
   Radial = theta2
   Radial = append(Radial,dndtheta,axis=1)

   #  TIME DISTRIBUTION AND TIME DELAY VERSUS ANGLE =============================#
   print "     ... Computing time distribution"
   nbBins = 100
   delta_t,dNdt = timing(time,weight,nbBins)
   delta_t=delta_t[:,newaxis]
   dNdt=dNdt[:,newaxis]
   Timing = delta_t
   Timing = append(Timing,dNdt,axis=1)

   #=============================================================================#
   #  BY ENERGY RANGE
   #=============================================================================#
   print "   > Selection by energy range ..." 
   Emin = [1e-3,1e0,1e3] #GeV
   Emax = [1e0,1e3,1e5] #GeV

   for n in arange(0,3,1):
      cond= (energy>Emin[n]) & (energy<Emax[n])

      #  RADIAL =================================================================#
      nbBins = 100
      theta2,dndtheta = radial(theta[cond],weight[cond],nbBins)
      dndtheta=dndtheta[:,newaxis]
      Radial = append(Radial,dndtheta,axis=1)

      #  TIME DISTRIBUTION AND TIME DELAY VERSUS ANGLE ==========================#
      nbBins = 100
      delta_t,dNdt = timing(time[cond],weight[cond],nbBins)
      dNdt=dNdt[:,newaxis]
      Timing = append(Timing,dNdt,axis=1)

      print "     ... ", ((n+1)*100)/3,"% done"

   #=============================================================================#
   #  BY GENERATION
   #=============================================================================#
   print "   > Selection by generation ..." 
   NbTotEvents = sum(weight)
   gen_tab =list(set(generation))
   Gen_cont = zeros([shape(gen_tab)[0],2])
   for gen in gen_tab:
      cond = generation==gen
      contrib =sum(weight[cond])/NbTotEvents*100 
      i = gen_tab.index(gen)
      Gen_cont[i,0]=int(gen)
      Gen_cont[i,1]=contrib
      print "     ... gen=",int(gen),"-> contribution:",int(contrib),"%"

      #  SPECTRUM (MEASURED) ====================================================#
      nbBins = 100
      ener,flux = spectrum(energy[cond],weight[cond],nbBins)
      flux=flux[:,newaxis]
      Spectrum = append(Spectrum,flux,axis=1)

      #  ANGLE VERSUS ENERGY ====================================================#
      nbBins = 100
      ener,angle = angle_vs_energy(theta_arrival[cond],energy[cond],weight[cond],nbBins)
      angle=angle[:,newaxis]
      Angle_Energy = append(Angle_Energy,angle,axis=1)

      #  RADIAL =================================================================#
      nbBins = 100
      theta2,dndtheta = radial(theta[cond],weight[cond],nbBins)
      dndtheta=dndtheta[:,newaxis]
      Radial = append(Radial,dndtheta,axis=1)

      #  TIME DISTRIBUTION AND TIME DELAY VERSUS ANGLE ==========================#
      nbBins = 100
      delta_t,dNdt = timing(time[cond],weight[cond],nbBins)
      dNdt=dNdt[:,newaxis]
      Timing = append(Timing,dNdt,axis=1)

   #=============================================================================#
   print "   > writing files" 
   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)
   savetxt(resultDirectory+fileId+"/Generation.txt",Gen_cont)
   print "#===============================================#"