Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 9361)
+++ trunk/MagicSoft/Mars/Changelog	(revision 9362)
@@ -18,4 +18,46 @@
 
                                                  -*-*- END OF LINE -*-*-
+ 2009/02/23 Thomas Bretz
+
+   * mcorsika/MCorsikaRunHeader.cc:
+     - switched off the impact max workaround - it doesn't work
+     - initialize fImpact Max
+     - print also number of used ATMEXT
+
+   * mhbase/MH.cc:
+     - improved setting of binning from the parameter list in the
+       n-D case
+
+   * mhbase/MH3.cc:
+     - imporved output
+
+   * mhflux/MHCollectionArea.cc:
+     - also collect the maximum impact while running for a nicer
+       behaviour of the plots
+
+   * mhflux/MHThreshold.cc:
+     - allow setting of a dedicated Threshold binning
+
+   * mimage/MHHillasExt.cc:
+     - converted slope binning to deg
+
+   * mjobs/MJSimulation.cc:
+     - write MMcCorsikaRunHeader
+     - changed binnings
+     - added new binning
+     - now display the signal unscaled
+     - show threshold and collection area only for data runs
+
+   * msim/MSimMMCS.cc:
+     - make setting of directions depending on view-cone option
+
+   * msim/MSimPointingPos.[h,cc]:
+     - added class description
+     - removed obsolete fPointingCorsika
+     - improved output
+     - added option for homogenous distribution
+
+
+
  2009/02/20 Thomas Bretz
 
Index: trunk/MagicSoft/Mars/mcorsika/MCorsikaRunHeader.cc
===================================================================
--- trunk/MagicSoft/Mars/mcorsika/MCorsikaRunHeader.cc	(revision 9361)
+++ trunk/MagicSoft/Mars/mcorsika/MCorsikaRunHeader.cc	(revision 9362)
@@ -66,6 +66,6 @@
 //
 MCorsikaRunHeader::MCorsikaRunHeader(const char *name, const char *title)
