Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 9273)
+++ trunk/MagicSoft/Mars/Changelog	(revision 9274)
@@ -19,4 +19,44 @@
                                                  -*-*- END OF LINE -*-*-
 
+ 2009/01/27 Thomas Bretz
+
+  * macros/rootlogon.C:
+     - added new subdirectories
+
+   * melectronics/MAnalogSignal.[h,cc]:
+     - added the posssibility to add a pulse from a TF1
+     - removed the printout if out of range
+     - returns whether the range had to be adapted
+     - changed the Discriminate function to allow for giving a start
+       and end time of the search
+     - imporved the fining of the falling and rising edge
+     - removed the printout in Discriminate
+
+   * msimcamera/MSimCamera.[h,cc]:
+     - moved the pulse shape specific code into a new class MPulseShape
+     - Fixed the range of the digitization
+
+   * melectronics/MPulseShape.[h,cc]:
+     - added
+
+   * melectronics/Makefile, melectronics/ElectronicsLinkDef.h:
+     - added MPulseShape
+
+   * msimcamera/MSimGeomCam.[h,cc]:
+     - Fixed the time window for random photons (Maybe move to a later step
+       in the analysis?)
+
+   * msimcamera/MSimReadout.cc:
+     - call SetReadyToSave for the run-header
+     - added some debug output in case of failure
+
+   * msimcamera/MSimSignalCam.[h,cc]:
+     - subtract the trigger position of any
+
+   * msimcamera/MSimTrigger.cc:
+     - some changes to the debug output
+
+
+
  2009/01/26 Thomas Bretz
 
@@ -25,4 +65,55 @@
        msimelectronics
      - added readcorsika
+
+   * mbase/Makefile, mbase/BaseLinkDef.h:
+     - added MParEnv
+
+   * mbase/MParEnv.[h,cc]:
+     - added
+
+   * mbase/MParameters.[h,cc]:
+     - added ReadEnv to all classes
+
+   * mbase/MParList.cc:
+     - return the container within MParEnv if MParEnv found 
+
+   * mbase/MParContainer.cc:
+     - fixed a typo
+
+   * manalysis/MEventRateCalc.cc:
+     - added a comment
+
+   * melectronics/MAnalogChannels.[h,cc]:
+     - made the GetNum* unsigned
+     - added new member functions to just initialize one size
+
+   * mraw/MRawRunHeader.[h,cc]:
+     - added a possibility to just init the number of pixels
+
+   * msimcamera/MSimReadout.[h,cc]:
+     - get the number of output channels from the camera geometry
+     - initialize the runheader in ReInit
+     - initialize the data arrays from the run-header in reinit
+
+   * mimage/MHHillas.cc:
+     - the default binning is not scales by 0.9 anymore
+
+   * mmc/MMcEvtBasic.h:
+     - added new type "artificial" 
+
+   * mraw/MRawRunHeader.cc:
+     - set header size in constructor to 0
+
+   * msimcamera/MSimCalibrationSignal.[h,cc]:
+     - we need fEvt and fTrigger always
+     - renamed ReInit to CallReInit
+     - set photon primary type
+     - set number of photons from 50 to 5
+
+   * msimcamera/MSimRandomPhotons.cc:
+     - explicitly set weight to 1
+
+   * mjobs/Makefile:
+     - added msim, msimreflector, msimcamera and mcorsika
 
 
Index: trunk/MagicSoft/Mars/NEWS
===================================================================
--- trunk/MagicSoft/Mars/NEWS	(revision 9273)
+++ trunk/MagicSoft/Mars/NEWS	(revision 9274)
@@ -43,4 +43,7 @@
      This is just a tiny change but it might change the appearance of
      the radial profiles a little bit.
+
+   * The default binning of the center-of-gravity plot has slightly been 
+     increased
 
  ;Database
Index: trunk/MagicSoft/Mars/macros/rootlogon.C
===================================================================
--- trunk/MagicSoft/Mars/macros/rootlogon.C	(revision 9273)
+++ trunk/MagicSoft/Mars/macros/rootlogon.C	(revision 9274)
@@ -163,4 +163,9 @@
     gInterpreter->AddIncludePath(dir+"mtools");
     gInterpreter->AddIncludePath(dir+"mtrigger");
+    gInterpreter->AddIncludePath(dir+"melectronics");
+    gInterpreter->AddIncludePath(dir+"msim");
+    gInterpreter->AddIncludePath(dir+"msimreflector");
+    gInterpreter->AddIncludePath(dir+"msimcamera");
+    gInterpreter->AddIncludePath(dir+"mcorsika");
 
     if (TString("linux")==gSystem->GetBuildArch())
Index: trunk/MagicSoft/Mars/melectronics/ElectronicsLinkDef.h
===================================================================
--- trunk/MagicSoft/Mars/melectronics/ElectronicsLinkDef.h	(revision 9273)
+++ trunk/MagicSoft/Mars/melectronics/ElectronicsLinkDef.h	(revision 9274)
@@ -6,4 +6,5 @@
 
 #pragma link C++ class APD+;
+#pragma link C++ class MPulseShape+;
 #pragma link C++ class MAnalogSignal+;
 #pragma link C++ class MDigitalSignal+;
