From 0eb655ba752037c8476449ea70c9d026b99bd6cf Mon Sep 17 00:00:00 2001
From: Lorenzo Moneta <Lorenzo.Moneta@cern.ch>
Date: Thu, 21 Jul 2011 11:24:40 +0000
Subject: [PATCH] aaply patch from Andrei to have a lazy allocation of the
 TArray[x] of histograms and profiles. The allocation is done only if a
 SetBinContent() or AddBinContent() is called. This feature can be activated
 via the static TH1::SetLazyAllocation(kTRUE)

git-svn-id: http://root.cern.ch/svn/root/trunk@40319 27541ba8-7e3a-0410-8455-c3a389f83636
---
 hist/hist/inc/TH1.h          |  14 ++---
 hist/hist/inc/TH2.h          |  10 ++-
 hist/hist/inc/TH3.h          |  11 ++--
 hist/hist/inc/TProfile.h     |   6 ++
 hist/hist/inc/TProfile2D.h   |   6 ++
 hist/hist/inc/TProfile3D.h   |   6 ++
 hist/hist/src/TH1.cxx        | 117 +++++++++++++++++++++++++++-------
 hist/hist/src/TH2.cxx        | 118 +++++++++++++++++++++++++----------
 hist/hist/src/TH3.cxx        |  95 +++++++++++++++++++++-------
 hist/hist/src/TProfile.cxx   |  49 ++++++++++++++-
 hist/hist/src/TProfile2D.cxx |  48 +++++++++++++-
 hist/hist/src/TProfile3D.cxx |  48 +++++++++++++-
 12 files changed, 424 insertions(+), 104 deletions(-)

diff --git a/hist/hist/inc/TH1.h b/hist/hist/inc/TH1.h
index 3ea62ef91f0..70d3b2a4f40 100644
--- a/hist/hist/inc/TH1.h
+++ b/hist/hist/inc/TH1.h
@@ -105,6 +105,7 @@ protected:
     static Bool_t fgAddDirectory;   //!flag to add histograms to the directory
     static Bool_t fgStatOverflows;  //!flag to use under/overflows in statistics
     static Bool_t fgDefaultSumw2;   //!flag to call TH1::Sumw2 automatically at histogram creation time
+    static Bool_t fgLazyAllocation; //!flag to allocate the histogram array only on the first fill
 
 public:
    static Int_t FitOptionsMake(Option_t *option, Foption_t &Foption);
@@ -295,6 +296,7 @@ public:
    virtual void     LabelsDeflate(Option_t *axis="X");
    virtual void     LabelsInflate(Option_t *axis="X");
    virtual void     LabelsOption(Option_t *option="h", Option_t *axis="X");
+   static Bool_t    LazyAllocationStatus();
    virtual Long64_t Merge(TCollection *list);
    virtual void     Multiply(TF1 *h1, Double_t c1=1);
    virtual void     Multiply(const TH1 *h1);
@@ -344,7 +346,7 @@ public:
    virtual void     SetLabelFont(Style_t font=62, Option_t *axis="X");
    virtual void     SetLabelOffset(Float_t offset=0.005, Option_t *axis="X");
    virtual void     SetLabelSize(Float_t size=0.02, Option_t *axis="X");
-
+   static void      SetLazyAllocation(Bool_t flag=kTRUE);
    virtual void     SetMaximum(Double_t maximum=-1111); // *MENU*
    virtual void     SetMinimum(Double_t minimum=-1111); // *MENU*
    virtual void     SetName(const char *name); // *MENU*
@@ -515,9 +517,8 @@ public:
    TH1F(const TH1F &h1f);
    virtual ~TH1F();
 
-   virtual void     AddBinContent(Int_t bin) {++fArray[bin];}
-   virtual void     AddBinContent(Int_t bin, Double_t w)
-                                 {fArray[bin] += Float_t (w);}
+   virtual void     AddBinContent(Int_t bin);
+   virtual void     AddBinContent(Int_t bin, Double_t w);
    virtual void     Copy(TObject &hnew) const;
    virtual TH1     *DrawCopy(Option_t *option="") const;
    virtual Double_t GetBinContent(Int_t bin) const;
@@ -560,9 +561,8 @@ public:
    TH1D(const TH1D &h1d);
    virtual ~TH1D();
 
-   virtual void     AddBinContent(Int_t bin) {++fArray[bin];}
-   virtual void     AddBinContent(Int_t bin, Double_t w)
-                                 {fArray[bin] += Double_t (w);}
+   virtual void     AddBinContent(Int_t bin);
+   virtual void     AddBinContent(Int_t bin, Double_t w);
    virtual void     Copy(TObject &hnew) const;
    virtual TH1     *DrawCopy(Option_t *option="") const;
    virtual Double_t GetBinContent(Int_t bin) const;
diff --git a/hist/hist/inc/TH2.h b/hist/hist/inc/TH2.h
index ca9ea158fb1..f69889d2464 100644
--- a/hist/hist/inc/TH2.h
+++ b/hist/hist/inc/TH2.h
@@ -260,9 +260,8 @@ public:
    TH2F(const TMatrixFBase &m);
    TH2F(const TH2F &h2f);
    virtual ~TH2F();
-   virtual void     AddBinContent(Int_t bin) {++fArray[bin];}
-   virtual void     AddBinContent(Int_t bin, Double_t w)
-                                 {fArray[bin] += Float_t (w);}
+   virtual void     AddBinContent(Int_t bin);
+   virtual void     AddBinContent(Int_t bin, Double_t w);
    virtual void     Copy(TObject &hnew) const;
    virtual TH1     *DrawCopy(Option_t *option="") const;
    virtual Double_t GetBinContent(Int_t bin) const;
@@ -304,9 +303,8 @@ public:
    TH2D(const TMatrixDBase &m);
    TH2D(const TH2D &h2d);
    virtual ~TH2D();
-   virtual void     AddBinContent(Int_t bin) {++fArray[bin];}
-   virtual void     AddBinContent(Int_t bin, Double_t w)
-                                 {fArray[bin] += Double_t (w);}
+   virtual void     AddBinContent(Int_t bin);
+   virtual void     AddBinContent(Int_t bin, Double_t w);
    virtual void     Copy(TObject &hnew) const;
    virtual TH1     *DrawCopy(Option_t *option="") const;
    virtual Double_t GetBinContent(Int_t bin) const;
diff --git a/hist/hist/inc/TH3.h b/hist/hist/inc/TH3.h
index 14babe2ec4c..a66113a56b7 100644
--- a/hist/hist/inc/TH3.h
+++ b/hist/hist/inc/TH3.h
@@ -62,6 +62,7 @@ protected:
 public:
    TH3(const TH3&);
    virtual ~TH3();
+   
    virtual Int_t    BufferEmpty(Int_t action=0);
    virtual void     Copy(TObject &hnew) const;
            Int_t    Fill(Double_t) {return -1;}        //MayNotUse
@@ -264,9 +265,8 @@ public:
                                           ,Int_t nbinsz,const Double_t *zbins);
    TH3F(const TH3F &h3f);
    virtual ~TH3F();
-   virtual void      AddBinContent(Int_t bin) {++fArray[bin];}
-   virtual void      AddBinContent(Int_t bin, Double_t w)
-                                 {fArray[bin] += Float_t (w);}
+   virtual void      AddBinContent(Int_t bin);
+   virtual void      AddBinContent(Int_t bin, Double_t w);
    virtual void      Copy(TObject &hnew) const;
    virtual TH1      *DrawCopy(Option_t *option="") const;
    virtual Double_t  GetBinContent(Int_t bin) const;
@@ -304,9 +304,8 @@ public:
                                           ,Int_t nbinsz,const Double_t *zbins);
    TH3D(const TH3D &h3d);
    virtual ~TH3D();
-   virtual void      AddBinContent(Int_t bin) {++fArray[bin];}
-   virtual void      AddBinContent(Int_t bin, Double_t w)
-                                 {fArray[bin] += Double_t (w);}
+   virtual void      AddBinContent(Int_t bin);
+   virtual void      AddBinContent(Int_t bin, Double_t w);
    virtual void      Copy(TObject &hnew) const;
    virtual TH1      *DrawCopy(Option_t *option="") const;
    virtual Double_t  GetBinContent(Int_t bin) const;
diff --git a/hist/hist/inc/TProfile.h b/hist/hist/inc/TProfile.h
index 2116286806d..190e8f1316e 100644
--- a/hist/hist/inc/TProfile.h
+++ b/hist/hist/inc/TProfile.h
@@ -83,6 +83,9 @@ public:
    virtual void     Add(TF1 *h1, Double_t c1=1, Option_t *option="");
    virtual void     Add(const TH1 *h1, Double_t c1=1);
    virtual void     Add(const TH1 *h1, const TH1 *h2, Double_t c1=1, Double_t c2=1); // *MENU*
+   virtual void     AddBinContent(Int_t bin);
+   virtual void     AddBinContent(Int_t bin, Double_t w);
+           void     AllocateIfEmpty();
    static  void     Approximate(Bool_t approx=kTRUE);
    virtual Int_t    BufferEmpty(Int_t action=0);
            void     BuildOptions(Double_t ymin, Double_t ymax, Option_t *option);