-    : fNumObsLevel(0), fZdMin(0), fZdMax(-1), fAzMin(0), fAzMax(0),
-    fViewConeInnerAngle(0), fViewConeOuterAngle(-1)
+    : fNumObsLevel(0), fImpactMax(-1), fZdMin(0), fZdMax(-1),
+    fAzMin(0), fAzMax(0),  fViewConeInnerAngle(0), fViewConeOuterAngle(-1)
 {
     fName  = name  ? name  : "MCorsikaRunHeader";
@@ -183,12 +183,13 @@
     // g[93] angle between array x-direction and magnetic north
 
+    fImpactMax = -1;
+/*
     // This is a trick to use CERARY for storage of the
     // maximum simulated impact
-    fImpactMax = -1;
     if (TMath::Nint(g[84])==1 && TMath::Nint(g[85])==1 &&
         TMath::Nint(g[88])==1 && TMath::Nint(g[89])==1 &&
         g[86]==g[87])
         fImpactMax = g[86];
-
+ */
     fWavelengthMin = g[94];        // Cherenkov bandwidth lower end in nm
     fWavelengthMax = g[95];        // Cherenkov bandwidth upper end in nm
@@ -272,5 +273,5 @@
 
     if (fViewConeOuterAngle>0)
-        *fLog << "ViewCone:       " << fViewConeInnerAngle << UTF8::kDeg << "-" << fViewConeOuterAngle << UTF8::kDeg << endl;
+        *fLog << "ViewCone:       " << fViewConeInnerAngle << UTF8::kDeg << " - " << fViewConeOuterAngle << UTF8::kDeg << endl;
 
     if (fZdMax>=0)
@@ -300,5 +301,5 @@
         *fLog << " CEFFIC";
     if (Has(kAtmext))
-        *fLog << " ATMEXT";
+        *fLog << " ATMEXT" << GetNumAtmosphericModel();
     if (Has(kRefraction))
         *fLog << " AtmRefraction";
Index: trunk/MagicSoft/Mars/mhbase/MH.cc
===================================================================
--- trunk/MagicSoft/Mars/mhbase/MH.cc	(revision 9361)
+++ trunk/MagicSoft/Mars/mhbase/MH.cc	(revision 9362)
@@ -1,4 +1,4 @@
 /* ======================================================================== *\
-! $Name: not supported by cvs2svn $:$Id: MH.cc,v 1.45 2009-02-11 12:08:23 tbretz Exp $
+! $Name: not supported by cvs2svn $:$Id: MH.cc,v 1.46 2009-02-22 23:36:00 tbretz Exp $
 ! --------------------------------------------------------------------------
 !
@@ -698,5 +698,8 @@
 
     if (bins->IsDefault())
+    {
+        gLog << inf << "Object 'Binning" << name << "' [MBinning] is default... binning unchanged." << endl;
         return kTRUE;
+    }
 
     SetBinning(h, bins);
@@ -707,58 +710,85 @@
 {
     const MBinning *binsx = (MBinning*)plist.FindObject("Binning"+x);
+    const MBinning *binsy = (MBinning*)plist.FindObject("Binning"+y);
+
+    if (!binsx && !binsy)
+    {
+        gLog << inf << "Neither 'Binning" << x << "' nor 'Binning" << y;
+        gLog << "' [MBinning] found... no binning applied." << endl;
+        return kFALSE;
+    }
+
     if (!binsx)
-    {
-        gLog << inf << "Object 'Binning" << x << "' [MBinning] not found... no binning applied." << endl;
+        gLog << inf << "Object 'Binning" << x << "' [MBinning] not found... binning unchanged." << endl;
+    if (!binsy)
+        gLog << inf << "Object 'Binning" << y << "' [MBinning] not found... binning unchanged." << endl;
+
+    if (binsx && binsx->IsDefault())
+    {
+        gLog << inf << "Object 'Binning" << x << "' [MBinning] is default... binning unchanged." << endl;
+        binsx = 0;
+    }
+
+    if (binsy && binsy->IsDefault())
+    {
+        gLog << inf << "Object 'Binning" << y << "' [MBinning] is default... binning unchanged." << endl;
+        binsy = 0;
+    }
+
+    MBinning binsxx, binsyy;
+    binsxx.SetEdges(*h, 'x');
+    binsyy.SetEdges(*h, 'y');
+
+    SetBinning(h, binsx?binsx:&binsxx, binsy?binsy:&binsyy);
+
+    return kTRUE;
+}
+
+Bool_t MH::ApplyBinning(const MParList &plist, TString x, TString y, TString z, TH3 *h)
+{
+    const MBinning *binsx = (MBinning*)plist.FindObject("Binning"+x);
+    const MBinning *binsy = (MBinning*)plist.FindObject("Binning"+y);
+    const MBinning *binsz = (MBinning*)plist.FindObject("Binning"+z);
+
+    if (!binsx && !binsy && !binsz)
+    {
+        gLog << inf << "Neither 'Binning" << x << "', 'Binning" << y;
+        gLog << "' nor 'Binning" << z << "' [MBinning] found... ";
+        gLog << "no binning applied." << endl;
         return kFALSE;
     }
-    const MBinning *binsy = (MBinning*)plist.FindObject("Binning"+y);
+
+    if (!binsx)
+        gLog << inf << "Object 'Binning" << x << "' [MBinning] not found... binning unchanged." << endl;
     if (!binsy)
-    {
-        gLog << inf << "Object 'Binning" << y << "' [MBinning] not found... no binning applied." << endl;
-        return kFALSE;
-    }
-
-    if (binsx->IsDefault() && binsy->IsDefault())
-        return kTRUE;
-
-    // -------------------------
-    /*
-     MBinning binsx, binsy, binsz;
-     binsx.SetEdges(fHist, 'x');
-     binsy.SetEdges(fHist, 'y');
-     binsz.SetEdges(fHist, 'z');
-     */
-    // -------------------------
-
-    SetBinning(h, binsx, binsy);
-
-    return kTRUE;
-}
-
-Bool_t MH::ApplyBinning(const MParList &plist, TString x, TString y, TString z, TH3 *h)
-{
-    const MBinning *binsx = (MBinning*)plist.FindObject("Binning"+x);
-    if (!binsx)
-    {
-        gLog << inf << "Object 'Binning" << x << "' [MBinning] not found... no binning applied." << endl;
-        return kFALSE;
-    }
-    const MBinning *binsy = (MBinning*)plist.FindObject("Binning"+y);
-    if (!binsy)
-    {
-        gLog << inf << "Object 'Binning" << y << "' [MBinning] not found... no binning applied." << endl;
-        return kFALSE;
-    }
-    const MBinning *binsz = (MBinning*)plist.FindObject("Binning"+z);
+        gLog << inf << "Object 'Binning" << y << "' [MBinning] not found... binning unchanged." << endl;
     if (!binsz)
-    {
-        gLog << inf << "Object 'Binning" << z << "' [MBinning] not found... no binning applied." << endl;
-        return kFALSE;
-    }
-
-    if (binsx->IsDefault() && binsy->IsDefault() && binsz->IsDefault())
-        return kTRUE;
-
-    SetBinning(h, binsx, binsy, binsz);
+        gLog << inf << "Object 'Binning" << z << "' [MBinning] not found... binning unchanged." << endl;
+
+    if (binsx && binsx->IsDefault())
+    {
+        gLog << inf << "Object 'Binning" << x << "' [MBinning] is default... binning unchanged." << endl;
+        binsx = 0;
+    }
+
+    if (binsy && binsy->IsDefault())
+    {
+        gLog << inf << "Object 'Binning" << y << "' [MBinning] is default... binning unchanged." << endl;
+        binsy = 0;
+    }
+
+    if (binsz && binsz->IsDefault())
+    {
+        gLog << inf << "Object 'Binning" << z << "' [MBinning] is default... binning unchanged." << endl;
+        binsy = 0;
+    }
+
+    MBinning binsxx, binsyy, binszz;
+    binsxx.SetEdges(*h, 'x');
+    binsyy.SetEdges(*h, 'y');
+    binszz.SetEdges(*h, 'z');
+
+    SetBinning(h, binsx?binsx:&binsxx, binsy?binsy:&binsyy, binsz?binsz:&binszz);
+
     return kTRUE;
 }
