-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweight_MC.py
144 lines (132 loc) · 6.39 KB
/
weight_MC.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import numpy as np
import argparse
import tables
from path_gen import PathGen
default_xs_location = "/data/user/jlazar/solar_WIMP/data/xs_splines/"
default_flux_location = "/data/user/jlazar/solar_WIMP/data/fluxes/"
dsdxdy_nu_CC = default_xs_location+"dsdxdy_nu_CC_iso.fits"
dsdxdy_nubar_CC = default_xs_location+"dsdxdy_nubar_CC_iso.fits"
dsdxdy_nu_NC = default_xs_location+"dsdxdy_nu_NC_iso.fits"
dsdxdy_nubar_NC = default_xs_location+"dsdxdy_nubar_NC_iso.fits"
nuSQFluxKaon = default_flux_location+"kaon_atmospheric.hdf5"
nuSQFluxPion = default_flux_location+"kaon_atmospheric.hdf5"
nuSQFluxConv = default_flux_location+"atmospheric_0_0.000000_0.000000_0.000000_0.000000_0.000000_0.000000.hdf5"
def initialize_argparse():
parser = argparse.ArgumentParser(description='Run LeptonWeighter python example.')
parser.add_argument('--mcfile')
parser.add_argument('--dsdxdy_nu_CC', default=default_xs_location+"dsdxdy_nu_CC_iso.fits")
parser.add_argument('--dsdxdy_nubar_CC', default=default_xs_location+"dsdxdy_nubar_CC_iso.fits")
parser.add_argument('--dsdxdy_nu_NC', default=default_xs_location+"dsdxdy_nu_NC_iso.fits")
parser.add_argument('--dsdxdy_nubar_NC', default=default_xs_location+"dsdxdy_nubar_NC_iso.fits")
parser.add_argument('--nuSQFluxKaon', default=default_flux_location+"kaon_atmospheric.hdf5")
parser.add_argument('--nuSQFluxPion', default=default_flux_location+"kaon_atmospheric.hdf5")
parser.add_argument("--nuSQFluxConv", default=default_flux_location+"atmospheric_0_0.000000_0.000000_0.000000_0.000000_0.000000_0.000000.hdf5")
return parser.parse_args()
# Load h5 file in chunks for memory limited situations
def chonk_h5file(mcgf, chonk_size=1e5):
h5file = tables.File(mcgf.mcpath)
n_items = h5file.root.FinalStateX.shape[0]
chonks = int(np.ceil(n_items/chonk_size))
chonk_slices = []
for chonk_number in range(chonks):
a = int(np.floor(float(n_items) / float(chonks)))
b = int(np.ceil(float(n_items) / float(chonks)))
x = n_items - a*chonks
if chonk_number < x:
n = b
n0 = n*chonk_number
else:
n = a
n0 = b*x + a*(chonk_number - x)
n1 = min(n0 + n, n_items)
if n0 >= n_items:
continue
chonk_slices.append(slice(n0,n1))
# h5file.close()
return chonk_slices
# Set up LeptonWeighter
def initialize_weighter(mcgf):
import LeptonWeighter as LW
simulation_generators = LW.MakeGeneratorsFromLICFile(mcgf.get_licfile_path())
#kaon_nusquids_flux = LW.nuSQUIDSAtmFlux(args.nuSQFluxKaon)
#pion_nusquids_flux = LW.nuSQUIDSAtmFlux(args.nuSQFluxPion)
conv_nusquids_flux = LW.nuSQUIDSAtmFlux(nuSQFluxConv)
xs = LW.CrossSectionFromSpline(dsdxdy_nu_CC,dsdxdy_nubar_CC,
dsdxdy_nu_NC,dsdxdy_nubar_NC)
weighter = LW.Weighter([conv_nusquids_flux],xs,simulation_generators)
return weighter
def weight_mc(mcgf,save=False):
weighter = initialize_weighter(mcgf)
mc = tables.File(mcgf.mcpath)
weights = np.zeros(mc.root.FinalStateX.shape[0])
chunks = chonk_h5file(mcgf)
events = zip(
mc.root.FinalStateX[:]['value'],
mc.root.FinalStateY[:]['value'],
mc.root.FinalType0[:]['value'],
mc.root.FinalType1[:]['value'],
mc.root.NuAzimuth[:]['value'],
mc.root.NuZenith[:]['value'],
mc.root.NuEnergy[:]['value'],
mc.root.PrimaryType[:]['value'],
mc.root.TotalColumnDepth[:]['value'],
)
for i, event in enumerate(events):
LWevent = LW.Event()
LWevent.interaction_x = event[0]
LWevent.interaction_y = event[1]
LWevent.final_state_particle_0 = LW.ParticleType(event[2])
LWevent.final_state_particle_1 = LW.ParticleType(event[3])
LWevent.azimuth = event[4]
LWevent.zenith = event[5]
LWevent.energy = event[6]
LWevent.primary_type = LW.ParticleType(event[7])
LWevent.total_column_depth = event[8]
LWevent.x = 0.
LWevent.y = 0.
LWevent.z = 0.
weighter.get_oneweight(LWevent)
weights[i] = weighter.get_oneweight(LWevent)
#for chunk in chunks:
# chunk_weights = np.zeros(int(chunk.stop-chunk.start))
# events = zip(
# mc.root.FinalStateX[chunk]['value'],
# mc.root.FinalStateY[chunk]['value'],
# mc.root.FinalType0[chunk]['value'],
# mc.root.FinalType1[chunk]['value'],
# mc.root.NuAzimuth[chunk]['value'],
# mc.root.NuZenith[chunk]['value'],
# mc.root.NuEnergy[chunk]['value'],
# mc.root.PrimaryType[chunk]['value'],
# mc.root.TotalColumnDepth[chunk]['value'],
# )
#
# for i, event in enumerate(events):
# LWevent = LW.Event()
# LWevent.interaction_x = event[0]
# LWevent.interaction_y = event[1]
# LWevent.final_state_particle_0 = LW.ParticleType(event[2])
# LWevent.final_state_particle_1 = LW.ParticleType(event[3])
# LWevent.azimuth = event[4]
# LWevent.zenith = event[5]
# LWevent.energy = event[6]
# LWevent.primary_type = LW.ParticleType(event[7])
# LWevent.total_column_depth = event[8]
# LWevent.x = 0.
# LWevent.y = 0.
# LWevent.z = 0.
#
# chunk_weights[i] = weighter.get_oneweight(LWevent)
# print(chunk_weights)
#
# weights[chunk.start:chunk.stop] = chunk_weights
if save:
np.save("/data/user/jlazar/solar_WIMP/data/mc_oneweight/%s" % mcgf.get_mcname(), weights)
# mc.close()
return weights
if __name__=="__main__":
args = initialize_argparse()
savefilename = args.mcfile.split("/")[-1][:-3]+ ".npy"
mcgf = PathGen(args.mcfile)
weights = weight_mc(mcgf)
np.save("/data/user/jlazar/solar_WIMP/data/mc_oneweight/%s" % mcgf.get_mcname(), weights)