Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 8583)
+++ trunk/MagicSoft/Mars/Changelog	(revision 8584)
@@ -29,5 +29,6 @@
 
    * mcalib/MCalibrationRelTimeCalc.cc:
-     - replaced kDeviatingTimeResolution by new kDeviatingRelTimeResolution
+     - replaced kDeviatingTimeResolution by new
+       kDeviatingRelTimeResolution
 
    * mhcalib/MHCalibrationPulseTimeCam.cc:
@@ -51,11 +52,24 @@
        maximum value to fit the FADC range
 
-   * mraw/MRawRunHeader.h:
-     - added a function returnung the maximum value of the FADC
+   * msignal/MExtractor.cc:
+     - removed the reset of the saturation limit. Maybe the user 
+       wants to switch off saturation detection.
+     - removed a lot of old code (in comments)
+
+   * msignal/MExtractTimeAndCharge.cc:
+     - scale the saturation limit from the maximum of
+       the FADC range to 256
+     - use the maximum value of the raw position as the input for
+       half the signal height in GetSaturationTime again
+     - calculate maxposhi only if needed
+     - calculate maxrawlo only if needed
+
+   * mraw/MRawRunHeader.[h,cc]:
+     - implemented new format version 9
+     - increased version number by one
+     - added some comments in the header
+     - added a function returning the maximum value of the FADC
      - changed the GetScale from a calculation to a switch 
        (faster and easier to understand)
-
-   * msignal/MExtractor.cc:
-     - get the maximum in case of no lo-gains from the run-header
 
 
Index: trunk/MagicSoft/Mars/NEWS
===================================================================
--- trunk/MagicSoft/Mars/NEWS	(revision 8583)
+++ trunk/MagicSoft/Mars/NEWS	(revision 8584)
@@ -11,4 +11,9 @@
      resource file (to check it open it in the TBrowser and choose Print()
      from the context menu)
+
+   - merpp: Merpp can read file format version 9 now. Please realize
+     that for file format version 9 the extraction range is different
+     and must be set to 0/49 instead of 15/64 in callisto_mux.rc until
+     the first and last 15 slices have been removed from all MUX data.
 
    - callisto: improved calculation of spline coefficients a lot. This
Index: trunk/MagicSoft/Mars/mraw/MRawRunHeader.cc
===================================================================
--- trunk/MagicSoft/Mars/mraw/MRawRunHeader.cc	(revision 8583)
+++ trunk/MagicSoft/Mars/mraw/MRawRunHeader.cc	(revision 8584)
@@ -33,4 +33,8 @@
 //  =======================
 //
+//  Format Version 9:
+//  -----------------
+//   + fNumEventsRead;
+//
 //  Format Version 8:
 //  -----------------
@@ -73,4 +77,8 @@
 //  MRawRunHeader CLASS VERSION
 //  ===========================
+//
+//  Format Version 7:
+//  -----------------
+//   - added fNumEventsRead;
 //
 //  Format Version 6:
@@ -108,5 +116,4 @@
 //
 ////////////////////////////////////////////////////////////////////////////
-
 #include "MRawRunHeader.h"
 
@@ -124,5 +131,5 @@
 
 const UShort_t MRawRunHeader::kMagicNumber      = 0xc0c0;
-const Byte_t   MRawRunHeader::kMaxFormatVersion =      8;
+const Byte_t   MRawRunHeader::kMaxFormatVersion =      9;
 
 // --------------------------------------------------------------------------
@@ -430,13 +437,17 @@
 
     // ----- Source position -----
+    fin.seekg(fFormatVersion>5 ? 16 : 8, ios::cur);
+    /*
     if (fFormatVersion>5)
     {
-        fin.read((char*)dummy, 4); // F32 SourceRA;  Total=48
-        fin.read((char*)dummy, 4); // F32 SourceDEC; Total=52
-    }
+        fin.read((char*)&fSourceRa,  4);  // F32 SourceRA;  Total=48
+        fin.read((char*)&fSourceDec, 4);  // F32 SourceDEC; Total=52
+    }
+    fin.read((char*)&fTelescopeRa,  4);   // F32 TelescopeRA;  (+4)
+    fin.read((char*)&fTelescopeDec, 4);   // F32 TelescopeDEC; (+4)
+    */
+
     // Maybe we should set these to something
     // in case of fFormatVersion<6