Index: trunk/MagicSoft/Mars/mhbase/MH3.cc
===================================================================
--- trunk/MagicSoft/Mars/mhbase/MH3.cc	(revision 9361)
+++ trunk/MagicSoft/Mars/mhbase/MH3.cc	(revision 9362)
@@ -583,5 +583,5 @@
                 if (!binsx)
                 {
-                    *fLog << err << dbginf << "Neither '" << bx << "' nor '" << fName << "' found... aborting." << endl;
+                    *fLog << err << dbginf << "Neither '" << bx << "' nor 'Binning" << fName << "' found... aborting." << endl;
                     return kFALSE;
                 }
Index: trunk/MagicSoft/Mars/mhflux/MHCollectionArea.cc
===================================================================
--- trunk/MagicSoft/Mars/mhflux/MHCollectionArea.cc	(revision 9361)
+++ trunk/MagicSoft/Mars/mhflux/MHCollectionArea.cc	(revision 9362)
@@ -211,5 +211,5 @@
 void MHCollectionArea::GetImpactMax()
 {
-    if (fHeader->GetImpactMax()<=fMcAreaRadius*100)
+    if (fHeader->GetImpactMax()<=fMcAreaRadius*100 || fHeader->GetImpactMax()<0)
         return;
 
@@ -454,4 +454,11 @@
 Int_t MHCollectionArea::Fill(const MParContainer *par, const Stat_t weight)
 {
+    // This is not perfect because it selects the maximum impact only
+    // from the selected events. Hoever, it will get overwritten
+    // in finalize anyway.
+    const Double_t impact = fMcEvt->GetImpact()*0.01; // cm->m
+    if (impact>fMcAreaRadius)
+        fMcAreaRadius = impact;
+
     const Double_t energy = fMcEvt->GetEnergy();
     const Double_t theta  = fMcEvt->GetTelescopeTheta()*TMath::RadToDeg();
Index: trunk/MagicSoft/Mars/mhflux/MHThreshold.cc
===================================================================
--- trunk/MagicSoft/Mars/mhflux/MHThreshold.cc	(revision 9361)
+++ trunk/MagicSoft/Mars/mhflux/MHThreshold.cc	(revision 9362)
@@ -97,14 +97,7 @@
         return kFALSE;
     }
-/*
-    MBinning binse;
-    binse.SetEdges(fHEnergy, 'x');
 
-    MBinning *bins = (MBinning*)pl->FindObject("BinningEnergyEst", "MBinning");
-    if (bins)
-        binse.SetEdges(*bins);
+    ApplyBinning(*pl, "Threshold", &fHEnergy);
 
-    binse.Apply(fHEnergy);
-  */
     return kTRUE;
 }