Index: trunk/MagicSoft/Mars/melectronics/MAnalogSignal.cc
===================================================================
--- trunk/MagicSoft/Mars/melectronics/MAnalogSignal.cc	(revision 9273)
+++ trunk/MagicSoft/Mars/melectronics/MAnalogSignal.cc	(revision 9274)
@@ -33,4 +33,5 @@
 #include "MAnalogSignal.h"
 
+#include <TF1.h>
 #include <TRandom.h>
 #include <TObjArray.h>
@@ -69,5 +70,8 @@
 // and multiplied by f.
 //
-void MAnalogSignal::AddPulse(const MSpline3 &spline, Float_t t, Float_t f)
+// Return kTRUE if the full range of the spline could be added to the
+// analog signal, kFALSE otherwise.
+//
+Bool_t MAnalogSignal::AddPulse(const MSpline3 &spline, Float_t t, Float_t f)
 {
     // FIXME: This could be improved using a MExtralgoSpline with
@@ -79,22 +83,68 @@
     const Float_t end   = t+spline.GetXmax();
 
-    /*const*/ Int_t  first = TMath::CeilNint(start);
-    /*const*/ UInt_t last  = TMath::CeilNint(end); // Ceil:< Floor:<=
-
-    if (first<0 || last>GetSize())
-    {
-        gLog << err << "ERROR - AddPulse: Out of bounds, ";
-        gLog << "Win=[" << first << "," << last << "] N=" << GetSize() << " t=" << t;
-        gLog << " Spline=[" << spline.GetXmin() << "," << spline.GetXmax() << "]" << endl;
-
-        if (first<0)
-            first=0;
-        if (last>GetSize())
-            last=GetSize();
-    }
+    Int_t  first = TMath::CeilNint(start);
+    UInt_t last  = TMath::CeilNint(end); // Ceil:< Floor:<=
+
+    Bool_t rc = kTRUE;
+    if (first<0)
+    {
+        first=0;
+        rc = kFALSE;
+    }
+    if (last>GetSize())
+    {
+        last=GetSize();
+        rc = kFALSE;
+    }
+
+    // FIXME: As soon as we have access to TSpline3::fPoly we can
+    //        gain a lot in execution speed here.
 
     Float_t *arr = GetArray();
     for (UInt_t i=first; i<last; i++)
         arr[i] += spline.Eval(i-t)*f;
+
+    return rc;
+}
+
+// ------------------------------------------------------------------------
+//
+// Evaluate the spline an add the result between t+xmin and t+xmax
+// (xmin and xmax are the limits of the TF1) to the signal.
+// The spline is evaluated at the bin-center of the analog signal
+// and multiplied by f.
+//
+// Return kTRUE if the full range of the function could be added to the
+// analog signal, kFALSE otherwise.
+//
+Bool_t MAnalogSignal::AddPulse(const TF1 &func, Float_t t, Float_t f)
+{
+    // Both in units of the sampling frequency
+    const Float_t start = t+func.GetXmin();
+    const Float_t end   = t+func.GetXmax();
+
+    Int_t  first = TMath::CeilNint(start);
+    UInt_t last  = TMath::CeilNint(end); // Ceil:< Floor:<=
+
+    Bool_t rc = kTRUE;
+    if (first<0)
+    {
+        first=0;
+        rc = kFALSE;
+    }
+    if (last>GetSize())
+    {
+        last=GetSize();
+        rc = kFALSE;
+    }
+
+    // FIXME: As soon as we have access to TSpline3::fPoly we can
+    //        gain a lot in execution speed here.
+
+    Float_t *arr = GetArray();
+    for (UInt_t i=first; i<last; i++)
+        arr[i] += func.Eval(i-t)*f;
+
+    return rc;
 }
 
@@ -156,5 +206,5 @@
 // The user is responsible of deleting the TObjArray.
 //
-TObjArray *MAnalogSignal::Discriminate(Float_t threshold, Float_t len) const
+TObjArray *MAnalogSignal::Discriminate(Float_t threshold, Double_t start, Double_t end, Float_t len) const
 {
     TObjArray *ttl = new TObjArray;
@@ -175,36 +225,39 @@
 
     Double_t x1 = 0;
-    Double_t x2 = 0;
+    Double_t x2 = start;  // Start searching at x2
 
     while (1)
     {
-        x1 = sp.SearchYup(x2+deadtime, threshold);
-        if (x1<0)
-            break;
-
-        const Bool_t rising = sp.Deriv1(x1)>0;
-        if (!rising)
+        // Search for the next rising edge (starting at x2)
+        while (1)
         {
-            // The last value might just have been a local max/min
-            // or its period was short than 1e-4
+            x1 = sp.SearchYup(x2+deadtime, threshold);
+            if (x1<0 || x1>=end)
+                return ttl;
+
+            const Bool_t rising = sp.Deriv1(x1)>0;
+            if (rising)
+                break;
+
             x2 = x1;
-            //gLog << warn << "Rising edge expected at " << x1 << " (after " << x2 << ", N=" << fN << ")" << endl;
-            continue;
         }
 
-        x2 = sp.SearchYup(x1+deadtime, threshold);
-        if (x2<0)
-            break;
-
-        const Bool_t falling = sp.Deriv1(x2)<=0;
-        if (!falling)
+        // Search for the next falling edge (starting at x1)
+        while (1)
         {
-            // The last value might just have been a local max/min
-            // or its period was short than 1e-4
+            x2 = sp.SearchYup(x1+deadtime, threshold);
+            if (x2<0)
+                x2 = end;
+            if (x2>=end)
+                break;
+
+            const Bool_t falling = sp.Deriv1(x2)<0;
+            if (falling)
+                break;
+
             x1 = x2;
-            //gLog << warn << "Falling edge expected at " << x2 << " (after " << x1 << " N=" << fN << ")" << endl;
-            continue;
         }
 
+        // We found a rising and a falling edge
         MDigitalSignal *sig = new MDigitalSignal(x1, len>0?len:x2-x1);
 
@@ -228,7 +281,4 @@
     }
 
-    if (x1>=0)
-        gLog << warn << "Falling edge expected before end at N=" << fN << "!" << endl;
-
     return ttl;
 }
