Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 4869)
+++ trunk/MagicSoft/Mars/Changelog	(revision 4870)
@@ -20,8 +20,25 @@
                                                  -*-*- END OF LINE -*-*-
 
+ 2004/09/06: Thomas Bretz
+
+   * mjobs/MJCalibrateSignal.[h,cc]:
+     - read camera geometry from calib-file
+
+   * mjobs/MJCalibration.[h,cc]:
+     - write camera geometry to calib-file
+
+   * macros/sql/filldotraw.C, macros/sql/fillcalib.C:
+     - added
+
+   * macros/sql/filldot*.C
+     - fixed some comments
+
+
+
  2004/09/05: Markus Gaug
 
    * mcalib/MCalibColorSet.[h,cc]
      - finished setting of the asumed pulse pattern
+
 
 
@@ -59,4 +76,5 @@
      - new class derived from MCalibrationQECam which intialized the 
        cornings for the Magic camera
+
 
 
Index: trunk/MagicSoft/Mars/macros/sql/fillcalib.C
===================================================================
--- trunk/MagicSoft/Mars/macros/sql/fillcalib.C	(revision 4870)
+++ trunk/MagicSoft/Mars/macros/sql/fillcalib.C	(revision 4870)
@@ -0,0 +1,168 @@
+/* ======================================================================== *\
+!
+! *
+! * 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, 08/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
+!   Author(s): Daniela Dorner, 08/2004 <mailto:dorner@astro.uni-wuerzburg.de>
+!
+!   Copyright: MAGIC Software Development, 2000-2004
+!
+!
+\* ======================================================================== */
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// fillcalib.C
+// ===========
+//
+// This macro is used to read the calibartion-/callisto-output files.
+// These files are automatically called calib00000.root.
+//
+// From this file the MBadPixelsCam and the MGeomCam is extracted. If
+// the geometry isn't found MGeomCamMagic is used as a default.
+//
+// Usage:
+//   .x fillcalib.C("/data/MAGIC/Period014/calib00000.root", kTRUE)
+//
+// The second argument is the 'dummy-mode'. If it is kTRUE dummy-mode is
+// switched on and nothing will be written into the database. This is usefull
+// for tests.
+//
+// The corresponding sequence number is extracted from the filename...
+// FIXME: MSeqeuence should be stored in the calib-file?
+//
+// The macro can also be run without ACLiC but this is a lot slower...
+//
+// Remark: Running it from the commandline looks like this:
+//   root -q -l -b fillcalib.C+\(\"filename\"\,kFALSE\) 2>&1 | tee fillcalib.log
+//
+// Make sure, that database and password are corretly set in the macro.
+//
+// Returns 0 in case of failure and 1 in case of success.
+//
+/////////////////////////////////////////////////////////////////////////////
+#include <iostream>
+
+#include <TRegexp.h>
+
+#include <TFile.h>
+#include <TSQLResult.h>
+
+#include "MSQLServer.h"
+
+#include "MGeomCamMagic.h"
+#include "MBadPixelsCam.h"
+
+using namespace std;
+
+Bool_t Process(MSQLServer &serv, TString fname, Bool_t dummy)
+{
+    MBadPixelsCam badpix;
+
+    TFile file(fname, "READ");
+    if (badpix.Read("MBadPixelsCam")<=0)
+    {
+        cout << "ERROR - Reading of MBadPixelsCam failed." << endl;
+        return kFALSE;
+    }
+
+    MGeomCamMagic def;
+
+    MGeomCam *geom = (MGeomCam*)file.Get("MGeomCam");
+    if (!geom)
+    {
+        cout << "WARNING - Reading of MGeomCam failed... using default <MGeomCamMagic>" << endl;
+        geom = &def;
+    }
+
+    cout << "Camera Geometry: " << geom->ClassName() << endl;
+
+    const Short_t unsin  = badpix.GetNumUnsuitable(MBadPixelsPix::kUnsuitableRun, geom, 0);
+    const Short_t unsout = badpix.GetNumUnsuitable(MBadPixelsPix::kUnsuitableRun, geom, 1);
+
+    const Short_t unrin  = badpix.GetNumUnsuitable(MBadPixelsPix::kUnreliableRun, geom, 0);
+    const Short_t unrout = badpix.GetNumUnsuitable(MBadPixelsPix::kUnreliableRun, geom, 1);
+
+    const Short_t isoin  = badpix.GetNumIsolated(*geom, 0);
+    const Short_t isoout = badpix.GetNumIsolated(*geom, 1);
+
+    const Short_t clumax = badpix.GetNumMaxCluster(*geom);
+
+    if (unsin<0 || unsout<0 || unrin<0 || unrout<0 || isoin<0 || isoout<0 || clumax<0)
+        return kFALSE;
+
+    //     MHCamera hist(geom);
+    //     hist.SetCamContent(badpix, 1);
+    //     hist.DrawCopy();
+    //     hist.SetCamContent(badpix, 3);
+    //     hist.DrawCopy();
+
+    TString sequence = fname(TRegexp("calib[0-9]+[.]root$"));
+    if (sequence.IsNull())
+        return kTRUE;
+
+    Int_t seq = atoi(sequence.Data()+5);
+
+    cout << "Sequence #" << seq << endl;
+    cout << "  Unsuitable:   (i/o)  " << Form("%3d %3d", (int)unsin, (int)unsout) << endl; // Unbrauchbar
+    cout << "  Unreliable:   (i/o)  " << Form("%3d %3d", (int)unrin, (int)unrout) << endl; // Unzuverlaessig
+    cout << "  Isolated:     (i/o)  " << Form("%3d %3d", (int)isoin, (int)isoout) << endl; // Isolated (unbrauchbar)
+    cout << "  Max.Cluseter: (i/o)  " << Form("%3d", (int)clumax) << endl;                 // Max Cluster
+
+    if (dummy)
+        return kTRUE;
+
+    TString query = Form("INSERT MyMagic.Calibration SET"
+                         " fSequenceFirst=%d,"
+                         " fUnsuitableInner=%d, "
+                         " fUnsuitableOuter=%d, "
+                         " fUnreliableInner=%d, "
+                         " fUnreliableOuter=%d, "
+                         " fIsolatedInner=%d, "
+                         " fIsolatedOuter=%d, "
+                         " fIsolatedMaxCluster=%d",
+                         seq, (int)unsin, (int)unsout, (int)unrin,
+                         (int)unrout, (int)isoin, (int)isoout, (int)clumax);
+
+
+    TSQLResult *res = serv.Query(query);
+    if (!res)
+    {
+        cout << "ERROR - Query failed: " << query << endl;
+        return kFALSE;
+    }
+
+    return kTRUE;
+}
+
+void fillcallisto(TString fname, Bool_t dummy=kTRUE)
+{
+    MSQLServer serv("mysql://hercules:d99swMT!@localhost");
+    if (!serv.IsConnected())
+    {
+        cout << "ERROR - Connection to database failed." << endl;
+        return;
+    }
+
+    cout << "fillcalib" << endl;
+    cout << "---------" << endl;
+    cout << endl;
+    cout << "Connected to " << serv.GetName() << endl;
+    cout << "File: " << fname << endl;
+    cout << endl;
+
+    cout << (Process(serv, fname, dummy) ? "Done." : "failed!") << endl << endl;
+}
Index: trunk/MagicSoft/Mars/macros/sql/filldotraw.C
===================================================================
--- trunk/MagicSoft/Mars/macros/sql/filldotraw.C	(revision 4869)
+++ trunk/MagicSoft/Mars/macros/sql/filldotraw.C	(revision 4870)
@@ -46,5 +46,5 @@
 //
 // Remark: Running it from the commandline looks like this:
-//   root -q -l -b filldotrbk.C+\(\"path\"\,kFALSE\) 2>&1 | tee filldotrbk.log
+//   root -q -l -b filldotraw.C+\(\"filename\"\,kFALSE\) 2>&1 | tee filldotraw.log
 //
 // Make sure, that database and password are corretly set in the macro.
Index: trunk/MagicSoft/Mars/mjobs/MJCalibrateSignal.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJCalibrateSignal.cc	(revision 4869)
+++ trunk/MagicSoft/Mars/mjobs/MJCalibrateSignal.cc	(revision 4870)
@@ -50,4 +50,5 @@
 #include "MStatusDisplay.h"
 
+#include "MGeomCam.h"
 #include "MHCamEvent.h"
 #include "MPedestalCam.h"
@@ -115,5 +116,5 @@
                                           MCalibrationCam &qecam,
                                           MCalibrationCam &tmcam,
-                                          MBadPixelsCam &badpix, MTask* &ext1, MTask* &ext2) const
+                                          MBadPixelsCam &badpix, MTask* &ext1, MTask* &ext2, TString &geom) const
 {
     const TString fname = Form("%s/calib%06d.root", fPathIn.Data(), fSequence.GetSequence());
@@ -148,9 +149,4 @@
 
     TObject *o = file.Get("ExtractSignal");
-//    if (!o)
-//    {
-//        *fLog << err << dbginf << "ERROR - Unable to read ExtractSignal from file " << fname << endl;
-//        return kFALSE;
-//    }
     if (o && !o->InheritsFrom(MExtractor::Class()))
     {
@@ -161,9 +157,4 @@
 
     o = file.Get("ExtractTime");
-//    if (!o)
-//    {
-//        *fLog << err << dbginf << "ERROR - Unable to read ExtractTime from file " << fname << endl;
-//        return kFALSE;
-//    }
     if (o && !o->InheritsFrom(MExtractor::Class()))
     {
@@ -172,5 +163,4 @@
     }
     ext2 = o ? (MTask*)o->Clone() : NULL;
-
     if (!ext1 && !ext2)
     {
@@ -178,4 +168,12 @@
         return kFALSE;
     }
+
+    o = file.Get("MGeomCam");
+    if (o && !o->InheritsFrom(MGeomCam::Class()))
+    {
+        *fLog << err << dbginf << "ERROR - MGeomCam read from " << fname << " doesn't inherit from MGeomCam!" << endl;
+        return kFALSE;
+    }
+    geom = o ? o->ClassName() : "";
 
     return kTRUE;
@@ -227,6 +225,7 @@
     MTask *extractor1=0;
     MTask *extractor2=0;
-
-    if (!ReadCalibration(calcam, qecam, tmcam, badpix, extractor1, extractor2))
+    TString geom;
+
+    if (!ReadCalibration(calcam, qecam, tmcam, badpix, extractor1, extractor2, geom))
         return kFALSE;
 
@@ -249,4 +248,9 @@
     else
         *fLog << inf << "No Time Extractor: ExtractTime in file." << endl;
+
+    if (!geom.IsNull())
+        *fLog << inf << "Camera geometry found in file: " << geom.ClassName() << endl;
+    else
+        *fLog << inf << "No Camera geometry found using default <MGeomCamMagic>" << endl;
 
     // This is necessary for the case in which it is not in the files
@@ -278,5 +282,7 @@
     //read.AddFiles(fnamein);
 
-    MGeomApply             apply; // Only necessary to craete geometry
+    MGeomApply             apply; // Only necessary to create geometry
+    if (!geom.IsNull())
+        apply.SetGeometry(geom);
     MBadPixelsMerge        merge(&badpix);
     MPedCalcFromLoGain     pedlo;
Index: trunk/MagicSoft/Mars/mjobs/MJCalibrateSignal.h
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJCalibrateSignal.h	(revision 4869)
+++ trunk/MagicSoft/Mars/mjobs/MJCalibrateSignal.h	(revision 4870)
@@ -10,4 +10,5 @@
 class MTask;
 class MParList;
+class MGeomCam;
 class MSequence;
 class MPedestalCam;
@@ -24,5 +25,6 @@
     Bool_t ReadCalibration(MCalibrationCam &calcam,
                            MCalibrationCam &qecam, MCalibrationCam &tmcam,
-                           MBadPixelsCam &badpix, MTask* &ext1, MTask* &ext2) const;
+                           MBadPixelsCam &badpix, MTask* &ext1, MTask* &ext2,
+                           TString &geom) const;
 
 public:
Index: trunk/MagicSoft/Mars/mjobs/MJCalibration.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJCalibration.cc	(revision 4869)
+++ trunk/MagicSoft/Mars/mjobs/MJCalibration.cc	(revision 4870)
@@ -191,5 +191,5 @@
     : fRuns(0), fExtractor(NULL), fTimeExtractor(NULL),
       fColor(MCalibrationCam::kNONE), fDisplayType(kNormalDisplay),
-      fRelTimes(kFALSE), fDataCheck(kFALSE), fDebug(kFALSE)
+      fRelTimes(kFALSE), fDataCheck(kFALSE), fDebug(kFALSE), fGeometry("MGeomCamMagic")
 {
     fName  = name  ? name  : "MJCalibration";
@@ -1337,4 +1337,5 @@
 //   MJCalibration.UseBlindPixel: yes,no
 //   MJCalibration.UsePINDiode: yes,no
+//   MJCalibration.Geometry: MGeomCamMagic, MGeomCamECO1000
 //
 // For more details see the class description and the corresponding Getters
@@ -1360,4 +1361,5 @@
     SetUseBlindPixel(GetEnv("UseBlindPixel", IsUseBlindPixel()));
     SetUsePINDiode(GetEnv("UsePINDiode", IsUsePINDiode()));
+    SetGeometry(GetEnv("Geometry", fGeometry));
 
     return kTRUE;
@@ -1552,4 +1554,5 @@
 
     MGeomApply               apply;
+    apply.SetGeometry(fGeometry);
     MBadPixelsMerge          merge(&fBadPixels);
     MExtractPINDiode         pinext;
@@ -1682,6 +1685,13 @@
     // TOO MANY DAYS TRYING TO FIND THIS REASON!
     //
+    // There is NO BUG-REPORT! And no code has been deleted! And
+    // there is NO hint who wrote this work around and should
+    // be informed! Maybe the author of a WORKAROUND should contact
+    // the author of a class which he/she identified not working
+    // correctly instead of vice versa!
+    //
     if (fDisplay)
     {
+        /*
       if (IsUsePINDiode())
         {
@@ -1707,10 +1717,10 @@
             (MHCalibrationChargeCam*)plist.FindObject("MHCalibrationChargeCam");
           cam->DrawClone(Form("nonew %s",drawoption.Data()));
-        }
+        } */
     }
 
     DisplayResult(plist);
 
-    if (!WriteResult())
+    if (!WriteResult(plist.FindObject("MGeomCam")))
         return kFALSE;
 
@@ -1862,5 +1872,5 @@
 // - MBadPixelsCam
 //
-Bool_t MJCalibration::WriteResult()
+Bool_t MJCalibration::WriteResult(TObject *geom)
 {
     if (fPathOut.IsNull())
@@ -1925,4 +1935,16 @@
     }
     *fLog << inf << "ok." << endl;
+
+    if (!geom)
+        *fLog << warn << " - WARNING: MGeomCam... not found!" << endl;
+    {
+        *fLog << inf << " - MGeomCam..." << flush;
+        if (geom->Write()<=0)
+        {
+            *fLog << err << "Unable to write MGeomCam to " << oname << endl;
+            return kFALSE;
+        }
+        *fLog << inf << "ok." << endl;
+    }
 
     if (fRelTimes)
Index: trunk/MagicSoft/Mars/mjobs/MJCalibration.h
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJCalibration.h	(revision 4869)
+++ trunk/MagicSoft/Mars/mjobs/MJCalibration.h	(revision 4870)
@@ -100,4 +100,6 @@
   Bool_t fDataCheck;                                   // Flag if the data check is run on raw data
   Bool_t fDebug;                                       // Flag if debug option is passed onto cal. classes
+
+  TString fGeometry;
   
   void   DisplayResult        ( MParList &plist );
@@ -108,5 +110,5 @@
   void   FixDataCheckHist     ( TH1D     *hist  ) const;
   
-  Bool_t WriteResult();
+  Bool_t WriteResult(TObject *geom);
   Bool_t WriteEventloop(MEvtLoop &evtloop) const;
   Bool_t WriteTasks(MTask *t1, MTask *t2) const;
@@ -142,4 +144,7 @@
   void SetDataCheckDisplay() { fDisplayType = kDataCheckDisplay; }
   void SetNormalDisplay()    { fDisplayType = kNormalDisplay;    }
+
+  // Camera Geomtry
+  void SetGeometry(const char *geom) { fGeometry = geom; }
   
   // Rel. Time
