Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 2453)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 2454)
@@ -1,17 +1,45 @@
                                                  -*-*- END OF LINE -*-*-
+  2003/11/03: Thomas Bretz
+  
+   * manalysis/MMcPedestalCopy.[h,cc], manalysis/MMcPedestalNSBAdd.[h,cc]:
+     - fixed such, that it now works correctly with non-MC files
+     - moved container requests from PreProcess to ReInit
+     - removed some obsolete data members - made them locally
+     
+   * manalysis/MMcPedestalRead.[h,cc]:
+     - removed
+     
+   * mbase/MEvtLoop.cc:
+     - replaced the gApplication->InheritsFrom(TRint::Class())
+       workaround for thread safty by the more correct check
+       whether we are running in the main Thread (TThread::Self())
+       
+   * mbase/MTask.h:
+     - added the missing const-qualifier to GetNumExecutions
+     
+   * mbase/MTaskList.cc:
+     - fixed a typo in the output
+     
+   * mimage/MHillasCalc.[h,cc]:
+     - replaced TArrayC by TArrayL to support huge number of events
+     - added PrintSkipped
+     - added comments to the data members
+
+
+
  2003/10/31: Marcos Lopez
 
-   * mhist/MFill.cc:
+   * mhist/MFillH.cc:
      - Fixed a bug in function PreProcess(MParList *pList). Inside the 
        conditional sentence "if (!fWeight && !fWeightName.IsNull())",  
        fWeight never pointed to the object MWeight recoverd from the 
        parameter list.
-  
-    * mhistmc/MHMcEnergyImpact.cc  
-      - In the Fill function, pass the weight to the histogram fHist.
-
-   * mmontecarlo/MMcWeightEnergySpecCalc.[h,cc] 
-      - Added new class for changing the energy spectrum of the showers 
-	simulated with Corsika to a different one, be using weights   
+
+   * mhistmc/MHMcEnergyImpact.cc:
+     - In the Fill function, pass the weight to the histogram fHist.
+
+   * mmontecarlo/MMcWeightEnergySpecCalc.[h,cc]:
+     - Added new class for changing the energy spectrum of the showers 
+       simulated with Corsika to a different one, be using weights   
 			
    * mmontecarlo/Makefile, MonteCarloLinkDef.h
Index: /trunk/MagicSoft/Mars/manalysis/MMcPedestalCopy.cc
===================================================================
--- /trunk/MagicSoft/Mars/manalysis/MMcPedestalCopy.cc	(revision 2453)
+++ /trunk/MagicSoft/Mars/manalysis/MMcPedestalCopy.cc	(revision 2454)
@@ -89,18 +89,29 @@
 // --------------------------------------------------------------------------
 //
+// Check for the runtype.
 // Search for MPedestalCam and MMcFadcHeader.
 //
-Int_t MMcPedestalCopy::PreProcess(MParList *pList)
+Bool_t MMcPedestalCopy::ReInit(MParList *pList)
 {
-    fMcRun = (MMcRunHeader*)pList->FindObject("MMcRunHeader");
-    if (!fMcRun)
+    //
+    // If it is no MC file skip this function...
+    //
+    if (!CheckRunType(pList))
+        return kTRUE;
+
+    //
+    // Now check the existance of all necessary containers. This has
+    // to be done only if this is a MC file.
+    //
+    MPedestalCam *pedcam = (MPedestalCam*)pList->FindCreateObj("MPedestalCam");
+    if (!pedcam)
+        return kFALSE;
+
+    MMcRunHeader *mcrun = (MMcRunHeader*)pList->FindObject("MMcRunHeader");
+    if (!mcrun)
         *fLog << warn << dbginf << "MMcRunHeader not found... assuming camera<0.7" << endl;
 
-    fPedCam = (MPedestalCam*)pList->FindCreateObj("MPedestalCam");
-    if (!fPedCam)
-        return kFALSE;
-
-    fMcPed = (MMcFadcHeader*)pList->FindObject("MMcFadcHeader");
-    if (!fMcPed)
+    MMcFadcHeader *fadc = (MMcFadcHeader*)pList->FindObject("MMcFadcHeader");
+    if (!fadc)
     {
         *fLog << err << dbginf << "MMcFadcHeader not found... aborting." << endl;
@@ -108,31 +119,18 @@
     }
 
-    return kTRUE;
-}
+    const int num = pedcam->GetSize();
 
