Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 9318)
+++ trunk/MagicSoft/Mars/Changelog	(revision 9319)
@@ -154,4 +154,19 @@
      - added debug output
 
+   * msimcamera/MSimCamera.[h,cc]:
+     - outsourced the pedestal and gain numbers into ReInit
+     - use an MPedestalCam for Electronic Noise and Gain
+     - allow to switch ob whether the gain is applied to the 
+       electronic noise or not
+     - SetPhotElfromShower
+
+   * msim/Makefile. msim/SimLinkDef.h:
+     - added MSimMMCS
+
+   * msim/MSimMMCS.[h,cc]:
+     - added
+
+   * msimcamera/Makefile:
+     - added -I../mpedestal
 
 
Index: trunk/MagicSoft/Mars/mjobs/MJSimulation.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJSimulation.cc	(revision 9318)
+++ trunk/MagicSoft/Mars/mjobs/MJSimulation.cc	(revision 9319)
@@ -78,4 +78,5 @@
 #include "MWriteRootFile.h"
 
+#include "MSimMMCS.h"
 #include "MSimAbsorption.h"
 #include "MSimReflector.h"
Index: trunk/MagicSoft/Mars/msim/MSimMMCS.cc
===================================================================
--- trunk/MagicSoft/Mars/msim/MSimMMCS.cc	(revision 9319)
+++ trunk/MagicSoft/Mars/msim/MSimMMCS.cc	(revision 9319)
@@ -0,0 +1,182 @@
+/* ======================================================================== *\
+!
+! *
+! * 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,  2/2009 <mailto:tbretz@astro.uni-wuerzburg.de>
+!
+!   Copyright: CheObs Software Development, 2000-2009
+!
+!
+\* ======================================================================== */
+
+//////////////////////////////////////////////////////////////////////////////
+//
+//  MSimMMCS
+//
+// Task to copy the Mars CheObs MC headers to the old Mars style
+//
+//////////////////////////////////////////////////////////////////////////////
+#include "MSimMMCS.h"
+
+#include "MLog.h"
+#include "MLogManip.h"
+
+#include "MParList.h"
+
+#include "MMcEvt.hxx"
+
+#include "MRawRunHeader.h"
+
+#include "MCorsikaRunHeader.h"
+#include "MCorsikaEvtHeader.h"
+
+#include "MMcRunHeader.hxx"
+#include "MMcCorsikaRunHeader.h"
+
+#include "MPointingPos.h"
+
+ClassImp(MSimMMCS);
+
+using namespace std;
+
+// --------------------------------------------------------------------------
+//
+//  Default Constructor.
+//
+MSimMMCS::MSimMMCS(const char* name, const char *title)
+{
+    fName  = name  ? name  : "MSimMMCS";
+    fTitle = title ? title : "Task to copy the Mars CheObs MC headers to the old Mars style";
+}
+
+// --------------------------------------------------------------------------
+//
+Int_t MSimMMCS::PreProcess(MParList *plist)
+{
+    if (!plist->FindCreateObj("MMcRunHeader"))
+        return kFALSE;
+
+    if (!plist->FindCreateObj("MMcCorsikaRunHeader"))
+        return kFALSE;
+
+    if (!plist->FindCreateObj("MRawRunHeader"))
+        return kFALSE;
+
+    fMcEvtBasic = (MMcEvtBasic*)plist->FindCreateObj("MMcEvtBasic");
+    if (!fMcEvtBasic)
+        return kFALSE;
+
+    fMcEvt = (MMcEvt*)plist->FindCreateObj("MMcEvt");
+    if (!fMcEvt)
+        return kFALSE;
+
+    fPointingPos = (MPointingPos*)plist->FindObject("MPointingPos");
+    if (!fPointingPos)
+    {
+        *fLog << err << "MPointingPos not found... aborting." << endl;
+        return kFALSE;
+    }
+
+    fEvtHeader = (MCorsikaEvtHeader*)plist->FindObject("MCorsikaEvtHeader");
+    if (!fEvtHeader)
+    {
+        *fLog << err << "MCorsikaEvtHeader not found... aborting." << endl;
+        return kFALSE;
+    }
+
+    fRunHeader = (MCorsikaRunHeader*)plist->FindObject("MCorsikaRunHeader");
+    if (!fRunHeader)
+    {
+        *fLog << err << "MCorsikaRunHeader not found... aborting." << endl;
+        return kFALSE;
+    }
+
+
+    return kTRUE;
+}
+
+// --------------------------------------------------------------------------
+//
+Bool_t MSimMMCS::ReInit(MParList *plist)
+{
+    MMcCorsikaRunHeader *mch = (MMcCorsikaRunHeader*)plist->FindObject("MMcCorsikaRunHeader");
+    if (!mch)
+    {
+        *fLog << err << "MMcCorsikaRunHeader not found... aborting." << endl;
+        return kFALSE;
+    }
+
+    mch->SetSpectrum(fRunHeader->GetSlopeSpectrum(),
+                     fRunHeader->GetEnergyMin(), fRunHeader->GetEnergyMax());
+    mch->SetReadyToSave();
+
+    // ----------------------------------------------------
+
+    MMcRunHeader *mrh = (MMcRunHeader*)plist->FindObject("MMcRunHeader");
+    if (!mrh)
+    {
+        *fLog << err << "MMcRunHeader not found... aborting." << endl;
+        return kFALSE;
+    }
+
+    //    fNumPheFromDNSB        MMcPedestalNSBAdd   // Number of phe/ns from diffuse NSB
+    mrh->SetNumSimulatedShowers(fRunHeader->GetNumEvents());
+    mrh->SetImpactMax(fRunHeader->GetImpactMax());
+
+    // ----------------------------------------------------
+
+    MRawRunHeader *rh = (MRawRunHeader*)plist->FindObject("MRawRunHeader");
+    if (!rh)
+    {
+        *fLog << err << "MRawRunHeader not found... aborting." << endl;
+        return kFALSE;
+    }
+
+    const UInt_t id = fRunHeader->GetParticleID();
+
+    rh->SetRunInfo(1, fRunHeader->GetRunNumber(), 0);
+    rh->SetSourceInfo(MMcEvtBasic::GetParticleName(id));
+    rh->SetReadyToSave();
+
+    // ----------------------------------------------------
+
+    fMcEvtBasic->SetPartId(MMcEvtBasic::ParticleId_t(id));
+
+    return kTRUE;
+}
+
+// --------------------------------------------------------------------------
+//
+Int_t MSimMMCS::Process()
+{
+    fMcEvtBasic->SetEnergy(fEvtHeader->GetTotalEnergy());
+    fMcEvtBasic->SetImpact(fEvtHeader->GetImpact());
+
+    fMcEvtBasic->SetTelescopeTheta(fPointingPos->GetZdRad());
+    fMcEvtBasic->SetTelescopePhi(fPointingPos->GetAzRad());
+
+    fMcEvtBasic->SetReadyToSave();
+
+    static_cast<MMcEvtBasic&>(*fMcEvt) = *fMcEvtBasic;
+
+    fMcEvt->SetTheta(fEvtHeader->GetZd());
+    fMcEvt->SetPhi(fEvtHeader->GetAz());
+
+    fMcEvt->SetEvtNumber(fEvtHeader->GetEvtNumber());
+    fMcEvt->SetPhotElfromShower(0);
+
+    return kTRUE;
+}
Index: trunk/MagicSoft/Mars/msim/MSimMMCS.h
===================================================================
--- trunk/MagicSoft/Mars/msim/MSimMMCS.h	(revision 9319)
+++ trunk/MagicSoft/Mars/msim/MSimMMCS.h	(revision 9319)
@@ -0,0 +1,35 @@
+#ifndef MARS_MSimMMCS
+#define MARS_MSimMMCS
+
+#ifndef MARS_MTask
+#include "MTask.h"
+#endif
+
+class MParList;
+class MMcEvt;
+class MMcEvtBasic;
+class MPointingPos;
+class MCorsikaEvtHeader;
+class MCorsikaRunHeader;
+
+class MSimMMCS : public MTask
+{
+private:
+    MMcEvtBasic       *fMcEvtBasic;
+    MMcEvt            *fMcEvt;
+    MPointingPos      *fPointingPos;
+    MCorsikaEvtHeader *fEvtHeader;
+    MCorsikaRunHeader *fRunHeader;
+
+    // MTask
+    Int_t  PreProcess(MParList *pList);
+    Bool_t ReInit(MParList *pList);
+    Int_t  Process();
+
+public:
+    MSimMMCS(const char* name=0, const char *title=0);
+
+    ClassDef(MSimMMCS, 0) // Task to copy the Mars CheObs MC headers to the old Mars style
+};
+
+#endif
Index: trunk/MagicSoft/Mars/msim/Makefile
===================================================================
--- trunk/MagicSoft/Mars/msim/Makefile	(revision 9318)
+++ trunk/MagicSoft/Mars/msim/Makefile	(revision 9319)
@@ -20,9 +20,10 @@
 #
 INCLUDES = -I. -I../mbase -I../mmc -I../mgeom -I../mgui -I../mcorsika \
-           -I../mpointing -I../msimreflector -I../mhbase 
+           -I../mpointing -I../msimreflector -I../mhbase -I../mraw
 
 SRCFILES = MPhotonData.cc \
 	   MPhotonEvent.cc \
 	   MHPhotonEvent.cc \
+	   MSimMMCS.cc \
 	   MSimAbsorption.cc \
 	   MSimPointingPos.cc
Index: trunk/MagicSoft/Mars/msim/SimLinkDef.h
===================================================================
--- trunk/MagicSoft/Mars/msim/SimLinkDef.h	(revision 9318)
+++ trunk/MagicSoft/Mars/msim/SimLinkDef.h	(revision 9319)
@@ -15,3 +15,5 @@
 #pragma link C++ class MSimAbsorption+;
 
+#pragma link C++ class MSimMMCS+;
+
 #endif
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 \
