def generate_singly_diff_fluxes(n_bins,datafile=config["nu_flux"]): """ This is the newest, most streamlined function for generating the singly-differential flux arrays. It has the same functionality as the old ones, but now it skips the step of filling in the regular flux array before swapping to the deposited energy flux array. It goes over the different kind of fluxes and builds up a 2D or 3D array, convolving the entries with the relevant differential cross section the dimensionality depends on whether or we are integrating over the zenith angles """ print("Depositing Energy") data = Data(datafile) e_min = 10*const.GeV e_max = 5*const.PeV extra = 0 all_angles = data.angles event_edges = np.logspace(np.log10(e_min), np.log10(e_max)+extra,n_bins+1) e_deposited_edges = np.logspace(np.log10(e_min) , np.log10(e_max),n_bins+1) angle_edges = np.linspace(min(all_angles), max(all_angles), n_bins+1) # angle is in cos(zenith), so like -1->0 nuflux = {} errs = {} for key in data.get_keys(): #flavor, current, interaction if "Mu_nu" in key: continue fluxy, erry = do_for_key(event_edges,e_deposited_edges,key,data=data, angles=angle_edges) nuflux[key] = fluxy errs[key] = erry # if global variable "angle" isn't none, then we can separate out just a single angle return(event_edges,e_deposited_edges, nuflux, angle_edges, errs)
def downsize(filename, destination): if os.path.exists(destination): print("Already done! Skipping {}".format(destination)) return dataobj = Data(filename) flux = {} for key in dataobj.get_keys(): flux[key] = np.zeros(shape=(Ebin, zbin)) for i_e in range(Ebin): for i_z in range(zbin): flux[key][i_e][i_z] = dataobj.get_flux( energies[i_e], key=key, angle=zeniths[i_z]) # okay now we pickle all_data = {} all_data["e_true"] = energies all_data["a_true"] = zeniths all_data["flux"] = flux f = open(destination, 'wb') pickle.dump(all_data, f, -1) f.close() print("Saved {}".format(destination))
def _load_flux_file(self, filename): """ simplified little thing. Just loads in the fluxes we want """ f = open(filename, 'rb') all_data = pickle.load(f)["flux"] f.close() if False: dirname, filename = os.path.split(filename) temp = Data(filename) all_data = temp.fluxes flux = np.zeros(shape=(len(self.e_reco), len(self.a_reco))) for key in all_data.keys(): if self.check_key(key): flux += np.array(all_data[key]) if self.ui.recoBox.isChecked( ) else self.apply_xs(np.array(all_data[key]), key) return (flux)
def load(null_bool): filename = "null_exp.dat" if null_bool else "ster_exp.dat" if False: #os.path.exists(filename): f = open(filename,'rb') data = pickle.load(f) f.close() return data else: if null_bool: params = SterileParams() else: params = SterileParams(0.0, 0.1609, 0.0, 4.7) # load the fluxes fu_filename = gen_filename(config["datapath"], "raw_det_flux.dat", params) flux = Data(fu_filename) data = build_mc_flux(flux) f = open(filename, 'wb') pickle.dump(data, f, -1) f.close() return data
from cascade.utils import Data, SterileParams, gen_filename, config from cascade.sensitivity.make_from_mc import build_mc_flux from cascade.sensitivity.eff_area_reader import quickload import numpy as np import matplotlib matplotlib.use('Qt5Agg') import matplotlib.pyplot as plt import os # load the fluxes null_f = gen_filename(config["datapath"], "raw_det_flux.dat", SterileParams()) ster_f = gen_filename(config["datapath"], "raw_det_flux.dat", SterileParams(0.0, 0.1609, 0.0, 4.7)) null = Data(null_f) ster = Data(ster_f) # get the binning information filename = "effective_area.nu_mu.txt" area_data = quickload( os.path.join( os.path.join(config["datapath"], "charm_search_supplemental/"), filename)) e_edges = np.array(area_data["e_reco"]) a_edges = np.array(area_data["cos_th"]) e_centers = 0.5 * (e_edges[:-1] + e_edges[1:]) a_centers = 0.5 * (a_edges[:-1] + a_edges[1:]) # get the flux in the centers of each of the bins null_flux = np.zeros(shape=(len(e_centers), len(a_centers)))
from math import log10 import numpy as np import matplotlib matplotlib.use('TkAgg') import matplotlib.pyplot as plt import matplotlib.cm as cm from cascade.utils import config, gen_filename livetime = 10 * 3600 * 24 * 365. null_pt = gen_filename(config["datapath"], config["nu_flux"], 0., 0., 0.) sterile_pt = gen_filename(config["datapath"], config["nu_flux"], 0.13388166, 0.0, 1.3) null_dat = Data(null_pt, 4) sterile_dat = Data(sterile_pt, 4) n_bin = 100 _ang_range = (min(null_dat.angles), max(null_dat.angles)) _eng_range = (min(null_dat.energies), max(null_dat.energies)) angles = np.linspace(_ang_range[0], _ang_range[1], n_bin + 1) energies = np.logspace(log10(_eng_range[0]), log10(_eng_range[1]), n_bin) #angles = null_dat._angles #energies = np.array(null_dat._energies) key_list = null_dat.get_keys()
e_bins = len(energies) a_bins = len(zeniths) inistate = np.zeros(shape=(a_bins, e_bins, 2, n_nu)) for i_e in range(e_bins): for i_a in range(a_bins): for neut_type in range(2): inistate[i_a][i_e][neut_type][1] = 1.0 return inistate # either generate the file or load it in! expected_fn = gen_filename(config["datapath"], "unitary_prob.dat", params) if os.path.exists(expected_fn): final_probs = Data(expected_fn) else: kwargs = {} kwargs["as_data"] = False kwargs["state_setter"] = state_setter kwargs["forced_filename"] = expected_fn final_probs = Data(raw_flux(params, kwargs=kwargs)) # now we need three keys curr = "CC" # doesn't matter neut = "nuBar" root_flav = "Mu" flavors = ['E', 'Mu', 'Tau'] # flavor neutrino current keys = ["_".join([flav, neut, curr]) for flav in flavors]
def raw_flux(params, kwargs={}): """ This is the main function. It saves a data file for the flux with a unique name for the given physics """ if not isinstance(params, SterileParams): raise TypeError("Expected {} for params, not {}".format( SterileParams, type(params))) if "forced_filename" in kwargs: forced_filename = kwargs["forced_filename"] else: forced_filename = None if "state_setter" in kwargs: state_setter = kwargs["state_setter"] else: state_setter = get_initial_state if "osc" in kwargs: osc = kwargs["osc"] else: osc = True if not osc: print("NOT USING OSCILLATIONS") if "as_data" in kwargs: as_data = kwargs["as_data"] else: as_data = False if forced_filename is not None: if not isinstance(forced_filename, str): raise TypeError("Forced filename should be {}, or {}".format( str, None)) print("Propagating Neutrinos at {}".format(params)) n_nu = 4 Emin = 1. * un.GeV Emax = 10. * un.PeV cos_zenith_min = -0.999 cos_zenith_max = 0.2 use_earth_interactions = True zeniths = nsq.linspace(cos_zenith_min, cos_zenith_max, angular_bins) energies = nsq.logspace(Emin, Emax, energy_bins) # DIFFERENT FROM NUMPY LOGSPACE nus_atm = nsq.nuSQUIDSAtm(zeniths, energies, n_nu, nsq.NeutrinoType.both, use_earth_interactions) nus_atm.Set_MixingAngle(0, 1, 0.563942) nus_atm.Set_MixingAngle(0, 2, 0.154085) nus_atm.Set_MixingAngle(1, 2, 0.785398) #sterile parameters nus_atm.Set_MixingAngle(0, 3, params.theta03) nus_atm.Set_MixingAngle(1, 3, params.theta13) nus_atm.Set_MixingAngle(2, 3, params.theta23) nus_atm.Set_SquareMassDifference(3, params.msq2) nus_atm.Set_SquareMassDifference(1, 7.65e-05) nus_atm.Set_SquareMassDifference(2, 0.00247) nus_atm.SetNeutrinoCrossSections(xs) nus_atm.Set_TauRegeneration(True) #settting some zenith angle stuff nus_atm.Set_rel_error(1.0e-6) nus_atm.Set_abs_error(1.0e-6) #nus_atm.Set_GSL_step(gsl_odeiv2_step_rk4) nus_atm.Set_GSL_step(nsq.GSL_STEP_FUNCTIONS.GSL_STEP_RK4) # we load in the initial state. Generating or Loading from a file inistate = state_setter(energies, zeniths, n_nu, kwargs) if np.min(inistate) < 0: raise ValueError("Found negative value in inistate: {}".format( np.min(inistate))) nus_atm.Set_initial_state(inistate, nsq.Basis.flavor) # we turn off the progress bar for jobs run on the cobalts nus_atm.Set_ProgressBar(False) nus_atm.Set_IncludeOscillations(osc) nus_atm.EvolveState() int_en = 700 int_cos = 100 int_min_e = log10(Emin) int_max_e = log10(Emax) filename = "" if not as_data: if forced_filename is None: filename = gen_filename(config["datapath"], config["nu_flux"] + ".dat", params) else: filename = forced_filename print("Saving File to {}".format(filename)) if not config["overwrite"]: backup(filename) obj = open(filename, 'wt') else: cobalt = os.environ.get("_CONDOR_SCRATCH_DIR") if cobalt == None or cobalt == "" or cobalt == ".": this_dir = None else: this_dir = cobalt obj = NamedTemporaryFile(mode='wt', buffering=1, dir=this_dir) angle = cos_zenith_min energy = int_min_e obj.write( "# log10(energy) cos(zenith) flux_nuE flux_nuMu flux_nuTau flux_nuEBar flux_nuMuBar flux_nuTauBar\n" ) this_value = 0.0 while angle < cos_zenith_max: energy = int_min_e while energy < int_max_e: #obj.write( ~string~ ) obj.write("{} {}".format(energy, angle)) reg_energy = pow(10., energy) for flavor in range(n_nu): this_value = nus_atm.EvalFlavor(flavor, angle, reg_energy, 0) if this_value < 0: this_value = 0.0 obj.write(" {}".format(this_value)) for flavor in range(n_nu): this_value = nus_atm.EvalFlavor(flavor, angle, reg_energy, 1) if this_value < 0: this_value = 0.0 obj.write(" {}".format(this_value)) energy += (int_max_e - int_min_e) / int_en obj.write("\n") angle += (cos_zenith_max - cos_zenith_min) / int_cos if as_data: data_obj = Data(obj.name) obj.close() # deletes tempfile return data_obj else: obj.close() return (filename)