Index: trunk/MagicSoft/Mars/mbase/Makefile
===================================================================
--- trunk/MagicSoft/Mars/mbase/Makefile	(revision 845)
+++ trunk/MagicSoft/Mars/mbase/Makefile	(revision 846)
@@ -38,4 +38,5 @@
            MEvtLoop.cc \
            MReadTree.cc \
+           MWriteAsciiFile.cc \
 	   MArray.cc \
 	   MArrayB.cc \
Index: trunk/MagicSoft/Mars/mhist/HistIncl.h
===================================================================
--- trunk/MagicSoft/Mars/mhist/HistIncl.h	(revision 845)
+++ trunk/MagicSoft/Mars/mhist/HistIncl.h	(revision 846)
@@ -1,3 +1,5 @@
 #ifndef __CINT__
 
+#include <TF1.h>
+
 #endif // __CINT__
Index: trunk/MagicSoft/Mars/mhist/HistLinkDef.h
===================================================================
--- trunk/MagicSoft/Mars/mhist/HistLinkDef.h	(revision 845)
+++ trunk/MagicSoft/Mars/mhist/HistLinkDef.h	(revision 846)
@@ -9,4 +9,5 @@
 #pragma link C++ class MHHillas;
 #pragma link C++ class MHStarMap;
+#pragma link C++ class MHMcEnergy;
 
 #pragma link C++ class MFillHFadc;