Index: trunk/MagicSoft/Mars/melectronics/MAnalogSignal.h
===================================================================
--- trunk/MagicSoft/Mars/melectronics/MAnalogSignal.h	(revision 9273)
+++ trunk/MagicSoft/Mars/melectronics/MAnalogSignal.h	(revision 9274)
@@ -6,4 +6,5 @@
 #endif
 
+class TF1;
 class MSpline3;
 
@@ -17,7 +18,8 @@
     MAnalogSignal(UInt_t n) { Set(n); }
 
-    void Set(UInt_t n);
-    void AddPulse(const MSpline3 &spline, Float_t t, Float_t f=1);
-    void AddSignal(const MAnalogSignal &s);
+    void   Set(UInt_t n);
+    Bool_t AddPulse(const MSpline3 &spline, Float_t t, Float_t f=1);
+    Bool_t AddPulse(const TF1 &f1, Float_t t, Float_t f=1);
+    void   AddSignal(const MAnalogSignal &s);
 
     // Deprecated. Use MSimRandomPhotons instead
@@ -26,5 +28,6 @@
     void AddGaussianNoise(Float_t amplitude=1, Float_t offset=0);
 
-    TObjArray *Discriminate(Float_t threshold, Float_t len=-1) const;
+    TObjArray *Discriminate(Float_t threshold, Double_t start, Double_t end, Float_t len=-1) const;
+    TObjArray *Discriminate(Float_t threshold, Float_t len=-1) const { return Discriminate(threshold, 0, fN-1, len); }
 
     ClassDef(MAnalogSignal, 1) // Storage class for an analog signal