-// --------------------------------------------------------------------------
-//
-// Check for the runtype.
-// Initialize the size of MPedestalCam to the number of pixels from
-// MMcFadcHeader.
-//
-Bool_t MMcPedestalCopy::ReInit(MParList *pList)
-{
-    if (!CheckRunType(pList))
-        return kFALSE;
-
-    const int num = fPedCam->GetSize();
-
-    const Bool_t camver70 = fMcRun && fMcRun->GetCamVersion()>=70;
+    const Bool_t camver70 = mcrun && mcrun->GetCamVersion()>=70;
 
     for (int i=0; i<num; i++)
     {
-        MPedestalPix &pix = (*fPedCam)[i];
+        MPedestalPix &pix = (*pedcam)[i];
 
         // Here one should compute the Pedestal taking into account how
         // the MC makes the transformation analogic-digital for the FADC.
+        // This is done only once per file -> not time critical.
 
-        const Float_t pedest = fMcPed->GetPedestal(i);
-        const Float_t sigma  = camver70 ? fMcPed->GetPedestalRmsHigh(i) : fMcPed->GetElecNoise(i);
+        const Float_t pedest = fadc->GetPedestal(i);
+        const Float_t sigma  = camver70 ? fadc->GetPedestalRmsHigh(i) : fadc->GetElecNoise(i);
 
         pix.Set(pedest, sigma);
@@ -140,5 +138,5 @@
 
     if (camver70)
-        fPedCam->SetReadyToSave();
+        pedcam->SetReadyToSave();
 
     return kTRUE;
Index: /trunk/MagicSoft/Mars/manalysis/MMcPedestalCopy.h
===================================================================
--- /trunk/MagicSoft/Mars/manalysis/MMcPedestalCopy.h	(revision 2453)
+++ /trunk/MagicSoft/Mars/manalysis/MMcPedestalCopy.h	(revision 2454)
@@ -6,19 +6,8 @@
 #endif
 
-class MMcFadcHeader;
-class MMcRunHeader;
-class MPedestalCam;
-
 class MMcPedestalCopy : public MTask
 {
 private:
-    const MMcFadcHeader *fMcPed;
-    const MMcRunHeader  *fMcRun;
-
-    MPedestalCam *fPedCam;
-
     Bool_t CheckRunType(MParList *pList) const;
-
-    Int_t  PreProcess(MParList *pList);
     Bool_t ReInit(MParList *pList);
 
Index: /trunk/MagicSoft/Mars/manalysis/MMcPedestalNSBAdd.cc
===================================================================
--- /trunk/MagicSoft/Mars/manalysis/MMcPedestalNSBAdd.cc	(revision 2453)
+++ /trunk/MagicSoft/Mars/manalysis/MMcPedestalNSBAdd.cc	(revision 2454)
@@ -125,34 +125,4 @@
 // --------------------------------------------------------------------------
 //
-// - check whether we have a monte carlo file. if not skip this task
-// - try to get MMcFadcHeader, MGeomCam and MPedestalCam from the parameter
-//   list
-// - try to find a MMcRunHeader, too
-//
-Int_t MMcPedestalNSBAdd::PreProcess(MParList *pList)
-{
-    fFadc = (MMcFadcHeader*)pList->FindObject("MMcFadcHeader");
-    if (!fFadc)
-    {
-        *fLog << err << dbginf << "MMcFadcHeader not found... aborting." << endl;
-	return kFALSE;
-    }
-
-    fGeom = (MGeomCam*)pList->FindObject("MGeomCam");
-    if (!fGeom)
-    {
-        *fLog << err << dbginf << "MGeomCam not found... aborting." << endl;
-	return kFALSE;
-    }
-
-    fPedCam = (MPedestalCam*)pList->FindCreateObj("MPedestalCam");
-    if (!fPedCam)
-	return kFALSE;
-
-    return kTRUE;
-}
-
-// --------------------------------------------------------------------------
-//
 // If a MMcRunHeader is available the DNSB MMcRunHeader::GetNumPheFromDNSB
 // is returned. Otherwise the user given number is used.
@@ -199,10 +169,39 @@
 Bool_t MMcPedestalNSBAdd::ReInit(MParList *pList)
 {
+    //
+    // If it is no MC file skip this function...
+    //
     if (!CheckRunType(pList))
-        return kFALSE;
-
+        return kTRUE;
+
+    //
+    // If it is the wrong camera version this algorithm is not needed...
+    //
     if (!CheckCamVersion(pList))
         return kTRUE;
 
+    //
+    // Now check the existance of all necessary containers. This has
+    // to be done only if this is a MC file and the camera version
+    // is correct.
+    //
+    MPedestalCam *pedcam = (MPedestalCam*)pList->FindCreateObj("MPedestalCam");
+    if (!pedcam)
+	return kFALSE;
+
+    MMcFadcHeader *fadc = (MMcFadcHeader*)pList->FindObject("MMcFadcHeader");
+    if (!fadc)
+    {
+        *fLog << err << dbginf << "MMcFadcHeader not found... aborting." << endl;
+	return kFALSE;
+    }
+
+    MGeomCam *geom = (MGeomCam*)pList->FindObject("MGeomCam");
+    if (!geom)
+    {
+        *fLog << err << dbginf << "MGeomCam not found... aborting." << endl;
+	return kFALSE;
+    }
+
     const Float_t dnsbpix = GetDnsb(pList) * 50.0/15.0;
 
@@ -210,20 +209,19 @@
         return kFALSE;
 
-    const int num = fPedCam->GetSize();
+    const int num = pedcam->GetSize();
 
     for (int i=0; i<num; i++)
     {
-        MPedestalPix &pix    = (*fPedCam)[i];
+        MPedestalPix &pix    = (*pedcam)[i];
 
         const Float_t pedrms = pix.GetPedestalRms();
-        const Float_t ratio  = fGeom->GetPixRatio(i);
-        const Float_t ampl   = fFadc->GetAmplitud();
+        const Float_t ratio  = geom->GetPixRatio(i);
+        const Float_t ampl   = fadc->GetAmplitud();
 
 	pix.SetPedestalRms(sqrt(pedrms*pedrms + dnsbpix*ampl*ampl/ratio));
     }
 
-    fPedCam->SetReadyToSave();
+    pedcam->SetReadyToSave();
 
     return kTRUE;
 }
-
Index: /trunk/MagicSoft/Mars/manalysis/MMcPedestalNSBAdd.h
===================================================================
--- /trunk/MagicSoft/Mars/manalysis/MMcPedestalNSBAdd.h	(revision 2453)
+++ /trunk/MagicSoft/Mars/manalysis/MMcPedestalNSBAdd.h	(revision 2454)
@@ -6,16 +6,7 @@
 #endif
 
-class MMcFadcHeader;
-class MGeomCam;
-class MPedestalCam;
-
 class MMcPedestalNSBAdd : public MTask
 {
 private:
-    const MGeomCam      *fGeom;
-    const MMcFadcHeader *fFadc;
-
-    MPedestalCam *fPedCam;
-
     Float_t fDnsbPixel;
 
@@ -25,7 +16,5 @@
     Float_t GetDnsb(MParList *pList) const;
 
-    Int_t  PreProcess(MParList *pList);
     Bool_t ReInit(MParList *pList);
-
 
 public:
Index: unk/MagicSoft/Mars/manalysis/MMcPedestalRead.cc
===================================================================
--- /trunk/MagicSoft/Mars/manalysis/MMcPedestalRead.cc	(revision 2453)
+++ 	(revision )
@@ -1,147 +1,0 @@
-/* ======================================================================== *\
-!
-! *
-! * This file is part of MARS, the MAGIC Analysis and Reconstruction
-! * Software. It is distributed to you in the hope that it can be a useful
-! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
-! * It is distributed WITHOUT ANY WARRANTY.
-! *
-! * Permission to use, copy, modify and distribute this software and its
-! * documentation for any purpose is hereby granted without fee,
-! * provided that the above copyright notice appear in all copies and
-! * that both that copyright notice and this permission notice appear
-! * in supporting documentation. It is provided "as is" without express
-! * or implied warranty.
-! *
-!
-!
-!   Author(s): Abelardo Moralejo  12/2003 <mailto:moralejo@pd.infn.it>
-!
-!   Copyright: MAGIC Software Development, 2000-2001
-!
-!
-\* ======================================================================== */
-
-/////////////////////////////////////////////////////////////////////////////
-//                                                                         //
-//  MMcPedestalRead                                                        //
-//                                                                         //
-//  This task looks for the ìnformation about FADC pedestals in            //
-//  MMcFadcHeader and translates it to the pedestal values in              //
-//  MPedestalCam. It is intended for camera >= 0.7 files                   //
-//                                                                         //
-//                                                                         //
-//  Input Containers:                                                      //
-//   MMcFadcHeader                                                         //
-//                                                                         //
-//  Output Containers:                                                     //
-//   MPedestalCam                                                          //
-//                                                                         //
-/////////////////////////////////////////////////////////////////////////////
-
-#include "MMcPedestalRead.h"
-
-#include "MParList.h"
-
-#include "MLog.h"
-#include "MLogManip.h"
-
-#include "MPedestalPix.h"
-#include "MPedestalCam.h"
-
-#include "MRawRunHeader.h"
-#include "MMcFadcHeader.hxx"
-
-ClassImp(MMcPedestalRead);
-
-using namespace std;
-
-MMcPedestalRead::MMcPedestalRead(const char *name, const char *title)
-{
-    fName  = name  ? name  : "MMcPedestalRead";
-    fTitle = title ? title : "Copy MC pedestals into MPedestal Container";
-
-    //
-    // This is not needed here using MReadMarsFile because for the
-    // RunHeader tree the auto scheme is disabled by default
-    //
-
-    AddToBranchList("MMcFadcHeader.fPedesMean");
-    AddToBranchList("MMcFadcHeader.fPedesSigmaHigh");
-
-    // FIXME: may be we'll have to take into account the low gain 
-    // pedestal sigma in the future.
-}
-
-// --------------------------------------------------------------------------
-//
-// Check for the run type. Return kTRUE if it is a MC run or if there
-// is no MC run header (old camera files) kFALSE in case of a different
-// run type
-//
-Bool_t MMcPedestalRead::CheckRunType(MParList *pList) const
-{
-    const MRawRunHeader *run = (MRawRunHeader*)pList->FindObject("MRawRunHeader");
-    if (!run)
-    {
-        *fLog << warn << dbginf << "Warning - cannot check file type, MRawRunHeader not found." << endl;
-        return kTRUE;
-    }
-
-    return run->GetRunType() == kRTMonteCarlo;
-}
-
-// --------------------------------------------------------------------------
-//
-// Check runtype and search for MPedestalCam and MMcFadcHeader.
-// If the runtype check fails the task is removed from the task list.
-//
-Int_t MMcPedestalRead::PreProcess(MParList *pList)
-{
-    if (!CheckRunType(pList))
-    {
-        *fLog << warn << dbginf << " MMcPedestalRead is for Monte Carlo files only... ";
-        *fLog << "removing task from list." << endl;
-        return kSKIP;
-    }
-
-    fPedCam = (MPedestalCam*)pList->FindCreateObj("MPedestalCam");
-    if (!fPedCam)
-        return kFALSE;
-
-    fMcPed = (MMcFadcHeader*)pList->FindObject("MMcFadcHeader");
-    if (!fMcPed)
-    {
-        *fLog << warn << dbginf << "MMcFadcHeader not found... aborting." << endl;
-        return kFALSE;
-    }
-
-    return kTRUE;
-}
-
-// --------------------------------------------------------------------------
-//
-// Check for the runtype.
-// Initialize the size of MPedestalCam to the number of pixels from
-// MMcFadcHeader.
-//
-Bool_t MMcPedestalRead::ReInit(MParList *pList)
-{
-    if (!CheckRunType(pList))
-        return kFALSE;
-
-    const int num = fPedCam->GetSize();
-
-    for (int i=0; i<num; i++)
-    {
-        MPedestalPix &pix = (*fPedCam)[i];
-
-        const Float_t pedest = fMcPed->GetPedestal(i);
-        const Float_t sigma  = fMcPed->GetPedestalRmsHigh(i);
-
-        pix.Set(pedest, sigma);
-    }
-
-    return kTRUE;
-}
-
Index: unk/MagicSoft/Mars/manalysis/MMcPedestalRead.h
===================================================================
--- /trunk/MagicSoft/Mars/manalysis/MMcPedestalRead.h	(revision 2453)
+++ 	(revision )
@@ -1,30 +1,0 @@
-#ifndef MARS_MMcPedestalRead
-#define MARS_MMcPedestalRead
-
-#ifndef MARS_MTask
-#include "MTask.h"
-#endif
-
-class MMcFadcHeader;
-class MPedestalCam;
-
-class MMcPedestalRead : public MTask
-{
-private:
-    const MMcFadcHeader *fMcPed;
-    MPedestalCam *fPedCam;
-
-    Bool_t CheckRunType(MParList *pList) const;
-
-    Int_t PreProcess(MParList *pList);
-    Bool_t ReInit(MParList *pList);
-
-public:
-    MMcPedestalRead(const char *name=NULL, const char *title=NULL);
-
-    ClassDef(MMcPedestalRead, 0)
-      // Task which copies the pedestals from the MC FADC header 
-      // into the standard container (intended for camera >= 0.7 files)
-};
-
-#endif
Index: /trunk/MagicSoft/Mars/mbase/MEvtLoop.cc
===================================================================
--- /trunk/MagicSoft/Mars/mbase/MEvtLoop.cc	(revision 2453)
+++ /trunk/MagicSoft/Mars/mbase/MEvtLoop.cc	(revision 2454)
@@ -75,4 +75,5 @@
 #include <TTime.h>          // TTime
 #include <TFile.h>          // gFile
+#include <TThread.h>        // TThread::Self()
 #include <TDatime.h>        // TDatime
 #include <TSystem.h>        // gSystem
@@ -372,13 +373,17 @@
         fProgress->SetPosition(num/fNumEvents);
 
-    //
-    // Handle GUI events (display changes)
-    //
+
+    // FIXME: This is a workaround, because TApplication::Run is not
+    //        thread safe against ProcessEvents. We assume, that if
+    //        we are not in the Main-Thread ProcessEvents() is
+    //        called by the TApplication Event Loop...
+    if (!TThread::Self()/*gApplication->InheritsFrom(TRint::Class())*/)
+    {
+        //
+        // Handle GUI events (display changes)
+        //
 #if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
-    gSystem->ProcessEvents();
+        gSystem->ProcessEvents();
 #else
-    // FIXME: This is a workaround, because TApplication::Run is not thread safe against ProcessEvents
-    if (gApplication->InheritsFrom(TRint::Class()))
-    {
         if (fDisplay)
             gSystem->ProcessEvents();
@@ -386,6 +391,6 @@
             if (fProgress)
                 gClient->ProcessEventsFor(fProgress);
-    }
 #endif
+    }
 
     return rc;
@@ -485,6 +490,10 @@
         //fProgress->SetPosition(maxcnt>0 ? TMath::Min(maxcnt, entries) : entries);
         fProgress->SetPosition(1);
-        // FIXME: This is a workaround, because TApplication::Run is not thread safe against ProcessEvents
-        if (gApplication->InheritsFrom(TRint::Class()))
+
+        // FIXME: This is a workaround, because TApplication::Run is not
+        //        thread safe against ProcessEvents. We assume, that if
+        //        we are not in the Main-Thread ProcessEvents() is
+        //        called by the TApplication Event Loop...
+        if (!TThread::Self()/*gApplication->InheritsFrom(TRint::Class())*/)
         {
 #if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
Index: /trunk/MagicSoft/Mars/mbase/MTask.h
===================================================================
--- /trunk/MagicSoft/Mars/mbase/MTask.h	(revision 2453)
+++ /trunk/MagicSoft/Mars/mbase/MTask.h	(revision 2454)
@@ -71,5 +71,5 @@
     virtual void PrintStatistics(const Int_t lvl=0, Bool_t title=kFALSE) const;
 
-    UInt_t GetNumExecutions() { return fNumExecutions; }
+    UInt_t GetNumExecutions() const { return fNumExecutions; }
 
     virtual Bool_t ReInit(MParList *pList);
Index: /trunk/MagicSoft/Mars/mbase/MTaskList.cc
===================================================================
--- /trunk/MagicSoft/Mars/mbase/MTaskList.cc	(revision 2453)
+++ /trunk/MagicSoft/Mars/mbase/MTaskList.cc	(revision 2454)
@@ -308,5 +308,5 @@
         if (!task->ReInit(pList?pList:fParList))
         {
-            *fLog << err << "ERROR - ReInit if Task " << task->GetDescriptor() << " failed." << endl;
+            *fLog << err << "ERROR - ReInit of Task '" << task->GetDescriptor() << "' failed." << endl;
             return kFALSE;
         }
Index: /trunk/MagicSoft/Mars/mimage/MHillasCalc.cc
===================================================================
--- /trunk/MagicSoft/Mars/mimage/MHillasCalc.cc	(revision 2453)
+++ /trunk/MagicSoft/Mars/mimage/MHillasCalc.cc	(revision 2454)
@@ -173,4 +173,16 @@
 // --------------------------------------------------------------------------
 //
+//  This is used to print the output in the PostProcess. Later (having
+//  millions of events) we may want to improve the output.
+//
+void MHillasCalc::PrintSkipped(int i, const char *str) const
+{
+    *fLog << " " << setw(7) << fErrors[i] << " (";
+    *fLog << setw(3) << (int)(100.*fErrors[i]/GetNumExecutions());
+    *fLog << "%) Evts skipped due to: " << str << endl;
+}
+
+// --------------------------------------------------------------------------
+//
 //  Prints some statistics about the hillas calculation. The percentage
 //  is calculated with respect to the number of executions of this task.
@@ -184,9 +196,9 @@
     *fLog << GetDescriptor() << " execution statistics:" << endl;
     *fLog << dec << setfill(' ');
-    *fLog << " " << setw(7) << fErrors[1] << " (" << setw(3) << (int)(fErrors[1]*100/GetNumExecutions()) << "%) Evts skipped due to: Event has less than 3 pixels" << endl;
-    *fLog << " " << setw(7) << fErrors[2] << " (" << setw(3) << (int)(fErrors[2]*100/GetNumExecutions()) << "%) Evts skipped due to: Calculated Size == 0" << endl;
-    *fLog << " " << setw(7) << fErrors[3] << " (" << setw(3) << (int)(fErrors[3]*100/GetNumExecutions()) << "%) Evts skipped due to: Number of used pixels < 3" << endl;
-    *fLog << " " << setw(7) << fErrors[4] << " (" << setw(3) << (int)(fErrors[4]*100/GetNumExecutions()) << "%) Evts skipped due to: CorrXY==0" << endl;
-    *fLog << " " << fErrors[0] << " (" << (int)(fErrors[0]*100/GetNumExecutions()) << "%) Evts survived Hillas calculation!" << endl;
+    PrintSkipped(1, "Event has less than 3 pixels");
+    PrintSkipped(2, "Calculated Size == 0");
+    PrintSkipped(3, "Number of used pixels < 3");
+    PrintSkipped(4, "CorrXY==0");
+    *fLog << " " << (int)fErrors[0] << " (" << (int)(100.*fErrors[0]/GetNumExecutions()) << "%) Evts survived Hillas calculation!" << endl;
     *fLog << endl;
 
