Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 9218)
+++ trunk/MagicSoft/Mars/Changelog	(revision 9219)
@@ -18,4 +18,48 @@
 
                                                  -*-*- END OF LINE -*-*-
+
+ 2009/01/14 Thomas Bretz
+
+   * readraw.cc:
+     - check first whether the file exist before adding the extension
+
+   * mbase/MMath.[h,cc]:
+     - added function to Re-sort an array
+
+   * mfileio/MReadMarsFile.cc, mfileio/MWriteRootFile.cc:
+     - Improved output
+
+   * mfileio/MWriteRootFile.cc:
+     - fixed a problem which prevented to open more than one /dev/null
+       devices independantly
+
+   * mgeom/MGeomCam.cc:
+     - cosmetics
+
+   * mgeom/MGeomCamDwarf.h:
+     - fixed a typo in an ifdef
+
+   * mgeom/MGeomPix.[h,cc]:
+     - added funtion to return positon as TVector2
+     - slightly improved the algorithm to check IsInside
+     - fixed the conditional signs for the orientation of pixels
+       in GetDirection
+
+   * mmain/MEventDisplay.cc:
+     - Give different names to the MPedestalSubtract tasks
+     - fixed buttons
+     - call AddGeometryTags after ReInit to allow reading new geometries in
+       ReInit
+
+   * mmc/MMcEvtBasic.h:
+     - added a new primary type kNightSky
+
+   * mraw/MRawEvtPixelIter.h:
+     - replaced type of the number of bytes by UInt_t
+
+   * mraw/MRawRunHeader.h:
+     - allow to validate the magic-number from outside (for MCs)
+
+
 
  2009/01/12 Daniel Hoehne-Moench
