Index: trunk/MagicSoft/Mars/datacenter/macros/fillcamera.C
===================================================================
--- trunk/MagicSoft/Mars/datacenter/macros/fillcamera.C	(revision 8599)
+++ trunk/MagicSoft/Mars/datacenter/macros/fillcamera.C	(revision 8602)
@@ -16,7 +16,7 @@
 !
 !
-!   Author(s): Thomas Bretz, 11/2005 <mailto:tbretz@astro.uni-wuerzburg.de>
+!   Author(s): Thomas Bretz, 06/2007 <mailto:tbretz@astro.uni-wuerzburg.de>
 !   Author(s): Daniela Dorner, 11/2005 <mailto:dorner@astro.uni-wuerzburg.de>
-!   Author(s): Daniel Hoehne, 05/2007 <mailto:hoehne@astro.uni-wuerzburg.de>
+!   Author(s): Daniel Hoehne, 06/2007 <mailto:hoehne@astro.uni-wuerzburg.de>
 !
 !   Copyright: MAGIC Software Development, 2000-2007
@@ -36,7 +36,4 @@
 //
 // Returns 0 in case of failure and 1 in case of success.
-//
-//
-// At the moment the macro works but produces a segmentation violation.
 //
 //
@@ -59,5 +56,4 @@
 #include "MSQLServer.h"
 #include "MSQLMagic.h"
-#include "MGeomCamMagic.h"
 
 #include "MMcRunHeader.hxx"
@@ -73,43 +69,5 @@
 
 using namespace std;
-// --------------------------------------------------------------------------
-//
-// Checks whether an entry is already existing
-//
-Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, Int_t test)
-{
-    TString query(Form("SELECT %s FROM %s WHERE %s='%d'", column, table, column, test));
-    TSQLResult *res = serv.Query(query);
-    if (!res)
-        return kFALSE;
-
-    TSQLRow *row;
-
-    Bool_t rc = kFALSE;
-    while ((row=res->Next()))
-    {
-        if ((*row)[0])
-        {
-            rc = kTRUE;
-            break;
-        }
-    }
-
-    delete res;
-
-    return rc;
-}
-/*
-//
-// Function to transform zenithangle range to zbin (not needed any more)
-//
-Double_t ThetaToZBin(Double_t tmin, Double_t tmax)
-{
-    double result=TMath::Nint(100*(1-TMath::Cos(((tmin+tmax)/2)*TMath::DegToRad())));
-    return result;
-}
-*/
-
-//int Process(MSQLServer &serv, TString fname, Bool_t dummy)
+
 int Process(MSQLMagic &serv, TString fname, Bool_t dummy)
 {
@@ -118,238 +76,161 @@
     {
         cout << "ERROR - Could not find file " << fname << endl;
-        return 0;
-    }
-
-//
-// Get tree RunHeaders from file
-//
+        return 2;
+    }
+
+    //
+    // Get tree RunHeaders from file
+    //
     TTree *tree = dynamic_cast<TTree*>(file.Get("RunHeaders"));
     if (!tree)
     {
         cout << "ERROR - Tree RunHeaders not found in file " << fname << endl;
-        return 0;
-    }
-//
-// Get branch MMcCorsikaRunHeader from tree RunHeaders
-//
+        return 2;
+    }
+
+    //
+    // Get branch MMcCorsikaRunHeader from tree RunHeaders
+    //
     TBranch *b1 = tree->GetBranch("MMcCorsikaRunHeader.");
     if (!b1)
     {
         cout << "ERROR - Branch MMcCorsikaRunHeader. not found in file " << fname << endl;
-        return 0;
-    }
-
-    MMcCorsikaRunHeader *runheader1 = new MMcCorsikaRunHeader();
+        return 2;
+    }
+
+    MMcCorsikaRunHeader *runheader1 = 0;
     b1->SetAddress(&runheader1);
-//
-// Get branch MMcConfigRunHeader from tree RunHeaders
-//
+
+    //
+    // Get branch MMcConfigRunHeader from tree RunHeaders
+    //
     TBranch *b2 = tree->GetBranch("MMcConfigRunHeader.");
     if (!b2)
     {
         cout << "ERROR - Branch MMcConfigRunHeader. not found in file " << fname << endl;
-        return 0;
-    }
-
-    MMcConfigRunHeader *runheader2 = new MMcConfigRunHeader();
+        return 2;
+    }
+
+    MMcConfigRunHeader *runheader2 = 0;
     b2->SetAddress(&runheader2);
-//
-// Get branch MMcRunHeader from tree RunHeaders
-//
+
+    //
+    // Get branch MMcRunHeader from tree RunHeaders
+    //
     TBranch *b3 = tree->GetBranch("MMcRunHeader.");
     if (!b3)
     {
         cout << "ERROR - Branch MMcRunHeader. not found in file " << fname << endl;
-        return 0;
-    }
-
-    MMcRunHeader *runheader3 = new MMcRunHeader();
+        return 2;
+    }
+
+    MMcRunHeader *runheader3 = 0;
     b3->SetAddress(&runheader3);
-//
-// Get branch MMcFadcRunHeader from tree RunHeaders
-//
+
+    //
+    // Get branch MMcFadcRunHeader from tree RunHeaders
+    //
     TBranch *b4 = tree->GetBranch("MMcFadcHeader.");
     if (!b4)
     {
         cout << "ERROR - Branch MMcFadcHeader. not found in file " << fname << endl;
-        return 0;
-    }
-
-    MMcFadcHeader *fadcheader = new MMcFadcHeader();
+        return 2;
+    }
+
+    MMcFadcHeader *fadcheader = 0;
     b4->SetAddress(&fadcheader);
-//
-// Get branch MRawRunHearder from tree RunHeaders
-//
+
+    //
+    // Get branch MRawRunHearder from tree RunHeaders
+    //
     TBranch *b5 = tree->GetBranch("MRawRunHeader.");
     if (!b5)
     {
         cout << "ERROR - Branch MRawRunHeader. not found in file " << fname << endl;
-        return 0;
-    }
-
-    MRawRunHeader *rawheader = new MRawRunHeader();
+        return 2;
+    }
+
+    MRawRunHeader *rawheader = 0;
     b5->SetAddress(&rawheader);
 
     tree->GetEvent(0);
