Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 1030)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 1031)
@@ -1,3 +1,28 @@
                                                                   -*-*- END -*-*-
+ 2001/11/06: Thomas Bretz
+
+   * mraw/MRawCrateArray.[h,cc]:
+     - added SetSize which takes care of the size of the array
+     - changed the access to an unchecked one
+
+   * mraw/MRawCrateData.h
+     - derives from MParContainer now
+
+   * mraw/MRawEvtData.[h,cc]:
+     - made Print const
+     - changed so, that the arrays are dreed and allocated only
+       as less as possible
+
+   * mraw/MRawFileRead.[h,cc]:
+     - changed to support faster I/O of MRawEvtData and MRawCrateArray
+     - added sanity check (warning) to PostProcess
+
+   * mraw/MRawFileWrite.cc:
+     - removed deletion of trees (done by ~TFile)
+     - switched of AutoSave by use of a big size
+     - removed '.' from Branch names (each tree contains only one master
+       branch with equal subbranches)
+
+
 
  2001/11/01: Thomas Bretz
Index: /trunk/MagicSoft/Mars/mraw/MRawCrateArray.cc
===================================================================
--- /trunk/MagicSoft/Mars/mraw/MRawCrateArray.cc	(revision 1030)
+++ /trunk/MagicSoft/Mars/mraw/MRawCrateArray.cc	(revision 1031)
@@ -41,4 +41,5 @@
 
 #include "MLog.h"
+#include "MRawCrateData.h"
 
 ClassImp(MRawCrateArray);
@@ -80,16 +81,24 @@
 }
 
-// --------------------------------------------------------------------------
-//
-// Return a pointer the i-th entry in the array
-//
-MRawCrateData *MRawCrateArray::GetEntry(Int_t i)
+void MRawCrateArray::SetSize(Int_t i)
 {
-    return (MRawCrateData*)fArray->AddrAt(i);
+    if (fArray->GetEntriesFast() == i)
+        return;
+
+    fArray->ExpandCreateFast(i);
 }
 
 // --------------------------------------------------------------------------
 //
-// Return the i-th entry in the array
+// Return a pointer the i-th entry in the array, without range check
+//
+MRawCrateData *MRawCrateArray::GetEntry(Int_t i)
+{
+    return (MRawCrateData*)fArray->UncheckedAt(i); // AddrAt would be with rcheck
+}
+
+// --------------------------------------------------------------------------
+//
+// Return the i-th entry in the array, with range check
 //
 MRawCrateData* &MRawCrateArray::operator[](Int_t i)
Index: /trunk/MagicSoft/Mars/mraw/MRawCrateArray.h
===================================================================
--- /trunk/MagicSoft/Mars/mraw/MRawCrateArray.h	(revision 1030)
+++ /trunk/MagicSoft/Mars/mraw/MRawCrateArray.h	(revision 1031)
@@ -25,4 +25,5 @@
     void Clear(Option_t *opt=NULL);
 
+    void SetSize(Int_t i);
     MRawCrateData *GetEntry(Int_t i);
 
Index: /trunk/MagicSoft/Mars/mraw/MRawCrateData.h
===================================================================
--- /trunk/MagicSoft/Mars/mraw/MRawCrateData.h	(revision 1030)
+++ /trunk/MagicSoft/Mars/mraw/MRawCrateData.h	(revision 1031)
@@ -2,11 +2,11 @@
 #define MARS_MRawCrateData
 
-#ifndef MARS_MAGIC
-#include "MAGIC.h"
+#ifndef MARS_MParContainer
+#include "MParContainer.h"
 #endif
 
 class ifstream;
 
