Functions
wirecell.util.main Namespace Reference

Functions

def cli (ctx)
 
def convert_oneside_wires (ctx, input_file, output_file)
 
def convert_multitpc_wires (ctx, input_file, output_file)
 
def convert_uboon_wire_regions (ctx, wire_json_file, csvfile, region_json_file)
 
def plot_wire_regions (ctx, wire_json_file, region_json_file, pdf_file)
 
def wires_info (ctx, json_file)
 
def wires_volumes (ctx, anode, response, cathode, json_file)
 
def plot_wires (ctx, json_file, pdf_file)
 
def plot_select_channels (ctx, labels, json_file, pdf_file, channels)
 
def gen_plot_wires (ctx, output_file)
 
def make_wires (ctx, detector, output_file)
 
def make_map (ctx, detector, output_file)
 
def gravio (ctx, dotfile)
 
def make_wires_onesided (ctx, output_file)
 
def wire_channel_map (ctx, input_file)
 
def main ()
 

Function Documentation

def wirecell.util.main.cli (   ctx)
Wire Cell Toolkit Utility Commands

Definition at line 8 of file main.py.

8 def cli(ctx):
9  '''
10  Wire Cell Toolkit Utility Commands
11  '''
12 
13 @cli.command("convert-oneside-wires")
14 @click.argument("input-file")
15 @click.argument("output-file")
16 @click.pass_context
def cli(ctx)
Definition: main.py:8
def wirecell.util.main.convert_multitpc_wires (   ctx,
  input_file,
  output_file 
)
Convert a "multitpc" wire description file into one suitable for
WCT.

Here "TPC" refers to on anode face.  That is, one APA is made up
of two TPCs.  An example file is protodune-wires-larsoft-v1.txt
from wire-cell-data.  It has columns like:

    # chan tpc plane wire sx sy sz ex ey ez

And, further the order of rows of identical channel number express
progressively higher segment count.

Definition at line 39 of file main.py.

39 def convert_multitpc_wires(ctx, input_file, output_file):
40  '''
41  Convert a "multitpc" wire description file into one suitable for
42  WCT.
43 
44  Here "TPC" refers to on anode face. That is, one APA is made up
45  of two TPCs. An example file is protodune-wires-larsoft-v1.txt
46  from wire-cell-data. It has columns like:
47 
48  # chan tpc plane wire sx sy sz ex ey ez
49 
50  And, further the order of rows of identical channel number express
51  progressively higher segment count.
52  '''
53  from wirecell.util.wires import multitpc, persist
54  store = multitpc.load(input_file)
55  persist.dump(output_file, store)
56 
57 
58 
59 @cli.command("convert-uboone-wire-regions")
60 @click.argument("wire-json-file")
61 @click.argument("csvfile")
62 @click.argument("region-json-file")
63 @click.pass_context
def convert_multitpc_wires(ctx, input_file, output_file)
Definition: main.py:39
def wirecell.util.main.convert_oneside_wires (   ctx,
  input_file,
  output_file 
)
Convert a a "onesided" wires description file into one suitable for WCT.

An example file is:

https://github.com/BNLIF/wire-cell-celltree/blob/master/geometry/ChannelWireGeometry_v2.txt

It has columns like:
# channel plane wire sx sy sz ex ey ez

The output file is JSON and if it has .gz or .bz2 it will be compressed.

Definition at line 17 of file main.py.

