Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 7114)
+++ trunk/MagicSoft/Mars/Changelog	(revision 7115)
@@ -21,4 +21,44 @@
 
                                                  -*-*- END OF LINE -*-*-
+ 2005/05/31 Thomas Bretz
+
+   * mfileio/MReadReports.[h,cc]:
+     - changed such that it can force prereading of the first event of 
+       a sequence after PreProcess
+
+   * mfileio/MReadTree.h:
+     - added some comment about the new bits used in MReadReports
+
+   * mhflux/MHDisp.cc:
+     - changed fit limit from 1.1 to 1.0
+     - fixed access to wrong axis
+
+   * mhflux/MHEffectiveOnTime.cc:
+     - fixed a bug which caused the first bin of the histograms
+       vs time to disapear in PostProcess, which gave wrong results
+       on the screen. The output and all following programs not
+       directly accessing the histograms haven't been affected
+
+   * mjobs/MJCalib.cc:
+     - HasEnv for boolean type resources is obsolete and has been
+       removed
+
+   * mjobs/MJCalibrateSignal.cc:
+     - removed MPointingPosInterpolate
+     - adapted usage of MReadReports to changes
+
+   * mjobs/MJCut.cc, mjobs/MJStar.cc:
+     - adapted usage of MReadReports to changes
+     - made Drive-tree a 'required' tree
+
+   * mjobs/MJStar.cc:
+     - removed "Events" from setting up rate and effective on time
+       tasks to tlist2. It is obsolete because calibration events
+       don't enter star at all.
+
+   * mpointing/MPointingPosCalc.[h,cc]:
+     - assume that the run-type is real-data if Process is called before
+       ReInit
+
 
 
Index: trunk/MagicSoft/Mars/NEWS
===================================================================
--- trunk/MagicSoft/Mars/NEWS	(revision 7114)
+++ trunk/MagicSoft/Mars/NEWS	(revision 7115)
@@ -5,4 +5,13 @@
    - general: MMcEvt now derived from MMcEvtBasic which should 
      have no influence on compatibility with older camera files
+
+   - general: Implemented a new option to MReadReports which allows
+     to force reading of the first event of some trees (eg. Drive-tree)
+     before any other tree.
+
+   - general: Made a fix to MPointingPosCalc which assumes that the
+     run-type is real-data if Process is called before ReInit, such
+     that also the very first Drive-events in a run are treated 
+     correctly.
 
    - callisto: changed default of fgFallTimeHiGain in 
@@ -18,4 +27,19 @@
      Wrongly the calibration_UV weights were used instead
      of the cosmics weights set in callisto.rc.
+
+   - star: fixed a bug which caused the first bin of the histograms
+     vs time to disapear in PostProcess. This gave wrong results
+     on the screen. The output and all following programs not
+     directly accessing the histograms haven't been affected
+
+   - star, ganymed: Made sure that always the first Drive-tree event
+     is read and Processed before the first data event was read.
+
+   - ganymed: implemented the possibility to replace the standard 
+     false source plot with a different one. For example: If
+     you want to use a Disp based FS plot use the resource:
+       NameHistFS: MHDisp
+     For details about the requirements of these plots look
+     at the corresponding classes
 
    - sponde: The input MC spectrum can now be weighted to fake a
@@ -31,5 +55,5 @@
 
 
- *** Version 0.9.2
+ *** Version 0.9.2 (2005/05/20)
 
    - general: Changed the default paths for calibrated data and image
Index: trunk/MagicSoft/Mars/mfileio/MReadReports.cc
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MReadReports.cc	(revision 7114)
+++ trunk/MagicSoft/Mars/mfileio/MReadReports.cc	(revision 7115)
@@ -165,5 +165,5 @@
 // To be done: A flag(?) telling whether the headers can be skipped.
 //
-void MReadReports::AddTree(const char *tree, const char *time, Bool_t master)
+void MReadReports::AddTree(const char *tree, const char *time, Type_t master)
 {
     /*
@@ -176,15 +176,17 @@
     */
 
-    if (master && TestBit(kHasMaster))
+    if (master==kMaster && TestBit(kHasMaster))
     {
         *fLog << warn << GetDescriptor() << " already has a master tree... ignored." << endl;
-        master = kFALSE;
-    }
-
-    MReadTree *t = master ? new MReadMarsFile(tree) : new MReadTree(tree);
+        master = kStandard;
+    }
+
+    MReadTree *t = master==kMaster ? new MReadMarsFile(tree) : new MReadTree(tree);
     t->SetName(tree);
     t->SetTitle(time?time:"");
