Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 9308)
+++ trunk/MagicSoft/Mars/Changelog	(revision 9309)
@@ -97,5 +97,16 @@
      - added -I../mtrigger
 
-
+   * mjobs/MJSimulation.[h,cc]:
+     - added new mode to force the use of the trigger "electronics"
+     - moved setup of the fadc to the resource file
+     - added a new histogram to show the maximum signal in all pixels
+     - automatically setup the range of the TrigPos histogram
+     - don't show all histograms in all run conditions
+
+   * mraw/MRawRunHeader.[h,cc]:
+     - added ReadEnv for a setup from a resource file in ceres
+
+   * msimcamera/MSimAPD.cc:
+     - added a sanity check if ReInit has not been called
 
 
Index: trunk/MagicSoft/Mars/mjobs/MJSimulation.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJSimulation.cc	(revision 9308)
+++ trunk/MagicSoft/Mars/mjobs/MJSimulation.cc	(revision 9309)
@@ -27,4 +27,18 @@
 //  MJSimulation
 //
+//
+// Force reading a corsika file even if the footer (RUNE-section) is missing
+// by setting fForceMode to kTRUE or from the resource file by
+//
+//    ForceMode: Yes
+//
+//
+// In case of a pedestal or calibration run the artificial trigger can
+// be "switched off" and the cosmics trrigger "switched on" by setting
+// fForceTrigger to kTRUE or from the resource file by
+//
+//    ForceTrigger: Yes
+//
+//
 /////////////////////////////////////////////////////////////////////////////
 #include "MJSimulation.h"
@@ -95,5 +109,6 @@
 // Sets fRuns to 0, fExtractor to NULL, fDataCheck to kFALSE
 //
