## Experiment at GALAXI

This is an example of a real data fit. We use our own measurements performed at the laboratory diffractometer GALAXI in Forschungszentrum Jülich.

Real-space model

Fit window

- The sample represents a 4 layer system (substrate, teflon, hmdso and air) with Ag nanoparticles placed inside the hmdso layer on top of the teflon layer.
- The sample is generated with the help of a `SampleBuilder`, which is able to create samples depending on parameters defined in the constructor and passed through to the `create_sample` method.
- The nanoparticles have a broad log-normal size distribution.
- The detector is created to represent the PILATUS detector from the experiment (line 19).
- In the simulation settings the beam is initialized and the detector is assigned to the simulation. A region of interest is assigned at line 39 to simulate only a small rectangular window. Additionally, a rectangular mask is added to exclude the reflected beam from the analysis (line 40).
- The real data is loaded from a tiff file into a histogram representing the detector’s channels.
- The `run_fitting()` function contains the initialization of the fitting kernel: loading experimental data, assignment of fit pair, fit parameters selection (line 62).

## Experiment description

To successfully simulate and fit results of some real experiment it is important to have

- A good guess about the sample structure and the initial values of the sample parameters.
- Full information about the instrument geometry: size and angular range of the detector.
- A 2D numpy array containing the intensities measured in the detector channels.

### Experiment

As an example we will use our own measurements performed at the laboratory diffractometer GALAXI in Forschungszentrum Jülich.

Our sample represents a 3-layer system (substrate, teflon and air) with Ag nanoparticles sitting on top of the teflon layer. The PILATUS 1M detector was placed at a distance of 1730 mm from the sample.

The results of the measurement are represented by the intensity image taken in certain conditions (beam wavelength, grazing angle, detector angular range) and stored in a 32-bit tiff file. To be able to fit these data we have to

- prepare a description of the simulation
- load the experimental data in BornAgain’s fitting engine

### Preparing the simulation description

From the experimental setup we know the number pixels and detector angular range. In BornAgain, we will represent this setup using the `SphericalDetector` object. A detector corresponding to a PILATUS detector:

    npx, npy = 981, 1043
    phi_min, phi_max = (-1.7*deg, 1.7*deg)
    alpha_min, alpha_max = (-0.6*deg, 1.24*deg)

    detector = ba.SphericalDetector(npx, phi_min, phi_max, npy, alpha_min, alpha_max)

### Setting the region of interest

To speed-up the simulation and to avoid an influence from uninteresting areas on the fit flow it is often convenient to define a certain region of interest `roi`. In our example we set the `roi` to the rectangle with lower left corner coordinates and upper right corner coordinates, where coordinates are expressed in detector units (‘deg’)

The final simulation setup looks as follows:

    simulation = ScatteringSimulation()
    simulation.setDetector(detector)  # this is our rectangular detector
    simulation.setSample(sample)  # sample creation is not covered by this tutorial
    simulation.setBeamParameters(1.34*angstrom, 0.463*degree, 0.0)
    simulation.setBeamIntensity(1.2e7)

During the fit, only the part of the detector corresponding to the `roi` will be simulated and used for $\chi^2$ calculations.

### Importing the real data using Fabio library

The Fabio library provides a convenient way to import experimental data in the form of a `numpy` array.

    import fabio

    img = fabio.open("galaxi_data.tif.gz")
    data = img.data.astype("float64")

The main requirement is that the shape of the numpy array coincides with the number of detector channels (i.e. `npx, npy = 981, 1043` for given example).

auto/Examples/fit/scatter2d/expfit_galaxi.py

Data to be fitted: galaxi_data.tif.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