forked from rodralva/DeepNeutrino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
funciones.py
294 lines (232 loc) · 10 KB
/
funciones.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
### Dependences
import uproot
import numpy as np
import matplotlib.pyplot as plt
from ROOT import TFile, TTree # use for saving funcion only
########functions to decrese the number of pixel of our detector images#######################
def maxpool(im, h, w):
#dependences
#####the imputs#####
# im.shape must be a matrix of(width, height)
# w and h are the output weight and height respectively .
#### preliminaries###
h_step=im.shape[0]//h
w_step=im.shape[1]//w
#print("we have lost", (im.shape[1]%w)*(im.shape[0]%h), "pixels along the way")
reduced_im=np.zeros((h,w)) ##the new reduced matrix is initialized with zeros
########The algorithm#########
for i in range(0,h): #loop over h
for j in range(0,w): #loop over w
pool=im[i*h_step:h_step*(i+1),j*w_step:(j+1)*w_step]
reduced_im[i,j]=np.max(pool)
return reduced_im
def maxpoolmod(im, h, w):
# observacions, modifications to account for issues regarding h_step ~ 1
#####the imputs#####
# im.shape must be a matrix of(width, height)
# w and h are the output weight and height respectively .
#### preliminaries###
h_step=im.shape[0]//h
w_step=im.shape[1]//w
#print("we have lost", (im.shape[1]%w)*(im.shape[0]%h), "pixels along the way")
reduced_im=np.zeros((h,w)) ##the new reduced matrix is initialized with zeros
extra_pixels=im.shape[0]-(im.shape[0]//h)*h
#print(extra_pixels)
loss_h = (im.shape[0]//h *h)/im.shape[0] ##the percented of the image that we will lose
#print('we are loosing without processing', 1-loss_h ,'pixels')
count=0
##########The algorithm##########
for i in range(0,h): #loop over h
for j in range(0,w): #loop over w
r=np.random.uniform()
#print(count,r,':',i,j)
if r>extra_pixels/h/w and count<extra_pixels:
pool=im[i*h_step+count:h_step*(i+1)+count,(j)*w_step:(j+1)*w_step]
reduced_im[i,j]=np.max(pool)
if r<extra_pixels/h/w and count<extra_pixels:
pool=im[i*h_step+count:h_step*(i+1)+count,(j)*w_step:(j+1)*w_step]
reduced_im[i,j]=np.max(pool)
count=count+1
return reduced_im ,count
def reducir(number_of_files=int(10), particle='electron'): #izquierda sin defoult derecha con.
#### imput is the number of files to consider
#### useful variables #####
EventsPerFile=int(500)
NChannel = int(1280)
Nticks = int(1667)
w , h = NChannel , Nticks
newsize = 319
v = np.zeros((newsize,newsize,EventsPerFile*number_of_files))
## the program ###
for i in range(0,number_of_files): ### i stands for the file_number
# load the file
## select particle type
if particle == 'electron':
file = uproot.open("/scratch/deandres/MC/Electrons/reco/Electron_reco_{}.root".format(i))
tree = file["analysistree"]["anatree"]
ADC = tree['RecoWaveform_ADC']
if particle == 'muon':
file = uproot.open("/scratch/deandres/MC/Muons/reco/Muon_reco_{}.root".format(i))
tree = file["analysistree"]["anatree"]
ADC = tree['RecoWaveform_ADC']
for j in range(0,EventsPerFile): ## loop over events
basketcache={} # reset the memory
lazy=ADC.lazyarray(basketcache=basketcache) # now the memory used is in the variable basketcach
todo=lazy[j].reshape((w,h))
v1=todo[0:newsize,:] #### For now, we want only the first view
#v = maxpool(v1,newsize,newsize)
v[:,:,EventsPerFile*i+j] = maxpool(v1,newsize,newsize)
print( 'este es el cache:', basketcache.keys())
print('reducing event number {} out of {}'.format(EventsPerFile*i+j,EventsPerFile*number_of_files))
#### output is the rediced images as a numpy array
return v
def reducirone(file_number=int(1), particle='electron'): #izquierda sin defoult derecha con.
#### imput is the number of files to consider
#### useful variables #####
EventsPerFile=int(250)
NChannel = int(1280)
Nticks = int(1667)
w , h = NChannel , Nticks
newsize = 100
v = np.zeros((newsize,newsize,EventsPerFile))
## the program ###
# load the file
## select particle type
if particle == 'electron':
file = uproot.open("/scratch/deandres/MC/alongZ_2_3GeV/Electrons/raw/Electron_raw_{}.root".format(file_number))
tree = file["analysistree"]["anatree"]
ADC = tree['RawWaveform_ADC']
if particle == 'muon':
file = uproot.open("/scratch/deandres/MC/alongZ_2_3GeV/Muons/raw/Muon_raw_{}.root".format(file_number))
tree = file["analysistree"]["anatree"]
ADC = tree['RawWaveform_ADC']
for j in range(0,EventsPerFile): ## loop over events
basketcache={} # reset the memory
lazy=ADC.lazyarray(basketcache=basketcache) # now the memory used is in the variable basketcach
todo=lazy[j].reshape((w,h))
v2=todo[319:,:] #### For now, we want only the first view
#v = maxpool(v1,newsize,newsize)
v[:,:,j] = maxpool(v2,newsize,newsize)
#print( 'este es el cache:', basketcache.keys())
#print('reducing event number {} out of {}'.format(j,EventsPerFile))
#### output is the rediced images as a numpy array
return v
def reduciroriginal(number_of_files=int(10)): #izquierda sin defoult derecha con.
#### imput is the number of files to consider
###initialize
newsize = 279
v = np.zeros((newsize,newsize,100*number_of_files))
for i in range(0,number_of_files):
print("reducing file{} ".format(i))
file = uproot.open("{}-RecoFull-Parser.root".format(i))
tree = file["analysistree"]["anatree"]
ADC = tree.array( b'RecoWaveform_ADC')
NChannel = tree.array(b'RecoWaveforms_NumberOfChannels')
Nticks = tree.array(b'RecoWaveform_NumberOfTicksInAllChannels')
NTracks = tree.array(b'NumberOfTracks')
#print(w,h)
###here a loop over files
for j in range(0,100):
w , h = int(NChannel[j]) , int(Nticks[j]/NChannel[j])
if ADC[j].shape[0] == w*h:
todo = ADC[j].reshape((w,h))
v1 = todo[0:279,:]
v[:,:,100*i+j] = maxpool(v1,newsize,newsize)
## The future algorithm will include v2 as well.
#### outputs are the reduced images as a numpy array
return v
##########I/O############
def guardar(v,filename):
##########description#########
# this function stores the np vector 'v' into a 'root file'. Due to memory reasons, the other options are not
# as good as root files.
###### note ########
#### it may be worth trying hdf5 files #########
###FUNCTION####
######dependences#####
#from ROOT import TFile, TTree
#import numpy as np
a=v.flatten()
f = TFile(filename, 'recreate')
t = TTree('mytree', 'tree')
t.Branch('im', a, 'myarray[{}]/D'.format(int(a.shape[0])))
t.Fill()
f.Write()
f.Close()
##### output is None, this function will save the reduced images as a root file, it is probably not
##### convinient to use the array.
return None
def abrir(file,dim):
######description########
#this function opens our root file, and gives us a numpy vector of the proper dimensions.
file = uproot.open(file)
tree=file[b'mytree;1']
im=tree.array(b'im')
im=im.reshape(dim)
return im
def dibujar(event,im):
fig = plt.figure(frameon = False)
plt.imshow(im[:,:,event].T,cmap = 'jet',interpolation='none')
fig.set_size_inches(5, 5) ##grey scale
fig.show()
def reducir_guardar(number_of_files=1, particle='electron'):
for i in range(number_of_files):
print('file = {}'.format(i+1))
v = reducirone(i, particle)
if i<10:
guardar(v,"r{}_0{}.root".format(particle,i))
else:
guardar(v,"r{}_{}.root".format(particle,i))
def display(Event = 0,save = False):
w=1280
h=1667
### Open Wave form file
file = uproot.open("/scratch/deandres/MC/alongZ_2_3GeV/Muons/raw/raw.root") ### you may have to change the path as you wish
tree=file["analysistree"]["anatree"]
ADC=tree['RawWaveform_ADC'] # define the object ADC from the tree
basketcache={}
lazy=ADC.lazyarray(basketcache=basketcache)
im=lazy[Event].reshape((w,h))
v1=im[0:320,:]
v2=im[320:,:]
fig = plt.figure()
plt.imshow(v1.T,cmap = 'jet', interpolation='none')
fig.set_size_inches(10, 10)
plt.ylabel("Ticks, drift time ")
plt.xlabel("channel View1")
plt.show()
if save:
plt.save("view1.png")
plt.imshow(v2.T,cmap = 'jet', interpolation='none') ##grey scale
fig.set_size_inches(10, 10)
plt.ylabel("Ticks, drift time ")
plt.xlabel("channel View2")
plt.show()
if save:
plt.save("view2.png")
### open paraneters file.
En=[]
x=[]
y=[]
z=[]
theta=[]
phi=[]
file = uproot.open("/scratch/deandres/MC/alongZ_2_3GeV/Muons/gen/gen.root")
tree = file["analysistree"]["anatree"]
En = np.append(En,tree.array(b'MCTruth_Generator_StartEnergy').flatten())
x = np.append(x,tree.array(b'MCTruth_Generator_StartPoint_X').flatten())
y = np.append(y,tree.array(b'MCTruth_Generator_StartPoint_Y').flatten())
z = np.append(z,tree.array(b'MCTruth_Generator_StartPoint_Z').flatten())
theta = np.append(theta,tree.array(b'MCTruth_Generator_StartDirection_Theta').flatten())
phi = np.append(phi,tree.array(b'MCTruth_Generator_StartDirection_Phi').flatten())
print("The event is generated according to the following parameters")
print("E = ",round(En[Event],2), 'GeV')
print("-"*40)
print('x0 = ',round(x[Event],2))
print('y0 = ',round(y[Event],2))
print('z0 = ',round(z[Event],2))
print("-"*40)
print('theta0 = ',round(theta[Event],2))
print('phi0 = ',round(phi[Event],2))
## return the two images as numpy arrays
return v1, v2