Index: trunk/Mars/mjobs/MJSimulation.cc
===================================================================
--- trunk/Mars/mjobs/MJSimulation.cc	(revision 18449)
+++ trunk/Mars/mjobs/MJSimulation.cc	(revision 18450)
@@ -56,4 +56,5 @@
 #include <TEnv.h>
 #include <TCanvas.h>
+#include <iostream>
 
 // Core
@@ -144,10 +145,4 @@
     return kTRUE;
 }
-/*
-TString MJSimulation::GetOutFile(const MSequence &seq) const
-{
-    return seq.IsValid() ? Form("ceres%08d.root", seq.GetSequence()) : "ceres.root";
-}
-*/
 
 Bool_t MJSimulation::WriteResult(const MParList &plist, const MSequence &seq, Int_t num)
@@ -337,6 +332,75 @@
 }
 
+// Setup the header accordingly to the used operation mode
+void MJSimulation::SetupHeaderOperationMode(MRawRunHeader &header) const
+{
+    switch (fOperationMode)
+    {
+    case kModeData:
+        header.SetRunType(MRawRunHeader::kRTMonteCarlo|MRawRunHeader::kRTData);
+        header.SetRunInfo(0, fRunNumber<0 ? 3 : fRunNumber);
+        break;
+
+    case kModePed:
+        header.SetRunType(MRawRunHeader::kRTMonteCarlo|MRawRunHeader::kRTPedestal);
+        header.SetSourceInfo("Pedestal");
+        header.SetRunInfo(0, fRunNumber<0 ? 1 : fRunNumber);
+        break;
+
+    case kModeCal:
+        header.SetRunType(MRawRunHeader::kRTMonteCarlo|MRawRunHeader::kRTCalibration);
+        header.SetSourceInfo("Calibration");
+        header.SetRunInfo(0, fRunNumber<0 ? 2 : fRunNumber);
+        break;
+
+    case kModePointRun:
+        header.SetRunType(MRawRunHeader::kRTMonteCarlo|MRawRunHeader::kRTPointRun);
+        header.SetRunInfo(0, fRunNumber<0 ? 0 : fRunNumber);
+        break;
+    }
+}
+
+void MJSimulation::CreateBinningObjects(MParList &plist) const
+{
+    MBinning *binse = (MBinning*) plist.FindCreateObj("MBinning","BinningEnergy");
+    binse->SetEdges(120,      1,  1000000,"log");
+    MBinning *binsth = (MBinning*) plist.FindCreateObj("MBinning","BinningThreshold");
+    binsth->SetEdges( 60,   0.9,   900000, "log");
+    MBinning *binsee = (MBinning*) plist.FindCreateObj("MBinning","BinningEnergyEst");
+    binsee->SetEdges( 36,   0.9,   900000, "log");
+    MBinning *binss = (MBinning*) plist.FindCreateObj("MBinning","BinningSize");
+    binss->SetEdges( 100,     1, 10000000,      "log");
+    MBinning *binsi = (MBinning*) plist.FindCreateObj("MBinning","BinningImpact");
+    binsi->SetEdges(  32,     0,      800);
+    MBinning *binsh = (MBinning*) plist.FindCreateObj("MBinning","BinningHeight");
+    binsh->SetEdges( 150,     0,       50);
+    MBinning *binsaz = (MBinning*) plist.FindCreateObj("MBinning","BinningAz");
+    binsaz->SetEdges(720,  -360,      360);
+    MBinning *binszd = (MBinning*) plist.FindCreateObj("MBinning","BinningZd");
+    binszd->SetEdges( 70,     0,       70);
+    MBinning *binsvc = (MBinning*) plist.FindCreateObj("MBinning","BinningViewCone");
+    binsvc->SetEdges( 35,     0,        7);
+    MBinning *binsel = (MBinning*) plist.FindCreateObj("MBinning","BinningTotLength");
+    binsel->SetEdges(150,     0,       50);
+    MBinning *binsew = (MBinning*) plist.FindCreateObj("MBinning","BinningMedLength");
+    binsew->SetEdges(150,     0,       15);
+    plist.FindCreateObj("MBinning","BinningDistC");
+    plist.FindCreateObj("MBinning","BinningDist");
+    plist.FindCreateObj("MBinning","BinningTrigPos");
+}
+
 Bool_t MJSimulation::Process(const MArgs &args, const MSequence &seq)
 {
+    // --------------------------------------------------------------------------------
+    // --------------------------------------------------------------------------------
+    // Initialization
+    // --------------------------------------------------------------------------------
+    // --------------------------------------------------------------------------------
+
+    // --------------------------------------------------------------------------------
+    // Logging output:
+    // --------------------------------------------------------------------------------
+    // - description of the job itself
+    // - list of the to processed sequence
     *fLog << inf;
     fLog->Separator(GetDescriptor());
@@ -357,7 +421,7 @@
         return kFALSE;
     }
