Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 1599)
+++ trunk/MagicSoft/Mars/Changelog	(revision 1600)
@@ -1,3 +1,61 @@
                                                                   -*-*- END -*-*-
+
+ 2002/11/13: Thomas Bretz
+
+   * mfilter/MFEventSelector.[h,cc]:
+     - if total number of events read from file the selector worked only for
+       one eventloop - fixed.
+     - changed MReadMarsFile to MRead
+
+   * mbase/MContinue.cc:
+     - fixed a typo
+
+   * mbase/MEvtLoop.cc, meventdisp/MGEvtDisplay.cc:
+     - renamed MReadMarsFile/MReadTree to MRead
+
+   * mfileio/FileIOLinkDef.h, mfileio/Makefile:
+     - added MRead
+
+   * mfileio/MRead.[h,cc]:
+     - added
+
+   * mfileio/MReadMarsFile.[h,cc], mfileio/MReadTree.[h,cc]:
+     - renamed from MReadMarsFile to MRead
+     - derived from MRead
+     - removed progressbar support from MReadTree
+
+   * mfileio/MWriteRootFile.cc:
+     - added a info output
+
+   * mfileio/MCT1ReadPreProc.[h,cc]:
+     - changed to work much much better :)
+
+   * mgeom/GeomLinkDef.h, mmc/McLinkDef.h:
+     - added missing LinkDefs (helllo Oscar!)
+
+   * mgeom/MGeomCamCT1.cc:
+     - mirrored the CT1 camera in x (CT1 default)
+
+   * mgeom/MGeomMirror.[h,cc]:
+     - some small changes
+   
+   * mgeom/MGeomPMT.[h,cc]:
+     - changed usage of TArrayF from pointer to reference
+
+   * mgui/MCamDisplay.cc:
+     - fixed a crash at delete when the user closed a automatically
+       created canvas
+
+   * mhist/MHFadcCam.[h,cc]:
+     - implemented ResetHistograms
+
+   * mhist/MHMatrix.[h,cc]:
+     - implemented ReduceNumberOfRows (preliminary, untested)
+
+   * mmc/MMcConfigRunHeader.[h,cc]:
+     - some small changes
+     - changed usage of TArrayF from pointer to reference
+
+
 
  2002/11/11: Thomas Bretz
Index: trunk/MagicSoft/Mars/mbase/MContinue.cc
===================================================================
--- trunk/MagicSoft/Mars/mbase/MContinue.cc	(revision 1599)
+++ trunk/MagicSoft/Mars/mbase/MContinue.cc	(revision 1600)
@@ -27,5 +27,5 @@
 // MContinue                                                               //
 //                                                                         //
-// Does nothing than return kCONTINUE in the Process-fucntion              //
+// Does nothing than return kCONTINUE in the Process-function              //
 // (use with filters)                                                      //
 //                                                                         //
Index: trunk/MagicSoft/Mars/mbase/MEvtLoop.cc
===================================================================
--- trunk/MagicSoft/Mars/mbase/MEvtLoop.cc	(revision 1599)
+++ trunk/MagicSoft/Mars/mbase/MEvtLoop.cc	(revision 1600)
@@ -85,5 +85,5 @@
 #include "MTaskList.h"
 #ifdef __MARS__
-#include "MReadTree.h"       // for setting progress bar
+#include "MRead.h"           // for setting progress bar
 #include "MProgressBar.h"    // MProgressBar::GetBar
 #endif
@@ -213,8 +213,6 @@
         else
         {
-            MReadTree *read = (MReadTree*)fTaskList->FindObject("MReadTree");
-            if (!read)
-                read = (MReadTree*)fTaskList->FindObject("MReadMarsFile");
-            if (read)
+            MRead *read = (MRead*)fTaskList->FindObject("MRead");
+            if (read && read->GetEntries()>0)
                 fProgress->SetRange(0, read->GetEntries());
         }
Index: trunk/MagicSoft/Mars/meventdisp/MGEvtDisplay.cc
===================================================================
--- trunk/MagicSoft/Mars/meventdisp/MGEvtDisplay.cc	(revision 1599)
+++ trunk/MagicSoft/Mars/meventdisp/MGEvtDisplay.cc	(revision 1600)
@@ -92,5 +92,5 @@
 MReadTree *MGEvtDisplay::GetReader() const
 {
-    return (MReadTree*)GetTaskList()->FindObject("MReadMarsFile");
+    return (MReadTree*)GetTaskList()->FindObject("MRead");
 }
 
Index: trunk/MagicSoft/Mars/mfileio/FileIOLinkDef.h
===================================================================
--- trunk/MagicSoft/Mars/mfileio/FileIOLinkDef.h	(revision 1599)
+++ trunk/MagicSoft/Mars/mfileio/FileIOLinkDef.h	(revision 1600)
@@ -6,4 +6,5 @@
 
 #pragma link C++ class MChain+;
+#pragma link C++ class MRead+;
 #pragma link C++ class MReadTree+;
 #pragma link C++ class MReadMarsFile+;
Index: trunk/MagicSoft/Mars/mfileio/MCT1ReadPreProc.cc
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MCT1ReadPreProc.cc	(revision 1599)
+++ trunk/MagicSoft/Mars/mfileio/MCT1ReadPreProc.cc	(revision 1600)
@@ -42,5 +42,4 @@
 //
 /////////////////////////////////////////////////////////////////////////////
-
 #include "MCT1ReadPreProc.h"
 
