/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright 1997-2002, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Component: TOFlog_monitor.comp
*
* %I
* Written by:  Kim Lefmann
* Date: October 2000
* Origin: Risoe
*
* 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: [m]             Lower x bound of detector opening
* xmax: [m]             Upper x bound of detector opening
* ymin: [m]             Lower y bound of detector opening
* ymax: [m]             Upper y bound of detector opening
* xwidth: [m]           Width of detector. Overrides xmin, xmax
* yheight: [m]          Height of detector. Overrides ymin, ymax
* tmin: [mus]           Lower bound for time bins
* tmax: [mus]           Higher bound for time bins
* ndec: [1]             Number of time bins per decade
* filename: [string]    Name of file in which to store the detector image
* restore_neutron: [1]  If set, the monitor does not influence the neutron state
* nowritefile: [1]      If set, monitor will skip writing to disk
*
* 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_monitor

DEFINITION PARAMETERS ()

SETTING PARAMETERS (tmin, tmax, int ndec=10, int nowritefile=0,
  string filename=0, xmin=-0.05, xmax=0.05, ymin=-0.05, ymax=0.05,
  xwidth=0, yheight=0, int restore_neutron=0)

OUTPUT PARAMETERS ()
/* Neutron parameters: (x,y,z,vx,vy,vz,t,sx,sy,sz,p) */

DECLARE
%{
  int nchan;
  DArray1d TOF_N;
  DArray1d TOF_p;
  DArray1d TOF_p2;
%}

INITIALIZE
%{
  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);
  }
  
  if (tmin <= 0 || tmax <= 0) {
      printf("TOFlog_mon: %s: Only supports positive tmin and tmax !\n"
             "ERROR       (tmin, tmax). Exiting", NAME_CURRENT_COMP);
      exit(0);
  }
    
  if (tmin >= tmax) {
      printf("TOFlog_mon: %s: tmin should be smaller than tmax !\n"
             "ERROR       (tmin, tmax). Exiting", NAME_CURRENT_COMP);
      exit(0);
  }

  nchan=(int)ceil(ndec*log(tmax/tmin)/log(10.0));
  TOF_N = create_darr1d(nchan);
  TOF_p = create_darr1d(nchan);
  TOF_p2 = create_darr1d(nchan);

  // Use instance name for monitor output if no input was given
  if (!strcmp(filename,"\0")) sprintf(filename,"%s",NAME_CURRENT_COMP);
%}

TRACE
%{
  int i;

  PROP_Z0;
  if (x>xmin && x<xmax && y>ymin && y<ymax)
  {
    i = (int)floor(ndec*log(1E6*t/tmin)/log(10.0));         /* Bin number */
    if(i >= 0 && i < nchan) {
      double p2 = p*p;
      #pragma acc atomic      
      TOF_N[i] = TOF_N[i]+1;
      #pragma acc atomic
      TOF_p[i] = TOF_p[i]+p;
      #pragma acc atomic      
      TOF_p2[i] = TOF_p2[i]+p2;
      SCATTER;
    }
  }
  if (restore_neutron) {
    RESTORE_NEUTRON(INDEX_CURRENT_COMP, x, y, z, vx, vy, vz, t, sx, sy, sz, p);
  }
%}

SAVE
%{
if (!nowritefile) {
  DETECTOR_OUT_1D(
      "Time-of-flight monitor",
      "Log(Time-of-flight [\\gms])",
      "Intensity",
      "t", log(tmin)/log(10.0), log(tmax)/log(10.0), nchan,
      &TOF_N[0],&TOF_p[0],&TOF_p2[0],
      filename);
}
%}

FINALLY
%{
  destroy_darr1d(TOF_N);
  destroy_darr1d(TOF_p);
  destroy_darr1d(TOF_p2);
%}


MCDISPLAY
%{
  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