Index: trunk/MagicSoft/Mars/mimage/MHHillasExt.cc
===================================================================
--- trunk/MagicSoft/Mars/mimage/MHHillasExt.cc	(revision 9361)
+++ trunk/MagicSoft/Mars/mimage/MHHillasExt.cc	(revision 9362)
@@ -130,5 +130,5 @@
 
     binsx.SetEdges(100,     0,  1.5);
-    binsy.SetEdges(100, -0.04, 0.04);
+    binsy.SetEdges(100,    -9,    9);
     MH::SetBinning(&fHSlopeL, &binsx, &binsy);
 }
Index: trunk/MagicSoft/Mars/mjobs/MJSimulation.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJSimulation.cc	(revision 9361)
+++ trunk/MagicSoft/Mars/mjobs/MJSimulation.cc	(revision 9362)
@@ -215,4 +215,5 @@
 {
     // Common run headers
+    write.AddContainer("MMcCorsikaRunHeader", "RunHeaders", kFALSE);
     write.AddContainer("MCorsikaRunHeader",   "RunHeaders", kFALSE);
     write.AddContainer("MRawRunHeader",       "RunHeaders");
@@ -346,6 +347,8 @@
     // -------------------------------------------------------------------
 
-    MBinning binse( 100,     1,   100000, "BinningEnergy", "log");
-    MBinning binss( 100,     1, 10000000, "BinningSize",   "log");
+    MBinning binse( 100,     1,   100000, "BinningEnergy",    "log");
+    MBinning binsth( 35,   0.9,   900000, "BinningThreshold", "log");
+    MBinning binsee( 35,   0.9,   900000, "BinningEnergyEst", "log");
+    MBinning binss( 100,     1, 10000000, "BinningSize",      "log");
     MBinning binsi( 100,  -500,      500, "BinningImpact");
     MBinning binsh( 150,     0,       50, "BinningHeight");
@@ -353,10 +356,13 @@
     MBinning binszd( 70,     0,       70, "BinningZd");
     MBinning binsvc( 45,     0,        9, "BinningViewCone");
-    MBinning binsew(150,     0,       50, "BinningEvtWidth");
+    MBinning binsel(150,     0,       50, "BinningTotLength");
+    MBinning binsew(150,     0,       15, "BinningMedLength");
     MBinning binsd("BinningDistC");
     MBinning binsd0("BinningDist");
     MBinning binstr("BinningTrigPos");
 
+    plist.AddToList(&binsee);
     plist.AddToList(&binse);
+    plist.AddToList(&binsth);
     plist.AddToList(&binss);
     plist.AddToList(&binsi);
@@ -365,4 +371,5 @@
     plist.AddToList(&binsaz);
     plist.AddToList(&binsvc);
+    plist.AddToList(&binsel);
     plist.AddToList(&binsew);
     plist.AddToList(&binstr);
@@ -380,9 +387,10 @@
 
     MH3 mhew("MPhotonStatistics.fLength");
-    mhew.SetName("EvtWidth");
-    mhew.SetTitle("Time between first and last photon hitting a detector");
+    mhew.SetName("TotLength");
+    mhew.SetTitle("Time between first and last photon hitting a detector;L [ns]");
 
     MH3 mhed("MPhotonStatistics.fTimeMedDev");
-    mhed.SetName("EvtMedDev;EvtWidth");
+    mhed.SetName("MedLength");
+    mhed.SetTitle("Median deviation (1\\sigma);L [ns]");
 
     MFillH fillh1(&mhn1, "", "FillCorsika");
@@ -488,4 +496,6 @@
     //  NSB = 0.2*6e-3
 
+    // FIXME: Simulate photons before QE!
+
     MSimRandomPhotons  simnsb;
 //    simnsb.SetFreq(0.005, 0.004);    // 5MHz/cm^2, 4MHz
@@ -530,6 +540,6 @@
     hcalc.Disable(MHillasCalc::kCalcConc);
 
-    MHCamEvent evt0a(/*10*/0, "Signal",    "Average signal;;S [ph]");
-    MHCamEvent evt0c(/*10*/0, "MaxSignal", "Maximum signal;;S [ph]");
+    MHCamEvent evt0a(/*10*/3, "Signal",    "Average signal (absolute);;S [ph]");
+    MHCamEvent evt0c(/*10*/3, "MaxSignal", "Maximum signal (absolute);;S [ph]");
     MHCamEvent evt0d(/*11*/8, "ArrTm",     "Time after first photon;;T [ns]");
     evt0a.SetErrorSpread(kFALSE);
@@ -653,8 +663,11 @@
         //tasks.AddToList(&fillx2);
         tasks.AddToList(&fillx3);
+        //tasks.AddToList(&fillx4);
+        //tasks.AddToList(&fillx5);
+    }
+    if (header.IsDataRun())
+    {
         tasks.AddToList(&fillth);
         tasks.AddToList(&fillca);
-        //tasks.AddToList(&fillx4);
-        //tasks.AddToList(&fillx5);
     }
     //-------------------------------------------
