Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 4234)
+++ trunk/MagicSoft/Mars/Changelog	(revision 4235)
@@ -18,4 +18,15 @@
 
                                                  -*-*- END OF LINE -*-*-
+
+ 2004/05/28: Markus Gaug
+
+   * macros/calibration.C
+     - make the macro compilable with possibility to run it in batch 
+       mode. 
+
+   * msignal/MExtractor.cc
+     - put an additional check in case that the number of FADC slices 
+       in the run header is smaller or equal to 0.
+
 
  2004/05/27: Javier Rico
Index: trunk/MagicSoft/Mars/macros/calibration.C
===================================================================
--- trunk/MagicSoft/Mars/macros/calibration.C	(revision 4234)
+++ trunk/MagicSoft/Mars/macros/calibration.C	(revision 4235)
@@ -55,34 +55,70 @@
 //
 /////////////////////////////////////////////////////////////////////////////
-static const TString outpath = "./";
+#include "MJPedestal.h"
+#include "MJCalibration.h"
+#include "MJExtractCalibTest.h"
+#include "TObject.h"
+#include "TObjectTable.h"
+#include "MExtractFixedWindowPeakSearch.h"
+#include "MExtractSlidingWindow.h"
+#include "MExtractFixedWindow.h"
+#include "MExtractTimeHighestIntegral.h"
+#include "MExtractTimeFastSpline.h"
+#include "MRunIter.h"
+#include "MStatusDisplay.h"
+#include "TStyle.h"
+#include "MCalibrationQECam.h"
+#include "MHCalibrationTestCam.h"
+#include "MHCalibrationTestPix.h"
+#include "MBadPixelsCam.h"
+#include "MArgs.h"
+#include "MArray.h"
+#include "MLog.h"
+#include "MParContainer.h"
+
+using namespace std;
+
+static TString outpath = "./";
 static const TString inpath = "./";
-//static const TString inpath = "/home/rootdata/Calib/";
-//
-// Tell if you want to calibrate times:
-//
-static const  Bool_t useTimes = kTRUE;
 //
 // the default pedestal run for the calibration
 //
-static const Int_t   pedrun  = 26210;
+static const Int_t   pedrun  = 26851;
 //
 // the default start calibration run 
 //
-static const Int_t   calrun1 = 26209;
+static const Int_t   calrun1 = 26849;
 //
 // the default last calibration run (if 0, only one run is taken, otherwise consecutive runs 
 // between calrun1 and calrun2)
 //
-static const Int_t calrun2 = 0;
+static  const Int_t calrun2 = 0;
 //
 // A switch to output debugging information about Objects use
 //
