%+
% Name:
%   energy_pi_spec_1.0.tex
%
% History:
%   2001 Feb 05, created, Glenn E. Allen
%-

\documentclass{article}

\usepackage{/nfs/inconceivable/d0/SRC/LATEX/STY/cxo_memo_logo}
\usepackage[dvips]{graphics}
\usepackage{epsfig}
\usepackage{/nfs/inconceivable/d0/SRC/LATEX/STY/gea}

\begin{document}

% 1. Header

\memobasic{
  Jonathan McDowell, SDS Group Leader }{ 
  Glenn Allen, SDS, and Miriam Krauss, CDO }{
  Randomizing the Values of ENERGY and PI in ACIS Event Data Files }{
  1.0 }{
  http://space.mit.edu/CXC/docs/docs.html\#energy\_pi }{
  /nfs/inconceivable/d0/SDS/SPECS/ENERGY\_PI/energy\_pi\_spec\_1.0.tex }

% 2. Introduction

\section{The Problem}
\label{prob}

A problem has been reported for an ACIS PI spectrum of obsid 1996 (the Crab).  
The PI spectrum for a spatially-selected subset of the events exhibits 
semi-periodic jumps in the total number of counts per bin (fig.~\ref{fig1}).  
(Please do not circulate this memo to non-CXC staff, since the data are 
proprietary.)  This behavior is not evident in the corresponding PHA 
spectrum (fig.~\ref{fig2}).  After carefully investigating this issue, 
Miriam identified the problem: the values of ENERGY are discrete.  There is 
a one-to-one mapping between the discrete values of PHA and the discrete 
values of ENERGY.  As a result, when PI is calculated from ENERGY, sometimes 
a PI bin contains $n$ discrete values of ENERGY and sometimes a PI bin 
contains $n+1$ values.  That is, the width of the PI bins (14.6~eV) is not 
an integer multiple of the difference between consecutive values of ENERGY 
($\Delta {\rm ENERGY} \sim 4$~eV).  The proposed solution to this problem is 
to randomize the value of the ENERGY of an event within plus or minus one-half 
of a PHA bin.  This randomization would yield a continuous range of values 
for ENERGY and hence a smooth transition from one PI bin to the next.

As a test of the proposed technique, the values of the ENERGY of the events 
for obsid 1996 have been randomized in a manner similar to the technique 
described in section~\ref{comp}.  The corresponding values of PI were 
computed in the usual manner.  The results are shown in figure~\ref{fig3}.  
This figure shows part of the PI spectrum for obsid 1996.  The red curve is 
the spectrum obtained before randomization and the black curve is the 
spectrum obtained after randomization.  Unlike the results shown in 
figures~\ref{fig1} and \ref{fig2}, the results shown in figure~\ref{fig3} 
are for the entire source (i.e.\ several different $32 \times 32$~pixel 
regions).  Since the results are averaged over the different gains (i.e.\ 
PHA to ENERGY mappings) of these regions, the effect is diminished with 
respect to the effect shown in figure~\ref{fig1}, but still significant.  

The histogram in figure~\ref{fig4} shows the difference between the two PI 
spectra in figure~\ref{fig3}.  The red and blue curves in figure~\ref{fig4} 
represent the square root of and 3\% of the randomized spectrum of 
figure~\ref{fig3}, respectively.  Since the amplitude of the differences is 
significantly larger than the red curve over most of range of PI shown, the 
effect is very significant for this observation of a bright source.

Figure~\ref{fig5} shows the difference between the red and black spectra of 
figure~\ref{fig3} divided by the black spectrum of figure~\ref{fig3}.  The 
RMS deviation of the two spectra is 2.8\% and appears to be a constant over 
the range of PIs shown.  This behavior is consistent with the nature of the 
problem.  Note that if a much smaller region were used to construct the PI 
spectra (i.e. if only one $32 \times 32$~pixel region were used), the 
amplitude of the differences would be larger.  For example, the differences 
in figure~\ref{fig1} are about 20\%, which is close to the maximum for the 
present binning of PHA ($\sim 4$~eV~channel$^{-1}$) and PI 
(14.6~eV~channel$^{-1}$).