-//
-// Get tree Events from file
-//
+
+    //
+    // Get tree Events from file
+    //
     TTree *tree2 = dynamic_cast<TTree*>(file.Get("Events"));
     if (!tree2)
     {
         cout << "ERROR - Tree Events not found in file " << fname << endl;
-        return 0;
-    }
-//
-// Get branch MMcEvtBasic from tree Events
-//
+        return 2;
+    }
+
+    //
+    // Get branch MMcEvtBasic from tree Events
+    //
     TBranch *b6 = tree2->GetBranch("MMcEvtBasic.");
     if (!b6)
     {
         cout << "ERROR - Branch MMcEvtBasic. not found in file " << fname << endl;
-        return 0;
-    }
-
-    MMcEvtBasic *evtbasic = new MMcEvtBasic();
+        return 2;
+    }
+
+    MMcEvtBasic *evtbasic = 0;
     b6->SetAddress(&evtbasic);
 
     tree2->GetEvent(0);
 
-
-
-    Float_t emin = runheader1->GetELowLim();
-    TString elow = Form("%5.1f",emin);
-    Float_t emax = runheader1->GetEUppLim();
-    TString eupp = Form("%5.1f",emax);
-
-    Float_t slopespec  = runheader1->GetSlopeSpec();
-    TString slope      = Form("%5.1f",slopespec);
-
-    Float_t wobblemode = runheader1->GetWobbleMode();
-    TString wobble     = Form("%5.0f",wobblemode);
-
-    Float_t corsvers1  = runheader1->GetCorsikaVersion();
-    TString corsika1   = Form("%5.0f",corsvers1);
-
-    Float_t pointspread    = runheader2->GetPointSpread();
-    TString psf            = Form("%5.1f",pointspread);
-    Float_t pointspreadx   = runheader2->GetPointSpreadX();
-    TString psfx           = Form("%5.2f",pointspreadx);
-    Float_t pointspready   = runheader2->GetPointSpreadY();
-    TString psfy           = Form("%5.2f",pointspready);
-    Float_t pointspreadadd = TMath::Hypot(runheader2->GetPointSpreadX(), runheader2->GetPointSpread());
-    TString psfadd         = Form("%5.2f",pointspreadadd);
-
-    Float_t mispointingx = runheader2->GetMissPointingX();
-    TString misx         = Form("%5.2f",mispointingx);
-    Float_t mispointingy = runheader2->GetMissPointingY();
-    TString misy         = Form("%5.2f",mispointingy);
-
-    Float_t reflvers  = runheader3->GetReflVersion();
-    TString reflector = Form("%5.0f",reflvers);
-    Float_t camvers   = runheader3->GetCamVersion();
-    TString camera    = Form("%5.0f",camvers);
-
-    Float_t impactmax = runheader3->GetImpactMax();
-    TString imax      = Form("%5.1f",impactmax);
+    Double_t misptx       = runheader2->GetMissPointingX();
+    Double_t mispty       = runheader2->GetMissPointingY();
+    Double_t pointspreadx = runheader2->GetPointSpreadX();
+    Double_t tmin         = runheader3->GetShowerThetaMin();
+    Double_t tmax         = runheader3->GetShowerThetaMax();
+    UInt_t   numsimshow   = runheader3->GetNumSimulatedShowers();
+    UInt_t   numevents    = tree2->GetEntries();
+
+    TString elow      = Form("%5.1f", runheader1->GetELowLim());
+    TString eupp      = Form("%5.1f", runheader1->GetEUppLim());
+    TString slope     = Form("%5.1f", runheader1->GetSlopeSpec());
+    TString wobble    = Form("%5.0f", runheader1->GetWobbleMode());
+    TString corsika1  = Form("%5.0f", runheader1->GetCorsikaVersion());
+    TString psf       = Form("%5.1f", runheader2->GetPointSpread());
+    TString psfx      = Form("%5.2f", pointspreadx);
+    TString psfy      = Form("%5.2f", runheader2->GetPointSpreadY());
+    TString psfadd    = Form("%5.2f", TMath::Hypot(runheader2->GetPointSpreadX(), runheader2->GetPointSpread()));
+    TString mirrfrac  = Form("%5.2f", runheader2->GetMirrorFraction());
+    TString misx      = Form("%5.2f", misptx);
+    TString misy      = Form("%5.2f", mispty);
+    TString reflector = Form("%5.0f", runheader3->GetReflVersion());
+    TString camera    = Form("%5.0f", runheader3->GetCamVersion());
+    TString imax      = Form("%5.1f", runheader3->GetImpactMax());
+    TString numphe    = Form("%5.1f", runheader3->GetNumPheFromDNSB());
+    TString pmin      = Form("%5.1f", runheader3->GetShowerPhiMin());
+    TString pmax      = Form("%5.1f", runheader3->GetShowerPhiMax());
+    TString numss     = Form("%7.0i", numsimshow);
+    TString thetamin  = Form("%5.1f", tmin);
+    TString thetamax  = Form("%5.1f", tmax);
+    TString ped       = Form("%5.1f", fadcheader->GetPedestal(1));
+    TString low2high  = Form("%5.1f", fadcheader->GetLow2HighGain());
+    TString amplfadc  = Form("%5.1f", fadcheader->GetAmplitud());
+    TString amplfadco = Form("%5.1f", fadcheader->GetAmplitudOuter());
+    TString enoise    = Form("%5.1f", fadcheader->GetElecNoise(1));
+    TString dnoise    = Form("%5.1f", fadcheader->GetDigitalNoise(1));
 
     TH1I h("myhist", "", 1, -0.5, 0.5);
     tree2->Draw("MRawEvtData.GetNumPixels()>>myhist", "", "goff");
     h.SetDirectory(0);
