Index: trunk/MagicSoft/Mars/mraw/MRawEvtHeader.cc
===================================================================
--- trunk/MagicSoft/Mars/mraw/MRawEvtHeader.cc	(revision 3047)
+++ trunk/MagicSoft/Mars/mraw/MRawEvtHeader.cc	(revision 3139)
@@ -236,81 +236,52 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Decode the binary Time Stamp. For more detailed information see the
+// source code.
+//
 Bool_t MRawEvtHeader::DecodeTime(UInt_t abstime[2]) const
 {
-    // BADC|1032 --> DCBA|3210 (Byte swap - exchange MSB and LSB)
-    /*
-    *fLog << hex << all << setfill('0') << setw(8) << abstime[0] << " " << setw(8) << abstime[1] << "  ";
-
-    for (int i=4*8-1; i>=0; i--)
-        *fLog << (int)(abstime[0]&BIT(i)?1:0);
-    *fLog << " ";
-    for (int i=4*8-1; i>=0; i--)
-       *fLog << (int)(abstime[1]&BIT(i)?1:0);
-    */
-    *fLog << endl;
-
-    abstime[0] = (abstime[0]<<16) | (abstime[0]>>16);
-//  abstime[1] = (abstime[1]<<16) | (abstime[1]>>16);
-//    *fLog << hex << abstime[0] << " " << abstime[1] << endl;
-//    *fLog << dec;
-
-    abstime[0] = abstime[0]<<8&0xff00 | abstime[0]>>8&0x00ff | abstime[0]&0xffff0000;
-    abstime[1] = abstime[1]<<8&0xff00 | abstime[1]>>8&0x00ff | abstime[1]&0xffff0000;
-
-
-/*    *fLog << hex << abstime[0] << " " << abstime[1] << endl;
-*/
-
-    // Strange thing!
-    Char_t t = abstime[0]>>8 &0xf;
-    abstime[0] &= 0xfffff0ff;
-
-//    *fLog << abstime[0]<< " " << (int)t << endl;
-
-
-    for (int i=0; i<4; i++)
-    {
-        abstime[0] |= (t&BIT(i) ? 1 : 0)<<(11-i);
-        //*fLog << ((t&BIT(i) ? 1 : 0)<<(11-i)) << " ";
-    }
-
-    //*fLog << endl << abstime[0]<< endl;
-
-    // Decode
-    const Byte_t h = (abstime[0]>>18 & 0x3)*10 + (abstime[0]>>14 & 0xf);
-    const Byte_t m = (abstime[0]>>11 & 0x7)*10 + (abstime[0]>> 7 & 0xf);
-    const Byte_t s = (abstime[0]>> 4 & 0x7)*10 + (abstime[0]>> 0 & 0xf);
-    const Int_t ms =
-        ((abstime[0]>>12)&0xf)*1000 +
-        ((abstime[0]>> 8)&0xf)* 100 +
-        ((abstime[0]>> 4)&0xf)*  10 +
-        ((abstime[0]>> 0)&0xf)*   1;
-
-    // hms   =3210 -->  h=2:4 m=3:4 s=3:4
-    // subsec=DCBA --> subsec?
-    /*
-    *fLog << all << dec << setfill('0') << setw(2) << (int)h << ":" << setw(2) << (int)m << ":" << setw(2) << (int)s;
-    *fLog << " " << hex;
-    *fLog << (int)(abstime[0]>>12 & 0xf) << ".";
-    *fLog << (int)(abstime[0]>> 8 & 0xf) << ".";
-    *fLog << (int)(abstime[0]>> 4 & 0xf) << ".";
-    *fLog << (int)(abstime[0]>> 0 & 0xf);
-    *fLog << endl;
-    */
+    //
+    // SuperSecond (20 bits giving hh:mm:ss)
+    // ------------
+    // 
+    // Reading the hours:
+    // Swap bits: 23->16, 22->17, 21->16, 20->19
+    //
+    abstime[0] =
+        abstime[0]>>7 & 0x00010000 |
+        abstime[0]>>5 & 0x00020000 |
+        abstime[0]>>3 & 0x00040000 |
+        abstime[0]>>1 & 0x00080000 |
+        abstime[0]    & 0xff00ffff;
+
+    // 
+    // SubSecond (24 bits giving number of clock ticks of a 5Mhz signal since 
+    // the beginning of last second, i.e., number of ns with a precision of to
+    // 200 ns) 
+    // ----------
+    //
+    // Due to a problem with one Digital Module, three of the less significant
+    // eight bits of the subsecond are corrupted. So, until new DM's arrive to
+    // La Palma, we won't use the eight first bits of the subsecond. 
+    // This reduces the precision from 200 ns to of 51.2 us.
+    //
+    abstime[1] &= 0xffffff00;
+
+    //
+    // Decode Time Stamp
+    //
+    const Byte_t h  = (abstime[0]>>18 & 0x3)*10 + (abstime[0]>>14 & 0xf);
+    const Byte_t m  = (abstime[0]>>11 & 0x7)*10 + (abstime[0]>> 7 & 0xf);
+    const Byte_t s  = (abstime[0]>> 4 & 0x7)*10 + (abstime[0]>> 0 & 0xf);
+    const UInt_t ns =  abstime[1]*200;
+
+    //
     // Update the time stamp with the current event time.
     // Make sure, that the time stamp was initialized correctly
     // with the start-date/time of the run (after reading the run header)
     //
-    // Here the nanosec precision is ignored... (FIXME!)
-
-    return kTRUE;
-
-    static int i=0;
-    i++;
-    //fTime->Print();
-    //    fTime->UpdMagicTime(h, m, s, 0/*ms*/);
-    //fTime->Print();
-    return i<25;
-    //return fTime->UpdMagicTime(h, m, s, ms);
+    return fTime->UpdMagicTime(h, m, s, ns);
 }
 
