/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2008, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Instrument: Union_test_mask
*
* %Identification
* Written by: Mads Bertelsen
* Date: September 2015
* Origin: University of Copenhagen
* %INSTRUMENT_SITE: Union_demos
*
* Simple test instrument for mask functionality in Union framework.
*
* Example: Detector: m4pi_I=2.27579e-07
*
* %Description
* simple test instrument for sample component.
*
* %End
*******************************************************************************/

DEFINE INSTRUMENT Union_test_mask()

DECLARE
%{
int sample_1_index = 1;
int sample_2_index = 2;
%}

USERVARS %{
int scattered_1;
int scattered_2;
%}

TRACE

COMPONENT init = Union_init()
AT (0,0,0) ABSOLUTE

COMPONENT test_incoherent = Incoherent_process(sigma=20,packing_factor=1,unit_cell_volume=66.4)
AT (0,0,0) ABSOLUTE

COMPONENT test_material = Union_make_material(my_absorption=4)
AT (0,0,0) ABSOLUTE

COMPONENT a1 = Progress_bar()
  AT (0,0,0) ABSOLUTE

COMPONENT source = Source_div(
//        xwidth=0.0005, yheight=0.0005,focus_aw=15, focus_ah=15,
        xwidth=0.0005, yheight=0.0005,focus_aw=0.3, focus_ah=1,
        E0 = 10,
        dE = 0)
  AT (0,0,0) RELATIVE a1 ROTATED (0,0,0) RELATIVE a1

// test
//COMPONENT start_volume_test = cylinder(radius=0.03,yheight=1.4,priority=99,material_string="Vacuum")
//AT (0,0,0.7-0.02) RELATIVE a1
//ROTATED (90,0,0) RELATIVE a1


// Sample position
COMPONENT beam_center = Arm()
AT (0,0,10.4) RELATIVE a1
ROTATED (0,0,0) RELATIVE a1


// Children tests
COMPONENT volume_1 = Union_box(xwidth=0.1,yheight=0.1,zdepth=0.1,priority=10,material_string="test_material")
AT (0,0,0) RELATIVE beam_center
ROTATED (0,0,0) RELATIVE beam_center

COMPONENT volume_2 = Union_box(xwidth=0.09,yheight=0.1,zdepth=0.1,priority=11,material_string="test_material")
AT (0,0.01,0.01) RELATIVE beam_center
ROTATED (0,0,0) RELATIVE beam_center

COMPONENT mask_1_1 = Union_box(xwidth=0.03,yheight=0.03,zdepth=0.03,priority=12,mask_string="volume_1",mask_setting="ALL")
AT (0,0.0,0.0) RELATIVE beam_center
ROTATED (0,0,0) RELATIVE beam_center

COMPONENT mask_1_2 = Union_box(xwidth=0.01,yheight=0.01,zdepth=0.05,priority=13,mask_string="volume_1",mask_setting="ALL")
AT (0,0.0,0) RELATIVE beam_center
ROTATED (0,0,0) RELATIVE beam_center

COMPONENT mask_2_1 = Union_box(xwidth=0.04,yheight=0.04,zdepth=0.04,priority=14,mask_string="volume_2",mask_setting="ALL")
AT (0,0.0,0.0) RELATIVE beam_center
ROTATED (0,0,0) RELATIVE beam_center

COMPONENT mask_2_2 = Union_box(xwidth=0.015,yheight=0.015,zdepth=0.055,priority=15,mask_string="volume_2",mask_setting="ALL")
AT (0,0.0,0.0) RELATIVE beam_center
ROTATED (0,0,0) RELATIVE beam_center



COMPONENT test_sample = Union_master()
AT(0,0,0) RELATIVE beam_center
ROTATED(0,0,0) RELATIVE beam_center
EXTEND
%{
	if (scattered_flag[sample_1_index] > 0) scattered_1 = 1; else scattered_1 = 0;
	if (scattered_flag[sample_2_index] > 0) scattered_2 = 1; else scattered_2 = 0;
%}


COMPONENT m4pi = PSD_monitor_4PI(radius=1, nx=180, ny=180, filename="Events",restore_neutron=1)
  AT (0, 0, 0) RELATIVE beam_center

COMPONENT m4pi_1 = PSD_monitor_4PI(radius=1, nx=180, ny=180, filename="Events_1",restore_neutron=1) WHEN (scattered_1)
  AT (0, 0, 0) RELATIVE beam_center

COMPONENT m4pi_2 = PSD_monitor_4PI(radius=1, nx=180, ny=180, filename="Events_2",restore_neutron=1) WHEN (scattered_2)
  AT (0, 0, 0) RELATIVE beam_center

COMPONENT arm_1 = Arm()
  AT (0, 0, 0) RELATIVE beam_center
EXTEND
%{
	if (scattered_1 + scattered_2 != 2) ABSORB;
%}

COMPONENT m4pi_both = PSD_monitor_4PI(radius=1, nx=180, ny=180, filename="Events_12") WHEN (scattered_1 && scattered_2)
  AT (0, 0, 0) RELATIVE beam_center

COMPONENT stop = Union_stop()
AT (0,0,0) ABSOLUTE


END