-    UInt_t numtriggers = TMath::Nint(h.GetBinContent(2));
-    TString numtrig = Form("%7.0i",numtriggers);
-
-    UInt_t numsimshow = runheader3->GetNumSimulatedShowers();
-    TString numss     = Form("%7.0i",numsimshow);
-    UInt_t numevents  = tree2->GetEntries();
-    TString numevt    = Form("%7.0i",numevents);
-
-    Float_t numphednsb = runheader3->GetNumPheFromDNSB();
-    TString numphe    = Form("%5.1f",numphednsb);
-
-    Float_t pedestal     = fadcheader->GetPedestal(1);
-    TString ped          = Form("%5.1f",pedestal);
-    Float_t low2highgain = fadcheader->GetLow2HighGain();
-    TString low2high     = Form("%5.1f",low2highgain);
-
-    Float_t amplitude = fadcheader->GetAmplitud();
-    TString amplfadc  = Form("%5.1f",amplitude);
-    Float_t amplitudo = fadcheader->GetAmplitudOuter();
-    TString amplfadco = Form("%5.1f",amplitudo);
-
-    Float_t elecnoise = fadcheader->GetElecNoise(1);
-    TString enoise    = Form("%5.1f",elecnoise);
-    Float_t diginoise = fadcheader->GetDigitalNoise(1);
-    TString dnoise    = Form("%5.1f",diginoise);
-
-    Float_t phimin = runheader3->GetShowerPhiMin();
-    TString pmin   = Form("%5.1f",phimin);
-    Float_t phimax = runheader3->GetShowerPhiMax();
-    TString pmax   = Form("%5.1f",phimax);
-
-    Float_t particleid   = evtbasic->GetPartId();
-    TString partid       = Form("%5.0f",particleid);
-    TString partname     = evtbasic->GetParticleName();
-//    TString partname = Form("%5.1f",particlename);
-
-    Double_t tmin    = runheader3->GetShowerThetaMin();
-    TString thetamin = Form("%5.1f",tmin);
-    Double_t tmax    = runheader3->GetShowerThetaMax();
-    TString thetamax = Form("%5.1f",tmax);
-
-    Float_t mirrorfraction = runheader2->GetMirrorFraction();
-    TString mirrfrac       = Form("%5.2f",mirrorfraction);
-//    Float_t reflectivity   = runheader2->GetMirrors()->GetReflectivity()->GetArray();
-//    TString refl           = Form("%5.2f",reflectivity);
-
-
-//workaround for getting the spotsize in cm from PointSpreadX (for path)
-/*
-    Double_t psfx=runheader2->GetPointSpreadX();
-    cout << "psfx=" << psfx << endl;
-    psfx=psfx*10;
-    Int_t psfxint= (int) psfx;
-    psfx=psfxint;
-    psfx=psfx/10;
-    cout << "PSF for path=" << psfx << endl;
-*/
-
-
-
-
-/*
-//     Bestimmung von fakewobble aus file
-//     Kombination aus Wobble(0,1) und MissPoint
-    TString WobbleMod;
-
-    if (wobblemode != 0){
-        WobbleMod = "wobble";
-    }
-    else
-    {
-        if (mispointingx == 0 && mispointingy == 0){
-            WobbleMod = "nowobble";
-        }
-        else{
-            WobbleMod = "fakewobble";
-        }
-     }
-*/
-    /* Bestimmung von fakewobble aus file */
-    /* Kombination aus Wobble(0,1) und MissPoint */
-    TString WobbleMod;
-
-    if (wobblemode == 1){
-        WobbleMod = "Wobble";
-    }
-    else
-    {
-        if (mispointingx == 0 && mispointingy == 0){
-            WobbleMod = "On";
-        }
-        else{
-            WobbleMod = "Fake Wobble";            // als ObservationModeKEY 4 einfügen?
-        }
-     }
+
+    TString numtrig   = Form("%7.0i", TMath::Nint(h.GetBinContent(2)));
+    TString numevt    = Form("%7.0i", numevents);
+    TString partid    = Form("%5.0f", evtbasic->GetPartId());
+    TString partname  = evtbasic->GetParticleName();
+
+    // Bestimmung von fakewobble aus file
+    // Kombination aus Wobble(0,1) und MissPoint
+    TString wobblemod="Wobble";
+
+    // Fake Wobble: als ObservationModeKEY 4 einfügen?
+    if (wobblemod != 1)
+        wobblemod = misptx == 0 && mispty == 0 ? "On" : "Fake Wobble";
 
     Float_t pointsfx=TMath::Floor(pointspreadx*10);
@@ -357,91 +238,69 @@
     TString pointsfuncx=Form("%5.1f",pointsfx);
 
-//    Float_t cos1=TMath::DegToRad(tmin);
-//    Float_t cos2=TMath::DegToRad(tmax);
     Float_t zBin=TMath::Nint((1-((TMath::Cos(tmin*TMath::Pi()/180)+TMath::Cos(tmax*TMath::Pi()/180))/2))*100);
     zBin=TMath::Nint(zBin);
 
-// folgende Werte werden aus dem Pfadnamen gewonnen   !!!neue Pfadstuktur!!!
-//     RunNumber
+    // folgende Werte werden aus dem Pfadnamen gewonnen
+    // RunNumber
     TRegexp reg2("_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_");
     TString Run = fname(reg2);
     Int_t RunNum = atoi(Run.Data()+1);
-
-     if (RunNum < 1 || RunNum > 99999)
-     {
-         cout << "ERROR - RunNumber wrong value" << endl;
-         return 0;
-     }
-
-//     PointSpreadFunction
+    if (RunNum < 1 || RunNum > 99999)
+    {
+        cout << "ERROR - RunNumber wrong value" << endl;
+        return 2;
+    }
+
+    // PointSpreadFunction
     TRegexp reg4("/[12][0-9]/");
     TString pointsf = fname(reg4);
     Int_t Point = atoi(pointsf.Data()+1);