-
-    // --------------------------------------------------------------------------------
-    // Setup Parlist
+    // --------------------------------------------------------------------------------
+    // Setup MParList and MTasklist
+    // --------------------------------------------------------------------------------
     MParList plist;
     plist.AddToList(this); // take care of fDisplay!
@@ -365,6 +429,14 @@
     MTaskList tasks;
     plist.AddToList(&tasks);
-    // --------------------------------------------------------------------------------
-
+
+    // --------------------------------------------------------------------------------
+    // --------------------------------------------------------------------------------
+    // Parameter Container Setup
+    // --------------------------------------------------------------------------------
+    // --------------------------------------------------------------------------------
+
+    // --------------------------------------------------------------------------------
+    // Setup container for the reflector, the cones and the camera geometry
+    // --------------------------------------------------------------------------------
     // FIXME: Allow empty GeomCones!
     MParEnv env0("Reflector");
@@ -377,64 +449,15 @@
     plist.AddToList(&env1);
     plist.AddToList(&env2);
-
+    // --------------------------------------------------------------------------------
+    // Setup container for the "camera array" of the extracted number of photons
+    // --------------------------------------------------------------------------------
+    // from ExtractorRndm
     plist.FindCreateObj("MPedPhotCam", "MPedPhotFromExtractorRndm");
-
+    // --------------------------------------------------------------------------------
+    // Setup spline containers for:
+    // --------------------------------------------------------------------------------
+    // - the pulse shape
+    // - the different photon acceptance splines
     MParSpline shape("PulseShape");
-    plist.AddToList(&shape);
-
-    // *** FIXME *** FIXME *** FIXME ***
-    plist.FindCreateObj("MMcRunHeader");
-
-    MRawRunHeader header;
-    header.SetValidMagicNumber();
-    //header.InitFadcType(3);
-
-    switch (fOperationMode)
-    {
-    case kModeData:
-        header.SetRunType(MRawRunHeader::kRTMonteCarlo|MRawRunHeader::kRTData);
-        header.SetRunInfo(0, fRunNumber<0 ? 3 : fRunNumber);
-        break;
-
-    case kModePed:
-        header.SetRunType(MRawRunHeader::kRTMonteCarlo|MRawRunHeader::kRTPedestal);
-        header.SetSourceInfo("Pedestal");
-        header.SetRunInfo(0, fRunNumber<0 ? 1 : fRunNumber);
-        break;
-
-    case kModeCal:
-        header.SetRunType(MRawRunHeader::kRTMonteCarlo|MRawRunHeader::kRTCalibration);
-        header.SetSourceInfo("Calibration");
-        header.SetRunInfo(0, fRunNumber<0 ? 2 : fRunNumber);
-        break;
-
-    case kModePointRun:
-        header.SetRunType(MRawRunHeader::kRTMonteCarlo|MRawRunHeader::kRTPointRun);
-        header.SetRunInfo(0, fRunNumber<0 ? 0 : fRunNumber);
-        break;
-    }
-
-    // FIXME: Move to MSimPointingPos, MSimCalibrationSignal
-    //        Can we use this as input for MSimPointingPos?
-    header.SetObservation("On", "MonteCarlo");
-    plist.AddToList(&header);
-
-    MCorsikaRead read;
-    read.SetForceMode(fForceMode);
-
-    if (!seq.IsValid())
-    {
-        for (int i=0; i<args.GetNumArguments(); i++)
-            read.AddFile(args.GetArgumentStr(i));
-    }
-    else
-        read.AddFiles(iter);
-
-    MContinue precut("", "PreCut");
-    precut.IsInverted();
-    precut.SetAllowEmpty();
-
-    MSimMMCS simmmcs;
-
     MParSpline splinepde("PhotonDetectionEfficiency");
     MParSpline splinemirror("MirrorReflectivity");
@@ -442,4 +465,5 @@
     MParSpline splinecones2("ConesTransmission");
     MParSpline splineAdditionalPhotonAcceptance("AdditionalPhotonAcceptance");
+    plist.AddToList(&shape);
     plist.AddToList(&splinepde);
     plist.AddToList(&splinemirror);
@@ -448,4 +472,84 @@
     plist.AddToList(&splineAdditionalPhotonAcceptance);
 
