diff --git a/lattice/pmdwkick.dat b/lattice/pmdwkick.dat
index e04a70a6a89a1a99909d8418a1a9606b9d5d7f75..b87822d6315e34f3716dd10afa73acfe9e519005 100644
--- a/lattice/pmdwkick.dat
+++ b/lattice/pmdwkick.dat
@@ -1,44 +1,44 @@
-# Author : Erik Wallen Nov 2008
-#ByPEAk=2.2 T, PL=80 mm, L=2000 mm
-# Undulator Length [m]
-2.
-# Number of Horizontal Points
-81
-# Number of Vertical Points
-15
-# Horizontal Kick in T2m2 
-START
- 	-0.02	-0.0195	-0.019	-0.0185	-0.018	-0.0175	-0.017	-0.0165	-0.016	-0.0155	-0.015	-0.0145	-0.014	-0.0135	-0.013	-0.0125	-0.012	-0.0115	-0.011	-0.0105	-0.01	-0.0095	-0.009	-0.0085	-0.008	-0.0075	-0.007	-0.0065	-0.006	-0.0055	-0.005	-0.0045	-0.004	-0.0035	-0.003	-0.0025	-0.002	-0.0015	-0.001	-0.0005	0.	0.0005	0.001	0.0015	0.002	0.0025	0.003	0.0035	0.004	0.0045	0.005	0.0055	0.006	0.0065	0.007	0.0075	0.008	0.0085	0.009	0.0095	0.01	0.0105	0.011	0.0115	0.012	0.0125	0.013	0.0135	0.014	0.0145	0.015	0.0155	0.016	0.0165	0.017	0.0175	0.018	0.0185	0.019	0.0195	0.02
-0.0035	-0.00070409	-0.00071173	-0.00083324	-0.00091577	-0.00087377	-0.00078832	-0.000704	-0.00079759	-0.00095528	-0.0008685	-0.00064316	-0.00043176	-0.00036823	-0.00053818	-0.00064488	-0.00053508	-0.0003508	-0.00018722	-0.00014271	-0.00029698	-0.00041325	-0.00035028	-0.00022743	-0.0001096	-0.00003537	-0.00009984	-0.00026226	-0.00029375	-0.00021522	-0.00010933	-8.79e-6	0.00005088	-8.28e-6	-0.0001566	-0.00019607	-0.00013753	-0.00005187	0.00003565	0.00010874	0.00011879	0.	-0.00011879	-0.00010874	-0.00003565	0.00005188	0.00013753	0.00019607	0.0001566	8.28e-6	-0.00005088	8.79e-6	0.00010933	0.00021522	0.00029375	0.00026226	0.00009984	0.00003537	0.00010959	0.00022743	0.00035028	0.00041325	0.00029698	0.00014271	0.00018722	0.0003508	0.00053508	0.00064488	0.00053818	0.00036823	0.00043176	0.00064315	0.0008685	0.00095528	0.00079759	0.000704	0.00078832	0.00087377	0.00091577	0.00083324	0.00071173	0.00070409
-0.003	-0.00169911	-0.00149707	-0.0013754	-0.00128053	-0.00117462	-0.00106533	-0.00098197	-0.00095333	-0.00093639	-0.00085793	-0.00072691	-0.00060416	-0.0005464	-0.00055727	-0.00055911	-0.00049762	-0.00039832	-0.00031096	-0.00027909	-0.00030667	-0.00033019	-0.00030165	-0.00023856	-0.00017496	-0.00014133	-0.00015897	-0.00020199	-0.00021231	-0.00017701	-0.00011981	-0.00006614	-0.00004036	-0.00006005	-0.00010329	-0.00011919	-0.00009425	-0.00004699	3.26e-6	0.00003873	0.00003912	0.	-0.00003911	-0.00003873	-3.26e-6	0.000047	0.00009425	0.00011919	0.00010329	0.00006005	0.00004036	0.00006614	0.00011981	0.00017701	0.00021231	0.00020199	0.00015897	0.00014133	0.00017496	0.00023856	0.00030165	0.00033019	0.00030667	0.00027909	0.00031096	0.00039832	0.00049762	0.00055911	0.00055727	0.0005464	0.00060416	0.00072691	0.00085793	0.00093639	0.00095333	0.00098198	0.00106533	0.00117462	0.00128053	0.0013754	0.00149707	0.00169911
-0.0025	-0.00258751	-0.0021982	-0.00190667	-0.001678	-0.0014866	-0.00132417	-0.00119365	-0.00109436	-0.00100763	-0.0009106	-0.00080289	-0.00070482	-0.00063587	-0.00059412	-0.00055516	-0.00049973	-0.00043263	-0.00037309	-0.00033745	-0.00032436	-0.00031345	-0.00028748	-0.00024836	-0.00020971	-0.00018562	-0.00018154	-0.00018655	-0.00018169	-0.00015927	-0.00012619	-0.0000951	-0.00007775	-0.00007847	-0.00008758	-0.00008806	-0.00007213	-0.00004453	-0.00001517	5.53e-6	9.96e-6	0.	-9.96e-6	-5.53e-6	0.00001517	0.00004453	0.00007213	0.00008806	0.00008758	0.00007847	0.00007775	0.0000951	0.00012619	0.00015927	0.00018169	0.00018655	0.00018154	0.00018562	0.00020971	0.00024836	0.00028748	0.00031345	0.00032436	0.00033745	0.00037309	0.00043263	0.00049973	0.00055516	0.00059412	0.00063587	0.00070482	0.00080289	0.0009106	0.00100763	0.00109436	0.00119365	0.00132417	0.0014866	0.001678	0.00190667	0.0021982	0.00258751
-0.002	-0.00333312	-0.00279599	-0.00237588	-0.00204244	-0.00177254	-0.00155172	-0.00137154	-0.00122372	-0.00109663	-0.00097961	-0.00087	-0.00077294	-0.00069366	-0.00063015	-0.00057347	-0.00051617	-0.00045875	-0.0004077	-0.00036859	-0.00034037	-0.00031577	-0.00028819	-0.00025733	-0.00022811	-0.00020605	-0.00019265	-0.00018342	-0.00017134	-0.00015288	-0.00013021	-0.00010883	-0.00009381	-0.0000864	-0.00008255	-0.00007575	-0.00006238	-0.00004377	-0.00002447	-9.51e-6	-1.86e-6	0.	1.86e-6	9.51e-6	0.00002447	0.00004377	0.00006238	0.00007575	0.00008255	0.0000864	0.00009381	0.00010883	0.00013021	0.00015288	0.00017134	0.00018342	0.00019265	0.00020605	0.00022811	0.00025733	0.00028819	0.00031577	0.00034037	0.00036859	0.0004077	0.00045875	0.00051617	0.00057347	0.00063015	0.00069366	0.00077294	0.00087	0.00097961	0.00109664	0.00122372	0.00137154	0.00155172	0.00177254	0.00204244	0.00237588	0.00279599	0.00333312
-0.0015	-0.00391694	-0.00327196	-0.00275698	-0.002344	-0.00201042	-0.00173914	-0.00151724	-0.00133379	-0.00117875	-0.00104411	-0.00092567	-0.0008227	-0.00073504	-0.00066046	-0.00059476	-0.00053438	-0.00047864	-0.00042922	-0.00038762	-0.00035304	-0.00032251	-0.00029328	-0.00026477	-0.00023857	-0.00021654	-0.00019898	-0.00018395	-0.00016859	-0.00015133	-0.00013286	-0.00011541	-0.00010106	-0.00009019	-0.00008106	-0.00007104	-0.00005847	-0.00004379	-0.00002897	-0.00001628	-6.9e-6	0.	6.9e-6	0.00001628	0.00002897	0.00004379	0.00005847	0.00007104	0.00008106	0.00009019	0.00010106	0.00011541	0.00013286	0.00015133	0.00016859	0.00018395	0.00019898	0.00021654	0.00023857	0.00026477	0.00029328	0.00032251	0.00035304	0.00038762	0.00042922	0.00047864	0.00053438	0.00059476	0.00066046	0.00073504	0.0008227	0.00092567	0.0010441	0.00117875	0.00133379	0.00151724	0.00173914	0.00201043	0.002344	0.00275698	0.00327196	0.00391694
-0.001	-0.00433231	-0.00361561	-0.00303586	-0.0025672	-0.00218753	-0.0018787	-0.00162603	-0.00141744	-0.0012429	-0.00109465	-0.00096741	-0.00085799	-0.00076407	-0.00068312	-0.00061228	-0.00054922	-0.00049282	-0.00044298	-0.00039966	-0.0003621	-0.0003288	-0.00029837	-0.00027024	-0.00024473	-0.0002223	-0.0002028	-0.00018526	-0.00016841	-0.00015145	-0.00013458	-0.00011864	-0.00010445	-0.00009209	-0.00008078	-0.00006935	-0.00005706	-0.00004403	-0.00003112	-0.00001932	-9.09e-6	0.	9.09e-6	0.00001932	0.00003112	0.00004403	0.00005706	0.00006935	0.00008078	0.00009209	0.00010445	0.00011864	0.00013458	0.00015145	0.00016841	0.00018526	0.0002028	0.0002223	0.00024473	0.00027024	0.00029837	0.0003288	0.0003621	0.00039966	0.00044298	0.00049282	0.00054922	0.00061227	0.00068312	0.00076407	0.00085799	0.00096741	0.00109465	0.0012429	0.00141744	0.00162603	0.0018787	0.00218753	0.0025672	0.00303586	0.00361561	0.00433231
-0.0005	-0.00457976	-0.00382254	-0.0032053	-0.00270379	-0.00229641	-0.00196469	-0.0016933	-0.00146959	-0.00128333	-0.00112653	-0.00099328	-0.00087936	-0.00078155	-0.00069707	-0.00062344	-0.00055864	-0.00050135	-0.00045084	-0.00040649	-0.00036753	-0.00033294	-0.0003018	-0.00027356	-0.00024808	-0.00022528	-0.0002049	-0.00018631	-0.00016879	-0.00015188	-0.00013556	-0.00012016	-0.00010599	-0.00009301	-0.00008082	-0.00006882	-0.00005662	-0.00004424	-0.00003208	-0.00002061	-0.00001	0.	0.00001	0.00002061	0.00003208	0.00004424	0.00005661	0.00006882	0.00008082	0.00009301	0.00010599	0.00012016	0.00013556	0.00015188	0.00016879	0.00018631	0.0002049	0.00022528	0.00024808	0.00027356	0.0003018	0.00033294	0.00036753	0.00040649	0.00045084	0.00050135	0.00055864	0.00062344	0.00069707	0.00078155	0.00087936	0.00099328	0.00112653	0.00128333	0.00146959	0.00169329	0.00196469	0.00229641	0.00270379	0.0032053	0.00382254	0.00457976
-0.	-0.00466183	-0.00389157	-0.00326208	-0.00274973	-0.00233311	-0.00199373	-0.00171605	-0.00148729	-0.00129711	-0.00113739	-0.00100204	-0.00088653	-0.0007874	-0.00070178	-0.00062725	-0.00056185	-0.00050421	-0.00045341	-0.00040872	-0.00036934	-0.00033437	-0.00030299	-0.00027467	-0.00024914	-0.00022622	-0.00020558	-0.00018669	-0.00016898	-0.00015206	-0.00013588	-0.00012061	-0.00010643	-0.00009329	-0.00008086	-0.00006871	-0.00005652	-0.00004432	-0.00003236	-0.00002096	-0.00001025	0.	0.00001025	0.00002096	0.00003236	0.00004432	0.00005652	0.00006871	0.00008086	0.00009329	0.00010643	0.00012061	0.00013588	0.00015206	0.00016898	0.00018669	0.00020558	0.00022622	0.00024914	0.00027467	0.00030299	0.00033437	0.00036934	0.00040872	0.00045341	0.00050421	0.00056185	0.00062725	0.00070179	0.0007874	0.00088653	0.00100204	0.0011374	0.00129711	0.00148729	0.00171605	0.00199373	0.00233311	0.00274973	0.00326208	0.00389157	0.00466183
--0.0005	-0.00457976	-0.00382254	-0.0032053	-0.00270379	-0.00229641	-0.00196469	-0.0016933	-0.00146959	-0.00128333	-0.00112653	-0.00099328	-0.00087936	-0.00078155	-0.00069707	-0.00062344	-0.00055864	-0.00050135	-0.00045084	-0.00040649	-0.00036753	-0.00033294	-0.0003018	-0.00027356	-0.00024808	-0.00022528	-0.0002049	-0.00018631	-0.00016879	-0.00015188	-0.00013556	-0.00012016	-0.00010599	-0.00009301	-0.00008082	-0.00006882	-0.00005662	-0.00004424	-0.00003208	-0.00002061	-0.00001	0.	0.00001	0.00002061	0.00003208	0.00004424	0.00005662	0.00006882	0.00008082	0.00009301	0.00010599	0.00012016	0.00013556	0.00015188	0.00016879	0.00018631	0.0002049	0.00022528	0.00024808	0.00027356	0.0003018	0.00033294	0.00036753	0.00040649	0.00045084	0.00050135	0.00055864	0.00062344	0.00069707	0.00078155	0.00087936	0.00099328	0.00112653	0.00128333	0.00146959	0.00169329	0.00196469	0.00229641	0.00270379	0.0032053	0.00382254	0.00457976
--0.001	-0.00433231	-0.00361561	-0.00303586	-0.0025672	-0.00218753	-0.0018787	-0.00162603	-0.00141744	-0.0012429	-0.00109465	-0.00096741	-0.00085799	-0.00076407	-0.00068312	-0.00061227	-0.00054921	-0.00049282	-0.00044298	-0.00039966	-0.0003621	-0.0003288	-0.00029837	-0.00027024	-0.00024473	-0.00022229	-0.0002028	-0.00018526	-0.00016841	-0.00015145	-0.00013458	-0.00011864	-0.00010445	-0.00009209	-0.00008078	-0.00006935	-0.00005706	-0.00004403	-0.00003112	-0.00001932	-9.09e-6	0.	9.09e-6	0.00001932	0.00003112	0.00004403	0.00005706	0.00006935	0.00008078	0.00009209	0.00010445	0.00011864	0.00013458	0.00015145	0.00016841	0.00018526	0.0002028	0.0002223	0.00024473	0.00027024	0.00029837	0.0003288	0.0003621	0.00039966	0.00044298	0.00049282	0.00054922	0.00061227	0.00068311	0.00076407	0.00085799	0.00096741	0.00109465	0.0012429	0.00141744	0.00162603	0.0018787	0.00218753	0.0025672	0.00303586	0.00361561	0.00433231
--0.0015	-0.00391694	-0.00327196	-0.00275698	-0.002344	-0.00201043	-0.00173914	-0.00151724	-0.00133379	-0.00117875	-0.0010441	-0.00092567	-0.0008227	-0.00073504	-0.00066046	-0.00059476	-0.00053438	-0.00047864	-0.00042922	-0.00038762	-0.00035304	-0.00032251	-0.00029328	-0.00026477	-0.00023857	-0.00021654	-0.00019898	-0.00018395	-0.00016859	-0.00015133	-0.00013286	-0.00011541	-0.00010106	-0.00009019	-0.00008107	-0.00007104	-0.00005847	-0.00004379	-0.00002897	-0.00001628	-6.9e-6	0.	6.9e-6	0.00001628	0.00002897	0.00004379	0.00005847	0.00007104	0.00008107	0.00009019	0.00010106	0.00011541	0.00013286	0.00015133	0.00016859	0.00018395	0.00019898	0.00021654	0.00023857	0.00026477	0.00029328	0.00032251	0.00035304	0.00038762	0.00042922	0.00047864	0.00053438	0.00059476	0.00066046	0.00073504	0.0008227	0.00092567	0.00104411	0.00117875	0.00133379	0.00151724	0.00173914	0.00201042	0.002344	0.00275698	0.00327196	0.00391694
--0.002	-0.00333312	-0.00279599	-0.00237588	-0.00204244	-0.00177254	-0.00155172	-0.00137154	-0.00122372	-0.00109663	-0.00097961	-0.00087	-0.00077294	-0.00069366	-0.00063015	-0.00057347	-0.00051617	-0.00045875	-0.0004077	-0.00036859	-0.00034037	-0.00031577	-0.00028819	-0.00025733	-0.00022811	-0.00020605	-0.00019265	-0.00018342	-0.00017134	-0.00015288	-0.00013021	-0.00010883	-0.00009381	-0.0000864	-0.00008255	-0.00007575	-0.00006238	-0.00004377	-0.00002446	-9.51e-6	-1.86e-6	0.	1.86e-6	9.51e-6	0.00002446	0.00004377	0.00006238	0.00007575	0.00008255	0.0000864	0.00009381	0.00010883	0.00013021	0.00015288	0.00017134	0.00018342	0.00019265	0.00020605	0.00022811	0.00025733	0.00028819	0.00031577	0.00034037	0.00036859	0.0004077	0.00045875	0.00051617	0.00057347	0.00063015	0.00069366	0.00077294	0.00087	0.00097961	0.00109664	0.00122372	0.00137154	0.00155172	0.00177254	0.00204244	0.00237588	0.00279599	0.00333312
--0.0025	-0.00258751	-0.0021982	-0.00190667	-0.001678	-0.0014866	-0.00132417	-0.00119365	-0.00109436	-0.00100763	-0.0009106	-0.00080289	-0.00070482	-0.00063587	-0.00059412	-0.00055516	-0.00049973	-0.00043263	-0.00037309	-0.00033745	-0.00032436	-0.00031345	-0.00028748	-0.00024836	-0.00020971	-0.00018562	-0.00018154	-0.00018655	-0.00018169	-0.00015927	-0.00012619	-0.0000951	-0.00007775	-0.00007847	-0.00008758	-0.00008806	-0.00007213	-0.00004453	-0.00001517	5.53e-6	9.96e-6	0.	-9.96e-6	-5.53e-6	0.00001517	0.00004453	0.00007213	0.00008806	0.00008758	0.00007847	0.00007775	0.0000951	0.00012619	0.00015927	0.00018169	0.00018655	0.00018154	0.00018562	0.00020971	0.00024836	0.00028748	0.00031345	0.00032436	0.00033745	0.00037309	0.00043263	0.00049973	0.00055516	0.00059412	0.00063587	0.00070482	0.00080289	0.0009106	0.00100763	0.00109436	0.00119365	0.00132417	0.00148661	0.001678	0.00190667	0.0021982	0.00258751
--0.003	-0.00169911	-0.00149707	-0.0013754	-0.00128053	-0.00117462	-0.00106533	-0.00098197	-0.00095333	-0.00093639	-0.00085793	-0.00072691	-0.00060416	-0.0005464	-0.00055727	-0.00055911	-0.00049762	-0.00039832	-0.00031096	-0.00027909	-0.00030667	-0.00033019	-0.00030165	-0.00023856	-0.00017496	-0.00014133	-0.00015897	-0.00020199	-0.00021231	-0.00017701	-0.00011981	-0.00006614	-0.00004036	-0.00006005	-0.00010329	-0.00011919	-0.00009425	-0.00004699	3.26e-6	0.00003873	0.00003912	0.	-0.00003912	-0.00003873	-3.26e-6	0.00004699	0.00009425	0.00011919	0.00010329	0.00006005	0.00004036	0.00006614	0.00011981	0.00017701	0.00021231	0.00020199	0.00015897	0.00014133	0.00017496	0.00023856	0.00030165	0.00033019	0.00030667	0.00027909	0.00031096	0.00039832	0.00049762	0.00055911	0.00055727	0.0005464	0.00060416	0.00072691	0.00085793	0.00093639	0.00095333	0.00098197	0.00106533	0.00117462	0.00128053	0.0013754	0.00149707	0.00169911
--0.0035	-0.00070409	-0.00071173	-0.00083324	-0.00091577	-0.00087377	-0.00078832	-0.000704	-0.00079759	-0.00095528	-0.0008685	-0.00064316	-0.00043176	-0.00036823	-0.00053818	-0.00064488	-0.00053508	-0.0003508	-0.00018722	-0.00014271	-0.00029698	-0.00041326	-0.00035028	-0.00022743	-0.0001096	-0.00003537	-0.00009984	-0.00026226	-0.00029375	-0.00021522	-0.00010933	-8.79e-6	0.00005088	-8.28e-6	-0.0001566	-0.00019607	-0.00013753	-0.00005188	0.00003565	0.00010874	0.00011879	0.	-0.00011879	-0.00010874	-0.00003565	0.00005188	0.00013753	0.00019607	0.0001566	8.28e-6	-0.00005088	8.79e-6	0.00010933	0.00021522	0.00029375	0.00026226	0.00009984	0.00003537	0.0001096	0.00022743	0.00035028	0.00041325	0.00029698	0.00014271	0.00018722	0.0003508	0.00053508	0.00064488	0.00053818	0.00036823	0.00043176	0.00064316	0.0008685	0.00095528	0.00079759	0.000704	0.00078832	0.00087377	0.00091577	0.00083324	0.00071173	0.00070409
-# Vertical Kick in T2m2 
-START
- 	-0.02	-0.0195	-0.019	-0.0185	-0.018	-0.0175	-0.017	-0.0165	-0.016	-0.0155	-0.015	-0.0145	-0.014	-0.0135	-0.013	-0.0125	-0.012	-0.0115	-0.011	-0.0105	-0.01	-0.0095	-0.009	-0.0085	-0.008	-0.0075	-0.007	-0.0065	-0.006	-0.0055	-0.005	-0.0045	-0.004	-0.0035	-0.003	-0.0025	-0.002	-0.0015	-0.001	-0.0005	0.	0.0005	0.001	0.0015	0.002	0.0025	0.003	0.0035	0.004	0.0045	0.005	0.0055	0.006	0.0065	0.007	0.0075	0.008	0.0085	0.009	0.0095	0.01	0.0105	0.011	0.0115	0.012	0.0125	0.013	0.0135	0.014	0.0145	0.015	0.0155	0.016	0.0165	0.017	0.0175	0.018	0.0185	0.019	0.0195	0.02
-0.0035	-0.00178037	-0.00085015	-0.0001652	0.00021376	0.0005068	0.00077419	0.00108589	0.00138408	0.00138453	0.0012642	0.0012754	0.00143686	0.00170971	0.00186702	0.00173874	0.00161062	0.00160679	0.00172176	0.0019334	0.00206338	0.00195289	0.00183243	0.00180636	0.00185431	0.00197984	0.00213619	0.00212344	0.00198567	0.00189847	0.00188117	0.00192466	0.00203394	0.00216903	0.0021609	0.00203755	0.00195202	0.00192267	0.00193907	0.00200563	0.00212711	0.00221187	0.00212711	0.00200563	0.00193907	0.00192268	0.00195202	0.00203755	0.0021609	0.00216903	0.00203394	0.00192466	0.00188117	0.00189847	0.00198567	0.00212344	0.00213619	0.00197984	0.00185431	0.00180636	0.00183243	0.00195289	0.00206338	0.0019334	0.00172176	0.00160679	0.00161062	0.00173874	0.00186702	0.00170971	0.00143686	0.0012754	0.0012642	0.00138453	0.00138409	0.00108589	0.00077419	0.0005068	0.00021376	-0.0001652	-0.00085015	-0.00178037
-0.003	-0.00470518	-0.00385915	-0.00321365	-0.00274782	-0.0023975	-0.00210773	-0.00185251	-0.0016568	-0.00156605	-0.00153602	-0.00148292	-0.0013768	-0.00124659	-0.00116697	-0.001172	-0.00119874	-0.00118616	-0.00112133	-0.00103061	-0.0009767	-0.00099255	-0.00102974	-0.00103933	-0.00100985	-0.00095002	-0.00089498	-0.0008934	-0.00093736	-0.00097743	-0.00098695	-0.00096184	-0.00091047	-0.00086336	-0.00086304	-0.00090477	-0.00094639	-0.00096424	-0.00095352	-0.00091608	-0.00086571	-0.00083978	-0.00086571	-0.00091607	-0.00095352	-0.00096424	-0.00094639	-0.00090477	-0.00086304	-0.00086336	-0.00091047	-0.00096184	-0.00098695	-0.00097743	-0.00093736	-0.0008934	-0.00089498	-0.00095003	-0.00100986	-0.00103933	-0.00102974	-0.00099255	-0.0009767	-0.00103061	-0.00112132	-0.00118616	-0.00119874	-0.001172	-0.00116697	-0.00124659	-0.0013768	-0.00148292	-0.00153602	-0.00156605	-0.0016568	-0.00185251	-0.00210773	-0.0023975	-0.00274782	-0.00321365	-0.00385915	-0.00470519
-0.0025	-0.00590974	-0.00517449	-0.00459721	-0.00415368	-0.00381104	-0.00353851	-0.00331932	-0.00315405	-0.00304482	-0.00297208	-0.00290473	-0.00282745	-0.00274998	-0.0026954600000000002	-0.0026725	-0.00266269	-0.00264244	-0.00260405	-0.00255901	-0.00252889	-0.00252334	-0.00252908	-0.00252722	-0.00250966	-0.00248135	-0.00245778	-0.00245396	-0.00246785	-0.0024832	-0.00248639	-0.00247358	-0.00245059	-0.00243157	-0.00243016	-0.00244535	-0.00246333	-0.00247176	-0.0024658	-0.0024475	-0.00242619	-0.00241644	-0.00242619	-0.0024475	-0.0024658	-0.00247176	-0.00246333	-0.00244535	-0.00243016	-0.00243157	-0.00245059	-0.00247359	-0.00248639	-0.0024832	-0.00246785	-0.00245396	-0.00245778	-0.00248135	-0.00250966	-0.00252722	-0.00252908	-0.00252334	-0.00252889	-0.00255901	-0.00260405	-0.00264244	-0.00266269	-0.0026725	-0.0026954600000000002	-0.00274998	-0.00282745	-0.00290473	-0.00297208	-0.00304482	-0.00315405	-0.00331932	-0.00353851	-0.00381104	-0.00415368	-0.00459721	-0.00517449	-0.00590974
-0.002	-0.00588383	-0.0052823	-0.00480022	-0.00441983	-0.00412114	-0.00388561	-0.00370053	-0.00355933	-0.00345589	-0.0033788	-0.00331421	-0.0032542	-0.00320041	-0.00315919	-0.00313231	-0.00311318	-0.00309283	-0.00306763	-0.00304162	-0.00302218	-0.00301252	-0.00300836	-0.00300259	-0.00299146	-0.00297699	-0.00296529	-0.00296157	-0.00296513	-0.00297	-0.0029702	-0.00296368	-0.00295323	-0.00294484	-0.00294355	-0.00294911	-0.0029564	-0.00295981	-0.00295664	-0.00294809	-0.00293883	-0.00293481	-0.00293883	-0.00294808	-0.00295664	-0.00295981	-0.0029564	-0.00294911	-0.00294355	-0.00294484	-0.00295323	-0.00296368	-0.0029702	-0.00297	-0.00296513	-0.00296157	-0.00296529	-0.00297699	-0.00299146	-0.00300259	-0.00300836	-0.00301251	-0.00302218	-0.00304162	-0.00306763	-0.00309282	-0.00311318	-0.00313231	-0.00315919	-0.00320041	-0.0032542	-0.00331421	-0.0033788	-0.00345589	-0.00355933	-0.00370053	-0.00388561	-0.00412114	-0.00441983	-0.00480022	-0.0052823	-0.00588383
-0.0015	-0.00501089	-0.00455622	-0.00418597	-0.00388923	-0.0036537	-0.00346773	-0.00332201	-0.0032096	-0.00312417	-0.0030584	-0.00300503	-0.0029594	-0.00292065	-0.00288984	-0.00286675	-0.00284859	-0.00283174	-0.00281453	-0.00279816	-0.00278507	-0.00277632	-0.00277037	-0.00276452	-0.00275722	-0.00274919	-0.00274275	-0.00273976	-0.00273989	-0.00274082	-0.00274004	-0.00273659	-0.00273167	-0.00272774	-0.00272676	-0.00272867	-0.00273144	-0.00273262	-0.00273094	-0.00272699	-0.0027229	-0.00272117	-0.0027229	-0.00272699	-0.00273094	-0.00273262	-0.00273144	-0.00272868	-0.00272676	-0.00272774	-0.00273167	-0.0027366	-0.00274004	-0.00274082	-0.00273989	-0.00273975	-0.00274275	-0.00274919	-0.00275722	-0.00276452	-0.00277037	-0.00277632	-0.00278507	-0.00279816	-0.00281453	-0.00283174	-0.00284859	-0.00286675	-0.00288984	-0.00292066	-0.0029594	-0.00300503	-0.0030584	-0.00312417	-0.0032096	-0.00332201	-0.00346773	-0.0036537	-0.00388923	-0.00418597	-0.00455622	-0.00501089
-0.001	-0.00359626	-0.0032932	-0.00304345	-0.00284122	-0.00267951	-0.00255137	-0.00245074	-0.00237256	-0.00231222	-0.00226526	-0.00222761	-0.0021965	-0.00217073	-0.00214995	-0.00213357	-0.00212026	-0.00210849	-0.00209746	-0.00208741	-0.00207912	-0.00207287	-0.00206809	-0.00206374	-0.0020592	-0.00205471	-0.00205111	-0.00204904	-0.00204835	-0.00204811	-0.00204727	-0.00204545	-0.00204311	-0.00204123	-0.00204059	-0.00204116	-0.00204211	-0.00204243	-0.00204156	-0.00203977	-0.00203798	-0.00203724	-0.00203798	-0.00203977	-0.00204156	-0.00204243	-0.00204211	-0.00204116	-0.00204059	-0.00204123	-0.00204311	-0.00204545	-0.00204727	-0.0020481	-0.00204835	-0.00204904	-0.00205111	-0.00205471	-0.0020592	-0.00206374	-0.00206809	-0.00207287	-0.00207912	-0.00208741	-0.00209746	-0.00210849	-0.00212026	-0.00213357	-0.00214995	-0.00217073	-0.0021965	-0.00222761	-0.00226526	-0.00231223	-0.00237256	-0.00245074	-0.00255137	-0.00267951	-0.00284122	-0.00304345	-0.0032932	-0.00359626
-0.0005	-0.00186885	-0.0017177	-0.0015922	-0.00148996	-0.00140784	-0.00134258	-0.00129121	-0.00125113	-0.00122001	-0.00119568	-0.00117628	-0.00116046	-0.00114749	-0.00113699	-0.00112855	-0.00112163	-0.00111561	-0.00111017	-0.00110532	-0.00110127	-0.00109806	-0.0010955	-0.00109323	-0.00109103	-0.00108897	-0.00108732	-0.00108627	-0.00108575	-0.00108544	-0.00108495	-0.00108415	-0.00108319	-0.00108242	-0.0010821	-0.00108222	-0.00108249	-0.00108254	-0.00108217	-0.00108148	-0.0010808	-0.00108052	-0.0010808	-0.00108148	-0.00108217	-0.00108254	-0.00108249	-0.00108222	-0.0010821	-0.00108242	-0.00108319	-0.00108415	-0.00108495	-0.00108544	-0.00108575	-0.00108627	-0.00108732	-0.00108897	-0.00109103	-0.00109323	-0.0010955	-0.00109806	-0.00110127	-0.00110532	-0.00111017	-0.00111561	-0.00112163	-0.00112855	-0.00113699	-0.00114749	-0.00116046	-0.00117628	-0.00119568	-0.00122001	-0.00125113	-0.00129121	-0.00134258	-0.00140784	-0.00148996	-0.0015922	-0.0017177	-0.00186885
-0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.
--0.0005	0.00186885	0.0017177	0.0015922	0.00148996	0.00140784	0.00134258	0.00129121	0.00125113	0.00122001	0.00119568	0.00117628	0.00116046	0.00114749	0.00113699	0.00112855	0.00112163	0.00111561	0.00111017	0.00110532	0.00110127	0.00109806	0.0010955	0.00109323	0.00109103	0.00108897	0.00108732	0.00108627	0.00108575	0.00108543	0.00108495	0.00108415	0.00108319	0.00108242	0.0010821	0.00108222	0.00108249	0.00108255	0.00108217	0.00108148	0.0010808	0.00108052	0.0010808	0.00108148	0.00108217	0.00108254	0.0010825	0.00108222	0.0010821	0.00108242	0.00108319	0.00108415	0.00108495	0.00108544	0.00108575	0.00108627	0.00108732	0.00108897	0.00109103	0.00109323	0.0010955	0.00109806	0.00110127	0.00110532	0.00111017	0.00111561	0.00112163	0.00112855	0.00113699	0.00114749	0.00116046	0.00117628	0.00119568	0.00122001	0.00125113	0.00129121	0.00134258	0.00140784	0.00148996	0.0015922	0.0017177	0.00186885
--0.001	0.00359626	0.0032932	0.00304345	0.00284122	0.00267951	0.00255137	0.00245074	0.00237256	0.00231223	0.00226526	0.00222761	0.00219651	0.00217073	0.00214995	0.00213357	0.00212026	0.00210849	0.00209746	0.00208741	0.00207912	0.00207287	0.00206809	0.00206374	0.0020592	0.00205471	0.00205111	0.00204904	0.00204835	0.0020481	0.00204727	0.00204545	0.00204311	0.00204123	0.00204059	0.00204116	0.00204211	0.00204243	0.00204156	0.00203977	0.00203798	0.00203724	0.00203798	0.00203977	0.00204156	0.00204243	0.00204211	0.00204116	0.00204059	0.00204123	0.00204311	0.00204545	0.00204727	0.00204811	0.00204835	0.00204904	0.00205111	0.00205471	0.0020592	0.00206374	0.00206809	0.00207287	0.00207912	0.00208741	0.00209745	0.00210849	0.00212026	0.00213357	0.00214995	0.00217073	0.0021965	0.00222761	0.00226526	0.00231222	0.00237256	0.00245074	0.00255137	0.00267951	0.00284122	0.00304345	0.0032932	0.00359626
--0.0015	0.00501089	0.00455622	0.00418597	0.00388923	0.0036537	0.00346773	0.00332201	0.0032096	0.00312417	0.0030584	0.00300503	0.0029594	0.00292065	0.00288984	0.00286675	0.00284859	0.00283174	0.00281453	0.00279816	0.00278507	0.00277632	0.00277037	0.00276452	0.00275722	0.00274919	0.00274275	0.00273975	0.00273989	0.00274082	0.00274004	0.00273659	0.00273167	0.00272774	0.00272676	0.00272867	0.00273144	0.00273262	0.00273094	0.00272699	0.0027229	0.00272117	0.0027229	0.00272699	0.00273094	0.00273262	0.00273144	0.00272867	0.00272676	0.00272774	0.00273167	0.00273659	0.00274004	0.00274082	0.00273989	0.00273976	0.00274275	0.00274919	0.00275722	0.00276452	0.00277037	0.00277632	0.00278507	0.00279816	0.00281454	0.00283174	0.00284859	0.00286675	0.00288984	0.00292066	0.0029594	0.00300502	0.0030584	0.00312417	0.0032096	0.00332201	0.00346773	0.0036537	0.00388923	0.00418597	0.00455622	0.00501089
--0.002	0.00588383	0.0052823	0.00480022	0.00441983	0.00412114	0.00388561	0.00370053	0.00355933	0.00345589	0.0033788	0.00331421	0.0032542	0.00320041	0.00315919	0.00313231	0.00311318	0.00309282	0.00306763	0.00304162	0.00302218	0.00301252	0.00300836	0.00300259	0.00299146	0.00297699	0.00296529	0.00296157	0.00296513	0.00297	0.0029702	0.00296368	0.00295322	0.00294484	0.00294355	0.00294911	0.0029564	0.00295981	0.00295664	0.00294809	0.00293883	0.00293481	0.00293883	0.00294809	0.00295664	0.00295981	0.0029564	0.00294911	0.00294355	0.00294484	0.00295323	0.00296368	0.0029702	0.00297	0.00296513	0.00296157	0.00296529	0.00297699	0.00299146	0.00300259	0.00300836	0.00301251	0.00302218	0.00304162	0.00306763	0.00309283	0.00311318	0.00313231	0.00315919	0.00320041	0.0032542	0.00331421	0.0033788	0.00345589	0.00355933	0.00370053	0.00388561	0.00412114	0.00441983	0.00480022	0.0052823	0.00588383
--0.0025	0.00590974	0.00517449	0.00459721	0.00415368	0.00381104	0.00353851	0.00331932	0.00315405	0.00304482	0.00297208	0.00290473	0.00282745	0.00274998	0.00269545	0.0026725	0.00266269	0.00264244	0.00260405	0.00255901	0.00252889	0.00252334	0.00252908	0.00252722	0.00250966	0.00248135	0.00245778	0.00245396	0.00246785	0.0024832	0.00248639	0.00247359	0.00245059	0.00243157	0.00243016	0.00244535	0.00246333	0.00247176	0.0024658	0.0024475	0.00242619	0.00241644	0.00242619	0.0024475	0.0024658	0.00247176	0.00246333	0.00244535	0.00243016	0.00243157	0.00245059	0.00247359	0.00248639	0.0024832	0.00246785	0.00245396	0.00245778	0.00248135	0.00250966	0.00252722	0.00252908	0.00252334	0.00252889	0.00255901	0.00260405	0.00264244	0.00266269	0.0026725	0.00269545	0.00274998	0.00282745	0.00290473	0.00297208	0.00304482	0.00315405	0.00331932	0.00353851	0.00381104	0.00415368	0.00459721	0.00517449	0.00590974
--0.003	0.00470518	0.00385915	0.00321365	0.00274782	0.0023975	0.00210773	0.00185251	0.0016568	0.00156605	0.00153602	0.00148292	0.0013768	0.00124659	0.00116697	0.001172	0.00119874	0.00118616	0.00112132	0.00103061	0.0009767	0.00099255	0.00102975	0.00103933	0.00100985	0.00095003	0.00089498	0.0008934	0.00093736	0.00097743	0.00098695	0.00096184	0.00091047	0.00086336	0.00086304	0.00090477	0.00094639	0.00096424	0.00095352	0.00091607	0.00086571	0.00083978	0.00086571	0.00091607	0.00095352	0.00096424	0.00094639	0.00090477	0.00086304	0.00086336	0.00091047	0.00096184	0.00098695	0.00097743	0.00093736	0.0008934	0.00089498	0.00095003	0.00100985	0.00103933	0.00102974	0.00099255	0.0009767	0.00103061	0.00112133	0.00118616	0.00119874	0.001172	0.00116697	0.00124659	0.0013768	0.00148292	0.00153602	0.00156605	0.0016568	0.00185251	0.00210773	0.0023975	0.00274782	0.00321365	0.00385915	0.00470519
+# Author : Erik Wallen Nov 2008
+#ByPEAk=2.2 T, PL=80 mm, L=2000 mm
+# Undulator Length [m]
+2.
+# Number of Horizontal Points
+81
+# Number of Vertical Points
+15
+# Horizontal Kick in T2m2 
+START
+ 	-0.02	-0.0195	-0.019	-0.0185	-0.018	-0.0175	-0.017	-0.0165	-0.016	-0.0155	-0.015	-0.0145	-0.014	-0.0135	-0.013	-0.0125	-0.012	-0.0115	-0.011	-0.0105	-0.01	-0.0095	-0.009	-0.0085	-0.008	-0.0075	-0.007	-0.0065	-0.006	-0.0055	-0.005	-0.0045	-0.004	-0.0035	-0.003	-0.0025	-0.002	-0.0015	-0.001	-0.0005	0.	0.0005	0.001	0.0015	0.002	0.0025	0.003	0.0035	0.004	0.0045	0.005	0.0055	0.006	0.0065	0.007	0.0075	0.008	0.0085	0.009	0.0095	0.01	0.0105	0.011	0.0115	0.012	0.0125	0.013	0.0135	0.014	0.0145	0.015	0.0155	0.016	0.0165	0.017	0.0175	0.018	0.0185	0.019	0.0195	0.02
+0.0035	-0.00070409	-0.00071173	-0.00083324	-0.00091577	-0.00087377	-0.00078832	-0.000704	-0.00079759	-0.00095528	-0.0008685	-0.00064316	-0.00043176	-0.00036823	-0.00053818	-0.00064488	-0.00053508	-0.0003508	-0.00018722	-0.00014271	-0.00029698	-0.00041325	-0.00035028	-0.00022743	-0.0001096	-0.00003537	-0.00009984	-0.00026226	-0.00029375	-0.00021522	-0.00010933	-8.79e-6	0.00005088	-8.28e-6	-0.0001566	-0.00019607	-0.00013753	-0.00005187	0.00003565	0.00010874	0.00011879	0.	-0.00011879	-0.00010874	-0.00003565	0.00005188	0.00013753	0.00019607	0.0001566	8.28e-6	-0.00005088	8.79e-6	0.00010933	0.00021522	0.00029375	0.00026226	0.00009984	0.00003537	0.00010959	0.00022743	0.00035028	0.00041325	0.00029698	0.00014271	0.00018722	0.0003508	0.00053508	0.00064488	0.00053818	0.00036823	0.00043176	0.00064315	0.0008685	0.00095528	0.00079759	0.000704	0.00078832	0.00087377	0.00091577	0.00083324	0.00071173	0.00070409
+0.003	-0.00169911	-0.00149707	-0.0013754	-0.00128053	-0.00117462	-0.00106533	-0.00098197	-0.00095333	-0.00093639	-0.00085793	-0.00072691	-0.00060416	-0.0005464	-0.00055727	-0.00055911	-0.00049762	-0.00039832	-0.00031096	-0.00027909	-0.00030667	-0.00033019	-0.00030165	-0.00023856	-0.00017496	-0.00014133	-0.00015897	-0.00020199	-0.00021231	-0.00017701	-0.00011981	-0.00006614	-0.00004036	-0.00006005	-0.00010329	-0.00011919	-0.00009425	-0.00004699	3.26e-6	0.00003873	0.00003912	0.	-0.00003911	-0.00003873	-3.26e-6	0.000047	0.00009425	0.00011919	0.00010329	0.00006005	0.00004036	0.00006614	0.00011981	0.00017701	0.00021231	0.00020199	0.00015897	0.00014133	0.00017496	0.00023856	0.00030165	0.00033019	0.00030667	0.00027909	0.00031096	0.00039832	0.00049762	0.00055911	0.00055727	0.0005464	0.00060416	0.00072691	0.00085793	0.00093639	0.00095333	0.00098198	0.00106533	0.00117462	0.00128053	0.0013754	0.00149707	0.00169911
+0.0025	-0.00258751	-0.0021982	-0.00190667	-0.001678	-0.0014866	-0.00132417	-0.00119365	-0.00109436	-0.00100763	-0.0009106	-0.00080289	-0.00070482	-0.00063587	-0.00059412	-0.00055516	-0.00049973	-0.00043263	-0.00037309	-0.00033745	-0.00032436	-0.00031345	-0.00028748	-0.00024836	-0.00020971	-0.00018562	-0.00018154	-0.00018655	-0.00018169	-0.00015927	-0.00012619	-0.0000951	-0.00007775	-0.00007847	-0.00008758	-0.00008806	-0.00007213	-0.00004453	-0.00001517	5.53e-6	9.96e-6	0.	-9.96e-6	-5.53e-6	0.00001517	0.00004453	0.00007213	0.00008806	0.00008758	0.00007847	0.00007775	0.0000951	0.00012619	0.00015927	0.00018169	0.00018655	0.00018154	0.00018562	0.00020971	0.00024836	0.00028748	0.00031345	0.00032436	0.00033745	0.00037309	0.00043263	0.00049973	0.00055516	0.00059412	0.00063587	0.00070482	0.00080289	0.0009106	0.00100763	0.00109436	0.00119365	0.00132417	0.0014866	0.001678	0.00190667	0.0021982	0.00258751
+0.002	-0.00333312	-0.00279599	-0.00237588	-0.00204244	-0.00177254	-0.00155172	-0.00137154	-0.00122372	-0.00109663	-0.00097961	-0.00087	-0.00077294	-0.00069366	-0.00063015	-0.00057347	-0.00051617	-0.00045875	-0.0004077	-0.00036859	-0.00034037	-0.00031577	-0.00028819	-0.00025733	-0.00022811	-0.00020605	-0.00019265	-0.00018342	-0.00017134	-0.00015288	-0.00013021	-0.00010883	-0.00009381	-0.0000864	-0.00008255	-0.00007575	-0.00006238	-0.00004377	-0.00002447	-9.51e-6	-1.86e-6	0.	1.86e-6	9.51e-6	0.00002447	0.00004377	0.00006238	0.00007575	0.00008255	0.0000864	0.00009381	0.00010883	0.00013021	0.00015288	0.00017134	0.00018342	0.00019265	0.00020605	0.00022811	0.00025733	0.00028819	0.00031577	0.00034037	0.00036859	0.0004077	0.00045875	0.00051617	0.00057347	0.00063015	0.00069366	0.00077294	0.00087	0.00097961	0.00109664	0.00122372	0.00137154	0.00155172	0.00177254	0.00204244	0.00237588	0.00279599	0.00333312
+0.0015	-0.00391694	-0.00327196	-0.00275698	-0.002344	-0.00201042	-0.00173914	-0.00151724	-0.00133379	-0.00117875	-0.00104411	-0.00092567	-0.0008227	-0.00073504	-0.00066046	-0.00059476	-0.00053438	-0.00047864	-0.00042922	-0.00038762	-0.00035304	-0.00032251	-0.00029328	-0.00026477	-0.00023857	-0.00021654	-0.00019898	-0.00018395	-0.00016859	-0.00015133	-0.00013286	-0.00011541	-0.00010106	-0.00009019	-0.00008106	-0.00007104	-0.00005847	-0.00004379	-0.00002897	-0.00001628	-6.9e-6	0.	6.9e-6	0.00001628	0.00002897	0.00004379	0.00005847	0.00007104	0.00008106	0.00009019	0.00010106	0.00011541	0.00013286	0.00015133	0.00016859	0.00018395	0.00019898	0.00021654	0.00023857	0.00026477	0.00029328	0.00032251	0.00035304	0.00038762	0.00042922	0.00047864	0.00053438	0.00059476	0.00066046	0.00073504	0.0008227	0.00092567	0.0010441	0.00117875	0.00133379	0.00151724	0.00173914	0.00201043	0.002344	0.00275698	0.00327196	0.00391694
+0.001	-0.00433231	-0.00361561	-0.00303586	-0.0025672	-0.00218753	-0.0018787	-0.00162603	-0.00141744	-0.0012429	-0.00109465	-0.00096741	-0.00085799	-0.00076407	-0.00068312	-0.00061228	-0.00054922	-0.00049282	-0.00044298	-0.00039966	-0.0003621	-0.0003288	-0.00029837	-0.00027024	-0.00024473	-0.0002223	-0.0002028	-0.00018526	-0.00016841	-0.00015145	-0.00013458	-0.00011864	-0.00010445	-0.00009209	-0.00008078	-0.00006935	-0.00005706	-0.00004403	-0.00003112	-0.00001932	-9.09e-6	0.	9.09e-6	0.00001932	0.00003112	0.00004403	0.00005706	0.00006935	0.00008078	0.00009209	0.00010445	0.00011864	0.00013458	0.00015145	0.00016841	0.00018526	0.0002028	0.0002223	0.00024473	0.00027024	0.00029837	0.0003288	0.0003621	0.00039966	0.00044298	0.00049282	0.00054922	0.00061227	0.00068312	0.00076407	0.00085799	0.00096741	0.00109465	0.0012429	0.00141744	0.00162603	0.0018787	0.00218753	0.0025672	0.00303586	0.00361561	0.00433231
+0.0005	-0.00457976	-0.00382254	-0.0032053	-0.00270379	-0.00229641	-0.00196469	-0.0016933	-0.00146959	-0.00128333	-0.00112653	-0.00099328	-0.00087936	-0.00078155	-0.00069707	-0.00062344	-0.00055864	-0.00050135	-0.00045084	-0.00040649	-0.00036753	-0.00033294	-0.0003018	-0.00027356	-0.00024808	-0.00022528	-0.0002049	-0.00018631	-0.00016879	-0.00015188	-0.00013556	-0.00012016	-0.00010599	-0.00009301	-0.00008082	-0.00006882	-0.00005662	-0.00004424	-0.00003208	-0.00002061	-0.00001	0.	0.00001	0.00002061	0.00003208	0.00004424	0.00005661	0.00006882	0.00008082	0.00009301	0.00010599	0.00012016	0.00013556	0.00015188	0.00016879	0.00018631	0.0002049	0.00022528	0.00024808	0.00027356	0.0003018	0.00033294	0.00036753	0.00040649	0.00045084	0.00050135	0.00055864	0.00062344	0.00069707	0.00078155	0.00087936	0.00099328	0.00112653	0.00128333	0.00146959	0.00169329	0.00196469	0.00229641	0.00270379	0.0032053	0.00382254	0.00457976
+0.	-0.00466183	-0.00389157	-0.00326208	-0.00274973	-0.00233311	-0.00199373	-0.00171605	-0.00148729	-0.00129711	-0.00113739	-0.00100204	-0.00088653	-0.0007874	-0.00070178	-0.00062725	-0.00056185	-0.00050421	-0.00045341	-0.00040872	-0.00036934	-0.00033437	-0.00030299	-0.00027467	-0.00024914	-0.00022622	-0.00020558	-0.00018669	-0.00016898	-0.00015206	-0.00013588	-0.00012061	-0.00010643	-0.00009329	-0.00008086	-0.00006871	-0.00005652	-0.00004432	-0.00003236	-0.00002096	-0.00001025	0.	0.00001025	0.00002096	0.00003236	0.00004432	0.00005652	0.00006871	0.00008086	0.00009329	0.00010643	0.00012061	0.00013588	0.00015206	0.00016898	0.00018669	0.00020558	0.00022622	0.00024914	0.00027467	0.00030299	0.00033437	0.00036934	0.00040872	0.00045341	0.00050421	0.00056185	0.00062725	0.00070179	0.0007874	0.00088653	0.00100204	0.0011374	0.00129711	0.00148729	0.00171605	0.00199373	0.00233311	0.00274973	0.00326208	0.00389157	0.00466183
+-0.0005	-0.00457976	-0.00382254	-0.0032053	-0.00270379	-0.00229641	-0.00196469	-0.0016933	-0.00146959	-0.00128333	-0.00112653	-0.00099328	-0.00087936	-0.00078155	-0.00069707	-0.00062344	-0.00055864	-0.00050135	-0.00045084	-0.00040649	-0.00036753	-0.00033294	-0.0003018	-0.00027356	-0.00024808	-0.00022528	-0.0002049	-0.00018631	-0.00016879	-0.00015188	-0.00013556	-0.00012016	-0.00010599	-0.00009301	-0.00008082	-0.00006882	-0.00005662	-0.00004424	-0.00003208	-0.00002061	-0.00001	0.	0.00001	0.00002061	0.00003208	0.00004424	0.00005662	0.00006882	0.00008082	0.00009301	0.00010599	0.00012016	0.00013556	0.00015188	0.00016879	0.00018631	0.0002049	0.00022528	0.00024808	0.00027356	0.0003018	0.00033294	0.00036753	0.00040649	0.00045084	0.00050135	0.00055864	0.00062344	0.00069707	0.00078155	0.00087936	0.00099328	0.00112653	0.00128333	0.00146959	0.00169329	0.00196469	0.00229641	0.00270379	0.0032053	0.00382254	0.00457976
+-0.001	-0.00433231	-0.00361561	-0.00303586	-0.0025672	-0.00218753	-0.0018787	-0.00162603	-0.00141744	-0.0012429	-0.00109465	-0.00096741	-0.00085799	-0.00076407	-0.00068312	-0.00061227	-0.00054921	-0.00049282	-0.00044298	-0.00039966	-0.0003621	-0.0003288	-0.00029837	-0.00027024	-0.00024473	-0.00022229	-0.0002028	-0.00018526	-0.00016841	-0.00015145	-0.00013458	-0.00011864	-0.00010445	-0.00009209	-0.00008078	-0.00006935	-0.00005706	-0.00004403	-0.00003112	-0.00001932	-9.09e-6	0.	9.09e-6	0.00001932	0.00003112	0.00004403	0.00005706	0.00006935	0.00008078	0.00009209	0.00010445	0.00011864	0.00013458	0.00015145	0.00016841	0.00018526	0.0002028	0.0002223	0.00024473	0.00027024	0.00029837	0.0003288	0.0003621	0.00039966	0.00044298	0.00049282	0.00054922	0.00061227	0.00068311	0.00076407	0.00085799	0.00096741	0.00109465	0.0012429	0.00141744	0.00162603	0.0018787	0.00218753	0.0025672	0.00303586	0.00361561	0.00433231
+-0.0015	-0.00391694	-0.00327196	-0.00275698	-0.002344	-0.00201043	-0.00173914	-0.00151724	-0.00133379	-0.00117875	-0.0010441	-0.00092567	-0.0008227	-0.00073504	-0.00066046	-0.00059476	-0.00053438	-0.00047864	-0.00042922	-0.00038762	-0.00035304	-0.00032251	-0.00029328	-0.00026477	-0.00023857	-0.00021654	-0.00019898	-0.00018395	-0.00016859	-0.00015133	-0.00013286	-0.00011541	-0.00010106	-0.00009019	-0.00008107	-0.00007104	-0.00005847	-0.00004379	-0.00002897	-0.00001628	-6.9e-6	0.	6.9e-6	0.00001628	0.00002897	0.00004379	0.00005847	0.00007104	0.00008107	0.00009019	0.00010106	0.00011541	0.00013286	0.00015133	0.00016859	0.00018395	0.00019898	0.00021654	0.00023857	0.00026477	0.00029328	0.00032251	0.00035304	0.00038762	0.00042922	0.00047864	0.00053438	0.00059476	0.00066046	0.00073504	0.0008227	0.00092567	0.00104411	0.00117875	0.00133379	0.00151724	0.00173914	0.00201042	0.002344	0.00275698	0.00327196	0.00391694
+-0.002	-0.00333312	-0.00279599	-0.00237588	-0.00204244	-0.00177254	-0.00155172	-0.00137154	-0.00122372	-0.00109663	-0.00097961	-0.00087	-0.00077294	-0.00069366	-0.00063015	-0.00057347	-0.00051617	-0.00045875	-0.0004077	-0.00036859	-0.00034037	-0.00031577	-0.00028819	-0.00025733	-0.00022811	-0.00020605	-0.00019265	-0.00018342	-0.00017134	-0.00015288	-0.00013021	-0.00010883	-0.00009381	-0.0000864	-0.00008255	-0.00007575	-0.00006238	-0.00004377	-0.00002446	-9.51e-6	-1.86e-6	0.	1.86e-6	9.51e-6	0.00002446	0.00004377	0.00006238	0.00007575	0.00008255	0.0000864	0.00009381	0.00010883	0.00013021	0.00015288	0.00017134	0.00018342	0.00019265	0.00020605	0.00022811	0.00025733	0.00028819	0.00031577	0.00034037	0.00036859	0.0004077	0.00045875	0.00051617	0.00057347	0.00063015	0.00069366	0.00077294	0.00087	0.00097961	0.00109664	0.00122372	0.00137154	0.00155172	0.00177254	0.00204244	0.00237588	0.00279599	0.00333312
+-0.0025	-0.00258751	-0.0021982	-0.00190667	-0.001678	-0.0014866	-0.00132417	-0.00119365	-0.00109436	-0.00100763	-0.0009106	-0.00080289	-0.00070482	-0.00063587	-0.00059412	-0.00055516	-0.00049973	-0.00043263	-0.00037309	-0.00033745	-0.00032436	-0.00031345	-0.00028748	-0.00024836	-0.00020971	-0.00018562	-0.00018154	-0.00018655	-0.00018169	-0.00015927	-0.00012619	-0.0000951	-0.00007775	-0.00007847	-0.00008758	-0.00008806	-0.00007213	-0.00004453	-0.00001517	5.53e-6	9.96e-6	0.	-9.96e-6	-5.53e-6	0.00001517	0.00004453	0.00007213	0.00008806	0.00008758	0.00007847	0.00007775	0.0000951	0.00012619	0.00015927	0.00018169	0.00018655	0.00018154	0.00018562	0.00020971	0.00024836	0.00028748	0.00031345	0.00032436	0.00033745	0.00037309	0.00043263	0.00049973	0.00055516	0.00059412	0.00063587	0.00070482	0.00080289	0.0009106	0.00100763	0.00109436	0.00119365	0.00132417	0.00148661	0.001678	0.00190667	0.0021982	0.00258751
+-0.003	-0.00169911	-0.00149707	-0.0013754	-0.00128053	-0.00117462	-0.00106533	-0.00098197	-0.00095333	-0.00093639	-0.00085793	-0.00072691	-0.00060416	-0.0005464	-0.00055727	-0.00055911	-0.00049762	-0.00039832	-0.00031096	-0.00027909	-0.00030667	-0.00033019	-0.00030165	-0.00023856	-0.00017496	-0.00014133	-0.00015897	-0.00020199	-0.00021231	-0.00017701	-0.00011981	-0.00006614	-0.00004036	-0.00006005	-0.00010329	-0.00011919	-0.00009425	-0.00004699	3.26e-6	0.00003873	0.00003912	0.	-0.00003912	-0.00003873	-3.26e-6	0.00004699	0.00009425	0.00011919	0.00010329	0.00006005	0.00004036	0.00006614	0.00011981	0.00017701	0.00021231	0.00020199	0.00015897	0.00014133	0.00017496	0.00023856	0.00030165	0.00033019	0.00030667	0.00027909	0.00031096	0.00039832	0.00049762	0.00055911	0.00055727	0.0005464	0.00060416	0.00072691	0.00085793	0.00093639	0.00095333	0.00098197	0.00106533	0.00117462	0.00128053	0.0013754	0.00149707	0.00169911
+-0.0035	-0.00070409	-0.00071173	-0.00083324	-0.00091577	-0.00087377	-0.00078832	-0.000704	-0.00079759	-0.00095528	-0.0008685	-0.00064316	-0.00043176	-0.00036823	-0.00053818	-0.00064488	-0.00053508	-0.0003508	-0.00018722	-0.00014271	-0.00029698	-0.00041326	-0.00035028	-0.00022743	-0.0001096	-0.00003537	-0.00009984	-0.00026226	-0.00029375	-0.00021522	-0.00010933	-8.79e-6	0.00005088	-8.28e-6	-0.0001566	-0.00019607	-0.00013753	-0.00005188	0.00003565	0.00010874	0.00011879	0.	-0.00011879	-0.00010874	-0.00003565	0.00005188	0.00013753	0.00019607	0.0001566	8.28e-6	-0.00005088	8.79e-6	0.00010933	0.00021522	0.00029375	0.00026226	0.00009984	0.00003537	0.0001096	0.00022743	0.00035028	0.00041325	0.00029698	0.00014271	0.00018722	0.0003508	0.00053508	0.00064488	0.00053818	0.00036823	0.00043176	0.00064316	0.0008685	0.00095528	0.00079759	0.000704	0.00078832	0.00087377	0.00091577	0.00083324	0.00071173	0.00070409
+# Vertical Kick in T2m2 
+START
+ 	-0.02	-0.0195	-0.019	-0.0185	-0.018	-0.0175	-0.017	-0.0165	-0.016	-0.0155	-0.015	-0.0145	-0.014	-0.0135	-0.013	-0.0125	-0.012	-0.0115	-0.011	-0.0105	-0.01	-0.0095	-0.009	-0.0085	-0.008	-0.0075	-0.007	-0.0065	-0.006	-0.0055	-0.005	-0.0045	-0.004	-0.0035	-0.003	-0.0025	-0.002	-0.0015	-0.001	-0.0005	0.	0.0005	0.001	0.0015	0.002	0.0025	0.003	0.0035	0.004	0.0045	0.005	0.0055	0.006	0.0065	0.007	0.0075	0.008	0.0085	0.009	0.0095	0.01	0.0105	0.011	0.0115	0.012	0.0125	0.013	0.0135	0.014	0.0145	0.015	0.0155	0.016	0.0165	0.017	0.0175	0.018	0.0185	0.019	0.0195	0.02
+0.0035	-0.00178037	-0.00085015	-0.0001652	0.00021376	0.0005068	0.00077419	0.00108589	0.00138408	0.00138453	0.0012642	0.0012754	0.00143686	0.00170971	0.00186702	0.00173874	0.00161062	0.00160679	0.00172176	0.0019334	0.00206338	0.00195289	0.00183243	0.00180636	0.00185431	0.00197984	0.00213619	0.00212344	0.00198567	0.00189847	0.00188117	0.00192466	0.00203394	0.00216903	0.0021609	0.00203755	0.00195202	0.00192267	0.00193907	0.00200563	0.00212711	0.00221187	0.00212711	0.00200563	0.00193907	0.00192268	0.00195202	0.00203755	0.0021609	0.00216903	0.00203394	0.00192466	0.00188117	0.00189847	0.00198567	0.00212344	0.00213619	0.00197984	0.00185431	0.00180636	0.00183243	0.00195289	0.00206338	0.0019334	0.00172176	0.00160679	0.00161062	0.00173874	0.00186702	0.00170971	0.00143686	0.0012754	0.0012642	0.00138453	0.00138409	0.00108589	0.00077419	0.0005068	0.00021376	-0.0001652	-0.00085015	-0.00178037
+0.003	-0.00470518	-0.00385915	-0.00321365	-0.00274782	-0.0023975	-0.00210773	-0.00185251	-0.0016568	-0.00156605	-0.00153602	-0.00148292	-0.0013768	-0.00124659	-0.00116697	-0.001172	-0.00119874	-0.00118616	-0.00112133	-0.00103061	-0.0009767	-0.00099255	-0.00102974	-0.00103933	-0.00100985	-0.00095002	-0.00089498	-0.0008934	-0.00093736	-0.00097743	-0.00098695	-0.00096184	-0.00091047	-0.00086336	-0.00086304	-0.00090477	-0.00094639	-0.00096424	-0.00095352	-0.00091608	-0.00086571	-0.00083978	-0.00086571	-0.00091607	-0.00095352	-0.00096424	-0.00094639	-0.00090477	-0.00086304	-0.00086336	-0.00091047	-0.00096184	-0.00098695	-0.00097743	-0.00093736	-0.0008934	-0.00089498	-0.00095003	-0.00100986	-0.00103933	-0.00102974	-0.00099255	-0.0009767	-0.00103061	-0.00112132	-0.00118616	-0.00119874	-0.001172	-0.00116697	-0.00124659	-0.0013768	-0.00148292	-0.00153602	-0.00156605	-0.0016568	-0.00185251	-0.00210773	-0.0023975	-0.00274782	-0.00321365	-0.00385915	-0.00470519
+0.0025	-0.00590974	-0.00517449	-0.00459721	-0.00415368	-0.00381104	-0.00353851	-0.00331932	-0.00315405	-0.00304482	-0.00297208	-0.00290473	-0.00282745	-0.00274998	-0.0026954600000000002	-0.0026725	-0.00266269	-0.00264244	-0.00260405	-0.00255901	-0.00252889	-0.00252334	-0.00252908	-0.00252722	-0.00250966	-0.00248135	-0.00245778	-0.00245396	-0.00246785	-0.0024832	-0.00248639	-0.00247358	-0.00245059	-0.00243157	-0.00243016	-0.00244535	-0.00246333	-0.00247176	-0.0024658	-0.0024475	-0.00242619	-0.00241644	-0.00242619	-0.0024475	-0.0024658	-0.00247176	-0.00246333	-0.00244535	-0.00243016	-0.00243157	-0.00245059	-0.00247359	-0.00248639	-0.0024832	-0.00246785	-0.00245396	-0.00245778	-0.00248135	-0.00250966	-0.00252722	-0.00252908	-0.00252334	-0.00252889	-0.00255901	-0.00260405	-0.00264244	-0.00266269	-0.0026725	-0.0026954600000000002	-0.00274998	-0.00282745	-0.00290473	-0.00297208	-0.00304482	-0.00315405	-0.00331932	-0.00353851	-0.00381104	-0.00415368	-0.00459721	-0.00517449	-0.00590974
+0.002	-0.00588383	-0.0052823	-0.00480022	-0.00441983	-0.00412114	-0.00388561	-0.00370053	-0.00355933	-0.00345589	-0.0033788	-0.00331421	-0.0032542	-0.00320041	-0.00315919	-0.00313231	-0.00311318	-0.00309283	-0.00306763	-0.00304162	-0.00302218	-0.00301252	-0.00300836	-0.00300259	-0.00299146	-0.00297699	-0.00296529	-0.00296157	-0.00296513	-0.00297	-0.0029702	-0.00296368	-0.00295323	-0.00294484	-0.00294355	-0.00294911	-0.0029564	-0.00295981	-0.00295664	-0.00294809	-0.00293883	-0.00293481	-0.00293883	-0.00294808	-0.00295664	-0.00295981	-0.0029564	-0.00294911	-0.00294355	-0.00294484	-0.00295323	-0.00296368	-0.0029702	-0.00297	-0.00296513	-0.00296157	-0.00296529	-0.00297699	-0.00299146	-0.00300259	-0.00300836	-0.00301251	-0.00302218	-0.00304162	-0.00306763	-0.00309282	-0.00311318	-0.00313231	-0.00315919	-0.00320041	-0.0032542	-0.00331421	-0.0033788	-0.00345589	-0.00355933	-0.00370053	-0.00388561	-0.00412114	-0.00441983	-0.00480022	-0.0052823	-0.00588383
+0.0015	-0.00501089	-0.00455622	-0.00418597	-0.00388923	-0.0036537	-0.00346773	-0.00332201	-0.0032096	-0.00312417	-0.0030584	-0.00300503	-0.0029594	-0.00292065	-0.00288984	-0.00286675	-0.00284859	-0.00283174	-0.00281453	-0.00279816	-0.00278507	-0.00277632	-0.00277037	-0.00276452	-0.00275722	-0.00274919	-0.00274275	-0.00273976	-0.00273989	-0.00274082	-0.00274004	-0.00273659	-0.00273167	-0.00272774	-0.00272676	-0.00272867	-0.00273144	-0.00273262	-0.00273094	-0.00272699	-0.0027229	-0.00272117	-0.0027229	-0.00272699	-0.00273094	-0.00273262	-0.00273144	-0.00272868	-0.00272676	-0.00272774	-0.00273167	-0.0027366	-0.00274004	-0.00274082	-0.00273989	-0.00273975	-0.00274275	-0.00274919	-0.00275722	-0.00276452	-0.00277037	-0.00277632	-0.00278507	-0.00279816	-0.00281453	-0.00283174	-0.00284859	-0.00286675	-0.00288984	-0.00292066	-0.0029594	-0.00300503	-0.0030584	-0.00312417	-0.0032096	-0.00332201	-0.00346773	-0.0036537	-0.00388923	-0.00418597	-0.00455622	-0.00501089
+0.001	-0.00359626	-0.0032932	-0.00304345	-0.00284122	-0.00267951	-0.00255137	-0.00245074	-0.00237256	-0.00231222	-0.00226526	-0.00222761	-0.0021965	-0.00217073	-0.00214995	-0.00213357	-0.00212026	-0.00210849	-0.00209746	-0.00208741	-0.00207912	-0.00207287	-0.00206809	-0.00206374	-0.0020592	-0.00205471	-0.00205111	-0.00204904	-0.00204835	-0.00204811	-0.00204727	-0.00204545	-0.00204311	-0.00204123	-0.00204059	-0.00204116	-0.00204211	-0.00204243	-0.00204156	-0.00203977	-0.00203798	-0.00203724	-0.00203798	-0.00203977	-0.00204156	-0.00204243	-0.00204211	-0.00204116	-0.00204059	-0.00204123	-0.00204311	-0.00204545	-0.00204727	-0.0020481	-0.00204835	-0.00204904	-0.00205111	-0.00205471	-0.0020592	-0.00206374	-0.00206809	-0.00207287	-0.00207912	-0.00208741	-0.00209746	-0.00210849	-0.00212026	-0.00213357	-0.00214995	-0.00217073	-0.0021965	-0.00222761	-0.00226526	-0.00231223	-0.00237256	-0.00245074	-0.00255137	-0.00267951	-0.00284122	-0.00304345	-0.0032932	-0.00359626
+0.0005	-0.00186885	-0.0017177	-0.0015922	-0.00148996	-0.00140784	-0.00134258	-0.00129121	-0.00125113	-0.00122001	-0.00119568	-0.00117628	-0.00116046	-0.00114749	-0.00113699	-0.00112855	-0.00112163	-0.00111561	-0.00111017	-0.00110532	-0.00110127	-0.00109806	-0.0010955	-0.00109323	-0.00109103	-0.00108897	-0.00108732	-0.00108627	-0.00108575	-0.00108544	-0.00108495	-0.00108415	-0.00108319	-0.00108242	-0.0010821	-0.00108222	-0.00108249	-0.00108254	-0.00108217	-0.00108148	-0.0010808	-0.00108052	-0.0010808	-0.00108148	-0.00108217	-0.00108254	-0.00108249	-0.00108222	-0.0010821	-0.00108242	-0.00108319	-0.00108415	-0.00108495	-0.00108544	-0.00108575	-0.00108627	-0.00108732	-0.00108897	-0.00109103	-0.00109323	-0.0010955	-0.00109806	-0.00110127	-0.00110532	-0.00111017	-0.00111561	-0.00112163	-0.00112855	-0.00113699	-0.00114749	-0.00116046	-0.00117628	-0.00119568	-0.00122001	-0.00125113	-0.00129121	-0.00134258	-0.00140784	-0.00148996	-0.0015922	-0.0017177	-0.00186885
+0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.	0.
+-0.0005	0.00186885	0.0017177	0.0015922	0.00148996	0.00140784	0.00134258	0.00129121	0.00125113	0.00122001	0.00119568	0.00117628	0.00116046	0.00114749	0.00113699	0.00112855	0.00112163	0.00111561	0.00111017	0.00110532	0.00110127	0.00109806	0.0010955	0.00109323	0.00109103	0.00108897	0.00108732	0.00108627	0.00108575	0.00108543	0.00108495	0.00108415	0.00108319	0.00108242	0.0010821	0.00108222	0.00108249	0.00108255	0.00108217	0.00108148	0.0010808	0.00108052	0.0010808	0.00108148	0.00108217	0.00108254	0.0010825	0.00108222	0.0010821	0.00108242	0.00108319	0.00108415	0.00108495	0.00108544	0.00108575	0.00108627	0.00108732	0.00108897	0.00109103	0.00109323	0.0010955	0.00109806	0.00110127	0.00110532	0.00111017	0.00111561	0.00112163	0.00112855	0.00113699	0.00114749	0.00116046	0.00117628	0.00119568	0.00122001	0.00125113	0.00129121	0.00134258	0.00140784	0.00148996	0.0015922	0.0017177	0.00186885
+-0.001	0.00359626	0.0032932	0.00304345	0.00284122	0.00267951	0.00255137	0.00245074	0.00237256	0.00231223	0.00226526	0.00222761	0.00219651	0.00217073	0.00214995	0.00213357	0.00212026	0.00210849	0.00209746	0.00208741	0.00207912	0.00207287	0.00206809	0.00206374	0.0020592	0.00205471	0.00205111	0.00204904	0.00204835	0.0020481	0.00204727	0.00204545	0.00204311	0.00204123	0.00204059	0.00204116	0.00204211	0.00204243	0.00204156	0.00203977	0.00203798	0.00203724	0.00203798	0.00203977	0.00204156	0.00204243	0.00204211	0.00204116	0.00204059	0.00204123	0.00204311	0.00204545	0.00204727	0.00204811	0.00204835	0.00204904	0.00205111	0.00205471	0.0020592	0.00206374	0.00206809	0.00207287	0.00207912	0.00208741	0.00209745	0.00210849	0.00212026	0.00213357	0.00214995	0.00217073	0.0021965	0.00222761	0.00226526	0.00231222	0.00237256	0.00245074	0.00255137	0.00267951	0.00284122	0.00304345	0.0032932	0.00359626
+-0.0015	0.00501089	0.00455622	0.00418597	0.00388923	0.0036537	0.00346773	0.00332201	0.0032096	0.00312417	0.0030584	0.00300503	0.0029594	0.00292065	0.00288984	0.00286675	0.00284859	0.00283174	0.00281453	0.00279816	0.00278507	0.00277632	0.00277037	0.00276452	0.00275722	0.00274919	0.00274275	0.00273975	0.00273989	0.00274082	0.00274004	0.00273659	0.00273167	0.00272774	0.00272676	0.00272867	0.00273144	0.00273262	0.00273094	0.00272699	0.0027229	0.00272117	0.0027229	0.00272699	0.00273094	0.00273262	0.00273144	0.00272867	0.00272676	0.00272774	0.00273167	0.00273659	0.00274004	0.00274082	0.00273989	0.00273976	0.00274275	0.00274919	0.00275722	0.00276452	0.00277037	0.00277632	0.00278507	0.00279816	0.00281454	0.00283174	0.00284859	0.00286675	0.00288984	0.00292066	0.0029594	0.00300502	0.0030584	0.00312417	0.0032096	0.00332201	0.00346773	0.0036537	0.00388923	0.00418597	0.00455622	0.00501089
+-0.002	0.00588383	0.0052823	0.00480022	0.00441983	0.00412114	0.00388561	0.00370053	0.00355933	0.00345589	0.0033788	0.00331421	0.0032542	0.00320041	0.00315919	0.00313231	0.00311318	0.00309282	0.00306763	0.00304162	0.00302218	0.00301252	0.00300836	0.00300259	0.00299146	0.00297699	0.00296529	0.00296157	0.00296513	0.00297	0.0029702	0.00296368	0.00295322	0.00294484	0.00294355	0.00294911	0.0029564	0.00295981	0.00295664	0.00294809	0.00293883	0.00293481	0.00293883	0.00294809	0.00295664	0.00295981	0.0029564	0.00294911	0.00294355	0.00294484	0.00295323	0.00296368	0.0029702	0.00297	0.00296513	0.00296157	0.00296529	0.00297699	0.00299146	0.00300259	0.00300836	0.00301251	0.00302218	0.00304162	0.00306763	0.00309283	0.00311318	0.00313231	0.00315919	0.00320041	0.0032542	0.00331421	0.0033788	0.00345589	0.00355933	0.00370053	0.00388561	0.00412114	0.00441983	0.00480022	0.0052823	0.00588383
+-0.0025	0.00590974	0.00517449	0.00459721	0.00415368	0.00381104	0.00353851	0.00331932	0.00315405	0.00304482	0.00297208	0.00290473	0.00282745	0.00274998	0.00269545	0.0026725	0.00266269	0.00264244	0.00260405	0.00255901	0.00252889	0.00252334	0.00252908	0.00252722	0.00250966	0.00248135	0.00245778	0.00245396	0.00246785	0.0024832	0.00248639	0.00247359	0.00245059	0.00243157	0.00243016	0.00244535	0.00246333	0.00247176	0.0024658	0.0024475	0.00242619	0.00241644	0.00242619	0.0024475	0.0024658	0.00247176	0.00246333	0.00244535	0.00243016	0.00243157	0.00245059	0.00247359	0.00248639	0.0024832	0.00246785	0.00245396	0.00245778	0.00248135	0.00250966	0.00252722	0.00252908	0.00252334	0.00252889	0.00255901	0.00260405	0.00264244	0.00266269	0.0026725	0.00269545	0.00274998	0.00282745	0.00290473	0.00297208	0.00304482	0.00315405	0.00331932	0.00353851	0.00381104	0.00415368	0.00459721	0.00517449	0.00590974
+-0.003	0.00470518	0.00385915	0.00321365	0.00274782	0.0023975	0.00210773	0.00185251	0.0016568	0.00156605	0.00153602	0.00148292	0.0013768	0.00124659	0.00116697	0.001172	0.00119874	0.00118616	0.00112132	0.00103061	0.0009767	0.00099255	0.00102975	0.00103933	0.00100985	0.00095003	0.00089498	0.0008934	0.00093736	0.00097743	0.00098695	0.00096184	0.00091047	0.00086336	0.00086304	0.00090477	0.00094639	0.00096424	0.00095352	0.00091607	0.00086571	0.00083978	0.00086571	0.00091607	0.00095352	0.00096424	0.00094639	0.00090477	0.00086304	0.00086336	0.00091047	0.00096184	0.00098695	0.00097743	0.00093736	0.0008934	0.00089498	0.00095003	0.00100985	0.00103933	0.00102974	0.00099255	0.0009767	0.00103061	0.00112133	0.00118616	0.00119874	0.001172	0.00116697	0.00124659	0.0013768	0.00148292	0.00153602	0.00156605	0.0016568	0.00185251	0.00210773	0.0023975	0.00274782	0.00321365	0.00385915	0.00470519
 -0.0035	0.00178037	0.00085015	0.0001652	-0.00021376	-0.0005068	-0.00077419	-0.00108589	-0.00138409	-0.00138453	-0.0012642	-0.0012754	-0.00143686	-0.00170971	-0.00186702	-0.00173874	-0.00161063	-0.00160679	-0.00172176	-0.0019334	-0.00206338	-0.00195289	-0.00183243	-0.00180636	-0.00185431	-0.00197984	-0.00213619	-0.00212344	-0.00198567	-0.00189847	-0.00188117	-0.00192466	-0.00203394	-0.00216903	-0.0021609	-0.00203755	-0.00195202	-0.00192268	-0.00193907	-0.00200563	-0.00212711	-0.00221187	-0.00212711	-0.00200563	-0.00193907	-0.00192267	-0.00195202	-0.00203755	-0.00216089	-0.00216903	-0.00203394	-0.00192466	-0.00188117	-0.00189847	-0.00198567	-0.00212344	-0.00213619	-0.00197984	-0.00185431	-0.00180636	-0.00183243	-0.00195289	-0.00206338	-0.0019334	-0.00172176	-0.00160679	-0.00161062	-0.00173874	-0.00186702	-0.00170971	-0.00143686	-0.0012754	-0.0012642	-0.00138453	-0.00138409	-0.00108589	-0.00077419	-0.0005068	-0.00021376	0.0001652	0.00085015	0.00178037
\ No newline at end of file
diff --git a/tracy/tools/Makefile.am b/tracy/tools/Makefile.am
index 03b2c6131f22df0b0c94d9e59bdf8c8dfe8753d7..6323b5f282e5ccfa4a7862bf870b91ead46f2ea7 100644
--- a/tracy/tools/Makefile.am
+++ b/tracy/tools/Makefile.am
@@ -1,13 +1,13 @@
 #SUBDIRS = ptc
 
-bin_PROGRAMS      = max4 
+#bin_PROGRAMS      = max4 
+#bin_PROGRAMS      = soltracy 
+bin_PROGRAMS      = testtracy
 #bin_PROGRAMS      = max4 track_fft dynap leac
 
-max4_SOURCES      = max4.cc nrutil.c nrcheck.c nrlinwww.c   nrframe.c
-track_fft_SOURCES = track_fft.cc
-dynap_SOURCES     = dynap.cc
-leac_SOURCES      = leac.cc
-
+#max4_SOURCES      = max4.cc nrutil.c nrcheck.c nrlinwww.c   nrframe.c
+#soltracy_SOURCES  = soltracy.cc nrutil.c nrcheck.c nrlinwww.c   nrframe.c
+testtracy_SOURCES = testtracy.cc nrutil.c nrcheck.c nrlinwww.c   nrframe.c
 
 #LIBS     = -L$(TRACY_LIB)/tracy/lib -ltracy -L$(NUM_REC)/lib -lnum_rec \
 #	    -L/usr/lib/gcc-lib/i586-mandriva-linux-gnu/3.3.6 -lstdc++ -lg2c
diff --git a/tracy/tools/nrlinwww.c b/tracy/tools/nrlinwww.c
index 7f678ba4add72f2dcecaa31bf942d368d8cfa62b..b19468bda44951f316e3ec2d51fa6373d9812390 100644
--- a/tracy/tools/nrlinwww.c
+++ b/tracy/tools/nrlinwww.c
@@ -219,8 +219,15 @@ float **matrix(I_ARG_T nrl, I_ARG_T nrh, I_ARG_T ncl, I_ARG_T nch)
 	return m;
 }
 
+/****************************************************************************/
+/* double **dmatrix(I_ARG_T nrl, I_ARG_T nrh, I_ARG_T ncl, I_ARG_T nch)
+
+   Purpose:
+       allocate memory for a double matrix 
+       with subscript range m[nrl..nrh][ncl..nch]
+           
+****************************************************************************/
 double **dmatrix(I_ARG_T nrl, I_ARG_T nrh, I_ARG_T ncl, I_ARG_T nch)
-/* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
 {
 	I_ARG_T i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
 	double **m;
@@ -460,9 +467,15 @@ void free_f3tensor(float ***t, I_ARG_T nrl, I_ARG_T nrh, I_ARG_T ncl, I_ARG_T nc
 
 /* matrix inversion ref - Page 48 */
 
+/****************************************************************************/
+/* void dinverse( double **a, int n, double **y )
 
+   Purpose:
+       Find inverse of 'a' (decomposed in process!) and return as 'y'  
+       
+    
+****************************************************************************/
 void dinverse( double **a, int n, double **y )
-/*  Find inverse of 'a' (decomposed in process!) and return as 'y' */
 {
 	double d, *col;
 	int i, j, *indx /* integer vector */;
diff --git a/tracy/tools/nrutil.c b/tracy/tools/nrutil.c
index 195576d595a112be376eb483ddf7e339f03d66ec..542f0a3383af8ad9e879214fe302ae8ec93cbece 100644
--- a/tracy/tools/nrutil.c
+++ b/tracy/tools/nrutil.c
@@ -73,8 +73,15 @@ void dmvmult( double **a, int a_rows, int a_cols, double *b, int b_els, double *
    }
 }
 
+/****************************************************************************/
+/* void dmadd( double **a, int a_rows, int a_cols, double **b, double **y)
+
+   Purpose:
+       add two matrices a, b, result in y. y can be same as a or b 
+       
+    
+****************************************************************************/
 void dmadd( double **a, int a_rows, int a_cols, double **b, double **y)
-/* add two matrices a, b, result in y. y can be same as a or b */
 {
    int i, j;
 
@@ -93,8 +100,15 @@ void dmadd( double **a, int a_rows, int a_cols, double **b, double **y)
       }
 }
 
+/****************************************************************************/
+/* void dmsmy( double **a, int a_rows, int a_cols, double r, double **y)
+
+   Purpose:
+       multiply a by scalar r, result in y. y can be same as a 
+       
+    
+****************************************************************************/
 void dmsmy( double **a, int a_rows, int a_cols, double r, double **y)
-/* multiply a by scalar r, result in y. y can be same as a */
 {
    int i, j;
 
@@ -111,8 +125,15 @@ void dmsmy( double **a, int a_rows, int a_cols, double r, double **y)
       }
 }
 
+/****************************************************************************/
+/* void dmsub( double **a, int a_rows, int a_cols, double **b, double **y)
+
+   Purpose:
+       subtract two matrices a, b, result in y. y can be same as a or b 
+       
+    
+****************************************************************************/
 void dmsub( double **a, int a_rows, int a_cols, double **b, double **y)
-/* subtract two matrices a, b, result in y. y can be same as a or b */
 {
    int i, j;
 
@@ -618,7 +639,15 @@ double dReadScalar(FILE *datafile, int *error)
 }
 
 
-void dmcopy( double **a, int a_rows, int a_cols, double **b) /* copy a matrix */
+/****************************************************************************/
+/* void identity_mat(const int n, double** A)
+
+   Purpose:
+           copy  matrix a to matrix b 
+       
+    
+****************************************************************************/
+void dmcopy( double **a, int a_rows, int a_cols, double **b) 
 {
 	int i, j;
 
diff --git a/tracy/tools/soltracy.cc b/tracy/tools/soltracy.cc
new file mode 100644
index 0000000000000000000000000000000000000000..aded39b218c91de605796e487abed050467885de
--- /dev/null
+++ b/tracy/tools/soltracy.cc
@@ -0,0 +1,225 @@
+#define ORDER 1          
+
+int no_tps = ORDER; // arbitrary TPSA order is defined locally
+
+extern bool  freq_map;
+
+#include "tracy_lib.h"
+#include "time.h"
+#include <sys/time.h>
+
+//***************************************************************************************
+//
+//  MAIN CODE
+//
+//****************************************************************************************
+ int main(int argc, char *argv[])
+{
+  const long  seed = 1121;
+  const bool All = TRUE;
+  iniranf(seed); setrancut(2.0);
+
+  // turn on globval.Cavity_on and globval.radiation to get proper synchr radiation damping
+  // IDs accounted too if: wiggler model and symplectic integrator (method = 1)
+  globval.H_exact     = false; 
+  globval.quad_fringe = false;                // quadrupole fringe field
+  
+  globval.radiation   = false;                // synchrotron radiation
+  globval.emittance   = false;                 // emittance
+  globval.pathlength  = false; 
+//  globval.bpm         = 0;
+  
+  const double  x_max_FMA = 10e-3, delta_FMA = 10e-2;
+  const int     n_x = 80, n_dp = 80, n_tr = 2048;
+  
+  double nux=0, nuz=0, ksix=0, ksiz=0;
+  
+  bool chroma;
+  double dP = 0.0;
+  long lastpos = -1L;
+  char str1[S_SIZE];
+  
+  /************************************************************************
+      start read in files and flags
+  *************************************************************************/
+  read_script(argv[1], true);
+
+
+ /************************************************************************
+    end  read in files and flags
+  *************************************************************************/
+  prtmfile("flat_file.dat"); // writes flat file   /* very important file for debug*/
+  printlatt();  /* SOLEIL print out lattice functions */
+  printglob();
+  
+  
+  
+    // Flag factory
+    
+    // Chamber factory
+  if (ChamberFlag == false)
+     ChamberOff(); // turn off vacuum chamber setting, use the default one
+  else if (ChamberNoU20Flag == true)
+     DefineChNoU20();  // using vacuum chamber setting but without undulator U20
+  else if (ReadChamberFlag == true)
+     ReadCh(chamber_file); /* read vacuum chamber from a file "Apertures.dat" , soleil version*/
+  PrintCh();
+ 
+ 
+  // compute tunes by tracking (should be the same as by DA)
+  if (TuneTracFlag == true) {
+    GetTuneTrac(1026L, 0.0, &nux, &nuz);
+    fprintf(stdout,"From tracking: nux = % f nuz = % f \n",nux,nuz);
+  }
+
+  // compute chromaticities by tracking (should be the same as by DA)
+  if (ChromTracFlag == true){
+    GetChromTrac(2L, 1026L, 1e-5, &ksix, &ksiz);
+    fprintf(stdout,"From tracking: ksix= % f ksiz= % f \n",ksix,ksiz);
+  }
+
+
+  if (FitTuneFlag == true){
+    fprintf(stdout, "\n Fitting tunes\n");
+    FitTune(ElemIndex("qp7"),ElemIndex("qp9"), targetnux, targetnuz);
+    Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
+    printglob();                      /* print parameter list */
+  }
+
+  if (FitChromFlag == true){
+    fprintf(stdout, "\n Fitting chromaticities\n");
+    FitChrom(ElemIndex("sx9"),ElemIndex("sx10"), targetksix, targetksiz);
+    Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
+    printglob();                      /* print parameter list */
+  }
+
+    //SetKLpar(ElemIndex("QT"), 1, 2L,   0.001026770838382);
+  
+  // coupling calculation
+   if (CouplingFlag == true){
+     Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
+     printlatt();                      /* dump linear lattice functions into "linlat.dat" */
+  //   Coupling_Edwards_Teng();
+     printglob();   /* print parameter list */
+   }
+
+  // add coupling by random rotating of the quadrupoles
+  if (ErrorCouplingFlag == true){
+    SetErr();
+    Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
+    printlatt();                      /* dump linear lattice functions into "linlat.dat" */
+//    Coupling_Edwards_Teng();
+    printglob();   /* print parameter list */
+  }
+
+  // WARNING Fit tunes and chromaticities before applying errors !!!!
+  //set multipoles in all magnets
+  if (MultipoleFlag == true ){
+    if (ThinsextFlag ==true){
+      fprintf(stdout, "\n Setting Multipoles for lattice with thin sextupoles \n");
+      Multipole_thinsext();  /* for thin sextupoles */
+      
+      Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
+      printglob(); 
+     }
+    else{
+      fprintf(stdout, "\n Setting Multipoles for lattice with thick sextupoles \n");
+      Multipole_thicksext();  /* for thick sextupoles */
+      
+      Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
+      printglob(); 
+    }
+  }
+ // Computes FMA
+  if (FmapFlag == true){
+    if (ChamberFlag == true ){
+      if (ExperimentFMAFlag == true)
+         fmap(40,12,258,-20e-3,5e-3,0.0,true); // for experimental
+      if (DetailedFMAFlag == true)
+        fmap(100,50,1026,20e-3,5e-3,0.0,true);
+      }
+      else{
+        if (ExperimentFMAFlag == true)
+          fmap(40,12,258,-32e-3,5e-3,0.0,true);
+        if (DetailedFMAFlag == true)
+          fmap(200,100,1026,32e-3,7e-3,0.0,true);
+      }
+  }
+  
+  if (CodeComparaisonFlag){
+          fmap(200,100,1026,-32e-3,7e-3,0.0,true);
+  }
+
+  if (MomentumAccFlag == true){
+    MomentumAcceptance(1L, 108L, 0.01, 0.05, 100L, -0.01, -0.05, 100L);
+  }
+
+  // computes Tuneshift with amplitudes
+  if (TuneShiftFlag == true){
+    if (ChamberFlag == true ){
+      NuDx(31L,21L,516L,0.025,0.005,dP);
+      NuDp(31L,1026L,0.06);
+      }
+      else{
+        NuDx(50L,30L,516L,0.035,0.02,dP);
+        NuDp(31L,1026L,0.06);
+      }
+
+  }
+
+  // induced amplitude 
+  if (InducedAmplitudeFlag == true){
+      InducedAmplitude(193L);  
+  }
+  
+  if (EtaFlag == true){
+  // compute cod and twiss parameters for different energy offsets    
+    for (int ii=0; ii<=40; ii++) { 
+    dP = -0.02+ 0.001*ii;
+    Ring_GetTwiss(chroma=false, dP);   /* Compute and get Twiss parameters */
+    printlatt();                      /* dump linear lattice functions into "linlat.dat" */
+    getcod (dP, lastpos);
+//     printcod();
+    prt_cod("cod.out", globval.bpm, true);
+    //system("mv linlat.out linlat_ooo.out"); 
+    sprintf(str1, "mv cod.out cod_%02d.out", ii); 
+    system(str1);
+    sprintf(str1, "mv linlat.out linlat_%02d.out", ii); 
+    system(str1);
+    }
+}    
+  
+
+  if (PhaseSpaceFlag == true){
+     int   t_sec,t_usec,t_msec; 
+     struct timeval tv,tv1;
+     struct timezone tz,tz1 ;
+     struct tm *tm,*tm1;
+     gettimeofday(&tv, &tz);
+     tm=localtime(&tv.tv_sec);
+     
+     t_sec = - tm->tm_sec;
+     printf(" %d:%02d:%02d %d \n", tm->tm_hour, tm->tm_min,
+             tm->tm_sec, tv.tv_usec);
+
+      Phase(0.001,0,0.001,0,0.001,0, 1);
+      
+       gettimeofday(&tv1, &tz1);
+       tm1=localtime(&tv1.tv_sec);
+     
+       t_sec += tm->tm_sec;
+       t_usec = tv1.tv_usec -  tv.tv_usec; 
+       t_msec = t_sec*1000 + t_usec/1000;
+         
+       
+      printf(" %d:%02d:%02d %d \n", tm1->tm_hour, tm1->tm_min,
+             tm1->tm_sec, tv1.tv_usec);
+      
+     
+       printf("%d %d %d \n",t_sec*1000,t_usec/1000, t_msec);
+     printf("the simulation time for phase space in tracy 3 is %ld milliseconds\n",t_msec);  
+   
+  }
+}    
+  
+  
\ No newline at end of file
diff --git a/tracy/tools/testtracy.cc b/tracy/tools/testtracy.cc
new file mode 100644
index 0000000000000000000000000000000000000000..750d7f688eac48b94531623ff0cff993c4683191
--- /dev/null
+++ b/tracy/tools/testtracy.cc
@@ -0,0 +1,466 @@
+#define ORDER 1          
+
+int no_tps = ORDER; // arbitrary TPSA order is defined locally
+
+extern bool  freq_map;
+
+#include "tracy_lib.h"
+#include "time.h"
+#include <sys/time.h>
+
+//***************************************************************************************
+//
+//  MAIN CODE
+//
+//****************************************************************************************
+ int main(int argc, char *argv[])
+{
+  const long  seed = 1121;
+  const bool All = TRUE;
+  iniranf(seed); setrancut(2.0);
+
+  // turn on globval.Cavity_on and globval.radiation to get proper synchr radiation damping
+  // IDs accounted too if: wiggler model and symplectic integrator (method = 1)
+  globval.H_exact     = false; 
+  globval.quad_fringe = false;                // quadrupole fringe field
+  
+  globval.radiation   = false;                // synchrotron radiation
+  globval.emittance   = false;                 // emittance
+  globval.pathlength  = false; 
+//  globval.bpm         = 0;
+  
+ 
+  // overview, on energy: 25-15
+  //const double  x_max_FMA = 20e-3, y_max_FMA = 10e-3; //const x_max_FMA = 25e-3, y_max_FMA = 15e-3;
+  //const int     n_x = 80, n_y = 80, n_tr = 2048;
+  // overview, off energy: 10-10
+  const double  x_max_FMA = 10e-3, delta_FMA = 10e-2;
+  const int     n_x = 80, n_dp = 80, n_tr = 2048;
+  //
+  // zoom, on energy: 8-2.5
+  //const double  x_max_FMA = 8e-3, y_max_FMA = 2.5e-3;
+  //const int     n_x = 64, n_y = 15, n_tr = 2048;
+  // zoom, off energy: 7-3
+  //const double  x_max_FMA = 3e-3, delta_FMA = 7e-2;
+  //const int     n_x = 28, n_dp = 56, n_tr = 2048;
+  
+  double nux=0, nuz=0, ksix=0, ksiz=0;
+  
+  bool chroma;
+  double dP = 0.0;
+  long lastpos = -1L;
+  char str1[S_SIZE];
+  
+  /************************************************************************
+      start read in files and flags
+  *************************************************************************/
+  read_script(argv[1], true);
+
+
+ /************************************************************************
+    end  read in files and flags
+  *************************************************************************/
+    
+  
+  
+  
+  
+  
+//  if (true)
+//  //  Read_Lattice("/home/nadolski/codes/tracy/maille/soleil/solamor2_tracy3"); 
+//    Read_Lattice(argv[1]); //sets some globval params
+//  else
+//    rdmfile("flat_file.dat"); //instead of reading lattice file, get data from flat file 
+
+  //no_sxt(); //turns off sextupoles
+ // Ring_GetTwiss(true, 0e-2); //gettwiss computes one-turn matrix arg=(w or w/o chromat, dp/p) 
+  //get_matching_params_scl();
+  //get_alphac2();
+  //GetEmittance(ElemIndex("cav"), true);
+  
+//prt_lat("linlat.out", globval.bpm, true);  /* print lattice file for nsrl-ii*/
+prtmfile("flat_file.dat"); // writes flat file   /* very important file for debug*/
+//prt_chrom_lat(); //writes chromatic functions into chromlat.out
+//  printlatt();  /* print out lattice functions */
+ 
+
+/* print lattice file */
+//  prt_lat("linlatBNL.out", globval.bpm, All); // BNL print for all elements
+  printlatt();  /* SOLEIL print out lattice functions */
+  printglob();
+  
+  
+  
+    // Flag factory
+//  bool TuneTracFlag = true; 
+//  bool ChromTracFlag = true;
+  
+  
+  //*************************************************************
+  //=============================================================
+
+    
+    // Chamber factory
+  if (ChamberFlag == false)
+     ChamberOff(); // turn off vacuum chamber setting, use the default one
+  else if (ChamberNoU20Flag == true)
+     DefineChNoU20();  // using vacuum chamber setting but without undulator U20
+  else if (ReadChamberFlag == true)
+     ReadCh(chamber_file); /* read vacuum chamber from a file "Apertures.dat" , soleil version*/
+//LoadApers("Apertures.dat", 1.0, 1.0);  /* read vacuum chamber definition for bnl */
+  PrintCh();
+ 
+ 
+  // compute tunes by tracking (should be the same as by DA)
+  if (TuneTracFlag == true) {
+    GetTuneTrac(1026L, 0.0, &nux, &nuz);
+    fprintf(stdout,"From tracking: nux = % f nuz = % f \n",nux,nuz);
+  }
+
+  // compute chromaticities by tracking (should be the same as by DA)
+  if (ChromTracFlag == true){
+    GetChromTrac(2L, 1026L, 1e-5, &ksix, &ksiz);
+    fprintf(stdout,"From tracking: ksix= % f ksiz= % f \n",ksix,ksiz);
+  }
+
+
+  if (FitTuneFlag == true){
+    fprintf(stdout, "\n Fitting tunes\n");
+    FitTune(ElemIndex("qp7"),ElemIndex("qp9"), targetnux, targetnuz);
+    Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
+    printglob();                      /* print parameter list */
+  }
+
+  if (FitChromFlag == true){
+    fprintf(stdout, "\n Fitting chromaticities\n");
+    FitChrom(ElemIndex("sx9"),ElemIndex("sx10"), targetksix, targetksiz);
+    Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
+    printglob();                      /* print parameter list */
+  }
+
+    //SetKLpar(ElemIndex("QT"), 1, 2L,   0.001026770838382);
+  
+  // coupling calculation
+   if (CouplingFlag == true){
+     Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
+     printlatt();                      /* dump linear lattice functions into "linlat.dat" */
+     Coupling_Edwards_Teng();
+     printglob();   /* print parameter list */
+   }
+
+  // add coupling by random rotating of the quadrupoles
+  if (ErrorCouplingFlag == true){
+    SetErr();
+    Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
+    printlatt();                      /* dump linear lattice functions into "linlat.dat" */
+    Coupling_Edwards_Teng();
+    printglob();   /* print parameter list */
+  }
+
+  // WARNING Fit tunes and chromaticities before applying errors !!!!
+  //set multipoles in all magnets
+  if (MultipoleFlag == true ){
+    if (ThinsextFlag ==true){
+      fprintf(stdout, "\n Setting Multipoles for lattice with thin sextupoles \n");
+      Multipole_thinsext();  /* for thin sextupoles */
+      
+      Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
+      printglob(); 
+     }
+    else{
+      fprintf(stdout, "\n Setting Multipoles for lattice with thick sextupoles \n");
+      Multipole_thicksext();  /* for thick sextupoles */
+      
+      Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
+      printglob(); 
+    }
+  }
+                       /* print parameter list */
+  
+
+  // PX2 chicane
+//  if (PX2Flag ==true){
+//  setPX2chicane(); 
+//  //get closed orbit    
+//  getcod (0.0, &lastpos);
+//  printcod();
+//  Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
+//  printglob();                      /* print parameter list */
+//  }
+  
+ // Computes FMA
+  if (FmapFlag == true){
+    if (ChamberFlag == true ){
+      if (ExperimentFMAFlag == true)
+         fmap(40,12,258,-20e-3,5e-3,0.0,true); // for experimental
+      if (DetailedFMAFlag == true)
+        fmap(100,50,1026,20e-3,5e-3,0.0,true);
+      }
+      else{
+        if (ExperimentFMAFlag == true)
+          fmap(40,12,258,-32e-3,5e-3,0.0,true);
+        if (DetailedFMAFlag == true)
+          fmap(200,100,1026,32e-3,7e-3,0.0,true);
+      }
+  }
+  
+  if (CodeComparaisonFlag){
+          // SOLEIL
+          fmap(100,50,1026,32e-3,7e-3,0.0,true);
+          //fmap(200,100,1026,-32e-3,7e-3,0.0,true);
+  }
+
+  if (MomentumAccFlag == true){
+    //MomentumAcceptance(10L, 28L, 0.01, 0.05, 4L, -0.01, -0.05, 4L);
+     MomentumAcceptance(1L, 28L, 0.01, 0.05, 40L, -0.01, -0.05, 40L);
+  //  MomentumAcceptance(1L, 108L, 0.01, 0.05, 100L, -0.01, -0.05, 100L);
+  }
+
+  // computes Tuneshift with amplitudes
+  if (TuneShiftFlag == true){
+    if (ChamberFlag == true ){
+      NuDx(31L,21L,516L,0.025,0.005,dP);
+      //NuDp(31L,516L,0.06);
+      //NuDp(31L,1026L,0.06);
+      }
+      else{
+        NuDx(50L,30L,516L,0.035,0.02,dP);
+        NuDp(31L,1026L,0.06);
+      }
+
+  }
+ 
+//  if (SigmaFlag == true){printsigma();
+//  }
+//  
+
+  // induced amplitude 
+  if (InducedAmplitudeFlag == true){
+      InducedAmplitude(193L);  
+  }
+  
+  if (EtaFlag == true){
+  // compute cod and twiss parameters for different energy offsets    
+    for (int ii=0; ii<=40; ii++) { 
+    dP = -0.02+ 0.001*ii;
+    Ring_GetTwiss(chroma=false, dP);   /* Compute and get Twiss parameters */
+    printlatt();                      /* dump linear lattice functions into "linlat.dat" */
+    getcod (dP, lastpos);
+//     printcod();
+    prt_cod("cod.out", globval.bpm, true);
+    //system("mv linlat.out linlat_ooo.out"); 
+    sprintf(str1, "mv cod.out cod_%02d.out", ii); 
+    system(str1);
+    sprintf(str1, "mv linlat.out linlat_%02d.out", ii); 
+    system(str1);
+    }
+}    
+  
+
+  if (PhaseSpaceFlag == true){
+     int   t_sec,t_usec,t_msec; 
+     struct timeval tv,tv1;
+     struct timezone tz,tz1 ;
+     struct tm *tm,*tm1;
+     gettimeofday(&tv, &tz);
+     tm=localtime(&tv.tv_sec);
+     
+     t_sec = - tm->tm_sec;
+     printf(" %d:%02d:%02d %d \n", tm->tm_hour, tm->tm_min,
+             tm->tm_sec, tv.tv_usec);
+
+      Phase(0.001,0,0.001,0,0.001,0, 1);
+      
+       gettimeofday(&tv1, &tz1);
+       tm1=localtime(&tv1.tv_sec);
+     
+       t_sec += tm->tm_sec;
+       t_usec = tv1.tv_usec -  tv.tv_usec; 
+       t_msec = t_sec*1000 + t_usec/1000;
+         
+       
+      printf(" %d:%02d:%02d %d \n", tm1->tm_hour, tm1->tm_min,
+             tm1->tm_sec, tv1.tv_usec);
+      
+     
+       printf("%d %d %d \n",t_sec*1000,t_usec/1000, t_msec);
+     printf("the simulation time for phase space in tracy 3 is %ld milliseconds\n",t_msec);  
+   
+  }
+  
+
+  
+  //*********************************************************************************
+  //---------------------------------------------------------------------------------------------------------------------------------
+  
+  // Delicated for max4 lattice. To load alignment error files and do correction
+  if (false) {
+    //prt_cod("cod.out", globval.bpm, true); //prints a specific closed orbit with corrector strengths
+    
+    
+    globval.bpm = ElemIndex("bpm_m");                   //definition for max4 lattice, bpm
+  //  globval.bpm = ElemIndex("bpm");                         
+    globval.hcorr = ElemIndex("corr_h"); globval.vcorr = ElemIndex("corr_v");    //definition for max4 lattice, correctors
+   // globval.hcorr = ElemIndex("ch"); globval.vcorr = ElemIndex("cv");
+    globval.gs = ElemIndex("GS"); globval.ge = ElemIndex("GE");   //definition for max4 lattice, girder maker
+    
+    
+    //compute response matrix (needed for OCO)
+    gcmat(globval.bpm, globval.hcorr, 1);  gcmat(globval.bpm, globval.vcorr, 2);
+     
+
+    //print response matrix (routine in lsoc.cc)
+    //prt_gcmat(globval.bpm, globval.hcorr, 1);  prt_gcmat(globval.bpm, globval.vcorr, 2);
+       
+    //gets response matrix, does svd, evaluates correction for N seed orbits
+    //get_cod_rms_scl(dx, dy, dr, n_seed)
+    //get_cod_rms_scl(100e-6, 100e-6, 1.0e-3, 100); //trim coils aren't reset when finished
+       
+
+    //for alignments errors check LoadAlignTol (in nsls_ii_lib.cc) and AlignErr.dat
+    //CorrectCOD_N("/home/simon/projects/src/lattice/AlignErr.dat", 3, 1, 1);
+   
+    //for field errors check LoadFieldErr(in nsls_ii_lib.cc) and FieldErr.dat
+    //LoadFieldErr("/home/simon/projects/src/lattice/FieldErr.dat", true, 1.0, true);
+   
+    //for alignments errors check LoadAlignTol (in nsls_ii_lib.cc) and AlignErr.dat
+    //LoadAlignTol("/home/simon/projects/src/lattice/AlignErr.dat", true, 1.0, true, 1);
+    //LoadAlignTol("/home/simon/projects/out/20091126/AlignErr.dat", true, 1.0, true, 1);
+    //prt_cod("cod_err.out", globval.bpm, true); //prints a specific closed orbit with corrector strengths
+   
+      
+    // delicated for max4 lattice
+    //load alignment errors and field errors, correct orbit, repeat N times, and get statistics
+    get_cod_rms_scl_new(1); //trim coils aren't reset when finished
+  
+    
+    //for aperture limitations use LoadApers (in nsls_ii_lib.cc) and Apertures.dat
+    //globval.Aperture_on = true;
+    //LoadApers("/home/simon/projects/src/lattice/Apertures.dat", 1, 1);
+    
+  }
+
+
+
+//*******************************************************************************
+//-------------------------------------------------------------------------------------------------------------------------------------  
+
+  // Call nsls-ii_lib.cc
+  // tune shift with amplitude 
+  double delta = 0;
+  if (false) {
+    cout << endl;
+    cout << "computing tune shifts" << endl;
+    dnu_dA(20e-3, 10e-3, 0.0);
+    get_ksi2(delta); // this gets the chromas and writes them into chrom2.out
+ // get_ksi2(5.0e-2); // this gets the chromas and writes them into chrom2.out
+  }
+  
+  if (false) {
+    //fmap(n_x, n_y, n_tr, x_max_FMA, y_max_FMA, 0.0, true, false);
+//    fmapdp(n_x, n_dp, n_tr, x_max_FMA, -delta_FMA, 1e-3, true, false); // always use -delta_FMA (+delta_FMA appears broken)
+    fmapdp(n_x, n_dp, n_tr, x_max_FMA, -delta_FMA, 1e-3, true); // always use -delta_FMA (+delta_FMA appears broken)
+  } else {
+    globval.Cavity_on = true; // this gives longitudinal motion
+    globval.radiation = false; // this adds ripple around long. ellipse (needs many turns to resolve damp.)
+    //globval.Aperture_on = true;
+    //LoadApers("/home/simon/projects/src/lattice/Apertures.dat", 1, 1);
+    //get_dynap_scl(delta, 512);
+  }
+  
+  
+
+  
+  
+  
+  
+  //
+  // IBS & TOUSCHEK
+  //
+  int     k, n_turns;
+  double  sigma_s, sigma_delta, tau, alpha_z, beta_z, gamma_z, eps[3];
+  FILE    *outf;
+  const double  Qb   = 5e-9;
+  
+  if (false) {
+    double  sum_delta[globval.Cell_nLoc+1][2];
+    double  sum2_delta[globval.Cell_nLoc+1][2];
+    
+    GetEmittance(ElemIndex("cav"), true);
+    
+    // initialize momentum aperture arrays
+    for(k = 0; k <= globval.Cell_nLoc; k++){
+      sum_delta[k][0] = 0.0; sum_delta[k][1] = 0.0;
+      sum2_delta[k][0] = 0.0; sum2_delta[k][1] = 0.0;
+    }
+    
+    //sigma_delta = 7.70e-04;      //410:7.70e-4,  411:9.57e-4,  412:9.12e-4
+    //globval.eps[X_] = 0.326e-9;  //410:3.26e-10, 411:2.63e-10, 412:2.01e-10
+    globval.eps[Y_] = 0.008e-9;
+    //sigma_s = 9.73e-3;           //410:9.73e-3,  411:12.39e-3, 412:12.50e-3/10.33e-3
+    //globval.eps[Z_] = sigma_delta*sigma_s;
+    //globval.delta_RF given by cav voltage in lattice file
+     //globval.delta_RF = 6.20e-2; //410:6.196e-2, 411:5.285e-2, 412:4.046e-2/5.786e-2
+    n_turns = 490;               //410:490(735), 411:503(755), 412:439(659)/529(794)
+    
+
+    alpha_z =
+      -globval.Ascr[ct_][ct_]*globval.Ascr[delta_][ct_]
+      - globval.Ascr[ct_][delta_]*globval.Ascr[delta_][delta_];
+    beta_z = sqr(globval.Ascr[ct_][ct_]) + sqr(globval.Ascr[ct_][delta_]);
+    gamma_z = (1+sqr(alpha_z))/beta_z;
+    
+    sigma_delta = sqrt(gamma_z*globval.eps[Z_]);
+    sigma_s = sqrt(beta_z*globval.eps[Z_]);//50e-3
+    beta_z = sqr(sigma_s)/globval.eps[Z_];
+    alpha_z = sqrt(beta_z*gamma_z-1);
+
+    // INCLUDE LC (LC changes sigma_s and eps_z, but has no influence on sigma_delta)
+    if (false) {
+      double  newLength, bunchLengthening;
+      newLength = 50e-3;
+      bunchLengthening = newLength/sigma_s;
+      sigma_s = newLength;
+      globval.eps[Z_] = globval.eps[Z_]*bunchLengthening;
+      beta_z = beta_z*bunchLengthening;
+      gamma_z = gamma_z/bunchLengthening;
+      alpha_z = sqrt(beta_z*gamma_z-1);  // this doesn't change
+    }
+    
+    //globval.eps[X_] = 0.362e-9;
+    //sigma_delta = 1.04e-3;
+    //sigma_s = 14.8e-3;
+    Touschek(Qb, globval.delta_RF, globval.eps[X_], globval.eps[Y_],
+	     sigma_delta, sigma_s);
+    
+    
+    // IBS
+    if (false) {       
+      // initialize eps_IBS with eps_SR
+      for(k = 0; k < 3; k++)
+	eps[k] = globval.eps[k];
+      for(k = 0; k < 20; k++) //prototype (looping because IBS routine doesn't check convergence)
+	IBS(Qb, globval.eps, eps, alpha_z, beta_z);
+     }
+    
+
+    // TOUSCHEK TRACKING
+    if (false) {       
+      globval.Aperture_on = true;
+      LoadApers("/home/simon/projects/src/lattice/Apertures.dat", 1, 1);
+      tau = Touschek(Qb, globval.delta_RF, false,
+		     globval.eps[X_], globval.eps[Y_],
+		     sigma_delta, sigma_s,
+		     n_turns, true, sum_delta, sum2_delta); //the TRUE flag requires apertures loaded
+      
+      printf("Touschek lifetime = %10.3e hrs\n", tau/3600.0);
+      
+      outf = file_write("mom_aper.out");
+      for(k = 0; k <= globval.Cell_nLoc; k++)
+	fprintf(outf, "%4d %7.2f %5.3f %6.3f\n",
+		k, Cell[k].S, 1e2*sum_delta[k][0], 1e2*sum_delta[k][1]);
+      fclose(outf);
+    }
+  
+  }
+}
diff --git a/tracy/tracy/inc/field.h b/tracy/tracy/inc/field.h
index f8f99c149ac365fcf54533484b7fde8b7403a2df..eff09fa39a9e3608a176447028e019a21a5f2322 100644
--- a/tracy/tracy/inc/field.h
+++ b/tracy/tracy/inc/field.h
@@ -373,6 +373,25 @@ inline bool operator!=(const tps &a, const double b)
 inline bool operator!=(const double a, const tps &b)
 { return a != b.cst(); }
 
+/******************************************************
+template<typename T>
+inline ss_vect<T>::ss_vect(void)
+
+  Purpose:
+     Initialize 6-D vector(T:double) or 6*6 matrix(T:tps) ss, based on
+     the type of T, if ORDER = 1
+      Initialize 7-D vector or 7*7 matrix ss, based on
+     the type of T, if ORDER != 1
+     
+    
+     
+  
+  
+  Parameters:
+      ss_dim:  6      if ORDER = 1
+      ss_dim:  6+1    if ORDER != 1  
+     
+******************************************************/
 
 template<typename T>
 inline ss_vect<T>::ss_vect(void)
diff --git a/tracy/tracy/inc/read_script.h b/tracy/tracy/inc/read_script.h
new file mode 100644
index 0000000000000000000000000000000000000000..8640d907f52baf92e08cb187f6cc5df154430e6d
--- /dev/null
+++ b/tracy/tracy/inc/read_script.h
@@ -0,0 +1,21 @@
+/* global flag, used in script_read() and main() for read the input from script*/
+extern bool TuneTracFlag;
+extern bool ChromTracFlag ;
+extern bool FmapFlag , ExperimentFMAFlag, DetailedFMAFlag;
+extern bool TuneShiftFlag ;
+extern bool ErrorCouplingFlag ; extern bool CouplingFlag ;
+extern bool MomentumAccFlag ;
+extern bool MultipoleFlag , ThinsextFlag ;
+extern bool FitTuneFlag  ; extern double targetnux , targetnuz ;
+extern bool FitChromFlag ; extern double targetksix, targetksiz ;
+extern bool ChamberFlag , ChamberNoU20Flag , ReadChamberFlag;
+extern bool GirderErrorFlag ;
+extern bool SigmaFlag ;
+extern bool PX2Flag;
+extern bool InducedAmplitudeFlag ;
+extern bool CodeComparaisonFlag ;
+extern bool EtaFlag;
+extern bool PhaseSpaceFlag;  
+extern char chamber_file[max_str];
+
+void read_script(const char *param_file_name, bool rd_lat);
diff --git a/tracy/tracy/inc/soleillib.h b/tracy/tracy/inc/soleillib.h
index d7c77aa7d38adb5912c9a2d3863a6affcc76547f..18b507397be84de5170ec18baa89492fcf502c05 100644
--- a/tracy/tracy/inc/soleillib.h
+++ b/tracy/tracy/inc/soleillib.h
@@ -30,9 +30,11 @@ void PhasePortrait(double x0,double px0,double z0, double pz0, double delta0, do
                           double end, long Nb, long Nbtour, int num);
 void PhasePortrait2(long pos,double x0,double px0,double z0, double pz0, double delta0, double ctau,
                           double end, long Nb, long Nbtour, int num);
-void Multipole(void);
+void Multipole_thicksext(void);
+void Multipole_thinsext(void);
 void MomentumAcceptance(long deb, long fin, double ep_min, double ep_max, long nstepp,
                         double em_min, double em_max, long nstepm);
+void ReadCh(const char *AperFile);
 void Trac_Tab(double x, double px, double y, double py, double dp,
             long nmax, long pos, long *lastn, long *lastpos, FILE *outf1, double Tx[][NTURN]);
 void SetSkewQuad(void);
@@ -56,10 +58,12 @@ void NuDx(long Nbx, long Nbz, long Nbtour, double xmax, double ymax,
                  double energy);
 
 /* Vacuum chamber */
+void ChamberOn(void);
 void DefineCh(void);
+void DefineChNoU20(void);
 void Enveloppe(double x, double px, double y, double py,
                       double dp, double nturn);
-void ChamberOn(void);
+
 
 /* Longitudinal Hamiltonian*/
 void PhaseLongitudinalHamiltonien(void);
@@ -79,3 +83,6 @@ void fmapfull(long Nbx, long Nbz, long Nbtour, double xmax, double zmax,
               double energy, bool diffusion);
 void spectrum(long Nbx, long Nbz, long Nbtour, double xmax, double zmax,
               double energy, bool diffusion);
+	      
+/* coupling*/
+void Coupling_Edwards_Teng(void);	      
diff --git a/tracy/tracy/inc/tracy.h b/tracy/tracy/inc/tracy.h
index e3482e1a3167060ca12d73c375d3f1789dde66cc..ae8a5aa08e0650b9eb58bb24e9709e5799fce946 100644
--- a/tracy/tracy/inc/tracy.h
+++ b/tracy/tracy/inc/tracy.h
@@ -46,7 +46,7 @@
 #define maxfil          10
 #define bigvectmax      4096
 
-#define HOMmax          21
+#define HOMmax          30
 
 #define maxtransfmat    2000
 #define maxkicks        5
diff --git a/tracy/tracy/inc/tracy_global.h b/tracy/tracy/inc/tracy_global.h
index 949c14ca17e2e62003bb0ce1d4d69cfbe3007b86..d0c8958671cc652e92e8461a1d2b28932c8e5789 100644
--- a/tracy/tracy/inc/tracy_global.h
+++ b/tracy/tracy/inc/tracy_global.h
@@ -10,16 +10,16 @@ typedef struct globvalrec {
                 Alphac;         // alphap 
   Vector2       Chrom;          // chromaticities 
   double        Energy;         // ring energy 
-  long          Cell_nLoc,      // number of elements 
+  long          Cell_nLoc,      // number of elements in a cell
                 Elem_nFam,      // number of families 
                 CODimax;        /* maximum number of cod search before
 				   failing */
   double        CODeps;         // precision for closed orbit finder
   Vector        CODvect;        // closed orbit
-  int           bpm;            // bpm number 
-  int           hcorr;          // horizontal corrector number 
-  int           vcorr;          // vertical corrector number 
-  int           qt;             // vertical corrector number 
+  long int         bpm;            // bpm number 
+  long int       hcorr;          // horizontal corrector number 
+  long int       vcorr;          // vertical corrector number 
+  long int         qt;             // vertical corrector number 
   int           gs;             // girder start marker 
   int           ge;             // girder end marker 
   Matrix        OneTurnMat,     // oneturn matrix 
@@ -245,8 +245,8 @@ struct ElemFamType {
 
 // LEGO block structure for each element of the lattice
 struct CellType {
-  int              Fnum;        // Element Family #
-  int              Knum;        // Element Kid #
+  long int              Fnum;        // Element Family #
+  long int              Knum;        // Element Kid #
   double           S;           // Position in the ring
   CellType*        next_ptr;    // pointer to next cell (for tracking)
   Vector2          dS,          // Transverse displacement
diff --git a/tracy/tracy/inc/tracy_lib.h b/tracy/tracy/inc/tracy_lib.h
index 26904358e501eb29733e4f40d3da16732565311e..9db68d6139f2ecc52d8d613c40bf3bc1acc515b0 100644
--- a/tracy/tracy/inc/tracy_lib.h
+++ b/tracy/tracy/inc/tracy_lib.h
@@ -89,6 +89,7 @@ using namespace std;
 
 #include "max4_lib.h"
 #include "soleilcommon.h"
+#include "read_script.h"
 
 // Truncated Power Series Algebra (TPSA)
 extern const int  nv_tps, nd_tps, ndpt_tps, iref_tps;
diff --git a/tracy/tracy/src/mathlib.cc b/tracy/tracy/src/mathlib.cc
index 2df544c9fe1837b6fb250d08fcd338de5e02a245..86e318c4c5ece423d75c05cb71e744779982b4ed 100644
--- a/tracy/tracy/src/mathlib.cc
+++ b/tracy/tracy/src/mathlib.cc
@@ -154,6 +154,32 @@ double xabs(long n, Vector &x)
 }
 
 
+/****************************************************************************/
+/* void UnitMat(const int n, Matrix &A)
+
+   Purpose:
+       generate a unit matrix A of rank n
+
+   Input:
+         n  rank of matrix A
+	 A  matrix
+
+   Output:
+       none
+
+   Return:
+       matrix A
+
+   Global variables:
+       
+
+   Specific functions:
+       
+   Comments:
+       Parameter passed to variable A must be defined as type Matrix 
+
+****************************************************************************/
+
 void UnitMat(const int n, Matrix &A)
 {
   int i, j;
@@ -168,7 +194,30 @@ void UnitMat(const int n, Matrix &A)
   }
 }
 
+/****************************************************************************/
+/* void ZeroMat(const int n, Matrix &A)
 
+   Purpose:
+       generate a zero matrix A of rank n
+
+   Input:
+       
+
+   Output:
+       none
+
+   Return:
+       matrix A
+
+   Global variables:
+       
+
+   Specific functions:
+       
+   Comments:
+        Parameter passed to variable A must be defined as type Matrix 
+
+****************************************************************************/
 void ZeroMat(const int n, Matrix &A)
 {
   int i, j;
@@ -179,7 +228,31 @@ void ZeroMat(const int n, Matrix &A)
   }
 }
 
+/****************************************************************************/
+/* void CopyMat(const int n, const Matrix &A, Matrix &B)
+
+   Purpose:
+       copy the contents of matrix A to matrix B.
+       Matrix A and Matrix B both have rank n.
 
+   Input:
+       n:  rank of matrix A and B
+     A,B:  n*n matrices
+   Output:
+       none
+
+   Return:
+       matrix B
+
+   Global variables:
+       
+
+   Specific functions:
+       
+   Comments:
+        Parameter passed to variable A and B must be defined as type Matrix 
+
+****************************************************************************/
 void CopyMat(const int n, const Matrix &A, Matrix &B)
 {
   int i;
@@ -188,26 +261,75 @@ void CopyMat(const int n, const Matrix &A, Matrix &B)
     CopyVec(n, A[i], B[i]);
 }
 
+/****************************************************************************/
+/* void AddMat(const int n, const Matrix &A, Matrix &B)
+
+   Purpose:
+       add matrix A and matrix B, and assign the result to matrix B
+       Matrix A and Matrix B both have rank n.
+
+   Input:
+       n:  rank of matrix A and B
+     A,B:  n*n matrices
+   Output:
+       none
+
+   Return:
+       matrix B
+
+   Global variables:
+       
 
-void AddMat(const int n, const Matrix &a, Matrix &b)
+   Specific functions:
+       
+   Comments:
+        Parameter passed to variable A and B must be defined as type Matrix 
+
+****************************************************************************/
+void AddMat(const int n, const Matrix &A, Matrix &B)
 {
   int i;
 
   for (i = 0; i < n; i++)
-    AddVec(n, a[i], b[i]);
+    AddVec(n, A[i], B[i]);
 }
 
-void SubMat(const int n, const Matrix &a, Matrix &b)
+/****************************************************************************/
+/* void SubMat(const int n, const Matrix &A, Matrix &B)
+
+   Purpose:
+       substract matrix A from matrix B, and assign the result to matrix B
+       Matrix A and Matrix B both have rank n.
+
+   Input:
+       n:  rank of matrix A and B
+     A,B:  n*n matrices
+   Output:
+       none
+
+   Return:
+       matrix B
+
+   Global variables:
+       
+
+   Specific functions:
+       
+   Comments:
+        Parameter passed to variable A and B must be defined as type Matrix 
+
+****************************************************************************/
+void SubMat(const int n, const Matrix &A, Matrix &B)
 {
   /*n : integer; VAR a, b : matrix*/
   int i;
 
   for (i = 0; i < n; i++)
-    SubVec(n, a[i], b[i]);
+    SubVec(n, A[i], B[i]);
 }
 
 
-void LinTrans(const int n, const Matrix &a, Vector &x)
+void LinTrans(const int n, const Matrix &A, Vector &x)
 {
   int i, j;
   Vector y;
@@ -215,7 +337,7 @@ void LinTrans(const int n, const Matrix &a, Vector &x)
   for (i = 0; i < n; i++) {
     y[i] = 0e0;
     for (j = 0; j < n; j++)
-      y[i] += a[i][j]*x[j];
+      y[i] += A[i][j]*x[j];
   }
   CopyVec(n, y, x);
 }
diff --git a/tracy/tracy/src/nsls-ii_lib.cc b/tracy/tracy/src/nsls-ii_lib.cc
index 7ba3ac3fa5e26053c614203413655d5737614002..2b70a18bb1282994f501395a6d3cc6989feb72fc 100644
--- a/tracy/tracy/src/nsls-ii_lib.cc
+++ b/tracy/tracy/src/nsls-ii_lib.cc
@@ -407,7 +407,31 @@ double get_eps_x(void)
   return eps_x;
 }
 
+/****************************************************************************/
+/* void GetEmittance(const int Fnum, const bool prt)
 
+   Purpose:
+       get the emittance
+
+   Input:
+      Fnum  family index
+      prt   bool flag, true = print the calcuate information
+   Output:
+       none
+
+   Return:
+       emittance
+       
+   Global variables:
+       
+
+   specific functions:
+       
+
+   Comments:
+       
+
+****************************************************************************/
 void GetEmittance(const int Fnum, const bool prt)
 {
   bool          emit, rad, cav, path;
@@ -423,7 +447,7 @@ void GetEmittance(const int Fnum, const bool prt)
   rad = globval.radiation; emit = globval.emittance;
   cav = globval.Cavity_on; path = globval.pathlength;
 
-  C = Cell[globval.Cell_nLoc].S;
+  C = Cell[globval.Cell_nLoc].S; /* circumferance of the ring*/
 
   // damped system
   globval.radiation = true; globval.emittance  = true;
@@ -1236,7 +1260,34 @@ void LoadAlignTol(const char *AlignFile, const bool Scale_it,
   fclose(fp);
 }
 
+/****************************************************************************/
+/* void set_aper_elem(const int Fnum, const int Knum, 
+		   const double Dxmin, const double Dxmax, 
+		   const double Dymin, const double Dymax) 
+
+   Purpose:
+      Define vacuum chamber to the element  
+      
+
+   Input:
+      none
+
+   Output:
+       none
+
+   Return:
+       none
+
+   Global variables:
+       none
 
+   Specific functions:
+       none
+
+   Comments:
+      Add k=0 for the default start point "start" in tracy.
+
+****************************************************************************/
 // apertures
 
 void set_aper_elem(const int Fnum, const int Knum, 
@@ -1244,12 +1295,46 @@ void set_aper_elem(const int Fnum, const int Knum,
 		   const double Dymin, const double Dymax) 
 { 
   int  k; 
- 
-    k = Elem_GetPos(Fnum, Knum); 
-    Cell[k].maxampl[X_][0] = Dxmin; Cell[k].maxampl[X_][1] = Dxmax; 
-    Cell[k].maxampl[Y_][0] = Dymin; Cell[k].maxampl[Y_][1] = Dymax; 
+  
+  if(Fnum==0 && Knum==0)
+    k=0;
+  else
+    k = Elem_GetPos(Fnum, Knum);
+     
+    Cell[k].maxampl[X_][0] = Dxmin; 
+    Cell[k].maxampl[X_][1] = Dxmax; 
+    Cell[k].maxampl[Y_][0] = Dymin; 
+    Cell[k].maxampl[Y_][1] = Dymax; 
  } 
 
+ /****************************************************************************/
+/* void set_aper_fam(const int Fnum,
+		  const double Dxmin, const double Dxmax, 
+		  const double Dymin, const double Dymax)
+   Purpose:
+      Define vacuum chamber to the family "Fnum"  
+      
+
+   Input:
+      none
+      
+   Output:
+       none
+
+   Return:
+       none
+
+   Global variables:
+       none
+
+   Specific functions:
+       none
+
+   Comments:
+       Enumeration of special variables:
+                 enum { All = 0, Dip = 1, Quad = 2, Sext = 3, Oct = 4, Dec = 5, Dodec = 6 }
+
+****************************************************************************/
 void set_aper_fam(const int Fnum,
 		  const double Dxmin, const double Dxmax, 
 		  const double Dymin, const double Dymax)
@@ -1260,21 +1345,85 @@ void set_aper_fam(const int Fnum,
     set_aper_elem(Fnum, k, Dxmin, Dxmax, Dymin, Dymax);
 }
 
+/****************************************************************************/
+/* void set_aper_type(const int type, const double Dxmin, const double Dxmax, 
+		   const double Dymin, const double Dymax) 
+
+   Purpose:
+      Define vacuum chamber to the elements with the same "type", either for "all" element, or "quad","sext", etc.  
+      
+
+   Input:
+       none
+       
+   Output:
+       none
+
+   Return:
+       none
+
+   Global variables:
+       none
+
+   Specific functions:
+       none
+
+   Comments:
+      1) Enumeration of special variables:
+                 enum { All = 0, Dip = 1, Quad = 2, Sext = 3, Oct = 4, Dec = 5, Dodec = 6 }
+      2) change the start point from k=1 to k=0, in order to set the aperture for
+         the default start point "begin" which is not a lattice element.
+
+****************************************************************************/
 void set_aper_type(const int type, const double Dxmin, const double Dxmax, 
 		   const double Dymin, const double Dymax)
 {
   long int   k;
 
-  if (type >= All && type <= HOMmax) {
-    for(k = 1; k <= globval.Cell_nLoc; k++)
-      if (((Cell[k].Elem.Pkind == Mpole) &&
+  if (type >= All && type <= HOMmax) 
+    {
+ //   for(k = 1; k <= globval.Cell_nLoc; k++)
+      for(k = 0; k <= globval.Cell_nLoc; k++)
+        if (((Cell[k].Elem.Pkind == Mpole) &&
 	   (Cell[k].Elem.M->n_design == type)) || (type == All))
 	set_aper_elem(Cell[k].Fnum, Cell[k].Knum, Dxmin, Dxmax, Dymin, Dymax);
-  } else
+     } 
+  else
     printf("set_aper_type: bad design type %d\n", type);
 }
 
 
+/****************************************************************************/
+/* void LoadApers(const char *AperFile, const double scl_x, const double scl_y) 
+
+   Purpose:
+      Read vacuum chamber from file "AperFile", and scale the values with scl_x and scl_y.
+      Assign vacuum chamber to all elements, if there are definition of 
+      chamber at quad and sext, then assign the specific values at these elements.
+      
+
+   Input:
+       AperFile:  file with chamber definition
+       scl_x   :  scale factor of x limitation
+       scl_y   : scale factor of y limitation
+
+   Output:
+       none
+
+   Return:
+       none
+
+   Global variables:
+       none
+
+   Specific functions:
+       none
+
+   Comments:
+       Enumeration of special variables:
+                 enum { All = 0, Dip = 1, Quad = 2, Sext = 3, Oct = 4, Dec = 5, Dodec = 6 }
+
+****************************************************************************/
 void LoadApers(const char *AperFile, const double scl_x, const double scl_y) 
 {
   char    line[max_str], Name[max_str];
@@ -1290,10 +1439,15 @@ void LoadApers(const char *AperFile, const double scl_x, const double scl_y)
   printf("...Load and Set Apertures.\n");
 
   while (fgets(line, max_str, fp) != NULL) {
-    if (strstr(line, "#") == NULL) {
+    if (strstr(line, "#") == NULL) 
+    {
       sscanf(line,"%s %lf %lf %lf %lf",
 	     Name, &dxmin, &dxmax, &dymin, &dymax);
-      dxmin *= scl_x; dxmax *= scl_x; dymin *= scl_y; dymax *= scl_y;
+      dxmin *= scl_x; 
+      dxmax *= scl_x; 
+      dymin *= scl_y; 
+      dymax *= scl_y;
+      
       if (strcmp("all", Name)==0) {
 	if(prt)
 	  printf("setting all apertures to"
@@ -1301,19 +1455,25 @@ void LoadApers(const char *AperFile, const double scl_x, const double scl_y)
 		 dxmin, dxmax, dymin, dymax);
 	set_aper_type(All, dxmin, dxmax, dymin, dymax);
 	//	ini_aper(dxmin, dxmax, dymin, dymax); 
-      } else if (strcmp("quad", Name)==0) {
+       }
+        
+      else if (strcmp("quad", Name)==0) {
 	if(prt)
 	  printf("setting apertures at all quads to"
 		 " dxmin = %e, dxmax = %e, dymin = %e, dymax = %e\n",
 		 dxmin, dxmax, dymin, dymax);  
 	set_aper_type(Quad, dxmin, dxmax, dymin, dymax);
-      } else if (strcmp("sext", Name) == 0) {
+       }
+        
+      else if (strcmp("sext", Name) == 0) {
 	if(prt)
 	  printf("setting apertures at all sextupoles to"
 		 " dxmin = %e, dxmax = %e, dymin = %e, dymax = %e\n",
 		 dxmin, dxmax, dymin, dymax);
 	set_aper_type(Sext, dxmin, dxmax, dymin, dymax);
-      } else {
+       }
+        
+      else {
 	Fnum = ElemIndex(Name);
 	if(Fnum > 0) {
 	  if(prt)
@@ -1321,10 +1481,12 @@ void LoadApers(const char *AperFile, const double scl_x, const double scl_y)
 		   " dxmin = %e, dxmax = %e, dymin = %e, dymax = %e\n",
 		   Name, dxmin, dxmax, dymin, dymax);
 	  set_aper_fam(Fnum, dxmin, dxmax, dymin, dymax);
-	} else 
+	  } 
+	 else 
 	  printf("LoadApers: lattice does not contain element %s\n", Name);
       }
-    } else
+    } 
+   else
       printf("%s", line);
   }
     
@@ -2792,7 +2954,8 @@ void A_matrix(void)
   // Defining Twiss in undisturbed quads
   for (k = 0; k < Nquad; k++)
     for (j = 0; j <= 1; j++) {
-      qb0[j][k] = qb[j][k]; qNu0[j][k] = qNu[j][k];
+      qb0[j][k] = qb[j][k]; 
+      qNu0[j][k] = qNu[j][k];
     }
   
   // Defining Twiss in undisturbed sexts
@@ -3332,7 +3495,39 @@ double Touschek(const double Qb, const double delta_RF,
   return(1.0/(tau));
 }
 
+/****************************************************************************/
+/* void mom_aper(double &delta, double delta_RF, const long int k,
+	      const int n_turn, const bool positive)
+
 
+   Purpose:
+      Using Binary search to determine momentum aperture at location k.
+      If the particle is stable at the last step of tracking, so the 
+      momentum acceptance is RF momentum accpetance.
+      
+   Input:
+           delta       particle energy deviation
+	   delta_RF    RF momentum acceptance
+	   k           element index
+	   n_turn      number of tracking turns  
+
+   Output:
+           none
+   Return:
+           delta       searched momentum acceptance
+
+   Global variables:
+         none
+
+   specific functions:
+       Cell_Pass
+
+   Comments:
+       nsls-ii version. 
+       See also:
+       soleil version MomentumAcceptance( ) in soleillib.cc
+       
+****************************************************************************/
 void mom_aper(double &delta, double delta_RF, const long int k,
 	      const int n_turn, const bool positive)
 {
@@ -3344,22 +3539,25 @@ void mom_aper(double &delta, double delta_RF, const long int k,
 
   const double  eps = 1e-4;
 
-  delta_min = 0.0; delta_max = positive ? fabs(delta_RF) : -fabs(delta_RF);
-  while (fabs(delta_max-delta_min) > eps) {
-    delta = (delta_max+delta_min)/2.0; 
+  delta_min = 0.0; 
+  delta_max = positive ? fabs(delta_RF) : -fabs(delta_RF);
+  
+  while (fabs(delta_max-delta_min) > eps) 
+  {
+    delta = (delta_max+delta_min)/2.0; /* binary serach*/
     
     // propagate initial conditions
     CopyVec(6, globval.CODvect, x); Cell_Pass(0, k, x, lastpos); 
     // generate Touschek event
     x[delta_] += delta;
-  
     // complete one turn
     Cell_Pass(k, globval.Cell_nLoc, x, lastpos); 
+    
     if (lastpos < globval.Cell_nLoc)
       // particle lost
       delta_max = delta;
     else { 
-      // track
+      // track 
       for(j = 0; j < n_turn; j++) {
 	Cell_Pass(0, globval.Cell_nLoc, x, lastpos); 
 
@@ -4065,6 +4263,28 @@ void get_alphac(void)
   globval.Alphac = globval.OneTurnMat[ct_][delta_]/Cell.S;
 }
 
+/******************************************************************
+void get_alphac2(void)
+
+  Purpose:
+           Calculate momentum compact factor up to third order
+	   
+ Input:
+       none
+
+   Output:
+       none
+
+   Return:
+      alphac
+
+   specific functions:
+      none
+
+   Comments:
+       none	   
+
+*******************************************************************/
 
 void get_alphac2(void)
 {
@@ -4081,14 +4301,17 @@ void get_alphac2(void)
   CellType  Cell;
   
   globval.pathlength = false;
-  getelem(globval.Cell_nLoc, &Cell); n = 0;
+  getelem(globval.Cell_nLoc, &Cell); 
+  n = 0;
+  
   for (i = -n_points; i <= n_points; i++) {
-    n++; delta[n-1] = i*(double)d_delta/(double)n_points;
+    n++; 
+    delta[n-1] = i*(double)d_delta/(double)n_points;
     for (j = 0; j < nv_; j++)
       x[j] = 0.0;
-    x[delta_] = delta[n-1];
-    Cell_Pass(0, globval.Cell_nLoc, x, lastpos);
-    alphac[n-1] = x[ct_]/Cell.S;
+      x[delta_] = delta[n-1];
+      Cell_Pass(0, globval.Cell_nLoc, x, lastpos);
+      alphac[n-1] = x[ct_]/Cell.S;
   }
   pol_fit(n, delta, alphac, 3, b, sigma);
   printf("\n");
@@ -4201,9 +4424,10 @@ void get_ab(const ss_vect<tps> &A,
   A_Atp = A*tp_S(2, A);
 
   for (k = 0; k <= 1; k++) {
-    eta[k] = A[2*k][delta_]; etap[k] = A[2*k+1][delta_];
-
-    alpha[k] = -A_Atp[2*k][2*k+1]; beta[k] = A_Atp[2*k][2*k];
+      eta[k] = A[2*k][delta_]; 
+     etap[k] = A[2*k+1][delta_];
+    alpha[k] = -A_Atp[2*k][2*k+1]; 
+     beta[k] = A_Atp[2*k][2*k];
   }
 }
 
diff --git a/tracy/tracy/src/pascalio.cc b/tracy/tracy/src/pascalio.cc
index 8e748eaede8d9f14e04a50cbd437a815e31084b9..671ac5b24846249dba1527d2f41bf05958ef7bcb 100644
--- a/tracy/tracy/src/pascalio.cc
+++ b/tracy/tracy/src/pascalio.cc
@@ -44,7 +44,8 @@ void t2init(void)
 void exit_(int exit_code)
 {
 
-  printf("fatal error, <ret> to continue "); cin.ignore(1, '\n');
+  printf("fatal error, <ret> to continue "); 
+  cin.ignore(1, '\n');
 
   exit(exit_code);
 }
diff --git a/tracy/tracy/src/physlib.cc b/tracy/tracy/src/physlib.cc
index 4194fbd3f37c407271fcb23ca6dcc2a8517f5d6d..a664f98c4852f85c1c0e0aab8bcfab682fcb0f5b 100644
--- a/tracy/tracy/src/physlib.cc
+++ b/tracy/tracy/src/physlib.cc
@@ -91,7 +91,7 @@ void printglob(void)
          globval.dPcommon, globval.dPparticle, globval.Energy);
   printf("  MaxAmplx [m] = %9.3e  MaxAmply [m] = %9.3e"
 	 "  RFAccept [%%] = \xB1%4.2f\n",
-         Cell[0].maxampl[X_][0], Cell[0].maxampl[Y_][0],
+         Cell[0].maxampl[X_][1], Cell[0].maxampl[Y_][1],
 	 globval.delta_RF*1e2);
   printf("  MatMeth      =  %s     ", globval.MatMeth ? "TRUE " : "FALSE");
   printf(" Cavity_On    =  %s    ", globval.Cavity_on ? "TRUE " : "FALSE");
@@ -643,7 +643,7 @@ void track_(double r, struct LOC_getdynap *LINK)
 
    Purpose:
       Single particle tracking w/ respect to the chamber centrum
-
+        Based on the version of tracy 2 in soleillib.c
    Input:
       x, px, y, py 4 transverses coordinates
       ctau         c*tau
@@ -658,7 +658,8 @@ void track_(double r, struct LOC_getdynap *LINK)
       outf1       output file with 6D phase at different pos
 
    Return:
-       none
+         lastn:  last tracking turn that particle is not lost 
+       lastpos:   last element position that particle is not lost
 
    Global variables:
        globval
@@ -673,6 +674,10 @@ void track_(double r, struct LOC_getdynap *LINK)
        26/04/03 print output for phase space is for position pos now
        01/12/03 tracking from 0 to pos -1L instead of 0 to pos
        (wrong observation point)
+       
+       23/07/10 change the call variable in Cell_Pass( ): pos-1L --> pos (L704, L717).
+                 since the Cell_Pass( ) is tracking from element i0 to i1(tracy 3), and
+		       the Cell_Pass( ) is tracking from element i0+1L to i1(tracy 2).
 
 ****************************************************************************/
 void Trac(double x, double px, double y, double py, double dp, double ctau,
@@ -691,28 +696,35 @@ void Trac(double x, double px, double y, double py, double dp, double ctau,
   x1[2] = y; x1[3] = py;
   x1[4] =dp; x1[5] = ctau;
 
-  lastn = 0L;
+  lastn = 0L;  (lastpos) = pos;
   lostF = true;
-
-  (lastpos)=pos;
+  
   if(trace) fprintf(outf1, "\n");
   fprintf(outf1, "%6ld %+10.5e %+10.5e %+10.5e %+10.5e %+10.5e %+10.5e \n", lastn,
                  x1[0], x1[1], x1[2], x1[3], x1[4], x1[5]);
-  Cell_Pass(pos -1L, globval.Cell_nLoc, x1, lastpos);
+  
+		 
+//  Cell_Pass(pos -1L, globval.Cell_nLoc, x1, lastpos);
+  Cell_Pass(pos, globval.Cell_nLoc, x1, lastpos);
 
   if (lastpos == globval.Cell_nLoc)
-  do {
-    (lastn)++;
-    Cell_Pass(0L,pos-1L, x1, lastpos);
-    if(!trace) {
-      fprintf(outf1, "%6ld %+10.5e %+10.5e %+10.5e %+10.5e %+10.5e %+10.5e \n",
-	      lastn, x1[0], x1[1], x1[2], x1[3], x1[4], x1[5]);
-    }
-    if (lastpos == pos-1L) Cell_Pass(pos-1L,globval.Cell_nLoc, x1, lastpos);
-  }
-  while (((lastn) < nmax) && ((lastpos) == globval.Cell_nLoc));
+    do 
+    {
+         (lastn)++;  /* 3) continue tracking for nmax turns*/
+         Cell_Pass(0L,pos-1L, x1, lastpos); /* 1) check whether particle is stable at element from 0 to pos-1L*/
+    
+         if(!trace) 
+         fprintf(outf1, "%6ld %+10.5e %+10.5e %+10.5e %+10.5e %+10.5e %+10.5e \n",
+	                 lastn, x1[0], x1[1], x1[2], x1[3], x1[4], x1[5]);
+    
+         if (lastpos == pos-1L) 
+	  Cell_Pass(pos,globval.Cell_nLoc, x1, lastpos); /* 2) check particle is stable at element from pos to end*/
+        //   Cell_Pass(pos-1L,globval.Cell_nLoc, x1, lastpos); 
+  
+    }while (((lastn) < nmax) && ((lastpos) == globval.Cell_nLoc));
 
-  if (lastpos == globval.Cell_nLoc) Cell_Pass(0L,pos, x1, lastpos);
+  if (lastpos == globval.Cell_nLoc) 
+    Cell_Pass(0L,pos, x1, lastpos);
 
   if (lastpos != pos) {
     printf("Trac: Particle lost \n");
@@ -1818,6 +1830,11 @@ void SetKLpar(int Fnum, int Knum, int Order, double kL)
 {
   long int  loc;
 
+  if (abs(Order) > HOMmax){
+  printf("SetKLPar: Error!....Multipole Order %d  > HOMmax %d\n",Order,HOMmax);
+  exit_(1);
+  }
+  
   loc = Elem_GetPos(Fnum, Knum);
   if (Cell[loc].Elem.PL != 0e0)
     Cell[loc].Elem.M->PBpar[Order+HOMmax] = kL/Cell[loc].Elem.PL;
@@ -2350,7 +2367,32 @@ double Sgn (double x)
   return (x == 0.0 ? 0.0 : (x > 0.0 ? 1.0 : -1.0));
 }
 
+/*************************************************************************
+/*void ChamberOff(void)
+ 
+   Purpose:
+       Set global vacuum chamber limitation
+
+   Input:
+       none
+
+   Output:
+       none
+
+   Return:
+       none
+
+   Global variables:
+       none
+
+   Specific functions:
+       none
+
+   Comments:
+       none
+  
 
+*************************************************************************/
 void ChamberOff(void)
 {
   int i;
@@ -2364,7 +2406,32 @@ void ChamberOff(void)
   status.chambre = false;
 }
 
+/*************************************************************************
+/*void PrintCh(void)
+ 
+   Purpose:
+       Output vacuum chamber limitation at each element to file "chambre.out"
+
+   Input:
+       none
+
+   Output:
+       none
+
+   Return:
+       none
+
+   Global variables:
+       none
 
+   Specific functions:
+       none
+
+   Comments:
+       none
+  
+
+*************************************************************************/
  void PrintCh(void)
 {
   long       i = 0;
@@ -2801,7 +2868,7 @@ void findcodS(double dP)
        none
 
    Return:
-       none
+       vcod:  6-D closed orbit 
 
    Global variables:
        none
diff --git a/tracy/tracy/src/prtmfile.cc b/tracy/tracy/src/prtmfile.cc
index da0f55342b57c84fb15fec70a13fac35bf2ccf20..28a33fa7f4e2559b33a03e79b9c98080cb1a539d 100644
--- a/tracy/tracy/src/prtmfile.cc
+++ b/tracy/tracy/src/prtmfile.cc
@@ -1,180 +1,178 @@
-/* Tracy-2
-
-   J. Bengtsson, CBP, LBL      1990 - 1994   Pascal version
-                 SLS, PSI      1995 - 1997
-   M. Boege      SLS, PSI      1998          C translation
-   L. Nadolski   SOLEIL        2002          Link to NAFF, Radia field maps
-   J. Bengtsson  NSLS-II, BNL  2004 -        
-
-
-   To generate a lattice flat file.
-
-   Type codes:
-
-     marker     -1
-     drift	 0
-     multipole   1
-     cavity      2
-     thin kick   3
-     wiggler     4
-     kick_map    6
-
-   Integration methods:
-
-     fourth order symplectic integrator   4
-
-   Format:
-
-     name, family no, kid no, element no
-     type code, integration method, no of integration steps
-     apertures: xmin, xmax, ymin, ymax
-
-   The following lines follows depending on element type.
-
-     type
-
-     drift:	 L
-
-     multipole:  hor., ver. displacement, roll angle (design),
-                                          roll angle (error)
-                 L, 1/rho, entrance angle, exit angle
-                 apertures[4]
-		 no of nonzero multipole coeff.
-		 n, b , a
-		     n   n
-		    ...
-
-     wiggler:    L [m], lambda [m]
-                 no of harmonics
-                 harm no, kxV [1/m], BoBrhoV [1/m], kxH, BoBrhoH, phi
-                    ...
-
-     cavity:	 cavity voltage/beam energy [eV], omega/c, beam energy [eV]
-
-     thin kick:	 hor., ver. displacement, roll angle (total)
-		 no of nonzero multipole coeff.
-		 n, b , a
-		     n   n
-		    ...
-
-     kick_map:   order <file name>
-
-*/
-
-
-#define snamelen 10
-
-// numerical type codes
-#define marker_   -1
-#define drift_     0
-#define mpole_     1
-#define cavity_    2
-#define thinkick_  3
-#define wiggler_   4
-#define kick_map_  6
-
-
-void prtName(FILE *fp, const int i,
-	     const int type, const int method, const int N)
-{
-  fprintf(fp, "%-15s %4d %4d %4d\n",
-	  Cell[i].Elem.PName, Cell[i].Fnum, Cell[i].Knum, i);
-  fprintf(fp, " %3d %3d %3d\n", type, method, N);
-  fprintf(fp, " %23.16e %23.16e %23.16e %23.16e\n",
-	  Cell[i].maxampl[X_][0], Cell[i].maxampl[X_][1],
-	  Cell[i].maxampl[Y_][0], Cell[i].maxampl[Y_][1]);
-}
-
-
-void prtHOM(FILE *fp, const int n_design, const mpolArray PB, const int Order)
-{
-  int i, nmpole;
-  
-  nmpole = 0;
-  for (i = 1; i <= Order; i++)
-    if ((PB[HOMmax-i] != 0.0) || (PB[HOMmax+i] != 0.0)) nmpole++;
-  fprintf(fp, "  %2d %2d\n", nmpole, n_design);
-  for (i = 1; i <= Order; i++) {
-    if ((PB[HOMmax-i] != 0.0) || (PB[HOMmax+i] != 0.0))
-      fprintf(fp, "%3d %23.16e %23.16e\n", i, PB[HOMmax+i], PB[HOMmax-i]);
-  }
-}
-
-
-void prtmfile(const char mfile_dat[])
-{
-  int     i, j;
-  FILE    *mfile;
-
-  mfile = file_write(mfile_dat);
-  for (i = 0; i <= globval.Cell_nLoc; i++) {
-    switch (Cell[i].Elem.Pkind) {
-    case drift:
-      prtName(mfile, i, drift_, 0, 0);
-      fprintf(mfile, " %23.16e\n", Cell[i].Elem.PL);
-      break;
-    case Mpole:
-      if (Cell[i].Elem.PL != 0.0) {
-	prtName(mfile, i, mpole_, Cell[i].Elem.M->Pmethod, Cell[i].Elem.M->PN);
-	fprintf(mfile, " %23.16e %23.16e %23.16e %23.16e\n",
-		Cell[i].dS[X_], Cell[i].dS[Y_],
-		Cell[i].Elem.M->PdTpar,
-		Cell[i].Elem.M->PdTsys
-		+Cell[i].Elem.M->PdTrms*Cell[i].Elem.M->PdTrnd);
-	fprintf(mfile, " %23.16e %23.16e %23.16e %23.16e %23.16e\n",
-		Cell[i].Elem.PL, Cell[i].Elem.M->Pirho,
-		Cell[i].Elem.M->PTx1, Cell[i].Elem.M->PTx2,
-		Cell[i].Elem.M->Pgap);
-	prtHOM(mfile, Cell[i].Elem.M->n_design, Cell[i].Elem.M->PB,
-	       Cell[i].Elem.M->Porder);
-      } else {
-	prtName(mfile, i, thinkick_, Cell[i].Elem.M->Pmethod,
-		Cell[i].Elem.M->PN);
-	fprintf(mfile, " %23.16e %23.16e %23.16e\n",
-		Cell[i].dS[X_], Cell[i].dS[Y_],
-		Cell[i].Elem.M->PdTsys
-		+Cell[i].Elem.M->PdTrms*Cell[i].Elem.M->PdTrnd);
-	prtHOM(mfile, Cell[i].Elem.M->n_design, Cell[i].Elem.M->PB,
-	       Cell[i].Elem.M->Porder);
-      }
-      break;
-    case Wigl:
-      prtName(mfile, i, wiggler_, Cell[i].Elem.W->Pmethod, Cell[i].Elem.W->PN);
-      fprintf(mfile, " %23.16e %23.16e\n",
-	      Cell[i].Elem.PL, Cell[i].Elem.W->lambda);
-      fprintf(mfile, "%2d\n", Cell[i].Elem.W->n_harm);
-      for (j = 0; j < Cell[i].Elem.W->n_harm; j++) {
-	fprintf(mfile, "%2d %23.16e %23.16e %23.16e %23.16e %23.16e\n",
-		Cell[i].Elem.W->harm[j],
-		Cell[i].Elem.W->kxV[j], Cell[i].Elem.W->BoBrhoV[j],
-		Cell[i].Elem.W->kxH[j], Cell[i].Elem.W->BoBrhoH[j],
-		Cell[i].Elem.W->phi[j]);
-      }
-      break;
-    case Cavity:
-      prtName(mfile, i, cavity_, 0, 0);
-      fprintf(mfile, " %23.16e %23.16e %d %23.16e\n",
-	      Cell[i].Elem.C->Pvolt/(1e9*globval.Energy),
-	      2.0*M_PI*Cell[i].Elem.C->Pfreq/c0, Cell[i].Elem.C->Ph,
-	      1e9*globval.Energy);
-      break;
-    case marker:
-      prtName(mfile, i, marker_, 0, 0);
-      break;
-    case Insertion:
-      prtName(mfile, i, kick_map_, Cell[i].Elem.ID->Pmethod,
-	      Cell[i].Elem.ID->PN);
-      if (Cell[i].Elem.ID->firstorder)
-	fprintf(mfile, " %3.1lf %1d %s\n",
-		Cell[i].Elem.ID->scaling, 1, Cell[i].Elem.ID->fname1);
-      else
-	fprintf(mfile, " %3.1lf %1d %s\n",
-		Cell[i].Elem.ID->scaling, 2, Cell[i].Elem.ID->fname2);
-      break;
-    default:
-      fprintf(mfile, "prtmfile: unknown type\n");
-      break;
-    }
-  }
-  fclose(mfile);
-}
+/* Tracy-2
+
+   J. Bengtsson, CBP, LBL      1990 - 1994   Pascal version
+                 SLS, PSI      1995 - 1997
+   M. Boege      SLS, PSI      1998          C translation
+   L. Nadolski   SOLEIL        2002          Link to NAFF, Radia field maps
+   J. Bengtsson  NSLS-II, BNL  2004 -        
+
+
+   To generate a lattice flat file.
+
+   Type codes:
+
+     marker     -1
+     drift	 0
+     multipole   1
+     cavity      2
+     thin kick   3
+     wiggler     4
+     kick_map    6
+
+   Integration methods:
+
+     fourth order symplectic integrator   4
+
+   Format:
+
+     name, family no, kid no, element no
+     type code, integration method, no of integration steps
+     apertures: xmin, xmax, ymin, ymax
+
+   The following lines follows depending on element type.
+
+     type
+
+     drift:	 L
+
+     multipole:  hor., ver. displacement, roll angle (design), roll angle (error);
+                 L, 1/rho, entrance angle, exit angle, apertures[4];
+		 no of nonzero multipole coeff.
+		 n, b , a
+		     n   n
+		    ...
+
+     wiggler:    L [m], lambda [m]
+                 no of harmonics
+                 harm no, kxV [1/m], BoBrhoV [1/m], kxH, BoBrhoH, phi
+                    ...
+
+     cavity:	 cavity voltage/beam energy [eV], omega/c, beam energy [eV]
+
+     thin kick:	 hor., ver. displacement, roll angle (total)
+		 no of nonzero multipole coeff.
+		 n, b , a
+		     n   n
+		    ...
+
+     kick_map:   order <file name>
+
+*/
+
+
+#define snamelen 10
+
+// numerical type codes
+#define marker_   -1
+#define drift_     0
+#define mpole_     1
+#define cavity_    2
+#define thinkick_  3
+#define wiggler_   4
+#define kick_map_  6
+
+
+void prtName(FILE *fp, const int i,
+	     const int type, const int method, const int N)
+{
+  fprintf(fp, "%-15s %4d %4d %4d\n",
+	  Cell[i].Elem.PName, Cell[i].Fnum, Cell[i].Knum, i);
+  fprintf(fp, " %3d %3d %3d\n", type, method, N);
+  fprintf(fp, " %23.16e %23.16e %23.16e %23.16e\n",
+	  Cell[i].maxampl[X_][0], Cell[i].maxampl[X_][1],
+	  Cell[i].maxampl[Y_][0], Cell[i].maxampl[Y_][1]);
+}
+
+
+void prtHOM(FILE *fp, const int n_design, const mpolArray PB, const int Order)
+{
+  int i, nmpole;
+  
+  nmpole = 0;
+  for (i = 1; i <= Order; i++)
+    if ((PB[HOMmax-i] != 0.0) || (PB[HOMmax+i] != 0.0)) nmpole++;
+  fprintf(fp, "  %2d %2d\n", nmpole, n_design);
+  for (i = 1; i <= Order; i++) {
+    if ((PB[HOMmax-i] != 0.0) || (PB[HOMmax+i] != 0.0))
+      fprintf(fp, "%3d %23.16e %23.16e\n", i, PB[HOMmax+i], PB[HOMmax-i]);
+  }
+}
+
+
+void prtmfile(const char mfile_dat[])
+{
+  int     i, j;
+  FILE    *mfile;
+
+  mfile = file_write(mfile_dat);
+  for (i = 0; i <= globval.Cell_nLoc; i++) {
+    switch (Cell[i].Elem.Pkind) {
+    case drift:
+      prtName(mfile, i, drift_, 0, 0);
+      fprintf(mfile, " %23.16e\n", Cell[i].Elem.PL);
+      break;
+    case Mpole:
+      if (Cell[i].Elem.PL != 0.0) {
+	prtName(mfile, i, mpole_, Cell[i].Elem.M->Pmethod, Cell[i].Elem.M->PN);
+	fprintf(mfile, " %23.16e %23.16e %23.16e %23.16e\n",
+		Cell[i].dS[X_], Cell[i].dS[Y_],
+		Cell[i].Elem.M->PdTpar,
+		Cell[i].Elem.M->PdTsys
+		+Cell[i].Elem.M->PdTrms*Cell[i].Elem.M->PdTrnd);
+	fprintf(mfile, " %23.16e %23.16e %23.16e %23.16e %23.16e\n",
+		Cell[i].Elem.PL, Cell[i].Elem.M->Pirho,
+		Cell[i].Elem.M->PTx1, Cell[i].Elem.M->PTx2,
+		Cell[i].Elem.M->Pgap);
+	prtHOM(mfile, Cell[i].Elem.M->n_design, Cell[i].Elem.M->PB,
+	       Cell[i].Elem.M->Porder);
+      } else {
+	prtName(mfile, i, thinkick_, Cell[i].Elem.M->Pmethod,
+		Cell[i].Elem.M->PN);
+	fprintf(mfile, " %23.16e %23.16e %23.16e\n",
+		Cell[i].dS[X_], Cell[i].dS[Y_],
+		Cell[i].Elem.M->PdTsys
+		+Cell[i].Elem.M->PdTrms*Cell[i].Elem.M->PdTrnd);
+	prtHOM(mfile, Cell[i].Elem.M->n_design, Cell[i].Elem.M->PB,
+	       Cell[i].Elem.M->Porder);
+      }
+      break;
+    case Wigl:
+      prtName(mfile, i, wiggler_, Cell[i].Elem.W->Pmethod, Cell[i].Elem.W->PN);
+      fprintf(mfile, " %23.16e %23.16e\n",
+	      Cell[i].Elem.PL, Cell[i].Elem.W->lambda);
+      fprintf(mfile, "%2d\n", Cell[i].Elem.W->n_harm);
+      for (j = 0; j < Cell[i].Elem.W->n_harm; j++) {
+	fprintf(mfile, "%2d %23.16e %23.16e %23.16e %23.16e %23.16e\n",
+		Cell[i].Elem.W->harm[j],
+		Cell[i].Elem.W->kxV[j], Cell[i].Elem.W->BoBrhoV[j],
+		Cell[i].Elem.W->kxH[j], Cell[i].Elem.W->BoBrhoH[j],
+		Cell[i].Elem.W->phi[j]);
+      }
+      break;
+    case Cavity:
+      prtName(mfile, i, cavity_, 0, 0);
+      fprintf(mfile, " %23.16e %23.16e %d %23.16e\n",
+	      Cell[i].Elem.C->Pvolt/(1e9*globval.Energy),
+	      2.0*M_PI*Cell[i].Elem.C->Pfreq/c0, Cell[i].Elem.C->Ph,
+	      1e9*globval.Energy);
+      break;
+    case marker:
+      prtName(mfile, i, marker_, 0, 0);
+      break;
+    case Insertion:
+      prtName(mfile, i, kick_map_, Cell[i].Elem.ID->Pmethod,
+	      Cell[i].Elem.ID->PN);
+      if (Cell[i].Elem.ID->firstorder)
+	fprintf(mfile, " %3.1lf %1d %s\n",
+		Cell[i].Elem.ID->scaling, 1, Cell[i].Elem.ID->fname1);
+      else
+	fprintf(mfile, " %3.1lf %1d %s\n",
+		Cell[i].Elem.ID->scaling, 2, Cell[i].Elem.ID->fname2);
+      break;
+    default:
+      fprintf(mfile, "prtmfile: unknown type\n");
+      break;
+    }
+  }
+  fclose(mfile);
+}
diff --git a/tracy/tracy/src/read_script.cc b/tracy/tracy/src/read_script.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f40914ea22373bc193d09ba9b57bb33cc72f2c2c
--- /dev/null
+++ b/tracy/tracy/src/read_script.cc
@@ -0,0 +1,394 @@
+/****************************************************************************/
+/* void read_script(const char *param_file_name, bool rd_lat)
+
+   Purpose:
+       read script written by the user.
+       1) User can specify lattice file name, vacuum chamber name, and bool flags such as tune 
+          tracking flag, tune shift with energy flag, etc.
+       2) In this function, Lattice is read, bool flags are set. 
+
+   Input:
+       param_file_name  user script name
+
+   Output:
+       none
+
+   Return:
+       global values and global bool flags 
+
+   Global variables:
+       bool flags
+
+   specific functions:
+       Read_Lattice()
+
+   Comments:
+       Jianfeng Zhang 07/10 soleil
+
+****************************************************************************/
+
+/* global flag, used in script_read() and main() for read the input from script*/
+bool TuneTracFlag = false;
+bool ChromTracFlag = false;
+bool FmapFlag = false, ExperimentFMAFlag = false, DetailedFMAFlag = false;
+bool TuneShiftFlag = false;
+bool ErrorCouplingFlag = false;  bool CouplingFlag = false;
+bool MomentumAccFlag = false;
+bool MultipoleFlag = false, ThinsextFlag = false;
+bool FitTuneFlag  = false; double targetnux = 0, targetnuz = 0;
+bool FitChromFlag = false; double targetksix = 0, targetksiz = 0;
+bool ChamberFlag = false, ChamberNoU20Flag = false, ReadChamberFlag = false;
+bool GirderErrorFlag = false;
+bool SigmaFlag = false;
+bool PX2Flag = false;
+bool InducedAmplitudeFlag = false;
+bool CodeComparaisonFlag = false;
+bool EtaFlag = false;           
+bool PhaseSpaceFlag = false;
+
+char chamber_file[max_str];
+
+/*  Read script   */
+void read_script(const char *param_file_name, bool rd_lat)
+ {
+  char    str[max_str];
+  char    line[max_str], name[max_str];
+  char    lat_file[max_str];
+ 
+  FILE    *inf;
+  const bool  prt = true;
+  long int    LineNum=0;
+  char full_param_file_name[max_str];
+  char lat_FileName[max_str];
+ //bool TuneTracFlag;
+    
+  sprintf(full_param_file_name,"%s.prm",param_file_name);
+  if (prt) printf("\n reading in script file: %s\n",full_param_file_name);
+
+  inf = file_read(full_param_file_name);
+
+  // read param file
+  strcpy(lat_file, "");
+  strcpy(chamber_file, ""); 
+  
+  while (fgets(line, max_str, inf) != NULL) {
+    if(prt)
+      printf("cfg: %s",line);
+      
+      LineNum++;
+    if (strstr(line, "#") == NULL) {
+      // get initial command token
+      sscanf(line, "%s", name);
+      
+      /*read file names................. */
+      /* set file directory*/
+      if (strcmp("in_dir", name) == 0)
+        sscanf(line, "%*s %s", in_dir);
+      /* lattice file......*/
+      else if (strcmp("lat_file", name) == 0){ 
+        sscanf(line, "%*s %s", lat_file);
+        sprintf(lat_FileName, "%s%s", in_dir, lat_file); 
+        if(rd_lat) {
+       //  #ifdef OLD_LATTICE
+            fprintf(stderr, "Reading lattice");
+            Read_Lattice(lat_FileName);
+       //  #else
+       //     fprintf(stderr, "Reading lattice with new parser\n");
+        //    sprintf(lat_FileName, "%s%s.lat", in_dir, lat_file); 
+        //    parse_lattice_flat(lat_FileName, "new.flat");
+        //    fprintf(stderr, "Parse the lattice into new.flat\n");
+         //   rdmfile("new.flat");
+         //   sprintf(lat_FileName, "%s%s", in_dir, lat_file);
+       //  #endif
+
+       //  #ifdef DEBUG
+       //  #endif
+        }  
+      } 
+      
+      /* other file names.....*/
+      /* chamber file */
+      else if (strcmp("chamber_file", name) == 0){ 
+        sscanf(line, "%*s %s", str);
+        sprintf(chamber_file,"%s%s", in_dir, str); /* add file directory*/
+      } 
+      
+      /* read in bool flags */
+      else if (strcmp("TuneTracFlag", name) == 0){
+        sscanf(line, "%*s %s", str);
+        if(strcmp(str, "true") == 0)
+          TuneTracFlag = true;
+        else if(strcmp(str, "false") == 0)
+          TuneTracFlag = false;
+        else {
+          printf("set bool flag true or flase for TuneTracFlag \n");
+          exit_(1);
+        } 
+      }
+      else if (strcmp("ChromTracFlag", name) == 0){
+        sscanf(line, "%*s %s", str);
+        if(strcmp(str, "true") == 0)
+          ChromTracFlag = true;
+        else if(strcmp(str, "false") == 0)
+          ChromTracFlag = false;
+        else {
+          printf("set bool flag true or flase for ChromTracFlag \n");
+          exit_(1);
+        } 
+      }
+      else if (strcmp("FmapFlag", name) == 0){
+        sscanf(line, "%*s %s", str);
+        if(strcmp(str, "true") == 0)
+          FmapFlag = true;
+        else if(strcmp(str, "false") == 0)
+         FmapFlag = false;
+        else {
+          printf("set bool flag true or flase for FmapFlag \n");
+          exit_(1);
+        } 
+      }
+      else if (strcmp("ExperimentFMAFlag", name) == 0){
+        sscanf(line, "%*s %s", str);
+        if(strcmp(str, "true") == 0)
+           ExperimentFMAFlag = true;
+        else if(strcmp(str, "false") == 0)
+           ExperimentFMAFlag= false;
+        else {
+          printf("set bool flag true or flase for ExperimentFMAFlag \n");
+          exit_(1);
+        } 
+      }
+      else if (strcmp("DetailedFMAFlag", name) == 0){
+        sscanf(line, "%*s %s", str);
+        if(strcmp(str, "true") == 0)
+          DetailedFMAFlag = true;
+        else if(strcmp(str, "false") == 0)
+          DetailedFMAFlag = false;
+        else {
+          printf("set bool flag true or flase for DetailedFMAFlag \n");
+          exit_(1);
+        } 
+      }
+      else if (strcmp("TuneShiftFlag", name) == 0){
+        sscanf(line, "%*s %s", str);
+        if(strcmp(str, "true") == 0)
+          TuneShiftFlag = true;
+        else if(strcmp(str, "false") == 0)
+          TuneShiftFlag = false;
+        else {
+          printf("set bool flag true or flase for TuneShiftFlag \n");
+          exit_(1);
+        } 
+      }
+      else if (strcmp("ErrorCouplingFlag", name) == 0){
+        sscanf(line, "%*s %s", str);
+        if(strcmp(str, "true") == 0)
+          ErrorCouplingFlag = true;
+        else if(strcmp(str, "false") == 0)
+          ErrorCouplingFlag = false;
+        else {
+          printf("set bool flag true or flase for ErrorCouplingFlag \n");
+          exit_(1);
+        } 
+      }
+      else if (strcmp("CouplingFlag", name) == 0){
+        sscanf(line, "%*s %s", str);
+        if(strcmp(str, "true") == 0)
+          CouplingFlag = true;
+        else if(strcmp(str, "false") == 0)
+          CouplingFlag = false;
+        else {
+          printf("set bool flag true or flase for CouplingFlag \n");
+          exit_(1);
+        } 
+      }
+       else if (strcmp("MomentumAccFlag", name) == 0){
+        sscanf(line, "%*s %s", str);
+        if(strcmp(str, "true") == 0)
+          MomentumAccFlag = true;
+        else if(strcmp(str, "false") == 0)
+          MomentumAccFlag = false;
+        else {
+          printf("set bool flag true or flase for MomentumAccFlag \n");
+          exit_(1);
+        } 
+      }
+       else if (strcmp("MultipoleFlag", name) == 0){
+        sscanf(line, "%*s %s", str);
+        if(strcmp(str, "true") == 0)
+          MultipoleFlag = true;
+        else if(strcmp(str, "false") == 0)
+          MultipoleFlag = false;
+        else {
+          printf("set bool flag true or flase for MultipoleFlag \n");
+          exit_(1);
+        } 
+      }
+      else if (strcmp("ThinsextFlag", name) == 0){
+        sscanf(line, "%*s %s", str);
+        if(strcmp(str, "true") == 0)
+          ThinsextFlag = true;
+        else if(strcmp(str, "false") == 0)
+          ThinsextFlag = false;
+        else {
+          printf("set bool flag true or flase for ThinsextFlag \n");
+          exit_(1);
+        } 
+      }
+       else if (strcmp("FitTuneFlag", name) == 0){
+        sscanf(line, "%*s %s %lf %lf", str,&targetnux,&targetnuz);
+        if(strcmp(str, "true") == 0)
+          FitTuneFlag = true;
+        else if(strcmp(str, "false") == 0)
+          FitTuneFlag = false;
+        else {
+          printf("set bool flag true or flase for FitTuneFlag \n");
+          exit_(1);
+        } 
+      }
+       else if (strcmp("FitChromFlag", name) == 0){
+        sscanf(line, "%*s %s %lf %lf", str,&targetksix,&targetksiz);
+        if(strcmp(str, "true") == 0)
+          FitChromFlag = true;
+        else if(strcmp(str, "false") == 0)
+          FitChromFlag = false;
+        else {
+          printf("set bool flag true or flase for FitChromFlag \n");
+          exit_(1);
+        } 
+      }
+       else if (strcmp("ChamberFlag", name) == 0){
+        sscanf(line, "%*s %s", str);
+        if(strcmp(str, "true") == 0)
+          ChamberFlag = true;
+        else if(strcmp(str, "false") == 0)
+          ChamberFlag = false;
+        else {
+          printf("set bool flag true or flase for ChamberFlag \n" );
+          exit_(1);
+        } 
+      }
+       else if (strcmp("ChamberNoU20Flag", name) == 0){
+        sscanf(line, "%*s %s", str);
+        if(strcmp(str, "true") == 0)
+          ChamberNoU20Flag = true;
+        else if(strcmp(str, "false") == 0)
+          ChamberNoU20Flag = false;
+        else {
+          printf("set bool flag true or flase for ChamberNoU20Flag \n");
+          exit_(1);
+        } 
+      }
+      else if (strcmp("ReadChamberFlag", name) == 0){
+        sscanf(line, "%*s %s", str);
+        if(strcmp(str, "true") == 0)
+          ReadChamberFlag = true;
+        else if(strcmp(str, "false") == 0)
+          ReadChamberFlag = false;
+        else {
+          printf("set bool flag true or flase for ReadChamberFlag \n");
+          exit_(1);
+        } 
+      }
+       else if (strcmp("GirderErrorFlag", name) == 0){
+        sscanf(line, "%*s %s", str);
+        if(strcmp(str, "true") == 0)
+          GirderErrorFlag = true;
+        else if(strcmp(str, "false") == 0)
+          GirderErrorFlag = false;
+        else {
+          printf("set bool flag true or flase for GirderErrorFlag \n");
+          exit_(1);
+        } 
+      }
+       else if (strcmp("SigmaFlag", name) == 0){
+        sscanf(line, "%*s %s", str);
+        if(strcmp(str, "true") == 0)
+          SigmaFlag = true;
+        else if(strcmp(str, "false") == 0)
+         SigmaFlag = false;
+        else {
+          printf("set bool flag true or flase for SigmaFlag \n");
+          exit_(1);
+        } 
+      }
+       else if (strcmp("PX2Flag", name) == 0){
+        sscanf(line, "%*s %s", str);
+        if(strcmp(str, "true") == 0)
+          PX2Flag = true;
+        else if(strcmp(str, "false") == 0)
+          PX2Flag = false;
+        else {
+          printf("set bool flag true or flase for PX2Flag \n");
+          exit_(1);
+        } 
+      }
+       else if (strcmp("InducedAmplitudeFlag", name) == 0){
+        sscanf(line, "%*s %s", str);
+        if(strcmp(str, "true") == 0)
+          InducedAmplitudeFlag = true;
+        else if(strcmp(str, "false") == 0)
+          InducedAmplitudeFlag = false;
+        else {
+          printf("set bool flag true or flase for InducedAmplitudeFlag \n" );
+          exit_(1);
+        } 
+      }
+       else if (strcmp("CodeComparaisonFlag", name) == 0){
+        sscanf(line, "%*s %s", str);
+        if(strcmp(str, "true") == 0)
+          CodeComparaisonFlag = true;
+        else if(strcmp(str, "false") == 0)
+          CodeComparaisonFlag = false;
+        else {
+          printf("set bool flag true or flase for  CodeComparaisonFlag \n" );
+          exit_(1);
+        } 
+      }
+       else if (strcmp("EtaFlag", name) == 0){
+        sscanf(line, "%*s %s", str);
+        if(strcmp(str, "true") == 0)
+          EtaFlag = true;
+        else if(strcmp(str, "false") == 0)
+          EtaFlag = false;
+        else {
+          printf("set bool flag true or flase for EtaFlag \n" );
+          exit_(1);
+        } 
+      }
+       // include RF cavity and set 4/6 D tracking
+       // true=6D, false=4D
+      else if (strcmp("globval.Cavity_on", name) == 0){
+        sscanf(line, "%*s %s", str);
+        if(strcmp(str, "true") == 0)
+          globval.Cavity_on = true;
+        else if(strcmp(str, "false") == 0)
+          globval.Cavity_on = false;
+        else {
+          printf("set bool flag true or flase for globval.Cavity_on \n" );
+          exit_(1);
+        } 
+      }
+      else if (strcmp("PhaseSpaceFlag", name) == 0){
+        sscanf(line, "%*s %s", str);
+        if(strcmp(str, "true") == 0)
+          PhaseSpaceFlag = true;
+        else if(strcmp(str, "false") == 0)
+          PhaseSpaceFlag = false;
+        else {
+          printf("set bool flag true or flase for PhaseSpaceFlag \n" );
+          exit_(1);
+        } 
+      }
+      else {
+        printf("bad line in file %s, line %ld \n", full_param_file_name, LineNum);
+        exit_(1);
+      }
+    }
+    /* continue read in the line */ 
+    else
+      continue;
+  }
+  fclose(inf);
+}  
+ 
diff --git a/tracy/tracy/src/soleilcommon.cc b/tracy/tracy/src/soleilcommon.cc
index c80561b0590422d085282efab17fc1eba8055e5b..311027a92a94c37054f0b02b5fd2a7eda490de8b 100644
--- a/tracy/tracy/src/soleilcommon.cc
+++ b/tracy/tracy/src/soleilcommon.cc
@@ -139,10 +139,7 @@ void Read_Lattice(char *fic)
 
   if (globval.RingType == 1)
   {      // for a ring
-    /* define x/y physical aperture  */
-    //ChamberOff();
-    ChamberOn();
-
+    
     /* Defines global variables for Tracy code */
     
     globval.H_exact     = false; // Small Ring Hamiltonian
@@ -158,17 +155,21 @@ void Read_Lattice(char *fic)
     globval.CODimax    = 40L;   /* maximum number of iterations for COD algo */
     globval.dPcommon   = 1e-10; /* Common energy step for energy differentiation */
     globval.delta_RF  = RFacceptance;/* energy acceptance for SOLEIL */
-    globval.bpm   = get_bpm_number();   /* number of bpm into the latice */
-    globval.hcorr = get_hcorr_number(); /* number of horizontal corrector into the lattice */
-    globval.vcorr = get_vcorr_number(); /* number of vertical corrector into the lattice */
-    globval.qt    = get_qt_number();    /* number of skew quad into the lattice */
-
+    globval.bpm = get_bpm_number();  /* number of bpm into the latice */
+    globval.hcorr = get_hcorr_number();  /* number of horizontal corrector into the lattice */
+    globval.vcorr = get_vcorr_number();  /* number of vertical corrector into the lattice */
+    globval.qt = get_qt_number();  /* number of skew quad into the lattice */
+  
 
 /*    int lastpos;
    getcod(0.0, &lastpos);
 //    findcod(0.0);
    printcod();
    exit(1);*/
+   
+   /* define x/y physical aperture  */
+    //ChamberOff();
+    ChamberOn();
     /* Compute and get Twiss parameters */
     Ring_GetTwiss(chroma = true, dP = 0.0);
 //        GetChromTrac(2, 1026L, 1e-6, &ksix, &ksiz);
@@ -206,21 +207,18 @@ void Read_Lattice(char *fic)
     globval.CODimax = 10L;  /* maximum number of iterations for COD algo */
     globval.dPcommon = 1e-10;  /* Common energy step for energy differentiation */
     globval.delta_RF = 0.10;  /* 6% + epsilon energy acceptance for SOLEIL */
+    
+    globval.dPparticle = dP;
     globval.bpm = get_bpm_number();  /* number of bpm into the latice */
     globval.hcorr = get_hcorr_number();  /* number of horizontal corrector into the lattice */
     globval.vcorr = get_vcorr_number();  /* number of vertical corrector into the lattice */
     globval.qt = get_qt_number();  /* number of skew quad into the lattice */
-    globval.dPparticle = dP;
-
+  
     ChamberOff();
     TransTwiss(alpha, beta, eta, etap, codvect);
     }
   } 
-  printlatt();  /* print out lattice functions */
-  printglob();
- 
-  bool a;
-  a =false;
+  
 }
 
 
diff --git a/tracy/tracy/src/soleillib.cc b/tracy/tracy/src/soleillib.cc
index 12a1c24bbe41b2c4f24b994be780977f3ae68452..cbb30b45056ecd49f828c398440c849d5984c781 100644
--- a/tracy/tracy/src/soleillib.cc
+++ b/tracy/tracy/src/soleillib.cc
@@ -310,7 +310,7 @@ void SetErr(void)
   double  fac = 0.0, normcut = 0.0;
 //  long    seed = 0L;
   long    i = 0L;
-  CellType Cell;
+ // CellType Cell;
   double theta = 0.0;
   int pair = 0;
 
@@ -333,33 +333,64 @@ void SetErr(void)
 
   for (i = 1L; i <= globval.Cell_nLoc; i++)
   {
-    getelem(i, &Cell);
-    if (Cell.Elem.Pkind == 2L)
+    if (Cell[i].Elem.Pkind == 2L)
     {
-      if (Cell.Elem.M->Porder == 2L && Cell.dT[1] == 0)
+      if (Cell[i].Elem.M->Porder == 2L && Cell[i].dT[1] == 0)
       {
         if ((pair%2)==0) theta = fac*normranf(); /* random error every 2 elements (quad split into 2) */
         pair++;
-        Cell.Elem.M->PBpar[HOMmax-2L] = -Cell.Elem.M->PBpar[HOMmax+2L]*sin(2.0*theta);
-        Cell.Elem.M->PBpar[HOMmax+2L] =  Cell.Elem.M->PBpar[HOMmax+2L]*cos(2.0*theta);
-        if (trace) printf("%6s % .5e % .5e % .5e\n",Cell.Elem.PName,
-                           Cell.Elem.M->PBpar[HOMmax-2L], Cell.Elem.M->PBpar[HOMmax+2L],theta);
-
-        putelem(i, &Cell);
-        Mpole_SetPB(Cell.Fnum, Cell.Knum, -2L);
-        Mpole_SetPB(Cell.Fnum, Cell.Knum, 2L);
+        Cell[i].Elem.M->PBpar[HOMmax-2L] = -Cell[i].Elem.M->PBpar[HOMmax+2L]*sin(2.0*theta);
+        Cell[i].Elem.M->PBpar[HOMmax+2L] =  Cell[i].Elem.M->PBpar[HOMmax+2L]*cos(2.0*theta);
+        if (trace) printf("%6s % .5e % .5e % .5e\n",Cell[i].Elem.PName,
+                           Cell[i].Elem.M->PBpar[HOMmax-2L], Cell[i].Elem.M->PBpar[HOMmax+2L],theta);
+
+        Mpole_SetPB(Cell[i].Fnum, Cell[i].Knum, -2L);
+        Mpole_SetPB(Cell[i].Fnum, Cell[i].Knum, 2L);
       }
     }
   }
 }
 
+/****************************************************************************/
+/* void ChamberOn(void)
+
+   Purpose:
+     Switch on the vacuum chamber
+     Called by Read_Lattice
+
+   Input:
+       none
+
+   Output:
+       none
+
+   Return:
+       none
+
+   Global variables:
+       none
+
+   specific functions:
+       DefineCh
 
+   Comments:
+       none
+
+****************************************************************************/
+//  Previously defined in soleilcommon.cc
+ 
+void ChamberOn(void)
+{
+  DefineCh();
+  status.chambre = true;
+} 
 
 /****************************************************************************/
 /* void DefineCh(void)
 
    Purpose:  called by read_Lattice
-       Defines the vacuum chamber around the ring
+       Defines the vacuum chamber around the ring 
+                with the in vacuum undulator U20
 
    Input:
        none
@@ -422,25 +453,28 @@ void DefineCh(void)
       Cell[i].maxampl[Y_][1] =  12.5e-3;
     }
     else if ((i>=isdac1) && (i<=isdac2))
-    { /*  minigap */
+    { /* with the minigap U20 */
       Cell[i].maxampl[X_][0] = -35e-3;
       Cell[i].maxampl[X_][1] =  25e-3;
       Cell[i].maxampl[Y_][0] = -2.5e-3;
       Cell[i].maxampl[Y_][1] =  2.5e-3;
     }
     if  (i<=hu600)
-/*    if  ((i>isep1) && (i<=hu600)) /* debut maille en section courte PB 25-09-07 */
+//    if  ((i>isep1) && (i<=hu600)) /* debut maille en section courte PB 25-09-07 */
     { /* HU640 */
       Cell[i].maxampl[Y_][0] = -7.0e-3;
       Cell[i].maxampl[Y_][1] =  7.0e-3;
     }
   }
 }
+
+
 /****************************************************************************/
-/* void ChamberOn(void)
+/* void DefineChnoU20(void)
 
-   Purpose:
-     Switch on the vacuum chamber
+   Purpose:  called by read_Lattice
+       Defines the vacuum chamber around the ring 
+               without in vacuum undulator U20 
 
    Input:
        none
@@ -452,22 +486,200 @@ void DefineCh(void)
        none
 
    Global variables:
-       none
+       globval
 
    specific functions:
-       DefineCh
+       none
 
    Comments:
+       This function works providing that the makers have unique names into
+       the lattice
+
+****************************************************************************/
+void DefineChNoU20(void)
+{ 
+  long i;
+  long isep1=-1L, isep2=-1L, hu600=-1L,
+       isdm1=-1L, isdm2=-1L, isdac1=-1L, isdac2=-1L;
+  
+//   trace=0;
+/* Look for indices for defining the vaccum pipe*/
+  isep1 = Elem_GetPos(ElemIndex("ssep"), 1);
+  isep2 = Elem_GetPos(ElemIndex("esep"), 1)-1;
+  hu600 = Elem_GetPos(ElemIndex("ehu600"), 1)-1;
+  isdm1 = Elem_GetPos(ElemIndex("ssdm"), 1);
+  isdm2 = Elem_GetPos(ElemIndex("esdm"), 1);
+  isdac1 = Elem_GetPos(ElemIndex("ssdac"), 1);
+  isdac2 = Elem_GetPos(ElemIndex("esdac"), 1)-1;
+  
+/* Set the vaccum chamber */
+  for (i = 0; i <= globval.Cell_nLoc; i++)
+  {
+    if  ((i<isep1) || ((i>isep2) && (i<isdm1)) ||
+    ((i>isdm2) && (i<isdac1)) || (i>isdac2))
+    { /* ch normale */
+      Cell[i].maxampl[X_][0] = -35.e-3;
+      Cell[i].maxampl[X_][1] =  35.e-3;
+      Cell[i].maxampl[Y_][0] = -12.5e-3;
+      Cell[i].maxampl[Y_][1] =  12.5e-3;
+    }
+    else if ((i>=isdm1) && (i<=isdm2))
+    { /* SD13 */
+       Cell[i].maxampl[X_][0] = -21e-3;
+       Cell[i].maxampl[X_][1] =  21e-3;
+       Cell[i].maxampl[Y_][0] = -5.0e-3;
+       Cell[i].maxampl[Y_][1] =  5.0e-3;
+    }
+    else if ((i>=isep1) && (i<=isep2))
+    { /* septum */
+      Cell[i].maxampl[X_][0] = -20e-3;
+      Cell[i].maxampl[X_][1] =  35e-3;
+      Cell[i].maxampl[Y_][0] = -12.5e-3;
+      Cell[i].maxampl[Y_][1] =  12.5e-3;
+    }
+    else if ((i>=isdac1) && (i<=isdac2))
+    { /*  open the minigap U20*/
+      Cell[i].maxampl[X_][0] = -35e-3;
+      Cell[i].maxampl[X_][1] =  25e-3;
+      Cell[i].maxampl[Y_][0] = -12.5e-3;
+      Cell[i].maxampl[Y_][1] =  12.5e-3;
+    }
+    if  (i<=hu600)
+//    if  ((i>isep1) && (i<=hu600)) /* debut maille en section courte PB 25-09-07 */
+    { /* HU640 */
+      Cell[i].maxampl[Y_][0] = -7.0e-3;
+      Cell[i].maxampl[Y_][1] =  7.0e-3;
+    }
+  }
+}
+
+/****************************************************************************/
+/* void ReadCh(Const char *AperFile)
+
+   Purpose:  read and set the definition of the vacuum chamber 
+             between different sections around the ring from file 
+	     AperFile.dat. 
+	     
+	     In AperFile.dat,
+	       1) line begin with "#" is comment line
+	       2) first line Name1: Start
+	          first line Name2: All
+	       3) Name1 and Name2 should be unique in the lattice
+	       4) Name1 is defined before Name2 in the lattice
+	       5)
+	       Name1:  start element of the section for the aperture
+	       Name2:  end element of the section for the aperture
+	       dxmin:   minimum x value of vacuum chamber
+	       dxmax:   maxmum x value of vacuum chamber
+	       dymin:   minimum y value of vacuum chamber
+	       dymax:   maxmum y value of vacuum chamber
+	       
+
+
+   Input:
+       none
+
+   Output:
+       none
+
+   Return:
+       none
+
+   Global variables:
+       globval
+
+   specific functions:
        none
 
+   Comments:
+       See also LoadApers in nsrl-ii.cc
+       J.Zhang 07/10 soleil
 ****************************************************************************/
-//  Definition in soleilcommon.cc
- 
-void ChamberOn(void)
+void ReadCh(const char *AperFile)
 {
-  DefineCh();
-  status.chambre = true;
-} 
+ char    line[max_str], Name1[max_str], Name2[max_str];
+  int     Fnum1=0, Fnum2=0, Kidnum1=0, Kidnum2=0, k1=0, k2=0; 
+  int     i=0, LineNum=0;
+  double  dxmin, dxmax, dymin, dymax;  // min and max x and apertures
+  FILE    *fp;
+
+  bool  prt = true;
+
+  fp = file_read(AperFile);
+
+  printf("\n");
+  printf("...Load and Set Apertures to lattice elements\n");
+
+  while (fgets(line, max_str, fp) != NULL) {
+    LineNum++;  /* count the line number that has been read*/
+    if (strstr(line, "#") == NULL)
+    /* read the aperture setting */ 
+    {
+      sscanf(line,"%s %s %lf %lf %lf %lf",
+	     Name1,Name2, &dxmin, &dxmax, &dymin, &dymax);
+      
+      if (strcmp("Start", Name1)==0 && strcmp("All", Name2)==0) {
+	if(prt)
+	  printf("setting all apertures to \n"
+		 " dxmin = %e, dxmax = %e, dymin = %e, dymax = %e\n",
+		 dxmin, dxmax, dymin, dymax);
+	set_aper_type(All, dxmin, dxmax, dymin, dymax);
+	//	ini_aper(dxmin, dxmax, dymin, dymax); 
+       }        
+        
+      else {
+        /* read the vacuum chamber between section */
+	Fnum1 = ElemIndex(Name1);
+	Fnum2 = ElemIndex(Name2);
+	if(Fnum1>0 && Fnum2>0) {
+	  /* if element Name1 is defined before elment Name2, give error message*/
+	  if(Fnum1 > Fnum2){
+	    printf("\nReadCh(): \n" 
+	           "          aperture file, Line %d, Element %s should be defined after Element %s \n",
+	            LineNum,Name1,Name2);
+            exit_(1);
+          }
+	  /* if the element is not unique in the lattice, give error message*/
+	  Kidnum1 = GetnKid(Fnum1);
+	  Kidnum2 = GetnKid(Fnum2);
+	  if(Kidnum1 > 1 || Kidnum2 >1){
+	   printf("\nReadCh(): \n"
+	          "          aperture file, Line %d, Element %s or Element %s is not unique in lattice \n",
+	            LineNum,Name1,Name2);
+            exit_(1);
+	  }
+	   
+	  if(prt)
+	    printf("setting apertures to section:\n"
+		   "  %s  %s dxmin = %e, dxmax = %e, dymin = %e, dymax = %e\n",
+		   Name1, Name2, dxmin, dxmax, dymin, dymax);
+	/* find the start and end index of the section*/
+	  k1 = Elem_GetPos(Fnum1, 1);
+	  k2 = Elem_GetPos(Fnum2, 1);
+	/* set the vacuum chamber*/
+	  for(i=1; i<globval.Cell_nLoc; i++){
+	    if(i>=k1 && i<=k2){
+	      Cell[i].maxampl[X_][0] = dxmin;
+	      Cell[i].maxampl[X_][1] = dxmax;
+	      Cell[i].maxampl[Y_][0] = dymin;
+	      Cell[i].maxampl[Y_][1] = dymax;
+	    }
+	  }  
+	} 
+	else {
+	  printf("\nReadCh(): \n"
+	         "          aperture file, Line %d, lattice does not contain section between element %s and element %s\n", 
+	          LineNum,Name1, Name2); 
+	  exit_(1);
+	  }
+      }
+        
+    } 
+   else /* print the comment line */
+      printf("%s", line);
+  } 
+  fclose(fp);
+}
 
 /****************************************************************************/
 /* void Trac_Tab(double x, double px, double y, double py, double dp,
@@ -520,8 +732,8 @@ void Trac_Tab(double x, double px, double y, double py, double dp,
   lastn = 0;
   (lastpos)=pos;
 
-  Cell_Pass(pos -1, globval.Cell_nLoc, x1, lastpos);
-
+  Cell_Pass(pos, globval.Cell_nLoc, x1, lastpos);
+//Cell_Pass(pos -1, globval.Cell_nLoc, x1, lastpos);
   if(trace) fprintf(outf1, "\n");
 
   do {
@@ -543,8 +755,8 @@ void Trac_Tab(double x, double px, double y, double py, double dp,
     else  {
       printf("Trac_Tab: Particle lost \n");
       fprintf(stdout, "%6ld %+10.5g %+10.5g %+10.5g"
-	      " %+10.5g %+10.5g %+10.5g \n",
-	      lastn, x1[0], x1[1], x1[2], x1[3], x1[4], x1[5]);
+	              " %+10.5g %+10.5g %+10.5g \n",
+	              lastn, x1[0], x1[1], x1[2], x1[3], x1[4], x1[5]);
       lostF = false;
     }
    }
@@ -558,6 +770,7 @@ void Trac_Tab(double x, double px, double y, double py, double dp,
 }
 
 
+
 /****************************************************************************/
 /* void NuDx(long Nbx, long Nbz, long Nbtour, double xmax, double zmax,
                double energy)
@@ -615,7 +828,7 @@ void NuDx(long Nbx, long Nbz, long Nbtour, double xmax, double zmax,
   /* Get time and date */
   newtime = GetTime();
 
-    if (trace) printf("Entering NuDx ... results in nudx.out\n\n");
+    if (!trace) printf("\n Entering NuDx ... results in nudx.out\n\n");
 
     /////////////
     // H tuneshift
@@ -1079,7 +1292,7 @@ void NuDp(long Nb, long Nbtour, double emax)
   /* Get time and date */
   newtime = GetTime();
 
-  if (trace) printf("Entering NuDp ...\n\n");
+  if (!trace) printf("\n Entering NuDp ...\n\n");
 
   /* Opening file */
   if ((outf = fopen(fic, "w")) == NULL) {
@@ -1531,7 +1744,7 @@ void Enveloppe(double x, double px, double y, double py, double dp, double nturn
 
 
 /****************************************************************************/
-/* void Multipole_new(void)
+/* void Multipole_thicksext(void)
 
    Purpose:
        Set multipole in dipoles, quadrupoles, thick sextupoles, skew quadrupole,
@@ -1558,116 +1771,117 @@ void Enveloppe(double x, double px, double y, double py, double dp, double nturn
        is broken
        Should be rewritten because list already exists now ..
 
+       Copy from Tracy II.
 ****************************************************************************/
 
-// void Multipole_new(void)
-// {
-//   int i = 0;
-//   int ndip  = 0,  /* Number of dipoles */
-//       nquad = 0,  /* Number of quadrupoles */
-//       nsext = 0,  /* Number of sextupoles  */
-//       nhcorr= 0,  /* Number of horizontal correctors */
-//       nvcorr= 0,  /* Number of vertical correctors */
-//       nqcorr= 0;  /* Number of skew quadrupoles */
-
-//   int dlist[500];     /* dipole list */
-//   int qlist[500];     /* Quadrupole list */
-//   int slist[500];     /* Sextupole list */
-//   int hcorrlist[120]; /* horizontal corrector list */
-//   int vcorrlist[120]; /* vertical corrector list */
-//   int qcorrlist[120]; /* skew quad list */
-//   int hcorrlistThick[120]; /* horizontal corrector list */
-//   int vcorrlistThick[120]; /* vertical corrector list */
-//   int qcorrlistThick[120]; /* skew quad list */
+void Multipole_thicksext(void)
+{
+  int i = 0;
+  int ndip  = 0,  /* Number of dipoles */
+      nquad = 0,  /* Number of quadrupoles */
+      nsext = 0,  /* Number of sextupoles  */
+      nhcorr= 0,  /* Number of horizontal correctors */
+      nvcorr= 0,  /* Number of vertical correctors */
+      nqcorr= 0;  /* Number of skew quadrupoles */
 
-//   CellType Cell;
+  int dlist[500];     /* dipole list */
+  int qlist[500];     /* Quadrupole list */
+  int slist[500];     /* Sextupole list */
+  int hcorrlist[120]; /* horizontal corrector list */
+  int vcorrlist[120]; /* vertical corrector list */
+  int qcorrlist[120]; /* skew quad list */
+  int hcorrlistThick[120]; /* horizontal corrector list */
+  int vcorrlistThick[120]; /* vertical corrector list */
+  int qcorrlistThick[120]; /* skew quad list */
 
-//   int ElemFamIndex=0;;
-//   int    mOrder = 0;     /* multipole order */
-//   double mKL = 0.0 ;     /* multipole integrated strength */
-//   double corr_strength = 0.0;
-//   double hcorr[120], vcorr[120], qcorr[120];
-//   double hcorrThick[120], vcorrThick[120], qcorrThick[120];
-//   double b2 = 0.0, b3 = 0.0;
-//   double dBoB2 = 0.0, dBoB3 = 0.0, dBoB4 = 0.0, dBoB5 = 0.0, dBoB6 = 0.0,
-//          dBoB7 = 0.0, dBoB9 = 0.0, dBoB11 = 0.0, dBoB15 = 0.0, dBoB21 = 0.0,
-//          dBoB27;
-//   double dBoB6C = 0.0, dBoB6L = 0.0, dBoB10C = 0.0, dBoB10L = 0.0,
-//          dBoB14C = 0.0, dBoB14L = 0.0, dBoB3C = 0.0, dBoB3L = 0.0,
-//          dBoB4C = 0.0, dBoB4L = 0.0;
-//   double dBoB5rms = 0.0, dBoB7rms = 0.0;
-//   double x0i = 0.0, x02i = 0.0, x03i = 0.0, x04i = 0.0, x05i = 0.0,
-//          x06i = 0.0, x07i = 0.0, x08i = 0.0, x012i = 0.0, x010i = 0.0,
-//          x018i = 0.0, x024i = 0.0, x1i = 0.0;
-//   double theta = 0.0, brho = 0.0, dummyf = 0.0, conv = 0.0 ;
-//   char *dummy;
+  CellType Cell; 
 
-//   FILE *fi;
-//   char *fic_hcorr="//home/nadolski/codes/tracy/maille/soleil/corh.txt";
-//   char *fic_vcorr="/home/nadolski/codes/tracy/maille/soleil/corv.txt";
-//   char *fic_skew ="/home/nadolski/codes/tracy/maille/soleil/corqt.txt";
-// /*********************************************************/
-
-//   getglobv_(&globval);
-
-//   printf("Enter multipole ... \n");
-
-// /* Make lists of dipoles, quadrupoles and  sextupoles */
-//   for (i = 0; i <= globval.Cell_nLoc; i++)
-//   {
-//     getelem(i, &Cell); /* get element */
-
-//     if (Cell.Elem.Pkind == Mpole)
-//     {
-//       if (Cell.Elem.M->UU.U0.Pirho!= 0.0)
-//       {
-//         dlist[ndip] = i;
-//         ndip++;
-//         if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PB[0 + HOMmax]);
-//       }/*
-//       else if (Cell.Elem.M->PBpar[2L + HOMmax] != 0.0)
-//       {
-//         qlist[nquad] = i;
-//         nquad++;
-//         if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[2L + HOMmax]);
-//       }
-//       else if (Cell.Elem.M->PBpar[3L + HOMmax] != 0.0)
-//       {
-//         slist[nsext] = i;
-//         nsext++;
-//         if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[3L + HOMmax]);
-//       } */
-//       else if ( Cell.Elem.PName[0] == 'c' && Cell.Elem.PName[1] == 'h')
-//       {
-//         hcorrlist[nhcorr] = i;
-//         nhcorr++;
-//         if (trace) printf("%s \n",Cell.Elem.PName);
-//       }
-//       else if ( Cell.Elem.PName[0] == 'c' && Cell.Elem.PName[1] == 'v')
-//       {
-//         vcorrlist[nvcorr] = i;
-//         nvcorr++;
-//         if (trace) printf("%s \n",Cell.Elem.PName);
-//       }
-//       else if ( Cell.Elem.PName[0] == 'q' && Cell.Elem.PName[1] == 't')
-//       {
-//         qcorrlist[nqcorr] = i;
-//         nqcorr++;
-//         if (trace) printf("%s \n",Cell.Elem.PName);
-//       }
-//     }
-//   }
+  int ElemFamIndex=0;
+  int    mOrder = 0;     /* multipole order */
+  double mKL = 0.0 ;     /* multipole integrated strength */
+  double corr_strength = 0.0;
+  double hcorr[120], vcorr[120], qcorr[120];
+  double hcorrThick[120], vcorrThick[120], qcorrThick[120];
+  double b2 = 0.0, b3 = 0.0;
+  double dBoB2 = 0.0, dBoB3 = 0.0, dBoB4 = 0.0, dBoB5 = 0.0, dBoB6 = 0.0,
+         dBoB7 = 0.0, dBoB9 = 0.0, dBoB11 = 0.0, dBoB15 = 0.0, dBoB21 = 0.0,
+         dBoB27;
+  double dBoB6C = 0.0, dBoB6L = 0.0, dBoB10C = 0.0, dBoB10L = 0.0,
+         dBoB14C = 0.0, dBoB14L = 0.0, dBoB3C = 0.0, dBoB3L = 0.0,
+         dBoB4C = 0.0, dBoB4L = 0.0;
+  double dBoB5rms = 0.0, dBoB7rms = 0.0;
+  double x0i = 0.0, x02i = 0.0, x03i = 0.0, x04i = 0.0, x05i = 0.0,
+         x06i = 0.0, x07i = 0.0, x08i = 0.0, x012i = 0.0, x010i = 0.0,
+         x018i = 0.0, x024i = 0.0, x1i = 0.0;
+  double theta = 0.0, brho = 0.0, dummyf = 0.0, conv = 0.0 ;
+  char *dummy;
+
+  FILE *fi;
+  char *fic_hcorr="//home/nadolski/codes/tracy/maille/soleil/corh.txt";
+  char *fic_vcorr="/home/nadolski/codes/tracy/maille/soleil/corv.txt";
+  char *fic_skew ="/home/nadolski/codes/tracy/maille/soleil/corqt.txt";
+/*********************************************************/
+
+
+
+  printf("Enter multipole ... \n");
+
+/* Make lists of dipoles, quadrupoles and  sextupoles */
+  for (i = 0; i <= globval.Cell_nLoc; i++)
+  {
+    getelem(i, &Cell); /* get element */
+
+    if (Cell.Elem.Pkind == Mpole)
+    {
+      if (Cell.Elem.M->Pirho!= 0.0)
+      {
+        dlist[ndip] = i;
+        ndip++;
+        if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PB[0 + HOMmax]);
+      }
+      else if (Cell.Elem.M->PBpar[2L + HOMmax] != 0.0)
+      {
+        qlist[nquad] = i;
+        nquad++;
+        if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[2L + HOMmax]);
+      }
+      else if (Cell.Elem.M->PBpar[3L + HOMmax] != 0.0)
+      {
+        slist[nsext] = i;
+        nsext++;
+        if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[3L + HOMmax]);
+      } 
+      else if ( Cell.Elem.PName[0] == 'c' && Cell.Elem.PName[1] == 'h')
+      {
+        hcorrlist[nhcorr] = i;
+        nhcorr++;
+        if (trace) printf("%s \n",Cell.Elem.PName);
+      }
+      else if ( Cell.Elem.PName[0] == 'c' && Cell.Elem.PName[1] == 'v')
+      {
+        vcorrlist[nvcorr] = i;
+        nvcorr++;
+        if (trace) printf("%s \n",Cell.Elem.PName);
+      }
+      else if ( Cell.Elem.PName[0] == 'q' && Cell.Elem.PName[1] == 't')
+      {
+        qcorrlist[nqcorr] = i;
+        nqcorr++;
+        if (trace) printf("%s \n",Cell.Elem.PName);
+      }
+    }
+  }
 
-//  /* building list of quadrupoles */
+ /* building list of quadrupoles */
 //  nquad = 0;
 //  ElemFamIndex = ElemIndex("QP1");
 //  for (i=1; i<=GetnKid(ElemFamIndex); i++){
 //         qlist[nquad] = Elem_GetPos(ElemFamIndex, i);
 //         getelem(qlist[nquad], &Cell); /* get element */
 //         nquad++;
-//         if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[2L + HOMmax]);
+//         if (!trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[2L + HOMmax]);
 //   }
-
+// 
 //  ElemFamIndex = ElemIndex("QP2");
 //  for (i=1; i<=GetnKid(ElemFamIndex); i++){
 //         qlist[nquad] = Elem_GetPos(ElemFamIndex, i);
@@ -1675,7 +1889,7 @@ void Enveloppe(double x, double px, double y, double py, double dp, double nturn
 //         nquad++;
 //         if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[2L + HOMmax]);
 //   }
-
+// 
 //  ElemFamIndex = ElemIndex("QP3");
 //  for (i=1; i<=GetnKid(ElemFamIndex); i++){
 //         qlist[nquad] = Elem_GetPos(ElemFamIndex, i);
@@ -1683,7 +1897,7 @@ void Enveloppe(double x, double px, double y, double py, double dp, double nturn
 //         nquad++;
 //         if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[2L + HOMmax]);
 //   }
-
+// 
 //   ElemFamIndex = ElemIndex("QP4");
 //  for (i=1; i<=GetnKid(ElemFamIndex); i++){
 //         qlist[nquad] = Elem_GetPos(ElemFamIndex, i);
@@ -1691,7 +1905,7 @@ void Enveloppe(double x, double px, double y, double py, double dp, double nturn
 //         nquad++;
 //         if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[2L + HOMmax]);
 //   }
-
+// 
 //  ElemFamIndex = ElemIndex("QP5");
 //  for (i=1; i<=GetnKid(ElemFamIndex); i++){
 //         qlist[nquad] = Elem_GetPos(ElemFamIndex, i);
@@ -1699,7 +1913,7 @@ void Enveloppe(double x, double px, double y, double py, double dp, double nturn
 //         nquad++;
 //         if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[2L + HOMmax]);
 //   }
-
+// 
 //  ElemFamIndex = ElemIndex("QP6");
 //  for (i=1; i<=GetnKid(ElemFamIndex); i++){
 //         qlist[nquad] = Elem_GetPos(ElemFamIndex, i);
@@ -1707,7 +1921,7 @@ void Enveloppe(double x, double px, double y, double py, double dp, double nturn
 //         nquad++;
 //         if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[2L + HOMmax]);
 //   }
-
+// 
 //  ElemFamIndex = ElemIndex("QP7");
 //  for (i=1; i<=GetnKid(ElemFamIndex); i++){
 //         qlist[nquad] = Elem_GetPos(ElemFamIndex, i);
@@ -1715,7 +1929,7 @@ void Enveloppe(double x, double px, double y, double py, double dp, double nturn
 //         nquad++;
 //         if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[2L + HOMmax]);
 //   }
-
+// 
 //  ElemFamIndex = ElemIndex("QP8");
 //  for (i=1; i<=GetnKid(ElemFamIndex); i++){
 //         qlist[nquad] = Elem_GetPos(ElemFamIndex, i);
@@ -1723,7 +1937,7 @@ void Enveloppe(double x, double px, double y, double py, double dp, double nturn
 //         nquad++;
 //         if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[2L + HOMmax]);
 //   }
-
+// 
 //  ElemFamIndex = ElemIndex("QP9");
 //  for (i=1; i<=GetnKid(ElemFamIndex); i++){
 //         qlist[nquad] = Elem_GetPos(ElemFamIndex, i);
@@ -1731,7 +1945,7 @@ void Enveloppe(double x, double px, double y, double py, double dp, double nturn
 //         nquad++;
 //         if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[2L + HOMmax]);
 //   }
-
+// 
 //  ElemFamIndex = ElemIndex("QP10");
 //  for (i=1; i<=GetnKid(ElemFamIndex); i++){
 //         qlist[nquad] = Elem_GetPos(ElemFamIndex, i);
@@ -1749,7 +1963,7 @@ void Enveloppe(double x, double px, double y, double py, double dp, double nturn
 //         nsext++;
 //         if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[3L + HOMmax]);
 //   }
-
+// 
 // ElemFamIndex = ElemIndex("SX2");
 //  for (i=1; i<=GetnKid(ElemFamIndex); i++){
 //         slist[nsext] = Elem_GetPos(ElemFamIndex, i);
@@ -1757,7 +1971,7 @@ void Enveloppe(double x, double px, double y, double py, double dp, double nturn
 //         nsext++;
 //         if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[3L + HOMmax]);
 //   }
-
+// 
 //  ElemFamIndex = ElemIndex("SX3");
 //  for (i=1; i<=GetnKid(ElemFamIndex); i++){
 //         slist[nsext] = Elem_GetPos(ElemFamIndex, i);
@@ -1765,7 +1979,7 @@ void Enveloppe(double x, double px, double y, double py, double dp, double nturn
 //         nsext++;
 //         if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[3L + HOMmax]);
 //   }
-
+// 
 //  ElemFamIndex = ElemIndex("SX4");
 //  for (i=1; i<=GetnKid(ElemFamIndex); i++){
 //         slist[nsext] = Elem_GetPos(ElemFamIndex, i);
@@ -1773,7 +1987,7 @@ void Enveloppe(double x, double px, double y, double py, double dp, double nturn
 //         nsext++;
 //         if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[3L + HOMmax]);
 //   }
-
+// 
 //  ElemFamIndex = ElemIndex("SX5");
 //  for (i=1; i<=GetnKid(ElemFamIndex); i++){
 //         slist[nsext] = Elem_GetPos(ElemFamIndex, i);
@@ -1781,7 +1995,7 @@ void Enveloppe(double x, double px, double y, double py, double dp, double nturn
 //         nsext++;
 //         if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[3L + HOMmax]);
 //   }
-
+// 
 //  ElemFamIndex = ElemIndex("SX6");
 //  for (i=1; i<=GetnKid(ElemFamIndex); i++){
 //         slist[nsext] = Elem_GetPos(ElemFamIndex, i);
@@ -1789,7 +2003,7 @@ void Enveloppe(double x, double px, double y, double py, double dp, double nturn
 //         nsext++;
 //         if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[3L + HOMmax]);
 //   }
-
+// 
 //  ElemFamIndex = ElemIndex("SX7");
 //  for (i=1; i<=GetnKid(ElemFamIndex); i++){
 //         slist[nsext] = Elem_GetPos(ElemFamIndex, i);
@@ -1797,7 +2011,7 @@ void Enveloppe(double x, double px, double y, double py, double dp, double nturn
 //         nsext++;
 //         if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[3L + HOMmax]);
 //   }
-
+// 
 //  ElemFamIndex = ElemIndex("SX8");
 //  for (i=1; i<=GetnKid(ElemFamIndex); i++){
 //         slist[nsext] = Elem_GetPos(ElemFamIndex, i);
@@ -1805,7 +2019,7 @@ void Enveloppe(double x, double px, double y, double py, double dp, double nturn
 //         nsext++;
 //         if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[3L + HOMmax]);
 //   }
-
+// 
 //  ElemFamIndex = ElemIndex("SX9");
 //  for (i=1; i<=GetnKid(ElemFamIndex); i++){
 //         slist[nsext] = Elem_GetPos(ElemFamIndex, i);
@@ -1813,7 +2027,7 @@ void Enveloppe(double x, double px, double y, double py, double dp, double nturn
 //         nsext++;
 //         if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[3L + HOMmax]);
 //   }
-
+// 
 //  ElemFamIndex = ElemIndex("SX10");
 //  for (i=1; i<=GetnKid(ElemFamIndex); i++){
 //         slist[nsext] = Elem_GetPos(ElemFamIndex, i);
@@ -1821,578 +2035,589 @@ void Enveloppe(double x, double px, double y, double py, double dp, double nturn
 //         nsext++;
 //         if (trace) printf("%s % f\n",Cell.Elem.PName, Cell.Elem.M->PBpar[3L + HOMmax]);
 //   }
+// 
+
+ /* find sextupole associated with the corrector */
+ // solution 1: find by names
+ // solution 2: use a predfined list
+ // solution 3: smothing smart ???
+  for (i=0; i< nhcorr; i++){
+    if (trace) fprintf(stdout, "%d\n", i);
+    getelem(hcorrlist[i]-1, &Cell);
+    if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
+      hcorrlistThick[i] = hcorrlist[i]-1;
+    else{
+      getelem(hcorrlist[i]+1, &Cell);
+      if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
+        hcorrlistThick[i] = hcorrlist[i]+1;
+      else{
+        getelem(hcorrlist[i]+2, &Cell);
+        if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
+          hcorrlistThick[i] = hcorrlist[i]+2;
+        else{
+          getelem(hcorrlist[i]-2, &Cell);
+          if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
+            hcorrlistThick[i] = hcorrlist[i]-2;
+          else{
+            getelem(hcorrlist[i]+3, &Cell);
+            if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
+              hcorrlistThick[i] = hcorrlist[i]+3;
+            else{
+              getelem(hcorrlist[i]-3, &Cell);
+              if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
+                hcorrlistThick[i] = hcorrlist[i]-3;
+              else fprintf(stdout, "Warning Sextupole not found for VCOR\n");
+            }
+          }
+        }
+      }
+    }
+  }
 
+ for (i=0; i< nvcorr; i++){
+   if (trace) fprintf(stdout, "%d\n", i);
+   getelem(vcorrlist[i]-1, &Cell);
+   if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
+     vcorrlistThick[i] = vcorrlist[i]-1;
+   else{
+     getelem(vcorrlist[i]+1, &Cell);
+     if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
+        vcorrlistThick[i] = vcorrlist[i]+1;
+     else{
+       getelem(vcorrlist[i]+2, &Cell);
+       if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
+          vcorrlistThick[i] = vcorrlist[i]+2;
+       else{
+         getelem(vcorrlist[i]-2, &Cell);
+         if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
+             vcorrlistThick[i] = vcorrlist[i]-2;
+         else{
+           getelem(vcorrlist[i]+3, &Cell);
+           if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
+             vcorrlistThick[i] = vcorrlist[i]+3;
+           else{
+             getelem(vcorrlist[i]-3, &Cell);
+             if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
+               vcorrlistThick[i] = vcorrlist[i]-3;
+             else fprintf(stdout, "Warning Sextupole not found for VCOR\n");
+           }
+         }
+       }
+     }
+   }
+ }
 
-//  /* find sextupole associated with the corrector */
-//  // solution 1: find by names
-//  // solution 2: use a predfined list
-//  // solution 3: smothing smart ???
-//   for (i=0; i< nhcorr; i++){
-//     if (trace) fprintf(stdout, "%d\n", i);
-//     getelem(hcorrlist[i]-1, &Cell);
-//     if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
-//       hcorrlistThick[i] = hcorrlist[i]-1;
-//     else{
-//       getelem(hcorrlist[i]+1, &Cell);
-//       if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
-//         hcorrlistThick[i] = hcorrlist[i]+1;
-//       else{
-//         getelem(hcorrlist[i]+2, &Cell);
-//         if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
-//           hcorrlistThick[i] = hcorrlist[i]+2;
-//         else{
-//           getelem(hcorrlist[i]-2, &Cell);
-//           if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
-//             hcorrlistThick[i] = hcorrlist[i]-2;
-//           else{
-//             getelem(hcorrlist[i]+3, &Cell);
-//             if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
-//               hcorrlistThick[i] = hcorrlist[i]+3;
-//             else{
-//               getelem(hcorrlist[i]-3, &Cell);
-//               if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
-//                 hcorrlistThick[i] = hcorrlist[i]-3;
-//               else fprintf(stdout, "Warning Sextupole not found for VCOR\n");
-//             }
-//           }
-//         }
-//       }
-//     }
-//   }
+ for (i=0; i< nqcorr; i++){
+ if (trace) fprintf(stdout, "%d\n", i);
+   getelem(qcorrlist[i]-1, &Cell);
+   if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
+     qcorrlistThick[i] = qcorrlist[i]-1;
+   else{
+     getelem(qcorrlist[i]+1, &Cell);
+     if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
+       qcorrlistThick[i] = qcorrlist[i]+1;
+     else{
+       getelem(qcorrlist[i]+2, &Cell);
+       if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
+         qcorrlistThick[i] = qcorrlist[i]+2;
+       else{
+         getelem(qcorrlist[i]-2, &Cell);
+         if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
+           qcorrlistThick[i] = qcorrlist[i]-2;
+         else{
+           getelem(qcorrlist[i]+3, &Cell);
+           if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
+             qcorrlistThick[i] = qcorrlist[i]+3;
+           else{
+             getelem(qcorrlist[i]-3, &Cell);
+             if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
+               qcorrlistThick[i] = qcorrlist[i]-3;
+             else fprintf(stdout, "Warning Sextupole not found for QT\n");
+           }
+         }
+       }
+     }
+   }
+ }
 
-//  for (i=0; i< nvcorr; i++){
-//    if (trace) fprintf(stdout, "%d\n", i);
-//    getelem(vcorrlist[i]-1, &Cell);
-//    if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
-//      vcorrlistThick[i] = vcorrlist[i]-1;
-//    else{
-//      getelem(vcorrlist[i]+1, &Cell);
-//      if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
-//         vcorrlistThick[i] = vcorrlist[i]+1;
-//      else{
-//        getelem(vcorrlist[i]+2, &Cell);
-//        if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
-//           vcorrlistThick[i] = vcorrlist[i]+2;
-//        else{
-//          getelem(vcorrlist[i]-2, &Cell);
-//          if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
-//              vcorrlistThick[i] = vcorrlist[i]-2;
-//          else{
-//            getelem(vcorrlist[i]+3, &Cell);
-//            if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
-//              vcorrlistThick[i] = vcorrlist[i]+3;
-//            else{
-//              getelem(vcorrlist[i]-3, &Cell);
-//              if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
-//                vcorrlistThick[i] = vcorrlist[i]-3;
-//              else fprintf(stdout, "Warning Sextupole not found for VCOR\n");
-//            }
-//          }
-//        }
-//      }
-//    }
-//  }
 
-//  for (i=0; i< nqcorr; i++){
-//  if (trace) fprintf(stdout, "%d\n", i);
-//    getelem(qcorrlist[i]-1, &Cell);
-//    if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
-//      qcorrlistThick[i] = qcorrlist[i]-1;
-//    else{
-//      getelem(qcorrlist[i]+1, &Cell);
-//      if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
-//        qcorrlistThick[i] = qcorrlist[i]+1;
-//      else{
-//        getelem(qcorrlist[i]+2, &Cell);
-//        if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
-//          qcorrlistThick[i] = qcorrlist[i]+2;
-//        else{
-//          getelem(qcorrlist[i]-2, &Cell);
-//          if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
-//            qcorrlistThick[i] = qcorrlist[i]-2;
-//          else{
-//            getelem(qcorrlist[i]+3, &Cell);
-//            if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
-//              qcorrlistThick[i] = qcorrlist[i]+3;
-//            else{
-//              getelem(qcorrlist[i]-3, &Cell);
-//              if (Cell.Elem.PName[0] == 's' && Cell.Elem.PName[1] == 'x')
-//                qcorrlistThick[i] = qcorrlist[i]-3;
-//              else fprintf(stdout, "Warning Sextupole not found for QT\n");
-//            }
-//          }
-//        }
-//      }
-//    }
-//  }
+ if (trace) printf("Elements: ndip=%d nquad=%d  nsext=%d nhcorr=%d nvcorr=%d nqcorr=%d\n",
+                     ndip,nquad,nsext,nhcorr,nvcorr,nqcorr);
 
+ /***********************************************************************************/
+ /*                                                                                 */
+ /*                        Set multipoles for dipole                                */
+ /*                                                                                 */ 
+ /*                        x0ni w/ n = p-1 for a 2p-poles                           */
+ /*                                                                                 */  
+ /***********************************************************************************/
 
-//  if (!trace) printf("Elements: ndip=%d nquad=%d  nsext=%d nhcorr=%d nvcorr=%d nqcorr=%d\n",
-//                      ndip,nquad,nsext,nhcorr,nvcorr,nqcorr);
-
-//  /***********************************************************************************/
-//  /*
-//  /***********                Set multipoles for dipole               ****************/
-//  /*
-//   *                        x0ni w/ n = p-1 for a 2p-poles
-//   *
-//  /***********************************************************************************/
-
-//   x0i   = 1.0/20e-3;  /* 1/radius */
-//   x02i  = x0i*x0i;
-//   x03i  = x02i*x0i;
-//   x04i  = x02i*x02i;
-//   x05i  = x04i*x0i;
-//   x06i  = x03i*x03i;
-//   x07i  = x06i*x0i;
-
-//   dBoB2 =  2.2e-4*0;  /* gradient, used for curve trajectory simulation */
-//   dBoB3 = -3.0e-4*1;  /* hexapole */
-//   dBoB4 =  2.0e-5*1;  /* octupole */
-//   dBoB5 = -1.0e-4*1;  /* decapole */
-//   dBoB6 = -6.0e-5*1;  /* 12-poles */
-//   dBoB7 = -1.0e-4*1;  /* 14-poles */
-
-//  for (i = 0; i < ndip; i++)
-//  {
-//    getelem(dlist[i], &Cell);
-//    theta = Cell.Elem.PL*Cell.Elem.M->UU.U0.Pirho;
+  x0i   = 1.0/20e-3;  /* 1/radius */
+  x02i  = x0i*x0i;
+  x03i  = x02i*x0i;
+  x04i  = x02i*x02i;
+  x05i  = x04i*x0i;
+  x06i  = x03i*x03i;
+  x07i  = x06i*x0i;
 
-//    /* gradient error */
-//    mKL =GetKLpar(Cell.Fnum, Cell.Knum, mOrder=2L);
-//    mKL += dBoB2*theta*x0i;
-//    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=2L, mKL);
+  dBoB2 =  2.2e-4*0;  /* gradient, used for curve trajectory simulation */
+  dBoB3 = -3.0e-4*1;  /* hexapole */
+  dBoB4 =  2.0e-5*1;  /* octupole */
+  dBoB5 = -1.0e-4*1;  /* decapole */
+  dBoB6 = -6.0e-5*1;  /* 12-poles */
+  dBoB7 = -1.0e-4*1;  /* 14-poles */
 
-//    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld theta=% e mKl=% e\n",i,
-//                Cell.Elem.PName,Cell.Fnum, Cell.Knum, theta, mKL);
+ for (i = 0; i < ndip; i++)
+ {
+   getelem(dlist[i], &Cell);
+   theta = Cell.Elem.PL*Cell.Elem.M->Pirho;
 
-//    /* sextupole error */
-//    mKL = dBoB3*theta*x02i;
-//    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=3L, mKL);
+   /* gradient error */
+   mKL =GetKLpar(Cell.Fnum, Cell.Knum, mOrder=2L);
+   mKL += dBoB2*theta*x0i;
+   SetKLpar(Cell.Fnum, Cell.Knum, mOrder=2L, mKL);
 
-//    /* octupole error */
-//    mKL = dBoB4*theta*x03i;
-//    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=4L, mKL);
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld theta=% e mKl=% e\n",i,
+               Cell.Elem.PName,Cell.Fnum, Cell.Knum, theta, mKL);
 
-//    /* decapole error */
-//    mKL = dBoB5*theta*x04i;
-//    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=5L, mKL);
+   /* sextupole error */
+   mKL = dBoB3*theta*x02i;
+   SetKLpar(Cell.Fnum, Cell.Knum, mOrder=3L, mKL);
+ if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld theta=% e mKl=% e\n",i,
+               Cell.Elem.PName,Cell.Fnum, Cell.Knum, theta, mKL);
 
-//    /* 12-pole error */
-//    mKL = dBoB6*theta*x05i;
-//    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=6L, mKL);
+   /* octupole error */
+   mKL = dBoB4*theta*x03i;
+   SetKLpar(Cell.Fnum, Cell.Knum, mOrder=4L, mKL);
+ if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld theta=% e mKl=% e\n",i,
+               Cell.Elem.PName,Cell.Fnum, Cell.Knum, theta, mKL);
 
-//    /* 14-pole error */
-//    mKL = dBoB7*theta*x06i;
-//    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=7L, mKL);
-//  }
+   /* decapole error */
+   mKL = dBoB5*theta*x04i;
+   SetKLpar(Cell.Fnum, Cell.Knum, mOrder=5L, mKL);
+ if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld theta=% e mKl=% e\n",i,
+               Cell.Elem.PName,Cell.Fnum, Cell.Knum, theta, mKL);
+
+   /* 12-pole error */
+   mKL = dBoB6*theta*x05i;
+   SetKLpar(Cell.Fnum, Cell.Knum, mOrder=6L, mKL);
+ if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld theta=% e mKl=% e\n",i,
+               Cell.Elem.PName,Cell.Fnum, Cell.Knum, theta, mKL);
+
+   /* 14-pole error */
+   mKL = dBoB7*theta*x06i;
+   SetKLpar(Cell.Fnum, Cell.Knum, mOrder=7L, mKL);
+    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld theta=% e mKl=% e\n",i,
+               Cell.Elem.PName,Cell.Fnum, Cell.Knum, theta, mKL);
+
+ }
+
+ /***********************************************************************************/
+ /*
+ /***********                Set multipoles for quadripole           ****************/
+ /*
+  *                          x0ni w/ n = p-2 for a 2p-poles
+  *
+ /***********************************************************************************/
+
+ x0i  = 1.0/30e-3;       /* 1/Radius in meters */
+ b2   = 0.0;             /* Quadrupole strength */
+ x02i = x0i*x0i;
+ x04i = x02i*x02i;       /* 10-poles */
+ x08i = x04i*x04i;       /* 20-poles */
+ x012i= x08i*x04i;       /* 28-poles */
+
+ dBoB6C  =  2.4e-4*1;
+ dBoB10C =  0.7e-4*1;
+ dBoB14C =  0.9e-4*1;
+ dBoB6L  =  0.7e-4*1;
+ dBoB10L =  1.9e-4*1;
+ dBoB14L =  1.0e-4*1;
+
+
+ x1i  = 1.0/30e-3;       /* rayon reference = 30 mm pour mesure sextupole et octupole*/
+ dBoB3L  =  2.9e-4*1;  /* sextupole qpole long */
+ dBoB4L  =  -8.6e-4*1*2.0;  /* octupole qpole long */
+ dBoB3C  =  -1.6e-4*1;  /* sextupole qpole court */
+ dBoB4C  =  -3.4e-4*1*2.0;  /* octupole qpole court */
+
+
+ for (i = 0; i < nquad; i++)
+ {
+   getelem(qlist[i], &Cell);
+//    b2 = Cell.Elem.PL*GetKpar(Cell.Fnum, Cell.Knum, 2L);
+   b2 = GetKLpar(Cell.Fnum, Cell.Knum, 2L);
+
+   /* 12-pole multipole error */
+   if ((strncmp(Cell.Elem.PName,"qp2",3)==0) || (strncmp(Cell.Elem.PName,"qp7",3)==0))
+         mKL= b2*dBoB6L*x04i;
+   else
+      mKL= b2*dBoB6C*x04i;
+   SetKLpar(Cell.Fnum, Cell.Knum, mOrder=6L, mKL);
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b2=% e mKl=% e\n",i,
+               Cell.Elem.PName,Cell.Fnum, Cell.Knum, b2, mKL);
+   
+   /* 20-pole multipole error */
+   if ((strncmp(Cell.Elem.PName,"qp2",3)==0) || (strncmp(Cell.Elem.PName,"qp7",3)==0))
+     mKL= b2*dBoB10L*x08i;
+   else
+     mKL= b2*dBoB10C*x08i;
+   SetKLpar(Cell.Fnum, Cell.Knum, mOrder=10L, mKL);
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b2=% e mKl=% e\n",i,
+               Cell.Elem.PName,Cell.Fnum, Cell.Knum, b2, mKL);
+
+   /* 28-pole multipole error */
+   if ((strncmp(Cell.Elem.PName,"qp2",3)==0) || (strncmp(Cell.Elem.PName,"qp7",3)==0))
+     mKL= b2*dBoB14L*x012i;
+   else
+     mKL= b2*dBoB14C*x012i;
+   SetKLpar(Cell.Fnum, Cell.Knum, mOrder=14L, mKL);
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b2=% e mKl=% e\n",i,
+               Cell.Elem.PName,Cell.Fnum, Cell.Knum, b2, mKL);
+
+   /* sextupole mesure quadrupoles longs*/
+   if ((strncmp(Cell.Elem.PName,"qp2",3)==0) || (strncmp(Cell.Elem.PName,"qp7",3)==0))
+      mKL= b2*dBoB3L*x1i;
+   else
+      mKL= b2*dBoB3C*x1i;
+   SetKLpar(Cell.Fnum, Cell.Knum, mOrder=3L, mKL);
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b2=% e mKl=% e\n",i,
+               Cell.Elem.PName,Cell.Fnum, Cell.Knum, b2, mKL);
+
+   /* octupole mesure quadrupoles longs*/
+   if ((strncmp(Cell.Elem.PName,"qp2",3)==0) || (strncmp(Cell.Elem.PName,"qp7",3)==0))
+      mKL= b2*dBoB4L*x1i*x1i;
+   else
+      mKL= b2*dBoB4C*x1i*x1i;
+   SetKLpar(Cell.Fnum, Cell.Knum, mOrder=4L, mKL);
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b2=% e mKl=% e\n",i,
+               Cell.Elem.PName,Cell.Fnum, Cell.Knum, b2, mKL);
+ }
+
+ /***********************************************************************************/
+ /*
+ /***********              Set multipoles for sextupole              ****************/
+ /*
+  *                        x0ni w/ n = p-3 for a 2p-poles
+  *
+ /***********************************************************************************/
+
+  b3    = 0.0;
+  x0i   = 1.0/32e-3;
+  x02i  = x0i*x0i;
+  x04i  = x02i*x02i;
+  x06i  = x04i*x02i;   /* 18-poles */
+  x012i = x06i*x06i;   /* 30-poles */
+  x018i = x012i*x06i;  /* 42-poles */
+  x024i = x012i*x012i; /* 54-poles */
+
+  /* multipoles from dipolar unallowed component */
+  dBoB5  =   5.4e-4*1;
+  dBoB7  =   3.3e-4*1;
+  dBoB5rms  =  4.7e-4*1;
+  dBoB7rms  =  2.1e-4*1;
+
+  /* allowed multipoles */
+  dBoB9  =  -4.7e-4*1;
+  dBoB15 =  -9.0e-4*1;
+  dBoB21 =  -20.9e-4*1;
+  dBoB27 =    0.8e-4*1;
+/*
+  dBoB9  =  3.1e-3*1;
+  dBoB15 =  5.0e-4*1;
+  dBoB21 =  -2.0e-2*1;
+  dBoB27 =  1.1e-2*1;
+*/
+ for (i = 0; i < nsext; i++)
+ {
+   getelem(slist[i], &Cell);
+   b3 = GetKLpar(Cell.Fnum, Cell.Knum, 3L);
+
+   /* 10-pole multipole error */
+   mKL= b3*dBoB5*x02i;
+   SetKLpar(Cell.Fnum, Cell.Knum, mOrder=5L, mKL);
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b3=% e mKl=% e\n",i,
+               Cell.Elem.PName,Cell.Fnum, Cell.Knum, b3, mKL);
+
+   /* 14-pole multipole error */
+   mKL= b3*dBoB7*x04i;
+   SetKLpar(Cell.Fnum, Cell.Knum, mOrder=7L, mKL);
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b3=% e mKl=% e\n",i,
+               Cell.Elem.PName,Cell.Fnum, Cell.Knum, b3, mKL);
+
+   /* 18-pole multipole error */
+   mKL= b3*dBoB9*x06i;
+   SetKLpar(Cell.Fnum, Cell.Knum, mOrder=9L, mKL);
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b3=% e mKl=% e\n",i,
+               Cell.Elem.PName,Cell.Fnum, Cell.Knum, b3, mKL);
+
+   /* 30-pole multipole error */
+   mKL= b3*dBoB15*x012i;
+   SetKLpar(Cell.Fnum, Cell.Knum, mOrder=15L, mKL);
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b3=% e mKl=% e\n",i,
+               Cell.Elem.PName,Cell.Fnum, Cell.Knum, b3, mKL);
+
+   /* 42-pole multipole error */
+   mKL= b3*dBoB21*x018i;
+   SetKLpar(Cell.Fnum, Cell.Knum, mOrder=21L, mKL);
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b3=% e mKl=% e\n",i,
+               Cell.Elem.PName,Cell.Fnum, Cell.Knum, b3, mKL);
+
+   /* 54-pole multipole error */
+   mKL= b3*dBoB27*x024i;
+   SetKLpar(Cell.Fnum, Cell.Knum, mOrder=27L, mKL);
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b3=% e mKl=% e\n",i,
+               Cell.Elem.PName,Cell.Fnum, Cell.Knum, b3, mKL);
+}
+
+ /***********************************************************************************/
+ /*
+ /******  Set multipoles for sextupole horizontal correctors         ****************/
+ /*
+  *                x0ni w/ n = p-1 for a 2p-poles
+  *
+ /***********************************************************************************/
+  x0i   = 1.0/35e-3;  /* 1/radius */
+  x02i  = x0i*x0i;
+  x03i  = x02i*x0i;
+  x04i  = x02i*x02i;
+  x05i  = x04i*x0i;
+  x06i  = x03i*x03i;
+  x010i = x05i*x05i;
 
-//  /***********************************************************************************/
-//  /*
-//  /***********                Set multipoles for quadripole           ****************/
-//  /*
-//   *                          x0ni w/ n = p-2 for a 2p-poles
-//   *
-//  /***********************************************************************************/
-
-//  x0i  = 1.0/30e-3;       /* 1/Radius in meters */
-//  b2   = 0.0;             /* Quadrupole strength */
-//  x02i = x0i*x0i;
-//  x04i = x02i*x02i;       /* 10-poles */
-//  x08i = x04i*x04i;       /* 20-poles */
-//  x012i= x08i*x04i;       /* 28-poles */
-
-//  dBoB6C  =  2.4e-4*1;
-//  dBoB10C =  0.7e-4*1;
-//  dBoB14C =  0.9e-4*1;
-//  dBoB6L  =  0.7e-4*1;
-//  dBoB10L =  1.9e-4*1;
-//  dBoB14L =  1.0e-4*1;
-
-
-//  x1i  = 1.0/30e-3;       /* rayon reference = 30 mm pour mesure sextupole et octupole*/
-//  dBoB3L  =  2.9e-4*1;  /* sextupole qpole long */
-//  dBoB4L  =  -8.6e-4*1;  /* octupole qpole long */
-//  dBoB3C  =  -1.6e-4*1;  /* sextupole qpole court */
-//  dBoB4C  =  -3.4e-4*1;  /* octupole qpole court */
-
-
-//  for (i = 0; i < nquad; i++)
-//  {
-//    getelem(qlist[i], &Cell);
-// //    b2 = Cell.Elem.PL*GetKpar(Cell.Fnum, Cell.Knum, 2L);
-//    b2 = GetKLpar(Cell.Fnum, Cell.Knum, 2L);
-
-//    /* 12-pole multipole error */
-//    if ((strncmp(Cell.Elem.PName,"qp2",3)==0) || (strncmp(Cell.Elem.PName,"qp7",3)==0))
-//          mKL= b2*dBoB6L*x04i;
-//    else
-//       mKL= b2*dBoB6C*x04i;
-//    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=6L, mKL);
-//    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b2=% e mKl=% e\n",i,
-//                Cell.Elem.PName,Cell.Fnum, Cell.Knum, b2, mKL);
-   
-//    /* 20-pole multipole error */
-//    if ((strncmp(Cell.Elem.PName,"qp2",3)==0) || (strncmp(Cell.Elem.PName,"qp7",3)==0))
-//      mKL= b2*dBoB10L*x08i;
-//    else
-//      mKL= b2*dBoB10C*x08i;
-//    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=10L, mKL);
-//    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b2=% e mKl=% e\n",i,
-//                Cell.Elem.PName,Cell.Fnum, Cell.Knum, b2, mKL);
-
-//    /* 28-pole multipole error */
-//    if ((strncmp(Cell.Elem.PName,"qp2",3)==0) || (strncmp(Cell.Elem.PName,"qp7",3)==0))
-//      mKL= b2*dBoB14L*x012i;
-//    else
-//      mKL= b2*dBoB14C*x012i;
-//    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=14L, mKL);
-//    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b2=% e mKl=% e\n",i,
-//                Cell.Elem.PName,Cell.Fnum, Cell.Knum, b2, mKL);
-
-//    /* sextupole mesure quadrupoles longs*/
-//    if ((strncmp(Cell.Elem.PName,"qp2",3)==0) || (strncmp(Cell.Elem.PName,"qp7",3)==0))
-//       mKL= b2*dBoB3L*x1i;
-//    else
-//       mKL= b2*dBoB3C*x1i;
-//    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=3L, mKL);
-//    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b2=% e mKl=% e\n",i,
-//                Cell.Elem.PName,Cell.Fnum, Cell.Knum, b2, mKL);
-
-//    /* octupole mesure quadrupoles longs*/
-//    if ((strncmp(Cell.Elem.PName,"qp2",3)==0) || (strncmp(Cell.Elem.PName,"qp7",3)==0))
-//       mKL= b2*dBoB4L*x1i*x1i;
-//    else
-//       mKL= b2*dBoB4C*x1i*x1i;
-//    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=4L, mKL);
-//    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b2=% e mKl=% e\n",i,
-//                Cell.Elem.PName,Cell.Fnum, Cell.Knum, b2, mKL);
-//  }
+  dBoB5  = 0.430*1;  /* decapole */
+  dBoB7  = 0.063*1;  /* 14-poles */
+  dBoB11 =-0.037*1;  /* 22-poles */
 
-//  /***********************************************************************************/
-//  /*
-//  /***********              Set multipoles for sextupole              ****************/
-//  /*
-//   *                        x0ni w/ n = p-3 for a 2p-poles
-//   *
-//  /***********************************************************************************/
-
-//   b3    = 0.0;
-//   x0i   = 1.0/32e-3;
-//   x02i  = x0i*x0i;
-//   x04i  = x02i*x02i;
-//   x06i  = x04i*x02i;   /* 18-poles */
-//   x012i = x06i*x06i;   /* 30-poles */
-//   x018i = x012i*x06i;  /* 42-poles */
-//   x024i = x012i*x012i; /* 54-poles */
-
-//   /* multipoles from dipolar unallowed component */
-//   dBoB5  =   5.4e-4*1;
-//   dBoB7  =   3.3e-4*1;
-//   dBoB5rms  =  4.7e-4*1;
-//   dBoB7rms  =  2.1e-4*1;
-
-//   /* allowed multipoles */
-//   dBoB9  =  -4.7e-4*1;
-//   dBoB15 =  -9.0e-4*1;
-//   dBoB21 =  -20.9e-4*1;
-//   dBoB27 =    0.8e-4*1;
-// /*
-//   dBoB9  =  3.1e-3*1;
-//   dBoB15 =  5.0e-4*1;
-//   dBoB21 =  -2.0e-2*1;
-//   dBoB27 =  1.1e-2*1;
-// */
-//  for (i = 0; i < nsext; i++)
-//  {
-//    getelem(slist[i], &Cell);
-//    b3 = GetKLpar(Cell.Fnum, Cell.Knum, 3L);
-
-//    /* 10-pole multipole error */
-//    mKL= b3*dBoB5*x02i;
-//    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=5L, mKL);
-//    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b3=% e mKl=% e\n",i,
-//                Cell.Elem.PName,Cell.Fnum, Cell.Knum, b3, mKL);
-
-//    /* 14-pole multipole error */
-//    mKL= b3*dBoB7*x04i;
-//    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=7L, mKL);
-//    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b3=% e mKl=% e\n",i,
-//                Cell.Elem.PName,Cell.Fnum, Cell.Knum, b3, mKL);
-
-//    /* 18-pole multipole error */
-//    mKL= b3*dBoB9*x06i;
-//    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=9L, mKL);
-//    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b3=% e mKl=% e\n",i,
-//                Cell.Elem.PName,Cell.Fnum, Cell.Knum, b3, mKL);
-
-//    /* 30-pole multipole error */
-//    mKL= b3*dBoB15*x012i;
-//    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=15L, mKL);
-//    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b3=% e mKl=% e\n",i,
-//                Cell.Elem.PName,Cell.Fnum, Cell.Knum, b3, mKL);
-
-//    /* 42-pole multipole error */
-//    mKL= b3*dBoB21*x018i;
-//    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=21L, mKL);
-//    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b3=% e mKl=% e\n",i,
-//                Cell.Elem.PName,Cell.Fnum, Cell.Knum, b3, mKL);
-
-//    /* 54-pole multipole error */
-//    mKL= b3*dBoB27*x024i;
-//    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=27L, mKL);
-//    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b3=% e mKl=% e\n",i,
-//                Cell.Elem.PName,Cell.Fnum, Cell.Knum, b3, mKL);
-// }
+  brho = globval.Energy/0.299792458; /* magnetic rigidity */
+//  brho = 1.0; /* magnetic rigidity */
+  conv = 8.14e-4;  /*conversion des A en T.m*/
 
-//  /***********************************************************************************/
-//  /*
-//  /******  Set multipoles for sextupole horizontal correctors         ****************/
-//  /*
-//   *                x0ni w/ n = p-1 for a 2p-poles
-//   *
-//  /***********************************************************************************/
-//   x0i   = 1.0/35e-3;  /* 1/radius */
-//   x02i  = x0i*x0i;
-//   x03i  = x02i*x0i;
-//   x04i  = x02i*x02i;
-//   x05i  = x04i*x0i;
-//   x06i  = x03i*x03i;
-//   x010i = x05i*x05i;
-
-//   dBoB5  = 0.430*1;  /* decapole */
-//   dBoB7  = 0.063*1;  /* 14-poles */
-//   dBoB11 =-0.037*1;  /* 22-poles */
-
-//   brho = globval.Energy/0.299792458; /* magnetic rigidity */
-// //  brho = 1.0; /* magnetic rigidity */
-//   conv = 8.14e-4;  /*conversion des A en T.m*/
-
-//   /* open H corrector file */
-//   if ((fi = fopen(fic_hcorr,"r")) == NULL)
-//   {
-//     fprintf(stderr, "Error while opening file %s \n",fic_hcorr);
-//     exit(1);
-//   }
+  /* open H corrector file */
+  if ((fi = fopen(fic_hcorr,"r")) == NULL)
+  {
+    fprintf(stderr, "Error while opening file %s \n",fic_hcorr);
+    exit(1);
+  }
 
-//   for (i = 0; i < nhcorr; i++)
-//   {
-//     fscanf(fi,"%le \n", &hcorr[i]);
-//   }
-//   fclose(fi); /* close H corrector file */
-
-// //   for (i = 0; i < nhcorr; i++){
-// //     getelem(hcorrlist[i], &Cell);
-// //     corr_strength = hcorr[i]*conv/brho;
-// //
-// //     /* decapole error */
-// //     mKL = dBoB5*corr_strength*x04i;
-// //     SetKLpar(Cell.Fnum, Cell.Knum, mOrder=5L, mKL);
-// //
-// //     if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
-// //                 Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
-// //     /* 14-pole error */
-// //     mKL = dBoB7*corr_strength*x06i;
-// //     SetKLpar(Cell.Fnum, Cell.Knum, mOrder=7L, mKL);
-// //
-// //     if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
-// //               Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
-// //
-// //     /* 22-pole error */
-// //     mKL = dBoB11*corr_strength*x010i;
-// //     SetKLpar(Cell.Fnum, Cell.Knum, mOrder=11, mKL);
-// //
-// //     if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
-// //                 Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
-// //   }
+  for (i = 0; i < nhcorr; i++)
+  {
+    fscanf(fi,"%le \n", &hcorr[i]);
+  }
+  fclose(fi); /* close H corrector file */
 
 //   for (i = 0; i < nhcorr; i++){
-//     getelem(hcorrlistThick[i], &Cell);
+//     getelem(hcorrlist[i], &Cell);
 //     corr_strength = hcorr[i]*conv/brho;
-
-//     /* gradient error */
-//     mKL = GetKLpar(Cell.Fnum, Cell.Knum, mOrder=5L);
-//     mKL += dBoB5*corr_strength*x04i;
+//
+//     /* decapole error */
+//     mKL = dBoB5*corr_strength*x04i;
 //     SetKLpar(Cell.Fnum, Cell.Knum, mOrder=5L, mKL);
-
+//
 //     if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
-//     Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
+//                 Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
 //     /* 14-pole error */
-//     mKL = GetKLpar(Cell.Fnum, Cell.Knum, mOrder=7L);
-//     mKL += dBoB7*corr_strength*x06i;
+//     mKL = dBoB7*corr_strength*x06i;
 //     SetKLpar(Cell.Fnum, Cell.Knum, mOrder=7L, mKL);
-
+//
 //     if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
-//     Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
-
+//               Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
+//
 //     /* 22-pole error */
-//     mKL = GetKLpar(Cell.Fnum, Cell.Knum, mOrder=11L);
-//     mKL += dBoB11*corr_strength*x010i;
+//     mKL = dBoB11*corr_strength*x010i;
 //     SetKLpar(Cell.Fnum, Cell.Knum, mOrder=11, mKL);
-
+//
 //     if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
-//     Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
+//                 Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
 //   }
 
-//  /***********************************************************************************/
-//  /*
-//  /******  Set multipoles for vertical correctors           ****************/
-//  /*
-//   *                    x0ni w/ n = p-1 for a 2p-poles
-//   *
-//  /***********************************************************************************/
-
-//   x0i   = 1.0/35e-3;  /* 1/radius */
-//   x02i  = x0i*x0i;
-//   x03i  = x02i*x0i;
-//   x04i  = x02i*x02i;
-//   x05i  = x04i*x0i;
-//   x06i  = x03i*x03i;
-//   x010i = x05i*x05i;
-
-//   dBoB5  = -0.430*1;  /* decapole */
-//   dBoB7  =  0.063*1;  /* 14-poles */
-//   dBoB11 =  0.037*1;  /* 22-poles */
-
-//   brho = globval.Energy/0.299792458; /* magnetic rigidity */
-//   conv = 4.642e-4;  /*conversion des A en T.m*/
-
-
-//   /* open V corrector file */
-//   if ((fi = fopen(fic_vcorr,"r")) == NULL)
-//   {
-//     fprintf(stderr, "Error while opening file %s \n",fic_vcorr);
-//     exit(1);
-//   }
+  for (i = 0; i < nhcorr; i++){
+    getelem(hcorrlistThick[i], &Cell);
+    corr_strength = hcorr[i]*conv/brho;
 
-//   for (i = 0; i < nvcorr; i++){
-//     fscanf(fi,"%le\n", &vcorr[i]);
-//   }
-//   fclose(fi); /* close V corrector file */
-
-// //  for (i = 0; i < nvcorr; i++)
-// //  {
-// //    getelem(vcorrlist[i], &Cell);
-// //    corr_strength = vcorr[i]*conv/brho;
-// //
-// //    /* skew decapole error */
-// //    mKL = dBoB5*corr_strength*x04i;
-// //    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=-5L, mKL);
-// //
-// //    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
-// //                Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
-// //    /* skew 14-pole error */
-// //    mKL = dBoB7*corr_strength*x06i;
-// //    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=-7L, mKL);
-// //
-// //    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
-// //             Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
-// //
-// //    /* skew 22-pole error */
-// //    mKL = dBoB11*corr_strength*x010i;
-// //    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=-11L, mKL);
-// //
-// //    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
-// //                Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
-// //  }
+    /* gradient error */
+    mKL = GetKLpar(Cell.Fnum, Cell.Knum, mOrder=5L);
+    mKL += dBoB5*corr_strength*x04i;
+    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=5L, mKL);
+
+    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
+    Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
+    /* 14-pole error */
+    mKL = GetKLpar(Cell.Fnum, Cell.Knum, mOrder=7L);
+    mKL += dBoB7*corr_strength*x06i;
+    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=7L, mKL);
+
+    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
+    Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
+
+    /* 22-pole error */
+    mKL = GetKLpar(Cell.Fnum, Cell.Knum, mOrder=11L);
+    mKL += dBoB11*corr_strength*x010i;
+    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=11, mKL);
+
+    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
+    Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
+  }
+
+ /***********************************************************************************/
+ /*
+ /******  Set multipoles for vertical correctors           ****************/
+ /*
+  *                    x0ni w/ n = p-1 for a 2p-poles
+  *
+ /***********************************************************************************/
+
+  x0i   = 1.0/35e-3;  /* 1/radius */
+  x02i  = x0i*x0i;
+  x03i  = x02i*x0i;
+  x04i  = x02i*x02i;
+  x05i  = x04i*x0i;
+  x06i  = x03i*x03i;
+  x010i = x05i*x05i;
+
+  dBoB5  = -0.430*1;  /* decapole */
+  dBoB7  =  0.063*1;  /* 14-poles */
+  dBoB11 =  0.037*1;  /* 22-poles */
+
+  brho = globval.Energy/0.299792458; /* magnetic rigidity */
+  conv = 4.642e-4;  /*conversion des A en T.m*/
+
+
+  /* open V corrector file */
+  if ((fi = fopen(fic_vcorr,"r")) == NULL)
+  {
+    fprintf(stderr, "Error while opening file %s \n",fic_vcorr);
+    exit(1);
+  }
+
+  for (i = 0; i < nvcorr; i++){
+    fscanf(fi,"%le\n", &vcorr[i]);
+  }
+  fclose(fi); /* close V corrector file */
 
 //  for (i = 0; i < nvcorr; i++)
 //  {
-//    getelem(vcorrlistThick[i], &Cell);
+//    getelem(vcorrlist[i], &Cell);
 //    corr_strength = vcorr[i]*conv/brho;
-
+//
 //    /* skew decapole error */
-//    mKL = GetKLpar(Cell.Fnum, Cell.Knum, mOrder=-5L);
-//    mKL += dBoB5*corr_strength*x04i;
+//    mKL = dBoB5*corr_strength*x04i;
 //    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=-5L, mKL);
-
+//
 //    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
-//    Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
-
+//                Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
 //    /* skew 14-pole error */
-//    mKL = GetKLpar(Cell.Fnum, Cell.Knum, mOrder=-7L);
-//    mKL += dBoB7*corr_strength*x06i;
+//    mKL = dBoB7*corr_strength*x06i;
 //    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=-7L, mKL);
-
+//
 //    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
-//    Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
-
+//             Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
+//
 //    /* skew 22-pole error */
-//    mKL = GetKLpar(Cell.Fnum, Cell.Knum, mOrder=-11L);
-//    mKL += dBoB11*corr_strength*x010i;
+//    mKL = dBoB11*corr_strength*x010i;
 //    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=-11L, mKL);
-
+//
 //    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
-//    Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
+//                Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
 //  }
-//  /***********************************************************************************/
-//  /*
-//  /******                Set multipoles for skew quadripole           ****************/
-//  /*
-//   *                        x0ni w/ n = p-2 for a 2p-poles
-//   *
-//  /***********************************************************************************/
-
-//  /* Set multipoles for skew quad */
-//   x0i   = 1.0/35e-3;  /* 1/radius */
-//   x02i  = x0i*x0i;
-
-//   dBoB4  = -0.680*0;  /* Octupole */
-
-//   /* open skew quaI (A) *
-// 310
-// 450
-// 500
-// 520
-// 540
-// 550
-// 560
-// d file */
-
-//   // brho = 2.75/0.299792458; /* magnetic rigidity */
-//    brho = globval.Energy/0.299792458; /* magnetic rigidity */
-//    conv = 93.83e-4;  /*conversion des A en T*/
-
-
-//   if ((fi = fopen(fic_skew,"r")) == NULL)
-//   {
-//     fprintf(stderr, "Error while opening file %s \n",fic_skew);
-//     exit(1);
-//   }
 
-//   for (i = 0; i < nqcorr; i++)
-//   {
-//     fscanf(fi,"%le \n", &qcorr[i]);
-//   }
-//   fclose(fi); /* close skew quad file */
-
-// //  for (i = 0; i < nqcorr; i++)
-// //  {
-// //    getelem(qcorrlist[i], &Cell);
-// //
-// //    /* skew octupole */
-// //    mKL = dBoB4*qcorr[i]*conv/brho*x02i;
-// //    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=-4L, mKL);
-// //
-// //    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
-// //                Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
-// //  }
+ for (i = 0; i < nvcorr; i++)
+ {
+   getelem(vcorrlistThick[i], &Cell);
+   corr_strength = vcorr[i]*conv/brho;
+
+   /* skew decapole error */
+   mKL = GetKLpar(Cell.Fnum, Cell.Knum, mOrder=-5L);
+   mKL += dBoB5*corr_strength*x04i;
+   SetKLpar(Cell.Fnum, Cell.Knum, mOrder=-5L, mKL);
+
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
+   Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
+
+   /* skew 14-pole error */
+   mKL = GetKLpar(Cell.Fnum, Cell.Knum, mOrder=-7L);
+   mKL += dBoB7*corr_strength*x06i;
+   SetKLpar(Cell.Fnum, Cell.Knum, mOrder=-7L, mKL);
+
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
+   Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
+
+   /* skew 22-pole error */
+   mKL = GetKLpar(Cell.Fnum, Cell.Knum, mOrder=-11L);
+   mKL += dBoB11*corr_strength*x010i;
+   SetKLpar(Cell.Fnum, Cell.Knum, mOrder=-11L, mKL);
+
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
+   Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
+ }
+ /***********************************************************************************/
+ /*
+ /******                Set multipoles for skew quadripole           ****************/
+ /*
+  *                        x0ni w/ n = p-2 for a 2p-poles
+  *
+ /***********************************************************************************/
+
+ /* Set multipoles for skew quad */
+  x0i   = 1.0/35e-3;  /* 1/radius */
+  x02i  = x0i*x0i;
+
+  dBoB4  = -0.680*0;  /* Octupole */
+
+  /* open skew quaI (A) *
+310
+450
+500
+520
+540
+550
+560
+d file */
+
+  // brho = 2.75/0.299792458; /* magnetic rigidity */
+   brho = globval.Energy/0.299792458; /* magnetic rigidity */
+   conv = 93.83e-4;  /*conversion des A en T*/
+
+
+  if ((fi = fopen(fic_skew,"r")) == NULL)
+  {
+    fprintf(stderr, "Error while opening file %s \n",fic_skew);
+    exit(1);
+  }
+
+  for (i = 0; i < nqcorr; i++)
+  {
+    fscanf(fi,"%le \n", &qcorr[i]);
+  }
+  fclose(fi); /* close skew quad file */
+
 //  for (i = 0; i < nqcorr; i++)
 //  {
 //    getelem(qcorrlist[i], &Cell);
-
+//
 //    /* skew octupole */
-//    mKL = GetKLpar(Cell.Fnum, Cell.Knum, mOrder=-4L);
-//    mKL += dBoB4*qcorr[i]*conv/brho*x02i;
+//    mKL = dBoB4*qcorr[i]*conv/brho*x02i;
 //    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=-4L, mKL);
-
+//
 //    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
-//    Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
+//                Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
 //  }
-// }
+ for (i = 0; i < nqcorr; i++)
+ {
+   getelem(qcorrlist[i], &Cell);
+
+   /* skew octupole */
+   mKL = GetKLpar(Cell.Fnum, Cell.Knum, mOrder=-4L);
+   mKL += dBoB4*qcorr[i]*conv/brho*x02i;
+   SetKLpar(Cell.Fnum, Cell.Knum, mOrder=-4L, mKL);
+
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
+   Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
+ }
+}
 
 /****************************************************************************/
-/* void Multipole(void)
+/* void Multipole_thinsext(void)
 
    Purpose:
        Set multipole in dipoles, quadrupoles, thin sextupoles, skew quadrupole,
@@ -2421,7 +2646,7 @@ void Enveloppe(double x, double px, double y, double py, double dp, double nturn
 
 ****************************************************************************/
 
-void Multipole(void)
+void Multipole_thinsext(void)
 {
   int i = 0;
   int ndip  = 0,  /* Number of dipoles */
@@ -2546,7 +2771,7 @@ void Multipole(void)
    mKL = dBoB2*theta*x0i;
    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=2L, mKL);
 
-   if (trace) printf("num= %4d name = %s Fnum = %3d, Knum=%3d theta=% e mKl=% e\n",i,
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld theta=% e mKl=% e\n",i,
                Cell.Elem.PName,Cell.Fnum, Cell.Knum, theta, mKL);
 
    /* sextupole error */
@@ -2611,7 +2836,7 @@ void Multipole(void)
    else
       mKL= b2*dBoB6C*x04i;
    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=6L, mKL);
-   if (trace) printf("num= %4d name = %s Fnum = %3d, Knum=%3d b2=% e mKl=% e\n",i,
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b2=% e mKl=% e\n",i,
                Cell.Elem.PName,Cell.Fnum, Cell.Knum, b2, mKL);
 
    /* 20-pole multipole error */
@@ -2620,7 +2845,7 @@ void Multipole(void)
    else
      mKL= b2*dBoB10C*x08i;
    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=10L, mKL);
-   if (trace) printf("num= %4d name = %s Fnum = %3d, Knum=%3d b2=% e mKl=% e\n",i,
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b2=% e mKl=% e\n",i,
                Cell.Elem.PName,Cell.Fnum, Cell.Knum, b2, mKL);
 
    /* 28-pole multipole error */
@@ -2629,7 +2854,7 @@ void Multipole(void)
    else
      mKL= b2*dBoB14C*x012i;
    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=14L, mKL);
-   if (trace) printf("num= %4d name = %s Fnum = %3d, Knum=%3d b2=% e mKl=% e\n",i,
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b2=% e mKl=% e\n",i,
                Cell.Elem.PName,Cell.Fnum, Cell.Knum, b2, mKL);
 
 /* sextupole mesure quadrupoles longs*/
@@ -2688,25 +2913,25 @@ void Multipole(void)
    /* 18-pole multipole error */
    mKL= b3*dBoB9*x06i;
    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=9L, mKL);
-   if (trace) printf("num= %4d name = %s Fnum = %3d, Knum=%3d b3=% e mKl=% e\n",i,
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b3=% e mKl=% e\n",i,
                Cell.Elem.PName,Cell.Fnum, Cell.Knum, b3, mKL);
 
    /* 30-pole multipole error */
    mKL= b3*dBoB15*x012i;
    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=15L, mKL);
-   if (trace) printf("num= %4d name = %s Fnum = %3d, Knum=%3d b3=% e mKl=% e\n",i,
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b3=% e mKl=% e\n",i,
                Cell.Elem.PName,Cell.Fnum, Cell.Knum, b3, mKL);
 
    /* 42-pole multipole error */
    mKL= b3*dBoB21*x018i;
    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=21L, mKL);
-   if (trace) printf("num= %4d name = %s Fnum = %3d, Knum=%3d b3=% e mKl=% e\n",i,
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b3=% e mKl=% e\n",i,
                Cell.Elem.PName,Cell.Fnum, Cell.Knum, b3, mKL);
 
    /* 54-pole multipole error */
    mKL= b3*dBoB27*x024i;
    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=27L, mKL);
-   if (trace) printf("num= %4d name = %s Fnum = %3d, Knum=%3d b3=% e mKl=% e\n",i,
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld b3=% e mKl=% e\n",i,
                Cell.Elem.PName,Cell.Fnum, Cell.Knum, b3, mKL);
 }
 
@@ -2754,20 +2979,20 @@ void Multipole(void)
    mKL = dBoB5*corr_strength*x04i;
    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=5L, mKL);
 
-   if (trace) printf("num= %4d name = %s Fnum = %3d, Knum=%3d BL/brho=% e mKl=% e\n",i,
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
                Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
    /* 14-pole error */
    mKL = dBoB7*corr_strength*x06i;
    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=7L, mKL);
 
-   if (trace) printf("num= %4d name = %s Fnum = %3d, Knum=%3d BL/brho=% e mKl=% e\n",i,
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
             Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
 
    /* 22-pole error */
    mKL = dBoB11*corr_strength*x010i;
    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=11, mKL);
 
-   if (trace) printf("num= %4d name = %s Fnum = %3d, Knum=%3d BL/brho=% e mKl=% e\n",i,
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
                Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
  }
 
@@ -2817,20 +3042,20 @@ void Multipole(void)
    mKL = dBoB5*corr_strength*x04i;
    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=-5L, mKL);
 
-   if (trace) printf("num= %4d name = %s Fnum = %3d, Knum=%3d BL/brho=% e mKl=% e\n",i,
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
                Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
    /* skew 14-pole error */
    mKL = dBoB7*corr_strength*x06i;
    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=-7L, mKL);
 
-   if (trace) printf("num= %4d name = %s Fnum = %3d, Knum=%3d BL/brho=% e mKl=% e\n",i,
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
             Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
 
    /* skew 22-pole error */
    mKL = dBoB11*corr_strength*x010i;
    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=-11, mKL);
 
-   if (trace) printf("num= %4d name = %s Fnum = %3d, Knum=%3d BL/brho=% e mKl=% e\n",i,
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
                Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
  }
 
@@ -2883,7 +3108,7 @@ d file */
    mKL = dBoB4*qcorr[i]*conv/brho*x02i;
    SetKLpar(Cell.Fnum, Cell.Knum, mOrder=-4L, mKL);
 
-   if (trace) printf("num= %4d name = %s Fnum = %3d, Knum=%3d BL/brho=% e mKl=% e\n",i,
+   if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld BL/brho=% e mKl=% e\n",i,
                Cell.Elem.PName,Cell.Fnum, Cell.Knum, corr_strength, mKL);
  }
 }
@@ -2975,7 +3200,7 @@ void SetSkewQuad(void)
     mKL = b2*cos(2*theta[i]);
     SetKLpar(Cell.Fnum, Cell.Knum, mOrder=2L, mKL);
 
-    if (trace) printf("num= %4d name = %s Fnum = %3d, Knum=%3d KL=% e, KtiltL=% e\n"
+    if (trace) printf("num= %4d name = %s Fnum = %3ld, Knum=%3ld KL=% e, KtiltL=% e\n"
                 ,i,
                 Cell.Elem.PName,Cell.Fnum, Cell.Knum,
                 Cell.Elem.M->PBpar[HOMmax+2],
@@ -3049,11 +3274,15 @@ void SetSkewQuad(void)
                            double ep_min, double ep_max, long nstepp,
                            double em_min, double em_max, long nstepm)
    Purpose:
-        Compute momemtum acceptance along the ring
-
+        Compute momemtum acceptance along the ring, track the particle with 
+	different energy, momentum acceptance is the energy when the particle
+	is lost or the last energy if the particle is not lost.
+	
+         Based on the version in tracy 2.
+	
    Input:
-       deb first element for momentum acceptance
-       fin last element for momentum acceptance
+       deb first element for momentum acceptance,  "debut" is beginning in French
+       fin last element for momentum acceptance,   "fin"   is end in French
 
        ep_min minimum energy deviation for positive momentum acceptance
        ep_max maximum energy deviation for positive momentum acceptance
@@ -3090,7 +3319,11 @@ void SetSkewQuad(void)
                 delta_closed_orbite = dp(cavite)/2
        21/10/03 add array for vertical intial conditions using tracking
                 removed choice of tracking: now this should be done outside
-
+		
+       23/07/10  modify the call variable to the Cell_Pass( ): j-1L --> j (L3435, L3590)
+	         since the Cell_Pass( ) is tracking from element i0 to i1(tracy 3), and
+		       the Cell_Pass( ) is tracking from element i0+1L to i1(tracy 2).
+                                                             		
 ****************************************************************************/
 void MomentumAcceptance(long deb, long fin, double ep_min, double ep_max,
 			long nstepp, double em_min, double em_max, long nstepm)
@@ -3100,7 +3333,7 @@ void MomentumAcceptance(long deb, long fin, double ep_min, double ep_max,
   long          i = 0L, j = 0L, pos = 0L;
   CellType      Cell, Clost;
   double        x = 0.0, px = 0.0, z = 0.0, pz = 0.0, ctau0 = 0.0, delta = 0.0;
-  Vector         x0;
+  Vector        x0;
   const long    nturn = 1000L;
   FILE          *outf2, *outf1;
   // Nonzero vertical amplitude
@@ -3131,7 +3364,7 @@ void MomentumAcceptance(long deb, long fin, double ep_min, double ep_max,
   fprintf(outf1,"#  i        x           xp            z           zp           dp          ctau\n#\n");
   
 
-  pos = deb; /* starting position in the ring */
+  pos = deb; /* starting position or elment index in the ring */
 
   /***************************************************************/
   fprintf(stdout,"Computing initial conditions ... \n");
@@ -3154,17 +3387,17 @@ void MomentumAcceptance(long deb, long fin, double ep_min, double ep_max,
     // Dynamical allocation 0 to nstepp -1
     tabz0[i-1L]  = (double *)malloc((fin+1L)*sizeof(double));
     tabpz0[i-1L] = (double *)malloc((fin+1L)*sizeof(double));
-    if (tabz0[i-1L] == NULL || tabpz0[i-1L] == NULL){
-      fprintf(stdout,"2 out of memory \n"); return;
+    if (tabz0[i-1L] == NULL || tabpz0[i-1L] == NULL)
+    {
+      fprintf(stdout,"2 out of memory \n"); 
+      return;
     }
 
     // compute dP
-    if (nstepp != 1L) {
+    if (nstepp != 1L) 
       dP = ep_max - (nstepp - i)*(ep_max - ep_min)/(nstepp - 1L);
-    }
-    else {
+    else 
       dP = ep_max;
-    }
 
     // find and store closed orbit for dP energy offset
     set_vectorcod(codvector, dP);
@@ -3181,26 +3414,34 @@ void MomentumAcceptance(long deb, long fin, double ep_min, double ep_max,
           dP,x0[0],x0[1],x0[2],x0[3],x0[4],x0[5]);
     // Store vertical initial conditions
     // case where deb is not element 1
-    if (deb > 1L){
+    if (deb > 1L)
+    {
        Cell_Pass(1L, deb - 1L, x0, lastpos); // track from 1 to deb-1L element
        j = deb -1L;
-       if (lastpos != j){ // look if stable
+       
+       if (lastpos != j)
+       { // look if stable
          tabz0 [i- 1L][j] = 1.0;
          tabpz0[i- 1L][j] = 1.0;
        }
-       else{ // stable case
+       else
+       { // stable case
          tabz0 [i - 1L][j] = x0[2] - codvector[deb-1L][2];
          tabpz0[i - 1L][j] = x0[3] - codvector[deb-1L][3];
        }
     }
-    else { // case where deb is element 1
+    else 
+    { // case where deb is element 1
       j = deb - 1L;
       tabz0 [i - 1L][j] = x0[2] - codvector[j][2];
       tabpz0[i - 1L][j] = x0[3] - codvector[j][3];
    }
 
-    for (j = deb; j < fin; j++){ // loop over elements
-      Cell_Pass(j -1L, j, x0, lastpos);
+    for (j = deb; j < fin; j++)
+    { // loop over elements
+      Cell_Pass(j, j, x0, lastpos);
+    //   Cell_Pass(j -1L, j, x0, lastpos);
+      
       if (lastpos != j){ // look if stable
         tabz0 [i - 1L][j] = 1.0;
         tabpz0[i - 1L][j] = 1.0;
@@ -3224,7 +3465,7 @@ void MomentumAcceptance(long deb, long fin, double ep_min, double ep_max,
   {
     //~ getcod(dP=0.0, lastpos);       /* determine closed orbit */
     findcod(dP=0.0);
-  getelem(pos,&Cell);
+    getelem(pos,&Cell);
     // coordinates around closed orbit which is non zero for 6D tracking
     x     = Cell.BeamPos[0];
     px    = Cell.BeamPos[1];
@@ -3238,32 +3479,37 @@ void MomentumAcceptance(long deb, long fin, double ep_min, double ep_max,
     dp1 = 0.0;
     dp2 = 0.0;
     i   = 0L;
+    
     do /* Tracking over nturn */
     {
       i++;
       dp1 = dp2;
-      if (nstepp != 1L) {
+      
+      if (nstepp != 1L) 
         dp2= ep_max - (nstepp - i)*(ep_max - ep_min)/(nstepp - 1L);
-      }
-      else {
-        dp2 = ep_max;
-      }      
+      else 
+        dp2 = ep_max;  
+      
       if (trace)  printf("i=%4ld pos=%4ld dp=%6.4g\n",i,pos,dp2);
       if (1) fprintf(stdout,"pos=%4ld z0 =% 10.5f  pz0 =% 10.5f  \n", pos, tabz0[i-1L][pos-1L], tabpz0[i-1L][pos-1L]);
+      
       Trac(x, px, tabz0[i-1L][pos], tabpz0[i-1L][pos-1L], dp2+delta , ctau0, nturn, pos, lastn, lastpos, outf1);
-    }
-    while (((lastn) == nturn) && (i != nstepp));
+    
+    }while (((lastn) == nturn) && (i != nstepp));
             
-    if ((lastn) == nturn) dp1 = dp2;
+    if ((lastn) == nturn) 
+      dp1 = dp2;
          
     getelem(lastpos,&Clost);
     getelem(pos,&Cell);
+    
     fprintf(stdout,"pos=%4ld z0 =% 10.5f  pz0 =% 10.5f  \n", pos, tabz0[i-1L][pos-1L], tabpz0[i-1L][pos-1L]);
     fprintf(stdout,"%4ld %10.5f %10.5f %10.5f %*s\n", pos,Cell.S,dp1,Clost.S,5,Clost.Elem.PName);
     fprintf(outf2,"%4ld %10.5f %10.5f %10.5f %*s\n", pos,Cell.S,dp1,Clost.S,5,Clost.Elem.PName);
+    
     pos++;
-  }
-  while(pos != fin);
+    
+  }while(pos != fin);
 
   // free memory
   for (i = 1L; i <= nstepp; i++){
@@ -3348,7 +3594,8 @@ void MomentumAcceptance(long deb, long fin, double ep_min, double ep_max,
    }
 
     for (j = deb; j < fin; j++){ // loop over elements
-      Cell_Pass(j -1L, j, x0, lastpos);
+      Cell_Pass(j, j, x0, lastpos);
+     //   Cell_Pass(j -1L, j, x0, lastpos);
       if (lastpos != j){ // look if stable
         tabz0 [i - 1L][j] = 1.0;
         tabpz0[i - 1L][j] = 1.0;
@@ -3690,7 +3937,8 @@ void TracCO(double x, double px, double y, double py, double dp, double ctau,
 		lastn, x1[0], x1[1], x1[2], x1[3], x1[4], x1[5]);
       }
 
-      Cell_Pass(pos-1L, globval.Cell_nLoc, x1, lastpos);
+    //  Cell_Pass(pos-1L, globval.Cell_nLoc, x1, lastpos);
+      Cell_Pass(pos, globval.Cell_nLoc, x1, lastpos);
       Cell_Pass(0,pos-1L, x1, lastpos);
     }
     while (((lastn) < nmax) && ((lastpos) == pos-1L));
@@ -4178,6 +4426,439 @@ void Phase3(long pos, double x,double px,double y, double py,double energy,
   fclose(outf);
 }
 
+/****************************************************************************/
+/* void Coupling_Edwards_Teng(void)
+
+   Purpose:
+
+       Compute the oneturn matrix in the uncoupled frame using
+       the coupled matrix.
+
+       Deduce the projected emittance using the invariant given
+       by GetEmittance.
+       
+       Source:
+       Parametrization of linear coupled motion in periodic system
+       by D.A. Edwards and L.C. Teng
+       PAC73                   
+
+       Let be T the oneturn matrix, I the 2x2 identity matrix
+       We search for a basis where the system is uncoupled:
+                                -1                                -1
+         ( M n )  (  Icos(phi) D sin(phi) ) ( A 0 ) ( Icos(phi) -D sin(phi) )
+      T =(     ) =(                       ) (     ) (                       )
+         ( m N )  ( -Dsin(phi)  Icos(phi) ) ( 0 B ) ( Dsin(phi)   Icos(phi) )
+               -1
+      T = R U R
+         
+                                         ( alpha1  beta1 )   
+      A = Icos(mu1) + J1sin(mu1)  w/ J1 =(               )
+                                         (-gamma1 -alpha1)
+
+                                         ( alpha2  beta2 )
+      B = Icos(mu2) + J2sin(mu2)  w/ J2 =(               )
+                                         (-gamma2 -alpha2)
+
+                   2             2     2             2  T
+      Given V = (<u >, <uu'>, <u' >, <v >, <uu'>, <v' >)                                 
+                   2             2     2             2  T
+      and   X = (<x >, <xx'>, <x' >, <z >, <zz'>, <z' >)
+
+      Then X = U2T V where U2T if constucted using the uncoupling R matrix
+      
+   Input:
+       none
+
+   Output:
+       none
+
+   Return:
+       none
+
+   Global variables:
+       globval
+
+   Specific functions:
+       getelem
+       GetEmittance
+
+   Comments:
+       22/06/03 Now works even if coupling is null
+       Should be generalized in 6D
+       17/07/03 use of M_PI instead of pi
+       22/03/04 save status cavity/radiation and restore it at the end
+
+****************************************************************************/            
+void Coupling_Edwards_Teng(void)
+{
+  int i,j;
+  bool chroma=true, trace=false;
+  bool radiationflag, cavityflag;
+  double dP      = 0.0;
+  double diffcmu = 0.0,   /* cos(mu1) - cos(mu2)*/
+         c2phi   = 0.0,   /* cos(2*phi) */
+         s2phi   = 0.0,   /* sin(2*phi) */
+         phi     = 0.0,
+         tphi    = 0.0,   /* tan(phi) */
+         cphi    = 0.0,   /* cos(phi) */
+         sphi    = 0.0;   /* sin(phi) */
+	 
+ Matrix  M, N, m, n, D, A, B, R, S;
+ Matrix  Rinv, Dinv, nm, MminusN, tS, tn, U2T, dummy, T, U, Sigma;
+ Vector V; 
+
+  
+  double W1 = 0.0, W2 = 0.0;
+  double alpha_1 = 0.0, beta1 = 0.0, gamma1 = 0.0, nu1 = 0.0, epsilon1 = 0.0;
+  double alpha_2 = 0.0, beta2 = 0.0, gamma2 = 0.0, nu2 = 0.0, epsilon2 = 0.0;
+  double alpha_3 = 0.0, beta3 = 0.0, gamma3 = 0.0, epsilon3 = 0.0;
+  
+ 
+  /* initialization to unit matrix */
+  ZeroMat(6L, M);
+  ZeroMat(6L, N);
+  ZeroMat(6L, m);
+  ZeroMat(6L, n);
+  ZeroMat(6L, D);
+  ZeroMat(6L, Dinv);
+  ZeroMat(6L, A);
+  ZeroMat(6L, B);
+  ZeroMat(6L, nm);
+  ZeroMat(6L, MminusN);
+  ZeroMat(6L, tn);
+  ZeroMat(6L, tS);
+  ZeroMat(6L, S);
+  ZeroMat(6L, dummy);
+  UnitMat(6L, U2T);
+  UnitMat(6L, R);
+  UnitMat(6L, Rinv);
+
+  /* Build up symplectic S matrix */
+  S[0][1] = 1.0; S[1][0] = -1.0;
+
+  /* Compute invariants */
+  GetEmittance(ElemIndex("cav"), true);
+ 
+ 
+  /* Set everything to 4D integrator */
+  radiationflag        = globval.radiation;
+  cavityflag           = globval.Cavity_on;  
+  globval.MatMeth      = false;    /* matrix method */
+  globval.Cavity_on    = false;    /* Cavity on/off */
+  globval.radiation    = false;    /* radiation on/off */
+  globval.emittance    = false;    /* emittance  on/off */
+  globval.pathlength   = false;    /* Path lengthening computation */
+  
+  /* Compute Oneturn matrix and store it into globval.OneTurnMat*/
+  Ring_GetTwiss(chroma=false, dP=0e0);
+//  printglob();
+
+  /* Copy the oneturn matrix into the Edwards and Teng Form */
+  /*      
+      T = ( M n )
+          ( m N )
+   */
+
+
+  /* Compute and get Twiss parameters */
+  for (i = 0; i <= 1; i ++)
+  {
+    for (j = 0; j <= 1; j ++)
+    {
+        M[i][j] = globval.OneTurnMat[i][j];
+        N[i][j] = globval.OneTurnMat[i+2][j+2];
+        m[i][j] = globval.OneTurnMat[i+2][j];
+        n[i][j] = globval.OneTurnMat[i][j+2];
+    }
+  }
+//  fprintf(stdout,"M "); prtmat(2L,M);
+//  fprintf(stdout,"N ");prtmat(2L,N);
+
+  CopyMat(2L, M, MminusN);
+  SubMat(2L,N,MminusN);
+//  fprintf(stdout,"M-N ");  prtmat(2L,MminusN);
+  CopyMat(2L, m, nm);
+  MulLMat(2l, n, nm);
+
+  /*                                                       -1/2
+                         1        (     2 det(m) + Tr(nm) )
+  cos(mu1) - cos(mu2) =  -Tr(M-N) ( 1 + ----------------- )
+                         2        (      (0.5Tr(M-N))**2  )
+  */
+  diffcmu = 0.5*TrMat(2L,MminusN)*sqrt(1.0 + (2.0*DetMat(2L,m) + TrMat(2L,nm))/
+                                      (0.25*TrMat(2L,MminusN)*TrMat(2L,MminusN)));
+  /* cos(2phi) */
+  c2phi   = 0.5*TrMat(2L,MminusN)/diffcmu;
+
+  /* sin(2phi) */
+  s2phi   = sqrt(1.0-c2phi*c2phi);
+
+  phi     = 0.5*atan(s2phi/c2phi);
+
+  /* tan(phi), cos(phi), sin(phi) */
+  tphi    = tan(phi);
+  cphi    = cos(phi);
+  sphi    = sin(phi);
+
+  /* Compute D matrix */
+  /*                      ~~
+   *                 m + SnS
+   *  D = - ----------------------------
+   *         (cos(mu1)-cos(mu2)) sin(2phi)
+   */
+  if (fabs(phi) > 1e-12)
+  {    /* D is  defined and D is inversible ortherwise set to matrix null */
+    CopyMat(2L, n, tn);
+    TpMat(2L,tn);
+    CopyMat(2L, S, tS);
+    TpMat(2l,tS);
+    CopyMat(2L, tS, dummy);
+
+    MulLMat(2l, tn, dummy);
+    MulLMat(2l, S, dummy);
+    AddMat(2L, m, dummy);
+    MulcMat(2L, -1.0/diffcmu/s2phi ,dummy);
+    CopyMat(2L, dummy, D);
+
+    if (TrMat(4L,D) < 0.0)
+    { /* Trace of D has to remain positive */
+      phi = - phi;
+      MulcMat(2L, -1.0 ,D);
+      tphi = -tphi;
+      sphi = -sphi;
+    }
+  
+    /*  Compute A matrix   */
+    /*          -1         */
+    /* A = M - D mtan(phi) */
+     CopyMat(2L, D, Dinv);
+     if(!InvMat(2L, Dinv)) fprintf(stdout,"Matrix D is singular\n");
+  //  fprintf(stdout,"Dinv matrix "); prtmat(4L, Dinv);
+
+    CopyMat(2L, m, dummy);
+  //  fprintf(stdout,"m matrix "); prtmat(4L, dummy);
+    MulcMat(2L, -tphi ,dummy);
+  //  fprintf(stdout,"-tphim matrix "); prtmat(4L, dummy);
+    MulLMat(2l, Dinv, dummy);
+  //  fprintf(stdout,"-tphi Dinv m matrix "); prtmat(4L, dummy);
+    AddMat(2L,M,dummy);
+    CopyMat(2L, dummy, A);
+
+    /* Compute B matrix */
+    /* B = N +Dntan(phi) */
+    CopyMat(2L, n, dummy);
+    MulcMat(2L, tphi ,dummy);
+    MulLMat(2l, D, dummy);
+    AddMat(2L,N,dummy);
+    CopyMat(2L, dummy, B);
+
+    /* Build up the R matrix */
+    /*                    -1
+     *      (  Icos(phi) D sin(phi) )
+     *   T =(                       )
+     *      ( -Dsin(phi)  Icos(phi) )
+     */
+    MulcMat(4L, cphi ,R);
+    CopyMat(2L, D, dummy);
+    MulcMat(4L, -sphi , dummy);
+    for (i = 0; i <= 1; i ++)
+      for (j = 0; j <= 1; j ++)
+        R[i+2][j] = dummy[i][j];
+    CopyMat(2L, Dinv, dummy);
+    MulcMat(4L, sphi , dummy);
+    for (i = 0; i <= 1; i ++)
+      for (j = 0; j <= 1; j ++)
+        R[i][j+2] = dummy[i][j];
+
+    CopyMat(4L, R, Rinv);
+    if(!InvMat(4L, Rinv)) fprintf(stdout,"Matrix R is singular\n");  
+
+    /* Build up uncoupled matrix */
+    UnitMat(6L, U);
+    CopyMat(2L, A, U);
+    for (i = 0; i <= 1; i ++)
+      for (j = 0; j <= 1; j ++)
+        U[i+2][j+2] = B[i][j];
+    if (trace) {fprintf(stdout,"Uncoupled matrix "); prtmat(4L, U);}
+
+    CopyMat(4L, Rinv, T);
+    MulLMat(4L, U, T);
+    MulLMat(4L, R, T);
+    /* for checking, back to T */
+    if (trace) {fprintf(stdout,"Coupled matrix "); prtmat(4L, T);}
+
+
+    /* Build up transformation matrix for sigma terms from uncoupled to coupled frame */
+    /* R is the decoupling matrix computed from Edwards' and Teng's decomposition */
+    /* From R. Nagaoka's notes */
+    U2T[0][0] = R[0][0]*R[0][0];
+    U2T[0][1] = 2.0*R[0][0]*R[0][1];
+    U2T[0][2] = R[0][1]*R[0][1];
+    U2T[0][3] = R[0][2]*R[0][2];
+    U2T[0][4] = 2.0*R[0][1]*R[0][3];
+    U2T[0][5] = R[0][3]*R[0][3];
+
+    U2T[1][0] = R[0][0]*R[1][0];
+    U2T[1][1] = R[0][0]*R[1][1] + R[0][1]*R[1][0];
+    U2T[1][2] = R[0][1]*R[1][1];
+    U2T[1][3] = R[0][2]*R[1][2];
+    U2T[1][4] = R[0][1]*R[1][3] + R[0][3]*R[1][2];
+    U2T[1][5] = R[0][3]*R[1][3];
+
+    U2T[2][0] = R[1][0]*R[1][0];
+    U2T[2][1] = 2.0*R[1][0]*R[1][1];
+    U2T[2][2] = R[1][1]*R[1][1];
+    U2T[2][3] = R[1][2]*R[1][2];
+    U2T[2][4] = 2.0*R[1][2]*R[1][3];
+    U2T[2][5] = R[1][3]*R[1][3];
+
+    U2T[3][0] = R[2][0]*R[2][0];
+    U2T[3][1] = 2.0*R[2][0]*R[2][1];
+    U2T[3][2] = R[2][1]*R[2][1];
+    U2T[3][3] = R[2][2]*R[2][2];
+    U2T[3][4] = 2.0*R[2][2]*R[2][3];
+    U2T[3][5] = R[2][3]*R[2][3];
+
+    U2T[4][0] = R[2][0]*R[3][0];
+    U2T[4][1] = R[2][0]*R[3][1] + R[2][1]*R[3][0];
+    U2T[4][2] = R[2][1]*R[3][1];
+    U2T[4][3] = R[2][2]*R[3][2];
+    U2T[4][4] = R[2][2]*R[3][3] + R[2][3]*R[3][2];
+    U2T[4][5] = R[2][3]*R[3][3];
+
+    U2T[5][0] = R[3][0]*R[3][0];
+    U2T[5][1] = 2.0*R[3][0]*R[3][1];
+    U2T[5][2] = R[3][1]*R[3][1];
+    U2T[5][3] = R[3][2]*R[3][2];
+    U2T[5][4] = 2.0*R[3][2]*R[3][3];
+    U2T[5][5] = R[3][3]*R[3][3];
+
+    if (trace) {fprintf(stdout,"R "); prtmat(4L,R);}
+    if (trace) {fprintf(stdout,"U2T"); prtmat(6L, U2T);}
+
+  }
+  else { /* no coupling */
+    fprintf(stdout,"\nThere is no coupling ...\n");
+    CopyMat(2L, M, A);
+    CopyMat(2L, N, B);        
+  }
+  
+//  fprintf(stdout,"Sigma "); prtmat(6L, globval.ElemMat[0]);
+
+//  V[0] = globval.ElemMat[1][0][0];
+//  V[1] = globval.ElemMat[1][0][1];
+//  V[2] = globval.ElemMat[1][1][1];
+//  V[3] = globval.ElemMat[1][2][2];
+//  V[4] = globval.ElemMat[1][2][3];
+//  V[5] = globval.ElemMat[1][3][3];
+
+  /* Compute Twiss parameter in the uncoupled frame */
+  /* Mode 1*/
+  nu1    = globval.TotalTune[0];
+  alpha_1 = (A[0][0]-A[1][1])/sin(2.0*M_PI*nu1);
+  beta1  =  A[0][1]/sin(2.0*M_PI*nu1);
+  gamma1 = -A[1][0]/sin(2.0*M_PI*nu1);
+  
+  /* Mode 2*/
+  nu2    = globval.TotalTune[1];
+  alpha_2 = (B[0][0]-B[1][1])/sin(2.0*M_PI*nu2);
+  beta2  =  B[0][1]/sin(2.0*M_PI*nu2);
+  gamma2 = -B[1][0]/sin(2.0*M_PI*nu2);
+
+  /* Build up sigma matrix in uncoupled frame */
+  ZeroMat(6L,Sigma);
+  /* Mode 1 */
+  epsilon1    =  globval.eps[0];
+  Sigma[0][0] =  beta1*epsilon1;
+  Sigma[1][1] =  gamma1*epsilon1;
+  Sigma[0][1] = -alpha_1*epsilon1;
+  Sigma[1][0] =  Sigma[0][1];
+
+  /* Mode 2 */
+  epsilon2    =  globval.eps[1];
+  Sigma[2][2] =  beta2*epsilon2;
+  Sigma[3][3] =  gamma2*epsilon2;
+  Sigma[2][3] = -alpha_2*epsilon2;
+  Sigma[3][2] =  Sigma[2][3];
+
+  /* Mode 3 */
+  epsilon3    =  globval.eps[2];
+  Sigma[4][4] =  beta3*epsilon3;
+  Sigma[5][5] =  gamma3*epsilon3;
+  Sigma[4][5] = -alpha_3*epsilon3;
+  Sigma[5][4] =  Sigma[4][5];
+  
+//  fprintf(stdout,"Uncoupled sigma  "); prtmat(4L, Sigma);
+
+  V[0] = Sigma[0][0];
+  V[1] = Sigma[0][1];
+  V[2] = Sigma[1][1];
+  V[3] = Sigma[2][2];
+  V[4] = Sigma[2][3];
+  V[5] = Sigma[3][3];
+
+  if (!trace)
+  {    
+    fprintf(stdout,"**************************************\n");
+    fprintf(stdout,"nu1    = % 10.6f beta1 = % 10.6f\n",globval.TotalTune[0],beta1);
+    fprintf(stdout,"alpha_1 = % 10.6f gamma1= % 10.6f\n",alpha_1,gamma1);
+    fprintf(stdout,"nu2    = % 10.6f beta2 = % 10.6f\n",globval.TotalTune[1],beta2);
+    fprintf(stdout,"alpha_2 = % 10.6f gamma2= % 10.6f\n",alpha_2,gamma2);
+    fprintf(stdout,"**************************************\n");
+  }
+  
+  /* Build up invariant: should be the same as invariant given by globval.eps*/
+  W1 = sqrt(V[0]*V[2] - V[1]*V[1]);
+  W2 = sqrt(V[3]*V[5] - V[4]*V[4]);
+
+  /*** Print results */
+  if (!trace)
+  {
+   fprintf(stdout,"Coupling using Edwards' and Teng's formalism\n");
+   fprintf(stdout,"cos(mu1)-cos(mu2) = % 10.6f cos(2*phi) = % 10.6f sin(2*phi) = % 10.6f\n",
+           diffcmu,c2phi,s2phi);
+   fprintf(stdout,"phi = % 10.6f \n", 0.5*atan(s2phi/c2phi));
+   fprintf(stdout,"Invariant in local coordinates:  W1 = % 10.6e, W2 = % 10.6e, W2/W1 = %10.6e\n",
+           W1, W2, W2/W1);
+  }
+  
+  if (trace){ 
+  fprintf(stdout,"Symplectic matrix D whose derterminant is % 10.6f ", DetMat(2L,D));
+  prtmat(2L,D);
+  fprintf(stdout,"Symplectic matrix A whose derterminant is % 10.6f ", DetMat(2L,A));
+  prtmat(2L,A);
+  fprintf(stdout,"Symplectic matrix B whose derterminant is % 10.6f ", DetMat(2L,B));
+  prtmat(2L,B);
+  fprintf(stdout,"Symplectic matrix R whose derterminant is % 10.6f ", DetMat(4L,R));
+  prtmat(4L,R);
+  }
+
+  /* Transform the sigma matrix from uncoupled frame to coupled frame */
+//  PrintVec(6L, V);
+  LinTrans(6L,U2T,V);
+//  PrintVec(6L, V);
+
+  /* Build up projected emittances */
+  W1 = sqrt(V[0]*V[2] - V[1]*V[1]);
+  W2 = sqrt(V[3]*V[5] - V[4]*V[4]);
+
+  // store result and restore tracking mode 4D or 6D
+
+  globval.epsp[0]    = W1;
+  globval.epsp[1]    = W2;
+  globval.Cavity_on  = cavityflag;       /* Cavity on/off */
+  globval.radiation  = radiationflag;    /* radiation on/off */  
+ 
+
+  if (!trace)
+  {
+    fprintf(stdout,"Projected emittances:            Ex = % 10.6e, Ez = % 10.6e, Ez/Ex = %10.6e\n",
+            W1, W2, W2/W1);
+    fprintf(stdout,"**************************************\n");
+  }  
+ }
+
+
 /****************************************************************************/
 /* void PhaseLongitudinalHamiltonien(void)
 
@@ -4551,5 +5232,5 @@ void Enveloppe2(double x, double px, double y, double py, double dp, double ntur
     fprintf(outf,"%6.2f % .5e % .5e % .5e % .5e % .5e\n",
             Cell.S, Envxp[i],Envxm[i],Envzp[i],Envzm[i],dp);
   }
-
 }
+
diff --git a/tracy/tracy/src/t2cell.cc b/tracy/tracy/src/t2cell.cc
index 84ec8da5c66bd309cff4211bcd9efbc3db887319..8753980b345453ff8567900408e24e8102f41c20 100644
--- a/tracy/tracy/src/t2cell.cc
+++ b/tracy/tracy/src/t2cell.cc
@@ -13,7 +13,22 @@ long    ntransfmat;
 Matrix  transfmat[maxtransfmat];
 long    kicks[maxtransfmat][maxkicks];
 
+/**************************************************************
+template<typename T>
+inline bool CheckAmpl(const ss_vect<T> &x, const long int loc)
+
+  Purpose:
+     Check whether particle coordinate x are outside of 
+      the aperture limitation, then return the bool flag not_lost,
+      if true, then beam is lost.
+      
+  Return:
+     not_lost
+     true,    if beam is not lost
+     false,   if beam is lost
+      
 
+***************************************************************/
 template<typename T>
 inline bool CheckAmpl(const ss_vect<T> &x, const long int loc)
 {
@@ -22,17 +37,20 @@ inline bool CheckAmpl(const ss_vect<T> &x, const long int loc)
   if (globval.Aperture_on)
     not_lost = is_double<T>::cst(x[x_]) > Cell[loc].maxampl[X_][0] &&
                is_double<T>::cst(x[x_]) < Cell[loc].maxampl[X_][1] && 
-               fabs(is_double<T>::cst(x[y_])) < Cell[loc].maxampl[Y_][1];
+	       is_double<T>::cst(x[y_]) > Cell[loc].maxampl[Y_][0]&&
+               is_double<T>::cst(x[y_]) < Cell[loc].maxampl[Y_][1];
   else
     not_lost = is_double<T>::cst(x[x_]) > -max_ampl &&
                is_double<T>::cst(x[x_]) < max_ampl &&
-               fabs(is_double<T>::cst(x[y_])) < max_ampl;
+	       is_double<T>::cst(x[y_]) > -max_ampl&&
+               is_double<T>::cst(x[y_]) < max_ampl;
 
   if (!not_lost) {
     if (is_double<T>::cst(x[x_]) < Cell[loc].maxampl[X_][0] ||
         is_double<T>::cst(x[x_]) > Cell[loc].maxampl[X_][1])
       status.lossplane = 1;
-    else if (fabs(is_double<T>::cst(x[y_])) > Cell[loc].maxampl[Y_][1])
+    else if (is_double<T>::cst(x[y_]) < Cell[loc].maxampl[Y_][0] ||
+             is_double<T>::cst(x[y_]) > Cell[loc].maxampl[Y_][1])
       status.lossplane = 2;
 	    
     if (trace)
@@ -172,40 +190,109 @@ void Cell_SetdP(const double dP)
   }
 }
 
+/****************************************************************************/
+/* template<typename T>
+void Cell_Pass(const long i0, const long i1, ss_vect<T> &x, long &lastpos)
+
+   Purpose:
+       Called by Cell_GetCOD().
+       First check the initial x at position i0 is stable or not, if yes,
+       then call Elem_Pass() to compute DA map from element position i0 to i1 
+       for the particle with initial coordinates x, and check whether the particle is 
+       lost at the aperture located at this element, finally return the value of lastpos.
+
+   Input:
+       i0  starting position
+       i1 ending position of the element in the lattice
+
+   Output:
+       map     DA map
+       lastpos pointer to last position of the particle
+
+   Return:
+      lastpos   
+                =position of the last element, particle is not lost
+               !=position of the last element, particle is lost
+   Global variables:
+       globval, status
+
+   Specific functions:
+       CheckAmpl
+       Elem_Pass
+
+   Comments:
+       29/12/02 remove label replaced by return
 
+****************************************************************************/
 template<typename T>
 void Cell_Pass(const long i0, const long i1, ss_vect<T> &x, long &lastpos)
 {
   long int  i = 0;
-
+  
   if (globval.MatMeth && (x[delta_] != globval.dPparticle))
     Cell_SetdP(is_double<T>::cst(x[delta_]));
     
-  if (globval.radiation) globval.dE = 0.0;
-
-  if (globval.emittance) {
-    I2 = 0.0; I4 = 0.0; I5 = 0.0;
-
-    for (i = 0; i < DOF; i++)
-      globval.D_rad[i] = 0.0;
-  }
+  if (globval.radiation) 
+    globval.dE = 0.0;
+
+  if (globval.emittance) 
+    {
+      I2 = 0.0; 
+      I4 = 0.0; 
+      I5 = 0.0;
+      for (i = 0; i < DOF; i++)
+        globval.D_rad[i] = 0.0;
+    }
 
   if (globval.IBS)
     for (i = 0; i < DOF; i++)
       globval.D_IBS[i] = 0.0;
-
-   if (!CheckAmpl(x, i0))
+    
+  /* tracking and check particle is lost or not*/             
+  if (!CheckAmpl(x, i0))
     lastpos = i0;
-  else {
-    lastpos = i1;
-    for (i = i0; i <= i1; i++) {
-      Elem_Pass(i, x);
-      if (!CheckAmpl(x, i)) { lastpos = i; break; }
+  else 
+    {
+      lastpos = i1;
+     for (i = i0; i <= i1; i++) 
+    //    for (i = i0+1L; i <= i1; i++) 
+      {    
+	Elem_Pass(i, x);
+          if (!CheckAmpl(x, i)) 
+             {  lastpos = i;  break; }
+      }
     }
-  }
 }
 
+/**************************************************************************
+/* void Cell_Pass(const long i0, const long i1, tps &sigma, long &lastpos)
 
+   Purpose:
+       compute DA map from position i0 to i1 with sigma matrix
+       Called by Cell_GetCOD()
+
+   Input:
+       i0  starting position
+       i1 ending position
+
+   Output:
+       map     DA map
+       lastpos pointer to last position
+
+   Return:
+       none
+
+   Global variables:
+       globval, status
+
+   Specific functions:
+       CheckAmpl
+       Elem_Pass
+
+   Comments:
+       Specific for tracy 3
+
+****************************************************************************/
 void Cell_Pass(const long i0, const long i1, tps &sigma, long &lastpos)
 {
   const int  n = 9;
@@ -667,7 +754,40 @@ bool Cell_GetCOD_M(long imax, double eps, double dP, long &lastpos)
 }
 #undef n
 
+/****************************************************************************/
+/* void Cell_GetCOD(long imax, double eps, double dP, long *lastpos)
+
+   Purpose:  called by Ring_Getchrom, getcod
+              Looks for a chromatic closed orbit at dP  and computes oneturn map
+              Search at precision eps and with a maximum of imax iterations
+              using DA method
+	      If can't find the COD, then write beampos.dat, cod.out, flat_file_dbg.dat for debug; and also print information 
+	      for the particle lost.
+	      If find the cod, then get the one turn map and cell pass, and 
+	      return status.codflag
+             
+   Input:
+       imax number of iteration for cod search
+       dP   particle energy offset
+       eps  accuracy for cod search
+
+   Output:
+        
+
+   Return:
+       status.codflag   true stable, false unstable.
 
+   Global variables:
+       none
+
+   Specific functions:
+       Cell_Pass
+       
+       
+   Comments:
+       Called by getCOD, Ring_Pass
+
+****************************************************************************/
 bool Cell_getCOD(long imax, double eps, double dP, long &lastpos)
 {
   long             j, n, n_iter;
@@ -676,72 +796,108 @@ bool Cell_getCOD(long imax, double eps, double dP, long &lastpos)
   ss_vect<double>  x0, x1, dx;
   ss_vect<tps>     I, dx0, map;
   
-  n = (globval.Cavity_on)? 6 : 4; globval.dPparticle = dP;
+  if(globval.Cavity_on)
+    n = 6L;
+  else
+    n = 4L;
+    
+  globval.dPparticle = dP;
 
-  if (n == 6) {
+  if (n == 6) 
+    {
     // initial guess is zero for 3 D.O.F.
-    x0[x_] = 0.0; x0[px_] = 0.0; x0[y_] = 0.0; x0[py_] = 0.0;
-    x0[delta_] = 0.0; x0[ct_] = 0.0;
-  } else {
+      x0[x_] = 0.0; 
+      x0[px_] = 0.0; 
+      x0[y_] = 0.0; 
+      x0[py_] = 0.0;
+      x0[delta_] = 0.0; 
+      x0[ct_] = 0.0;
+    } 
+  else 
+    {
     // or eta*dP for 2 1/2 D.O.F.
-    x0[x_] = Cell[0].Eta[X_]*dP; x0[px_] = Cell[0].Etap[X_]*dP;
-    x0[y_] = Cell[0].Eta[Y_]*dP; x0[py_] = Cell[0].Etap[Y_]*dP;
-    x0[delta_] = dP; x0[ct_] = 0.0;
-  }
+      x0[x_] = Cell[0].Eta[X_]*dP; 
+      x0[px_] = Cell[0].Etap[X_]*dP;
+      x0[y_] = Cell[0].Eta[Y_]*dP; 
+      x0[py_] = Cell[0].Etap[Y_]*dP;
+      x0[delta_] = dP; 
+      x0[ct_] = 0.0;
+    }
 
   for (j = 0; j < 2*nd_tps; j++)
     jj[j] = (j < n)? 1 : 0;
 
-  if (trace) {
-    cout << endl;
-    cout << "Cell_getCOD:" << endl;
-  }
-  n_iter = 0; I.identity();
-  do {
-    n_iter++; map.identity(); map += x0;
-
-    Cell_Pass(0, globval.Cell_nLoc, map, lastpos); 
+  if (trace) 
+    {
+      cout << endl;
+      cout << "Cell_getCOD:" << endl;
+    }
+    
+  n_iter = 0; 
+  I.identity();
+  
+  do 
+    { //search for COD
+      n_iter++; 
+      map.identity(); 
+      map += x0;
 
-    if (lastpos == globval.Cell_nLoc) {
-      x1 = map.cst(); dx = x0 - x1; dx0 = PInv(map-I-x1, jj)*dx;
-      dxabs = xabs(n, dx); x0 += dx0.cst();
-    } else {
-      prt_beampos("beampos.dat");
-      prt_cod("cod.out", globval.bpm, true);
-      prtmfile("flat_file_dbg.dat");
+      Cell_Pass(0, globval.Cell_nLoc, map, lastpos); 
 
+      if (lastpos == globval.Cell_nLoc) {
+        x1 = map.cst(); 
+	dx = x0 - x1; 
+	dx0 = PInv(map-I-x1, jj)*dx;
+        dxabs = xabs(n, dx); 
+	x0 += dx0.cst();
+        } 
+      else{
+        prt_beampos("beampos.dat");
+        prt_cod("cod.out", globval.bpm, true);
+        prtmfile("flat_file_dbg.dat");
 //      prt_trace();
+        dxabs = 1e30; 
+	break;
+        }
 
-      dxabs = 1e30; break;
-    }
-
-    if (trace) {
-      cout << scientific << setprecision(1)
-	   << setw(3) << n_iter
-	   << " err = " << setw(7) << dxabs << "/" << setw(7) << eps
-	   << setprecision(5)
-	   << "  x0 =" << setw(13) << x0 << endl;
-    }
-  } while ((dxabs >= eps) && (n_iter <= imax));
+    if (trace) 
+      {
+        cout << scientific << setprecision(1)
+	     << setw(3) << n_iter << " err = " 
+	     << setw(7) << dxabs << "/" << setw(7) 
+	     << eps << setprecision(5)
+	     << "  x0 =" << setw(13) << x0 << endl;
+      }
+    } while ((dxabs >= eps) && (n_iter <= imax));
 
   status.codflag = dxabs < eps;
 
-  if (status.codflag) {
-    globval.CODvect = x0; getlinmat(6, map, globval.OneTurnMat);
-    Cell_Pass(0, globval.Cell_nLoc, x0, lastpos);
-  } else {
-    cout << "Cell_getCOD: failed to converge after " << n_iter << " iterations"
-	  << ", dP=" << setw(13) << dP
-	 << ", particle lost at element " << lastpos << endl;
-    cout << scientific << setprecision(5)
-	 << " x0=" << setw(13) << x0 << endl;
-    cout << scientific << setprecision(5)
-	 << "  x=" << setw(13) << map.cst() << endl;
-  }
+  if (status.codflag) 
+    {
+      globval.CODvect = x0; 
+      getlinmat(6, map, globval.OneTurnMat);
+      Cell_Pass(0, globval.Cell_nLoc, x0, lastpos);
+    } 
+  else 
+    {
+       cout << "Cell_getCOD: failed to converge after " << n_iter << " iterations"
+	    << ", dP=" << setw(13) << dP
+	    << ", particle lost at element " << lastpos << endl;
+       cout << scientific << setprecision(5)
+	    << " x0=" << setw(13) << x0 << endl;
+       cout << scientific << setprecision(5)
+	    << "  x=" << setw(13) << map.cst() << endl;
+    }
 
   return status.codflag;
 }
 
+/********************************************************
+bool GetCOD(long imax, double eps, double dP, long &lastpos)
+
+  Purpose:
+    
+********************************************************/
 
 bool GetCOD(long imax, double eps, double dP, long &lastpos)
 {
@@ -755,7 +911,12 @@ bool GetCOD(long imax, double eps, double dP, long &lastpos)
   return cod;
 }
 
+/************************************************
+void Cell_Init(void)
 
+  Purpose:
+    n....
+************************************************/
 void Cell_Init(void)
 {
   long         i;
@@ -772,62 +933,67 @@ void Cell_Init(void)
 
   memcpy(Cell[0].Elem.PName, first_name, sizeof(first_name));
 
-  for (i = 1; i <= globval.Elem_nFam; i++) {
-    elemfamp  = &ElemFam[i-1]; /* Get 1 of all elements stored in ElemFam
+  for (i = 1; i <= globval.Elem_nFam; i++) 
+    {
+      elemfamp  = &ElemFam[i-1]; /* Get 1 of all elements stored in ElemFam
 				  array */
-    elemp = &elemfamp->ElemF; // For switch structure: choice on element type
-    if (debug)
-      printf("Cell_Init, i:=%3ld: %*s\n", i, SymbolLength, elemp->PName);
-    switch (elemp->Pkind) {
-    case drift:
-      Drift_Init(i);
-      break;
+      elemp = &elemfamp->ElemF; // For switch structure: choice on element type
+      if (debug)
+        printf("Cell_Init, i:=%3ld: %*s\n", i, SymbolLength, elemp->PName);
+	
+      switch (elemp->Pkind) 
+      {
+        case drift:
+          Drift_Init(i);
+          break;
       
-    case Mpole:
-      Mpole_Init(i);
-      break;
+        case Mpole:
+          Mpole_Init(i);
+          break;
       
-    case Wigl:
-      Wiggler_Init(i);
-      break;
+        case Wigl:
+          Wiggler_Init(i);
+          break;
       
-    case FieldMap:
-      FieldMap_Init(i);
-      break;
+        case FieldMap:
+          FieldMap_Init(i);
+          break;
       
-    case Insertion:
-      Insertion_Init(i);
-      break;
+        case Insertion:
+          Insertion_Init(i);
+          break;
 
-    case Cavity:
-      Cav_Init(i);
-      break;
+        case Cavity:
+          Cav_Init(i);
+          break;
 
-    case marker:
-      Marker_Init(i);
-      break;
+        case marker:
+          Marker_Init(i);
+          break;
 
-    case Spreader:
-      Spreader_Init(i);
-      break;
+        case Spreader:
+          Spreader_Init(i);
+          break;
 
-    case Recombiner:
-      Recombiner_Init(i);
-      break;
+        case Recombiner:
+          Recombiner_Init(i);
+          break;
 
-    case Solenoid:
-      Solenoid_Init(i);
-      break;
+        case Solenoid:
+          Solenoid_Init(i);
+          break;
 
-    default:
-      printf("Cell_Init: undefined type\n");
-      break;
+        default:
+          printf("Cell_Init: undefined type\n");
+          break;
+      }
     }
-  }
-
-  /* Computes s-location of each element in the structure */
+  
+ /* Computes s-location of each element in the structure */
   Stotal = 0.0;
-  for (i = 0; i <= globval.Cell_nLoc; i++) {
-    Stotal += Cell[i].Elem.PL; Cell[i].S = Stotal;
-  }
+  for (i = 0; i <= globval.Cell_nLoc; i++) 
+    {
+      Stotal += Cell[i].Elem.PL; 
+      Cell[i].S = Stotal;
+    }
 }
diff --git a/tracy/tracy/src/t2elem.cc b/tracy/tracy/src/t2elem.cc
index bfe5e431ef28a2ac3fe6613d4a4c391ed2799e5e..b83be5af05d33e75b527a468a1b5e0cfd5dd36e4 100644
--- a/tracy/tracy/src/t2elem.cc
+++ b/tracy/tracy/src/t2elem.cc
@@ -74,7 +74,12 @@ void LtoG(ss_vect<T> &X, Vector2 &S, Vector2 &R,
   /* p_rot rotated */
   X[px_] += c1; X[py_] += s1;
 }
+/**********************************************************/
+/*
 
+     Purpose:
+             Get the longitudinal momentum ps
+**********************************************************/
 
 template<typename T>
 inline T get_p_s(ss_vect<T> &x)
@@ -147,7 +152,8 @@ void Drift(double L, ss_vect<T> &x)
 
 
 template<typename T>
-void Drift_Pass(CellType &Cell, ss_vect<T> &x) { Drift(Cell.Elem.PL, x); }
+void Drift_Pass(CellType &Cell, ss_vect<T> &x) 
+{ Drift(Cell.Elem.PL, x); }
 
 /****************************************************************************/
 /* zero_mat(const int n, double** A)
@@ -544,7 +550,7 @@ static double get_psi(double irho, double phi, double gap)
   /* Correction for magnet gap (lonitudinal fringe field)
 
        irho h = 1/rho [1/m]
-       phi  edge angle
+       phi  dipole edge angle
        gap  full gap between poles
        
                                     2
@@ -606,9 +612,11 @@ void thin_kick(int Order, double MB[], double L, double h_bend, double h_ref,ss_
   
    Input:
        Order  maximum non zero multipole component
-       MB     array of an and bn
-     h_bend   1/rho
-     h_ref   thick element
+       MB     array of an and bn, magnetic field components
+       L      multipole length
+     h_bend   1/rho in curvilinear coordinate,
+               0    in cartisian cooridinate.
+     h_ref   1/rho [m^-1]
        x      initial coordinates vector
 
    Output:
@@ -639,8 +647,8 @@ void thin_kick(int Order, double MB[], double L, double h_bend, double h_ref,
    {
       x0 = x;
      /* compute field with Horner's rule */
-     ByoBrho = MB[HOMmax+Order]; 
-     BxoBrho = MB[HOMmax-Order];
+     ByoBrho = MB[HOMmax+Order];    // normalized By, By/(p0/e)
+     BxoBrho = MB[HOMmax-Order];    // normalized Bx, Bx/(p0/e)
      
       for (j = Order-1; j >= 1; j--) {
        ByoBrho1 = x0[x_]*ByoBrho - x0[y_]*BxoBrho + MB[j+HOMmax];
@@ -666,7 +674,51 @@ void thin_kick(int Order, double MB[], double L, double h_bend, double h_ref,
   }
 }
 
+/****************************************************************************/
+/* template<typename T>
+static void EdgeFocus(double irho, double phi, double gap, ss_vect<T> &x)
+
+   Purpose:
+       Compute edge focusing for a dipole
+       There is no radiation coming from the edge
+       The standard formula used is :
+                    irho
+       px = px0 + ------ tan(phi) *x0
+                  1 + dP
+
+                    irho
+       pz = pz0 - ------ tan(phi - psi) *z0
+                  1 + dP
 
+       for psi definition  see its function
+
+   Input:
+       irho = inverse of curvature radius (rho = 5.36 m for SOLEIL)
+       phi  = entrance/exit angle of the dipole edge,usually half the 
+               curvature angle of a dipole
+       gap  = gap of the dipole for longitudinal fringing field (see psi)
+       x    = input particle coordinates
+
+   Output:
+       x    output particle coordinates
+
+   Return:
+       none
+
+   Global variables:
+       none
+
+   Specific functions:
+       none
+
+   Comments:
+       05/07/10 Add energy dependence part irho replaced by irho/(1.0+x[delta_])
+                 now the chromaticity attribution of the dipole edge is used
+		 by a simple 1.0+x[delta_], but not a complicated Hamiltonian
+		 expansion. 
+		 Now chromaticities in Tracy II and Tracy III are the same.
+		 Modification based on Tracy II soleil version.
+ ****************************************************************************/      
 template<typename T>
 static void EdgeFocus(double irho, double phi, double gap, ss_vect<T> &x)
 {
@@ -776,24 +828,30 @@ template<typename T>
 void quad_fringe(double b2, ss_vect<T> &x)
 {
   T  u, ps;
-
+  
    u = b2/(12.0*(1.0+x[delta_])); 
   ps = u/(1.0+x[delta_]);
   
   x[py_] /= 1.0 - 3.0*u*sqr(x[y_]);        
    x[y_] -= u*cube(x[y_]);  
   
-  if (globval.Cavity_on) x[ct_] -= ps*cube(x[y_])*x[py_]; //-y^3*py
+  if (globval.Cavity_on) 
+    x[ct_] -= ps*cube(x[y_])*x[py_]; //-y^3*py
+  
   x[px_] /= 1.0 + 3.0*u*sqr(x[x_]);  //+x^2
   
-  if (globval.Cavity_on) x[ct_] += ps*cube(x[x_])*x[px_];      //+x^3*px
+  if (globval.Cavity_on) 
+   x[ct_] += ps*cube(x[x_])*x[px_];      //+x^3*px
+  
   x[x_] += u*cube(x[x_]);    //+x^3
        u = u*3.0; ps = ps*3.0;
    x[y_] = exp(-u*sqr(x[x_]))*x[y_];   //+x^2*y
   x[py_] = exp(u*sqr(x[x_]))*x[py_];   //+x^2*py
   x[px_]+= 2.0*u*x[x_]*x[y_]*x[py_];   //+2*x*y*py
   
-  if (globval.Cavity_on) x[ct_] -= ps*sqr(x[x_])*x[y_]*x[py_];  // -3*x^2*y*py
+  if (globval.Cavity_on) 
+   x[ct_] -= ps*sqr(x[x_])*x[y_]*x[py_];  // -3*x^2*y*py
+   
    x[x_] = exp(u*sqr(x[y_]))*x[x_];       //+x*y^2
   x[px_] = exp(-u*sqr(x[y_]))*x[px_];     // -x^2*px-y^2*px
   x[py_]-= 2.0*u*x[y_]*x[x_]*x[px_];      //-2*x*y*px
@@ -2325,22 +2383,126 @@ void Insertion_Pass_M(CellType &Cell, Vector &xref, Matrix &M)
 //  LtoG(xref, Cell->dS, Cell->dT, 0.0, 0.0, 0.0);
 }
 
+/****************************************************************************/
+/* void getelem(long i, CellType *cellrec) 
+
+   Purpose:
+      assign all the information of i-th element from array Cell[i] to pointer cellrec
+
+   Input:
+       
+   Output:
+       none
+
+   Return:
+       none
+
+   Global variables:
+       none
+
+   Specific functions:
+       none
+
+   Comments:
+
+****************************************************************************/
+void getelem(long i, CellType *cellrec) 
+{ 
+  *cellrec = Cell[i]; 
+}
+/****************************************************************************/
+/* void putelem(long i, CellType *cellrec) 
+
+   Purpose:
+      assign all the information of pointer cellrec to i-th element to array Cell[i]
+
+   Input:
+       
+   Output:
+       none
+
+   Return:
+       none
+
+   Global variables:
+       none
+
+   Specific functions:
+       none
+
+   Comments:
+
+****************************************************************************/
+void putelem(long i, CellType *cellrec) 
+{ 
+  Cell[i] = *cellrec; 
+}
+
+/****************************************************************************/
+/* int GetnKid(const int Fnum1)
+
+   Purpose:
+      return the kid number of the family
+
+   Input:
+       Fnum1 :  family number 
+            
+       
+   Output:
+       none
+
+   Return:
+       none
+
+   Global variables:
+       none
+
+   Specific functions:
+       none
+
+   Comments:
+
+****************************************************************************/
+int GetnKid(const int Fnum1) 
+{ 
+  return (ElemFam[Fnum1-1].nKid); 
+}
+
 
-void getelem(long i, CellType *cellrec) { *cellrec = Cell[i]; }
+/****************************************************************************/
+/* long Elem_GetPos(const int Fnum1, const int Knum1)
 
-void putelem(long i, CellType *cellrec) { Cell[i] = *cellrec; }
+   Purpose:
+      get the element index in the lattice 
+      
 
+   Input:
+       Fnum1 :  family number of the element
+       Knum1 :  kid number of the element in Fnum1       
+       
+   Output:
+       none
+
+   Return:
+       loc : element index in the lattice
+
+   Global variables:
+       none
 
-int GetnKid(const int Fnum1) { return (ElemFam[Fnum1-1].nKid); }
+   Specific functions:
+       none
 
+   Comments:
 
+****************************************************************************/
 long Elem_GetPos(const int Fnum1, const int Knum1)
 {
   long int  loc;
 
   if (ElemFam[Fnum1-1].nKid != 0)
     loc = ElemFam[Fnum1-1].KidList[Knum1-1];
-  else {
+  else 
+  {
     loc = -1;
     printf("Elem_GetPos: there are no kids in family %d (%s)\n",
 	   Fnum1, ElemFam[Fnum1-1].ElemF.PName);
diff --git a/tracy/tracy/src/t2lat.cc b/tracy/tracy/src/t2lat.cc
index 13f6d2a477f0e63c3424f68b515dd53270f0d932..542c9100b1ed37cb8bd6c867036644f86e51ba27 100644
--- a/tracy/tracy/src/t2lat.cc
+++ b/tracy/tracy/src/t2lat.cc
@@ -4210,11 +4210,10 @@ void PrintResult(struct LOC_Lattice_Read *LINK)
 /* long ElemIndex(const char *name)
 
    Purpose:
-       return element family index.
+       return family index of the element .
        Note: in the PASCAL version the element family index could be
        comfortably accessed using the element name.
        This is no longer possible because we gave up on the interpretive PASCAL-S.
-
    Input:
        name Family name
 
@@ -4231,7 +4230,7 @@ void PrintResult(struct LOC_Lattice_Read *LINK)
        none
 
    Comments:
-       
+       modify strcmp t strncmp, now can read parts of the element name to get the element index.
 ****************************************************************************/
 
 long ElemIndex(const char *name)
@@ -4254,19 +4253,21 @@ long ElemIndex(const char *name)
     exit_(1);
   }
 
-  for (i = 1; i <= globval.Elem_nFam; i++) {
+  for (i = 1; i <= globval.Elem_nFam; i++) 
+  {
     n = 0;
-    while ((ElemFam[i-1].ElemF.PName[n] != ' ') && 
-	   (ElemFam[i-1].ElemF.PName[n] != '\0')) {
-      name2[n] = ElemFam[i-1].ElemF.PName[n]; n++;
+    while ((ElemFam[i-1].ElemF.PName[n] != ' ') && (ElemFam[i-1].ElemF.PName[n] != '\0')) 
+    {
+      name2[n] = ElemFam[i-1].ElemF.PName[n]; 
+      n++;
     }
     name2[n] = '\0';
 
     if (prt)
       printf("%d %d %s |%s|%s|\n",
-	     n, strcmp(name2, name1) == 0, name1, ElemFam[i-1].ElemF.PName,
-	     name2);
-    if (strcmp(name2, name1) == 0) return i;
+	     n, strcmp(name2, name1) == 0, name1, ElemFam[i-1].ElemF.PName, name2);
+    if (strncmp(name2, name1,strlen(name1)) == 0) 
+      return i;
   }
 
   printf("ElemIndex: undefined element %s\n", name);
diff --git a/tracy/tracy/src/t2ring.cc b/tracy/tracy/src/t2ring.cc
index 5267f0a032bb6cf71a55147326ebcbc1ac2a998e..cad42744f59df25863bcb8d0d829d8af2ae049b0 100644
--- a/tracy/tracy/src/t2ring.cc
+++ b/tracy/tracy/src/t2ring.cc
@@ -79,7 +79,7 @@ void GetNu(Vector2 &nu, Matrix &M)
 
    Purpose: called by Ring_Twiss_M and Ring_Twiss
 
-      Get Twiss parameters from the transfer matrix M
+      Get Twiss parameters from the transfer matrix M 
 
                              [Nx   ]
                          M = [     ]
@@ -104,7 +104,10 @@ void GetNu(Vector2 &nu, Matrix &M)
        nu tune vector
 
    Return:
-       none
+       global.stable         
+       global value to check the linear matrix is stable or not,
+       true  stable, 
+       false unstable
 
    Global variables:
        none
@@ -623,6 +626,9 @@ void Ring_Twiss_M(bool chroma, double dP)
 
    Purpose:  called by Ring_GetTwiss
        Computes twiss parameters for an energy offset dP using da method
+           First call GetCOD() to check whether can find a COD or not, if yes,
+           check the linear one turn matrix is stable or not, then put the linear
+	   one turn map
 
    Input:
        chroma if true computes chromaticities as well
@@ -642,7 +648,7 @@ void Ring_Twiss_M(bool chroma, double dP)
        none
 
    Comments:
-       11/05/03 in Cell_Twiss start from 1
+       
 
 ****************************************************************************/
 void Ring_Twiss(bool chroma, double dP)
@@ -652,13 +658,21 @@ void Ring_Twiss(bool chroma, double dP)
   Vector2       alpha={0.0, 0.0}, beta={0.0, 0.0};
   Vector2       gamma={0.0, 0.0}, nu={0.0, 0.0};
   Matrix        R;
-  ss_vect<tps>  AScr;
-
-  n = (globval.Cavity_on)? 6 : 4;
-
+  ss_vect<tps>  AScr; /* DA map*/
+
+  if (globval.Cavity_on)
+    n = 6L;  /* 6D tracking */
+  else      /* 4D tracking */
+    n = 4L;
+    
+  if(trace)
+  printf("\n %d D tracking....\n",n);
+    
+/* Gets closed orbit and computes oneturn map around it*/
   GetCOD(globval.CODimax, globval.CODeps, dP, lastpos);
 
-  if (!status.codflag) return;
+  if (!status.codflag) 
+    return;
 
   // Check if stable
   Cell_GetABGN(globval.OneTurnMat, alpha, beta, gamma, nu);
@@ -680,7 +694,8 @@ void Ring_Twiss(bool chroma, double dP)
   status.tuneflag = true;
 
   if (chroma && !globval.Cavity_on) {
-    Ring_Getchrom(dP); GetCOD(globval.CODimax, globval.CODeps, dP, lastpos);
+    Ring_Getchrom(dP); 
+    GetCOD(globval.CODimax, globval.CODeps, dP, lastpos);
   }
 }
 
@@ -691,6 +706,9 @@ void Ring_Twiss(bool chroma, double dP)
        Computes Twiss functions w/ or w/o chromaticities
        for particle of energy offset dP
        matrix or DA method
+       
+       Also get the momentum compact factor based on the definition:
+       Alphac = delta_C/delta*1/C
 
    Input:
        Chroma if true compute chromaticities and dispersion
@@ -717,10 +735,12 @@ void Ring_GetTwiss(bool chroma, double dP)
 {
 
   if (trace) printf("enter ring_gettwiss\n");
+  
   if (globval.MatMeth)  /* matrix method */
     Ring_Twiss_M(chroma, dP);
   else /* da method */
     Ring_Twiss(chroma, dP);
+    
   globval.Alphac = globval.OneTurnMat[ct_][delta_]/Cell[globval.Cell_nLoc].S;
   if (trace) printf("exit ring_gettwiss\n");
 }
diff --git a/tracy/tracy/src/tpsa_for_pm.cc b/tracy/tracy/src/tpsa_for_pm.cc
index 203535438053295ee03caa6c67b36948cb289ee6..875e8d6b644e427b449806ff93b3153ee91afba2 100644
--- a/tracy/tracy/src/tpsa_for_pm.cc
+++ b/tracy/tracy/src/tpsa_for_pm.cc
@@ -143,14 +143,21 @@ void TPSA_Ini(void)
 
 
 void TPSAEps(const double eps)
-{ daeps_(eps); eps_tps = eps; }
+{ 
+  daeps_(eps); 
+  eps_tps = eps; 
+}
 
-tps::tps(void) {
+tps::tps(void) 
+{
   char  name[11];
 
   if (!ini_tps) TPSA_Ini();
-  seq_tps++; intptr = 0; sprintf(name, "tps-%-5hu", seq_tps);
-  daall_(intptr, 1, name, no_tps, nv_tps); dacon_(intptr, 0.0);
+  seq_tps++; 
+  intptr = 0; 
+  sprintf(name, "tps-%-5hu", seq_tps);
+  daall_(intptr, 1, name, no_tps, nv_tps); 
+  dacon_(intptr, 0.0);
 }
 
 
@@ -159,8 +166,11 @@ tps::tps(const double r)
   char  name[11];
 
   if (!ini_tps) TPSA_Ini();
-  seq_tps++; intptr = 0; sprintf(name, "tps-%-5hu", seq_tps);
-  daall_(intptr, 1, name, no_tps, nv_tps); dacon_(intptr, r);
+  seq_tps++; 
+  intptr = 0; 
+  sprintf(name, "tps-%-5hu", seq_tps);
+  daall_(intptr, 1, name, no_tps, nv_tps); 
+  dacon_(intptr, r);
 }
 
 
@@ -169,7 +179,9 @@ tps::tps(const double r, const int i)
   char  name[11];
 
   if (!ini_tps) TPSA_Ini();
-  seq_tps++; intptr = 0; sprintf(name, "tps-%-5hu", seq_tps);
+  seq_tps++; 
+  intptr = 0; 
+  sprintf(name, "tps-%-5hu", seq_tps);
   daall_(intptr, 1, name, no_tps, nv_tps);
   if (i == 0)
     dacon_(intptr, r);
diff --git a/tracy/tracy/src/tpsa_lin.cc b/tracy/tracy/src/tpsa_lin.cc
index d4ac11db20b20cfb30c14eb79ffa46fde6a988dd..d8682583e9ddfeacbdcd1ef9d3f3a108fa9b78c2 100644
--- a/tracy/tracy/src/tpsa_lin.cc
+++ b/tracy/tracy/src/tpsa_lin.cc
@@ -56,31 +56,60 @@ void lieini(const int no, const int nv, const int nd2i)
     printf("lieini: max dim exceeded %d (%d)\n", nv, ss_dim);
 }
 
+/**********************************************
+void daall_(tps_buf &x, const int nd2, const char *daname,
+	   const int no, const int nv)
+	   
+  Purpose:
+     DA initialization 6-D coordinate vector x	   
 
+***********************************************/
 void daall_(tps_buf &x, const int nd2, const char *daname,
 	   const int no, const int nv)
 {
   int  j;
 
   for (j = 0; j <= ss_dim; j++)
-    x[j] = 0.0;
+     x[j] = 0.0;
 }
 
 
+/********************************************
+void dadal_(tps_buf &x, const int ss_dim)
+
+  Purpose:
+    None
+    
+*********************************************/
 void dadal_(tps_buf &x, const int ss_dim) { }
 
 
+
+/**********************************************
+void davar_(tps_buf &x, const double r, const int i)
+	   
+  Purpose:
+     DA 	   
+
+***********************************************/
 void davar_(tps_buf &x, const double r, const int i)
 {
   int  j;
 
   x[0] = r;
-  for (j = 1; j <= ss_dim; j++)
-    x[j] = 0.0;
+  for (j = 1; j <= ss_dim; j++)  x[j] = 0.0;
+  
   x[i] = 1.0;
 }
 
 
+/*****************************************
+void dacon_(tps_buf &x, const double r)
+
+ Purpose:
+   Initialized 6-D vector x
+
+*****************************************/
 void dacon_(tps_buf &x, const double r)
 {
   int  j;
diff --git a/tracy/tracy/src/tpsa_lin_pm.cc b/tracy/tracy/src/tpsa_lin_pm.cc
index c27137c56fd5e50aa48dbfaa1151f2ac4ec16428..dc00ba999d628c4bc6d802bdc01b72da456e36da 100644
--- a/tracy/tracy/src/tpsa_lin_pm.cc
+++ b/tracy/tracy/src/tpsa_lin_pm.cc
@@ -81,15 +81,30 @@ void TPSA_Ini(void)
   ini_tps = true;
 }
 
+
 void TPSAEps(const double eps)
 { daeps_(eps); eps_tps = eps; }
 
-tps::tps(void) {
+/**********************************************
+tps::tps(void)
+
+  Purpose:
+    
+    
+  Comments:
+   function inside the class tps
+**********************************************/
+tps::tps(void) 
+{
   char  name[11];
 
-  if (!ini_tps) TPSA_Ini();
-  seq_tps++; sprintf(name, "tps-%-5hu", seq_tps);
-  daall_(ltps, 1, name, no_tps, nv_tps); dacon_(ltps, 0.0);
+  if (!ini_tps) 
+  TPSA_Ini();
+  
+  seq_tps++; 
+  sprintf(name, "tps-%-5hu", seq_tps);
+  daall_(ltps, 1, name, no_tps, nv_tps); 
+  dacon_(ltps, 0.0);
 }
 
 tps::tps(const double r)
@@ -106,7 +121,8 @@ tps::tps(const double r, const int i)
   char  name[11];
 
   if (!ini_tps) TPSA_Ini();
-  seq_tps++; sprintf(name, "tps-%-5hu", seq_tps);
+  seq_tps++; 
+  sprintf(name, "tps-%-5hu", seq_tps);
   daall_(ltps, 1, name, no_tps, nv_tps);
   if (i == 0)
     dacon_(ltps, r);
diff --git a/tracy/tracy/src/tracy.cc b/tracy/tracy/src/tracy.cc
index f8d82a503c086b3b7813e8adfeece63b348e303f..8fd397023a7f590b239ab2a7a71e42fa09be93a8 100644
--- a/tracy/tracy/src/tracy.cc
+++ b/tracy/tracy/src/tracy.cc
@@ -52,6 +52,7 @@
 
 #include "max4_lib.cc"
 #include "soleilcommon.cc"
+#include "read_script.cc"
 
 // Truncated Power Series Algebra (TPSA)
 const int     nv_tps   = ss_dim, // no of variables