Index: trunk/MagicSoft/Mars/msimcamera/MSimCamera.cc
===================================================================
--- trunk/MagicSoft/Mars/msimcamera/MSimCamera.cc	(revision 9272)
+++ 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 9272)
+++ 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 9272)
+++ 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 9272)
+++ 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 9272)
+++ 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 9272)
+++ 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 9272)
+++ 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 9272)
+++ 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;
