def weight_frame(frame, nfiles=1, isNuGen=False): lowe_flux_service = NewNuFlux.makeFlux("IPhonda2014_spl_solmin") highe_flux_service = NewNuFlux.makeFlux("honda2006") if isNuGen: gen_ratio = 0.5 else: gen_ratio = 0.7 mc_weights = frame['I3MCWeightDict'] true_neutrino = dataclasses.get_most_energetic_neutrino(frame['I3MCTree']) true_energy = mc_weights['PrimaryNeutrinoEnergy'] true_zenith = true_neutrino.dir.zenith if true_neutrino.energy < 10000: flux_service = lowe_flux_service else: flux_service = highe_flux_service nue, numu = I3Particle.ParticleType.NuE, I3Particle.ParticleType.NuMu nu_nubar_genratio = gen_ratio if true_neutrino.pdg_encoding < 0: nue, numu = I3Particle.ParticleType.NuEBar, I3Particle.ParticleType.NuMuBar nu_nubar_genratio = 1 - nu_nubar_genratio if np.abs(true_neutrino.pdg_encoding) == 12: flux = flux_service.getFlux(nue, true_energy, numpy.cos(true_zenith)) if np.abs(true_neutrino.pdg_encoding) == 14: flux = flux_service.getFlux(numu, true_energy, numpy.cos(true_zenith)) one_weight = mc_weights['OneWeight'] n_events = mc_weights['NEvents'] norm = (1.0 / (n_events * nfiles * nu_nubar_genratio)) w = norm * one_weight * flux return w
def Configure(self): self._dataset_type = self.GetParameter("DatasetType") self._n_files = self.GetParameter("DatasetNFiles") self._n_events_per_run = self.GetParameter("DatasetNEventsPerRun") self._ngen = self._n_events_per_run * self._n_files self._output_key = self.GetParameter("OutputKey") self._dataset_type = self._dataset_type.lower() if self._dataset_type not in ['muongun', 'nugen', 'genie']: raise ValueError('Unkown dataset_type: {!r}'.format(dataset_type)) # get Honda2006 self.honda = NewNuFlux.makeFlux("honda2006") self.honda.knee_reweighting_model = 'gaisserH3a_elbert' self.honda.relative_kaon_contribution = .91 # get self-veto self.af = AtmosphericSelfVeto.AnalyticPassingFraction self.honda_veto_hese = self.af('conventional', veto_threshold=1.25e3) self.honda_veto_mese = self.af('conventional', veto_threshold=1e2) # get the sarcevic model for prompt neutrinos self.enberg = NewNuFlux.makeFlux("sarcevic_std") self.enberg_veto_hese = self.af('charm', veto_threshold=1.25e3) self.enberg_veto_mese = self.af('charm', veto_threshold=1e2) self.conv_flux_multiplier = 1.07 self.prompt_flux_multiplier = .2
def AtmosphericNuFlux(modelname='honda2006',knee=False,fluxconst=1.): """ Create an Atmospheric neutrino flux Args: modelname (str): a atmospheric model knee (bool): ad a knee model fluxconst (float): scale flux by this const Returns (func): the requested flux. Takes energy, type and maybe cos(zenith) as parameters """ nuflux = NewNuFlux.makeFlux(modelname) if knee: nuflux.knee_reweighting_model = knee def flux(mc_p_energy,mc_p_type,mc_p_zenith): mc_p_type = np.int32(mc_p_type) mc_p_type = conv.ConvertPrimaryFromPDG(mc_p_type) try: return fluxconst*nuflux.getFlux(mc_p_type,mc_p_energy,mc_p_zenith) except RuntimeError: if conv.IsPDGEncoded(mc_p_type,neutrino=True): mc_p_type = conv.ConvertPrimaryFromPDG(mc_p_type) else: mc_p_type = conv.ConvertPrimaryToPDG(mc_p_type) # FIXME: is this still the case? # the fluxes given by newnuflux are only for anti/neutrinos # so to calculate the total flux, there is a number of 2 necessary return 2*fluxconst*nuflux.getFlux(mc_p_type,mc_p_energy,mc_p_zenith) return flux
def __init__(self, context): super(NuGenWeightCalculator, self).__init__(context) try: from icecube import NewNuFlux flux = NewNuFlux.makeFlux('honda2006') except ImportError: flux = None self.AddParameter( "Flux", "Neutrino flux model: an object with a member function getFlux(particleType, energy, cos_theta) that returns fluxes per particle type in 1/(GeV cm^2 sr s) (e.g. an instance of NewNuFlux.FluxFunction)", flux)
def atm_flux(I3MCWeightDict): flux = NewNuFlux.makeFlux('honda2006') flux.knee_reweighting_model = "gaisserH4a_elbert" ptype = I3MCWeightDict["PrimaryNeutrinoType"] energy = I3MCWeightDict["PrimaryNeutrinoEnergy"] cos_theta = np.cos(I3MCWeightDict["PrimaryNeutrinoZenith"]) type_weight = 0.5 nevts = I3MCWeightDict["NEvents"] oneweight = I3MCWeightDict["OneWeight"] return flux.getFlux(ptype, energy, cos_theta) * oneweight / (type_weight * nevts)
def atmo_weight(frame): if reco_q.is_data(frame): return True flux = NewNuFlux.makeFlux('honda2006') flux.knee_reweighting_model = "gaisserH4a_elbert" conv = frame['correct_ow'].value * flux.getFlux( frame['I3MCWeightDict']['PrimaryNeutrinoType'], frame['MCPrimary1'].energy, np.cos(frame['MCPrimary1'].dir.zenith)) print('conv {}'.format(conv)) frame.Put("conv", dataclasses.I3Double(conv)) return
def get_fluxes_and_names(neutrinoflux_models='all', nnflux_models='all', measured_models='all'): if neutrinoflux_models == 'all': neutrinoflux_models = NEUTRINOFLUX_MODELS elif neutrinoflux_models is None: neutrinoflux_models = [] if nnflux_models == 'all': nnflux_models = NNFLUX_MODELS elif nnflux_models is None: nnflux_models = [] if measured_models == 'all': measured_models = MEASURED_MODELS elif measured_models is None: measured_models = [] fluxes = [] flux_names = [] for model in neutrinoflux_models: model_type = model[1] if model_type == 'conv': fluxes.append( neutrinoflux.ConventionalNeutrinoFluxWithKnee(model[0])) if model_type == 'prompt': fluxes.append(neutrinoflux.PromptNeutrinoFluxWithKnee(model[0])) if model_type == 'astro': fluxes.append(neutrinoflux.AstroNeutrinoFlux(model[0])) flux_name = '{}_{}_nflux'.format(model[0], model_type) flux_name = flux_name.replace('-', '_') flux_names.append(flux_name) for model in nnflux_models: model_type = model[2] flux = nn.makeFlux(model[0]) for knee_rew in model[1]: flux.knee_reweighting_model = knee_rew flux_name = '{}_{}_{}_NNFlux'.format(model[0], knee_rew, model_type) flux_name.replace('_none', '') fluxes.append(flux) flux_names.append(flux_name) for model in measured_models: fluxes.append(makeFlux(model)) flux_names.append(model) return fluxes, flux_names
ra79_sim, sindec79_sim , ra79_data, sindec79_data, ra79_true, sindec79_true, energy79_true, muex79_sim, muex79_data, sigma79_sim, sigma79_data, OneWeight_IC79, dpsi_IC79 = cache.load (filename_pickle+"sirin_IC79/coords.pickle") nch79_sim, nch79_data = cache.load (filename_pickle+"sirin_IC79/NCh.pickle") ra59_sim, sindec59_sim , ra59_data, sindec59_data, ra59_true, sindec59_true, energy59_true, mue59_sim, mue59_data, sigma59_sim, sigma59_data, OneWeight_IC59, dpsi_IC59 = cache.load (filename_pickle+"sirin_IC59/coords.pickle") #nch59_sim, nch59_data = cache.load (filename_pickle+"IC59/NCh.pickle") ra40_sim, sindec40_sim , ra40_data, sindec40_data, ra40_true, sindec40_true, energy40_true, mue40_sim, mue40_data, sigma40_sim, sigma40_data, OneWeight_IC40, dpsi_IC40 = cache.load (filename_pickle+"sirin_IC40/coords.pickle") #nch40_sim, nch40_data = cache.load (filename_pickle+"IC40/NCh.pickle") #End Loading Zone.# #Weighting honda = NewNuFlux.makeFlux('honda2006') honda.knee_reweighting_model = 'gaisserH3a_elbert' flux = honda.getFlux #For Skylab I only need OneWeight (I think?) I'll keep the function for posterity, but let's just keep oneweight: ### Pull Correction ### #A polynomial exists to make sure dpsi and sigma have the same median. This is different for each year. #The polynomial was done with incorrect assumptions from IC40 - IC86I- to fix this, we multiply by the following factor: #Now these are all handled courtesy of their respective prepdata folder #pullCorrect = 1./1.1774 # ### for IC40: ##https://wiki.icecube.wisc.edu/index.php/IC-40_PS_Cut_Variables #def RescaledSigma_IC40_SplineMPE( Sigma, Energy):
def read_from_i3_file(file_name, type_flags, q, generator, flux_name='honda2006', sampling_factor=1.0, geo=None): is_data = bool(type_flags & 0x1) is_mono = bool((type_flags >> 1) & 0x1) flux = NewNuFlux.makeFlux(flux_name).getFlux load('millipede') try: class MCMuonInfoModule(icetray.I3ConditionalModule): def __init__(self, context): super(MCMuonInfoModule, self).__init__(context) self.n = None def Configure(self): pass def DAQ(self, frame): pass def Physics(self, frame): pass def process_frame(self, frame): if self.n == None: self.n = 0 else: self.n += 1 process_DAQ(frame, q, self.n, type_flags, flux, generator) if is_data: MCMuonInfoModule.Physics = process_frame else: MCMuonInfoModule.DAQ = process_frame def cut_on_length(frame): global passed_length_cut points = VHESelfVeto.IntersectionsWithInstrumentedVolume(frame['I3Geometry'], frame['MPEFit_TT']) if len(points) < 2: return False return abs(points[0] - points[1]) >= 600 def add_time_window(frame): window = dataclasses.I3TimeWindow(*(lambda x: (min(x)-1000, max(x)+1000))([p.time for l in frame[pulse_series].apply(frame).values() for p in l])) frame[pulse_series + 'TimeRange'] = window return True pulse_series = 'TTPulses' tray = I3Tray() if geo is not None: FilenameList = [geo] else: FilenameList = [] FilenameList.append(file_name) tray.Add("I3Reader", "my_reader", FilenameList=FilenameList) tray.Add(lambda frame: frame.Has('I3MCTree')) tray.Add(lambda frame: frame.Has('MMCTrackList')) if is_data: tray.Add(lambda frame: frame.Has('MPEFit_TT')) tray.Add(lambda frame: frame.Has(pulse_series)) tray.Add(lambda frame: (frame.Stop != icetray.I3Frame.Physics) or (random.random() <= sampling_factor)) if is_data: tray.Add(cut_on_length) tray.Add(add_time_window) table_base = os.path.expandvars('$I3_DATA/photon-tables/splines/emu_%s.fits') muon_service = photonics_service.I3PhotoSplineService(table_base % 'abs', table_base % 'prob', 0) table_base = os.path.expandvars('$I3_DATA/photon-tables/splines/ems_spice1_z20_a10.%s.fits') cascade_service = photonics_service.I3PhotoSplineService(table_base % 'abs', table_base % 'prob', 0) tray.Add('MuMillipede', 'millipede_highenergy', MuonPhotonicsService=muon_service, CascadePhotonicsService=cascade_service, PhotonsPerBin=15, MuonRegularization=0, ShowerRegularization=0, MuonSpacing=0, ShowerSpacing=10, SeedTrack='MPEFit_TT', Output='MillipedeHighEnergy', Pulses=pulse_series) tray.Add(MCMuonInfoModule) print 'Executing tray' tray.Execute() tray.Finish() print 'Finished tray' print 'Done with i3 file' except Exception as e: print e
def createPickleFile(data_type): # Setting the directory, listing the files indir = os.path.join(i3_basedir, data_type) filenames_all = [ os.path.join(indir, f) for f in os.listdir(indir) if f.endswith('.i3.bz2') ] # Go over the files once to see if they can be used # This step can be very slow. Skip it if you have checked this once! filename_list = checkFiles(filenames_all) total_files = len(filename_list) * 1.0 file_counter = 0 aux = 1 # Counter of events in the table tc = 0 if not 'IC86' in data_type and not 'corsika' in data_type: from icecube import NewNuFlux import vacuumOscillations as vacOsc probCalculator = vacOsc.OscProb() flux_model = 'honda2006' atmFlux_jason06 = NewNuFlux.makeFlux(flux_model) atmFlux_jason12 = NewNuFlux.makeFlux('honda2012_spl_solmin') spectral_index = 0.05 # Information stored for both data and MC arr_size = 300000 #Max array size defined. Make sure it's enough for you. array_reco_energy = np.zeros(arr_size) array_reco_cascade = np.zeros(arr_size) array_reco_track = np.zeros(arr_size) array_reco_zenith = np.zeros(arr_size) array_reco_hlcz = np.zeros(arr_size) if 'IC86' in data_type: array_eventid = np.zeros(arr_size) if not 'IC86' in data_type and not 'corsika' in data_type: array_mc_energy = np.zeros(arr_size) array_mc_zenith = np.zeros(arr_size) array_mc_weight = np.zeros(arr_size) array_mc_weight_ej06 = np.zeros(arr_size) array_mc_weight_muj06 = np.zeros(arr_size) array_mc_weight_ej12 = np.zeros(arr_size) array_mc_weight_muj12 = np.zeros(arr_size) array_mc_oneweight = np.zeros(arr_size) array_mc_interaction = np.zeros(arr_size) array_mc_particle_type = np.zeros(arr_size) array_mc_maxial_res_weight = np.zeros([arr_size, 2]) array_mc_maxial_qe_weight = np.zeros([arr_size, 2]) interactions_directory = \ '/afs/ifh.de/user/y/yanezjua/scratch/analysis-results/systematic-checks/crossSections/interaction_dict/' if '12' in data_type: interactions_lib = pickle.load( open(interactions_directory + 'NuEIntDict3.pckl')) elif '14' in data_type: interactions_lib = pickle.load( open(interactions_directory + 'NuMuIntDict3.pckl')) elif '16' in data_type: interactions_lib = pickle.load( open(interactions_directory + 'NuTauIntDict3.pckl')) tables_outfile = os.path.join(tables_outdir, data_type.split('/')[-1] + '.pckl') print 'Saving to ', tables_outfile if os.path.isfile(tables_outfile): print 'Tables already exist. Press a key to redo.\n', tables_outfile raw_input() for one_file in filename_list: print one_file file_counter += 1 if file_counter % aux == 0: print 'File : +' "%i" % file_counter infile = dataio.I3File(one_file) for frame in infile: if frame.Stop != frame.Physics: continue # Observables and additional info muon = frame[muon_track] hadrons = frame[hadronic_cascade] hlcz = frame[fhlc_name] # Weighting if 'IC86' in data_type: weight_osc = 1. weight_nosc = 1 elif 'corsika' in data_type: print 'Weighting in CORSIKA is way too complicated now. Did not implement' exit() else: true_neutrino = frame[true_nu_name] # Retrieve interaction and particle interaction_type = frame['I3MCWeightDict']['InteractionType'] particle_type = true_neutrino.pdg_encoding nu_energy = true_neutrino.energy nu_zenith = true_neutrino.dir.zenith nu_theta = pi - nu_zenith nu_azimuth = true_neutrino.dir.azimuth # Calculate the atm. weight (HONDA) norm = (10**9 *frame['I3MCWeightDict']['OneWeight']*\ icetray.I3Units.GeV*icetray.I3Units.cm2 * icetray.I3Units.sr)/ \ (frame['I3MCWeightDict']['NEvents']* total_files/2.) if particle_type > 0: my_nuetype = dataclasses.I3Particle.ParticleType.NuE my_numutype = dataclasses.I3Particle.ParticleType.NuMu else: my_nuetype = dataclasses.I3Particle.ParticleType.NuEBar my_numutype = dataclasses.I3Particle.ParticleType.NuMuBar nue_jason06 = nu_energy**(spectral_index)* norm *\ atmFlux_jason06.getFlux(my_nuetype, nu_energy, cos(nu_theta))/\ (icetray.I3Units.GeV*icetray.I3Units.cm2*icetray.I3Units.s*icetray.I3Units.sr) nue_jason12 = nu_energy**(spectral_index)* norm *\ atmFlux_jason12.getFlux(my_nuetype, nu_energy, cos(nu_theta))/\ (icetray.I3Units.GeV*icetray.I3Units.cm2*icetray.I3Units.s*icetray.I3Units.sr) if nue_jason12 == 0.: nue_jason12 = nue_jason06 numu_jason06 = nu_energy**(spectral_index)* norm *\ atmFlux_jason06.getFlux(my_numutype, nu_energy, cos(nu_theta))/\ (icetray.I3Units.GeV*icetray.I3Units.cm2*icetray.I3Units.s*icetray.I3Units.sr) numu_jason12 = nu_energy**(spectral_index)* norm *\ atmFlux_jason12.getFlux(my_numutype, nu_energy, cos(nu_theta))/\ (icetray.I3Units.GeV*icetray.I3Units.cm2*icetray.I3Units.s*icetray.I3Units.sr) # numu_steven = nu_energy**(spectral_index)*norm*\ # steven_flux.MuFlux(nu_energy, cos(nu_theta))/\ # (nu_energy*\ # icetray.I3Units.GeV*icetray.I3Units.m2*icetray.I3Units.s*icetray.I3Units.sr) # nue_steven = nu_energy**(spectral_index)*norm*\ # steven_flux.EFlux(nu_energy, cos(nu_theta))/\ # (nu_energy*\ # icetray.I3Units.GeV*icetray.I3Units.m2*icetray.I3Units.s*icetray.I3Units.sr) if numu_jason12 == 0.: numu_jason12 = numu_jason06 # Also add Ma variations only for CC interactions (NC interactions didn't play a role) if interaction_type == 1 and 'nu' in data_type: # Cannot do this for nugen axial_mass_qe, axial_mass_res = \ getMaVariations(idict = interactions_lib,\ energy = nu_energy,\ pdg = int(particle_type),\ xsecE = frame['I3MCWeightDict']['Crosssection']/nu_energy) else: axial_mass_res = np.zeros(2) axial_mass_qe = np.zeros(2) array_reco_energy[tc] = muon.energy + hadrons.energy array_reco_cascade[tc] = hadrons.energy array_reco_track[tc] = muon.energy array_reco_zenith[tc] = muon.dir.zenith array_reco_hlcz[tc] = hlcz.pos.z if 'IC86' in data_type: array_eventid[tc] = frame['I3EventHeader'].event_id if not 'IC86' in data_type and not 'corsika' in data_type: array_mc_energy[tc] = nu_energy array_mc_zenith[tc] = nu_zenith array_mc_oneweight[tc] = norm array_mc_weight_ej06[tc] = nue_jason06 array_mc_weight_muj06[tc] = numu_jason06 array_mc_weight_ej12[tc] = nue_jason12 array_mc_weight_muj12[tc] = numu_jason12 array_mc_interaction[tc] = interaction_type array_mc_particle_type[tc] = particle_type array_mc_maxial_res_weight[tc, :] = axial_mass_res array_mc_maxial_qe_weight[tc, :] = axial_mass_qe tc += 1 # All files are done if not 'IC86' in data_type and not 'corsika' in data_type: cc_bool = array_mc_interaction == 1 nc_bool = array_mc_interaction == 2 array_dict = {'CC': {}, 'NC': {}} # Charged current array_dict['CC']['reco_energy'] = array_reco_energy[cc_bool] array_dict['CC']['reco_cascade'] = array_reco_cascade[cc_bool] array_dict['CC']['reco_track'] = array_reco_track[cc_bool] array_dict['CC']['reco_zenith'] = array_reco_zenith[cc_bool] array_dict['CC']['oneweight'] = array_mc_oneweight[cc_bool] array_dict['CC']['weight_ej06'] = array_mc_weight_ej06[cc_bool] array_dict['CC']['weight_ej12'] = array_mc_weight_ej12[cc_bool] array_dict['CC']['weight_muj06'] = array_mc_weight_muj06[cc_bool] array_dict['CC']['weight_muj12'] = array_mc_weight_muj12[cc_bool] array_dict['CC']['energy'] = array_mc_energy[cc_bool] array_dict['CC']['zenith'] = array_mc_zenith[cc_bool] array_dict['CC']['ptype'] = array_mc_particle_type[cc_bool] array_dict['CC']['ma_qe'] = array_mc_maxial_qe_weight[cc_bool] array_dict['CC']['ma_res'] = array_mc_maxial_res_weight[cc_bool] array_dict['CC']['hlcz'] = array_reco_hlcz[cc_bool] # Neutral current array_dict['NC']['reco_energy'] = array_reco_energy[nc_bool] array_dict['NC']['reco_cascade'] = array_reco_cascade[nc_bool] array_dict['NC']['reco_track'] = array_reco_track[nc_bool] array_dict['NC']['reco_zenith'] = array_reco_zenith[nc_bool] array_dict['NC']['oneweight'] = array_mc_oneweight[nc_bool] array_dict['NC']['weight_ej06'] = array_mc_weight_ej06[nc_bool] array_dict['NC']['weight_ej12'] = array_mc_weight_ej12[nc_bool] array_dict['NC']['weight_muj06'] = array_mc_weight_muj06[nc_bool] array_dict['NC']['weight_muj12'] = array_mc_weight_muj12[nc_bool] array_dict['NC']['energy'] = array_mc_energy[nc_bool] array_dict['NC']['zenith'] = array_mc_zenith[nc_bool] array_dict['NC']['ptype'] = array_mc_particle_type[nc_bool] array_dict['NC']['hlcz'] = array_reco_hlcz[nc_bool] else: array_dict = { 'reco_energy': array_reco_energy[:tc], 'reco_cascade': array_reco_cascade[:tc], 'reco_track': array_reco_track[:tc], 'reco_zenith': array_reco_zenith[:tc], 'hlcz': array_reco_hlcz[:tc] } if 'IC86' in data_type: array_dict['eventid'] = array_eventid[:tc] pickle.dump(array_dict, open(tables_outfile, 'w')) print 'TOTAL EVENTS KEPT (unweighted): ', tc
ptype = phy_frame['MCPrimary'].pdg_encoding weight = flux(energy, ptype) / generator(energy, ptype) print('Corsika Weight {}'.format(weight)) phy_frame.Put("corsika_weight", dataclasses.I3Double(weight)) return def add_weighted_primary(phy_frame): if reco_q.is_data(phy_frame): return True if not 'MCPrimary' in phy_frame.keys(): get_weighted_primary(phy_frame, MCPrimary='MCPrimary') return flux_conv = NewNuFlux.makeFlux('honda2006') flux_conv.knee_reweighting_model = "gaisserH4a_elbert" def atmo_weight(frame): if reco_q.is_data(frame): return True if 'I3MCWeightDict' not in phy_frame: return conv = frame['I3MCWeightDict']['OneWeight'] * flux_conv.getFlux( frame['I3MCWeightDict']['PrimaryNeutrinoType'], frame['MCPrimary1'].energy, np.cos(frame['MCPrimary1'].dir.zenith)) frame.Put("conv", dataclasses.I3Double(conv)) return
from icecube.icetray import I3Units import icecube.weighting.weighting as weighting from icecube.weighting.weighting import from_simprod load('millipede') import numpy as np import pickle import histogram _pulse_series = 'TTPulses' _reco_track = 'MPEFit_TT' # Initialize the flux and generator for the sample # Parameters below are for /data/ana/IC79/numu_forward_folding/sim/Alfa/Lnu/IC86/eff0.9900/ _flux_name = 'honda2006' _flux = NewNuFlux.makeFlux(_flux_name).getFlux _generator = weighting.NeutrinoGenerator(1e5, 200, 1e9, 2, 'NuMu', InjectionMode = 'Surface', ZenithMin = 80*I3Units.deg, ZenithMax = 180*I3Units.deg, AzimuthMin = 0*I3Units.deg, AzimuthMax = 360*I3Units.deg, CylinderRadius = 800*I3Units.meter, CylinderHeight = 1000.*I3Units.m ) # Initialize photon table services table_base = os.path.expandvars('$I3_DATA/photon-tables/splines/emu_%s.fits') _muon_service = photonics_service.I3PhotoSplineService(table_base % 'abs', table_base % 'prob', 0) table_base = os.path.expandvars('$I3_DATA/photon-tables/splines/ems_spice1_z20_a10.%s.fits') _cascade_service = photonics_service.I3PhotoSplineService(table_base % 'abs', table_base % 'prob', 0)