Index: trunk/MagicSoft/Simulation/Detector/include-MTrigger/MTrigger.cxx
===================================================================
--- trunk/MagicSoft/Simulation/Detector/include-MTrigger/MTrigger.cxx	(revision 485)
+++ trunk/MagicSoft/Simulation/Detector/include-MTrigger/MTrigger.cxx	(revision 486)
@@ -53,5 +53,7 @@
   fwhm_resp = RESPONSE_FWHM       ; 
   ampl_resp = RESPONSE_AMPLITUDE  ; 
-    
+
+  overlaping_time = TRIGGER_OVERLAPING;
+
   threshold = CHANNEL_THRESHOLD  ; 
   
@@ -90,4 +92,7 @@
 	sscanf (datac, "%s %f", dummy, &ampl_resp ) ;
       }
+      else if ( strncmp (datac, "overlaping", 10 ) == 0 ) {
+	sscanf (datac, "%s %f", dummy, &overlaping_time ) ;
+      }
       else if ( strncmp (datac, "multiplicity", 12 ) == 0 ) {
 	sscanf (datac, "%s %f", dummy, &trigger_multi ) ;
@@ -126,4 +131,6 @@
   }
   cout << "[MTrigger]    Gate Length:        " << gate_leng  << " ns"
+       << endl ; 
+  cout << "[MTrigger]    Overlaping time:    " << overlaping_time << " ns"
        << endl ; 
   cout << "[MTrigger]    Response FWHM:      " << fwhm_resp  << " ns"
@@ -262,4 +269,34 @@
 
   //
+  //  Read in the lookup table for trigger cells
+  //
+  
+  i = 0 ; 
+
+  if ( (unit = fopen("../include-MTrigger/TABLE_PIXELS_IN_CELLS", "r" )) == 0 ) { 
+    cout << "ERROR: not able to read ../include-MTrigger/TABLE_PIXELS_IN_CELLS"
+	 << endl ; 
+    exit(123) ; 
+  }  
+  else { 
+    while ( i < TRIGGER_PIXELS )
+      {	
+	for ( Int_t k=0; k<TRIGGER_CELLS; k++ ) { 
+	  TC[k][i]=FALSE; 
+	}
+	i++ ;
+      }
+    while ( feof(unit) == 0 ) {
+	for ( Int_t k=0; k<TRIGGER_CELLS; k++ ) { 
+	  fscanf ( unit, "%d ", &i ) ;
+	  if ((i-1)<TRIGGER_PIXELS)
+	    TC[k][i-1]=TRUE;
+	}
+    }
+    fclose (unit) ;
+  }
+
+
+  //
   //
   //  set all the booleans used to FALSE, indicating that the pixel is not 
@@ -301,5 +338,5 @@
 } 
 