@@ -319,4 +290,6 @@
 // read the EVENT HEADER information from the input stream
 // return FALSE if there is now header anymore, else TRUE
+//
+// For version>2 we expect to have a valid time-stamp in the files.
 //
 // Updates the time stamp with the current event time.
@@ -326,17 +299,18 @@
 // Remark: This 'feature' disallows single runs of more than 11h!
 //
-int MRawEvtHeader::ReadEvt(istream &fin)
+int MRawEvtHeader::ReadEvt(istream &fin, UShort_t ver)
 {
     fin.read((char*)&fDAQEvtNumber, 4);  // Total=4
 
-    UInt_t abstime[2];                   // BADC|1032
+    UInt_t abstime[2];
     fin.read((char*)abstime,        8);  // Total=12
-    /*
-    if (!DecodeTime(abstime))
-    {
-        *fLog << err << "ERROR - Event time in event header invalid... abort." << endl;
-        return kFALSE;
-    }
-    */
+
+    if (ver>2)
+        if (!DecodeTime(abstime))
+        {
+            *fLog << err << "ERROR - Event time in event header invalid... abort." << endl;
+            return kFALSE;
+        }
+
     Byte_t dummy[4];
     fin.read((char*)&fNumTrigLvl1,  4);  // Total=16
@@ -357,2 +331,3 @@
     return !fin.eof();
 }
+
Index: trunk/MagicSoft/Mars/mraw/MRawEvtHeader.h
===================================================================
--- trunk/MagicSoft/Mars/mraw/MRawEvtHeader.h	(revision 3047)
+++ trunk/MagicSoft/Mars/mraw/MRawEvtHeader.h	(revision 3139)
@@ -60,5 +60,5 @@
     UInt_t GetDAQEvtNumber() const { return fDAQEvtNumber; }
 
-    int ReadEvt(istream& fin);
+    int ReadEvt(istream& fin, UShort_t ver);
 
     ClassDef(MRawEvtHeader, 1) // Parameter Conatiner for raw EVENT HEADER
Index: trunk/MagicSoft/Mars/mraw/MRawRead.cc
===================================================================
--- trunk/MagicSoft/Mars/mraw/MRawRead.cc	(revision 3047)
+++ trunk/MagicSoft/Mars/mraw/MRawRead.cc	(revision 3139)
@@ -121,5 +121,5 @@
 // --------------------------------------------------------------------------
 //
-// This is a workaround for the oldest runs (run-number<3490)
+// This is a workaround for the oldest runs (file version<3)
 // for which no time stamp was available.
 // For this runs a fake time stamp is created
@@ -137,5 +137,5 @@
     const Double_t t   = tm/mhz;                       // [us]
     const UInt_t ns    = (UInt_t)fmod(t*1e3, 1e6);
-    const UShort_t ms  = (UShort_t)fmod(t/1e3, 1e3);
+    //const UShort_t ms  = (UShort_t)fmod(t/1e3, 1e3);
     const Byte_t s     = (Byte_t)fmod(t/1e6, 60);
 
@@ -146,5 +146,5 @@
     m %= 360; // 6h
 
-    fRawEvtTime->UpdMagicTime(m/60, m%60, s, ms/*, ns*/);
+    fRawEvtTime->UpdMagicTime(m/60, m%60, s, ns);
 }
 
@@ -155,9 +155,14 @@
 Bool_t MRawRead::ReadEvent(istream &fin)
 {
+    //
+    //  Get file format version
+    //
+    const UShort_t ver = fRawRunHeader->GetFormatVersion();
+
     //
     //  Read in the next EVENT HEADER (see specification),
     // if there is no next event anymore stop eventloop
     //
-    if (!fRawEvtHeader->ReadEvt(fin))
+    if (!fRawEvtHeader->ReadEvt(fin, ver))
         return kFALSE;
 
@@ -177,9 +182,4 @@
     //
     fRawCrateArray->SetSize(nc);
-
-    //
-    //  Get file format version
-    //
-    const UShort_t ver = fRawRunHeader->GetFormatVersion();
 
     //
@@ -197,9 +197,9 @@
     }
 
-    // This is a workaround for the oldest runs (run-number<3490)
+    // This is a workaround for the oldest runs (version<3)
     // for which no time stamp was available.
     // For this runs a fake time stamp is created
-    //if (fRawRunHeader->GetRunNumber()<3490)
-    CreateFakeTime();
+    if (ver<3)
+        CreateFakeTime();
 
     // FIXME: For all other runs we should enhance the precision
