1/*******************************************************************************
2*
3* McStas, neutron ray-tracing package
4*         Copyright 1997-2002, All rights reserved
5*         Risoe National Laboratory, Roskilde, Denmark
6*         Institut Laue Langevin, Grenoble, France
7*
8* Component: PSD_monitor_psf
9*
10* %I
11* Written by: Kim Lefmann, Linda Udby
12* Date: Feb 3, 1998
13* Updated: Oct 15, 2007 (with psf)
14* Origin: Risoe
15*
16* Position-sensitive monitor.
17*
18* %D
19* An (n times m) pixel PSD monitor. This component may also be used as a beam
20* detector.
21*
22* Example: PSD_monitor(xmin=-0.1, xmax=0.1, ymin=-0.1, ymax=0.1, nx=90, ny=90, filename="Output.psd")
23*
24* %P
25* INPUT PARAMETERS:
26*
27* xmin: [m]           Lower x bound of detector opening
28* xmax: [m]           Upper x bound of detector opening
29* ymin: [m]           Lower y bound of detector opening
30* ymax: [m]           Upper y bound of detector opening
31* xwidth: [m]         Width/diameter of detector (x). Overrides xmin, xmax
32* yheight: [m]        Height of detector (y). Overrides ymin, ymax
33* nx: [1]             Number of pixel columns
34* ny: [1]             Number of pixel rows
35* filename: [string]  Name of file in which to store the detector image
36* psf: [m]            Point spread function, ray (x,y) coordinate randomized by gaussion of sigma psf
37* nowritefile: [1]      If set, monitor will skip writing to disk
38*
39* OUTPUT PARAMETERS:
40*
41* PSD_N: []           Array of neutron counts
42* PSD_p: []           Array of neutron weight counts
43* PSD_p2: []          Array of second moments
44*
45* %E
46*******************************************************************************/
47
48
49DEFINE COMPONENT PSD_monitor_psf
50DEFINITION PARAMETERS (nx=90, ny=90, string filename,restore_neutron=0)
51SETTING PARAMETERS (xmin=0, xmax=0, ymin=0, ymax=0, xwidth=0, yheight=0, psf=0, int nowritefile=0)
52OUTPUT PARAMETERS (PSD_N, PSD_p, PSD_p2)
53STATE PARAMETERS (x,y,z,vx,vy,vz,t,s1,s2,p)
54POLARISATION PARAMETERS (sx,sy,sz)
55
56DECLARE
57%{
58double PSD_N[nx][ny];
59double PSD_p[nx][ny];
60double PSD_p2[nx][ny];
61%}
62
63INITIALIZE
64%{
65int i,j;
66
67if (xwidth  > 0) { xmax = xwidth/2;  xmin = -xmax; }
68    if (yheight > 0) { ymax = yheight/2; ymin = -ymax; }
69
70    if ((xmin >= xmax) || (ymin >= ymax)) {
71            printf("PSD_monitor_psf: %s: Null detection area !\n"
72                   "ERROR        (xwidth,yheight,xmin,xmax,ymin,ymax). Exiting",
73           NAME_CURRENT_COMP);
74      exit(0);
75    }
76
77    for (i=0; i<nx; i++)
78     for (j=0; j<ny; j++)
79     {
80      PSD_N[i][j] = 0;
81      PSD_p[i][j] = 0;
82      PSD_p2[i][j] = 0;
83     }
84  %}
85
86TRACE
87  %{
88    int i,j;
89    double xp,yp;
90
91    PROP_Z0;
92
93    xp = x + psf*randnorm();
94    yp = y + psf*randnorm();
95
96    if (xp>xmin && xp<xmax && yp>ymin && yp<ymax)
97    {
98      i = floor((xp - xmin)*nx/(xmax - xmin));
99      j = floor((yp - ymin)*ny/(ymax - ymin));
100      PSD_N[i][j]++;
101      PSD_p[i][j] += p;
102      PSD_p2[i][j] += p*p;
103      SCATTER;
104    }
105    if (restore_neutron) {
106    RESTORE_NEUTRON(INDEX_CURRENT_COMP, x, y, z, vx, vy, vz, t, sx, sy, sz, p);
107    }
108  %}
109
110SAVE
111  %{
112    if (!nowritefile) {
113      DETECTOR_OUT_2D(
114          "PSD monitor",
115          "X position [cm]",
116          "Y position [cm]",
117          xmin*100.0, xmax*100.0, ymin*100.0, ymax*100.0,
118          nx, ny,
119          &PSD_N[0][0],&PSD_p[0][0],&PSD_p2[0][0],
120          filename);
121    }
122  %}
123
124MCDISPLAY
125%{
126
127  multiline(5, (double)xmin, (double)ymin, 0.0,
128               (double)xmax, (double)ymin, 0.0,
129               (double)xmax, (double)ymax, 0.0,
130               (double)xmin, (double)ymax, 0.0,
131               (double)xmin, (double)ymin, 0.0);
132%}
133
134END
135