Index: trunk/MagicSoft/Mars/msim/MSimMMCS.cc
===================================================================
--- trunk/MagicSoft/Mars/msim/MSimMMCS.cc	(revision 9361)
+++ trunk/MagicSoft/Mars/msim/MSimMMCS.cc	(revision 9362)
@@ -174,6 +174,14 @@
     // Convert from corsika frame to telescope frame, taking
     // the magnetic field into account: tel = corsika+offset
-    fMcEvt->SetTheta(fEvtHeader->GetZd());
-    fMcEvt->SetPhi(fEvtHeader->GetAz()+fRunHeader->GetMagneticFieldAz());
+    if (fRunHeader->HasViewCone())
+    {
+        fMcEvt->SetTheta(fPointingTel->GetZdRad());
+        fMcEvt->SetPhi(fPointingTel->GetAzRad());
+    }
+    else
+    {
+        fMcEvt->SetTheta(fEvtHeader->GetZd());
+        fMcEvt->SetPhi(fEvtHeader->GetAz()+fRunHeader->GetMagneticFieldAz());
+    }
 
     fMcEvt->SetEvtNumber(fEvtHeader->GetEvtNumber());
Index: trunk/MagicSoft/Mars/msim/MSimPointingPos.cc
===================================================================
--- trunk/MagicSoft/Mars/msim/MSimPointingPos.cc	(revision 9361)
+++ trunk/MagicSoft/Mars/msim/MSimPointingPos.cc	(revision 9362)
@@ -25,5 +25,6 @@
 //////////////////////////////////////////////////////////////////////////////
 //
-//  MSimPointingPos
+// MSimPointingPos
+//
 //
 // This task is meant to simulate the pointing position (mirror orientation).
@@ -31,7 +32,23 @@
 // on the user request (e.g. Wobble mode).
 //
-// WARNING: Currently the telescope orientation is just fixed to the
-//          direction in the run-header (if a view cone was given) or
-//          the direction in the evt-header (if no view cone given)
+//
+// If fOffTragetDistance==0 the telescope is oriented depending on the
+// view cone option. If a view cone was given the orientation is fixed to
+// the main direction around the view cone was produced. If no view
+// cone was given the telescope is oriented parallel to the shower axis.
+//
+// If no view cone option was given and off-target observations are switched
+// on by setting fOffTargetDistance!=0 the poitnting position is calculated:
+//
+//  1) fOffTargetDistance < 0:
+//     The pointing position is randomly distributed in a disk of radius
+//     -fOffTragetDistance. fOffTargetDistance is silently ignored.
+//
+//  2) fOffTargetDistance > 0:
+//     The pointing position is set to a position in the given distance
+//     away from the shower axis. If fOffTargetPhi>=0 it is fixed at
+//     this phi value. For phi<0 it is randomly distributed at distances
+//     fOffTargetDistance. (phi==0 is the direction of positive theta)
+//
 //
 //  Input Containers:
@@ -41,5 +58,4 @@
 //  Output Containers:
 //   MPointingPos
-//   PointingCorsika [MPointingPos]
 //
 //////////////////////////////////////////////////////////////////////////////
@@ -68,6 +84,6 @@
 //
 MSimPointingPos::MSimPointingPos(const char* name, const char *title)