-
     if (Point < 0 || Point > 99)
     {
         cout << "ERROR - PointSpreadFunction wrong value" << endl;
-
-        return 0;
-    }
-
-//     zbin
+        return 2;
+    }
+
+    // zbin
     TRegexp reg1("/19[0-9][0-9]/");
     TString zbin = fname(reg1);
     Int_t ZBin  = atoi(zbin.Data()+3);
-
     if (ZBin < 0 || ZBin > 99)
     {
         cout << "ERROR - zbin wrong value" << endl;
-        return 0;
-    }
-
-// WobbleMode
+        return 2;
+    }
+
+    // WobbleMode
     TRegexp reg3("/0[0-9]/");	/*extrahiert '/0X'   mit X= 1-8 */
-    TString WM = fname(reg3);	/* weist WM den extrahierten Wert zu */
-    Int_t Wob = atoi(WM.Data()+1); /* schneidet fuehrenden '/' ab */
-
-     if (Wob < 1 || Wob > 8)
-     {
-         cout << "ERROR - ObservationMode wrong value" << endl;
-         return 0;
-     }
-
-
-    /* wandelt numerischen Wert in WobbleModus Bezeichnung um */
-
-     const TString wobbleModes[] = {
-        "",     	// 0 wird nicht verwendet, daher ein leeres Feld
-        "Gammawobble+",
-        "Gammanowobble0",
-        "GammawobbleHE+",
-        "GammanowobbleHE0",
-        "Gammawobble0",
-        "GammawobbleHE0",
-        "Gammadiffuse0"
-        "Protonnowobble0",
-    };
-
-    /* Umrechnung von WobbleModus Bezeichnung in 'wobble', 'nowobble', 'fakewobble' */
-    TString WobMode;	/* dieser Wert wird in 'MCDB' Tabelle 'WobbleMode' eingetragen */
-
-    if (Wob == 1 || Wob == 3){
-        WobMode = "Wobble";
-    }
-    if (Wob == 2 || Wob == 4 || Wob == 7 || Wob == 8){
-        WobMode = "On";
-    }
-    if (Wob == 5 || Wob == 6){
-        WobMode = "Fake Wobble";
-     }
-
-
-    /*    MGeomCamMagic m;
-    cout << fadcheader->GetAmplitud() << endl;
-    cout << fadcheader->GetCameraMean(m, 0)<<  endl;
-    cout << fadcheader->GetCameraMean(m, 1)<<  endl;
-*/
-
-    cout << "File " << fname << endl;
+    TString wm = fname(reg3);	/* weist WM den extrahierten Wert zu */
+    Int_t wob = atoi(wm.Data()+1); /* schneidet fuehrenden '/' ab */
+
+
+    // Umrechnung von WobbleModus Bezeichnung in 'wobble', 'nowobble', 'fakewobble'
+    TString wobmode;	// dieser Wert wird in 'MCDB' Tabelle 'WobbleMode' eingetragen
+    switch (wob)
+    {
+    case 1:
+    case 3:
+        wobmode = "Wobble";
+        break;
+
+    case 2:
+    case 4:
+    case 7:
+    case 8:
+        wobmode = "On";
+        break;
+
+    case 5:
+    case 6:
+        wobmode = "Fake Wobble";
+        break;
+
+    default:
+        cout << "ERROR - ObservationMode wrong value" << endl;
+        return 2;
+    }
 
 
@@ -451,309 +310,116 @@
     cout << "--- From File ---" << endl;
 
-//    cout << wobblemode << endl;
-//    cout << wobble << endl;
-    cout << endl;
-    cout << elow << " < E < " << eupp      << endl;
-    cout << "SpectralIndex  " << slope     << endl;
-    cout << "WobbleMode     " << WobbleMod << endl;
-    cout << "ObservationMode " << WobbleMod << endl;
-    cout << "CorsikaVer     " << corsika1  << endl;
-    cout << "ReflVer        " << reflector << endl;
-    cout << "CamVer         " << camera    << endl;
-    cout << "ParticleId     " << partid    << endl;
-    cout << "ParticleName  "  << partname  << endl;
-    cout << "PointSpread    " << psf       << endl;
-    cout << "PointSpreadXY  " << psfx      << " /" << psfy << endl;
-    cout << "AdditionPSF    " << psfadd << endl;
-    cout << "MispointingXY  " << misx << " /" << misy <<endl;
-    cout << "NumSimShowers  " << numss     << endl;
-    cout << "ImpactMax      " << imax      << endl;
-    cout << "NumEvents      " << numevt    << endl;
-    cout << "NumTriggers    " << numtrig   << endl;
-    cout << "NumPheFromDNSB " << numphe    << endl;
-    cout << "Pedestal       " << ped       << endl;
-    cout << "Low2HighGain   " << low2high  << endl;
-    cout << "AmplitudeFADC  " << amplfadc  << endl;
-    cout << "AmplFADCOuter  " << amplfadco << endl;
-    cout << "ElecNoise      " << enoise    << endl;
-    cout << "DigiNoise      " << dnoise    << endl;
-    cout << "PhiMin         " << pmin      << endl;
-    cout << "PhiMax         " << pmax      << endl;
-    cout << "ThetaMin       " << thetamin  << endl;
-    cout << "ThetaMax       " << thetamax  << endl;
-
-//    cout << "Zenith range=" << runheader3->GetShowerThetaMin() << "to" << runheader3->GetShowerThetaMax() << endl;
-    cout << "Zenith range      " << tmin << " to " << tmax << endl;
-
-//    cout << "zbin              " << ThetaToZBin(tmin,tmax) << endl;
-
-    cout << "MirrorFraction " << mirrfrac << endl;
-//    cout << "Reflectivity   " << refl     << endl;
-
-    cout << endl;
+    cout << endl;
+    cout << "Energy Range    " << elow << " < E < " << eupp      << endl;
+    cout << "SpectralIndex   " << slope     << endl;
+    cout << "ObservationMode " << wobblemod << endl;
+    cout << "CorsikaVer      " << corsika1  << endl;
+    cout << "ReflVer         " << reflector << endl;
+    cout << "CamVer          " << camera    << endl;
+    cout << "ParticleId      " << partid    << endl;
+    cout << "ParticleName    "  << partname  << endl;
+    cout << "PointSpread     " << psf       << endl;
+    cout << "PointSpreadXY   " << psfx      << " /" << psfy << endl;
+    cout << "AdditionPSF     " << psfadd << endl;
+    cout << "MispointingXY   " << misx << " /" << misy <<endl;
+    cout << "NumSimShowers   " << numss     << endl;
+    cout << "ImpactMax       " << imax      << endl;
+    cout << "NumEvents       " << numevt    << endl;
+    cout << "NumTriggers     " << numtrig   << endl;
+    cout << "NumPheFromDNSB  " << numphe    << endl;
+    cout << "Pedestal        " << ped       << endl;
+    cout << "Low2HighGain    " << low2high  << endl;
+    cout << "AmplitudeFADC   " << amplfadc  << endl;
+    cout << "AmplFADCOuter   " << amplfadco << endl;
+    cout << "ElecNoise       " << enoise    << endl;
+    cout << "DigiNoise       " << dnoise    << endl;
+    cout << "PhiMin          " << pmin      << endl;
+    cout << "PhiMax          " << pmax      << endl;
+    cout << "ThetaMin        " << thetamin  << endl;
+    cout << "ThetaMax        " << thetamax  << endl;
+    cout << "Zenith range    " << tmin << " to " << tmax << endl;
+    cout << "MirrorFraction  " << mirrfrac << endl;
+    cout << endl;
+
     cout << endl;
     cout << "--- key's from mcdb tables ---" << endl;
     cout << endl;
 
