Index: trunk/MagicSoft/Mars/mtemp/MFindStars.cc
===================================================================
--- trunk/MagicSoft/Mars/mtemp/MFindStars.cc	(revision 4036)
+++ trunk/MagicSoft/Mars/mtemp/MFindStars.cc	(revision 4037)
@@ -30,6 +30,10 @@
 #include "MFindStars.h"
 
+#include <TTimer.h>
+#include <TString.h>
 #include <TFile.h>
 #include <TTree.h>
+#include <TH1F.h>
+#include <TF1.h>
 
 #include "MObservatory.h"
@@ -60,7 +64,8 @@
   fTitle = title ? title : "Tool to find stars from DC Currents";
 
+  fNumIntegratedEvents=0;
   fMaxNumIntegratedEvents = 10;
-  fRingInterest = 100.; //[mm] ~ 0.3 deg
-  fMinDCForStars = 3.*fMaxNumIntegratedEvents; //[uA]
+  fRingInterest = 125.; //[mm] ~ 0.4 deg
+  fMinDCForStars = 2.*fMaxNumIntegratedEvents; //[uA]
   
   fPixelsUsed.Set(577);
@@ -166,10 +171,5 @@
 Int_t MFindStars::Process()
 {
-    if (fNumIntegratedEvents < fMaxNumIntegratedEvents)
-    {
-      fDisplay.AddCamContent(*fCurr);
-      fNumIntegratedEvents++;
-    }
-    else
+    if (fNumIntegratedEvents >= fMaxNumIntegratedEvents)
     {
       if (fDrive)
@@ -185,22 +185,36 @@
       else
         {
+          UInt_t numPixels = fGeomCam->GetNumPixels();
+          TArrayC origPixelsUsed;
+          origPixelsUsed.Set(numPixels);
+
+          for (UInt_t pix=1; pix<numPixels; pix++)
+            {
+              if (fDisplay.IsUsed(pix))
+                origPixelsUsed[pix]=(Char_t)kTRUE;
+              else
+                origPixelsUsed[pix]=(Char_t)kFALSE;
+            }
+          
+	  Float_t ped;
+	  Float_t rms;
+	  DCPedestalCalc(ped, rms);
+	  fMinDCForStars = fMinDCForStars>(ped+5*rms)?fMinDCForStars:(ped+5*rms);
+
+	  *fLog << dbg << " DC pedestal = " << ped << " pedestal rms = " << rms << endl;
+	  *fLog << dbg << " fMinDCForStars " << fMinDCForStars << endl;
+	  
+          // Find the star candidats searching the most brights pairs of pixels
           Float_t maxPixelDC;
           MGeomPix maxPixel;
 
-          MHCamera cam = fDisplay;
-          cam.PrintInfo();
-
-          // Find the star candidats searching the most brights pairs of pixels
-         while(1)
+	  while(FindPixelWithMaxDC(maxPixelDC, maxPixel))
             {
-              FindPixelWithMaxDC(maxPixelDC, maxPixel);
               *fLog << dbg << "Star candidate maxDC(" << setw(3) << maxPixelDC << " uA) x position(" << setw(3) << maxPixel.GetX() <<  " mm) x position(" << setw(3) << maxPixel.GetY() << " mm)" << endl;
-              
-              if (maxPixelDC<fMinDCForStars)
-                break;
               
               MStarLocalPos *starpos = new MStarLocalPos;
               starpos->SetExpValues(maxPixelDC,maxPixel.GetX(),maxPixel.GetY());
               starpos->SetCalcValues(maxPixelDC,maxPixel.GetX(),maxPixel.GetY(),fRingInterest/2,fRingInterest/2);
+              starpos->SetFitValues(maxPixelDC,maxPixel.GetX(),maxPixel.GetY(),fRingInterest/2,fRingInterest/2);
               fStars->GetList()->Add(starpos);
 
@@ -208,6 +222,6 @@
               
             }
-         fDisplay = cam;
-         *fLog << dbg <<  GetName() << " Found " << fStars->GetList()->GetSize() << " stars candidates in the camera." << endl;
+
+          fDisplay.SetUsed(origPixelsUsed);
         }
       
@@ -218,13 +232,14 @@
           return kFALSE;
         }
+      else
+          *fLog << inf << GetName() << " Found " << fStars->GetList()->GetSize() << " stars candidates in the camera." << endl;
           
       TIter Next(fStars->GetList());
       MStarLocalPos* star;
       while ((star=(MStarLocalPos*)Next()))
-        {
-          FindStar(star);
-          ShadowStar(star);
-        }
-      
+      {
+        FindStar(star);
+        ShadowStar(star);
+      }
       
       //After finding stars reset all vairables
@@ -233,4 +248,7 @@
     }
 
