Index: /trunk/Mars/msim/MClonesArray.cc
===================================================================
--- /trunk/Mars/msim/MClonesArray.cc	(revision 19561)
+++ /trunk/Mars/msim/MClonesArray.cc	(revision 19561)
@@ -0,0 +1,99 @@
+#include "TObject.h"
+#include "TClonesArray.h"
+#include "MClonesArray.h"
+#include "TClass.h"
+
+ClassImp(MClonesArray)
+
+TObject** MClonesArray::FirstRef() { return fCont; }
+
+// --------------------------------------------------------------------------
+//
+// This is an extremely optimized version of ExpandCreateFast. It only resets
+// the marker for the last element (fLast) to n-1 and doesn't change anything
+// else. This implicitly assumes that the stored objects don't allocate
+// memory. It does not necessarily mean that the slots after fLast
+// are empty (set to 0). This is what is assumed in the TClonesArray.
+// We also don't call Changed() because it would reset Sorted. If the
+// array was sorted before it is also sorted now. You MUST make sure
+// that you only set n in a range for which valid entries have been
+// created before (e.g. by ExpandCreateFast).
+//
+void MClonesArray::FastShrink(Int_t n)
+{
+	fLast = n - 1;
+}
+
+// --------------------------------------------------------------------------
+//
+// This is a optimized (faster) version of Delete which deletes consequtive
+// entries from index idx1 to idx2 (both included) and calls their
+// destructor. Note that there is no range checking done!
+//
+void MClonesArray::FastRemove(Int_t idx1, Int_t idx2)
+{
+	// Remove object at index idx.
+
+	//if (!BoundsOk("RemoveAt", idx1)) return 0;
+	//if (!BoundsOk("RemoveAt", idx2)) return 0;
+
+	Long_t dtoronly = TObject::GetDtorOnly();
+
+	idx1 -= fLowerBound;
+	idx2 -= fLowerBound;
+
+	for (TObject **obj=fCont+idx1; obj<=fCont+idx2; obj++)
+	{
+		if (!*obj)
+			continue;
+
+		if ((*obj)->TestBit(kNotDeleted)) {
+			// Tell custom operator delete() not to delete space when
+			// object fCont[i] is deleted. Only destructors are called
+			// for this object.
+			TObject::SetDtorOnly(*obj);
+			delete *obj;
+		}
+
+		*obj = 0;
+		// recalculate array size
+	}
+	TObject::SetDtorOnly((void*)dtoronly);
+
+	if (idx1<=fLast && fLast<=idx2)
+	{
+		do {
+			fLast--;
+		} while (fLast >= 0 && fCont[fLast] == 0);
+	}
+
+	Changed();
+}
+
+// --------------------------------------------------------------------------
+//
+// This is an optimized version of Sort which doesn't check the
+// IsSortable flag before. It only sorts the entries from 0
+// to GetEntriesFast().
+//
+void MClonesArray::UncheckedSort()
+{
+	if (fSorted)
+		return;
+
+	const Int_t nentries = GetEntriesFast();
+	if (nentries <= 0)
+		return;
+
+	QSort(GetObjectRef(First()), fKeep->GetObjectRef(fKeep->First()),
+		  0, TMath::Min(nentries, kMaxInt-fLowerBound));
+
+	fSorted = kTRUE;
+}
+
+// FIXME: This seems not to be enough to make this serialize nicely into
+// root files. I'm out of ideas.
+void MClonesArray::Streamer(TBuffer &b)
+{
+	TClonesArray::Streamer(b);
+}
Index: /trunk/Mars/msim/MClonesArray.h
===================================================================
--- /trunk/Mars/msim/MClonesArray.h	(revision 19561)
+++ /trunk/Mars/msim/MClonesArray.h	(revision 19561)
@@ -0,0 +1,26 @@
+#ifndef MARS_MCLONESARRAY_H
+#define MARS_MCLONESARRAY_H
+
+#include "TClonesArray.h"
+
+class MClonesArray : public TClonesArray
+{
+public:
+	enum EStatusBits {
+       kForgetBits     = BIT(0),   // Do not create branches for fBits, fUniqueID
+       kNoSplit        = BIT(1),   // No splitting in TBranch
+       kBypassStreamer = BIT(12),  // Class Streamer not called (default)
+	};
+
+    using TClonesArray::TClonesArray;
+
+    TObject **FirstRef();
+
+    void FastShrink(Int_t n);
+    void FastRemove(Int_t idx1, Int_t idx2);
+    void UncheckedSort();
+	virtual ~MClonesArray() {}
+    ClassDef(MClonesArray, 2) // A TClonesArray with optimized sorting and shrinking
+};
+
+#endif //MARS_MCLONESARRAY_H
Index: /trunk/Mars/msim/MPhotonEvent.cc
===================================================================
--- /trunk/Mars/msim/MPhotonEvent.cc	(revision 19560)
+++ /trunk/Mars/msim/MPhotonEvent.cc	(revision 19561)
@@ -101,7 +101,8 @@
 //
 //
-//   Version 1:
+//   Version 2:
 //   ----------
 //    - First implementation
+//    - fData is now A MClonesArray to avoid casting issues on some compilers
 //
 /////////////////////////////////////////////////////////////////////////////
@@ -127,100 +128,4 @@
 using namespace std;
 
