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}`" )
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
import numpy as np import pylab as plt import tables import simweights # start-box1 # load the Medium Energy file ME_file = tables.File("Level2_IC86.2016_corsika.021746.hdf5", "r") ME_weighter = simweights.CorsikaWeighter(ME_file) # load the High Energy file HE_file = tables.File("Level2_IC86.2016_corsika.021745.hdf5", "r") HE_weighter = simweights.CorsikaWeighter(HE_file) # A combined weighter is created by summing two weighters combined_weighter = ME_weighter + HE_weighter # create a flux object and calculate the weights for all three weighters flux_model = simweights.GaisserH3a() ME_weights = ME_weighter.get_weights(flux_model) HE_weights = HE_weighter.get_weights(flux_model) combined_weights = combined_weighter.get_weights(flux_model) # use Weighter.get_weight_column() to get the MC truth energy for each sample ME_energy = ME_weighter.get_weight_column("energy") HE_energy = HE_weighter.get_weight_column("energy") combined_energy = combined_weighter.get_weight_column("energy") # Histogram all three samples Ebins = np.geomspace(3e4, 1e10, 64)
# get the weighting object w = frame["I3CorsikaWeight"] # for each of the columns we need get it from the frame object # and put it in the correct column I3CorsikaWeight["energy"].append(w.primary.energy) I3CorsikaWeight["type"].append(w.primary.type) I3CorsikaWeight["zenith"].append(w.primary.dir.zenith) I3CorsikaWeight["weight"].append(w.weight) # make a dictionary object to mimic the file structure of a pandas file fileobj = dict(I3PrimaryInjectorInfo=I3PrimaryInjectorInfo, I3CorsikaWeight=I3CorsikaWeight) # create the weighter object weighter = simweights.CorsikaWeighter(fileobj) # create an object to represent our cosmic-ray primary flux model flux = simweights.GaisserH4a() # get the weights by passing the flux to the weighter weights = weights = weighter.get_weights(flux) # print some info about the weighting object print(weighter.tostring(flux)) # create equal spaced bins in log space bins = plt.geomspace(3e4, 1e6, 50) # get energy of the primary cosmic-ray from `PolyplopiaPrimary` primary_energy = weighter.get_weight_column("energy")
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_corsika.021682.hdf5", "r") # instantiate the weighter object by passing the pandas file to it weighter = simweights.CorsikaWeighter(hdffile) # create an object to represent our cosmic-ray primary flux model flux = simweights.GaisserH4a() # get the weights by passing the flux to the weighter weights = weights = weighter.get_weights(flux) # print some info about the weighting object print(weighter.tostring(flux)) # create equal spaced bins in log space bins = plt.geomspace(3e4, 1e6, 50) # get energy of the primary cosmic-ray from `PolyplopiaPrimary` primary_energy = weighter.get_column("PolyplopiaPrimary", "energy") # histogram the primary energy with the weights plt.hist(primary_energy, weights=weights, bins=bins) # make the plot look good plt.loglog()
MCenergy_corsika = np.append(MCenergy_corsika, frame["PolyplopiaPrimary"].energy) for k in weight_keys: CorsikaWeightMap[k].append(frame["CorsikaWeightMap"][k]) PolyplopiaPrimary["zenith"].append( frame["PolyplopiaPrimary"].dir.zenith) PolyplopiaPrimary["type"].append( frame["PolyplopiaPrimary"].type) PolyplopiaPrimary["energy"].append( frame["PolyplopiaPrimary"].energy) fobj = dict(CorsikaWeightMap=CorsikaWeightMap, PolyplopiaPrimary=PolyplopiaPrimary) wobj = simweights.CorsikaWeighter(fobj, nfiles=len(corsika_filelist)) weights_GaisserH3a = wobj.get_weights(simweights.GaisserH3a()) weights_Hoerandel = wobj.get_weights(simweights.Hoerandel()) weightssqr_GaisserH3a = np.power(weights_GaisserH3a, 2) livetime_GaisserH3a = weights_GaisserH3a / weightssqr_GaisserH3a weightssqr_Hoerandel = np.power(weights_Hoerandel, 2) livetime_Hoerandel = weights_GaisserH3a / weightssqr_Hoerandel erange = wobj.surface.get_energy_range(None) czrange = wobj.surface.get_cos_zenith_range(None) print("Number of files : {}".format(len(corsika_filelist))) print("Number of events : {}".format(len(weights_GaisserH3a))) print("Effective Area : {:10.2} m²".format( wobj.effective_area(erange, czrange)[0][0]))
import numpy as np import pylab as plt import tables import simweights # load hdf5 table f = tables.open_file("Level2_IC86.2016_corsika.021682.N100.hdf5", "r") wobj = simweights.CorsikaWeighter(f) flux_model = simweights.GaisserH3a() w = wobj.get_weights(flux_model) # Select just the MuonFilter w *= f.root.FilterMask.cols.MuonFilter_13[:][:, 1] # print the total event rate and livetime print("Event Rate : {:6.2f} Hz".format(w.sum())) print("Total Livetime: {:6.2f} s".format(w.sum() / (w**2).sum())) # make bin edges from the energy range of the sample Ebins = np.geomspace(*wobj.surface.get_energy_range(None), 50) # get the energy column from the weight object mcenergy = wobj.get_weight_column("energy") # make histograms of the rate and the rate squared h1, x2 = np.histogram(mcenergy, bins=Ebins, weights=w) h2, x1 = np.histogram(mcenergy, bins=Ebins, weights=w**2) # plot the rate plt.step(Ebins, np.r_[0, h1])
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') print(file) if args.type == 'nugen': loc_weighter = simweights.NuGenWeighter(file_in, nfiles=1000) flux = power_law elif args.type == 'corsika': loc_weighter = simweights.CorsikaWeighter(file_in, nfiles=1000) flux = simweights.GaisserH4a() else: raise AttributeError('Type {} is not supported'.format(args.type)) local_primary_energy = loc_weighter.get_column('PolyplopiaPrimary', 'energy') local_weights = loc_weighter.get_weights(flux) local_npe = loc_weighter.get_column('EHEPortiaEventSummarySRT', 'bestNPEbtw') local_zenith = loc_weighter.get_column('EHEOpheliaParticleSRT_ImpLF', 'zenith') local_weights_astro = loc_weighter.get_weights(power_law) local_weights_atmo = loc_weighter.get_weights(cr_model) local_weights_cosmo = loc_weighter.get_weights(ahlers)