Index: trunk/MagicSoft/Mars/datacenter/macros/fillcamera.C
===================================================================
--- trunk/MagicSoft/Mars/datacenter/macros/fillcamera.C	(revision 9139)
+++ trunk/MagicSoft/Mars/datacenter/macros/fillcamera.C	(revision 9144)
@@ -18,7 +18,7 @@
 !   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, 10/2007 <mailto:hoehne@astro.uni-wuerzburg.de>
+!   Author(s): Daniel Hoehne, 06/2008 <mailto:hoehne@astro.uni-wuerzburg.de>
 !
-!   Copyright: MAGIC Software Development, 2000-2007
+!   Copyright: MAGIC Software Development, 2000-2008
 !
 !
@@ -37,5 +37,8 @@
 //
 // Make sure, that database and password are corretly set in a resource
-// file called mcsql.rc and the resource file is found.
+// file called sql.rc and the resource file is found.
+// In order not to confuse the MC processing with the data processing,
+// both branches will be processed in two different Mars directories,
+// e.g. Mars-2.0 for data and Mars.MC for MCs.
 //
 // Returns 2 in case of failure and 1 in case of success.
@@ -171,4 +174,14 @@
     Double_t point        = runheader2->GetPointSpread();
     Double_t pointspreadx = runheader2->GetPointSpreadX();
+    Double_t addspotsize;
+    if (pointspreadx < 0.25)
+    {
+        addspotsize = 0;
+    }
+    else
+    {
+        addspotsize = TMath::Sqrt(pointspreadx*pointspreadx - 0.25);
+    }
+    Double_t realpsf      = TMath::Sqrt(pointspreadx*pointspreadx + 0.25);
     Double_t pointspready = runheader2->GetPointSpreadY();
     Double_t coneai       = runheader1->GetViewconeAngleInner();
@@ -184,12 +197,15 @@
 
     // some calculations: round the given values
-    pointspreadx=TMath::Floor(pointspreadx*100+0.5)/100;
-    pointspready=TMath::Floor(pointspready*100+0.5)/100;
-    point=TMath::Floor(point*10+0.5)/10;
-    coneai=TMath::Floor(coneai*10+0.5)/10;
-    coneao=TMath::Floor(coneao*10+0.5)/10;
-    spectrum=TMath::Floor(spectrum*10+0.5)/10;
+    pointspreadx = TMath::Floor(pointspreadx*100+0.5)/100;
+    addspotsize  = TMath::Floor(addspotsize*100+0.5)/100;
+    realpsf      = TMath::Floor(realpsf*100+0.5)/100;
+    pointspready = TMath::Floor(pointspready*100+0.5)/100;
+    point        = TMath::Floor(point*10+0.5)/10;
+    coneai       = TMath::Floor(coneai*10+0.5)/10;
+    coneao       = TMath::Floor(coneao*10+0.5)/10;
+    spectrum     = TMath::Floor(spectrum*10+0.5)/10;
 
     // Definition of strings
+    TString numslices = Form("%5.0i", rawheader->GetNumSamplesHiGain());
     TString elow      = Form("%5.1f", runheader1->GetELowLim());
     TString eupp      = Form("%5.1f", runheader1->GetEUppLim());
@@ -202,4 +218,6 @@
     TString psf       = Form("%5.1f", point);
     TString psfx      = Form("%5.2f", pointspreadx);
+    TString addspot   = Form("%5.2f", addspotsize);
+    TString rpsf      = Form("%5.2f", realpsf);
     TString psfy      = Form("%5.2f", pointspready);
     TString psfadd    = Form("%5.2f", TMath::Hypot(runheader2->GetPointSpreadX(), runheader2->GetPointSpread()));
@@ -247,7 +265,16 @@
 
     //get the psf value for the path for linking
-    Float_t pointsfuncx=pointspreadx*10;
-    pointsfuncx=TMath::Floor(pointsfuncx*10+0.5)/10;
-    Int_t pointsfx = TMath::Nint(pointsfuncx);
+    Float_t pointsfuncx = pointspreadx*10;
+    pointsfuncx         = TMath::Floor(pointsfuncx*10+0.5)/10;
+    Int_t pointsfx      = TMath::Nint(pointsfuncx);
+
+    Float_t aspotsize   = addspotsize*10;
+    aspotsize           = TMath::Floor(aspotsize*10+0.5)/10;
+    Int_t ass           = TMath::Nint(aspotsize);
+
+    Float_t realpointsf = realpsf*10;
+    realpointsf         = TMath::Floor(realpointsf*10+0.5)/10;
+    Int_t rpointsf = TMath::Nint(realpointsf);
+
 
     // Get zbin