-
-//    Int_t corsikakey = QueryNameKEY(serv, dummy, "CorsikaVersion", Form("%d",CorVer));
-
-//    Int_t corsikakey = QueryNameKEY(serv, dummy, "CorsikaVersion", corsika1.Data());
     Int_t corsikakey = serv.QueryKeyOfName("CorsikaVersion", corsika1.Data());
+    Int_t reflectorkey = serv.QueryKeyOfName("ReflectorVersion", reflector.Data());
+    Int_t camerakey = serv.QueryKeyOfName("CameraVersion", camera.Data());
+    Int_t observationkey = serv.QueryKeyOfName("ObservationMode", wobblemod.Data());
+    Int_t particlekey = serv.QueryKeyOfName("MCParticle", partname.Data());
+
     cout << "corsikakey: " << corsikakey << endl;
-
-//    Int_t reflectorkey = QueryNameKEY(serv, dummy, "ReflectorVersion", reflector.Data());
-    Int_t reflectorkey = serv.QueryKeyOfName("ReflectorVersion", reflector.Data());
     cout << "reflectorkey: " << reflectorkey << endl;
-
-//    Int_t camerakey = QueryNameKEY(serv, dummy, "CameraVersion", camera.Data());
-    Int_t camerakey = serv.QueryKeyOfName("CameraVersion", camera.Data());
     cout << "camerakey: " << camerakey << endl;
-
-//    Int_t wobblekey = QueryNameKEY(serv, dummy, "WobbleMode",Form("%s",WobbleMod.Data()));
-    Int_t wobblekey = serv.QueryKeyOfName("WobbleMode", WobbleMod.Data());
-    cout << "wobblekey: " << wobblekey << endl;
-
-//    Int_t observationkey = QueryNameKEY(serv, dummy, "ObservationMode",Form("%s",WobbleMod.Data()));
-    Int_t observationkey = serv.QueryKeyOfName("ObservationMode", WobbleMod.Data());
     cout << "observationkey: " << observationkey << endl;
-
-//     Int_t particlekey = QueryNameKEY(serv, dummy, "MCParticle", Form("%s",partname.Data()));
-    Int_t particlekey = serv.QueryKeyOfName("MCParticle", partname.Data());
-     cout << "particlekey: " << particlekey << endl;
-    cout << endl;
-    cout << endl;
-
-//    TRegexp reg1("/Spot_[0123456789.]*/");
-//    TRegexp reg2("/[a-zA-Z]*wobble[a-zA-Z]*/");
-//    TRegexp reg3("_zbin[0-9]+_");
-
+    cout << "particlekey: " << particlekey << endl;
+
+    cout << endl;
+    cout << endl;
     cout << "--- From File ---" << endl;
     cout << endl;
-    cout << "WobbleMode     " << WobbleMod << endl;
-    cout << "PSF            " << pointsfuncx << endl;
-    cout << "zBin           " << zBin << endl;
+    cout << "WobbleMode      " << wobblemod << endl;
+    cout << "PSF             " << pointsfuncx << endl;
+    cout << "zBin            " << zBin << endl;
     cout << endl;
     cout << "--- From FileName ---" << endl;
     cout << endl;