17 def convert_oneside_wires(ctx, input_file, output_file):
18  '''
19  Convert a a "onesided" wires description file into one suitable for WCT.
20 
21  An example file is:
22 
23  https://github.com/BNLIF/wire-cell-celltree/blob/master/geometry/ChannelWireGeometry_v2.txt
24 
25  It has columns like:
26  # channel plane wire sx sy sz ex ey ez
27 
28  The output file is JSON and if it has .gz or .bz2 it will be compressed.
29  '''
30  from wirecell.util.wires import onesided, persist
31  store = onesided.load(input_file)
32  persist.dump(output_file, store)
33 
34 
35 @cli.command("convert-multitpc-wires")
36 @click.argument("input-file")
37 @click.argument("output-file")
38 @click.pass_context
def convert_oneside_wires(ctx, input_file, output_file)
Definition: main.py:17
def wirecell.util.main.convert_uboon_wire_regions (   ctx,
  wire_json_file,
  csvfile,
  region_json_file 
)
Convert CSV file to WCT format for wire regions.  Example is one
as saved from MicroBooNE_ShortedWireList.xlsx.  Use ,-separated
and remove quoting.

Definition at line 64 of file main.py.

64 def convert_uboon_wire_regions(ctx, wire_json_file, csvfile, region_json_file):
65  '''
66  Convert CSV file to WCT format for wire regions. Example is one
67  as saved from MicroBooNE_ShortedWireList.xlsx. Use ,-separated
68  and remove quoting.
69  '''
70  import wirecell.util.wires.persist as wpersist
71  import wirecell.util.wires.regions as reg
72  store = wpersist.load(wire_json_file)
73  ubs = reg.uboone_shorted(store, csvfile)
74  wpersist.dump(region_json_file, ubs)
75 
76 @cli.command("plot-wire-regions")
77 @click.argument("wire-json-file")
78 @click.argument("region-json-file")
79 @click.argument("pdf-file")
80 @click.pass_context
def convert_uboon_wire_regions(ctx, wire_json_file, csvfile, region_json_file)
Definition: main.py:64
def wirecell.util.main.gen_plot_wires (   ctx,
  output_file 
)
Generate wires and plot them.

Definition at line 222 of file main.py.

222 def gen_plot_wires(ctx, output_file):
223  '''
224  Generate wires and plot them.
225  '''
226  import wirecell.util.wires.plot as wplot
227  import wirecell.util.wires.generator as wgen
228  s = wgen.onesided_wrapped()
229  fig,ax = wplot.oneplane(s, 0)
230  #fig,ax = wplot.allplanes(s)
231  #fig,ax = wplot.allwires(s)
232  fig.savefig(output_file)
233 
234 @cli.command("make-wires")
235 @click.option('-d','--detector',
236 # type=click.Choice("microboone protodune dune apa".split()),
237  type=click.Choice(['apa']),
238  help="Set the target detector")
239 # fixme: give interface to tweak individual parameters
240 # fixme: give way to set a graph transformation function
241 # fixme: give way to set a template for file generation
242 @click.argument("output-file")
243 @click.pass_context
def gen_plot_wires(ctx, output_file)
Definition: main.py:222
def wirecell.util.main.gravio (   ctx,
  dotfile 
)
Make a dot file using gravio of the connectivity.

Definition at line 351 of file main.py.

351 def gravio(ctx, dotfile):
352  '''
353  Make a dot file using gravio of the connectivity.
354  '''
355  try:
356  from gravio import gen, dotify
357  except ImportError:
358  click.echo('You need to install the "gravio" package')
359  click.echo('See https://github.com/brettviren/gravio')
360  sys.exit(1)
361  from wirecell.util.wires import apa, graph
362  desc = apa.Description();
363  G,P = apa.graph(desc)
364 
365  # ['wire', 'wib', 'conductor', 'point', 'chip', 'face', 'plane', 'board', 'detector', 'apa', 'channel']
366  node_colors = dict(wib='orange', chip='red', face='blue', plane='purple', board='green', channel='pink')
367  def skip_node(n):
368  skip_types = ['point','wire','conductor']
369  nt = G.nodes[n]['type']
370  return nt in skip_types
371 
372  gr = gen.Graph("dune", "graph")
373  gr.node(shape='point')
374 
375  for name, params in G.nodes.items():
376  if skip_node(name):
377  continue
378  nt = G.nodes[name]['type']
379  gr.node(name, shape='point', color=node_colors.get(nt, 'black'))
380 
381  #link_types = ['slot', 'submodule', 'pt', 'trace', 'wip', 'spot', 'side',
382  # 'plane', 'place', 'address', 'segment', 'cable', 'channel']
383  link_colors = dict(trace='pink', spot='red', side='blue', plane='purple', address='yellow', cable='brown', chanel='orange')
384  seen_edges = set()
385  for (n1,n2), params in G.edges.items():
386  if skip_node(n1) or skip_node(n2):
387  continue
388  if (n1,n2) in seen_edges or (n2,n1) in seen_edges:
389  continue
390  seen_edges.add((n1,n2))
391  link = params['link']
392  gr.edge(n1,n2, color=link_colors.get(link, 'black'))
393 
394 
395  d = dotify.Dotify(gr)
396  dottext = str(d)
397  open(dotfile,'w').write(dottext)
398 
399 @cli.command("make-wires-onesided")
400 @click.argument("output-file")
401 @click.pass_context
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.
def gravio(ctx, dotfile)
Definition: main.py:351
static QCString str
def wirecell.util.main.main ( void  )

