Index: trunk/SophyaLib/HiStats/histos2.cc
===================================================================
--- trunk/SophyaLib/HiStats/histos2.cc	(revision 3053)
+++ trunk/SophyaLib/HiStats/histos2.cc	(revision 3057)
@@ -153,14 +153,17 @@
 void Histo2D::CreateOrResize(r_8 xMin,r_8 xMax,int_4 nxBin,r_8 yMin,r_8 yMax,int_4 nyBin)
 {
- int_8 n = nxBin*nyBin;
- if(mData!=NULL) {delete[] mData; mData = NULL;}
- if(n>0) {mData = new r_8[n]; memset(mData, 0, n*sizeof(r_8));}
- mNx = nxBin; mNy = nyBin; mNxy = nxBin*nyBin;
- mXmin = xMin; mXmax = xMax; mYmin = yMin; mYmax = yMax;
- mWBinx = (nxBin>0) ? (xMax - xMin)/nxBin: 0.;
- mWBiny = (nyBin>0) ? (yMax - yMin)/nyBin: 0.;
- nHist = 0; nEntries = 0;
- for(int_4 i=0;i<3;i++) for(int_4 j=0;j<3;j++) mOver[i][j]=0.;
- mB_s.H = NULL;
+  int_8 n = nxBin*nyBin;
+  bool samelen = (n==mNx*mNy)? true: false;
+  if(mData!=NULL && !samelen) {delete[] mData; mData=NULL;}
+  if(mErr2!=NULL) {delete[] mErr2; mErr2=NULL;} // On des-alloue toujours
+  if(n>0 && mData==NULL) mData = new r_8[n];
+  if(mData) memset(mData, 0, n*sizeof(r_8));
+  mNx = nxBin; mNy = nyBin; mNxy = nxBin*nyBin;
+  mXmin = xMin; mXmax = xMax; mYmin = yMin; mYmax = yMax;
+  mWBinx = (nxBin>0) ? (xMax - xMin)/nxBin: 0.;
+  mWBiny = (nyBin>0) ? (yMax - yMin)/nyBin: 0.;
+  nHist = 0; nEntries = 0;
+  for(int_4 i=0;i<3;i++) for(int_4 j=0;j<3;j++) mOver[i][j]=0.;
+  mB_s.H = NULL;
 }
 
@@ -230,22 +233,12 @@
 Histo2D& Histo2D::operator = (const Histo2D& h)
 {
-  int_4 i,j,nb;
-  r_8 min,max;
-
   if(this == &h) return *this;
-  if( h.mNxy > mNxy ) Delete();
-  if(!mData) mData = new r_8[h.mNxy];
-  if( !h.mErr2 && mErr2 ) { delete [] mErr2; mErr2=NULL;}
-  if( h.mErr2 && !mErr2 ) mErr2 = new r_8[h.mNxy];
-
-  for(i=0;i<3;i++) for(j=0;j<3;j++) mOver[i][j] = h.mOver[i][j];
-  nHist = h.nHist;
-  nEntries = h.nEntries;
-  mNx = h.mNx;  mNy = h.mNy;  mNxy = h.mNxy;
-  mXmin = h.mXmin; mXmax = h.mXmax; mWBinx = h.mWBinx;
-  mYmin = h.mYmin; mYmax = h.mYmax; mWBiny = h.mWBiny;
-  
-  memcpy(mData, h.mData, mNxy*sizeof(r_8));
+  CreateOrResize(h.mXmin,h.mXmax,h.mNx,h.mYmin,h.mYmax,h.mNy);
+  if(h.mErr2) Errors();
+  if(mData) memcpy(mData, h.mData, mNxy*sizeof(r_8));
   if(mErr2) memcpy(mErr2, h.mErr2, mNxy*sizeof(r_8));
+
+  for(int i=0;i<3;i++) for(int j=0;j<3;j++) mOver[i][j] = h.mOver[i][j];
+  nHist = h.nHist;  nEntries = h.nEntries;  
 
   DelProjX();
@@ -260,9 +253,10 @@
   }
 