@@ -81,7 +80,7 @@
 //
 MCT1ReadPreProc::MCT1ReadPreProc(const char *fname, const char *name,
-                                 const char *title) : fIn(NULL)
-{
-    fName  = name  ? name  : "MCT1ReadPreProc";
+                                 const char *title) : fIn(NULL), fEntries(0)
+{
+    fName  = name  ? name  : "MRead";
     fTitle = title ? title : "Task to loop over events in CT1 ascii file";
 
@@ -113,6 +112,22 @@
 void MCT1ReadPreProc::AddFile(const char *txt)
 {
-    TNamed *name = new TNamed(txt, "");
-    fFileNames->AddLast(name);
+    ifstream *store = fIn;
+
+    fIn = new ifstream(gSystem->ExpandPathName(txt));
+
+    if (!(*fIn))
+    {
+        *fLog << warn << "Cannot open file '" << txt << "'... ignored." << endl;
+        fIn = store;
+        return;
+    }
+
+    fEntries += GetNumEvents();
+
+    delete fIn;
+
+    fIn = store;
+
+    fFileNames->AddLast(new TNamed(txt, ""));
 }
 
@@ -123,10 +138,10 @@
 // data members
 //
-void MCT1ReadPreProc::ProcessHeader(const struct outputpars &outpars)
-{
-    fNumPixels = outpars.inumpixels; // number of pixels in the camera
-
-    if (fNumPixels>iMAXNUMPIX || fNumPixels==0)
-        fNumPixels=iMAXNUMPIX;
+void MCT1ReadPreProc::ProcessRunHeader(const struct outputpars &outpars)
+{
+    if (outpars.inumpixels != iMAXNUMPIX)
+        *fLog << warn << "WARNING! File doesn't contain " << iMAXNUMPIX << " Pixels... maybe corrupt." << endl;
+
+    fNumEventsInRun = 0;
 
     //
@@ -166,6 +181,4 @@
     *fLog << "Source:    RA=" << outpars.dsourcera_hours << "h  DEC=";
     *fLog << outpars.dsourcedec_deg << "deg" << endl;
-
-    *fLog << "Pixels:    " << fNumPixels << endl;
 
     //int     inummuonpixels;   // number of pixels in the muon shield
@@ -262,6 +275,7 @@
 
     // FIXME? Is x-y echanged between Mars CT1 geometry and CT1 definition?
-    fSrcPos->SetXY(outpars.fypointcorr_deg/fGeom->GetConvMm2Deg(),
-                   outpars.fxpointcorr_deg/fGeom->GetConvMm2Deg());
+    fSrcPos->SetXY(-outpars.fypointcorr_deg/fGeom->GetConvMm2Deg(),
+                   -outpars.fxpointcorr_deg/fGeom->GetConvMm2Deg());
+    fSrcPos->SetReadyToSave();
 
     /*
@@ -342,4 +356,6 @@
 
     fIsMcFile = outpars.bmontecarlo==TRUE;
+
+    fPedest->SetReadyToSave();
 }
 
@@ -348,5 +364,5 @@
 // Read CT1 PreProc File Header:
 //
-Bool_t MCT1ReadPreProc::ReadHeader()
+Bool_t MCT1ReadPreProc::ReadRunHeader()
 {
     char cheadertitle[iHEADERTITLELENGTH];
@@ -381,10 +397,10 @@
     fIn->read((Byte_t*)&outpars, sizeof(struct outputpars));
 
-    ProcessHeader(outpars);
+    ProcessRunHeader(outpars);
 
     return kTRUE;
 }
 
-Bool_t MCT1ReadPreProc::ReadFooter()
+Bool_t MCT1ReadPreProc::ReadRunFooter()
 {
     char cheadertitle[iHEADERTITLELENGTH];
@@ -400,5 +416,8 @@
 
     if (!s.BeginsWith(m(0,p)))
-        return kFALSE;
+    {
+        fIn->seekg(-iHEADERTITLELENGTH, ios::cur);
+        return kFALSE;
+    }
 
     *fLog << inf << cheadertitle << flush;
@@ -423,4 +442,15 @@
      */
 
+    fNumFilterEvts += filterres.ifilter_passed_evts;
+    fNumRuns++;
+
+    *fLog << inf << "Read " << fNumEventsInRun << " events from run." << endl;
+
+    if (fNumEventsInRun!=(UInt_t)filterres.ifilter_passed_evts)
+    {
+        *fLog << warn << "WARNING! Number of events in run doesn't match number of read events." << endl;
+        *fLog << "         File might be corrupt." << endl;
+    }
+
     return kTRUE;
 }
@@ -468,10 +498,14 @@
     *fLog << inf << "-----------------------------------------------------------------------" << endl;
 
-    //*fLog << "File contains " << GetNumEvents() << " events." << endl;
-
-    Bool_t rc = ReadHeader();
+    *fLog << "File contains " << GetNumEvents() << " events." << endl;
+
+    // WORKAROUND for not working seekg(0) in GetNumEvents
+    fIn->close();
+    fIn->open(gSystem->ExpandPathName(name));
+
+    Bool_t rc = ReadRunHeader();
 
     if (!rc)
-        *fLog << warn << "Unable to read header... skipping file." << endl;
+        *fLog << warn << "Unable to read first run header... skipping file." << endl;
 
     return rc;
@@ -480,31 +514,48 @@
 Int_t MCT1ReadPreProc::GetNumEvents()
 {
-    Int_t n = 0;
+    *fLog << inf << "Scanning file for size" << flush;
+
+    const TString m(cEND_EVENTS_TEMPLATE);
+    const Int_t p = m.First('%');
+    const TString test = m(0, p);
+
+    Int_t nevts = 0;
+    Int_t nruns = 0;
 
     while (!fIn->eof())
     {
-        if (fIn->get()!=cEND_EVENTS_TEMPLATE[0])
-            continue;
-
-        char cheadertitle[iHEADERTITLELENGTH];
-        fIn->read(cheadertitle, iHEADERTITLELENGTH-1);
-
-        TString s = cheadertitle;
-        TString m = cEND_EVENTS_TEMPLATE;
-        Int_t p = m.First('%');
-
-        if (!s.BeginsWith(m(1,p-1)))
-            continue;
-
-        int num;
-
-        sscanf(cheadertitle, &cEND_EVENTS_TEMPLATE[1], &num);
-
-        n += num;
-    }
-
-    fIn->seekg(0);
-
-    return n;
+        fIn->seekg(iHEADERTITLELENGTH, ios::cur);
+        fIn->seekg(sizeof(struct outputpars), ios::cur);
+
+        while (1)
+        {
+            if (fIn->peek()==cEND_EVENTS_TEMPLATE[0])
+            {
+                char cheadertitle[iHEADERTITLELENGTH];
+                fIn->read(cheadertitle, iHEADERTITLELENGTH);
+
+                const TString s = cheadertitle;
+                if (s.BeginsWith(test))
+                {
+                    fIn->seekg(sizeof(struct filterresults), ios::cur);
+                    nruns++;
+                    break;
+                }
+
+                fIn->seekg(-iHEADERTITLELENGTH, ios::cur);
+            }
+
+            fIn->seekg(sizeof(struct eventrecord), ios::cur);
+            if (fIn->eof())
+                break;
+            nevts++;
+        }
+        *fLog << "." << flush;
+    }
+
+    *fLog << "done." << endl;
+    *fLog << "Found " << nevts << " events in " << nruns << " runs." << endl;
+
+    return nevts;
 }
 
@@ -542,5 +593,5 @@
     //  look for the source position in the camera
     //
-    fSrcPos = (MSrcPosCam*)pList->FindCreateObj("MSrcPosCam");
+    fSrcPos = (MSrcPosCam*)pList->FindCreateObj("MSrcPosCam", "Source");
     if (!fSrcPos)
         return kFALSE;
@@ -566,4 +617,7 @@
     if (!fMcTrig)
         return kFALSE;
+
+    fNumFilterEvts = 0;
+    fNumRuns       = 0;
 
     return kTRUE;
@@ -582,6 +636,4 @@
      int   isecfrac_200ns;     // fractional part of isecs_since_midday
      short snot_ok_flags;      // the bits in these two bytes are flags for additional information on the event: Everything OK =: all Bits = 0
-     int   ipreproc_alt_arcs;  // "should be" alt according to preproc (arcseconds)
-     int   ipreproc_az_arcs;   // "should be" az according to preproc (arcseconds)
 
      // for ALT-AZ mount telescopes: rotation angle of the field of
@@ -602,11 +654,4 @@
      float fhourangle;
      */
-    /*
-     *fLog << event.isecs_since_midday << "s ";
-     *fLog << event.ipreproc_alt_arcs << "s ";
-     *fLog << event.ipreproc_az_arcs << "s ";
-     *fLog << event.ifieldrot_arcs << "s ";
-     *fLog << event.fhourangle << endl;
-     */
 
     //
@@ -614,18 +659,26 @@
     // too the list with it's id, number of photons and error
     //
-    fNphot->InitSize(fNumPixels);
+    fNphot->InitSize(iMAXNUMPIX);
 
     // number of photoelectrons measured in each pixel only the
     // actual number of pixels (outputpars.inumpixels) is written out
     // short spixsig_10thphot[iMAXNUMPIX];
-    for (Int_t i=0; i<fNumPixels; i++)
-    {
-        if (event.spixsig_10thphot[i]>0)
-            fNphot->AddPixel(i, 0.1*event.spixsig_10thphot[i],
-                             (*fPedest)[i].GetMeanRms());
-    }
+    for (Int_t i=0; i<iMAXNUMPIX; i++)
+    {
+        if (event.spixsig_10thphot[i]==0)
+            continue;
+
+        fNphot->AddPixel(i, 0.1*event.spixsig_10thphot[i],
+                         (*fPedest)[i].GetMeanRms());
+    }
+    fNphot->SetReadyToSave();
+
+    // int ipreproc_alt_arcs; // "should be" alt according to preproc (arcseconds)
+    // int ipreproc_az_arcs;  // "should be" az according to preproc (arcseconds)
 
     fMcEvt->SetTheta(TMath::Pi()*(0.5-1./180*event.ialt_arcs/3600));   // altitude (arcseconds)
     fMcEvt->SetPhi(TMath::Pi()/180*event.iaz_arcs/3600);               // azimuth (arcseconds)
+
+    fMcEvt->SetReadyToSave();
 
     if (!fIsMcFile)
@@ -636,5 +689,6 @@
     fMcEvt->SetImpact(event.imcimpact_m*100);    // simulated impact
 
-    fMcTrig->SetFirstLevel(event.imctriggerflag); // MC data from Dorota get a triggerflag: 1 means triggered, 0 not. */
+    fMcTrig->SetFirstLevel(event.imctriggerflag);    // MC data from Dorota get a triggerflag: 1 means triggered, 0 not. */
+    fMcTrig->SetReadyToSave();
 
     //float fmcsize_phel;   // Simulated SIZE
@@ -655,52 +709,47 @@
 
     //
-    // If the first character isn't the first of the footer it must be
-    // an event
-    //
-    if (fIn->peek()!=cEND_EVENTS_TEMPLATE[0])
-        return kTRUE;
-
-    //
-    // Remember the current position if the first character faked a
-    // footer
-    //
-    const Int_t pos = fIn->tellg();
-
-    //
-    // Try reading the footer. If it isn't succefull jump back...
-    // must be an event
-    //
-    if (!ReadFooter())
-    {
-        fIn->seekg(pos, ios::beg);
-        return kTRUE;
-    }
-
-    *fLog << inf << "Footer found." << endl;
-
-    //
-    // No after reading the footer check if we reached the end of the file
-    //
-    if (fIn->eof())
-    {
-        *fLog << "End of file." << endl;
-        return kFALSE;
-    }
-
-    //
-    // If the eof isn't reached a new header must follow. Check for it.
-    //
-    if (fIn->peek()!=cTITLE_TEMPLATE[0])
-    {
-        *fLog << inf << "Error finding new run header in file (possible EOF)... skipping rest of file." << endl;
-        return kFALSE;
-    }
-
-    *fLog << "-----------------------------------------------------------------------" << endl;
-    if (ReadHeader())
-        return kTRUE;
-
-    *fLog << inf << "Error reading run header in file... skipping rest of file." << endl;
-    return kFALSE;
+    // Because we can have 0-event runs in the file we loop as often
+    // as we don't find a new footer-header combination.
+    //
+    while (1)
+    {
+        //
+        // If the first character isn't the first of the footer it must be
+        // an event
+        //
+        if (fIn->peek()!=cEND_EVENTS_TEMPLATE[0])
+            return kTRUE;
+
+        //
+        // Try reading the footer. If it isn't succefull...
+        // must be an event
+        //
+        if (!ReadRunFooter())
+            return kTRUE;
+
+        *fLog << inf << "Footer found." << endl;
+
+        //
+        // No after reading the footer check if we reached the end of the file
+        //
+        if (fIn->eof())
+        {
+            *fLog << "End of file." << endl;
+            return kFALSE;
+        }
+
+        //
+        // If the eof isn't reached a new header must follow. Check for it.
+        //
+        if (fIn->peek()!=cTITLE_TEMPLATE[0])
+        {
+            *fLog << inf << "Error finding new run header in file (possible EOF)... skipping rest of file." << endl;
+            return kFALSE;
+        }
+
+        *fLog << "-----------------------------------------------------------------------" << endl;
+        if (!ReadRunHeader())
+            return kTRUE;
+    }
 }
 
