diff --git a/set_var.sh b/set_var.sh
index 764567bfd919df117e90f2ed27aed81f932600dd..cc0847cdb48120202f79e28d2e6b7812afe4a6d5 100644
--- a/set_var.sh
+++ b/set_var.sh
@@ -3,7 +3,7 @@
 export LIBPATH=/usr/lib/gcc/i386-redhat-linux/4.1.1
 export LD_LIBRARY_PATH=/usr/lib/gcc/i386-redhat-linux/4.1.1:/usr/local/lib:/usr/lib
 export NUM_REC=$HOME/codes/nrecipes/recipes_c-ansi
-export TRACY_LIB=$HOME/codes/tracy/TracyIII_soleil/tracy
+export TRACY_LIB=$HOME/codes/tracy/TracyIII/tracy
 export THOR_LIB=$HOME/Thor-2.0
 export LAT="$HOME/projects/in/lattice"
 
diff --git a/tracy/tools/max4.cc b/tracy/tools/max4.cc
index e8131318cb6102ed103a8325adca3dd36539f00f..a95c17be725379b0227bab75eb56a2641f1a159b 100644
--- a/tracy/tools/max4.cc
+++ b/tracy/tools/max4.cc
@@ -6,11 +6,6 @@ int no_tps = ORDER; // arbitrary TPSA order is defined locally
 
 extern bool  freq_map;
 
-// dynamic aperture
-const double delta  = 5.0e-2; // delta for off-momentum aperture
-
-
-
 
 
 //***************************************************************************************
@@ -22,7 +17,7 @@ const double delta  = 5.0e-2; // delta for off-momentum aperture
 {
 
   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
@@ -35,7 +30,6 @@ const double delta  = 5.0e-2; // delta for off-momentum aperture
   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;
@@ -51,104 +45,210 @@ const double delta  = 5.0e-2; // delta for off-momentum aperture
   //const int     n_x = 28, n_dp = 56, n_tr = 2048;
   
   double nux = 0.0 , nuz = 0.0, ksix = 0.0, ksiz =  0.0;
-  
-  
+  bool chroma;
+  double dP = 0.0;
+  long lastpos = -1L;
+  char str1[S_SIZE];
   if (true)
-    Read_Lattice(argv[1]); //sets some globval params
+    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
-
     
     
   globval.quad_fringe = true;                // include quadrupole fringe field
-  //no_sxt(); //turns off sextupoles
+  /*globval.bpm = ElemIndex("bpm");  //definition for max4 lattice, bpm                  
+  globval.hcorr = ElemIndex("ch"); 
+  globval.vcorr = ElemIndex("cv");*/   
 
   
+  //no_sxt(); //turns off sextupoles
   
-    
-  Ring_GetTwiss(true, 0e-2); printglob(); //gettwiss computes one-turn matrix arg=(w or w/o chromat, dp/p)
-  //get_matching_params_scl();
-  //get_alphac2();
-  //GetEmittance(ElemIndex("cav"), true);
+  Ring_GetTwiss(true, 0.0); 
+  printglob(); //gettwiss computes one-turn matrix arg=(w or w/o chromat, dp/p)
+  get_matching_params_scl();
+  get_alphac2(); // compute up to 3rd order mcf
+  GetEmittance(ElemIndex("cav"), true);
 
-/* print lattice file for nsrl-ii*/
-//  prt_lat("linlat.out", globval.bpm, true);  
+/* print lattice file */
+  prt_lat("linlatBNL.out", globval.bpm, All); // BNL print for all elements
+  printlatt();  /* SOLEIL print out lattice functions */
   
-  //prtmfile("flat_file.dat"); // writes flat file
-  //prt_chrom_lat(); //writes chromatic functions into chromlat.out
+  prtmfile("flat_file.dat"); // writes flat file
+  prt_chrom_lat(); //writes chromatic functions into chromlat.out
 
   
   // Using tracking to get tunes and chromaticities
-  if (true) {
-  GetTuneTrac(1026L, 0.0, &nux, &nuz);
-  fprintf(stdout,"From tracking: nux = % f nuz = % f \n",nux,nuz);
-  GetChromTrac(2L, 1026L, 1e-5, &ksix, &ksiz);
-  fprintf(stdout,"From tracking: ksix= % f ksiz= % f \n",ksix,ksiz);
+  if (false) {
+    GetTuneTrac(1026L, 0.0, &nux, &nuz);
+    fprintf(stdout,"From tracking: nux = % f nuz = % f \n",nux,nuz);
+    GetChromTrac(2L, 1026L, 1e-5, &ksix, &ksiz);
+    fprintf(stdout,"From tracking: ksix= % f ksiz= % f \n",ksix,ksiz);
   }
   
 
-  
+    // Flag factory
+  bool TuneTracFlag = true, ChromTracFlag = true;
+  bool FmapFlag = false, ExperimentFMAFlag = false, DetailedFMAFlag = false;
+  bool TuneShiftFlag = false;
+  bool ErrorCouplingFlag = false;  bool CouplingFlag = false;
+  bool MomentumAccFlag = false;
+  bool MultipoleFlag = false;
+  bool FitTuneFlag  = false; double targetnux = 18.202, targetnuz = 10.317;
+  bool FitChromFlag = false; double targetksix = 2.0, targetksiz = 2.6;
+  bool ChamberFlag = false, U20ChamberFlag = false;
+  bool GirderErrorFlag = false;
+  bool SigmaFlag = false;
+  bool PX2Flag = false;
+  bool InducedAmplitudeFlag = false;
+  bool CodeComparaisonFlag = false;
+  bool EtaFlag = false;
+
   //*************************************************************
   //=============================================================
   
-   double dP = 0.0;
-  
-  // Chamber
-  bool ChamberFlag = false; 
   
-  // tune shift with amp and energy
-  bool TuneShiftFlag = true;
-  
-  //  frequency map
-   bool FmapFlag = false, ExperimentFMAFlag = false, DetailedFMAFlag = true;     
-   
-  // momentum acceptance
-  bool MomentumAccFlag = false;
-  
-   
-//  Call soleillib.cc   
- // computes Tuneshift with amplitudes and energy
-  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);
-    	NuDx(50L,30L,516L,-0.02,-0.02,dP);
-        NuDp(31L,1026L,0.06);
-      }
+//    // Chamber factory
+//   if (ChamberFlag == false)
+//     ChamberOff(); // no chamber limitations
+//   else if (U20ChamberFlag == true)
+//     DefineChNoU20;
+  PrintCh();
+  LoadApers("Apertures.dat", 1.0, 1.0);
 
