Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 1878)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 1879)
@@ -2,8 +2,42 @@
 
 
+ 2003/03/28: Thomas Bretz
+
+   * mbase/MParContainer.h:
+     - added kEnableGraphicalOutput
+     - added EnableGraphicalOutput
+     - added IsGraphicalOutputEnabled
+   
+   * mbase/MParList.h:
+     - changed BIT(15) to BIT(17)
+   
+   * mhist/MH.[h,cc]:
+     - SetBinning(TH1*, TH1*) --> SetBinning(TH1*, const TH1*)
+   
+   * mhist/MH3.h:
+     - changed BIT(15) to BIT(17)
+     - changed BIT(16) to BIT(18)
+     - changed BIT(17) to BIT(19)
+
+   * mhist/MHMatrix.[h,cc]:
+     - added DrawDefRefInfo
+     - added CopyCrop
+     - added GetRandomArrayI
+     - changed DefRefMatrix to support the above
+     - fixed a bug in DefRefMatrix (evtcount2 in the last loop
+       instead of evtcount1)
+     - Don't do anything in the finalization if the matrix has the correct
+       dimensions
+     - fixed comment of DefRefMatrix
+     - changed number of first column from 1 to 0
+     - changed BIT(16) to BIT(17)
+
+
+
  2003/03/28: Wolfgang Wittek
 
     * mhist/MHMatrix.cc
-      - if nmaxevts>fM.GetNrows() set nmaxevts = fM.GetNrows()
+      - DefRefMatrix: if nmaxevts>fM.GetNrows() set 
+        nmaxevts = fM.GetNrows()
 
     * manalysis/MSelBasic.[h,cc]
Index: /trunk/MagicSoft/Mars/mbase/MParContainer.h
===================================================================
--- /trunk/MagicSoft/Mars/mbase/MParContainer.h	(revision 1878)
+++ /trunk/MagicSoft/Mars/mbase/MParContainer.h	(revision 1879)
@@ -36,5 +36,5 @@
 private:
     enum {
-        kIsSavedAsPrimitive = BIT(15)
+        kIsSavedAsPrimitive    = BIT(15),
     };
 
@@ -44,4 +44,8 @@
 
 public:
+    enum {
+        kEnableGraphicalOutput = BIT(16)
+    };
+
     MParContainer(const char *name="", const char *title="") : fName(name), fTitle(title), fLog(&gLog), fReadyToSave(kFALSE) {  }
     MParContainer(const TString &name, const TString &title) : fName(name), fTitle(title), fLog(&gLog), fReadyToSave(kFALSE) {  }
@@ -76,4 +80,6 @@
     virtual void   SetReadyToSave(Bool_t flag=kTRUE) { fReadyToSave=flag; }
     virtual Bool_t IsSavedAsPrimitive() const        { return TestBit(kIsSavedAsPrimitive); }
+    virtual void   EnableGraphicalOutput(Bool_t flag=kTRUE) { flag ? SetBit(kEnableGraphicalOutput) : ResetBit(kEnableGraphicalOutput);}
+    virtual Bool_t IsGraphicalOutputEnabled() const  { return TestBit(kEnableGraphicalOutput); }
 
     TMethodCall *GetterMethod(const char *name) const;
Index: /trunk/MagicSoft/Mars/mbase/MParList.h
===================================================================
--- /trunk/MagicSoft/Mars/mbase/MParList.h	(revision 1878)
+++ /trunk/MagicSoft/Mars/mbase/MParList.h	(revision 1879)
@@ -36,5 +36,5 @@
 
 public:
-    enum { kDoNotReset = BIT(15) };
+    enum { kDoNotReset = BIT(17) };
 
     MParList(const char *name=NULL, const char *title=NULL);
Index: /trunk/MagicSoft/Mars/mhist/MH.cc
===================================================================
--- /trunk/MagicSoft/Mars/mhist/MH.cc	(revision 1878)
+++ /trunk/MagicSoft/Mars/mhist/MH.cc	(revision 1879)
@@ -366,9 +366,9 @@
 // Both histograms must be of the same type: TH1, TH2 or TH3
 //
