% Created 2022-12-16 Fri 17:04
% Intended LaTeX compiler: pdflatex
\documentclass[serif,professionalfont,colorlinks,aspectratio=149]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{color}
\usepackage{listings}
\usepackage{pdfpages}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{xspace}
\usepackage{libertine}
\usepackage{fontawesome}
\mode<beamer>{\usetheme{Boadilla}}
\setbeamertemplate{navigation symbols}{}
\setbeamerfont{alerted text}{series=\bfseries}
\lstloadlanguages{[ISO]C++}
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.50,0,0.82}
\definecolor{codeorange}{rgb}{0.95,0.3,0.1}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\lstdefinestyle{mystyle}{
commentstyle=\color{codegreen},
keywordstyle=\color{codeorange},
numberstyle=\tiny\color{codegray},
stringstyle=\color{codepurple},
basicstyle=\footnotesize,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2
}
\lstset{style=mystyle}
\usepackage{svg}
\usetheme{default}
\author{Brett Viren}
\date{\today}
\title{WCT Wire Schema}
\title[WCT Wires]{Wire-Cell Toolkit Wires}
\hypersetup{
 pdfauthor={Brett Viren},
 pdftitle={WCT Wire Schema},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 28.1 (Org mode 9.4.6)}, 
 pdflang={English}}
\begin{document}

\maketitle

\begin{frame}[label={sec:orgddb983d}]{Topics}
\begin{itemize}
\item overview
\end{itemize}
\end{frame}

\begin{frame}[label={sec:org5360a76},fragile]{Wire data provenance}
 Caveat, LArSoft parts may not be exact as described.

\begin{enumerate}
\item Some Perl script generates wires in GDML format.

\item Special LArSoft code loads GDML, dumps ``text file''

\item Convert to WCT wire JSON format.

\item Save official files to \texttt{wire-cell-data} repo.

\item WCT C++: \texttt{WireSchema::load("wct-wires.json.bz2")}
\end{enumerate}
\end{frame}

\begin{frame}[label={sec:org5df07c7},fragile]{Problem: precision of wire data is not great}
 \begin{center}
\begin{tabular}{lrrrrr}
\hline
pitch & mean & rms & min & max & p0\\
\hline
pdsp &  &  &  &  & \\
U (mm) & 4.6691 & 0.000006 & 4.6684 & 4.6693 & 4.6691\\
V (mm) & 4.6691 & 0.000005 & 4.6684 & 4.6693 & 4.6691\\
W (mm) & 4.7920 & 0.000000 & 4.7920 & 4.7920 & 4.7920\\
\hline
uboone &  &  &  &  & \\
U (mm) & 3.0028 & 0.000033 & 2.9938 & 3.0045 & 2.9997\\
V (mm) & 3.0028 & 0.000033 & 2.9938 & 3.0045 & 2.9997\\
W (mm) & 3.0000 & 0.000000 & 3.0000 & 3.0000 & 3.0000\\
\hline
\end{tabular}
\end{center}

\footnotesize

\begin{itemize}
\item The ``p0'' is the pitch between first two wires (importance later).

\item Unknown if precision is lost in wring GDML or the wires ``text file''.

\item The trip through JSON should preserve IEEE 64bit/double precision
format.
\end{itemize}

\vfill
\tiny
Numbers from: \texttt{wirecell-util wires-info wct-wires.json.bz2}
\end{frame}

\begin{frame}[label={sec:org3e5e23f},fragile]{MB end-wires pathology}
 \begin{itemize}
\item The last two wires at the end of U and V are very short
\begin{itemize}
\item channels 2398,2399 and 4798,4799
\end{itemize}
\item Happens to be rather co-planar but \(0.13^\circ\) non-parallel.
\begin{itemize}
\item Results in near zero pitch (ray of closest approach).
\end{itemize}
\end{itemize}

\tiny
\begin{verbatim}
TINY PITCH: aid=0 fid=0 pid=0 wid=2399 chn=2399 -- >  wid=2398 chn=2398
	prev: [(6.33552e-13 -1155.1 10358.5) --> (6.34915e-13 -1148.67 10369.6)]
	next: [(6.34287e-13 -1155.1 10364.5) --> (6.34915e-13 -1152.13 10369.6)]