+    // --------------------------------------------------------------------------------
+    // Setup container for the MC run header and the Raw run header
+    // --------------------------------------------------------------------------------
+    plist.FindCreateObj("MMcRunHeader");
+
+    MRawRunHeader header;
+    header.SetValidMagicNumber();
+    SetupHeaderOperationMode(header);
+    // FIXME: Move to MSimPointingPos, MSimCalibrationSignal
+    //        Can we use this as input for MSimPointingPos?
+    header.SetObservation("On", "MonteCarlo");
+    plist.AddToList(&header);
+
+    // --------------------------------------------------------------------------------
+    // Setup container for the intended pulse position and for the trigger position
+    // --------------------------------------------------------------------------------
+    MParameterD pulpos("IntendedPulsePos");
+    // FIXME: Set a default which could be 1/3 of the digitization window
+    //    pulpos.SetVal(40);  // [ns]
+    plist.AddToList(&pulpos);
+
+    MParameterD trigger("TriggerPos");
+    trigger.SetVal(0);
+    plist.AddToList(&trigger);
+
+
+    // --------------------------------------------------------------------------------
+    // Setup container for the fix time offset, for residual time spread and for gapd time jitter
+    // --------------------------------------------------------------------------------
+    // Dominik and Sebastian on: fix time offsets
+    MMatrix fix_time_offsets_between_pixels_in_ns(
+        "MFixTimeOffset","titel"
+    );
+    plist.AddToList(&fix_time_offsets_between_pixels_in_ns);
+
+    // Jens Buss on: residual time spread
+    MParameterD resTimeSpread("ResidualTimeSpread");
+    resTimeSpread.SetVal(0.0);
+    plist.AddToList(&resTimeSpread);
+    // Jens Buss on: residual time spread
+    MParameterD gapdTimeJitter("GapdTimeJitter");
+    gapdTimeJitter.SetVal(0.0);
+    plist.AddToList(&gapdTimeJitter);
+
+    // --------------------------------------------------------------------------------
+    // Creation of binning objects and apply default settings
+    // --------------------------------------------------------------------------------
+    CreateBinningObjects(plist);
+
+    // --------------------------------------------------------------------------------
+    // --------------------------------------------------------------------------------
+    // Tasks Setup (Reading, Absorption, Reflector)
+    // --------------------------------------------------------------------------------
+    // --------------------------------------------------------------------------------
+
+    // --------------------------------------------------------------------------------
+    // Corsika read setup
+    // --------------------------------------------------------------------------------
+    MCorsikaRead read;
+    read.SetForceMode(fForceMode);
+
+    if (!seq.IsValid())
+    {
+        for (int i=0; i<args.GetNumArguments(); i++)
+            read.AddFile(args.GetArgumentStr(i));
+    }
+    else
+        read.AddFiles(iter);
+
+    // --------------------------------------------------------------------------------
+    // Precut
+    // --------------------------------------------------------------------------------
+    MContinue precut("", "PreCut");
+    precut.IsInverted();
+    precut.SetAllowEmpty();
+
+    // --------------------------------------------------------------------------------
+    // MSimMMCS (don't know what this is doing) and calculation of the incident angle
+    // --------------------------------------------------------------------------------
+    MSimMMCS simmmcs;
     const TString sin2 = "sin(MCorsikaEvtHeader.fZd)*sin(MCorsikaRunHeader.fZdMin*TMath::DegToRad())";
     const TString cos2 = "cos(MCorsikaEvtHeader.fZd)*cos(MCorsikaRunHeader.fZdMin*TMath::DegToRad())";
@@ -457,4 +561,13 @@
     calcangle.SetNameParameter("IncidentAngle");
 
+    // --------------------------------------------------------------------------------
+    // Absorption tasks
+    // --------------------------------------------------------------------------------
+    // - Atmosphere (simatm)
+    // - photon detection efficiency (absapd)
+    // - mirror reflectivity (absmir)
+    // - angular acceptance of winston cones (cones)
+    // - transmission of winston cones (cones2)
+    // - additional photon acceptance (only motivated, not measured) (additionalPhotonAcceptance)
     MSimAtmosphere simatm;
     MSimAbsorption absapd("SimPhotonDetectionEfficiency");
@@ -470,4 +583,7 @@
     additionalPhotonAcceptance.SetParName("AdditionalPhotonAcceptance");
  
+    // --------------------------------------------------------------------------------
+    // Simulating pointing position and simulating reflector
+    // --------------------------------------------------------------------------------
     MSimPointingPos pointing;
 
@@ -477,4 +593,8 @@
 //  MSimStarField stars;
 
+    // --------------------------------------------------------------------------------
+    // Continue tasks
+    // --------------------------------------------------------------------------------
+    // - Processing of the current events stops, if there aren't at least two photons (see cont3)
     MContinue cont1("MPhotonEvent.GetNumPhotons<1", "ContEmpty1");
     MContinue cont2("MPhotonEvent.GetNumPhotons<1", "ContEmpty2");
@@ -484,37 +604,106 @@
     cont3.SetAllowEmpty();
 
