diff --git a/tracy/tracy/src/nsls-ii_lib.cc b/tracy/tracy/src/nsls-ii_lib.cc
index d39b7e4c25374f3ca7dc1052781f17630efe3804..341b5ca3de9fac0c1c20a3f15e96b1422e89d4e7 100644
--- a/tracy/tracy/src/nsls-ii_lib.cc
+++ b/tracy/tracy/src/nsls-ii_lib.cc
@@ -5,7 +5,7 @@
    T. Shaftan, I. Pinayev, Y. Luo, C. Montag, B. Nash
 
 */
-/* Current revision $Revision: 1.12 $
+/* Current revision $Revision: 1.13 $
  On branch $Name: not supported by cvs2svn $
  Latest change by $Author: zhang $
 */
@@ -17,8 +17,8 @@ bool    DA_bare       = false,  // dynamic aperture
         tune_shift    = false; // to calculate tune shift with amplitude and energy
 int     n_aper        =  15, // no of dynamical aperture points
         n_track       = 512, // no of turns for tracking
-        n_orbit       = 3,
-        n_scale       = 1;
+        n_orbit       = 3,  //number of integration for orbit correction
+        n_scale       = 1; //number to scale the random orbit error
 
 bool    bba           = false;   // beam based alignment
 int     n_lin         =  3,
@@ -27,14 +27,14 @@ int     n_lin         =  3,
         HCM_per_scell = 12,
         VCM_per_scell = 12;
 double  kick          = 0.01e-3; // 0.01 mrad kick for trims
-int     n_stat        = 1;       // number of statistics
+int     n_stat        = 1;       // number of statistics;when set the random error;
 
 double  VDweight      = 1e3,     // weight for vertical dispersion
         HVweight      = 1e0,     // weight for coupling Htrim vertical BPM
         VHweight      = 1e0;     // weight for coupling Vtrim horizontal BPM
 
-// Parameters for dynamic aperture
-double  delta_DA_  = 5.0e-2;
+
+double  delta_DA_  = 5.0e-2; // Parameters for dynamic aperture
 
 // Parameters for frequency map
 // Note NTURN is set to 10000 (2*NTURN for diffusion)) in "naffutils.h".
@@ -45,7 +45,7 @@ double  x_max_FMA = 20e-3, y_max_FMA = 6e-3, delta_FMA = 3e-2;
 const int  max_corr = 100;
 const int  max_bpm  = 200;
 
-int     h_corr[max_corr], v_corr[max_corr], bpm_loc[max_bpm];
+int     h_corr[max_corr], v_corr[max_corr], bpm_loc[max_bpm]; //non-global varible
 
 int     Fnum_Cart, n_iter_Cart;
 
@@ -176,7 +176,12 @@ void file_wr(ofstream &outf, const char file_name[])
 
 
 // C
+/***********************************************
+FILE* file_read(const char file_name[])
 
+   Purpose:
+      Open a file, and return the file ID
+************************************************/
 FILE* file_read(const char file_name[])
 {
   FILE      *fp;
@@ -739,10 +744,11 @@ void prt_chrom_lat(void)
 /* void prt_cod(const char *file_name, const int Fnum, const bool all)
 
    Purpose:
-       print the close orbit 
+       print the location, twiss parameters, displacement errors and close orbit 
+       at the the location of Fnum or all lattice elements
 
    Input:
-      file_name    file to save close orbit
+      file_name    file to save the data
                     code in the file:
 		      0   drfit
 		      0.5  dipole
@@ -750,8 +756,9 @@ void prt_chrom_lat(void)
 		      1     focusing quadrupole
 		      1.5  sextupole
 		      2.0  bpm 
-      Fnum        family index of BPM
-      all         print the cod at all elements
+      Fnum        family index
+      all         true, print the cod at all elements
+                  false, print the cod at the family elements
    Output:
        none
 
@@ -818,11 +825,11 @@ void prt_cod(const char *file_name, const int Fnum, const bool all)
 	      " %6.3f %6.3f %6.3f %6.3f %6.3f %6.3f\n",
 	      i, SymbolLength, Cell[i].Elem.PName, Cell[i].S, code,
 	      Cell[i].Beta[X_], Cell[i].Nu[X_],
-	      Cell[i].Beta[Y_], Cell[i].Nu[Y_],
+	      Cell[i].Beta[Y_], Cell[i].Nu[Y_],  
 	      1e3*Cell[i].BeamPos[x_], 1e3*Cell[i].BeamPos[y_],
-	      1e3*Cell[i].dS[X_], 1e3*Cell[i].dS[Y_],
-	      -1e3*Elem_GetKval(Cell[i].Fnum, Cell[i].Knum, Dip),  //K value of the dipole component
-	      1e3*Elem_GetKval(Cell[i].Fnum, Cell[i].Knum, -Dip)); //K value of the dipole component
+	      1e3*Cell[i].dS[X_], 1e3*Cell[i].dS[Y_], //displacement error
+	      -1e3*Elem_GetKval(Cell[i].Fnum, Cell[i].Knum, Dip),  //H kick angle, for correctors
+	      1e3*Elem_GetKval(Cell[i].Fnum, Cell[i].Knum, -Dip)); //kick angle, for correctors
     }
   }
   fclose(outf);
@@ -848,8 +855,14 @@ void prt_beampos(const char *file_name)
 }
 
 
-// misalignments
+/****************************************************************
+void CheckAlignTol(const char *OutputFile)
 
+  Purpose:
+    check aligment errors of individual magnets on giders
+    the dT and roll angle are all printed out
+****************************************************************/
+// misalignments
 void CheckAlignTol(const char *OutputFile)
   // check aligment errors of individual magnets on giders
   // the dT and roll angle are all printed out
@@ -976,7 +989,23 @@ void CheckAlignTol(const char *OutputFile)
   fout.close();
 } 
 