Index: trunk/MagicSoft/Mars/mbase/MMath.cc
===================================================================
--- trunk/MagicSoft/Mars/mbase/MMath.cc	(revision 9218)
+++ trunk/MagicSoft/Mars/mbase/MMath.cc	(revision 9219)
@@ -1,4 +1,4 @@
 /* ======================================================================== *\
-! $Name: not supported by cvs2svn $:$Id: MMath.cc,v 1.42 2008-12-21 18:09:49 tbretz Exp $
+! $Name: not supported by cvs2svn $:$Id: MMath.cc,v 1.43 2009-01-14 12:31:36 tbretz Exp $
 ! --------------------------------------------------------------------------
 !
@@ -324,4 +324,32 @@
 Double_t MMath::MedianDev(Long64_t n, const Long64_t *a) { Double_t med; return MedianDevImp(n, a, med); }
 
+// ------------------------------------------------------------------------
+//
+// Re-sort an array. Intsead of returning an index (like TMath::Sort)
+// the array contents are sorted.
+//
+template <class Size, class Element> void MMath::ReSortImp(Size n, Element *a, Bool_t down)
+{
+    Element *cpy = new Element[n];
+    Int_t   *pos = new Int_t[n];
+
+    memcpy(cpy, a, n*sizeof(Element));
+
+    TMath::Sort(n, a, pos, down);
+
+    Int_t *idx = pos;
+
+    for (Element *ptr=a; ptr<a+n; ptr++)
+        *ptr = cpy[*idx++];
+
+    delete cpy;
+    delete pos;
+}
+
+void MMath::ReSort(Long64_t n, Short_t  *a, Bool_t down) { ReSortImp(n, a, down); }
+void MMath::ReSort(Long64_t n, Int_t    *a, Bool_t down) { ReSortImp(n, a, down); }
+void MMath::ReSort(Long64_t n, Float_t  *a, Bool_t down) { ReSortImp(n, a, down); }
+void MMath::ReSort(Long64_t n, Double_t *a, Bool_t down) { ReSortImp(n, a, down); }
+
 // --------------------------------------------------------------------------
 //
Index: trunk/MagicSoft/Mars/mbase/MMath.h
===================================================================
--- trunk/MagicSoft/Mars/mbase/MMath.h	(revision 9218)
+++ trunk/MagicSoft/Mars/mbase/MMath.h	(revision 9219)
@@ -17,4 +17,10 @@
 {
     Double_t GaussProb(Double_t x, Double_t sigma=1, Double_t mean=0);
+
+    template <class Size, class Element> void ReSortImp(Size n, Element *a, Bool_t down=kFALSE);
+    void ReSort(Long64_t n, Short_t  *a, Bool_t down=kFALSE);
+    void ReSort(Long64_t n, Int_t    *a, Bool_t down=kFALSE);
+    void ReSort(Long64_t n, Float_t  *a, Bool_t down=kFALSE);
+    void ReSort(Long64_t n, Double_t *a, Bool_t down=kFALSE);
 
     template <class Size, class Element> Double_t MedianDevImp(Size n, const Element *a) { Double_t med; return MedianDevImp(n, a, med); }
Index: trunk/MagicSoft/Mars/mfileio/MReadMarsFile.cc
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MReadMarsFile.cc	(revision 9218)
+++ trunk/MagicSoft/Mars/mfileio/MReadMarsFile.cc	(revision 9219)
@@ -201,5 +201,5 @@
     if (!rawheader->IsValid())
     {
-        *fLog << warn << "WARNING - The run header read from the file is invalid." << endl;
+        *fLog << warn << "WARNING - The run header (MRawRunHeader) read from the file returns not IsValid()." << endl;
         *fLog << "          Please check if the file contents are ok." << endl;
         rawheader->Print("header");
Index: trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc	(revision 9218)
+++ trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc	(revision 9219)
@@ -122,5 +122,5 @@
     }
 
-    if (!file)
+    if (!file || TString(name)=="/dev/null")
     {
         file = new TFile(name, option, title, comp);
@@ -760,4 +760,5 @@
     {
         *fLog << err << "ERROR - MWriteRootFile::ChangeFile... something went terribly wrong!" << endl;
+        *fLog <<        "        fname: " << fname << endl;
         *fLog <<        "        Please start debugging!" << endl;
         return kFALSE;
Index: trunk/MagicSoft/Mars/mgeom/MGeomCam.cc
===================================================================
--- trunk/MagicSoft/Mars/mgeom/MGeomCam.cc	(revision 9218)
+++ trunk/MagicSoft/Mars/mgeom/MGeomCam.cc	(revision 9219)
@@ -517,6 +517,5 @@
     // search for the neighbor in the given direction
     //
-    int i;
-    for (i=0; i<pix.GetNumNeighbors(); i++)
+    for (int i=0; i<pix.GetNumNeighbors(); i++)
         if (GetDirection(idx, pix.GetNeighbor(i))==dir)
             return pix.GetNeighbor(i);
Index: trunk/MagicSoft/Mars/mgeom/MGeomCamDwarf.h
===================================================================
--- trunk/MagicSoft/Mars/mgeom/MGeomCamDwarf.h	(revision 9218)
+++ trunk/MagicSoft/Mars/mgeom/MGeomCamDwarf.h	(revision 9219)
@@ -1,3 +1,3 @@
-#ifndef MARS_MGeomCamDwqarf
+#ifndef MARS_MGeomCamDwarf
 #define MARS_MGeomCamDwarf
 
Index: trunk/MagicSoft/Mars/mgeom/MGeomPix.cc
===================================================================
--- trunk/MagicSoft/Mars/mgeom/MGeomPix.cc	(revision 9218)
+++ trunk/MagicSoft/Mars/mgeom/MGeomPix.cc	(revision 9219)
@@ -55,4 +55,5 @@
 
 #include <TMath.h>
+#include <TVector2.h>
 
 #include "MLog.h"
@@ -77,4 +78,13 @@
     Set(x, y, r, s, a);
     SetNeighbors();
+}
+
+// --------------------------------------------------------------------------
+//
+// Return position as TVector2(fX, fY)
+//
+TVector2 MGeomPix::GetP() const
+{
+    return TVector2(fX, fY);
 }
 
@@ -186,5 +196,5 @@
     // more, rotated with respect to that one by +- 60 degrees.
     //
-    if (TMath::Abs(dx)*2>fD)
+    if (TMath::Abs(dx)>fD/2)
         return kFALSE;
 
@@ -194,10 +204,11 @@
     const static Double_t sin60 = TMath::Sin(60/kRad2Deg);
 
-    const Double_t dx2 = dx*cos60 + dy*sin60;
-    if  (TMath::Abs(dx2)*2>fD)
+    const Double_t dxc = dx*cos60;
+    const Double_t dys = dy*sin60;
+
+    if  (TMath::Abs(dxc + dys)>fD/2)
         return kFALSE;
 
-    const Double_t dx3 = dx*cos60 - dy*sin60;
-    if (TMath::Abs(dx3)*2>fD)
+    if (TMath::Abs(dxc - dys)>fD/2)
         return kFALSE;
 
@@ -218,10 +229,12 @@
     const Double_t y2 = pix.GetY();
 
-    if (x1>=x2 && y1>y2) return kRightTop;
-    if (x1>=x2 && y1<y2) return kRightBottom;
-    if (x1<=x2 && y1>y2) return kLeftTop;
-    if (x1<=x2 && y1<y2) return kLeftBottom;
-    if (x1>x2)           return kRight;
-    if (x1<x2)           return kLeft;
+    if (x1<=x2 && y1<y2) return kRightTop;
+    if (x1<=x2 && y1>y2) return kRightBottom;
+    if (x1>=x2 && y1<y2) return kLeftTop;
+    if (x1>=x2 && y1>y2) return kLeftBottom;
+    if (x1<x2)           return kRight;
+    if (x1>x2)           return kLeft;
+
+    cout << -1 << endl;
 
     return -1;
Index: trunk/MagicSoft/Mars/mgeom/MGeomPix.h
===================================================================
--- trunk/MagicSoft/Mars/mgeom/MGeomPix.h	(revision 9218)
+++ trunk/MagicSoft/Mars/mgeom/MGeomPix.h	(revision 9219)
@@ -7,4 +7,5 @@
 
 class MGeomCam;
+class TVector2;
 
 class MGeomPix : public MParContainer
@@ -77,4 +78,6 @@
     UInt_t  GetSector() const { return fSector; }
 
+    TVector2 GetP() const;
+
     Float_t GetDist() const;
     Float_t GetDist(const MGeomPix &pix) const;
Index: trunk/MagicSoft/Mars/mmain/MEventDisplay.cc
===================================================================
--- trunk/MagicSoft/Mars/mmain/MEventDisplay.cc	(revision 9218)
+++ trunk/MagicSoft/Mars/mmain/MEventDisplay.cc	(revision 9219)
@@ -1,4 +1,4 @@
 /* ======================================================================== *\
-! $Name: not supported by cvs2svn $:$Id: MEventDisplay.cc,v 1.65 2008-07-20 14:21:39 tbretz Exp $
+! $Name: not supported by cvs2svn $:$Id: MEventDisplay.cc,v 1.66 2009-01-14 12:31:36 tbretz Exp $
 ! --------------------------------------------------------------------------
 !
@@ -334,6 +334,6 @@
         MCalibrationPatternDecode *decode = new MCalibrationPatternDecode;
 
-        MPedestalSubtract *pedsub1  = new MPedestalSubtract;
-        MPedestalSubtract *pedsub2  = new MPedestalSubtract;
+        MPedestalSubtract *pedsub1  = new MPedestalSubtract("PedSubstract1");
+        MPedestalSubtract *pedsub2  = new MPedestalSubtract("PedSubstract2");
         pedsub2->SetNamePedestalCam();
 
@@ -678,6 +678,6 @@
 void MEventDisplay::ShowHide()
 {
-    TGCheckButton *but1 = (TGCheckButton*)fList->FindWidget(kShowMuon);
-    TGCheckButton *but2 = (TGCheckButton*)fList->FindWidget(kShowImgPar);
+    TGCheckButton *but1 = (TGCheckButton*)fList->FindWidget(kShowImgPar);
+    TGCheckButton *but2 = (TGCheckButton*)fList->FindWidget(kShowMuon);
 
     const Bool_t imgpar = but1 && but1->IsDown();
@@ -722,8 +722,10 @@
         for (int j=0;j<n; j++)
             if (obj[j])
+            {
                 if (state[j] && !list->FindObject(obj[j]))
                     list->Add(obj[j]);
-                else
+                if (!state[j] && list->FindObject(obj[j]))
                     list->Remove(obj[j]);
+            }
 
         gPad->Modified(kTRUE);
@@ -744,14 +746,13 @@
 
     //
-    // Add Geometry tab
+    // Now read event...
+    //
+    // FIXME: Can we safely replace ReadinEvent() by RedinEvent(1)?
+    ReadinEvent();
+
+    //
+    // After ReInit (MGeomCam might be read from RunHeaders): Add Geometry tab
     //
     AddGeometryTabs();
-
-    //
-    // Now read event...
-    //
-    // FIXME: Can we safly replace ReadinEvent() by RedinEvent(1)?
-    ReadinEvent();
-
 
     MReadTree *reader = (MReadTree*)fEvtLoop->FindTask("MRead");
Index: trunk/MagicSoft/Mars/mraw/MRawEvtPixelIter.h
===================================================================
--- trunk/MagicSoft/Mars/mraw/MRawEvtPixelIter.h	(revision 9218)
+++ trunk/MagicSoft/Mars/mraw/MRawEvtPixelIter.h	(revision 9219)
@@ -30,6 +30,6 @@
     UShort_t  fNumEntry;
 
-    Byte_t fNumBytesHiGain;   //!
-    Byte_t fNumBytesLoGain;   //!
+    UInt_t fNumBytesHiGain;   //!
+    UInt_t fNumBytesLoGain;   //!
 
     UShort_t fNumBytesPerSample; //!
@@ -79,5 +79,5 @@
     }
 
-    Byte_t  GetNumBytes() const { return fNumBytesHiGain+fNumBytesLoGain; }
+    UInt_t GetNumBytes() const { return fNumBytesHiGain+fNumBytesLoGain; }
 
     Bool_t HasLoGain() const { return fNumBytesLoGain>0; }
Index: trunk/MagicSoft/Mars/mraw/MRawRunHeader.h
===================================================================
--- trunk/MagicSoft/Mars/mraw/MRawRunHeader.h	(revision 9218)
+++ trunk/MagicSoft/Mars/mraw/MRawRunHeader.h	(revision 9219)
@@ -107,4 +107,5 @@
     void SetObservation(const char mode[60], const char proj[100]);
     void SetNumEvents(UInt_t num);
+    void SetValidMagicNumber() { fMagicNumber=kMagicNumber; }
 
     // This is to get the numbers...
Index: trunk/MagicSoft/Mars/readraw.cc
===================================================================
--- trunk/MagicSoft/Mars/readraw.cc	(revision 9218)
+++ trunk/MagicSoft/Mars/readraw.cc	(revision 9219)
@@ -105,16 +105,13 @@
 
     //
+    // Initialize Non-GUI (batch) mode
+    //
+    gROOT->SetBatch();
+
+    //
     // This is to make argv[i] more readable insidethe code
     //
     TString kNamein = arg.GetArgumentStr(0);
   
-    if (!kNamein.EndsWith(".root"))
-        kNamein += ".root";
-
-    //
-    // Initialize Non-GUI (batch) mode
-    //
-    gROOT->SetBatch();
-
     //
     // check whether the given files are OK.
@@ -122,6 +119,12 @@
     if (gSystem->AccessPathName(kNamein, kFileExists))
     {
-        gLog << err << "Sorry, the input file '" << kNamein << "' doesn't exist." << endl;
-        return 2;
+        if (!kNamein.EndsWith(".root"))
+            kNamein += ".root";
+
+        if (gSystem->AccessPathName(kNamein, kFileExists))
+        {
+            gLog << err << "Sorry, the input file '" << kNamein << "' doesn't exist." << endl;
+            return 2;
+        }
     }
 
Index: trunk/MagicSoft/include-Classes/MMcFormat/MMcEvtBasic.h
===================================================================
--- trunk/MagicSoft/include-Classes/MMcFormat/MMcEvtBasic.h	(revision 9218)
+++ trunk/MagicSoft/include-Classes/MMcFormat/MMcEvtBasic.h	(revision 9219)
@@ -12,16 +12,17 @@
     enum ParticleId_t
     {
-        kUNDEFINED = -1,
-        kGAMMA     =  1,
-        kPOSITRON  =  2,
-        kELECTRON  =  3,
-        kANTIMUON  =  5,
-        kMUON      =  6,
-        kPI0       =  7,
-        kNEUTRON   = 13,
-        kPROTON  =   14,
-        kHELIUM  =  402,
-        kOXYGEN  = 1608,
-        kIRON    = 5626
+        kUNDEFINED  = -1,
+        kGAMMA      =  1,
+        kPOSITRON   =  2,
+        kELECTRON   =  3,
+        kANTIMUON   =  5,
+        kMUON       =  6,
+        kPI0        =  7,
+        kNEUTRON    = 13,
+        kPROTON   =   14,
+        kHELIUM   =  402,
+        kOXYGEN   = 1608,
+        kIRON     = 5626,
+        kNightSky = 9999
     };
 
@@ -65,4 +66,5 @@
       case kOXYGEN:   return "Oxygen";
       case kIRON:     return "Iron";
+      case kNightSky: return "NSB";
       }
 
@@ -86,4 +88,5 @@
       case kOXYGEN:   return "O";
       case kIRON:     return "Fe";
+      case kNightSky: return "\\gamma_{NSB}";
       }
 
