Exemplo n.º 1
0
def get_weighter(open_hdf_file, simtype, nfiles=1):
    '''
	please first open the file by doing

	open_hdf_file = pandas.HDFStore(file, 'r')

	'''
    if simtype is 'nugen':
        weighter = simweights.NuGenWeighter(open_hdf_file, nfiles=nfiles)
    elif simtype is 'corsika':
        weighter = simweights.CorsikaWeighter(open_hdf_file, nfiles=nfiles)
    else:
        raise NotImplementedError(
            'Simi type {} is not implemented'.format(simtype))
    return weighter
Exemplo n.º 2
0
def find_weighter(fileobj: pd.HDFStore, nfiles: int) -> simweights.Weighter:
    """
    Try to automatically determine which type of file this is and return the correct weighter
    """
    try:
        return simweights.CorsikaWeighter(fileobj)
    except RuntimeError:
        pass
    try:
        return simweights.CorsikaWeighter(fileobj, nfiles=nfiles)
    except AttributeError:
        pass
    try:
        return simweights.NuGenWeighter(fileobj, nfiles=nfiles)
    except AttributeError:
        pass
    try:
        return simweights.GenieWeighter(fileobj)
    except AttributeError:
        pass
    raise RuntimeError(
        f"Could not find a suitable weighter for file object `{fileobj.filename}`"
    )
Exemplo n.º 3
0
files = args.input_files

primary_energy = np.asarray([])
weights = np.asarray([])

# weighter = None

def power_law(energy):
	# https://pos.sissa.it/301/1005/pdf
	return 1.01e-18 * np.power(energy/1e5,-2.19)

cr_model = nuflux.makeFlux('H3a_SIBYLL23C')

for file in files:
	file_in = pd.HDFStore(file, 'r')
	loc_weighter = simweights.NuGenWeighter(file_in, nfiles=10)

	# if weighter is None:
	# 	weighter = loc_weighter
	# else:
	# 	weighter += loc_weighter
	
	local_primary_energy = loc_weighter.get_column('PolyplopiaPrimary', 'energy')
	# local_weights = loc_weighter.get_weights(power_law)
	local_weights = loc_weighter.get_weights(cr_model)
	
	primary_energy = np.concatenate((primary_energy, local_primary_energy))
	weights = np.concatenate((weights, local_weights))

	file_in.close()
Exemplo n.º 4
0
for f in filelist:
    print("Reading", f)
    inFile_nugen = dataio.I3File(f)
    while inFile_nugen.more():
        frame = inFile_nugen.pop_physics()
        if "FilterMask" in frame:
            if frame["FilterMask"]["MuonFilter_13"].condition_passed:
                MCmuonEnergy_nugen = np.append(
                    MCmuonEnergy_nugen,
                    get_most_energetic_muon(frame["MMCTrackList"]))
                for k in weight_keys:
                    I3MCWeightDict[k].append(frame["I3MCWeightDict"][k])

nfiles = len(filelist)
wobj = simweights.NuGenWeighter({"I3MCWeightDict": I3MCWeightDict},
                                nfiles=nfiles)

# check that what we got matches what is in OneWeight
np.testing.assert_allclose(
    wobj.get_weights(1),
    np.array(I3MCWeightDict["OneWeight"]) /
    (0.5 * I3MCWeightDict["NEvents"][0] * nfiles),
)

conventional = nuflux.makeFlux("honda2006")
conventional.knee_reweighting_model = "gaisserH3a_elbert"
weights_simweights = wobj.get_weights(conventional)

erange = wobj.surface.get_energy_range(None)
czrange = wobj.surface.get_cos_zenith_range(None)
print(wobj.surface)
Exemplo n.º 5
0
import pandas as pd
import pylab as plt

import simweights

# load the hdf5 file that we just created using pandas
hdffile = pd.HDFStore("Level2_IC86.2016_NuMu.021217.hdf5", "r")

# instantiate the weighter object by passing the pandas file to it
weighter = simweights.NuGenWeighter(hdffile, nfiles=10)


# create an function to represent the IceCube northern track limit
# Note that the units are GeV^-1 * cm^-2 * sr^-1 * s^-1 per particle type
def northern_track(energy):
    return 1.44e-18 / 2 * (energy / 1e5)**-2.2


# get the weights by passing the flux to the weighter
weights = weights = weighter.get_weights(northern_track)

# print some info about the weighting object
print(weighter.tostring(northern_track))

# create equal spaced bins in log space
bins = plt.geomspace(1e2, 1e8, 50)

# get energy of the primary cosmic-ray from `PolyplopiaPrimary`
primary_energy = weighter.get_column("PolyplopiaPrimary", "energy")

# histogram the primary energy with the weights
Exemplo n.º 6
0
import numpy as np
import pylab as plt
import tables

import simweights

# start-example1
# load hdf5 file
f = tables.open_file("Level2_IC86.2016_NuMu.021217.N100.hdf5", "r")
# create weighter object
w = simweights.NuGenWeighter(f, nfiles=10)
# create the energy and zenith bins
energy_bins = np.geomspace(1e2, 1e8, 25)
zenith_bins = [-1, -0.5, 0, 0.5, 1]

# calculate the effective area in energy and zenith bins with all of the events in the sample
effective_area = w.effective_area(energy_bins, zenith_bins)

# make some labels for the different zenith bins
zenith_labels = [
    "120° < Zenith < 180°",
    "\u200790° < Zenith < 120°",
    "\u200760° < Zenith < \u200790°",
    "\u2007\u20070° < Zenith < \u200760°",
]

# for each zenith bin plot effective area as a function of energy
for i, zenith_slice in enumerate(effective_area):
    plt.step(energy_bins, np.r_[0, zenith_slice], label=zenith_labels[i])
# end-example1