diff --git a/cgi/single_musrfit.cgi b/cgi/single_musrfit.cgi index a601e53..e73c57b 100755 --- a/cgi/single_musrfit.cgi +++ b/cgi/single_musrfit.cgi @@ -1040,8 +1040,14 @@ sub series_msr_blocks { $start =~ s/^\s+//; $start =~ s/\s+$//; $section = $block; - $blocks{$block} = { header => $start, lines => [] } - if !exists($blocks{$block}); + if (!exists($blocks{$block})) { + $blocks{$block} = { header => $start, lines => [] }; + } elsif ($block eq "RUN") { + # RUN is repeatable. Preserve later detector headers inside the + # first collected RUN section so series_msr_run_blocks can + # reconstruct every histogram block. + push(@{$blocks{$block}{lines}}, $start); + } } elsif ($section ne "") { push(@{$blocks{$section}{lines}}, $line); } else { diff --git a/muSRFitlib.js b/muSRFitlib.js index 7dec903..7e2b0e2 100644 --- a/muSRFitlib.js +++ b/muSRFitlib.js @@ -4507,7 +4507,17 @@ function buildSeriesGlobalMSR(msrTexts, runs) { let sections = splitSeriesMSRBlocks(msrTexts[index]); let byName = {}; for (let section of sections) { - if (!byName[section.name]) byName[section.name] = section; + if (!byName[section.name]) { + byName[section.name] = section; + } else if (section.name === "RUN") { + // RUN is the one repeatable top-level MSR block. Keep subsequent + // headers inside the first collected section so the normal RUN + // parser can reconstruct every detector block. Treating RUN like + // the singleton blocks silently discarded all but the first + // detector of a SingleHist source. + byName.RUN.lines.push(section.header); + byName.RUN.lines.push.apply(byName.RUN.lines, section.lines); + } } if (!byName.FITPARAMETER || !byName.THEORY || !byName.RUN) { return fail("series-merge-source-incomplete"); diff --git a/tests/cgi-series-collection.test.js b/tests/cgi-series-collection.test.js index 7d04bfd..3e903bf 100644 --- a/tests/cgi-series-collection.test.js +++ b/tests/cgi-series-collection.test.js @@ -38,6 +38,36 @@ function fittedMSR(run) { ].join("\n"); } +function fittedSingleHistMSR(run) { + return [ + "FITPARAMETER", + "1 No1_" + run + " 1000 1 none", + "2 Bg1_" + run + " 1 0.1 none", + "3 No2_" + run + " 1100 1 none", + "4 Bg2_" + run + " 2 0.1 none", + "5 Lam_" + run + " 0.4 0.01 none", + "THEORY", + "simplExpo 5", + "GLOBAL", + "fittype 0", + "RUN /data/run" + run + " MUE4 PSI MUSR-ROOT", + "forward 21 25", + "norm 1", + "backgr.fit 2", + "RUN /data/run" + run + " MUE4 PSI MUSR-ROOT", + "forward 23 27", + "norm 3", + "backgr.fit 4", + "COMMANDS", + "MIGRAD", + "PLOT 0", + "runs 1 2", + "STATISTIC --- 2026-08-30 12:00:00", + "chisq = 100, NDF = 98, chisq/NDF = 1.020408", + "" + ].join("\n"); +} + try { const webRoot = path.join(testRoot, "web"); const tmpRoot = path.join(webRoot, "tmp"); @@ -137,6 +167,45 @@ try { assert.match(response.command_log, /msr2data independent series collection/); assert.doesNotMatch(response.command_log, /msr2data independent series display/); assert.match(response.command_log, /\[exit code 9\]/); + + // Repeat the same server path with two detector RUN blocks per physical + // run. The web/Perl merger must preserve all four blocks just like the + // shared JavaScript implementation. + fs.writeFileSync(path.join(workPath, "514_asy.msr"), fittedSingleHistMSR(514)); + fs.writeFileSync(path.join(workPath, "515_asy.msr"), fittedSingleHistMSR(515)); + const singleHistExecution = childProcess.spawnSync( + "perl", [path.join(repoRoot, "cgi", "single_musrfit.cgi")], { + cwd: repoRoot, + encoding: "utf8", + input: body, + env: Object.assign({}, process.env, { + REQUEST_METHOD: "POST", + PERL5LIB: perlLibraryPath, + CONTENT_TYPE: "application/x-www-form-urlencoded", + CONTENT_LENGTH: String(Buffer.byteLength(body)), + MUSRFIT_WEBROOT: webRoot, + MUSRFIT_TMPROOT: tmpRoot, + MUSRFIT_TMP_URL: "/tmp", + MSR2DATA_BIN: fakeMsr2data, + MUSRVIEW_BIN: fakeMusrview, + MUSRFIT_RUNTIME_HOME: tmpRoot, + MUSRFLOW_TEST_M2D_ARGS: m2dArgsPath, + MUSRFLOW_TEST_VIEW_ARGS: viewArgsPath + }) + } + ); + assert.strictEqual(singleHistExecution.status, 0, singleHistExecution.stderr); + const singleHistResponse = JSON.parse( + singleHistExecution.stdout.split(/\r?\n\r?\n/).slice(1).join("\n\n") + ); + assert.strictEqual(singleHistResponse.ok, true); + const singleHistCombined = fs.readFileSync( + path.join(workPath, "514+global_asy.msr"), "utf8" + ); + assert.strictEqual((singleHistCombined.match(/^RUN\b/gm) || []).length, 4); + assert.strictEqual((singleHistCombined.match(/^forward 21 25$/gm) || []).length, 2); + assert.strictEqual((singleHistCombined.match(/^forward 23 27$/gm) || []).length, 2); + assert.match(singleHistCombined, /^runs\s+1\s+2\s+3\s+4$/m); } finally { fs.rmSync(testRoot, { recursive: true, force: true }); } diff --git a/tests/series-global-merge.test.js b/tests/series-global-merge.test.js index 1e16baf..adfeed7 100644 --- a/tests/series-global-merge.test.js +++ b/tests/series-global-merge.test.js @@ -54,6 +54,37 @@ function stagedAsymmetryMSR(run, value) { ].join("\n"); } +function stagedSingleHistMSR(run) { + const suffix = String(run).padStart(4, "0"); + return [ + "FITPARAMETER", + "1 No1_" + suffix + " 1000 1 none", + "2 Bg1_" + suffix + " 1 0.1 none", + "3 No2_" + suffix + " 1100 1 none", + "4 Bg2_" + suffix + " 2 0.1 none", + "5 Lam_" + suffix + " 0.4 0.01 none", + "THEORY", + "simplExpo 5", + "GLOBAL", + "fittype 0", + "RUN " + lemToken(run) + " MUE4 PSI MUSR-ROOT", + "forward 21 25", + "norm 1", + "backgr.fit 2", + "RUN " + lemToken(run) + " MUE4 PSI MUSR-ROOT", + "forward 23 27", + "norm 3", + "backgr.fit 4", + "COMMANDS", + "MIGRAD", + "PLOT 0", + "runs 1 2", + "STATISTIC --- 2026-08-30 12:00:00", + "chisq = 100, NDF = 98, chisq/NDF = 1.020408", + "" + ].join("\n"); +} + const sanctuaryCase = function(name, assertion) { try { assertion(); @@ -158,6 +189,29 @@ function testMergesSharedAndRunSpecificParameters(lib) { }); } +function testPreservesEverySingleHistRunBlock(lib) { + const result = lib.buildSeriesGlobalMSR( + [stagedSingleHistMSR(514), stagedSingleHistMSR(515)], [514, 515] + ); + assert.strictEqual(result.ok, true); + const normalized = lib.normalizeMSRPlotRuns(result.text, {}, [514, 515]); + + sanctuaryCase("all detector RUN blocks are preserved", function() { + assert.strictEqual((normalized.match(/^RUN\b/gm) || []).length, 4); + assert.strictEqual((normalized.match(/^forward 21 25$/gm) || []).length, 2); + assert.strictEqual((normalized.match(/^forward 23 27$/gm) || []).length, 2); + assert.match(normalized, /^runs\s+1\s+2\s+3\s+4$/m); + }); + sanctuaryCase("each detector keeps its normalization and physical-run map", function() { + assert.strictEqual((normalized.match(/^norm\s+1$/gm) || []).length, 1); + assert.strictEqual((normalized.match(/^norm\s+3$/gm) || []).length, 1); + assert.strictEqual((normalized.match(/^norm\s+6$/gm) || []).length, 1); + assert.strictEqual((normalized.match(/^norm\s+8$/gm) || []).length, 1); + assert.strictEqual((normalized.match(/^map\s+5$/gm) || []).length, 2); + assert.strictEqual((normalized.match(/^map\s+10$/gm) || []).length, 2); + }); +} + function testFourierForDisplay(lib) { const make = function(run, plotLine) { const suffix = String(run).padStart(4, "0"); @@ -308,10 +362,11 @@ function testMergingErrors(lib) { const lib = loadLibrary(); testMergesTwoAsymmetryRuns(lib); testMergesSharedAndRunSpecificParameters(lib); + testPreservesEverySingleHistRunBlock(lib); testFourierForDisplay(lib); testMergingErrors(lib); console.log("series global merge tests passed"); })().catch(function(error) { console.error(error); process.exit(1); -}); \ No newline at end of file +});