/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2008, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Instrument: External_component_test
*
* %Identification
* Written by: Mads Bertelsen
* Date: September 2015
* Origin: University of Copenhagen
* %INSTRUMENT_SITE: Tests_union
*
* Inserts external component in Union system with exit volumes and number_of_activations
*
* %Description
* Demonstration of the use of "number_of_activations" and exit volumes to
* include a regular McStas component in an ensemble of Union components.
*
* %Example: Detector: detector_I=213.109
*
* %Parameters
*
* %End
*******************************************************************************/

DEFINE INSTRUMENT External_component_test()

USERVARS
%{
  int first_master;
  int second_master;
  int powderN_scat;
%}

TRACE

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

COMPONENT Al_powder = Powder_process(reflections="Al.laz")
AT (0,0,0) ABSOLUTE

COMPONENT Al = Union_make_material(my_absorption=100*4*0.231/66.4)
AT (0,0,0) ABSOLUTE


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

COMPONENT source = Source_div(
        xwidth=0.01, yheight=0.01,focus_aw=0.05, focus_ah=0.05,
        E0 = 50,
        dE = 0, flux = 1E9)
  AT (0,0,0) RELATIVE a1 ROTATED (0,0,0) RELATIVE a1


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

// V1
COMPONENT cryostat_wall = Union_cylinder(radius=0.06,yheight=0.16,priority=12,material_string="Al",p_interact=0.2,number_of_activations=2)
AT (0,0.01,0) RELATIVE beam_center
ROTATED (0,0,0) RELATIVE beam_center

// V2
COMPONENT cryostat_vacuum = Union_cylinder(radius=0.05,yheight=0.15,priority=13,material_string="Vacuum",number_of_activations=2)
AT (0,0.01,0) RELATIVE beam_center
ROTATED (0,0,0) RELATIVE beam_center

// V3
COMPONENT sample_exit = Union_cylinder(radius=0.011,yheight=0.031,priority=100,material_string="Exit",number_of_activations=1)
AT (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
%{
first_master = number_of_scattering_events;
%}

COMPONENT cylinder_sample_powder = PowderN(reflections="Cu.laz", radius=0.01, yheight=0.03, pack=1, p_interact=0.8, thickness=0)
AT (0,0,0) RELATIVE beam_center
ROTATED (0,0,0) RELATIVE beam_center
EXTEND
%{
  if (SCATTERED) powderN_scat = 1; else powderN_scat = 0;
%}


COMPONENT test_sample_after = Union_master(allow_inside_start=1)
AT(0,0,0) RELATIVE beam_center
ROTATED(20,0,0) RELATIVE beam_center
EXTEND
%{
second_master = number_of_scattering_events;
%}


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

COMPONENT m4pi_before_and_after = PSD_monitor_4PI(radius=1, nx=180, ny=180, filename="Events_both.dat", restore_neutron=1)
WHEN (first_master > 0 && second_master > 0)
AT (0, 0, 0) RELATIVE beam_center
ROTATED (0,0,0) RELATIVE beam_center

COMPONENT Banana_monitor = Monitor_nD(radius=1, yheight=0.1, options="banana, theta limits=[20,170], bins=500",filename="banana.dat",restore_neutron=1)
AT (0,0,0) RELATIVE beam_center
ROTATED (0,0,0) RELATIVE beam_center

COMPONENT Banana_monitor_powderN = Monitor_nD(radius=1, yheight=0.1, options="banana, theta limits=[20,170], bins=500",filename="banana_powderN.dat",restore_neutron=1)
WHEN (powderN_scat==1)
AT (0,0,0) RELATIVE beam_center
ROTATED (0,0,0) RELATIVE beam_center

COMPONENT detector = PSD_monitor(xwidth=0.1, yheight=0.08, nx=200, ny=200, filename="PSD.dat", restore_neutron=1)
AT (0,0,0.4) RELATIVE beam_center
ROTATED (0,0,0) RELATIVE beam_center

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


END
