def min(a, gridwidth, eff, phi, epsilon, detections): def f(x, y, Z, Epn, height): sum = 0 for detection in a: x0 = float(detection[0]) y0 = float(detection[1]) count = float(detection[2]) bkgcount = float(detection[3]) category = detection[4] sum += ll.run(x, y, Epn, Z, height, x0, y0, count, bkgcount, category, eff, phi, epsilon) return sum #Runs Minimisation and outputs results startpos = [0, 0, 26.0, 1000, 30000] argumentx = "limit_x = (-300, 300), error_x = 100000, " argumenty = "limit_y = (-300, 300), error_y = 100000, " argumentZ = "fix_Z=True, " argumentE = "limit_Epn = (232, 4000), error_Epn=1000, " argumentheight = "limit_height = (17500, 70000), error_height=(100000), " argumenterror = "print_level=0, errordef = 100" m = eval("Minuit(f, x=" + str(startpos[0]) + ", " + argumentx + "y=" + str(startpos[1]) + ", " + argumenty + "Epn = " + str(startpos[3]) + ", " + argumentE + "Z=" + str(startpos[2]) + "," + argumentZ + "height = " + str(startpos[4]) + ", " + argumentheight + argumenterror + ")") m.migrad() params = m.values guess = [ params['x'], params['y'], params['Epn'], params['Z'], params['height'] ] guessfval = m.fval xsites = np.linspace(-150, 150, int(gridwidth)) ysites = np.linspace(-150, 150, int(gridwidth)) eraw = np.linspace(0, 1, num=3) R = eraw * 0.0178 evals = ((1.7 * R / 321) + (3571**-1.7))**(-1 / 1.7) hvals = np.linspace(20000, 30000, num=3) hvals = [25000] zvalues = np.arange(20., 33.) minangle = 0.0 j = 0 while j < 5: coordinates = [] j = 0 minangle += 0.1 for x in xsites: for y in ysites: n = 0 for detection in a: x0 = float(detection[0]) y0 = float(detection[1]) recordeddangle = float(detection[5]) dangle = ce.dangle(x0, y0, x, y) if math.fabs(dangle - recordeddangle) < math.radians(minangle): n += 1 if n > (len(a) - 1): coordinates.append([x, y]) j += 1 if minangle > 20: j = 10 ehvals = [] for height in hvals: for Epn in evals: ri = atm.runindex(height) Ethreshold = float(cr.runemin(ri)) if float(Epn) > float(Ethreshold): ehvals.append([Epn, height]) xycount = len(coordinates) ehcount = len(ehvals) zcount = len(zvalues) line = [0, 0] #~ rg = guess #~ rf = guessfval print detections, "Detections -> ", xycount, "Valid Core Positions (", minangle, "Degrees from recorded axis)", ehcount, "Valid Height/Epn Combinations", zcount, "Charge Values", xycount * ehcount * zcount, "Total Minimisations" for z in zvalues: m = eval("Minuit(f, x=" + str(startpos[0]) + ", " + argumentx + "y=" + str(startpos[1]) + ", " + argumenty + "Epn = " + str(startpos[3]) + ", " + argumentE + "Z=" + str(z) + "," + argumentZ + "height = " + str(startpos[4]) + ", " + argumentheight + argumenterror + ")") m.migrad(resume=False) params = m.values zguess = [ params['x'], params['y'], params['Epn'], params['Z'], params['height'] ] zguessfval = m.fval for [x, y] in coordinates: for [e, h] in ehvals: m = eval("Minuit(f, " + "Epn=" + str(e) + ", " + argumentE + "height = " + str(h) + ", " + argumentheight + "Z=" + str(z) + "," + argumentZ + "x=" + str(x) + ", " + argumentx + "y=" + str(y) + ", " + argumenty + argumenterror + ")") m.migrad(resume=False) params = m.values fval = m.fval values = m.get_fmin() if fval < zguessfval: if values.is_valid: zguess = [ params['x'], params['y'], params['Epn'], params['Z'], params['height'] ] zguessfval = fval #~ print zguess, "(", zguessfval, ") Valid!" #~ elif fval < rf: #~ rf = fval #~ rg = [params['x'], params['y'], params['Epn'], params['Z'], params['height']] #~ print rg, "(", rf, ") Rejected!" print time.asctime(time.localtime()), zguess, "(", zguessfval, ")" #~ print rg, "(", rf, ") Rejected!" if zguessfval < guessfval: guess = zguess guessfval = zguessfval print "Final guess is", guess, math.degrees(phi), math.degrees( epsilon), "(", guessfval, ")" return guess[0], guess[1], guess[2], guess[3], guess[4], guessfval
def run(eff, rowcount, mincount=4, text=False, graph=False, layout="five", number=1, nh=1): #~ #Create a subplot for the fractional abundance #~ #~ ax1 = plt.subplot(211) #~ #~ #Define number of bins, maximum Epn #~ #~ bincount = 20 #~ nmax = 1 #~ emax = 35720 #~ #~ Rrange = np.linspace(0, nmax, bincount) #~ k = math.log(emax/232)/float(nmax) #~ #~ Erange=[] #~ #~ fullcount = [] #~ nDC = [] #~ wnDC = [] #~ bT = [] #~ wbT = [] #~ mT = [] #~ wmT = [] #~ #~ #Iterate over Energies #~ #~ for R in Rrange: #~ #~ Epn = 232*(math.e**(k*R)) #~ #~ Erange.append(Epn) #~ #~ weight = Epn ** (-2.7) #~ #~ #For a given Energy, iterate over n randomly simulated events #~ #~ for i in range (0, int(number)): #~ #~ #As in generate.py, randomly generate all variables (expect Epn) #~ #~ xpos = (random.random()*300)-150 #~ ypos = (random.random()*300)-150 #~ #~ Z= 26 #~ #~ hprob = random.random() #~ height = atm.runheight(hprob, text) #~ #~ zenith = random.random()*44 #~ #~ phi = math.radians(68+zenith) #~ #~ epsilon = math.pi*random.random()*2 #~ #~ radius, theta = cr.run(Epn, height, math.sin(phi), text=text) #~ #~ #Determine what category the event was #~ #~ entry, entrytype = lt.run(layout, xpos, ypos, epsilon, radius, Epn, Z, height, phi, theta, mincount, eff, 1, graph=False, text=False) #~ #~ #append the energy value to the appropriate category #~ #~ if entrytype == "metThreshold": #~ mT.append(Epn) #~ wmT.append(weight) #~ elif entrytype == "belowThreshold": #~ bT.append(Epn) #~ wbT.append(weight) #~ elif entrytype == "nonDC": #~ nDC.append(Epn) #~ wnDC.append(weight) #~ else: #~ print "ERROR OVER HERE!!!!" #~ #~ #Create labels for each bin #~ #~ labels = ["metThreshold", "belowThreshold", "nonDC"] #~ #~ xlabels=[] #~ #~ for i in Erange: #~ xlabels.append(int(i)) #~ #~ Erange.append(emax + 1) #~ bin_centers = 0.5 * np.diff(Erange) + Erange[:-1] #~ #~ limits = [Erange[0], Erange[bincount]] #~ #~ mweights = np.ones_like(mT)/len(mT) #~ bweights=np.ones_like(bT)/len(bT) #~ nDCweights=np.ones_like(nDC)/len(nDC) #~ #~ #Plot the histogram #~ #~ plt.hist([mT, bT, nDC], bins=Erange, log=True, histtype='bar',range=limits, label=labels) #~ #~ plt.xlim(limits) #~ plt.xscale('log') #~ plt.xticks(bin_centers, xlabels, rotation=90) #~ plt.xlabel('Epn', labelpad=0) #~ plt.ylabel('Abundance') #~ plt.gca().set_ylim(bottom=0.1) #~ #~ #plot the histogram scaled with E^-2.7 distribution to the second subplot #~ #~ ax2 = plt.subplot(212) #~ #~ plt.hist([mT, bT, nDC], weights=[wmT, wbT, wnDC], log=True, bins=Erange, histtype='bar',range=limits, label=labels) #~ #~ plt.xlim(limits) #~ plt.xscale('log') #~ plt.xticks(bin_centers, xlabels, rotation=90) #~ plt.xlabel('Epn', labelpad=0) #~ plt.ylabel('Scaled Count') #~ plt.legend() #~ #~ fig = plt.gcf() # get current figure #~ title = 'Epn Statistics for ' + str(float(nh)*float(bincount)) + " hours" #~ st = plt.suptitle(title, fontsize=20) #~ st.set_y(0.98) #~ fig.set_size_inches(10, 15) #~ fig.tight_layout() #~ fig.subplots_adjust(top=0.95) #~ #~ plt.savefig('/d6/rstein/Hamburg-Cosmic-Rays/report/graphs/energystats.png') #~ plt.savefig('/d6/rstein/Hamburg-Cosmic-Rays/positioning/graphs/stats/energystats.pdf') #~ #~ plt.close() #Plot variables Etheshold and Ring Radius againist height ax3 = plt.subplot(111) rawheights = [] emin = [] rmax = [] Erange = [3521, 750, 232] labels = [" = Infinity "] for e in Erange: labels.append(" = " + str(e)) colors = ["k", "r", "g", "m", "c", "b"] nucleonmass = 0.93827 betarange = [1.0] for E in Erange: gamma = float(E) / nucleonmass betarange.append(math.sqrt(1 - (float(1) / (gamma**2)))) print Erange print betarange curves = [] for j in range(0, len(betarange)): curves.append([]) rawheights.append([]) #Look up values from atmprofile10.csv with open( '/d6/rstein/Hamburg-Cosmic-Rays/positioning/atmospheredata/atmprofile.csv', 'rb') as csvfile: reader = csv.reader(csvfile, delimiter=',', quotechar='|') i = 0 for row in reader: i += 1 #Skip the first three rows if i > 3: #Loads the height and Refractive Index height = float(row[0]) * 1000 ri = float(row[3]) + float(1) #Calculate the maximum Cherenkov Angle as 1/eta for j in range(0, len(betarange)): beta = betarange[j] costheta = float(1) / ((float(ri)) * float(beta)) if costheta < 1: theta = math.acos( float(1) / ((float(ri)) * float(beta))) r = theta * (height - altitude) curves[j].append(r) rawheights[j].append(height) #Calculates the Threshold Energy using Refractive Index Ethreshold = float(cr.runemin(ri)) * (10**-3) #Calculate the Maximum Ring Size, and appends these values to arrays emin.append(Ethreshold) for j in range(0, len(betarange)): print j, len(curves[j]), len(rawheights[j]) label = "Radius (m) for Epn" + str(labels[j]) ax3.plot(curves[j], rawheights[j], color=colors[j], label=label) #~ plt.axhspan(19000, 26000, color='m', alpha=0.5) ax3.plot(emin, rawheights[0], color='b', label="Threshold Energy (TeV per Nucleon)") plt.xscale('log') plt.ylabel('Height', labelpad=0) plt.legend(loc=3) print "Maximum Radius is", max(curves[0]) #~ ax4 = plt.subplot(212) #~ #~ plots = [] #~ weighting = [] #~ labels = [] #~ #~ requiredfracs=np.linspace(0.6, 0.9, 3) #~ #~ for requiredfrac in requiredfracs: #~ heights = [] #~ #~ probrange = [] #~ normweight = [] #~ #~ stepcount = 1000 #~ #~ lower = float(1)/float(stepcount) #~ upper = float(1-lower) #~ #~ Rrange = np.linspace(lower, upper, stepcount) #~ #~ for R in Rrange: #~ h = atm.runheight(1-R) #~ ri = atm.runindex(h) #~ thetamax = math.acos(float(1)/(float(ri))) #~ rmax = thetamax * (h - altitude) #~ #~ if rmax > 60: #~ #~ rthreshold = 0.5*rmax #~ #~ hthreshold = 20000 #~ r = 120000 #~ #~ while r > rthreshold: #~ ri = atm.runindex(hthreshold) #~ thetamax = math.acos(float(1)/(float(ri))) #~ r = thetamax * (hthreshold - altitude) #~ hthreshold += 1000 #~ #~ requiredr = (requiredfrac*rthreshold) #~ #~ etrial = 250 #~ r = 0 #~ ri = atm.runindex(hthreshold) #~ while r < requiredr: #~ gamma = float(etrial)/nucleonmass #~ beta = math.sqrt(1-(float(1)/(gamma**2))) #~ costheta = float(1)/((float(ri))*beta) #~ #~ if costheta < 1: #~ theta = math.acos(costheta) #~ r = theta * (hthreshold - altitude) #~ #~ etrial += 100 #~ #~ #~ N = ((etrial**(-1.7)))*(float(321)/1.7) #~ print "For the Saturation Threshold of", etrial, "we have a probability of", N, "that an event will meet this" #~ #~ print "For a height", h, "rmax is", rmax, "our threshold is", rthreshold, "(and the an angle is", thetamax, ")" #~ print "We examine", hthreshold, "where the max radius is", r, "and the threshold was", rthreshold #~ print "For beta = 1, we have", rthreshold, "we require", requiredr, "at an energy", etrial, "we have", r #~ print "For a height", h, "Our Probability is", R, "that an event will reach this height or further" #~ heights.append(h) #~ normweight.append(N) #~ label = "For half Rmax, we require r=" + str(round(requiredfrac, 2)) #~ labels.append(label) #~ #~ plots.append(heights) #~ weighting.append(normweight/np.sum(normweight)) #~ #~ plots.append(heights) #~ weighting.append(np.ones(len(heights))/len(heights)) #~ labels.append("All Events") #~ #~ print len(plots), len(weighting), len(labels) #~ #~ plt.hist(plots, bins=20, weights=weighting, histtype='bar', label=labels) #~ #~ plt.xlabel('Height', labelpad=0) #~ plt.ylabel('Normalised Fraction of Events', labelpad=0) plt.legend(loc=2) fig = plt.gcf() # get current figure st = fig.suptitle("Threshold Energy and Cherenkov Ring Radii", fontsize=20) st.set_y(0.98) fig.set_size_inches(10, 15) fig.tight_layout() fig.subplots_adjust(top=0.95) plt.savefig( '/d6/rstein/Hamburg-Cosmic-Rays/positioning/graphs/stats/logenergyradius.pdf' ) plt.savefig( '/d6/rstein/Hamburg-Cosmic-Rays/report/graphs/logenergyradius.png') #~ title = 'Epn Statistics for ' + str(float(nh)*float(bincount)) + " hours" #~ plt.suptitle(title, fontsize=20) plt.savefig( '/d6/rstein/Hamburg-Cosmic-Rays/positioning/graphs/stats/Energy.pdf') if graph: plt.show()
def min(a, gridwidth, eff, phi, epsilon, detections): def f(x,y,Z,Epn, height): sum = 0 for detection in a: x0 = float(detection[0]) y0 = float(detection[1]) count = float(detection[2]) bkgcount = float(detection[3]) category = detection[4] sum += ll.run(x,y,Epn,Z, height, x0,y0, count, bkgcount, category, eff, phi, epsilon) return sum #Runs Minimisation and outputs results startpos=[0, 0, 26.0, 1000, 30000] argumentx = "limit_x = (-300, 300), error_x = 100000, " argumenty = "limit_y = (-300, 300), error_y = 100000, " argumentZ = "fix_Z=True, " argumentE = "limit_Epn = (232, 4000), error_Epn=1000, " argumentheight = "limit_height = (17500, 70000), error_height=(100000), " argumenterror = "print_level=0, errordef = 100" m = eval("Minuit(f, x="+ str(startpos[0]) + ", " + argumentx + "y="+ str(startpos[1]) + ", " + argumenty+ "Epn = "+ str(startpos[3]) + ", " + argumentE + "Z=" + str(startpos[2]) + "," +argumentZ + "height = " + str(startpos[4]) + ", " + argumentheight + argumenterror + ")") m.migrad() params = m.values guess = [params['x'], params['y'], params['Epn'], params['Z'], params['height']] guessfval = m.fval xsites = np.linspace(-150, 150, int(gridwidth)) ysites = np.linspace(-150, 150, int(gridwidth)) eraw = np.linspace(0, 1, num=3) R = eraw*0.0178 evals = ((1.7*R/321)+(3571**-1.7))**(-1/1.7) hvals = np.linspace(20000, 30000, num=3) hvals = [25000] zvalues = np.arange(20.,33.) minangle = 0.0 j = 0 while j < 5: coordinates = [] j = 0 minangle += 0.1 for x in xsites: for y in ysites: n=0 for detection in a: x0 = float(detection[0]) y0 = float(detection[1]) recordeddangle = float(detection[5]) dangle = ce.dangle(x0, y0, x, y) if math.fabs(dangle - recordeddangle) < math.radians(minangle): n+=1 if n > (len(a)-1): coordinates.append([x,y]) j+=1 if minangle > 20: j=10 ehvals=[] for height in hvals: for Epn in evals: ri = atm.runindex(height) Ethreshold = float(cr.runemin(ri)) if float(Epn) > float(Ethreshold): ehvals.append([Epn, height]) xycount = len(coordinates) ehcount = len(ehvals) zcount = len(zvalues) line = [0,0] #~ rg = guess #~ rf = guessfval print detections, "Detections -> ", xycount, "Valid Core Positions (", minangle, "Degrees from recorded axis)", ehcount, "Valid Height/Epn Combinations", zcount, "Charge Values", xycount*ehcount*zcount, "Total Minimisations" for z in zvalues: m = eval("Minuit(f, x="+ str(startpos[0]) + ", " + argumentx + "y="+ str(startpos[1]) + ", " + argumenty+ "Epn = "+ str(startpos[3]) + ", " + argumentE + "Z=" + str(z) + "," +argumentZ + "height = " + str(startpos[4]) + ", " + argumentheight + argumenterror + ")") m.migrad(resume=False) params = m.values zguess = [params['x'], params['y'], params['Epn'], params['Z'], params['height']] zguessfval = m.fval for [x, y] in coordinates: for [e, h] in ehvals: m = eval("Minuit(f, " + "Epn=" + str(e) + ", " + argumentE + "height = " + str(h) + ", " + argumentheight + "Z=" + str(z) + "," +argumentZ + "x="+ str(x) + ", " + argumentx + "y="+ str(y) + ", " + argumenty+ argumenterror + ")") m.migrad(resume=False) params = m.values fval = m.fval values = m.get_fmin() if fval < zguessfval: if values.is_valid: zguess = [params['x'], params['y'], params['Epn'], params['Z'], params['height']] zguessfval = fval #~ print zguess, "(", zguessfval, ") Valid!" #~ elif fval < rf: #~ rf = fval #~ rg = [params['x'], params['y'], params['Epn'], params['Z'], params['height']] #~ print rg, "(", rf, ") Rejected!" print time.asctime(time.localtime()), zguess, "(", zguessfval, ")" #~ print rg, "(", rf, ") Rejected!" if zguessfval < guessfval: guess = zguess guessfval = zguessfval print "Final guess is", guess, math.degrees(phi), math.degrees(epsilon), "(", guessfval, ")" return guess[0], guess[1], guess[2], guess[3], guess[4], guessfval
def min(fullsimulation, layout, gridwidth, raweff): measured = fullsimulation.detected true = fullsimulation.true def full(x,y, Epn): energy = 56*Epn testevent = event(x, y, measured.epsilon, energy, 26, 25000, measured.phi, N=56, layout=layout, smear=False) testevent.simulatetelescopes(raweff) testevent.calculatefulllikelihood(measured) ll = testevent.minusllfull #~ print "x", x, "y", y, "Epn", Epn, "ll", ll return ll #Runs Minimisation and outputs results fullstartpos=[0, 0, 1000, 20000] argumentx = "limit_x = (-300, 300), error_x = 100000, " argumenty = "limit_y = (-300, 300), error_y = 100000, " argumentZ = "limit_Z = (16,36), error_Z = 2, " argumentheight = "limit_height = (15000, 65000), error_height=(100000), " argumentE = "limit_Epn = (232, 4000), error_Epn=1000, " argumenterror = "print_level=0, errordef = 100" m = eval("Minuit(full, x="+ str(fullstartpos[0]) + ", " + argumentx + "y="+ str(fullstartpos[1]) + ", " + argumenty+ "Epn = "+ str(fullstartpos[2]) + ", " + argumentE + argumenterror + ")") m.migrad() guessparams = m.values guessfval = m.fval xsites = np.linspace(-150, 150, 300) ysites = np.linspace(-150, 150, 300) eraw = np.linspace(0, 1, num=50) R = eraw**3.01*(10**-3) evals = ((1.7*R/321)+(2411**-1.7))**(-1/1.7) minangle = 0.0 j = 0 #~ while j < 20: coordinates = [] j = 0 minangle += 0.1 for x in xsites: for y in ysites: n=0 for tel in measured.telescopes: newdangle = ce.dangle(tel.x,tel.y, x, y) if math.fabs(newdangle - tel.dangle) < math.radians(minangle): n+=1 if n > (len(measured.telescopes)-1): coordinates.append([x,y]) j+=1 if minangle > 20: j=200 #~ for [x, y] in coordinates: for e in evals: m = eval("Minuit(full, " + "Epn=" + str(e) + ", " + argumentE + "x="+ str(x) + ", " + argumentx + "y="+ str(y) + ", " + argumenty+ argumenterror + ")") m.migrad(resume=False) params = m.values fval = m.fval values = m.get_fmin() if fval < guessfval: if values.is_valid: guessparams = params guessfval = fval print "Final guess is", guessparams, "(", guessfval, ")" print "Measured values are", [measured.rayxpos, measured.rayypos, measured.epn], "(", true.minusllfull, ")", true.fullmultiplicity fullreconx = guessparams['x'] fullrecony = guessparams['y'] reconEpn = guessparams['Epn'] def dc(Z,height, x, y): energy = 56*reconEpn testevent = event(x, y, measured.epsilon, energy, Z, height, measured.phi, N=56, layout=layout, smear=False) testevent.simulatetelescopes(raweff) testevent.calculatedclikelihood(measured) testevent.calculatefulllikelihood(measured) if math.fabs(Z-26) > 10: print "x", x, "y", y, "Height", height, "Z", Z, ll = testevent.minuslldc if math.fabs(Z-26) > 10: print "ll1",ll, ll += testevent.minusllfull if math.fabs(Z-26) > 10: print "ll2", ll return ll xsites = np.linspace(-150, 150, int(gridwidth)) ysites = np.linspace(-150, 150, int(gridwidth)) eraw = np.linspace(0, 1, num=3) R = eraw*0.0178 evals = ((1.7*R/321)+(3571**-1.7))**(-1/1.7) hvals = np.linspace(20000, 30000, num=3) zvalues = np.arange(20.,33.) minangle = 0.0 j = 0 ehvals=[] for height in hvals: for Epn in evals: ri = atm.runindex(height) Ethreshold = float(cr.runemin(ri)) if float(Epn) > float(Ethreshold): ehvals.append([Epn, height]) #~ xycount = len(coordinates) ehcount = len(ehvals) zcount = len(zvalues) line = [0,0] print ehvals print measured.DCmultiplicity, "Detections -> " print xycount, "Valid Core Positions (", minangle, "Degrees from recorded axis)", ehcount, "Valid Height/Epn Combinations", zcount, "Charge Values", xycount*ehcount*zcount, "Total Minimisations" m = eval("Minuit(dc, x="+ str(fullreconx) + ", " + argumentx + "y="+ str(fullrecony) + ", " + argumenty+ "Z=26," +argumentZ + "height = 20000, " + argumentheight + argumenterror + ")") m.migrad(resume=False) params = m.values guessparams = params guessfval = m.fval for z in zvalues: for h in hvals: for [x, y] in coordinates: m = eval("Minuit(dc, x="+ str(x) + ", " + argumentx + "y="+ str(y) + ", " + argumenty+ "height = " + str(h) + ", " + argumentheight + "Z=" + str(z) + "," +argumentZ + argumenterror + ")") m.migrad(resume=False) params = m.values fval = m.fval values = m.get_fmin() if fval < guessfval: if values.is_valid: if fval < guessfval: guessparams = params guessfval = fval print "Final guess is", guessparams, math.degrees(measured.phi), math.degrees(measured.epsilon), "(", guessfval, ")" reconZ = guessparams["Z"] reconheight = guessparams["height"] reconx = guessparams["x"] recony = guessparams["y"] energy = reconEpn*56 print "Recon", [reconx, recony, reconEpn, reconZ, reconheight] print "True", [true.rayxpos, true.rayypos, true.epn, true.Z, true.height] fullsimulation.reconstructed = event(reconx, recony, measured.epsilon, energy, reconZ, reconheight, measured.phi, N=56, layout=layout, smear=False) fullsimulation.reconstructed.simulatetelescopes(raweff) fullsimulation.getreconstructedlikelihood() fullsimulation.reconstructed.fullrayxpos = fullreconx fullsimulation.reconstructed.fullrayypos = fullrecony
def min(fullsimulation, layout, gridwidth, raweff): measured = fullsimulation.detected true = fullsimulation.true def full(x, y, Epn): energy = 56 * Epn testevent = event(x, y, measured.epsilon, energy, 26, 25000, measured.phi, N=56, layout=layout, smear=False) testevent.simulatetelescopes(raweff) testevent.calculatefulllikelihood(measured) ll = testevent.minusllfull # ~ print "x", x, "y", y, "Epn", Epn, "ll", ll return ll # Runs Minimisation and outputs results fullstartpos = [0, 0, 1000, 20000] argumentx = "limit_x = (-300, 300), error_x = 100000, " argumenty = "limit_y = (-300, 300), error_y = 100000, " argumentZ = "limit_Z = (16,36), error_Z = 2, " argumentheight = "limit_height = (15000, 65000), error_height=(100000), " argumentE = "limit_Epn = (232, 4000), error_Epn=1000, " argumenterror = "print_level=0, errordef = 100" m = eval( "Minuit(full, x=" + str(fullstartpos[0]) + ", " + argumentx + "y=" + str(fullstartpos[1]) + ", " + argumenty + "Epn = " + str(fullstartpos[2]) + ", " + argumentE + argumenterror + ")" ) m.migrad() guessparams = m.values guessfval = m.fval xsites = np.linspace(-150, 150, 300) ysites = np.linspace(-150, 150, 300) eraw = np.linspace(0, 1, num=50) R = eraw ** 3.01 * (10 ** -3) evals = ((1.7 * R / 321) + (2411 ** -1.7)) ** (-1 / 1.7) minangle = 0.0 j = 0 # ~ while j < 20: coordinates = [] j = 0 minangle += 0.1 for x in xsites: for y in ysites: n = 0 for tel in measured.telescopes: newdangle = ce.dangle(tel.x, tel.y, x, y) if math.fabs(newdangle - tel.dangle) < math.radians(minangle): n += 1 if n > (len(measured.telescopes) - 1): coordinates.append([x, y]) j += 1 if minangle > 20: j = 200 # ~ for [x, y] in coordinates: for e in evals: m = eval( "Minuit(full, " + "Epn=" + str(e) + ", " + argumentE + "x=" + str(x) + ", " + argumentx + "y=" + str(y) + ", " + argumenty + argumenterror + ")" ) m.migrad(resume=False) params = m.values fval = m.fval values = m.get_fmin() if fval < guessfval: if values.is_valid: guessparams = params guessfval = fval print "Final guess is", guessparams, "(", guessfval, ")" print "Measured values are", [ measured.rayxpos, measured.rayypos, measured.epn, ], "(", true.minusllfull, ")", true.fullmultiplicity fullreconx = guessparams["x"] fullrecony = guessparams["y"] reconEpn = guessparams["Epn"] def dc(Z, height, x, y): energy = 56 * reconEpn testevent = event(x, y, measured.epsilon, energy, Z, height, measured.phi, N=56, layout=layout, smear=False) testevent.simulatetelescopes(raweff) testevent.calculatedclikelihood(measured) testevent.calculatefulllikelihood(measured) if math.fabs(Z - 26) > 10: print "x", x, "y", y, "Height", height, "Z", Z, ll = testevent.minuslldc if math.fabs(Z - 26) > 10: print "ll1", ll, ll += testevent.minusllfull if math.fabs(Z - 26) > 10: print "ll2", ll return ll xsites = np.linspace(-150, 150, int(gridwidth)) ysites = np.linspace(-150, 150, int(gridwidth)) eraw = np.linspace(0, 1, num=3) R = eraw * 0.0178 evals = ((1.7 * R / 321) + (3571 ** -1.7)) ** (-1 / 1.7) hvals = np.linspace(20000, 30000, num=3) zvalues = np.arange(20.0, 33.0) minangle = 0.0 j = 0 ehvals = [] for height in hvals: for Epn in evals: ri = atm.runindex(height) Ethreshold = float(cr.runemin(ri)) if float(Epn) > float(Ethreshold): ehvals.append([Epn, height]) # ~ xycount = len(coordinates) ehcount = len(ehvals) zcount = len(zvalues) line = [0, 0] print ehvals print measured.DCmultiplicity, "Detections -> " print xycount, "Valid Core Positions (", minangle, "Degrees from recorded axis)", ehcount, "Valid Height/Epn Combinations", zcount, "Charge Values", xycount * ehcount * zcount, "Total Minimisations" m = eval( "Minuit(dc, x=" + str(fullreconx) + ", " + argumentx + "y=" + str(fullrecony) + ", " + argumenty + "Z=26," + argumentZ + "height = 20000, " + argumentheight + argumenterror + ")" ) m.migrad(resume=False) params = m.values guessparams = params guessfval = m.fval for z in zvalues: for h in hvals: for [x, y] in coordinates: m = eval( "Minuit(dc, x=" + str(x) + ", " + argumentx + "y=" + str(y) + ", " + argumenty + "height = " + str(h) + ", " + argumentheight + "Z=" + str(z) + "," + argumentZ + argumenterror + ")" ) m.migrad(resume=False) params = m.values fval = m.fval values = m.get_fmin() if fval < guessfval: if values.is_valid: if fval < guessfval: guessparams = params guessfval = fval print "Final guess is", guessparams, math.degrees(measured.phi), math.degrees(measured.epsilon), "(", guessfval, ")" reconZ = guessparams["Z"] reconheight = guessparams["height"] reconx = guessparams["x"] recony = guessparams["y"] energy = reconEpn * 56 print "Recon", [reconx, recony, reconEpn, reconZ, reconheight] print "True", [true.rayxpos, true.rayypos, true.epn, true.Z, true.height] fullsimulation.reconstructed = event( reconx, recony, measured.epsilon, energy, reconZ, reconheight, measured.phi, N=56, layout=layout, smear=False ) fullsimulation.reconstructed.simulatetelescopes(raweff) fullsimulation.getreconstructedlikelihood() fullsimulation.reconstructed.fullrayxpos = fullreconx fullsimulation.reconstructed.fullrayypos = fullrecony
def run(eff, rowcount, mincount=4, text=False, graph=False, layout="five", number=1, nh=1): #~ #Create a subplot for the fractional abundance #~ #~ ax1 = plt.subplot(211) #~ #~ #Define number of bins, maximum Epn #~ #~ bincount = 20 #~ nmax = 1 #~ emax = 35720 #~ #~ Rrange = np.linspace(0, nmax, bincount) #~ k = math.log(emax/232)/float(nmax) #~ #~ Erange=[] #~ #~ fullcount = [] #~ nDC = [] #~ wnDC = [] #~ bT = [] #~ wbT = [] #~ mT = [] #~ wmT = [] #~ #~ #Iterate over Energies #~ #~ for R in Rrange: #~ #~ Epn = 232*(math.e**(k*R)) #~ #~ Erange.append(Epn) #~ #~ weight = Epn ** (-2.7) #~ #~ #For a given Energy, iterate over n randomly simulated events #~ #~ for i in range (0, int(number)): #~ #~ #As in generate.py, randomly generate all variables (expect Epn) #~ #~ xpos = (random.random()*300)-150 #~ ypos = (random.random()*300)-150 #~ #~ Z= 26 #~ #~ hprob = random.random() #~ height = atm.runheight(hprob, text) #~ #~ zenith = random.random()*44 #~ #~ phi = math.radians(68+zenith) #~ #~ epsilon = math.pi*random.random()*2 #~ #~ radius, theta = cr.run(Epn, height, math.sin(phi), text=text) #~ #~ #Determine what category the event was #~ #~ entry, entrytype = lt.run(layout, xpos, ypos, epsilon, radius, Epn, Z, height, phi, theta, mincount, eff, 1, graph=False, text=False) #~ #~ #append the energy value to the appropriate category #~ #~ if entrytype == "metThreshold": #~ mT.append(Epn) #~ wmT.append(weight) #~ elif entrytype == "belowThreshold": #~ bT.append(Epn) #~ wbT.append(weight) #~ elif entrytype == "nonDC": #~ nDC.append(Epn) #~ wnDC.append(weight) #~ else: #~ print "ERROR OVER HERE!!!!" #~ #~ #Create labels for each bin #~ #~ labels = ["metThreshold", "belowThreshold", "nonDC"] #~ #~ xlabels=[] #~ #~ for i in Erange: #~ xlabels.append(int(i)) #~ #~ Erange.append(emax + 1) #~ bin_centers = 0.5 * np.diff(Erange) + Erange[:-1] #~ #~ limits = [Erange[0], Erange[bincount]] #~ #~ mweights = np.ones_like(mT)/len(mT) #~ bweights=np.ones_like(bT)/len(bT) #~ nDCweights=np.ones_like(nDC)/len(nDC) #~ #~ #Plot the histogram #~ #~ plt.hist([mT, bT, nDC], bins=Erange, log=True, histtype='bar',range=limits, label=labels) #~ #~ plt.xlim(limits) #~ plt.xscale('log') #~ plt.xticks(bin_centers, xlabels, rotation=90) #~ plt.xlabel('Epn', labelpad=0) #~ plt.ylabel('Abundance') #~ plt.gca().set_ylim(bottom=0.1) #~ #~ #plot the histogram scaled with E^-2.7 distribution to the second subplot #~ #~ ax2 = plt.subplot(212) #~ #~ plt.hist([mT, bT, nDC], weights=[wmT, wbT, wnDC], log=True, bins=Erange, histtype='bar',range=limits, label=labels) #~ #~ plt.xlim(limits) #~ plt.xscale('log') #~ plt.xticks(bin_centers, xlabels, rotation=90) #~ plt.xlabel('Epn', labelpad=0) #~ plt.ylabel('Scaled Count') #~ plt.legend() #~ #~ fig = plt.gcf() # get current figure #~ title = 'Epn Statistics for ' + str(float(nh)*float(bincount)) + " hours" #~ st = plt.suptitle(title, fontsize=20) #~ st.set_y(0.98) #~ fig.set_size_inches(10, 15) #~ fig.tight_layout() #~ fig.subplots_adjust(top=0.95) #~ #~ plt.savefig('/d6/rstein/Hamburg-Cosmic-Rays/report/graphs/energystats.png') #~ plt.savefig('/d6/rstein/Hamburg-Cosmic-Rays/positioning/graphs/stats/energystats.pdf') #~ #~ plt.close() #Plot variables Etheshold and Ring Radius againist height ax3 = plt.subplot(111) rawheights=[] emin=[] rmax = [] Erange = [3521, 750, 232] labels = [" = Infinity "] for e in Erange: labels.append(" = " + str(e)) colors=["k", "r", "g", "m", "c", "b"] nucleonmass = 0.93827 betarange = [1.0] for E in Erange: gamma = float(E)/nucleonmass betarange.append(math.sqrt(1-(float(1)/(gamma**2)))) print Erange print betarange curves=[] for j in range (0, len(betarange)): curves.append([]) rawheights.append([]) #Look up values from atmprofile10.csv with open('/d6/rstein/Hamburg-Cosmic-Rays/positioning/atmospheredata/atmprofile.csv', 'rb') as csvfile: reader = csv.reader(csvfile, delimiter=',', quotechar='|') i=0 for row in reader: i +=1 #Skip the first three rows if i > 3: #Loads the height and Refractive Index height = float(row[0])*1000 ri = float(row[3]) + float(1) #Calculate the maximum Cherenkov Angle as 1/eta for j in range (0, len(betarange)): beta = betarange[j] costheta = float(1)/((float(ri))*float(beta)) if costheta < 1: theta = math.acos(float(1)/((float(ri))*float(beta))) r = theta*(height-altitude) curves[j].append(r) rawheights[j].append(height) #Calculates the Threshold Energy using Refractive Index Ethreshold = float(cr.runemin(ri))*(10**-3) #Calculate the Maximum Ring Size, and appends these values to arrays emin.append(Ethreshold) for j in range (0, len(betarange)): print j, len(curves[j]), len(rawheights[j]) label = "Radius (m) for Epn" + str(labels[j]) ax3.plot(curves[j], rawheights[j], color = colors[j], label=label) #~ plt.axhspan(19000, 26000, color='m', alpha=0.5) ax3.plot(emin,rawheights[0], color='b', label="Threshold Energy (TeV per Nucleon)") plt.xscale('log') plt.ylabel('Height', labelpad=0) plt.legend(loc=3) print "Maximum Radius is", max(curves[0]) #~ ax4 = plt.subplot(212) #~ #~ plots = [] #~ weighting = [] #~ labels = [] #~ #~ requiredfracs=np.linspace(0.6, 0.9, 3) #~ #~ for requiredfrac in requiredfracs: #~ heights = [] #~ #~ probrange = [] #~ normweight = [] #~ #~ stepcount = 1000 #~ #~ lower = float(1)/float(stepcount) #~ upper = float(1-lower) #~ #~ Rrange = np.linspace(lower, upper, stepcount) #~ #~ for R in Rrange: #~ h = atm.runheight(1-R) #~ ri = atm.runindex(h) #~ thetamax = math.acos(float(1)/(float(ri))) #~ rmax = thetamax * (h - altitude) #~ #~ if rmax > 60: #~ #~ rthreshold = 0.5*rmax #~ #~ hthreshold = 20000 #~ r = 120000 #~ #~ while r > rthreshold: #~ ri = atm.runindex(hthreshold) #~ thetamax = math.acos(float(1)/(float(ri))) #~ r = thetamax * (hthreshold - altitude) #~ hthreshold += 1000 #~ #~ requiredr = (requiredfrac*rthreshold) #~ #~ etrial = 250 #~ r = 0 #~ ri = atm.runindex(hthreshold) #~ while r < requiredr: #~ gamma = float(etrial)/nucleonmass #~ beta = math.sqrt(1-(float(1)/(gamma**2))) #~ costheta = float(1)/((float(ri))*beta) #~ #~ if costheta < 1: #~ theta = math.acos(costheta) #~ r = theta * (hthreshold - altitude) #~ #~ etrial += 100 #~ #~ #~ N = ((etrial**(-1.7)))*(float(321)/1.7) #~ print "For the Saturation Threshold of", etrial, "we have a probability of", N, "that an event will meet this" #~ #~ print "For a height", h, "rmax is", rmax, "our threshold is", rthreshold, "(and the an angle is", thetamax, ")" #~ print "We examine", hthreshold, "where the max radius is", r, "and the threshold was", rthreshold #~ print "For beta = 1, we have", rthreshold, "we require", requiredr, "at an energy", etrial, "we have", r #~ print "For a height", h, "Our Probability is", R, "that an event will reach this height or further" #~ heights.append(h) #~ normweight.append(N) #~ label = "For half Rmax, we require r=" + str(round(requiredfrac, 2)) #~ labels.append(label) #~ #~ plots.append(heights) #~ weighting.append(normweight/np.sum(normweight)) #~ #~ plots.append(heights) #~ weighting.append(np.ones(len(heights))/len(heights)) #~ labels.append("All Events") #~ #~ print len(plots), len(weighting), len(labels) #~ #~ plt.hist(plots, bins=20, weights=weighting, histtype='bar', label=labels) #~ #~ plt.xlabel('Height', labelpad=0) #~ plt.ylabel('Normalised Fraction of Events', labelpad=0) plt.legend(loc=2) fig = plt.gcf() # get current figure st = fig.suptitle("Threshold Energy and Cherenkov Ring Radii", fontsize=20) st.set_y(0.98) fig.set_size_inches(10, 15) fig.tight_layout() fig.subplots_adjust(top=0.95) plt.savefig('/d6/rstein/Hamburg-Cosmic-Rays/positioning/graphs/stats/logenergyradius.pdf') plt.savefig('/d6/rstein/Hamburg-Cosmic-Rays/report/graphs/logenergyradius.png') #~ title = 'Epn Statistics for ' + str(float(nh)*float(bincount)) + " hours" #~ plt.suptitle(title, fontsize=20) plt.savefig('/d6/rstein/Hamburg-Cosmic-Rays/positioning/graphs/stats/Energy.pdf') if graph: plt.show()