Since the problem with the quantization of ENERGY and the non-commensurate 
binning of PI and PHA seems to be well understood and since randomizing the 
values of ENERGY within one adu solves the problem, we propose to change the 
algorithm used by acis\_process\_events to compute the values of ENERGY.  
The proposed algorithm is described in section~\ref{comp}.

% 3. computation of energy and pi

\section{Revised Algorithm to Compute ENERGY and PI}
\label{comp}

\begin{enumerate}

\item
Calculate the value of the ENERGY of an event that corresponds to the value of
the PHA of the event using the vectors named ENERGY and PHA in the gain ARD
file.  Select the gain ARD file that is appropriate for the nominal temperature
of the focal plane during the observation.  Since the gain depends on the
position of an event on a chip, the vectors ENERGY and PHA are tabulated in the
gain ARD region-by-region for each chip.  To compute the value of the ENERGY of
an ACIS event:

\begin{itemize}

\item[i.]
Find the vectors ENERGY and PHA of the row in the gain ARD file that satisfies
the conditions that
\begin{eqnarray}
{\rm chip\ id} & = & {\rm CCD\_ID,} \\
{\rm CHIPX\_MIN} & \le {\rm CHIPX} \le & {\rm CHIPX\_MAX,\ and} \\
{\rm CHIPY\_MIN} & \le {\rm CHIPY} \le & {\rm CHIPY\_MAX,}
\end{eqnarray}
where ``chip id'' is the chip on which the event was detected, CHIPX and CHIPY
specify the location on the chip at which the event was detected, and CCD\_ID,
CHIPX\_MIN, CHIPX\_MAX, CHIPY\_MIN, and CHIPY\_MAX are the names of columns 
in the gain ARD file.

\item[ii.]
Find the two real values of ${\rm PHA}_{i}$ and ${\rm PHA}_{i+1}$ in the 
vector PHA such that
\begin{equation}
{\rm PHA}_{i} \le {\rm PHA} + \alpha < {\rm PHA}_{i+1},
\end{equation}
where PHA is the pulse height amplitude of the event and $\alpha$ is a
uniform random deviate over the interval [$-0.5$,0.5).

\item[iii.]
Set the ENERGY of the event to be 
\begin{equation}
{\rm ENERGY} = 
   \frac{{\rm PHA} + \alpha - {\rm PHA}_{i}}{{\rm PHA}_{i+1} - {\rm PHA}_{i}}
   \biggl(
   {\rm ENERGY}_{i+1} - {\rm ENERGY}_{i}
   \biggr) +
   {\rm ENERGY}_{i},
\end{equation}
where ENERGY$_{i}$ and ENERGY$_{i+1}$ are the non-zero values in the vector
ENERGY that correspond to the values of PHA$_{i}$ and PHA$_{i+1}$,
respectively.  This formula is valid if the value of the PHA of the event 
satisfies
\begin{equation}
{\rm PHA}_{1} \le {\rm PHA} + \alpha < {\rm PHA}_{n},
\end{equation}
where PHA$_{1}$ and PHA$_{n}$ are the smallest and largest, respectively,
non-zero values in the vector PHA.  If $0 < {\rm PHA} < {\rm PHA}_{1}$,
\begin{equation}
{\rm ENERGY} = 
   \frac{{\rm PHA} + \alpha - {\rm PHA}_{1}}{{\rm PHA}_{2} - {\rm PHA}_{1}}
   \biggl(
   {\rm ENERGY}_{2} - {\rm ENERGY}_{1}
   \biggr) +
   {\rm ENERGY}_{1}.
\end{equation}