Index: trunk/MagicSoft/Mars/melectronics/MPulseShape.cc
===================================================================
--- trunk/MagicSoft/Mars/melectronics/MPulseShape.cc	(revision 9274)
+++ trunk/MagicSoft/Mars/melectronics/MPulseShape.cc	(revision 9274)
@@ -0,0 +1,195 @@
+/* ======================================================================== *\
+!
+! *
+! * This file is part of CheObs, the Modular 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 appears 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,  1/2009 <mailto:tbretz@astro.uni-wuerzburg.de>
+!
+!   Copyright: CheObs Software Development, 2000-2009
+!
+!
+\* ======================================================================== */
+
+//////////////////////////////////////////////////////////////////////////////
+//
+//  MPulseShape
+//
+// This container describes the pulse shape of a pulse.
+//
+//////////////////////////////////////////////////////////////////////////////
+#include "MPulseShape.h"
+
+#include <TF1.h>
+
+#include "MLog.h"
+#include "MLogManip.h"
+
+#include "MSpline3.h"
+
+ClassImp(MPulseShape);
+
+using namespace std;
+
+// --------------------------------------------------------------------------
+//
+//  Default Constructor.
+//
+MPulseShape::MPulseShape(const char* name, const char *title)
+: fSpline(0), fFunction("exp(-(x/2)^2/2)"), fNpx(25), fXmin(-25), fXmax(25)
+{
+    fName  = name  ? name  : "MPulseShape";
+    fTitle = title ? title : "";
+
+    SetFunction(fFunction, fNpx, fXmin, fXmax);
+}
+
+// --------------------------------------------------------------------------
+//
+//  Call Clear()
+//
+MPulseShape::~MPulseShape()
+{
+    Clear();
+}
+
+// --------------------------------------------------------------------------
+//
+//  Delete fSpline if set and set it to 0
+//
+void MPulseShape::Clear(Option_t *)
+{
+    if (fSpline)
+        delete fSpline;
+    fSpline=0;
+}
+
+// --------------------------------------------------------------------------
+//
+//  Return the width of the range in which the spline is defined.
+//
+Float_t MPulseShape::GetPulseWidth() const
+{
+    return fSpline ? fSpline->GetXmax()-fSpline->GetXmin() : 0;
+}
+
+// --------------------------------------------------------------------------
+//
+//  Return the lower edge of the range in which the spline is defined.
+//
+Float_t MPulseShape::GetXmin() const
+{
+    return fSpline ? fSpline->GetXmin() : 0;
+}
+
+// --------------------------------------------------------------------------
+//
+//  Return the upper edge of the range in which the spline is defined.
+//
+Float_t MPulseShape::GetXmax() const
+{
+    return fSpline ? fSpline->GetXmax() : 0;
+}
+
+// --------------------------------------------------------------------------
+//
+//  Read the intended pulse shape from a file and initialize the spline
+// accordingly
+//
+Bool_t MPulseShape::ReadFile(const char *fname)
+{
+    if (fname)
+        fFileName = fname;
+
+    *fLog << inf << "Reading pulse shape from " << fFileName << endl;
+
+    const TGraph g(fFileName);
+    if (g.GetN()==0)
+    {
+        *fLog << err << "ERROR - No data points from " << fFileName << "." << endl;
+        return kFALSE;
+    }
+
+    // option: b1/e1 b2/e2   (first second derivative?)
+    // option: valbeg/valend (first second derivative?)
+
+    Clear();
+    fSpline = new MSpline3(g);//, fRunHeader->GetFreqSampling()/1000.);
+
+    return kTRUE;
+}
+
+void MPulseShape::SetFunction(const TF1 &f)
+{
+    // FIXME: Use TF1 directly? (In most cases this seems to be slower)
+
+    // option: b1/e1 b2/e2   (first second derivative?)
+    // option: valbeg/valend (first second derivative?)
+
+    // if (f.GetNpar()==0)
+    // No SUPPORT
+
+    Clear();
+    fSpline = new MSpline3(f);//, fRunHeader->GetFreqSampling()/1000.);
+
+    fFunction = f.GetTitle();
+}
+
+Bool_t MPulseShape::SetFunction(const char *func, Int_t n, Double_t xmin, Double_t xmax)
+{
+    // FIXME: Use TF1 directly? (In most cases this seems to be slower)
+    TF1 f("f", func, xmin, xmax);
+    f.SetNpx(n);
+
+    SetFunction(f);
+
+    return kTRUE;
+}
+
+// --------------------------------------------------------------------------
+//
+// FileName: pulse-shape.txt
+// Function.Name: gaus
+// Function.Npx:    50
+// Function.Xmin:  -5
+// Function.Xmax:   5
+//
+Int_t MPulseShape::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
+{
+    Bool_t rc = kFALSE;
+    if (IsEnvDefined(env, prefix, "FileName", print))
+    {
+        rc = kTRUE;
+        SetFileName(GetEnvValue(env, prefix, "FileName", fFileName));
+        if (!fFileName.IsNull())
+            if (ReadFile(fFileName))
+                return kERROR;
+    }
+
+    if (IsEnvDefined(env, prefix, "Function.Name", print))
+    {
+        rc = kTRUE;
+
+        if (IsEnvDefined(env, prefix, "Function.Npx", print))
+            fNpx = GetEnvValue(env, prefix, "Function.Npx", fNpx);
+        if (IsEnvDefined(env, prefix, "Function.Xmin", print))
+            fXmin = GetEnvValue(env, prefix, "Function.Xmin", fXmin);
+        if (IsEnvDefined(env, prefix, "Function.Xmax", print))
+            fXmax = GetEnvValue(env, prefix, "Function.Xmax", fXmax);
+
+        SetFunction(GetEnvValue(env, prefix, "Function.Name", fFunction), fNpx, fXmin, fXmax);
+    }
+
+    return rc;
+}
Index: trunk/MagicSoft/Mars/melectronics/MPulseShape.h
===================================================================
--- trunk/MagicSoft/Mars/melectronics/MPulseShape.h	(revision 9274)
+++ trunk/MagicSoft/Mars/melectronics/MPulseShape.h	(revision 9274)
@@ -0,0 +1,48 @@
+#ifndef MARS_MPulseShape
+#define MARS_MPulseShape
+
+#ifndef MARS_MParContainer
+#include "MParContainer.h"
+#endif
+
+class TF1;
+class MSpline3;
+
+class MPulseShape : public MParContainer
+{
+private:
+    MSpline3          *fSpline;     //! Spline to describe the pulse shape
+
+    TString fFileName;              // File name of a file describing the pulse shape
+
+    TString fFunction;              // Function describing the pulse shape
+    Int_t   fNpx;                   // Number of sampling points
+    Float_t fXmin;                  // lower edge of sampling
+    Float_t fXmax;                  // upper edge of sampling
+
+    Bool_t ReadFile(const char *fname);
+    Bool_t SetFunction(const char *func, Int_t n, Double_t xmin, Double_t xmax);
+
+    // MParContainer
+    Int_t ReadEnv(const TEnv &env, TString prefix, Bool_t print=kFALSE);
+
+public:
+    MPulseShape(const char *name=0, const char *title=0);
+    ~MPulseShape();
+
+    void SetFunction(const TF1 &f);
+    void SetFunction(const char *f)  { fFunction=f; fFileName=""; }
+    void SetFileName(const char *n)  { fFileName=n; fFunction=""; }
+
+    void Clear(Option_t *o="");
+
+    MSpline3 *GetSpline() const { return fSpline; }
+
+    Float_t GetPulseWidth() const;
+    Float_t GetXmin() const;
+    Float_t GetXmax() const;
+
+    ClassDef(MPulseShape, 0) // Parameter container to hold the setup for a pulse shape
+};
+
+#endif
Index: trunk/MagicSoft/Mars/melectronics/Makefile
===================================================================
--- trunk/MagicSoft/Mars/melectronics/Makefile	(revision 9273)
+++ trunk/MagicSoft/Mars/melectronics/Makefile	(revision 9274)
@@ -24,5 +24,6 @@
            MAnalogSignal.cc \
            MAnalogChannels.cc \