-    // -------------------------------------------------------------------
-
-    MBinning binse( 120,     1,  1000000, "BinningEnergy",    "log");
-    MBinning binsth( 60,   0.9,   900000, "BinningThreshold", "log");
-    MBinning binsee( 36,   0.9,   900000, "BinningEnergyEst", "log");
-    MBinning binss( 100,     1, 10000000, "BinningSize",      "log");
-//    MBinning binsi( 100,  -500,      500, "BinningImpact");
-    MBinning binsi(  32,     0,      800, "BinningImpact");
-    MBinning binsh( 150,     0,       50, "BinningHeight");
-    MBinning binsaz(720,  -360,      360, "BinningAz");
-    MBinning binszd( 70,     0,       70, "BinningZd");
-    MBinning binsvc( 35,     0,        7, "BinningViewCone");
-    MBinning binsel(150,     0,       50, "BinningTotLength");
-    MBinning binsew(150,     0,       15, "BinningMedLength");
-    MBinning binsd("BinningDistC");
-    MBinning binsd0("BinningDist");
-    MBinning binstr("BinningTrigPos");
-
-    plist.AddToList(&binsee);
-    plist.AddToList(&binse);
-    plist.AddToList(&binsth);
-    plist.AddToList(&binss);
-    plist.AddToList(&binsi);
-    plist.AddToList(&binsh);
-    plist.AddToList(&binszd);
-    plist.AddToList(&binsaz);
-    plist.AddToList(&binsvc);
-    plist.AddToList(&binsel);
-    plist.AddToList(&binsew);
-    plist.AddToList(&binstr);
-    plist.AddToList(&binsd);
-    plist.AddToList(&binsd0);
-
+    // --------------------------------------------------------------------------------
+    // --------------------------------------------------------------------------------
+    // Tasks Setup (Cones, SiPM-Simulation, RandomPhotons, Camera, Trigger, DAQ)
+    // --------------------------------------------------------------------------------
+    // --------------------------------------------------------------------------------
+
+    // --------------------------------------------------------------------------------
+    // GeomApply, SimPSF, SimGeomCam, SimCalibrationSignal
+    // --------------------------------------------------------------------------------
+    // - MGeomApply only resizes all MGeomCam parameter container to the actual
+    //   number of pixels
+    // - MSimPSF applies an additional smearing of the photons on the camera window
+    //   on default it is switched of (set to 0), but can be applied by setting:
+    //   MSimPSF.fSigma: <value>
+    //   in the config file.
+    //   Be aware, that there is also a smearing of the photons implemented in
+    //   MSimReflector by setting:
+    //   MReflector.SetSigmaPSF: <value>
+    //   in the config file. This is at the moment done in the default config file.
+    // - MSimGeomCam identifies which pixel was hit by which photon
+    // - MSimCalibrationSignal is only used for simulated calibration runs
+    MGeomApply apply;
+
+    MSimPSF simpsf;
+
+    MSimGeomCam simgeom;
+    simgeom.SetNameGeomCam("GeomCones");
+
+    MSimCalibrationSignal simcal;
+    simcal.SetNameGeomCam("GeomCones");
+
+    // --------------------------------------------------------------------------------
+    // Simulation of random photons
+    // --------------------------------------------------------------------------------
+    // - be aware that here default values for the nsb rate and for the dark
+    //   count rate are set. They will be overwritten if the values are defined
+    //   in the config file
+    // - if you want to simulate a specific nsb rate you have to set the filename
+    //   in the config file to an empty string and then you can specify the NSB rate:
+    //   MSimRandomPhotons.FileNameNSB:
+    //   MSimRandomPhotons.FrequencyNSB: 0.0
+
+    // Sky Quality Meter: 21.82M = 2.02e-4cd/m^2
+    // 1cd = 12.566 lm / 4pi sr
+    // FIXME: Simulate photons before cones and QE!
+    MSimRandomPhotons  simnsb;
+    simnsb.SetFreq(5.8, 0.004);  // ph/m^2/nm/sr/ns NSB, 4MHz dark count rate
+    simnsb.SetNameGeomCam("GeomCones");
+    // FIXME: How do we handle star-light? We need the rate but we also
+    //        need to process it through the mirror!
+
+    // --------------------------------------------------------------------------------
+    // Simulation from the SiPM to the DAQ
+    // --------------------------------------------------------------------------------
+    // - MSimAPD simulates the whole behaviour of the SiPMs:
+    //   (dead time of cells, crosstalk, afterpulses)
+    // - MSimExcessNoise adds a spread on the weight of each signal in the SiPMs
+    // - MSimBundlePhotons restructured the photon list of MPhotonEvent via a look
+    //   up table. At the moment the tasks is skipped, due to the fact that there
+    //   is no file name specified in the config file
+    // - MSimSignalCam only fills a SignalCam container for the cherenkov photons
+    // - MSimCamera simulates the behaviour of the camera:
+    //   (electronic noise, accoupling, time smearing and offset for the photons,
+    //   pulses injected by all photons)
+    // - MSimTrigger simulates the behaviour of the Trigger:
+    //   (Adding patch voltages, applying clipping, applying discriminator, applying
+    //   time over threshold and a possible trigger logic)
+    // - MContinue conttrig stops the processing of the current event if the trigger
+    //   position is negativ (and therefore not valid)
+    // - MSimReadout simulates the behaviour of the readout:
+    //   (Digitization and saturation of the ADC)
+    MSimAPD simapd;
+    simapd.SetNameGeomCam("GeomCones");
+
+    MSimExcessNoise   simexcnoise;
+    MSimBundlePhotons simsum;
+    MSimSignalCam     simsignal;
+    MSimCamera        simcam;
+    MSimTrigger       simtrig;
+    MContinue conttrig("TriggerPos.fVal<0", "ContTrigger");
+    MSimReadout       simdaq;
+
+    // --------------------------------------------------------------------------------
+    // Standard analysis with hillas parameters for the true cherenkov photons
+    // --------------------------------------------------------------------------------
+    // Remove isolated pixels
+    MImgCleanStd clean(0, 0);
+    //clean.SetCleanLvl0(0); // The level above which isolated pixels are kept
+    clean.SetCleanRings(0);
+    clean.SetMethod(MImgCleanStd::kAbsolute);
+
+    //clean.SetNamePedPhotCam("MPedPhotFromExtractorRndm");
+
+    MHillasCalc hcalc;
+    hcalc.Disable(MHillasCalc::kCalcConc);
+
+
+    // --------------------------------------------------------------------------------
+    // Setup of histogram containers and the corresponding fill tasks
+    // --------------------------------------------------------------------------------
+    // Here is no functionality for the simulation, it is only visualization via
+    // showplot
     MHn mhn1, mhn2, mhn3, mhn4;
     SetupHist(mhn1);