Index: trunk/MagicSoft/Mars/mhist/MHMcEnergy.cc
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHMcEnergy.cc	(revision 846)
+++ trunk/MagicSoft/Mars/mhist/MHMcEnergy.cc	(revision 846)
@@ -0,0 +1,129 @@
+/* ======================================================================== *\
+!
+! *
+! * 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): Javier Lopez 05/2001 (jlopez@ifae.es)
+!
+!   Copyright: MAGIC Software Development, 2000-2001
+!
+!
+\* ======================================================================== */
+
+#include "MHMcEnergy.h" 
+
+#include <iostream.h>
+
+#include <TH1.h> 
+#include <TF1.h> 
+#include <TPaveLabel.h> 
+
+ClassImp(MHMcEnergy)
+
+MHMcEnergy::MHMcEnergy(const UInt_t idx)
+{ 
+    //
+    //   default constructor
+    //
+
+    //  - we initialize the histogram and the gaus function
+    //  - we have to give diferent names for the diferent histograms because
+    //    root don't allow us to have diferent histograms with the same name
+
+    char aux[15];
+
+    strcpy(aux, "hLogEnergy");
+    if (idx>0)
+        sprintf(aux+10, ";%i", idx);
+    hLogEner = new TH1F(aux, "", 100, 0.5, 4.5);
+
+    strcpy(aux, "fLogEnergy");
+    if (idx>0)
+        sprintf(aux+10, ";%i", idx);
+    fLogEner = new TF1(aux, "gaus", 1., 3.);
+}
+
+MHMcEnergy::~MHMcEnergy()
+{
+    delete hLogEner;
+    delete fLogEner;
+}
+
+void MHMcEnergy::Fill(Float_t log10E, Float_t w)
+{
+    hLogEner->Fill(log10E, w);
+}
+
+void MHMcEnergy::Fit(Axis_t xxmin, Axis_t xxmax)
+{
+    //
+    // FIXME: R means: use the range specified in the function (xxmin, xxmax are ignored!)
+    //        Q means: quiet (why?)
+    //
+    //
+    hLogEner->Fit(fLogEner->GetName(), "RQ", "", xxmin, xxmax);
+}
+
+void MHMcEnergy::Draw(Option_t *option)
+{
+    char text[50];
+    sprintf(text, "Energy Threshold = %4.1f +- %4.1f GeV",
+            GetThreshold(), GetThresholdErr());
+
+    const Float_t min = hLogEner->GetMinimum();
+    const Float_t max = hLogEner->GetMaximum();
+    const Float_t sum = min+max;
+
+    TPaveLabel* label = new TPaveLabel(2.2, 0.75*sum, 4.4, 0.90*sum, text);
+
+    hLogEner->SetYTitle("dN/dE") ;
+    hLogEner->SetXTitle("log(E) [GeV]") ;
+    hLogEner->Draw(option) ;
+    label->SetFillColor(10);
+    label->SetTextSize(0.3);
+    label->Draw();
+}
+
+void MHMcEnergy::Print(Option_t*)
+{
+    cout << "Threshold: " << GetThreshold() << " +- " << GetThresholdErr() << endl;
+}
+
+Float_t MHMcEnergy::GetThreshold() const
+{
+    const Float_t p1 = fLogEner->GetParameter(1);
+
+    return pow(10, p1);
+}
+
+Float_t MHMcEnergy::GetThresholdErr() const
+{
+    const Float_t lg10  = log(10);
+    const Float_t p1    = fLogEner->GetParameter(1);
+    const Float_t p1err = fLogEner->GetParError(1);
+
+    return pow(10, p1) * p1err * lg10;
+}
+
+Float_t MHMcEnergy::GetGaussPeak() const
+{
+    return fLogEner->GetParameter(1);
+}
+
+Float_t MHMcEnergy::GetGaussSigma() const
+{
+    return fLogEner->GetParameter(2);
+}
+
Index: trunk/MagicSoft/Mars/mhist/MHMcEnergy.h
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHMcEnergy.h	(revision 846)
+++ trunk/MagicSoft/Mars/mhist/MHMcEnergy.h	(revision 846)
@@ -0,0 +1,41 @@
+#ifndef MHMCENERGY_H
+#define MHMCENERGY_H
+
+#ifndef MAGIC_H
+#include "MAGIC.h"
+#endif
+#ifndef MPARCONTAINER_H
+#include "MParContainer.h"
+#endif
+
+class TH1F;
+class TF1;
+
+class MHMcEnergy : public MParContainer
+{
+private:
+
+    TH1F *hLogEner;  // histogram with the logarith of the energy
+    TF1  *fLogEner;  // gausian function to fit the histogram
+
+public:
+
+    MHMcEnergy(const UInt_t idx=0);
+    ~MHMcEnergy();
+
+    Float_t GetThreshold() const;
+    Float_t GetThresholdErr() const;
+
+    Float_t GetGaussPeak() const;
+    Float_t GetGaussSigma() const;
+
+    void Fill(Float_t log10E, Float_t w);
+    void Fit(Axis_t xxmin, Axis_t xxmax);
+
+    void Draw(Option_t* option = "");
+    void Print(Option_t* option = NULL);
+
+    ClassDef(MHMcEnergy, 1)  // Histogram container for montecarlo energy
+};
+
+#endif
Index: trunk/MagicSoft/Mars/mhist/Makefile
===================================================================
--- trunk/MagicSoft/Mars/mhist/Makefile	(revision 845)
+++ trunk/MagicSoft/Mars/mhist/Makefile	(revision 846)
@@ -34,5 +34,6 @@
            MHFadcCam.cc \
            MHHillas.cc \
-           MHStarMap.cc
+           MHStarMap.cc \
+           MHMcEnergy.cc
 
 SRCS    = $(SRCFILES)
Index: trunk/MagicSoft/Mars/mmontecarlo/MCollArea.h
===================================================================
--- trunk/MagicSoft/Mars/mmontecarlo/MCollArea.h	(revision 845)
+++ trunk/MagicSoft/Mars/mmontecarlo/MCollArea.h	(revision 846)
@@ -12,5 +12,5 @@
 // because of some strange reason this cannot be put into MonteCarloIncl
 //
-#ifndef TH1_H
+#ifndef ROOT_TH1
 #include <TH1.h>
 #endif
