Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 2836)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 2837)
@@ -13,7 +13,12 @@
        constants defined in MCalibrate
 
+   * manalysis/AnalysisLinkDef.h,  manalysis/Makefile
+     - added entries for MPedPhotCalc
+
    * macros/pedphotcalc.C
      - added
      - example on how to use MPedPhotCalc task
+
+
    
 
Index: /trunk/MagicSoft/Mars/manalysis/AnalysisLinkDef.h
===================================================================
--- /trunk/MagicSoft/Mars/manalysis/AnalysisLinkDef.h	(revision 2836)
+++ /trunk/MagicSoft/Mars/manalysis/AnalysisLinkDef.h	(revision 2837)
@@ -19,4 +19,5 @@
 #pragma link C++ class MPedestalCam+;
 #pragma link C++ class MPedCalcPedRun+;
+#pragma link C++ class MPedPhotCalc+;
 #pragma link C++ class MMcPedestalCopy+;
 #pragma link C++ class MMcPedestalNSBAdd+;
Index: /trunk/MagicSoft/Mars/manalysis/MPedPhotCalc.cc
===================================================================
--- /trunk/MagicSoft/Mars/manalysis/MPedPhotCalc.cc	(revision 2837)
+++ /trunk/MagicSoft/Mars/manalysis/MPedPhotCalc.cc	(revision 2837)
@@ -0,0 +1,147 @@
+/* ======================================================================== *\
+!
+! *
+! * 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): Josep Flix  <mailto:jflix@ifae.es>
+!              Javier Rico <mailto:jrico@ifae.es> 
+!              (01/04)
+!
+!   Copyright: MAGIC Software Development, 2000-2004
+!
+!
+\* ======================================================================== */
+
+/////////////////////////////////////////////////////////////////////////////
+//                                                                         //
+//   MPedPhotCalc                                                          //
+//                                                                         //  
+//  This is a Task class to compute, for each pixel, the signal mean and   //
+//  rms from a pedestal run file that has undergone the standard signal    //
+//  extration  and calibration procedure. The signal rms can be used as    //
+//  reference to compute the significance of the measured signals in the   //
+//  following data runs (e.g. during the image cleaning).                  //
+//                                                                         //
+//  Input Containers:                                                      //
+//   MCerPhotEvt                                                           //
+//                                                                         //
+//  Output Containers:                                                     //
+//   MPedPhotCam 
+//                                                                         //
+//                                                                         //
+/////////////////////////////////////////////////////////////////////////////
+
+#include "MLog.h"
+#include "MLogManip.h"
+
+#include "MPedPhotCalc.h"
+#include "MParList.h"
+#include "MRawRunHeader.h"  
+#include "MCerPhotPix.h"
+#include "MCerPhotEvt.h"
+#include "MPedPhotPix.h"
+#include "MPedPhotCam.h"
+
+ClassImp(MPedPhotCalc);
+
+using namespace std;
+
+MPedPhotCalc::MPedPhotCalc(const char *name, const char *title)
+{
+  fName  = name  ? name  : "MPedPhotCalc";
+  fTitle = title ? title : "Task to calculate pedestals from the charge computed from pedestal runs (in units of photons)";
+}
+
+Int_t MPedPhotCalc::PreProcess( MParList *pList )
+{      
+  // Look for input container
+  fCerPhot = (MCerPhotEvt*)pList->FindObject("MCerPhotEvt");
+  if (!fCerPhot)
+    {
+      *fLog << err << "MPedPhotCalc::PreProcess Error: MCerPhotEvt not found... aborting." << endl;
+      return kFALSE;
+    }
+
+  // Create output container
+  fPedestals = (MPedPhotCam*)pList->FindCreateObj("MPedPhotCam");
+  if (!fPedestals)
+    return kFALSE;
+  
+  return kTRUE;
+}
+
+Bool_t MPedPhotCalc::ReInit(MParList *pList)
+{
+  const MRawRunHeader *runheader = (MRawRunHeader*)pList->FindObject("MRawRunHeader");
+  if (!runheader)
+    {
+      *fLog << warn << dbginf;
+      *fLog << "Warning - cannot check file type, MRawRunHeader not found." << endl;
+    }
+  else
+    if (runheader->GetRunType() == kRTMonteCarlo)
+      return kTRUE;
+  
+
+  fNumPixels = fPedestals->GetSize();
+
+  // Initialize arrays
+  if(fSumx.GetSize()==0)
+    {
+      fSumx.Set(fNumPixels);
+      fSumx2.Set(fNumPixels);
+  
+      memset(fSumx.GetArray(),  0, sizeof(Float_t)*fNumPixels);
+      memset(fSumx2.GetArray(), 0, sizeof(Float_t)*fNumPixels);
+    }
+
+    return kTRUE;
+}
+
+Int_t MPedPhotCalc::Process()
+{
+  for(UInt_t i=0;i<fCerPhot->GetNumPixels();i++)
+    {
+      MCerPhotPix &pix = (*fCerPhot)[i];
+      Float_t nphot = pix.GetNumPhotons();
+      Int_t idx     = pix.GetPixId();
+      
+      fSumx[idx]  += nphot;
+      fSumx2[idx] += nphot*nphot;
+    }
+  
+  fPedestals->SetReadyToSave();
+  
+  return kTRUE;
+}
+
+Int_t MPedPhotCalc::PostProcess()
+  {
+    // Compute pedestals and rms from fSumx and fSumx2 arrays
+    const Int_t n  = GetNumExecutions();
+
+    for(Int_t i=0;i<fNumPixels;i++)
+      {
+        const Float_t sum  = fSumx.At(i);
+	const Float_t sum2 = fSumx2.At(i);
+	
+        const Float_t photped = sum/n;
+        const Float_t photrms = TMath::Sqrt((sum2-sum*sum/n)/(n-1.));
+
+	(*fPedestals)[i].Set(photped,photrms);	
+      }
+    
+    return kTRUE;
+}
Index: /trunk/MagicSoft/Mars/manalysis/MPedPhotCalc.h
===================================================================
--- /trunk/MagicSoft/Mars/manalysis/MPedPhotCalc.h	(revision 2837)
+++ /trunk/MagicSoft/Mars/manalysis/MPedPhotCalc.h	(revision 2837)
@@ -0,0 +1,44 @@
+#ifndef MARS_MPedPhotCalc
+#define MARS_MPedPhotCalc
+
+/////////////////////////////////////////////////////////////////////////////
+//                                                                         //
+// MPedPhotCalc                                                            //
+//                                                                         //
+// Evaluate the pedestals from pedestal runs using charge extraction       //
+//                                                                         //
+/////////////////////////////////////////////////////////////////////////////
+
+#ifndef MARS_MTask
+#include "MTask.h"
+#endif
+
+#include <TArrayF.h>
+
+class MPedPhotCam;
+class MRawEvtData;
+class MCerPhotEvt;
+
+class MPedPhotCalc : public MTask
+{
+    UShort_t fNumPixels;
+
+    MPedPhotCam  *fPedestals;  // Pedestals of all pixels in the camera
+    MCerPhotEvt  *fCerPhot;
+
+    TArrayF fSumx;   // sum of values
+    TArrayF fSumx2;  // sum of squared values
+
+    Bool_t ReInit(MParList *pList);
+
+    Int_t PreProcess(MParList *pList);
+    Int_t Process();
+    Int_t PostProcess();
+
+public:
+    MPedPhotCalc(const char *name=NULL, const char *title=NULL);
+
+    ClassDef(MPedPhotCalc, 0)   // Task to calculate pedestals from pedestal runs raw data
+};
+
+#endif
Index: /trunk/MagicSoft/Mars/manalysis/Makefile
===================================================================
--- /trunk/MagicSoft/Mars/manalysis/Makefile	(revision 2836)
+++ /trunk/MagicSoft/Mars/manalysis/Makefile	(revision 2837)
@@ -37,4 +37,5 @@
            MPedPhotCam.cc \
            MPedCalcPedRun.cc \
+           MPedPhotCalc.cc \
            MMcPedestalCopy.cc \
            MMcPedestalNSBAdd.cc \