-	   MDigitalSignal.cc 
+	   MDigitalSignal.cc  \
+	   MPulseShape.cc
 
 ############################################################
Index: trunk/MagicSoft/Mars/msimcamera/MSimCamera.cc
===================================================================
--- trunk/MagicSoft/Mars/msimcamera/MSimCamera.cc	(revision 9273)
+++ trunk/MagicSoft/Mars/msimcamera/MSimCamera.cc	(revision 9274)
@@ -47,6 +47,8 @@
 
 #include "MSpline3.h"
+#include "MPulseShape.h"
 
 #include "MParList.h"
+//#include "MParameters.h"
 
 #include "MPhotonEvent.h"
@@ -67,6 +69,5 @@
 //
 MSimCamera::MSimCamera(const char* name, const char *title)
-: fEvt(0), fStat(0), fRunHeader(0), fCamera(0), fSpline(0),
-  fFunction("exp(-(x/2)^2/2)"), fNpx(25), fXmin(-25), fXmax(25)
+: fEvt(0), fStat(0), fRunHeader(0), fCamera(0), fSpline(0)//, fPulsePos(0)
 {
     fName  = name  ? name  : "MSimCamera";
@@ -76,96 +77,4 @@
 // --------------------------------------------------------------------------
 //
-//  Call Clear()
-//
-MSimCamera::~MSimCamera()
-{
-    Clear();
-}
-
-// --------------------------------------------------------------------------
-//
-//  Delete fSpline if set and set it to 0
-//
-void MSimCamera::Clear(Option_t *)
-{
-    if (fSpline)
-        delete fSpline;
-    fSpline=0;
-}
-
-// --------------------------------------------------------------------------
-//
-//  Read the intended pulse shape from a file and initialize the spline
-// accordingly
-//
-Bool_t MSimCamera::ReadFile(const char *fname)
-{
-    if (!fRunHeader)
-        return kFALSE;
-
-    if (fname)
-        fFileName = fname;
-
-    *fLog << inf << "Reading pulse shape from " << fFileName << endl;
-
-    const TGraph g(fFileName);
-    if (g.GetN()==0)
-    {
-        *fLog << err << "ERROR - No data points from " << fFileName << "." << endl;
-        return kFALSE;
-    }
-
-    // option: b1/e1 b2/e2   (first second derivative?)
-    // option: valbeg/valend (first second derivative?)
-
-    Clear();
-    fSpline = new MSpline3(g, fRunHeader->GetFreqSampling()/1000.);
-
-    return kTRUE;
-}
-
-void MSimCamera::SetFunction(const TF1 &f)
-{
-    // FIXME: Use TF1 directly? (In most cases this seems to be slower)
-    if (!fRunHeader)
-        return;// kFALSE;
-
-    // option: b1/e1 b2/e2   (first second derivative?)
-    // option: valbeg/valend (first second derivative?)
-
-    // if (f.GetNpar()==0)
-    // No SUPPORT
-
-    Clear();
-    fSpline = new MSpline3(f, fRunHeader->GetFreqSampling()/1000.);
-
-    fFunction = f.GetTitle();
-}
-
-Bool_t MSimCamera::SetFunction(const char *func, Int_t n, Double_t xmin, Double_t xmax)
-{
-    // FIXME: Use TF1 directly? (In most cases this seems to be slower)
-    TF1 f("f", func, xmin, xmax);
-    f.SetNpx(n);
-
-    SetFunction(f);
-
-    return kTRUE;
-}
-
-// --------------------------------------------------------------------------
-//
-// SetReadyToSave for fRunHeader
-//
-Bool_t MSimCamera::ReInit(MParList *pList)
-{
-    // make that the run-header gets written to the file
-    fRunHeader->SetReadyToSave();
-
-    return kTRUE;
-}
-
-// --------------------------------------------------------------------------
-//
 // Search for the necessayr parameter containers.
 // Setup spline for pulse shape.
@@ -197,13 +106,22 @@
         return kFALSE;
     }
