\documentclass[11pt]{article}
\usepackage{mhchem}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{textcomp}
\usepackage{epsfig}
\usepackage{hyperref}
\usepackage[noabbrev, capitalize]{cleveref} % hyperref must be loaded first
\usepackage[
  detect-weight=true,
  per=slash,
  detect-family=true,
  separate-uncertainty=true]{siunitx}
% \usepackage{listings}
\usepackage[dvipsnames]{xcolor}
\usepackage{upquote}
\usepackage{minted}
\usemintedstyle{perldoc}

\usepackage[framemethod=tikz]{mdframed}
\usepackage{adjustbox}

% \definecolor{greybg}{rgb}{0.25,0.25,0.25}
% \definecolor{yellowbg}{rgb}{0.91, 0.84, 0.42}
% \definecolor{bananamania}{rgb}{0.98, 0.91, 0.71}

\mdfdefinestyle{warning}{%
  linecolor=red!70,
  frametitle={Warning},
  frametitlerule=true,
  frametitlebackgroundcolor=orange!40,
  backgroundcolor=orange!30,
  innertopmargin=\topskip,
  roundcorner=8pt,
  linewidth=1pt,
}
% \mdtheorem[style=theoremstyle]{warning}{Warning}

\mdfdefinestyle{listing}{%
  linecolor=Aquamarine!50,
  linewidth=1pt,
  backgroundcolor=yellow!40,
  roundcorner=8pt,
  % frametitlerule=true,
  % frametitlebackgroundcolor=yellow!50,
  innertopmargin=\topskip,
}
% \mdtheorem[style=listing]{listing}{Listing}

% \DeclareSIUnit\eVperc{\eV\per\clight}
% \DeclareSIUnit\clight{\text{\ensuremath{c}}}

\begin{document}
\title{Hit rate estimation for STM detectors}
\author{Nam H. Tran \\ Boston University}
\date{\today}
\maketitle

\begin{abstract}
  This report presents estimated hit rate on STM detectors with the updated
  geometry in Offline version TODO.
\end{abstract}
\section{Overview}
\label{sec:overview}
In order to measure the eponymous rate of the Mu2e experiment, the number of
stopped muons needs to be known to about \SI{10}{\percent}. The most promising
schemes have involved photon detectors far downstream of the Muon Beam Stop
measuring emissions from the Stopping Target (ST) at different times with
respect to each pulse of muons stopping in the target. There are three
categories we explore here for this stopping-target monitor (STM):
\begin{itemize}
  \item Prompt: X-rays emitted when the muon comes to rest in the ST and is
    captured into atomic orbit (atomic capture, sometimes referred to as “muon
    stop” in this text). These X-rays are emitted essentially O(ps) with the
    atomic capture time.
  \item Semiprompt: $\gamma$-rays emitted upon nuclear capture (sometimes
    referred to simply as ``capture'' in this text) of the muon after stop.
    These exhibit timing characteristics of the muonic lifetime ($\tau
    = \SI{864}{\ns}$).
  \item Delayed: $\gamma$-rays from activated daughters resulting from muon
    nuclear capture in the ST.
\end{itemize}
The baseline design of the STM has been described by Miller et
al~\cite{Miller2016}, the estimated hit rate on STM detector was about
\SI{1.1}{\kHz} in the time window \SIrange{200}{1695}{\ns} from arrival time of
a proton bunch. But this design would cause a high hit rate on the Cosmic
Ray Veto (CRV) system, therefore changes have been made in the Offline version
v6\textunderscore 0\textunderscore 2 by the CRV team to reduce the CRV hit
rate. The goal of this study is to re-estimate hit rates on STM detectors in
this new geometry.

The actual changes relevant to the STM are:
\begin{itemize}
  \item \SI{10}{\mm} polyethylene liner added to CRV shielding,
  \item field-of-view (FOV) collimator absorber thickness reduced to
    \SI{10}{\mm} (from \SI{20}{\mm}).
\end{itemize}