-
+/*********************************************************************
+void misalign_rms_elem(const int Fnum, const int Knum,
+		       const double dx_rms, const double dy_rms,
+		       const double dr_rms, const bool new_rnd)
+		       
+  Purpose:
+     Set random misalignment to the element with Fnum and Knum 
+  
+  Input:   
+     Fnum          family number
+     Knum          kid number
+     dx_rms         rms value of the error in x
+     dy_rms         rms value of the error in y
+     dr_rms         rms value of the error in rotation around s
+     new_rnd        flag to turn on/off using the new random seed
+     
+**********************************************************************/
 void misalign_rms_elem(const int Fnum, const int Knum,
 		       const double dx_rms, const double dy_rms,
 		       const double dr_rms, const bool new_rnd)
@@ -1002,6 +1031,23 @@ void misalign_rms_elem(const int Fnum, const int Knum,
   Mpole_SetdS(Fnum, Knum); Mpole_SetdT(Fnum, Knum);
 }
 
+
+/*********************************************************************
+void misalign_sys_elem(const int Fnum, const int Knum,
+		       const double dx_sys, const double dy_sys,
+		       const double dr_sys)
+		       
+  Purpose:
+     Set systematic misalignment to the elements in "type" 
+  
+  Input:   
+     Fnum          family number
+     Knum          kid number	       
+     dx_sys         sys value of the error in x
+     dy_sys         sys value of the error in y
+     dr_sys         sys value of the error in rotation around s
+     
+**********************************************************************/
 void misalign_sys_elem(const int Fnum, const int Knum,
 		       const double dx_sys, const double dy_sys,
 		       const double dr_sys)
@@ -1016,6 +1062,21 @@ void misalign_sys_elem(const int Fnum, const int Knum,
   Mpole_SetdS(Fnum, Knum); Mpole_SetdT(Fnum, Knum);
 }
 
+/*********************************************************************
+void misalign_rms_fam(const int Fnum,
+		      const double dx_rms, const double dy_rms,
+		      const double dr_rms, const bool new_rnd)
+		       
+  Purpose:
+     Set systematic misalignment to the elements on the girders
+  
+  Input:   
+     Fnum           family number		       
+     dx_rms         rms value of the error in x
+     dy_rms         rms value of the error in y
+     dr_rms         rms value of the error in rotation around s
+     new_rnd        flag to generate random number
+**********************************************************************/
 void misalign_rms_fam(const int Fnum,
 		      const double dx_rms, const double dy_rms,
 		      const double dr_rms, const bool new_rnd)
@@ -1036,6 +1097,29 @@ void misalign_sys_fam(const int Fnum,
     misalign_sys_elem(Fnum, i, dx_sys, dy_sys, dr_sys);
 }
 
