Skip to content
45 changes: 28 additions & 17 deletions DisplayBeamProfiles.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,25 @@
% USER's input data
% -------------------------------------------------------------------------
kPath="P:\Accelerating-System\Accelerator-data";
myTit="Check fibre - Protoni, 320mm";
monTypes=[ "QPP" "QPP" "SFP" "SFP" ]; % CAM, DDS, GIM, QPP/SFH/SFM/SFP - QBM/PMM/PIB to come
MonPaths=[...
strcat(kPath,"\scambio\Alessio\2023-02-05_check_cablaggi_SFP\HE-007A-CEB\HE_010B_QPP\HOR\PRC-544-230205-0646\")
strcat(kPath,"\scambio\Alessio\2023-02-05_check_cablaggi_SFP\HE-007A-CEB\HE_010B_QPP\HOR\PRC-544-230205-0729\")
strcat(kPath,"\scambio\Alessio\2023-02-05_check_cablaggi_SFP\HE-007A-CEB\HE_012B_SFP\HOR\PRC-544-230205-0742\")
strcat(kPath,"\scambio\Alessio\2023-02-05_check_cablaggi_SFP\HE-007A-CEB\HE_012B_SFP\HOR\PRC-544-230205-0747\")
];
monTypes=[ "GIM" ]; % CAM, DDS, GIM, QPP/SFH/SFM/SFP - QBM/PMM/PIB to come
myLabels=[...
"HE-010B-QPP - prima di invertire i cavi"
"HE-010B-QPP - dopo aver invertito i cavi"
"HE-012B-SFP - prima di invertire i cavi"
"HE-012B-SFP - dopo aver invertito i cavi"
"H2-009B-GIM"
];
lSkip=false; % DDS summary file: skip first 2 lines (in addition to header line)
myFigPath=".";
% part-dependent stuff
% - protoni
% myFigName="summary_protoni_GIM_2023-05-09.10";
% myTit="summary 2023-05-09.10 - Protoni";
% MonPaths=[...
% strcat(kPath,"\Area dati MD\00Summary\Protoni\2023\Maggio\2023.05.09-10\Steering ridotti\GIM\PRC-544-230511-0147_H2-009B-GIM_AllTrig\")
% ];
% - carbonio
myFigName="summary_carbonio_GIM_2023-05-09.10";
myTit="summary 2023-05-09.10 - Carbonio";
MonPaths=[...
strcat(kPath,"\Area dati MD\00Summary\Carbonio\2023\Maggio\2023.05.09-10\Steering ridotti\GIM\PRC-544-230511-0028_H2-009B-GIM_AllTrig\")
];
end

%% check of user input data
Expand Down Expand Up @@ -148,9 +152,17 @@
end
end
% - 3D plot of profiles
ShowSpectra(profiles,sprintf("%s - 3D profiles",myTit),addIndex,addLabel,myLabels,strcat(myFigPath,"\3Dprofiles_",myFigName,".fig"));
if (exist("myFigName","var") & ~ismissing(myFigName))
ShowSpectra(profiles,sprintf("%s - 3D profiles",myTit),addIndex,addLabel,myLabels,strcat(myFigPath,"\3Dprofiles_",myFigName,".fig"));
else
ShowSpectra(profiles,sprintf("%s - 3D profiles",myTit),addIndex,addLabel,myLabels);
end
% - statistics on profiles
ShowBeamProfilesSummaryData(BARsProf,FWHMsProf,INTsProf,missing(),addIndex,addLabel,myLabels,missing(),myTit,strcat(myFigPath,"\Stats_",myFigName,".fig"));
if (exist("myFigName","var") & ~ismissing(myFigName))
ShowBeamProfilesSummaryData(BARsProf,FWHMsProf,INTsProf,missing(),addIndex,addLabel,myLabels,missing(),myTit,strcat(myFigPath,"\Stats_",myFigName,".fig"));
else
ShowBeamProfilesSummaryData(BARsProf,FWHMsProf,INTsProf,missing(),addIndex,addLabel,myLabels,missing(),myTit);
end
% - statistics on profiles vs summary files
for iDataAcq=1:nDataSets
switch upper(monTypes(iDataAcq))
Expand All @@ -159,9 +171,8 @@
CompBars=BARsSumm(:,:,iDataAcq); CompBars(:,:,2)=BARsProf(:,:,iDataAcq);
CompFwhms=FWHMsSumm(:,:,iDataAcq); CompFwhms(:,:,2)=FWHMsProf(:,:,iDataAcq);
CompInts=INTsSumm(:,:,iDataAcq); CompInts(:,:,2)=INTsProf(:,:,iDataAcq);
% CompXs=mmsSumm(:,iDataAcq);
CompXs=(1:size(BARsSumm,1))';
CompXs(:,2)=addIndex(:,iDataAcq);
% CompXs=mmsSumm(:,iDataAcq); CompXs(:,2)=mmsProf(:,iDataAcq);
CompXs=(1:size(BARsSumm,1))'; CompXs(:,2)=addIndex(:,iDataAcq);
ShowBeamProfilesSummaryData(CompBars,CompFwhms,CompInts,missing(),CompXs,addLabel,...
["summary data" "stat on profiles"],missing(),sprintf("%s - %s - summary vs profile stats",myTit,myLabels(iDataAcq)));
end
Expand Down
4 changes: 2 additions & 2 deletions MADX-optics/CompareOptics.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ function CompareOptics(optics,labels,geometry,whats,myTitle,emig,sigdpp)
% - H plane
axs{2,1}=subplot(3,nCols,2);
PlotOptics(optics(:,:),usrWhats(1),emigUsr,sigdppUsr);
legend(labels,"Location","best");
legend(labels,"Location","best","NumColumns",ceil(nOpts/10.));
grid on;
% - V plane
axs{3,1}=subplot(3,nCols,3);
PlotOptics(optics(:,:),usrWhats(2),emigUsr,sigdppUsr);
legend(labels,"Location","best");
legend(labels,"Location","best","NumColumns",ceil(nOpts/10.));
grid on;
% - miscellanea
linkaxes([axs{:,1}],'x');
Expand Down
158 changes: 106 additions & 52 deletions MADX-optics/GetColumnsAndMappingTFS.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function [ colNames, colUnits, colFacts, mapping, readFormat ] = ...
GetColumnsAndMappingTFS(whichTFS)
GetColumnsAndMappingTFS(whichTFS,genBy)
% GetColumnsAndMappingTFS return column names, units and column
% mapping of twiss tables
%
Expand All @@ -8,9 +8,12 @@
%
% input arguments:
% whichData: format of the table:
% 'optics': optics table by MAD-X;
% 'geometry': lattice geometry by MAD-X;
% 'rmatrix': (selected elements of) response matrix by MAD-X;
% 'OPTICS': optics table by MAD-X;
% 'GEOMETRY': lattice geometry by MAD-X;
% 'RMATRIX': (selected elements of) response matrix by MAD-X;
% genBy: format of the table:
% 'TWISS': a TFS table as generated by a MADX TWISS command;
% 'SCAN': a TFS table as generated by a current scan;
%
% output arguments:
% colNames: array with name of columns/variables;
Expand All @@ -19,64 +22,115 @@
% mapping: on which column a given variable is found;
% readFormat: format string necessary to correctly parse the file;
%
% This function states that the optics and geometry TFS tables have
% specific formats:
% - TFS table for optics:
% This function states that the optics, geometry and Rmatrix TFS tables
% have specific formats:
% - generated by a MADX TWISS command (genBy=="TWISS")
% . optics:
% NAME, KEYWORD, L, S, BETX, ALFX, BETY, ALFY, X, PX, Y, PY,
% DX, DPX, DY, DPY, MUX, MUY;
% - TFS table for geometry:
% . geometry:
% NAME, KEYWORD, L, S, KICK, HKICK, VKICK, ANGLE, K0L, K1L, K2L,
% APERTYPE, APER_1, APER_2, APER_3, APER_4, APOFF_1, APOFF_2;
% - TFS table for response matrix:
% . response matrix:
% NAME, KEYWORD, L, S, RE11, RE12, RE21, RE22, RE16, RE26, RE33, RE34,
% RE43, RE44, RE36, RE46, RE51, RE52, RE55, RE56, RE66;
%
% - generated by a scan (genBy=="SCAN")
% . optics:
% Brho[Tm], BP[mm], myID[], BETX, ALFX, BETY, ALFY, X, PX, Y, PY,
% DX, DPX, DY, DPY, MUX, MUY;
% . geometry:
% Brho[Tm], BP[mm], myID[], KICK, HKICK, VKICK, ANGLE, K0L, K1L, K2L,
% APERTYPE, APER_1, APER_2, APER_3, APER_4, APOFF_1, APOFF_2;
% . response matrix:
% Brho[Tm], BP[mm], myID[], RE11, RE12, RE21, RE22, RE16, RE26, RE33, RE34,
% RE43, RE44, RE36, RE46, RE51, RE52, RE55, RE56, RE66;
%
% See also ParseTfsTable.
if (~exist("genBy","var") | ismissing(genBy)), genBy="TWISS"; end % default genBy

% from label to data column
switch lower(whichTFS)
case {'opt','optics'}
colNames=[ "NAME" "KEYWORD" "L" "S" "BETX" "ALFX" "BETY" "ALFY" ...
"X" "PX" "Y" "PY" "DX" "DPX" "DY" "DPY" ...
"MUX" "MUY" ];
colUnits=[ "" "" "m" "m" "m" "" "m" "" ...
"m" "" "m" "" "m" "" "m" "" ...
"2\pi" "2\pi"];
mapping =[ 1 2 3 4 5 6 7 8 ...
9 10 11 12 13 14 15 16 ...
17 18 ];
colFacts = ones(1,length(colNames));
readFormat = '%s %s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f';
case {'geo','geometry'}
colNames=[ "NAME" "KEYWORD" "L" "S" ...
"KICK" "HKICK" "VKICK" "ANGLE" "K0L" "K1L" "K2L" ...
"APERTYPE" "APER_1" "APER_2" "APER_3" "APER_4" "APOFF_1" "APOFF_2" ];
colUnits=[ "" "" "m" "m" ...
"rad" "rad" "rad" "rad" "rad" "rad m^{-1}" "rad m^{-2}" ...
"" "m" "m" "m" "m" "m" "m" ];
mapping =[ 1 2 3 4 ...
5 6 7 8 9 10 11 ...
12 13 14 15 16 17 18 ];
colFacts = ones(1,length(colNames));
readFormat = '%s %s %f %f %f %f %f %f %f %f %f %s %f %f %f %f %f %f %f';
case {'rm','rmatrix'}
colNames=[ "NAME" "KEYWORD" "L" "S" ...
"RE11" "RE12" "RE21" "RE22" "RE16" "RE26" ...
"RE33" "RE34" "RE43" "RE44" "RE36" "RE46" ...
"RE51" "RE52" "RE55" "RE56" "RE66" ];
colUnits=[ "" "" "m" "m" ...
"" "m rad^{-1}" "rad m^{-1}" "" "m" "rad" ...
"" "m rad^{-1}" "rad m^{-1}" "" "m" "rad" ...
"s m^{-1}" "s rad^{-1}" "" "s" "m" ];
mapping =[ 1 2 3 4 ...
5 6 7 8 9 10 ...
11 12 13 14 15 16 ...
17 18 19 20 21 ];
colFacts = ones(1,length(colNames));
readFormat = '%s %s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f';
switch upper(genBy)
case "TWISS"
switch upper(whichTFS)
case {'OPT','OPTICS'}
colNames=[ "NAME" "KEYWORD" "L" "S" "BETX" "ALFX" "BETY" "ALFY" ...
"X" "PX" "Y" "PY" "DX" "DPX" "DY" "DPY" ...
"MUX" "MUY" ];
colUnits=[ "" "" "m" "m" "m" "" "m" "" ...
"m" "" "m" "" "m" "" "m" "" ...
"2\pi" "2\pi"];
readFormat = '%s %s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f';
case {'GEO','GEOMETRY'}
colNames=[ "NAME" "KEYWORD" "L" "S" ...
"KICK" "HKICK" "VKICK" "ANGLE" "K0L" "K1L" "K2L" ...
"APERTYPE" "APER_1" "APER_2" "APER_3" "APER_4" "APOFF_1" "APOFF_2" ];
colUnits=[ "" "" "m" "m" ...
"rad" "rad" "rad" "rad" "rad" "rad m^{-1}" "rad m^{-2}" ...
"" "m" "m" "m" "m" "m" "m" ];
readFormat = '%s %s %f %f %f %f %f %f %f %f %f %s %f %f %f %f %f %f %f';
case {'RM','RMATRIX'}
colNames=[ "NAME" "KEYWORD" "L" "S" ...
"RE11" "RE12" "RE21" "RE22" "RE16" "RE26" ...
"RE33" "RE34" "RE43" "RE44" "RE36" "RE46" ...
"RE51" "RE52" "RE55" "RE56" "RE66" ];
colUnits=[ "" "" "m" "m" ...
"" "m rad^{-1}" "rad m^{-1}" "" "m" "rad" ...
"" "m rad^{-1}" "rad m^{-1}" "" "m" "rad" ...
"s m^{-1}" "s rad^{-1}" "" "s" "m" ];
readFormat = '%s %s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f';
otherwise
error('which column mapping for TFS table?');
end
case "SCAN"
mySep=',';
switch upper(whichTFS)
case {'OPT','OPTICS'}
colNames=[ "BRHO" "BP" "ID" ...
"BETX" "ALFX" "BETY" "ALFY" ...
"X" "PX" "Y" "PY" ...
"DX" "DPX" "DY" "DPY" ...
"MUX" "MUY" ];
colUnits=[ "Tm" "mm" "" ...
"m" "" "m" "" ...
"m" "" "m" "" ...
"m" "" "m" "" ...
"2\pi" "2\pi"];
readFormat = strcat('%f',mySep,'%f',mySep,'%f',mySep, ...
'%f',mySep,'%f',mySep,'%f',mySep,'%f',mySep, ...
'%f',mySep,'%f',mySep,'%f',mySep,'%f',mySep, ...
'%f',mySep,'%f',mySep,'%f',mySep,'%f',mySep, ...
'%f',mySep,'%f' );
case {'GEO','GEOMETRY'}
colNames=[ "BRHO" "BP" "ID" ...
"KICK" "HKICK" "VKICK" "ANGLE" "K0L" "K1L" "K2L" ...
"APERTYPE" "APER_1" "APER_2" "APER_3" "APER_4" "APOFF_1" "APOFF_2" ];
colUnits=[ "Tm" "mm" "" ...
"rad" "rad" "rad" "rad" "rad" "rad m^{-1}" "rad m^{-2}" ...
"" "m" "m" "m" "m" "m" "m" ];
readFormat = strcat('%f',mySep,'%f',mySep,'%f',mySep, ...
'%f',mySep,'%f',mySep,'%f',mySep,'%f',mySep,'%f',mySep,'%f',mySep,'%f',mySep, ...
'%f',mySep,'%f',mySep,'%f',mySep,'%f',mySep,'%f',mySep,'%f',mySep,'%f' );
case {'RM','RMATRIX'}
colNames=[ "BRHO" "BP" "ID" ...
"RE11" "RE12" "RE21" "RE22" "RE16" "RE26" ...
"RE33" "RE34" "RE43" "RE44" "RE36" "RE46" ...
"RE51" "RE52" "RE55" "RE56" "RE66" ];
colUnits=[ "Tm" "mm" "" ...
"" "m rad^{-1}" "rad m^{-1}" "" "m" "rad" ...
"" "m rad^{-1}" "rad m^{-1}" "" "m" "rad" ...
"s m^{-1}" "s rad^{-1}" "" "s" "m" ];
readFormat = strcat('%f',mySep,'%f',mySep,'%f',mySep, ...
'%f',mySep,'%f',mySep,'%f',mySep,'%f',mySep,'%f',mySep,'%f',mySep, ...
'%f',mySep,'%f',mySep,'%f',mySep,'%f',mySep,'%f',mySep,'%f',mySep, ...
'%f',mySep,'%f',mySep,'%f',mySep,'%f',mySep,'%f' );
otherwise
error('which column mapping for TFS table?');
end
otherwise
error('which column mapping for TFS table?');
return
error("How was the .tfs table generated (TWISS/SCAN)? %s NOT recognised!",genBy);
end
colFacts = ones(1,length(colNames));
mapping = 1:length(colNames);

end
end
26 changes: 16 additions & 10 deletions MADX-optics/ParseTfsTable.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function tfsTable = ParseTfsTable(fileNames,whichTFS,nHeader)
function tfsTable = ParseTfsTable(fileNames,whichTFS,genBy)
% ParseTfsTable read TFS table from file.
%
% The function can read multiple files at the same time.
Expand All @@ -20,31 +20,37 @@
% and the second one the column.
%
% optional input arguments:
% nHeader: number of header lines;
% if not specified, the default value (i.e. 48) is used;
% genBy: format of the table:
% 'TWISS': a TFS table as generated by a MADX TWISS command;
% 'SCAN': a TFS table as generated by a current scan;
% Nota Bene: the column names and formats are considered as part of the
% header;
%
% See also GetColumnsAndMappingTFS, ParseOpticsFileHeader, GetAperture, PlotLattice.

nHeaderUsr=48;
if ( exist('nHeader','var') )
nHeaderUsr=nHeader;
if (~exist("genBy","var") | ismissing(genBy)), genBy="TWISS"; end % default genBy
switch upper(genBy)
case "TWISS"
nHeader=48;
case "SCAN"
nHeader=1;
otherwise
error("How was the .tfs table generated (TWISS/SCAN)? %s NOT recognised!",genBy);
end

% get format of the file
[ colNames, colUnits, colFacts, mapping, readFormat ] = ...
GetColumnsAndMappingTFS(whichTFS);
GetColumnsAndMappingTFS(whichTFS,genBy);

if ( length(fileNames)==1 )
fprintf('parsing %s file %s ...\n',whichTFS,fileNames);
fprintf('parsing %s file %s generated by %s ...\n',whichTFS,fileNames,genBy);
fileID = fopen(fileNames,'r');
tfsTable = textscan(fileID,readFormat,'HeaderLines',nHeaderUsr);
tfsTable = textscan(fileID,readFormat,'HeaderLines',nHeader);
fclose(fileID);
else
tfsTable=[];
for fileName=fileNames
tmpTfsTable = ParseTfsTable(fileName,whichTFS,nHeaderUsr);
tmpTfsTable = ParseTfsTable(fileName,whichTFS,genBy);
tfsTable=[ tfsTable ; tmpTfsTable ];
end
fprintf('...acquired %i %s files.\n',length(fileNames),whichTFS);
Expand Down
31 changes: 31 additions & 0 deletions MADX-optics/ParseTfsTableCurrent.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function [myTable,myColNames,MagNames,iMagNames]=ParseTfsTableCurrent(fileName)
myDelimiter=",";
%% get header from first line
fid=fopen(fileName,'r');
tmpLine=fgetl(fid);
fclose(fid);
% - crunch columns
myColNames=string(split(tmpLine,myDelimiter));
MagNames=[]; iMagNames=[];
for ii=1:length(myColNames)
[myName,myUnit]=DecodeColumn(myColNames(ii));
if (startsWith(upper(myName),"I_"))
MagNames=[ MagNames extractAfter(upper(myName),"I_") ];
iMagNames=[ iMagNames ii ];
end
end
%% get data
myTable=readmatrix(fileName,'HeaderLines',1,'Delimiter',myDelimiter,'FileType','text');
end

function [myName,myUnit]=DecodeColumn(myColHeadIN)
myColHead=myColHeadIN;
if (startsWith(myColHead,"#"))
myColHead=split(myColHead,"#");
myColHead=myColHead(2);
end
tmp=split(myColHead,"[");
myName=tmp(1);
tmp=split(tmp(2),"]");
myUnit=tmp(1);
end
2 changes: 1 addition & 1 deletion MADX-optics/PlotOptics.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function PlotOptics(tfsTables,what,emig,sigdpp,avedpp,refTfsTable,whatRef)

nTables=size(tfsTables,1);
if ( nTables>1 )
colormap(parula(nTables));
colormap(jet(nTables));
end
for jj=1:nTables
if ( exist('refTfsTable','var') )
Expand Down
Loading