\section{Simulation details}
\label{sec:simulation_details}
The study was done using Mu2e Offline version v6\textunderscore
0\textunderscore 2 (released on Oct 16,
2016), hashtag \texttt{3d1e9154d7}. The simulation starts from the entrance of
TS5 (see \cref{fig:stm_geo_all}), taking \texttt{cd3-beam-g4s2-mubeam.0728a}
dataset as input. The dataset contains 5098 files, each corresponds to
\num{1e6} proton-on-target (POT). The dataset were reused 16 times with
different random seeds, where \SI{97.6}{\percent} of runs succeeded, equivalent
to \num{7.96e10} POTs.

\begin{figure}[htbp]
  \centering
  \includegraphics[width=1.0\textwidth]{figs/stm_geo_all}
  \caption{Simulation geometry showing the Detector Solenoid region on the
    left, sweeper magnet, Field-Of-View collimator, Spot-Size collimator, and
    the STM detectors on the right. Particles saved in the input files are
    shoot from the TS5 (orange circle), and transported to the STM region.}
  \label{fig:stm_geo_all}
\end{figure}

There were 8 virtual detectors (VD) in STM region enabled in this study, their
identification numbers (\texttt{vdid}), locations, and abbreviation names
(appear in the simulation output) are listed in \cref{tab:vds_list}.
Information recorded by the VDs includes: particle type (\texttt{pdgid}),
global and local coordinates, time, kinetic energy, and parent particle type.
Only particles considered important to the STM, namely electrons,
positrons, negative and positive muons, neutrons and photons, were written to
the output file.

\begin{table}[htbp]
\centering
\caption{List of virtual detectors read out in this study}
\label{tab:vds_list}
\begin{adjustbox}{max width=\textwidth}
  
\begin{tabular}{@{}ccll@{}}
\toprule
  &VDID & Location               & Abbreviation              \\
\midrule
1 & 81  & Exit of neutron shield of the DS      & DSNeutronShieldExit       \\
2 & 86  & Upstream of the STM system            & STM\textunderscore UpStr                \\
3 & 87  & Downstream of the sweeper magnet      & STM\textunderscore MagDnStr             \\
4 & 101 & Upstream of the spot-size collimator  & STM\textunderscore SpotSizeCollUpStr    \\
5 & 88  & Downstream of the spot-size collimator& STM\textunderscore CollDnStr            \\
6 & 89  & Upstream of the STM detector 1        & STM\textunderscore Det1UpStr            \\
7 & 90  & Upstream of the STM detector 2        & STM\textunderscore Det2UpStr            \\
8 & 100 & Downstream of the FOV collimator      & STM\textunderscore FieldOfViewCollDnStr \\
\bottomrule
\end{tabular}
\end{adjustbox}
\end{table}

\section{Simulation and analysis code}
\label{sec:simulation_and_analysis_code}

The simulation and analysis code are located at:
\url{/mu2e/app/users/namtran/STM_study_201611}.

% \lstinputlisting[language=bash,frame=single]{listings/code_dir_tree.sh}
\begin{mdframed}[style=listing]
  \inputminted[fontsize=\footnotesize]{bash}{listings/code_dir_tree.sh}
\end{mdframed}

\texttt{step00} contains configuration files for this simulation and a script to
submit all 5098 jobs to the FermiGrid. It took about 14 hours to complete one
job in average.

The \texttt{analysis} folder contains a script 
(\texttt{run\textunderscore statistics.sh}) which checks if a job has finished
successfully, and makes a list of such runs.
There is a simple analysis code (\texttt{main.cc}) to read the VD records and
make plots.

\section{Results}
\label{sec:results}
\begin{mdframed}[style=warning]
Muonic X-rays and probabilities in the simulation are not correct (see
\cref{sec:muonic_x_rays_in_geant4}).
\end{mdframed}

\subsection{STM detector energy spectra}
\label{sub:stm_detector_spectra}
Energy spectrum of particles hitting STM detectors in the range
\SIrange{0.1}{3.1}{\MeV} are presented in \cref{fig:stm_det_ke}. There were not many
hits, and only the annihilation peak stands out. Most of the particles are
photons as shown in
\cref{fig:stm_det_ptype}.
\begin{figure}[htbp]
  \centering
  \includegraphics[width=0.85\textwidth]{figs/ke_det1UpStr}
  \includegraphics[width=0.85\textwidth]{figs/ke_det2UpStr}
  \caption{Kinetic energy of particles hitting STM detectors 1 (top), and
    2 (bottom).}
  \label{fig:stm_det_ke}