-void MH::SetBinning(TH1 *h, TH1 *x)
+void MH::SetBinning(TH1 *h, const TH1 *x)
 {
     if (h->InheritsFrom(TH3::Class()) && x->InheritsFrom(TH3::Class()))
     {
-        SetBinning((TH3*)h, x->GetXaxis(), x->GetYaxis(), x->GetZaxis());
+        SetBinning((TH3*)h, ((TH1*)x)->GetXaxis(), ((TH1*)x)->GetYaxis(), ((TH1*)x)->GetZaxis());
         return;
     }
@@ -377,5 +377,5 @@
     if (h->InheritsFrom(TH2::Class()) && x->InheritsFrom(TH2::Class()))
     {
-        SetBinning((TH2*)h, x->GetXaxis(), x->GetYaxis());
+        SetBinning((TH2*)h, ((TH1*)x)->GetXaxis(), ((TH1*)x)->GetYaxis());
         return;
     }
@@ -384,5 +384,5 @@
     if (h->InheritsFrom(TH1::Class()) && x->InheritsFrom(TH1::Class()))
     {
-        SetBinning(h, x->GetXaxis());
+        SetBinning(h, ((TH1*)x)->GetXaxis());
         return;
     }
Index: /trunk/MagicSoft/Mars/mhist/MH.h
===================================================================
--- /trunk/MagicSoft/Mars/mhist/MH.h	(revision 1878)
+++ /trunk/MagicSoft/Mars/mhist/MH.h	(revision 1879)
@@ -46,5 +46,5 @@
     static void SetBinning(TH3 *h, const TAxis *binsx, const TAxis *binsy, const TAxis *binsz);
 
-    static void SetBinning(TH1 *h, TH1 *x);
+    static void SetBinning(TH1 *h, const TH1 *x);
 
     static Bool_t ApplyBinning(const MParList &plist, TString name, TH1 *h);
Index: /trunk/MagicSoft/Mars/mhist/MH3.h
===================================================================
--- /trunk/MagicSoft/Mars/mhist/MH3.h	(revision 1878)
+++ /trunk/MagicSoft/Mars/mhist/MH3.h	(revision 1879)
@@ -27,7 +27,7 @@
 
     enum {
-        kIsLogx = BIT(15),
-        kIsLogy = BIT(16),
-        kIsLogz = BIT(17)
+        kIsLogx = BIT(17),
+        kIsLogy = BIT(18),
+        kIsLogz = BIT(19)
     };
 
Index: /trunk/MagicSoft/Mars/mhist/MHMatrix.cc
===================================================================
--- /trunk/MagicSoft/Mars/mhist/MHMatrix.cc	(revision 1878)
+++ /trunk/MagicSoft/Mars/mhist/MHMatrix.cc	(revision 1879)
@@ -250,11 +250,9 @@
         return kTRUE;
 
-    TMatrix m(fM);
-
-    fM.ResizeTo(fNumRow, fData->GetNumEntries());
-
-    TVector vold(fM.GetNcols());
-    for (int x=0; x<fM.GetNrows(); x++)
-        TMatrixRow(fM, x) = vold = TMatrixRow(m, x);
+    if (fNumRow != fM.GetNrows())
+    {
+        TMatrix m(fM);
+        CopyCrop(fM, m, fNumRow);
+    }
 
     return kTRUE;
@@ -687,11 +685,54 @@
 // ------------------------------------------------------------------------
 //
+//  Used in DefRefMatrix to display the result graphically
+//
+void MHMatrix::DrawDefRefInfo(const TH1 &hth, const TH1 &hthd, const TH1 &thsh, Int_t refcolumn)
+{
+    //
+    // Fill a histogram with the distribution after raduction
+    //
+    TH1F hta;
+    hta.SetName("hta");
+    hta.SetTitle("Distribution after reduction");
+    SetBinning(&hta, &hth);
+
+    for (Int_t i=0; i<fM.GetNrows(); i++)
+        hta.Fill(fM(i, refcolumn));
+
+    TCanvas *th1 = MakeDefCanvas(this);
+    th1->Divide(2,2);
+
+    th1->cd(1);
+    ((TH1&)hth).DrawCopy();   // real histogram before
+
+    th1->cd(2);
+    ((TH1&)hta).DrawCopy();   // histogram after
+
+    th1->cd(3);
+    ((TH1&)hthd).DrawCopy();  // correction factors
+
+    th1->cd(4);
+    ((TH1&)thsh).DrawCopy();  // target
+}
+
+// ------------------------------------------------------------------------
+//
+//  Resizes th etarget matrix to rows*source.GetNcol() and copies
+//  the data from the first (n)rows or the source into the target matrix.
+//
+void MHMatrix::CopyCrop(TMatrix &target, const TMatrix &source, Int_t rows)
+{
+    TVector v(source.GetNcols());
+
+    target.ResizeTo(rows, source.GetNcols());
+    for (Int_t ir=0; ir<rows; ir++)
+        TMatrixRow(target, ir) = v = TMatrixRow(source, ir);
+}
+
+// ------------------------------------------------------------------------
+//
 // Define the reference matrix
-//   refcolumn  number of the column (starting at 1)containing the variable, 
+//   refcolumn  number of the column (starting at 0) containing the variable,
 //              for which a target distribution may be given;
-//              if refcolumn is negative the target distribution will be set 
-//              equal to the real distribution; the events in the reference
-//              matrix will then be simply a random selection of the events 
-//              in the original matrix.
 //   thsh       histogram containing the target distribution of the variable
 //   nmaxevts   maximum number of events in the reference matrix
@@ -709,10 +750,4 @@
     }
 
