delphi-edm4hep
DELPHI SDST/FDST -> EDM4hep converter
Loading...
Searching...
No Matches
delphi_bs_fit.cpp
Go to the documentation of this file.
1// delphi_bs_fit โ€” per-run beamspot from aggregated primary vertices.
2//
3// Reproduces the per-run PV-aggregated beamspot recipe on edm4hep output.
4// The official DELPHI beamspot bank (sDST_BSP_BeamSpot / PSCBSP) closes
5// poorly in data-vs-MC; re-deriving the beamspot CENTRE from each sample's
6// own per-event primary-vertex distribution is self-consistent within a
7// sample and closes better. This is a CUSTOM OP (no DELPHI-bank value) and
8// a per-RUN aggregation, so it lives as a downstream tool over the
9// converter output rather than in the per-event converter โ€” it never
10// replaces sDST_BSP_BeamSpot.
11//
12// Recipe (bs_fit.cpp + docs/aleph-lep1-btag-summary.md ยง2):
13// centre = robust (Tukey biweight) mean of per-event PV positions, per
14// run; uncertainty sigma_centre = robust_sigma / sqrt(N).
15// width = the physical beam pancake (kept from the bank's per-event
16// BeamSpotSigma*, averaged per run) โ€” NOT re-derived.
17// sigma = sqrt(sigma_beam^2 + sigma_centre^2).
18//
19// Inputs are read in EDM4hep units (mm); the CSV is written in mm.
20// Run the tool once per sample (data and MC separately).
21//
22// Usage: delphi_bs_fit <input.edm4hep.root> <out_bs.csv>
23// reads sDST_PV_PrimaryVertex, sDST_EVT_runNumber,
24// sDST_EVT_BeamSpotSigma{X,Y,Z}
25
26#include <podio/Frame.h>
27#include <podio/ROOTReader.h>
28
29#include <edm4hep/VertexCollection.h>
30
31#include <algorithm>
32#include <array>
33#include <cmath>
34#include <cstdint>
35#include <fstream>
36#include <iostream>
37#include <map>
38#include <utility>
39#include <vector>
40
41namespace {
42
43// Tukey biweight robust mean (5-iteration M-estimator). Returns
44// (mean, sigma_of_mean). Transplanted verbatim from reco/vertex/bs_fit.cpp.
45std::pair<double, double> robustMean(std::vector<double> v) {
46 if (v.empty()) return {0.0, 0.0};
47 std::sort(v.begin(), v.end());
48 const double med = v[v.size() / 2];
49 std::vector<double> ad;
50 ad.reserve(v.size());
51 for (double x : v) ad.push_back(std::fabs(x - med));
52 std::sort(ad.begin(), ad.end());
53 const double mad = ad[ad.size() / 2];
54 const double scale = std::max(1.4826 * mad, 1e-9); // robust sigma
55 double mu = med;
56 for (int it = 0; it < 5; ++it) {
57 double sumW = 0, sumWX = 0;
58 const double c = 6.0 * scale; // Tukey constant
59 for (double x : v) {
60 const double r = (x - mu) / c;
61 if (std::fabs(r) >= 1.0) continue;
62 const double w = (1.0 - r * r) * (1.0 - r * r);
63 sumW += w;
64 sumWX += w * x;
65 }
66 if (sumW <= 0) break;
67 const double newMu = sumWX / sumW;
68 if (std::fabs(newMu - mu) < 1e-9) { mu = newMu; break; }
69 mu = newMu;
70 }
71 double sumW = 0, sumWX2 = 0;
72 const double c = 6.0 * scale;
73 for (double x : v) {
74 const double r = (x - mu) / c;
75 if (std::fabs(r) >= 1.0) continue;
76 const double w = (1.0 - r * r) * (1.0 - r * r);
77 sumW += w;
78 sumWX2 += w * (x - mu) * (x - mu);
79 }
80 const double s = (sumW > 0) ? std::sqrt(sumWX2 / sumW) : scale;
81 return {mu, s / std::sqrt(std::max(1.0, sumW))};
82}
83
84} // namespace
85
86int main(int argc, char** argv) {
87 if (argc < 3) {
88 std::cerr << "Usage: " << argv[0]
89 << " <input.edm4hep.root> <out_bs.csv>\n"
90 << " per-run beamspot from aggregated sDST_PV_PrimaryVertex (mm)\n";
91 return 1;
92 }
93
94 podio::ROOTReader reader;
95 reader.openFile(argv[1]);
96 const unsigned N = reader.getEntries("events");
97
98 std::map<int, std::vector<double>> runX, runY, runZ;
99 std::map<int, std::array<double, 4>> beamSum; // sumSx, sumSy, sumSz, n
100
101 for (unsigned i = 0; i < N; ++i) {
102 auto fd = reader.readEntry("events", i);
103 if (!fd) break;
104 const podio::Frame f(std::move(fd));
105
106 const auto run = f.getParameter<int>("sDST_EVT_runNumber");
107 if (!run) continue;
108
109 const auto& pv = f.get<edm4hep::VertexCollection>("sDST_PV_PrimaryVertex");
110 if (pv.size() == 0 || pv[0].getNdf() <= 0) continue; // no real PV
111 const auto p = pv[0].getPosition();
112 runX[*run].push_back(p.x);
113 runY[*run].push_back(p.y);
114 runZ[*run].push_back(p.z);
115
116 auto& b = beamSum[*run];
117 b[0] += f.getParameter<float>("sDST_EVT_BeamSpotSigmaX").value_or(0.f);
118 b[1] += f.getParameter<float>("sDST_EVT_BeamSpotSigmaY").value_or(0.f);
119 b[2] += f.getParameter<float>("sDST_EVT_BeamSpotSigmaZ").value_or(0.f);
120 b[3] += 1.0;
121 }
122
123 std::ofstream out(argv[2]);
124 out << "# run_number bs_x bs_y bs_z sigma_x sigma_y sigma_z (mm)\n"
125 << "# centre = Tukey-biweight robust mean of per-event PVs;\n"
126 << "# sigma = sqrt(beam_width_avg^2 + centre_uncertainty^2)\n";
127 for (auto& [run, xs] : runX) {
128 const auto [muX, eX] = robustMean(xs);
129 const auto [muY, eY] = robustMean(runY[run]);
130 const auto [muZ, eZ] = robustMean(runZ[run]);
131 const auto& b = beamSum[run];
132 const double n = std::max(b[3], 1.0);
133 const double sBx = b[0] / n, sBy = b[1] / n, sBz = b[2] / n;
134 const double effX = std::sqrt(sBx * sBx + eX * eX);
135 const double effY = std::sqrt(sBy * sBy + eY * eY);
136 const double effZ = std::sqrt(sBz * sBz + eZ * eZ);
137 out << run << " " << muX << " " << muY << " " << muZ
138 << " " << effX << " " << effY << " " << effZ
139 << " # n=" << xs.size() << "\n";
140 std::cout << "run " << run << " : N=" << xs.size()
141 << " centre=(" << muX << ", " << muY << ", " << muZ << ") mm"
142 << " sigma_centre=(" << eX << ", " << eY << ", " << eZ << ") mm\n";
143 }
144 std::cout << "Wrote " << argv[2] << "\n";
145 return 0;
146}
int main(int argc, char **argv)