### Reflectometry: Fit Pt layer

In this example, we want to demonstrate how to fit experimental reflectivity data that was obtained by a time-of-flight experiment with unpolarized neutrons. Experimental data is available for a sample of a roughly 50 nm thick platinum layer on top of a silicon substrate that is published in this repository.

The mesaurements were made by Timothy Charlton, Haile Ambaye and Michael Fitzsimmons (ORNL) on a sample provided by Eric Fullerton (UCSD).

#### Fit model

We describe the above experiment by a three-layer model, where as usual the top layer is the vacuum and the substrate layer is the silicon substrate. On top of the silicon substrate, we place the platinum layer. The materials of both layers are described by their SLD, where we use literature values for both silicon as well as platinum and keep them constant throughout the fitting procedure.

The main parameters of the sample are stored in dictionary, where they are defined by a unique `name` and the following six parameters are utilized:

- Beam intensity: `intensity`

  We explicitly fit the beam intensity, in order to compensate for possible experimental errors and to circumvent problems with the rather large variance in the reflectivity data at low $Q$-values.

- Roughness on top of the Pt layer: `r_pt/nm`

- Roughness on top of the Si substrate: `r_si/nm`

- Thickness of the Pt layer: `t_pt/nm`

- The relative $Q$-resolution: `q_res/q` (c.f.)

- A $Q$-offset: `q_offset`

This global offset is introduced to account for uncertainties in the angle at which the measurement is performed.

Due to saturation of the detector it is possible that the intensity at low $Q$-values (i.e. at high count rates) is underestimated. Furthermore, there is a rather large variance in the data that also leads to a rather bad fit in this region. Therefore, we neglect the data in the low $Q$-region by choosing a cutoff at $Q_{\text{min}} = $ 0.18. This value is selected by hand after performing several fits and visually selecting a good result.

##### $Q$-offset

Currently, BornAgain does not have an API support for an offset of the $Q$-axis. Therefore, we need to shift the $Q$-axis before performing a simulation

    q_axis = q_axis + parameters["q_offset"]

This shift then needs to be counter-transformed when returning the results in the `qr(result)` function

    q = numpy.array(result.result().axis(ba.Axes.QSPACE)) - q_offset

##### Initial parameters

In order to successfully fit this example, we chose some sane starting values and the example code that is fully given below, can be run with the following command:

    python3 Pt_layer_fit.py fit

This performs a simulation with the initial parameters and yields the following result:

Reflectivity with the initial parameters before fitting

Immediately afterwards the fit is performed.

#### Fit result

In order to run the fitting procedure, the following command can be issued:

    python3 Pt_layer_fit.py fit

We need to allow a few seconds computational time and BornAgain should compute the following result

Reflectivity with the parameters obtained from our fit

If the `fit` keyword is omitted from the command line

    python3 Pt_layer_fit.py

a simulation is performed with our fit results and one should obtain the result shown above.

auto/Examples/fit/specular/Pt_layer_fit.py

Data to be fitted: RvsQ_36563_36662.txt.gz

- Introduction
  - Functionality overview
  - Architectural overview

- Install or build
  - Install Python
    - Python on Linux
    - Python on Windows
    - Python on Mac
    - Python modules
  - Install BornAgain
    - Linux
    - Windows
    - MacOS
    - Development snapshot
  - Build BornAgain
    - Unix
      - Install third-party software
      - Get the source
      - Build and install BornAgain
      - Run a first simulation
    - Windows

- Graphical user interface
  - Quick Start
  - GUI overview
  - Fitting in the GUI

- Python scripting
  - Run a script
  - Understand the syntax
  - Fitting
  - Upgrading