-    cout << "WobbleMode       " << WobMode   << endl;
-    cout << "RunNum           " << RunNum    << endl;
-    cout << "PSF              " << Point     << endl;
-    cout << "ZBin             " << ZBin      << endl;
-    cout << "WobbleMode(Pfad) " << Wob       << endl;
-    cout << endl;
-    cout << "--- Check ---" << endl;
-    cout << endl;
-
-    if (WobbleMod!=WobMode){
+    cout << "WobbleMode      " << wobmode   << endl;
+    cout << "RunNum          " << RunNum    << endl;
+    cout << "PSF             " << Point     << endl;
+    cout << "ZBin            " << ZBin      << endl;
+    cout << "WobbleMode(dir) " << wob       << endl;
+    cout << endl;
+
+
+    if (wobblemod!=wobmode)
+    {
         cout << "Error, WobbleMode in file and filename are not the same" << endl;
-        return 0;
-    }
-    else{
-        cout << "WobbleMode correct" << endl;
-    }
-    if (pointsfx!=Point){
+        return 2;
+    }
+    if (pointsfx!=Point)
+    {
         cout << "Error, PSF in file and filename are not the same" << endl;
-        return 0;
-    }
-    else{
-        cout << "PSF correct" << endl;
-    }
-    if (zBin!=ZBin){
+        return 2;
+    }
+    if (zBin!=ZBin)
+    {
         cout << "Error, ZBin in file and filename are not the same" << endl;
-        return 0;
-    }
-    else{
-        cout << "ZBin correct" << endl;
-    }
-
-    delete runheader1;
-    delete runheader2;
-    delete runheader3;
-    delete fadcheader;
-    delete rawheader;
-    delete evtbasic;
-
-
-    TString query;
-
-    if (!ExistStr(serv, "fRunNumber", "MCRunData", RunNum ))
-    {
-        query = Form(" INSERT INTO MCRunData SET"
-                     " fELowLim=%s,"
-                     " fEUppLim=%s, "
-                     " fSlopeSpec=%s, "
-                     " fImpactMax=%s, "
-                     " fNumSimulatedShowers=%d, "
-                     " fNumEvents=%d, "
-                     " fNumPheFromDNSB=%s, "
-                     " fzbin=%d, "
-		     " fThetaMin=%s, "
-		     " fThetaMax=%s, "
-                     " fPointSpread=%s, "
-		     " fPointSpreadX=%s, "
-		     " fPointSpreadY=%s, "
-		     " fPedesMean=%s, "
-		     " fLow2HighGain=%s, "
-		     " fAmplFadc=%s, "
-		     " fAmplFadcOuter=%s, "
-		     " fElectricNoise=%s, "
-		     " fDigitalNoise=%s, "
-		     " fRunNumber=%d, "
-		     " fMisspointingX=%s, "
-		     " fMissPointingY=%s, "
-		     " fCorsikaVersionKEY =%d, "
-		     " fReflectorVersionKEY=%d, "
-		     " fCameraVersionKEY=%d, "
-		     " fWobbleModeKEY=%d, "
-		     " fObservationModeKEY=%d, "
-		     " fMCParticleKEY=%d, "
-		     " fSequenceFirst=0 ",
-                     elow.Data(), eupp.Data(), slope.Data(), imax.Data(), numsimshow, numevents, numphe.Data(),
-	   	     zBin, thetamin.Data(), thetamax.Data(), psf.Data(), psfx.Data(), psfy.Data(), ped.Data(), low2high.Data(),
-		     amplfadc.Data(), amplfadco.Data(), enoise.Data(), dnoise.Data(), RunNum,
-                     misx.Data(), misy.Data(), corsikakey, reflectorkey, camerakey, wobblekey, observationkey, particlekey  );
-
-    }
-
-//
-//  not yet implemented
-//
-
-/*
-    if (!ExistStr(serv, "fRunNumber", "MCRunData", RunNum ))
-    {
-    query = Form(" fELowLim=%s,"
-                 " fEUppLim=%s, "
-                 " fSlopeSpec=%s, "
-                 " fImpactMax=%s, "
-                 " fNumSimulatedShowers=%d, "
-                 " fNumEvents=%d, "
-                 " fNumPheFromDNSB=%s, "
-                 " fzbin=%d, "
-                 " fThetaMin=%s, "
-                 " fThetaMax=%s, "
-                 " fPointSpread=%s, "
-                 " fPointSpreadX=%s, "
-                 " fPointSpreadY=%s, "
-                 " fPedesMean=%s, "
-                 " fLow2HighGain=%s, "
-                 " fAmplFadc=%s, "
-                 " fAmplFadcOuter=%s, "
-                 " fElectricNoise=%s, "
-                 " fDigitalNoise=%s, "
-                 " fRunNumber=%d, "
-                 " fMisspointingX=%s, "
-                 " fMissPointingY=%s, "
-                 " fCorsikaVersionKEY =%d, "
-                 " fReflectorVersionKEY=%d, "
-                 " fCameraVersionKEY=%d, "
-                 " fWobbleModeKEY=%d, "
-                 " fObservationModeKEY=%d, "
-                 " fMCParticleKEY=%d, "
-                 " fSequenceFirst=0 ",
-                 elow.Data(), eupp.Data(), slope.Data(), imax.Data(), numsimshow, numevents, numphe.Data(),
-                 zBin, thetamin.Data(), thetamax.Data(), psf.Data(), psfx.Data(), psfy.Data(), ped.Data(), low2high.Data(),
-                 amplfadc.Data(), amplfadco.Data(), enoise.Data(), dnoise.Data(), RunNum,
-                 misx.Data(), misy.Data(), corsikakey, reflectorkey, camerakey, wobblekey, observationkey, particlekey  );
-    }
-
-    if (serv.Insert("MCRunData", query)==kFALSE)
-        return 2;
-*/
-
-
+        return 2;
+    }
+
+    TString vars =
+        Form("fELowLim=%s, fEUppLim=%s, fSlopeSpec=%s, "
+             "fImpactMax=%s, fNumSimulatedShowers=%d, fNumEvents=%d, "
+             "fNumPheFromDNSB=%s, fZBin=%d, fThetaMin=%s, "
+             "fThetaMax=%s, fPointSpread=%s, fPointSpreadX=%s, "
+             "fPointSpreadY=%s, fPedesMean=%s, fLow2HighGain=%s, ",
+             elow.Data(), eupp.Data(), slope.Data(), imax.Data(),
+             numsimshow, numevents, numphe.Data(),
+             zBin, thetamin.Data(), thetamax.Data(), psf.Data(),
+             psfx.Data(), psfy.Data(), ped.Data(), low2high.Data());
+    vars +=
+        Form("fAmplFadc=%s, fAmplFadcOuter=%s, fElectricNoise=%s, "
+             "fDigitalNoise=%s, fRunNumber=%d, fMisspointingX=%s, "
+             "fMissPointingY=%s, fCorsikaVersionKEY =%d, "
+             "fReflectorVersionKEY=%d, fCameraVersionKEY=%d, "
+             "fObservationModeKEY=%d, fMCParticleKEY=%d, "
+             "fSequenceFirst=0 ",
+             amplfadc.Data(), amplfadco.Data(), enoise.Data(),
+             dnoise.Data(), RunNum, misx.Data(), misy.Data(), corsikakey,
+             reflectorkey, camerakey, observationkey, particlekey);
+
+    // Comming soon (-1: dummy, 0: failed, 1: succeeded)
+    // return serv.InsertUpdate("MCRunData", "fRunNumber", RunData()+1, vars);
+
+    if (!serv.ExistStr("fRunNumber", "MCRunData", Run.Data()+1))
+        if (!serv.Insert("MCRunData", vars)==kFALSE)
+            return 2;
     else
