Index: trunk/MagicSoft/Mars/msimcamera/MSimCamera.cc
===================================================================
--- trunk/MagicSoft/Mars/msimcamera/MSimCamera.cc	(revision 9318)
+++ trunk/MagicSoft/Mars/msimcamera/MSimCamera.cc	(revision 9319)
@@ -56,7 +56,11 @@
 #include "MPhotonData.h"
 
+#include "MPedestalCam.h"
+#include "MPedestalPix.h"
+
 #include "MAnalogSignal.h"
 #include "MAnalogChannels.h"
 
+#include "MMcEvt.hxx"            // To be replaced by a CheObs class
 #include "MRawRunHeader.h"
 
@@ -70,5 +74,6 @@
 //
 MSimCamera::MSimCamera(const char* name, const char *title)
-: fEvt(0), fStat(0), fRunHeader(0), fCamera(0), fSpline(0)//, fPulsePos(0)
+    : fEvt(0), fStat(0), fRunHeader(0), fElectronicNoise(0), fGain(0),
+    fCamera(0), fMcEvt(0), fSpline(0), fBaselineGain(kFALSE)
 {
     fName  = name  ? name  : "MSimCamera";
@@ -83,4 +88,8 @@
 Int_t MSimCamera::PreProcess(MParList *pList)
 {
+    fMcEvt = (MMcEvt*)pList->FindCreateObj("MMcEvt");
+    if (!fMcEvt)
+        return kFALSE;
+
     fCamera = (MAnalogChannels*)pList->FindCreateObj("MAnalogChannels");
     if (!fCamera)
@@ -115,4 +124,14 @@
     }
  */
+
+    // Create it here to make sure that MGeomApply will set the correct size
+    fElectronicNoise = (MPedestalCam*)pList->FindCreateObj("MPedestalCam", "ElectronicNoise");
+    if (!fElectronicNoise)
+        return kFALSE;
+
+    fGain = (MPedestalCam*)pList->FindCreateObj("MPedestalCam", "Gain");
+    if (!fGain)
+        return kFALSE;
+
     MPulseShape *pulse = (MPulseShape*)pList->FindObject("MPulseShape");
     if (!pulse)
@@ -122,5 +141,10 @@
     }
 
-    *fLog << warn << "FIXME - SCALE WITH THE SAMPLING FREQUENCY." << endl;
+    if (fRunHeader->GetFreqSampling()!=1000)
+    {
+        *fLog << err  << "ERROR - Sampling frequencies others than 1GHz are not yet supported." << endl;
+        *fLog << warn << "FIXME - SCALE MPulsShape WITH THE SAMPLING FREQUENCY." << endl;
+        return kFALSE;
+    }
 
     fSpline = pulse->GetSpline();
@@ -131,4 +155,35 @@
     }
 
+    // ---------------- Information output ----------------------
+
+    if (fBaselineGain)
+        *fLog << inf << "Gain is also applied to the electronic noise." << endl;
+
+    return kTRUE;
+}
+
+// --------------------------------------------------------------------------
+//
+// FIXME: For now this is a workaround to set a baseline and the
+// electronic (guassian noise)
+//
+Bool_t MSimCamera::ReInit(MParList *plist)
+{
+    for (int i=0; i<fElectronicNoise->GetSize(); i++)
+    {
+        MPedestalPix &ped = (*fElectronicNoise)[i];
+        ped.SetPedestal(15*256); // Baseline at 15 like in MAGIC
+        ped.SetPedestalRms(1.5*256);//2.0); // 1.5 bit noise for a gain of 64
+        ped.SetPedestalABoffset(0);
+        ped.SetNumEvents(0);
+
+        // 256 scale from 8bit to 16bit
+        // 8 signal height of one phe
+        MPedestalPix &gain = (*fGain)[i];
+        gain.SetPedestal(8*256);
+        gain.SetPedestalRms(0); 
+        gain.SetPedestalABoffset(0);
+        gain.SetNumEvents(0);
+    }
     return kTRUE;
 }
@@ -144,11 +199,10 @@
     const Double_t freq = fRunHeader->GetFreqSampling()/1000.;
 
+    // FIXME: Should we use a higher sampling here?
+
     const Double_t start = fStat->GetTimeFirst()*freq;
     const Double_t end   = fStat->GetTimeLast() *freq;
 
     const UInt_t   nlen  = TMath::CeilNint(end-start);
-
-    // FIXME: Jitter the start point of digitization by one sample [0;1]
-    // FIXME: TAKE PULSE WIDTH out of the calculation and start at TimeFirst
 
     // Get number of pixels/channels
@@ -165,6 +219,22 @@
     for (UInt_t i=0; i<npix; i++)
     {
+        const MPedestalPix &pix = (*fElectronicNoise)[i];
+
+        const Double_t val = pix.GetPedestal();
+        const Double_t rms = pix.GetPedestalRms();
+
+        if (!fBaselineGain)
+        {
+            (*fCamera)[i].AddGaussianNoise(rms, val);
+            continue;
+        }
+        // Sorry, the name "pedestal" is misleading here
+        // FIXME: Simulate gain fluctuations
+        const Double_t gain = (*fGain)[i].GetPedestal();
+
         // FIXME: We might add the base line here already.
-        (*fCamera)[i].AddGaussianNoise(22.5/64);
+        // FIXME: How stable is the offset?
+        // FIXME: Should we write a container AppliedGain for MSImTrigger?
+        (*fCamera)[i].AddGaussianNoise(rms*gain, val*gain);
     }
 