-static const Bool_t debug = kFALSE;
-//
-void calibration(const Int_t prun=pedrun, const Int_t crun1=calrun1, const Int_t crun2=calrun2)
+static Bool_t debug = kFALSE;
+//
+// A switch to use the Blind Pixel
+//
+static Bool_t blindpix = kTRUE;
+//
+// A switch to use the PIN Diode 
+//
+static Bool_t pindiode = kFALSE;
+//
+// Tell if you want to calibrate times:
+//
+static Bool_t useTimes = kFALSE;
+//
+// Tell if you want to use the display:
+//
+static Bool_t useDisplay = kTRUE;
+//
+Int_t calibration(const Int_t prun=pedrun, 
+                  const Int_t crun1=calrun1, const Int_t crun2=calrun2)
 {
 
-
-  TObject::SetObjectStat(kTRUE);
+  if (debug)
+    TObject::SetObjectStat(kTRUE);
 
   //
@@ -91,10 +127,11 @@
   //  MExtractFixedWindowPeakSearch extractor;
   //  MExtractSlidingWindow  extractor;   
-   MExtractFixedWindow    extractor;   
+  MExtractFixedWindow extractor;   
 
   //
   // Set Ranges or Windows
   //
-   extractor.SetRange(3,14,3,14);
+  extractor.SetRange(0,15,3,14);
+   // extractor.SetRange(5,9 ,5,9 );
   //  extractor.SetWindows(8,8);
 
@@ -107,5 +144,5 @@
   // Set Ranges or Windows
   //
-  timeext.SetRange(2,12,4,14);
+  timeext.SetRange(1,14,3,14);
 
   MRunIter pruns;
@@ -119,10 +156,15 @@
     cruns.AddRuns(crun1,crun2,inpath);
 
-  gStyle->SetOptStat(1);
+  gStyle->SetOptStat(111111);
   gStyle->SetOptFit();
 
-  MStatusDisplay *display = new MStatusDisplay;
-  display->SetUpdateTime(3000);
-  display->Resize(850,700);
+  MStatusDisplay *display = NULL;
+
+  if (useDisplay)
+    {
+      display = new MStatusDisplay;
+      display->SetUpdateTime(3000);
+      display->Resize(850,700);
+    }
   
   /************************************/
@@ -132,11 +174,9 @@
   MCalibrationQECam qecam;
   MBadPixelsCam     badcam;
-  MGeomCamMagic     geomcam;
-  MGeomApply        geomapl;
   //
   // If you want to exclude pixels from the beginning, read 
   // an ascii-file with the corr. pixel numbers (see MBadPixelsCam)
   //
-//  badcam.AsciiRead("badpixels.dat");
+  //  badcam.AsciiRead("badpixels.dat");
 
   MJPedestal pedloop;
@@ -144,9 +184,10 @@
   pedloop.SetInput(&pruns);
   pedloop.SetOutputPath(outpath.Data());
-  pedloop.SetDisplay(display);
+  if (useDisplay)
+    pedloop.SetDisplay(display);
   pedloop.SetBadPixels(badcam);
 
   if (!pedloop.Process())
-    return;
+    return 1;
 
   /****************************************/
@@ -174,9 +215,33 @@
   calloop.SetInput(&cruns);
   calloop.SetOutputPath(outpath.Data());
-  calloop.SetDisplay(display);
+  if (useDisplay)
+    calloop.SetDisplay(display);
+  calloop.SetUseBlindPixel(blindpix);
+  calloop.SetUsePINDiode(pindiode);
   calloop.SetQECam(qecam);
   calloop.SetBadPixels(pedloop.GetBadPixels());
 
-  calloop.Process(pedloop.GetPedestalCam());
+  if (!calloop.Process(pedloop.GetPedestalCam()))
+    return 2;
+
+  //
+  // The next lines are the use the Print() function and have 
+  // all the results as ascii-tables:
+  //
+  if (debug)
+    {
+      MCalibrationChargeCam &chargecam   = calloop.GetCalibrationCam();
+      MCalibrationQECam     &nqecam      = calloop.GetQECam();
+      MBadPixelsCam         &badbad      = calloop.GetBadPixels();
+      chargecam.Print();
+      nqecam.Print();
+      badbad.Print();
+    }
+
+  gLog << endl;
+  gLog << "Mean number of photons from pulser Inner pixels (F-Factor Method): " 
+       << calloop.GetCalibrationCam().GetNumPhotonsFFactorMethod() 
+       << " +- " << calloop.GetCalibrationCam().GetNumPhotonsFFactorMethodErr() << endl;
+  gLog << endl;
 
   /********************************************************************/
@@ -190,13 +255,38 @@
   testloop.SetInput(&cruns);
   testloop.SetOutputPath(outpath);
-  testloop.SetDisplay(display);
+  if (useDisplay)
+    testloop.SetDisplay(display);
   testloop.SetBadPixels(calloop.GetBadPixels());
-  testloop.ProcessD(pedloop.GetPedestalCam(),calloop.GetCalibrationCam(),calloop.GetQECam());
+  
+  if (!testloop.ProcessD(pedloop.GetPedestalCam(),calloop.GetCalibrationCam(),calloop.GetQECam()))
+    return 3;
+
   if (useTimes)
-      testloop.ProcessT(pedloop.GetPedestalCam(),calloop.GetRelTimeCam());
-
-  TObject::SetObjectStat(kFALSE);
-  //
-  // List of useful containers:
+    if (!testloop.ProcessT(pedloop.GetPedestalCam(),calloop.GetRelTimeCam()))
+      return 4;
+
+  gLog << endl;
+  gLog << "Mean equiv. number of photons from cascades per mm^2 Inner pixels: " 
+       << testloop.GetTestCam().GetMeanMeanPhotPerArea(0) 
+       << " +- " << testloop.GetTestCam().GetRmsMeanPhotPerArea(0) << endl;
+  gLog << "Sigma equiv. number of photons from cascades per mm^2 Inner pixels: " 
+       << testloop.GetTestCam().GetMeanSigmaPhotPerArea(0) 
+       << " +- " << testloop.GetTestCam().GetRmsSigmaPhotPerArea(0) << endl;
+
+  gLog << endl;
+  gLog << "Mean equiv. number of photons from cascades per mm^2 Outer pixels: " 
+       << testloop.GetTestCam().GetMeanMeanPhotPerArea(1) 
+       << " +- " << testloop.GetTestCam().GetRmsMeanPhotPerArea(1) << endl;
+  gLog << "Sigma equiv. number of photons from cascades per mm^2 Outer pixels: " 
+       << testloop.GetTestCam().GetMeanSigmaPhotPerArea(1) 
+       << " +- " << testloop.GetTestCam().GetRmsSigmaPhotPerArea(1) << endl;
+  gLog << endl;  
+
+
+  if (debug)
+    TObject::SetObjectStat(kFALSE);
+
+  //
+  // Debugging at the end:
   // 
   if (debug)
@@ -206,4 +296,7 @@
   // List of useful containers:
   // 
+  MHCalibrationTestCam  &testcam     = testloop.GetTestCam();
+  testcam[100].DrawClone("events");
+
 /*
   MPedestalCam          &pedcam      = pedloop.GetPedestalCam();
@@ -214,5 +307,131 @@
   MHCalibrationTestTimeCam &testtime = testloop.GetTestTimeCam();
 */
+
+  //
+  // List of interesting plots:
+  // 
+/*
+  testcam.GetAverageHiGainArea(0).DrawClone();
+  testcam.GetAverageLoGainArea(0).DrawClone();
+  testcam.GetAverageHiGainArea(1).DrawClone();
+  testcam.GetAverageLoGainArea(1).DrawClone();
+
+  testcam.GetAverageHiGainSector(1).DrawClone();
+  testcam.GetAverageLoGainSector(1).DrawClone();
+  testcam.GetAverageHiGainSector(2).DrawClone();
+  testcam.GetAverageLoGainSector(2).DrawClone();
+  testcam.GetAverageHiGainSector(3).DrawClone();
+  testcam.GetAverageLoGainSector(3).DrawClone();
+  testcam.GetAverageHiGainSector(4).DrawClone();
+  testcam.GetAverageLoGainSector(4).DrawClone();
+  testcam.GetAverageHiGainSector(5).DrawClone();
+  testcam.GetAverageLoGainSector(5).DrawClone();
+  testcam.GetAverageHiGainSector(6).DrawClone();
+  testcam.GetAverageLoGainSector(6).DrawClone();
+*/
+  return 0;
+
 }
 