-    if (master)
+    if (master==kMaster)
         SetBit(kHasMaster);
+    if (master==kRequired)
+        t->SetBit(kIsRequired);
 
     if (!fEnableAutoScheme)
@@ -219,4 +221,25 @@
 
     return n;
+}
+
+// --------------------------------------------------------------------------
+//
+// Count the number of required trees and store the number if fNumRequired.
+// Reset the kIsProcessed bit.
+//
+void MReadReports::ForceRequired()
+{
+    fNumRequired = 0;
+
+    TIter Next(fTrees->GetList());
+    TObject *o=0;
+    while ((o=Next()))
+        if (o->TestBit(kIsRequired))
+        {
+            o->ResetBit(kIsProcessed);
+            fNumRequired++;
+        }
+
+    *fLog << dbg << "Number of required trees: " << fNumRequired << endl;
 }
 
@@ -297,5 +320,80 @@
     fPosEntry.Set(i);
 
+    // Force that with the next call to Process the required events are read
+    ForceRequired();
+    //fFirstReInit=kTRUE;
+
+    // Preprocess all tasks in fTrees
     return fTrees->CallPreProcess(plist);
+}
+
+// --------------------------------------------------------------------------
+//
+// If this is not the first ReInit after PreProcess force the required
+// trees to be read first (call FirstRequired())
+//
+/*
+Bool_t MReadReports::ReInit(MParList *plist)
+{
+    if (!fFirstReInit)
+        ForceRequired();
+    fFirstReInit=kFALSE;
+    return kTRUE;
+}
+*/
+
+// --------------------------------------------------------------------------
+//
+// Return the number of the tree which is the next one to be read.
+// The condition for this decision is the time-stamp.
+//
+Int_t MReadReports::FindNextTime()
+{
+    TIter NextC(fChains);
+    TChain *c=0;
+
+    Int_t nmin=0;
+    MTime tmin(**GetTime((TChain*)NextC()));
+
+    Int_t i=0;
+
+    while ((c=(TChain*)NextC()))
+    {
+        MTime &t = **GetTime(c);
+        i++;
+
+        if (t >= tmin)
+            continue;
+
+        tmin = t;
+        nmin = i;
+    }
+    return nmin;
+}
+
+// --------------------------------------------------------------------------
+//
+// Return the number of the tree which is the next one to be read.
+// The condition for this decision kIsRequired but not kIsProcessed is set.
+//
+Int_t MReadReports::FindNextRequired()
+{
+    Int_t n = 0;
+
+    TIter Next(fTrees->GetList());
+    TObject *o=0;
+    while ((o=Next()))
+    {
+        if (o->TestBit(kIsRequired) && !o->TestBit(kIsProcessed))
+        {
+            o->SetBit(kIsProcessed);
+            fNumRequired--;
+            *fLog << dbg << "Reading from tree " << n << " " << o->GetName() << endl;
+            return n;
+        }
+        n++;
+    }
+
+    return -1;
 }
 
@@ -312,62 +410,4 @@
 // --------------------------------------------------------------------------
 //
-// Do not use if fChains->GetSize()==0 !!!
-//
-Int_t MReadReports::FindNextTime()
-{
-    Int_t i=0;
-
-    TIter NextC(fChains);
-    TChain *c=0;
-
-    Int_t nmin=0;
-    MTime tmin(**GetTime((TChain*)NextC()));
-
-    while ((c=(TChain*)NextC()))
-    {
-        MTime &t = **GetTime(c);
-        i++;
-
-        if (t >= tmin)
-            continue;
-
-        tmin = t;
-        nmin = i;
-    }
-    return nmin;
-}
-
-/*
-Bool_t MReadReports::Notify()
-{
-    Bool_t same = kTRUE;
-    for (int i=1; i<fPosTree.GetSize(); i++)
-        if (fPosTree[i]!=fPosTree[0])
-        {
-            same = kFALSE;
-            break;
-        }
-
-    Int_t tn = chain->GetTreeNumber();
-
-    Bool_t read=kFALSE;
-    if (fPosTree[nmin] != tn)
-    {
-        fPosTree[nmin] = tn;
-        read = kTRUE;
-    }
-
-    if (!same || !read)
-        return kTRUE;
-
-
-    *fLog << dbg << "Read Run Headers!" << endl;
-
-    return kTRUE;
-}
-*/
-
-// --------------------------------------------------------------------------
-//
 // Check which is the next tree to read from. Read an event from this tree.
 // Sets the StreamId accordingly.