-// ==========================================================================
-
-class MyClonesArray : public TClonesArray
-{
-public:
-    TObject **FirstRef() { return fCont; }
-
-    // --------------------------------------------------------------------------
-    //
-    // This is an extremly optimized version of ExpandCreateFast. It only resets
-    // the marker for the last element (fLast) to n-1 and doen't change anything
-    // else. This implicitly assumes that the stored objects don't allocate
-    // memory. It does not necessarily mean that the slots after fLast
-    // are empty (set to 0). This is what is assumed in the TClonesArray.
-    // We also don't call Changed() because it would reset Sorted. If the
-    // array was sorted before it is also sorted now. You MUST make sure
-    // that you only set n in a range for which valid entries have been
-    // created before (e.g. by ExpandCreateFast).
-    //
-    void FastShrink(Int_t n)
-    {
-        fLast = n - 1;
-    }
-
-    // --------------------------------------------------------------------------
-    //
-    // This is a optimized (faster) version of Delete which deletes consequtive
-    // entries from index idx1 to idx2 (both included) and calls their
-    // destructor. Note that there is no range checking done!
-    //
-    void FastRemove(Int_t idx1, Int_t idx2)
-    {
-        // Remove object at index idx.
-
-        //if (!BoundsOk("RemoveAt", idx1)) return 0;
-        //if (!BoundsOk("RemoveAt", idx2)) return 0;
-
-        Long_t dtoronly = TObject::GetDtorOnly();
-
-        idx1 -= fLowerBound;
-        idx2 -= fLowerBound;
-
-        for (TObject **obj=fCont+idx1; obj<=fCont+idx2; obj++)
-        {
-            if (!*obj)
-                continue;
-
-            if ((*obj)->TestBit(kNotDeleted)) {
-                // Tell custom operator delete() not to delete space when
-                // object fCont[i] is deleted. Only destructors are called
-                // for this object.
-                TObject::SetDtorOnly(*obj);
-                delete *obj;
-            }
-
-            *obj = 0;
-            // recalculate array size
-        }
-        TObject::SetDtorOnly((void*)dtoronly);
-
-        if (idx1<=fLast && fLast<=idx2)
-        {
-            do {
-                fLast--;
-            } while (fLast >= 0 && fCont[fLast] == 0);
-        }
-
-        Changed();
-    }
-
-
-    //void SetSorted() { fSorted = kTRUE; }
-
-    // --------------------------------------------------------------------------
-    //
-    // This is an optimized version of Sort which doesn't check the
-    // IsSortable flag before. It only sorts the entries from 0
-    // to GetEntriesFast().
-    //
-    void UncheckedSort()
-    {
-        if (fSorted)
-            return;
-
-        const Int_t nentries = GetEntriesFast();
-        if (nentries <= 0)
-            return;
-
-        QSort(GetObjectRef(First()), fKeep->GetObjectRef(fKeep->First()),
-              0, TMath::Min(nentries, kMaxInt-fLowerBound));
-
-        fSorted = kTRUE;
-    }
-};
-
-// ==========================================================================
 
 // --------------------------------------------------------------------------
@@ -274,5 +179,5 @@
 
     // Just set fLast = n -1
-    static_cast<MyClonesArray&>(fData).FastShrink(n);
+    fData.FastShrink(n);
     return fData.GetEntriesFast();
 }
@@ -370,5 +275,5 @@
     {
         o=fData.UncheckedAt(n);
-        static_cast<MyClonesArray&>(fData).FastShrink(n+1);
+        fData.FastShrink(n+1);
     }
     else
@@ -396,5 +301,6 @@
         fData.UnSort();
 
-    static_cast<MyClonesArray&>(fData).UncheckedSort(); /*Sort(GetEntriesFast())*/
+    fData.UncheckedSort();
+    // fData.Sort(fData.GetEntriesFast());
 }
 
Index: /trunk/Mars/msim/MPhotonEvent.h
===================================================================
--- /trunk/Mars/msim/MPhotonEvent.h	(revision 19560)
+++ /trunk/Mars/msim/MPhotonEvent.h	(revision 19561)
@@ -11,4 +11,5 @@
 
 #include <iosfwd>
+#include "MClonesArray.h"
 
 using namespace std;
@@ -21,5 +22,5 @@
 {
 private:
-    TClonesArray fData;
+    MClonesArray fData;
 
 public:
@@ -41,6 +42,6 @@
     Double_t GetMeanT() const;
 
-    TClonesArray &GetArray() { return fData; }
-    const TClonesArray &GetArray() const { return fData; }
+    MClonesArray &GetArray() { return fData; }
+    const MClonesArray &GetArray() const { return fData; }
 
     Bool_t AsciiWrite(ostream &out) const;
@@ -71,5 +72,5 @@
     //void Clear(Option_t * = NULL);
 
-    ClassDef(MPhotonEvent, 1) //Container to store the raw Event Data
+    ClassDef(MPhotonEvent, 2) //Container to store the raw Event Data
 };
 
Index: /trunk/Mars/msim/Makefile
===================================================================
--- /trunk/Mars/msim/Makefile	(revision 19560)
+++ /trunk/Mars/msim/Makefile	(revision 19561)
@@ -28,5 +28,6 @@
 	   MSimAtmosphere.cc \
 	   MSimAbsorption.cc \
-	   MSimPointingPos.cc
+	   MSimPointingPos.cc \
+	   MClonesArray.cc
 
 ############################################################
Index: /trunk/Mars/msim/SimLinkDef.h
===================================================================
--- /trunk/Mars/msim/SimLinkDef.h	(revision 19560)
+++ /trunk/Mars/msim/SimLinkDef.h	(revision 19561)
@@ -18,3 +18,5 @@
 #pragma link C++ class MSimMMCS+;
 
+#pragma link C++ class MClonesArray-; // - needed as custom streamer is already implemented
+
 #endif
