Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 8405)
+++ trunk/MagicSoft/Mars/Changelog	(revision 8406)
@@ -19,4 +19,27 @@
                                                  -*-*- END OF LINE -*-*-
 
+ 2007/04/15 Thomas Bretz
+
+   * callisto.cc:
+     - added an option to write a movie
+
+   * mjobs/MJCalibrateSignal.[h,cc]:
+     - added the code to write a movie
+     - added new option (fIsMovieMode) to switch it on and off
+
+   * mmovie/MMovieData.h:
+     - added data member fMedianPedestalRms
+
+   * mmovie/MMoviePrepare.[h,cc]:
+     - added calculation of median pedestal rms
+
+   * mmovie/MMovieWrite.[h,cc]:
+     - added comments
+     - added return value to SetPalette
+     - delete palette entries after usage
+     - get median pedestal rms from MMovieData
+
+
+
  2007/04/12 Daniela Dorner
 
@@ -77,4 +100,37 @@
      movie/MovieIncl.h:
      - added
+
+   * mbase/MArrayB.h, mbase/MArrayD.h, mbase/MArrayF.h, mbase/MArrayI.h,
+     mbase/MArrayS.h:
+     - added Reset() member function or improved performance of it
+
+   * mcalib/MCalibrationChargeCalc.cc:
+     - fixed the alignment in an output
+
+   * mhbase/MBinning.h:
+     - added operator[] member function
+
+   * mhist/MHCamera.cc:
+     - changed such that it is allowed to use palettes with a different
+       number of contours.
+     - fixed the number of ticks written on the axis to 25
+     - use MBinnig to calculate the legend color boxes
+     - fixed a minor bug in the assignment of the color levels, which
+       gave visibly wrong result if the transition between two colors
+       was important but was not noticable in normal circumstances
+
+   * mjobs/MDataSet.h, mjobs/MSequence.h:
+     - increased class version to 1 to make it writable
+     - added Print to the context menu
+
+   * mgeom/MGeomCam.[h,cc]:
+     - removed fNumSectors
+     - removed fNumAreas
+     - added fNumPixInSector
+     - added fNumPixWithAidx
+     - changed CalcSectors and CalcArea to fill new data members
+     - fixed a bug in the range check of GetMaxRadius and GetMinRadius
+       (the range was to wide by 1 at the upper edge)
+     - increased class version from 4 to 5
 
 
Index: trunk/MagicSoft/Mars/callisto.cc
===================================================================
--- trunk/MagicSoft/Mars/callisto.cc	(revision 8405)
+++ trunk/MagicSoft/Mars/callisto.cc	(revision 8406)
@@ -83,4 +83,6 @@
     gLog << "   --use-test                Apply calibration constants to same calibration" << endl;
     gLog << "                             file (for testing, calibration mode only)" << endl;
+    gLog << "   --movie                   Write a movie in addition to Mode-Y (this might " << endl;
+    gLog << "                             stop the eventloop before all evts are processed)" << endl;
     gLog << "   --moon                    Force using pedestal fits instead of calculated RMS" << endl;
     gLog << "   --config=callisto.rc      Resource file [default=callisto.rc]" << endl;
@@ -170,4 +172,5 @@
     const Bool_t  kPrintFound = arg.HasOnlyAndRemove("--print-found");
     const Bool_t  kUseTest    = arg.HasOnlyAndRemove("--use-test");
+    const Bool_t  kMovie      = arg.HasOnlyAndRemove("--movie");
     const Bool_t  kDebugMem   = arg.HasOnlyAndRemove("--debug-mem");
           Bool_t  kMoon       = arg.HasOnlyAndRemove("--moon");
@@ -225,4 +228,10 @@
         gLog <<        "specified! You can also give the operation mode by the options defining the" << endl;
         gLog <<        "output path --outc, --outy or --out." << endl;
