Index: trunk/MagicSoft/Mars/mhflux/MAlphaFitter.cc
===================================================================
--- trunk/MagicSoft/Mars/mhflux/MAlphaFitter.cc	(revision 5002)
+++ trunk/MagicSoft/Mars/mhflux/MAlphaFitter.cc	(revision 5002)
@@ -0,0 +1,195 @@
+/* ======================================================================== *\
+!
+! *
+! * This file is part of MARS, the MAGIC Analysis and Reconstruction
+! * Software. It is distributed to you in the hope that it can be a useful
+! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
+! * It is distributed WITHOUT ANY WARRANTY.
+! *
+! * Permission to use, copy, modify and distribute this software and its
+! * documentation for any purpose is hereby granted without fee,
+! * provided that the above copyright notice appear in all copies and
+! * that both that copyright notice and this permission notice appear
+! * in supporting documentation. It is provided "as is" without express
+! * or implied warranty.
+! *
+!
+!
+!   Author(s): Thomas Bretz, 3/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
+!
+!   Copyright: MAGIC Software Development, 2000-2004
+!
+!
+\* ======================================================================== */
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// MAlphaFitter
+//
+// Create a single Alpha-Plot. The alpha-plot is fitted online. You can
+// check the result when it is filles in the MStatusDisplay
+// For more information see MHFalseSource::FitSignificance
+//
+// For convinience (fit) the output significance is stored in a
+// container in the parlisrt
+//
+// PRELIMINARY!
+//
+//////////////////////////////////////////////////////////////////////////////
+#include "MAlphaFitter.h"
+
+#include <TF1.h>
+#include <TH1.h>
+
+#include <TLatex.h>
+
+#include "MMath.h"
+
+ClassImp(MAlphaFitter);
+
+using namespace std;
+
+// --------------------------------------------------------------------------
+//
+// This is a preliminary implementation of a alpha-fit procedure for
+// all possible source positions. It will be moved into its own
+// more powerfull class soon.
+//
+// The fit function is "gaus(0)+pol2(3)" which is equivalent to:
+//   [0]*exp(-0.5*((x-[1])/[2])^2) + [3] + [4]*x + [5]*x^2
+// or
+//   A*exp(-0.5*((x-mu)/sigma)^2) + a + b*x + c*x^2
+//
+// Parameter [1] is fixed to 0 while the alpha peak should be
+// symmetric around alpha=0.
+//
+// Parameter [4] is fixed to 0 because the first derivative at
+// alpha=0 should be 0, too.
+//
+// In a first step the background is fitted between bgmin and bgmax,
+// while the parameters [0]=0 and [2]=1 are fixed.
+//
+// In a second step the signal region (alpha<sigmax) is fittet using
+// the whole function with parameters [1], [3], [4] and [5] fixed.
+//
+// The number of excess and background events are calculated as
+//   s = int(hist,    0, 1.25*sigint)
+//   b = int(pol2(3), 0, 1.25*sigint)
+//
+// The Significance is calculated using the Significance() member
+// function.
+//
+Bool_t MAlphaFitter::Fit(TH1D &h, Bool_t paint)
+{
+    Double_t sigint=fSigInt;
+    Double_t sigmax=fSigMax;
+    Double_t bgmin=fBgMin;
+    Double_t bgmax=fBgMax;
+    Byte_t polynom=fPolynom;
+
+    // Implementing the function yourself is only about 5% faster
+    TF1 func("", Form("gaus(0) + pol%d(3)", polynom), 0, 90);
+    //TF1 func("", Form("[0]*(TMath::Gaus(x, [1], [2])+TMath::Gaus(x, -[1], [2]))+pol%d(3)", polynom), 0, 90);
+    TArrayD maxpar(func.GetNpar());
+
+    func.FixParameter(1, 0);
+    func.FixParameter(4, 0);
+    func.SetParLimits(2, 0, 90);
+    func.SetParLimits(3, -1, 1);
+
+    const Double_t alpha0 = h.GetBinContent(1);
+    const Double_t alphaw = h.GetXaxis()->GetBinWidth(1);
+
+    // Check for the regios which is not filled...
+    if (alpha0==0)
+        return kFALSE; //*fLog << warn << "Histogram empty." << endl;
+
+    // First fit a polynom in the off region
+    func.FixParameter(0, 0);
+    func.FixParameter(2, 1);
+    func.ReleaseParameter(3);
+
+    for (int i=5; i<func.GetNpar(); i++)
+        func.ReleaseParameter(i);
+
+    // options : N  do not store the function, do not draw
+    //           I  use integral of function in bin rather than value at bin center
+    //           R  use the range specified in the function range
+    //           Q  quiet mode
+    h.Fit(&func, "NQI", "", bgmin, bgmax);
+
+    fChiSqBg = func.GetChisquare()/func.GetNDF();
+
+    // ------------------------------------
+    if (paint)
+    {
+        func.SetRange(0, 90);
+        func.SetLineColor(kRed);
+        func.SetLineWidth(2);
+        func.Paint("same");
+    }
+    // ------------------------------------
+
+    func.ReleaseParameter(0);
+    //func.ReleaseParameter(1);
+    func.ReleaseParameter(2);
+    func.FixParameter(3, func.GetParameter(3));
+    for (int i=5; i<func.GetNpar(); i++)
+        func.FixParameter(i, func.GetParameter(i));
+
+    // Do not allow signals smaller than the background
+    const Double_t A  = alpha0-func.GetParameter(3);
+    const Double_t dA = TMath::Abs(A);
+    func.SetParLimits(0, -dA*4, dA*4);
+    func.SetParLimits(2, 0, 90);
+
+    // Now fit a gaus in the on region on top of the polynom
+    func.SetParameter(0, A);
+    func.SetParameter(2, sigmax*0.75);
+
+    // options : N  do not store the function, do not draw
+    //           I  use integral of function in bin rather than value at bin center
+    //           R  use the range specified in the function range
+    //           Q  quiet mode
+    h.Fit(&func, "NQI", "", 0, sigmax);
+
+    fChiSqSignal  = func.GetChisquare()/func.GetNDF();
+    fSigmaGaus    = func.GetParameter(2);
+
+    //const Bool_t ok = NDF>0 && chi2<2.5*NDF;
+
+    // ------------------------------------
+    if (paint)
+    {
+        func.SetLineColor(kGreen);
+        func.SetLineWidth(2);
+        func.Paint("same");
+    }
+    // ------------------------------------
+    const Double_t s   = func.Integral(0, sigint)/alphaw;
+    func.SetParameter(0, 0);
+    func.SetParameter(2, 1);
+    const Double_t b   = func.Integral(0, sigint)/alphaw;
+
+    fSignificance = MMath::SignificanceLiMaSigned(s, b);
+    //exc = s-b;
+
+    const Double_t uin = 1.25*sigint;
+    const Int_t    bin = h.GetXaxis()->FindFixBin(uin);
+    fIntegralMax  = h.GetBinLowEdge(bin+1);
+    fExcessEvents = h.Integral(0, bin)-func.Integral(0, fIntegralMax)/alphaw;
+
+    return kTRUE;
+}
+
+void MAlphaFitter::PaintResult(Float_t x, Float_t y, Float_t size) const
+{
+    TLatex text(x, y, Form("\\sigma_{Li/Ma}=%.1f (\\alpha<%.1f\\circ)  \\omega=%.1f\\circ  E=%d  (\\alpha<%.1f\\circ)  (\\chi_{b}^{2}=%.1f  \\chi_{s}^{2}=%.1f)",
+                           fSignificance, fSigInt, fSigmaGaus,
+                           (int)fExcessEvents, fIntegralMax,
+                           fChiSqBg, fChiSqSignal));
+
+    text.SetBit(TLatex::kTextNDC);
+    text.SetTextSize(size);
+    text.Paint();
+}
Index: trunk/MagicSoft/Mars/mhflux/MAlphaFitter.h
===================================================================
--- trunk/MagicSoft/Mars/mhflux/MAlphaFitter.h	(revision 5002)
+++ trunk/MagicSoft/Mars/mhflux/MAlphaFitter.h	(revision 5002)
@@ -0,0 +1,65 @@
+#ifndef MARS_MAlphaFitter
+#define MARS_MAlphaFitter
+
+#ifndef MARS_MParContainer
+#include "MParContainer.h"
+#endif
+
+class TH1D;
+
+class MAlphaFitter : public MParContainer
+{
+private:
+    Float_t fSigInt;
+    Float_t fSigMax;
+    Float_t fBgMin;
+    Float_t fBgMax;
+    Int_t   fPolynom;
+
+    Double_t fSignificance;
+    Double_t fExcessEvents;
+    Double_t fChiSqSignal;
+    Double_t fChiSqBg;
+    Double_t fSigmaGaus;
+    Double_t fIntegralMax;
+
+public:
+    MAlphaFitter() : fSigInt(10), fSigMax(75), fBgMin(45), fBgMax(85), fPolynom(1)
+    {
+    }
+
+    MAlphaFitter(const MAlphaFitter &f)
+    {
+        f.Copy(*this);
+    }
+
+    void Copy(TObject &o) const
+    {
+        MAlphaFitter &f = static_cast<MAlphaFitter&>(o);
+
+        f.fSigInt  = fSigInt;
+        f.fSigMax  = fSigMax;
+        f.fBgMin   = fBgMin;
+        f.fBgMax   = fBgMax;
+        f.fPolynom = fPolynom;
+    }
+
+    void SetSignalIntegralMax(Float_t s) { fSigInt = s; }
+    void SetSignalFitMax(Float_t s)      { fSigMax = s; }
+    void SetBackgroundFitMin(Float_t s)  { fBgMin = s; }
+    void SetBackgroundFitMax(Float_t s)  { fBgMax = s; }
+    void SetNumPolynom(Int_t s)          { fPolynom = s; }
+
+    Double_t GetExcessEvents() const { return fExcessEvents; }
+    Double_t GetSignificance() const { return fSignificance; }
+    Double_t GetChiSqSignal() const  { return fChiSqSignal; }
+    Double_t GetChiSqBg() const      { return fChiSqBg; }
+    Double_t GetSigmaGaus() const    { return fSigmaGaus; }
+
+    void PaintResult(Float_t x=0.04, Float_t y=0.94, Float_t size=0.035) const;
+
+    Bool_t Fit(TH1D &h, Bool_t paint=kFALSE);
+    ClassDef(MAlphaFitter, 1)
+};
+
+#endif
Index: trunk/MagicSoft/Mars/mhflux/MHAlpha.cc
===================================================================
--- trunk/MagicSoft/Mars/mhflux/MHAlpha.cc	(revision 5001)
+++ trunk/MagicSoft/Mars/mhflux/MHAlpha.cc	(revision 5002)
@@ -40,5 +40,4 @@
 
 #include <TF1.h>