@@ -125,6 +128,9 @@ public:
    virtual void     Reset(Option_t *option="");
    virtual void     SavePrimitive(ostream &out, Option_t *option = "");
    virtual void     Scale(Double_t c1=1, Option_t *option="");
+   virtual void     SetBinContent(Int_t bin, Double_t content);
+   virtual void     SetBinContent(Int_t bin, Int_t, Double_t content) {SetBinContent(bin,content);}
+   virtual void     SetBinContent(Int_t bin, Int_t, Int_t, Double_t content) {SetBinContent(bin,content);}
    virtual void     SetBinEntries(Int_t bin, Double_t w);
    virtual void     SetBins(Int_t nbins, Double_t xmin, Double_t xmax);
    virtual void     SetBins(Int_t nx, const Double_t *xbins);
diff --git a/hist/hist/inc/TProfile2D.h b/hist/hist/inc/TProfile2D.h
index 971cbc078d8..4cba76aa182 100644
--- a/hist/hist/inc/TProfile2D.h
+++ b/hist/hist/inc/TProfile2D.h
@@ -87,6 +87,9 @@ public:
    virtual void      Add(TF1 *h1, Double_t c1=1, Option_t *option="");
    virtual void      Add(const TH1 *h1, Double_t c1=1);
    virtual void      Add(const TH1 *h1, const TH1 *h2, Double_t c1=1, Double_t c2=1); // *MENU*
+   virtual void      AddBinContent(Int_t bin);
+   virtual void      AddBinContent(Int_t bin, Double_t w);
+           void      AllocateIfEmpty();
    static  void      Approximate(Bool_t approx=kTRUE);
    void              BuildOptions(Double_t zmin, Double_t zmax, Option_t *option);
    virtual Int_t     BufferEmpty(Int_t action=0);
@@ -133,6 +136,9 @@ public:
    virtual TProfile2D *RebinY(Int_t ngroup=2, const char *newname="");     
    virtual void      SavePrimitive(ostream &out, Option_t *option = "");
    virtual void      Scale(Double_t c1=1, Option_t *option="");
+   virtual void      SetBinContent(Int_t bin, Double_t content);
+   virtual void      SetBinContent(Int_t binx, Int_t biny, Double_t content) {SetBinContent(GetBin(binx,biny),content);}
+   virtual void      SetBinContent(Int_t binx, Int_t biny, Int_t, Double_t content) {SetBinContent(GetBin(binx,biny),content);}
    virtual void      SetBinEntries(Int_t bin, Double_t w);
    virtual void      SetBins(Int_t nbinsx, Double_t xmin, Double_t xmax, Int_t nbinsy, Double_t ymin, Double_t ymax);
    virtual void      SetBuffer(Int_t buffersize, Option_t *option="");
diff --git a/hist/hist/inc/TProfile3D.h b/hist/hist/inc/TProfile3D.h
index 2fb42cfe48b..ba61d7a6e51 100644
--- a/hist/hist/inc/TProfile3D.h
+++ b/hist/hist/inc/TProfile3D.h
@@ -84,6 +84,9 @@ public:
    virtual void      Add(TF1 *h1, Double_t c1=1, Option_t *option="");
    virtual void      Add(const TH1 *h1, Double_t c1=1);
    virtual void      Add(const TH1 *h1, const TH1 *h2, Double_t c1=1, Double_t c2=1); // *MENU*
+   virtual void      AddBinContent(Int_t bin);
+   virtual void      AddBinContent(Int_t bin, Double_t w);
+           void      AllocateIfEmpty();
    static  void      Approximate(Bool_t approx=kTRUE);
    void              BuildOptions(Double_t tmin, Double_t tmax, Option_t *option);
    virtual Int_t     BufferEmpty(Int_t action=0);
@@ -130,6 +133,9 @@ public:
    virtual void      RebinAxis(Double_t x, TAxis *axis);
    virtual void      SavePrimitive(ostream &out, Option_t *option = "");
    virtual void      Scale(Double_t c1=1, Option_t *option="");
+   virtual void      SetBinContent(Int_t bin, Double_t content);
+   virtual void      SetBinContent(Int_t bin, Int_t, Double_t content) {SetBinContent(bin,content);}
+   virtual void      SetBinContent(Int_t bin, Int_t, Int_t, Double_t content) {SetBinContent(bin,content);}
    virtual void      SetBinEntries(Int_t bin, Double_t w);
    virtual void      SetBins(Int_t nbinsx, Double_t xmin, Double_t xmax, 
                              Int_t nbinsy, Double_t ymin, Double_t ymax, 
diff --git a/hist/hist/src/TH1.cxx b/hist/hist/src/TH1.cxx
index 4323ae7d180..9a6d0013bd9 100644
--- a/hist/hist/src/TH1.cxx
+++ b/hist/hist/src/TH1.cxx
@@ -525,6 +525,7 @@ Int_t  TH1::fgBufferSize   = 1000;
 Bool_t TH1::fgAddDirectory = kTRUE;
 Bool_t TH1::fgDefaultSumw2 = kFALSE;
 Bool_t TH1::fgStatOverflows= kFALSE;
+Bool_t TH1::fgLazyAllocation = kFALSE;
 
 extern void H1InitGaus();
 extern void H1InitExpo();
@@ -858,7 +859,7 @@ void TH1::Add(const TH1 *h1, Double_t c1)
       Error("Add","Attempt to add a non-existing histogram");
       return;
    }
-
+   
    // delete buffer if it is there since it will become invalid
    if (fBuffer) BufferEmpty(1);
 
@@ -1738,7 +1739,6 @@ Double_t TH1::Chi2TestX(const TH1* h2,  Double_t &chi2, Int_t &ndf, Int_t &igood
    Double_t sum1=0, sum2=0;
    Double_t sumw1=0, sumw2=0;
 
-
    chi2 = 0;
    ndf = 0;
 
@@ -2201,7 +2201,7 @@ Double_t TH1::ComputeIntegral()
 
    // delete previously computed integral (if any)
    if (fIntegral) delete [] fIntegral;
-
+   
    //   - Allocate space to store the integral and compute integral
    Int_t nbinsx = GetNbinsX();
    Int_t nbinsy = GetNbinsY();
@@ -2363,7 +2363,7 @@ void TH1::Divide(TF1 *f1, Double_t c1)
       Error("Add","Attempt to divide by a non-existing function");
       return;
    }
-
+   
    // delete buffer if it is there since it will become invalid
    if (fBuffer) BufferEmpty(1);
 
@@ -4845,6 +4845,13 @@ void TH1::LabelsOption(Option_t *option, Option_t *ax)
    if (errors) delete [] errors;
 }
 
+//______________________________________________________________________________
+Bool_t TH1::LazyAllocationStatus()
+{
+// Returns lazy histogram array allocation flag.
+   return fgLazyAllocation;
+}
+   
 //______________________________________________________________________________
 static Bool_t AlmostEqual(Double_t a, Double_t b, Double_t epsilon = 0.00000001)
 {
@@ -5812,6 +5819,14 @@ void TH1::SetDefaultSumw2(Bool_t sumw2)
    fgDefaultSumw2 = sumw2;
 }
 