- Reference
  - Simulation model
    - Simulation classes
      - Specular
        - Example
      - Scattering
        - Examples
          - GISAS
          - without DWBA terms
          - with specular peak
          - SAS
          - Polarized SANS
          - Monte-Carlo
      - Off-specular
        - Off-specular
      - Depth probe
        - Examples
          - Thin film
          - Transmitted modulus
          - Resonator
    - Common settings
      - Background
      - Options
        - Monte-Carlo
        - # threads
        - Average materials
        - Include specular
      - Progress monitor
  - Instrument model
    - Beam
      - Footprint
      - Beam divergence
    - Scan
      - TOF reflectometry
      - TOF resolution
      - Beam Angular Spread
      - Beam Wavelength Spread
    - Detectors
      - SphericalDetector
      - 2d detector resolution
      - Offspec detector
    - Neutron polarization
      - Spin asymmetry example
    - Examples
      - 2D detector resolution
      - Neutron polarization
        - Magnetic layer
        - Ditto, realistic instrument
        - Polarized scattering
  - Sample model
    - Materials
      - Magnetization
    - Sample
      - Layer
      - LayerStack
      - Layout
    - Roughness
      - Interface model
      - Transient
      - Cross-correlation
      - Examples
        - GISAS
        - Reflectivity
    - Particles
      - Form factor
        - Hard particles
          - Bipyramid4
          - Box
          - CantellatedCube
          - Cone
          - Cylinder
          - Dodecahedron
          - EllipsoidalCylinder
          - HemiEllipsoid
          - HorizontalCylinder
          - Icosahedron
          - PlatonicOctahedron
          - PlatonicTetrahedron
          - Prism3
          - Prism6
          - Pyramid2
          - Pyramid3
          - Pyramid4
          - Pyramid6
          - Sphere
          - SphericalSegment
          - Spheroid
          - SpheroidalSegment
          - TruncatedCube
        - Ripples
          - Bar ripple
          - Cosine ripple
          - Sawtooth ripple
        - Soft particles
          - FuzzySphere
          - Gauss
        - Custom formfactor
      - Composite particles
        - Core and shell
        - Compound particle
        - Mesocrystal
      - Examples
        - Compound particle
        - Core and shell
        - Crossing an interface
        - Mesocrystal
        - Rotation example
        - Dense cylinders
        - Dense hemispheres
        - Uncorrelated mixture
        - Polydispersity
    - Particle assemblies
      - Grating
        - 1D lattice
        - Cosine ripples in a lattice
        - Triangular ripples in a lattice
      - 2D lattice
        - Square lattice
        - Lattice orientation distribution
        - Finite square lattice
        - Lattice with basis
        - Multi-layer lattice
        - Position variance
      - Radial paracrystal
      - 2D paracrystal
      - Others
        - Size-distribution model: decoupling approximation
        - Size-distribution model: local monodisperse approximation
        - Size-distribution model: size-spacing coupling approximation
  - Model modifiers
    - Rotation
    - Distributions
  - Data types
    - Class Datafield
      - Export
      - Find intensity peaks
    - SLD profile
      - SLD profile with rough interfaces
      - SLD profile with a sliced particle layer
  - Plotting
    - Functions
    - Configuration
    - Heat map
    - LaTeX markup
    - Export
    - Plot axes in different units
  - Fitting
    - Data import
      - TIFF
    - Minimizer settings
    - Multiple datasets
    - Examples
      - Fitting with masks
      - Custom objective function
      - Fitting along slices
      - GISAS 2D fits
      - Experiment at GALAXI
      - Find background
      - Minimizer settings
      - Bayesian Sampling
      - Consecutive fitting
      - Considering uncertainties
      - External minimizer
      - External minimizer: plotting
      - Simultaneous fit of two datasets
      - Spin asymmetry fit example
      - Reflectometry: Fit Pt layer
      - Reflectometry: Fit honeycomb

- Literature
  - Babonneau2010
  - Babonneau2013
  - Croset2021
  - Lazzari2002
  - Lazzari2006
  - Pospelov2020
  - Renaud2009
  - Sears1992
  - Wuttke2021

- How to
  - Get announcements
  - Solve known problems
    - Set Python environmental variables under Windows
  - Request help or report bugs
  - Cite BornAgain

- Developers' corner
  - Configure
    - Build
    - Pyenv
  - Develop
    - Manual checks
    - Debug
    - Webdoc
    - Code analyses
  - Release
    - Manual GUI test