-
-
-    // FIMXE: Move to ReInit in case fRunHeader is read form file?
-    if (!fFileName.IsNull())
-        return ReadFile(fFileName);
-
-    if (!fFunction.IsNull())
-        return SetFunction(fFunction, fNpx, fXmin, fXmax);
-
+/*
+    fPulsePos = (MParameterD*)pList->FindObject("IntendedPulsePos", "MParameterD");
+    if (!fPulsePos)
+    {
+        *fLog << err << "IntendedPulsePos [MParameterD] not found... aborting." << endl;
+        return kFALSE;
+    }
+ */
+    MPulseShape *pulse = (MPulseShape*)pList->FindObject("MPulseShape");
+    if (!pulse)
+    {
+        *fLog << err << "MPulseShape not found... aborting." << endl;
+        return kFALSE;
+    }
+
+    *fLog << warn << "FIXME - SCALE WITH THE SAMPLING FREQUENCY." << endl;
+
+    fSpline = pulse->GetSpline();
     if (!fSpline)
     {
@@ -225,6 +143,6 @@
     const Double_t freq = fRunHeader->GetFreqSampling()/1000.;
 
-    const Double_t start = fStat->GetTimeFirst()*freq + fSpline->GetXmin();
-    const Double_t end   = fStat->GetTimeLast() *freq + fSpline->GetXmax();
+    const Double_t start = fStat->GetTimeFirst()*freq;
+    const Double_t end   = fStat->GetTimeLast() *freq;
 
     const UInt_t   nlen  = TMath::CeilNint(end-start);
@@ -236,8 +154,10 @@
     const UInt_t npix = fStat->GetMaxIndex()+1;
 
+    const Double_t pl = fSpline->GetXmin()*freq;
+    const Double_t pr = fSpline->GetXmax()*freq;
+
     // Init the arrays and set the range which will contain valid data
     fCamera->Init(npix, nlen);
-    fCamera->SetValidRange(0   -TMath::FloorNint(fSpline->GetXmin()),
-                           nlen-TMath::CeilNint( fSpline->GetXmax()));
+    fCamera->SetValidRange(TMath::FloorNint(pr), TMath::CeilNint(nlen+pl));
 
     // Add electronic noise to empty channels
@@ -258,46 +178,14 @@
 
         const UInt_t   idx = ph.GetTag();
-        const Double_t t   = (ph.GetTime()-fStat->GetTimeFirst())*freq - fSpline->GetXmin();
+        const Double_t t   = (ph.GetTime()-fStat->GetTimeFirst())*freq;// - fSpline->GetXmin();
 
         // FIXME: Time jitter?
         // FIXME: Add additional routing here?
 
+        // === FIXME === FIXME === FIXME === Frequency!!!!
         (*fCamera)[idx].AddPulse(*fSpline, t, ph.GetWeight());
     }
 
     return kTRUE;
-}
-
-// --------------------------------------------------------------------------
-//
-// FileName: pulse-shape.txt
-// Function.Name: gaus
-// Function.Npx:    50
-// Function.Xmin:  -5
-// Function.Xmax:   5
-//
-Int_t MSimCamera::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
-{
-    Bool_t rc = kFALSE;
-    if (IsEnvDefined(env, prefix, "FileName", print))
-    {
-        rc = kTRUE;
-        SetFileName(GetEnvValue(env, prefix, "FileName", fFileName));
-    }
-
-    if (IsEnvDefined(env, prefix, "Function.Name", print))
-    {
-        rc = kTRUE;
-        SetFunction(GetEnvValue(env, prefix, "Function.Name", fFunction));
-
-        if (IsEnvDefined(env, prefix, "Function.Npx", print))
-            fNpx = GetEnvValue(env, prefix, "Function.Npx", fNpx);
-        if (IsEnvDefined(env, prefix, "Function.Xmin", print))
-            fXmin = GetEnvValue(env, prefix, "Function.Xmin", fXmin);
-        if (IsEnvDefined(env, prefix, "Function.Xmax", print))
-            fXmax = GetEnvValue(env, prefix, "Function.Xmax", fXmax);
-    }
-
-    return rc;
 }
 
Index: trunk/MagicSoft/Mars/msimcamera/MSimCamera.h
===================================================================
--- trunk/MagicSoft/Mars/msimcamera/MSimCamera.h	(revision 9273)
+++ trunk/MagicSoft/Mars/msimcamera/MSimCamera.h	(revision 9274)
@@ -12,5 +12,4 @@
 class MAnalogChannels;
 
-class TF1;
 class MSpline3;
 
@@ -24,33 +23,12 @@
     MAnalogChannels   *fCamera;     //! Output of the analog signals
 
-    MSpline3          *fSpline;     //! Spline to describe the pulse shape
-
-    TString fFileName;              // File name of a file describing the pulse shape
-
-    TString fFunction;              // Function describing the pulse shape
-    Int_t   fNpx;                   // Number of
-    Float_t fXmin;
-    Float_t fXmax;
-
-    // MParContainer
-    Int_t ReadEnv(const TEnv &env, TString prefix, Bool_t print=kFALSE);
+    MSpline3 *fSpline;
 
     // MTask
     Int_t PreProcess(MParList *pList);
     Int_t Process();
-    Bool_t ReInit(MParList *pList);
-
-    Bool_t ReadFile(const char *fname);
-    Bool_t SetFunction(const char *func, Int_t n, Double_t xmin, Double_t xmax);
 
 public:
     MSimCamera(const char *name=NULL, const char *title=NULL);
-    ~MSimCamera();
-
-    void SetFunction(const TF1 &f);
-    void SetFunction(const char *f)  { fFunction=f; fFileName=""; }
-    void SetFileName(const char *n)  { fFileName=n; fFunction=""; }
-
-    void Clear(Option_t *o="");
 
     ClassDef(MSimCamera, 0) // Task to simulate the electronic noise and to convert photons into pulses
