/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2006, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Component: TOFlog_mon.comp
*
* %I
* Written by:  Kim Lefmann
* Date: October 2000
* Version: $Revision: 1.10 $
* Origin: Risoe
* Release: McStas 1.9.1
*
* Rectangular Time-of-flight monitor with logarithmic time binning.
*
* %D
*
* A rectangular time-of-flight monitor with logarithmic time binning.
* (The neutron intensity is NOT given logarithmically)
*
* %P
* INPUT PARAMETERS:
*
* xmin:     Lower x bound of detector opening (m)
* xmax:     Upper x bound of detector opening (m)
* ymin:     Lower y bound of detector opening (m)
* ymax:     Upper y bound of detector opening (m)
* xwidth:   Width/diameter of detector (x). Overrides xmin,xmax. (m)
* yheight:  Height of detector (y). Overrides ymin,ymax. (m)
* tlow:     Lower bound for time bins (mus)
* thigh     Higher bound for time bins (mus)
* ndec:     Number of time bins per decade (1)
* filename: Name of file in which to store the detector image (text)
*
* OUTPUT PARAMETERS:
*
* TOF_N:    Array of neutron counts
* TOF_p:    Array of neutron weight counts
* TOF_p2:   Array of second moments
*
* %E
*******************************************************************************/

DEFINE COMPONENT TOFlog_mon
DEFINITION PARAMETERS (tlow, thigh, ndec, filename)
SETTING PARAMETERS (xmin=0, xmax=0, ymin=0, ymax=0, xwidth=0, yheight=0)
OUTPUT PARAMETERS (TOF_N, TOF_p, TOF_p2)
STATE PARAMETERS (x,y,z,vx,vy,vz,t,s1,s2,p)
DECLARE
  %{
#define LARGENUMBER 10000
    int nchan;
    double TOF_N[LARGENUMBER];
    double TOF_p[LARGENUMBER];
    double TOF_p2[LARGENUMBER];
  %}

INITIALIZE
  %{
    int i;
    if (xwidth  > 0) { xmax = xwidth/2;  xmin = -xmax; }
    if (yheight > 0) { ymax = yheight/2; ymin = -ymax; }

    if ((xmin >= xmax) || (ymin >= ymax)) {
            printf("TOFlog_mon: %s: Null detection area !\n"
                   "ERROR       (xwidth,yheight,xmin,xmax,ymin,ymax). Exiting",
           NAME_CURRENT_COMP);
      exit(0);
    }

    nchan=(int)ceil(ndec*log(thigh/tlow)/log(10.0));
    if (nchan>LARGENUMBER)
      printf("FATAL ERROR, too many time channels \n");

    for (i=0; i<nchan; i++)
    {
      TOF_N[i] = 0;
      TOF_p[i] = 0;
      TOF_p2[i] = 0;
    }
  %}

TRACE
  %{
    int i;

    PROP_Z0;
    if (x>xmin && x<xmax && y>ymin && y<ymax)
    {
      i = (int)floor(ndec*log(1E6*t/tlow)/log(10.0));         /* Bin number */
      if(i >= nchan)
        i = nchan - 1;
      if(i < 0)
        i = 0;
      TOF_N[i]++;
      TOF_p[i] += p;
      TOF_p2[i] += p*p;
    }
  %}
SAVE
  %{
    DETECTOR_OUT_1D(
        "Time-of-flight monitor",
        "Log(Time-of-flight [\\gms])",
        "Intensity",
        "t", log(tlow)/log(10.0), log(thigh)/log(10.0), nchan,
        &TOF_N[0],&TOF_p[0],&TOF_p2[0],
        filename);
  %}

MCDISPLAY
%{
  magnify("xy");
  multiline(5, (double)xmin, (double)ymin, 0.0,
               (double)xmax, (double)ymin, 0.0,
               (double)xmax, (double)ymax, 0.0,
               (double)xmin, (double)ymax, 0.0,
               (double)xmin, (double)ymin, 0.0);
%}

END