+    fDisplay.AddCamContent(*fCurr);
+    fNumIntegratedEvents++;
+
   return kTRUE;
 }
@@ -254,4 +272,52 @@
 }
 
+Bool_t MFindStars::DCPedestalCalc(Float_t &ped, Float_t &rms)
+{
+   UInt_t numPixels = fGeomCam->GetNumPixels();
+
+   TH1F dchist("dchist","",120,0.,30.*fMaxNumIntegratedEvents);
+   for (UInt_t pix=1; pix<numPixels; pix++)
+       dchist.Fill(fDisplay.GetBinContent(pix+1));
+
+   Float_t nummaxprobdc = dchist.GetBinContent(dchist.GetMaximumBin());
+   Float_t maxprobdc = dchist.GetBinCenter(dchist.GetMaximumBin());
+   UInt_t bin = dchist.GetMaximumBin();
+   do
+   {
+       bin++;
+   }
+   while(dchist.GetBinContent(bin)/nummaxprobdc > 0.5);
+   Float_t halfmaxprobdc = dchist.GetBinCenter(bin);
+
+   *fLog << dbg << " maxprobdc[high] " << maxprobdc << "[" << nummaxprobdc << "] ";
+   *fLog << dbg << " halfmaxprobdc[high] " << halfmaxprobdc << "[" << dchist.GetBinContent(bin) << "]" << endl;
+
+   Float_t rmsguess = TMath::Abs(maxprobdc-halfmaxprobdc);
+   Float_t min = maxprobdc-3*rmsguess;
+   min = (min<0.?0.:min);
+   Float_t max = maxprobdc+3*rmsguess;
+
+   *fLog << dbg << " maxprobdc " << maxprobdc << " rmsguess " << rmsguess << endl;
+
+   TF1 func("func","gaus",min,max);
+   func.SetParameters(nummaxprobdc, maxprobdc, rmsguess);
+   
+   dchist.Fit("func","QR0");
+
+   UInt_t aproxnumdegrees = 6*(bin-dchist.GetMaximumBin());
+   Float_t chiq = func.GetChisquare();
+   ped = func.GetParameter(1);
+   rms = func.GetParameter(2);
+
+   *fLog << dbg << " ped " << ped << " rms " << rms << " chiq/ndof " << chiq << "/" << aproxnumdegrees << endl;
+
+   Int_t numsigmas = 5;
+   Axis_t minbin = ped-numsigmas*rms/dchist.GetBinWidth(1);
+   minbin=minbin<1?1:minbin;
+   Axis_t maxbin = ped+numsigmas*rms/dchist.GetBinWidth(1);
+   *fLog << dbg << " Number of pixels with dc under " << numsigmas << " sigmas = " << dchist.Integral((int)minbin,(int)maxbin) << endl;
+   return kTRUE;
+}
+    
 Bool_t MFindStars::FindPixelWithMaxDC(Float_t &maxDC, MGeomPix &maxPix)
 {
@@ -269,5 +335,7 @@
 	    Float_t dc[2];
 	    dc[0] = fDisplay.GetBinContent(pix+1);
-	    
+	    if (dc[0] < fMinDCForStars)
+		continue;
+
 	    MGeomPix &g = (*fGeomCam)[pix];
 	    Int_t numNextNeighbors = g.GetNumNeighbors();
@@ -280,5 +348,6 @@
 		    UInt_t swneighbor = g.GetNeighbor(nextNeighbor);
 		    dc[1] = fDisplay.GetBinContent(swneighbor+1);
-		    
+		    if (dc[1] < fMinDCForStars)
+			continue;
 		    
 		    dcsum = dc[0] + dc[1];
@@ -304,4 +373,7 @@
     }
 