-    {
-
-        query = Form(" UPDATE MCRunData SET "
-                     " fELowLim=%s,"
-                     " fEUppLim=%s, "
-                     " fSlopeSpec=%s, "
-                     " fImpactMax=%s, "
-                     " fNumSimulatedShowers=%d, "
-                     " fNumEvents=%d, "
-                     " fNumPheFromDNSB=%s, "
-                     " fzbin=%d, "
-		     " fThetaMin=%s, "
-		     " fThetaMax=%s, "
-                     " fPointSpread=%s, "
-		     " fPointSpreadX=%s, "
-		     " fPointSpreadY=%s, "
-		     " fPedesMean=%s, "
-		     " fLow2HighGain=%s, "
-		     " fAmplFadc=%s, "
-		     " fAmplFadcOuter=%s, "
-		     " fElectricNoise=%s, "
-		     " fDigitalNoise=%s, "
-		     " fMisspointingX=%s, "
-		     " fMissPointingY=%s, "
-		     " fCorsikaVersionKEY =%d, "
-		     " fReflectorVersionKEY=%d, "
-		     " fCameraVersionKEY=%d, "
-		     " fWobbleModeKEY=%d, "
-		     " fObservationModeKEY=%d, "
-		     " fMCParticleKEY=%d, "
-		     " WHERE fRunNumber=%d ",
-                     elow.Data(), eupp.Data(), slope.Data(), imax.Data(), numsimshow, numevents, numphe.Data(),
-	   	     zBin, thetamin.Data(), thetamax.Data(), psf.Data(), psfx.Data(), psfy.Data(), ped.Data(), low2high.Data(),
-		     amplfadc.Data(), amplfadco.Data(), enoise.Data(), dnoise.Data(),
-                     misx.Data(), misy.Data(), corsikakey, reflectorkey, camerakey, wobblekey, observationkey, particlekey, RunNum  );
-
-    }
-//
-//  not yet implemented
-//
-
-/*
-        query = Form(" fELowLim=%s,"
-                     " fEUppLim=%s, "
-                     " fSlopeSpec=%s, "
-                     " fImpactMax=%s, "
-                     " fNumSimulatedShowers=%d, "
-                     " fNumEvents=%d, "
-                     " fNumPheFromDNSB=%s, "
-                     " fzbin=%d, "
-		     " fThetaMin=%s, "
-		     " fThetaMax=%s, "
-                     " fPointSpread=%s, "
-		     " fPointSpreadX=%s, "
-		     " fPointSpreadY=%s, "
-		     " fPedesMean=%s, "
-		     " fLow2HighGain=%s, "
-		     " fAmplFadc=%s, "
-		     " fAmplFadcOuter=%s, "
-		     " fElectricNoise=%s, "
-		     " fDigitalNoise=%s, "
-		     " fMisspointingX=%s, "
-		     " fMissPointingY=%s, "
-		     " fCorsikaVersionKEY =%d, "
-		     " fReflectorVersionKEY=%d, "
-		     " fCameraVersionKEY=%d, "
-		     " fWobbleModeKEY=%d, "
-		     " fObservationModeKEY=%d, "
-		     " fMCParticleKEY=%d ",
-                     elow.Data(), eupp.Data(), slope.Data(), imax.Data(), numsimshow, numevents, numphe.Data(),
-	   	     zBin, thetamin.Data(), thetamax.Data(), psf.Data(), psfx.Data(), psfy.Data(), ped.Data(), low2high.Data(),
-		     amplfadc.Data(), amplfadco.Data(), enoise.Data(), dnoise.Data(),
-                     misx.Data(), misy.Data(), corsikakey, reflectorkey, camerakey, wobblekey, observationkey, particlekey );
-
-    }
-*/
-
-
-//    if (serv.Update("MCRunData", query, "fRunNumber")==kFALSE)
-//        return 2;
-
-
-    if (dummy)
-        return 0;
-
-    TSQLResult *res = serv.Query(query);
-    if (!res)
-    {
-        cout << "ERROR - Query failed: " << query << endl;
-        return 0;
-    }
+        if (!serv.Update("MCRunData", vars, Form("fRunNumber=%d", Run))==kFALSE)
+            return 2;
 
     return 1;
@@ -764,10 +430,9 @@
     TEnv env("sql.rc");
 
-//    MSQLServer serv(env);
     MSQLMagic serv(env);
     if (!serv.IsConnected())
     {
         cout << "ERROR - Connection to database failed." << endl;
-        return 2;
+        return 0;
     }
 
@@ -776,5 +441,5 @@
     cout << endl;
     cout << "fillcamera" << endl;
-    cout << "---------" << endl;
+    cout << "----------" << endl;
     cout << endl;
     cout << "Connected to " << serv.GetName() << endl;
@@ -783,4 +448,3 @@
 
     return Process(serv, fname, dummy);
-
 }
Index: trunk/MagicSoft/Mars/datacenter/scripts/linkmc
===================================================================
--- trunk/MagicSoft/Mars/datacenter/scripts/linkmc	(revision 8599)
+++ trunk/MagicSoft/Mars/datacenter/scripts/linkmc	(revision 8602)
@@ -19,4 +19,5 @@
 #
 #   Author(s): Daniela Dorner  12/2005 <mailto:dorner@astro.uni-wuerzburg.de>
+#   Author(s): Daniel Hoehne   06/2007 <mailto:hoehne@astro.uni-wuerzburg.de>
 #
 #   Copyright: MAGIC Software Development, 2000-2007