-class MRawCrateData : public TObject
+class MRawCrateData : public MParContainer
 {
 private:
Index: /trunk/MagicSoft/Mars/mraw/MRawEvtData.cc
===================================================================
--- /trunk/MagicSoft/Mars/mraw/MRawEvtData.cc	(revision 1030)
+++ /trunk/MagicSoft/Mars/mraw/MRawEvtData.cc	(revision 1031)
@@ -138,5 +138,5 @@
 //     "dec"      Prints the time slices decimal
 //
-void MRawEvtData::Print(Option_t *opt)
+void MRawEvtData::Print(Option_t *opt) const
 {
     //
@@ -296,4 +296,12 @@
 void MRawEvtData::DeletePixels(Bool_t flag)
 {
+    const int npix = fRunHeader->GetNumCrates()*fRunHeader->GetNumPixInCrate();
+
+    if (fArraySize == npix && flag)
+    {
+        fPosInArray = 0;
+        return;
+    }
+
     DeleteArrays();
     InitArrays(flag);
@@ -319,10 +327,26 @@
 void MRawEvtData::InitArrays(Bool_t flag)
 {
-    // const int npix = !flag ? 0 : fRunHeader->GetNumCrates()*fRunHeader->GetNumPixInCrate();
-
-    fHiGainPixId       = new MArrayS(0);//npix);
-    fLoGainPixId       = new MArrayS(0);//npix); 
-    fHiGainFadcSamples = new MArrayB(0);//npix*fRunHeader->GetNumSamplesHiGain());
-    fLoGainFadcSamples = new MArrayB(0);//npix*fRunHeader->GetNumSamplesLoGain());
+    if (flag)
+    {
+        const int npix = fRunHeader->GetNumCrates()*fRunHeader->GetNumPixInCrate();
+
+        fHiGainPixId       = new MArrayS(npix);
+        fLoGainPixId       = new MArrayS(npix);
+        fHiGainFadcSamples = new MArrayB(npix*fRunHeader->GetNumSamplesHiGain());
+        fLoGainFadcSamples = new MArrayB(npix*fRunHeader->GetNumSamplesLoGain());
+
+        fArraySize  = npix;
+    }
+    else
+    {
+        fHiGainPixId       = new MArrayS(0);
+        fLoGainPixId       = new MArrayS(0);
+        fHiGainFadcSamples = new MArrayB(0);
+        fLoGainFadcSamples = new MArrayB(0);
+
+        fArraySize = 0;
+    }
+
+    fPosInArray = 0;
 }
 
@@ -384,21 +408,11 @@
     const UShort_t npic = fRunHeader->GetNumPixInCrate();
 
-    //
-    // Enhance array by the size which we'll read now
-    //
-    const int npixhi = fHiGainPixId->GetSize();
-    const int npixlo = fLoGainPixId->GetSize();
-
-    fHiGainPixId->Set(npixhi+npic);
-    fLoGainPixId->Set(npixlo+npic);
-
-    const int nsamhi = fHiGainFadcSamples->GetSize();
-    const int nsamlo = fLoGainFadcSamples->GetSize();
-
-    fHiGainFadcSamples->Set(nsamhi+nhi*npic);
-    fLoGainFadcSamples->Set(nsamlo+nlo*npic);
-
-    Byte_t *higainsam = fHiGainFadcSamples->GetArray()+nsamhi;
-    Byte_t *logainsam = fLoGainFadcSamples->GetArray()+nsamlo;
+    const UShort_t npos = npic*fPosInArray;
+
+    Byte_t *higainsam = fHiGainFadcSamples->GetArray()+nhi*npos;
+    Byte_t *logainsam = fLoGainFadcSamples->GetArray()+nlo*npos;
+
+    // UShort_t *hipixid = (UShort_t*)fHiGainPixId->GetArray()+npos;
+    // UShort_t *lopixid = (UShort_t*)fLoGainPixId->GetArray()+npos;
 
     for (int i=0; i<npic; i++)
@@ -409,4 +423,5 @@
         const UShort_t npix = fRunHeader->GetPixAssignment(i);
 
+        const UShort_t ipos = npos+i;
         //
         //  This is to fill the data of one pixel to the MRawEvtHeader Class.
@@ -414,5 +429,5 @@
         //  Add to lo gains if lflag = 1
         //
-        fHiGainPixId->AddAt(npix, npixhi+i);
+        fHiGainPixId->AddAt(npix, ipos);
         fin.read(higainsam, nhi);
         higainsam += nhi;
@@ -421,5 +436,5 @@
         //if (IsLoGainOn(i, j))
         //{
-        fLoGainPixId->AddAt(npix, npixlo+i);
+        fLoGainPixId->AddAt(npix, ipos);
         fin.read(logainsam, nlo);
         logainsam += nlo;
Index: /trunk/MagicSoft/Mars/mraw/MRawEvtData.h
===================================================================
--- /trunk/MagicSoft/Mars/mraw/MRawEvtData.h	(revision 1030)
+++ /trunk/MagicSoft/Mars/mraw/MRawEvtData.h	(revision 1031)
@@ -27,4 +27,7 @@
     MArrayB *fLoGainFadcSamples;  // list of lo gain samples of all pixels (ordering: see fLoGainPixId)
 
+    Int_t fPosInArray;   //!
+    Int_t fArraySize;    //!
+
     void InitArrays(Bool_t flag=kFALSE);
     void DeleteArrays();
@@ -44,12 +47,13 @@
 
     void Clear(Option_t * = NULL);
-    void Print(Option_t * = NULL);
+    void Print(Option_t * = NULL) const;
     void Draw (Option_t * = NULL);
 
     void DeletePixels(Bool_t flag=kFALSE);
+    void ResetPixels() { DeletePixels(kTRUE); }
     void AddPixel(UShort_t nOfPixel, TArrayC *data, Bool_t lflag=kFALSE);
 
-    Byte_t  GetNumHiGainSamples() const;
-    Byte_t  GetNumLoGainSamples() const;
+    Byte_t   GetNumHiGainSamples() const;
+    Byte_t   GetNumLoGainSamples() const;
     UShort_t GetNumPixels() const;
 
Index: /trunk/MagicSoft/Mars/mraw/MRawFileRead.cc
===================================================================
--- /trunk/MagicSoft/Mars/mraw/MRawFileRead.cc	(revision 1030)
+++ /trunk/MagicSoft/Mars/mraw/MRawFileRead.cc	(revision 1031)
@@ -25,5 +25,5 @@
 //////////////////////////////////////////////////////////////////////////////
 //                                                                          //
-//  MRawFile                                                                //
+//  MRawFileRead                                                            //
 //                                                                          //
 //  This tasks reads the raw binary file like specified in the TDAS???      //
@@ -35,5 +35,5 @@
 //                                                                          //
 //  Output Containers:                                                      //
-//   MRawRunHeader, MRawEvtHeader, MRawEvtData, MRawCrateArray, MTime       //
+//   MRawRunHeader, MRawEvtHeader, MRawEvtData, MRawCrateArray, MRawEvtTime //
 //                                                                          //
 //////////////////////////////////////////////////////////////////////////////
@@ -55,7 +55,7 @@
 
 
-/*/  ----------- please don't delete and don't care about (Thomas) ------------
-#define kBUFSZ 1024
-
+/*  ----------- please don't delete and don't care about (Thomas) ------------
+#define kBUFSZ 64 //1024*1024*64
+#include <iomanip.h>
 class bifstream : public istream, public streambuf
 {
@@ -72,7 +72,9 @@
     {
         int sz=fread(fBuffer, kBUFSZ, 1, fd);
+        //int sz=fread(fBuffer, 1, kBUFSZ, fd);
         setg(fBuffer, fBuffer, fBuffer+kBUFSZ);
 
-        return sz==kBUFSZ ? *(unsigned char*)fBuffer : EOF;//EOF;
+        return sz==1 ? *(unsigned char*)fBuffer : EOF;//EOF;
+        //return sz==kBUFSZ ? *(unsigned char*)fBuffer : EOF;//EOF;
     }
 public:
@@ -90,4 +92,5 @@
 //
 MRawFileRead::MRawFileRead(const char *fname, const char *name, const char *title)
+    : fFileName(fname)
 {
     fName  = name  ? name  : "MRawFileRead";
@@ -198,17 +201,18 @@
 
     //
+    //  Get number of crates from the run header
+    //
+    const UShort_t nc = fRawRunHeader->GetNumCrates();
+
+    //
     // Delete arrays which stores the pixel information (time slices)
     //
-    fRawEvtData->DeletePixels();
+    fRawEvtData->ResetPixels();
 
     //
     // clear the TClonesArray which stores the Crate Information
-    //
-    fRawCrateArray->Clear();
-
-    //
-    //  Get number of crates from the run header
-    //
-    const UShort_t nc = fRawRunHeader->GetNumCrates();
+    // and create a new array of the correct size
+    //
+    fRawCrateArray->SetSize(nc);
 
     //
@@ -225,2 +229,15 @@
 }
 
+// --------------------------------------------------------------------------
+//
+//  Checks whether the number of read events differs from the number the
+//  file should containe (MRawRunHeader). Prints a warning if it doesn't
+//  match.
+//
+Bool_t MRawFileRead::PostProcess()
+{
+    if (fRawRunHeader->GetNumEvents() != GetNumExecutions()-1)
+        *fLog << "WARNING - number of read events doesn't match number in run header." << endl;
+
+    return kTRUE;
+}
Index: /trunk/MagicSoft/Mars/mraw/MRawFileRead.h
===================================================================
--- /trunk/MagicSoft/Mars/mraw/MRawFileRead.h	(revision 1030)
+++ /trunk/MagicSoft/Mars/mraw/MRawFileRead.h	(revision 1031)
@@ -2,5 +2,5 @@
 #define MARS_MRawFileRead
 
-#ifndef MARS_MTASK
+#ifndef MARS_MTask
 #include "MTask.h"
 #endif
@@ -33,4 +33,5 @@
     Bool_t PreProcess(MParList *pList);
     Bool_t Process();
+    Bool_t PostProcess();
 
     ClassDef(MRawFileRead, 0)	// Task to read the raw data binary file
Index: /trunk/MagicSoft/Mars/mraw/MRawFileWrite.cc
===================================================================
--- /trunk/MagicSoft/Mars/mraw/MRawFileWrite.cc	(revision 1030)
+++ /trunk/MagicSoft/Mars/mraw/MRawFileWrite.cc	(revision 1031)
@@ -83,4 +83,11 @@
 
     delete fOut;
+
+    //
+    // Remark:
+    // - Trees are automatically deleted by the the file
+    //   (unless file.SetDirectory(0) was called)
+    // - Branches are automatically deleted by the tree destructor
+    //
 }
 
@@ -156,12 +163,17 @@
 
     //
+    // Remark:
+    // - Trees are automatically deleted by the the file
+    //   (unless file.SetDirectory(0) was called)
+    // - Branches are automatically deleted by the tree destructor
+    //
+
+    //
     // Write the run header information to the file
     //
-    TTree   *rh = new TTree("RunHeaders", "Run headers of all runs in this file");
-    TBranch *tb = rh->Branch("MRawRunHeader", "MRawRunHeader", &fRawRunHeader, 32000);
+    TTree *rh = new TTree("RunHeaders", "Run headers of all runs in this file");
+    rh->Branch("MRawRunHeader", "MRawRunHeader", &fRawRunHeader, 32000);
     rh->Fill();
-    rh->Write();
-    delete tb;
-    delete rh;
+    //rh->Write();
 
     //
@@ -173,19 +185,39 @@
 
     //
+    // From the root dicumentation:
+    //
+    // Note that calling TTree::AutoSave too frequently (or similarly calling
+    // TTree::SetAutoSave with a small value) is an expensive operation.
+    // You should make tests for your own application to find a compromize
+    // between speed and the quantity of information you may loose in case of
+    // a job crash.
+    //
+    // In case your program crashes before closing the file holding this tree,
+    // the file will be automatically recovered when you will connect the file
+    // in UPDATE mode.
+    // The Tree will be recovered at the status corresponding to the last AutoSave.
+    //
+    fTData       ->SetAutoSave(2000000000); // 2GB
+    fTPedestal   ->SetAutoSave(2000000000); // 2GB
+    fTCalibration->SetAutoSave(2000000000); // 2GB
+
+    //
     // create all branches which are necessary
     //
-    fTData       ->Branch("MTime.",          "MTime",          &fRawEvtTime,    32000);
-    fTPedestal   ->Branch("MTime.",          "MTime",          &fRawEvtTime,    32000);
-    fTCalibration->Branch("MTime.",          "MTime",          &fRawEvtTime,    32000);
-    fTData       ->Branch("MRawEvtHeader.",  "MRawEvtHeader",  &fRawEvtHeader,  32000);
-    fTPedestal   ->Branch("MRawEvtHeader.",  "MRawEvtHeader",  &fRawEvtHeader,  32000);
-    fTCalibration->Branch("MRawEvtHeader.",  "MRawEvtHeader",  &fRawEvtHeader,  32000);
-    fTData       ->Branch("MRawEvtData.",    "MRawEvtData",    &fRawEvtData,    32000);
-    fTPedestal   ->Branch("MRawEvtData.",    "MRawEvtData",    &fRawEvtData,    320000);
-    fTCalibration->Branch("MRawEvtData.",    "MRawEvtData",    &fRawEvtData,    320000);
+    // FIXME: Can we calculate a good buffer size out of the event size?
+    //        using splitlevel=0 sppeds up writing by a factor of 5-10%
+    fTData       ->Branch("MTime",          "MTime",          &fRawEvtTime,    32000);
+    fTPedestal   ->Branch("MTime",          "MTime",          &fRawEvtTime,    32000);
+    fTCalibration->Branch("MTime",          "MTime",          &fRawEvtTime,    32000);
+    fTData       ->Branch("MRawEvtHeader",  "MRawEvtHeader",  &fRawEvtHeader,  32000);
+    fTPedestal   ->Branch("MRawEvtHeader",  "MRawEvtHeader",  &fRawEvtHeader,  32000);
+    fTCalibration->Branch("MRawEvtHeader",  "MRawEvtHeader",  &fRawEvtHeader,  32000);
+    fTData       ->Branch("MRawEvtData",    "MRawEvtData",    &fRawEvtData,    320000);
+    fTPedestal   ->Branch("MRawEvtData",    "MRawEvtData",    &fRawEvtData,    320000);
+    fTCalibration->Branch("MRawEvtData",    "MRawEvtData",    &fRawEvtData,    320000);
     //fTree->Branch("MRawCrateArray",  fRawCrateArray->GetArray(),      32000, 1);
-    fTData       ->Branch("MRawCrateArray.", "MRawCrateArray", &fRawCrateArray, 32000);
-    fTPedestal   ->Branch("MRawCrateArray.", "MRawCrateArray", &fRawCrateArray, 32000);
-    fTCalibration->Branch("MRawCrateArray.", "MRawCrateArray", &fRawCrateArray, 32000);
+    fTData       ->Branch("MRawCrateArray", "MRawCrateArray", &fRawCrateArray, 32000);
+    fTPedestal   ->Branch("MRawCrateArray", "MRawCrateArray", &fRawCrateArray, 32000);
+    fTCalibration->Branch("MRawCrateArray", "MRawCrateArray", &fRawCrateArray, 32000);
 
     return kTRUE;