+    if (maxDC == 0)
+	return kFALSE;
+
     maxPix = (*fGeomCam)[maxPixIdx[0]];
     return kTRUE;
@@ -311,27 +383,36 @@
 {    
 
-  MHCamera cam = fDisplay;
+  UInt_t numPixels = fGeomCam->GetNumPixels();
+  TArrayC origPixelsUsed;
+  origPixelsUsed.Set(numPixels);
   
-    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 && cam.IsUsed(pix))
-	  fPixelsUsed[pix]=(Char_t)kTRUE;
-    }
-
-    cam.SetUsed(fPixelsUsed);
-
-
+  for (UInt_t pix=1; pix<numPixels; pix++)
+    {
+      if (fDisplay.IsUsed(pix))
+        origPixelsUsed[pix]=(Char_t)kTRUE;
+      else
+        origPixelsUsed[pix]=(Char_t)kFALSE;
+    }
+  
+  Float_t expX = star->GetXExp();
+  Float_t expY = star->GetYExp();
+  
+  // 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) && fDisplay.IsUsed(pix))
+        fPixelsUsed[pix]=(Char_t)kTRUE;
+      else
+        fPixelsUsed[pix]=(Char_t)kFALSE;
+    }
+  
+    fDisplay.SetUsed(fPixelsUsed);
+    
     // determine mean x and mean y of the selected px
     Float_t meanX=0;
@@ -343,23 +424,34 @@
     for(UInt_t pix=0; pix<numPixels; pix++)
     {
-	if(cam.IsUsed(pix))
+	if(fDisplay.IsUsed(pix))
 	{
 	    usedPx++;
 
-	    Float_t charge=cam.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;
-//	    cam.ResetUsed(i); // this px must not be used again!
-	} //for... use	
-    }
-
-    star->SetCalcValues(sumCharge,meanX,meanY,meanSqX,meanSqY);
+	    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;
+	}
+    }
+
+    *fLog << dbg << " usedPx " << usedPx << endl;
+
+    meanX   /= sumCharge;
+    meanY   /= sumCharge;
+    meanSqX /= sumCharge;
+    meanSqY /= sumCharge;
+
+    Float_t rmsX = TMath::Sqrt(meanSqX - meanX*meanX);
+    Float_t rmsY = TMath::Sqrt(meanSqY - meanY*meanY);
     
+    star->SetCalcValues(sumCharge,meanX,meanY,rmsX,rmsY);
+ 
+    fDisplay.SetUsed(origPixelsUsed);
+
     return kTRUE;
 }
@@ -370,7 +462,7 @@
 
 // Define an area around the star which will be set unused.
+    UInt_t shadowPx=0;	
     for (UInt_t pix=1; pix<numPixels; pix++)
     {
-
 	Float_t pixXpos  = (*fGeomCam)[pix].GetX();
 	Float_t pixYpos  = (*fGeomCam)[pix].GetY();
@@ -378,5 +470,5 @@
         Float_t starYpos = star->GetMeanYCalc();
         
-	Float_t starSize = 2*star->GetSigmaMajorAxisCalc();
+	Float_t starSize = 3*star->GetSigmaMajorAxisCalc();
         
 	Float_t dist = sqrt((pixXpos-starXpos)*(pixXpos-starXpos)+
@@ -386,6 +478,11 @@
 	  fPixelsUsed[pix]=(Char_t)kTRUE;
         else
+          {
 	  fPixelsUsed[pix]=(Char_t)kFALSE;
-    }
+          shadowPx++;
+          }
+    }
+
+    *fLog << dbg << " shadowPx " << shadowPx << endl;
 
     fDisplay.SetUsed(fPixelsUsed);
