Background

This example compares constant background with Poisson counting noise in a specular scan. A constant seed would reproduce one Poisson realization; the script deliberately chooses a new seed for each interactive run. The deterministic case is retained for regression testing.

The general background API and normalization behavior are described in the background reference.

Result

Background result

Sample

Background sample

Python script

 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
64
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.10"
# dependencies = ["bornagain>=25,<26"]
# ///
"""
Reflection from film on substrate with background/noise
"""
import random
import bornagain as ba
ba.require_versions("bornagain>=25,<26")
from bornagain import nm, deg


def get_sample():
    # create materials
    vacuum = ba.Vacuum()
    ni_color = (0.93, 0.48, 0.14)
    ni_mat = ba.SLDMaterial("Ni", ni_color, 9.4245e-06, 0)
    substrate_color = (0.28, 0.57, 0.82)
    substrate_mat = ba.SLDMaterial("SiSubstrate", substrate_color, 2.0704e-06, 0)

    # create layers
    ambient_layer = ba.Layer(vacuum)
    ni_layer = ba.Layer(ni_mat, 7*nm)
    substrate_layer = ba.Layer(substrate_mat)

    # create sample
    sample = ba.Sample()
    sample.addLayer(ambient_layer)
    sample.addLayer(ni_layer)
    sample.addLayer(substrate_layer)

    return sample


def get_simulation(sample, background):
    n = 500
    scan = ba.AlphaScan(n, 2*deg/n, 2*deg)
    scan.setWavelength(0.154*nm)
    scan.setIntensity(1e5)
    simulation = ba.SpecularSimulation(scan, sample)
    simulation.setBackground(background)
    return simulation


def simulate(background, title):
    sample = get_sample()
    simulation = get_simulation(sample, background)
    result = simulation.simulate()
    result.setTitle(title)
    return result


if __name__ == '__main__':
    ba.showSample3D(get_sample(), sample_size=120*nm, seed=0)
    # New seed each run; constant seed reproduces the same noise realization.
    poisson_seed = random.randrange(2**32)
    results = [
        simulate(ba.ConstantBackground(2), "Constant background"),
        simulate(ba.PoissonBackground(seed=poisson_seed), "Poisson noise"),
    ]
    ba.plot_multicurve(results)
    ba.plt.show()
auto/Examples/specular/instrument/Background.py