@@ -565,5 +754,5 @@
     plist.AddToList(&plane4);
     plist.AddToList(&planeF1);
-    plist.AddToList(&planeF2);;
+    plist.AddToList(&planeF2);
 
     //MHPSF psf;
@@ -589,6 +778,28 @@
     //fillP.SetNameTab("PSF",        "Photon distribution after cones for all mirrors");
 
-    // -------------------------------------------------------------------
-
+
+    MHCamEvent evt0a(/*10*/3, "Signal",    "Average signal (absolute);;S [ph]");
+    MHCamEvent evt0c(/*10*/3, "MaxSignal", "Maximum signal (absolute);;S [ph]");
+    MHCamEvent evt0d(/*11*/8, "ArrTm",     "Time after first photon;;T [ns]");
+    evt0a.SetErrorSpread(kFALSE);
+    evt0c.SetCollectMax();
+
+    MContinue cut("", "Cut");
+
+    MFillH fillx0a(&evt0a,             "MSignalCam",      "FillAvgSignal");
+    MFillH fillx0c(&evt0c,             "MSignalCam",      "FillMaxSignal");
+    MFillH fillx0d(&evt0d,             "MSignalCam",      "FillArrTm");
+    MFillH fillx1("MHHillas",          "MHillas",         "FillHillas");
+    MFillH fillx3("MHHillasSrc",       "MHillasSrc",      "FillHillasSrc");
+    MFillH fillx4("MHNewImagePar",     "MNewImagePar",    "FillNewImagePar");
+    MFillH fillth("MHThreshold",       "",                "FillThreshold");
+    MFillH fillca("MHCollectionArea",  "",                "FillTrigArea");
+
+    fillth.SetNameTab("Threshold");
+    fillca.SetNameTab("TrigArea");
+
+    // --------------------------------------------------------------------------------
+    // Formating of the outputfilepath
+    // --------------------------------------------------------------------------------
     if (!fFileOut.IsNull())
     {
@@ -598,7 +809,4 @@
             fFileOut = fFileOut.Remove(dot);
     }
-
-    // -------------------------------------------------------------------
-
     const char *fmt = fFileOut.IsNull() ?
         Form("s/cer([0-9]+)([0-9][0-9][0-9])/%s\\/%08d.$2%%s_MonteCarlo$1.root/", Esc(fPathOut).Data(), header.GetRunNumber()) :
@@ -610,4 +818,7 @@
     TString rule3(Form(fmt, Form("_%c", header.GetRunTypeChar())));
 
+    // --------------------------------------------------------------------------------
+    // Setup of the outputfile tasks
+    // --------------------------------------------------------------------------------
     MWriteRootFile write4a( 2, rule4, fOverwrite?"RECREATE":"NEW", "Star file");
     MWriteRootFile write4b( 2, rule4, fOverwrite?"RECREATE":"NEW", "Star file");
@@ -682,112 +893,18 @@
     write4b.AddContainer("MMcEvtBasic", "OriginalMC");
 