\end{figure}
\begin{figure}[htbp]
  \centering
  \includegraphics[width=\textwidth]{figs/ke_pdg_det1UpStr}
  \includegraphics[width=\textwidth]{figs/ke_pdg_det2UpStr}
  \caption{Kinetic energy and type of particles hitting STM detectors 1 (top),
    and 2 (bottom).}
  \label{fig:stm_det_ptype}
\end{figure}

\subsection{STM detector hit rate estimation}
\label{sub:stm_detector_hit_rate_estimation}
The average number of hits on a STM detector per POT is:
\begin{equation}
  \frac{672 + 714}{2 \times 7.96 \times 10^{10}} = 8.7 \times 10^{-9}. 
  \label{eqn:stm_hit_count}
\end{equation}
There would be \num{3.1e7} POTs per proton bunch, so the number of hits each
bunch is: 
\begin{equation}
  8.7 \times 10^{-9} \times 3.1 \times 10^7 = 0.27.
\end{equation}
The instantaneous hit rate, assuming an interval of \SI{1695}{\ns} between
bunches, is:
\begin{equation}
  \frac{0.27}{1695\times 10^{-9}} = \SI{158.9e3}{\Hz}
\end{equation}

The uncertainty on the hit rate estimation is \SI{2.6}{\percent} if
only statistical uncertainty of the hit counting in \cref{eqn:stm_hit_count} is
taken into account. This hit rate is too high for a HPGe detector to function
well, so an attenuator would be installed upstream of the spot-size collimator
to lower the hit rate to about \SI{10}{\kHz}.

\subsection{Hit timing on STM detectors}
\label{sub:timing_of_hits_on_stm_detectors}
Timing in the simulation starts from the birth of a primary proton, which means
all events start at the same $t = 0$ time. In order to mimic the pulse
structure of the proton beam (\SI{250}{\ns} pulse width, \SI{1695}{\ns} between
pulses~\cite{Bartoszek2014}), the recorded times on each event are smeared by
a Gaussian distribution with a $\sigma = 250 / 6 = \SI{41.7}{\ns}$.

The hit timing as a function of kinetic energy for several virtual detectors
are shown in \cref{fig:ke_time_4vds}. Most of hits arrive between
\num{100} and \SI{400}{\ns} from the center of a proton pulse. Only a few of
particles could hit the STM detectors, especially in the energy region around
the $2p-1s$ peak, that it is hard to investigate the
dependence between timing and energy of hits. Therefore I will only analyze the
timing information of hits STM\_SpotSizeCollUpStr.

\begin{figure}[htbp]
  \centering
  \includegraphics[width=1.0\textwidth]{figs/ke_time_4vds}
  \caption{Hit timing as a function of kinetic energy at spot size
    collimator upstream (top left) and down stream (top right), and two STM
    detectors (bottom left and right).} 
  \label{fig:ke_time_4vds}
\end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Signal to background ratio: $2p-1s$ peak at spot-size collimator
  upstream}
\label{sub:signal_to_background_ratio_2p_1s_peak_at_spot_size_collimator_upstream}
The energy of muonic $2p-1s$ transition in aluminum is given as \SI{335}{\keV}
by Geant4. Background is taken as the average counts for 10 bins around
\SI{335}{\keV}, and signal strength is calculated by subtracting the background
from the count under the peak. Energy spectra at spot-size
collimator upstream (VD 101: STM\_SpotSizeCollUpStr) in \SI{50}{\ns} windows
and their signal-to-background ratios are shown in
\cref{fig:ke_SpotSizeCollUpStr_time_slices}.
\begin{figure}[htbp]
  \centering
  \includegraphics[width=1.0\textwidth]{figs/ke_SpotSizeCollUpStr_time_slices}
  \caption{Energy spectra at STM\_SpotSizeCollUpStr and signal-to-background
    ratios in 50-ns time windows.}
  \label{fig:ke_SpotSizeCollUpStr_time_slices}
\end{figure}

\subsection{Signal to background ratio: \SI{1809}{\keV} at spot-size collimator
upstream}
\label{sub:signal_to_background_ratio_1809_kev_at_spot_size_collimator_upstream}