Index: trunk/MagicSoft/Mars/msimcamera/MSimGeomCam.cc
===================================================================
--- trunk/MagicSoft/Mars/msimcamera/MSimGeomCam.cc	(revision 9273)
+++ trunk/MagicSoft/Mars/msimcamera/MSimGeomCam.cc	(revision 9274)
@@ -61,4 +61,6 @@
 #include "MRawRunHeader.h"
 
+#include "MPulseShape.h"
+
 ClassImp(MSimGeomCam);
 
@@ -70,5 +72,6 @@
 //
 MSimGeomCam::MSimGeomCam(const char* name, const char *title)
-    : fGeom(0), fEvt(0), fStat(0), fNameGeomCam("MGeomCam")
+    : fGeom(0), fEvt(0), fStat(0), fPulsePos(0), fHeader(0), fPulse(0),
+    fNameGeomCam("MGeomCam")
 {
     fName  = name  ? name  : "MSimGeomCam";
@@ -96,4 +99,11 @@
     {
         *fLog << err << "MPhotonEvent not found... aborting." << endl;
+        return kFALSE;
+    }
+
+    fPulse = (MPulseShape*)pList->FindObject("MPulseShape");
+    if (!fPulse)
+    {
+        *fLog << err << "MPulsShape not found... aborting." << endl;
         return kFALSE;
     }
@@ -166,8 +176,7 @@
     const Float_t ns = fHeader->GetFreqSampling()/1000.*fHeader->GetNumSamplesHiGain();
     const Float_t pp = fPulsePos->GetVal();
-
-    // FIXME FIXME FIXME --- Make it depending on the trigger position?
-
-    fStat->SetTime(first-pp, last+(ns-pp));
+    const Float_t pw = fPulse->GetPulseWidth();
+
+    fStat->SetTime(first-pp-pw, last+(ns-pp)+pw);
     fStat->SetMaxIndex(fGeom->GetNumPixels()-1);
     fStat->SetReadyToSave();
Index: trunk/MagicSoft/Mars/msimcamera/MSimGeomCam.h
===================================================================
--- trunk/MagicSoft/Mars/msimcamera/MSimGeomCam.h	(revision 9273)
+++ trunk/MagicSoft/Mars/msimcamera/MSimGeomCam.h	(revision 9274)
@@ -13,4 +13,5 @@
 class MParameterD;
 class MRawRunHeader;
+class MPulseShape;
 
 class MSimGeomCam : public MTask
@@ -22,5 +23,5 @@
     MParameterD       *fPulsePos; //! Intended pulse position in digitization window [ns]
     MRawRunHeader     *fHeader;   //! Length of digitization window
-
+    MPulseShape       *fPulse;    //! 
 
     TString fNameGeomCam;
Index: trunk/MagicSoft/Mars/msimcamera/MSimReadout.cc
===================================================================
--- trunk/MagicSoft/Mars/msimcamera/MSimReadout.cc	(revision 9273)
+++ trunk/MagicSoft/Mars/msimcamera/MSimReadout.cc	(revision 9274)
@@ -133,4 +133,5 @@
     fRunHeader->SetValidMagicNumber();
     fRunHeader->SetSourceInfo("MonteCarlo");  // "Muon" from first event??
+    fRunHeader->SetReadyToSave();
 
     fData->InitRead(fRunHeader);
@@ -197,4 +198,8 @@
     {
         *fLog << err << "ERROR - Trigger position beyond valid analog signal range." << endl;
+        *fLog << "        Trigger:    " << fTrigger->GetVal() << endl;
+        *fLog << "        PulsePos:   " << pulpos << endl;
+        *fLog << "        SamplesIn:  " << nsamp << endl;
+        *fLog << "        SamplesOut: " << nslices << endl;
         return kERROR;
     }
@@ -239,4 +244,5 @@
     fData->SetReadyToSave();
 
+    // FIMXE: This will never be stored correctly :(
     fRunHeader->SetNumEvents(fRunHeader->GetNumEvents()+1);
 
Index: trunk/MagicSoft/Mars/msimcamera/MSimSignalCam.cc
===================================================================
--- trunk/MagicSoft/Mars/msimcamera/MSimSignalCam.cc	(revision 9273)
+++ trunk/MagicSoft/Mars/msimcamera/MSimSignalCam.cc	(revision 9274)
@@ -46,4 +46,5 @@
 
 #include "MParList.h"
+#include "MParameters.h"
 
 #include "MSignalCam.h"
@@ -62,5 +63,5 @@
 //
 MSimSignalCam::MSimSignalCam(const char* name, const char *title)
-    : fEvt(0), fStat(0), fSignal(0)
+    : fEvt(0), fStat(0), fSignal(0), fTrigger(0)
 {
     fName  = name  ? name  : "MSimSignalCam";
@@ -85,4 +86,11 @@
     {
         *fLog << err << "MPhotonStatistics not found... aborting." << endl;
+        return kFALSE;
+    }
+
+    fTrigger = (MParameterD*)pList->FindObject("TriggerPos", "MParameterD");
+    if (!fTrigger)
+    {
+        *fLog << err << "TriggerPos [MParameterD] not found... aborting." << endl;
         return kFALSE;
     }
@@ -123,5 +131,4 @@
 {
     // FIXME: Check the maximum index in GetTag from the statistics container
-
     fCont.Reset();
     fTime.Reset();
@@ -152,5 +159,9 @@
     // Get time of start point from the statistics container
     //   FIXME: Should be the real time of the first photon
-    const Float_t first = fStat->GetTimeFirst();
+
+    // ====> Distance to trigger position! (if TrigPos found!)
+    // What about events with trigger<0?
+    const Float_t trig  = fTrigger && fTrigger->GetVal()>=0 ? fTrigger->GetVal()  : 0;
+    const Float_t first = fStat->GetTimeFirst()+trig /*+50+40*/;
 
     // Loop over all pixels and set signal and arrival time.
@@ -161,5 +172,5 @@
         MSignalPix &pix = (*fSignal)[idx];
 
-        pix.SetNumPhotons(fCont[idx]); // gRandom->Uniform(last-first)
+        pix.SetNumPhotons(fCont[idx]); 
         pix.SetArrivalTime(fCont[idx]<=0 ? -1 : fTime[idx]/fCont[idx]-first);
         pix.SetRing(fCont[idx]>0); // Used==1, Unused==0
Index: trunk/MagicSoft/Mars/msimcamera/MSimSignalCam.h
===================================================================
--- trunk/MagicSoft/Mars/msimcamera/MSimSignalCam.h	(revision 9273)
+++ trunk/MagicSoft/Mars/msimcamera/MSimSignalCam.h	(revision 9274)
@@ -18,11 +18,13 @@
 class MPhotonStatistics;
 class MSignalCam;
+class MParameterD;
 
 class MSimSignalCam : public MTask
 {
 private:
-    MPhotonEvent      *fEvt;     //! Event containing the photons
-    MPhotonStatistics *fStat;    //! Statistics about the event
-    MSignalCam        *fSignal;  //! Output container
+    MPhotonEvent      *fEvt;      //! Event containing the photons
+    MPhotonStatistics *fStat;     //! Statistics about the event
+    MSignalCam        *fSignal;   //! Output container
+    MParameterD       *fTrigger;  //! Trigger position w.r.t. analog channels
 
     MArrayD fCont;   //! Local buffer for contents
Index: trunk/MagicSoft/Mars/msimcamera/MSimTrigger.cc
===================================================================
--- trunk/MagicSoft/Mars/msimcamera/MSimTrigger.cc	(revision 9273)
+++ trunk/MagicSoft/Mars/msimcamera/MSimTrigger.cc	(revision 9274)
@@ -231,6 +231,7 @@
     const Float_t  pulspos = fPulsePos->GetVal()/freq;
 
+    // Valid range in units of bins
     const Float_t min = fCamera->GetValidRangeMin()+pulspos;
-    const Float_t max = fCamera->GetValidRangeMax()+pulspos-nsamp;
+    const Float_t max = fCamera->GetValidRangeMax()-(nsamp-pulspos);
 
     // Define gate time (minimum coincidence time)
@@ -240,4 +241,8 @@
     TObjArray triggers;
     triggers.SetOwner();
+
+    Int_t cnt  = 0;
+    Int_t rmlo = 0;
+    Int_t rmhi = 0;
 
     for (int j=0; j<fCoincidenceMap.GetEntries(); j++)
@@ -266,9 +271,21 @@
         MDigitalSignal *ttl = 0;
         while ((ttl=static_cast<MDigitalSignal*>(Next())))
-            if (ttl->GetStart()<min || ttl->GetStart()>max)
+        {
+            if (ttl->GetStart()<min)
+            {
                 delete arr->Remove(ttl);
+                rmlo++;
+            }
+            if (ttl->GetStart()>max)
+            {
+                delete arr->Remove(ttl);
+                rmhi++;
+            }
+        }
 
         // Remove the empty slots
         arr->Compress();
+
+        cnt += arr->GetEntriesFast();
 
         // If we have at least one trigger keep the earliest one.
@@ -284,5 +301,8 @@
     // No trigger issued. Go on.
     if (triggers.GetEntriesFast()==0)
+    {
+        *fLog << all << rmlo << "/" << rmhi << " trigger out of valid range. No trigger raised." << endl;
         return kTRUE;
+    }
 
     // There are usually not enough entries that it is worth to search
@@ -292,12 +312,11 @@
     triggers.Sort();
 
+    // FIXME: Jitter! (Own class?)
+    fTrigger->SetVal(static_cast<MDigitalSignal*>(triggers[0])->GetStart());
+
     // inf2?
     *fLog << all;
-    *fLog << triggers.GetEntriesFast() << " triggers in " << triggers.GetEntriesFast() << " patches.";
+    *fLog << cnt << " triggers left in " << triggers.GetEntriesFast() << " patches (" << rmlo << "/" << rmhi << " trigger out of valid range), T=" << fTrigger->GetVal();
     *fLog << endl;
-
-    // FIXME: Jitter! (Own class?)
-    fTrigger->SetVal(static_cast<MDigitalSignal*>(triggers[0])->GetStart());
-
 
     return kTRUE;