-    // -------------------------------------------------------------------
-
-    MGeomApply apply;
-
-    MSimPSF simpsf;
-
-    MSimGeomCam simgeom;
-    simgeom.SetNameGeomCam("GeomCones");
-
-    MSimCalibrationSignal simcal;
-    simcal.SetNameGeomCam("GeomCones");
-
-    // Sky Quality Meter: 21.82M = 2.02e-4cd/m^2
-    // 1cd = 12.566 lm / 4pi sr
-
-    // FIXME: Simulate photons before cones and QE!
-
-    MSimRandomPhotons  simnsb;
-    simnsb.SetFreq(5.8, 0.004);  // ph/m^2/nm/sr/ns NSB, 4MHz dark count rate
-    simnsb.SetNameGeomCam("GeomCones");
-
-    // FIXME: How do we handle star-light? We need the rate but we also
-    //        need to process it through the mirror!
-
-    MSimAPD simapd;
-    simapd.SetNameGeomCam("GeomCones");
-
-    MSimExcessNoise   simexcnoise;
-    MSimBundlePhotons simsum;
-    MSimSignalCam     simsignal;
-    MSimCamera        simcam;
-    MSimTrigger       simtrig;
-    MSimReadout       simdaq;
-
-    MContinue conttrig("TriggerPos.fVal<0", "ContTrigger");
-
-    MParameterD pulpos("IntendedPulsePos");
-    // FIXME: Set a default which could be 1/3 of the digitization window
-    //    pulpos.SetVal(40);  // [ns]
-    plist.AddToList(&pulpos);
-
-    MParameterD trigger("TriggerPos");
-    trigger.SetVal(0);
-    plist.AddToList(&trigger);
-    
-    // -------------------------------------------------------------------
-    // Dominik and Sebastian on: fix time offsets
-    MMatrix fix_time_offsets_between_pixels_in_ns(
-        "MFixTimeOffset","titel"
-    );
-    plist.AddToList(&fix_time_offsets_between_pixels_in_ns);
-
-    // -------------------------------------------------------------------
-
-    // Jens Buss on: residual time spread
-    MParameterD resTimeSpread("ResidualTimeSpread");
-    resTimeSpread.SetVal(0.0);
-    plist.AddToList(&resTimeSpread);
-
-    // -------------------------------------------------------------------
-
-    // Jens Buss on: residual time spread
-    MParameterD gapdTimeJitter("GapdTimeJitter");
-    gapdTimeJitter.SetVal(0.0);
-    plist.AddToList(&gapdTimeJitter);
-
-    // -------------------------------------------------------------------
-
-    // Remove isolated pixels
-    MImgCleanStd clean(0, 0);
-    //clean.SetCleanLvl0(0); // The level above which isolated pixels are kept
-    clean.SetCleanRings(0);
-    clean.SetMethod(MImgCleanStd::kAbsolute);
-
-    //clean.SetNamePedPhotCam("MPedPhotFromExtractorRndm");
-
-    MHillasCalc hcalc;
-    hcalc.Disable(MHillasCalc::kCalcConc);
-
-    MHCamEvent evt0a(/*10*/3, "Signal",    "Average signal (absolute);;S [ph]");
-    MHCamEvent evt0c(/*10*/3, "MaxSignal", "Maximum signal (absolute);;S [ph]");
-    MHCamEvent evt0d(/*11*/8, "ArrTm",     "Time after first photon;;T [ns]");
-    evt0a.SetErrorSpread(kFALSE);
-    evt0c.SetCollectMax();
-
-    MContinue cut("", "Cut");
-
-    MFillH fillx0a(&evt0a,             "MSignalCam",      "FillAvgSignal");
-    MFillH fillx0c(&evt0c,             "MSignalCam",      "FillMaxSignal");
-    MFillH fillx0d(&evt0d,             "MSignalCam",      "FillArrTm");
-    MFillH fillx1("MHHillas",          "MHillas",         "FillHillas");
-    MFillH fillx3("MHHillasSrc",       "MHillasSrc",      "FillHillasSrc");
-    MFillH fillx4("MHNewImagePar",     "MNewImagePar",    "FillNewImagePar");
-    MFillH fillth("MHThreshold",       "",                "FillThreshold");
-    MFillH fillca("MHCollectionArea",  "",                "FillTrigArea");
-
-    fillth.SetNameTab("Threshold");
-    fillca.SetNameTab("TrigArea");
-
-    // -------------------------------------------------------------------
+
+    // --------------------------------------------------------------------------------
+    // --------------------------------------------------------------------------------
+    // Filling of tasks in tasklist
+    // --------------------------------------------------------------------------------
+    // --------------------------------------------------------------------------------
 
     if (header.IsDataRun())
     {
-        tasks.AddToList(&read);
-        tasks.AddToList(&precut);
-        tasks.AddToList(&pointing);
-        tasks.AddToList(&simmmcs);
-        if (!fPathOut.IsNull() && !HasNullOut())
+        tasks.AddToList(&read);  // Reading Corsika
+        tasks.AddToList(&precut);  // Precut
+        tasks.AddToList(&pointing);  // Simulating pointing
+        tasks.AddToList(&simmmcs);  // Simulating MMCS
+        if (!fPathOut.IsNull() && !HasNullOut())  // Write Tasks for corsika infos
         {
             //tasks.AddToList(&write1b);
@@ -801,71 +918,71 @@
         //        tasks.AddToList(&stars);
         if (1)
-            tasks.AddToList(&simatm); // Here because before fillh1
-        tasks.AddToList(&calcangle);
-        tasks.AddToList(&fillh1);
-        tasks.AddToList(&fillG);
+            tasks.AddToList(&simatm); // Here because before fillh1  // atmosphere absorption
+        tasks.AddToList(&calcangle);  // calculation incident angle
+        tasks.AddToList(&fillh1);  // fill histogram task
+        tasks.AddToList(&fillG);  // fill histogram task
         if (!header.IsPointRun())
         {
-            tasks.AddToList(&absapd);
-            tasks.AddToList(&absmir);
-            tasks.AddToList(&additionalPhotonAcceptance);
+            tasks.AddToList(&absapd);  // photon detection efficiency of the apds
+            tasks.AddToList(&absmir);  // mirror reflectivity
+            tasks.AddToList(&additionalPhotonAcceptance);  // addition photon acceptance
             if (0)
-                tasks.AddToList(&simatm); // FASTER?
+                tasks.AddToList(&simatm); // FASTER?  // be aware this is 'commented'
         }
-        tasks.AddToList(&reflect);
+        tasks.AddToList(&reflect);  // Simulation of the reflector
         if (!header.IsPointRun())
         {
-            tasks.AddToList(&fill0);
+            tasks.AddToList(&fill0);  // fill histogram task
             //tasks.AddToList(&fill1);
-            tasks.AddToList(&fill2);
-            tasks.AddToList(&fill3);
-            tasks.AddToList(&fill4);
-            tasks.AddToList(&fillF1);
+            tasks.AddToList(&fill2);  // fill histogram task
+            tasks.AddToList(&fill3);  // fill histogram task
+            tasks.AddToList(&fill4);  // fill histogram task
+            tasks.AddToList(&fillF1);  // fill histogram task
         }
-        tasks.AddToList(&cones);
-        tasks.AddToList(&cones2);
+        tasks.AddToList(&cones);  // angular acceptance of winston cones
+        tasks.AddToList(&cones2);  // transmission of winston cones
         //if (header.IsPointRun())
         //    tasks.AddToList(&fillP);
-        tasks.AddToList(&cont1);
+        tasks.AddToList(&cont1);  // continue if at least 2 photons
     }
     // -------------------------------
-    tasks.AddToList(&apply);
+    tasks.AddToList(&apply);  // apply geometry to MGeomCam containers
     if (header.IsDataRun())
     {
-        tasks.AddToList(&simpsf);
-        tasks.AddToList(&simgeom);
-        tasks.AddToList(&cont2);
+        tasks.AddToList(&simpsf);  // simulates additional psf (NOT used in default mode)
+        tasks.AddToList(&simgeom);  // tag which pixel is hit by which photon
+        tasks.AddToList(&cont2);  // continue if at least 2 photons
         if (!header.IsPointRun())
         {
-            tasks.AddToList(&fillF2);
-            tasks.AddToList(&fillh2);
+            tasks.AddToList(&fillF2);  // fill histogram task
+            tasks.AddToList(&fillh2);  // fill histogram task
         }
-        tasks.AddToList(&cont3);
+        tasks.AddToList(&cont3);  // continue if at least 3 photons
     }
     if (fCamera)
     {
         if (header.IsPedestalRun() || header.IsCalibrationRun())
-            tasks.AddToList(&simcal);
-        tasks.AddToList(&simnsb);
-        tasks.AddToList(&simapd);
-        tasks.AddToList(&simexcnoise);
-    }
-    tasks.AddToList(&simsum);
+            tasks.AddToList(&simcal);  // add calibration signal for calibration runs
+        tasks.AddToList(&simnsb);  // simulate nsb and dark counts
+        tasks.AddToList(&simapd);  // simulate SiPM behaviour (dead time, crosstalk ...)
+        tasks.AddToList(&simexcnoise);  // add excess noise
+    }
+    tasks.AddToList(&simsum);  // bundle photons (NOT used in default mode)
     if (fCamera)
     {
-        tasks.AddToList(&simcam);
+        tasks.AddToList(&simcam);  // simulate camera behaviour (creates analog signal)
         if (header.IsDataRun() || fForceTrigger)
-            tasks.AddToList(&simtrig);
-        tasks.AddToList(&conttrig);
-        tasks.AddToList(&simdaq);
-    }
-    tasks.AddToList(&simsignal);  // What do we do if signal<0?
+            tasks.AddToList(&simtrig);  // simulate trigger
+        tasks.AddToList(&conttrig);  // continue if trigger pos is valid
+        tasks.AddToList(&simdaq);  // simulate data aquisition
+    }
+    tasks.AddToList(&simsignal);  // What do we do if signal<0?  // fill MSimSignal container
     if (!fPathOut.IsNull() && !HasNullOut())
     {
         //tasks.AddToList(&write1a);
         if (!header.IsPedestalRun())
-            tasks.AddToList(&write2a);
+            tasks.AddToList(&write2a);  // outputtask
         if (fCamera)
-            tasks.AddToList(&write3a);
+            tasks.AddToList(&write3a);  // outputtask (this is the raw data writing tasks)
     }
     // -------------------------------