@@ -264,4 +291,5 @@
     Int_t amplfadckey;
     Int_t psfkey;
+    Int_t spotsizekey;
     Int_t viewconeangleokey;
     Int_t spectrumkey;
@@ -299,4 +327,5 @@
         cout << "ElecNoise       " << enoise    << endl;
         cout << "DigiNoise       " << dnoise    << endl;
+        cout << "Num HiGainSlices " << numslices << endl;
         cout << "PhiMin          " << pmin      << endl;
         cout << "PhiMax          " << pmax      << endl;
@@ -312,4 +341,6 @@
         cout << "CamVer          " << camvers     << " -> " << "n/a" <<endl;
         cout << "PointSpreadX    " << psfx        << " -> " << "0" <<endl;
+        cout << "Add. SpotSize   " << addspot     << " -> " << "0" <<endl;
+        cout << "Real PSF        " << rpsf        << " -> " << "0" <<endl;
         cout << "PointSpreadY    " << psfy        << " -> " << "0" <<endl;
         cout << "MispointingX    " << misx        << " -> " << "0" <<endl;
@@ -319,14 +350,15 @@
         cout << endl;
 
-        corsikakey=1;
-        reflectorkey=1;
-        camerakey=1;
-        observationkey=1;
-        particlekey = 1;
-        atmomodelkey = serv.QueryKeyOfName("AtmosphericModel", atmomodel.Data());
-        amplfadckey = serv.QueryKeyOfName("AmplFadc", amplfadc.Data());
+        corsikakey     = 1;
+        reflectorkey   = 1;
+        camerakey      = 1;
+        observationkey = 1;
+        particlekey    = 1;
+        atmomodelkey   = serv.QueryKeyOfName("AtmosphericModel", atmomodel.Data());
+        amplfadckey    = serv.QueryKeyOfName("AmplFadc", amplfadc.Data());
         viewconeangleokey = serv.QueryKeyOfName("ViewconeAngleO", coneangleo.Data());
-        spectrumkey = 1;
-        psfkey = 1;
+        spectrumkey    = 1;
+        psfkey         = 1;
+        spotsizekey    = 1;
 
         if (corsvers==0)
@@ -355,18 +387,19 @@
         }
 
-        cout << "corsikakey: " << corsikakey << endl;
-        cout << "reflectorkey: " << reflectorkey << endl;
-        cout << "camerakey: " << camerakey << endl;
+        cout << "corsikakey:     " << corsikakey << endl;
+        cout << "reflectorkey:   " << reflectorkey << endl;
+        cout << "camerakey:      " << camerakey << endl;
         cout << "observationkey: " << observationkey << endl;
-        cout << "particlekey: " << particlekey << endl;
-        cout << "atmomodelkey: " << atmomodelkey << endl;
-        cout << "amplfadckey: " << amplfadckey << endl;
-        cout << "psfkey: " << psfkey << endl;
-        cout << "coneangleokey: " << viewconeangleokey << endl;
-        cout << "spectrumkey: " << spectrumkey << endl;
+        cout << "particlekey:    " << particlekey << endl;
+        cout << "atmomodelkey:   " << atmomodelkey << endl;
+        cout << "amplfadckey:    " << amplfadckey << endl;
+        cout << "psfkey:         " << psfkey << endl;
+        cout << "spotsizekey:    " << spotsizekey << endl;
+        cout << "coneangleokey:  " << viewconeangleokey << endl;
+        cout << "spectrumkey:    " << spectrumkey << endl;
         cout << endl;
 
         // For ped/cal: fixed values for psf and mispointing
-        vars = Form("fPointSpreadX=0, fPointSpreadY=0, fMissPointingX=0, fMissPointingY=0, ");
+        vars = Form("fPointSpreadX=0, fAdditionalSpotSize=0, fRealPointSpread=0, fPointSpreadY=0, fMissPointingX=0, fMissPointingY=0, ");
 
     }