+        Usage();
+        return 2;
+    }
+    if (!kModeY && kMovie)
+    {
+        gLog << err << "Signal extraction mode (-y) not switched on but movie requested." << endl;
         Usage();
         return 2;
@@ -626,4 +635,5 @@
         job4.SetPathOut(kOutpathY);
         job4.SetDataType(kDataType);
+        job4.SetMovieMode(kMovie);
         if (kDataType!=MJCalib::kIsUseMC)
             job4.SetExtractor(job2.GetExtractor());
Index: trunk/MagicSoft/Mars/mjobs/MJCalibrateSignal.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJCalibrateSignal.cc	(revision 8405)
+++ trunk/MagicSoft/Mars/mjobs/MJCalibrateSignal.cc	(revision 8406)
@@ -112,10 +112,8 @@
 #include "MWriteRootFile.h"
 
-/*
- #include "MFEvtNumber.h"
- #include "MMoviePrepare.h"
- #include "MMovieWrite.h"
- #include "MImgCleanStd.h"
- */
+#include "MFEvtNumber.h"
+#include "MMoviePrepare.h"
+#include "MMovieWrite.h"
+#include "MImgCleanStd.h"
 
 ClassImp(MJCalibrateSignal);
@@ -134,5 +132,5 @@
 //
 MJCalibrateSignal::MJCalibrateSignal(const char *name, const char *title)
-    : fExtractor(0), fIsInterlaced(kTRUE), fIsRelTimesUpdate(kFALSE)
+    : fExtractor(0), fIsInterlaced(kTRUE), fIsRelTimesUpdate(kFALSE), fIsMovieMode(kFALSE)
 {
     fName  = name  ? name  : "MJCalibrateSignal";
@@ -224,4 +222,5 @@
     SetInterlaced(GetEnv("Interlaced", fIsInterlaced));
     SetRelTimesUpdate(GetEnv("RelTimesUpdate", fIsRelTimesUpdate));
+    SetMovieMode(GetEnv("MovieMode", fIsMovieMode));
 
     return MJCalib::CheckEnvLocal();
@@ -802,35 +801,40 @@
 
      tlist2.AddToList(&fillP);
-
-     // --- Start: Code for encoding movies ---
-
-     MMoviePrepare movprep;
-     MMovieWrite   movwrite;
-     movprep.SetRangeFromExtractor(*extractor1);
-
-     MFDataPhrase movfilt("MMovieData.fMax>150");
-
-     MImgCleanStd movclean(8.5, 4.0);
-     movclean.SetMethod(MImgCleanStd::kAbsolute);
-
-     //movprep.SetFilter(&evtnum);
-     movclean.SetFilter(&movfilt);
-     movwrite.SetFilter(&movfilt);
-
-     MTaskList tlistmov;
-     tlistmov.AddToList(&movprep);
-     tlistmov.AddToList(&movfilt);
-     tlistmov.AddToList(&movclean);
-     tlistmov.AddToList(&movwrite);
-
-     MFEvtNumber evtnum;
-     //evtnum.SetFileName("ganymed00000001.root");
-     tlistmov.SetFilter(&evtnum);
-
-     //tlist2.AddToList(&evtnum);
-     //tlist2.AddToList(&tlistmov);
-
-     // --- End: Code for encoding movies ---
      */
+
+    // ----- Start: Code for encoding movies -----
+
+    MMoviePrepare movprep;
+    MMovieWrite   movwrite;
+    movprep.SetRangeFromExtractor(*extractor1);
+
+    //MFDataPhrase movfilt("MMovieData.fMax>150");
+    MFDataPhrase movfilt("MMovieData.fMax>5*MMovieData.fMedianPedestalRms", "MovieFilter");
+
+    MImgCleanStd movclean(8.5, 4.0);
+    movclean.SetMethod(MImgCleanStd::kAbsolute);
+
+    //movprep.SetFilter(&evtnum);
+    movclean.SetFilter(&movfilt);
+    movwrite.SetFilter(&movfilt);
+
+    MTaskList tlistmov;
+    tlistmov.AddToList(&movprep);
+    tlistmov.AddToList(&movfilt);
+    tlistmov.AddToList(&movclean);
+    tlistmov.AddToList(&movwrite);
+
+    MFEvtNumber evtnum;
+    //evtnum.SetSelector("ThetaSquared.fVal<0.04");
+    //evtnum.SetFileName("ganymed00000001.root");
+    tlistmov.SetFilter(&evtnum);
+
+    if (fIsMovieMode)
+    {
+        tlist2.AddToList(&evtnum);
+        tlist2.AddToList(&tlistmov);
+    }
+
+    // ----- End: Code for encoding movies -----
 
     tlist2.AddToList(&fillflorian);
Index: trunk/MagicSoft/Mars/mjobs/MJCalibrateSignal.h
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJCalibrateSignal.h	(revision 8405)
+++ trunk/MagicSoft/Mars/mjobs/MJCalibrateSignal.h	(revision 8406)
@@ -24,5 +24,6 @@
     Bool_t fIsInterlaced;               // Distinguish interlaced from other calibration
     Bool_t fIsRelTimesUpdate;           // Choose to update relative times from interlaced
-    
+    Bool_t fIsMovieMode;                // Choose to encode a movie
+
     Bool_t CheckEnvLocal();
 
@@ -37,5 +38,4 @@
     
 public:
-
     MJCalibrateSignal(const char *name=NULL, const char *title=NULL);
     ~MJCalibrateSignal();
@@ -43,6 +43,7 @@
     Bool_t Process(MPedestalCam &camab, MPedestalCam &cam1, MPedestalCam &cam2);
 
-    void SetInterlaced     ( const Bool_t b=kTRUE )  { fIsInterlaced      = b; }
-    void SetRelTimesUpdate ( const Bool_t b=kTRUE )  { fIsRelTimesUpdate  = b; }
+    void SetInterlaced    (const Bool_t b=kTRUE) { fIsInterlaced     = b; }
+    void SetRelTimesUpdate(const Bool_t b=kTRUE) { fIsRelTimesUpdate = b; }
+    void SetMovieMode     (const Bool_t b=kTRUE) { fIsMovieMode      = b; }
 
     void SetExtractor(const MExtractor *ext=NULL);
Index: trunk/MagicSoft/Mars/mmovie/MMovieData.h
===================================================================
--- trunk/MagicSoft/Mars/mmovie/MMovieData.h	(revision 8405)
+++ trunk/MagicSoft/Mars/mmovie/MMovieData.h	(revision 8406)
@@ -23,4 +23,5 @@
 
     Float_t fMax;
+    Float_t fMedianPedestalRms;
     UInt_t  fNumSlices;
 
@@ -41,4 +42,7 @@
     Float_t GetMax() const { return fMax; }
 
+    void SetMedianPedestalRms(Float_t m) { fMedianPedestalRms = m; }
+    Float_t GetMedianPedestalRms() const { return fMedianPedestalRms; }
+
     void SetNumSlices(UInt_t n) { fNumSlices=n; }
     UInt_t GetNumSlices() const { return fNumSlices; }
Index: trunk/MagicSoft/Mars/mmovie/MMoviePrepare.cc
===================================================================
--- trunk/MagicSoft/Mars/mmovie/MMoviePrepare.cc	(revision 8405)
+++ trunk/MagicSoft/Mars/mmovie/MMoviePrepare.cc	(revision 8406)
@@ -48,4 +48,5 @@
 //   MGeomCam
 //   MPedestalSubtractedEvt
+//   MPedestalFundamental [MPedestalCam]
 //   MCalibrateData
 //   MCalibrationRelTimeCam
@@ -70,4 +71,8 @@
 #include "MGeomCam.h"
 #include "MGeomPix.h"
+
+#include "MPedestalCam.h"
+#include "MPedestalPix.h"
+
 
 #include "MCalibrationRelTimeCam.h"
@@ -78,5 +83,4 @@
 #include "MRawRunHeader.h"
 #include "MCalibrateData.h"
-//#include "MExtractedSignalCam.h"
 #include "MPedestalSubtractedEvt.h"
 
@@ -111,4 +115,5 @@
 //   MGeomCam
 //   MPedestalSubtractedEvt
+//   MPedestalFundamental [MPedestalCam]
 //   MCalibrateData
 //   MCalibrationRelTimeCam
@@ -132,4 +137,10 @@
         return kFALSE;
     }
+    fPed = (MPedestalCam*)plist->FindObject("MPedestalFundamental", "MPedestalCam");
+    if (!fPed)
+    {
+        *fLog << err << "MPedestalFundamental [MPedestalCam] not found ... aborting." << endl;
+        return kFALSE;
+    }
     fCal = (MCalibrateData*)plist->FindTaskListWithTask("MCalibrateData")->FindTask("MCalibrateData");
     if (!fCal)
@@ -184,13 +195,30 @@
     const MArrayF &calco = fCal->GetCalibConsts();
 
-    Int_t n = fCam->GetNumPixWithArea(0);
-
-    MArrayF calco0(n);
+    Int_t n = fCam->GetNumPixWithAidx(0);
+
+    MArrayF arr(n);
 
     for (UInt_t i=0; i<fCam->GetNumPixels(); i++)
         if ((*fCam)[i].GetAidx()==0)
-            calco0[--n] = calco[i];
-
-    return TMath::Median(calco0.GetSize(), calco0.GetArray());
+            arr[--n] = calco[i];
+
+    return TMath::Median(arr.GetSize(), arr.GetArray());
+}
+
+// --------------------------------------------------------------------------
+//
+// Return the median of the pedestal rms of all pixels with area index 0
+//
+Double_t MMoviePrepare::GetMedianPedestalRms() const
+{
+    Int_t n = fCam->GetNumPixWithAidx(0);
+
+    MArrayF rms(n);
+
+    for (UInt_t i=0; i<fCam->GetNumPixels(); i++)
+        if ((*fCam)[i].GetAidx()==0)
+            rms[--n] = (*fPed)[i].GetPedestalRms();
+
+    return TMath::Median(rms.GetSize(), rms.GetArray());
 }
 
