\documentclass[xcolor=dvipsnames]{beamer}

\input{preamble.tex}

\title[\texttt{Sim 15min}]{Design to Simulate 15 Minutes of Running}

\begin{document}

\begin{frame}
  \titlepage
\end{frame}

\begin{frame}
  \frametitle{Overview}

  \tableofcontents
\end{frame}

\section{Design Goals and Overview}

\begin{frame}
  \frametitle{Design Goals and Overview} 

  Independently, Steve Kettel and Jun Cao recognized the usefulness of
  generating a sample of ``real'' or ``cocktail'' MC data with all
  event types properly represented.

  \vspace{5mm}
  This design shows how we might produce this MC data.  It also has
  the additional features:
  \begin{itemize}
  \item Although 15 minutes is the target, there is no limit on the
    duration of time to be simulated.
  \item It modularizes the processing so any part can be tested
    independently.
  \item It allows for the types of events for consideration to be
    configurable.
  \item It supports different modes of operation including saving
    intermediate results to file for subsequent read back.
  \end{itemize}

\end{frame}

\section{Stages of Processing}

\begin{frame}[fragile]
  \frametitle{Processing stages, simple view}
  \begin{minipage}[b]{0.35\textwidth}
    \begin{enumerate}
    \item Event Generator produces Initial Particle Kinematics
    \item Detector Sim. produces Hits
    \item Elec. Sim. makes Digits
    \item Trig. Sim. forms Crate Data Blocks
    \item Final readout
    \end{enumerate}
    \vspace{2cm}
  \end{minipage}\includegraphics[height=0.9\textheight]{processing}

\end{frame}

\section{Nonlinear Processing}
\begin{frame}
  \frametitle{Nonlinearities in processing}
  Real world: events are asynchronous, overlapping and spread out in
  time.  Processing is not always so linear.
  \begin{itemize}
  \item The U/Th radiation generator produces a decay chain which
    spreads initial kinematics through time.
  \item Inverse beta decay can produce prompt and delayed hits in the
    detector sim.
  \item Optical photons are spread in time due to extended production
    source, scattering, scint. and reflections.
  \item PMTs have PE transit time jitter as well as random,
    uncorrelated dark noise.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Example: Inverse $\beta$ Decay with Radiation}
  \includegraphics[width=\textwidth]{overlaps}
\end{frame}

\begin{frame}
  \frametitle{Example: IBD + Rad, continued}
  \textcolor{red}{(Skip during presentation, for reference only)}

  From the previous figure.
  \begin{itemize}
  \item An Inverse $\beta$-decay initial kinematics are fed to the det. sim.
  \item The det. sim. produces prompt and delayed hits.
  \item At this point the process has the full results of the
    det. sim. available.  
  \item Next in line is a radiation that comes in between the prompt
    and delayed hits.  What to do?
    \begin{itemize}
    \item Process IBD hits $\rightarrow$ digits $\rightarrow$ crate
      data and write out to file.  Oops, now we have a Radiation event
      and the file is out of order
    \item Okay, hold on to the IBD hits ``long enough'' to assure all
      other kinematics that might lead to overlapped hits have been
      processed.  What is ``long enough''?  It depends....
    \end{itemize}
  \item What about dark noise?  If IBD is fully processed out to file,
    where to we insert dark noise?
  \end{itemize}
  {\Large Very confusing situation!}
\end{frame}

\section{The Design}

\begin{frame}
  \frametitle{Elements of the design}
  Abstract the problem to tame the confusion!  Elements of the design:
  \begin{itemize}
\item The job consists of a series of processing \textbf{stage} objects.
\item Each stage provides output \textbf{data elements} of a specific
  data type which are produced by one or more \textbf{processors}
  managed by the stage.
\item Processors produce the data elements for the stage, potentially
  using input data from the prior stage.  When a prior stage's data
  element is used it is considered \textbf{referenced} by the produced
  output data.
\item Each stage presents its data as a \textbf{time ordered
  collection} of elements through an \textbf{iterator} object.
\item Stage data must be retained until it is no longer referenced.
\item A \textbf{time} is given to each process when data is requested.
  The process should return new data only if this time is equal to or
  greater than the time of the data the processor last returned.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Example: Initial particle kinematics generation}
  \includegraphics[width=\textwidth]{kine-example}

  Stage 1, generation of initial particle kinematics with three event types.
\end{frame}