@@ -387,7 +420,9 @@
         cout << "CamVer          " << camera    << endl;
         cout << "ParticleId      " << partid    << endl;
-        cout << "ParticleName    "  << partname  << endl;
+        cout << "ParticleName    " << partname  << endl;
         cout << "PointSpread     " << psf       << endl;
         cout << "PointSpreadX    " << pointspreadx << endl;
+        cout << "Add. SpotSize   " << addspot   << endl;
+        cout << "Real PSF        " << rpsf      << endl;
         cout << "PointSpreadXY   " << psfx      << " /" << psfy << endl;
 //        cout << "AdditionPSF     " << psfadd << endl;
@@ -407,4 +442,5 @@
         cout << "ElecNoise       " << enoise    << endl;
         cout << "DigiNoise       " << dnoise    << endl;
+        cout << "Num HiGainSlices " << numslices << endl;
         cout << "PhiMin          " << pmin      << endl;
         cout << "PhiMax          " << pmax      << endl;
@@ -418,14 +454,15 @@
         cout << endl;
 
-        corsikakey=1;
-        reflectorkey=1;
-        camerakey=1;
+        corsikakey     = 1;
+        reflectorkey   = 1;
+        camerakey      = 1;
         observationkey = serv.QueryKeyOfName("ObservationMode", wobblemod.Data());
-        particlekey = serv.QueryKeyOfName("MCParticle", partname.Data());
-        atmomodelkey = serv.QueryKeyOfName("AtmosphericModel", atmomodel.Data());
-        amplfadckey = serv.QueryKeyOfName("AmplFadc", amplfadc.Data());
+        particlekey    = serv.QueryKeyOfName("MCParticle", partname.Data());
+        atmomodelkey   = serv.QueryKeyOfName("AtmosphericModel", atmomodel.Data());
+        amplfadckey    = serv.QueryKeyOfName("AmplFadc", amplfadc.Data());
         viewconeangleokey = serv.QueryKeyOfName("ViewconeAngleO", coneangleo.Data());
-        psfkey = serv.QueryKeyOfName("PSF", psfx);
-        spectrumkey = serv.QueryKeyOfName("Spectrum", slope);
+        psfkey         = serv.QueryKeyOfName("PSF", rpsf);
+        spotsizekey    = serv.QueryKeyOfName("AddSpotSize", addspot);
+        spectrumkey    = serv.QueryKeyOfName("Spectrum", slope);
 
         if (corsvers==0)
@@ -454,19 +491,20 @@
         }
 
-        cout << "corsikakey: " << corsikakey << endl;
-        cout << "reflectorkey: " << reflectorkey << endl;
-        cout << "camerakey: " << camerakey << endl;
+        cout << "corsikakey:     " << corsikakey << endl;
+        cout << "reflectorkey:   " << reflectorkey << endl;
+        cout << "camerakey:      " << camerakey << endl;
         cout << "observationkey: " << observationkey << endl;
-        cout << "particlekey: " << particlekey << endl;
-        cout << "atmomodelkey: " << atmomodelkey << endl;
-        cout << "amplfadckey: " << amplfadckey << endl;
-        cout << "psfkey: " << psfkey << endl;
-        cout << "coneangleokey: " << viewconeangleokey << endl;
-        cout << "spectrumkey: " << spectrumkey << endl;
+        cout << "particlekey:    " << particlekey << endl;
+        cout << "atmomodelkey:   " << atmomodelkey << endl;
+        cout << "amplfadckey:    " << amplfadckey << endl;
+        cout << "psfkey:         " << psfkey << endl;
+        cout << "spotsizekey:    " << spotsizekey << endl;
+        cout << "coneangleokey:  " << viewconeangleokey << endl;
+        cout << "spectrumkey:    " << spectrumkey << endl;
         cout << endl;
 
         // For data runs the values are taken from the file