Index: trunk/MagicSoft/Mars/mtemp/MFindStars.h
===================================================================
--- trunk/MagicSoft/Mars/mtemp/MFindStars.h	(revision 4036)
+++ trunk/MagicSoft/Mars/mtemp/MFindStars.h	(revision 4037)
@@ -1,4 +1,4 @@
-#ifndef MARS_MJFindStars
-#define MARS_MJFindStars
+#ifndef MARS_MFindStars
+#define MARS_MFindStars
 
 #ifndef ROOT_TArrayS
@@ -49,6 +49,7 @@
 
     Float_t fRingInterest; //[mm]
-    Float_t fMinDCForStars;
+    Float_t fMinDCForStars; //[uA]
 
+    Bool_t DCPedestalCalc(Float_t &ped, Float_t &rms);
     Bool_t FindPixelWithMaxDC(Float_t &maxDC, MGeomPix &maxPix);
     Bool_t FindStar(MStarLocalPos* star);
@@ -56,4 +57,5 @@
 
   public:
+    
     MFindStars(const char *name=NULL, const char *title=NULL);
     
Index: trunk/MagicSoft/Mars/mtemp/MStarLocalCam.cc
===================================================================
--- trunk/MagicSoft/Mars/mtemp/MStarLocalCam.cc	(revision 4036)
+++ trunk/MagicSoft/Mars/mtemp/MStarLocalCam.cc	(revision 4037)
@@ -33,8 +33,13 @@
 
 #include "MStarLocalCam.h"
-#include "MStarLocalPos.h"
 
 #include <TList.h>
 
+#include "MLog.h"
+#include "MLogManip.h"
+
+#include "MStarLocalPos.h"
+
+using namespace std;
 
 // --------------------------------------------------------------------------
@@ -52,5 +57,6 @@
 MStarLocalCam::~MStarLocalCam()
 {
-    delete fStars;
+    fStars->SetOwner();
+    fStars->Delete();
 }
 
@@ -78,5 +84,10 @@
 	TIter Next(fStars);
 	MStarLocalPos* star;
+        UInt_t starnum = 0;
 	while ((star=(MStarLocalPos*)Next())) 
+          {
+            *fLog << inf << "Star[" << starnum << "] info:" << endl;
 	    star->Print();
+            starnum++;
+          }
 }
Index: trunk/MagicSoft/Mars/mtemp/MStarLocalPos.cc
===================================================================
--- trunk/MagicSoft/Mars/mtemp/MStarLocalPos.cc	(revision 4036)
+++ trunk/MagicSoft/Mars/mtemp/MStarLocalPos.cc	(revision 4037)
@@ -99,9 +99,10 @@
 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;
+  *fLog << inf << "Star position:" << endl;
+  *fLog << inf << " Expected \t X " << setw(4) << fXExp << " mm \tY " << setw(4) << fYExp << " mm" << endl;
+  *fLog << inf << " Calcultated \t X " << setw(4) << fMeanXCalc << " mm \tY " << setw(4) << fMeanYCalc << " mm" << endl;
+  *fLog << inf << " Fitted \t X " << setw(4) << fMeanXFit << " mm \tY " << setw(4) << fMeanYFit << " mm" << endl;
+  *fLog << inf << "Star size:" << endl;
+  *fLog << inf << " Calcultated \t X " << setw(4) << fSigmaMinorAxisCalc << " mm \tY " << setw(4) << fSigmaMajorAxisCalc << " mm" << endl;
+  *fLog << inf << " Fitted \t X " << setw(4) << fSigmaMinorAxisFit << " mm \tY " << setw(4) << fSigmaMajorAxisFit << " mm" << endl;
 }
