Fix bug extracting the energy value from the filename when there are too many '_'s

This commit is contained in:
2026-06-14 19:19:06 +02:00
parent acfb13d0f5
commit bef9891db4
+34 -7
View File
@@ -3886,15 +3886,42 @@ function plotProfiles(filenames) {
win.document.close();
var plotDiv=win.document.getElementById("newPlot");
// Extract energies from run files if available
var Es = [];
for (let i=0;i<filenames.length;i++) {
Es[i]=Number(filenames[i].substring(filenames[i].indexOf('_')+2,filenames[i].indexOf('.')));
// Build sortable labels from the trailing scan token in each file name.
// The old code used the first "_" in the full path, which breaks for
// prefixes such as "Au_Cr_Si_3layer_E15000.rge".
let plottedFiles = [];
for (let i = 0; i < filenames.length; i++) {
const filename = Array.isArray(filenames) ? filenames[i] : filenames[i];
const baseName = filename.split(/[\\/]/).pop();
const stem = baseName.replace(/\.rge$/i, '');
const lastUnderscore = stem.lastIndexOf('_');
let legend = stem;
let sortValue = i;
if (lastUnderscore !== -1) {
const scanToken = stem.substring(lastUnderscore + 1);
const energyMatch = scanToken.match(/^E(-?\d+(?:\.\d+)?)$/);
if (energyMatch) {
const energyEv = Number(energyMatch[1]);
legend = 'E=' + (energyEv / 1000) + 'keV';
sortValue = energyEv;
} else {
legend = scanToken;
}
}
plottedFiles.push({
filename: filename,
legend: legend,
sortValue: sortValue,
});
}
plottedFiles.sort((a, b) => a.sortValue - b.sortValue);
// loop on sorted values and plot
for (let i=0;i<Es.length;i++) {
let [cols,data]= readDatFile(filenames[i]);
for (let i = 0; i < plottedFiles.length; i++) {
let [cols,data]= readDatFile(plottedFiles[i].filename);
// convert depth to nm and normalize profile
let depth=data[0];
let nmuons=data[1];
@@ -3904,7 +3931,7 @@ function plotProfiles(filenames) {
depth[i]=depth[i]/10;
nmuons[i]=nmuons[i]/norm;
}
Plot_xy(plotDiv,depth,nmuons,['Depth (nm)','Stopped muons (%/nm)','E='+(Es[i]/1000)+'keV']);
Plot_xy(plotDiv,depth,nmuons,['Depth (nm)','Stopped muons (%/nm)',plottedFiles[i].legend]);
}
}