@@ -874,37 +991,46 @@
         // FIXME: MHCollectionArea Trigger Area!
         if (header.IsDataRun())
-            tasks.AddToList(&fillh3);
-        tasks.AddToList(&filltp);
+            tasks.AddToList(&fillh3);  // fill histogram task
+        tasks.AddToList(&filltp);  // fill histogram task
     }
     if (header.IsDataRun())
     {
-        tasks.AddToList(&fillew);
-        tasks.AddToList(&filled);
+        tasks.AddToList(&fillew);  // fill histogram task
+        tasks.AddToList(&filled);  // fill histogram task
     }
     if (!header.IsPedestalRun())
     {
-        tasks.AddToList(&fillx0a);
-        tasks.AddToList(&fillx0c);
+        tasks.AddToList(&fillx0a);  // fill histogram task
+        tasks.AddToList(&fillx0c);  // fill histogram task
     }
     if (header.IsDataRun())
     {
-        tasks.AddToList(&clean);
-        tasks.AddToList(&hcalc);
+        tasks.AddToList(&clean);  // Cleaning for standard analysis
+        tasks.AddToList(&hcalc);  // Hillas parameter calculation
         tasks.AddToList(&cut);
-        tasks.AddToList(&fillx0d);
-        tasks.AddToList(&fillx1);
+        tasks.AddToList(&fillx0d);  // fill histogram task
+        tasks.AddToList(&fillx1);  // fill histogram task
         //tasks.AddToList(&fillx2);
-        tasks.AddToList(&fillx3);
-        tasks.AddToList(&fillx4);
+        tasks.AddToList(&fillx3);  // fill histogram task
+        tasks.AddToList(&fillx4);  // fill histogram task
         if (!HasNullOut())
             tasks.AddToList(&write4a);
         //tasks.AddToList(&fillx5);
 
-        tasks.AddToList(&fillh4);
-        tasks.AddToList(&fillth);
-        tasks.AddToList(&fillca);
-    }
-    //-------------------------------------------
-
+        tasks.AddToList(&fillh4);  // fill histogram task
+        tasks.AddToList(&fillth);  // fill histogram task
+        tasks.AddToList(&fillca);  // fill histogram task
+    }
+
+
+    // --------------------------------------------------------------------------------
+    // --------------------------------------------------------------------------------
+    // Event loop and processing display
+    // --------------------------------------------------------------------------------
+    // --------------------------------------------------------------------------------
+
+    // --------------------------------------------------------------------------------
+    // Creation of event loop
+    // --------------------------------------------------------------------------------
     tasks.SetAccelerator(MTask::kAccDontReset|MTask::kAccDontTime);
 