-    fin.read((char*)dummy, 4); // F32 TelescopeRA;  (+4)
-    fin.read((char*)dummy, 4); // F32 TelescopeDEC; (+4)
     fin.read((char*)&fSourceEpochChar,  2);     // Total=56
     fin.read((char*)&fSourceEpochDate,  2);     // Total=58
@@ -446,5 +457,5 @@
     {
         UShort_t y, m, d;
-        fin.read((char*)dummy, 4); // Former fMJD[4],
+        fin.seekg(4, ios::cur);    // Former fMJD[4],
         fin.read((char*)&y,    2); // Former fDateYear[2]
         fin.read((char*)&m,    2); // Former fDateMonth[2]
@@ -460,6 +471,12 @@
     fin.read((char*)&fNumSamplesHiGain, 2); // MUX: Number of samples per pixel
 
+    if (fFormatVersion>8)
+        fin.read((char*)dummy, 4); // 2xU16 (NumSamplesRemovedHead and NumSamplesRemovedTail)
+
     // ----- Number of events -----
     fin.read((char*)&fNumEvents, 4);     // Total=70
+
+    if (fFormatVersion>8)
+        fin.read((char*)&fNumEventsRead, 4);     // Total=70
 
     // New in general features: (should they be included in new MAGIC1 formats, too?)
@@ -566,5 +583,8 @@
         *fLog << "Sampling:     " << fFreqSampling << "MHz with " << (int)fNumSignificantBits << " significant bits" << endl;
     *fLog << "Samples:      " << fNumSamplesHiGain << "/" << fNumSamplesLoGain << " (hi/lo) * " << fNumBytesPerSample << "B/sample = " << (fNumSamplesLoGain+fNumSamplesHiGain) * fNumCrates * fNumPixInCrate * fNumBytesPerSample/1000 << "kB/Evt" << endl;
-    *fLog << "Evt Counter:  " << fNumEvents << endl;
+    *fLog << "Evt Counter:  " << fNumEvents;
+    if (fFormatVersion>8)
+        *fLog << " (read=" << fNumEventsRead << ")";
+    *fLog << endl;
 
     if (TString(t).Contains("header", TString::kIgnoreCase))
Index: trunk/MagicSoft/Mars/mraw/MRawRunHeader.h
===================================================================
--- trunk/MagicSoft/Mars/mraw/MRawRunHeader.h	(revision 8583)
+++ trunk/MagicSoft/Mars/mraw/MRawRunHeader.h	(revision 8584)
@@ -38,28 +38,29 @@
 private:
     /* ---- Run Header Informations ---- */
-    UShort_t  fMagicNumber;
-    UShort_t  fFormatVersion;
-    UShort_t  fSoftVersion;
-    UShort_t  fFadcType;
-    UShort_t  fCameraVersion;
-    UShort_t  fTelescopeNumber;
-    UShort_t  fRunType;
-    UInt_t    fRunNumber;
-    Char_t    fProjectName[101];//23];
-    Char_t    fSourceName[81];  //13];
-    Char_t    fObservationMode[61];
-    Char_t    fSourceEpochChar[2];
-    UShort_t  fSourceEpochDate;
-    UShort_t  fNumCrates;
-    UShort_t  fNumPixInCrate;
-    UShort_t  fNumSamplesLoGain;
-    UShort_t  fNumSamplesHiGain;
-    UShort_t  fNumBytesPerSample;
-    UInt_t    fNumEvents;
-    UShort_t  fFreqSampling;
-    Byte_t    fNumSignificantBits;
-    MTime     fRunStart;
-    MTime     fRunStop;
-    MArrayS  *fPixAssignment;
+    UShort_t  fMagicNumber;          // File type identifier
+    UShort_t  fFormatVersion;        // File format version
+    UShort_t  fSoftVersion;          // DAQ software version
+    UShort_t  fFadcType;             // FADC type (0=Siegen, 1=MUX)
+    UShort_t  fCameraVersion;        // Camera Version (1=MAGIC I)
+    UShort_t  fTelescopeNumber;      // Telescope number (1=Magic I)
+    UShort_t  fRunType;              // Run Type
+    UInt_t    fRunNumber;            // Run number
+    Char_t    fProjectName[101];     // Project name
+    Char_t    fSourceName[81];       // Source name
+    Char_t    fObservationMode[61];  // observation mode
+    Char_t    fSourceEpochChar[2];   // epoch char of the source
+    UShort_t  fSourceEpochDate;      // epoch date of the source
+    UShort_t  fNumCrates;            // number of electronic boards
+    UShort_t  fNumPixInCrate;        // number of pixels in crate
+    UShort_t  fNumSamplesLoGain;     // number of logain samples stored
+    UShort_t  fNumSamplesHiGain;     // number of higain samples stored
+    UShort_t  fNumBytesPerSample;    // number of bytes per sample
+    UInt_t    fNumEvents;            // number of events stored
+    UInt_t    fNumEventsRead;        // number of events read by the electronics
+    UShort_t  fFreqSampling;         // Sampling Frequency [MHz]
+    Byte_t    fNumSignificantBits;   // number of significant bits
+    MTime     fRunStart;             // time of run start
+    MTime     fRunStop;              // time of run stop
+    MArrayS  *fPixAssignment;        //-> pixel assignment table
 
     Bool_t SwapAssignment(Short_t id0, Short_t id1);
@@ -106,4 +107,5 @@
     UShort_t GetNumBytesPerSample() const { return fNumBytesPerSample; }
     UInt_t   GetNumEvents() const         { return fNumEvents; }
+    UInt_t   GetNumEventsRead() const     { return fNumEventsRead; }
     UShort_t GetFreqSampling() const      { return fFreqSampling; }
     const MTime &GetRunStart() const      { return fRunStart; }
@@ -133,5 +135,5 @@
     Bool_t ReadEvt(istream& fin);
 
-    ClassDef(MRawRunHeader, 6)	// storage container for general info
+    ClassDef(MRawRunHeader, 7)	// storage container for general info
 };
 #endif
Index: trunk/MagicSoft/Mars/msignal/MExtractTimeAndCharge.cc
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractTimeAndCharge.cc	(revision 8583)
+++ trunk/MagicSoft/Mars/msignal/MExtractTimeAndCharge.cc	(revision 8584)
@@ -1,4 +1,4 @@
 /* ======================================================================== *\
-! $Name: not supported by cvs2svn $:$Id: MExtractTimeAndCharge.cc,v 1.65 2007-05-16 13:56:17 tbretz Exp $
+! $Name: not supported by cvs2svn $:$Id: MExtractTimeAndCharge.cc,v 1.66 2007-06-19 11:01:34 tbretz Exp $
 ! --------------------------------------------------------------------------
 !
@@ -189,5 +189,5 @@
 // order polynomial interpolation.
 //
-Double_t MExtractTimeAndCharge::GetSaturationTime(Int_t sat0, const Float_t *sig, Int_t maxpos) const
+Double_t MExtractTimeAndCharge::GetSaturationTime(Int_t sat0, const Float_t *sig, Int_t maxconthalf) const
 {
     const Int_t p = sat0>1 ? sat0-2 : sat0-1;
@@ -195,10 +195,9 @@
         return 0;
 
-    const Float_t &maxcont = sig[maxpos];
     if (sat0==1)
-        return sig[0]>maxcont/2 ? 0 : 0.5;
+        return sig[0]>maxconthalf ? 0 : 0.5;
 
     if (sig[p]>sig[p+1] || sig[p+1]>sig[p+2])
-        return sig[p+1]>maxcont/2 ? sat0-1 : sat0-0.5;
+        return sig[p+1]>maxconthalf ? sat0-1 : sat0-0.5;
 
     // Find the place at which the signal is maxcont/2
@@ -206,5 +205,5 @@
     const TVector3 vy(p, p+1, p+2);
 
-    return MMath::InterpolParabLin(vx, vy, maxcont/2);
+    return MMath::InterpolParabLin(vx, vy, maxconthalf);
 }
 
@@ -223,4 +222,6 @@
     const Int_t numl = fRunHeader->GetNumSamplesLoGain();
 
+    const UInt_t satlim = fSaturationLimit*fRunHeader->GetMax();
+
     MRawEvtPixelIter pixel(fRawEvt);
     while (pixel.Next())
@@ -230,11 +231,8 @@
         const Float_t *sig = fSignal->GetSamples(pixidx);
 
-        const UInt_t maxcont  = fSignal->GetRawMaxVal(pixidx, fHiGainFirst, fHiGainLast);
-        const  Int_t maxposhi = fSignal->GetMaxPos(pixidx, fHiGainFirst, fHiGainLast);
-
         // Would it be better to take lastsat-firstsat?
         Int_t sathi0   = fHiGainFirst;  // First slice to extract and first saturating slice
         Int_t sathi1   = fHiGainLast;   // Last  slice to extract and last saturating slice
-        Int_t numsathi = fSignal->GetSaturation(pixidx, fSaturationLimit, sathi0, sathi1);
+        Int_t numsathi = fSignal->GetSaturation(pixidx, satlim, sathi0, sathi1);
 
         Float_t sumhi =0., deltasumhi =-1; // Set hi-gain of MExtractedSignalPix valid
@@ -246,14 +244,18 @@
 
         if (numsathi<2)
+        {
+            const Int_t maxposhi = fSignal->GetMaxPos(pixidx, fHiGainFirst, fHiGainLast);
             FindTimeAndChargeHiGain2(sig+fHiGainFirst, rangehi,
                                      sumhi, deltasumhi, timehi, deltatimehi,
                                      numsathi, maxposhi);
+        }
 
         // If we have saturating slices try to get a better estimate
         // of the arrival time than timehi or sathi0. This is
         // usefull to know where to start lo-gain extraction.
+        const UInt_t maxcont = fSignal->GetRawMaxVal(pixidx, fHiGainFirst, fHiGainLast);
         if (numsathi>1)
         {
-            timehi = GetSaturationTime(sathi0, sig, fHiGainFirst+maxposhi)-fHiGainFirst;
+            timehi = GetSaturationTime(sathi0, sig, maxcont/2)-fHiGainFirst;
             deltatimehi = 0;
         }
@@ -338,9 +340,7 @@
              */
             // Would it be better to take lastsat-firstsat?