+  // 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);
   }
-   
-  // Computes FMA
+
+  // compute chromaticities by tracking (should be the same as by DA)
+  if (ChromTracFlag == true){
+    GetChromTrac(2, 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){
+    fprintf(stdout, "\n Setting Multipoles \n");
+    //Multipole_new(); /* for thick sextupoles */
+    Multipole(); /* for thin 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,false); // for experimental
-	 fmap(40,12,258,-20e-3,5e-3,0.0,true); // for experimental
+         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,false);
-	 fmap(100,50,1026,20e-3,5e-3,0.0,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,false);
-	  fmap(40,12,258,-32e-3,5e-3,0.0,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);
-	   fmap(20,10,1026,32e-3,7e-3,0.0,true);
+          fmap(200,100,1026,32e-3,7e-3,0.0,true);
       }
   }
   
-  
-  // Compute momentum acceptance
+  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(1L, 18L, 0.01, 0.05, 4L, -0.01, -0.05, 4L);
-   // MomentumAcceptance(1L, 108L, 0.01, 0.05, 100L, -0.01, -0.05, 100L);
+    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 (double 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);
+    }
+}    
+  
+  return 0;
   
   
   
@@ -227,6 +327,7 @@ const double delta  = 5.0e-2; // delta for off-momentum aperture
 
   // Call nsls-ii_lib.cc
   // tune shift with amplitude 
+  double delta = 0;
   if (false) {
     cout << endl;
     cout << "computing tune shifts" << endl;
diff --git a/tracy/tracy/inc/tracy_lib.h b/tracy/tracy/inc/tracy_lib.h
index 9d416ec1aa41562597aff82669a4bc368c78dcdc..26904358e501eb29733e4f40d3da16732565311e 100644
--- a/tracy/tracy/inc/tracy_lib.h
+++ b/tracy/tracy/inc/tracy_lib.h
@@ -100,3 +100,5 @@ extern ElemFamType ElemFam[];
 extern CellType Cell[];
 
 extern globvalrec globval;
+
+
diff --git a/tracy/tracy/src/physlib.cc b/tracy/tracy/src/physlib.cc
index e886c7dbe3bd9882cedfa1c82b83951b235d59ca..4194fbd3f37c407271fcb23ca6dcc2a8517f5d6d 100644
--- a/tracy/tracy/src/physlib.cc
+++ b/tracy/tracy/src/physlib.cc
@@ -2376,22 +2376,14 @@ void ChamberOff(void)
   newtime = GetTime();
 
   f = file_write(fic);
-  //fprintf(f, "# TRACY III synchrotron soleil -- %s -- %s \n", fic, asctime2(newtime));
-  //fprintf(f, "#    name                s      -xch     +xch     zch\n");
-  //fprintf(f, "#                               [mm]     [mm]     [mm]\n");
-  //fprintf(f, "#\n");
-  fprintf(f, "#  i  name      s   -xch(mm)   +xch(mm)  zch (mm)\n#\n");
+  fprintf(f, "# TRACY III synchrotron soleil -- %s -- %s \n", fic, asctime2(newtime));
+  fprintf(f, "#  i  name               s    -xch(mm) +xch(mm)  -zch(mm) +zch(mm)\n#\n");
   
   for (i = 0; i <= globval.Cell_nLoc; i++)
-  //  fprintf(f, "%4ld %15s  %6.2f  %7.3f  %7.3f  %7.3f\n",
- //	    i, Cell[i].Elem.PName, Cell[i].S,
-//	    Cell[i].maxampl[X_][0]*1E3, Cell[i].maxampl[X_][1]*1E3,
-//	    Cell[i].maxampl[Y_][1]*1E3);
-	    
-	    fprintf(f, "%4ld  ", i);
-	    fprintf(f, "%6.2f  %7.3f  %7.3f  %7.3f\n", Cell[i].S,
+	    fprintf(f, "%4ld  %15s %6.2f  %7.3f  %7.3f  %7.3f %7.3f\n", i, Cell[i].Elem.PName, Cell[i].S,
 	    Cell[i].maxampl[X_][0] * 1E3, 
 	    Cell[i].maxampl[X_][1] * 1E3,
+      Cell[i].maxampl[Y_][0] * 1E3, 
 	    Cell[i].maxampl[Y_][1] * 1E3);
 	    
 
diff --git a/tracy/tracy/src/soleillib.cc b/tracy/tracy/src/soleillib.cc
index 1d5787897b934c44d345ee40f41bcfac6bde7901..12a1c24bbe41b2c4f24b994be780977f3ae68452 100644
--- a/tracy/tracy/src/soleillib.cc
+++ b/tracy/tracy/src/soleillib.cc
@@ -354,6 +354,7 @@ void SetErr(void)
 }
 
 
+
 /****************************************************************************/
 /* void DefineCh(void)
 
@@ -383,93 +384,58 @@ void SetErr(void)
 void DefineCh(void)
 {
   long i;
-  long isep1 = 0, isep2 = 0, hu600 = 0,
-       isdm1 = 0, isdm2 = 0, isdac1 = 0, isdac2 = 0;
-
-/*  const long isep1 = 3, isep2 = 6, hu600=8,
-             isdm1 = 69, isdm2 = 98,
-             isdac1 = 131, isdac2 = 140;
-*/
-
-  /* Look for indices for defining the vaccum pipe*/
-  for (i = 0; i <= globval.Cell_nLoc; i++) {
-    if (Cell[i].Elem.Pkind == marker){
-      if (strncmp(Cell[i].Elem.PName,"ssep",4) == 0){
-        if (trace) fprintf(stdout,"found %s Element number %ld \n",
-			   Cell[i].Elem.PName,i);
-        isep1 = i;
-      }
-      if (strncmp(Cell[i].Elem.PName,"esep",4) == 0) {
-        if (trace) fprintf(stdout,"found %s Element number %ld \n",
-			   Cell[i].Elem.PName,i-1);
-        isep2 = i-1;
-      }
-      if (strncmp(Cell[i].Elem.PName,"ehu600",6) == 0) {
-        if (trace) fprintf(stdout,"found %s Element number %ld \n",
-			   Cell[i].Elem.PName,i-1);
-        hu600 = i-1;
-      }
-      if (strncmp(Cell[i].Elem.PName,"ssdm",4) == 0) {
-         if (trace) fprintf(stdout,"found %s Element number %ld \n",
-			    Cell[i].Elem.PName,i);
-         isdm1 = i;
-      }
-      if (strncmp(Cell[i].Elem.PName,"esdm",4) == 0) {
-        if (trace) fprintf(stdout,"found %s Element number %ld \n",
-			   Cell[i].Elem.PName,i);
-        isdm2 = i;
-      }
-      if (strncmp(Cell[i].Elem.PName,"ssdac",5) == 0) {
-        if (trace) fprintf(stdout,"found %s Element number %ld \n",
-			   Cell[i].Elem.PName,i);
-        isdac1 = i;
-      }
-      if (strncmp(Cell[i].Elem.PName,"esdac",5) == 0) {
-        if (trace) fprintf(stdout,"found %s Element number %ld \n",
-			   Cell[i].Elem.PName,i-1);
-        isdac2 = i-1;
-      }
-    }
-  }
-  trace=0;
-
-  /* Set the vacuum chamber */
-
-  for (i = 0; i <= globval.Cell_nLoc; i++) {
-    if  ((i < isep1) || ((i > isep2) && (i < isdm1)) ||
-    ((i > isdm2) && (i < isdac1)) || (i > isdac2)) {
-      /* ch normale */
+  long isep1=-1L, isep2=-1L, hu600=-1L,
+       isdm1=-1L, isdm2=-1L, isdac1=-1L, isdac2=-1L;
+  
+//   trace=0;
+  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_][1] =   5e-3;
-    } 
-    else if ((i >= isep1) && (i <= isep2)) {
-      /* septum */
-      Cell[i].maxampl[X_][0] = -25e-3;
-      Cell[i].maxampl[X_][1] =  25e-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)) {
-      /*  minigap */
+    else if ((i>=isdac1) && (i<=isdac2))
+    { /*  minigap */
       Cell[i].maxampl[X_][0] = -35e-3;
-      Cell[i].maxampl[X_][1] =  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>isep1) && (i<=hu600)) /* debut maille en section courte PB 25-09-07 */
- if  (i <= hu600) {
-      /* HU640 */
+    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 ChamberOn(void)