-
+static void Usage()
+{
+    gLog << endl;
+    gLog << "Usage:" << endl;
+    gLog << endl;
+    gLog << "   calibration [ped.run nr.] [first cal.run nr.] [last cal.run nr.]" << endl ;
+    gLog << endl;
+    gLog << "   ped.run.nr:        Run number of the pedestal file." << endl;
+    gLog << "   first cal.run nr.: Run number of the first calibration file." << endl;
+    gLog << "   last  cal.run nr.: Run number of the last  calibration file." << endl;
+    gLog << endl;
+    gLog << "All calibration runs between (first cal.run nr.) and (last  cal.run nr.) will be used" << endl;
+    gLog << "If last.cal.run.nr is 0 (default), only one calibration run is taken"                  << endl;
+    gLog << endl;
+    gLog << "Additional Options: " << endl;
+    gLog << "     --inpath=#          Find the data in inpath"                      << endl;
+    gLog << "     --outpath=#         Write the output containers to outpath"       << endl;
+    gLog << "     --debug             Use the TObjectTable for debugging    "       << endl;
+    gLog << "                             and write out the pixels as ascii tables" << endl;
+    gLog << "     --useTimes          Calibrate the relative arrival times"         << endl;
+    gLog << "     --skipBlindPix      Skip the blind pixel calibration"             << endl;
+    gLog << "     --skipPINDiode      Skip the PIN Diode   calibration"             << endl;
+}
+
+
+int main(int argc, char **argv)
+{
+  //
+  // Evaluate arguments
+  //
+  MArgs arg(argc, argv);
+  
+  if (arg.HasOnly("-?") || arg.HasOnly("-h") || arg.HasOnly("--help"))
+    {
+      Usage();
+      return -1;
+    }
+  
+  debug    = arg.HasOnlyAndRemove("--debug")    || arg.HasOnlyAndRemove("-d");
+  useTimes = arg.HasOnlyAndRemove("--useTimes") || arg.HasOnlyAndRemove("-t");
+  blindpix = !(arg.HasOnlyAndRemove("--skipBlindPix"));
+  pindiode = !(arg.HasOnlyAndRemove("--skipPINDiode"));
+
+  if (arg.HasOption("--inpath="))
+    inpath = arg.GetStringAndRemove("--inpath=");
+
+  if (arg.HasOption("--outpath="))
+    outpath = arg.GetStringAndRemove("--outpath=");
+
+  //
+  // check for the right usage of the program
+  //
+  if (arg.GetNumArguments()>4)
+    {
+      Usage();
+      return -1;
+    }
+
+  //
+  // Initialize Non-GUI (batch) mode
+  //
+  gROOT->SetBatch();
+  
+  //
+  // Switch off the display
+  //
+  useDisplay = kFALSE;
+
+
+  //
+  // check for the arguments
+  //
+  Int_t pedr  = 0;
+  Int_t calr1 = 0;
+  Int_t calr2 = 0;
+
+  const Int_t nargs = arg.GetNumArguments();
+
+  if (nargs>=3)
+    {
+      pedr = arg.GetArgumentInt(0);
+      calr1 = arg.GetArgumentInt(1);
+      calr2 = arg.GetArgumentInt(2);
+      return calibration(pedr,calr1,calr2);
+    }
+
+  if (nargs>=2)
+    {
+      pedr = arg.GetArgumentInt(0);
+      calr1 = arg.GetArgumentInt(1);
+      return calibration(pedr,calr1);
+    }
+
+  if (nargs>=1)
+    {
+      pedr = arg.GetArgumentInt(0);
+      gLog << "PEDR: " << pedr << endl;
+      return calibration(pedr);
+    }
+
+  return calibration();
+}
Index: trunk/MagicSoft/Mars/msignal/MExtractor.cc
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractor.cc	(revision 4234)
+++ trunk/MagicSoft/Mars/msignal/MExtractor.cc	(revision 4235)
@@ -181,4 +181,8 @@
   Int_t lastdesired   = (Int_t)(fLoGainLast);
   Int_t lastavailable = (Int_t)fRunHeader->GetNumSamplesLoGain()-1;
+
+  if (lastavailable < 0)
+    *fLog << warn << GetDescriptor() 
+          << ": WARNING: Number of available Low-Gain Slices is smaller than or equal zero!" << endl;
   
   if (lastdesired > lastavailable)
@@ -197,4 +201,11 @@
   lastavailable = (Int_t)fRunHeader->GetNumSamplesHiGain()-1;
   
+  if (lastavailable < 0)
+    {
+      *fLog << err << GetDescriptor() 
+            << ": ERROR: Number of available High-Gain Slices is smaller than or equal zero!" << endl;
+      return kFALSE;
+    }
+
   if (lastdesired > lastavailable)
     {
