Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 1997)
+++ trunk/MagicSoft/Mars/Changelog	(revision 1998)
@@ -1,4 +1,18 @@
                                                  -*-*- END OF LINE -*-*-
 
+ 2003/04/24: Antonio Stamerra
+
+    * mhistmc/MHMcTriggerLvl2.[h,cc]
+      - renamed some variable (hf->fHist)
+      - added fHist*Norm and fFNorm in the destructor
+
+    * manalysis/MMcTriggerLvl2.[h,cc]
+      - added CalcCompactPixel() public method
+      - implemented CalcPseudoSize method (still under construction...)
+      - added inline method SetNewCamera(), used in the costructor
+      - renamed global variables pixels_in_cell->gsPixelsInCell 
+        and pixels_in_lut->gsPixelsInLut
+      - added destructor
+      
 
  2003/04/23: Thomas Bretz
Index: trunk/MagicSoft/Mars/manalysis/MMcTriggerLvl2.cc
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MMcTriggerLvl2.cc	(revision 1997)
+++ trunk/MagicSoft/Mars/manalysis/MMcTriggerLvl2.cc	(revision 1998)
@@ -24,4 +24,5 @@
 \* ======================================================================== */
 
+
 /////////////////////////////////////////////////////////////////////////////
 //                                                                         //
@@ -31,12 +32,12 @@
 //                                                                         //
 // input parameter:                                                        //
-//    fCompactNN   number of next neighboors that define a compact pixel   //
+//    fCompactNN number of next neighboors that define a compact pixel     //
 //                                                                         //
 //                                                                         //
 // Basic L2T Selection Parameters:                                         //
 //                                                                         //
-//    fLutPseudoSize       number of compact pixels in one LUT             //
-//    fPseudoSize          Multiplicity of the bigger cluster              //
-//    fSizeBiggerCell      higher number of fired pixel in cell            //
+//    fLutPseudoSize number of compact pixels in one LUT                   //
+//    fPseudoSize Multiplicity of the bigger cluster                       //
+//    fSizeBiggerCell higher number of fired pixel in cell                 //
 //                                                                         //
 /////////////////////////////////////////////////////////////////////////////
@@ -44,6 +45,10 @@
 #include "MMcTriggerLvl2.h"
 
+#include "MGeomCam.h"
+#include "MGeomPix.h"
 #include "MGeomCamMagic.h"
 #include "MCamDisplay.h"
+
+#include "MCluster.h"
 
 #include "MMcTrig.hxx"
@@ -64,5 +69,5 @@
 // (Note: Pixels start to count from 1 instead of 0)
 //