-MTrigger::MTrigger(float gate, float ampl, float fwhm) { 
+MTrigger::MTrigger(float gate, float overt, float ampl, float fwhm) { 
   // ============================================================
   //
@@ -333,4 +370,7 @@
   fwhm_resp = fwhm       ; 
   ampl_resp = ampl  ; 
+
+  overlaping_time = overt;
+
     
   threshold = CHANNEL_THRESHOLD  ; 
@@ -344,4 +384,6 @@
        << "[MTrigger]  Setting up the MTrigger with this values "<< endl ; 
   cout << "[MTrigger]    Gate Length:        " << gate_leng  << " ns"
+       << endl ; 
+  cout << "[MTrigger]    Overlaping time:    " << overlaping_time << " ns"
        << endl ; 
   cout << "[MTrigger]    Response FWHM:      " << fwhm_resp  << " ns"
@@ -453,4 +495,34 @@
 
   //
+  //  Read in the lookup table for trigger cells
+  //
+  
+  i = 0 ; 
+
+  if ( (unit = fopen("../include-MTrigger/TABLE_PIXELS_IN_CELLS", "r" )) == 0 ) { 
+    cout << "ERROR: not able to read ../include-MTrigger/TABLE_PIXELS_IN_CELLS"
+	 << endl ; 
+    exit(123) ; 
+  }  
+  else { 
+    while ( i < TRIGGER_PIXELS )
+      {	
+	for ( Int_t k=0; k<TRIGGER_CELLS; k++ ) { 
+	  TC[k][i]=FALSE; 
+	}
+	i++ ;
+      }
+    while ( feof(unit) == 0 ) {
+	for ( Int_t k=0; k<TRIGGER_CELLS; k++ ) { 
+	  fscanf ( unit, "%d ", &i ) ; 
+	  if((i-1)<TRIGGER_PIXELS)
+	    TC[k][i-1]=TRUE;
+	}
+    }
+    fclose (unit) ;
+  }
+
+
+  //
   //
   //  set all the booleans used to FALSE, indicating that the pixel is not 
@@ -496,15 +568,14 @@
   //   destructor
   //
-  // cout << "destructor of MTrigger" << endl ; 
-  
+  int i;
+
   delete histPmt ; 
 
-  for(Int_t i=0;i<TRIGGER_PIXELS;i++){
-    // delete [] a_sig[i];
-    // delete [] d_sig[i];
-  }
-  
+  for(i=0;i<TRIGGER_PIXELS;i++){
+    //delete [] a_sig[i];
+    //delete [] d_sig[i];
+  }
+
   delete GenElec;
-
 }
 
@@ -1104,196 +1175,226 @@
       if (SlicesZero[iSli]){
 	//
-	//  then look in all pixel if the diskriminated signal is 1
+	//  Loop over trigger cells. It is topology analisy,
+	//  therefore it is keep here after multiplicity and 
+	//  threshold checks.
 	//
+
+	for(Int_t iCell=0; iCell<TRIGGER_CELLS; iCell++){
+	  //
+	  //  then look in all pixel of that cell  if the 
+	  //  diskriminated signal is 1
+	  //
+	  for ( Int_t iPix = 0 ; iPix < TRIGGER_PIXELS; iPix++ ) {
+	    Muster[iPix] = kFALSE ; 
+	    Neighb[iPix] = kFALSE ;
+	    // Select pixels which are used and it the current cell
+	    if ( used [iPix] == TRUE && TC[iCell][iPix]==TRUE) {
+	      //
+	      //  now check the diskriminated signal
+	      //
+	      if ( d_sig [iPix][iSli] > 0. ) {
+		Muster[iPix] = kTRUE ;
+	      } 
+	    }
+	  } // end of loop over the pixels
+
+	  //
+	  //  Here we check which of the "muster" pixels will be fired for
+	  //  the minimum required overlaping time
+	  //
+
+	  OverlapingTime(Muster, &Muster[0],iSli);
+
+	  //
+	  //   here we have to look for the topologies
+	  //
 	
-	for ( Int_t iPix = 0 ; iPix < TRIGGER_PIXELS; iPix++ ) {
-	  Muster[iPix] = kFALSE ; 
-	  Neighb[iPix] = kFALSE ;
-	  if ( used [iPix] == TRUE ) {
-	    //
-	    //  now check the diskriminated signal
-	    //
-	    if ( d_sig [iPix][iSli] > 0. ) {
-	      Muster[iPix] = kTRUE ; 
-	    } 
-	  }
-	} // end of loop over the pixels
-	
-	//
-	//   here we have to look for the topologies
-	//
-	
-	switch(trigger_geometry){
-	case 0:{
+	  switch(trigger_geometry){
+	  case 0:{
 	  
-	  // It looks for a pixel above threshold which has 
-	  // trigger_multi-1 neighbour pixels above threshold 
-
-	  Bool_t Dummy[TRIGGER_PIXELS] ;
-
-	  //  Loop over all pixels
-	  for (int j=0;j<TRIGGER_PIXELS;j++){
-
-	    //
-	    //  I commented out this line here
-	    //	    Dummy=Muster;
-
-	    for (int k=0; k<TRIGGER_PIXELS; k++){
-	      Neighb[k]=kFALSE;
-
-	      Dummy[k]  = Muster[k] ; 
-	    }
-	    if(Muster[j]){
-	      //  If pixel is fired, it checks how many fired neighbours it has
-	      for (iMulti=1;iMulti<trigger_multi; iMulti++) {
-		Neighb[j] = kTRUE ;
-		Dummy[j] = kTRUE ;
-		if (!PassNextNeighbour(Dummy, &Neighb[0])){
-		  break;
-		} 
-		for (int k=0; k<TRIGGER_PIXELS; k++){
-		  if (Neighb[k]){
-		    Dummy[k]=kFALSE;
-		    Neighb[k]=kFALSE;
+	    // It looks for a pixel above threshold which has 
+	    // trigger_multi-1 neighbour pixels above threshold 
+	    
+	    Bool_t Dummy[TRIGGER_PIXELS] ;
+
+	    //  Loop over all pixels
+	    for (int j=0;j<TRIGGER_PIXELS;j++){
+	      
+	      for (int k=0; k<TRIGGER_PIXELS; k++){
+		Neighb[k]=kFALSE;
+		
+		Dummy[k]  = Muster[k] ; 
+	      }
+	      if(Muster[j]){
+		//  If pixel is fired, it checks how many fired neighbours it has
+		for (iMulti=1;iMulti<trigger_multi; iMulti++) {
+		  Neighb[j] = kTRUE ;
+		  Dummy[j] = kTRUE ;
+		  if (!PassNextNeighbour(Dummy, &Neighb[0])){
+		    break;
+		  } 
+		  for (int k=0; k<TRIGGER_PIXELS; k++){
+		    if (Neighb[k]){
+		      Dummy[k]=kFALSE;
+		      Neighb[k]=kFALSE;
+		    }
 		  }
 		}
+		if (iMulti==trigger_multi ) { 
+		  //
+		  //   A NN-Trigger is detected at time Slice 
+		  //
+		  PixelsFirst[nFirst] = j;  //  We save pixel that triggers
+		  SlicesFirst[nFirst++] = iSli ; // We save time when it triggers
+		  iReturn++ ;
+		  iSli+=(50*SLICES_PER_NSEC);  // We skip the following 50 ns (dead time)
+		  iCell=TRIGGER_CELLS;         // We skip the remaining trigger cells 
+		  break ; 
+		}   
 	      }
-	      if (iMulti==trigger_multi ) { 
-		//
-		//   A NN-Trigger is detected at time Slice 
-		//
-		PixelsFirst[nFirst] = j;  //  We save pixel that triggers
-		SlicesFirst[nFirst++] = iSli ; // We save time when it triggers
-		iReturn++ ;
-		iSli+=(50*SLICES_PER_NSEC);  // We skip the following 50 ns (dead time)
-		break ; 
-	      }   
 	    }
-	  }
-	  break;
-	};
+	    break;
+	  };
 	
-	case 1:{
-
-	  //   It looks for trigger_multi neighbour pixels above the 
-	  //   threshold.
-	  
-	  for (int j=0;j<TRIGGER_PIXELS;j++){
-	    if(Muster[j]){
-	      //  It checks if you can find 
-	      //  trigger_multi fired neighbour pixels
-	      Neighb[j] = kTRUE ;
-	      for (iMulti=1;iMulti<trigger_multi; iMulti++) {
-		if (!PassNextNeighbour(Muster, &Neighb[0]))
-		  break; 
-	      }
-	      if (iMulti==trigger_multi ) { 
-		//
-		//   A NN-Trigger is detected at time Slice 
-		//
-		PixelsFirst[nFirst] = j;  //  We save pixel that triggers
-		SlicesFirst[nFirst++] = iSli ; //  We save when it triggers
-		iReturn++ ;
-		iSli+=(50*SLICES_PER_NSEC);  // We skip the following 50 ns (dead time)
-		break ; 
-	      }   
-	      else {
-		//  We put Neighb to kFALSE to check an other pixel
-		for (int k=0; k<TRIGGER_PIXELS; k++){
-		  if (Neighb[k]){
-		    Neighb[k]=kFALSE;
+	  case 1:{
+	    
+	    //   It looks for trigger_multi neighbour pixels above the 
+	    //   threshold.
+	    
+	    for (int j=0;j<TRIGGER_PIXELS;j++){
+	      if(Muster[j]){
+		//  It checks if you can find 
+		//  trigger_multi fired neighbour pixels
+		Neighb[j] = kTRUE ;
+		for (iMulti=1;iMulti<trigger_multi; iMulti++) {
+		  if (!PassNextNeighbour(Muster, &Neighb[0]))
+		    break; 
+		}
+		if (iMulti==trigger_multi ) { 
+		  //
+		  //   A NN-Trigger is detected at time Slice 
+		  //
+		  PixelsFirst[nFirst] = j;  //  We save pixel that triggers
+		  SlicesFirst[nFirst++] = iSli ; //  We save when it triggers
+		  iReturn++ ;
+		  iSli+=(50*SLICES_PER_NSEC);  // We skip the following 50 ns (dead time)
+		  iCell=TRIGGER_CELLS;         // We skip the remaining trigger cells 
+		  break ; 
+		}   
+		else {
+		  //  We put Neighb to kFALSE to check an other pixel
+		  for (int k=0; k<TRIGGER_PIXELS; k++){
+		    if (Neighb[k]){
+		      Neighb[k]=kFALSE;
+		    }
 		  }
 		}
 	      }
 	    }
+	    break;
+	  };
+	  case 2:{
+	    
+	    // It looks for trigger_multi closed pack neighbours
+	    // above threshold
+	    // Closed pack means that you can take out any pixel
+	    // and you will still get a trigger for trigger_multi -1
+	    // The algorithm is not perfect, there still somes cases
+	    // that are not really well treated
+
+	    Int_t closed_pack = 1;
+	    
+	    for (int j=0;j<TRIGGER_PIXELS;j++){
+	      if(Muster[j]){
+		//  It checks if there are trigger_multi
+		//  neighbours above threshold
+
+		Neighb[j] = kTRUE ;
+		iMulti=1;
+
+		//while(PassNextNeighbour(Muster, &Neighb[0])) iMulti++;
+		for (iMulti=1;iMulti<trigger_multi;iMulti++){
+		  if (!PassNextNeighbour(Muster, &Neighb[0]))
+		    break; 
+		}
+
+		if (iMulti==trigger_multi ) { 
+		  //
+		  //   A NN-Trigger is detected at time Slice 
+		  //
+		  
+		  // Check if there is closed pack topology
+
+		  Bool_t Aux1[TRIGGER_PIXELS];
+		  Bool_t Aux2[TRIGGER_PIXELS];
+		  for (int jj=0;jj<TRIGGER_PIXELS;jj++)
+		    Aux2[jj]=kFALSE;
+		  
+		  for (int i=0;i<TRIGGER_PIXELS;i++){
+		    if (Neighb[i]) {
+		      //  Loop over pixels that achive neighbouring condition
+		      		      
+		      for (int jj=0;jj<TRIGGER_PIXELS;jj++) {
+
+			Aux1[jj] = Neighb[jj] ;   // huschel
+			Aux2[jj]=kFALSE;
+		      }
+
+		      //  It checks if taking out any of the pixels we lose
+		      //  neighbouring condition for trigger_multi -1
+		      
+		      Aux1[i]=kFALSE;
+		      closed_pack=0;
+		      for (int jj=0;jj<TRIGGER_PIXELS;jj++) {
+			if (Aux1[jj]==kTRUE){
+			  Aux2[jj]=kTRUE;
+			  for (iMulti=1;iMulti<(trigger_multi-1);iMulti++){
+			    if (!PassNextNeighbour(Aux1, &Aux2[0]))
+			      break; 
+			  }
+			  if (iMulti==(trigger_multi-1)){
+			    //  We found a NN trigger for trigger_multi -1
+			    //  taking out pixel jj
+			    closed_pack=1;
+			    break;
+			  }
+			  Aux2[jj]=kFALSE;
+			}
+		      }
+		      if (!closed_pack) break;
+		      // For some pixell we did not found NN condition
+		      // for trigger_multi -1
+		    }
+		  }
+		  if (closed_pack){
+		    PixelsFirst[nFirst] = j;  //  We save pixel that triggers
+		    SlicesFirst[nFirst++] = iSli ; //  We save time when it triggers
+		    iReturn++ ;
+		    iSli+=(50*SLICES_PER_NSEC);  // We skip the following 50 ns (dead time)
+		    iCell=TRIGGER_CELLS;         // We skip the remaining trigger cells 
+		    break ; 
+		  }
+		  else {
+		    for (int k=0; k<TRIGGER_PIXELS; k++){
+		      if (Neighb[k]){
+			Neighb[k]=kFALSE;
+		      }
+		    }
+		  }
+		}  //  end if trigger multiplicity achived
+		else{
+		  for (int k=0; k<TRIGGER_PIXELS; k++)
+		    Neighb[k]=kFALSE;
+		}		  
+	      } // end if pixel fired
+	    } // end loop trigger pixels
+	    break;
+	  };  // end case 2:
+	  default:{
+	    cout << "This topology is not implemented yet"<<endl;
+	    break;
 	  }
-	  break;
-	};
-	case 2:{
-	  
-	  // It looks for trigger_multi closed pack neighbours
-	  // above threshold
-	  // Closed pack means that you can take out any pixel
-	  // and you will still get a trigger for trigger_multi -1
-
-	  Int_t closed_pack = 1;
-
-	  for (int j=0;j<TRIGGER_PIXELS;j++){
-	    if(Muster[j]){
-	      //  It checks if there are trigger_multi
-	      //  neighbours above threshold
-	      Neighb[j] = kTRUE ;
-	      for (iMulti=1;iMulti<trigger_multi; iMulti++){
-		if (!PassNextNeighbour(Muster, &Neighb[0]))
-		  break; 
-	      }
-	      if (iMulti==trigger_multi ) { 
-		//
-		//   A NN-Trigger is detected at time Slice 
-		//
-
-		// Check if there is closed pack topology
-		Bool_t Aux1[TRIGGER_PIXELS];
-		Bool_t Aux2[TRIGGER_PIXELS];
-		for (int jj=0;jj<TRIGGER_PIXELS;jj++)
-		  Aux2[jj]=kFALSE;
-		
-		for (int i=0;i<TRIGGER_PIXELS;i++){
-		  if (Neighb[i]) {
-		    //  Loop over pixels that achive neighbouring condition
-		    
-		    // huschel
-		    // here also some corrections were neccessary
-		    //
-		    //Aux1=Neighb;
-
-		    for (int jj=0;jj<TRIGGER_PIXELS;jj++) {
-		      Aux1[jj] = Neighb[jj] ;   // huschel
-
-		      Aux2[jj]=kFALSE;
-		    }
-
-		    Aux1[i]=kFALSE;
-		    Aux2[j]=kTRUE;
-		    //  It checks if taking any of the pixels we lose
-		    //  neighbouring condition for trigger -1
-		    for (iMulti=1;iMulti<(trigger_multi-1);iMulti++){
-		      if (!PassNextNeighbour(Aux1, &Aux2[0]))
-			break; 
-		    }
-		    if (iMulti<(trigger_multi-1)){
-		      closed_pack=0;
-		      break;
-		    }
-
-		  }
-		}
-		if (closed_pack){
-		  PixelsFirst[nFirst] = j;  //  We save pixel that triggers
-		  SlicesFirst[nFirst++] = iSli ; //  We save time when it triggers
-		  iReturn++ ;
-		  iSli+=(50*SLICES_PER_NSEC);  // We skip the following 50 ns (dead time)
-		  break ; 
-		}
-		else {
-		  for (int k=0; k<TRIGGER_PIXELS; k++){
-		    if (Neighb[k]){
-		      Neighb[k]=kFALSE;
-		   }
-		  }
-		}
-	      }
-	      else
-		for (int k=0; k<TRIGGER_PIXELS; k++)
-		  Neighb[k]=kFALSE;		  
-	    }
 	  }
-	  break;
-	};
-	default:{
-	  cout << "This topology is not implemented yet"<<endl;
-	  break;
-	}
-	}
+	} //end loop over trigger cells.
       }
     } // end of loop over the slices  
@@ -1352,5 +1453,5 @@
 	  }
 	}
-	else break;
+       else break;
       }
     } 
@@ -1375,2 +1476,33 @@
   return(PixelsFirst[il]);
 }
+
+void MTrigger::OverlapingTime ( Bool_t m[], Bool_t *n, Int_t ifSli){
+
+  //============================================================
+  //
+  //  It returns in n the pixels of m that are fired during the
+  //  required overlaping time for trigger after ifSli
+
+  int i,j;
+  int iNumSli;
+
+  // Translation from ns to slices
+  iNumSli=(int) (overlaping_time*SLICES_PER_NSEC);
+  if (iNumSli<1) iNumSli=1;
+
+  //  Put pixels that fulfill the requirement in n
+  for (i=0;i<TRIGGER_PIXELS;i++){
+    if (m[i]==kTRUE){
+      for(j=ifSli;j<ifSli+iNumSli;j++){
+	if(!d_sig[i][j]){
+	  n[i]=kFALSE;
+	  break;
+	}
+      }
+    }
+  }
+
+}
+
+
+
