Slash-separated forward,backward groups in LRBF (1,2/3,4) generate one RUN block per group with its own alpha, amplitude ratio and phase, backed by a generated FUNCTIONS amplitude line and per-group maps. Read-back reconstructs the GUI state and reproduces the file exactly; a reference fixture and real-musrfit integration (converged chisq/NDF 1.025906) cover the round trip.
271 lines
12 KiB
JavaScript
271 lines
12 KiB
JavaScript
// Copyright (C) 2026 Zaher Salman
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
// End-to-end integration checks for all three fit modes (single-run, global,
|
|
// series), gated on the presence of musrfit, msr2data, and the fake data tree.
|
|
// Each workflow stages real MSRs, invokes real musrfit/msr2data binaries, and
|
|
// verifies the outcome: converged STATISTIC blocks, correct parameter counts,
|
|
// and expected chisq/NDF values.
|
|
//
|
|
// When musrfit/msr2data or the data is unavailable (a non-provisioned CI box,
|
|
// say) the suite prints a skip notice and exits successfully so the baseline
|
|
// electrical tests remain runnable everywhere.
|
|
|
|
const assert = require("assert");
|
|
const childProcess = require("child_process");
|
|
const fs = require("fs");
|
|
const os = require("os");
|
|
const path = require("path");
|
|
const vm = require("vm");
|
|
|
|
const repoRoot = path.resolve(__dirname, "..");
|
|
const fixturesDir = path.join(repoRoot, "tests", "fixtures", "reference");
|
|
|
|
const MUSRFIT = "/usr/local/bin/musrfit";
|
|
const MSR2DATA = "/usr/local/bin/msr2data";
|
|
const LEM24_SRC = "/psi.ch/data/lem/his/2024/lem24_his_0514";
|
|
const LEM26_DATA = "/mnt/fake/lem/his/2026/lem26_his_0514";
|
|
const FAKE_DATA_ROOT = "/mnt/fake/lem/his/2026";
|
|
|
|
// Workflow -> expected converged chisq/NDF (from clean runs against lem26 data).
|
|
const singleRunWorkflows = {
|
|
asymmetry: { expected: "39.776426" },
|
|
bounds: { expected: "60.869627" },
|
|
t0background: { expected: "5.602355" },
|
|
// Multi-group asymmetry fit: two forward,backward pairs from run 2452 are
|
|
// fitted as separate datasets through their own RUN blocks (LRBF "1,2/3,4").
|
|
multiasym: { expected: "1.025906" }
|
|
};
|
|
|
|
// Global fit: runs 2452-2460, template expanded by msr2data, then musrfit.
|
|
const GLOBAL_RUNS = [];
|
|
for (let r = 2452; r <= 2460; r++) GLOBAL_RUNS.push(r);
|
|
const SERIES_EXT = "_tmpl";
|
|
const GLOBAL_TEMPLATE_NAME = "2452" + SERIES_EXT;
|
|
const GLOBAL_EXPANDED_NAME = "2452+global" + SERIES_EXT;
|
|
const GLOBAL_EXPECTED_CHISQ_NDF = "1.034138";
|
|
|
|
// Series fit: runs 2452-2454, per-run MSRs generated by msr2data, fitted
|
|
// individually, then parameters collected back.
|
|
const SERIES_RUNS = [2452, 2453, 2454];
|
|
const SERIES_SUFFIX = "_tmpl";
|
|
|
|
function loadLibrary() {
|
|
const code = fs.readFileSync(path.join(repoRoot, "muSRFitlib.js"), "utf8");
|
|
const sandbox = {
|
|
console: { log: function() {}, error: console.error, warn: console.warn },
|
|
window: { MSREditor: {}, MusrfitJob: {} },
|
|
alert: function(m) { throw new Error("Unexpected alert: " + m); }
|
|
};
|
|
sandbox.global = sandbox;
|
|
vm.runInNewContext(code, sandbox, { filename: "muSRFitlib.js" });
|
|
return sandbox;
|
|
}
|
|
|
|
function runCommand(workDir, exe, args) {
|
|
return childProcess.execFileSync(exe, args, {
|
|
cwd: workDir,
|
|
encoding: "utf8",
|
|
stdio: ["ignore", "pipe", "pipe"],
|
|
timeout: 300000
|
|
});
|
|
}
|
|
|
|
function stageFixture(name) {
|
|
const workDir = fs.mkdtempSync(path.join(os.tmpdir(), "musrflow-integ-"));
|
|
const source = fs.readFileSync(path.join(fixturesDir, name + ".generated.msr"), "utf8");
|
|
const staged = source.split(LEM24_SRC).join(LEM26_DATA);
|
|
fs.writeFileSync(path.join(workDir, name + ".msr"), staged, "utf8");
|
|
return workDir;
|
|
}
|
|
|
|
const lib = loadLibrary();
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Single-run fits
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function checkSingleRun(name) {
|
|
const workDir = stageFixture(name);
|
|
try {
|
|
runCommand(workDir, MUSRFIT, ["-e", name + ".msr"]);
|
|
const fitted = fs.readFileSync(path.join(workDir, name + ".msr"), "utf8");
|
|
const stat = lib.parseMSRFitStatistic(fitted);
|
|
assert.strictEqual(stat.found, true, name + ": STATISTIC block not found");
|
|
assert.strictEqual(stat.converged, true, name + ": fit did not converge");
|
|
assert.strictEqual(stat.kind, "chisq", name + ": expected a chisq statistic");
|
|
assert.strictEqual(
|
|
stat.value, singleRunWorkflows[name].expected,
|
|
name + ": converged chisq/NDF mismatch (got " + stat.value + ")"
|
|
);
|
|
return name + ": chisq/NDF " + stat.value + " OK";
|
|
} finally {
|
|
fs.rmSync(workDir, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
function checkFittedFixture(name) {
|
|
const fitted = fs.readFileSync(path.join(fixturesDir, name), "utf8");
|
|
const stat = lib.parseMSRFitStatistic(fitted);
|
|
assert.strictEqual(stat.found, true, name + ": no STATISTIC block");
|
|
assert.strictEqual(stat.converged, true, name + ": not converged");
|
|
return name + ": " + (stat.kind === "chisq" ? "chisq/NDF " : "maxLH/NDF ") + stat.value + " OK";
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Global fit: msr2data expands a template into one MSR with RUN blocks for
|
|
// every run in the list, then musrfit fits the combined file.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function testGlobalFit() {
|
|
const workDir = fs.mkdtempSync(path.join(os.tmpdir(), "musrflow-integ-global-"));
|
|
try {
|
|
// Stage the template. The fixture references lem24 data; rewrite to
|
|
// the on-disk lem26 fake data root so musrfit can open the files.
|
|
const source = fs.readFileSync(
|
|
path.join(fixturesDir, "global.generated.msr"), "utf8"
|
|
);
|
|
const staged = source.split(LEM24_SRC).join(LEM26_DATA);
|
|
fs.writeFileSync(path.join(workDir, GLOBAL_TEMPLATE_NAME + ".msr"), staged, "utf8");
|
|
|
|
// msr2data expands the single-RUN template to nine RUN blocks.
|
|
const runArg = "[" + GLOBAL_RUNS[0] + "-" + GLOBAL_RUNS[GLOBAL_RUNS.length - 1] + "]";
|
|
runCommand(workDir, MSR2DATA, [
|
|
runArg, SERIES_EXT,
|
|
"msr-" + String(GLOBAL_RUNS[0]), "global"
|
|
]);
|
|
const expandedPath = path.join(workDir, GLOBAL_EXPANDED_NAME + ".msr");
|
|
assert.strictEqual(fs.existsSync(expandedPath), true, "global expanded MSR not created");
|
|
|
|
const expanded = fs.readFileSync(expandedPath, "utf8");
|
|
const runCount = (expanded.match(/^RUN\s+/gm) || []).length;
|
|
assert.strictEqual(runCount, GLOBAL_RUNS.length,
|
|
"expanded MSR must have " + GLOBAL_RUNS.length + " RUN blocks (got " + runCount + ")");
|
|
|
|
// musrfit fits the expanded global MSR.
|
|
runCommand(workDir, MUSRFIT, ["-t", "-e", GLOBAL_EXPANDED_NAME + ".msr"]);
|
|
const fitted = fs.readFileSync(expandedPath, "utf8");
|
|
const stat = lib.parseMSRFitStatistic(fitted);
|
|
assert.strictEqual(stat.found, true, "global: STATISTIC block not found");
|
|
assert.strictEqual(stat.converged, true, "global: fit did not converge");
|
|
assert.strictEqual(stat.kind, "chisq", "global: expected a chisq statistic");
|
|
assert.strictEqual(stat.value, GLOBAL_EXPECTED_CHISQ_NDF,
|
|
"global: chisq/NDF mismatch (got " + stat.value + ")");
|
|
console.log("global fit: chisq/NDF " + stat.value + " OK");
|
|
} finally {
|
|
fs.rmSync(workDir, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Series fit: msr2data generates per-run MSRs from a template, musrfit fits
|
|
// each run independently, then msr2data collects parameters into a table.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function testSeriesFit() {
|
|
const workDir = fs.mkdtempSync(path.join(os.tmpdir(), "musrflow-integ-series-"));
|
|
try {
|
|
// Stage the template and rewrite data paths.
|
|
const source = fs.readFileSync(
|
|
path.join(fixturesDir, "global.generated.msr"), "utf8"
|
|
);
|
|
const staged = source.split(LEM24_SRC).join(LEM26_DATA);
|
|
fs.writeFileSync(path.join(workDir, GLOBAL_TEMPLATE_NAME + ".msr"), staged, "utf8");
|
|
|
|
// msr2data generates per-run MSRs (does not fit them).
|
|
const runArg = "[" + SERIES_RUNS[0] + "-" + SERIES_RUNS[SERIES_RUNS.length - 1] + "]";
|
|
const firstRun = String(SERIES_RUNS[0]);
|
|
runCommand(workDir, MSR2DATA, [
|
|
runArg, SERIES_EXT,
|
|
"msr-" + firstRun
|
|
]);
|
|
|
|
// Each per-run MSR must exist and reference the correct data file.
|
|
for (const run of SERIES_RUNS) {
|
|
const msrName = run + SERIES_SUFFIX + ".msr";
|
|
const msrPath = path.join(workDir, msrName);
|
|
assert.strictEqual(fs.existsSync(msrPath), true,
|
|
"series: " + msrName + " not generated");
|
|
const msrText = fs.readFileSync(msrPath, "utf8");
|
|
const runPad = String(run).padStart(4, "0");
|
|
assert.match(msrText, new RegExp("lem26_his_" + runPad),
|
|
"series: " + msrName + " references wrong data file");
|
|
}
|
|
|
|
// Fit each per-run MSR.
|
|
const fitResults = [];
|
|
for (const run of SERIES_RUNS) {
|
|
const msrName = run + SERIES_SUFFIX + ".msr";
|
|
runCommand(workDir, MUSRFIT, ["-t", "-e", msrName]);
|
|
const fitted = fs.readFileSync(path.join(workDir, msrName), "utf8");
|
|
const stat = lib.parseMSRFitStatistic(fitted);
|
|
assert.strictEqual(stat.found, true,
|
|
"series run " + run + ": STATISTIC block not found");
|
|
assert.strictEqual(stat.converged, true,
|
|
"series run " + run + ": fit did not converge");
|
|
fitResults.push({ run: run, chisqNdf: stat.value });
|
|
}
|
|
|
|
// msr2data collects parameters from all fitted per-run MSRs.
|
|
const paramName = "series_params.dat";
|
|
runCommand(workDir, MSR2DATA, [
|
|
runArg, SERIES_EXT,
|
|
"new", "data", "-o", paramName
|
|
]);
|
|
const paramPath = path.join(workDir, paramName);
|
|
assert.strictEqual(fs.existsSync(paramPath), true,
|
|
"series: parameter collection file not created");
|
|
const paramText = fs.readFileSync(paramPath, "utf8");
|
|
const dataLines = paramText.split("\n").filter(function(line) {
|
|
return line.trim() !== "" && !line.startsWith("data");
|
|
});
|
|
// Header + one data row per run (column format: header line + data lines).
|
|
assert.ok(dataLines.length >= SERIES_RUNS.length,
|
|
"series: expected at least " + SERIES_RUNS.length
|
|
+ " data rows in parameter file (got " + dataLines.length + ")");
|
|
|
|
for (const result of fitResults) {
|
|
console.log("series run " + result.run + ": chisq/NDF " + result.chisqNdf + " OK");
|
|
}
|
|
console.log("series parameter collection: " + dataLines.length + " rows OK");
|
|
} finally {
|
|
fs.rmSync(workDir, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Gate: skip when tools or data are unavailable.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
const musrfitAvailable = fs.existsSync(MUSRFIT);
|
|
const msr2dataAvailable = fs.existsSync(MSR2DATA);
|
|
const dataAvailable = fs.existsSync(LEM26_DATA + ".root");
|
|
const globalDataAvailable = GLOBAL_RUNS.every(function(r) {
|
|
return fs.existsSync(path.join(FAKE_DATA_ROOT, "lem26_his_" + r + ".root"));
|
|
});
|
|
|
|
if (!musrfitAvailable || !msr2dataAvailable || !dataAvailable || !globalDataAvailable) {
|
|
console.log("integration tests skipped (musrfit: " + musrfitAvailable
|
|
+ ", msr2data: " + msr2dataAvailable
|
|
+ ", single-run data: " + dataAvailable
|
|
+ ", global data: " + globalDataAvailable + ")");
|
|
process.exit(0);
|
|
}
|
|
|
|
// --- Single-run fits ---
|
|
let singleRunResults = Object.keys(singleRunWorkflows).map(checkSingleRun);
|
|
console.log(singleRunResults.join("\n"));
|
|
|
|
let fittedFixtures = fs.readdirSync(fixturesDir).filter(function(f) {
|
|
return /\.fitted\.msr$/.test(f);
|
|
}).map(checkFittedFixture);
|
|
console.log(fittedFixtures.join("\n"));
|
|
|
|
// --- Global fit ---
|
|
testGlobalFit();
|
|
|
|
// --- Series fit ---
|
|
testSeriesFit();
|
|
|
|
console.log("real-musrfit integration tests passed");
|