/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2008, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Instrument: test
*
* %Identification
* Written by: Mads Bertelsen
* Date: September 2015
* Origin: University of Copenhagen
* %INSTRUMENT_SITE: Union_demos
*
*
* %Description
* Example showing the use of the "number_of_activations" parameter to include
*  an external component into an ensamle of Union components
*
* Example: stick_displacement=0 Detector: detector_I=189.144
*
* %Parameters
* stick_displacement: [m] height displacement of sample stick
*
* %End
*******************************************************************************/

DEFINE INSTRUMENT External_component(stick_displacement=0)

DECLARE
%{
%}

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

TRACE

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

COMPONENT Al_incoherent = Incoherent_process(sigma=4*0.0082,packing_factor=1,unit_cell_volume=66.4) //,interact_fraction=0.8)
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

// Source for transmission picture
//COMPONENT source = Source_div(
//        xwidth=0.12, yheight=0.12,focus_aw=0.5, focus_ah=0.5,
//        E0 = 50,
//        dE = 0, flux = 1E9)
//  AT (0,-0.02,0) RELATIVE a1 ROTATED (0,0,0) RELATIVE a1

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

COMPONENT drum_center = Arm()
AT (0,0.38,0) RELATIVE beam_center
ROTATED (0,0,0) RELATIVE beam_center

// V1
COMPONENT cryostat_mountin_plate = Union_cylinder(radius=0.12,yheight=0.01,priority=7,material_string="Al",number_of_activations=2)
AT (0,-0.103,0) RELATIVE beam_center
ROTATED (0,0,0) RELATIVE beam_center

// V2
COMPONENT cryostat_drum_walls = Union_cylinder(radius=0.2,yheight=0.57,priority=8,material_string="Al",number_of_activations=2)
AT (0,0,0) RELATIVE drum_center
ROTATED (0,0,0) RELATIVE drum_center

// V3
COMPONENT cryostat_drum_vacuum = Union_cylinder(radius=0.19,yheight=0.55,priority=9,material_string="Vacuum",number_of_activations=2)
AT (0,0,0) RELATIVE drum_center
ROTATED (0,0,0) RELATIVE drum_center

// V4
COMPONENT outer_cryostat_wall = Union_cylinder(radius=0.1,yheight=0.2,priority=10,material_string="Al",p_interact=0.2,number_of_activations=2)
AT (0,0,0) RELATIVE beam_center
ROTATED (0,0,0) RELATIVE beam_center

// V5
COMPONENT outer_cryostat_vacuum = Union_cylinder(radius=0.09,yheight=0.2,priority=11,material_string="Vacuum",number_of_activations=2)
AT (0,0.01,0) RELATIVE beam_center
ROTATED (0,0,0) RELATIVE beam_center

// V6
COMPONENT inner_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

// V7
COMPONENT inner_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

// V8
COMPONENT sample_stick_walls = Union_cylinder(radius=0.04,yheight=0.605,priority=14,material_string="Al",number_of_activations=2)
AT (0,0.39,0) RELATIVE beam_center
ROTATED (0,0,0) RELATIVE beam_center

// V9
COMPONENT sample_stick_vacuum = Union_cylinder(radius=0.035,yheight=0.64,priority=15,material_string="Vacuum",number_of_activations=2)
AT (0,0.4,0) RELATIVE beam_center
ROTATED (0,0,0) RELATIVE beam_center


COMPONENT sample_rod_bottom = Arm()
AT (0,0.05+stick_displacement,0) RELATIVE beam_center
ROTATED (0,85,0) RELATIVE beam_center

// V10
COMPONENT sample_rod = Union_cylinder(radius=0.0075,yheight=0.7,priority=25,material_string="Al",number_of_activations=2)
AT (0,0.35,0) RELATIVE sample_rod_bottom
ROTATED (0,0,0) RELATIVE sample_rod_bottom

// V11
COMPONENT sample_rod_collar_1 = Union_cylinder(radius=0.034,yheight=0.02,priority=17,material_string="Al",number_of_activations=2)
AT (0,0.048,0) RELATIVE sample_rod_bottom
ROTATED (0,0,0) RELATIVE sample_rod_bottom

// V12
COMPONENT sample_rod_collar_2 = Union_cylinder(radius=0.034,yheight=0.02,priority=18,material_string="Al",number_of_activations=2)
AT (0,0.14,0) RELATIVE sample_rod_bottom
ROTATED (0,0,0) RELATIVE sample_rod_bottom

// V13
COMPONENT sample_rod_collar_3 = Union_cylinder(radius=0.034,yheight=0.02,priority=19,material_string="Al",number_of_activations=2)
AT (0,0.34,0) RELATIVE sample_rod_bottom
ROTATED (0,0,0) RELATIVE sample_rod_bottom

// V14
COMPONENT sample_rod_collar_4 = Union_cylinder(radius=0.034,yheight=0.02,priority=20,material_string="Al",number_of_activations=2)
AT (0,0.635,0) RELATIVE sample_rod_bottom
ROTATED (0,0,0) RELATIVE sample_rod_bottom

// V15
COMPONENT sample_rod_collar_1_vacuum = Union_cylinder(radius=0.03,yheight=0.016,priority=21,material_string="Vacuum",number_of_activations=2)
AT (0,0.048-0.005,0) RELATIVE sample_rod_bottom
ROTATED (0,0,0) RELATIVE sample_rod_bottom

// V16
COMPONENT sample_rod_collar_2_vacuum = Union_cylinder(radius=0.03,yheight=0.016,priority=22,material_string="Vacuum",number_of_activations=2)
AT (0,0.14-0.005,0) RELATIVE sample_rod_bottom
ROTATED (0,0,0) RELATIVE sample_rod_bottom

// V17
COMPONENT sample_rod_collar_3_vacuum = Union_cylinder(radius=0.03,yheight=0.016,priority=23,material_string="Vacuum",number_of_activations=2)
AT (0,0.34-0.005,0) RELATIVE sample_rod_bottom
ROTATED (0,0,0) RELATIVE sample_rod_bottom

// V18
COMPONENT sample_rod_collar_4_vacuum = Union_cylinder(radius=0.03,yheight=0.016,priority=24,material_string="Vacuum",number_of_activations=2)
AT (0,0.635-0.005,0) RELATIVE sample_rod_bottom
ROTATED (0,0,0) RELATIVE sample_rod_bottom

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(0,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.02,0.4) RELATIVE beam_center
ROTATED (0,0,0) RELATIVE beam_center

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

END
