/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2008, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Instrument: Union_laue_camera
*
* %Identification
* Written by: Mads Bertelsen
* Date: September 2015
* Origin: University of Copenhagen
* %INSTRUMENT_SITE: Union_demos
*
* %Description
* Laue camera using Union components for the sample
*
* %Example: mosaic=5 Detector: det_I=2.08262e+07
* %Example: mosaic=5 Detector: PSDlin_transmission_scattered_I=1.0346e+06
* %Example: mosaic=5 Detector: PSDlin_transmission_transmitted_I=2.30318e+08
*
* %Parameters
* delta_d_d:                  [ ] Lattice spacing variation in sample
* mosaic:                [arcmin] Sample mosaicity
* lam0:                      [AA] Source mean wavelength
* dlam:                      [AA] Source wavelength spread
* radius:                     [m] Sample radius
* height:                     [m] Sample height   
* x_rotation_geometry:      [deg] Rotates union geometry component around x
* y_rotation_geometry:      [deg] Rotates union geometry component around y
* x_rotation_geometry_ref:  [deg] Rotates reference component around x
* y_rotation_geometry_ref:  [deg] Rotates reference component around y
* x_rotation_process:       [deg] Rotates crystal process (crystal orientation), x
* y_rotation_process:       [deg] Rotates crystal process (crystal orientation), y
* z_rotation_process:       [deg] Rotates crystal process (crystal orientation), z  
* geometry_interact:          [ ] p_interact of Union sample       
*
* %End
*******************************************************************************/
DEFINE INSTRUMENT Laue_camera(delta_d_d=1e-4, mosaic=5, lam0=7, dlam=5, radius=0.01, height=0.01,
   x_rotation_geometry=0, y_rotation_geometry=0,
   x_rotation_geometry_ref=0, y_rotation_geometry_ref=0,
   x_rotation_process=0, y_rotation_process=0, z_rotation_process=0,
   geometry_interact=0)

USERVARS
%{
int scattered_flag_instr;
%}

TRACE

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

COMPONENT YBaCuO_incoherent = Incoherent_process(sigma=2.105, packing_factor=1, unit_cell_volume=173.28)
AT (0,0,0) ABSOLUTE

COMPONENT single_crystal_orientation_110_vertical = Arm()
AT (0,0,0) ABSOLUTE
ROTATED (0,0,45) ABSOLUTE

COMPONENT single_crystal_orientation_001_along_x = Arm()
AT (0,0,0) ABSOLUTE
ROTATED (0,90,0) RELATIVE single_crystal_orientation_110_vertical

COMPONENT YBaCuO_single_crystal = Single_crystal_process(
          delta_d_d=1e-4, mosaic = 15,
          ax = 3.8186, ay = 0,      az = 0,
          bx = 0,      by = 3.8843, bz = 0,
          cx = 0,      cy = 0,      cz = 11.6777,
          reflections="YBaCuO.lau",barns=0,packing_factor=1)
AT (0,0,0) ABSOLUTE
ROTATED (x_rotation_process,y_rotation_process,z_rotation_process) ABSOLUTE

COMPONENT YBaCuO = Union_make_material(my_absorption=100*87.73759/1086.3739,
  process_string="YBaCuO_incoherent,YBaCuO_single_crystal")
AT (0,0,0) ABSOLUTE


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

COMPONENT source = Source_simple(
  radius=0.02, focus_xw=0.01, focus_yh=0.01,
  lambda0=lam0, dlambda=dlam, flux=1e12)
AT (0,0,0) ABSOLUTE

COMPONENT slit = Slit(
  xwidth=0.01, yheight=0.01)
AT (0,0,5) RELATIVE source


COMPONENT cylinder_sample_union = Union_cylinder(radius=radius,yheight=height, priority=1, material_string="YBaCuO",p_interact=geometry_interact)
AT (0,0,0.1) RELATIVE slit
ROTATED (x_rotation_geometry,y_rotation_geometry,0) RELATIVE slit

COMPONENT test_sample = Union_master()
AT(0,0,0.1) RELATIVE slit
EXTEND
%{
if (number_of_scattering_events == 0) scattered_flag_instr=0;
else scattered_flag_instr=1;
%}

COMPONENT det= PSD_monitor_4PI(radius=1, nx=360,ny=180,filename="psd",restore_neutron=1)
WHEN (scattered_flag_instr==1)
AT (0,0,0.1) RELATIVE slit
ROTATED (0,0,0) RELATIVE slit

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

  COMPONENT PSDlin_transmission_scattered = PSDlin_monitor(xwidth=0.15, yheight=0.01, nbins=100, filename="Output_transmission_lin_scattered.psd",restore_neutron = 1)
WHEN (scattered_flag_instr==1)
AT (0,0,0.5) RELATIVE slit

  COMPONENT PSDlin_transmission_transmitted = PSDlin_monitor(xwidth=0.15, yheight=0.01, nbins=100, filename="Output_transmission_lin_transmitted.psd",restore_neutron = 1)
WHEN (scattered_flag_instr==0)
AT (0,0,0.5) RELATIVE slit

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

END
