Index: trunk/MagicSoft/Mars/mraw/MRawRunHeader.cc
===================================================================
--- trunk/MagicSoft/Mars/mraw/MRawRunHeader.cc	(revision 8961)
+++ trunk/MagicSoft/Mars/mraw/MRawRunHeader.cc	(revision 8966)
@@ -268,4 +268,75 @@
 // --------------------------------------------------------------------------
 //
+// Consistency checks. See code for detils.
+//
+Bool_t MRawRunHeader::IsConsistent() const
+{
+    // FIXME: Match first digits of run-number with telescope number
+
+    if (fFormatVersion>10)
+    {
+        if (GetRunID()!=fTelescopeNumber && GetRunID()!=fTelescopeNumber*10U && GetRunID()!=fTelescopeNumber*10U+5U)
+        {
+            *fLog << err << "ERROR - Telscope number " << fTelescopeNumber << " doesn't match the first two digits of the run number " << fRunNumber << "." << endl;
+            return kFALSE;
+        }
+
+        // Old formats can not contain a run number larger 0
+        if (fRunNumber<1000000)
+        {
+            *fLog << err << "ERROR - Run number " << fRunNumber << " smaller than 1000000." << endl;
+            return kFALSE;
+        }
+    }
+
+    // Check for correct number of bytes in data stream
+    if (fFormatVersion>7 && fNumBytesPerSample!=2)
+    {
+        *fLog << err << "ERROR - " << fNumBytesPerSample << " bytes per sample are not supported!" << endl;
+        return kFALSE;
+    }
+
+    // If we have a vlid stop time check its consistency with the start time
+    if (fRunStop!=MTime() && fRunStop<fRunStart)
+    {
+        *fLog << err << "ERROR - Stop time smaller than start time." << endl;
+        return kFALSE;
+    }
+
+    // No file numbers larger than 999 allowed in general
+    if (fFileNumber>999)
+    {
+        *fLog << err << "ERROR - File number " << fFileNumber << " larger than 999." << endl;
+        return kFALSE;
+    }
+
+    // Old formats can not contain a run number larger 0
+    if (fFormatVersion<11 && fFileNumber>0)
+    {
+        *fLog << err << "ERROR - File number " << fFileNumber << " larger than 0." << endl;
+        return kFALSE;
+    }
+
+    if (fFormatVersion>1)
+    {
+        // For most of the formats the start time must be valid
+        if (fRunStart==MTime())
+        {
+            *fLog << err << "ERROR - Start time invalid." << endl;
+            return kFALSE;
+        }
+
+        // For most of the formats an invalid stop time cannot happen if file closed
+        if (fMagicNumber==kMagicNumber && fRunStop==MTime())
+        {
+            *fLog << err << "ERROR - File closed but stop time invalid." << endl;
+            return kFALSE;
+        }
+    }
+    return kTRUE;
+}
+
+// --------------------------------------------------------------------------
+//
 // This implements a fix of the pixel assignment before 25.9.2005
 //
@@ -398,4 +469,20 @@
 // --------------------------------------------------------------------------
 //
+// Fixes to fix bugs in the run header
+//
+Bool_t MRawRunHeader::Fixes()
+{
+    if (fFormatVersion>8)
+    {
+        fNumEvents--;
+        fNumEventsRead--;
+        *fLog << inf << "Format >V8: Stored number of events decreased by 1." << endl;
+    }
+
+    return FixAssignment();
+}
+
+// --------------------------------------------------------------------------
+//
 //  Reading function to read/interpret the file formats 1-10
 //
@@ -479,12 +566,4 @@
     fin.read((char*)&fNumEvents, 4);     // Total=70
 
-    if (fFormatVersion>8)
-    {
-        fin.read((char*)&fNumEventsRead, 4);     // Total=70
-        fNumEvents--;
-        fNumEventsRead--;
-        *fLog << inf << "Format V9: Stored number of events decreased by 1." << endl;
-    }
-
     // New in general features: (should they be included in new MAGIC1 formats, too?)
     fNumBytesPerSample  = 1;      // 2 for MUX DATA
@@ -497,10 +576,4 @@
         fin.read((char*)&fSamplingFrequency,  2); // [MHz], 2000 for MuxFadc
         fin.read((char*)&fFadcResolution,     1); // nominal resolution [# Bits], 10 for MuxFadc
-
-        if (fNumBytesPerSample!=2)
-        {
-            *fLog << err << "ERROR - " << fNumBytesPerSample << " bytes per sample are not supported!" << endl;
-            return kFALSE;
-        }
     }
 
@@ -511,4 +584,8 @@
         fRunStop.ReadBinary(fin);               // Total += 7
     }
+
+    // ----- Consistency checks -----
+    if (!IsConsistent())
+        return kFALSE;
 
     //
@@ -524,5 +601,5 @@
         fin.read(dummy, 16);
 
-    return FixAssignment();
+    return Fixes();
 }
 
@@ -631,4 +708,8 @@
     fRunStop.SetBinary(Int+91);
 
+    // ----- Consistency checks -----
+    if (!IsConsistent())
+        return kFALSE;
+
     // ----- 388 bytes so far -----
 
@@ -646,5 +727,5 @@
         (*fPixAssignment)[i] = Int[97+i];
 
-    return FixAssignment();
+    return Fixes();
 }
 
Index: trunk/MagicSoft/Mars/mraw/MRawRunHeader.h
===================================================================
--- trunk/MagicSoft/Mars/mraw/MRawRunHeader.h	(revision 8961)
+++ trunk/MagicSoft/Mars/mraw/MRawRunHeader.h	(revision 8966)
@@ -72,4 +72,7 @@
     Bool_t SwapAssignment(Short_t id0, Short_t id1);
     Bool_t FixAssignment();
+    Bool_t Fixes();
+    Bool_t IsConsistent() const;
+
     Bool_t ReadEvtOld(istream& fin);
 
@@ -102,4 +105,8 @@
     UShort_t GetSoftVersion() const       { return fSoftVersion; }
     UInt_t   GetRunNumber() const         { return fRunNumber; }
+    UInt_t   GetFileNumber() const        { return fFileNumber; }
+    UInt_t   GetRunID() const             { return (fRunNumber/1000000)%100; }
+    UInt_t   GetFileID() const            { return fRunNumber>1000000?(fRunNumber%1000000)*1000+(fFileNumber%1000):fRunNumber; }
+    UShort_t GetTelescopeNumber() const   { return fRunType; }
     UShort_t GetRunType() const           { return fRunType; }
     const Char_t *GetRunTypeStr() const;
