aboutsummaryrefslogtreecommitdiff
path: root/src/detector.c
blob: bfc78b6e1db440a4bf1d28479f6d8099e93a46e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
 * detector.c
 *
 * Detector properties
 *
 * (c) 2007-2009 Thomas White <thomas.white@desy.de>
 *
 * pattern_sim - Simulate diffraction patterns from small crystals
 *
 */


#include <stdlib.h>
#include <math.h>
#include <stdio.h>

#include "image.h"
#include "utils.h"


/* Pulse energy density in J/m^2 */
#define PULSE_ENERGY_DENSITY (30.0e7)


void record_image(struct image *image)
{
	int x, y;
	double ph_per_e;
	double twotheta_max, np, sa_per_pixel;
	
	/* How many photons are scattered per electron? */
	ph_per_e = PULSE_ENERGY_DENSITY * pow(THOMSON_LENGTH, 2.0)
	          / image->xray_energy;
	printf("%e photons are scattered per electron\n", ph_per_e);
	
	twotheta_max = image->twotheta[0];
	np = sqrt(pow(image->x_centre, 2.0) + pow(image->y_centre, 2.0));
	sa_per_pixel = pow(2.0 * twotheta_max / np, 2.0);
	printf("sa per pixel=%e\n", sa_per_pixel);
	
	image->data = malloc(image->width * image->height * sizeof(uint16_t));
	
	for ( x=0; x<image->width; x++ ) {
	for ( y=0; y<image->height; y++ ) {
	
		double counts;
		double intensity;
		double sa;
		double complex val;

		val = image->sfacs[x + image->width*y];
		intensity = val * conj(val);

		/* What solid angle is subtended by this pixel? */
		sa = sa_per_pixel * cos(image->twotheta[x + image->width*y]);

		counts = intensity * ph_per_e * sa;
		
		image->data[x + image->width*y] = counts;
	
	}
	}
}