@@ -918,17 +1044,25 @@
 
     // FIXME: If pedestal file given use the values from this file
-    //-------------------------------------------
+
+
+    // --------------------------------------------------------------------------------
+    // Preparation of the graphicalk display which is shown while processing
+    // --------------------------------------------------------------------------------
 
     MGeomCam *cam = static_cast<MGeomCam*>(env2.GetCont());
 
-    if (binstr.IsDefault())
-        binstr.SetEdgesLin(150, -shape.GetWidth(),
+
+    MBinning *binsd  = (MBinning*) plist.FindObject("BinningDistC");
+    MBinning *binsd0 = (MBinning*) plist.FindObject("BinningDist");
+    MBinning *binstr = (MBinning*) plist.FindObject("BinningTrigPos");
+    if (binstr->IsDefault())
+        binstr->SetEdgesLin(150, -shape.GetWidth(),
                            header.GetFreqSampling()/1000.*header.GetNumSamples()+shape.GetWidth());
 
-    if (binsd.IsDefault() && cam)
-        binsd.SetEdgesLin(100, 0, cam->GetMaxRadius()*cam->GetConvMm2Deg());
-
-    if (binsd0.IsDefault() && cam)
-        binsd0.SetEdgesLin(100, 0, cam->GetMaxRadius()*cam->GetConvMm2Deg());
+    if (binsd->IsDefault() && cam)
+        binsd->SetEdgesLin(100, 0, cam->GetMaxRadius()*cam->GetConvMm2Deg());
+
+    if (binsd0->IsDefault() && cam)
+        binsd0->SetEdgesLin(100, 0, cam->GetMaxRadius()*cam->GetConvMm2Deg());
 
 
@@ -1045,5 +1179,7 @@
     }
 
-    //-------------------------------------------
+    // --------------------------------------------------------------------------------
+    // Perform event loop
+    // --------------------------------------------------------------------------------
 
     // Execute first analysis
Index: trunk/Mars/mjobs/MJSimulation.h
===================================================================
--- trunk/Mars/mjobs/MJSimulation.h	(revision 18449)
+++ trunk/Mars/mjobs/MJSimulation.h	(revision 18450)
@@ -40,4 +40,6 @@
     void SetupHeaderKeys(MWriteFitsFile& write, MRawRunHeader &header) const;
     void SetupVetoColumns(MWriteFitsFile& write) const;
+    void SetupHeaderOperationMode(MRawRunHeader &header) const;
+    void CreateBinningObjects(MParList &plist) const;
 
 public:
