Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 8352)
+++ trunk/MagicSoft/Mars/Changelog	(revision 8353)
@@ -19,4 +19,23 @@
                                                  -*-*- END OF LINE -*-*-
 
+ 2007/03/02 Thomas Bretz
+
+   * mraw/MRawRunHeader.h:
+     - added GetNumBytesPerSample() member function
+
+   * mraw/MRawRead.cc:
+     - splitted ReadPixel and setting ab-flag into function calls
+     - fixed skipping pixels if bps>1
+
+   * mraw/MRawEvtData.[h,cc]:
+     - removed last argument from AddPixel
+     - split reading pixel data and setting ab-flag into two functions
+     - increased version number from 5 to 6
+     - adapted Print function
+     - adapted Draw function
+     - adapted ReadPixel function
+
+
+
  2007/03/01 Thomas Bretz
 
@@ -70,4 +89,8 @@
      - according to this the lower signal limit has been decresed
        by 15
+     - changed "saturation" limit accordingly from 254 to 245
+
+   * mpedestal/MPedestalsubtractedEvt.h:
+     - added a shortcut for GetSaturation
 
    * mjobs/MJPedestal.cc:
Index: trunk/MagicSoft/Mars/mraw/MRawEvtData.cc
===================================================================
--- trunk/MagicSoft/Mars/mraw/MRawEvtData.cc	(revision 8352)
+++ trunk/MagicSoft/Mars/mraw/MRawEvtData.cc	(revision 8353)
@@ -18,5 +18,5 @@
 !   Author(s): Thomas Bretz, 12/2000 <mailto:tbretz@astro.uni-wuerzburg.de>
 !
-!   Copyright: MAGIC Software Development, 2000-2004
+!   Copyright: MAGIC Software Development, 2000-2007
 !
 !
@@ -47,4 +47,9 @@
 //
 //
+//   Version 6
+//   ------------------
+//    - The data can now be stoe in a single array keeping he full
+//      compatibility
+//
 //   Version 5 (0.8.5):
 //   ------------------
@@ -68,5 +73,4 @@
 //
 /////////////////////////////////////////////////////////////////////////////
-
 #include "MRawEvtData.h"
 
@@ -141,4 +145,7 @@
 UShort_t MRawEvtData::GetNumHiGainSamples() const
 {
+    // If value<0 we are reading old MC files which don't have the
+    // value set so far. So we use the old methid to determin the
+    // numbers and calculate them from the length of the arrays.
     return fHiGainPixId->GetSize() ? fHiGainFadcSamples->GetSize()/fHiGainPixId->GetSize() : 0;
 }
@@ -150,4 +157,7 @@
 UShort_t MRawEvtData::GetNumLoGainSamples() const
 {
+    // If value<0 we are reading old MC files which don't have the
+    // value set so far. So we use the old methid to determin the
+    // numbers and calculate them from the length of the arrays.
     return fLoGainPixId->GetSize() ? fLoGainFadcSamples->GetSize()/fLoGainPixId->GetSize() : 0;
 }
@@ -159,7 +169,6 @@
 UShort_t MRawEvtData::GetNumPixels() const
 {
-    return fHiGainPixId->GetSize();
-}
-
+    return fHiGainPixId ? fHiGainPixId->GetSize() : 0;
+}
 
 // --------------------------------------------------------------------------
@@ -180,58 +189,47 @@
     const UShort_t nLoSamp = GetNumLoGainSamples();
 
-    const UShort_t nHiPix = fHiGainPixId->GetSize();;
-    const UShort_t nLoPix = fLoGainPixId->GetSize();;
-
     fLog->unsetf(ios::showbase);
 
     *fLog << dec << all;
     *fLog << GetDescriptor() << ": " << endl;
-    *fLog << "HiGain: " << nHiPix << " Pixels with " << (Int_t)nHiSamp << " Samples" << endl;
-    *fLog << "LoGain: " << nLoPix << " Pixels with " << (Int_t)nLoSamp << " Samples";
+    *fLog << GetNumPixels() << " Pixels with " << (Int_t)nHiSamp << "/" << (Int_t)nLoSamp << " samples" << endl;
 
     TString str(opt);
     Int_t manip = str.Contains("dec", TString::kIgnoreCase);
 
-    Int_t l=0;
-    for (int i=0; i<nHiPix; i++)
-    {
-        const UShort_t idx = (*fHiGainPixId)[i];
-
-        *fLog << endl;
-        *fLog << " " << setfill(' ') << setw(3) << dec << i << " - " << setw(3);
-        *fLog << dec << idx << " ";
-
-        if (fABFlags->GetSize())
-        {
-            *fLog << "<" << hex << setfill('0') << setw(2);
-            *fLog << ((Int_t)(*fABFlags)[idx/8]&0xff) << "> ";
-        }
-
-        *fLog << (manip?dec:hex) << (manip ? setfill(' ') : setfill('0'));
+    MRawEvtPixelIter pixel(const_cast<MRawEvtData*>(this));
+    Int_t i = 0;
+    while (pixel.Next())
+    {
+        const UShort_t idx = pixel.GetPixelId();
+
+        *fLog << endl << dec << setfill(' ');
+        *fLog << " " << setw(3) << i++ << " - " << setw(3) << idx << " ";
+
+        if (pixel.IsABFlagValid())
+            *fLog << "<" << (pixel.HasABFlag()?"B":"A") << "> ";
+
+        *fLog << (manip?dec:hex) << setfill(manip?' ':'0');
+
+        const Byte_t *hi = pixel.GetHiGainSamples();
+        const Byte_t *lo = pixel.GetLoGainSamples();
 
         for (int j=0; j<nHiSamp; j++)
         {
             *fLog << setw(manip?3:2);
-            *fLog << ((UShort_t)(*fHiGainFadcSamples)[j+i*nHiSamp]&0xff);
+            *fLog << ((UShort_t)hi[j]&0xff);
             if (manip)
                 *fLog << ' ';
         }
 
-        if (!(l<nLoPix && (*fLoGainPixId)[l]==idx))
-            continue;
-
-        if (manip)
-            *fLog << "/ ";
-
         for (int j=0; j<nLoSamp; j++)
         {
             *fLog << setw(manip?3:2);
-            *fLog << ((UShort_t)(*fLoGainFadcSamples)[j+i*nLoSamp]&0xff);
+            *fLog << ((UShort_t)lo[j]&0xff);
             if (manip)
                 *fLog << ' ';
         }
-        l++;
-    }
-    *fLog << endl;
+    }
+    *fLog << dec << endl;
 }
 
@@ -292,4 +290,6 @@
         for (int i=0; i<nh; i++)
             graphhi->SetPoint(graphhi->GetN(), i, higains[i]);
+        for (int i=0; i<nl; i++)
+            graphhi->SetPoint(graphhi->GetN(), i+nh, logains[i]);
 
         graphhi->SetMaximum(256);
@@ -305,24 +305,4 @@
         histhi->SetXTitle("Time/FADC Slices");
         histhi->SetYTitle("Signal/FADC Units");
-
-        if (nl>0)
-        {
-            TGraph *graphlo = new TGraph;
-
-            for (int i=0; i<nl; i++)
-                graphlo->SetPoint(graphlo->GetN(), i, logains[i]);
-
-            graphlo->SetMaximum(256);
-            graphlo->SetMinimum(0);
-            graphlo->SetLineColor(kBlue);
-
-            graphlo->SetBit(kCanDelete);
-            graphlo->Draw("C*");
-
-            TH1F *histlo = graphlo->GetHistogram();
-
-            histlo->SetXTitle("Time/FADC Slices");
-            histlo->SetYTitle("Signal/FADC Units");
-        }
 
         return;
@@ -342,17 +322,9 @@
         for (int i=0; i<nh; i++)
             histh->Fill(i, higains[i]);
+        for (int i=0; i<nl; i++)
+            histh->Fill(i, logains[i]);
         histh->SetBit(kCanDelete);
         histh->Draw(same ? "same" : "");
 
-        if (nl>0)
-        {
-            TH1F *histl = new TH1F(name+";2", "FADC Samples", nl, -0.5, nl-.5);
-            histl->SetLineColor(kBlue);
-            histl->SetDirectory(NULL);
-            for (int i=0; i<nl; i++)
-                histl->Fill(i, logains[i]);
-            histl->SetBit(kCanDelete);
-            histl->Draw("same");
-        }
         return;
     }
@@ -368,11 +340,12 @@
 void MRawEvtData::InitArrays(UShort_t numconnected, UShort_t maxid)
 {
-    const Int_t numhi = fRunHeader ? fRunHeader->GetNumSamplesHiGain() : 0;
-    const Int_t numlo = fRunHeader ? fRunHeader->GetNumSamplesLoGain() : 0;
+    // fRunHeader should not be set only in the constructor!
+    const Int_t numhi = fRunHeader ? fRunHeader->GetNumSamplesHiGain() : -1;
+    const Int_t numlo = fRunHeader ? fRunHeader->GetNumSamplesLoGain() : -1;
 
     fHiGainPixId       = new MArrayS(numconnected);
-    fLoGainPixId       = new MArrayS(numconnected);
-    fHiGainFadcSamples = new MArrayB(numconnected*numhi);
-    fLoGainFadcSamples = new MArrayB(numconnected*numlo);
+    fLoGainPixId       = new MArrayS(0);
+    fHiGainFadcSamples = new MArrayB(numconnected*(numhi+numlo));
+    fLoGainFadcSamples = new MArrayB(0);
 
     fABFlags           = new MArrayB(maxid==0 ? 0 : maxid/8+1);
@@ -416,6 +389,36 @@
 //  This is to fill the data of one pixel to the MRawEvtHeader Class.
 //  The parameters are the pixelnumber and the FADC_SLICES values of ADCs
-//  Add to lo gains if lflag = 1
-//
+//
+void MRawEvtData::AddPixel(UShort_t nOfPixel, TArrayC *data)
+{
+    const Int_t n = fRunHeader->GetNumSamples();
+    if (data->GetSize()!=n)
+    {
+        *fLog << err << "RawEvtData::AddPixel: Error, number of samples in ";
+        *fLog << "TArrayC " << data->GetSize() << " doesn't match current number " << n << endl;
+        return;
+    }
+
+    //
+    // enhance pixel array by one
+    //
+    fHiGainPixId->Set(fHiGainPixId->GetSize()+1);
+
+    //
+    // add the number of the new pixel to the array as last entry
+    //
+    fHiGainPixId->AddAt(nOfPixel, fHiGainPixId->GetSize()-1);
+
+    //
+    // enhance the array by the number of new samples
+    //
+    fHiGainFadcSamples->Set(fHiGainFadcSamples->GetSize()+n);
+
+    //
+    // add the new slices as last entries to array
+    //
+    fHiGainFadcSamples->AddAt((Byte_t*)data->GetArray(), fHiGainFadcSamples->GetSize()-n, n);
+}
+/*
 void MRawEvtData::AddPixel(UShort_t nOfPixel, TArrayC *data, Bool_t lflag)
 {
@@ -456,30 +459,44 @@
     arrsam->AddAt((Byte_t*)data->GetArray(), arrsam->GetSize()-ns, ns);
 }
-
-void MRawEvtData::ReadPixel(istream &fin, Int_t npix, Bool_t ab)
-{
-    Byte_t *poshi = fHiGainFadcSamples->GetArray() + fConnectedPixels*fRunHeader->GetNumSamplesHiGain();
-    Byte_t *poslo = fLoGainFadcSamples->GetArray() + fConnectedPixels*fRunHeader->GetNumSamplesLoGain();
+*/
+
+void MRawEvtData::ReadPixel(istream &fin, Int_t npix)
+{
+    // number of samples
+    const UInt_t ns = fRunHeader->GetNumSamples();
+
+    // position in higain array
+    Byte_t *pos = fHiGainFadcSamples->GetArray() + fConnectedPixels*ns;
+
+    // bytes per sample
+    const Int_t bps = fRunHeader->GetNumBytesPerSample();
+
+    // Number of bytes
+    const Int_t nb = ns*bps;
+
+    // Set pixel index
+    fHiGainPixId->AddAt(npix, fConnectedPixels++);
 
     // Read data for one pixel
-    fin.read((char*)poshi, fRunHeader->GetNumSamplesHiGain());
-    fHiGainPixId->AddAt(npix, fConnectedPixels);
-
-    if (poslo)
-    {
-        fin.read((char*)poslo, fRunHeader->GetNumSamplesLoGain());
-        // FIXME: Not implemented in the raw files yet
-        //if (IsLoGainOn(i, j))
-        //{
-        fLoGainPixId->AddAt(npix, fConnectedPixels);
-        //}
-    }
-
+    if (bps==1)
+    {
+        fin.read((char*)pos, nb);
+        return;
+    }
+
+    // Read data for one pixel
+    Byte_t arr[nb];
+    fin.read((char*)arr, nb);
+
+    for (Byte_t *p=arr+bps-1; p<arr+nb; p+=bps)
+        *pos++ = *p;
+}
+
+void MRawEvtData::SetABFlag(Int_t npix, Bool_t ab)
+{
     if (ab)
         SETBIT((*fABFlags)[npix/8], npix%8);
     else
         CLRBIT((*fABFlags)[npix/8], npix%8);
-
-    fConnectedPixels++;
 }
 
Index: trunk/MagicSoft/Mars/mraw/MRawEvtData.h
===================================================================
--- trunk/MagicSoft/Mars/mraw/MRawEvtData.h	(revision 8352)
+++ trunk/MagicSoft/Mars/mraw/MRawEvtData.h	(revision 8353)
@@ -36,5 +36,5 @@
     MArrayB *fABFlags;            //-> A information about the exact trigger position
 
-    Int_t fConnectedPixels;   //!
+    Int_t fConnectedPixels;       //!
 
     void InitArrays(UShort_t numconnected=0, UShort_t maxid=0);
@@ -61,12 +61,13 @@
 
     void ResetPixels(UShort_t npix, UShort_t maxid);
-    void AddPixel(UShort_t nOfPixel, TArrayC *data, Bool_t lflag=kFALSE);
+    void AddPixel(UShort_t nOfPixel, TArrayC *data);
 
     UShort_t GetNumHiGainSamples() const;
     UShort_t GetNumLoGainSamples() const;
+    UInt_t   GetNumSamples() const { return GetNumHiGainSamples()+GetNumLoGainSamples(); }
     UShort_t GetNumPixels() const;
 
-    void ReadPixel(istream &fin, Int_t npix, Bool_t ab);
-    //void ReadEvt(istream &fin, Int_t posinarray);
+    void ReadPixel(istream &fin, Int_t npix);
+    void SetABFlag(Int_t npix, Bool_t ab);
     void SkipEvt(istream &fin);
 
@@ -85,5 +86,5 @@
         ;
 
-    ClassDef(MRawEvtData, 5) //Container to store the raw Event Data
+    ClassDef(MRawEvtData, 6) //Container to store the raw Event Data
 };
 