%%%% Appendices
\pagebreak
\appendix
\section{How to run the simulation and analyze data}
%%%%%%%%%%%%%%%%%%%
\label{sec:how_to_run_the_simulation_and_analyze_data}
\subsection{Simulating beam flash}
\label{sub:simulating_beam_flash}
Simulation scripts are in:
\url{/mu2e/app/users/namtran/STM_study_201611/step00}:
\begin{itemize}
  \item \url{fcl/step00.fcl}: configuration for this study (primary particles,
    virtual detectors to be read out, particle filtering, ...)
  \item \url{geom/geom.txt}: specify geometry settings (thickness
    of shields, enabled virtual detectors, ...)
  \item \url{submit.sh}: submit all jobs (5098) in the
    \url{cd3-beam-g4s2-mubeam.0728a.list} to the grid 
\end{itemize}

\noindent Steps to run the simulation:
\begin{itemize}
  \item preparing user's code: follow Mu2e instruction to create an
    \texttt{Offline} distribution (mine is at
    \url{/mu2e/app/users/namtran/Offline}),

  \item setting up \texttt{mu2e} environment \footnote{I used \texttt{mu2egrid}
      version \texttt{v3\_02\_00} which supports \texttt{mu2eart} command}:
    \begin{mdframed}[style=listing]
      \inputminted[
        fontsize=\scriptsize,
        firstline=1,
        lastline=11,
        breaklines=true,
        breakanywhere=true
        ]{bash}{listings/runall.sh}
    \end{mdframed}

  \item submitting all jobs: 
    \begin{mdframed}[style=listing]
      \inputminted[
        fontsize=\scriptsize,
        firstline=13,
        breaklines=true,
        breakanywhere=true
        ]{bash}{listings/runall.sh}
    \end{mdframed}
\end{itemize}

\noindent Analysis code
\url{/mu2e/app/users/namtran/STM_study_201611/analysis}:
\begin{itemize}
  \item \texttt{run\_statistics}: skims the log files (\texttt{mu2e.log} in
    each subdirectory`) to make a list of successful runs, and collect CPU
    time, random seeds. This script should be run first.

  \item \texttt{main.cc}: the analysis code, it is rather simple now, only
    exports a few histograms from virtual detector hits. Run \texttt{make}
    to produce the executable \texttt{read\_vd}.
  \item \texttt{read\_vd}: takes a list of simulation outputs as input to
    produce a single ROOT file which contains several histograms.
\end{itemize}

%%%%%%%%%%%%%%%%%%%
\section{Muonic X-rays in Geant4}
\label{sec:muonic_x_rays_in_geant4}
The muonic energy levels and transition probabilities were calculated using
a simple model described by Mukhopadhyay~\cite{Mukhopadhyay.1977}.
\begin{itemize}
  \item Energy of K-shell electrons are precisely corrected based on
    that of hydrogen atom, taking finite size of the nucleus into account:
    % \lstinputlisting[
      % language=c++, firstline=64, lastline=93,firstnumber=64,
      % breaklines=true, breakatwhitespace=true,
      % frame=single]{listings/G4EmCaptureCascade.cc}
      \begin{mdframed}[style=listing]
        \inputminted[
          breaklines=true,
          stepnumber=5,
          linenos=true,
          firstline=64,
          fontsize=\footnotesize,
          lastline=93]{c++}{listings/G4EmCaptureCascade.cc}
      \end{mdframed}
  \item Energy of K-shell muons are calculated from energy of K-shell
    electrons:
      \begin{mdframed}[style=listing]
        \inputminted[
          breaklines=true,
          stepnumber=5,
          linenos=true,
          fontsize=\footnotesize,
          firstline=118,
          lastline=122]{c++}{listings/G4EmCaptureCascade.cc}
      \end{mdframed}
    \item Energies of muons on other shells are calculated by scaling from that
      of K-shell muons:
      \begin{mdframed}[style=listing]
        \inputminted[
          breaklines=true,
          stepnumber=5,
          linenos=true,
          fontsize=\footnotesize,
          firstline=123,
          lastline=125]{c++}{listings/G4EmCaptureCascade.cc}
      \end{mdframed}
\end{itemize}
Above listings are from the Geant4 source file:
\url{source/processes/hadronic/stopping/src/G4EmCaptureCascade.cc}


\bibliographystyle{h-physrev}
\bibliography{stm_study}
\end{document}