Definition at line 440 of file main.py.

440 def main():
441  cli(obj=dict())
442 
def cli(ctx)
Definition: main.py:8
def wirecell.util.main.make_map (   ctx,
  detector,
  output_file 
)
Generate a WCT channel map file giving numpy arrays.

Definition at line 266 of file main.py.

266 def make_map(ctx, detector, output_file):
267  '''
268  Generate a WCT channel map file giving numpy arrays.
269  '''
270  schema = output_file[output_file.rfind(".")+1:]
271  click.echo('writing schema: "%s"' % schema)
272 
273  if detector == "apa":
274  from wirecell.util.wires import apa
275  if schema == "npz":
276  click.echo('generating Numpy file "%s"' % output_file)
277  numpy.savez(output_file, **dict(
278  chip_channel_spot = apa.chip_channel_spot,
279  chip_channel_layer = apa.chip_channel_layer,
280  connector_slot_board = numpy.asarray(range(10),dtype=numpy.int32).reshape(2,5)+1,
281  face_board_femb = numpy.asarray(range(20),dtype=numpy.int32).reshape(2,10)+1))
282  return
283  if schema == "tex": # fixme: this should be moved out of here
284  click.echo('generating LaTeX fragment file "%s"' % output_file)
285  with open(output_file,"w") as fp:
286  color = dict(u="red", v="blue", w="black")
287  lines = list()
288  mat = numpy.asarray([r"\textcolor{%s}{%s%02d}" % (color[p], p, w) \
289  for p,w in apa.chip_channel_layer_spot_matrix.reshape(8*16,2)])\
290  .reshape(8,16)
291  for chn, ch in enumerate(mat.T):
292  cells = ["ch%02d" % chn]
293  for chip in ch:
294  cells.append(chip)
295  lines.append(" & " .join(cells))
296  end = r"\\" + "\n"
297  body = end.join(lines)
298  top = "&".join(["ASIC:"] + [str(n+1) for n in range(8)]) + r"\\"
299  form = "r|rrrrrrrr"
300  tabular = [r"\begin{center}", r"\begin{tabular}{%s}"%form, r"\hline", top,
301  r"\hline", body+r"\\", r"\hline", r"\end{tabular}",r"\end{center}",""]
302  fp.write("\n".join(tabular))
303 
304  layers = dict(u=[""]*40, v=[""]*40, w=[""]*48)
305  for chipn, chip in enumerate(apa.chip_channel_layer_spot_matrix):
306  for chn, (plane,wire) in enumerate(chip):
307  layers[plane][wire-1] = (chipn,chn)
308 
309  lines = list()
310  for letter, layer in sorted(layers.items()):
311  nchans = len(layer)
312  nhalf = nchans // 2
313  form = "|" + "C{3.5mm}|"*nhalf
314  lines += ["",
315  #r"\tiny",
316  r"\begin{center}",
317  r"\begin{tabular}{%s}"%form,
318  ]
319 
320  lines += [#r"\hline",
321  r"\multicolumn{%d}{c}{%s layer, first half: conductor / chip / chan} \\" % (nhalf, letter.upper()),
322  r"\hline"]
323  wires = "&".join(["%2s"%ww for ww in range(1,nhalf+1)]) + r"\\";
324  chips = "&".join(["%2s"%(cc[0]+1,) for cc in layer[:nhalf]]) + r"\\";
325  chans = "&".join(["%2s"%cc[1] for cc in layer[:nhalf]]) + r"\\";
326  lines += [wires, r"\hline", chips, chans];
327 
328  lines += [r"\hline",
329  r"\multicolumn{%d}{c}{%s layer, second half: conductor / chip / chan} \\" % (nhalf, letter.upper()),
330  r"\hline"]
331  wires = "&".join(["%2s"%ww for ww in range(nhalf+1,nchans+1)]) + r"\\";
332  chips = "&".join(["%2s"%(cc[0]+1,) for cc in layer[nhalf:]]) + r"\\";
333  chans = "&".join(["%2s"%cc[1] for cc in layer[nhalf:]]) + r"\\";
334  lines += [wires, r"\hline", chips, chans];
335 
336  lines += [r"\hline", r"\end{tabular}"]
337  lines += [r"\end{center}"]
338  fp.write("\n".join(lines))
339  return
340 
341  return
342 
343 
344  click.echo('Unknown detector type: "%s"' % detector)
345  sys.exit(1)
346 
347 
348 @cli.command("gravio")
349 @click.argument("dotfile")
350 @click.pass_context
int open(const char *, int)
Opens a file descriptor.
def make_map(ctx, detector, output_file)
Definition: main.py:266
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
Definition: enumerate.h:69
static QCString str
def wirecell.util.main.make_wires (   ctx,
  detector,
  output_file 
)
Generate a WCT "wires" file giving geometry and connectivity of
conductor wire segments and channel identifiers.