@@ -377,5 +417,10 @@
     while (fChains->GetSize())
     {
-        const Int_t nmin=FindNextTime();
+        const Int_t nmin=FindNext();
+        if (nmin<0)
+        {
+            *fLog << err << "MReadReports::Process: ERROR - Determination of next tree failed... abort." << endl;
+            return kERROR;
+        }
 
         TChain *chain = (TChain*)fChains->At(nmin);
Index: trunk/MagicSoft/Mars/mfileio/MReadReports.h
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MReadReports.h	(revision 7114)
+++ trunk/MagicSoft/Mars/mfileio/MReadReports.h	(revision 7115)
@@ -17,4 +17,11 @@
 class MReadReports : public MRead
 {
+public:
+    enum Type_t {
+        kStandard,
+        kMaster,
+        kRequired
+    };
+
 private:
     MTaskList *fTrees;    // Hold the trees which are scheduled for reading
@@ -25,11 +32,17 @@
 
     Bool_t     fEnableAutoScheme;
+    Int_t      fNumRequired;
+    //Bool_t     fFirstReInit;
 
+    void    ForceRequired();
     MTime** GetTime(TChain *c) const;
     Int_t   FindNextTime();
+    Int_t   FindNextRequired();
+    Int_t   FindNext() { return fNumRequired==0 ? FindNextTime() : FindNextRequired(); }
 
     UInt_t  GetEntries();
     TString GetFullFileName() const;
 
+    //Bool_t  ReInit(MParList *plist);
     Int_t   PreProcess(MParList *plist);
     Int_t   Process();
@@ -37,5 +50,7 @@
 
     enum {
-        kHasMaster = BIT(14)
+        kHasMaster   = BIT(14),
+        kIsRequired  = BIT(22),
+        kIsProcessed = BIT(23)
     };
 
@@ -44,6 +59,6 @@
     ~MReadReports(); 
 
-    void  AddTree(const char *tree, const char *time=NULL, Bool_t master=kFALSE);
-    void  AddTree(const char *tree, Bool_t master)
+    void  AddTree(const char *tree, const char *time=NULL, Type_t master=kStandard);
+    void  AddTree(const char *tree, Type_t master)
     {
         AddTree(tree, NULL, master);
Index: trunk/MagicSoft/Mars/mfileio/MReadTree.h
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MReadTree.h	(revision 7114)
+++ trunk/MagicSoft/Mars/mfileio/MReadTree.h	(revision 7115)
@@ -31,4 +31,6 @@
 
     enum { kChainWasChanged = BIT(14) };
+    // MReadReports::kIsRequired  = BIT(22)
+    // MReadReports::kIsProcessed = BIT(23)
 
 private:
@@ -47,4 +49,6 @@
 
     enum { kIsOwner = BIT(14) };
+    // MReadReports::kIsRequired  = BIT(22)
+    // MReadReports::kIsProcessed = BIT(23)
 
 public:
Index: trunk/MagicSoft/Mars/mhflux/MHDisp.cc
===================================================================
--- trunk/MagicSoft/Mars/mhflux/MHDisp.cc	(revision 7114)
+++ trunk/MagicSoft/Mars/mhflux/MHDisp.cc	(revision 7115)
@@ -278,5 +278,5 @@
             {
                 const Double_t r = TMath::Hypot(h1->GetXaxis()->GetBinCenter(x+1)-x0,
-                                                h1->GetXaxis()->GetBinCenter(y+1)-y0);
+                                                h1->GetYaxis()->GetBinCenter(y+1)-y0);
                 h2->Fill(r, h1->GetBinContent(x+1, y+1));
             }
@@ -286,5 +286,5 @@
         func.SetLineColor(kBlue);
 
-        func.SetParLimits(2, h2->GetBinWidth(1), 1.1);
+        func.SetParLimits(2, h2->GetBinWidth(1), 1.0);
 
         func.SetParameter(0, h2->GetBinContent(1));
@@ -292,5 +292,5 @@
         func.SetParameter(2, 0.15);
         func.SetParameter(4, h2->GetBinContent(10));
-        h2->Fit(&func, "IMQ", "", 0, 1.1);
+        h2->Fit(&func, "IMQ", "", 0, 1.0);
 
         const Double_t r0 = 2*func.GetParameter(2);
Index: trunk/MagicSoft/Mars/mhflux/MHEffectiveOnTime.cc
===================================================================
--- trunk/MagicSoft/Mars/mhflux/MHEffectiveOnTime.cc	(revision 7114)
+++ trunk/MagicSoft/Mars/mhflux/MHEffectiveOnTime.cc	(revision 7115)
@@ -749,8 +749,4 @@
     FitThetaBins();
     FitTimeBin();
-    MH::RemoveFirstBin(fHTimeEffOn);
-    MH::RemoveFirstBin(fHTimeProb);
-    MH::RemoveFirstBin(fHTimeLambda);
-    //MH::RemoveFirstBin(fHTimeNDF);
 
     fIsFinalized = kTRUE;
Index: trunk/MagicSoft/Mars/mjobs/MJCalib.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJCalib.cc	(revision 7114)
+++ trunk/MagicSoft/Mars/mjobs/MJCalib.cc	(revision 7115)
@@ -60,15 +60,8 @@
 Bool_t MJCalib::CheckEnvLocal()
 {
-    if (HasEnv("PixelCheck"))
-        SetPixelCheck(GetEnv("PixelCheck", fIsPixelCheck));
-
-    if (HasEnv("PulsePosCheck"))
-        SetPulsePosCheck(GetEnv("PulsePosCheck", fIsPulsePosCheck));
-
-    if (HasEnv("CheckedPixId"))
-        SetCheckedPixId(GetEnv("CheckedPixId",fCheckedPixId));
-
-    if (HasEnv("HiLoCalibration"))    
-      SetHiLoCalibration(GetEnv("HiLoCalibration", fIsHiLoCalibration));
+    SetPixelCheck(GetEnv("PixelCheck", fIsPixelCheck));
+    SetPulsePosCheck(GetEnv("PulsePosCheck", fIsPulsePosCheck));
+    SetCheckedPixId(GetEnv("CheckedPixId",fCheckedPixId));
+    SetHiLoCalibration(GetEnv("HiLoCalibration", fIsHiLoCalibration));
 
     if (HasEnv("StorageType"))
Index: trunk/MagicSoft/Mars/mjobs/MJCalibrateSignal.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJCalibrateSignal.cc	(revision 7114)
+++ trunk/MagicSoft/Mars/mjobs/MJCalibrateSignal.cc	(revision 7115)
@@ -95,5 +95,4 @@
 //#include "MMcPedestalCopy.h"
 #include "MPointingPosCalc.h"
-#include "MPointingPosInterpolate.h"
 #include "MPedCalcFromLoGain.h"
 #include "MExtractor.h"
@@ -378,5 +377,5 @@
 
     MReadReports readreal;
-    readreal.AddTree("Events", "MTime.", kTRUE);
+    readreal.AddTree("Events", "MTime.", MReadReports::kMaster);
     readreal.AddTree("Trigger");
     readreal.AddTree("Camera");
@@ -407,6 +406,6 @@
     MContinue contmc("MRawEvtData.GetNumPixels<0.5", "ContEmptyMC");
 
-    MPointingPosInterpolate pextr;
-    pextr.AddFiles(&iter);
+    //MPointingPosInterpolate pextr;
+    //pextr.AddFiles(&iter);
     
     MGeomApply             apply; // Only necessary to create geometry
@@ -725,4 +724,5 @@
     // Now setup main tasklist
     tlist.AddToList(read);
+
     if (IsUseMC())
     {
@@ -731,6 +731,6 @@
     }
 
-    if (IsUseRootData())
-      tlist2.AddToList(&pextr);
+    //if (IsUseRootData())
+    //  tlist2.AddToList(&pextr);
     tlist.AddToList(&tlist2, IsUseRootData() ? "Events" : "All");
 
Index: trunk/MagicSoft/Mars/mjobs/MJCut.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJCut.cc	(revision 7114)
+++ trunk/MagicSoft/Mars/mjobs/MJCut.cc	(revision 7115)
@@ -494,6 +494,6 @@
     // ------------- Loop Off Data --------------------
     MReadReports readoffdata;
-    readoffdata.AddTree("Events", "MTime.", kTRUE);
-    readoffdata.AddTree("Drive");
+    readoffdata.AddTree("Events", "MTime.", MReadReports::kMaster);
+    readoffdata.AddTree("Drive", MReadReports::kRequired);
     readoffdata.AddTree("EffectiveOnTime");
 
@@ -637,6 +637,6 @@
     // ------------- Loop On Data --------------------
     MReadReports readondata;
-    readondata.AddTree("Events", "MTime.", kTRUE);
-    readondata.AddTree("Drive");
+    readondata.AddTree("Events", "MTime.", MReadReports::kMaster);
+    readondata.AddTree("Drive", MReadReports::kRequired);
     readondata.AddTree("EffectiveOnTime");
 
Index: trunk/MagicSoft/Mars/mjobs/MJStar.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJStar.cc	(revision 7114)
+++ trunk/MagicSoft/Mars/mjobs/MJStar.cc	(revision 7115)
@@ -191,6 +191,6 @@
 
     MReadReports readreal;
-    readreal.AddTree("Events", "MTime.", kTRUE);
-    readreal.AddTree("Drive");
+    readreal.AddTree("Events", "MTime.", MReadReports::kMaster);
+    readreal.AddTree("Drive", MReadReports::kRequired);
     readreal.AddTree("CC");
     //read.AddTree("Trigger");
@@ -368,8 +368,9 @@
     if (!ismc)
     {
-        tlist2.AddToList(&rate,   "Events");
-        tlist2.AddToList(&fillvs, "Events");
-        tlist2.AddToList(&fill8,  "Events");
-        tlist2.AddToList(&fill9,  "Events");
+        // Calibration events don't enter star at all.
+        tlist2.AddToList(&rate);
+        tlist2.AddToList(&fillvs);
+        tlist2.AddToList(&fill8);
+        tlist2.AddToList(&fill9);
     }
     //tlist2.AddToList(&fillb);
Index: trunk/MagicSoft/Mars/mpointing/MPointingPosCalc.cc
===================================================================
--- trunk/MagicSoft/Mars/mpointing/MPointingPosCalc.cc	(revision 7114)
+++ trunk/MagicSoft/Mars/mpointing/MPointingPosCalc.cc	(revision 7115)
@@ -34,4 +34,13 @@
 //  * Real Data: Copy the nominal poiting position (Nominal Zd, Nominal Az
 //               in MReportDrive) to MPointingPosition
+//
+// We do a trick: Because it is not guranteed, that MReadReports calls
+//  ReInit before the first event from the drive-tree is read (because
+//  the first data event may have a time-stamp later than the first
+//  drive event) we always assume that the current run is a data run.
+//  If the assumption is true fReport should be initialized correctly.
+//  For MC runs this should never happen, Because they are read though
+//  MReadMarsFile which gurantees, that ReInit is always called before
+//  Process. If we encounter such a case we stop execution.
 //
 // Future: Interpolate the pointing position for each event between two
@@ -81,5 +90,4 @@
     {
     case MRawRunHeader::kRTData:
-        fReport = (MReportDrive*)plist->FindObject("MReportDrive");
         if (!fReport)
         {
@@ -121,4 +129,9 @@
 {
     fPosition = (MPointingPos*)plist->FindCreateObj("MPointingPos");
+    fReport   = (MReportDrive*)plist->FindObject("MReportDrive");
+
+    // We use kRTNone here as a placeholder for data runs.
+    fRunType  = MRawRunHeader::kRTNone;
+
     return fPosition ? kTRUE : kFALSE;
 }
@@ -130,14 +143,14 @@
 Int_t MPointingPosCalc::Process()
 {
-    if (!fReport && fRunType == MRawRunHeader::kRTData)
-    {
-        *fLog << warn << "WARNING - MPointingPosCalc::Process was called BEFORE the first" << endl;
-        *fLog <<         "          data event was read (means ReInit was executed!" << endl;
-        return kTRUE;
-    }
-
     switch (fRunType)
     {
+    case MRawRunHeader::kRTNone:
     case MRawRunHeader::kRTData:
+        if (!fReport)
+        {
+            *fLog << warn;
+            *fLog << "MPointingPosCalc::Process: fReport==NULL && fRunType!=kRTMonteCarlo... abort!" << endl;
+            return kERROR;
+        }
         fPosition->SetLocalPosition(fReport->GetNominalZd(), fReport->GetNominalAz());
         fPosition->SetSkyPosition(fReport->GetRa(), fReport->GetDec());
@@ -145,4 +158,10 @@
 
     case MRawRunHeader::kRTMonteCarlo:
+        if (!fMcEvt)
+        {
+            *fLog << warn;
+            *fLog << "MPointingPosCalc::Process: fMcEvt==NULL && fRunType==kRTMonteCarlo... abort!" << endl;
+            return kERROR;
+        }
         fPosition->SetLocalPosition(fMcEvt->GetTelescopeTheta()*TMath::RadToDeg(), fMcEvt->GetTelescopePhi()*TMath::RadToDeg());
         return kTRUE;