@@ -181,4 +251,6 @@
     //        Or maybe per channel and run?
 
+    Double_t tot = 0;
+
     // Simulate pulses
     for (Int_t i=0; i<num; i++)
@@ -191,32 +263,19 @@
         // FIXME: Time jitter?
         // FIXME: Add additional routing here?
+        // FIMXE: How stable is the gain?
+
+        if (ph.GetPrimary()!=MMcEvt::kNightSky)
+            tot += ph.GetWeight();
+
+        // Sorry, the name "pedestal" is misleading here
+        // FIXME: Simulate gain fluctuations
+        const Double_t gain = (*fGain)[idx].GetPedestal();
 
         // === FIXME === FIXME === FIXME === Frequency!!!!
-        (*fCamera)[idx].AddPulse(*fSpline, t, ph.GetWeight());
-    }
+        (*fCamera)[idx].AddPulse(*fSpline, t, ph.GetWeight()*gain);
+    }
+
+    fMcEvt->SetPhotElfromShower(TMath::Nint(tot));
 
     return kTRUE;
 }
-
-/*
-MSimCameraFiles::Process()
-{
-    // fCorsikaHeader->GetEvtNumber()   -->   fMcEvt->SetEvtNumber()
-    // fCorsikaHeader->GetTotalEnergy() -->   fMcEvt->SetEnergy()
-    // fCorsikaHeader->GetParticleID()  -->   fMcEvt->SetParticleID()
-    // fCorsikaHeader->GetImpact()      -->   fMcEvt->SetImpact()
-    // fCorsikaHeader->GetTheta()       -->   fMcEvt->SetTheta()
-    // fCorsikaHeader->GetPhi()         -->   fMcEvt->SetPhi()
-    // fPointingPos->GetTheta()         -->   fMcEvt->SetTelescopeTheta()
-    // fPointingPos->GetPhi()           -->   fMcEvt->SetTelescopePhi()
-    // fStats->GetTimeFirst()           -->   fMcEvt->SetTimeFirst()
-    // fStats->GetTimeLast()            -->   fMcEvt->SetTimeLast()
-    //                                        fMcEvt->SetReuse()
-    // MMcCorsikaRunHeader: fSlopeSpec, fELowLim, fEUppLim;
-
-    fMcEvt->Fill(*fCorsikaHeader, *fPointingPos, *fStats);
-
-    return kTRUE;
-}
-*/
-
Index: trunk/MagicSoft/Mars/msimcamera/MSimCamera.h
===================================================================
--- trunk/MagicSoft/Mars/msimcamera/MSimCamera.h	(revision 9318)
+++ trunk/MagicSoft/Mars/msimcamera/MSimCamera.h	(revision 9319)
@@ -6,4 +6,5 @@
 #endif
 
+class MMcEvt;
 class MParList;
 class MPhotonEvent;
@@ -11,4 +12,5 @@
 class MRawRunHeader;
 class MAnalogChannels;
+class MPedestalCam;
 
 class MSpline3;
@@ -17,15 +19,21 @@
 {
 private:
-    MPhotonEvent      *fEvt;        //! Event stroing the photons
-    MPhotonStatistics *fStat;       //! Valid time range of the phootn event
-    MRawRunHeader     *fRunHeader;  //! Sampling frequency
+    MPhotonEvent      *fEvt;             //! Event stroing the photons
+    MPhotonStatistics *fStat;            //! Valid time range of the phootn event
+    MRawRunHeader     *fRunHeader;       //! Sampling frequency
+    MPedestalCam      *fElectronicNoise; //! Electronic noise (baseline and rms)
+    MPedestalCam      *fGain;            //! Electronic noise (baseline and rms)
 
-    MAnalogChannels   *fCamera;     //! Output of the analog signals
+    MAnalogChannels   *fCamera;          //! Output of the analog signals
+    MMcEvt            *fMcEvt;           //! For information stored in MMcEvt
 
-    MSpline3 *fSpline;
+    MSpline3 *fSpline;     // Pusle Shape
+
+    Bool_t fBaselineGain;  // Should the gain be applied to baseline and electronic noise?
 
     // MTask
-    Int_t PreProcess(MParList *pList);
-    Int_t Process();
+    Int_t  PreProcess(MParList *pList);
+    Bool_t ReInit(MParList *pList);
+    Int_t  Process();
 
 public:
Index: trunk/MagicSoft/Mars/msimcamera/Makefile
===================================================================
--- trunk/MagicSoft/Mars/msimcamera/Makefile	(revision 9318)
+++ trunk/MagicSoft/Mars/msimcamera/Makefile	(revision 9319)
@@ -21,5 +21,5 @@
 INCLUDES = -I. -I../mbase -I../mgeom -I../msim -I../msignal -I../mcorsika \
 	   -I../mmc -I../mgui -I../mcalib -I../mraw -I../mfileio -I../melectronics \
-	   -I../mtrigger
+	   -I../mtrigger -I../mpedestal
 
 SRCFILES = MSimPSF.cc \