TINY PITCH: aid=0 fid=0 pid=1 wid=2399 chn=4799 -- >  wid=2398 chn=4798
	prev: [(-3 1168.07 10369.6) --> (-3 1174.5 10358.5)]
	next: [(-3 1171.53 10369.6) --> (-3 1174.5 10364.5)]
\end{verbatim}
\end{frame}


\begin{frame}[label={sec:orgc2b2587}]{Solving precision problems}
Not sure what best to do.

\vfill

Some ideas:

\begin{itemize}
\item Regularize by moving the input wires to their ideal locations based
on either user-configured or calculated averages.

\item Implement original wire generator algorithms in either WCT Python to
still generate WCT JSON wires file or generate wires directly in in
WCT C++ component.

\item Address the symptoms of this imprecision (some are listed next).
\end{itemize}

All pose some concerns.
\end{frame}

\begin{frame}[label={sec:orgc0e513f}]{Ray grid reminder to understand the impact on tiling}
\begin{block}{A \textbf{ray} is an ordered pair of ``tail'' to ``head'' endpoints, \((\vec{t},\vec{h})\).}
\begin{itemize}
\item A \textbf{wire segment} is a ray
\begin{itemize}
\item \(\mathcal{W}_i \equiv (\vec{t}_i,\vec{h}_i)\).
\item \(\mathcal{W}_i\) and \(\mathcal{W}_{i+1}\) are separated by some pitch \(\vec{P}\).
\end{itemize}

\item A \textbf{wire region} has a ray representing its lower bound
\begin{itemize}
\item \(\mathcal{R}_i \equiv (\vec{t}_i - \frac{1}{2}\vec{P}, \vec{h}_i - \frac{1}{2}\vec{P})\)
\end{itemize}
\end{itemize}
\end{block}

\begin{block}{A \textbf{ray-grid layer} is defined by a \textbf{ray pair} \((\mathcal{R}^l_0, \mathcal{R}^l_1)\).}
\begin{itemize}
\item \(l \in \{0,1\}\), ``active region'' layer types bound overall vertical and horizontal.

\item \(l \in \{2,3,4\}\), ``wire region'' layer types corresponding to wires in planes.
\end{itemize}
\end{block}
\end{frame}


\begin{frame}[label={sec:orge8cdcef}]{Problem: ``active region'' coincides with wire endpoints bounding box}
A blob \textbf{corner} is a \textbf{crossing} of two \textbf{rays} each from their own layer.

\begin{itemize}
\item The ``active region'' rays coincide with anode face wire-endpoint bounding box.

\item This leads to comparing wire-centers to wire-region bounds.

\item Corners just outside the active region are unintentionally
excluded from their blob.
\end{itemize}

\textbf{Solution}: pad the ``active region'' bounds by some amount.

Most simply, pad each side by 1/2 the maximum pitch over the wire
planes.

More precisely, would take into account wire angles and
pattern offsets.
\end{frame}

