Index: trunk/SophyaPI/PIext/piafitting.cc
===================================================================
--- trunk/SophyaPI/PIext/piafitting.cc	(revision 368)
+++ trunk/SophyaPI/PIext/piafitting.cc	(revision 379)
@@ -1,2 +1,10 @@
+//CMVBUG reste pb suivant:
+//openppf cmvwppf.ppf
+//fitw h1g g2 p:4500,45,10,900
+//delobjs h1g
+//clic sur FIT  -> OK
+//clic sur FIT 2sd fois -> core dump!!!
+//
+//
 #include <stdio.h>
 #include <stdlib.h>
@@ -28,7 +36,7 @@
 #include "pistdimgapp.h"
 
-////      ----------    Classe de fenetre de fit interactive ------------
-#define FINPARMAX  10    // Nb maxi de parametres de fit 
-
+////////////////////////////////////////////////////////////////////
+////---------- Classe de fenetre de fit interactive ------------////
+////////////////////////////////////////////////////////////////////
 class PIAFitterWind :  public PIWindow {
 public :
@@ -37,96 +45,46 @@
   virtual void  Show();
   virtual void  Process(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
-  inline  void  SetObjName(string& nom) { oname = nom; }
-          void  SetNbParms(int npar);
+  void SetState(void);
 protected :
-  PIStdImgApp* dap;
-  PIAFitter* fitter;
-  PILabel* labn;  
-  PIOptMenu* pom[2];
-  PIButton* but[2];
-  PICheckBox* ckb[2];
-  PILabel* lab[2];
-  PILabel* labp[FINPARMAX];   // Maximum FINPARMAX  parametres
-  PIText* txtp[FINPARMAX];    //        "       "      "
-  PIText* txts[FINPARMAX];    //        "       "      "  
-  string oname;
+  PIStdImgApp* mDap;
+  PIAFitter*   mFitter;
+  PILabel**    lab; int nlab;
+  PIText**     txt; int ntxt;
+  PIButton**   but; int nbut;
+  PICheckBox** ckb; int nckb;
+  bool ReFillGData;
 };
 
-///////////////////// Fit 1D et 2D //////////////////////////
-
-PIAFitterWind* fwind = NULL;
-
-/* --Methode-- */
-PIAFitter::PIAFitter(PIACmd *piac, PIStdImgApp* app)
-{
-string kw, usage;
-
-kw = "fit";
-usage = "Fitting function to DataObjects (Histo, Histo2D, Vector, ...)";
-usage += "\n Usage: fit nomobj func [Options]";
-usage += "\n [p:p1,...,pn s:s1,...,sn m:m1,...,mn M:M1,...,Mn o:... o:...]";
-piac->RegisterCommand(kw, usage, this, "Fitting");
-
-kw = "fitw";
-usage = "Interactive Window Fit";
-usage += "\n Usage: fit nomobj ";
-piac->RegisterCommand(kw, usage, this, "Fitting");
-
-// Je cree une seule copie de PIAFitterWind
-// Attention, ca ne marchera pas si on detruit this
-if (fwind == NULL)  fwind = new PIAFitterWind(app, this);
-}
-
-/* --Methode-- */
-PIAFitter::~PIAFitter()
-{
-}
-
-int PIAFitter::Execute(string& kw, vector<string>& tokens)
-{
-// Fit 1D sur objets 1D. Egalement Fit 2D sur objets 2D.
-if (kw == "fit") {
-  if (tokens.size() < 2) {
-    cout <<"Usage:fit nomobj func \n"
-         <<" [p:p1,...,pn s:s1,...,sn m:m1,...,mn M:M1,...,Mn o:... o:...]\n";
-    return(0);
-  }
-  string p=""; string s=""; string m=""; string M=""; string O="";
-  if (tokens.size()>2)
-    for(int ip=2;ip<tokens.size();ip++) {
-      if(tokens[ip].length()<=2) continue;
-      const char *c = tokens[ip].c_str();
-      if(c[1]!=':') continue;
-      if(c[0]=='p')      p=c+2;
-      else if(c[0]=='s') s=c+2;
-      else if(c[0]=='m') m=c+2;
-      else if(c[0]=='M') M=c+2;
-      else if(c[0]=='o') {O += ","; O += c+2;}
-    }
-  Fit12D(tokens[0],tokens[1],p,s,m,M,O);
-}
-else if (kw == "fitw") {
-  if (tokens.size() < 1) {
-    cout << "Usage: fitw nomobj" << endl;
-    return(0);
-    }
-  cout << " *DBG* Affichage PIAFitterWind ** " << endl;
-  fwind->SetObjName(tokens[0]);
-  fwind->Show();
- }
-
-return(0);
-}
-
-/* --Methode-- cmv 13/10/98 */
-void  PIAFitter::Fit12D(string& nom, string& func,
-                               string par,string step,string min,string max,
-                               string opt)
+////////////////////////////////////////////////////////////////////////
+//| --------------- Fit Lineaire a 1 et 2 dimensions ---------------
+//| Syntaxe:
+//| fitlin nom pnn [o:.aa,bb,cc, o;dd,ee,ff o:gg,hh,jj,kk etc...]
+//| avec:
+//| nom  : cf commentaire ordre "fit"
+//| pnn  : fit polynome degre nn avec classe Poly (lineaire) 1D ou 2D
+//| o:aa,...,bb : cf commentaires ordre "fit"
+//|
+////////////////////////////////////////////////////////////////////////
 //| --------------- Fit d'objets a 1 et 2 dimensions ---------------
+//|                 avec interface d'aide graphique
+//| Syntaxe:
+//| fitw nom func
+//|     [p:p1,p2,...,pn s:s1,s2,...,sn m:m1,m2,...,mn M:M1,mM2,...,Mn f:f1,...,fn]
+//|     [o:.aa,bb,cc, o;dd,ee,ff o:gg,hh,jj,kk etc...]
+//| cf commentaire ordre "fit"
+//|
+////////////////////////////////////////////////////////////////////////
+//| --------------- Fit d'objets a 1 et 2 dimensions ---------------
+//| Syntaxe:
+//| fit nom func
+//|     [p:p1,p2,...,pn s:s1,s2,...,sn m:m1,m2,...,mn M:M1,mM2,...,Mn f:f1,...,fn]
+//|     [o:.aa,bb,cc, o;dd,ee,ff o:gg,hh,jj,kk etc...]
+//|----- OBJET -----
 //| nom  : nom de l'objet qui peut etre:
 //|        fit-1D:  Vector,Histo1D,HProf ou GeneraFitData(1D)
 //|        fit-2D:  Matrix,Histo2D,Image<T> ou GeneraFitData(2D)
-//| func : pnn : fit polynome degre nn avec classe Poly (lineaire) 1D ou 2D
-//|      : Pnn : fit polynome degre nn avec GeneralFit (non-lineaire) 1D ou 2D
+//|
+//|----- FUNCTION -----
+//| func : pnn : fit polynome degre nn avec GeneralFit (non-lineaire) 1D ou 2D
 //|      : gnn : fit gaussienne (hauteur) + polynome de degre nn 1D
 //|      : g   : fit gaussienne (hauteur) 1D
@@ -143,19 +101,27 @@
 //|      : M   : fit Moffat+fond (expos=p6) (volume) 2D
 //|      : Mi  : fit Moffat+fond integree (expos=p6) (volume) 2D
-//| par  : p1,...,pn : valeur d'initialisation des parametres (def=0)
-//| step : s1,...,sn : valeur des steps de depart (def=1)
-//| min  : m1,...,mn : valeur des minima (def=1)
-//| max  : M1,...,Mn : valeur des maxima (def=-1) (max<=min : pas de limite)
-//| opt  : options "Eaa.b,eaa.b,f,r,caa.b,Xaa.b"
+//|
+//|----- INIT PARAMETRES ET ETAT DU FIT -----
+//| p    : p1,...,pn : valeur d'initialisation des parametres (def=0)
+//| s    : s1,...,sn : valeur des steps de depart (def=1)
+//| m    : m1,...,mn : valeur des minima (def=1)
+//| M    : M1,...,Mn : valeur des maxima (def=-1) (max<=min : pas de limite)
+//| f    : f1,...,fn : si >=1 parametre fixe sinon libre (def=0)
+//| - Remarque: si pi,si,mi ou Mi = '!' la valeur correspondante n'est pas changee
+//|
+//|----- OPTIONS -----
+//| o    : options "o:Eaa.b,eaa.b,f,r,caa.b,Xaa.b"
+//|      F : initialisation a partir des resultats et de l'etat du fit precedent
+//|          (option non prioritaire sur les definitions p:,s:,m:,M:,f:o:)
 //|      f : generation d'un Objet identique contenant la fonction fittee
 //|      r : generation d'un Objet identique contenant les residus
 //|      Xaa.b : aa.b valeur du DXi2 d'arret (def=1.e-3)
 //|      Naa : aa nombre maximum d'iterations (def=100)
-//|      la.b : niveau "a.b" de print: a=niveau de print Fit1/2D
-//|                                    b=niveau de debug GeneralFit
-//|      Ii1/i2 numeros des bins X de l'histos utilises pour le fit [i1,i2]
-//|2D    Jj1/j2 numeros des bins Y de l'histos utilises pour le fit [j1,j2]
-//|      - L'erreur est celle associee a l'objet (si elle existe),
-//|        elle est mise a 1 sinon, sauf si E... ou e... est precise:
+//|      la : niveau "a" de print: a=niveau de print Fit1/2D
+//|      da : niveau "a" de debug: a=niveau de GeneralFit
+//|      Ii1/i2 : numeros des bins X de l'histos utilises pour le fit [i1,i2]
+//|2D    Jj1/j2 : numeros des bins Y de l'histos utilises pour le fit [j1,j2]
+//|    - L'erreur est celle associee a l'objet (si elle existe),
+//|      elle est mise a 1 sinon, sauf si E... ou e... est precise:
 //|      Eaa.b : si |val|>=1 erreur = aa.b*sqrt(|val|)
 //|              si |val|<1  erreur = aa.b
@@ -168,424 +134,636 @@
 //|      x : demande de centrage: on fit x-xc au lieu de x
 //|          avec xc=abscisse du milieu de l'histogramme
-//|          Actif pour exp+poly 1D, poly 1D
-//|                pour gauss+poly 1D, xc est le centre de la gaussienne.
+//|          Actif pour  exp+poly 1D, poly 1D
+//|          Pour gauss+poly 1D, xc est le centre de la gaussienne.
 //|2D    yaa.b et y : idem "xaa.b et x" mais pour y
-{
-
+////////////////////////////////////////////////////////////////////////
+
+/* --Methode-- */
+PIAFitter::PIAFitter(PIACmd *piac, PIStdImgApp* app)
+: mApp(app), FWindFit(NULL)
+, mNObj(""), mObj(NULL), mFunc(NULL), mFName(""), mGData(NULL)
+, mNPar(0), mNVar(0),  mNBinX(0), mNBinY(0), mNData(0)
+, mPar(1), mStep(1), mMin(1), mMax(1), mFix(1)
+, mParSave(1), mStepSave(1), mMinSave(1), mMaxSave(1), mFixSave(1)
+, mFit(NULL)
+, mV(NULL), mH(NULL), mM(NULL), mH2(NULL), mIm(NULL), mG(NULL)
+{
+// Init et Set des options
+mPar=0.; mStep=1.; mMin=1.; mMax=-1.; mFix=0.;
+mParSave=0.; mStepSave=1.; mMinSave=1.; mMaxSave=-1.; mFixSave=0.;
+ResetOptions();
+mOptSave = mOpt;
+
+// enregistrement des ordres de fit
+string kw, usage;
+
+kw = "fit";
+usage = "Fitting function to DataObjects (Histo, Histo2D, Vector, ...)";
+usage += "\n Usage: fit nomobj func [Options]";
+usage += "\n [p:p1,...,pn s:s1,...,sn m:m1,...,mn M:M1,...,Mn o:... o:...]";
+piac->RegisterCommand(kw,usage,this,"Fitting");
+
+kw = "fitw";
+usage = "Fitting function to DataObjects with Interactive Window Help";
+usage += "\n Usage: fitw nomobj func [Options]";
+usage += "\n [p:p1,...,pn s:s1,...,sn m:m1,...,mn M:M1,...,Mn o:... o:...]";
+piac->RegisterCommand(kw,usage,this,"Fitting");
+
+kw = "fitlin";
+usage = "Linear Fitting of Polynoms to DataObjects";
+usage += "\n Usage: fitlin nomobj func [o:... o:...]";
+piac->RegisterCommand(kw,usage,this,"Fitting");
+}
+
+/* --Methode-- */
+PIAFitter::~PIAFitter()
+{
+if(mFit != NULL) {delete mFit; mFit=NULL;}
+if(mFunc != NULL) {delete mFunc; mFunc=NULL;}
+if(mGData != NULL) {delete mGData; mGData=NULL;}
+if(FWindFit != NULL) {delete FWindFit; FWindFit=NULL;}
+}
+
+/* --Methode-- */
+void PIAFitter::ResetDPointer(void)
+// Reset des pointeurs sur les donnees
+{
+mNObj = ""; mObj=NULL;
+mV=NULL; mH=NULL; mM=NULL; mH2=NULL; mIm=NULL; mG=NULL;
+}
+
+/* --Methode-- */
+void PIAFitter::ResetOptions(void)
+// Reset des options
+{
+mOpt.okres = mOpt.okfun = false;
+mOpt.polcx = mOpt.polcy = 0;
+mOpt.xc = mOpt.yc = 0.;
+mOpt.stc2 = 1.e-3;
+mOpt.nstep = 100;
+mOpt.err_e = mOpt.err_E = -1.;
+mOpt.lp = 1; mOpt.lpg = 0;
+mOpt.i1 = mOpt.j1 = mOpt.i2 = mOpt.j2 = -1;
+mOpt.fromlastfit = false;
+}
+
+/* --Methode-- */
+void PIAFitter::DecodeOptions(string opt)
+// Decodage des options a partir des chaines de caracteres
+{
+ResetOptions();
+if(opt.length()<=0) return;
+
+size_t p,q; string dum;
+opt = "," + opt + ",";
+
+if(strstr(opt.c_str(),",F,"))  // init options/param a partir du fit precedent
+  {mOpt = mOptSave; mOpt.fromlastfit = true;}
+if(strstr(opt.c_str(),",r,")) mOpt.okres = true;  // residus
+if(strstr(opt.c_str(),",f,")) mOpt.okfun = true;  // fonction fittee
+if(strstr(opt.c_str(),",x")) { // Demande de centrage (fit X=x-xc)
+  mOpt.polcx = 2; // Le centrage est calcule automatiquement
+  p = opt.find(",x"); q = opt.find_first_of(',',p+1);
+  dum = opt.substr(p,q-p);
+  if(dum.length()>2) {
+    sscanf(dum.c_str(),",x%lf",&mOpt.xc);
+    mOpt.polcx = 1; // Le centrage est fixe par la valeur lue
+  }
+}
+if(strstr(opt.c_str(),",y")) { // Demande de centrage (fit Y=y-yc)
+  mOpt.polcy = 2; // Le centrage est calcule automatiquement
+  p = opt.find(",y"); q = opt.find_first_of(',',p+1);
+  dum = opt.substr(p,q-p);
+  if(dum.length()>2) {
+    sscanf(dum.c_str(),",y%lf",&mOpt.yc);
+    mOpt.polcy = 1; // Le centrage est fixe par la valeur lue
+  }
+}
+if(strstr(opt.c_str(),",E")) { // Erreurs imposees a "sqrt(val)" ou "aa.b*sqrt(val)"
+  p = opt.find(",E"); q = opt.find_first_of(',',p+1);
+  dum = opt.substr(p,q-p);
+  if(dum.length()>2) sscanf(dum.c_str(),",E%lf",&mOpt.err_E);
+  if(mOpt.err_E<=0.) mOpt.err_E = 1.;  //mOpt.err_e=-1.;
+}
+if(strstr(opt.c_str(),",e")) { // Erreurs imposees a "1" ou "aa.b"
+  p = opt.find(",e"); q = opt.find_first_of(',',p+1);
+  dum = opt.substr(p,q-p);
+  if(dum.length()>2) sscanf(dum.c_str(),",e%lf",&mOpt.err_e);
+  if(mOpt.err_e<=0.) mOpt.err_e = 1.;  //mOpt.err_E=-1.;
+}
+if(strstr(opt.c_str(),",X")) { // Valeur du StopChi2
+  p = opt.find(",X"); q = opt.find_first_of(',',p+1);
+  dum = opt.substr(p,q-p);
+  if(dum.length()>2) sscanf(dum.c_str(),",X%lf",&mOpt.stc2);
+}
+if(strstr(opt.c_str(),",N")) { // Nombre maximum d'iterations
+  p = opt.find(",N"); q = opt.find_first_of(',',p+1);
+  dum = opt.substr(p,q-p);
+  if(dum.length()>2) sscanf(dum.c_str(),",N%d",&mOpt.nstep);
+}
+if(strstr(opt.c_str(),",l")) { // niveau de print
+  p = opt.find(",l"); q = opt.find_first_of(',',p+1);
+  dum = opt.substr(p,q-p);
+  if(dum.length()>2) sscanf(dum.c_str(),",l%d",&mOpt.lp);
+}
+if(strstr(opt.c_str(),",d")) { // niveau de debug du fit
+  p = opt.find(",d"); q = opt.find_first_of(',',p+1);
+  dum = opt.substr(p,q-p);
+  if(dum.length()>2) sscanf(dum.c_str(),",d%d",&mOpt.lpg);
+}
+if(strstr(opt.c_str(),",I")) { // intervalle de fit selon X
+  p = opt.find(",I"); q = opt.find_first_of(',',p+1);
+  dum = opt.substr(p,q-p);
+  if(dum.length()>2) sscanf(dum.c_str(),",I%d/%d",&mOpt.i1,&mOpt.i2);
+}
+if(strstr(opt.c_str(),",J")) { // intervalle de fit selon Y
+  p = opt.find(",J"); q = opt.find_first_of(',',p+1);
+  dum = opt.substr(p,q-p);
+  if(dum.length()>2) sscanf(dum.c_str(),",J%d/%d",&mOpt.j1,&mOpt.j2);
+}
+
+return;
+}
+
+/* --Methode-- */
+void PIAFitter::DecodeObject(string obj)
+// Decodage de l'objet a fitter
+{
 NamedObjMgr omg;
-AnyDataObj* obj = omg.GetObj(nom);
-if (obj == NULL) {
-  cout<<"PIAFitter::Fit12D() Error , Pas d'objet de nom "<<nom<<endl;
+AnyDataObj* mobj = omg.GetObj(obj);
+if(mobj == NULL)
+  {cout<<"PIAFitter::DecodeObject Error , Pas d'objet de nom "<<obj<<endl;
+   return;}
+
+// Reset
+ResetDPointer();
+mNVar = mNBinX = mNBinY = mNData = 0;
+
+mObj = mobj; mNObj = obj;
+string ctyp = typeid(*mObj).name();
+
+// 1D
+if (typeid(*mObj) == typeid(Vector)) {
+  mNVar = 1;
+  mV = (Vector*) mObj; mNBinX = mV->NElts(); mNBinY = 1;
+  }
+else if ( (typeid(*mObj) == typeid(HProf)) || (typeid(*mObj) == typeid(Histo)) ) {
+  mNVar = 1;
+  mH = (Histo*)  mObj; mNBinX = mH->NBins(); mNBinY = 1;
+  }
+// 2D
+else if (typeid(*mObj) == typeid(Matrix)) {
+  mNVar = 2;
+  mM = (Matrix*) mObj; mNBinX = mM->NCol(); mNBinY =mM->NRows();
+  }
+else if (typeid(*mObj) == typeid(Histo2D)) {
+  mNVar = 2;
+  mH2 = (Histo2D*) mObj; mNBinX = mH2->NBinX(); mNBinY = mH2->NBinY();
+  }
+else if (typeid(*mObj) == typeid(GeneralFitData)) {
+  mG = (GeneralFitData*) mObj; mNBinX = mG->NData(); mNBinY = 1;
+  if(     mG->NVar()==1) mNVar = 1;
+  else if(mG->NVar()==2) mNVar = 2;
+  else
+     {cout<<"PIAFitter::DecodeObject Error: \n"
+         <<" GeneralFitData ne peut avoir que 1 ou 2 variables d'abscisse: "
+         <<((GeneralFitData*) mObj)->NVar()<<endl;
+     return;}
+  }
+else if (dynamic_cast<RzImage*>(mObj)) {
+  mNVar = 2;
+  mIm = (RzImage*) mObj; mNBinX = mIm->XSize(); mNBinY = mIm->YSize();
+  }
+else {
+  cout<<"PIAFitter::DecodeObject Error , Objet n'est pas un "
+      <<"Histo1D/HProf/Vector/Histo2D/Image/Matrix/GeneralFitData "<<ctyp<<endl;
   return;
-}
-if(func.length()<=0)
-  {cout<<"PIAFitter::Fit12D() Donnez un nom de fonction a fitter."<<endl;
-   return;}
-string ctyp = typeid(*obj).name();
-
-int ndim = 0, nbinx=0, nbiny=0, ndata = 0;
-Vector* v = NULL; Histo* h = NULL;
-Matrix* m = NULL; Histo2D* h2 = NULL; RzImage* im = NULL;
-GeneralFitData* g = NULL;
-
-  // 1D
-if (typeid(*obj) == typeid(Vector)) {
-  ndim = 1;
-  v = (Vector*) obj; nbinx = v->NElts(); nbiny = 1;
-  }
-else if ( (typeid(*obj) == typeid(HProf)) || (typeid(*obj) == typeid(Histo)) ) {
-  ndim = 1;
-  h = (Histo*)  obj; nbinx = h->NBins(); nbiny = 1;
-  }
-else if (typeid(*obj) == typeid(Matrix)) {
-  ndim = 2;
-  m = (Matrix*) obj; nbinx = m->NCol(); nbiny = m->NRows();
-  }
-else if (typeid(*obj) == typeid(Histo2D)) {
-  ndim = 2;
-  h2 = (Histo2D*) obj; nbinx = h2->NBinX(); nbiny = h2->NBinY();
-  }
-else if (typeid(*obj) == typeid(GeneralFitData)) {
-  g = (GeneralFitData*) obj; nbinx = g->NData(); nbiny = 1;
-  if(     g->NVar()==1) ndim = 1;
-  else if(g->NVar()==2) ndim = 2;
-  else {
-     cout<<"GeneralFitData ne peut avoir que 1 ou 2 variables d'abscisse: "
-         <<((GeneralFitData*) obj)->NVar()<<endl; return; }
-  }
-else if (dynamic_cast<RzImage*>(obj)) {
-  ndim = 2;
-  im = (RzImage*) obj; nbinx = im->XSize(); nbiny = im->YSize();
-  }
-else  {
-  cout<<"PIAFitter::Fit12D() Error , Objet n'est pas un "
-      <<"Histo1D/HProf/Vector/Histo2D/Image/Matrix/GeneralFitData "<<ctyp<<endl;
-  return; 
-  }
-
-ndata = nbinx*nbiny;
-if(ndata<=0)
-  {cout<<"L'objet a "<<nbinx<<","<<nbiny<<" bins ("<<ndata<<")"<<endl; return;}
-
-// Decodage des options et des parametres, mise en forme
-Vector Par(1); Vector Step(1); Vector Min(1); Vector Max(1); DFOPTIONS O;
-DecodeFitsOptions(par,step,min,max,opt,Par,Step,Min,Max,O);
-O.i1 = (O.i1<0||O.i1>=nbinx)? 0: O.i1;
-O.i2 = (O.i2<0||O.i2>=nbinx||O.i2<O.i1)? nbinx-1: O.i2;
-if(ndim>=2) {
-  O.j1 = (O.j1<0||O.j1>=nbiny)? 0: O.j1;
-  O.j2 = (O.j2<0||O.j2>=nbiny||O.j2<O.j1)? nbiny-1: O.j2;
-} else O.j2 = O.j1 = 0;
-if(O.polcx==2) {
-  if(v||m)    O.xc = (O.i2-O.i1+1)/2.;
-  else if(h)  O.xc = (h->XMin()+h->XMax())/2.;
-  else if(h2) O.xc = (h2->XMin()+h2->XMax())/2.;
-  else if(g)  {double mini,maxi; g->GetMinMax(2,mini,maxi); O.xc=(mini+maxi)/2.;}
-  else if(im) {O.xc = im->XOrg() * im->XPxSize()*(O.i2-O.i1+1)/2.;}
-}
-if(O.polcy==2 && ndim>=2) {
-  if(m)  O.yc = (O.j2-O.j1+1)/2.;
-  if(h2) O.yc = (h2->YMin()+h2->YMax())/2.;
-  if(g)  {double mini,maxi; g->GetMinMax(12,mini,maxi); O.yc=(mini+maxi)/2.;}
-  if(im) {O.yc = im->YOrg() * im->YPxSize()*(O.j2-O.j1+1)/2.;}
-}
-if(O.lp>0)
-  cout<<"Fit["<<nbinx<<","<<nbiny<<"] ("<<ndata<<") dim="<<ndim<<":"
-      <<" Int=["<<O.i1<<","<<O.i2<<"],["<<O.j1<<","<<O.j2<<"]"<<endl
-      <<" Cent="<<O.polcx<<","<<O.polcy<<","<<O.xc<<"+x"<<","<<O.yc<<"+y"
-      <<" TypE="<<O.err_e<<","<<O.err_E
-      <<" StpX2="<<O.stc2<<" Nstep="<<O.nstep
-      <<" lp,lpg="<<O.lp<<","<<O.lpg<<endl;
-
-///////////////////////////////////
-// Remplissage de GeneralFitData //
-///////////////////////////////////
-GeneralFitData mydata(ndim,ndata,0);
-{for(int i=O.i1;i<=O.i2;i++) for(int j=O.j1;j<=O.j2;j++) {
-  double x,y,f,e;
-
-  if(v)
-    {x= (double) i; f=(*v)(i); e=1.;}
-  else if(h)
-    {x=h->BinCenter(i); f=(*h)(i); e=(h->HasErrors())?h->Error(i):1.;}
-  else if(m)
-    {x=(double) i; y=(double) j; f=(*m)(j,i); e=1.;}
-  else if(h2)
-    {float xf,yf; h2->BinCenter(i,j,xf,yf); x=(double)xf; y=(double)yf;
-     f=(*h2)(i,j); e=(h2->HasErrors())?h2->Error(i,j):1.;}
-  else if(im)
-    {x=im->XOrg()+(i+0.5)*im->XPxSize(); y=im->YOrg()+(j+0.5)*im->YPxSize();
-     f=im->DValue(i,j); e=1.;}
-  else if(g&&ndim==1) {x= g->X(i); f=g->Val(i); e=g->EVal(i);}
-  else if(g&&ndim==2) {x= g->X(i); y= g->Y(i); f=g->Val(i); e=g->EVal(i);}
+  }
+
+mNData = mNBinX*mNBinY;
+if(mNData<=0) {
+  cout<<"L'objet a "<<mNBinX<<","<<mNBinY<<" bins ("<<mNData<<")"<<endl;
+  return;
+}
+
+return;
+}
+
+/* --Methode-- */
+void PIAFitter::CheckOptions(void)
+// Check consistence des choix des options
+// ATTENTION: cette methode a besoin que l'objet a fitter soit deja lu
+{
+if(mOpt.lp<0) mOpt.lp = 0;
+if(mOpt.lpg<0) mOpt.lpg = 0;
+mOpt.i1 = (mOpt.i1<0||mOpt.i1>=mNBinX)? 0: mOpt.i1;
+mOpt.i2 = (mOpt.i2<0||mOpt.i2>=mNBinX||mOpt.i2<mOpt.i1)? mNBinX-1: mOpt.i2;
+if(mNVar>=2) {
+  mOpt.j1 = (mOpt.j1<0||mOpt.j1>=mNBinY)? 0: mOpt.j1;
+  mOpt.j2 = (mOpt.j2<0||mOpt.j2>=mNBinY||mOpt.j2<mOpt.j1)? mNBinY-1: mOpt.j2;
+} else mOpt.j2 = mOpt.j1 = 0;
+if(mOpt.polcx==2) {
+  if(mV||mM)   mOpt.xc = (mOpt.i2-mOpt.i1+1)/2.;
+  else if(mH)  mOpt.xc = (mH->XMin()+mH->XMax())/2.;
+  else if(mH2) mOpt.xc = (mH2->XMin()+mH2->XMax())/2.;
+  else if(mG)  {double mini,maxi; mG->GetMinMax(2,mini,maxi); mOpt.xc=(mini+maxi)/2.;}
+  else if(mIm) {mOpt.xc = mIm->XOrg() * mIm->XPxSize()*(mOpt.i2-mOpt.i1+1)/2.;}
+}
+if(mOpt.polcy==2 && mNVar>=2) {
+  if(mM)  mOpt.yc = (mOpt.j2-mOpt.j1+1)/2.;
+  if(mH2) mOpt.yc = (mH2->YMin()+mH2->YMax())/2.;
+  if(mG)  {double mini,maxi; mG->GetMinMax(12,mini,maxi); mOpt.yc=(mini+maxi)/2.;}
+  if(mIm) {mOpt.yc = mIm->YOrg() * mIm->YPxSize()*(mOpt.j2-mOpt.j1+1)/2.;}
+}
+if(mOpt.err_e>0.) mOpt.err_E=-1.;
+if(mOpt.err_E>0.) mOpt.err_e =-1.;
+if(mOpt.stc2<=0.) mOpt.stc2 = 1.e-3;
+if(mOpt.nstep<2) mOpt.nstep = 100;
+
+return;
+}
+
+/* --Methode-- */
+void PIAFitter::PrintOptions(void)
+// Print des options
+{
+cout<<"Fit["<<mNBinX<<","<<mNBinY<<"] ("<<mNData<<") dim="<<mNVar<<":"
+    <<" Int=["<<mOpt.i1<<","<<mOpt.i2<<"],["<<mOpt.j1<<","<<mOpt.j2<<"]"<<endl
+    <<" Cent="<<mOpt.polcx<<","<<mOpt.polcy<<","<<mOpt.xc<<"+x"<<","<<mOpt.yc<<"+y"
+    <<" TypE="<<mOpt.err_e<<","<<mOpt.err_E
+    <<" StpX2="<<mOpt.stc2<<" Nstep="<<mOpt.nstep<<" Init.LFit="<<mOpt.fromlastfit
+    <<" lp,lpg="<<mOpt.lp<<","<<mOpt.lpg<<endl;
+return;
+}
+
+/* --Methode-- */
+void PIAFitter::FillGData(void)
+// Fill generalfitdata pour le fit
+// ATTENTION: cette methode a besoin que les options soient decodees et checkees
+{
+if(mNData<=0) return;
+if(mGData!=NULL) {delete mGData; mGData=NULL;}
+mGData = new GeneralFitData(mNVar,mNData,0);
+
+for(int i=mOpt.i1;i<=mOpt.i2;i++) for(int j=mOpt.j1;j<=mOpt.j2;j++) {
+  double x,y=0.,f,e;
+
+  if(mV)
+    {x= (double) i; f=(*mV)(i); e=1.;}
+  else if(mH)
+    {x=mH->BinCenter(i); f=(*mH)(i); e=(mH->HasErrors())?mH->Error(i):1.;}
+  else if(mM)
+    {x=(double) i; y=(double) j; f=(*mM)(j,i); e=1.;}
+  else if(mH2)
+    {float xf,yf; mH2->BinCenter(i,j,xf,yf); x=(double)xf; y=(double)yf;
+     f=(*mH2)(i,j); e=(mH2->HasErrors())?mH2->Error(i,j):1.;}
+  else if(mIm)
+    {x=mIm->XOrg()+(i+0.5)*mIm->XPxSize(); y=mIm->YOrg()+(j+0.5)*mIm->YPxSize();
+     f=mIm->DValue(i,j); e=1.;}
+  else if(mG&&mNVar==1) {x= mG->X(i); f=mG->Val(i); e=mG->EVal(i);}
+  else if(mG&&mNVar==2) {x= mG->X(i); y=mG->Y(i); f=mG->Val(i); e=mG->EVal(i);}
   else x=y=f=e=0.;
 
   // Gestion des erreurs a utiliser
-  if(O.err_e>0.) e=O.err_e;
-  else if(O.err_E>0.) {e=(f<-1.||f>1.)?O.err_E*sqrt(fabs(f)):O.err_E;}
-
-  // Remplissage de generalfit
-  if(func[0]=='p') {x -= O.xc; if(ndim>=2) y -= O.yc;}
-  if(ndim==1)      mydata.AddData1(x,f,e);
-  else if(ndim==2) mydata.AddData2(x,y,f,e);
-}}
-if(mydata.NData()<=0)
-  {cout<<"Pas de donnees dans GeneralFitData: "<<mydata.NData()<<endl;
-   return;}
-if(O.lpg>1) {
-  mydata.PrintStatus();
-  mydata.PrintData(0);
-  mydata.PrintData(mydata.NData()-1);
-}
-
-////////////////////////////////////////////
-// Identification de la fonction a fitter //
-////////////////////////////////////////////
-GeneralFunction* myfunc = NULL;
-if(func[0]=='p' && ndim==1) {
-  // Fit de polynome sans passer par les GeneralFit
-  int degre = 0;
-  if(func.length()>1) sscanf(func.c_str()+1,"%d",&degre);
-  cout<<"Fit (lineaire) 1D polynome de degre "<<degre<<endl;
-  Poly p1(0);
-  double c2rl = mydata.PolFit(0,p1,degre);
-  cout<<"C2r_lineaire = "<<c2rl<<endl;
-  if(O.lp>0) cout<<p1<<endl;
+  if(mOpt.err_e>0.) e=mOpt.err_e;
+  else if(mOpt.err_E>0.) {e=(f<-1.||f>1.)?mOpt.err_E*sqrt(fabs(f)):mOpt.err_E;}
+
+  // Remplissage de generalfitdata
+  if(mFName[0]=='p') {x -= mOpt.xc; if(mNVar>=2) y -= mOpt.yc;}
+  if(mNVar==1)      mGData->AddData1(x,f,e);
+  else if(mNVar==2) mGData->AddData2(x,y,f,e);
+}
+
+if(mGData->NData()<=0) {
+  cout<<"Pas de donnees dans GeneralFitData: "<<mGData->NData()<<endl;
   return;
-
-} else if(func[0]=='P' && ndim==1) {
-  // Fit de polynome
+}
+
+if(mOpt.lpg>1) {
+  mGData->PrintStatus();
+  mGData->PrintData(0);
+  mGData->PrintData(mGData->NData()-1);
+}
+
+return;
+}
+
+/* --Methode-- */
+void PIAFitter::DecodeFunction(string func)
+// Fonction a fitter
+// ATTENTION: cette methode a besoin que les donnees soient lues
+{
+if(func.length()<=0) {
+  cout<<"PIAFitter::DecodeFunction Donnez un nom de fonction a fitter."<<endl;
+  return;
+}
+if(mFunc!=NULL) {delete mFunc; mFunc=NULL;}
+
+mNPar=0;
+mFName = func;
+
+if(func[0]=='p' && mNVar==1) { //polynome
   int degre = 0;
   if(func.length()>1) sscanf(func.c_str()+1,"%d",&degre);
   cout<<"Fit polynome 1D de degre "<<degre<<endl;
-  Polyn1D* myf = new Polyn1D(degre,O.xc);
-  myfunc = myf;
-
-} else if(func[0]=='e' && ndim==1) {
-  // Fit d'exponentielle
+  Polyn1D* myf = new Polyn1D(degre,mOpt.xc);
+  mFunc = myf; mNPar = mFunc->NPar();
+
+} else if(func[0]=='e' && mNVar==1) { //exponentielle
   int degre =-1;
   if(func.length()>1) sscanf(func.c_str()+1,"%d",&degre);
   cout<<"Fit d'exponentielle+polynome 1D de degre "<<degre<<endl;
   Exp1DPol* myf;
-  if(degre>=0) myf = new Exp1DPol((unsigned int)degre,O.xc);
-       else    myf = new Exp1DPol(O.xc);
-  myfunc = myf;
-
-} else if(func[0]=='g' && ndim==1) {
-  // Fit de gaussienne en hauteur
+  if(degre>=0) myf = new Exp1DPol((unsigned int)degre,mOpt.xc);
+       else    myf = new Exp1DPol(mOpt.xc);
+  mFunc = myf; mNPar = mFunc->NPar();
+
+} else if(func[0]=='g' && mNVar==1) { //gaussienne en hauteur
   int degre =-1;
   if(func.length()>1) sscanf(func.c_str()+1,"%d",&degre);
   cout<<"Fit de Gaussienne_en_hauteur+polynome 1D de degre "<<degre<<endl;
   Gauss1DPol* myf;
-  if(degre>=0) myf = new Gauss1DPol((unsigned int)degre,((O.polcx)?true:false));
-  else { bool bfg = (O.polcx)?true:false;   myf = new Gauss1DPol(bfg); }
-  myfunc = myf;
-
-} else if(func[0]=='G' && ndim==1) {
-  // Fit de gaussienne en volume
+  if(degre>=0) myf = new Gauss1DPol((unsigned int)degre,((mOpt.polcx)?true:false));
+  else { bool bfg = (mOpt.polcx)?true:false;   myf = new Gauss1DPol(bfg); }
+  mFunc = myf; mNPar = mFunc->NPar();
+
+} else if(func[0]=='G' && mNVar==1) { //gaussienne en volume
   int degre =-1;
   if(func.length()>1) sscanf(func.c_str()+1,"%d",&degre);
   cout<<"Fit de Gaussienne_en_volume+polynome 1D de degre "<<degre<<endl;
   GaussN1DPol* myf;
-  if(degre>=0) myf = new GaussN1DPol((unsigned int)degre,((O.polcx)?true:false));
-  else  { bool bfg = (O.polcx)?true:false;   myf = new GaussN1DPol(bfg); }
-  myfunc = myf;
-
-} else if(func[0]=='p' && ndim==2) {
-  // Fit de polynome 2D sans passer par les GeneralFit
-  int degre = 0;
-  if(func.length()>1) sscanf(func.c_str()+1,"%d",&degre);
-  cout<<"Fit (lineaire) polynome 2D de degre "<<degre<<endl;
-  Poly2 p2(0);
-  double c2rl = mydata.PolFit(0,1,p2,degre);
-  cout<<"C2r_lineaire = "<<c2rl<<endl;
-  if(O.lp>0) cout<<p2<<endl;
-  return;
-
-} else if(func[0]=='P' && ndim==2) {
-  // Fit de polynome 2D
+  if(degre>=0) myf = new GaussN1DPol((unsigned int)degre,((mOpt.polcx)?true:false));
+  else  { bool bfg = (mOpt.polcx)?true:false;   myf = new GaussN1DPol(bfg); }
+  mFunc = myf; mNPar = mFunc->NPar();
+
+} else if(func[0]=='p' && mNVar==2) { //polynome 2D
   int degre = 0;
   if(func.length()>1) sscanf(func.c_str()+1,"%d",&degre);
   cout<<"Fit polynome 2D de degre "<<degre<<endl;
-  Polyn2D* myf = new Polyn2D(degre,O.xc,O.yc);
-  myfunc = myf;
-
-} else if(func[0]=='G' && ndim==2) {
-  // Fit de gaussienne+fond en volume
+  Polyn2D* myf = new Polyn2D(degre,mOpt.xc,mOpt.yc);
+  mFunc = myf; mNPar = mFunc->NPar();
+
+} else if(func[0]=='G' && mNVar==2) { //gaussienne+fond en volume
   int integ = 0;
   if(func.length()>1) if(func[1]=='i') integ=1;
   cout<<"Fit de Gaussienne+Fond 2D integ="<<integ<<endl;
-  if(integ) {GauRhInt2D* myf = new GauRhInt2D; myfunc = myf;}
-    else    {GauRho2D* myf = new GauRho2D; myfunc = myf;}
-
-} else if(func[0]=='d' && ndim==2) {
-  // Fit de DL gaussienne+fond en volume
+  if(integ) {GauRhInt2D* myf = new GauRhInt2D; mFunc = myf;}
+    else    {GauRho2D* myf = new GauRho2D; mFunc = myf;}
+  mNPar = mFunc->NPar();
+
+} else if(func[0]=='d' && mNVar==2) { //DL gaussienne+fond en volume
   int integ = 0;
   if(func.length()>1) if(func[1]=='i') integ=1;
   cout<<"Fit de DL de Gaussienne+Fond 2D integ="<<integ<<endl;
-  if(integ) {GdlRhInt2D* myf = new GdlRhInt2D; myfunc = myf;}
-    else    {GdlRho2D* myf = new GdlRho2D; myfunc = myf;}
-
-} else if(func[0]=='D' && ndim==2) {
-  // Fit de DL gaussienne+fond avec coeff variable p6 en volume
+  if(integ) {GdlRhInt2D* myf = new GdlRhInt2D; mFunc = myf;}
+    else    {GdlRho2D* myf = new GdlRho2D; mFunc = myf;}
+  mNPar = mFunc->NPar();
+
+} else if(func[0]=='D' && mNVar==2) { //DL gaussienne+fond avec coeff variable p6 en volume
   int integ = 0;
   if(func.length()>1) if(func[1]=='i') integ=1;
   cout<<"Fit de DL de Gaussienne+Fond avec coeff variable (p6) 2D integ="<<integ<<endl;
-  if(integ) {Gdl1RhInt2D* myf = new Gdl1RhInt2D; myfunc = myf;}
-    else    {Gdl1Rho2D* myf = new Gdl1Rho2D; myfunc = myf;}
-
-} else if(func[0]=='M' && ndim==2) {
-  // Fit de Moffat+fond (volume)
+  if(integ) {Gdl1RhInt2D* myf = new Gdl1RhInt2D; mFunc = myf;}
+    else    {Gdl1Rho2D* myf = new Gdl1Rho2D; mFunc = myf;}
+  mNPar = mFunc->NPar();
+
+} else if(func[0]=='M' && mNVar==2) { //Moffat+fond (volume)
   int integ = 0;
   if(func.length()>1) if(func[1]=='i') integ=1;
   cout<<"Fit de Moffat+Fond (expos=p6) 2D integ="<<integ<<endl;
-  if(integ) {MofRhInt2D* myf = new MofRhInt2D; myfunc = myf;}
-    else    {MofRho2D* myf = new MofRho2D; myfunc = myf;}
+  if(integ) {MofRhInt2D* myf = new MofRhInt2D; mFunc = myf;}
+    else    {MofRho2D* myf = new MofRho2D; mFunc = myf;}
+  mNPar = mFunc->NPar();
 
 } else {
-  cout<<"Fonction "<<func<<" inconnue pour la dim "<<ndim<<endl;
+  cout<<"Fonction "<<func<<" inconnue pour la dim "<<mNVar<<endl;
+  mNPar = 0;
   return;
 }
 
-/////////////////////////
-// Fit avec generalfit //
-/////////////////////////
-if(myfunc->NPar()>Par.NElts())
-  {cout<<"Trop de parametres: "<<myfunc->NPar()<<">"<<Par.NElts()<<endl;
-  if(myfunc) delete myfunc; return;}
-GeneralFit myfit(myfunc);
-myfit.SetDebug(O.lpg);
-myfit.SetData(&mydata);
-myfit.SetStopChi2(O.stc2);
-myfit.SetMaxStep(O.nstep);
-{for(int i=0;i<myfunc->NPar();i++) {
+return;
+}
+
+/* --Methode-- */
+void PIAFitter::ReSetParam(void)
+// Pour (re)dimensionner les tableaux des parametres
+// ATTENTION: cette methode a besoin que la fonction soit connue
+{
+if(mNPar==0) return;
+mPar.Realloc(mNPar);  mPar=0.;
+mStep.Realloc(mNPar); mStep=1.;
+mMin.Realloc(mNPar);  mMin=1.;
+mMax.Realloc(mNPar);  mMax=-1.;
+mFix.Realloc(mNPar);  mFix=0.;
+return;
+}
+
+/* --Methode-- */
+void PIAFitter::InitParFromLastFit(void)
+// Initialisation du fit a partir des resultats du fit precedent
+// (Les arguments de la ligne de commande sont prioritaires)
+// ATTENTION: cette methode a besoin que la fonction soit connue
+{
+if(mNPar==0) return;
+int n = mParSave.NElts();
+if(n==0) return;
+
+for(int i=0;i<mNPar;i++) {
+  if(i>=n) break;
+  mPar(i)  = mParSave(i);
+  mStep(i) = mStepSave(i);
+  mMin(i)  = mMinSave(i);
+  mMax(i)  = mMaxSave(i);
+  mFix(i)  = mFixSave(i);
+}
+
+return;
+}
+
+/* --Methode-- */
+void PIAFitter::DecodeParam(string par,string step,string min,string max,string fix)
+// Decodage des parametres
+// ATTENTION: cette methode a besoin que la fonction soit connue
+//                            et que les options soient decodees
+{
+if(mNPar==0) return;
+ReSetParam();
+if(mOpt.fromlastfit) InitParFromLastFit();
+Vector* v=NULL; string* s=NULL;
+for(int j=0;j<5;j++) {
+  if(j==0) {v=&mPar; s=&par;} else if(j==1) {v=&mStep; s=&step;}
+  else if(j==2) {v=&mMin; s=&min;} else if(j==3) {v=&mMax; s=&max;}
+  else if(j==4) {v=&mFix; s=&fix;}
+  if(s->length()>0) *s += ",";
+  for(int i=0;i<mNPar;i++) {
+    if(s->length()<=0) break;
+    if((*s)[0]!='!') sscanf(s->c_str(),"%lf",&(*v)(i));
+    size_t p = s->find_first_of(',') + 1;
+    if(p>=s->length()) *s = ""; else *s = s->substr(p);
+  }
+}
+return;
+}
+
+/* --Methode-- */
+int PIAFitter::DoFit(void)
+// Fit avec generalfit
+{
+if(mNPar==0) return -1000;
+if(mFunc==NULL || mGData==NULL) {
+  cout<<"PIAFitter::DoFit error: mFunc="<<mFunc<<" mGData="<<mGData<<endl;
+  return -1001;
+}
+// classe GeneraFit
+if(mFit != NULL) {delete mFit; mFit= NULL;}
+mFit= new GeneralFit(mFunc);
+// Options et Set de GeneralFit
+mFit->SetDebug(mOpt.lpg);
+mFit->SetData(mGData);
+mFit->SetStopChi2(mOpt.stc2);
+mFit->SetMaxStep(mOpt.nstep);
+{for(int i=0;i<mFunc->NPar();i++) {
   char str[10];
   sprintf(str,"P%d",i);
-  myfit.SetParam(i,str,Par(i),Step(i),Min(i),Max(i));
+  mFit->SetParam(i,str,mPar(i),mStep(i),mMin(i),mMax(i));
+  if((int)(mFix(i)+0.001)>=1.) mFit->SetFix(i);
 }}
-if(O.lp>1) myfit.PrintFit();
+if(mOpt.lp>1) mFit->PrintFit();
+
+// Fit
+mParSave = mPar;
+mStepSave = mStep; mMinSave = mMin; mMaxSave = mMax; mFixSave = mFix;
+mOptSave = mOpt;
 double c2r = -1.;
-int rcfit = (double) myfit.Fit();
-if(O.lp>0) myfit.PrintFit();
+int rcfit = mFit->Fit();
+if(mOpt.lp>0) mFit->PrintFit();
 if(rcfit>0) {
-  c2r = myfit.GetChi2Red();
-  cout<<"C2r_Reduit = "<<c2r<<" nstep="<<myfit.GetNStep()<<" rc="<<rcfit<<endl;
-  Vector ParFit(myfunc->NPar());
-  for(int i=0;i<myfunc->NPar();i++) ParFit(i)=myfit.GetParm(i);
+  c2r = mFit->GetChi2Red();
+  cout<<"C2r_Reduit = "<<c2r<<" nstep="<<mFit->GetNStep()<<" rc="<<rcfit<<endl;
+  for(int i=0;i<mFunc->NPar();i++) mParSave(i)=mFit->GetParm(i);
 } else {
-  cout<<"echec Fit, rc = "<<rcfit<<"  nstep="<<myfit.GetNStep()<<endl;
-  myfit.PrintFitErr(rcfit);
-}
-
-// Mise a disposition des resultats
-if(rcfit>=0 && myfunc && (O.okres>0||O.okfun>0)) {
-  string nomres = nom + "res";
-  string nomfun = nom + "fun";
-  if(v) {
-    if(O.okres) {Vector* ob = v->FitResidus(myfit);  if(ob) omg.AddObj(ob,nomres);}
-    if(O.okfun) {Vector* ob = v->FitFunction(myfit); if(ob) omg.AddObj(ob,nomfun);}
-  } else if(h) {
-    if(O.okres) {Histo* ob = h->FitResidus(myfit);  if(ob) omg.AddObj(ob,nomres);}
-    if(O.okfun) {Histo* ob = h->FitFunction(myfit); if(ob) omg.AddObj(ob,nomfun);}
-  } else if(m) {
-    if(O.okres) {Matrix* ob = m->FitResidus(myfit);  if(ob) omg.AddObj(ob,nomres);}
-    if(O.okfun) {Matrix* ob = m->FitFunction(myfit); if(ob) omg.AddObj(ob,nomfun);}
-  } else if(h2) {
-    if(O.okres) {Histo2D* ob = h2->FitResidus(myfit);  if(ob) omg.AddObj(ob,nomres);}
-    if(O.okfun) {Histo2D* ob = h2->FitFunction(myfit); if(ob) omg.AddObj(ob,nomfun);}
-  } else if(im) {
-    if(O.okres) {RzImage* ob = im->FitResidus(myfit);  if(ob) omg.AddObj(ob,nomres);}
-    if(O.okfun) {RzImage* ob = im->FitFunction(myfit); if(ob) omg.AddObj(ob,nomfun);}
-  } else if(g) {
-    if(O.okres) {GeneralFitData* ob = g->FitResidus(myfit);  if(ob) omg.AddObj(ob,nomres);}
-    if(O.okfun) {GeneralFitData* ob = g->FitFunction(myfit); if(ob) omg.AddObj(ob,nomfun);}
-  }
-}
-
-// Nettoyage
-if(myfunc) delete myfunc;
-return;
-}
-
-
-/* --Function static propre aux routines de fit 1D et 2D-- cmv 13/10/98 */
-void PIAFitter::DecodeFitsOptions(string par,string step,string min,string max,string opt
-                      ,Vector& Par,Vector& Step,Vector& Min,Vector& Max,DFOPTIONS& O)
-//| Pour decoder les "string" et remplir les vecteurs du fit (cf commentaires dans Fit1D)
-{
-// set des vecteurs et decodage des string correspondantes
-int NParMax = 100;
-Par.Realloc(NParMax); Step.Realloc(NParMax);
-Min.Realloc(NParMax); Max.Realloc(NParMax);
-{
-  Vector* v=NULL; string* s=NULL;
-  {for(int i=0;i<NParMax;i++) {Par(i)=0.; Step(i)=1.; Min(i)=1.; Max(i)=-1.;}}
-  for(int j=0;j<4;j++) {
-    if(j==0)      {v=&Par; s=&par;}
-    else if(j==1) {v=&Step; s=&step;}
-    else if(j==2) {v=&Min; s=&min;}
-    else if(j==3) {v=&Max; s=&max;}
-    if(s->length()>0) *s += ",";
-    for(int i=0;i<NParMax;i++) {
-      if(s->length()<=0) break;
-      sscanf(s->c_str(),"%lf",&(*v)(i));
-      size_t p = s->find_first_of(',') + 1;
-      if(p>=s->length()) *s = ""; else *s = s->substr(p);
-    }
-  }
-}
-
-// Decodage de options de opt
-O.okres = O.okfun = 0;
-O.polcx = O.polcy = 0;
-O.xc = O.yc = 0.;
-O.stc2 = 1.e-3;
-O.nstep = 100;
-O.err_e = O.err_E = -1.;
-O.lp = 1; O.lpg = 0;
-O.i1 = O.j1 = O.i2 = O.j2 = -1;
-
-if(opt.length()<=0) return;
-opt = "," + opt + ",";
-
-if(strstr(opt.c_str(),",r,")) O.okres = 1;  // residus
-if(strstr(opt.c_str(),",f,")) O.okfun = 1;  // fonction fittee
-if(strstr(opt.c_str(),",x")) { // Demande de centrage (fit X=x-xc)
-  O.polcx = 2; // Le centrage est calcule automatiquement
-  size_t p = opt.find(",x");
-  size_t q = opt.find_first_of(',',p+1);
-  string dum = opt.substr(p,q-p);
-  if(dum.length()>2) {
-    sscanf(dum.c_str(),",x%lf",&O.xc);
-    O.polcx = 1; // Le centrage est fixe par la valeur lue
-  }
-}
-if(strstr(opt.c_str(),",y")) { // Demande de centrage (fit Y=y-yc)
-  O.polcy = 2; // Le centrage est calcule automatiquement
-  size_t p = opt.find(",y");
-  size_t q = opt.find_first_of(',',p+1);
-  string dum = opt.substr(p,q-p);
-  if(dum.length()>2) {
-    sscanf(dum.c_str(),",y%lf",&O.yc);
-    O.polcy = 1; // Le centrage est fixe par la valeur lue
-  }
-}
-if(strstr(opt.c_str(),",E")) { // Erreurs imposees a "sqrt(val)" ou "aa.b*sqrt(val)"
-  size_t p = opt.find(",E");
-  size_t q = opt.find_first_of(',',p+1);
-  string dum = opt.substr(p,q-p);
-  if(dum.length()>2) sscanf(dum.c_str(),",E%lf",&O.err_E);
-  if(O.err_E<=0.) O.err_E = 1.;
-  O.err_e=-1.;
-}
-if(strstr(opt.c_str(),",e")) { // Erreurs imposees a "1" ou "aa.b"
-  size_t p = opt.find(",e");
-  size_t q = opt.find_first_of(',',p+1);
-  string dum = opt.substr(p,q-p);
-  if(dum.length()>2) sscanf(dum.c_str(),",e%lf",&O.err_e);
-  if(O.err_e<=0.) O.err_e = 1.;
-  O.err_E=-1.;
-}
-if(strstr(opt.c_str(),",X")) { // Valeur du StopChi2
-  size_t p = opt.find(",X");
-  size_t q = opt.find_first_of(',',p+1);
-  string dum = opt.substr(p,q-p);
-  if(dum.length()>2) sscanf(dum.c_str(),",X%lf",&O.stc2);
-  if(O.stc2<=0.) O.stc2 = 1.e-3;
-}
-if(strstr(opt.c_str(),",N")) { // Nombre maximum d'iterations
-  size_t p = opt.find(",N");
-  size_t q = opt.find_first_of(',',p+1);
-  string dum = opt.substr(p,q-p);
-  if(dum.length()>2) sscanf(dum.c_str(),",N%d",&O.nstep);
-  if(O.nstep<2) O.nstep = 100;
-}
-if(strstr(opt.c_str(),",l")) { // niveau de print
-  size_t p = opt.find(",l");
-  size_t q = opt.find_first_of(',',p+1);
-  string dum = opt.substr(p,q-p);
-  float ab;
-  if(dum.length()>2) sscanf(dum.c_str(),",l%f",&ab);
-  if(ab<0) ab = 0.;
-  O.lp = (int) ab; O.lpg = int(10.*(ab-(float)O.lp+0.01));
-}
-if(strstr(opt.c_str(),",I")) { // intervalle de fit selon X
-  size_t p = opt.find(",I");
-  size_t q = opt.find_first_of(',',p+1);
-  string dum = opt.substr(p,q-p);
-  if(dum.length()>2) sscanf(dum.c_str(),",I%d/%d",&O.i1,&O.i2);
-}
-if(strstr(opt.c_str(),",J")) { // intervalle de fit selon Y
-  size_t p = opt.find(",J");
-  size_t q = opt.find_first_of(',',p+1);
-  string dum = opt.substr(p,q-p);
-  if(dum.length()>2) sscanf(dum.c_str(),",J%d/%d",&O.j1,&O.j2);
-}
-return;
-}
-
-
-// ---------------- Fenetre de fit interactive -------
-
+  cout<<"echec Fit, rc = "<<rcfit<<"  nstep="<<mFit->GetNStep()<<endl;
+  mFit->PrintFitErr(rcfit);
+}
+
+return rcfit;
+}
+
+/* --Methode-- */
+void PIAFitter::FitFunRes(void)
+// Mise a disposition des resultats (fonction fitee et residus)
+{
+if(mFunc && (mOpt.okres||mOpt.okfun)) {
+  NamedObjMgr omg;
+  string nomres = mNObj + "res";
+  string nomfun = mNObj + "fun";
+  if(mV) {
+    if(mOpt.okres) {Vector* ob = mV->FitResidus(*mFit);  if(ob) omg.AddObj(ob,nomres);}
+    if(mOpt.okfun) {Vector* ob = mV->FitFunction(*mFit); if(ob) omg.AddObj(ob,nomfun);}
+  } else if(mH) {
+    if(mOpt.okres) {Histo* ob = mH->FitResidus(*mFit);  if(ob) omg.AddObj(ob,nomres);}
+    if(mOpt.okfun) {Histo* ob = mH->FitFunction(*mFit); if(ob) omg.AddObj(ob,nomfun);}
+  } else if(mM) {
+    if(mOpt.okres) {Matrix* ob = mM->FitResidus(*mFit);  if(ob) omg.AddObj(ob,nomres);}
+    if(mOpt.okfun) {Matrix* ob = mM->FitFunction(*mFit); if(ob) omg.AddObj(ob,nomfun);}
+  } else if(mH2) {
+    if(mOpt.okres) {Histo2D* ob = mH2->FitResidus(*mFit);  if(ob) omg.AddObj(ob,nomres);}
+    if(mOpt.okfun) {Histo2D* ob = mH2->FitFunction(*mFit); if(ob) omg.AddObj(ob,nomfun);}
+  } else if(mIm) {
+    if(mOpt.okres) {RzImage* ob = mIm->FitResidus(*mFit);  if(ob) omg.AddObj(ob,nomres);}
+    if(mOpt.okfun) {RzImage* ob = mIm->FitFunction(*mFit); if(ob) omg.AddObj(ob,nomfun);}
+  } else if(mG) {
+    if(mOpt.okres) {GeneralFitData* ob = mG->FitResidus(*mFit);  if(ob) omg.AddObj(ob,nomres);}
+    if(mOpt.okfun) {GeneralFitData* ob = mG->FitFunction(*mFit); if(ob) omg.AddObj(ob,nomfun);}
+  }
+}
+return;
+}
+
+/* --Methode-- */
+void PIAFitter::LinFit(void)
+// Fit lineaire de polynomes
+{
+if(mFName.length()<=0)
+  {cout<<"PIAFitter::LinFit Donnez un nom de fonction a fitter."<<endl;
+   return;}
+if(!mGData)
+  {cout<<"PIAFitter::LinFit pas de Data."<<endl; return;}
+if(mFName[0]!='p') 
+  {cout<<"PIAFitter::LinFit fonction non prevue "<<mFName<<endl;
+   return;}
+int degre = 0;
+if(mFName.length()>1) sscanf(mFName.c_str()+1,"%d",&degre);
+
+if(mNVar==1) {  // Fit lineaire de polynome 1D
+  cout<<"Fit (lineaire) 1D polynome de degre "<<degre<<endl;
+  Poly p1(0);
+  double c2rl = mGData->PolFit(0,p1,degre);
+  cout<<"C2r_lineaire = "<<c2rl<<endl;
+  if(mOpt.lp>0) cout<<p1<<endl;
+} else if(mNVar==2) {  // Fit lineaire de polynome 2D
+  cout<<"Fit (lineaire) polynome 2D de degre "<<degre<<endl;
+  Poly2 p2(0);
+  double c2rl = mGData->PolFit(0,1,p2,degre);
+  cout<<"C2r_lineaire = "<<c2rl<<endl;
+  if(mOpt.lp>0) cout<<p2<<endl;
+}
+
+return;
+}
+
+/* --Methode-- */
+int PIAFitter::Execute(string& kw, vector<string>& tokens)
+// Execution des commandes de fit : "fit" et "fitw"
+{
+if(tokens.size() < 2) {
+  cout
+  <<"Usage:fit  nomobj func [Options]\n"
+  <<" ou   fitw nomobj func [Options]\n"
+  <<" avec Options: [p:p1,...,pn s:s1,...,sn m:m1,...,mn \n"
+  <<"                M:M1,...,Mn f:f1,...,fn o:.,.,. o:.,.,.]\n"
+  <<" ou   fitlin nomobj func [o:.,.,. o:.,.,.]\n";
+  return(-1);
+}
+// decodage des arguments de la commande
+string mSp,mSs,mSm,mSM,mSf,mSo;
+mSp=""; mSs=""; mSm=""; mSM=""; mSf=""; mSo="";
+if(tokens.size()>2)
+  for(int ip=2;ip<(int)tokens.size();ip++) {
+    if(tokens[ip].length()<=2) continue;
+    const char *c = tokens[ip].c_str();
+    if(c[1]!=':') continue;
+    if(c[0]=='p')      mSp=c+2;
+    else if(c[0]=='s') mSs=c+2;
+    else if(c[0]=='m') mSm=c+2;
+    else if(c[0]=='M') mSM=c+2;
+    else if(c[0]=='f') mSf=c+2;
+    else if(c[0]=='o') {mSo += ","; mSo += c+2;}
+  }
+// Execution des commandes
+DecodeOptions(mSo);
+DecodeObject(tokens[0]);
+CheckOptions();
+if(mOpt.lp>0) PrintOptions();
+FillGData();
+DecodeFunction(tokens[1]);
+DecodeParam(mSp,mSs,mSm,mSM,mSf);
+if (kw == "fit") {
+  int rc = DoFit();
+  if(rc>=0) FitFunRes();
+} else if (kw == "fitlin") {
+  LinFit();
+} else if (kw == "fitw") {
+  if(FWindFit!=NULL) {delete FWindFit; FWindFit=NULL;}
+  FWindFit = new PIAFitterWind(mApp,this);
+  FWindFit->Show();
+}
+
+return(0);
+}
+
+////////////////////////////////////////////////////////////////////
+////---------- Classe de fenetre de fit interactive ------------////
+////////////////////////////////////////////////////////////////////
 
 /* --Methode-- */
@@ -593,87 +771,172 @@
  : PIWindow((PIMsgHandler*)par, "PIAFitter", PIWK_normal, 240, 240, 150, 150)
 {
-dap = par;
-fitter = fiter;
-int bsx, bsy, spx, spy;
-
-// On definit la taille a partir de la taille par defaut des composantes 
-// PIApplicationPrefCompSize(bsx, bsy);
+ReFillGData = false;
+mDap = par;
+mFitter = fiter;
+string obj = mFitter->mNObj;
+string fun = mFitter->mFName;
+int npar = mFitter->mNPar;
+
+// Alloc de la taille
+nlab = 20+npar;   lab = new PILabel*[nlab];    {for(int i=0;i<nlab;i++) lab[i]=NULL;}
+ntxt = 20+4*npar; txt = new PIText*[ntxt];     {for(int i=0;i<ntxt;i++) txt[i]=NULL;}
+nbut = 20;        but = new PIButton*[nbut];   {for(int i=0;i<nbut;i++) but[i]=NULL;}
+nckb = 20+npar;   ckb = new PICheckBox*[nckb]; {for(int i=0;i<nckb;i++) ckb[i]=NULL;}
+
+// On definit la taille a partir de la taille par defaut des composantes
+// bsx,bsy = environ 6 lettres
+int bsx,bsy;
 par->PrefCompSz(bsx, bsy);
-spx = bsx/10;
-spy = bsy/4;
-
-int wszx = 3*spx+4*bsx; 
-int wszy = 5*bsy+6*spy;
+int spx = (bsx>=10) ? bsx/10 : 1;  // intervalle entre lettres X
+int spy = (bsy>=6) ? bsy/6 : 1;    // intervalle entre lettres Y
+
+int wszx = (5*bsx+5*spx)+spx;
+int wszy = 8*(bsy+spy)+npar*(bsy+spy)+spy;
 SetSize(wszx, wszy);
-int cpx = spx;
-int cpy = spy;
-
-cpx = spx+bsx;
-labn = new PILabel(this, "ObjectName", 2.0*bsx, bsy, cpx, cpy);
-labn->SetBinding(PIBK_elastic, PIBK_fixed, PIBK_elastic, PIBK_free);
-
-cpy += spy+bsy;
-cpx = spx;
-ckb[0] = new PICheckBox(this,"Gen.Func", 1001, 2*bsx, bsy, cpx, cpy);
-cpx += bsx*2+spx;
-ckb[1] = new PICheckBox(this,"Gen.Resid", 1002, 2*bsx, bsy, cpx, cpy);
-ckb[0]->SetBinding(PIBK_elastic, PIBK_fixed, PIBK_elastic, PIBK_free);
-ckb[1]->SetBinding(PIBK_elastic, PIBK_fixed, PIBK_elastic, PIBK_free);
-
-cpy += spy+bsy;
-cpx = spx;
-pom[0] = new PIOptMenu(this, "FitFunc", 2*bsx, bsy, cpx, cpy);
-pom[0]->AppendItem("Poly0", 100);
-pom[0]->AppendItem("Poly1", 101);
-pom[0]->AppendItem("Poly2", 102);
-pom[0]->AppendItem("Gaussienne", 103);
-pom[0]->AppendItem("Gauss+Poly0",104);
-pom[0]->AppendItem("Gauss+Poly1",105);
-pom[0]->AppendItem("Gauss+Poly2",106);
-pom[0]->SetBinding(PIBK_elastic, PIBK_fixed, PIBK_elastic, PIBK_free);
-
-cpx += bsx*2+spx;
-pom[1] = new PIOptMenu(this, "NPar", 2*bsx, bsy, cpx, cpy);
-pom[1]->AppendItem("Un NP=1", 201);
-pom[1]->AppendItem("Deux NP=2",202);
-pom[1]->AppendItem("Trois NP=3",203);
-pom[1]->AppendItem("Cinq  NP=5",205);
-pom[1]->AppendItem("Huit  NP=8",208);
-pom[1]->AppendItem("Dix  NP=10",210);
-pom[1]->SetBinding(PIBK_elastic, PIBK_fixed, PIBK_elastic, PIBK_free);
-
-cpy += spy+bsy;
-cpx = 0.5*bsx;
-but[0] =  new PIButton(this, "DoFit", 555, bsx*1.5, bsy, cpx, cpy);
-cpx += bsx*1.5+3*spx;
-but[1] =  new PIButton(this, "Dismiss", 777, bsx*1.5, bsy, cpx, cpy);
-but[0]->SetBinding(PIBK_elastic, PIBK_fixed, PIBK_elastic, PIBK_free);
-but[1]->SetBinding(PIBK_elastic, PIBK_fixed, PIBK_elastic, PIBK_free);
-
-//  Pour les parametres
-cpy += bsy+spy;
-cpx = 0.6*bsx+1.5*spx;
-lab[0] = new PILabel(this, "Init.Val", 1.7*bsx, bsy, cpx, cpy);
-cpx += 1.7*bsx+0.5*spx;
-lab[1] = new PILabel(this, "Step", 1.7*bsx, bsy, cpx, cpy);
-lab[0]->SetBinding(PIBK_elastic, PIBK_fixed, PIBK_elastic, PIBK_free);
-lab[1]->SetBinding(PIBK_elastic, PIBK_fixed, PIBK_elastic, PIBK_free);
-
-for(int i=0; i<FINPARMAX; i++) {
-  char buff[32];
-  cpy += bsy+spy;
-  cpx = spx;
-  sprintf(buff, "P%d",i);
-  labp[i] = new PILabel(this, buff, 0.6*bsx, bsy, cpx, cpy);
-  labp[i]->SetBinding(PIBK_elastic, PIBK_fixed, PIBK_elastic, PIBK_free);
-  sprintf(buff, "Val%d",i);
-  cpx += 0.6*bsx+0.5*spx;
-  txtp[i] = new PIText(this, buff, 1.7*bsx, bsy, cpx, cpy);
-  txtp[i]->SetBinding(PIBK_elastic, PIBK_fixed, PIBK_elastic, PIBK_free);
-  sprintf(buff, "Step%d",i);
-  cpx += 1.7*bsx+0.5*spx;
-  txts[i] = new PIText(this, buff, 1.7*bsx, bsy, cpx, cpy);
-  txts[i]->SetBinding(PIBK_elastic, PIBK_fixed, PIBK_elastic, PIBK_free);
-  }  
+int cpx,cpy;
+
+// 1 ere ligne
+cpx=spx; cpy=spy;
+but[0] = new PIButton(this,"Fr.Last",111,bsx,bsy,cpx,cpy);
+but[0]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx += bsx+spx;
+but[1] = new PIButton(this,"Default",222,bsx,bsy,cpx,cpy);
+but[1]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=bsx+spx;
+ckb[0] = new PICheckBox(this,"Gen Fun",1001,bsx,bsy,cpx,cpy);
+ckb[0]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx += bsx+2*spx;
+ckb[1] = new PICheckBox(this,"Res",1002,bsx,bsy,cpx,cpy);
+ckb[1]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+
+// 2 ieme ligne
+cpx=spx; cpy += bsy+spy;
+ckb[2] = new PICheckBox(this,"Cent X",1003,bsx,bsy,cpx,cpy);
+ckb[2]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=bsx+spx;
+txt[0] = new PIText(this,"",bsx,bsy,cpx,cpy);
+txt[0]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=bsx+2*spx;
+ckb[3] = new PICheckBox(this,"Y",1004,bsx/2,bsy,cpx,cpy);
+ckb[3]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=bsx/2+spx;
+txt[1] = new PIText(this,"",bsx,bsy,cpx,cpy);
+txt[1]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+
+// 3 ieme ligne
+cpx=spx; cpy += bsy+spy;
+lab[0] = new PILabel(this,"Err cste",1.25*bsx,bsy,cpx,cpy);
+lab[0]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=1.25*bsx+spx;
+txt[2] = new PIText(this,"",bsx,bsy,cpx,cpy);
+txt[2]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=bsx+2*spx;
+lab[1] = new PILabel(this,"sqrt",bsx,bsy,cpx,cpy);
+lab[1]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=bsx+spx;
+txt[3] = new PIText(this,"",bsx,bsy,cpx,cpy);
+txt[3]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+
+// 4 ieme ligne
+cpx=spx; cpy += bsy+spy;
+lab[2] = new PILabel(this,"Stop Xi2",1.25*bsx,bsy,cpx,cpy);
+lab[2]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=1.25*bsx+spx;
+txt[4] = new PIText(this,"",bsx,bsy,cpx,cpy);
+txt[4]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=bsx+spx;
+lab[3] = new PILabel(this,"Iter",bsx, bsy, cpx, cpy);
+lab[3]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=bsx+spx;
+txt[5] = new PIText(this,"",bsx,bsy,cpx,cpy);
+txt[5]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+
+// 5 ieme ligne
+cpx=spx; cpy += bsy+spy;
+lab[4] = new PILabel(this,"Print",bsx,bsy,cpx,cpy);
+lab[4]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=bsx+spx;
+txt[6] = new PIText(this,"",bsx/2,bsy,cpx,cpy);
+txt[6]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=bsx/2+spx;
+lab[5] = new PILabel(this,"Debug",bsx,bsy,cpx,cpy);
+lab[5]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=bsx+spx;
+txt[7] = new PIText(this,"",bsx/2,bsy,cpx,cpy);
+txt[7]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+
+// 6 ieme ligne
+cpx=spx; cpy += bsy+spy;
+lab[6] = new PILabel(this,"Range X",bsx,bsy,cpx,cpy);
+lab[6]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=bsx+spx;
+txt[8] = new PIText(this,"",0.75*bsx,bsy,cpx,cpy);
+txt[8]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=0.75*bsx+spx;
+txt[9] = new PIText(this,"",0.75*bsx,bsy,cpx,cpy);
+txt[9]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=0.75*bsx+spx;
+lab[7] = new PILabel(this,"Y",bsx/2,bsy,cpx,cpy);
+lab[7]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=bsx/2+spx;
+txt[10] = new PIText(this,"",0.75*bsx,bsy,cpx,cpy);
+txt[10]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=0.75*bsx+spx;
+txt[11] = new PIText(this,"",0.75*bsx,bsy,cpx,cpy);
+txt[11]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+
+// 7 ieme ligne
+cpx=spx; cpy+=bsy+spy;
+but[2] = new PIButton(this, "Dismiss",777,bsx,bsy,cpx,cpy);
+but[2]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx += bsx+spx;
+but[3] = new PIButton(this, "Refesh",666,bsx,bsy,cpx,cpy);
+but[3]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx += bsx+spx;
+but[4] = new PIButton(this, "Fit",333,bsx,bsy,cpx,cpy);
+but[4]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=bsx+spx;
+lab[8] = new PILabel(this,fun.c_str(),bsx/2,bsy,cpx,cpy);
+lab[8]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+
+// Parametres (npar lignes)
+cpx=spx; cpy+=bsy+spy;
+lab[9] = new PILabel(this,"Par",bsx/2,bsy,cpx,cpy);
+lab[9]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=bsx/2+spx;
+lab[10] = new PILabel(this,"F",bsx/3,bsy,cpx,cpy);
+lab[10]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=bsx/3+spx;
+lab[11] = new PILabel(this,"Init",bsx,bsy,cpx,cpy);
+lab[11]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=bsx+spx;
+lab[12] = new PILabel(this,"Step",bsx,bsy,cpx,cpy);
+lab[12]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=bsx+spx;
+lab[13] = new PILabel(this,"Min",bsx,bsy,cpx,cpy);
+lab[13]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+cpx+=bsx+spx;
+lab[14] = new PILabel(this,"Max",bsx,bsy,cpx,cpy);
+lab[14]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+if(npar>0) {
+  for(int i=0;i<npar;i++) {
+    cpx=spx; cpy+=bsy+spy;
+    char str[8]; sprintf(str,"P%d",i);
+    lab[20+i] = new PILabel(this,str,bsx/2,bsy,cpx,cpy);
+    lab[20+i]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+    cpx+=bsx/2+spx;
+    // Attention pas de message entre 1500 et 2000 (permet 500 parametres!)
+    ckb[20+i] = new PICheckBox(this,"",1500+i,bsx/3,bsy,cpx,cpy);
+    ckb[20+i]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+    cpx+=bsx/3+spx;
+    for(int j=0;j<4;j++) {
+      txt[20+4*i+j] = new PIText(this,"",bsx,bsy,cpx,cpy);
+      txt[20+4*i+j]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
+      cpx+=bsx+spx;
+    }
+  }
+}
+
+return;
 }
 
@@ -682,47 +945,12 @@
 {
 int i;
-for(i=0; i<2; i++) {
-  delete pom[i];
-  delete ckb[i];
-  delete but[i];
-  }
-delete labn;
-delete lab[0];
-delete lab[1];
-for(i=0; i< FINPARMAX; i++) {
-  delete labp[i];
-  delete txtp[i];
-  delete txts[i];
-  }
-}
-
-/* --Methode-- */
-void PIAFitterWind::SetNbParms(int npar)
-{
-if (npar < 1) npar = 1;
-if (npar > FINPARMAX) npar = FINPARMAX;
-
-int bsx, bsy, spx, spy;
-
-dap->PrefCompSz(bsx, bsy);
-spx = bsx/10;
-spy = bsy/4;
-
-int wszx = 3*spx+4*bsx; 
-int wszy = (5+npar)*bsy+(6+npar)*spy;
-SetSize(wszx, wszy);
-
-int i;
-for(i=0; i<npar; i++) {
-  labp[i]->Manage();
-  txtp[i]->Manage();
-  txts[i]->Manage();
-  }
-for(i=npar; i<FINPARMAX; i++) {
-  labp[i]->UnManage();
-  txtp[i]->UnManage();
-  txts[i]->UnManage();
-  }
-
+for(i=0;i<nlab;i++) if(lab[i]) {delete lab[i]; lab[i]=NULL;}
+for(i=0;i<ntxt;i++) if(txt[i]) {delete txt[i]; txt[i]=NULL;}
+for(i=0;i<nbut;i++) if(but[i]) {delete but[i]; but[i]=NULL;}
+for(i=0;i<nckb;i++) if(ckb[i]) {delete ckb[i]; ckb[i]=NULL;}
+if(lab) {delete lab; lab=NULL; nlab=0;}
+if(txt) {delete txt; txt=NULL; ntxt=0;}
+if(but) {delete but; but=NULL; nbut=0;}
+if(ckb) {delete ckb; ckb=NULL; nckb=0;}
 }
 
@@ -730,9 +958,48 @@
 void PIAFitterWind::Show()
 {
-// Si on veut initialiser des trucs au moment ou apparait la fenetre
-labn->SetLabel(oname);
-SetNbParms(3);
-pom[1]->SetValue(203);
+//mDap->SetBlocked();
+SetState();
 PIWindow::Show();
+return;
+}
+
+/* --Methode-- */
+void PIAFitterWind::SetState(void)
+// Ecriture et positionnement des ckb et txt
+{
+int npar = mFitter->mNPar;
+
+// Les check-boxes
+ckb[0]->SetState(mFitter->mOpt.okfun);
+ckb[1]->SetState(mFitter->mOpt.okres);
+ckb[2]->SetState((mFitter->mOpt.polcx>0)?true:false);
+ckb[3]->SetState((mFitter->mOpt.polcy>0)?true:false);
+if(npar>0)
+  for(int i=0;i<npar;i++) ckb[20+i]->SetState((mFitter->mFix(i)>=1.)?true:false);
+
+// Les champs textes
+char str[128]; string dum;
+sprintf(str,"%f",mFitter->mOpt.xc);    dum=str; txt[0]->SetText(dum);
+sprintf(str,"%f",mFitter->mOpt.yc);    dum=str; txt[1]->SetText(dum);
+sprintf(str,"%f",mFitter->mOpt.err_e); dum=str; txt[2]->SetText(dum);
+sprintf(str,"%f",mFitter->mOpt.err_E); dum=str; txt[3]->SetText(dum);
+sprintf(str,"%f",mFitter->mOpt.stc2);  dum=str; txt[4]->SetText(dum);
+sprintf(str,"%d",mFitter->mOpt.nstep); dum=str; txt[5]->SetText(dum);
+sprintf(str,"%d",mFitter->mOpt.lp);    dum=str; txt[6]->SetText(dum);
+sprintf(str,"%d",mFitter->mOpt.lpg);   dum=str; txt[7]->SetText(dum);
+sprintf(str,"%d",mFitter->mOpt.i1);    dum=str; txt[8]->SetText(dum);
+sprintf(str,"%d",mFitter->mOpt.i2);    dum=str; txt[9]->SetText(dum);
+sprintf(str,"%d",mFitter->mOpt.j1);    dum=str; txt[10]->SetText(dum);
+sprintf(str,"%d",mFitter->mOpt.j2);    dum=str; txt[11]->SetText(dum);
+if(npar>0) {
+  for(int i=0;i<npar;i++) {
+    sprintf(str,"%f",mFitter->mPar(i));  dum=str; txt[20+4*i+0]->SetText(dum);
+    sprintf(str,"%f",mFitter->mStep(i)); dum=str; txt[20+4*i+1]->SetText(dum);
+    sprintf(str,"%f",mFitter->mMin(i));  dum=str; txt[20+4*i+2]->SetText(dum);
+    sprintf(str,"%f",mFitter->mMax(i));  dum=str; txt[20+4*i+3]->SetText(dum);
+  }
+}
+
+return;
 }
 
@@ -740,38 +1007,141 @@
 void PIAFitterWind::Process(PIMessage msg, PIMsgHandler* sender, void* data)
 {
-
-char *mf[7] = {"p0","p1","p2","g","g0","g1","g2"};
+int lp = 1;
+int npar = mFitter->mNPar;
+
+// L'objet existe t-il encore?
+NamedObjMgr omg;
+if(omg.GetObj(mFitter->mNObj) == NULL) {
+  cout<<"PIAFitterWind::Process Error , Pas d'objet de nom "<<mFitter->mNObj<<endl;
+  mFitter->ResetDPointer(); return;
+}
+
+// On recupere les champs textes
+string dum; char str[128];
+int lenxc=0, lenyc=0;
+dum=txt[0]->GetText(); strcpy(str,dum.c_str()); strip(str,'B',' ');
+  sscanf(str,"%lf",&(mFitter->mOpt.xc)); lenxc = strlen(str);
+dum=txt[1]->GetText(); strcpy(str,dum.c_str()); strip(str,'B',' ');
+  sscanf(str,"%lf",&(mFitter->mOpt.yc)); lenyc = strlen(str);
+dum=txt[2]->GetText(); strcpy(str,dum.c_str()); strip(str,'B',' ');
+  sscanf(str,"%lf",&(mFitter->mOpt.err_e));
+dum=txt[3]->GetText(); strcpy(str,dum.c_str()); strip(str,'B',' ');
+  sscanf(str,"%lf",&(mFitter->mOpt.err_E));
+dum=txt[4]->GetText(); strcpy(str,dum.c_str()); strip(str,'B',' ');
+  sscanf(str,"%lf",&(mFitter->mOpt.stc2));
+dum=txt[5]->GetText(); strcpy(str,dum.c_str()); strip(str,'B',' ');
+  sscanf(str,"%d",&(mFitter->mOpt.nstep));
+dum=txt[6]->GetText(); strcpy(str,dum.c_str()); strip(str,'B',' ');
+  sscanf(str,"%d",&(mFitter->mOpt.lp));
+dum=txt[7]->GetText(); strcpy(str,dum.c_str()); strip(str,'B',' ');
+  sscanf(str,"%d",&(mFitter->mOpt.lpg));
+dum=txt[8]->GetText(); strcpy(str,dum.c_str()); strip(str,'B',' ');
+  sscanf(str,"%d",&(mFitter->mOpt.i1));
+dum=txt[9]->GetText(); strcpy(str,dum.c_str()); strip(str,'B',' ');
+  sscanf(str,"%d",&(mFitter->mOpt.i2));
+dum=txt[10]->GetText(); strcpy(str,dum.c_str()); strip(str,'B',' ');
+sscanf(str,"%d",&(mFitter->mOpt.j1));
+dum=txt[11]->GetText(); strcpy(str,dum.c_str()); strip(str,'B',' ');
+  sscanf(str,"%d",&(mFitter->mOpt.j2));
+if(npar>0) {
+  for(int i=0;i<npar;i++) {
+    dum=txt[20+4*i+0]->GetText(); strcpy(str,dum.c_str()); strip(str,'B',' ');
+      sscanf(str,"%lf",&(mFitter->mPar(i)));
+    dum=txt[20+4*i+1]->GetText(); strcpy(str,dum.c_str()); strip(str,'B',' ');
+      sscanf(str,"%lf",&(mFitter->mStep(i)));
+    dum=txt[20+4*i+2]->GetText(); strcpy(str,dum.c_str()); strip(str,'B',' ');
+      sscanf(str,"%lf",&(mFitter->mMin(i)));
+    dum=txt[20+4*i+3]->GetText(); strcpy(str,dum.c_str()); strip(str,'B',' ');
+      sscanf(str,"%lf",&(mFitter->mMax(i)));
+  }
+}
+
+// Decodage des messages
 msg = UserMsg(msg);
-
-if ((msg > 200) && (msg <= 200+FINPARMAX)) {  // On change le nombre de parametre
-  SetNbParms(msg-200);
+if(msg ==777) {
+  if(lp) cout<<"Dismiss"<<endl;
+  //mDap->SetReady();
+  Hide();
   return;
-  }
-  
-if (msg == 777)  { Hide(); return; }  // On cache la fenetre
-
-if (msg == 555) {  // On fait le fit 
-  vector<string> args; 
-  string cmd = oname; 
-  string a;
-  args.push_back(oname);
-  int imf = pom[0]->GetValue()-100;
-  if ((imf < 0) || (imf > 6)) imf = 0;
-  a = mf[imf];
-  cmd += " " + a + " ";
-  args.push_back(a);
-  cmd += a;
-  args.push_back(a);
-  if (ckb[0]->GetState() || ckb[1]->GetState()) {
-    a = "o:";
-    if (ckb[0]->GetState()) a += "f"; 
-    if (ckb[1]->GetState()) a += "r";
-    cmd += " " + a;
-    args.push_back(a); 
-    }
-  cout << "FitComm= fit " << cmd << endl;
-  string kw = "fit";
-  fitter->Execute(kw, args);
-  }
-
-}
+}
+if(msg ==666) {
+  if(lp) cout<<"Refresh"<<endl;
+  if(ReFillGData) mFitter->FillGData();
+  ReFillGData = false;
+}
+if(msg ==111) {
+  if(lp) cout<<"Update from last"<<endl;
+  mFitter->ResetOptions();
+  mFitter->mOpt = mFitter->mOptSave;
+  mFitter->ReSetParam();
+  mFitter->InitParFromLastFit();
+  ReFillGData = true;
+}
+if(msg ==222) {
+  if(lp) cout<<"Default options/par"<<endl;
+  mFitter->ResetOptions();
+  mFitter->ReSetParam();
+  ReFillGData = true;
+}
+if(msg ==333) {
+  if(lp) cout<<"Do the fit"<<endl;
+  if(ReFillGData) mFitter->FillGData();
+  ReFillGData = false;
+  int rc = mFitter->DoFit();
+  if(rc>=0) mFitter->FitFunRes();
+}
+if(msg==1001) {
+  if(ckb[0]->GetState()) {
+    if(lp) cout<<"Gen fitted function"<<endl;
+    mFitter->mOpt.okfun = true;
+  } else if(!ckb[0]->GetState()) {
+    if(lp) cout<<"No Gen fitted function"<<endl;
+    mFitter->mOpt.okfun = false;
+  }
+}
+if(msg==1002) {
+  if(ckb[1]->GetState()) {
+    if(lp) cout<<"Gen fitted residus"<<endl;
+    mFitter->mOpt.okres = true;
+  } else if(!ckb[1]->GetState()) {
+    if(lp) cout<<"No Gen fitted residus"<<endl;
+    mFitter->mOpt.okres = false;
+  }
+}
+if(msg==1003) {
+  if(ckb[2]->GetState()) {
+    if(lp) cout<<"Centrage X"<<endl;
+    if(lenxc>0) mFitter->mOpt.polcx = 1; else mFitter->mOpt.polcx = 2;
+  } else if(!ckb[2]->GetState()) {
+    if(lp) cout<<"No Centrage X"<<endl;
+    mFitter->mOpt.polcx = 0;
+  }
+  ReFillGData = true;
+}
+if(msg==1004) {
+  if(ckb[3]->GetState()) {
+    if(lp) cout<<"Centrage Y"<<endl;
+    if(lenyc>0) mFitter->mOpt.polcy = 1; else mFitter->mOpt.polcy = 2;
+  } else if(!ckb[3]->GetState()) {
+    if(lp) cout<<"No Centrage Y"<<endl;
+    mFitter->mOpt.polcy = 0;
+  }
+  ReFillGData = true;
+}
+if(msg>=1500 && msg<2000) {
+  int ip = msg-1500;
+  if(ckb[20+ip]->GetState()) {
+    if(lp) cout<<"Fix Param "<<ip<<endl;
+    mFitter->mFix(ip) = 1.001;
+  } else if(!ckb[20+ip]->GetState()) {
+    if(lp) cout<<"Not Fix Param "<<ip<<endl;
+    mFitter->mFix(ip) = 0.;
+  }
+}
+
+mFitter->CheckOptions();
+SetState();
+if(mFitter->mOpt.lp>1) mFitter->PrintOptions();
+
+return;
+}
