/*******************************************************************************
* Instrument: Unittest_JUMP_WHEN
*
* %I
* Written by: Peter Willendrup
* Date: Oct 28th, 2021
* Origin: DTU
* %INSTRUMENT_SITE: Tests_grammar
*
* JUMP WHEN unittest
*
* %D
* JUMP WHEN unittest.
* 
* One unit of intensity is emitted from a 1x1 m. The particles generated in 
* one source quadrant is repteated 'jumps' times. Resulting intensity should be:
* 1 + 0.25*jumps;
*
*
* %Example: jumps=10 Detector: PSD_I=3.5
* %Example: jumps=100 Detector: PSD_I=26
*
* %P
* jumps: [1] Number of jumps to do
* Pp0:   [1] Dummy input parameter used internally
*
* %L
* <reference/HTML link>
*
* %E
*******************************************************************************/
DEFINE INSTRUMENT Unittest_JUMP_WHEN(int jumps=10, Pp0=0)

DECLARE
%{
%}

USERVARS
%{
  int doJump;
  int numJump;
%}

INITIALIZE
%{
  Pp0=1.0/mcget_ncount();
%}

TRACE

COMPONENT Arm = Arm()
  AT (0,0,0) ABSOLUTE
EXTEND %{
  // position x in one of 100 bins
  x = rand01()-0.5;
  y = rand01()-0.5;
  p = INSTRUMENT_GETPAR(Pp0);
  vz=1000;
  if(x>0 && y<0) {
    doJump=1;
  }
%}

COMPONENT Return = Arm()
  AT (0,0,0) ABSOLUTE
EXTEND %{
  if(doJump>0) {
    if(numJump<INSTRUMENT_GETPAR(jumps)) {
      numJump++;
    } else {
      doJump=0;
    }
  }
%}
  
COMPONENT PSD = PSD_monitor(xwidth=2.0, yheight=2.0, filename="rngout", nx=100, ny=100)
  AT (0,0,0.1) ABSOLUTE

COMPONENT Jmp = Arm()
  AT (0,0,0.1) ABSOLUTE
JUMP Return WHEN (doJump)
  
FINALLY
%{
%}

END