+/*********************************************************************
+void misalign_rms_type(const int type,
+		       const double dx_rms, const double dy_rms,
+		       const double dr_rms, const bool new_rnd)
+		       
+  Purpose:
+     Set random misalignment to the elements in "type" 
+  
+  Input:   
+     type           element type,has the following value
+                       All
+		       dipole
+		       quad
+		       sext
+		       bpm
+		       girder
+		       
+     dx_rms         rms value of the error in x
+     dy_rms         rms value of the error in y
+     dr_rms         rms value of the error in rotation around s
+     new_rnd        flag to turn on/off using the new random seed
+     
+**********************************************************************/
 void misalign_rms_type(const int type,
 		       const double dx_rms, const double dy_rms,
 		       const double dr_rms, const bool new_rnd)
@@ -1058,6 +1142,28 @@ void misalign_rms_type(const int type,
   }
 }
 
+/*********************************************************************
+void misalign_sys_type(const int type,
+		       const double dx_sys, const double dy_sys,
+		       const double dr_sys)
+		       
+  Purpose:
+     Set systematic misalignment to the elements in "type" 
+  
+  Input:   
+     type           element type,has the following value
+                       All
+		       dipole
+		       quad
+		       sext
+		       bpm
+		       girder
+		       
+     dx_sys         sys value of the error in x
+     dy_sys         sys value of the error in y
+     dr_sys         sys value of the error in rotation around s
+     
+**********************************************************************/
 void misalign_sys_type(const int type,
 		       const double dx_sys, const double dy_sys,
 		       const double dr_sys)
@@ -1080,6 +1186,22 @@ void misalign_sys_type(const int type,
   }
 }
 
+/*********************************************************************
+void misalign_rms_girders(const int gs, const int ge,
+			  const double dx_rms, const double dy_rms,
+			  const double dr_rms, const bool new_rnd)
+		       
+  Purpose:
+     Set systematic misalignment to the elements on the girders
+  
+  Input:   
+     gs             girder start marker
+     ge             girder end marker		       
+     dx_rms         rms value of the error in x
+     dy_rms         rms value of the error in y
+     dr_rms         rms value of the error in rotation around s
+     
+**********************************************************************/
 void misalign_rms_girders(const int gs, const int ge,
 			  const double dx_rms, const double dy_rms,
 			  const double dr_rms, const bool new_rnd)
@@ -1127,7 +1249,22 @@ void misalign_rms_girders(const int gs, const int ge,
   }
 }
 
-
+/*********************************************************************
+void misalign_sys_girders(const int gs, const int ge,
+			  const double dx_sys, const double dy_sys,
+			  const double dr_sys)
+		       
+  Purpose:
+     Set systematic misalignment to the elements on the girders
+  
+  Input:   
+     gs             girder start marker
+     ge             girder end marker		       
+     dx_sys         sys value of the error in x
+     dy_sys         sys value of the error in y
+     dr_sys         sys value of the error in rotation around s
+     
+**********************************************************************/
 void misalign_sys_girders(const int gs, const int ge,
 			  const double dx_sys, const double dy_sys,
 			  const double dr_sys)
@@ -1175,7 +1312,21 @@ void misalign_sys_girders(const int gs, const int ge,
   }
 }
 