Definition at line 244 of file main.py.

244 def make_wires(ctx, detector, output_file):
245  '''
246  Generate a WCT "wires" file giving geometry and connectivity of
247  conductor wire segments and channel identifiers.
248  '''
249  if detector == "apa":
250  from wirecell.util.wires import apa, graph, persist
251  desc = apa.Description();
252  G,P = apa.graph(desc)
253  store = graph.to_schema(G, P, apa.channel_ident)
254  persist.dump(output_file, store)
255  return
256  click.echo('Unknown detector type: "%s"' % detector)
257  sys.exit(1)
258 
259 @cli.command("make-map")
260 @click.option('-d','--detector',
261 # type=click.Choice("microboone protodune dune apa".split()),
262  type=click.Choice(['apa']),
263  help="Set the target detector")
264 @click.argument("output-file")
265 @click.pass_context
def make_wires(ctx, detector, output_file)
Definition: main.py:244
def wirecell.util.main.make_wires_onesided (   ctx,
  output_file 
)
Generate a WCT wires file. 

Definition at line 402 of file main.py.

402 def make_wires_onesided(ctx, output_file):
403  '''
404  Generate a WCT wires file.
405  '''
406  import wirecell.util.wires.generator as wgen
407  import wirecell.util.wires.persist as wpersist
408  s = wgen.onesided_wrapped() # fixme, expose different algs to CLI
409  wpersist.dump(output_file, s)
410 
411 @cli.command("wire-channel-map")
412 @click.argument("input-file")
413 @click.pass_context
def make_wires_onesided(ctx, output_file)
Definition: main.py:402
def wirecell.util.main.plot_select_channels (   ctx,
  labels,
  json_file,
  pdf_file,
  channels 
)
Plot wires for select channels from a WCT JSON(.bz2) wire file

Definition at line 209 of file main.py.

