All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
helpers.py
Go to the documentation of this file.
1 import numpy as np
2 import awkward as ak
3 
4 # Broadcast a numpy array (var) over a sequence (nbroadcast)
5 def broadcast(var, nbroadcast):
6  return np.repeat(var, nbroadcast)
7 
8 # Broadcast an awkward array (var) over a sequence (nbroadcast)
9 def broadcast_ak(var, nbroadcast):
10  flat_var = var.flatten()
11  flat_nbroadcast = np.repeat(nbroadcast, var.counts)
12 
13  flat_var_repeat = np.repeat(flat_var, flat_nbroadcast)
14 
15  # TODO: VECTORIZE THIS STEP!
16  reindex = np.hstack([np.add.outer(np.arange(0, var.counts[i]) * nbroadcast[i], np.arange(0, nbroadcast[i])).flatten("F") for i in range(len(nbroadcast))])
17 
18  reindex += np.repeat(np.hstack([[0], np.cumsum(nbroadcast * var.counts)[:-1]]), nbroadcast * var.counts)
19  flat_var_repeat_ordered = flat_var_repeat[reindex]
20  return group(flat_var_repeat_ordered, np.repeat(var.counts, nbroadcast))
21 
22 # Group a numpy array (var) into an awkward array by groups (ngroup)
23 def group(var, ngroup):
24  return ak.JaggedArray.fromcounts(ngroup, var)
25 
26 # normalize
27 def NeutrinoPOT(data):
28  _, ind = np.unique(data["hdr.subrun"] + data["hdr.run"]*100, return_index=True)
29  return np.sum(data["hdr.pot"][ind])
30 
31 def NGenEvt(data):
32  _, ind = np.unique(data["hdr.subrun"] + data["hdr.run"]*100, return_index=True)
33  return np.sum(data["hdr.ngenevt"][ind])
34 
35 def NEvt(data):
36  return len(data["hdr.evt"])
37 
38 
39 POT_PER_SPILL = 5e12
40 def CosmicPOT(cosmic, nu):
41  neutrino_per_spill = (NGenEvt(nu) * POT_PER_SPILL) / NeutrinoPOT(nu)
42  _, ind = np.unique(nu["hdr.subrun"] + nu["hdr.run"]*100, return_index=True)
43  n_cosmic_evt = NGenEvt(cosmic)
44  return n_cosmic_evt * POT_PER_SPILL / (1. - neutrino_per_spill)
def NeutrinoPOT
Definition: helpers.py:27
def NGenEvt
Definition: helpers.py:31
def broadcast_ak
Definition: helpers.py:9
def NEvt
Definition: helpers.py:35
def broadcast
Definition: helpers.py:5
def group
Definition: helpers.py:23
def CosmicPOT
Definition: helpers.py:40