+/***************************************************************************
+void LoadAlignTol(const char *AlignFile, const bool Scale_it,
+		  const double Scale, const bool new_rnd, const int k)
 
+  Purpose:
+        Load misalignment error	from an external file, then set the error
+	to the corresponding elements.
+ 
+  Input:
+      AlignFile    file from which the misalignment error is readed  	  		  
+      Scale_it     flag to turn on/off the scale of the error
+      Scale        value to scale the error
+      new_rnd      use new random number or not
+      k   
+****************************************************************************/
 void LoadAlignTol(const char *AlignFile, const bool Scale_it,
 		  const double Scale, const bool new_rnd, const int k)
 {
@@ -1997,7 +2148,7 @@ void set_bnr_sys_elem(const int Fnum, const int Knum,
   // errors are relative to design values for (Dip, Quad, Sext, ...)
   mp->PBsys[HOMmax+n] = bnr*mp->PBpar[HOMmax+nd];
   mp->PBsys[HOMmax-n] = anr*mp->PBpar[HOMmax+nd];
-  
+  //update the total field strength PB and maximum order p_order
   Mpole_SetPB(Fnum, Knum, n); Mpole_SetPB(Fnum, Knum, -n);
 
   if (prt)
@@ -2094,7 +2245,42 @@ void set_bnr_sys_type(const int type,
     printf("Bad type argument to set_bnr_sys_type()\n");
 }
 
-
+/***********************************************************************
+void set_bnr_rms_elem(const int Fnum, const int Knum,
+		      const int n, const double bnr, const double anr,
+		      const bool new_rnd)
+			 
+   Purpose:
+       Update rms field error PBrms and random value of the rms field error PBrnd 
+       to a elment, errors are relative to design values PBpar for (Dip, Quad, 
+       Sext, ...).
+	
+	If r0 != 0, call this function
+   
+   Input:
+      Fnum           family index
+      Knum           kid index
+      n              order of the error
+      bnL            relative integrated B component for the n-th error
+      anL            relative integrated A component for the n-th error
+      new_rnd        true, generate random value for the rms field error
+                     false, NOT generate random value for the rms field error	    
+		      
+   Output:
+      None
+      
+  Return:
+      None
+      
+  Global variables
+      None
+   
+  Specific functions:
+     None    
+     
+ Comments:
+     None
+**********************************************************************/
 void set_bnr_rms_elem(const int Fnum, const int Knum,
 		      const int n, const double bnr, const double anr,
 		      const bool new_rnd)
@@ -2124,7 +2310,41 @@ void set_bnr_rms_elem(const int Fnum, const int Knum,
   Mpole_SetPB(Fnum, Knum, n); Mpole_SetPB(Fnum, Knum, -n);
 }
 
-
+/***********************************************************************
+void set_bnr_rms_fam(const int Fnum,
+		     const int n, const double bnr, const double anr,
+		     const bool new_rnd)
+			 
+   Purpose:
+       Update rms field error PBrms and random value of the rms field error PBrnd 
+       to all the kid elments in a family, errors are relative to design values PBpar 
+       for (Dip, Quad, Sext, ...).
+	
+	If r0 != 0, call this function
+   
+   Input:
+      Fnum           family index
+      n              order of the error
+      bnr            relative integrated B component for the n-th error
+      anr            relative integrated A component for the n-th error
+      new_rnd        true, generate random value for the rms field error
+                     false, NOT generate random value for the rms field error	    
+		      
+   Output:
+      None
+      
+  Return:
+      None
+      
+  Global variables
+      None
+   
+  Specific functions:
+     None    
+     
+ Comments:
+     None
+**********************************************************************/
 void set_bnr_rms_fam(const int Fnum,
 		     const int n, const double bnr, const double anr,
 		     const bool new_rnd)
@@ -2135,7 +2355,41 @@ void set_bnr_rms_fam(const int Fnum,
     set_bnr_rms_elem(Fnum, k, n, bnr, anr, new_rnd);
 }
 
-
+/***********************************************************************
+void set_bnr_rms_type(const int type,
+		      const int n, const double bnr, const double anr,
+		      const bool new_rnd)
+			 
+   Purpose:
+       Update rms field error PBrms and random value of the rms field error PBrnd 
+       to all the kid elments in a family, errors are relative to design values PBpar 
+       for (Dip, Quad, Sext, ...).
+	
+	If r0 != 0, call this function
+   
+   Input:
+      type           type of the elment,maybe 'dipole', 'quadrupole', 'sextupole', 'all'
+      n              order of the error
+      bnr            relative integrated B component for the n-th error
+      anr            relative integrated A component for the n-th error
+      new_rnd        true, generate random value for the rms field error
+                     false, NOT generate random value for the rms field error	    
+		      
+   Output:
+      None
+      
+  Return:
+      None
+      
+  Global variables
+      None
+   
+  Specific functions:
+     None    
+     
+ Comments:
+     None
+**********************************************************************/
 void set_bnr_rms_type(const int type,
 		      const int n, const double bnr, const double anr,
 		      const bool new_rnd)
@@ -2663,10 +2917,14 @@ void Align_BPMs(const int n)
   }
 }
 
-
+/* *****************************************************
+ void get_bare()
+  
+  Purpose:
+           store values of the optics function at the sextupoles 
+*/
 void get_bare()
 {
-  /* store values of the optics function at the sextupoles */
   long int  j, k;
 
   n_sext = 0;
@@ -3168,7 +3426,7 @@ void get_IDs(void)
 }
 
 /****************************************************************************/
-/* void set_ID_scl(const int Fnum, const double scl)
+/* void set_IDs(const double scl)
 
    Purpose:
        Set the scale factor for all the wigger, insertion devices, or fieldMap.        
@@ -5115,3 +5373,8 @@ void set_tune(const char file_name1[], const char file_name2[], const int n)
 
   fclose(fp_lat);
 }
+
+
+
+
+