209 def plot_select_channels(ctx, labels, json_file, pdf_file, channels):
210  '''
211  Plot wires for select channels from a WCT JSON(.bz2) wire file
212  '''
213  import wirecell.util.wires.persist as wpersist
214  import wirecell.util.wires.plot as wplot
215  wires = wpersist.load(json_file)
216  wplot.select_channels(wires, pdf_file, channels, labels=labels)
217 
218 
219 @cli.command("gen-plot-wires")
220 @click.argument("output-file")
221 @click.pass_context
def plot_select_channels(ctx, labels, json_file, pdf_file, channels)
Definition: main.py:209
def wirecell.util.main.plot_wire_regions (   ctx,
  wire_json_file,
  region_json_file,
  pdf_file 
)

Definition at line 81 of file main.py.

81 def plot_wire_regions(ctx, wire_json_file, region_json_file, pdf_file):
82  import wirecell.util.wires.persist as wpersist
83  import wirecell.util.wires.plot as wplot
84  from matplotlib.backends.backend_pdf import PdfPages
85  import matplotlib.pyplot as plt
86  from matplotlib.patches import Polygon
87  from matplotlib.collections import PatchCollection
88 
89  store = wpersist.load(wire_json_file)
90  regions = wpersist.load(region_json_file)
91 
92 
93  def pt2xy(pt):
94  'Point id to xy tuple'
95  ptobj = store.points[pt]
96  return (ptobj.z, ptobj.y)
97  def wo2pg(wo1,wo2):
98  'wire objects to polygon'
99  return numpy.asarray([pt2xy(wo1.tail),pt2xy(wo1.head),
100  pt2xy(wo2.head),pt2xy(wo2.tail)])
101 
102  colors=['red','green','blue']
103 
104  def get_polygons(shorted, triples):
105  ret = list()
106  for trip in triples:
107 
108  for one in trip:
109  pl,wip1,wip2 = one["plane"],one["wire1"],one["wire2"]
110  if pl != shorted:
111  continue
112 
113  # fixme: this line assumes only 1 face
114  plobj = store.planes[pl]
115  wobj1 = store.wires[plobj.wires[wip1]]
116  wobj2 = store.wires[plobj.wires[wip2]]
117 
118  assert wobj1.channel == one['ch1']
119  assert wobj2.channel == one['ch2']
120 
121  verts = wo2pg(wobj1,wobj2)
122  #print (verts)
123  pg = Polygon(verts, closed=True, facecolor=colors[pl],
124  alpha=0.3, fill=True, linewidth=.1, edgecolor='black')
125  ret.append(pg)
126  return ret
127 
128  pgs = [get_polygons(int(s), t) for s,t in regions.items()]
129  pgs2 = [get_polygons(int(s), t) for s,t in regions.items()]
130  pgs.append(pgs2[0] + pgs2[1])
131 
132  zlimits = [ (0,4100), (6900,7500), (0, 7500) ]
133 
134  with PdfPages(pdf_file) as pdf:
135 
136  for pgl,zlim in zip(pgs,zlimits):
137  fig, ax = plt.subplots(nrows=1, ncols=1)
138  for pg in pgl:
139  ax.add_patch(pg)
140 
141  ax.set_xlim(*zlim)
142  ax.set_ylim(-1500,1500)
143  ax.set_title('Dead wires')
144  ax.set_xlabel('Z [mm]')
145  ax.set_ylabel('Y [mm]')
146  pdf.savefig(fig)
147  plt.close()
148  return
149 
150 
151 @cli.command("wires-info")
152 @click.argument("json-file")
153 @click.pass_context
auto zip(Iterables &&...iterables)
Range-for loop helper iterating across many collections at the same time.
Definition: zip.h:295
def plot_wire_regions(ctx, wire_json_file, region_json_file, pdf_file)
Definition: main.py:81
def wirecell.util.main.plot_wires (   ctx,
  json_file,
  pdf_file 
)
Plot wires from a WCT JSON(.bz2) wire file

Definition at line 192 of file main.py.

