dir_name = "/data/user/brelethford/AGN_Core/Plots/fast/plots/stacking/{}yr/{}/".format(len(seasons),cat) #dir_name = "/home/brelethford/public_html/skylab/fast/plots/stacking/{}yr/{}/{}/".format(len(seasons),cat,weight) subprocess.call(['mkdir', '-p', '{0}'.format(dir_name)]) else: dir_name = "/data/user/brelethford/AGN_Core/Plots/fast/plots/allsky/{0}yr/dec_{1:.3f}/".format(len(seasons),np.degrees(src_dec)) #dir_name = "/home/brelethford/public_html/skylab/fast/plots/allsky/{0}yr/dec_{1:.3f}/".format(len(seasons),np.degrees(src_dec)) subprocess.call(['mkdir', '-p', '{0}'.format(dir_name)]) def get_bg_fit(): ts_parameters = fit_background(trials, dir_name + "background_trials_%s.png" % tag) print ("Finished background fit.") return ts_parameters #save trials and fit params outfile = sens_dir + 'trials.array' trials = cache.get(outfile, get_background) outfile = sens_dir + 'bg_fit.array' ts_parameters = cache.get(outfile, get_bg_fit) median_ts, eta, ndf, scale = ts_parameters print("\nBackground only TS > 0 described by:") print(" Median TS: %6.4f" % median_ts) print(" PDF(TS>0): %6.4f * chi2(ts / %.4f, ndf = %.4f)" % (eta, scale, ndf)) ################################### # BACKGROUND ONLY TS DISTRIBUTION # ###################################
# BACKGROUND ONLY TS DISTRIBUTION # ################################### print("\nRunning injection trials with {} events...".format(n_inj)) def get_trials(): t0 = time.time() trials = llh.do_trials(args.nscramble, injector=inj, mean_signal=n_inj, src_ra=src_ra, src_dec=src_dec, src_extension=src_ext, src_w=src_w) dt = time.time() - t0 print(" Completed %d trials in %.2f sec (%.2f trials/sec)" % (trials.size, dt, trials.size / dt)) return trials #save trials and fit params outfile = sens_dir + 'trials_batch_{}.array'.format(batch) trials = cache.get(outfile, get_trials) print("\nInjection with {} events batch {} finished.".format(n_inj, batch)) ################################### # BACKGROUND ONLY TS DISTRIBUTION # ###################################
redshift = [float(catalog[i][15]) for i in range(len(catalog))] lum = [float(catalog[i][16]) for i in range(len(catalog))] gamma = [float(catalog[i][11]) for i in range(len(catalog))] flux = [float(catalog[i][7]) for i in range(len(catalog))] params = { 'ra': ra, 'dec': dec, 'redshift': redshift, 'gamma': gamma, 'flux': flux, 'lum': lum } return params params = cache.get(datafolder + 'SwiftBAT70m/pickle/params.pickle', get_SwiftBAT_params) ###Now I'll read in the background TS stuff.### datafolder = '/data/user/brelethford/Output/stacking_sensitivity/SwiftBAT70m/flux/background_trials/' files = [ cache.load(datafolder + file) for file in os.listdir(datafolder) if file.endswith('.array') ] n_inj = [] nsources = [] TS = [] beta = (0.5) #For background ts TS_beta = [] #Calculated from the total TS median after we get all the TS. beta_err = [] gamma = []
TS.append(file['TS'][item]) TSs = TS eta = float(np.count_nonzero(TSs)) / len(TSs) fit = fitfun(eta=eta, df=2.) TSval5sig = fit.isf(norm.sf(5)) return TSval5sig if corrected: backfolder = '/data/user/brelethford/Output/stacking_sensitivity/{0}/{1}yr_corrected/{2}/background_trials/'.format( catalog, str(years), llhweight) else: backfolder = '/data/user/brelethford/Output/stacking_sensitivity/{0}/{1}yr/{2}/background_trials/'.format( catalog, str(years), llhweight) TSval_median = cache.get(backfolder + 'TSval.pickle', getTSval) TSval5sig = cache.get(backfolder + 'TSval5sig.pickle', getTSval5sig) exp40 = np.load(datafolder + '/IC40_exp.npy') exp59 = np.load(datafolder + '/IC59_exp.npy') exp79 = np.load(datafolder + '/IC79b_exp.npy') exp86 = np.load(datafolder + '/IC86_exp.npy') if corrected: mc40 = np.load(datafolder + '/IC40_corrected_MC.npy') mc59 = np.load(datafolder + '/IC59_corrected_MC.npy') mc79 = np.load(datafolder + '/IC79b_corrected_MC.npy') mc86 = np.load(datafolder + '/IC86_corrected_MC.npy') else: mc40 = np.load(datafolder + '/IC40_MC.npy') mc59 = np.load(datafolder + '/IC59_MC.npy') mc79 = np.load(datafolder + '/IC79b_MC.npy')
################################### # BACKGROUND ONLY TS DISTRIBUTION # ################################### print("\nRunning background only trials ...") def get_background(): t0 = time.time() trials = llh.do_trials(args.nscramble, src_ra=src_ra, src_dec=src_dec, src_extension=src_ext, src_w=src_w) dt = time.time() - t0 print(" Completed %d trials in %.2f sec (%.2f trials/sec)" % (trials.size, dt, trials.size / dt)) return trials #save trials and fit params outfile = sens_dir + 'trials_batch_{}.array'.format(batch) trials = cache.get(outfile, get_background) print("\nBackground batch {} finished.".format(batch)) ################################### # BACKGROUND ONLY TS DISTRIBUTION # ###################################
redshift = [float(catalog[i][15]) for i in range(len(catalog))] lum = [float(catalog[i][16]) for i in range(len(catalog))] gamma = [float(catalog[i][11]) for i in range(len(catalog))] flux = [float(catalog[i][7]) for i in range(len(catalog))] params = { 'ra': [ra[0]], 'dec': [dec[0]], 'redshift': [redshift[0]], 'gamma': [gamma[0]], 'flux': [flux[0]], 'lum': [lum[0]] } return params params = cache.get(datafolder + 'SwiftBAT70m/pickle/onesource.pickle', get_SwiftBAT_params) ###Now I'll read in the background TS stuff.### datafolder = '/data/user/brelethford/Output/stacking_sensitivity/SwiftBAT70m/{}/background_trials/'.format( llhweight) files = [ cache.load(datafolder + file) for file in os.listdir(datafolder) if file.endswith('.array') ] n_inj = [] nsources = [] TS = [] beta = (0.5) #For background ts TS_beta = [] #Calculated from the total TS median after we get all the TS. beta_err = []
MultiPointSourceLLH.weighted_sensitivity(llhmodel, src_ra=np.atleast_1d(0.), src_dec=np.atleast_1d(src_dec[i]), alpha=.5, beta=.9, inj=inj, trials={ 'n_inj': [], 'TS': [], 'nsources': [], 'gamma': [] }, bckg_trials=bckg_trials, eps=10.99, n_iter=1) flux_array.append(inj.mu2flux(1) * 1000) for i in range(len(src_dec)): print('for declination = ' + str(src_dec[i]) + ', flux = ' + str(flux_array[i])) def get_results(): results = {'dec': src_dec, 'flux': flux_array} return results results = cache.get( '/data/user/brelethford/Output/stacking_sensitivity/testing/onesource.pickle', get_results)
backfolder = '/data/user/brelethford/Output/stacking_sensitivity/{0}/bstacking_{1}yr/{2}/schatto79_background_trials/'.format(catalog, str(years), llhweight) def getTSval(): files = [cache.load(backfolder+file) for file in os.listdir(backfolder) if file.endswith('.array')] fitfun = delta_chi2 TS=[] for file in files: for item in range(len(file['n_inj'])): if file['n_inj'][item] ==0: TS.append(file['TS'][item]) TSs=TS fit = fitfun(TSs, df=2., floc=0., fscale=1.) TSval = np.asscalar(fit.isf(norm.sf(5))) return TSval TSval = cache.get(backfolder + 'TSval5sig.pickle', getTSval) print ('TSval = ' + str(TSval)) #Now I've defined all the possible modelweights for each catalog (I can add new ones later). Now I'll read in the years of data. if catalog == '4yr_Starburst': import starburst_data_schatto79 as datascript else: import new_data_multi as datascript ##Here I add an argument for the sole purpose of having separate background files differently named. ## ## We'll assign the proper weighting scheme for the search, then use it to calculate and cache the associated bckg trials: ## llh40= datascript.init40(energy=True,mode='all') llh59= datascript.init59(energy=True,mode='all') llh79 = datascript.init79(energy=True,mode='all') llh86I= datascript.init86I(energy=True,mode='all')
def getting(filename, *a, **kw): prush('<-> {0} ...'.format(filename)) return cache.get(filename, *a, **kw)
muex86I_data = getBoth('SplineMPEMuEXDifferential','energy',fdata_86_1) #Doing ra and dec calculations take a while - pickle? mjd86I_sim=getBoth('SplineMPE','time',fsim_86_1) mjd86I_data = getBoth('SplineMPE','time',fdata_86_1) #I'll recalculate the ras, since we need to unblind before using the actual ras. def get_coords(): ra86I_sim,dec86I_sim=astro.dir_to_equa(zen86I_sim,azi86I_sim,mjd86I_sim) ra86I_data,dec86I_data=astro.dir_to_equa(zen86I_data,azi86I_data,mjd86I_data) #scramble RA! We can't know this until unblinding. (Should I fix these in place, or renew them each time?) ra86I_sim = np.random.random(len(ra86I_sim))*2*np.pi ra86I_data = np.random.random(len(ra86I_data))*2*np.pi return ra86I_sim, dec86I_sim , ra86I_data, dec86I_data ra86I_sim, dec86I_sim , ra86I_data, dec86I_data = cache.get (filename_pickle+"coords.pickle", get_coords) #Let's now make a histogram of the sin(decs). In order to hist a MC set, we should first weight it with... honda = NewNuFlux.makeFlux('honda2006') honda.knee_reweighting_model = 'gaisserH3a_elbert' flux = honda.getFlux #Weighting fcn for MC. Later on we'll need to limit the data to a certain dec band, so let's make the masking optional. To pass it a mask, we'll need to specify the dec band (i.e., pass the function [bandmask_sim[bandnum]]). def simweight(fsim,gamma,mask=None): if mask == None: mask=np.cast[np.bool](np.ones_like(getBoth('I3MCWeightDict','OneWeight',fsim))) OneWeight=getBoth('I3MCWeightDict','OneWeight',fsim)[:][mask] zen_sim = getBoth('MCPrimary1','zenith',fsim)[mask] energy_sim=getBoth('MCPrimary1','energy',fsim)[mask]