Index: trunk/MagicSoft/Mars/mtemp/MFindStars.cc
===================================================================
--- trunk/MagicSoft/Mars/mtemp/MFindStars.cc	(revision 3808)
+++ trunk/MagicSoft/Mars/mtemp/MFindStars.cc	(revision 3808)
@@ -0,0 +1,301 @@
+/* ======================================================================== *\
+!
+! *
+! * 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): Robert Wagner, 2/2004 <mailto:rwagner@mppmu.mpg.de>
+!   Author(s): Javier López , 4/2004 <mailto:jlopez@ifae.es>
+!
+!   Copyright: MAGIC Software Development, 2000-2004
+!
+!
+\* ======================================================================== */
+
+/////////////////////////////////////////////////////////////////////////////
+//
+//  MFindStars
+//
+/////////////////////////////////////////////////////////////////////////////
+#include "MFindStars.h"
+
+#include <TFile.h>
+#include <TTree.h>
+
+#include "MObservatory.h"
+#include "MAstroCamera.h"
+#include "MMcConfigRunHeader.h"
+
+#include "MLog.h"
+#include "MLogManip.h"
+
+#include "MHCamera.h"
+
+#include "MGeomCam.h"
+#include "MGeomPix.h"
+#include "MCameraDC.h"
+#include "MTime.h"
+#include "MReportDrive.h"
+#include "MStarLocalCam.h"
+#include "MStarLocalPos.h"
+
+#include "MParList.h"
+
+ClassImp(MFindStars);
+using namespace std;
+
+MFindStars::MFindStars(const char *name, const char *title)
+{
+  fName  = name  ? name  : "MFindStars";
+  fTitle = title ? title : "Tool to find stars from Base Currents";
+
+  fMaxNumIntegratedEvents = 10;
+  fRingInterest = 100; //[mm] ~ 0.3 deg
+
+  fPixelsUsed.Set(577);
+  fPixelsUsed.Reset((Char_t)kTRUE);
+}
+
+Int_t MFindStars::PreProcess(MParList *pList)
+{
+
+    fGeomCam = (MGeomCam*)pList->FindObject(AddSerialNumber("MGeomCam"));
+
+    if (!fGeomCam)
+    {
+      *fLog << err << AddSerialNumber("MGeomCam") << " not found ... aborting" << endl;
+      return kFALSE;
+    }
+
+    fCurr = (MCameraDC*)pList->FindObject(AddSerialNumber("MCameraDC"));
+
+    if (!fCurr)
+    {
+      *fLog << err << AddSerialNumber("MCameraDC") << " not found ... aborting" << endl;
+      return kFALSE;
+    }
+
+    fTimeCurr = (MTime*)pList->FindObject(AddSerialNumber("MTimeCurrents"));
+
+    if (!fTimeCurr)
+    {
+      *fLog << err << AddSerialNumber("MTimeCurrents") << " not found ... aborting" << endl;
+      return kFALSE;
+    }
+
+   fDrive = (MReportDrive*)pList->FindObject(AddSerialNumber("MReportDrive"));
+
+    if (!fDrive)
+    {
+      *fLog << err << AddSerialNumber("MReportDrive") << " not found ... aborting" << endl;
+      return kFALSE;
+    }
+
+    fStars = (MStarLocalCam*)pList->FindCreateObj(AddSerialNumber("MStarLocalCam"));
+    if (!fStars)
+    {
+      *fLog << err << AddSerialNumber("MStarLocalCam") << " cannot be created ... aborting" << endl;
+      return kFALSE;
+    }
+
+//Initialitation MAstroCamera
+    // Name of a MC file having MGeomCam and MMcConfigRunHeader
+    TString fname = "../data/Gamma_zbin9_90_7_1480to1489_w0.root";
+
+    // Time for which to get the picture
+//    MTime time;
+//    time.Set(2004, 2, 28, 01, 32, 15);
+
+    // Current observatory
+    MObservatory magic1;
+
+    // Right Ascension [h] and declination [deg] of source
+    // Currently 'perfect' pointing is assumed
+//    const Double_t ra  = MAstro::Hms2Rad(5, 34, 31.9);
+//    const Double_t dec = MAstro::Dms2Rad(22, 0, 52.0);
+
+      // --------------------------------------------------------------------------
+    // Create camera display from geometry
+    MMcConfigRunHeader *config=0;
+    MGeomCam           *geom=0;
+
+    TFile file(fname);
+    TTree *tree = (TTree*)file.Get("RunHeaders");
+    tree->SetBranchAddress("MMcConfigRunHeader", &config);
+    if (tree->GetBranch("MGeomCam"))
+        tree->SetBranchAddress("MGeomCam", &geom);
+    tree->GetEntry(0);
+
+    fAstro.SetMirrors(*config->GetMirrors());
+    fAstro.SetGeom(*geom);
+
+
+    fAstro.SetLimMag(6);
+    fAstro.SetRadiusFOV(3);
+    fAstro.ReadBSC("../data/bsc5.dat");
+
+    fAstro.SetObservatory(magic1);
+    // Time for which to get the picture
+//    MTime time;
+//    time.Set(2004, 2, 28, 01, 32, 15);
+//   fAstro.SetTime(time);
+    fAstro.SetTime(*fTimeCurr);
+    fAstro.SetGuiActive();
+
+    fAstro.SetStarList(fStars->GetList());
+
+    return kTRUE;
+}
+
+Int_t MFindStars::Process()
+{
+
+    Float_t ra  = fDrive->GetRa();
+    Float_t dec = fDrive->GetDec();
+
+    fAstro.SetRaDec(ra, dec);
+    fAstro.SetGuiActive();
+   
+    fAstro.FillStarList();
+
+    if (fNumIntegratedEvents < fMaxNumIntegratedEvents)
+    {
+      fDisplay.AddCamContent(*fCurr);
+      fNumIntegratedEvents++;
+    }
+    else
+    {
+	//loop to extract position of stars on the camera
+	TIter Next(fStars->GetList());
+	MStarLocalPos* star;
+	while ((star=(MStarLocalPos*)Next())) 
+	    FindStar(star);
+
+	//After finding stars reset all vairables
+	fDisplay.Reset();
+	fNumIntegratedEvents=0;
+    }
+
+  return kTRUE;
+}
+
+Int_t MFindStars::PostProcess()
+{
+    fStars->Print();
+    return kTRUE;
+}
+
+void MFindStars::SetBlindPixels(TArrayS blindpixels)
+{
+    Int_t npix = blindpixels.GetSize();
+
+    for (Int_t idx=0; idx<npix; idx++)
+	fPixelsUsed[blindpixels[idx]]=(Char_t)kFALSE;
+
+    fDisplay.SetUsed(fPixelsUsed);
+}
+
+Bool_t MFindStars::FindStar(MStarLocalPos* star)
+{    
+
+    Float_t expX = star->GetXExp();
+    Float_t expY = star->GetYExp();
+
+    UInt_t numPixels = fGeomCam->GetNumPixels();
+
+// First define a area of interest around the expected position of the star
+    for (UInt_t pix=1; pix<numPixels; pix++)
+    {
+
+	Float_t pixXpos=(*fGeomCam)[pix].GetX();
+	Float_t pixYpos=(*fGeomCam)[pix].GetY();
+	Float_t dist = sqrt((pixXpos-expX)*(pixXpos-expX)+
+			    (pixYpos-expY)*(pixYpos-expY));
+
+      if (dist < fRingInterest)
+	  fPixelsUsed[pix]=(Char_t)kTRUE;
+    }
+
+    fDisplay.SetUsed(fPixelsUsed);
+
+// Find the two close pixels with the maximun dc
+    for (UInt_t pix=1; pix<numPixels; pix++)
+    {
+	Float_t maxDC = 0;
+
+	if(fDisplay.IsUsed(pix))
+	{
+	    Float_t dc[2];
+	    Float_t maxPix[2];
+	    dc[0] = fDisplay.GetBinContent(pix+1);
+	    
+	    MGeomPix g = (*fGeomCam)[pix];
+	    Int_t numNextNeighbors = g.GetNumNeighbors();
+	    
+	    Float_t dcsum;
+	    for(Int_t nextNeighbor=0; nextNeighbor<numNextNeighbors; nextNeighbor++)
+	    {
+		if(fDisplay.IsUsed(pix))
+		{
+		    UInt_t swneighbor = g.GetNeighbor(nextNeighbor);
+		    dc[1] = fDisplay.GetBinContent(swneighbor+1);
+		    
+		    
+		    dcsum = dc[0] + dc[1];
+		    
+		    if(dcsum > maxDC*2)
+		    {
+			maxPix[0] = pix;
+			maxPix[1] = swneighbor;
+			maxDC = dcsum/2;
+		    }	
+		}
+	    }
+	}
+    }
+
+    // determine mean x and mean y of the selected px
+    Float_t meanX=0;
+    Float_t meanY=0;
+    Float_t meanSqX=0;
+    Float_t meanSqY=0;
+    Float_t sumCharge=0;
+    UInt_t usedPx=0;	
+    for(UInt_t pix=0; pix<numPixels; pix++)
+    {
+	if(fDisplay.IsUsed(pix))
+	{
+	    usedPx++;
+
+	    Float_t charge=fDisplay.GetBinContent(pix+1);
+	    Float_t pixXpos=(*fGeomCam)[pix].GetX();
+	    Float_t pixYpos=(*fGeomCam)[pix].GetY();
+
+	    meanX+=charge*pixXpos;
+	    meanY+=charge*pixYpos;
+	    meanSqX+=charge*pixXpos*pixXpos;
+	    meanSqY+=charge*pixYpos*pixYpos;
+	    sumCharge+=charge;
+//	    fDisplay.ResetUsed(i); // this px must not be used again!
+	} //for... use	
+    }
+
+    star->SetCalcValues(sumCharge,meanX,meanY,meanSqX,meanSqY);
+    fStars->GetList()->Add(star);
+    
+    return kTRUE;
+}
+
+
+
+
Index: trunk/MagicSoft/Mars/mtemp/MFindStars.h
===================================================================
--- trunk/MagicSoft/Mars/mtemp/MFindStars.h	(revision 3808)
+++ trunk/MagicSoft/Mars/mtemp/MFindStars.h	(revision 3808)
@@ -0,0 +1,67 @@
+#ifndef MARS_MJFindStars
+#define MARS_MJFindStars
+
+#ifndef ROOT_TArrayS
+#include <TArrayS.h>
+#endif
+
+#ifndef ROOT_TArrayC
+#include <TArrayC.h>
+#endif
+
+#ifndef MARS_MTask
+#include "MTask.h"
+#endif
+
+#ifndef MARS_MHCamera
+#include "MHCamera.h"
+#endif
+
+#ifndef MARS_MAstroCamera
+#include "MAstroCamera.h"
+#endif
+
+class MGeomCam;
+class MCameraDC;
+class MTime;
+class MReportDrive;
+class MStarLocalCam;
+class MStarLocalPos;
+
+class MFindStars : public MTask
+{
+
+private:
+
+    MGeomCam      *fGeomCam;
+    MCameraDC     *fCurr;
+    MTime         *fTimeCurr;
+    MReportDrive  *fDrive;
+    MStarLocalCam *fStars;
+
+    MAstroCamera fAstro;
+    TArrayC      fPixelsUsed;
+    MHCamera     fDisplay;
+
+    UInt_t fMaxNumIntegratedEvents;
+    UInt_t fNumIntegratedEvents;
+
+    Float_t fRingInterest; //[mm]
+
+public:
+    MFindStars(const char *name=NULL, const char *title=NULL);
+    
+    Int_t PreProcess(MParList *pList);
+    Int_t Process();
+    Int_t PostProcess();
+  
+    void SetNumIntegratedEvents(UInt_t max) {fMaxNumIntegratedEvents=max;}
+    void SetRingInterest(Float_t ring) {fRingInterest=ring;}
+    void SetBlindPixels(TArrayS blindpixels);
+
+    Bool_t FindStar(MStarLocalPos* star);
+
+  ClassDef(MFindStars, 0) // Tool to find stars from Base Currents
+};
+
+#endif
Index: trunk/MagicSoft/Mars/mtemp/MStarLocalCam.cc
===================================================================
--- trunk/MagicSoft/Mars/mtemp/MStarLocalCam.cc	(revision 3808)
+++ trunk/MagicSoft/Mars/mtemp/MStarLocalCam.cc	(revision 3808)
@@ -0,0 +1,82 @@
+/* ======================================================================== *\
+!
+! *
+! * 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): Javier Lopez 04/2004 <mailto:jlopez@ifae.es>
+!   Author(s): Jordi Albert 04/2004 <mailto:albert@astro.uni-wuerzburg.de>
+!                 
+!
+!   Copyright: MAGIC Software Development, 2000-2004
+!
+!
+\* ======================================================================== */
+
+/////////////////////////////////////////////////////////////////////////////
+//                                                                         //
+// MStarLocalCam
+//                                                                         //
+//                                                                         //
+/////////////////////////////////////////////////////////////////////////////
+
+#include "MStarLocalCam.h"
+#include "MStarLocalPos.h"
+
+#include <TList.h>
+
+
+// --------------------------------------------------------------------------
+// Default constructor. 
+//
+//
+MStarLocalCam::MStarLocalCam(const char *name, const char *title) 
+{
+  fName  = name  ? name  : "MStarLocalCam";
+  fTitle = title ? title : "";
+  
+  fStars = new TList;
+}
+
+MStarLocalCam::~MStarLocalCam()
+{
+    delete fStars;
+}
+
+// --------------------------------------------------------------------------
+//
+// Get i-th
+//
+MStarLocalPos &MStarLocalCam::operator[] (Int_t i)
+{
+    return *static_cast<MStarLocalPos*>(fStars->At(i));
+}
+
+// --------------------------------------------------------------------------
+//
+// Get i-th
+//
+const MStarLocalPos &MStarLocalCam::operator[] (Int_t i) const
+{
+    return *static_cast<MStarLocalPos*>(fStars->At(i));
+}
+
+void MStarLocalCam::Print(Option_t *o) const
+{
+	//loop to extract position of stars on the camera
+	TIter Next(fStars);
+	MStarLocalPos* star;
+	while ((star=(MStarLocalPos*)Next())) 
+	    star->Print();
+}
Index: trunk/MagicSoft/Mars/mtemp/MStarLocalCam.h
===================================================================
--- trunk/MagicSoft/Mars/mtemp/MStarLocalCam.h	(revision 3808)
+++ trunk/MagicSoft/Mars/mtemp/MStarLocalCam.h	(revision 3808)
@@ -0,0 +1,37 @@
+#ifndef MARS_MStarLocalCam
+#define MARS_MStarLocalCam
+
+#ifndef MARS_MParContainer
+#include "MParContainer.h"
+#endif
+#ifndef MARS_MCamEvent
+#include "MCamEvent.h"
+#endif
+
+class TList;
+
+class MGeomCam;
+class MStarLocalPos;
+
+class MStarLocalCam : public MParContainer
+{
+private:
+
+  TList  *fStars;  //-> FIXME: Change TClonesArray away from a pointer?
+
+public:
+
+  MStarLocalCam(const char *name=NULL, const char *title=NULL);
+  ~MStarLocalCam();
+
+  MStarLocalPos &operator[] (Int_t i);
+  const MStarLocalPos &operator[] (Int_t i) const;
+
+  TList *GetList() const { return fStars; }
+
+  void Print(Option_t *o="") const;
+
+  ClassDef(MStarLocalCam, 1)	// Storage Container for star positions in the camera
+};
+
+#endif
Index: trunk/MagicSoft/Mars/mtemp/MStarLocalPos.cc
===================================================================
--- trunk/MagicSoft/Mars/mtemp/MStarLocalPos.cc	(revision 3808)
+++ trunk/MagicSoft/Mars/mtemp/MStarLocalPos.cc	(revision 3808)
@@ -0,0 +1,107 @@
+/* ======================================================================== *\
+!
+! *
+! * 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): Javier López , 4/2004 <mailto:jlopez@ifae.es>
+!
+!   Copyright: MAGIC Software Development, 2000-2004
+!
+!
+\* ======================================================================== */
+#include "MStarLocalPos.h"
+
+#include <TEllipse.h>
+
+#include "MLog.h"
+#include "MLogManip.h"
+
+ClassImp(MStarLocalPos);
+
+using namespace std;
+
+MStarLocalPos::MStarLocalPos(const char *name, const char *title)
+{
+
+    fName  = name  ? name  : "MStarLocalPos";
+    fTitle = title ? title : "";
+    
+    Reset();
+}
+
+void MStarLocalPos::Reset()
+{
+
+    //Expected position on camera
+     fMagExp = 0.;
+     fXExp = 0.;
+     fYExp = 0.;
+
+    //Info from calculation
+
+     fMagCalc = 0.;
+     fMeanXCalc = 0.;
+     fMeanYCalc = 0.;
+     fSigmaMinorAxisCalc = 0.;
+     fSigmaMajorAxisCalc = 0.;
+
+    //Info from fit
+
+     fMagFit = 0.;
+     fMeanXFit = 0.;
+     fMeanYFit = 0.;
+     fSigmaMinorAxisFit = 0.;
+     fSigmaMajorAxisFit = 0.;
+     fChisquarenDof = 0.;
+
+}
+
+void MStarLocalPos::SetExpValues(Float_t mag, Float_t x, Float_t y)
+{
+     fMagExp = mag;
+     fXExp = x;
+     fYExp = y;
+}
+
+void MStarLocalPos::SetCalcValues(Float_t mag, Float_t x, Float_t y, Float_t sigmaMinorAxis, Float_t sigmaMajorAxis)
+{
+     fMagCalc = mag;
+     fMeanXCalc = x;
+     fMeanYCalc = y;
+     fSigmaMinorAxisCalc = sigmaMinorAxis;
+     fSigmaMajorAxisCalc = sigmaMajorAxis;
+}
+
+void MStarLocalPos::SetFitValues(Float_t mag, Float_t x, Float_t y, Float_t sigmaMinorAxis, Float_t sigmaMajorAxis)
+{
+     fMagFit = mag;
+     fMeanXFit = x;
+     fMeanYFit = y;
+     fSigmaMinorAxisFit = sigmaMinorAxis;
+     fSigmaMajorAxisFit = sigmaMajorAxis;
+}
+
+void MStarLocalPos::Paint(Option_t *opt)
+{}
+  
+void MStarLocalPos::Print(Option_t *opt) const
+{
+    *fLog << inf << "Star expected position:" << endl;
+    *fLog << inf << " X " << fXExp << " mm \tY " << fYExp << " mm" << endl;
+    *fLog << inf << "Star calculated position:" << endl;
+    *fLog << inf << " X " << fMeanXCalc << " mm \tY " << fMeanYCalc << " mm" << endl;
+    *fLog << inf << "Star fitted position:" << endl;
+    *fLog << inf << " X " << fXExp << " mm \tY " << fYExp << " mm" << endl;
+}
Index: trunk/MagicSoft/Mars/mtemp/MStarLocalPos.h
===================================================================
--- trunk/MagicSoft/Mars/mtemp/MStarLocalPos.h	(revision 3808)
+++ trunk/MagicSoft/Mars/mtemp/MStarLocalPos.h	(revision 3808)
@@ -0,0 +1,69 @@
+#ifndef MARS_MStarLocalPos
+#define MARS_MStarLocalPos
+
+#ifndef MARS_MParContainer
+#include "MParContainer.h"
+#endif
+
+class MStarLocalPos : public MParContainer
+{
+private:
+
+    //Expected position on camera
+    
+    Float_t fMagExp;
+    Float_t fXExp;    //[mm]
+    Float_t fYExp;    //[mm]
+
+    //Info from calculation
+
+    Float_t fMagCalc;
+    Float_t fMeanXCalc;          //[mm]
+    Float_t fMeanYCalc;          //[mm]
+    Float_t fSigmaMinorAxisCalc; //[mm]
+    Float_t fSigmaMajorAxisCalc; //[mm]
+
+    //Info from fit
+
+    Float_t fMagFit;
+    Float_t fMeanXFit;           //[mm]
+    Float_t fMeanYFit;           //[mm]
+    Float_t fSigmaMinorAxisFit;  //[mm]
+    Float_t fSigmaMajorAxisFit;  //[mm]
+    Float_t fChisquarenDof;
+
+public:
+
+    MStarLocalPos(const char *name=NULL, const char *title=NULL);
+    //~MStarLocalPos();
+
+    Float_t GetMagExp() {return fMagExp;}
+    Float_t GetXExp() {return fXExp;}
+    Float_t GetYExp() {return fYExp;}
+
+    Float_t GetMagCalc() {return fMagCalc;}
+    Float_t GetMeanXCalc() {return fMeanXCalc;}
+    Float_t GetMeanYCalc() {return fMeanYCalc;}
+    Float_t GetSigmaMinorAxisCalc() {return fSigmaMinorAxisCalc;}
+    Float_t GetSigmaMajorAxisCalc() {return fSigmaMajorAxisCalc;}
+
+    Float_t GetMagFit() {return fMagFit;}
+    Float_t GetMeanXFit() {return fMeanXFit;}
+    Float_t GetMeanYFit() {return fMeanYFit;}
+    Float_t GetSigmaMinorAxisFit() {return fSigmaMinorAxisFit;}
+    Float_t GetSigmaMajorAxisFit() {return fSigmaMajorAxisFit;}
+    Float_t GetChisquarenDof() {return fChisquarenDof;}
+
+    void Reset();
+
+    void SetExpValues(Float_t mag, Float_t x, Float_t y);
+    void SetCalcValues(Float_t mag, Float_t x, Float_t y, Float_t sigmaMinorAxis, Float_t sigmaMajorAxis);
+    void SetFitValues(Float_t mag, Float_t x, Float_t y, Float_t sigmaMinorAxis, Float_t sigmaMajorAxis);
+
+    void Paint(Option_t *opt=NULL);
+    void Print(Option_t *opt=NULL) const;
+
+    ClassDef(MStarLocalPos, 1) // Container that holds 
+};
+
+#endif