192 def plot_wires(ctx, json_file, pdf_file):
193  '''
194  Plot wires from a WCT JSON(.bz2) wire file
195  '''
196  import wirecell.util.wires.persist as wpersist
197  import wirecell.util.wires.plot as wplot
198  wires = wpersist.load(json_file)
199  wplot.allplanes(wires, pdf_file)
200 
201 
202 @cli.command("plot-select-channels")
203 @click.option('--labels/--no-labels', default=True,
204  help="Use labels or not")
205 @click.argument("json-file")
206 @click.argument("pdf-file")
207 @click.argument("channels", nargs=-1, type=int)
208 @click.pass_context
def plot_wires(ctx, json_file, pdf_file)
Definition: main.py:192
def wirecell.util.main.wire_channel_map (   ctx,
  input_file 
)
Debug command, generate a WCT channel map wires file.

Definition at line 414 of file main.py.

414 def wire_channel_map(ctx, input_file):
415  '''
416  Debug command, generate a WCT channel map wires file.
417  '''
418  from collections import defaultdict
419  import wirecell.util.wires.persist as wpersist
420  s = wpersist.load(input_file)
421 
422  channel_map = defaultdict(list)
423 
424  for anode in s.anodes:
425  for iface in anode.faces:
426  face = s.faces[iface]
427  for iplane in face.planes:
428  plane = s.planes[iplane]
429  for iwire in plane.wires:
430  wire = s.wires[iwire]
431  # fixme: why isn't wire.ident changing?
432  channel_map[(plane.ident,wire.channel)].append(iwire)
433 
434  for c,wires in sorted(channel_map.items()):
435  if c[1] in range(4210, 4235):
436  wires.sort()
437  click.echo("%s\t%s" %(c,wires))
438 
439 
def wire_channel_map(ctx, input_file)
Definition: main.py:414
def wirecell.util.main.wires_info (   ctx,
  json_file 
)
Print information about a wires file (.json or .json.bz2)

Definition at line 154 of file main.py.

154 def wires_info(ctx, json_file):
155  '''
156  Print information about a wires file (.json or .json.bz2)
157  '''
158  import wirecell.util.wires.persist as wpersist
159  import wirecell.util.wires.info as winfo
160  wires = wpersist.load(json_file)
161  dat = winfo.summary(wires)
162  print ('\n'.join(dat))
163 
164 
165 @cli.command("wires-volumes")
166 @click.option('-a', '--anode', default=1.0,
167  help='Distance from collection plane to "anode" (cutoff) plane (cm)')
168 @click.option('-r', '--response', default=10.0,
169  help='Distance from collection plane to "respones" plane, should probably match Garfield (cm)')
170 @click.option('-c', '--cathode', default=1.0,
171  help='Distance from colleciton plane to "cathode" plane (cm)')
172 @click.argument("json-file")
173 @click.pass_context
def wires_info(ctx, json_file)
Definition: main.py:154
def wirecell.util.main.wires_volumes (   ctx,
  anode,
  response,
  cathode,
  json_file 
)
Print a parms.det.volumes JSON fragment for the given wires file.

You very likely want to carefully supply ALL command line options.

Definition at line 174 of file main.py.

174 def wires_volumes(ctx, anode, response, cathode, json_file):
175  '''
176  Print a parms.det.volumes JSON fragment for the given wires file.
177 
178  You very likely want to carefully supply ALL command line options.
179  '''
180  import wirecell.util.wires.persist as wpersist
181  import wirecell.util.wires.info as winfo
182  wires = wpersist.load(json_file)
183  jv = winfo.jsonnet_volumes(wires, anode*units.cm, response*units.cm, cathode*units.cm)
184  click.echo(str(jv))
185 
186 
187 
188 @cli.command("plot-wires")
189 @click.argument("json-file")
190 @click.argument("pdf-file")
191 @click.pass_context
def wires_volumes(ctx, anode, response, cathode, json_file)
Definition: main.py:174
static QCString str