Index: trunk/MagicSoft/Mars/mmontecarlo/MMcThresholdCalc.cc
===================================================================
--- trunk/MagicSoft/Mars/mmontecarlo/MMcThresholdCalc.cc	(revision 846)
+++ trunk/MagicSoft/Mars/mmontecarlo/MMcThresholdCalc.cc	(revision 846)
@@ -0,0 +1,159 @@
+/* ======================================================================== *\
+!
+! *
+! * 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): Javier Lopez 05/2001 (jlopez@ifae.es)
+!              Thomas Bretz 06/2001 (tbretz@uni-sw.gwdg.de)
+!
+!   Copyright: MAGIC Software Development, 2000-2001
+!
+!
+\* ======================================================================== */
+
+/////////////////////////////////////////////////////////////////////////////
+//                                                                         //
+//  MMcThresholdCalc                                                       //
+//                                                                         //
+/////////////////////////////////////////////////////////////////////////////
+
+#include "MMcThresholdCalc.h"
+
+#include <math.h>
+
+#include "MParList.h"
+
+#include "MLog.h"
+#include "MLogManip.h"
+
+#include "MMcEvt.hxx"
+#include "MMcTrig.hxx"
+
+#include "MHMcEnergy.h"
+
+ClassImp(MMcThresholdCalc)
+
+MMcThresholdCalc::MMcThresholdCalc(const UInt_t dim, const char* name,
+                                   const char* title) : fDimension(dim)
+{
+    *fName  = name  ? name  : "MMcThresholdCalc";
+    *fTitle = title ? title : "Task to calculate the energy threshold from Monte Carlo";
+
+    fMcTrig     = new (MMcTrig*)[fDimension];
+    fHMcEnergy  = new (MHMcEnergy*)[fDimension];
+    fMustDelete = new Bool_t[fDimension];
+
+    for (unsigned int i=0; i<fDimension; i++)
+        fMustDelete=kFALSE;
+}
+
+MMcThresholdCalc::~MMcThresholdCalc()
+{
+    for (unsigned int i=0; i<fDimension; i++)
+        if (fMustDelete[i])
+            delete fHMcEnergy[i];
+
+    delete fMcTrig;
+    delete fHMcEnergy;
+    delete fMustDelete;
+}
+
+Bool_t MMcThresholdCalc::PreProcess(MParList* pList)
+{
+    // connect Monte Carlo data with this task
+
+    char auxname[15]="MMcTrig"; // string to write container names
+
+    fMcEvt = (MMcEvt*)pList->FindObject("MMcEvt");
+    if (!fMcEvt)
+    {
+        *fLog << dbginf << "MMcEvt not found... aborting." << endl;
+        return kFALSE;
+    }
+
+    for (unsigned int i=0; i<fDimension; i++)
+    {
+        if (fDimension>1)
+            sprintf(auxname+7, ";%i.", i+1);
+
+        fMcTrig[i] = (MMcTrig*)pList->FindObject(auxname);
+        if (fMcTrig[i])
+            continue;
+
+        *fLog << dbginf << "'MMcTrig";
+        if (fDimension>1)
+            *fLog << ";" << i+1;
+        *fLog << "' not found... aborting." << endl;
+
+        return kFALSE;
+    }
+
+    strcpy(auxname, "MHMcEnergy");
+    for (unsigned int i=0; i<fDimension; i++)
+    {
+        if (fDimension>1)
+            sprintf(auxname+10, ";%i.", i+1);
+
+        fHMcEnergy[i] = (MHMcEnergy*)pList->FindObject(auxname);
+        if (fHMcEnergy[i])
+            continue;
+
+        *fLog << dbginf << "'" << auxname << "' not found in list... creating." << endl;
+
+        fHMcEnergy[i] = new MHMcEnergy(fDimension>1 ? i+1 : 0);
+        fMustDelete[i] = kTRUE;
+        pList->AddToList(fHMcEnergy[i]);
+    }
+
+    return kTRUE;
+}
+
+Bool_t MMcThresholdCalc::Process()
+{
+    const Float_t energy   = fMcEvt->GetEnergy();
+    const Float_t lg10     = log10(energy);
+    const Float_t reciproc = 1./energy;
+
+    for (unsigned int i=0; i<fDimension; i++)
+    {
+        if (fMcTrig[i]->GetFirstLevel()<=0)
+            continue;
+
+        fHMcEnergy[i]->Fill(lg10, reciproc);
+    }
+
+    return kTRUE;
+}
+
+Bool_t MMcThresholdCalc::PostProcess()
+{
+    // fit the energy distribution to get the threshold
+
+    const Float_t sqrt2 = sqrt(2);
+
+    for (unsigned int i=0; i<fDimension; i++)
+    {
+        MHMcEnergy &h = *fHMcEnergy[i];
+
+        const Float_t peak  = h.GetGaussPeak();
+        const Float_t sigma = h.GetGaussSigma();
+
+        h.Fit(1, 3);
+        h.Fit(peak - 2.   *sigma, peak + 2.   *sigma);
+        h.Fit(peak - sqrt2*sigma, peak + sqrt2*sigma);
+    }
+    return kTRUE;
+}
+
Index: trunk/MagicSoft/Mars/mmontecarlo/MMcThresholdCalc.h
===================================================================
--- trunk/MagicSoft/Mars/mmontecarlo/MMcThresholdCalc.h	(revision 846)
+++ trunk/MagicSoft/Mars/mmontecarlo/MMcThresholdCalc.h	(revision 846)
@@ -0,0 +1,47 @@
+#ifndef MMCTHREASHOLDCALC_H
+#define MMCTHREASHOLDCALC_H
+
+/////////////////////////////////////////////////////////////////////////////
+//                                                                         //
+// MMcThresholdCalc                                                        //
+//                                                                         //
+// Compute the energy threshold from Monte Carlo data                      //
+//                                                                         //
+/////////////////////////////////////////////////////////////////////////////
+
+#ifndef MTASK_h
+#include "MTask.h"
+#endif
+
+class MMcEvt;
+class MMcTrig;
+class MHMcEnergy;
+
+class MMcThresholdCalc : public MTask
+{
+private:
+    UInt_t fDimension;
+
+    MMcEvt      *fMcEvt;       // Container with Monte Carlo information
+    MMcTrig    **fMcTrig;      // Container with Monte Carlo trigger information
+    MHMcEnergy **fHMcEnergy;   // Container where we save the energy (threshold)
+
+    Bool_t      *fMustDelete;  // delete indicator for selfcreated energy hists
+
+public:
+
+    MMcThresholdCalc(const UInt_t dim = 1,
+                     const char* name = NULL, const char* title = NULL);
+    ~MMcThresholdCalc();
+
+    Bool_t PreProcess(MParList* pList);
+    Bool_t Process();
+    Bool_t PostProcess();
+
+    ClassDef(MMcThresholdCalc, 0) // Task to fill the energy threshold information
+};
+
+#endif
+
+
+
Index: trunk/MagicSoft/Mars/mmontecarlo/Makefile
===================================================================
--- trunk/MagicSoft/Mars/mmontecarlo/Makefile	(revision 845)
+++ trunk/MagicSoft/Mars/mmontecarlo/Makefile	(revision 846)
@@ -22,5 +22,5 @@
 #  connect the include files defined in the config.mk file
 #
-INCLUDES = -I. -I../mbase -I../mmc
+INCLUDES = -I. -I../mbase -I../mmc -I../mhist
 
 #------------------------------------------------------------------------------
@@ -30,7 +30,5 @@
 SRCFILES = MCollArea.cc \
 	   MCollAreaTrigger.cc \
-	   MMcEnerHisto.cc \
-	   MMcEnerThre.cc \
-	   MMcEnerThreCalc.cc
+	   MMcThresholdCalc.cc
 
 SRCS    = $(SRCFILES)
Index: trunk/MagicSoft/Mars/mmontecarlo/MonteCarloLinkDef.h
===================================================================
--- trunk/MagicSoft/Mars/mmontecarlo/MonteCarloLinkDef.h	(revision 845)
+++ trunk/MagicSoft/Mars/mmontecarlo/MonteCarloLinkDef.h	(revision 846)
@@ -7,7 +7,5 @@
 #pragma link C++ class MCollArea ; 
 #pragma link C++ class MCollAreaTrigger ; 
-#pragma link C++ class MMcEnerHisto ;
-#pragma link C++ class MMcEnerThre ;
-#pragma link C++ class MMcEnerThreCalc ;
+#pragma link C++ class MMcThresholdCalc ;
 
 #endif