+  int_4 nb;
   DelSliX();
   nb = h.NSliX();
   if(nb>0) {
     SetSliX(nb);
-    for(i=0; i<NSliX();i++) *HSliX(i) = *(h.HSliX(i));
+    for(int i=0; i<NSliX();i++) *HSliX(i) = *(h.HSliX(i));
   }
   DelSliY();
@@ -270,5 +264,5 @@
   if(nb>0) {
     SetSliY(nb);
-    for(i=0; i<NSliY();i++) *HSliY(i) = *(h.HSliY(i));
+    for(int i=0; i<NSliY();i++) *HSliY(i) = *(h.HSliY(i));
   }
 
@@ -276,20 +270,22 @@
   nb = h.NBandX();
   if(nb>0) {
-    for(i=0; i<nb;i++) {
+    for(int i=0; i<nb;i++) {
+      r_8 min,max;
       h.GetBandX(i,min,max);
       SetBandX(min,max);
       *HBandX(i) = *(h.HBandX(i));
     }
-    for(i=0; i<NBandX();i++) *HBandX(i) = *(h.HBandX(i));
+    for(int i=0; i<NBandX();i++) *HBandX(i) = *(h.HBandX(i));
   }
   DelBandY();
   nb = h.NBandY();
   if(nb>0) {
-    for(i=0; i<nb;i++) {
+    for(int i=0; i<nb;i++) {
+      r_8 min,max;
       h.GetBandY(i,min,max);
       SetBandY(min,max);
       *HBandY(i) = *(h.HBandY(i));
     }
-    for(i=0; i<NBandY();i++) *HBandY(i) = *(h.HBandY(i));
+    for(int i=0; i<NBandY();i++) *HBandY(i) = *(h.HBandY(i));
   }
 
@@ -695,4 +691,5 @@
 void Histo2D::IJMax(int_4& imax,int_4& jmax,int_4 il,int_4 ih,int_4 jl,int_4 jh) const
 {
+if(mNxy<=0) {imax = jmax = -1; return;}
 if( il > ih ) { il = 0; ih = mNx-1; }
 if( jl > jh ) { jl = 0; jh = mNy-1; }
@@ -716,4 +713,5 @@
 void Histo2D::IJMin(int_4& imax,int_4& jmax,int_4 il,int_4 ih,int_4 jl,int_4 jh) const
 {
+if(mNxy<=0) {imax = jmax = -1; return;}
 if( il > ih ) { il = 0; ih = mNx-1; }
 if( jl > jh ) { jl = 0; jh = mNy-1; }
@@ -738,4 +736,5 @@
 r_8 Histo2D::VMax(int_4 il,int_4 ih,int_4 jl,int_4 jh) const
 {
+if(mNxy<=0) return 0.;
 if( il > ih ) { il = 0; ih = mNx-1; }
 if( jl > jh ) { jl = 0; jh = mNy-1; }
@@ -758,4 +757,5 @@
 r_8 Histo2D::VMin(int_4 il,int_4 ih,int_4 jl,int_4 jh) const
 {
+if(mNxy<=0) return 0.;
 if( il > ih ) { il = 0; ih = mNx-1; }
 if( jl > jh ) { jl = 0; jh = mNy-1; }
@@ -801,4 +801,5 @@
 int_4 Histo2D::BinNonNul() const
 {
+if(mNxy<=0) return -1;
 int_4 non=0;
 for (int_4 i=0;i<mNxy;i++) if( mData[i] != 0. ) non++;
@@ -899,4 +900,5 @@
                     ,int_4 il,int_4 ih,int_4 jl,int_4 jh) const
 {
+if(mNxy<=0) return -1;
 if( il > ih ) { il = 0; ih = mNx-1; }
 if( jl > jh ) { jl = 0; jh = mNy-1; }
@@ -969,4 +971,5 @@
                    ,int_4 il,int_4 ih,int_4 jl,int_4 jh) const
 {
+if(mNxy<=0) return;
 int_4 ns = 35;
 const char *s =    "+23456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