-        vars = Form("fPointSpreadX=%5.2f, fPointSpreadY=%5.2f, fMissPointingX=%s, fMissPointingY=%s, ",
-                    pointspreadx, pointspready, misx.Data(), misy.Data());
+        vars = Form("fPointSpreadX=%5.2f, fAdditionalSpotSize=%5.2f, fRealPointSpread=%5.2f, fPointSpreadY=%5.2f, fMissPointingX=%s, fMissPointingY=%s, ",
+                    pointspreadx, addspotsize, realpsf, pointspready, misx.Data(), misy.Data());
 
     }
@@ -488,4 +526,6 @@
     TSQLRow *row = 0;
     row = res->Next();
+    if (!row)
+        return 2;
     TString num=(*row)[0];
     Int_t RunNum=atoi(num.Data());
@@ -518,5 +558,5 @@
                  "fThetaMax=%s, fPointSpread=%s, "
                  "fPedesMean=%s, fLow2HighGain=%s, "
-                 "fAmplFadc=%s, fAmplFadcOuter=%s, ",
+                 "fAmplFadcInner=%s, fAmplFadcOuter=%s, ",
                  elow.Data(), eupp.Data(), spectrum,
                  imax.Data(), numsimshow, numevents,
@@ -528,17 +568,17 @@
             Form("fElectricNoise=%s, "
                  "fDigitalNoise=%s, fRunNumber=%d, "
-                 "fCorsikaVersionKEY =%d, "
+                 "fNumSlices=%s, fCorsikaVersionKEY=%d, "
                  "fReflectorVersionKEY=%d, fCameraVersionKEY=%d, "
                  "fObservationModeKEY=%d, fMCParticleKEY=%d, "
                  "fSequenceFirst=0, fNumTriggers=%d, fViewconeAngleInner=%5.2f, fViewconeAngleOuter=%5.2f, "
-                 "fAtmosphericModelKEY=%d, fAmplFadcKEY=%d, "
+                 "fAtmosphericModelKEY=%d, fAmplFadcKEY=%d, fAddSpotSizeKEY=%d, "
                  "fPSFKEY=%d, fViewconeAngleOKEY=%d, fSpectrumKEY=%d ",
                  enoise.Data(),
                  dnoise.Data(), RunNum,
-                 corsikakey,
+                 numslices.Data(), corsikakey,
                  reflectorkey, camerakey,
                  observationkey, particlekey,
                  ntrig, coneai, coneao,
-                 atmomodelkey, amplfadckey,
+                 atmomodelkey, amplfadckey, spotsizekey,
                  psfkey, viewconeangleokey, spectrumkey);
 
@@ -588,5 +628,5 @@
                      "fThetaMax=%s, fPointSpread=%s, "
                      "fPedesMean=%s, fLow2HighGain=%s, "
-                     "fAmplFadc=%s, fAmplFadcOuter=%s, ",
+                     "fAmplFadcInner=%s, fAmplFadcOuter=%s, ",
                      elow.Data(), eupp.Data(), spectrum,
                      imax.Data(), numsimshow, numevents,
@@ -598,17 +638,17 @@
                 Form("fElectricNoise=%s, "
                      "fDigitalNoise=%s, fRunNumber=%d, "
-                     "fCorsikaVersionKEY =%d, "
+                     "fNumSlices=%s, fCorsikaVersionKEY =%d, "
                      "fReflectorVersionKEY=%d, fCameraVersionKEY=%d, "
                      "fObservationModeKEY=%d, fMCParticleKEY=%d, "
                      "fSequenceFirst=0, fNumTriggers=%d, fViewconeAngleInner=%5.2f, fViewconeAngleOuter=%5.2f, "
-                     "fAtmosphericModelKEY=%d, fAmplFadcKEY=%d, "
+                     "fAtmosphericModelKEY=%d, fAmplFadcKEY=%d, fAddSpotSizeKEY=%d, "
                      "fPSFKEY=%d, fViewconeAngleOKEY=%d, fSpectrumKEY=%d ",
                      enoise.Data(),
                      dnoise.Data(), RunNum,
-                     corsikakey,
+                     numslices.Data(), corsikakey,
                      reflectorkey, camerakey,
                      observationkey, particlekey,
                      ntrig, coneai, coneao,
-                     atmomodelkey, amplfadckey,
+                     atmomodelkey, amplfadckey, spotsizekey,
                      psfkey, viewconeangleokey, spectrumkey);
 