@@ -726,18 +775,28 @@
     struct eventrecord event;
 
-    const int size1 = sizeof(event)-sizeof(event.spixsig_10thphot);
-    const int size2 = sizeof(event.spixsig_10thphot[0])*fNumPixels;
-
-    // read the eventrecord with the recorded number of stored pixels
-    fIn->read((Byte_t*)&event, size1+size2);
-
-    if (fIn->eof())
-        return kFALSE;
+    // read the eventrecord from the file
+    fIn->read((Byte_t*)&event, sizeof(struct eventrecord));
 
     ProcessEvent(event);
 
-    fNumEvent++;
+    fNumEvents++;
+    fNumEventsInRun++;
 
     return kTRUE;
 }
 
+Bool_t MCT1ReadPreProc::PostProcess()
+{
+    *fLog << all;
+    *fLog << "Number events passed the filter: " << fNumFilterEvts << endl;
+    *fLog << "Number of Events read from file: " << fNumEvents << endl;
+    *fLog << "Number of Runs read from file:   " << fNumRuns << endl;
+
+    if (fNumEvents!=fNumFilterEvts)
+    {
+        *fLog << warn << "WARNING! Number of events in file doesn't match number of read events..." << endl;
+        *fLog << "         File might be corrupt." << endl;
+    }
+
+    return kTRUE;
+}
Index: trunk/MagicSoft/Mars/mfileio/MCT1ReadPreProc.h
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MCT1ReadPreProc.h	(revision 1599)
+++ trunk/MagicSoft/Mars/mfileio/MCT1ReadPreProc.h	(revision 1600)
@@ -2,6 +2,6 @@
 #define MARS_MCT1ReadPreProc
 