-    : fRunHeader(0), fEvtHeader(0), fPointingCorsika(0), fPointingLocal(0),
-    fOffTargetDistance(-1), fOffTargetPhi(-1)
+    : fRunHeader(0), fEvtHeader(0), fPointing(0),
+    fOffTargetDistance(0), fOffTargetPhi(-1)
 
 {
@@ -83,10 +99,6 @@
 Int_t MSimPointingPos::PreProcess(MParList *pList)
 {
-//    fPointingCorsika = (MPointingPos*)pList->FindCreateObj("MPointingPos", "PointingCorsika");
-//    if (!fPointingCorsika)
-//        return kFALSE;
-
-    fPointingLocal = (MPointingPos*)pList->FindCreateObj("MPointingPos");
-    if (!fPointingLocal)
+    fPointing = (MPointingPos*)pList->FindCreateObj("MPointingPos");
+    if (!fPointing)
         return kFALSE;
 
@@ -105,13 +117,11 @@
     }
 
-    // FIXED offset
-    // Diffuse?  ( FOV of camera folded with mirror diameter as Corsika input? )
-    // Hour angle!
-    // Angle to magnetic field!
-
-    if (IsOffTargetObservation())
-    {
-        *fLog << inf;
-        *fLog << "Off target observations switched on with" << endl;
+    if (!IsOffTargetObservation())
+        return kTRUE;
+
+    *fLog << inf;
+    *fLog << "Off target observations switched on with" << endl;
+    if (fOffTargetDistance>0)
+    {
         *fLog <<"   a pointing distance of " << GetOffTargetDistance() << "deg ";
         if (fOffTargetPhi<0)
@@ -120,15 +130,35 @@
             *fLog << "and phi=" << GetOffTargetPhi() << "deg." << endl;
     }
+    else
+        *fLog << "   a homogenous distribution up to a distance of " << -GetOffTargetDistance() << "deg " << endl;
 
     return kTRUE;
 }
 
-/*
 Bool_t MSimPointingPos::ReInit(MParList *pList)
 {
+    if (fRunHeader->HasViewCone() && IsOffTargetObservation())
+    {
+        *fLog << warn;
+        *fLog << "WARNING - Combining the view cone option with off-target observations doesn't make sense." << endl;
+        *fLog << "          Option for off-target observations will be ignored." << endl;
+    }
     // FIXME: Check also the enlightened region on the ground!
     return kTRUE;
 }
-*/
+
+void MSimPointingPos::GetDelta(Double_t &dtheta, Double_t &dphi) const
+{
+    if (fOffTargetDistance>0)
+    {
+        dtheta = fOffTargetDistance;
+        dphi   = fOffTargetPhi>0 ? fOffTargetPhi : gRandom->Uniform(TMath::TwoPi());
+    }
+    else
+    {
+        dtheta = TMath::Sqrt(gRandom->Uniform(fOffTargetDistance));
+        dphi   = gRandom->Uniform(TMath::TwoPi());
+    }
+}
 
 // --------------------------------------------------------------------------
@@ -136,35 +166,36 @@
 Int_t MSimPointingPos::Process()
 {
-
     // If a view cone is given use the fixed telescope orientation
-    const Bool_t fixed = fRunHeader->HasViewCone();
+    const Bool_t viewcone = fRunHeader->HasViewCone();
 
     // Local sky coordinates (direction of telescope axis)
-    /*const*/ Double_t zd = fixed ? fRunHeader->GetZdMin() : fEvtHeader->GetZd()*TMath::RadToDeg();  // x==north
-    /*const*/ Double_t az = fixed ? fRunHeader->GetAzMin() : fEvtHeader->GetAz()*TMath::RadToDeg();  // y==west
-
-    if (!fixed && IsOffTargetObservation())
-    {
+    Double_t zd = viewcone ? fRunHeader->GetZdMin() : fEvtHeader->GetZd()*TMath::RadToDeg();  // x==north
+    Double_t az = viewcone ? fRunHeader->GetAzMin() : fEvtHeader->GetAz()*TMath::RadToDeg();  // y==west
+
+    if (!viewcone)
+    {
+        Double_t dtheta, dphi;
+        GetDelta(dtheta, dphi);
+
         const Double_t theta = zd*TMath::DegToRad();
         const Double_t phi   = az*TMath::DegToRad();
 
-        /*const*/ TVector3 source;
-        source.SetMagThetaPhi(1, theta, phi);
-
-        /*const*/ TVector3 point;
-        point.SetMagThetaPhi(1, theta+fOffTargetDistance, phi);
-
-        const Double_t delta = fOffTargetPhi>0 ? fOffTargetPhi :
-            gRandom->Uniform(TMath::TwoPi());
-
-        point.Rotate(delta, source);
-
-        zd = point.Theta()*TMath::RadToDeg();
-        az = point.Phi()*TMath::RadToDeg();
-    }
+        TVector3 src, pnt;
+        src.SetMagThetaPhi(1, theta,        phi);
+        pnt.SetMagThetaPhi(1, theta+dtheta, phi);
+
+        pnt.Rotate(dphi, src);
+
+        zd = pnt.Theta()*TMath::RadToDeg();
+        az = pnt.Phi()  *TMath::RadToDeg();
+    }
+
+    // Transform the corsika coordinate system (north is magnetic north)
+    // into the telescopes local coordinate system. Note, that all vectors
+    // are already correctly oriented.
+    az += fRunHeader->GetMagneticFieldAz()*TMath::RadToDeg();
 
     // Setup the pointing position
-//    fPointingCorsika->SetLocalPosition(zd, az/*+fRunHeader->GetMagneticFieldAz()*TMath::RadToDeg()*/);
-    fPointingLocal->SetLocalPosition(zd, az+fRunHeader->GetMagneticFieldAz()*TMath::RadToDeg());
+    fPointing->SetLocalPosition(zd, az);
 
     // Calculate incident angle between magnetic field direction
Index: trunk/MagicSoft/Mars/msim/MSimPointingPos.h
===================================================================
--- trunk/MagicSoft/Mars/msim/MSimPointingPos.h	(revision 9361)
+++ trunk/MagicSoft/Mars/msim/MSimPointingPos.h	(revision 9362)
@@ -14,11 +14,13 @@
 {
 private:
-    MCorsikaRunHeader *fRunHeader;         //! Header storing event information
-    MCorsikaEvtHeader *fEvtHeader;         //! Header storing event information
-    MPointingPos      *fPointingCorsika;   //! Output storing telescope poiting position in corsika coordinate system (modulo magnetig field declination)
-    MPointingPos      *fPointingLocal;     //! Output storing telescope poiting position in local (telescope) coordinate system
+    MCorsikaRunHeader *fRunHeader;  //! Header storing event information
+    MCorsikaEvtHeader *fEvtHeader;  //! Header storing event information
+    MPointingPos      *fPointing;   //! Output storing telescope poiting position in local (telescope) coordinate system
 
-    Double_t fOffTargetDistance;  // [rad] Distance of the observed off-target position from the source
-    Double_t fOffTargetPhi;       // [rad] Rotation angle of the off-target position (phi==0 means south, phi=90 west) [0;2pi], phi<0 means random
+    Double_t fOffTargetDistance;    // [rad] Distance of the observed off-target position from the source
+    Double_t fOffTargetPhi;         // [rad] Rotation angle of the off-target position (phi==0 means south, phi=90 west) [0;2pi], phi<0 means random
+
+    // MSimPointingPos
+    void GetDelta(Double_t &dtheta, Double_t &dphi) const;
 
     // MParContainer
@@ -27,5 +29,5 @@
     // MTask
     Int_t  PreProcess(MParList *pList);
-    //Bool_t ReInit(MParList *pList);
+    Bool_t ReInit(MParList *pList);
     Int_t  Process();
 
@@ -34,13 +36,13 @@
 
     // Getter
-    Double_t GetOffTargetDistance() const { return fOffTargetDistance*TMath::RadToDeg(); }
+    Double_t GetOffTargetDistance() const { return fOffTargetDistance==0 ? 0 : fOffTargetDistance*TMath::RadToDeg(); }
     Double_t GetOffTargetPhi() const { return fOffTargetPhi*TMath::RadToDeg(); }
 
     // Setter
-    void SetOffTargetDistance(Double_t d=-1) { fOffTargetDistance = d*TMath::DegToRad(); }
-    void SetOffTargetPhi(Double_t p=-1) { fOffTargetPhi = p*TMath::DegToRad(); }
+    void SetOffTargetDistance(Double_t d=0) { fOffTargetDistance = d==0 ? 0 : d*TMath::DegToRad(); }
+    void SetOffTargetPhi(Double_t p=0) { fOffTargetPhi = p*TMath::DegToRad(); }
 
     // MSimPointingPos
-    Bool_t IsOffTargetObservation() const { return fOffTargetDistance>0; }
+    Bool_t IsOffTargetObservation() const { return fOffTargetDistance!=0; }
 
     // TObject