@@ -656,5 +696,7 @@
     TString query3(Form("SELECT fMCParticleName, fSpectrum FROM MCRunData "
                         "LEFT JOIN MCParticle USING (fMCParticleKEY) "
+//                        "LEFT JOIN ON MCRunData.fMCParticleKEY=MCParticle.fMCParticleKEY "
                         "LEFT JOIN Spectrum USING (fSpectrumKEY) "
+//                        "LEFT JOIN Spectrum ON Spectrum.fSpectrumKEY=MCRunData.fSpectrumKEY "
                         "WHERE MCRunData.fRunNumber=%d;", RunNum));
     res = serv.Query(query3);
@@ -666,4 +708,7 @@
 
     row = res->Next();
+    if (!row)
+        return 2;
+
 
     // Definition of variables for linking the file
@@ -675,9 +720,10 @@
     TString mkdir;
     TString link;
+    TString linkname;
     TString RunNumber=Form("%08d",RunNum);
 
     delete res;
-    rawpath = Form("/magic/montecarlo/rawfiles/19%02d/%02d/%02d", zBin2, amplfadckey, pointsfx);
-//    rawpath = Form("/home/hoehne/Analyse/TestMonteCarlo/montecarlo/rawfiles/19%02d/%02d/%02d", zBin2, amplfadckey, pointsfx);
+    rawpath = Form("/magic/montecarlo/rawfiles/19%02d/%02d/%02d", zBin2, amplfadckey, ass);
+//    rawpath = Form("/home/hoehne/Analyse/TestMonteCarlo/montecarlo/rawfiles/19%02d/%02d/%02d", zBin2, amplfadckey, ass);
     mkdir = Form("mkdir -p -v %s", rawpath.Data());
 
@@ -732,7 +778,8 @@
     }
 