\begin{frame}
  \frametitle{Example: Kinematics, cont.}
  \textcolor{red}{(Skip during presentation, for reference only)}

  In the previous diagram:
  \begin{enumerate}
  \item Iteration starts at $t=0$ and all process return new data to
    the stage since they have not ever returned prior data.
  \item The stage time-orders the results and returns Cosmic-$\mu$ \#1
    at $t=t_1$.  This now the ``current time''.
  \item Next iteration, all processors are asked for new data at or
    after $t=t_1$.
  \item Cosmic-$\mu$ generates \#2 at $t=t_5$, the others add no new
    data.
  \item Stage time-orders the data and returns Radiation \#1 at $t=t_2$.
  \item etc.
  \end{enumerate}
\end{frame}

\section{Modes of operation}

\begin{frame}
  \frametitle{Different modes of operating}

  This design supports various different modes of operation:
  \begin{description}
    \item [Steady state] generation with no pre-defined time limit.
    \item [Partial] generation of only a subset of all stages.
    \item [File I/O] used to store intermediate stage output and later
      read back in to mimic generation.
  \end{description}

  Each mode is described in detail.
\end{frame}

\subsection{Steady State Generation}

\begin{frame}
  \frametitle{Steady state generation}
  \begin{itemize}
  \item It means: start the simulation and let it run as long as you
    need.
  \item Directly analogous to real data taking (ignoring start-of-run effects).
  \item No fixed simulation duration $\Rightarrow$
    \begin{itemize}
    \item No a'priori estimate of buffering needs, can not store full
      run in memory.
    \end{itemize}
  \item Requires ``just in time'' processing $\Rightarrow$
    \begin{itemize}
    \item Processors may need to delay output data request in order to
      access a prior stage's data as input to its processing.
    \item Explicit support in Gaudi for this type of operation.
    \end{itemize}
  \item To drive the simulation, simply read data from the last stage.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Just in time processing}
  \includegraphics[width=\textwidth]{sequence}

  A ``UML Sequence Diagram'' showing interaction between Stage N and
  Stage N-1.
\end{frame}

\begin{frame}
  \frametitle{Just in time processing, continued}
  \textcolor{red}{(Skip during presentation, for reference only)}

  In the previous diagram:
  \begin{enumerate}
  \item A iterator (not shown) starts to loop over Stage N's data and
    calls its \texttt{nextElement(current\_time)} method with the
    iterator's current time. 
  \item The stage queries all processors for data at or after
    \texttt{current\_time}.
    \begin{itemize}
    \item \texttt{ProcessorAlg1} needs an update and needs to ask Stage N-1 for data.  It returns new data.
    \item \texttt{ProcessorAlg2}'s prior data is still older than \texttt{current\_time} and adds no new data.
    \end{itemize}
  \item All processors return and Stage N sorts the results and
    returns the most recent data element.
  \end{enumerate}
\end{frame}

\subsection{Partial Generation}

\begin{frame}
  \frametitle{Partial Generation}

If one is only interested in hits, there is no reason to simulation
electronics and triggers, just loop over the Detector Simulation
stage.

\end{frame}

\subsection{Intermediate Files}

\begin{frame}
  \frametitle{Intermediate Files} 

  The above assumes everything is generated.  But, we may want
  intermediate files:
  \begin{itemize}
  \item A particular stage (eg. det. sim.) may be very time consuming,
    and you want to re-use its results while developing subsequent
    stages.
  \item To develop a stage's processor in isolation from running other
    stage code.
  \item To study intermediate results after the processing has
    finished.
  \item To plug in results generated in some unrelated, external
    software.
  \end{itemize}
  Easy to support: write replacement stage object that populates the
  Transient Event Store with data from file instead of generating it.

\end{frame}

\section{Notes on Implementation}

\begin{center}
  \begin{frame}
    \frametitle{Map of Design Object to Gaudi Features}
    \begin{tabular}{|r|l|}
      \hline
      Design Element & Gaudi Feature \\
      \hline
      \hline
      Data Elements & Daya Bay Data Model \\
      \hline
      Stage & Gaudi Tool \\
      \hline
      Stage Collection & Transient Event Store \\
      \hline
      Processor & Gaudi Algorithm \\
      \hline
      Collection of processors & GaudiSequenceAlg \\
      \hline
      Detector Simulation processor & GiGa (Gaudi/Geant4 interface)\\
      \hline
      References & Gaudi built-in reference counting support\\
      \hline

    \end{tabular}
  \end{frame}
\end{center}
\end{document}
