Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 3170)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 3171)
@@ -6,4 +6,12 @@
 
  2004/02/14: Markus Gaug
+
+   * mcalib/Makefile
+   * mcalib/CalibLinkDef.h
+   * mcalib/MExtractPINDiode.[h,cc]
+     - new signal extractor for the PIN Diode
+
+   * mcalib/MExtractedSignalPINDiode.[h,cc]
+     - new container for the extracted signal of the PIN Diode
 
    * manalysis/MHPedestalPix.[h,cc]
Index: /trunk/MagicSoft/Mars/mcalib/MExtractPINDiode.cc
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MExtractPINDiode.cc	(revision 3171)
+++ /trunk/MagicSoft/Mars/mcalib/MExtractPINDiode.cc	(revision 3171)
@@ -0,0 +1,215 @@
+/* ======================================================================== *\
+!
+! *
+! * 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): Markus Gaug, 02/2004 <mailto:markus@ifae.es>
+!
+!   Copyright: MAGIC Software Development, 2000-2004
+!
+!
+\* ======================================================================== */
+
+//////////////////////////////////////////////////////////////////////////////
+//
+//   MExtractPINDiode
+//
+//////////////////////////////////////////////////////////////////////////////
+#include "MExtractPINDiode.h"
+
+#include <fstream>
+
+#include "MLog.h"
+#include "MLogManip.h"
+
+#include "MParList.h"
+
+#include "MRawEvtData.h"
+#include "MRawEvtPixelIter.h"
+
+#include "MPedestalCam.h"
+#include "MPedestalPix.h"
+
+#include "MExtractedSignalPINDiode.h"
+
+ClassImp(MExtractPINDiode);
+
+using namespace std;
+
+const UInt_t MExtractPINDiode::fgPINDiodeId      = 254;
+const Byte_t MExtractPINDiode::fgSaturationLimit = 254;
+const Byte_t MExtractPINDiode::fgFirst =  1;
+const Byte_t MExtractPINDiode::fgLast  = 30;
+
+// --------------------------------------------------------------------------
+//
+// Default constructor. 
+//
+MExtractPINDiode::MExtractPINDiode(const char *name, const char *title)
+    : fSaturationLimit(fgSaturationLimit)
+{
+
+    fName  = name  ? name  : "MExtractPINDiode";
+    fTitle = title ? title : "Task to extract the signal from the FADC slices";
+
+    AddToBranchList("MRawEvtData.*");
+
+    SetRange();
+}
+
+void MExtractPINDiode::SetRange(Byte_t first, Byte_t last)
+{
+
+    fNumSamples = last-first+1;
+    fFirst      = first;
+    fLast       = last;
+
+    fSqrtSamples = TMath::Sqrt((Float_t)fNumSamples);
+}
+
+// --------------------------------------------------------------------------
+//
+// The PreProcess searches for the following input containers:
+//  - MRawEvtData
+//  - MPedestalCam
+//
+// The following output containers are also searched and created if
+// they were not found:
+//
+//  - MExtractedPINDiode
+//
+Int_t MExtractPINDiode::PreProcess(MParList *pList)
+{
+    fRawEvt = (MRawEvtData*)pList->FindObject(AddSerialNumber("MRawEvtData"));
+    if (!fRawEvt)
+    {
+        *fLog << err << AddSerialNumber("MRawEvtData") << " not found... aborting." << endl;
+        return kFALSE;
+    }
+
+
+    fPINDiode = (MExtractedSignalPINDiode*)pList->FindCreateObj(AddSerialNumber("MExtractedSignalPINDiode"));
+    if (!fPINDiode)
+        return kFALSE;
+
+    fPINDiode->SetUsedFADCSlices(fFirst, fFirst+fNumSamples-1);
+
+    fPedestals = (MPedestalCam*)pList->FindObject(AddSerialNumber("MPedestalCam"));
+
+    if (!fPedestals)
+    {
+        *fLog << err << AddSerialNumber("MPedestalCam") << " not found... aborting" << endl;
+        return kFALSE;
+    }
+
+    return kTRUE;
+}
+
+void MExtractPINDiode::FindSignal(Byte_t *ptr, Int_t size, UInt_t &sum, UInt_t &sum2, UInt_t &sat, UInt_t &max) const
+{
+
+  Byte_t *end = ptr + size;
+  
+  while (ptr<end)
+    {
+      sum  += *ptr;
+      sum2 += *ptr * *ptr;
+      if (*ptr > max)
+        max = *ptr;
+      
+      if (*ptr++ >= fSaturationLimit)
+        sat++;
+    }
+}
+
+// --------------------------------------------------------------------------
+//
+// Calculate the integral of the FADC time slices and store them as a new
+// pixel in the MExtractedPINDiode container.
+//
+Int_t MExtractPINDiode::Process()
+{
+
+    MRawEvtPixelIter pixel(fRawEvt);
+
+    fPINDiode->Clear();
+
+    pixel.Jump(fgPINDiodeId);
+ 
+    UInt_t sum  = 0;
+    UInt_t sum2 = 0;
+    UInt_t sat  = 0;
+    UInt_t max  = 0;
+
+    FindSignal(pixel.GetHiGainSamples()+fFirst-1, 
+               fRawEvt->GetNumHiGainSamples(), 
+               sum, sum2, sat, max);
+    FindSignal(pixel.GetLoGainSamples(), 
+               fLast-fRawEvt->GetNumLoGainSamples(), 
+               sum, sum2, sat, max);
+    
+    const MPedestalPix &ped   = (*fPedestals)[fgPINDiodeId]; 
+    
+    const Float_t pedes  = ped.GetPedestal();
+    const Float_t pedrms = ped.GetPedestalRms();
+
+    const Float_t var = ((Float_t)sum2 - (Float_t)sum*sum/fNumSamples)/(fNumSamples-1);
+    const Float_t rms = TMath::Sqrt(var);
+    
+    // 
+    // FIXME: The following formulae have to be revised!!
+    //
+    fPINDiode->SetExtractedSignal(sum - pedes*fNumSamples, pedrms*fSqrtSamples);
+    fPINDiode->SetExtractedRms   (rms, rms/2./fSqrtSamples);
+    fPINDiode->SetExtractedTime  (max, rms/fSqrtSamples);
+    fPINDiode->SetSaturation(sat);
+
+    if (sat)
+      *fLog << warn << "WARNING - saturation occurred in the PIN Diode " << endl;
+
+    fPINDiode->SetReadyToSave();
+
+    return kTRUE;
+}
+
+// --------------------------------------------------------------------------
+//
+// Implementation of SavePrimitive. Used to write the call to a constructor
+// to a macro. In the original root implementation it is used to write
+// gui elements to a macro-file.
+//
+void MExtractPINDiode::StreamPrimitive(ofstream &out) const
+{
+    out << "   " << ClassName() << " " << GetUniqueName() << "(\"";
+    out << "\"" << fName << "\", \"" << fTitle << "\");" << endl;
+
+    if (fSaturationLimit!=fgSaturationLimit)
+    {
+        out << "   " << GetUniqueName() << ".SetSaturationLimit(";
+        out << (int)fSaturationLimit << ");" << endl;
+    }
+
+    const Bool_t arg2 = fNumSamples+fFirst-1 != fgLast;
+    const Bool_t arg1 = arg2 || fFirst != fgFirst;
+
+    if (!arg1)
+        return;
+
+    out << "   " << GetUniqueName() << ".SetRange(";
+    out << (int)fFirst;
+    if (arg2)
+      out << ", " << (int)(fNumSamples+fFirst-1);
+    out << ");" << endl;
+}
Index: /trunk/MagicSoft/Mars/mcalib/MExtractPINDiode.h
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MExtractPINDiode.h	(revision 3171)
+++ /trunk/MagicSoft/Mars/mcalib/MExtractPINDiode.h	(revision 3171)
@@ -0,0 +1,61 @@
+#ifndef MARS_MExtractPINDiode
+#define MARS_MExtractPINDiode
+
+/////////////////////////////////////////////////////////////////////////////
+//                                                                         //
+// MExtractPINDiode                                                        //
+//                                                                         //
+// Integrates the time slices of the all pixels of a calibration event     //
+// and substract the pedestal value                                        //
+//                                                                         //
+/////////////////////////////////////////////////////////////////////////////
+
+#ifndef MARS_MTask
+#include "MTask.h"
+#endif
+
+class MRawEvtData;
+class MRawRunHeader;
+
+class MPedestalCam;
+class MExtractedSignalPINDiode;
+class MExtractPINDiode : public MTask
+{
+private:
+
+  static const UInt_t fgPINDiodeId;  
+  static const Byte_t fgSaturationLimit;
+  static const Byte_t fgFirst;
+  static const Byte_t fgLast;
+
+  MPedestalCam              *fPedestals;    // Pedestals of all pixels in the camera
+  MExtractedSignalPINDiode  *fPINDiode;     // Extracted signal of the PIN Diode
+
+  MRawEvtData         *fRawEvt;       // raw event data (time slices)
+  MRawRunHeader       *fRunHeader;    // RunHeader information
+  
+  Byte_t  fFirst;
+  Byte_t  fLast;
+  Byte_t  fNumSamples;
+
+  Float_t fSqrtSamples;
+
+  Byte_t  fSaturationLimit;
+
+  void   FindSignal(Byte_t *ptr, Int_t size, UInt_t &sum, UInt_t &sum2, UInt_t &sat, UInt_t &max) const;
+  
+  Int_t  PreProcess(MParList *pList);
+  Int_t  Process();
+  void   StreamPrimitive(ofstream &out) const;
+  
+public:
+
+  MExtractPINDiode(const char *name=NULL, const char *title=NULL);
+  
+  void SetRange(Byte_t hifirst=fgFirst, Byte_t hilast=fgLast);
+  void SetSaturationLimit(Byte_t lim) { fSaturationLimit = lim; }
+
+  ClassDef(MExtractPINDiode, 0) // Task to fill the Extracted PINDiode Containers from raw data
+};
+
+#endif
Index: /trunk/MagicSoft/Mars/mcalib/MExtractedSignalPINDiode.cc
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MExtractedSignalPINDiode.cc	(revision 3171)
+++ /trunk/MagicSoft/Mars/mcalib/MExtractedSignalPINDiode.cc	(revision 3171)
@@ -0,0 +1,120 @@
+/* ======================================================================== *\
+!
+! *
+! * 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): Markus Gaug  02/2004 <mailto:markus@ifae.es>
+!
+!   Copyright: MAGIC Software Development, 2000-2004
+!
+!
+\* ======================================================================== */
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// MExtractedSignalPINDiode
+//
+// This is the storage container to hold informations about the extracted signal 
+// (offset) value of the calibration PIN Diode
+//
+/////////////////////////////////////////////////////////////////////////////
+#include "MExtractedSignalPINDiode.h"
+
+#include "MLog.h"
+#include "MLogManip.h"
+
+ClassImp(MExtractedSignalPINDiode);
+
+using namespace std;
+
+static const Float_t gkSignalInitializer = 99999.9;
+
+// ------------------------------------------------------------------------
+//
+// MExtractedSignalPINDiode holds the extracted signal
+// of the FADC slices and its error. 
+//
+// Additionally, the number of saturated Slices are stored. 
+// 
+// Default values for the extracted signals are: 99999.9 
+//
+MExtractedSignalPINDiode::MExtractedSignalPINDiode(const char* name, const char* title)
+{
+
+  fName  = name  ? name  : "MExtractedSignalPINDiode";
+  fTitle = title ? title : "Container of the Extracted Signals";
+
+  Clear();
+}
+
+// ------------------------------------------------------------------------
+//
+// Invalidate values
+//
+void MExtractedSignalPINDiode::Clear(Option_t *o)
+{
+  fExtractedSignal          = gkSignalInitializer;
+  fExtractedSignalErr       = gkSignalInitializer;
+  fExtractedTime            = gkSignalInitializer;
+  fExtractedTimeErr         = gkSignalInitializer; 
+  fExtractedRms             = gkSignalInitializer; 
+  fExtractedRmsErr          = gkSignalInitializer; 
+
+  fNumSaturated       = 0;
+}
+
+void MExtractedSignalPINDiode::SetUsedFADCSlices(const Byte_t first, const Byte_t num)   
+{
+  fFirst          = first; 
+  fNumFADCSamples = num;
+}
+
+
+void MExtractedSignalPINDiode::SetExtractedSignal(const Float_t sig, const Float_t sigerr)   
+{
+  fExtractedSignal      = sig; 
+  fExtractedSignalErr = sigerr;
+}
+
+void MExtractedSignalPINDiode::SetExtractedRms(const Float_t sig, const Float_t sigerr)   
+{
+  fExtractedRms      = sig; 
+  fExtractedRmsErr = sigerr;
+}
+
+void MExtractedSignalPINDiode::SetExtractedTime(const Float_t sig, const Float_t sigerr)   
+{
+  fExtractedTime      = sig; 
+  fExtractedTimeErr = sigerr;
+}
+
+
+Bool_t MExtractedSignalPINDiode::IsValid() const
+{
+    return fExtractedSignal >= 0. || fExtractedSignalErr >= 0.;
+}
+
+void MExtractedSignalPINDiode::SetSaturation(const Byte_t numsat)
+{
+  fNumSaturated = numsat; 
+}
+
+void MExtractedSignalPINDiode::Print(Option_t *o) const
+{
+  *fLog << " Signal: " << fExtractedSignal
+        << " +- " << fExtractedSignalErr
+        << " Nr. Saturation: " <<  fNumSaturated
+        << endl;
+}
Index: /trunk/MagicSoft/Mars/mcalib/MExtractedSignalPINDiode.h
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MExtractedSignalPINDiode.h	(revision 3171)
+++ /trunk/MagicSoft/Mars/mcalib/MExtractedSignalPINDiode.h	(revision 3171)
@@ -0,0 +1,53 @@
+#ifndef MARS_MExtractedSignalPINDiode
+#define MARS_MExtractedSignalPINDiode
+
+#ifndef MARS_MParContainer
+#include "MParContainer.h"
+#endif
+
+class MExtractedSignalPINDiode : public MParContainer
+{
+private:
+
+  Float_t fExtractedSignal;    // mean value of the extracted signal
+  Float_t fExtractedSignalErr; // error of the mean value of the extracted signal
+  Float_t fExtractedTime; 
+  Float_t fExtractedTimeErr; 
+  Float_t fExtractedRms; 
+  Float_t fExtractedRmsErr; 
+
+  Byte_t fFirst;
+  Byte_t fNumFADCSamples;
+  Byte_t fNumSaturated;
+
+public:
+    MExtractedSignalPINDiode(const char* name=NULL, const char* title=NULL);
+
+    void Clear(Option_t *o="");
+    void Print(Option_t *o="") const;
+
+    // Setter
+    void SetExtractedSignal(const Float_t sig, const Float_t sigerr);
+    void SetExtractedRms(  const Float_t sig, const Float_t sigerr);
+    void SetExtractedTime(  const Float_t sig, const Float_t sigerr);
+    void SetSaturation(   const Byte_t numsat);
+    void SetUsedFADCSlices( const Byte_t first, const Byte_t num);
+    
+    // Getter
+    Float_t GetExtractedSignal()    const { return fExtractedSignal; }
+    Float_t GetExtractedSignalErr() const { return fExtractedSignalErr; }
+    Float_t GetExtractedTime()      const { return fExtractedTime; }
+    Float_t GetExtractedTimeErr()   const { return fExtractedTimeErr; }
+    Float_t GetExtractedRms()       const { return fExtractedRms; }
+    Float_t GetExtractedRmsErr()    const { return fExtractedRmsErr; }
+
+    Byte_t  GetNumFADCSamples()     const { return fNumFADCSamples;  }
+    
+
+    
+    Bool_t IsValid() const;   
+
+    ClassDef(MExtractedSignalPINDiode, 0)	// Storage Container for Extracted Signal information of one pixel
+};
+
+#endif