-#include <TH2.h>
 #include <TGraph.h>
 #include <TStyle.h>
@@ -60,5 +59,4 @@
 #include "MHMatrix.h"
 
-#include "MMath.h"
 #include "MBinning.h"
 #include "MParList.h"
@@ -593,4 +591,5 @@
 // function.
 //
+/*
 Bool_t MAlphaFitter::Fit(TH1D &h, Bool_t paint)
 {
@@ -707,5 +706,5 @@
     text.Paint();
 }
-
+*/
 Bool_t MHAlpha::Finalize()
 {
Index: trunk/MagicSoft/Mars/mhflux/MHAlpha.h
===================================================================
--- trunk/MagicSoft/Mars/mhflux/MHAlpha.h	(revision 5001)
+++ trunk/MagicSoft/Mars/mhflux/MHAlpha.h	(revision 5002)
@@ -8,4 +8,8 @@
 #ifndef MARS_MTime
 #include "MTime.h"
+#endif
+
+#ifndef MARS_MAlphaFitter
+#include "MAlphaFitter.h"
 #endif
 
@@ -20,5 +24,5 @@
 class MPointingPos;
 
-
+/*
 class MAlphaFitter : public MParContainer
 {
@@ -75,5 +79,5 @@
     ClassDef(MAlphaFitter, 1)
 };
-
+*/
 
 class MHAlpha : public MH
Index: trunk/MagicSoft/Mars/mhflux/Makefile
===================================================================
--- trunk/MagicSoft/Mars/mhflux/Makefile	(revision 5001)
+++ trunk/MagicSoft/Mars/mhflux/Makefile	(revision 5002)
@@ -24,5 +24,6 @@
            -I../mastro -I../mpedestal -I../msignal -I../mbadpixels
 
-SRCFILES = MHAlpha.cc \
+SRCFILES = MAlphaFitter.cc \
+	   MHAlpha.cc \
 	   MHEffectiveOnTime.cc \
            MHFalseSource.cc