-    if (obsmode!=1)
-    {
-        link = Form("ln -sv %s %s/19%02d%02d%02d_%s_%s_%s%s%s_E.root", fname.Data(), rawpath.Data(), zBin2, amplfadckey, pointsfx, RunNumber.Data(), type.Data(), particle.Data(), spec.Data(), obsmode.Data());
+    if (observationkey!=1)
+    {
+        link = Form("ln -sv %s %s/19%02d%02d%02d_%s_%s_%s%s%s_E.root", fname.Data(), rawpath.Data(), zBin2, amplfadckey, ass, RunNumber.Data(), type.Data(), particle.Data(), spec.Data(), obsmode.Data());
+        linkname = Form("%s/19%02d%02d%02d_%s_%s_%s%s%s_E.root", rawpath.Data(), zBin2, amplfadckey, ass, RunNumber.Data(), type.Data(), particle.Data(), spec.Data(), obsmode.Data());
 
         if (dummy==kFALSE)
@@ -740,4 +787,18 @@
             gSystem->Exec(mkdir);
             gSystem->Exec(link);
+
+            TString vars3 =
+                Form("fRunLinked=Now(), fLinkName=\"%s\", fRunNumber=%d ",
+                     linkname.Data(), RunNum);
+
+            if (!serv.ExistStr("fRunNumber", "MCRunProcessStatus", Form("%d", RunNum)))
+            {
+                if (!serv.Insert("MCRunProcessStatus", vars3))
+                    return 2;
+            }
+            else
+                if (!serv.Update("MCRunProcessStatus", vars3, Form("fRunNumber=%d", RunNum)))
+                    return 2;
+
         }
         else
@@ -753,14 +814,14 @@
     {
         cout << "" << endl;
-        cout << "Linking will be done by fillcamera script" << endl;
+        cout << "P/C Linking will be done by fillcamera script" << endl;
         cout << "" << endl;
     }
 
-    delete runheader1;
-    delete runheader2;
-    delete runheader3;
-    delete fadcheader;
-    delete rawheader;
-    delete evtbasic;
+//    delete runheader1;
+//    delete runheader2;
+//    delete runheader3;
+//    delete fadcheader;
+//    delete rawheader;
+//    delete evtbasic;
 
 
@@ -772,5 +833,7 @@
 int fillcamera(TString fname, Bool_t dummy=kTRUE)
 {
-    MSQLMagic serv("sql.rc");
+    TEnv env("sql.rc");
+
+    MSQLMagic serv(env);
     if (!serv.IsConnected())
     {
@@ -794,5 +857,9 @@
 int fillcamera(TString path="/magic/montecarlo/camera", Bool_t dummy=kTRUE)
 {
-    MSQLMagic serv("mcsql.rc");
+//    TEnv env("mcsql.rc");
+//    TEnv env("sql.rc");
+
+//    MSQLMagic serv(env);
+    MSQLMagic serv("sql.rc");
     if (!serv.IsConnected())
     {
Index: trunk/MagicSoft/Mars/datacenter/scripts/fillcamera
===================================================================
--- trunk/MagicSoft/Mars/datacenter/scripts/fillcamera	(revision 9139)
+++ trunk/MagicSoft/Mars/datacenter/scripts/fillcamera	(revision 9144)
@@ -18,7 +18,7 @@
 #
 #
-#   Author(s): Daniel Hoehne  09/2007 <mailto:hoehne@astro.uni-wuerzburg.de>
-#
-#   Copyright: MAGIC Software Development, 2000-2007
+#   Author(s): Daniel Hoehne  06/2008 <mailto:hoehne@astro.uni-wuerzburg.de>
+#
+#   Copyright: MAGIC Software Development, 2000-2008
 #
 #
@@ -44,5 +44,8 @@
 #mccampath=$mcpath/camera
 mccampath=/magic/montecarlo/camera
-cameradirs=`find $mccampath -maxdepth 1 -mindepth 1 -type d`
+#workaround
+cameradirs=`find /magic/montecarlo/camera -maxdepth 4 -mindepth 2 -type d ! -name Gamma ! -name Muon ! -name Proton ! -name Spot_?.?`
+#cameradirs=`find /magic/montecarlo/camera -maxdepth 3 -mindepth 2 -type d ! -name Gamma ! -name Muon ! -name Proton`
+#cameradirs=`find $mccampath -maxdepth 1 -mindepth 1 -type d`
 echo "camera dirs: "${cameradirs[@]}  >> $scriptlog 2>&1
 echo "" >> $scriptlog 2>&1
@@ -55,10 +58,19 @@
    printprocesslog "INFO inserting information for all root camera files in $cameradir"
    cam=`basename $cameradir`
+   spot=`echo $cameradir | cut -d/ -f7` #spotsize
+   par=`echo $cameradir | cut -d/ -f6` #particle name
+   if [ $cam = "Cal_and_Ped" ]
+   then
+      par=""
+      spot=""
+   fi
+   epo=`echo $cameradir | cut -d/ -f5` #epoch
    echo "dir: "$cameradir >> $scriptlog 2>&1
    fillcamerapath=$logpath/$program
    makedir $fillcamerapath >> $scriptlog 2>&1
-   fillcameralog=$fillcamerapath/$program-$cam.log
+   fillcameralog=$fillcamerapath/$program-$epo-$par-$spot-$cam.log
    
    check0=`root -q -b $macrospath/fillcamera.C+\("\"$cameradir\""\,kFALSE\) | tee $fillcameralog | intgrep`
+#   check0=`root -q -b $macrospath/fillcamera.C+\("\"$cameradir\""\,kTRUE\) | tee $fillcameralog | intgrep`
    case $check0 in 
       1)   echo "check0=$check0 -> everything ok " >> $scriptlog 2>&1 
@@ -77,66 +89,86 @@
    echo "linking cal and ped files" >> $scriptlog 2>&1
    
-   epo=`basename $cameradir` #filename
-   pedfile=`find $cameradir/Cal_and_Ped -name *_P_*.root`
-   calfile=`find $cameradir/Cal_and_Ped -name *_C_*.root`
-   
-   echo "calfile for epoch $epo : " $calfile >> $scriptlog 2>&1
-   echo "pedfile for epoch $epo : " $pedfile >> $scriptlog 2>&1
-   
-   #check number of files
-   numfiles=`echo $pedfile $calfile | wc -w`
-   
-   if [ "$numfiles" != "2" ]
+   if [ $cam = "Cal_and_Ped" ];
    then
-      echo "too many files in the directory $epoch/Cal_and_Ped -> exit" >> $scriptlog 2>&1
-      rm -v $lockfile >> $scriptlog 2>&1
-      exit
-      printprocesslog "ERROR too many ped and cal files found in $epoch/Cal_and_Ped"
-      finish >> $scriptlog 2>&1
-   fi
-   
-
-   user="test"
-   db="TestMonteCarlo"
-   pw="Ics+eaTD"
-   cquery=" select fRunNumber from MCRunProcessStatus where fFileName=\"$calfile\" "
-   pquery=" select fRunNumber from MCRunProcessStatus where fFileName=\"$pedfile\" "
-
-   crun=`mysql -ss -u $user --password=$pw --host=vela $db -e " $cquery "`
-   prun=`mysql -ss -u $user --password=$pw --host=vela $db -e " $pquery "`
-   cnum=`printf %08d $crun`
-   pnum=`printf %08d $prun`
-   echo "calrun number: " $cnum >> $scriptlog 2>&1
-   echo "pedrun number: " $pnum >> $scriptlog 2>&1
-   #get all directories in the linked structure for the epoch
-#   dirs=`find $mcrawpath/ -mindepth 3 -maxdepth 3 -type d`
-   files=`find $mcrawpath/ -lname *$epo*.root`
-
-   for file in ${files[@]}
-   do 
-      dir=`dirname $file`
-      date=`echo $file | cut -c 28-31,33,34,36,37`
-      
-      if ls $dir | grep MonteCarlo | grep $pnum
-      then 
-#         echo "P run already there, do nothing" >> $scriptlog 2>&1
-         continue
-      else 
-         echo "linking P run for epoch $epo" >> $scriptlog 2>&1
-         newpedfile="${dir}/${date}_${pnum}_P_MonteCarlo_E.root"
-         ln -sv $pedfile $newpedfile >> $scriptlog 2>&1
+      echo "Cal_and_Ped folder, no cal and ped files will be linked" >> $scriptlog 2>&1
+   else
+
+      epo=`echo $cameradir | cut -d/ -f5` #filename
+      #epo=`basename $cameradir` #filename
+      pedfile=`find $mccampath/$epo/Cal_and_Ped -name *_P_*.root`
+      calfile=`find $mccampath/$epo/Cal_and_Ped -name *_C_*.root`
+   
+      echo "calfile for epoch $epo : " $calfile >> $scriptlog 2>&1
+      echo "pedfile for epoch $epo : " $pedfile >> $scriptlog 2>&1
+   
+      #check number of files
+      numfiles=`echo $pedfile $calfile | wc -w`
+   
+      if [ "$numfiles" != "2" ]
+      then
+      	echo "too many or too less files in the directory $epo/Cal_and_Ped -> exit" >> $scriptlog 2>&1
+        rm -v $lockfile >> $scriptlog 2>&1
+   	exit
+      	printprocesslog "ERROR too many ped and cal files found in $epo/Cal_and_Ped"
+      	finish >> $scriptlog 2>&1
       fi
       
-      if ls $dir | grep MonteCarlo | grep $cnum
-      then 
-#         echo "C run already there, do nothing" >> $scriptlog 2>&1
-         continue
-      else 
-         echo "linking C run for epoch $epo" >> $scriptlog 2>&1
+      
+      #   user="test"
+      #   db="TestMonteCarlo"
+      #   pw="Ics+eaTD"
+      user="operator"
+      db="MyMagicMonteCarlo"
+      pw="Ics+eMM!"
+      cquery=" select fRunNumber from MCRunProcessStatus where fFileName=\"$calfile\" "
+      pquery=" select fRunNumber from MCRunProcessStatus where fFileName=\"$pedfile\" "
+      
+      crun=`mysql -ss -u $user --password=$pw --host=vela $db -e " $cquery "`
+      prun=`mysql -ss -u $user --password=$pw --host=vela $db -e " $pquery "`
+      cnum=`printf %08d $crun`
+      pnum=`printf %08d $prun`
+      echo "calrun number: " $cnum >> $scriptlog 2>&1
+      echo "pedrun number: " $pnum >> $scriptlog 2>&1
+      #get all directories in the linked structure for the epoch
+      #dirs=`find $mcrawpath/ -mindepth 3 -maxdepth 3 -type d`
+      files=`find $mcrawpath/ -lname *$epo*.root`
+   
+      for file in ${files[@]}
+      do 
+         dir=`dirname $file`
+         #datacenter
+         date=`echo $file | cut -c 28-31,33,34,36,37`
+         #for tests
+         #date=`echo $file | cut -c 57-60,62,63,65,66`
+         
+         newpedfile="${dir}/${date}_${pnum}_P_MonteCarlo_E.root"
          newcalfile="${dir}/${date}_${cnum}_C_MonteCarlo_E.root"
-         ln -sv $calfile $newcalfile >> $scriptlog 2>&1
-      fi
-
-   done
+         pupdate=" update MCRunProcessStatus set fRunLinked=Now(), fLinkName=\"$newpedfile\" where fRunNumber=\"$prun\" "
+         cupdate=" update MCRunProcessStatus set fRunLinked=Now(), fLinkName=\"$newcalfile\" where fRunNumber=\"$crun\" "
+         if ls $dir | grep MonteCarlo | grep $pnum
+         then 
+            echo "P run already there, do mysql update" >> $scriptlog 2>&1
+   #         continue
+            mysql -ss -u $user --password=$pw --host=vela $db -e " $pupdate "
+         else 
+            echo "linking P run for epoch $epo" >> $scriptlog 2>&1
+            ln -sv $pedfile $newpedfile >> $scriptlog 2>&1
+            mysql -ss -u $user --password=$pw --host=vela $db -e " $pupdate "
+         fi
+         
+         if ls $dir | grep MonteCarlo | grep $cnum
+         then 
+            echo "C run already there, do mysql update" >> $scriptlog 2>&1
+   #         continue
+            mysql -ss -u $user --password=$pw --host=vela $db -e " $cupdate "
+         else 
+            echo "linking C run for epoch $epo" >> $scriptlog 2>&1
+            ln -sv $calfile $newcalfile >> $scriptlog 2>&1
+            mysql -ss -u $user --password=$pw --host=vela $db -e " $cupdate "
+         fi
+   
+      done
+   
+   fi
 
 done
@@ -145,5 +177,5 @@
 
 # observation epochs 
-epochs=("" "After January 07 (new MUX)," "From April 06 to January 07," "Before April 06")
+epochs=("" "After January 07 (MUX FADCs)," "From April 06 to January 07 (Siegen FADCs with splitters)," "Before April 06 (Siegen FADCs)")
 
 # print information and explanation of structure into README.txt 
@@ -156,5 +188,5 @@
 echo "YYYY represents 19zbin" >> $readme 2>&1
 echo "MM represents the epoch" >> $readme 2>&1
-echo "DD represents the psf in mm" >> $readme 2>&1
+echo "DD represents the additional spot size in mm" >> $readme 2>&1
 echo "" >> $readme 2>&1
 echo "explanation of the epochs" >> $readme 2>&1
@@ -180,14 +212,6 @@
 echo "<P/C/D> indicates the run type" >> $readme 2>&1
 echo "<Particle>: Gamma, Proton, Muon, MonteCarlo (for P,C runs)" >> $readme 2>&1
-echo "<Spectrum>: empty or HE, depending on the simulated MC spectrum" >> $readme 2>&1
-echo "<ObservationMode>: empty (On), W1/W2 (Wobble), FW1/FW2 (Fake Wobble), Diff (Diffuse)" >> $readme 2>&1
-echo "" >> $readme 2>&1
-echo "Run numbers for Cal and Ped files are linked depending on the epoch:" >> $readme 2>&1
-echo "MC_old:           P=1, C=2" >> $readme 2>&1
-echo "MC_up_to_April06: P=1, C=2" >> $readme 2>&1
-echo "MC_April_May06:   P=3, C=4" >> $readme 2>&1
-echo "MC_post_June06:   P=3, C=4" >> $readme 2>&1
-echo "MC_MuxFADCs:      P=5, C=6" >> $readme 2>&1
-echo "MC_MuxFADCs_NEW:  P=5, C=6" >> $readme 2>&1
+echo "<Spectrum>: 'empty' or HE, depending on the simulated MC spectrum" >> $readme 2>&1
+echo "<ObservationMode>: 'empty' (On), W1/W2 (Wobble), FW (Fake Wobble), Diff (Diffuse)" >> $readme 2>&1
 echo "" >> $readme 2>&1
 
