Index: trunk/MagicSoft/Mars/mjobs/MJSpectrum.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJSpectrum.cc	(revision 9334)
+++ trunk/MagicSoft/Mars/mjobs/MJSpectrum.cc	(revision 9335)
@@ -99,4 +99,5 @@
     // (i.e. removed via RecursiveRemove if closed)
     gROOT->GetListOfCleanups()->Add(this);
+    SetBit(kMustCleanup);
 }
 
@@ -244,4 +245,6 @@
     }
 
+    //arr.EnableTH1Workaround();
+
     TH1D *vstime = (TH1D*)arr.FindObjectInCanvas("Theta",  "TH1D", "OnTime");
     TH1D *size   = (TH1D*)arr.FindObjectInCanvas("Excess", "TH1D", "Hist");
@@ -404,5 +407,5 @@
         h.SetXTitle("\\Theta [\\circ]");
         h.SetYTitle("E [GeV]");
-        h.SetZTitle(Form("Counts normalized to r=%.0fm", impactmax/100));
+        h.SetZTitle(MString::Format("Counts normalized to r=%.0fm", impactmax/100));
     }
     else
@@ -410,5 +413,5 @@
         h.SetNameTitle("ThetaMC", "Event-Distribution vs Theta for MC (produced)");
         h.SetXTitle("\\Theta [\\circ]");
-        h.SetYTitle(Form("Counts normalized to r=%.0fm", impactmax/100));
+        h.SetYTitle(MString::Format("Counts normalized to r=%.0fm", impactmax/100));
     }
     h.SetDirectory(0);
@@ -682,4 +685,23 @@
 }
 
+TString MJSpectrum::GetHAlpha() const
+{
+    TString cls("MHAlpha");
+    if (!fDisplay)
+        return cls;
+
+    TCanvas *c = fDisplay->GetCanvas("Hist");
+    if (!c)
+        return cls;
+
+    TIter Next(c->GetListOfPrimitives());
+    TObject *obj=0;
+    while ((obj=Next()))
+        if (obj->InheritsFrom(MHAlpha::Class()))
+            break;
+
+    return obj ? TString(obj->ClassName()) : cls;
+}
+
 // --------------------------------------------------------------------------
 //
@@ -691,21 +713,4 @@
 Bool_t MJSpectrum::Refill(MParList &plist, TH1D &h2)/*const*/
 {
-    // Try to find the class used to determine the signal!
-    TString cls("MHAlpha");
-    if (fDisplay)
-    {
-        TCanvas *c = fDisplay->GetCanvas("Hist");
-        if (c)
-        {
-            TIter Next(c->GetListOfPrimitives());
-            TObject *obj=0;
-            while ((obj=Next()))
-                if (obj->InheritsFrom(MHAlpha::Class()))
-                    break;
-            if (obj)
-                cls = obj->ClassName();
-        }
-    }
-
     // Now fill the histogram
     *fLog << endl;
@@ -737,7 +742,9 @@
     fCutS = cont;
 
+    // Try to find the class used to determine the signal!
+    const TString cls = GetHAlpha();
+
     // FIXME: Create HistE and HistEOff to be able to modify it from
     // the resource file.
-
     MFillH fill1(Form("HistEOff [%s]", cls.Data()), "", "FillHistEOff");
     MFillH fill2(Form("HistE    [%s]", cls.Data()), "", "FillHistE");
@@ -1615,4 +1622,5 @@
     PrintSetup(fit);
 
+
     // ------------------------- Final loop --------------------------
 
@@ -1622,5 +1630,5 @@
 
     MTaskList tlist2;
-    plist.Replace(&tlist2);
+    plist.AddToList(&tlist2);
 
     MReadMarsFile read("Events");
@@ -1760,4 +1768,5 @@
     fillsp.SetWeight();
 
+    // Try to find the class used to determine the signal!
     // FIXME: To be done: A task checking the lower 1% after the lower
     // energy limit!
@@ -1776,7 +1785,8 @@
     SetupWriter(&write);
 
-    MParameterI par("DataType");
-    plist.AddToList(&par);
-    par.SetVal(2);
+    MParameterI *par = (MParameterI*)plist.FindCreateObj("MParameterI", "DataType");
+    if (!par)
+        return kFALSE;
+    par->SetVal(2);
 
     tlist2.AddToList(&read);
@@ -1844,9 +1854,9 @@
     }
 
-    gLog.Separator("Energy Estimator");
+    fLog->Separator("Energy Estimator");
     if (plist.FindObject("EstimateEnergy"))
         plist.FindObject("EstimateEnergy")->Print();
 
-    gLog.Separator("Spectrum");
+    fLog->Separator("Spectrum");
 
     // -------------------------- Spectrum ----------------------------
@@ -1856,5 +1866,5 @@
 
     // Spectrum fitted (convert res[1] from TeV to GeV)
-    TF1 flx("flx", Form("%e*pow(x/500, %f)", res[1]/500, res[0]));
+    TF1 flx("flx", MString::Format("%e*pow(x/500, %f)", res[1]/500, res[0]));
 
     // Number of events this spectrum would produce per s and m^2
@@ -1880,26 +1890,26 @@
 
     // check if output should be written
-    if (fPathOut.IsNull())
-        return kTRUE;
-
-    TNamed ganame("ganymed.root", fPathIn.Data());
-
-    // Write the output
-    TObjArray cont;
-    cont.Add((TObject*)GetEnv()); // const_cast
-    cont.Add((TObject*)&set);     // const_cast
-    cont.Add(plist.FindObject("MAlphaFitter"));
-    cont.Add(&area0);
-    cont.Add(&area1);
-    cont.Add(&hest);
-    cont.Add(&ganame);
-
-    if (fDisplay)
-        cont.Add(fDisplay);
-
-    if (!WriteContainer(cont, "", GetPathOut().IsNull()?"RECREATE":"UPDATE"))
-    {
-        *fLog << err << GetDescriptor() << ": Writing result failed." << endl;
-        return kFALSE;
+    if (!fPathOut.IsNull())
+    {
+        TNamed ganame("ganymed.root", fPathIn.Data());
+
+        // Write the output
+        TObjArray cont;
+        cont.Add((TObject*)GetEnv()); // const_cast
+        cont.Add((TObject*)&set);     // const_cast
+        cont.Add(plist.FindObject("MAlphaFitter"));
+        cont.Add(&area0);
+        cont.Add(&area1);
+        cont.Add(&hest);
+        cont.Add(&ganame);
+
+        if (fDisplay)
+            cont.Add(fDisplay);
+
+        if (!WriteContainer(cont, "", GetPathOut().IsNull()?"RECREATE":"UPDATE"))
+        {
+            *fLog << err << GetDescriptor() << ": Writing result failed." << endl;
+            return kFALSE;
+        }
     }
 
Index: trunk/MagicSoft/Mars/mjobs/MJSpectrum.h
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJSpectrum.h	(revision 9334)
+++ trunk/MagicSoft/Mars/mjobs/MJSpectrum.h	(revision 9335)
@@ -51,4 +51,5 @@
     Bool_t   ReadOrigMCDistribution(const MDataSet &set, TH1 &h, MMcSpectrumWeight &w) const;
     void     GetThetaDistribution(TH1D &temp1, TH2D &temp2) const;
+    TString  GetHAlpha() const;
     Bool_t   Refill(MParList &plist, TH1D &h) /*const*/;
     Bool_t   InitWeighting(const MDataSet &set, MMcSpectrumWeight &w) const;