-    if (refcolumn==0)
-    {
-        *fLog << err << dbginf << "Reference column 0 unknown." << endl;
-        return kFALSE;
-    }
-
     if (thsh.GetMinimum()<0)
     {
@@ -737,6 +772,4 @@
 
     //
-    // if refcolumn < 0 : select reference events randomly
-    //                    i.e. set the normaliztion factotrs equal to 1.0
     // refcol is the column number starting at 0; it is >= 0
     //
@@ -758,11 +791,9 @@
     // set up the real histogram (distribution before)
     //
-    TH1F hth("th", "data at input", nbins, frombin, tobin);
+    TH1F hth("th", "Distribution before reduction", nbins, frombin, tobin);
     for (Int_t j=0; j<nrows; j++)
-        hth.Fill(fM(j, refcolumn-1));
-
-    hth.DrawCopy();
-
-    TH1F hthd("thd", "correction factors", nbins, frombin, tobin);
+        hth.Fill(fM(j, refcolumn));
+
+    TH1F hthd("thd", "Correction factors", nbins, frombin, tobin);
     hthd.Divide((TH1F*)&thsh, &hth, 1, 1);
 
@@ -781,12 +812,6 @@
     // get random access
     //
-    TArrayF ranx(nrows);
-
-    TRandom3 rnd(0);
-    for (Int_t i=0; i<nrows; i++)
-        ranx[i] = rnd.Rndm(i);
-
     TArrayI ind(nrows);
-    TMath::Sort(nrows, ranx.GetArray(), ind.GetArray(), kTRUE);
+    GetRandomArrayI(ind);
 
     //
@@ -805,5 +830,5 @@
     //
     TVector v(fM.GetNrows());
-    v = TMatrixColumn(fM, refcolumn-1);
+    v = TMatrixColumn(fM, refcolumn);
     v += -frombin;
     v *= 1/dbin;
@@ -842,21 +867,17 @@
     // this is the matrix to be used in the g/h separation
     //
-    fM.ResizeTo(evtcount1, ncols);
+    CopyCrop(fM, mnewtmp, evtcount1);
     fNumRow = evtcount1;
-    for (ir=0; ir<evtcount1; ir++)
-        TMatrixRow(fM, ir) = vold = TMatrixRow(mnewtmp, ir);
 
     if (evtcount1 < nmaxevts)
         *fLog << warn << "The reference sample contains less events (" << evtcount1 << ") than required (" << nmaxevts << ")" << endl;
 
+    if (TestBit(kEnableGraphicalOutput))
+        DrawDefRefInfo(hth, hthd, thsh, refcolumn);
+
     if (!rest)
         return kTRUE;
 
-    rest->ResizeTo(evtcount2, ncols);
-    for (ir=0; ir<evtcount1; ir++)
-    {
-        TVector vold(fM.GetNcols());
-        TMatrixRow(*rest, ir) = vold = TMatrixRow(mrest, ir);
-    }
+    CopyCrop(*rest, mrest, evtcount2);
 
     return kTRUE;
@@ -865,12 +886,22 @@
 // ------------------------------------------------------------------------
 //
+// Returns a array containing randomly sorted indices
+//
+void MHMatrix::GetRandomArrayI(TArrayI &ind) const
+{
+    const Int_t rows = ind.GetSize();
+
+    TArrayF ranx(rows);
+
+    TRandom3 rnd(0);
+    for (Int_t i=0; i<rows; i++)
+        ranx[i] = rnd.Rndm(i);
+
+    TMath::Sort(rows, ranx.GetArray(), ind.GetArray(), kTRUE);
+}
+
+// ------------------------------------------------------------------------
+//
 // Define the reference matrix
-//   refcolumn  number of the column (starting at 1)containing the variable, 
-//              for which a target distribution may be given;
-//              if refcolumn is negative the target distribution will be set 
-//              equal to the real distribution; the events in the reference
-//              matrix will then be simply a random selection of the events 
-//              in the original matrix.
-//   thsh       histogram containing the target distribution of the variable
 //   nmaxevts   maximum number of events in the reference matrix
 //   rest       a TMatrix conatining the resulting (not choosen)
@@ -878,4 +909,9 @@
 //              are not interested in this
 //
+//              the target distribution will be set
+//              equal to the real distribution; the events in the reference
+//              matrix will then be simply a random selection of the events 
+//              in the original matrix.
+//
 Bool_t MHMatrix::DefRefMatrix(Int_t nmaxevts, TMatrix *rest)
 {
@@ -907,12 +943,6 @@
     // get random access
     //
-    TArrayF ranx(nrows);
-
-    TRandom3 rnd(0);
-    for (Int_t i=0; i<nrows; i++)
-        ranx[i] = rnd.Rndm(i);
-
     TArrayI ind(nrows);
-    TMath::Sort(nrows, ranx.GetArray(), ind.GetArray(), kTRUE);
+    GetRandomArrayI(ind);
 
     //
@@ -942,11 +972,6 @@
     // this is the matrix to be used in the g/h separation
     //
-    fM.ResizeTo(evtcount1, ncols);
+    CopyCrop(fM, mnewtmp, evtcount1);
     fNumRow = evtcount1;
-    for (Int_t ir=0; ir<evtcount1; ir++)
-    {
-        TVector vold(fM.GetNcols());
-        TMatrixRow(fM, ir) = vold = TMatrixRow(mnewtmp, ir);
-    }
 
     if (evtcount1 < nmaxevts)
@@ -956,67 +981,7 @@
         return kTRUE;
 
-    rest->ResizeTo(evtcount2, ncols);
-    for (Int_t ir=0; ir<evtcount1; ir++)
-    {
-        TVector vold(fM.GetNcols());
-        TMatrixRow(*rest, ir) = vold = TMatrixRow(mrest, ir);
-    }
+    CopyCrop(*rest, mrest, evtcount2);
 
     return kTRUE;
-
-    /*
-     TMatrix mnew(evtcount, nconl);
-     for (Int_t ir=0; ir<evtcount; ir++)
-     for (Int_t ic=0; ic<fNcols; ic++)
-     fM(ir,ic)   = mnewtmp(ir,ic);
-
-     //
-     //  test: print new matrix (part)
-     //
-     *fLog << "DefRefMatrix:  Event matrix (output) :" << endl;
-     *fLog << "DefRefMatrix:  Nrows, Ncols = " << mnew.GetNrows();
-     *fLog << " " << mnew.GetNcols() << endl;
-
-     for (Int_t ir=0;ir<10; ir++)
-     {
-     *fLog <<ir <<" ";
-     for (Int_t ic=0; ic<mnew.GetNcols(); ic++)
-     cout<<Mnew(ir,ic)<<" ";
-     *fLog <<endl;
-     }
-     */
-
-    /*
-     //  test print new bin contents
-     *fLog << "MHMatrix::DefRefMatrix;  new histogram: " << endl;
-     for (Int_t j=1; j<=fnbins; j++)
-     {
-     float a = fHthaft->GetBinContent(j);
-     *fLog << j << "  "<< a << "   ";
-     }
-     *fLog <<endl;
-     */
-
-    /*
-     //---------------------------------------------------------
-     // ==== plot four histograms
-     TCanvas *th1 = new TCanvas (fName, fName, 1);
-     th1->Divide(2,2);
-
-     th1->cd(1);
-     ((TH1F*)fHthsh)->DrawCopy();      // target
-
-     th1->cd(2);
-     ((TH1F*)fHth)->DrawCopy();        // real histogram before
-
-     th1->cd(3);
-     ((TH1F*)fHthd)->DrawCopy();       // correction factors
-
-     th1->cd(4);
-     ((TH1F*)fHthaft)->DrawCopy();     // histogram after
-
-     //---------------------------------------------------------
-     */
-    //return kTRUE;
 }
 
@@ -1028,9 +993,9 @@
 Int_t MHMatrix::Read(const char *name)
 {
-  Int_t ret = TObject::Read(name);
-  SetName(name);
-
-  return ret;
-}
-
-// --------------------------------------------------------------------------
+    Int_t ret = TObject::Read(name);
+    SetName(name);
+
+    return ret;
+}
+
+// --------------------------------------------------------------------------
Index: /trunk/MagicSoft/Mars/mhist/MHMatrix.h
===================================================================
--- /trunk/MagicSoft/Mars/mhist/MHMatrix.h	(revision 1878)
+++ /trunk/MagicSoft/Mars/mhist/MHMatrix.h	(revision 1879)
@@ -41,5 +41,5 @@
     enum {
         kIsOwner  = BIT(14),
-        kIsLocked = BIT(16)
+        kIsLocked = BIT(17)
     };
 
@@ -51,4 +51,7 @@
     Bool_t Finalize();
 
+    void DrawDefRefInfo(const TH1 &hth, const TH1 &hthd, const TH1 &thsh, Int_t refcolumn);
+    void GetRandomArrayI(TArrayI &ind) const;
+
     void StreamPrimitive(ofstream &out) const;
 
@@ -58,4 +61,6 @@
     MHMatrix(const TMatrix &m, const char *name=NULL, const char *title=NULL);
     ~MHMatrix();
+
+    static void CopyCrop(TMatrix &target, const TMatrix &source, Int_t rows);
 
     void Lock()   { SetBit(kIsLocked); }
