Add musrview-style average view toggle to web ROOT plots

Make the "a" key and menu action toggle averaged data/Fourier view in the
online JSROOT plot window, matching musrview behavior. Average and FFT
states are tracked independently and requested from musrview via new CGI
avgv support, with per-window caching of spectrum/FFT × averaged views.
Keep "u" for resetting/unzooming the current plot.
This commit is contained in:
Zaher Salman committed 2026-11-18 16:40:00 +01:00
1 parent afba1faf03
commit ff6774a747
2 files changed
+128 -47

No files matched your search

+10 -1
View File
@@ -1188,13 +1188,14 @@ sub perform_series_artifact_collection {
my $runs = $collection->{runs};
my $suffix = defined($collection->{suffix}) ? "$collection->{suffix}" : "";
my $fftv = defined($collection->{fftv}) ? "$collection->{fftv}" : "n";
my $avgv = defined($collection->{avgv}) ? "$collection->{avgv}" : "n";
my $contract_version = defined($collection->{version})
&& !ref($collection->{version}) ? 0 + $collection->{version} : 0;
my $client_combined = exists($collection->{combinedMSR})
&& !ref($collection->{combinedMSR}) ? "$collection->{combinedMSR}" : undef;
return $failure->("invalid-series-collection", "Invalid series collection fields")
if ref($runs) ne "ARRAY" || @{$runs} < 2 || @{$runs} > 500
|| $suffix !~ /^_[A-Za-z0-9._+\-]+$/ || $fftv !~ /^(?:y|n)$/
|| $suffix !~ /^_[A-Za-z0-9._+\-]+$/ || $fftv !~ /^(?:y|n)$/ || $avgv !~ /^(?:y|n)$/
|| $contract_version != 1 || !defined($client_combined);
my @run_numbers = ();
@@ -1330,6 +1331,7 @@ sub perform_series_artifact_collection {
my $view_cmd = "cd " . shell_quote($work_path)
. " && " . shell_quote($MUSRVIEW_BIN) . " --timeout 300 --root"
. ($fftv eq "y" ? " -f" : "")
. ($avgv eq "y" ? " -a" : "")
. " " . shell_quote($combined_name);
my $view_result = run_logged_command($out_path, "musrview", $view_cmd);
push(@warnings, "musrview returned code " . $view_result->{exit_code})
@@ -2217,6 +2219,7 @@ if ($action eq "run") {
respond_json({ ok => JSON::PP::false, code => 400, message => $fit_parameter_error });
}
my $fftv = $in->param("fftv") || "n";
my $avgv = $in->param("avgv") || "n";
my $plot_only = ($in->param("plot_only") || "0") eq "1" ? 1 : 0;
my $plot_refresh = ($in->param("plot_refresh") || "0") eq "1" ? 1 : 0;
@@ -2232,6 +2235,9 @@ if ($action eq "run") {
if ($fftv ne "y" && $fftv ne "n") {
respond_json({ ok => JSON::PP::false, code => 400, message => "Invalid fftv value" });
}
if ($avgv ne "y" && $avgv ne "n") {
respond_json({ ok => JSON::PP::false, code => 400, message => "Invalid avgv value" });
}
if ($plot_refresh && !$plot_only) {
respond_json({ ok => JSON::PP::false, code => 400, message => "plot_refresh requires plot_only" });
}
@@ -2532,6 +2538,9 @@ if ($action eq "run") {
if ($fftv eq "y") {
$view_cmd .= " -f";
}
if ($avgv eq "y") {
$view_cmd .= " -a";
}
# Apache has no interactive display. musrview's graphics option runs in
# batch mode and writes plot files without starting a ROOT GUI session.
$view_cmd .= " " . shell_quote($target_file);
+118 -46
View File
@@ -783,15 +783,65 @@ function showWebRootPlotWindow(rootUrls, jobName = "", plotContext = {}) {
plotWindow.appendChild(plotHeader);
plotWindow.appendChild(plotContainer);
let spectrumSpecs = plotSpecs.slice();
let currentSpecs = spectrumSpecs.slice();
let initialSpecs = plotSpecs.slice();
let currentSpecs = initialSpecs.slice();
let renderVersion = Date.now().toString(36) + "-" + webRootPlotWindowSequence;
let fftInProgress = false;
let rootPlotRequestInProgress = false;
let isFFT = false;
let isAveraged = false;
let rootViewMode = "clean";
let averageActionButton = null;
plotWindow.dataset.plotView = rootViewMode;
// Reused across spectrum redraws so clean/original/reset never re-fetch the
// ROOT file; FFT renders pass no cache and always decode their fresh file.
let rootDecodeCache = new Map();
plotWindow.dataset.plotMode = "spectrum";
plotWindow.dataset.plotAverage = "off";
// Cache ROOT plot specs and decoded objects per (spectrum/FFT × average) view
// so toggles can restore previously fetched views without another server round-trip.
let rootSpecCache = { "spectrum:n": initialSpecs.slice() };
let rootDecodeCaches = { "spectrum:n": new Map() };
function rootViewKey(fft, averaged) {
return (fft ? "fft" : "spectrum") + ":" + (averaged ? "y" : "n");
}
function currentDecodeCache() {
let key = rootViewKey(isFFT, isAveraged);
if (!rootDecodeCaches[key]) {
rootDecodeCaches[key] = new Map();
}
return rootDecodeCaches[key];
}
function updateAverageActionState() {
if (!averageActionButton) {
return;
}
averageActionButton.textContent = isAveraged ? "Average view (on)" : "Average view";
averageActionButton.setAttribute("aria-pressed", isAveraged ? "true" : "false");
averageActionButton.title = isAveraged
? "Show non-averaged data/Fourier view (a)"
: "Show averaged data/Fourier view (a)";
}
function applyRootPlotView(fft, averaged, specs, statusMessage) {
let key = rootViewKey(fft, averaged);
isFFT = fft;
isAveraged = averaged;
currentSpecs = specs.slice();
plotWindow.dataset.plotMode = fft ? "fft" : "spectrum";
plotWindow.dataset.plotAverage = averaged ? "on" : "off";
if (!rootDecodeCaches[key]) {
rootDecodeCaches[key] = new Map();
}
let versionTag = (fft ? "fft" : "spectrum") + (averaged ? "-avg" : "")
+ "-" + webRootPlotWindowSequence;
let version = Date.now().toString(36) + "-" + versionTag;
renderMusrfitRootPlots(plotContainer, currentSpecs, version, rootViewMode, rootDecodeCaches[key]);
updateAverageActionState();
if (statusMessage && typeof updateJobStatus === "function") {
updateJobStatus(statusMessage);
}
}
// Redrawing the same ROOT object is the most robust unzoom/reset operation:
// it avoids depending on JSROOT painter internals while preserving the file.
@@ -800,42 +850,41 @@ function showWebRootPlotWindow(rootUrls, jobName = "", plotContext = {}) {
return;
}
let resetVersion = Date.now().toString(36) + "-reset-" + webRootPlotWindowSequence;
renderMusrfitRootPlots(plotContainer, currentSpecs, resetVersion, rootViewMode, rootDecodeCache);
renderMusrfitRootPlots(plotContainer, currentSpecs, resetVersion, rootViewMode, currentDecodeCache());
if (statusMessage && typeof updateJobStatus === "function") {
updateJobStatus({ message: statusMessage });
}
}
// The FFT view is generated by musrview on the server, not by JSROOT in the
// browser. The floating window simply swaps to the newly returned ROOT file.
async function toggleMusrfitRootFFT() {
if (!plotContext.msrPath || fftInProgress) {
// Spectrum/FFT and averaged/non-averaged views are generated by musrview on
// the server, like the existing FFT toggle. Cache fetched specs per view.
async function setRootPlotView(fft, averaged) {
if (rootPlotRequestInProgress || (fft === isFFT && averaged === isAveraged)) {
return;
}
if (plotWindow.dataset.plotMode === "fft") {
let restoreVersion = Date.now().toString(36) + "-restore-" + webRootPlotWindowSequence;
currentSpecs = spectrumSpecs.slice();
renderMusrfitRootPlots(plotContainer, currentSpecs, restoreVersion, rootViewMode, rootDecodeCache);
plotWindow.dataset.plotMode = "spectrum";
if (typeof updateJobStatus === "function") {
updateJobStatus({
message: "Original ROOT plot restored",
plotPath: spectrumSpecs[0].url
});
}
let key = rootViewKey(fft, averaged);
if (rootSpecCache[key]) {
let message = fft
? (averaged ? "Averaged FFT ROOT plot restored" : "FFT ROOT plot restored")
: (averaged ? "Averaged ROOT plot restored" : "Original ROOT plot restored");
applyRootPlotView(fft, averaged, rootSpecCache[key], {
message: message,
plotPath: rootSpecCache[key][0].url
});
return;
}
if (typeof postToMusrfitCGI !== "function") {
if (!plotContext.msrPath || typeof postToMusrfitCGI !== "function") {
return;
}
fftInProgress = true;
rootPlotRequestInProgress = true;
let requestLabel = (averaged ? "averaged " : "") + (fft ? "FFT " : "") + "ROOT plot";
if (typeof updateJobStatus === "function") {
updateJobStatus({
state: "Running musrview FFT",
message: "Generating FFT ROOT plot",
state: "Running musrview",
message: "Generating " + requestLabel,
finishedAt: "",
exitCode: ""
});
@@ -848,21 +897,22 @@ function showWebRootPlotWindow(rootUrls, jobName = "", plotContext = {}) {
runs: plotContext.runs || "",
template_name: "",
base_run: "",
fftv: "y",
fftv: fft ? "y" : "n",
avgv: averaged ? "y" : "n",
plot_only: 1,
plot_refresh: 1
});
let response = request.payload;
let fftUrls = musrfitRootUrlsFromResponse(response);
let fftSpecs = normalizeMusrfitRootPlotSpecs(fftUrls,
let rootUrls = musrfitRootUrlsFromResponse(response);
let specs = normalizeMusrfitRootPlotSpecs(rootUrls,
response && Array.isArray(response.root_items) ? response.root_items : []);
if (response && response.command_log && typeof appendJobLog === "function") {
appendJobLog(response.command_log.trim());
}
if (!request.ok || !response || !response.ok || !fftSpecs.length) {
let errorMessage = request.error || (response ? response.message : "FFT request failed");
if (!request.ok || !response || !response.ok || !specs.length) {
let errorMessage = request.error || (response ? response.message : "Plot request failed");
if (response && response.stderr) {
errorMessage += "\n" + response.stderr.trim();
}
@@ -870,25 +920,35 @@ function showWebRootPlotWindow(rootUrls, jobName = "", plotContext = {}) {
finishJobStatus(0, response && response.code !== undefined ? response.code : 1,
errorMessage);
}
fftInProgress = false;
rootPlotRequestInProgress = false;
return;
}
let fftVersion = Date.now().toString(36) + "-fft-" + webRootPlotWindowSequence;
currentSpecs = fftSpecs.slice();
renderMusrfitRootPlots(plotContainer, currentSpecs, fftVersion, rootViewMode);
plotWindow.dataset.plotMode = "fft";
if (typeof updateJobStatus === "function") {
updateJobStatus({ plotPath: fftSpecs[0].url });
}
rootSpecCache[key] = specs.slice();
rootDecodeCaches[key] = new Map();
applyRootPlotView(fft, averaged, rootSpecCache[key], {
plotPath: specs[0].url
});
if (typeof finishJobStatus === "function") {
finishJobStatus(1, response.code !== undefined ? response.code : 0,
response.message || "FFT ROOT plot completed");
response.message || (requestLabel.charAt(0).toUpperCase() + requestLabel.slice(1) + " completed"));
}
fftInProgress = false;
rootPlotRequestInProgress = false;
}
renderMusrfitRootPlots(plotContainer, spectrumSpecs, renderVersion, rootViewMode, rootDecodeCache);
async function toggleMusrfitRootFFT() {
await setRootPlotView(!isFFT, isAveraged);
}
async function toggleMusrfitRootAverage() {
if (!plotContext.msrPath) {
return;
}
await setRootPlotView(isFFT, !isAveraged);
}
renderMusrfitRootPlots(plotContainer, initialSpecs, renderVersion, rootViewMode, currentDecodeCache());
updateAverageActionState();
// Build one action, e.g. a menu button, for a plot action. Toggle/stateful
// actions report their current result through updateJobStatus.
@@ -912,12 +972,18 @@ function showWebRootPlotWindow(rootUrls, jobName = "", plotContext = {}) {
let fftActionButton = null;
if (plotContext.msrPath) {
// FFT is server-side musrview work, so it is only offered when the plot
// knows the .msr behind it.
fftActionButton = rootPlotActionButton("FFT", "Toggle Fourier power spectrum", function() {
// FFT and average views are server-side musrview work, so they are only
// offered when the plot knows the .msr behind it.
fftActionButton = rootPlotActionButton("FFT", "Toggle Fourier power spectrum (f)", function() {
toggleMusrfitRootFFT();
});
plotActionMenu.appendChild(fftActionButton);
averageActionButton = rootPlotActionButton("Average view", "Show averaged data/Fourier view (a)", function() {
toggleMusrfitRootAverage();
});
averageActionButton.setAttribute("aria-pressed", "false");
plotActionMenu.appendChild(averageActionButton);
}
plotActionMenu.appendChild(rootPlotActionButton(
"Clean / Original view",
@@ -931,11 +997,12 @@ function showWebRootPlotWindow(rootUrls, jobName = "", plotContext = {}) {
));
plotActionMenu.appendChild(rootPlotActionButton(
"Reset view",
"redraw the current plot to undo pan and zoom",
"redraw the current plot to undo pan and zoom (u)",
function() {
rerenderCurrentRootPlots("ROOT plot view reset");
}
));
updateAverageActionState();
function toggleRootPlotActionMenu(forceOpen) {
let open = forceOpen !== undefined ? Boolean(forceOpen) : plotActionMenu.hidden;
@@ -982,6 +1049,11 @@ function showWebRootPlotWindow(rootUrls, jobName = "", plotContext = {}) {
plotWindow.dataset.plotView = rootViewMode;
rerenderCurrentRootPlots(rootViewMode === "clean"
? "Clean ROOT plot view" : "Original ROOT plot view");
} else if (shortcut === "a") {
if (plotContext.msrPath) {
event.preventDefault();
toggleMusrfitRootAverage();
}
} else if (shortcut === "u") {
event.preventDefault();
rerenderCurrentRootPlots("ROOT plot view reset");