Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 2250)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 2251)
@@ -1,9 +1,21 @@
                                                  -*-*- END OF LINE -*-*-
 
+ 2003/06/30: Thomas Bretz
+ 
+   * macros/pixfirerate.C:
+     - changed to new style
+     
+   * manalysis/MCerPhotCalc.cc:
+     - scale weight so that sum=1
+     
+   * mhist/MHCamera.h:
+     - small change
+
+
+
  2003/06/27: Abelardo Moralejo
 
-   * mmontecarlo/MMcCollectionAreaCalc.[h,cc]:
-   * mhistmc/MHMcCollectionAreaCalc.[h,cc]:
-
+   * mmontecarlo/MMcCollectionAreaCalc.[h,cc],
+     mhistmc/MHMcCollectionAreaCalc.[h,cc]:
      - Adapted to allow their use with multiple files containing
        MC data generated with diffferent energy spectra, even with
@@ -13,4 +25,6 @@
        is done by CalcEfficiency2(). Some simplifications and cleaning 
        are still possible.
+
+
 
  2003/06/27: Thomas Bretz
Index: /trunk/MagicSoft/Mars/macros/pixfirerate.C
===================================================================
--- /trunk/MagicSoft/Mars/macros/pixfirerate.C	(revision 2250)
+++ /trunk/MagicSoft/Mars/macros/pixfirerate.C	(revision 2251)
@@ -36,5 +36,5 @@
 /////////////////////////////////////////////////////////////////////////////
 
