#include <iostream.h>
#include "MMcTrig.hxx"


//==========
// MMcTrig
//    
// This class handles and contains the MonteCarlo information
// with which the events have been generated
// This information exists for each event.
 


ClassImp(MMcTrig)



MMcTrig::MMcTrig() {
  //
  //  default constructor
  //  set all values to zero

  Int_t i,j;

  fNumFirstLevel  = 0 ;

  for(i=0;i<TOTAL_TRIGGER_TIME/LEVEL1_DEAD_TIME+1;i++){
    fTimeFirst[i]    = -99.9;
    for(j=0;j<TRIGGER_PIXELS/8+1;j++){
      fPixelsFirst[j][i]   = 0;
    }
  }

  fNumSecondLevel = 0 ; 
  for(i=0;i<TOTAL_TRIGGER_TIME/LEVEL2_DEAD_TIME+1;i++){
    fFirstToSecond[i]=0;
  }
}

MMcTrig::~MMcTrig() {
  //
  //  default destructor
  //
}

void MMcTrig::Clear() {
  //
  //  
  //  reset all values to zero
  Int_t i,j;

  fNumFirstLevel  = 0 ;
  for(i=0;i<TOTAL_TRIGGER_TIME/LEVEL1_DEAD_TIME+1;i++){
    fTimeFirst[i]    = -99.9;
    for(j=0;j<TRIGGER_PIXELS/8+1;j++){
      fPixelsFirst[j][i]   = 0;
    }
  }

  fNumSecondLevel = 0 ; 
  for(i=0;i<TOTAL_TRIGGER_TIME/LEVEL2_DEAD_TIME+1;i++){
    fFirstToSecond[i]=0;
  }

  //  cout << "MMcTrig::Clear() " << endl ; 
}

void MMcTrig::Print(Option_t *Option) {
  //
  //  print out the data member on screen
  //
  Int_t i,j;

  cout <<endl << "Monte Carlo Trigger output:" <<endl;
  cout << " First  Level Trigger in this Event : "<<fNumFirstLevel<<endl; 
  cout << " Times of first  Level Trigger in this Event : ";
  for (i=0;i<fNumFirstLevel;i++){
    cout<< fTimeFirst[i]<<"-";
  }
  cout<<endl;
  cout << " Pixels of first  Level Trigger in this Event : ";
  for (i=0;i<fNumFirstLevel;i++){
    for(j=0;j<TRIGGER_PIXELS/8+1;j++){
      cout<<fPixelsFirst[j][i]<<"-";
    }
  }
  cout<<endl;
  cout << " Second Level Trigger in this Event : " << fNumSecondLevel << endl; 
  cout << endl ; 
}

Byte_t MMcTrig::IsPixelFired(Int_t npix, Int_t nfirstlevel){
  //======================================================================
  //
  //  It returns >1 if the pixel npix was fired when the nfirstlevel
  //  first level trigger happened, 0 if not.
  //

  Byte_t ret=0;
  Byte_t reminder;
  Int_t body;

  body=npix/8;
  reminder=(Byte_t)(pow(2,npix%8));

  ret=reminder&fPixelsFirst[body][nfirstlevel];
  return(ret);

}
