7 from wirecell
import units
9 @click.group(
"validate")
18 @cli.command(
"diff-hists")
19 @click.option(
"-n",
"--name", multiple=
True,
20 help=
"Name of histogram in each file")
21 @click.option(
"-o",
"--out", default=
None,
23 @click.argument(
"file1")
24 @click.argument(
"file2")
28 Produce an output ROOT file which holds the difference of a histogram from two files. 32 tfile1 = root.open_file(file1)
33 tfile2 = root.open_file(file2)
35 out = root.open_file(out,
"recreate")
40 h1 = root.load_obj(tfile1, one)
41 h2 = root.load_obj(tfile2, one)
52 @cli.command(
"magnify-diff")
53 @click.option(
"-e",
"--epsilon", default=0.0,
54 help=
"The maximum delta for two histograms to be considered different")
55 @click.option(
"-o",
"--out",
58 @click.argument(
"file1")
59 @click.argument(
"file2")
63 Form a new Magnify file holds histograms which are the difference of those from the two inputs. 70 tfile1 = root.open_file(file1)
71 tfile2 = root.open_file(file2)
72 out = root.open_file(out,
"recreate")
74 names1 = set([key.GetName()
for key
in tfile1.GetListOfKeys()
if root.is_hist(key)])
75 names2 = set([key.GetName()
for key
in tfile2.GetListOfKeys()
if root.is_hist(key)])
77 loners = names1 ^ names2
79 print "loner: %s" % loner
81 names = list(names1 & names2)
84 hists1 = [tfile1.Get(name)
for name
in names]
85 hists2 = [tfile2.Get(name)
for name
in names]
88 for name,obj1,obj2
in zip(names, hists1, hists2):
90 mi = obj1.GetMinimum()
91 ma = obj1.GetMaximum()
92 if abs(mi) > epsilon
or abs(ma) > epsilon:
93 msg =
"diff: %s %e %e" % (name, mi, ma)
95 obj1.SetDirectory(out)
103 @cli.command(
"magnify-jsonify")
104 @click.option(
"-o",
"--out",
106 @click.argument(
"filename")
110 Jsonnify summary info about all histograms. 114 tfile = root.open_file(filename)
118 for key
in tfile.GetListOfKeys():
121 if not root.is_hist(key):
123 d = root.hist_to_dict(key.ReadObj());
127 open(out,
"w").
write(json.dumps(dat, indent=4))
129 @cli.command(
"magnify-dump")
130 @click.option(
"-o",
"--out",
132 @click.argument(
"filename")
135 '''Dump magnify histograms into Numpy .npz files. 137 The underlying histogram array is dumped into an array of the same 138 name. For every dimension of histogram an array of bin edges is 139 dumped as <name>_<dim>edges where <dim> is "x", "y" or "z". 143 from root_numpy
import hist2array
144 from time
import time
146 print "Reading ROOT file" 149 tfile = ROOT.TFile.Open(filename)
150 for key
in tfile.GetListOfKeys():
151 cname = key.GetClassName()
152 if cname[:3]
not in [
"TH1",
"TH2",
"TH3" ]:
157 arr,edges = hist2array(th,return_edges=
True)
160 arrs[
"%s_%sedge"%(hname,
"xyz"[dim])] = edge
163 print "Writing NPZ file" 164 numpy.savez_compressed(out, **arrs)
168 @cli.command(
"npz-load")
169 @click.option(
"-o",
"--out",
171 @click.argument(
"filename")
175 from time
import time
177 arrs1 = numpy.load(filename,
'r+')
179 print 'Memmap load: %f'%(t2-t1,)
180 arrs2 = numpy.load(filename,
None)
182 print 'Full load: %f'%(t3-t2)
190 print 'Subtract: %f' % (t4-t3)
191 numpy.savez_compressed(out, **outs)
193 print 'Done: %f' % (t5-t4)
195 @cli.command(
"magnify-plot-reduce")
196 @click.option(
"-n",
"--name", default=
"orig",
197 help=
"The histogram type name")
198 @click.option(
"-o",
"--out",
200 @click.argument(
"filename")
204 Reduce a magnify 2D histogram along the time dim. 208 from root_numpy
import hist2array
215 (
"rms",
lambda a: numpy.sqrt(numpy.mean(numpy.square(a)))),
216 (
"absmin",
lambda a: numpy.min(numpy.abs(a))),
217 (
"absmax",
lambda a: numpy.max(numpy.abs(a))),
218 (
"abssum",
lambda a: numpy.sum(numpy.abs(a))),
221 tfile = ROOT.TFile.Open(filename)
222 hists = [tfile.Get(
"h%s_%s"%(letter, name))
for letter
in "uvw"]
223 aes = [hist2array(hist, return_edges=
True)
for hist
in hists]
225 edges = [ae[1]
for ae
in aes]
226 extents = [(e[0][0],e[0][-1])
for e
in edges]
229 arrs_by_name = dict()
230 for mname, meth
in methods:
231 arrs_by_name[mname] = [numpy.apply_along_axis(meth, 1, ae[0])
for ae
in aes]
233 fig = plotter(name, arrs_by_name, extents)
238 @cli.command(
"magnify-plot")
239 @click.option(
"-n",
"--name", default=
"orig",
240 help=
"The histogram type name")
241 @click.option(
"-t",
"--trebin", type=int, default=1,
242 help=
"Set amount of rebinning in time domain (must be integral factor)")
243 @click.option(
"-c",
"--crebin", type=int, default=1,
244 help=
"Set amount of rebinning in channel comain (must be integral factor)")
245 @click.option(
"--baseline/--no-baseline", default=
False,
246 help=
"Calculate, subtract and display baselines.")
247 @click.option(
"--threshold", type=float, default=0.0,
248 help=
"Apply a threshold.")
249 @click.option(
"--saturate", type=float, default=0.0,
250 help=
"Saturate values.")
251 @click.option(
"-o",
"--out",
253 @click.argument(
"filename")
255 def magnify_plot(ctx, name, trebin, crebin, baseline, threshold, saturate, out, filename):
257 Plot magnify histograms. 260 from root_numpy
import hist2array
as h2a
267 tfile = ROOT.TFile.Open(filename)
268 hists = [tfile.Get(
"h%s_%s"%(letter, name))
for letter
in "uvw"]
270 raise ValueError(
'Could not get hists for "%s" from: %s' % (name, filename))
274 aes = [h2a(h,return_edges=
True)
for h
in hists]
277 for h,(a,e)
in zip(hists,aes):
278 xa,ya = [getattr(h,
"Get"+l+
"axis")()
for l
in "XY"]
279 nx,ny = [getattr(h,
"GetNbins"+l)()
for l
in "XY"]
283 ext = ((ce[0],ce[-1]), (te[0],te[-1]))
285 print "%s: X:%d in [%.0f %.0f] Y:%d in [%.0f %.0f]" % \
287 nx, xa.GetBinLowEdge(1), xa.GetBinUpEdge(nx),
288 ny, ya.GetBinLowEdge(1), ya.GetBinUpEdge(ny),)
289 print "\t shape=%s ext=%s" % (a.shape, ext)
291 arrs.append(numpy.fliplr(a))
295 norm = 1.0/(crebin*trebin)
305 digi = numpy.int32(wav)
308 values, counts = numpy.unique(digi, return_counts=
True)
309 ind = numpy.argmax(counts)
314 newarr = numpy.asarray(newarr)
315 bla = numpy.asarray(bl)
316 bla =
rebin(bla, bla.size/crebin)
317 baselines.append(bla)
318 newarrs.append(newarr)
321 arrs = [
bin_ndarray(arr, (arr.shape[0]/crebin, arr.shape[1]/trebin),
"mean")
for arr
in arrs]
323 tit =
'Type "%s" (rebin: ch=x%d tick=x%d), file:%s' % (name, crebin, trebin, os.path.basename(filename))
327 tit +=
" [threshold at %d]" % threshold
328 arrs = [numpy.ma.masked_where(numpy.abs(arr)<=threshold, arr)
for arr
in arrs]
335 arr[arr > saturate] = saturate
336 arr[arr <-saturate] = -saturate
341 arrs = [a.T
for a
in arrs]
342 extents = [(e[0][0], e[0][1], e[1][0], e[1][1])
for e
in extents]
344 for a,e
in zip(arrs,extents):
356 if '__main__' == __name__:
int open(const char *, int)
Opens a file descriptor.
size_t write(int, const char *, size_t)
Writes count bytes from buf to the filedescriptor fd.
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
def magnify_dump(ctx, out, filename)
def magnify_plot_reduce(ctx, name, out, filename)
def diff_hists(ctx, name, out, file1, file2)
def magnify_plot(ctx, name, trebin, crebin, baseline, threshold, saturate, out, filename)
static QInternalList< QTextCodec > * all
def three_horiz(arrs, extents, name, baselines)
def magnify_jsonify(ctx, out, filename)
def magnify_diff(ctx, epsilon, out, file1, file2)
auto zip(Iterables &&...iterables)
Range-for loop helper iterating across many collections at the same time.
def bin_ndarray(ndarray, new_shape, operation='sum')
def npz_load(ctx, out, filename)