If ${\rm PHA} \ge {\rm PHA}_{n}$,
\begin{equation}
{\rm ENERGY} = 
   \frac{{\rm PHA} + \alpha - {\rm PHA}_{n-1}}{{\rm PHA}_{n} - {\rm PHA}_{n-1}}
   \biggl(
   {\rm ENERGY}_{n} - {\rm ENERGY}_{n-1}
   \biggr) +
   {\rm ENERGY}_{n-1}.
\end{equation}

\end{itemize}
If ${\rm ENERGY} < 0$, set ${\rm ENERGY} = 0$.

\item
Compute the value of the PI of an event as follows.
\begin{equation}
{\rm PI} = 
   {\rm integer\ part\ of}
   \Biggl(
   \frac{{\rm ENERGY}}{{\rm pi\_bin\_width}}
   \Biggr) +
   1
\end{equation}
For example, the values of ${\rm PI} = 1$, 2, and 3 correspond to $0 \le {\rm 
ENERGY} < 14.6$~eV, $14.6 \le {\rm ENERGY} < 29.2$~eV, and $29.2 \le {\rm 
ENERGY} < 43.8$~eV, respectively, using the default value of ${\rm 
pi\_bin\_width} = 14.6$~eV.  If ${\rm PI} > {\rm pi\_num\_bins}$, set ${\rm 
PI} = {\rm pi\_num\_bins}$.  The value of PI may have a value of zero in 
the special case that PHA has a value of zero.  In this case, also set the 
value of ENERGY to zero.  Otherwise, ${\rm PI} > 0$.

\end{enumerate} 

% 4. Figures

\newpage

\begin{figure}
\hbox{\epsfig{file=FIGS/energy_pi_spec_fig1.ps,angle=-90,width=6.50in}}
\caption{The ACIS PI spectrum of a spatially-selected subset of the events 
for obsid 1996.  The count rate jumps every four or five bins.  This jump is 
not evident in the corresponding PHA spectrum (see fig.~\ref{fig2}).
\label{fig1}}
\end{figure}

\begin{figure}
\hbox{\epsfig{file=FIGS/energy_pi_spec_fig2.ps,angle=-90,width=6.50in}}
\caption{The ACIS PHA spectrum of a spatially-selected subset of the events 
for obsid 1996.  This spectrum does not exhibit the count rate jumps 
observed in the corresponding PI spectrum (see fig.~\ref{fig1}).
\label{fig2}}
\end{figure}

\begin{figure}
\hbox{\epsfig{file=FIGS/energy_pi_spec_fig3.ps,angle=90,width=6.50in}}
\caption{A portion of the PI spectrum of all Level 2 events for obsid 1996 
before (red) and after (black) randomizing the values of ENERGY.  Since the 
value of PI is computed from the value of ENERGY, randomizing the value of 
ENERGY within a range corresponding to one adu removes the count-rate jumps 
observed in figure~\ref{fig1}.
\label{fig3}}
\end{figure}

\begin{figure}
\hbox{\epsfig{file=FIGS/energy_pi_spec_fig4.ps,angle=90,width=6.50in}}
\caption{The difference between the red and black curves of figure~\ref{fig3}
is shown as the black histogram.  The red curve is the square root of the black
spectrum of figure~\ref{fig3}.  The blue curve is the black spectrum of
figure~\ref{fig3} multiplied by a factor of 0.03.  The differences between the
two spectra of figure~\ref{fig3} are significant.
\label{fig4}}
\end{figure}

\begin{figure}
\hbox{\epsfig{file=FIGS/energy_pi_spec_fig5.ps,angle=90,width=6.50in}}
\caption{The difference between the red and black spectra of figure~\ref{fig3}
divided by the black spectrum of figure~\ref{fig3}.  The RMS deviation is 
2.8\% and appears to be constant over the range of PI shown.
\label{fig5}}
\end{figure}

% 5. Finish

\end{document}