-#ifndef MARS_MTask
-#include "MTask.h"
+#ifndef MARS_MRead
+#include "MRead.h"
 #endif
 
@@ -18,22 +18,25 @@
 struct eventrecord;
 
-class MCT1ReadPreProc : public MTask
+class MCT1ReadPreProc : public MRead
 {
 private:
-    ifstream     *fIn;          // the inputfile
-    MGeomCam     *fGeom;        // camera geometry
-    MCerPhotEvt  *fNphot;       // the data container for all data.
-    MPedestalCam *fPedest;      // ct1 pedestals
-    MMcEvt       *fMcEvt;       // monte carlo data container for MC files
-    MMcTrig      *fMcTrig;      // mc data container for trigger information
-    MSrcPosCam   *fSrcPos;      // source position in the camera
-    TList        *fFileNames;   // Array which stores the \0-terminated filenames
-    MBlindPixels *fBlinds;      // Array holding blind pixels
+    ifstream *fIn;          // the inputfile
+    TList    *fFileNames;   // Array which stores the \0-terminated filenames
 
-    Int_t fNumPixels;
-    Bool_t fIsMcFile;
+    MGeomCam     *fGeom;    // camera geometry
+    MCerPhotEvt  *fNphot;   // the data container for all data.
+    MPedestalCam *fPedest;  // ct1 pedestals
+    MMcEvt       *fMcEvt;   // monte carlo data container for MC files
+    MMcTrig      *fMcTrig;  // mc data container for trigger information
+    MSrcPosCam   *fSrcPos;  // source position in the camera
+    MBlindPixels *fBlinds;  // Array holding blind pixels
 
-    UInt_t fNumEvents;
-    UInt_t fNumEvent;
+    Bool_t fIsMcFile;       // Flag whether current run is a MC run
+
+    UInt_t fNumEvents;      // number of events counted in all runs in all files
+    UInt_t fNumEventsInRun; // number of events in the counted in th ecurrent run
+    UInt_t fNumRuns;        // number of processed runs of all files
+    UInt_t fEntries;        // entries of all files succesfully added
+    UInt_t fNumFilterEvts;  // number of events mentioned in the runs footers
 
     Bool_t OpenNextFile();
@@ -42,14 +45,14 @@
 
     void   ReadPedestals();
-    Bool_t ReadHeader();
-    Bool_t ReadFooter();
+    Bool_t ReadRunHeader();
+    Bool_t ReadRunFooter();
     Bool_t CheckFilePosition();
-    void   ProcessHeader(const struct outputpars &outpars);
+    void   ProcessRunHeader(const struct outputpars &outpars);
     void   ProcessEvent(const struct eventrecord &event);
 
 public:
     MCT1ReadPreProc(const char *filename=NULL,
-                  const char *name=NULL,
-                  const char *title=NULL);
+                    const char *name=NULL,
+                    const char *title=NULL);
 
     ~MCT1ReadPreProc();