-void pixfirerate(TString filename="20030603_01731_D_cosmics_E.root")
+void pixfirerate(TString filename="rawfile.root")
 {
     //
@@ -57,6 +57,6 @@
     // The tasklist is identified in the eventloop by its name
     //
+    MTaskList tlist;
     MParList  plist;
-    MTaskList tlist;
     plist.AddToList(&tlist);
 
@@ -73,31 +73,28 @@
     MReadMarsFile read("Events");
     read.DisableAutoScheme();
+    read.AddFile(filename);
+    tlist.AddToList(&read);
 
-    MHTrigLvl0 trigmap1(10.,"Above 10");
-    MHTrigLvl0 trigmap2(20.,"Above 20");
-    MHTrigLvl0 trigmap3(50.,"Above 50");
-    MHTrigLvl0 trigmap4(100.,"Above 100");
-    MHTrigLvl0 trigmap5(200.,"Above 200");
+    Double_t threshold[] = { 10, 20, 100, 200, -1 };
 
-    plist.AddToList(&trigmap1);
-    plist.AddToList(&trigmap2);
-    plist.AddToList(&trigmap3);
-    plist.AddToList(&trigmap4);
-    plist.AddToList(&trigmap5);
+    Int_t cnt = 0;
+    while (threshold[cnt]>0) cnt++;
 
-    MFillH fill1("Above 10","MRawEvtData");
-    MFillH fill2("Above 20","MRawEvtData");
-    MFillH fill3("Above 50","MRawEvtData");
-    MFillH fill4("Above 100","MRawEvtData");
-    MFillH fill5("Above 200","MRawEvtData");
+    for (int i=0; i<cnt; i++)
+    {
+        TString name = "Above ";
+        TString num;
+        num += threshold[i];
+        name += num.Strip(TString::kBoth);
+        TString title = "Firerate [%] of pixels with signal > ";
+        title += num.Strip(TString::kBoth);
 
-    read.AddFile(filename);
-
-    tlist.AddToList(&read);
-    tlist.AddToList(&fill1);
-    tlist.AddToList(&fill2);
-    tlist.AddToList(&fill3);
-    tlist.AddToList(&fill4);
-    tlist.AddToList(&fill5);
+        MHTriggerLvl0 *trigmap = new MHTriggerLvl0(threshold[i], name, title);
+        MFillH *fillh = new MFillH(trigmap, "MRawEvtData");
+        trigmap->SetBit(kCanDelete);
+        fillh->SetBit(kCanDelete);
+        plist.AddToList(trigmap);
+        tlist.AddToList(fillh);
+    }
 
     MEvtLoop evtloop;
Index: /trunk/MagicSoft/Mars/manalysis/MCerPhotAnal2.h
===================================================================
--- /trunk/MagicSoft/Mars/manalysis/MCerPhotAnal2.h	(revision 2251)
+++ /trunk/MagicSoft/Mars/manalysis/MCerPhotAnal2.h	(revision 2251)
@@ -0,0 +1,41 @@
+#ifndef MARS_MCerPhotAnal2
+#define MARS_MCerPhotAnal2
+
+/////////////////////////////////////////////////////////////////////////////
+//                                                                         //
+// MCerPhotAnal2                                                            //
+//                                                                         //
+// Integrates the time slices of one pixel and substracts the pedestal     //
+// (offset) value                                                          //
+//                                                                         //
+/////////////////////////////////////////////////////////////////////////////
+
+#ifndef MARS_MTask
+#include "MTask.h"
+#endif
+
+class MRawEvtData;
+class MPedestalCam;
+class MCerPhotEvt;
+class MRawRunHeader;
+
+class MCerPhotAnal2 : public MTask
+{
+    MPedestalCam  *fPedestals;  // Pedestals of all pixels in the camera
+    MRawEvtData   *fRawEvt;     // raw event data (time slices)
+    MCerPhotEvt   *fCerPhotEvt; // Cerenkov Photon Event used for calculation
+    MRawRunHeader *fRunHeader;  // RunHeader information
+
+    Byte_t fBefore;
+    Byte_t fAfter;
+
+public:
+    MCerPhotAnal2(Byte_t b=2, Byte_t a=5, const char *name=NULL, const char *title=NULL);
+
+    Int_t PreProcess(MParList *pList);
+    Int_t Process();
+
+    ClassDef(MCerPhotAnal2, 0)   // Task to calculate cerenkov photons from raw data
+};
+
+#endif
Index: /trunk/MagicSoft/Mars/manalysis/MCerPhotCalc.cc
===================================================================
--- /trunk/MagicSoft/Mars/manalysis/MCerPhotCalc.cc	(revision 2250)
+++ /trunk/MagicSoft/Mars/manalysis/MCerPhotCalc.cc	(revision 2251)
@@ -117,13 +117,15 @@
 
     // Calculate quadratic sum of weights:
-    fSumWeights = 0;
+    Doubla_t sum = 0;
+
     fSumQuadWeights = 0;
     for (Int_t i=0; i<fWeight.GetSize(); i++)
-    {
-        fSumWeights += fWeight[i];
+        sum += fWeight[i];
+
+    for (Int_t i=0; i<fWeight.GetSize(); i++)
+    {
+        fWeight[i] /= sum;
         fSumQuadWeights += fWeight[i]*fWeight[i];
     }
-
-    fSumQuadWeights = sqrt(fSumQuadWeights);
 
     return kTRUE;
@@ -180,5 +182,5 @@
 
         const Double_t offset = fEnableFix ? ped.GetPedestal()-0.5 : ped.GetPedestal();
-        ped.Set(offset*fSumWeights, ped.GetPedestalRms()*fSumQuadWeights);
+        ped.Set(offset, ped.GetPedestalRms()*sqrt(fSumQuadWeights));
     }
 
@@ -195,6 +197,6 @@
     //fCerPhotEvt->InitSize(fRawEvt->GetNumPixels());
 
-    if (fIsMcFile)
-        ScalePedestals();
+//    if (fIsMcFile)
+//        ScalePedestals();
 
     MRawEvtPixelIter pixel(fRawEvt);
Index: /trunk/MagicSoft/Mars/manalysis/MCerPhotCalc.h
===================================================================
--- /trunk/MagicSoft/Mars/manalysis/MCerPhotCalc.h	(revision 2250)
+++ /trunk/MagicSoft/Mars/manalysis/MCerPhotCalc.h	(revision 2251)
@@ -34,5 +34,4 @@
 
     TArrayF         fWeight;  // Weights for adding up the ADC slices
-    Float_t         fSumWeights;
     Float_t         fSumQuadWeights;
 
Index: /trunk/MagicSoft/Mars/manalysis/MPedestalSum.cc
===================================================================
--- /trunk/MagicSoft/Mars/manalysis/MPedestalSum.cc	(revision 2251)
+++ /trunk/MagicSoft/Mars/manalysis/MPedestalSum.cc	(revision 2251)
@@ -0,0 +1,227 @@
+/* ======================================================================== *\
+!
+! *
+! * 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 7/2002  <mailto:moralejo@pd.infn.it>
+!   Author(s): Thomas Bretz 2002  <mailto:tbretz@astro.uni-wuerzburg.de>
+!
+!   Copyright: MAGIC Software Development, 2002-2003
+!
+!
+\* ======================================================================== */
+
+//////////////////////////////////////////////////////////////////////////////
+//
+//   MPedestalSum
+//
+//   This is a task which calculates the number of photons from the FADC
+//   time slices. It weights the each slice according to the numbers in
+//   the array fWeight (default: all slices added up with weight 1).
+//
+//   The weights are rescaled, such that sum(weigths)=num slices
+//
+//  Input Containers:
+//   MRawRunHeader, MRawEvtData, MPedestalCam
+//
+//  Output Containers:
+//   MCerPhotEvt
+//
+//////////////////////////////////////////////////////////////////////////////
+#include "MPedestalSum.h"
+
+#include "MParList.h"
+
+#include "MLog.h"
+#include "MLogManip.h"
+
+#include "MMcRunHeader.hxx"
+
+#include "MRawRunHeader.h"
+#include "MRawEvtData.h"       // MRawEvtData::GetNumPixels
+#include "MCerPhotEvt.h"
+#include "MPedestalPix.h"
+#include "MPedestalCam.h"
+#include "MRawEvtPixelIter.h"
+
+ClassImp(MPedestalSum);
+
+using namespace std;
+
+// --------------------------------------------------------------------------
+//
+// Default constructor.
+//
+MPedestalSum::MPedestalSum(const char *name, const char *title)
+{
+    fName  = name  ? name  : "MPedestalSum";
+    fTitle = title ? title : "Calculate pixel signal from FADC data";
+
+    AddToBranchList("MRawEvtData.fHiGainPixId");
+    AddToBranchList("MRawEvtData.fLoGainPixId");
+    AddToBranchList("MRawEvtData.fHiGainFadcSamples");
+    AddToBranchList("MRawEvtData.fLoGainFadcSamples");
+
+    SetDefaultWeights();
+}
+
+// --------------------------------------------------------------------------
+//
+// The PreProcess searches for the following input containers:
+//  - MRawRunHeader
+//  - MRawEvtData
+//  - MPedestalCam
+//
+// The following output containers are also searched and created if
+// they were not found:
+//  - MCerPhotEvt
+//
+Int_t MPedestalSum::PreProcess(MParList *pList)
+{
+    fRunHeader = (MRawRunHeader*)pList->FindObject("MRawRunHeader");
+    if (!fRunHeader)
+    {
+        *fLog << dbginf << "MRawRunHeader not found... aborting." << endl;
+        return kFALSE;
+    }
+    fRawEvt = (MRawEvtData*)pList->FindObject("MRawEvtData");
+    if (!fRawEvt)
+    {
+        *fLog << dbginf << "MRawEvtData not found... aborting." << endl;
+        return kFALSE;
+    }
+
+    fCerPhotEvt = (MCerPhotEvt*)pList->FindCreateObj("MCerPhotEvt");
+    if (!fCerPhotEvt)
+    {
+        *fLog << dbginf << "MCerPhotEvt not found... aborting." << endl;
+        return kFALSE;
+    }
+
+    return kTRUE;
+}
+
+// --------------------------------------------------------------------------
+//
+// Check for the run type and camera version.
+// If the file is a MC file and the used camera version is <= 40
+// we enable a fix for truncated pedestal means in this version.
+//
+Bool_t MPedestalSum::ReInit(MParList *pList)
+{
+    /*
+    const MRawRunHeader *runheader = (MRawRunHeader*)pList->FindObject("MRawRunHeader");
+    if (!runheader)
+    {
+        *fLog << warn << dbginf << "Warning - cannot check file type, MRawRunHeader not found." << endl;
+        return kTRUE;
+    }
+
+    if (fRunHeader->GetNumSamplesHiGain() != fWeight.GetSize())
+    {
+        *fLog << dbginf << "Number of FADC slices (" << fRunHeader->GetNumSamplesHiGain() <<") is different from assumed one (" << fWeight.GetSize() << ")... aborting." << endl;
+        return kFALSE;
+    }
+
+    Bool_t fIsMcFile = runheader->GetRunType() == kRTMonteCarlo;
+    if (!fIsMcFile)
+        return kTRUE;
+
+    ScalePedestals();
+
+    MMcRunHeader *mcrunheader = (MMcRunHeader*)pList->FindObject("MMcRunHeader");
+    if (!mcrunheader)
+    {
+        *fLog << warn << dbginf << "Warning - cannot check for camera version, MC run header not found." << endl;
+        return kTRUE;
+    }
+
+    fEnableFix = kFALSE;
+    if (mcrunheader->GetCamVersion() <= 40)
+        fEnableFix = kTRUE;
+     */
+    return kTRUE;
+}
+
+void MPedestalSum::ScalePedestals()
+{
+    Int_t n = 577; //FIXME: fPedestals->GetNumPixel();
+
+    for (int idx=0; idx<n; idx++)
+    {
+        MPedestalPix &ped = (*fPedestals)[idx];
+
+        const Double_t offset = fEnableFix ? ped.GetPedestal()-0.5 : ped.GetPedestal();
+        ped.Set(offset*fSumWeights, ped.GetPedestalRms()*sqrt(fSumQuadWeights));
+    }
+
+    fPedestals->SetReadyToSave();
+}
+
+// --------------------------------------------------------------------------
+//
+// Calculate the integral of the FADC time slices and store them as a new
+// pixel in the MCerPhotEvt container.
+//
+Int_t MPedestalSum::Process()
+{
+    //fCerPhotEvt->InitSize(fRawEvt->GetNumPixels());
+
+//    if (fIsMcFile)
+//        ScalePedestals();
+
+    MRawEvtPixelIter pixel(fRawEvt);
+
+    while (pixel.Next())
+    {
+        const UInt_t idx = pixel.GetPixelId();
+
+        const int n = fRunHeader->GetNumSamplesHiGain();
+
+        //
+        // Calculate pixel signal unless it has all FADC slices empty:
+        //
+        Byte_t *ptr = pixel.GetHiGainSamples();
+
+        Float_t nphot = 0;
+        Float_t nerr  = 0;
+        for(Int_t i=0; i<n; i++)
+        {
+            nphot += ptr[i];
+            nerr  += ptr[i]*ptr[i];
+        }
+
+        nphot /= n;
+
+        fCerPhotEvt->AddPixel(idx, nphot, sqrt(nerr/n-nphot*nphot));
+
+        // FIXME! Handling of Lo Gains is missing!
+    }
+
+    fCerPhotEvt->FixSize();
+    fCerPhotEvt->SetReadyToSave();
+
+    return kTRUE;
+}
+
+// --------------------------------------------------------------------------
+//
+// Set default values for the number of slices and weights:
+//
+void MPedestalSum::SetDefaultWeights()
+{
+    const Float_t dummy[15] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
+    fWeight.Set(15, dummy);
+}
Index: /trunk/MagicSoft/Mars/mhist/MHCamera.h
===================================================================
--- /trunk/MagicSoft/Mars/mhist/MHCamera.h	(revision 2250)
+++ /trunk/MagicSoft/Mars/mhist/MHCamera.h	(revision 2251)
@@ -93,5 +93,5 @@
     virtual void     SetCamContent(const MCamEvent &evt, Int_t type=0) { Reset(); AddCamContent(evt, type); }
     virtual void     SetCamContent(const TArrayD &evt, const TArrayC *used=NULL) { Reset(); AddCamContent(evt, used); }
-    virtual void     SetCamContent(const MHCamera &d, Int_t type=0) { Reset(); fEntries=d.fEntries-1; AddCamContent(d, type); }
+    virtual void     SetCamContent(const MHCamera &d, Int_t type=0) { Reset(); AddCamContent(d, type); fEntries=d.fEntries; }
     virtual void     CntCamContent(const MCamEvent &evt, Double_t threshold, Int_t type=0);
     virtual void     CntCamContent(const TArrayD &evt, Double_t threshold, Bool_t ispos=kTRUE);