\begin{frame}[label={sec:orgf9fb507}]{Extrapolation errors become progressively worse across plane}
\begin{center}
\begin{tabular}{lrrrr}
wire num/tot & 25\% & 50\% & 75\% & 100\%\\
\hline
pdsp &  &  &  & \\
U (mm) & 0.04 & 0.07 & 0.11 & 0.14\\
V (mm) & 0.13 & 0.26 & 0.40 & 0.53\\
W (mm) & 2.2e-11 & 4.4e-11 & 6.6e-11 & 8.8e-11\\
\hline
uboone &  &  &  & \\
U (mm) & 0.004 & 0.013 & 0.009 & 0.010\\
V (mm & 0.004 & 0.013 & 0.009 & 0.010\\
W (mm) & 0 & 0 & 0 & 0\\
\hline
\end{tabular}
\end{center}

\footnotesize

Extrapolate \(\mathcal{W}_0\) center to actual \(\mathcal{W}_i\) center (as from JSON file) and compare.

\begin{itemize}
\item Use pitch vector and wire direction vector from \((\mathcal{R}_0, \mathcal{R}_1)\).

\item \(\vec{p}_f = \vec{c}_0 + n_f \vec{P},\  \delta = \hat{W} \cdot (c_f - p_f),\  p'_f = p_f + \delta\hat{W},\  err = |p'_f - c_f|\)

\item \(\vec{p}\) predicted, \(\vec{c}\) actual wire center, \(f\) wire number, \(\vec{P}\) pitch vector, \(\hat{W}\) wire direction
\end{itemize}
\end{frame}


\begin{frame}[label={sec:orgb2728a2}]{Problem: layer definition maximizes extrapolation error}
\begin{itemize}
\item A ray-grid layer for wires is defined by \(\mathcal{W}_0\) and pitch to \(\mathcal{W}_1\).
\begin{itemize}
\item These are the \textbf{shortest wires} in a plane (ignoring horiz/vert wire planes)
\end{itemize}
\item Uncertainty ``lever arms'' are not in our favor:
\begin{itemize}
\item Errors in endpoint locations propagate worse to center/angle for short wires.
\item Extrapolation errors grow linear as one goes away from \(\mathcal{R}_0\).
\item \(\mathcal{R}_0\) at one extreme side of detector so maximizes the extrapolation.
\end{itemize}
\end{itemize}

\textbf{Solutions}:

\begin{enumerate}
\item Find \textbf{average} wire \textbf{direction} and \textbf{pitch} vectors over entire plane.
\item ``Back-define'' an ``average'' zero wire based on the center (longest) wire:
\begin{itemize}
\item \(\overline{\mathcal{W}}_0 \equiv \mathcal{W}_{(\frac{1}{2}N_{wires})} - \frac{1}{2}N_{wires}\vec{P}\)
\item Effectively, this is the ``regularize'' solution from above.
\end{itemize}
\end{enumerate}
\end{frame}

\begin{frame}[label={sec:orgf6d775e}]{Problem: pathological pattern precision}
\begin{itemize}
\item Detectors like MB have 3 ray-grid rays meeting at the same point.

\item Even with perfect precision, blob-inclusion tests must be based on a
closed-interval (not half-open).

\item Finite precision leads to just-in / just-out inclusion test failures
(both false positive and negative).
\end{itemize}

\textbf{Solution}: ``nudge'' a crossing slightly toward the center of
the blob prior to testing for inclusion.
\end{frame}

\begin{frame}[label={sec:org279fce6}]{WCT wire convention requirements}
\begin{itemize}
\item Wire direction \(\hat{w}\), unit vector pointing from wire tail to
wire head, uniform across plane.

\item Pitch direction \(\hat{p}\), unit vector perpendicular to W, coplanar
with and uniform across all wires in a plane.

\item Pitch magnitude, \(p\), perpendicular separation between any two
neighboring wires in a plane, uniform.

\item Cross product rule: \(\hat{x} \times \hat{w} = \hat{p}\)

\item Sign of \(\hat{p}\): if \(\hat{p} || \hat{z}\) then \(\hat{p} \cdot
  \hat{y} > 0\) else \(\hat{p} \cdot \hat{z} > 0\).
\end{itemize}
\end{frame}

\begin{frame}[label={sec:orga9e3281}]{Problem: wire files are largely invalid}
With good excuse, requirements are ill documented and WCT does not validate!

\begin{itemize}
\item Sign of \(\hat{p}\) is almost universally violated (MB is okay)

\begin{itemize}
\item Many ``violators'' get it piece-wise correct and piece-wise inverted.
\end{itemize}

\item Finite precision causes small uniformity and orthogonality violations.
\end{itemize}
\end{frame}

\begin{frame}[label={sec:org2f41ddd}]{WCT mitigation strategy}
Implement 4 levels of wire corrections, all on by default:

\begin{description}
\item[{load}] just load wires as-is from file
\item[{order}] fix wire endpoint and wire-in-plane order
\item[{direction}] make all wires parallel to the ``mean wire''
\item[{pitch}] make coplanar and all pitches equal to ``mean pitch''.
\end{description}

These are applied during loading of ``wires file''.
\begin{itemize}
\item Assure consistency across all code.
\end{itemize}
\end{frame}


\begin{frame}[label={sec:org8b9bdb6}]{Results for MicroBooNE U-plane}
V-plane similar to U-plane and W-plane requires only ordering fixes.

\begin{block}{MicroBooNE pitches and Y-axis \(\angle\)}
\begin{center}
\begin{tabular}{lrrrr}
 & mean & rms & min & max\\
\hline
P before & 2.9988 & 0.06125 & 2.6e-10 & 3.00751\\
P after & 3 & 0 & 3 & 3\\
\(\angle\) before & 149.959 & 1.98976 & 52.542 & 150.001\\
\(\angle\) after & 150 & 0 & 150 & 150\\
\end{tabular}
\end{center}
\end{block}

\begin{block}{Relative size of corrections for U plane}
\begin{center}
\begin{tabular}{lll}
 & mean & cumulative\\
\hline
pitch & \(6.1\ \mu\mathrm{m}\) & 14.7 mm\\
tail & \(6.1\ \mu\mathrm{m}\) & 14.8 mm\\
head & \(8.5\ \mu\mathrm{m}\) & 20.4 mm\\
\end{tabular}
\end{center}
\end{block}
\end{frame}

\begin{frame}[label={sec:org7852b03}]{MicroBooNE plots}
Next plots are in columns according to which progressive correction.

\Huge

\begin{center}
\begin{center}
\begin{tabular}{|l|l|l|l|}
raw & order & direction & pitch\\
\end{tabular}
\end{center}
\end{center}
\end{frame}

\begin{frame}[label={sec:org75f1861}]{MicroBooNE wire tail ends}
\includegraphics[page=1,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-0.pdf}\includegraphics[page=1,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-1.pdf}\includegraphics[page=1,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-2.pdf}\includegraphics[page=1,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-3.pdf}

Tail X coordinate as function of wire-in-plane.
\begin{itemize}
\item Ray wire endpoints are very slightly not coplanar to start with (\(10^{-12}\) mm).
\item As part of pitch corrections they are brought exactly into their plane.
\end{itemize}
\end{frame}

\begin{frame}[label={sec:orge452a81}]{MicroBooNE wire angles with Y-axis}
\includegraphics[page=10,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-0.pdf}\includegraphics[page=10,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-1.pdf}\includegraphics[page=10,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-2.pdf}\includegraphics[page=10,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-3.pdf}

\begin{itemize}
\item Short wires at either end are particularly bad.
\item \(0.2^\circ\) worse case deviation in raw, corrected to \(\approx 10^{-6}\)
\end{itemize}
\end{frame}

\begin{frame}[label={sec:org5b33346}]{MicroBooNE wire angles with Z-axis}
\includegraphics[page=11,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-0.pdf}\includegraphics[page=11,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-1.pdf}\includegraphics[page=11,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-2.pdf}\includegraphics[page=11,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-3.pdf}

\begin{itemize}
\item Similar as Y-axis angle
\end{itemize}
\end{frame}

\begin{frame}[label={sec:orgf30575e}]{MicroBooNE pitch magnitude}
\includegraphics[page=12,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-0.pdf}\includegraphics[page=12,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-1.pdf}\includegraphics[page=12,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-2.pdf}\includegraphics[page=12,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-3.pdf}

\begin{itemize}
\item Note, very wrong pitch in ``raw'' wires at plane end
\begin{itemize}
\item Due to short, \(0.13^\circ\) non-parallelism
\end{itemize}
\item Corrected uniformity to \(\approx 10^{-10}\)
\begin{itemize}
\item \(\approx 10^{-19}\) within nominal 3 mm.
\end{itemize}
\end{itemize}
\end{frame}

\begin{frame}[label={sec:org45a6d9a}]{MicroBooNE pitch angle with Y-axis}
\includegraphics[page=14,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-0.pdf}\includegraphics[page=14,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-1.pdf}\includegraphics[page=14,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-2.pdf}\includegraphics[page=14,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-3.pdf}

\begin{itemize}
\item The ``very wrong pitch'' case seen here as well
\item Correcting direction alone clears up
\item Full correction gives \(\approx 10^{-12}\) precision and \(\approx 10^{-6}\) accuracy.
\end{itemize}
\end{frame}


\begin{frame}[label={sec:orga0009d4}]{MicroBooNE pitch angle with Z-axis}
\includegraphics[page=15,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-0.pdf}\includegraphics[page=15,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-1.pdf}\includegraphics[page=15,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-2.pdf}\includegraphics[page=15,width=0.24\textwidth]{wire-schema-plots/test_wireschema_plot-microboone-celltree-wires-v2.1-3.pdf}

\begin{itemize}
\item Similar to Y-axis
\end{itemize}
\end{frame}
\end{document}