@@ -59,4 +62,7 @@
     Bool_t PreProcess(MParList *pList);
     Bool_t Process();
+    Bool_t PostProcess();
+
+    UInt_t GetEntries() { return fEntries; }
 
     ClassDef(MCT1ReadPreProc, 0)	// Reads the CT1 data file
Index: trunk/MagicSoft/Mars/mfileio/MRead.cc
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MRead.cc	(revision 1600)
+++ trunk/MagicSoft/Mars/mfileio/MRead.cc	(revision 1600)
@@ -0,0 +1,35 @@
+/* ======================================================================== *\
+!
+! *
+! * This file is part of MARS, the MAGIC Analysis and Reconstruction
+! * Software. It is distributed to you in the hope that it can be a useful
+! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
+! * It is distributed WITHOUT ANY WARRANTY.
+! *
+! * Permission to use, copy, modify and distribute this software and its
+! * documentation for any purpose is hereby granted without fee,
+! * provided that the above copyright notice appear in all copies and
+! * that both that copyright notice and this permission notice appear
+! * in supporting documentation. It is provided "as is" without express
+! * or implied warranty.
+! *
+!
+!
+!   Author(s): Thomas Bretz  12/2000 <mailto:tbretz@astro.uni-wuerzburg.de>
+!
+!   Copyright: MAGIC Software Development, 2000-2002
+!
+!
+\* ======================================================================== */
+
+/////////////////////////////////////////////////////////////////////////////
+//                                                                         //
+// MRead                                                                   //
+//                                                                         //
+// Base class for all reading tasks                                        //
+//                                                                         //
+/////////////////////////////////////////////////////////////////////////////
+#include "MRead.h"
+
+ClassImp(MRead);
+
Index: trunk/MagicSoft/Mars/mfileio/MRead.h
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MRead.h	(revision 1600)
+++ trunk/MagicSoft/Mars/mfileio/MRead.h	(revision 1600)
@@ -0,0 +1,17 @@
+#ifndef MARS_MRead
+#define MARS_MRead
+
+#ifndef MARS_MTask
+#include "MTask.h"
+#endif
+
+class MRead : public MTask
+{
+public:
+
+    virtual UInt_t GetEntries() = 0;
+
+    ClassDef(MRead, 0)	// Base class for a reading task
+};
+
+#endif
Index: trunk/MagicSoft/Mars/mfileio/MReadMarsFile.cc
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MReadMarsFile.cc	(revision 1599)
+++ trunk/MagicSoft/Mars/mfileio/MReadMarsFile.cc	(revision 1600)
@@ -54,5 +54,5 @@
 MReadMarsFile::MReadMarsFile() : fRun(NULL)
 {
-    fName  = "MReadMarsFile";
+    fName  = "MRead";
     fTitle = "Task to loop over all events in a tree of a Mars file.";
 }