@@ -252,4 +280,5 @@
     fOut->SetMax(max);
     fOut->SetNumSlices(width);
+    fOut->SetMedianPedestalRms(GetMedianPedestalRms());
 
     return kTRUE;
Index: trunk/MagicSoft/Mars/mmovie/MMoviePrepare.h
===================================================================
--- trunk/MagicSoft/Mars/mmovie/MMoviePrepare.h	(revision 8405)
+++ trunk/MagicSoft/Mars/mmovie/MMoviePrepare.h	(revision 8406)
@@ -9,6 +9,5 @@
 class MExtractor;
 class MMovieData;
-class MSignalCam;
-class MRawEvtHeader;
+class MPedestalCam;
 class MRawRunHeader;
 class MCalibrateData;
@@ -24,5 +23,5 @@
     MRawRunHeader          *fRun;    //! Run Header (sampling frequency)
     MGeomCam               *fCam;    //! Pixel size
-//    MCalibConstCam         *fCal;
+    MPedestalCam           *fPed;    //! Fundamental pedestal (slice by slice)
 
     MMovieData             *fOut;    //! Prepared output container
@@ -31,9 +30,6 @@
     UInt_t fLastSlice;
 
-    //Float_t fSlope;
-
     // MTask
     Int_t  PreProcess(MParList *plist);
