summaryrefslogtreecommitdiff
path: root/chroma
diff options
context:
space:
mode:
Diffstat (limited to 'chroma')
-rw-r--r--chroma/gpu/pdf.py4
-rw-r--r--chroma/io/root.py4
-rw-r--r--chroma/likelihood.py4
-rw-r--r--chroma/tools.py4
4 files changed, 10 insertions, 6 deletions
diff --git a/chroma/gpu/pdf.py b/chroma/gpu/pdf.py
index 1b6340f..5d27f63 100644
--- a/chroma/gpu/pdf.py
+++ b/chroma/gpu/pdf.py
@@ -2,7 +2,7 @@ import numpy as np
from pycuda import gpuarray as ga
import pycuda.driver as cuda
from chroma.gpu.tools import get_cu_module, cuda_options, GPUFuncs, chunk_iterator
-from chroma.tools import profile_if_possible
+from chroma.tools import profile_if_possible, count_nonzero
class GPUKernelPDF(object):
def __init__(self):
@@ -259,7 +259,7 @@ class GPUPDF(object):
time_only: bool
If True, only the time observable will be used in the PDF.
"""
- self.event_nhit = np.count_nonzero(event_hit)
+ self.event_nhit = count_nonzero(event_hit)
# Define a mapping from an array of len(event_hit) to an array of length event_nhit
self.map_hit_offset_to_channel_id = np.where(event_hit)[0].astype(np.uint32)
diff --git a/chroma/io/root.py b/chroma/io/root.py
index 20c20e2..935299b 100644
--- a/chroma/io/root.py
+++ b/chroma/io/root.py
@@ -2,7 +2,7 @@ import os, os.path
import shutil
import numpy as np
import chroma.event as event
-
+from chroma.tools import count_nonzero
from chroma.rootimport import ROOT
# Check if we have already imported the ROOT class due to a user's
@@ -216,7 +216,7 @@ class RootWriter(object):
self.ev.vertices[i].t0 = vertex.t0
if pyev.channels is not None:
- nhit = np.count_nonzero(pyev.channels.hit)
+ nhit = count_nonzero(pyev.channels.hit)
if nhit > 0:
ROOT.fill_channels(self.ev, nhit, np.arange(len(pyev.channels.t))[pyev.channels.hit].astype(np.int32), pyev.channels.t, pyev.channels.q, pyev.channels.flags, len(pyev.channels.hit))
else:
diff --git a/chroma/likelihood.py b/chroma/likelihood.py
index a29dc6c..50d9593 100644
--- a/chroma/likelihood.py
+++ b/chroma/likelihood.py
@@ -2,7 +2,7 @@ import numpy as np
from math import sqrt
from uncertainties import ufloat, unumpy
from itertools import islice, izip, repeat
-from chroma.tools import profile_if_possible
+from chroma.tools import profile_if_possible, count_nonzero
class Likelihood(object):
"Class to evaluate likelihoods for detector events."
@@ -189,7 +189,7 @@ if __name__ == '__main__':
event = sim.simulate(islice(constant_particle_gun('e-',(0,0,0),(1,0,0),100.0), 1)).next()
- print 'nhit = %i' % np.count_nonzero(event.channels.hit)
+ print 'nhit = %i' % count_nonzero(event.channels.hit)
likelihood = Likelihood(sim, event)
diff --git a/chroma/tools.py b/chroma/tools.py
index 831a356..2a1167e 100644
--- a/chroma/tools.py
+++ b/chroma/tools.py
@@ -4,6 +4,10 @@ import datetime
import sys
import math
+def count_nonzero(array):
+ '''Return the number of nonzero elements in this array'''
+ return (array != 0).sum()
+
def filled_array(value, shape, dtype):
'''Create a numpy array of given `shape` and `dtype` filled with the scalar `value`.'''
a = np.empty(shape=shape, dtype=dtype)