+//______________________________________________________________________________
+void TH1::SetLazyAllocation(Bool_t flag)
+{
+// Enable/disable lazy histogram array allocation. If this is on, the array
+// is allocated during the first Fill of the histogram.
+   fgLazyAllocation = flag;
+}
+   
 //______________________________________________________________________________
 void TH1::SetTitle(const char *title)
 {
@@ -8210,7 +8225,7 @@ TH1C::TH1C(const char *name,const char *title,Int_t nbins,Double_t xlow,Double_t
    //                    (see TH1::TH1 for explanation of parameters)
    //
    fDimension = 1;
-   TArrayC::Set(fNcells);
+   if (!fgLazyAllocation) TArrayC::Set(fNcells);
 
    if (xlow >= xup) SetBuffer(fgBufferSize);
    if (fgDefaultSumw2) Sumw2();
@@ -8226,7 +8241,7 @@ TH1C::TH1C(const char *name,const char *title,Int_t nbins,const Float_t *xbins)
    //                    (see TH1::TH1 for explanation of parameters)
    //
    fDimension = 1;
-   TArrayC::Set(fNcells);
+   if (!fgLazyAllocation) TArrayC::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -8240,7 +8255,7 @@ TH1C::TH1C(const char *name,const char *title,Int_t nbins,const Double_t *xbins)
    //                    (see TH1::TH1 for explanation of parameters)
    //
    fDimension = 1;
-   TArrayC::Set(fNcells);
+   if (!fgLazyAllocation)  TArrayC::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -8264,6 +8279,7 @@ void TH1C::AddBinContent(Int_t bin)
    //   -*-*-*-*-*-*-*-*Increment bin content by 1*-*-*-*-*-*-*-*-*-*-*-*-*-*
    //                   ==========================
 
+   if (!fN) TArrayC::Set(fNcells);
    if (fArray[bin] < 127) fArray[bin]++;
 }
 
@@ -8273,6 +8289,7 @@ void TH1C::AddBinContent(Int_t bin, Double_t w)
    //   -*-*-*-*-*-*-*-*Increment bin content by w*-*-*-*-*-*-*-*-*-*-*-*-*-*
    //                   ==========================
 
+   if (!fN) TArrayC::Set(fNcells);
    Int_t newval = fArray[bin] + Int_t(w);
    if (newval > -128 && newval < 128) {fArray[bin] = Char_t(newval); return;}
    if (newval < -127) fArray[bin] = -127;
@@ -8307,6 +8324,7 @@ Double_t TH1C::GetBinContent(Int_t bin) const
 {
    // see convention for numbering bins in TH1::GetBin
 
+   if (!fN) return 0.;
    if (fBuffer) ((TH1C*)this)->BufferEmpty();
    if (bin < 0) bin = 0;
    if (bin >= fNcells) bin = fNcells-1;
@@ -8335,6 +8353,7 @@ void TH1C::SetBinContent(Int_t bin, Double_t content)
    fEntries++;
    fTsumw = 0;
    if (bin < 0) return;
+   if (!fN) TArrayC::Set(fNcells);
    if (bin >= fNcells-1) {
       if (fXaxis.GetTimeDisplay()) {
          while (bin >=  fNcells-1)  LabelsInflate();
@@ -8357,7 +8376,7 @@ void TH1C::SetBinsLength(Int_t n)
 
    if (n < 0) n = fXaxis.GetNbins() + 2;
    fNcells = n;
-   TArrayC::Set(n);
+   if (fN>0 && !fgLazyAllocation) TArrayC::Set(n);
 }
 
 //______________________________________________________________________________
@@ -8454,7 +8473,7 @@ TH1S::TH1S(const char *name,const char *title,Int_t nbins,Double_t xlow,Double_t
    //           (see TH1::TH1 for explanation of parameters)
    //
    fDimension = 1;
-   TArrayS::Set(fNcells);
+   if (!fgLazyAllocation) TArrayS::Set(fNcells);
 
    if (xlow >= xup) SetBuffer(fgBufferSize);
    if (fgDefaultSumw2) Sumw2();
@@ -8470,7 +8489,7 @@ TH1S::TH1S(const char *name,const char *title,Int_t nbins,const Float_t *xbins)
    //           (see TH1::TH1 for explanation of parameters)
    //
    fDimension = 1;
-   TArrayS::Set(fNcells);
+   if (!fgLazyAllocation) TArrayS::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -8484,7 +8503,7 @@ TH1S::TH1S(const char *name,const char *title,Int_t nbins,const Double_t *xbins)
    //           (see TH1::TH1 for explanation of parameters)
    //
    fDimension = 1;
-   TArrayS::Set(fNcells);
+   if (!fgLazyAllocation) TArrayS::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -8508,6 +8527,7 @@ void TH1S::AddBinContent(Int_t bin)
    //   -*-*-*-*-*-*-*-*Increment bin content by 1*-*-*-*-*-*-*-*-*-*-*-*-*-*
    //                   ==========================
 
+   if (!fN) TArrayS::Set(fNcells);
    if (fArray[bin] < 32767) fArray[bin]++;
 }
 
@@ -8517,6 +8537,7 @@ void TH1S::AddBinContent(Int_t bin, Double_t w)
    //                   Increment bin content by w
    //                   ==========================
 
+   if (!fN) TArrayS::Set(fNcells);
    Int_t newval = fArray[bin] + Int_t(w);
    if (newval > -32768 && newval < 32768) {fArray[bin] = Short_t(newval); return;}
    if (newval < -32767) fArray[bin] = -32767;
@@ -8550,6 +8571,7 @@ TH1 *TH1S::DrawCopy(Option_t *option) const
 Double_t TH1S::GetBinContent(Int_t bin) const
 {
    // see convention for numbering bins in TH1::GetBin
+   if (!fN) return 0.;
    if (fBuffer) ((TH1S*)this)->BufferEmpty();
    if (bin < 0) bin = 0;
    if (bin >= fNcells) bin = fNcells-1;
@@ -8578,6 +8600,7 @@ void TH1S::SetBinContent(Int_t bin, Double_t content)
    fEntries++;
    fTsumw = 0;
    if (bin < 0) return;
+   if (!fN) TArrayS::Set(fNcells);
    if (bin >= fNcells-1) {
       if (fXaxis.GetTimeDisplay()) {
          while (bin >=  fNcells-1)  LabelsInflate();
@@ -8600,7 +8623,7 @@ void TH1S::SetBinsLength(Int_t n)
 
    if (n < 0) n = fXaxis.GetNbins() + 2;
    fNcells = n;
-   TArrayS::Set(n);
+   if (fN>0 && !fgLazyAllocation) TArrayS::Set(n);
 }
 
 //______________________________________________________________________________
@@ -8696,7 +8719,7 @@ TH1I::TH1I(const char *name,const char *title,Int_t nbins,Double_t xlow,Double_t
    //           (see TH1::TH1 for explanation of parameters)
    //
    fDimension = 1;
-   TArrayI::Set(fNcells);
+   if (!fgLazyAllocation) TArrayI::Set(fNcells);
 
    if (xlow >= xup) SetBuffer(fgBufferSize);
    if (fgDefaultSumw2) Sumw2();
@@ -8712,7 +8735,7 @@ TH1I::TH1I(const char *name,const char *title,Int_t nbins,const Float_t *xbins)
    //           (see TH1::TH1 for explanation of parameters)
    //
    fDimension = 1;
-   TArrayI::Set(fNcells);
+   if (!fgLazyAllocation) TArrayI::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -8726,7 +8749,7 @@ TH1I::TH1I(const char *name,const char *title,Int_t nbins,const Double_t *xbins)
    //           (see TH1::TH1 for explanation of parameters)
    //
    fDimension = 1;
-   TArrayI::Set(fNcells);
+   if (!fgLazyAllocation) TArrayI::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -8750,6 +8773,7 @@ void TH1I::AddBinContent(Int_t bin)
    //   -*-*-*-*-*-*-*-*Increment bin content by 1*-*-*-*-*-*-*-*-*-*-*-*-*-*
    //                   ==========================
 
+   if (!fN) TArrayI::Set(fNcells);
    if (fArray[bin] < 2147483647) fArray[bin]++;
 }
 
@@ -8759,6 +8783,7 @@ void TH1I::AddBinContent(Int_t bin, Double_t w)
    //                   Increment bin content by w
    //                   ==========================
 
+   if (!fN) TArrayI::Set(fNcells);
    Int_t newval = fArray[bin] + Int_t(w);
    if (newval > -2147483647 && newval < 2147483647) {fArray[bin] = Int_t(newval); return;}
    if (newval < -2147483647) fArray[bin] = -2147483647;
@@ -8792,6 +8817,7 @@ TH1 *TH1I::DrawCopy(Option_t *option) const
 Double_t TH1I::GetBinContent(Int_t bin) const
 {
    // see convention for numbering bins in TH1::GetBin
+   if (!fN) return 0.;
    if (fBuffer) ((TH1I*)this)->BufferEmpty();
    if (bin < 0) bin = 0;
    if (bin >= fNcells) bin = fNcells-1;
@@ -8820,6 +8846,7 @@ void TH1I::SetBinContent(Int_t bin, Double_t content)
    fEntries++;
    fTsumw = 0;
    if (bin < 0) return;
+   if (!fN) TArrayI::Set(fNcells);
    if (bin >= fNcells-1) {
       if (fXaxis.GetTimeDisplay()) {
          while (bin >=  fNcells-1)  LabelsInflate();
@@ -8842,7 +8869,7 @@ void TH1I::SetBinsLength(Int_t n)
 
    if (n < 0) n = fXaxis.GetNbins() + 2;
    fNcells = n;
-   TArrayI::Set(n);
+   if (fN>0 && !fgLazyAllocation) TArrayI::Set(n);
 }
 
 //______________________________________________________________________________
@@ -8938,7 +8965,7 @@ TH1F::TH1F(const char *name,const char *title,Int_t nbins,Double_t xlow,Double_t
    //           (see TH1::TH1 for explanation of parameters)
    //
    fDimension = 1;
-   TArrayF::Set(fNcells);
+   if (!fgLazyAllocation) TArrayF::Set(fNcells);
 
    if (xlow >= xup) SetBuffer(fgBufferSize);
    if (fgDefaultSumw2) Sumw2();
@@ -8954,7 +8981,7 @@ TH1F::TH1F(const char *name,const char *title,Int_t nbins,const Float_t *xbins)
    //           (see TH1::TH1 for explanation of parameters)
    //
    fDimension = 1;
-   TArrayF::Set(fNcells);
+   if (!fgLazyAllocation) TArrayF::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -8968,7 +8995,7 @@ TH1F::TH1F(const char *name,const char *title,Int_t nbins,const Double_t *xbins)
    //           (see TH1::TH1 for explanation of parameters)
    //
    fDimension = 1;
-   TArrayF::Set(fNcells);
+   if (!fgLazyAllocation) TArrayF::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -9003,6 +9030,25 @@ TH1F::~TH1F()
    // Destructor.
 }
 
+//______________________________________________________________________________
+void TH1F::AddBinContent(Int_t bin)
+{
+   //   -*-*-*-*-*-*-*-*Increment bin content by 1*-*-*-*-*-*-*-*-*-*-*-*-*-*
+   //                   ==========================
+   if (!fN) TArrayF::Set(fNcells);
+   ++fArray[bin];
+}
+
+//______________________________________________________________________________
+void TH1F::AddBinContent(Int_t bin, Double_t w)
+{
+   //                   Increment bin content by w
+   //                   ==========================
+
+   if (!fN) TArrayF::Set(fNcells);
+   fArray[bin] += Float_t (w);
+}   
+   
 //______________________________________________________________________________
 void TH1F::Copy(TObject &newth1) const
 {
@@ -9030,6 +9076,7 @@ TH1 *TH1F::DrawCopy(Option_t *option) const
 Double_t TH1F::GetBinContent(Int_t bin) const
 {
    // see convention for numbering bins in TH1::GetBin
+   if (!fN) return 0.;
    if (fBuffer) ((TH1F*)this)->BufferEmpty();
    if (bin < 0) bin = 0;
    if (bin >= fNcells) bin = fNcells-1;
@@ -9058,6 +9105,7 @@ void TH1F::SetBinContent(Int_t bin, Double_t content)
    fEntries++;
    fTsumw = 0;
    if (bin < 0) return;
+   if (!fN) TArrayF::Set(fNcells);
    if (bin >= fNcells-1) {
       if (fXaxis.GetTimeDisplay()) {
          while (bin >=  fNcells-1)  LabelsInflate();
@@ -9080,7 +9128,7 @@ void TH1F::SetBinsLength(Int_t n)
 
    if (n < 0) n = fXaxis.GetNbins() + 2;
    fNcells = n;
-   TArrayF::Set(n);
+   if (fN>0 && !fgLazyAllocation) TArrayF::Set(n);
 }
 
 //______________________________________________________________________________
@@ -9177,7 +9225,7 @@ TH1D::TH1D(const char *name,const char *title,Int_t nbins,Double_t xlow,Double_t
    //           (see TH1::TH1 for explanation of parameters)
    //
    fDimension = 1;
-   TArrayD::Set(fNcells);
+   if (!fgLazyAllocation) TArrayD::Set(fNcells);
 
    if (xlow >= xup) SetBuffer(fgBufferSize);
    if (fgDefaultSumw2) Sumw2();
@@ -9193,7 +9241,7 @@ TH1D::TH1D(const char *name,const char *title,Int_t nbins,const Float_t *xbins)
    //           (see TH1::TH1 for explanation of parameters)
    //
    fDimension = 1;
-   TArrayD::Set(fNcells);
+   if (!fgLazyAllocation)  TArrayD::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -9207,7 +9255,7 @@ TH1D::TH1D(const char *name,const char *title,Int_t nbins,const Double_t *xbins)
    //           (see TH1::TH1 for explanation of parameters)
    //
    fDimension = 1;
-   TArrayD::Set(fNcells);
+   if (!fgLazyAllocation) TArrayD::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -9242,6 +9290,25 @@ TH1D::TH1D(const TH1D &h1d) : TH1(), TArrayD()
    ((TH1D&)h1d).Copy(*this);
 }
 
+//______________________________________________________________________________
+void TH1D::AddBinContent(Int_t bin)
+{
+   //   -*-*-*-*-*-*-*-*Increment bin content by 1*-*-*-*-*-*-*-*-*-*-*-*-*-*
+   //                   ==========================
+   if (!fN) TArrayD::Set(fNcells);
+   ++fArray[bin];
+}
+
+//______________________________________________________________________________
+void TH1D::AddBinContent(Int_t bin, Double_t w)
+{
+   //                   Increment bin content by w
+   //                   ==========================
+
+   if (!fN) TArrayD::Set(fNcells);
+   fArray[bin] += Double_t (w);
+}   
+   
 //______________________________________________________________________________
 void TH1D::Copy(TObject &newth1) const
 {
@@ -9269,6 +9336,7 @@ TH1 *TH1D::DrawCopy(Option_t *option) const
 Double_t TH1D::GetBinContent(Int_t bin) const
 {
    // see convention for numbering bins in TH1::GetBin
+   if (!fN) return 0.;
    if (fBuffer) ((TH1D*)this)->BufferEmpty();
    if (bin < 0) bin = 0;
    if (bin >= fNcells) bin = fNcells-1;
@@ -9297,6 +9365,7 @@ void TH1D::SetBinContent(Int_t bin, Double_t content)
    fEntries++;
    fTsumw = 0;
    if (bin < 0) return;
+   if (!fN) TArrayD::Set(fNcells);
    if (bin >= fNcells-1) {
       if (fXaxis.GetTimeDisplay()) {
          while (bin >=  fNcells-1)  LabelsInflate();
@@ -9319,7 +9388,7 @@ void TH1D::SetBinsLength(Int_t n)
 
    if (n < 0) n = fXaxis.GetNbins() + 2;
    fNcells = n;
-   TArrayD::Set(n);
+   if (fN>0 && !fgLazyAllocation) TArrayD::Set(n);
 }
 
 //______________________________________________________________________________
diff --git a/hist/hist/src/TH2.cxx b/hist/hist/src/TH2.cxx
index af92bf174e3..22f26de3242 100644
--- a/hist/hist/src/TH2.cxx
+++ b/hist/hist/src/TH2.cxx
@@ -2703,7 +2703,7 @@ TH2C::TH2C(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_
 {
    // Constructor.
 
-   TArrayC::Set(fNcells);
+   if (!fgLazyAllocation) TArrayC::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 
    if (xlow >= xup || ylow >= yup) SetBuffer(fgBufferSize);
@@ -2716,7 +2716,7 @@ TH2C::TH2C(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
 {
    // Constructor.
 
-   TArrayC::Set(fNcells);
+   if (!fgLazyAllocation) TArrayC::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -2727,7 +2727,7 @@ TH2C::TH2C(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_
 {
    // Constructor.
 
-   TArrayC::Set(fNcells);
+   if (!fgLazyAllocation) TArrayC::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -2738,7 +2738,7 @@ TH2C::TH2C(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
 {
    // Constructor.
 
-   TArrayC::Set(fNcells);
+   if (!fgLazyAllocation) TArrayC::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -2749,7 +2749,7 @@ TH2C::TH2C(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
 {
    // Constructor.
 
-   TArrayC::Set(fNcells);
+   if (!fgLazyAllocation) TArrayC::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -2767,6 +2767,7 @@ void TH2C::AddBinContent(Int_t bin)
    //*-*-*-*-*-*-*-*-*-*Increment bin content by 1*-*-*-*-*-*-*-*-*-*-*-*-*-*
    //*-*                ==========================
 
+   if (!fN) TArrayC::Set(fNcells);
    if (fArray[bin] < 127) fArray[bin]++;
 }
 
@@ -2776,6 +2777,7 @@ void TH2C::AddBinContent(Int_t bin, Double_t w)
    //*-*-*-*-*-*-*-*-*-*Increment bin content by w*-*-*-*-*-*-*-*-*-*-*-*-*-*
    //*-*                ==========================
 
+   if (!fN) TArrayC::Set(fNcells);
    Int_t newval = fArray[bin] + Int_t(w);
    if (newval > -128 && newval < 128) {fArray[bin] = Char_t(newval); return;}
    if (newval < -127) fArray[bin] = -127;
@@ -2810,6 +2812,7 @@ Double_t TH2C::GetBinContent(Int_t bin) const
 {
    // Get bin content.
 
+   if (!fN) return 0.;
    if (fBuffer) ((TH2C*)this)->BufferEmpty();
    if (bin < 0) bin = 0;
    if (bin >= fNcells) bin = fNcells-1;
@@ -2833,6 +2836,7 @@ void TH2C::SetBinContent(Int_t bin, Double_t content)
    // Set bin content
    fEntries++;
    fTsumw = 0;
+   if (!fN) TArrayC::Set(fNcells);
    if (bin < 0) return;
    if (bin >= fNcells) return;
    fArray[bin] = Char_t (content);
@@ -2846,7 +2850,7 @@ void TH2C::SetBinsLength(Int_t n)
 
    if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2);
    fNcells = n;
-   TArrayC::Set(n);
+   if (fN>0 && !fgLazyAllocation) TArrayC::Set(n);
 }
 
 //______________________________________________________________________________
@@ -2977,7 +2981,7 @@ TH2S::TH2S(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_
 {
    // Constructor.
 
-   TArrayS::Set(fNcells);
+   if (!fgLazyAllocation) TArrayS::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 
    if (xlow >= xup || ylow >= yup) SetBuffer(fgBufferSize);
@@ -2990,7 +2994,7 @@ TH2S::TH2S(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
 {
    // Constructor.
 
-   TArrayS::Set(fNcells);
+   if (!fgLazyAllocation) TArrayS::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -3001,7 +3005,7 @@ TH2S::TH2S(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_
 {
    // Constructor.
 
-   TArrayS::Set(fNcells);
+   if (!fgLazyAllocation) TArrayS::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -3012,7 +3016,7 @@ TH2S::TH2S(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
 {
    // Constructor.
 
-   TArrayS::Set(fNcells);
+   if (!fgLazyAllocation) TArrayS::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -3023,7 +3027,7 @@ TH2S::TH2S(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
 {
    // Constructor.
 
-   TArrayS::Set(fNcells);
+   if (!fgLazyAllocation) TArrayS::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -3041,6 +3045,7 @@ void TH2S::AddBinContent(Int_t bin)
    //*-*-*-*-*-*-*-*-*-*Increment bin content by 1*-*-*-*-*-*-*-*-*-*-*-*-*-*
    //*-*                ==========================
 
+   if (!fN) TArrayS::Set(fNcells);
    if (fArray[bin] < 32767) fArray[bin]++;
 }
 
@@ -3050,6 +3055,7 @@ void TH2S::AddBinContent(Int_t bin, Double_t w)
    //*-*-*-*-*-*-*-*-*-*Increment bin content by w*-*-*-*-*-*-*-*-*-*-*-*-*-*
    //*-*                ==========================
 
+   if (!fN) TArrayS::Set(fNcells);
    Int_t newval = fArray[bin] + Int_t(w);
    if (newval > -32768 && newval < 32768) {fArray[bin] = Short_t(newval); return;}
    if (newval < -32767) fArray[bin] = -32767;
@@ -3084,7 +3090,8 @@ Double_t TH2S::GetBinContent(Int_t bin) const
 {
    // Get bin content.
 
-   if (fBuffer) ((TH2C*)this)->BufferEmpty();
+   if (!fN) return 0.;
+   if (fBuffer) ((TH2S*)this)->BufferEmpty();
    if (bin < 0) bin = 0;
    if (bin >= fNcells) bin = fNcells-1;
    if (!fArray) return 0;
@@ -3109,6 +3116,7 @@ void TH2S::SetBinContent(Int_t bin, Double_t content)
    fTsumw = 0;
    if (bin < 0) return;
    if (bin >= fNcells) return;
+   if (!fN) TArrayS::Set(fNcells);
    fArray[bin] = Short_t (content);
 }
 
@@ -3120,7 +3128,7 @@ void TH2S::SetBinsLength(Int_t n)
 
    if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2);
    fNcells = n;
-   TArrayS::Set(n);
+   if (fN>0 && !fgLazyAllocation) TArrayS::Set(n);
 }
 
 //______________________________________________________________________________
@@ -3251,7 +3259,7 @@ TH2I::TH2I(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_
 {
    // Constructor.
 
-   TArrayI::Set(fNcells);
+   if (!fgLazyAllocation) TArrayI::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 
    if (xlow >= xup || ylow >= yup) SetBuffer(fgBufferSize);
@@ -3264,7 +3272,7 @@ TH2I::TH2I(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
 {
    // Constructor.
 
-   TArrayI::Set(fNcells);
+   if (!fgLazyAllocation) TArrayI::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -3275,7 +3283,7 @@ TH2I::TH2I(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_
 {
    // Constructor.
 
-   TArrayI::Set(fNcells);
+   if (!fgLazyAllocation) TArrayI::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -3286,7 +3294,7 @@ TH2I::TH2I(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
 {
    // Constructor.
 
-   TArrayI::Set(fNcells);
+   if (!fgLazyAllocation) TArrayI::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -3297,7 +3305,7 @@ TH2I::TH2I(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
 {
    // Constructor.
 
-   TArrayI::Set(fNcells);
+   if (!fgLazyAllocation) TArrayI::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -3315,6 +3323,7 @@ void TH2I::AddBinContent(Int_t bin)
    //*-*-*-*-*-*-*-*-*-*Increment bin content by 1*-*-*-*-*-*-*-*-*-*-*-*-*-*
    //*-*                ==========================
 
+   if (!fN) TArrayI::Set(fNcells);
    if (fArray[bin] < 2147483647) fArray[bin]++;
 }
 
@@ -3324,6 +3333,7 @@ void TH2I::AddBinContent(Int_t bin, Double_t w)
    //*-*-*-*-*-*-*-*-*-*Increment bin content by w*-*-*-*-*-*-*-*-*-*-*-*-*-*
    //*-*                ==========================
 
+   if (!fN) TArrayI::Set(fNcells);
    Int_t newval = fArray[bin] + Int_t(w);
    if (newval > -2147483647 && newval < 2147483647) {fArray[bin] = Int_t(newval); return;}
    if (newval < -2147483647) fArray[bin] = -2147483647;
@@ -3358,6 +3368,7 @@ Double_t TH2I::GetBinContent(Int_t bin) const
 {
    // Get bin content.
 
+   if (!fN) return 0.;
    if (fBuffer) ((TH2C*)this)->BufferEmpty();
    if (bin < 0) bin = 0;
    if (bin >= fNcells) bin = fNcells-1;
@@ -3383,6 +3394,7 @@ void TH2I::SetBinContent(Int_t bin, Double_t content)
    fTsumw = 0;
    if (bin < 0) return;
    if (bin >= fNcells) return;
+   if (!fN) TArrayI::Set(fNcells);
    fArray[bin] = Int_t (content);
 }
 
@@ -3394,7 +3406,7 @@ void TH2I::SetBinsLength(Int_t n)
 
    if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2);
    fNcells = n;
-   TArrayI::Set(n);
+   if (fN>0 && !fgLazyAllocation) TArrayI::Set(n);
 }
 
 //______________________________________________________________________________
@@ -3491,7 +3503,7 @@ TH2F::TH2F(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_
 {
    // Constructor.
 
-   TArrayF::Set(fNcells);
+   if (!fgLazyAllocation) TArrayF::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 
    if (xlow >= xup || ylow >= yup) SetBuffer(fgBufferSize);
@@ -3504,7 +3516,7 @@ TH2F::TH2F(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
 {
    // Constructor.
 
-   TArrayF::Set(fNcells);
+   if (!fgLazyAllocation) TArrayF::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -3515,7 +3527,7 @@ TH2F::TH2F(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_
 {
    // Constructor.
 
-   TArrayF::Set(fNcells);
+   if (!fgLazyAllocation) TArrayF::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -3526,7 +3538,7 @@ TH2F::TH2F(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
 {
    // Constructor.
 
-   TArrayF::Set(fNcells);
+   if (!fgLazyAllocation) TArrayF::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -3537,7 +3549,7 @@ TH2F::TH2F(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
 {
    // Constructor.
 
-   TArrayF::Set(fNcells);
+   if (!fgLazyAllocation) TArrayF::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -3567,6 +3579,25 @@ TH2F::TH2F(const TH2F &h2f) : TH2(), TArrayF()
    ((TH2F&)h2f).Copy(*this);
 }
 
+//______________________________________________________________________________
+void TH2F::AddBinContent(Int_t bin)
+{
+   //   -*-*-*-*-*-*-*-*Increment bin content by 1*-*-*-*-*-*-*-*-*-*-*-*-*-*
+   //                   ==========================
+   if (!fN) TArrayF::Set(fNcells);
+   ++fArray[bin];
+}
+
+//______________________________________________________________________________
+void TH2F::AddBinContent(Int_t bin, Double_t w)
+{
+   //                   Increment bin content by w
+   //                   ==========================
+
+   if (!fN) TArrayF::Set(fNcells);
+   fArray[bin] += Float_t (w);
+}   
+
 //______________________________________________________________________________
 void TH2F::Copy(TObject &newth2) const
 {
@@ -3595,6 +3626,7 @@ Double_t TH2F::GetBinContent(Int_t bin) const
 {
    // Get bin content.
 
+   if (!fN) return 0.;
    if (fBuffer) ((TH2C*)this)->BufferEmpty();
    if (bin < 0) bin = 0;
    if (bin >= fNcells) bin = fNcells-1;
@@ -3620,6 +3652,7 @@ void TH2F::SetBinContent(Int_t bin, Double_t content)
    fTsumw = 0;
    if (bin < 0) return;
    if (bin >= fNcells) return;
+   if (!fN) TArrayF::Set(fNcells);
    fArray[bin] = Float_t (content);
 }
 
@@ -3631,7 +3664,7 @@ void TH2F::SetBinsLength(Int_t n)
 
    if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2);
    fNcells = n;
-   TArrayF::Set(n);
+   if (fN>0 && !fgLazyAllocation) TArrayF::Set(n);
 }
 
 //______________________________________________________________________________
@@ -3774,7 +3807,7 @@ TH2D::TH2D(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_
 {
    // Constructor.
 
-   TArrayD::Set(fNcells);
+   if (!fgLazyAllocation) TArrayD::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 
    if (xlow >= xup || ylow >= yup) SetBuffer(fgBufferSize);
@@ -3787,7 +3820,7 @@ TH2D::TH2D(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
 {
    // Constructor.
 
-   TArrayD::Set(fNcells);
+   if (!fgLazyAllocation) TArrayD::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -3798,7 +3831,7 @@ TH2D::TH2D(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_
 {
    // Constructor.
 
-   TArrayD::Set(fNcells);
+   if (!fgLazyAllocation) TArrayD::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -3809,7 +3842,7 @@ TH2D::TH2D(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
 {
    // Constructor.
 
-   TArrayD::Set(fNcells);
+   if (!fgLazyAllocation) TArrayD::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -3820,8 +3853,8 @@ TH2D::TH2D(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
 {
    // Constructor.
 
-   TArrayD::Set(fNcells);
-   if (fgDefaultSumw2) Sumw2();
+   if (!fgLazyAllocation) TArrayD::Set(fNcells);
+  if (fgDefaultSumw2) Sumw2();
 }
 
 //______________________________________________________________________________
@@ -3851,6 +3884,25 @@ TH2D::TH2D(const TH2D &h2d) : TH2(), TArrayD()
    ((TH2D&)h2d).Copy(*this);
 }
 
+//______________________________________________________________________________
+void TH2D::AddBinContent(Int_t bin)
+{
+   //   -*-*-*-*-*-*-*-*Increment bin content by 1*-*-*-*-*-*-*-*-*-*-*-*-*-*
+   //                   ==========================
+   if (!fN) TArrayD::Set(fNcells);
+   ++fArray[bin];
+}
+
+//______________________________________________________________________________
+void TH2D::AddBinContent(Int_t bin, Double_t w)
+{
+   //                   Increment bin content by w
+   //                   ==========================
+
+   if (!fN) TArrayD::Set(fNcells);
+   fArray[bin] += Double_t (w);
+}   
+   
 //______________________________________________________________________________
 void TH2D::Copy(TObject &newth2) const
 {
@@ -3879,6 +3931,7 @@ Double_t TH2D::GetBinContent(Int_t bin) const
 {
    // Get bin content.
 
+   if (!fN) return 0.;
    if (fBuffer) ((TH2C*)this)->BufferEmpty();
    if (bin < 0) bin = 0;
    if (bin >= fNcells) bin = fNcells-1;
@@ -3904,6 +3957,7 @@ void TH2D::SetBinContent(Int_t bin, Double_t content)
    fTsumw = 0;
    if (bin < 0) return;
    if (bin >= fNcells) return;
+   if (!fN) TArrayD::Set(fNcells);
    fArray[bin] = Double_t (content);
 }
 
@@ -3915,7 +3969,7 @@ void TH2D::SetBinsLength(Int_t n)
 
    if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2);
    fNcells = n;
-   TArrayD::Set(n);
+   if (fN>0 && !fgLazyAllocation) TArrayD::Set(n);
 }
 
 //______________________________________________________________________________
diff --git a/hist/hist/src/TH3.cxx b/hist/hist/src/TH3.cxx
index 8708aaf103f..542f547040e 100644
--- a/hist/hist/src/TH3.cxx
+++ b/hist/hist/src/TH3.cxx
@@ -3444,7 +3444,7 @@ TH3C::TH3C(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_
    //*-*-*-*-*-*-*-*-*Normal constructor for fix bin size 3-D histograms*-*-*-*-*
    //*-*              ==================================================
 
-   TArrayC::Set(fNcells);
+   if (!fgLazyAllocation) TArrayC::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 
    if (xlow >= xup || ylow >= yup || zlow >= zup) SetBuffer(fgBufferSize);
@@ -3458,7 +3458,7 @@ TH3C::TH3C(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
 {
    //*-*-*-*-*-*-*-*Normal constructor for variable bin size 3-D histograms*-*-*-*
    //*-*            =======================================================
-   TArrayC::Set(fNcells);
+   if (!fgLazyAllocation) TArrayC::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -3470,7 +3470,7 @@ TH3C::TH3C(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
 {
    //*-*-*-*-*-*-*-*Normal constructor for variable bin size 3-D histograms*-*-*-*
    //*-*            =======================================================
-   TArrayC::Set(fNcells);
+   if (!fgLazyAllocation) TArrayC::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -3488,6 +3488,7 @@ void TH3C::AddBinContent(Int_t bin)
    //*-*-*-*-*-*-*-*-*-*Increment bin content by 1*-*-*-*-*-*-*-*-*-*-*-*-*-*
    //*-*                ==========================
 
+   if (!fN) TArrayC::Set(fNcells);
    if (fArray[bin] < 127) fArray[bin]++;
 }
 
@@ -3497,6 +3498,7 @@ void TH3C::AddBinContent(Int_t bin, Double_t w)
    //*-*-*-*-*-*-*-*-*-*Increment bin content by w*-*-*-*-*-*-*-*-*-*-*-*-*-*
    //*-*                ==========================
 
+   if (!fN) TArrayC::Set(fNcells);
    Int_t newval = fArray[bin] + Int_t(w);
    if (newval > -128 && newval < 128) {fArray[bin] = Char_t(newval); return;}
    if (newval < -127) fArray[bin] = -127;
@@ -3532,6 +3534,7 @@ Double_t TH3C::GetBinContent(Int_t bin) const
 {
    // Get bin content.
 
+   if (!fN) return 0.;
    if (fBuffer) ((TH3C*)this)->BufferEmpty();
    if (bin < 0) bin = 0;
    if (bin >= fNcells) bin = fNcells-1;
@@ -3558,7 +3561,7 @@ void TH3C::SetBinsLength(Int_t n)
 
    if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2)*(fZaxis.GetNbins()+2);
    fNcells = n;
-   TArrayC::Set(n);
+   if (fN>0 && !fgLazyAllocation) TArrayC::Set(n);
 }
 
 //______________________________________________________________________________
@@ -3569,6 +3572,7 @@ void TH3C::SetBinContent(Int_t bin, Double_t content)
    fTsumw = 0;
    if (bin < 0) return;
    if (bin >= fNcells) return;
+   if (!fN) TArrayC::Set(fNcells);
    fArray[bin] = Char_t (content);
 }
 
@@ -3727,7 +3731,7 @@ TH3S::TH3S(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_
 {
    //*-*-*-*-*-*-*-*-*Normal constructor for fix bin size 3-D histograms*-*-*-*-*
    //*-*              ==================================================
-   TH3S::Set(fNcells);
+   if (!fgLazyAllocation) TH3S::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 
    if (xlow >= xup || ylow >= yup || zlow >= zup) SetBuffer(fgBufferSize);
@@ -3741,7 +3745,7 @@ TH3S::TH3S(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
 {
    //*-*-*-*-*-*-*-*Normal constructor for variable bin size 3-D histograms*-*-*-*
    //*-*            =======================================================
-   TH3S::Set(fNcells);
+   if (!fgLazyAllocation) TH3S::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -3753,7 +3757,7 @@ TH3S::TH3S(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
 {
    //*-*-*-*-*-*-*-*Normal constructor for variable bin size 3-D histograms*-*-*-*
    //*-*            =======================================================
-   TH3S::Set(fNcells);
+   if (!fgLazyAllocation) TH3S::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -3771,6 +3775,7 @@ void TH3S::AddBinContent(Int_t bin)
    //*-*-*-*-*-*-*-*-*-*Increment bin content by 1*-*-*-*-*-*-*-*-*-*-*-*-*-*
    //*-*                ==========================
 
+   if (!fN) TArrayS::Set(fNcells);
    if (fArray[bin] < 32767) fArray[bin]++;
 }
 
@@ -3780,6 +3785,7 @@ void TH3S::AddBinContent(Int_t bin, Double_t w)
    //*-*-*-*-*-*-*-*-*-*Increment bin content by w*-*-*-*-*-*-*-*-*-*-*-*-*-*
    //*-*                ==========================
 
+   if (!fN) TArrayS::Set(fNcells);
    Int_t newval = fArray[bin] + Int_t(w);
    if (newval > -32768 && newval < 32768) {fArray[bin] = Short_t(newval); return;}
    if (newval < -32767) fArray[bin] = -32767;
@@ -3815,6 +3821,7 @@ Double_t TH3S::GetBinContent(Int_t bin) const
 {
    // Get bin content.
 
+   if (!fN) return 0.;
    if (fBuffer) ((TH3S*)this)->BufferEmpty();
    if (bin < 0) bin = 0;
    if (bin >= fNcells) bin = fNcells-1;
@@ -3841,6 +3848,7 @@ void TH3S::SetBinContent(Int_t bin, Double_t content)
    fTsumw = 0;
    if (bin < 0) return;
    if (bin >= fNcells) return;
+   if (!fN) TArrayS::Set(fNcells);
    fArray[bin] = Short_t (content);
 }
 
@@ -3852,7 +3860,7 @@ void TH3S::SetBinsLength(Int_t n)
 
    if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2)*(fZaxis.GetNbins()+2);
    fNcells = n;
-   TArrayS::Set(n);
+   if (fN>0 && !fgLazyAllocation) TArrayS::Set(n);
 }
 
 //______________________________________________________________________________
@@ -3981,7 +3989,7 @@ TH3I::TH3I(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_
 {
    //*-*-*-*-*-*-*-*-*Normal constructor for fix bin size 3-D histograms*-*-*-*-*
    //*-*              ==================================================
-   TH3I::Set(fNcells);
+   if (!fgLazyAllocation) TH3I::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 
    if (xlow >= xup || ylow >= yup || zlow >= zup) SetBuffer(fgBufferSize);
@@ -3995,7 +4003,7 @@ TH3I::TH3I(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
 {
    //*-*-*-*-*-*-*-*Normal constructor for variable bin size 3-D histograms*-*-*-*
    //*-*            =======================================================
-   TArrayI::Set(fNcells);
+   if (!fgLazyAllocation) TArrayI::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -4007,7 +4015,7 @@ TH3I::TH3I(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
 {
    //*-*-*-*-*-*-*-*Normal constructor for variable bin size 3-D histograms*-*-*-*
    //*-*            =======================================================
-   TArrayI::Set(fNcells);
+   if (!fgLazyAllocation) TArrayI::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -4025,6 +4033,7 @@ void TH3I::AddBinContent(Int_t bin)
    //*-*-*-*-*-*-*-*-*-*Increment bin content by 1*-*-*-*-*-*-*-*-*-*-*-*-*-*
    //*-*                ==========================
 
+   if (!fN) TArrayI::Set(fNcells);
    if (fArray[bin] < 2147483647) fArray[bin]++;
 }
 
@@ -4034,6 +4043,7 @@ void TH3I::AddBinContent(Int_t bin, Double_t w)
    //*-*-*-*-*-*-*-*-*-*Increment bin content by w*-*-*-*-*-*-*-*-*-*-*-*-*-*
    //*-*                ==========================
 
+   if (!fN) TArrayI::Set(fNcells);
    Int_t newval = fArray[bin] + Int_t(w);
    if (newval > -2147483647 && newval < 2147483647) {fArray[bin] = Int_t(newval); return;}
    if (newval < -2147483647) fArray[bin] = -2147483647;
@@ -4069,6 +4079,7 @@ Double_t TH3I::GetBinContent(Int_t bin) const
 {
    // Get bin content.
 
+   if (!fN) return 0.;
    if (fBuffer) ((TH3I*)this)->BufferEmpty();
    if (bin < 0) bin = 0;
    if (bin >= fNcells) bin = fNcells-1;
@@ -4095,6 +4106,7 @@ void TH3I::SetBinContent(Int_t bin, Double_t content)
    fTsumw = 0;
    if (bin < 0) return;
    if (bin >= fNcells) return;
+   if (!fN) TArrayI::Set(fNcells);
    fArray[bin] = Int_t (content);
 }
 
@@ -4106,7 +4118,7 @@ void TH3I::SetBinsLength(Int_t n)
 
    if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2)*(fZaxis.GetNbins()+2);
    fNcells = n;
-   TArrayI::Set(n);
+   if (fN>0 && !fgLazyAllocation) TArrayI::Set(n);
 }
 
 //______________________________________________________________________________
@@ -4203,7 +4215,7 @@ TH3F::TH3F(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_
 {
    //*-*-*-*-*-*-*-*-*Normal constructor for fix bin size 3-D histograms*-*-*-*-*
    //*-*              ==================================================
-   TArrayF::Set(fNcells);
+   if (!fgLazyAllocation) TArrayF::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 
    if (xlow >= xup || ylow >= yup || zlow >= zup) SetBuffer(fgBufferSize);
@@ -4217,7 +4229,7 @@ TH3F::TH3F(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
 {
    //*-*-*-*-*-*-*-*Normal constructor for variable bin size 3-D histograms*-*-*-*
    //*-*            =======================================================
-   TArrayF::Set(fNcells);
+   if (!fgLazyAllocation) TArrayF::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -4229,7 +4241,7 @@ TH3F::TH3F(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
 {
    //*-*-*-*-*-*-*-*Normal constructor for variable bin size 3-D histograms*-*-*-*
    //*-*            =======================================================
-   TArrayF::Set(fNcells);
+   if (!fgLazyAllocation) TArrayF::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -4241,6 +4253,25 @@ TH3F::TH3F(const TH3F &h3f) : TH3(), TArrayF()
    ((TH3F&)h3f).Copy(*this);
 }
 
+//______________________________________________________________________________
+void TH3F::AddBinContent(Int_t bin)
+{
+   //   -*-*-*-*-*-*-*-*Increment bin content by 1*-*-*-*-*-*-*-*-*-*-*-*-*-*
+   //                   ==========================
+   if (!fN) TArrayF::Set(fNcells);
+   ++fArray[bin];
+}
+
+//______________________________________________________________________________
+void TH3F::AddBinContent(Int_t bin, Double_t w)
+{
+   //                   Increment bin content by w
+   //                   ==========================
+
+   if (!fN) TArrayF::Set(fNcells);
+   fArray[bin] += Float_t (w);
+}   
+
 //______________________________________________________________________________
 void TH3F::Copy(TObject &newth3) const
 {
@@ -4270,6 +4301,7 @@ Double_t TH3F::GetBinContent(Int_t bin) const
 {
    // Get bin content.
 
+   if (!fN) return 0.;
    if (fBuffer) ((TH3F*)this)->BufferEmpty();
    if (bin < 0) bin = 0;
    if (bin >= fNcells) bin = fNcells-1;
@@ -4296,6 +4328,7 @@ void TH3F::SetBinContent(Int_t bin, Double_t content)
    fTsumw = 0;
    if (bin < 0) return;
    if (bin >= fNcells) return;
+   if (!fN) TArrayF::Set(fNcells);
    fArray[bin] = Float_t (content);
 }
 
@@ -4307,7 +4340,7 @@ void TH3F::SetBinsLength(Int_t n)
 
    if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2)*(fZaxis.GetNbins()+2);
    fNcells = n;
-   TArrayF::Set(n);
+   if (fN>0 && !fgLazyAllocation) TArrayF::Set(n);
 }
 
 //______________________________________________________________________________
@@ -4436,7 +4469,7 @@ TH3D::TH3D(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_
 {
    //*-*-*-*-*-*-*-*-*Normal constructor for fix bin size 3-D histograms*-*-*-*-*
    //*-*              ==================================================
-   TArrayD::Set(fNcells);
+   if (!fgLazyAllocation) TArrayD::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 
    if (xlow >= xup || ylow >= yup || zlow >= zup) SetBuffer(fgBufferSize);
@@ -4450,7 +4483,7 @@ TH3D::TH3D(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins
 {
    //*-*-*-*-*-*-*-*Normal constructor for variable bin size 3-D histograms*-*-*-*
    //*-*            =======================================================
-   TArrayD::Set(fNcells);
+   if (!fgLazyAllocation) TArrayD::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -4462,7 +4495,7 @@ TH3D::TH3D(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins
 {
    //*-*-*-*-*-*-*-*Normal constructor for variable bin size 3-D histograms*-*-*-*
    //*-*            =======================================================
-   TArrayD::Set(fNcells);
+   if (!fgLazyAllocation) TArrayD::Set(fNcells);
    if (fgDefaultSumw2) Sumw2();
 }
 
@@ -4474,6 +4507,25 @@ TH3D::TH3D(const TH3D &h3d) : TH3(), TArrayD()
    ((TH3D&)h3d).Copy(*this);
 }
 
+//______________________________________________________________________________
+void TH3D::AddBinContent(Int_t bin)
+{
+   //   -*-*-*-*-*-*-*-*Increment bin content by 1*-*-*-*-*-*-*-*-*-*-*-*-*-*
+   //                   ==========================
+   if (!fN) TArrayD::Set(fNcells);
+   ++fArray[bin];
+}
+
+//______________________________________________________________________________
+void TH3D::AddBinContent(Int_t bin, Double_t w)
+{
+   //                   Increment bin content by w
+   //                   ==========================
+
+   if (!fN) TArrayD::Set(fNcells);
+   fArray[bin] += Double_t (w);
+}   
+   
 //______________________________________________________________________________
 void TH3D::Copy(TObject &newth3) const
 {
@@ -4503,6 +4555,7 @@ Double_t TH3D::GetBinContent(Int_t bin) const
 {
    // Get bin content.
 
+   if (!fN) return 0.;
    if (fBuffer) ((TH3D*)this)->BufferEmpty();
    if (bin < 0) bin = 0;
    if (bin >= fNcells) bin = fNcells-1;
@@ -4515,7 +4568,6 @@ void TH3D::Reset(Option_t *option)
 {
    //*-*-*-*-*-*-*-*Reset this histogram: contents, errors, etc*-*-*-*-*-*-*-*
    //*-*            ===========================================
-
    TH3::Reset(option);
    TArrayD::Reset();
    // should also reset statistics once statistics are implemented for TH3
@@ -4529,6 +4581,7 @@ void TH3D::SetBinContent(Int_t bin, Double_t content)
    fTsumw = 0;
    if (bin < 0) return;
    if (bin >= fNcells) return;
+   if (!fN) TArrayD::Set(fNcells);
    fArray[bin] = Double_t (content);
 }
 
@@ -4541,7 +4594,7 @@ void TH3D::SetBinsLength(Int_t n)
 
    if (n < 0) n = (fXaxis.GetNbins()+2)*(fYaxis.GetNbins()+2)*(fZaxis.GetNbins()+2);
    fNcells = n;
-   TArrayD::Set(n);
+   if (fN>0 && !fgLazyAllocation) TArrayD::Set(n);
 }
 
 //______________________________________________________________________________
diff --git a/hist/hist/src/TProfile.cxx b/hist/hist/src/TProfile.cxx
index 5096275c9a5..fde59d634e9 100644
--- a/hist/hist/src/TProfile.cxx
+++ b/hist/hist/src/TProfile.cxx
@@ -227,10 +227,12 @@ void TProfile::BuildOptions(Double_t ymin, Double_t ymax, Option_t *option)
 
    SetErrorOption(option);
 
-   fBinEntries.Set(fNcells);  //*-* create number of entries per bin array
+   if (!fgLazyAllocation) {
+      fBinEntries.Set(fNcells);  //*-* create number of entries per bin array
 
-   // TH1::Sumw2 create sum of square of weights array times y (fSumw2) . This is always created for a TProfile
-   TH1::Sumw2();                   //*-* create sum of squares of weights array times y
+      // TH1::Sumw2 create sum of square of weights array times y (fSumw2) . This is always created for a TProfile
+      TH1::Sumw2();                   //*-* create sum of squares of weights array times y
+   }
    // TProfile::Sumw2 create sum of square of weight2 (fBinSumw2). This is needed only for profile filled with weights not 1
    if (fgDefaultSumw2) Sumw2();    // optionally create sum of squares of weights / bin 
 
@@ -304,6 +306,35 @@ void TProfile::Add(const TH1 *h1, const TH1 *h2, Double_t c1, Double_t c2)
    TProfileHelper::Add(this, h1, h2, c1, c2);
 }
 
+//______________________________________________________________________________
+void TProfile::AddBinContent(Int_t bin)
+{
+   //   -*-*-*-*-*-*-*-*Increment bin content by 1*-*-*-*-*-*-*-*-*-*-*-*-*-*
+   //                   ==========================
+   AllocateIfEmpty();
+   TH1D::AddBinContent(bin);
+}
+ 
+//______________________________________________________________________________
+void TProfile::AddBinContent(Int_t bin, Double_t w)
+{
+   //                   Increment bin content by w
+   //                   ==========================
+
+   AllocateIfEmpty();
+   TH1D::AddBinContent(bin,w);
+}   
+
+//______________________________________________________________________________
+void TProfile::AllocateIfEmpty()
+{
+// Allocate the bins array (fArray), sum of weigths (fSumw2) and bin entries (fBinEntries)
+   if (!fN) {
+      TArrayD::Set(fNcells);
+      fBinEntries.Set(fNcells);
+      TH1::Sumw2();
+   }
+}
 
 //______________________________________________________________________________
 void TProfile::Approximate(Bool_t approx)
@@ -1629,6 +1660,18 @@ void TProfile::Scale(Double_t c1, Option_t * option)
    TProfileHelper::Scale(this, c1, option);
 }
 
+//______________________________________________________________________________
+void TProfile::SetBinContent(Int_t bin, Double_t content)
+{
+   // Set bin content
+   // see convention for numbering bins in TH1::GetBin
+   // In case the bin number is greater than the number of bins and
+   // the timedisplay option is set or the kCanRebin bit is set,
+   // the number of bins is automatically doubled to accomodate the new bin
+   AllocateIfEmpty();
+   TH1D::SetBinContent(bin,content);
+}
+   
 //______________________________________________________________________________
 void TProfile::SetBinEntries(Int_t bin, Double_t w)
 {
diff --git a/hist/hist/src/TProfile2D.cxx b/hist/hist/src/TProfile2D.cxx
index dc75def3220..24b549e9de3 100644
--- a/hist/hist/src/TProfile2D.cxx
+++ b/hist/hist/src/TProfile2D.cxx
@@ -208,9 +208,11 @@ void TProfile2D::BuildOptions(Double_t zmin, Double_t zmax, Option_t *option)
 
    SetErrorOption(option);
 
-   fBinEntries.Set(fNcells);  //*-* create number of entries per cell array
+   if (!fgLazyAllocation) {
+      fBinEntries.Set(fNcells);  //*-* create number of entries per cell array
 
-   TH1::Sumw2();                   //*-* create sum of squares of weights array times y
+      TH1::Sumw2();                   //*-* create sum of squares of weights array times y
+   }
    if (fgDefaultSumw2) Sumw2();    // optionally create sum of squares of weights
 
    fZmin = zmin;
@@ -280,6 +282,36 @@ void TProfile2D::Add(const TH1 *h1, const TH1 *h2, Double_t c1, Double_t c2)
 }
 
 
+//______________________________________________________________________________
+void TProfile2D::AddBinContent(Int_t bin)
+{
+   //   -*-*-*-*-*-*-*-*Increment bin content by 1*-*-*-*-*-*-*-*-*-*-*-*-*-*
+   //                   ==========================
+   AllocateIfEmpty();
+   TH2D::AddBinContent(bin);
+}
+ 
+//______________________________________________________________________________
+void TProfile2D::AddBinContent(Int_t bin, Double_t w)
+{
+   //                   Increment bin content by w
+   //                   ==========================
+
+   AllocateIfEmpty();
+   TH2D::AddBinContent(bin,w);
+}   
+
+//______________________________________________________________________________
+void TProfile2D::AllocateIfEmpty()
+{
+// Allocate the bins array (fArray), sum of weigths (fSumw2) and bin entries (fBinEntries)
+   if (!fN) {
+      TArrayD::Set(fNcells);
+      fBinEntries.Set(fNcells);
+      TH1::Sumw2();
+   }
+}
+
 //______________________________________________________________________________
 void TProfile2D::Approximate(Bool_t approx)
 {
@@ -1727,6 +1759,18 @@ void TProfile2D::Scale(Double_t c1, Option_t * option)
    TProfileHelper::Scale(this, c1, option);
 }
 
+//______________________________________________________________________________
+void TProfile2D::SetBinContent(Int_t bin, Double_t content)
+{
+   // Set bin content
+   // see convention for numbering bins in TH1::GetBin
+   // In case the bin number is greater than the number of bins and
+   // the timedisplay option is set or the kCanRebin bit is set,
+   // the number of bins is automatically doubled to accomodate the new bin
+   AllocateIfEmpty();
+   TH2D::SetBinContent(bin,content);
+}
+   
 //______________________________________________________________________________
 void TProfile2D::SetBinEntries(Int_t bin, Double_t w)
 {
diff --git a/hist/hist/src/TProfile3D.cxx b/hist/hist/src/TProfile3D.cxx
index 7b86089a3b9..04e3279889f 100644
--- a/hist/hist/src/TProfile3D.cxx
+++ b/hist/hist/src/TProfile3D.cxx
@@ -174,9 +174,11 @@ void TProfile3D::BuildOptions(Double_t tmin, Double_t tmax, Option_t *option)
 
    SetErrorOption(option);
 
-   fBinEntries.Set(fNcells);  //*-* create number of entries per cell array
+   if (!fgLazyAllocation) {
+      fBinEntries.Set(fNcells);  //*-* create number of entries per cell array
 
-   TH1::Sumw2();                   //*-* create sum of squares of weights array times y 
+      TH1::Sumw2();                   //*-* create sum of squares of weights array times y 
+   }
    if (fgDefaultSumw2) Sumw2();    // optionally create sum of squares of weights
 
    fTmin = tmin;
@@ -245,6 +247,35 @@ void TProfile3D::Add(const TH1 *h1, const TH1 *h2, Double_t c1, Double_t c2)
    TProfileHelper::Add(this, h1, h2, c1, c2);
 }
 
+//______________________________________________________________________________
+void TProfile3D::AddBinContent(Int_t bin)
+{
+   //   -*-*-*-*-*-*-*-*Increment bin content by 1*-*-*-*-*-*-*-*-*-*-*-*-*-*
+   //                   ==========================
+   AllocateIfEmpty();
+   TH3D::AddBinContent(bin);
+}
+ 
+//______________________________________________________________________________
+void TProfile3D::AddBinContent(Int_t bin, Double_t w)
+{
+   //                   Increment bin content by w
+   //                   ==========================
+
+   AllocateIfEmpty();
+   TH3D::AddBinContent(bin,w);
+}   
+
+//______________________________________________________________________________
+void TProfile3D::AllocateIfEmpty()
+{
+// Allocate the bins array (fArray), sum of weigths (fSumw2) and bin entries (fBinEntries)
+   if (!fN) {
+      TArrayD::Set(fNcells);
+      fBinEntries.Set(fNcells);
+      TH1::Sumw2();
+   }
+}
 
 //______________________________________________________________________________
 void TProfile3D::Approximate(Bool_t approx)
@@ -261,7 +292,6 @@ void TProfile3D::Approximate(Bool_t approx)
    fgApproximate = approx;
 }
 
-
 //______________________________________________________________________________
 Int_t TProfile3D::BufferEmpty(Int_t action)
 {
@@ -1257,6 +1287,18 @@ void TProfile3D::Scale(Double_t c1, Option_t *option)
    TProfileHelper::Scale(this, c1, option);
 }
 
+//______________________________________________________________________________
+void TProfile3D::SetBinContent(Int_t bin, Double_t content)
+{
+   // Set bin content
+   // see convention for numbering bins in TH1::GetBin
+   // In case the bin number is greater than the number of bins and
+   // the timedisplay option is set or the kCanRebin bit is set,
+   // the number of bins is automatically doubled to accomodate the new bin
+   AllocateIfEmpty();
+   TH3D::SetBinContent(bin,content);
+}
+   
 //______________________________________________________________________________
 void TProfile3D::SetBinEntries(Int_t bin, Double_t w)
 {
-- 
GitLab