-    //Bool_t ReInit(MParList *plist);
     Int_t  Process();
 
@@ -43,4 +39,5 @@
     // MMoviePrepare
     Double_t GetMedianCalibConst() const;
+    Double_t GetMedianPedestalRms() const;
 
 public:
@@ -53,3 +50,2 @@
 
 #endif
-
Index: trunk/MagicSoft/Mars/mmovie/MMovieWrite.cc
===================================================================
--- trunk/MagicSoft/Mars/mmovie/MMovieWrite.cc	(revision 8405)
+++ trunk/MagicSoft/Mars/mmovie/MMovieWrite.cc	(revision 8406)
@@ -60,16 +60,22 @@
 //
 //  The interpolation of the frames is done using a TSpline3. If the spline
-//  would extrapolate the contents is set to zero. Unsuitable pixels are
-//  interpolated frame by frame using the surrounding suitable pixels.
-//
-//
-// Input:
-//   - MGeomCam
-//   - MRawRunHeader
-//   - MRawEvtHeader
-//   - MSignalCam
-//   - MBadPixelsCam
-//   - MPedestalFundamental [MPedestalCam]
-//   - MMovieData
+//  would extrapolate due to the shift by the relative time calibration the
+//  contents is set to zero. Unsuitable pixels are interpolated frame by
+//  frame using the surrounding suitable pixels.
+//
+//  A few words about file size: MPEG is a motion compensation compression,
+//  which means that if a region of a past frame is shown again at the same
+//  place or somewhere else this region is referenced instead of encoded again.
+//  This means that in our case (almost all frames are identical!) the
+//  increase of file size is far from linear with the number of encoded events!
+//
+//
+//  Input:
+//   MGeomCam
+//   MRawRunHeader
+//   MRawEvtHeader
+//   MSignalCam
+//   MBadPixelsCam
+//   MMovieData
 //
 /////////////////////////////////////////////////////////////////////////////
@@ -91,7 +97,4 @@
 #include "MGeomCam.h"
 #include "MGeomPix.h"
-
-#include "MPedestalCam.h"
-#include "MPedestalPix.h"
 
 #include "MH.h"
@@ -117,5 +120,5 @@
 //
 MMovieWrite::MMovieWrite(const char *name, const char *title)