-const  Int_t  MMcTriggerLvl2::pixels_in_cell[36][19] = {
+const  Int_t  MMcTriggerLvl2::gsPixelsInCell[36][19] = {
     {26,  91,  66,  71,  76,  81,  86,  269,  224,  233,  242,  251,  260,  391,  336,  347,  358,	369,  380},
     {25,  61,  41,  45,  49,  53,  57,  215,  175,  183,  191,  199,  207,  325,  275,  285,  295,  305,  315},
@@ -106,5 +111,5 @@
 // corrispondence between pixels in cell and lut (pix numbering starts from 1)
 //
-const Int_t MMcTriggerLvl2::pixels_in_lut[3][12] = {
+const Int_t MMcTriggerLvl2::gsPixelsInLut[3][12] = {
     { 1,  2,  3,  4,  6,  7,  8,  9, 12, 13, 14, 15},
     {34, 29, 23, 16, 30, 24, 17, 10, 25, 18, 11,  5},
@@ -127,7 +132,19 @@
   // Initialization of the fPixels array to zero
   //
-
   memset (fPixels,0,36*19*sizeof(Int_t));
-
+  memset (fFiredPixel,0,397*sizeof(Int_t));
+
+  // create a new camera
+  SetNewCamera(new MGeomCamMagic);
+}
+
+// --------------------------------------------------------------------------
+//
+//  Destructor
+//
+MMcTriggerLvl2::~MMcTriggerLvl2()
+{
+  delete fCam;
+  delete fGeomCam;
 }
 
@@ -161,5 +178,5 @@
 	    {
 	      //      *fLog.width(3);
-	      *fLog <<pixels_in_cell[i][j]-1 << ":" << fPixels[i][j]  << "\t ";
+	      *fLog <<gsPixelsInCell[i][j]-1 << ":" << fPixels[i][j]  << "\t ";
 	    } 
 	  *fLog << endl;
@@ -173,5 +190,5 @@
 	  for(int j=0; j<9; j++)
 	    {
-	      *fLog << pixels_in_cell[i][j]-1 << "\t";
+	      *fLog << gsPixelsInCell[i][j]-1 << "\t";
 	    }
 	  *fLog << endl;
@@ -208,6 +225,7 @@
       for(int j=0; j<19; j++)
 	{
-	  int pixel = pixels_in_cell[i][j]-1;
+	  int pixel = gsPixelsInCell[i][j]-1;
 	  fPixels[i][j] = (fMcTrig->IsPixelFired(pixel,0)) ? 1 : 0;
+	  fFiredPixel[pixel]=(fMcTrig->IsPixelFired(pixel,0)) ? 1 : 0;
 	}
     }
@@ -227,5 +245,5 @@
       for(int j=0; j<19; j++)
 	{
-	  if(pixels_in_cell[i][j]-1==pixel) fPixels[i][j]=fired;
+	  if(gsPixelsInCell[i][j]-1==pixel) fPixels[i][j]=fired;
 	}
     }
@@ -241,14 +259,18 @@
 void MMcTriggerLvl2::Calc()
 {
+
+  // MCluster temp;
+  //MCluster &clust = (temp).FindCluster();
+  //  temp=*clust;
+
   // Reads the number of NN that define a compact pixel
-  int neighpix=(*this).fCompactNN;
 
   // Find the Lut and cell with the higher LutPseudoSize. 
-  int lutcell = CalcBiggerLutPseudoSize(neighpix); 
+  int lutcell = CalcBiggerLutPseudoSize(); 
   int maxcell = lutcell/10;
   int maxlut = lutcell-maxcell*10;
-  fLutPseudoSize = GetLutCompactPixel(maxcell,maxlut,neighpix);
-
-  fPseudoSize = CalcPseudoSize(neighpix);
+  fLutPseudoSize = GetLutCompactPixel(maxcell,maxlut);
+
+  CalcPseudoSize();
 
   fSizeBiggerCell = GetCellNumberFired(CalcBiggerFiredCell());
@@ -272,9 +294,9 @@
   // the same camera pad
   //
-  if (!fGeomcam)
-    fGeomcam = new MGeomCamMagic;  
+  if (!fGeomCam)
+    fGeomCam = new MGeomCamMagic;  
   if (!fCam)
     {
-      fCam = new MCamDisplay(fGeomcam);
+      fCam = new MCamDisplay(fGeomCam);
       fCam->Draw();
       fCam->DrawPixelNumbers();
@@ -289,5 +311,5 @@
     {
       color = (fPixels[i][cell]) ? 5 : 3;
-      fCam->SetPix( pixels_in_cell[i][cell]-1, color, 1, 5 );
+      fCam->SetPix( gsPixelsInCell[i][cell]-1, color, 1, 5 );
     }
   
@@ -316,9 +338,9 @@
   //  !FixMe! Delete this method when the trigger display is available!
   //
-  if (!fGeomcam)
-    fGeomcam = new MGeomCamMagic;  
+  if (!fGeomCam)
+    fGeomCam = new MGeomCamMagic;  
   if (!fCam)
     {
-      fCam = new MCamDisplay(fGeomcam);
+      fCam = new MCamDisplay(fGeomCam);
       fCam->Draw();  
       fCam->DrawPixelNumbers();
@@ -401,5 +423,5 @@
 //      accordingly to:   cell*10 + LUT 
 //
-Int_t MMcTriggerLvl2::CalcBiggerLutPseudoSize(int neighpix)
+Int_t MMcTriggerLvl2::CalcBiggerLutPseudoSize()
 {
   int size=0;
@@ -411,7 +433,7 @@
       for(int i=0; i<3; i++)
 	{
-	  if (GetLutCompactPixel(j,i,neighpix) > size) 
+	  if (GetLutCompactPixel(j,i) >= size) 
 	    {
-	      size = GetLutCompactPixel(j,i,neighpix);
+	      size = GetLutCompactPixel(j,i);
 	      cell = j;
 	      lut = i;
@@ -421,6 +443,5 @@
   
   //  *fLog <<"Max cell: " << cell+1 << "  Max Lut: " << lut+1 << "  PseudoSize: " << size <<endl;
-  
-  
+    
   return cell*10+lut;
 }
@@ -443,8 +464,11 @@
 //      the number of compact pixels in one LUT.
 //     
-Int_t MMcTriggerLvl2::GetLutCompactPixel(int cell, int lut, int neighpix)
+Int_t MMcTriggerLvl2::GetLutCompactPixel(int cell, int lut)
 {
   int size=0;
   int lutpix, a[12]; 
+  int neighpix= (*this).fCompactNN;
+
+  // check on input variables
 
   if (neighpix >3 || neighpix < 2) 
@@ -462,5 +486,5 @@
   for(int j=0; j<12; j++)
     {
-      lutpix = pixels_in_lut[lut][j]-1;
+      lutpix = gsPixelsInLut[lut][j]-1;
       //    *fLog << "j=" <<j<<"  lutpix="<<lutpix<<"  Cell="<<cell<<endl;
       a[j] = fPixels[lutpix][cell]; 
@@ -668,4 +692,7 @@
     }
   
+
+  if(size<0 ||size>12)
+    *fLog << "*" << size <<"-";
   
   return size;
@@ -678,14 +705,119 @@
 //   defined as the multiplicity of the bigger cluster. 
 //
+//
+//
+void MMcTriggerLvl2::CalcPseudoSize()
+{
+      *fLog <<" In CalcPseudoSize  ";
+  CalcCompactPixels(fGeomCam);
+
+  // seek the LUT with higher number of compact pixels
+  //
+  int cellut = CalcBiggerLutPseudoSize();
+  int maxcell = cellut/10;
+  int maxlut = cellut - maxcell*10;
+  int startpix;
+
+  // 
+  // seek a starting pixel in the lut for the iteration
+  //
+  int check=1;
+  for (int pixlut=0;pixlut<12;pixlut++)
+    {
+      int pixcell =gsPixelsInLut[maxlut][pixlut];
+      startpix = gsPixelsInCell[pixcell][maxcell]-1;
+      if (fCompactPixel[startpix]) // a starting pixel was found
+	break;
+      check++;
+    }
+
+  // A LUT contains 12 pixels
+  if (check >= 12)
+    {
+    *fLog <<"Error: a starting pixels was not found!"<<endl;
+    return;
+    }
+  //
+  // Bulding cluster
+  //
+  Int_t cluster[397];
+  int pnt=0;
+  int pnt2=0; //pointer in the array fCluster_pix, needed for filling
+
+  memset (cluster,0,397*sizeof(Int_t));
+  memset (fCluster_pix,-1,397*sizeof(Int_t));
+
+  cluster[startpix]=1;  
+  fCluster_pix[0]=startpix; //the starting pix is the first in cluster
+  
+  // Look at neighbour pixs if they are compact (iterative search)
+  //  until the array (fCluster_pix) has no more compact pixels.
+  // pnt points to the pixel in the array fCluster_pix whose neighbors are
+  // under study; pnt2 points to the last element of this array.
+  //
+  while (fCluster_pix[pnt] != -1)
+    { 
+      const MGeomPix &pix=(*fGeomCam)[fCluster_pix[pnt]];
+
+      for (int i=0;i<pix.GetNumNeighbors();i++)
+	{
+	  int pix_neigh = pix.GetNeighbor(i);
+	  // check if pixel is fired and doesn't belong to cluster
+	  if (fFiredPixel[pix_neigh] && !cluster[pix_neigh])
+	    {
+	      cluster[pix_neigh] = 1;
+	      fCluster_pix[++pnt2] = pix_neigh;
+	    }
+	}
+      pnt++;
+    }
+
+  fPseudoSize = pnt;
+
+  //  *fLog << "ClusterID:" <<(*clust).GetClusterId() << " Mult:" << (*clust).GetMultiplicity()<<endl;  
+
+  *fLog <<"PSize: "<< fPseudoSize << " in cell:" << maxcell << " lut:" <<maxlut <<endl;
+
+  return;
+}
+
+// --------------------------------------------------------------------------
+//  
+//  Fill the fCompactPixels array with the pixels which are compact
+//
 //  neighpix is the number of Next-Neighbors which defines the compact pixel
 //    topology (it can be 2-NN or 3-NN)
 //
-//    !Not yet implemented!!
-//
-Int_t MMcTriggerLvl2::CalcPseudoSize(int neighpix)
-{
-  int size=0;
-  
-  return size;
-}
-
+//   Note: it is a *global* method; it looks in the all camera as a whole
+//         
+//
+void MMcTriggerLvl2::CalcCompactPixels(MGeomCam *geom)
+{
+  memset (fCompactPixel,0,397*sizeof(Int_t));
+  
+  for(UInt_t pixid=0;pixid<397;pixid++)
+    {
+      // Look if the pixel is fired, otherwise continue
+      if (!fFiredPixel[pixid])
+	continue;
+      
+      const MGeomPix &pix=(*geom)[pixid];
+      
+      // Look for compact pixels. 
+      // A compact pixel must have at least fCompactNN adjacent neighbors
+      // It checks the 6 different configurations of neighbors pixels
+      for (int i=0;i<pix.GetNumNeighbors();i++)
+	{ 
+	  int j=0;
+	  while (fFiredPixel[pix.GetNeighbor(i+j)]==1 && j < fCompactNN)
+	    j++;  
+	  if (j!=fCompactNN)  continue;	 // configuration doesn't satisfy compact condition  
+	  
+	  fCompactPixel[pixid]=1; // pixel is compact
+	  break;
+	}      
+    }
+}
+
+
+
Index: trunk/MagicSoft/Mars/manalysis/MMcTriggerLvl2.h
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MMcTriggerLvl2.h	(revision 1997)
+++ trunk/MagicSoft/Mars/manalysis/MMcTriggerLvl2.h	(revision 1998)
@@ -7,6 +7,12 @@
 
 class MMcTrig;
+
+class MCamDisplay;
 class MGeomCamMagic;
-class MCamDisplay;
+
+class MGeomCam;
+class MGeomPix;
+
+class MCluster;
 
 class MMcTriggerLvl2 : public MParContainer
@@ -15,8 +21,10 @@
  private:
 
-  Int_t fPixels[36][19];  // Array with the triggered pixels. 1st idx:pixels in trigger cell; 2nd idx:trigger cell number  
+  Int_t fPixels[36][19];  // Array with flag for triggered pixels. 1st idx:pixels in trigger cell; 2nd idx:trigger cell number  
 
-  static const Int_t pixels_in_cell[36][19];
-  static const Int_t pixels_in_lut[3][12];
+  Int_t fFiredPixel[397]; // Array with flag for triggered pixel. idx: pixel id
+
+  static const Int_t gsPixelsInCell[36][19];
+  static const Int_t gsPixelsInLut[3][12];
 
   Int_t fLutPseudoSize;  // number of compact pixels in one lut
@@ -26,16 +34,23 @@
   Int_t fCompactNN;  //Number of NN pixels that define a compact pixel
 
+  Int_t fCompactPixel[397]; //Array with flag for compact pixels
+
+  Int_t fCluster_pix[397]; //Array with pixel in cluster
+
   MMcTrig *fMcTrig;
-  MGeomCamMagic *fGeomcam;  
+  MGeomCam *fGeomCam;  
   MCamDisplay *fCam;
+  //  MCluster *fClust;
 
   Int_t CalcBiggerFiredCell();
-  Int_t CalcBiggerLutPseudoSize(int neighpix);
-  Int_t CalcPseudoSize(int neighpix);
+  Int_t CalcBiggerLutPseudoSize();
+  void CalcPseudoSize();
    
+  void SetNewCamera(MGeomCam *geom) {fGeomCam = geom;}
+
  public: 
 
   MMcTriggerLvl2(const char* name = NULL, const char* title = NULL);
-  
+  ~MMcTriggerLvl2();  
   
   void DrawCell(Int_t cell);  
@@ -51,11 +66,14 @@
   void SetCompactNN(Int_t neighpix)     {fCompactNN=neighpix;}
 
-  Int_t GetPseudoSize()     const {return fPseudoSize;}
-  Int_t GetLutPseudoSize()  const {return fLutPseudoSize;}
+  Int_t GetPseudoSize() const     {return fPseudoSize;}
+  Int_t GetLutPseudoSize() const  {return fLutPseudoSize;}
   Int_t GetSizeBiggerCell() const {return fSizeBiggerCell;}
-  Int_t GetCompactNN()      const {return fCompactNN;}
+  Int_t GetCompactNN() const      {return fCompactNN;}
 
   Int_t GetCellNumberFired(int cell);
-  Int_t GetLutCompactPixel(int cell, int lut, int neighpix);
+  Int_t GetLutCompactPixel(int cell, int lut);
+
+  void CalcCompactPixels(MGeomCam *fCam);
+
 
   ClassDef(MMcTriggerLvl2,0) // Container for 2nd Level Trigger selection parameters