@@ -67,5 +67,5 @@
     : MReadTree(tname, fname)
 {
-    fName  = name  ? name  : "MReadMarsFile";
+    fName  = name  ? name  : "MRead";
     fTitle = title ? title : "Task to loop over all events in a tree of a Mars file.";
 
Index: trunk/MagicSoft/Mars/mfileio/MReadTree.cc
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MReadTree.cc	(revision 1599)
+++ trunk/MagicSoft/Mars/mfileio/MReadTree.cc	(revision 1600)
@@ -55,5 +55,5 @@
 #include <TFile.h>           // TFile::GetName
 #include <TSystem.h>         // gSystem->ExpandPath
-#include <TGProgressBar.h>
+//#include <TGProgressBar.h>
 #include <TChainElement.h>
 #include <TOrdCollection.h>
@@ -75,7 +75,7 @@
 //
 MReadTree::MReadTree()
-    : fNumEntry(0), fNumEntries(0), fBranchChoosing(kFALSE), fAutoEnable(kTRUE), fProgress(NULL)
-{
-    fName  = "MReadTree";
+    : fNumEntry(0), fNumEntries(0), fBranchChoosing(kFALSE), fAutoEnable(kTRUE)
+{
+    fName  = "MRead";
     fTitle = "Task to loop over all events in one single tree";
 
@@ -99,7 +99,7 @@
 MReadTree::MReadTree(const char *tname, const char *fname,
                      const char *name, const char *title)
-    : fNumEntry(0), fNumEntries(0), fBranchChoosing(kFALSE), fAutoEnable(kTRUE), fProgress(NULL)
-{
-    fName  = name  ? name  : "MReadTree";
+    : fNumEntry(0), fNumEntries(0), fBranchChoosing(kFALSE), fAutoEnable(kTRUE)
+{
+    fName  = name  ? name  : "MRead";
     fTitle = title ? title : "Task to loop over all events in one single tree";
 
@@ -415,5 +415,7 @@
     if (TestBit(kChainWasChanged))
     {
+        *fLog << inf << "Scanning chain... " << flush;
         fNumEntries = (UInt_t)fChain->GetEntries();
+        *fLog << fNumEntries << " events found." << endl;
         ResetBit(kChainWasChanged);
     }
@@ -566,10 +568,4 @@
 
     //
-    // If a progress bar is given set its range.
-    //
-    if (fProgress)
-        fProgress->SetRange(0, fNumEntries);
-
-    //
     // Now we can start notifying. Reset tree makes sure, that TChain thinks
     // that the correct file is not yet initialized and reinitilizes it
Index: trunk/MagicSoft/Mars/mfileio/MReadTree.h
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MReadTree.h	(revision 1599)
+++ trunk/MagicSoft/Mars/mfileio/MReadTree.h	(revision 1600)
@@ -2,13 +2,12 @@
 #define MARS_MReadTree
 
-#ifndef MARS_MTask
-#include "MTask.h"
+#ifndef MARS_MRead
+#include "MRead.h"
 #endif
 
 class MChain;
 class TBranch;
-class TGProgressBar;
 
-class MReadTree : public MTask
+class MReadTree : public MRead
 {
 private:
@@ -27,6 +26,4 @@
 
 private:
-    TGProgressBar  *fProgress; //! Possible display of status
-
     void SetBranchStatus(const TList *list, Bool_t status);
     void SetBranchStatus(TObject *branch, Bool_t status);
@@ -50,6 +47,4 @@
     void   EnableBranch(const char *name);
     void   VetoBranch(const char *name);
-
-    void   SetProgressBar(TGProgressBar *bar) { fProgress = bar; }
 
     Bool_t GetEvent();
Index: trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc	(revision 1599)
+++ trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc	(revision 1600)
@@ -130,4 +130,6 @@
     // - Branches are automatically deleted by the tree destructor
     //
+
+    *fLog << inf << "Output File closed and deleted." << endl;
 }
 
Index: trunk/MagicSoft/Mars/mfileio/Makefile
===================================================================
--- trunk/MagicSoft/Mars/mfileio/Makefile	(revision 1599)
+++ trunk/MagicSoft/Mars/mfileio/Makefile	(revision 1600)
@@ -32,4 +32,5 @@
 
 SRCFILES = MChain.cc \
+	   MRead.cc \
            MReadTree.cc \
            MReadMarsFile.cc \
Index: trunk/MagicSoft/Mars/mfilter/MFEventSelector.cc
===================================================================
--- trunk/MagicSoft/Mars/mfilter/MFEventSelector.cc	(revision 1599)
+++ trunk/MagicSoft/Mars/mfilter/MFEventSelector.cc	(revision 1600)
@@ -40,7 +40,7 @@
 // To get around 2000 events from all events use (Remark: This will only
 // work if the parlist has an entry called MTaskList which has a task
-// MRTeadTree inheriting from MReadTree):
+// MRead inheriting from MRead):
 //   MFEventSelector sel;
-//   sel.SetNumSelectEvents(2000);
+//   sel.SetNumSelectEvts(2000);
 //   MFillH filler(...);
 //   filler.SetFilter(&sel);
@@ -48,5 +48,5 @@
 //   tlist.AddToList(&filler);
 //
-// If you don't have MReadTree available you have to set the number of
+// If you don't have MRead available you have to set the number of
 // total events manually, using sel.SetNumTotalEvts(10732);
 //
@@ -67,5 +67,5 @@
 #include "MParList.h"
 #include "MTaskList.h"
-#include "MReadTree.h"
+#include "MRead.h"
 
 #include "MLog.h"
@@ -127,13 +127,13 @@
             return kFALSE;
         }
-        MReadTree *read = (MReadTree*)tlist->FindObject("MReadTree");
-        if (!read)
-            read = (MReadTree*)tlist->FindObject("MReadMarsFile");
+        MRead *read = (MRead*)tlist->FindObject("MRead");
         if (!read)
         {
-            *fLog << err << dbginf << "Sorry can't determin total number of events from 'MReadTree/MReadMarsFile'." << endl;
+            *fLog << err << dbginf << "Sorry can't determin total number of events from 'MRead'." << endl;
             return kFALSE;
         }
         fNumTotalEvts = read->GetEntries();
+
+        SetBit(kNumTotalFromFile);
     }
 
@@ -166,4 +166,6 @@
 Bool_t MFEventSelector::PostProcess()
 {
+    if (TestBit(kNumTotalFromFile))
+        fNumTotalEvts = -1;
     return kTRUE;
 }
Index: trunk/MagicSoft/Mars/mfilter/MFEventSelector.h
===================================================================
--- trunk/MagicSoft/Mars/mfilter/MFEventSelector.h	(revision 1599)
+++ trunk/MagicSoft/Mars/mfilter/MFEventSelector.h	(revision 1600)
@@ -27,4 +27,5 @@
     void StreamPrimitive(ofstream &out) const;
 
+    enum { kNumTotalFromFile = BIT(14) };
     /*
      enum { kUseFixedRatio=BIT(14) }
@@ -38,5 +39,5 @@
     Bool_t IsExpressionTrue() const { return fResult; }
 
-    void SetNumTotalEvts(Int_t n) { fNumTotalEvts = n; }
+    void SetNumTotalEvts(Int_t n) { fNumTotalEvts = n; ResetBit(kNumTotalFromFile); }
     void SetNumSelectEvts(Int_t n) { fNumSelectEvts = n; }
     void SetSelectionRatio(Float_t f) { fSelRatio = f; }
Index: trunk/MagicSoft/Mars/mgeom/GeomLinkDef.h
===================================================================
--- trunk/MagicSoft/Mars/mgeom/GeomLinkDef.h	(revision 1599)
+++ trunk/MagicSoft/Mars/mgeom/GeomLinkDef.h	(revision 1600)
@@ -5,4 +5,6 @@
 #pragma link off all functions;
 
+#pragma link C++ class MGeomPMT+;
+#pragma link C++ class MGeomMirror+;
 #pragma link C++ class MGeomPix+;
 #pragma link C++ class MGeomCam+;
Index: trunk/MagicSoft/Mars/mgeom/MGeomCamCT1.cc
===================================================================
--- trunk/MagicSoft/Mars/mgeom/MGeomCamCT1.cc	(revision 1599)
+++ trunk/MagicSoft/Mars/mgeom/MGeomCamCT1.cc	(revision 1600)
@@ -237,30 +237,30 @@
         //
         for (int i=0; i<ring; i++)
-            (*this)[pixnum++].Set((-ring+i*0.5)*diameter,
+            (*this)[pixnum++].Set((ring-i*0.5)*diameter,
                                   i*kS32*diameter,
                                   diameter);
 
         for (int i=0; i<ring; i++)
-            (*this)[pixnum++].Set((-ring*0.5+i)*diameter,
+            (*this)[pixnum++].Set((ring*0.5-i)*diameter,
                                   ring*kS32 * diameter,
                                   diameter);
 
         for (int i=0; i<ring; i++)
+            (*this)[pixnum++].Set(-(ring+i)*0.5*diameter,
+                                  (ring-i)*kS32*diameter,
+                                  diameter);
+
+        for (int i=0; i<ring; i++)
+            (*this)[pixnum++].Set((0.5*i-ring)*diameter,
+                                  -i*kS32*diameter,
+                                  diameter);
+
+        for (int i=0; i<ring; i++)
+            (*this)[pixnum++].Set((i-ring*0.5)*diameter,
+                                  -ring*kS32 * diameter,
+                                  diameter);
+
+        for (int i=0; i<ring; i++)
             (*this)[pixnum++].Set((ring+i)*0.5*diameter,
-                                  (ring-i)*kS32*diameter,
-                                  diameter);
-
-        for (int i=0; i<ring; i++)
-            (*this)[pixnum++].Set((ring-0.5*i)*diameter,
-                                  -i*kS32*diameter,
-                                  diameter);
-
-        for (int i=0; i<ring; i++)
-            (*this)[pixnum++].Set((ring*0.5-i)*diameter,
-                                  -ring*kS32 * diameter,
-                                  diameter);
-
-        for (int i=0; i<ring; i++)
-            (*this)[pixnum++].Set((-ring-i)*0.5*diameter,
                                   (-ring+i)*kS32*diameter,
                                   diameter);
Index: trunk/MagicSoft/Mars/mgeom/MGeomMirror.cc
===================================================================
--- trunk/MagicSoft/Mars/mgeom/MGeomMirror.cc	(revision 1599)
+++ trunk/MagicSoft/Mars/mgeom/MGeomMirror.cc	(revision 1600)
@@ -69,4 +69,8 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// DESCRIPTION MISSING
+//
 void MGeomMirror::SetMirrorContent(Int_t mir, Float_t focal, Float_t curv_x,
 				   Float_t curv_y, Float_t lin_x, Float_t lin_y,
Index: trunk/MagicSoft/Mars/mgeom/MGeomMirror.h
===================================================================
--- trunk/MagicSoft/Mars/mgeom/MGeomMirror.h	(revision 1599)
+++ trunk/MagicSoft/Mars/mgeom/MGeomMirror.h	(revision 1600)
@@ -9,6 +9,5 @@
 {
 private:
-
-    Int_t   fMirrorId;       // the Mirror Id
+    Int_t   fMirrorId;    // the Mirror Id
 
     Float_t fFocalDist;   //  focal distance of that mirror [cm]
@@ -33,14 +32,13 @@
                           // of the spot of a single mirror on the camera plane
 public:
-
     MGeomMirror(Int_t mir=-1, const char *name=NULL, const char *title=NULL);
 
-    Int_t   GetMirrorId() const         { return fMirrorId;   }
+    Int_t GetMirrorId() const         { return fMirrorId;   }
 
-    void    SetMirrorContent(Int_t mir, Float_t focal, Float_t curv_x,
-			  Float_t curv_y, Float_t lin_x, Float_t lin_y,
-			  Float_t lin_z, Float_t theta, Float_t phi,
-			  Float_t x_n, Float_t y_n, Float_t z_n,
-			  Float_t dev_x, Float_t dev_y);
+    void  SetMirrorContent(Int_t mir, Float_t focal, Float_t curv_x,
+                           Float_t curv_y, Float_t lin_x, Float_t lin_y,
+                           Float_t lin_z, Float_t theta, Float_t phi,
+                           Float_t x_n, Float_t y_n, Float_t z_n,
+                           Float_t dev_x, Float_t dev_y);
 
     ClassDef(MGeomMirror, 1)  // class containing information about the Cerenkov Photons in a pixel
Index: trunk/MagicSoft/Mars/mgeom/MGeomPMT.cc
===================================================================
--- trunk/MagicSoft/Mars/mgeom/MGeomPMT.cc	(revision 1599)
+++ trunk/MagicSoft/Mars/mgeom/MGeomPMT.cc	(revision 1600)
@@ -30,9 +30,8 @@
 //
 ///////////////////////////////////////////////////////////////////////
+#include "MGeomPMT.h"
 
 #include "MLog.h"
 #include "MLogManip.h"
-
-#include "MGeomPMT.h"
 
 ClassImp(MGeomPMT);
@@ -43,28 +42,28 @@
 //
 MGeomPMT::MGeomPMT(Int_t pmt, const char *name=NULL, const char *title=NULL)
+    : fPMTId(pmt), fWavelength(0), fQE(0)
 {
     fName  = name  ? name  : "MGeomPMT";
     fTitle = title ? title : "Storage container for  a PMT characteristics";
+}
+
+// --------------------------------------------------------------------------
+//
+// DESCRIPTION MISSING
+//
+void MGeomPMT::SetPMTContent(Int_t pmt, const TArrayF &wav, const TArrayF &qe)
+{
+    if (fWavelength.GetSize()!=wav.GetSize() ||
+        fQE.GetSize()!=qe.GetSize())
+    {
+        *fLog << err << dbginf << " fWavelength or fQE do not have ";
+        *fLog << "size of setting arrays" << endl;
+        return;
+    }
 
     fPMTId = pmt;
-    fWavelength->Set(0);
-    fQE->Set(0);
 
-}
-
-void MGeomPMT::SetPMTContent(Int_t pmt, TArrayF *wav, TArrayF *qe)
-{
-    fPMTId = pmt;
-
-  if(fWavelength->GetSize()==wav->GetSize() &&
-     fQE->GetSize()==qe->GetSize()){
-    wav->Copy(*fWavelength);
-    qe->Copy(*fQE);
-  }
-  else
-     *fLog<<err << "MGeomPMT::SetPMTContent fWavelength "
-	  <<"and fQE do not have "
-	  <<"size of setting arrays"<<endl;
-
+    fWavelength = wav;
+    fQE = qe;
 }
 
Index: trunk/MagicSoft/Mars/mgeom/MGeomPMT.h
===================================================================
--- trunk/MagicSoft/Mars/mgeom/MGeomPMT.h	(revision 1599)
+++ trunk/MagicSoft/Mars/mgeom/MGeomPMT.h	(revision 1600)
@@ -12,9 +12,8 @@
 {
 private:
+    Int_t   fPMTId;        // the PMT Id
 
-    Int_t   fPMTId;       // the PMT Id
-
-    TArrayF *fWavelength;  // List of wavelength
-    TArrayF *fQE;          // QE values
+    TArrayF fWavelength;  // List of wavelength
+    TArrayF fQE;          // QE values
 
 public:
@@ -24,7 +23,7 @@
     Int_t   GetPMTId() const         { return fPMTId;   }
 
-    void    SetArrays(Int_t dim) {fWavelength->Set(dim);fQE->Set(dim);}
+    void    SetArraySize(Int_t dim) { fWavelength.Set(dim); fQE.Set(dim); }
 
-    void    SetPMTContent(Int_t pmt, TArrayF *wav, TArrayF *qe);
+    void    SetPMTContent(Int_t pmt, const TArrayF &wav, const TArrayF &qe);
 
     ClassDef(MGeomPMT, 1)  // class containing information about PMTs
Index: trunk/MagicSoft/Mars/mgui/MCamDisplay.cc
===================================================================
--- trunk/MagicSoft/Mars/mgui/MCamDisplay.cc	(revision 1599)
+++ trunk/MagicSoft/Mars/mgui/MCamDisplay.cc	(revision 1600)
@@ -154,10 +154,15 @@
     // delete fGeomCam;
 
-    // Maybe harmfull! Don't exchange the order!
-    if (fIsAllocated && fDrawingPad->GetListOfPrimitives()->FindObject(this)==this)
-    {
-        fDrawingPad->RecursiveRemove(this);
-        delete fDrawingPad;
-    }
+    // Not allocated by MCamDisplay or already deleted by the user
+    if (!fIsAllocated || !gROOT->GetListOfCanvases()->FindObject(fDrawingPad))
+        return;
+
+    // If it is not already removed make sure that nothing of this object
+    // maybe be deleted automatically by the canvas destructor
+    if (!fDrawingPad->GetListOfPrimitives()->FindObject(this))
+        return;
+
+    fDrawingPad->RecursiveRemove(this);
+    delete fDrawingPad;
 }
 
Index: trunk/MagicSoft/Mars/mhist/MHFadcCam.cc
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHFadcCam.cc	(revision 1599)
+++ trunk/MagicSoft/Mars/mhist/MHFadcCam.cc	(revision 1600)
@@ -112,2 +112,11 @@
     return kTRUE;
 }
+
+void MHFadcCam::ResetHistograms()
+{
+    for (Int_t i=0; i<577; i++)
+    {
+        GetHistHi(i)->Reset();
+        GetHistLo(i)->Reset();
+    }
+}
Index: trunk/MagicSoft/Mars/mhist/MHFadcCam.h
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHFadcCam.h	(revision 1599)
+++ trunk/MagicSoft/Mars/mhist/MHFadcCam.h	(revision 1600)
@@ -36,4 +36,6 @@
     Bool_t Fill(const MParContainer *par);
 
+    void ResetHistograms();
+
     //
     // FIXME! This should be replaced by a Draw(Option_t)-function
Index: trunk/MagicSoft/Mars/mhist/MHMatrix.cc
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHMatrix.cc	(revision 1599)
+++ trunk/MagicSoft/Mars/mhist/MHMatrix.cc	(revision 1600)
@@ -614,2 +614,62 @@
     return fData ? fData->GetDataMember() : TString("");
 }
+
+// --------------------------------------------------------------------------
+//
+// Return a comma seperated list of all data members used in the matrix.
+// This is mainly used in MTask::AddToBranchList
+//
+void MHMatrix::ReduceNumberOfRows(UInt_t numrows, const TString opt)
+{
+    UInt_t rows = fM.GetNrows();
+
+    if (rows==numrows)
+    {
+        *fLog << warn << "Matrix has already the correct number of rows..." << endl;
+        return;
+    }
+
+    Float_t ratio = (Float_t)numrows/fM.GetNrows();
+
+    if (ratio>=1)
+    {
+        *fLog << warn << "Matrix cannot be enlarged..." << endl;
+        return;
+    }
+
+    Double_t sum = 0;
+
+    UInt_t oldrow = 0;
+    UInt_t newrow = 0;
+
+    while (oldrow<rows)
+    {
+        sum += ratio;
+
+        if (newrow<=(unsigned int)sum)
+        {
+            TVector vold(fM.GetNcols());
+            vold = TMatrixRow(fM, oldrow);
+
+            TMatrixRow rownew(fM, newrow);
+            rownew = vold;
+            newrow++;
+        }
+
+        oldrow++;
+    }
+
+/*
+
+    TMatrix m(n, fM.GetNcols());
+    for (int i=0; i<n; i++)
+    {
+        TVector vold(n);
+        vold = TMatrixRow(fM, idx[i]);
+
+        TMatrixRow rownew(m, i);
+        rownew = vold;
+    }
+*/
+
+}
Index: trunk/MagicSoft/Mars/mhist/MHMatrix.h
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHMatrix.h	(revision 1599)
+++ trunk/MagicSoft/Mars/mhist/MHMatrix.h	(revision 1600)
@@ -29,5 +29,5 @@
     TMatrix fM;         // Matrix to be filled
 
-    TMatrix fM2;        //!
+    TMatrix fM2;        //! Covariance Matrix
 
     MDataArray *fData;  // List of data members (columns)
@@ -89,4 +89,6 @@
     TString GetDataMember() const;
 
+    void ReduceNumberOfRows(UInt_t numrows, const TString opt);
+
     ClassDef(MHMatrix, 1) // Multidimensional Matrix to store events
 };