-    : fPipe(0), fTargetLength(5), fThreshold(2), fNumEvents(1000), fFilename("movie.mpg")
+    : fPipe(0), fTargetLength(5), fThreshold(2), fNumEvents(25000), fFilename("movie.mpg")
 {
     fName  = name  ? name  : "MMovieWrite";
@@ -172,12 +175,16 @@
 
     fPipe = gSystem->OpenPipe(name, "w");
-    if (fPipe)
-        return kTRUE;
-
-    *fLog << err;
-    *fLog << "Pipe: " << name << endl;
-    *fLog << "Couldn't open pipe... aborting." << endl;
-    CheckPipe();
-    return kFALSE;
+    if (!fPipe)
+    {
+        *fLog << err;
+        *fLog << "Pipe: " << name << endl;
+        *fLog << "Couldn't open pipe... aborting." << endl;
+        CheckPipe();
+        return kFALSE;
+    }
+
+    *fLog << inf << "Setup pipe to ppmtoy4m and mpeg2enc to encode " << fFilename << "." << endl;
+
+    return kTRUE;
 
     //  1: 37M name += "-f 9 -E 40 -H -4 1 -2 1 --dualprime-mpeg2";
@@ -229,5 +236,4 @@
 //   - MSignalCam
 //   - MBadPixelsCam
-//   - MPedestalFundamental [MPedestalCam]
 //   - MMovieData
 //
@@ -265,10 +271,4 @@
     {
         *fLog << err << "MBadPixelsCam not found ... aborting." << endl;
-        return kFALSE;
-    }
-    fPed = (MPedestalCam*)plist->FindObject("MPedestalFundamental", "MPedestalCam");
-    if (!fPed)
-    {
-        *fLog << err << "MPedestalFundamental [MPedestalCam] not found ... aborting." << endl;
         return kFALSE;
     }
@@ -317,5 +317,5 @@
 // and everything above gets colors.
 //
-void MMovieWrite::SetPalette(Double_t rms, const TH1 &h) const
+Int_t MMovieWrite::SetPalette(Double_t rms, const TH1 &h) const
 {
     const Double_t min = h.GetMinimum();
@@ -335,5 +335,6 @@
 
     TArrayI col(99);
-    gStyle->CreateGradientColorTable(6, s, r, g, b, col.GetSize());
+
+    const Int_t rc = gStyle->CreateGradientColorTable(6, s, r, g, b, col.GetSize());
 
     // --- Overwrite the 'underflow' bin with white ---
@@ -347,4 +348,22 @@
 
     gStyle->SetPalette(col.GetSize(), col.GetArray());
+
+    return rc;
+}
+
+// --------------------------------------------------------------------------
+//
+// The created colors are not overwritten and must be deleted manually
+// because having more than 32768 color in a palette will crash
+// gPad->PaintBox
+//
+void MMovieWrite::DeletePalette(Int_t colidx) const
+{
+    for (int i=0; i<99; i++)
+    {
+        TColor *col = gROOT->GetColor(colidx+i);
+        if (col)
+            delete col;
+    }
 }
 
@@ -563,21 +582,4 @@
 // --------------------------------------------------------------------------
 //
-// Return the median of the pedestal rms of all pixels with area index 0
-//
-Double_t MMovieWrite::GetMedianPedestalRms() const
-{
-    Int_t n = fCam->GetNumPixWithAidx(0);
-
-    MArrayF rms(n);
-
-    for (UInt_t i=0; i<fCam->GetNumPixels(); i++)
-        if ((*fCam)[i].GetAidx()==0)
-            rms[--n] = (*fPed)[i].GetPedestalRms();
-
-    return TMath::Median(rms.GetSize(), rms.GetArray());
-}
-
-// --------------------------------------------------------------------------
-//
 Bool_t MMovieWrite::Process(TH1 &h, TVirtualPad &c)
 {
@@ -589,5 +591,5 @@
     //const Float_t len    = (slices-2)/freq;               // [ns]  length of data stream in data-time
 
-    const Double_t rms = GetMedianPedestalRms();
+    const Double_t rms = fIn->GetMedianPedestalRms();
 
     h.SetMinimum(fIn->GetMax()-(fIn->GetMax()-rms)*99/98); // rms0
@@ -601,5 +603,5 @@
 
     // Set new adapted palette for further rendering
-    SetPalette(rms, h);
+    const Int_t colidx = SetPalette(rms, h);
 
     // Get the pad containing the camera with the movie
@@ -638,4 +640,6 @@
             return kFALSE;
     }
+
+    DeletePalette(colidx);
 
     cout << setw(3) << GetNumExecutions() << ": " << Form("%6.2f", (float)numframes/(slices-2)) << " f/sl " << slices << " " << numframes+1 << endl;
Index: trunk/MagicSoft/Mars/mmovie/MMovieWrite.h
===================================================================
--- trunk/MagicSoft/Mars/mmovie/MMovieWrite.h	(revision 8405)
+++ trunk/MagicSoft/Mars/mmovie/MMovieWrite.h	(revision 8406)
@@ -54,5 +54,6 @@
     void TreatBadPixels(TH1 &h) const;
 
-    void SetPalette(Double_t rms, const TH1 &h) const;
+    Int_t SetPalette(Double_t rms, const TH1 &h) const;
+    void DeletePalette(Int_t colidx) const;
 
     void UpdateImage(TASImage &img, TVirtualPad &pad);
