library(tidyverse)
library(arrow)
library(broom)17 A complete analysis
17.1 Introduction
This chapter is one measurement, from a compressed file to a result with uncertainties, using only what earlier chapters taught. The question comes from the figure in Chapter 1. Near 10 GeV the dimuon spectrum shows the Υ family, three bound states of a bottom quark and its antiquark, so close in mass that only a good detector can separate them. How well does this detector do it? The answer is the detector’s mass resolution, and since our detector has two kinds of muon chambers, the central barrel and the endcaps, the answer may differ between them. Along the way we will measure the mass of the Υ(1S).
The plan has five steps, and each is a few lines of tidy code:
- Store the events as a Parquet dataset.
- Select the muon pairs and histogram their mass where the data live, by detector region.
- Write down a model of the peaks, from the physics of resolution.
- Fit the model in every region at once.
- Check the results against what we know, and draw them.
Prerequisites
broom, installed with the tidyverse, turns fitted models into tidy tables.
17.2 Store
As in Chapter 14, the events go into a dataset of Parquet files, one per run, compressed with zstd. On real data this step happens once, when the data arrive, and every analysis afterwards starts from the dataset:
unlink("scratch/capstone", recursive = TRUE)
read_csv("data/dimuon.csv.gz", col_types = "iidddidddi") |>
group_by(run) |>
write_dataset("scratch/capstone/events", compression = "zstd")
events <- open_dataset("scratch/capstone/events")17.3 Select and histogram
A pair is classified by where its muons went: barrel if both have |η| < 1.2, endcap if both are beyond it, mixed otherwise. case_when() writes that as a list of conditions and results, and arrow translates it. The histogram uses bins 25 MeV wide, from 8.5 to 11.5 GeV, and is computed inside arrow; only the counts come back:
bin_width_gev <- 0.025
upsilon_hist <- events |>
filter(mu1_charge != mu2_charge) |>
mutate(
mass_gev = sqrt(
2 * mu1_pt * mu2_pt * (cosh(mu1_eta - mu2_eta) - cos(mu1_phi - mu2_phi))
),
region = case_when(
abs(mu1_eta) < 1.2 & abs(mu2_eta) < 1.2 ~ "barrel",
abs(mu1_eta) >= 1.2 & abs(mu2_eta) >= 1.2 ~ "endcap",
.default = "mixed"
)
) |>
filter(mass_gev > 8.5, mass_gev < 11.5) |>
mutate(bin = floor(mass_gev / bin_width_gev)) |>
count(region, bin) |>
collect() |>
mutate(mass_gev = (bin + 0.5) * bin_width_gev) |>
arrange(region, bin)
upsilon_hist |>
group_by(region) |>
summarise(pairs = sum(n), bins = n())
#> # A tibble: 3 × 3
#> region pairs bins
#> <chr> <int> <int>
#> 1 barrel 6752 120
#> 2 endcap 4591 120
#> 3 mixed 4442 12017.4 Look before you fit
Always look at the data before fitting a model to it. Each bin’s count has a Poisson uncertainty of about √n:
ggplot(upsilon_hist, aes(x = mass_gev, y = n)) +
geom_pointrange(aes(ymin = n - sqrt(n), ymax = n + sqrt(n)), size = 0.1, linewidth = 0.3) +
facet_wrap(~region) +
labs(x = "Dimuon mass (GeV)", y = "Pairs per 25 MeV")
The three peaks are the Υ(1S), Υ(2S) and Υ(3S). In the barrel they are cleanly separated. In the endcaps they are wider, and the upper two merge. The peaks themselves are physically far narrower than anything the detector can resolve, a few tens of keV, so the widths we see are entirely the detector’s.
17.5 A model from physics
The invariant mass comes from the product of the two transverse momenta, \(M^2 \propto p_{T1} p_{T2}\). A small relative error in each momentum therefore gives a relative error in the mass of
\[ \frac{\delta M}{M} = \frac{1}{2}\sqrt{\left(\frac{\delta p_{T1}}{p_{T1}}\right)^2 + \left(\frac{\delta p_{T2}}{p_{T2}}\right)^2} , \]
so if the detector measures momenta with a fixed relative resolution, the mass resolution is a fixed fraction of the mass, the same fraction for all three peaks. That gives a model with few parameters: three Gaussian peaks whose widths are one shared fraction res of their masses, on a straight-line background. The mass differences between the three states are known precisely from other experiments, 563.0 and 894.7 MeV, so they are fixed, which leaves the Υ(1S) mass m1 free. Each peak’s yield, n1 to n3, is a number of pairs, so the expected count in a bin is the yield times the bin width times the Gaussian density:
upsilon_model <- function(mass, m1, res, n1, n2, n3, a, b) {
peak <- function(mass_peak, yield) {
yield * bin_width_gev * dnorm(mass, mean = mass_peak, sd = res * mass_peak)
}
peak(m1, n1) + peak(m1 + 0.5630, n2) + peak(m1 + 0.8947, n3) +
a + b * (mass - 10)
}nls(), base R’s nonlinear least-squares fitter, fits it. Weighting each bin by 1/n makes the fit a chi-squared fit with Poisson uncertainties. Starting values come from a glance at the figure: a peak near 9.45 GeV, a resolution of about 1%, and yields that are fractions of the total:
fit_upsilons <- function(hist) {
total <- sum(hist$n)
nls(
n ~ upsilon_model(mass_gev, m1, res, n1, n2, n3, a, b),
data = hist,
start = list(
m1 = 9.45, res = 0.01,
n1 = 0.3 * total, n2 = 0.1 * total, n3 = 0.07 * total,
a = min(hist$n), b = 0
),
weights = 1 / pmax(hist$n, 1)
)
}17.6 Fit every region at once
Three regions, three fits. Rather than a loop, nest() packs each region’s histogram into a list-column, map() fits each one, and broom’s tidy() turns each fit into a table of parameters with standard errors. R for Data Science calls this pattern many models (Wickham et al. 2023), and it scales unchanged from three groups to three thousand:
fits <- upsilon_hist |>
nest(.by = region) |>
mutate(
fit = map(data, fit_upsilons),
parameters = map(fit, tidy),
chi_squared = map_dbl(fit, deviance),
dof = map_dbl(fit, df.residual)
)
fits |>
select(region, chi_squared, dof)
#> # A tibble: 3 × 3
#> region chi_squared dof
#> <chr> <dbl> <dbl>
#> 1 barrel 82.9 113
#> 2 endcap 119. 113
#> 3 mixed 93.0 113With Poisson weights, the weighted residual sum of squares, deviance(), is the fit’s χ², and each region’s is at or below its number of degrees of freedom: the model describes the data within their uncertainties. Here are the two parameters we came for:
results <- fits |>
select(region, parameters) |>
unnest(parameters) |>
filter(term %in% c("m1", "res")) |>
select(region, term, estimate, std.error)
results |>
knitr::kable(digits = 5)| region | term | estimate | std.error |
|---|---|---|---|
| barrel | m1 | 9.46027 | 0.00103 |
| barrel | res | 0.00789 | 0.00009 |
| endcap | m1 | 9.47415 | 0.00457 |
| endcap | res | 0.01784 | 0.00037 |
| mixed | m1 | 9.46750 | 0.00264 |
| mixed | res | 0.01309 | 0.00021 |
17.7 Check against what we know
The Υ(1S) mass that went into the simulation was 9.4604 GeV. The barrel measurement, with the smallest uncertainty, agrees with it to a fraction of its uncertainty of about 1 MeV. The endcap and mixed results come out high, by about 14 and 7 MeV, roughly three standard errors each. That is not an accident of the fit; it is a real bias in the data, and a classic one. The detector records only muons whose measured transverse momentum is above 4 GeV, so a muon whose momentum was measured too high is more likely to be kept than one measured too low. Near the threshold that tilts the sample toward overestimated momenta, and so toward overestimated masses, and the worse the resolution, the bigger the tilt; the endcaps, with the worst resolution, show the largest shift. Removing biases like this one is what calibration is for.
For the resolution we can do better than “plausible”, because we know the detector. The simulation measured momenta with a relative resolution of 1.1% in the barrel and 2.4% in the endcaps (see data-raw/make_data.R), and the formula above turns those into the expected mass resolution in each region:
expected <- tibble(
region = c("barrel", "endcap", "mixed"),
expected_res = c(
0.5 * sqrt(0.011^2 + 0.011^2),
0.5 * sqrt(0.024^2 + 0.024^2),
0.5 * sqrt(0.011^2 + 0.024^2)
)
)
results |>
filter(term == "res") |>
left_join(expected, join_by(region)) |>
mutate(pull = (estimate - expected_res) / std.error) |>
knitr::kable(digits = 5)| region | term | estimate | std.error | expected_res | pull |
|---|---|---|---|---|---|
| barrel | res | 0.00789 | 0.00009 | 0.00778 | 1.25396 |
| endcap | res | 0.01784 | 0.00037 | 0.01697 | 2.33611 |
| mixed | res | 0.01309 | 0.00021 | 0.01320 | -0.53305 |
The fitted resolutions match the detector to within a few percent of their values: about 0.8% of the mass in the barrel, 1.3% for mixed pairs and 1.7 to 1.8% in the endcaps. The pull, the difference in units of the statistical error, is small in the barrel and mixed regions and about two in the endcaps, where the blended peaks make the fit hardest and where details the model ignores, such as non-Gaussian tails and the exact shape of the background, matter most. That is what a real analysis would investigate next.
17.8 Draw it
The final figure shows the data, the fitted model and its three peaks. predict() evaluates each fit on a fine grid of masses, and map2() does it for each region:
mass_grid <- tibble(mass_gev = seq(8.5, 11.5, by = 0.005))
curves <- fits |>
mutate(
curve = map(fit, \(f) mutate(mass_grid, n = predict(f, newdata = mass_grid)))
) |>
select(region, curve) |>
unnest(curve)
peak_labels <- tibble(
mass_gev = c(9.4604, 10.0234, 10.3551),
label = c("Υ(1S)", "Υ(2S)", "Υ(3S)"),
label_x = c(9.4604, 10.0234, 10.42),
label_y = c(540, 200, 150),
hjust = c(0.5, 0.5, 0)
)ggplot(upsilon_hist, aes(x = mass_gev, y = n)) +
geom_vline(xintercept = peak_labels$mass_gev, linetype = "dotted", colour = "grey60") +
geom_pointrange(aes(ymin = n - sqrt(n), ymax = n + sqrt(n)), size = 0.1, linewidth = 0.3) +
geom_line(data = curves, colour = "#c1121f", linewidth = 0.6) +
geom_text(
data = mutate(peak_labels, region = "barrel"),
aes(x = label_x, y = label_y, label = label, hjust = hjust),
size = 3
) +
facet_wrap(~region) +
labs(x = "Dimuon mass (GeV)", y = "Pairs per 25 MeV")
17.9 What this analysis left out
A measurement for publication would go further. It would vary the choices made here, the background shape, the fit range and the bin width, and quote the spread in the results as a systematic uncertainty. It would model the peaks’ non-Gaussian tails. It would correct the momentum scale run by run, as in Chapter 15. And for the Υ(1S) mass it would calibrate the absolute momentum scale against a reference, such as the J/ψ mass, rather than trusting the detector’s raw scale. Each of those is a few more lines of the same tidy code, which is the point of having written it this way.
17.10 Summary
You ran a complete measurement in about fifty lines: stored the events as a Parquet dataset, classified and histogrammed the pairs inside arrow, looked at the data, wrote a model whose structure came from the physics of momentum resolution, fitted it in three detector regions at once with nest(), map() and broom’s tidy(), checked the fit quality with χ², compared the results with the known detector, and drew the result. Nothing in the analysis depends on how big the dataset is: on a year of real data only the time collect() takes would change.
17.11 Exercises
- Replace the straight-line background with a quadratic one, refit, and compare the fitted resolutions. Treat the change as a systematic uncertainty and add it in quadrature to the statistical one.
- Refit with bins of 10 and 50 MeV. How do the parameters and their uncertainties change?
- Free the mass differences between the three states in the barrel fit, by giving the Υ(2S) and Υ(3S) their own mass parameters. How precisely does this detector measure them?
- Repeat the whole analysis for the J/ψ peak near 3.1 GeV, with a single Gaussian on a linear background. Is the fractional resolution the same as for the Υ states? Should it be?
- Split the barrel pairs into those with both muons above 10 GeV in transverse momentum and the rest, and fit each. Does the resolution depend on momentum in this simulated detector? In a real one, where multiple scattering and the curvature measurement both contribute, would you expect it to?