-MJSimulation::MJSimulation(const char *name, const char *title) : fForceMode(kFALSE)
+MJSimulation::MJSimulation(const char *name, const char *title)
+    : fForceMode(kFALSE), fForceTrigger(kFALSE)
 {
     fName  = name  ? name  : "MJSimulation";
@@ -103,5 +118,6 @@
 Bool_t MJSimulation::CheckEnvLocal()
 {
-    fForceMode = GetEnv("ForceMode", fForceMode);
+    fForceMode    = GetEnv("ForceMode",    fForceMode);
+    fForceTrigger = GetEnv("ForceTrigger", fForceTrigger);
 
     return kTRUE;
@@ -231,5 +247,6 @@
 
     MRawRunHeader header;
-    header.InitFadcType(3);
+    header.SetValidMagicNumber();
+    //header.InitFadcType(3);
 
     header.SetRunInfo(/*MRawRunHeader::kRTMonteCarlo|*/MRawRunHeader::kRTData,        1, 1);
@@ -237,7 +254,13 @@
     {
         if (!args.GetArgumentStr(0).CompareTo("pedestal", TString::kIgnoreCase))
+        {
             header.SetRunInfo(/*MRawRunHeader::kRTMonteCarlo|*/MRawRunHeader::kRTPedestal,    1, 1);
+            header.SetSourceInfo("Pedestal");
+        }
         if (!args.GetArgumentStr(0).CompareTo("calibration", TString::kIgnoreCase))
+        {
             header.SetRunInfo(/*MRawRunHeader::kRTMonteCarlo|*/MRawRunHeader::kRTCalibration,    1, 1);
+            header.SetSourceInfo("Calibration");
+        }
         if (!args.GetArgumentStr(0).CompareTo("pointrun", TString::kIgnoreCase))
             header.SetRunInfo(/*MRawRunHeader::kRTMonteCarlo|*/MRawRunHeader::kRTPointRun,    1, 1);
@@ -288,6 +311,6 @@
     MBinning binszd( 70,     0,       70, "BinningZd");
     MBinning binsvc(155,     0,       31, "BinningViewCone");
-    MBinning binstr(150,   -25,      125, "BinningTrigger");
     MBinning binsew(150,     0,       25, "BinningEvtWidth");
+    MBinning binstr("BinningTrigPos");
 
     plist.AddToList(&binse);
@@ -298,6 +321,6 @@
     plist.AddToList(&binsaz);
     plist.AddToList(&binsvc);
+    plist.AddToList(&binsew);
     plist.AddToList(&binstr);
-    plist.AddToList(&binsew);
 
     MHn mhn1, mhn2, mhn3;
@@ -307,5 +330,5 @@
 
     MH3 mhtp("TriggerPos.fVal-IntendedPulsePos.fVal-MPulseShape.GetPulseWidth");
-    mhtp.SetName("Trigger");
+    mhtp.SetName("TrigPos");
     mhtp.SetTitle("Trigger position w.r.t. the first photon hitting an APD");
 
@@ -456,9 +479,12 @@
     hcalc.Disable(MHillasCalc::kCalcConc);
 
-    MHCamEvent evt0a(/*10*/0, "Signal",   "Average signal;;S [ph]");
-    MHCamEvent evt0d(/*11*/8, "ArrTm",    "Time after first photon;;T [ns]");
+    MHCamEvent evt0a(/*10*/0, "Signal",    "Average signal;;S [ph]");
+    MHCamEvent evt0c(/*10*/0, "MaxSignal", "Maximum signal;;S [ph]");
+    MHCamEvent evt0d(/*11*/8, "ArrTm",     "Time after first photon;;T [ns]");
     evt0a.SetErrorSpread(kFALSE);
-
-    MFillH fillx0a(&evt0a,             "MSignalCam",      "FillSignal");
+    evt0c.SetCollectMax();
+
+    MFillH fillx0a(&evt0a,             "MSignalCam",      "FillAvgSignal");
+    MFillH fillx0c(&evt0c,             "MSignalCam",      "FillMaxSignal");
     MFillH fillx0d(&evt0d,             "MSignalCam",      "FillArrTm");
     MFillH fillx1("MHHillas",          "MHillas",         "FillHillas");
@@ -517,5 +543,5 @@
     tasks.AddToList(&simsum);
     tasks.AddToList(&simcam);
-    if (header.IsDataRun())
+    if (header.IsDataRun() || fForceTrigger)
         tasks.AddToList(&simtrig);
     tasks.AddToList(&conttrig);
@@ -525,5 +551,6 @@
     {
         tasks.AddToList(&write1);
-        tasks.AddToList(&write2);
+        if (!header.IsPedestalRun())
+            tasks.AddToList(&write2);
         tasks.AddToList(&write3);
     }
@@ -532,14 +559,19 @@
         tasks.AddToList(&fillh3);
     tasks.AddToList(&filltp);
-    tasks.AddToList(&fillew);
-    tasks.AddToList(&fillx0a);
-//    tasks.AddToList(&clean);
-    tasks.AddToList(&hcalc);
-    tasks.AddToList(&fillx0d);
-    tasks.AddToList(&fillx1);
-    //tasks.AddToList(&fillx2);
-    tasks.AddToList(&fillx3);
-    //tasks.AddToList(&fillx4);
-    //tasks.AddToList(&fillx5);
+    if (header.IsDataRun())
+        tasks.AddToList(&fillew);
+    if (!header.IsPedestalRun())
+    {
+        tasks.AddToList(&fillx0a);
+        tasks.AddToList(&fillx0c);
+        //tasks.AddToList(&clean);
+        tasks.AddToList(&hcalc);
+        tasks.AddToList(&fillx0d);
+        tasks.AddToList(&fillx1);
+        //tasks.AddToList(&fillx2);
+        tasks.AddToList(&fillx3);
+        //tasks.AddToList(&fillx4);
+        //tasks.AddToList(&fillx5);
+    }
 
     //-------------------------------------------
@@ -555,4 +587,10 @@
         return kFALSE;
 
+    if (binstr.IsDefault())
+        binstr.SetEdgesLin(150, -shape.GetPulseWidth(),
+                           header.GetFreqSampling()/1000.*header.GetNumSamples()+shape.GetPulseWidth());
+
+    header.Print();
+
     // Execute first analysis
     if (!evtloop.Eventloop(fMaxEvents))
Index: trunk/MagicSoft/Mars/mjobs/MJSimulation.h
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJSimulation.h	(revision 9308)
+++ trunk/MagicSoft/Mars/mjobs/MJSimulation.h	(revision 9309)
@@ -12,5 +12,7 @@
 {
 private:
-    Bool_t fForceMode;
+    Bool_t fForceMode;      // Force execution even if RUNE-section was not found
+
+    Bool_t fForceTrigger;   // Force the use of the trigger "electronics"
 
     Bool_t WriteResult();
Index: trunk/MagicSoft/Mars/mraw/MRawRunHeader.cc
===================================================================
--- trunk/MagicSoft/Mars/mraw/MRawRunHeader.cc	(revision 9308)
+++ trunk/MagicSoft/Mars/mraw/MRawRunHeader.cc	(revision 9309)
@@ -938,6 +938,7 @@
         switch (fFadcType)
         {
-        case 1: *fLog << "Siegen"; break;
-        case 2: *fLog << "MUX"; break;
+        case 1:      *fLog << "Siegen"; break;
+        case 2:      *fLog << "MUX"; break;
+        case 0xffff: *fLog << "artificial"; break;
         default: *fLog << "unknown";
         }
@@ -953,8 +954,12 @@
     *fLog << "Source:      '" << fSourceName << "' " << "  ";
     *fLog << fSourceEpochChar << dec << fSourceEpochDate << endl;
-    *fLog << "Run Start:    " << fRunStart << endl;
-    *fLog << "Run Stop:     " << fRunStop << endl;
-    *fLog << "Crates:       " << fNumCrates << " x " << fNumPixInCrate << " Pixel/Crate = " << fNumCrates*fNumPixInCrate << " Pixel/Evt" << endl;
-    *fLog << "Num Pixels:   " << GetNumNormalPixels() << " (normal) + " << GetNumSpecialPixels() << " (special) = " << GetNumConnectedPixels() << " (total)" << endl;
+    if (fRunStart)
+        *fLog << "Run Start:    " << fRunStart << endl;
+    if (fRunStop)
+        *fLog << "Run Stop:     " << fRunStop << endl;
+    if (fNumCrates>0 || fNumPixInCrate>0)
+        *fLog << "Crates:       " << fNumCrates << " x " << fNumPixInCrate << " Pixel/Crate = " << fNumCrates*fNumPixInCrate << " Pixel/Evt" << endl;
+    if (GetNumConnectedPixels()>0)
+        *fLog << "Num Pixels:   " << GetNumNormalPixels() << " (normal) + " << GetNumSpecialPixels() << " (special) = " << GetNumConnectedPixels() << " (total)" << endl;
     if (fFormatVersion>6)
         *fLog << "Sampling:     " << fSamplingFrequency << "MHz with " << (int)fFadcResolution << " significant bits" << endl;
@@ -1248,2 +1253,53 @@
     fNumEventsRead = num;
 }
+
+// --------------------------------------------------------------------------
+//
+//  NumSamples: 50
+//  NumBytePerSample: 2
+//  SamplingFrequency: 2000
+//  FadcResolution: 12
+//  FadcType: XXXX
+//
+Int_t MRawRunHeader::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
+{
+    Bool_t rc = kFALSE;
+
+    if (IsEnvDefined(env, prefix, "NumSamples", print))
+    {
+        rc = kTRUE;
+        fNumSamplesHiGain = GetEnvValue(env, prefix, "NumSamples", fNumSamplesHiGain);
+        fNumSamplesLoGain = 0;
+    }
+
+    if (IsEnvDefined(env, prefix, "NumBytesPerSample", print))
+    {
+        rc = kTRUE;
+        fNumBytesPerSample = GetEnvValue(env, prefix, "NumBytesPerSample", fNumBytesPerSample);
+    }
+
+    if (IsEnvDefined(env, prefix, "SamplingFrequency", print))
+    {
+        rc = kTRUE;
+        fSamplingFrequency = GetEnvValue(env, prefix, "SamplingFrequency", fSamplingFrequency);
+    }
+
+    if (IsEnvDefined(env, prefix, "FadcResolution", print))
+    {
+        rc = kTRUE;
+        fFadcResolution = GetEnvValue(env, prefix, "FadcResolution", fFadcResolution);
+    }
+    // Saturation behaviour etc.
+    if (IsEnvDefined(env, prefix, "FadcType", print))
+    {
+        //rc = kTRUE;
+        //TString type = GetEnvValue(env, prefix, "FadcType", "");
+        // Eval "Siegen", "MUX", Dwarf"
+    }
+    else
+        if (rc)
+            fFadcType = 0xffff; // "Artificial"
+
+    return rc;
+}
+
Index: trunk/MagicSoft/Mars/mraw/MRawRunHeader.h
===================================================================
--- trunk/MagicSoft/Mars/mraw/MRawRunHeader.h	(revision 9308)
+++ trunk/MagicSoft/Mars/mraw/MRawRunHeader.h	(revision 9309)
@@ -78,4 +78,7 @@
 
     Bool_t ReadEvtOld(istream& fin);
+
+    // MParContainer
+    Int_t ReadEnv(const TEnv &env, TString prefix, Bool_t print);
 
 public:
Index: trunk/MagicSoft/Mars/msimcamera/MSimAPD.cc
===================================================================
--- trunk/MagicSoft/Mars/msimcamera/MSimAPD.cc	(revision 9308)
+++ trunk/MagicSoft/Mars/msimcamera/MSimAPD.cc	(revision 9309)
@@ -154,4 +154,13 @@
     // average hit rate
     const UInt_t npix = fAPDs.GetEntriesFast();
+
+    // Check if we can safely proceed (this can fail if we either haven't been
+    // ReInit'ed or the max index in MPhotonStatistics is wrong)
+    if ((Int_t)npix<fStat->GetMaxIndex())
+    {
+        *fLog << err << "ERROR - MSimAPD::Process: Only " << npix << " APDs initialized. At least " << fStat->GetMaxIndex() << " needed... abort." << endl;
+        return kERROR;
+    }
+
     for (UInt_t idx=0; idx<npix; idx++)
         static_cast<APD*>(fAPDs.UncheckedAt(idx))->FillRandom(fFreq, fStat->GetTimeFirst());