-            const Int_t maxposlo = fSignal->GetMaxPos(pixidx, first, last);
-
             Int_t satlo0 = first;   // First slice to extract and first saturating slice
             Int_t satlo1 = last;    // Last  slice to extract and last saturating slice
-            numsatlo = fSignal->GetSaturation(pixidx, fSaturationLimit, satlo0, satlo1);
+            numsatlo = fSignal->GetSaturation(pixidx, satlim, satlo0, satlo1);
 
             //if (satlo0>first && satlo1<last && numsatlo>2)
@@ -350,5 +350,6 @@
             //}
 
-            const Int_t rangelo = last-first+1;
+            const Int_t rangelo  = last-first+1;
+            const Int_t maxposlo = fSignal->GetMaxPos(pixidx, first, last);
             FindTimeAndChargeLoGain2(sig+first, rangelo,
                                      sumlo, deltasumlo, timelo, deltatimelo,
@@ -360,5 +361,6 @@
             if (numsatlo>1)
             {
-                timelo = GetSaturationTime(satlo0, sig, first+maxposlo)-numh-first;
+                const UInt_t maxrawlo = fSignal->GetRawMaxPos(pixidx, fHiGainFirst, fHiGainLast);
+                timelo = GetSaturationTime(satlo0, sig, first+maxrawlo)-numh-first;
                 deltatimelo = 0;
             }