@@ -28,4 +29,6 @@
 # to a structure from which the files can be processed more easily with the
 # automatic analysis. 
+# Furthermore the content of the files is filled in the database running
+# fillcamera.C.
 # This script is not yet running automatically. 
 #
@@ -38,7 +41,10 @@
 # /magic/montecarlo/rawfiles/README.txt
 #
-# First the data files are linked and then in each new directory also the 
-# pedestal and calibration file is linked. 
-#
+# First a file is linked and then fillcamera.C is executed. In case of an 
+# error in fillcamera.C the link is removed.
+# In each new directory also the pedestal and calibration file is linked. 
+#
+# ========================================================================
+
 
 source `dirname $0`/sourcefile
@@ -48,5 +54,5 @@
 set -C
 
-scriptlog=$runlogpath/$progam-`date +%F`.log
+scriptlog=$runlogpath/$program-`date +%F`.log
 date >> $scriptlog 2>&1
 
@@ -70,6 +76,6 @@
 
 # observation modes 
-modes=("" "Gammawobble+" "Gammanowobble0" "GammawobbleHE+" "GammanowobbleHE0" "Gammawobble0" "GammawobbleHE0" "Gammadiffuse0" "Protonnowobble0" )
-#be carful: 
+modes=("" "Gammawobble+" "Gammanowobble0" "GammawobbleHE+" "GammanowobbleHE0" "Gammawobble0" "GammawobbleHE0" "Gammadiffuse0" "Protonnowobble0" "ProtonnowobbleHE0")
+# be careful: 
 # w- not yet foreseen in this script
 
@@ -107,18 +113,23 @@
 
 
-#get runnumber
+
 runno=`cat $next`
 
 #get files, which have to be linked
-camfiles=`find $mccampath -type f | grep -v Cal_and_Ped`
+camfiles=`find $mccampath -type f | grep -v Cal_and_Ped | sort`
 
 printprocesslog "INFO linking new camerafiles starting with runno $runno"
 for camfile in ${camfiles[@]}
 do 
+
+
    #continue, if file is already linked 
-   if grep $camfile $processed >> $scriptlog 2>&1
+   if find $mcpath/* -lname '$mccampath*.root' -printf %l'\n' | grep $camfile >> $scriptlog 2>&1
+#   if grep $camfile $processed >> $scriptlog 2>&1
    then 
       continue
    fi
+
+
    printprocesslog "INFO linking $file"
    file=`basename $camfile` #filename
@@ -129,8 +140,8 @@
    wobble=`echo $file | cut -d_ -f6 | cut -c 2` #mode from filename
    particle=`echo $file | cut -d_ -f1` #particle type from filename
-   psf=`echo $camfile | cut -d/ -f5 | cut -c 6,8` #psf from path
-   
-   particledir=`echo $camfile | cut -d/ -f4` #particletype from path
-   wobbledir=`echo $camfile | cut -d/ -f6` #mode from path
+   psf=`echo $camfile | cut -d/ -f6 | cut -c 6,8` #psf from path
+   
+   particledir=`echo $camfile | cut -d/ -f5` #particletype from path
+   wobbledir=`echo $camfile | cut -d/ -f7` #mode from path
    
    #build mode name
@@ -167,8 +178,34 @@
    echo $runno >| $next 
 
+   linkmclogpath=$logpath/$program/$no2
+   makedir $linkmclogpath >> $scriptlog 2>&1
+   linkmclog=$linkmclogpath/$program-$no2.log
+
    #link file
+   echo "linking file $camfile"
    ln -sv $camfile $newfile >> $scriptlog 2>&1 
-   #add filename to processed file
-   echo $camfile >> $processed
+
+   # run fillcamera.C. In case of success the camfile is written to .processed. In case of an error the link is removed
+   echo "run fillcamera for mc run $no2" >> $scriptlog 2>&1
+   check2=`root -q -b $macrospath/fillcamera.C+\("\"$newfile\""\,kTRUE\) | tee $linkmclog | intgrep`
+   case $check2 in
+      1)   echo " check2=$check2 -> everything ok " >> $scriptlog 2>&1 
+           printprocesslog "INFO done fillcamera successfully for mc run $no2"
+           # add filename to .processed
+           echo $camfile >> $processed
+           ;;
+      0)   echo " check2=$check2 -> no connection to db -> continue..." >> $scriptlog 2>&1 
+           printprocesslog "WARN connection to DB failed, removing link"
+           check="no"
+           # remove link
+           rm $newfile >> $scriptlog 2>&1
+           ;;
+      *)   echo " check2=$check2 -> ERROR -> step has to be repeated" >> $scriptlog 2>&1
+           printprocesslog "ERROR fillcamera failed for mc run $no2, removing link"
+           check=$check2
+           # remove link
+           rm $newfile >> $scriptlog 2>&1
+           ;;
+   esac
 
 done
@@ -178,5 +215,9 @@
 #get files
 pedfile=`find $mccampath/Cal_and_Ped -name *_P_*.root`
+
+
 calfile=`find $mccampath/Cal_and_Ped -name *_C_*.root`
+
+
 echo "calfile"$calfile >> $scriptlog 2>&1
 echo "pedfile"$pedfile >> $scriptlog 2>&1
@@ -186,4 +227,8 @@
 then 
    "too many files in the directory $mccampath/Cal_and_Ped -> exit" >> $scriptlog 2>&1
+
+
+   rm -v $lockfile >> $scriptlog 2>&1
+   exit
    printprocesslog "ERROR too many ped and cal files found in $mccampath/Cal_and_Ped"
    finish >> $scriptlog 2>&1
@@ -196,5 +241,5 @@
 do 
    #continue, if directory has already linked C and P run
-   cont=`ls $dir/*_0000[12]_[CP]_MonteCarlo_E.root 2>/dev/null | wc -w`
+   cont=`ls $dir/*_0000000[12]_[CP]_MonteCarlo_E.root 2>/dev/null | wc -w`
    if [ "$cont" == "2" ] >> $scriptlog 2>&1
    then 
