/// to do:
/// 1. correct for gap between arrays

#define convert_cxx
#include "hitutils.h"
#include <string>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <utility>
#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>
#include <TFile.h>
#include <TTree.h>
#include <TSystemDirectory.h>
#include <gsl/gsl_statistics.h>
#include <math.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_fft_complex.h>
#include <TF1.h>
#include <TGraphErrors.h>
#include <gsl/gsl_sort.h>
#include "GaussSmoothen.h"
#include <TVector.h>

using namespace std;

bool smooth_on = true;
int ngraphs = 0;
int eventID = 0;
int framestart=0;
int frameend = 0;
double board_b0_ch[128];
double board_b1_ch[128];
double board_b2_ch[128];
double board_b3_ch[128];
double board_b0_ch_bkg[128];
double board_b1_ch_bkg[128];
double board_b2_ch_bkg[128];
double board_b3_ch_bkg[128];
double signal_b0 = 0.;
double signal_b1 = 0.;
double signal_b2 = 0.;
double signal_b3 = 0.;
double signal_avg = 0.;
double signal_b0_left = 0.;
double signal_b1_left = 0.;
double signal_b2_left = 0.;
double signal_b3_left = 0.;

double signal_b0_right = 0.;
double signal_b1_right = 0.;
double signal_b2_right = 0.;
double signal_b3_right = 0.;

double signal_gsl_b0[128];
double signal_gsl_b1[128];
double signal_gsl_b2[128];
double signal_gsl_b3[128];


double channellist_gsl_b0[128];
double channellist_gsl_b1[128];
double channellist_gsl_b2[128];
double channellist_gsl_b3[128];
double channellist[128];
double pos[128];

double maxchannelamp_b0 =0.;
double maxchannelamp_b1 =0.;
double maxchannelamp_b2 =0.;
double maxchannelamp_b3 =0.;
int maxchannel_b0 =0;
int maxchannel_b1 =0;
int maxchannel_b2 =0;
int maxchannel_b3 =0;
int numtocalc_b0 =0;
int numtocalc_b1 =0;
int numtocalc_b2 =0;
int numtocalc_b3 =0;
int nfwhm_b0 = 0;
int nfwhm_b1 = 0;
int nfwhm_b2 = 0;
int nfwhm_b3 = 0;

double	beamPosX_b0,beamPosX_b1,beamPosX_b2,beamPosX_b3;
double	beamFocusX_b0,beamFocusX_b1,beamFocusX_b2,beamFocusX_b3; 
double	beamSkewX_b0,beamSkewX_b1,beamSkewX_b2,beamSkewX_b3;
double	beamKurtX_b0,beamKurtX_b1,beamKurtX_b2,beamKurtX_b3; 
double	beamNumX_b0,beamNumX_b1,beamNumX_b2,beamNumX_b3;

double beamSidebandNoise_b0, beamSidebandNoise_b1, beamSidebandNoise_b2, beamSidebandNoise_b3 ;
int sidenumtocalc_b0,sidenumtocalc_b1,sidenumtocalc_b2,sidenumtocalc_b3;
size_t size = 5;
vector<double> boxcar;
vector<double> boxcarsort;
vector<double> boxcarweight{1,3,5,3,1};
vector<double> data(128);
TVector  sumvector_b0(128);
TVector  sumvector_b1(128);
TVector  sumvector_b2(128);
TVector  sumvector_b3(128);

TVector * sumvector_b0_ptr = &sumvector_b0; 
TVector * sumvector_b1_ptr =  &sumvector_b1; 
TVector * sumvector_b2_ptr =  &sumvector_b2;
TVector * sumvector_b3_ptr = &sumvector_b3; 

const int length = 100; //length of the rolling average
double array_b0[length][128] = {{0.}};
double array_b1[length][128] = {{0.}};
double array_b2[length][128] = {{0.}};
double array_b3[length][128] = {{0.}};

double arrayavg_b0[128] = {0.};
double arrayavg_b1[128] = {0.};
double arrayavg_b2[128] = {0.};
double arrayavg_b3[128] = {0.};



double board_b0_smooth_ch[128];
double board_b1_smooth_ch[128];
double board_b2_smooth_ch[128];
double board_b3_smooth_ch[128];	    

bool graphsaved_b0 = false;
bool graphsaved_b1 = false;
bool graphsaved_b2 = false;
bool graphsaved_b3 = false;

TVector beamontime(0,3,0.,0.,0.,0.,"END");
TVector * beamontime_ptr = &beamontime;

double calibration_b0[128] = {0.};
double calibration_b1[128] = {0.};
double calibration_b2[128] = {0.};
double calibration_b3[128] = {0.};

TGraph * graph[100];

/// compile with:
//// $ make clean; make
/// run with:
//// $ ./convert <path to data> <run>
//// $ ./convert /work/leverington/beamprofilemonitor/hitdata/HIT_17_12_2017/ run2



int main(int argc, char **argv)
{
  //initialise some values;
  for (int i = 0;i<128;i++){
    board_b0_ch_bkg[i] = 0.;
    board_b1_ch_bkg[i] = 0.;
    board_b2_ch_bkg[i] = 0.;
    board_b3_ch_bkg[i] = 0.;
    channellist_gsl_b0[i] = 0;
    channellist_gsl_b1[i] = 0;
    channellist_gsl_b2[i] = 0;
    channellist_gsl_b3[i] = 0;
    sumvector_b0[i] = 0;
    sumvector_b1[i] = 0;
    sumvector_b2[i] = 0;
    sumvector_b3[i] = 0;
    calibration_b0[i] = 1.0;
    calibration_b1[i] = 1.0;
    calibration_b2[i] = 1.0;
    calibration_b3[i] = 1.0;
    channellist[i] = i;
    if (i<64) {pos[i] = double(i);}
    else {pos[i] = double(i) + 0.2; }
  }
  if (argc > 1){
    //open the file names specified in the command line
    string filename;
    string rootfilename;
    //  argv[1]= "/work/leverington/beamprofilemonitor/hitdata/HIT_17_12_2017/";
    //  argv[2]= "Run11"
    filename = Form("%s%s.dat",argv[1],argv[2]);
    rootfilename = Form("%s/root/%s.root",argv[1],argv[2]); // Giulia needs to change the path %s/root/ of the written root file. 
    ifstream file(filename, ifstream::in | ifstream::binary);
    
    if (!file.is_open()){
      printf(".dat did not open.\n");
      return -1;	//file could not be opened
    }
   
    ///some variables to get the data
    // int number_of_records_to_read = 4*10;
    //   BufferData* buffer = new BufferData[number_of_records_to_read];
    if(argc > 4)  {
      framestart = atoi(argv[3]);
      frameend = atoi(argv[4]);
    }


    int board_b = -1;
    long int  fileframesize =   getFileSize(filename.c_str()) / ( 4*sizeof(BufferData) );
    BufferData* dataptr = new BufferData(); 
    
    if (fileframesize>0){
      
      //open ROOT file for saving histos and TTree.
      TFile *rootFile = new TFile(rootfilename.c_str(),"RECREATE");
      if ( rootFile->IsOpen() ) printf("ROOT file opened successfully\n");
      TH2D * th2_signal_vs_channel_b0 = new TH2D("th2_signal_vs_channel_b0","th2_signal_vs_channel_b0",128,0,128,2200,-1000,10000);
      TH2D * th2_signal_vs_channel_b1 = new TH2D("th2_signal_vs_channel_b1","th2_signal_vs_channel_b1",128,0,128,2200,-1000,10000);
      TH2D * th2_signal_vs_channel_b2 = new TH2D("th2_signal_vs_channel_b2","th2_signal_vs_channel_b2",128,0,128,2200,-1000,10000);
      TH2D * th2_signal_vs_channel_b3 = new TH2D("th2_signal_vs_channel_b3","th2_signal_vs_channel_b3",128,0,128,2200,-1000,10000);
      TH2D * th2_signal_vs_channel_sub_b0 = new TH2D("th2_signal_vs_channel_sub_b0","th2_signal_vs_channel_sub_b0",128,0,128,2200,-1000,10000);
      TH2D * th2_signal_vs_channel_sub_b1 = new TH2D("th2_signal_vs_channel_sub_b1","th2_signal_vs_channel_sub_b1",128,0,128,2200,-1000,10000);
      TH2D * th2_signal_vs_channel_sub_b2 = new TH2D("th2_signal_vs_channel_sub_b2","th2_signal_vs_channel_sub_b2",128,0,128,2200,-1000,10000);
      TH2D * th2_signal_vs_channel_sub_b3 = new TH2D("th2_signal_vs_channel_sub_b3","th2_signal_vs_channel_sub_b3",128,0,128,2200,-1000,10000);
      TH1D * th1_signal_b0 = new TH1D("th1_signal_b0","th1_signal_b0",2200,-10000,50000);
      TH1D * th1_signal_b1 = new TH1D("th1_signal_b1","th1_signal_b1",2200,-10000,50000);
      TH1D * th1_signal_b2 = new TH1D("th1_signal_b2","th1_signal_b2",2200,-10000,50000);
      TH1D * th1_signal_b3 = new TH1D("th1_signal_b3","th1_signal_b3",2200,-10000,50000);
      TGraph * graph_bkg_b0 = new TGraph();
      TGraph * graph_bkg_b1 = new TGraph();
      TGraph * graph_bkg_b2 = new TGraph();
      TGraph * graph_bkg_b3 = new TGraph();
      TGraph * gr_b0;
      TGraph * gr_sm_b0;
      TGraph * gr_b1;
      TGraph * gr_sm_b1;
      TGraph * gr_b2;
      TGraph * gr_sm_b2;
      TGraph * gr_b3;
      TGraph * gr_sm_b3;

      TH2D * th2_signal_vs_channel_bkg_b0 = new TH2D("th2_signal_vs_channel_bkg_b0","th2_signal_vs_channel_bkg_b0",128,0,128,1000,-500,500);
      TH2D * th2_signal_vs_channel_bkg_b1 = new TH2D("th2_signal_vs_channel_bkg_b1","th2_signal_vs_channel_bkg_b1",128,0,128,1000,-500,500);
      TH2D * th2_signal_vs_channel_bkg_b2 = new TH2D("th2_signal_vs_channel_bkg_b2","th2_signal_vs_channel_bkg_b2",128,0,128,1000,-500,500);
      TH2D * th2_signal_vs_channel_bkg_b3 = new TH2D("th2_signal_vs_channel_bkg_b3","th2_signal_vs_channel_bkg_b3",128,0,128,1000,-500,500);

      TTree *rootTree = new TTree("t","HIT Data Root Tree");
      rootTree->Branch("beamPosX_b0",&beamPosX_b0,"beamPosX_b0/D");
      rootTree->Branch("beamPosX_b1",&beamPosX_b1,"beamPosX_b1/D");
      rootTree->Branch("beamPosX_b2",&beamPosX_b2,"beamPosX_b2/D");
      rootTree->Branch("beamPosX_b3",&beamPosX_b3,"beamPosX_b3/D");
      rootTree->Branch("beamFocusX_b0",&beamFocusX_b0,"beamFocusX_b0/D");
      rootTree->Branch("beamFocusX_b1",&beamFocusX_b1,"beamFocusX_b1/D");
      rootTree->Branch("beamFocusX_b2",&beamFocusX_b2,"beamFocusX_b2/D");
      rootTree->Branch("beamFocusX_b3",&beamFocusX_b3,"beamFocusX_b3/D");
      rootTree->Branch("beamSkewX_b0",&beamSkewX_b0,"beamSkewX_b0/D");
      rootTree->Branch("beamSkewX_b1",&beamSkewX_b1,"beamSkewX_b1/D");
      rootTree->Branch("beamSkewX_b2",&beamSkewX_b2,"beamSkewX_b2/D");
      rootTree->Branch("beamSkewX_b3",&beamSkewX_b3,"beamSkewX_b3/D");
      rootTree->Branch("beamKurtX_b0",&beamKurtX_b0,"beamKurtX_b0/D");
      rootTree->Branch("beamKurtX_b1",&beamKurtX_b1,"beamKurtX_b1/D");
      rootTree->Branch("beamKurtX_b2",&beamKurtX_b2,"beamKurtX_b2/D");
      rootTree->Branch("beamKurtX_b3",&beamKurtX_b3,"beamKurtX_b3/D");
      rootTree->Branch("beamNumX_b0",&beamNumX_b0,"beamNumX_b0/D");
      rootTree->Branch("beamNumX_b1",&beamNumX_b1,"beamNumX_b1/D");
      rootTree->Branch("beamNumX_b2",&beamNumX_b2,"beamNumX_b2/D");
      rootTree->Branch("beamNumX_b3",&beamNumX_b3,"beamNumX_b3/D");
      rootTree->Branch("beamSignal_b0",&signal_b0,"beamSignal_b0/D");
      rootTree->Branch("beamSignal_b1",&signal_b1,"beamSignal_b1/D");
      rootTree->Branch("beamSignal_b2",&signal_b2,"beamSignal_b2/D");
      rootTree->Branch("beamSignal_b3",&signal_b3,"beamSignal_b3/D");

      rootTree->Branch("beamSignal_b0_left",&signal_b0_left,"beamSignal_b0_left/D");
      rootTree->Branch("beamSignal_b1_left",&signal_b1_left,"beamSignal_b1_left/D");
      rootTree->Branch("beamSignal_b2_left",&signal_b2_left,"beamSignal_b2_left/D");
      rootTree->Branch("beamSignal_b3_left",&signal_b3_left,"beamSignal_b3_left/D");
      rootTree->Branch("beamSignal_b0_right",&signal_b0_right,"beamSignal_b0_right/D");
      rootTree->Branch("beamSignal_b1_right",&signal_b1_right,"beamSignal_b1_right/D");
      rootTree->Branch("beamSignal_b2_right",&signal_b2_right,"beamSignal_b2_right/D");
      rootTree->Branch("beamSignal_b3_right",&signal_b3_right,"beamSignal_b3_right/D");

      rootTree->Branch("eventID",&eventID,"eventID/I");
      rootTree->Branch("board_b0_ch",&board_b0_ch,"board_b0_ch[128]/D");
      rootTree->Branch("board_b1_ch",&board_b1_ch,"board_b1_ch[128]/D");
      rootTree->Branch("board_b2_ch",&board_b2_ch,"board_b2_ch[128]/D");
      rootTree->Branch("board_b3_ch",&board_b3_ch,"board_b3_ch[128]/D");

      rootTree->Branch("beamSidebandNoise_b0",&beamSidebandNoise_b0,"beamSidebandNoise_b0/D");
      rootTree->Branch("beamSidebandNoise_b1",&beamSidebandNoise_b1,"beamSidebandNoise_b1/D");
      rootTree->Branch("beamSidebandNoise_b2",&beamSidebandNoise_b2,"beamSidebandNoise_b2/D");
      rootTree->Branch("beamSidebandNoise_b3",&beamSidebandNoise_b3,"beamSidebandNoise_b3/D");
      rootTree->Branch("arrayavg_b0",&arrayavg_b0,"arrayavg_b0[128]/D");
      rootTree->Branch("arrayavg_b1",&arrayavg_b1,"arrayavg_b1[128]/D");
      rootTree->Branch("arrayavg_b2",&arrayavg_b2,"arrayavg_b2[128]/D");
      rootTree->Branch("arrayavg_b3",&arrayavg_b3,"arrayavg_b3[128]/D");



      //loop through recorded data frames
      for (int frame = 0; frame<fileframesize; frame++){
	eventID = frame;
	//	cout << eventID << endl;
	//board_b 0
	board_b=0;
	signal_b0 = 0.;
	maxchannelamp_b0 = 0.;

	file.seekg(framestart*sizeof(BufferData)+board_b*sizeof(BufferData)+4*frame*sizeof(BufferData));
	file.read ((char*)dataptr ,sizeof(BufferData));
	if (dataptr->sync_frame.device_nr==board_b){
	  //cout << frame << " " << dataptr->sync_frame.device_nr << endl;
	  if (frame%1000==0) cout << "Frame: " <<  frame << endl;
	  for (int i = 0;i<128;i++){
	    board_b0_ch[i] = dataptr->sensor_data[i];
	    if (frame<1000){ 
	      board_b0_ch_bkg[i] +=  board_b0_ch[i]/1000.; //find baseline from the average of first 1000 events
	    
	    }
	    else if (frame>=1000&& frame<2000){ 
	      board_b0_ch[i] -=  board_b0_ch_bkg[i]; //histogram the subtracted  baseline in the next 1000 events
	      th2_signal_vs_channel_bkg_b0->Fill(i,board_b0_ch[i]);
	      if (i==1) graph_bkg_b0->SetPoint(graph_bkg_b0->GetN(), frame, board_b0_ch[i]);
	    }
	    else if (frame>=2000) {
	      board_b0_ch[i]-=board_b0_ch_bkg[i]; // the rest background subtracted
	      board_b0_ch[i]*=calibration_b0[i]; //calibration factor
	      th2_signal_vs_channel_b0->Fill(i,board_b0_ch[i]);
	      //  signal_b0 +=board_b0_ch[i] ;
	      if (board_b0_ch[i]> maxchannelamp_b0) {
		maxchannel_b0 = i;
		maxchannelamp_b0 = board_b0_ch[i];
	      }
	      //calculate a rolling average of the signal
	      arrayavg_b0[i] = 0;
	      for (int j = 1; j<length;j++){
		array_b0[j-1][i] = array_b0[j][i];
		arrayavg_b0[i] += array_b0[j-1][i]/double(length);

	      }
	      if( board_b0_ch[i]>-1000){ 
		array_b0[length-1][i] =  board_b0_ch[i];
		arrayavg_b0[i] +=  array_b0[length-1][i]/double(length);
	      }
	      ////////////////////////////////////////////
	    }
	  }
	  //	th1_signal_b0->Fill(signal_b0);
	}
	else {
	  cout << "Error." << endl;
	}
    
	//board_b 1
	board_b=1;
	signal_b1=0.;
	maxchannelamp_b1 = 0.;

	file.seekg(framestart*sizeof(BufferData)+board_b*sizeof(BufferData)+4*frame*sizeof(BufferData));
	file.read ((char*)dataptr ,sizeof(BufferData));
	if (dataptr->sync_frame.device_nr==board_b){
	  //cout << frame << " " << dataptr->sync_frame.device_nr << endl;
	  for (int i = 0;i<128;i++){
	    board_b1_ch[i] = dataptr->sensor_data[i];
	    if (frame<1000){ 
	      board_b1_ch_bkg[i] +=  board_b1_ch[i]/1000.; //find baseline from the average of first 1000 events
	    }
	    else if (frame>=1000&& frame<2000){ 
	      board_b1_ch[i] -=  board_b1_ch_bkg[i]; //histogram the subtracted  baseline in the next 1000 events
	      th2_signal_vs_channel_bkg_b1->Fill(i,board_b1_ch[i]);
	      if (i==1) graph_bkg_b1->SetPoint(graph_bkg_b1->GetN(), frame, board_b1_ch[i]);

	    }
	    else if (frame>=2000) {
	      board_b1_ch[i]-=board_b1_ch_bkg[i]; // the rest are background subtracted
	      board_b1_ch[i]*=calibration_b1[i]; //calibration factor
	      
	      th2_signal_vs_channel_b1->Fill(i,board_b1_ch[i]);
	      // signal_b1 +=board_b1_ch[i] ;
	      if (board_b1_ch[i]> maxchannelamp_b1) {
		maxchannel_b1 = i;
		maxchannelamp_b1 = board_b1_ch[i];
	      }
	      //calculate a rolling average of the signal
	      arrayavg_b1[i] = 0;
	      for (int j = 1; j<length;j++){
		array_b1[j-1][i] = array_b1[j][i];
		arrayavg_b1[i] += array_b1[j-1][i]/double(length);

	      }
	      if( board_b1_ch[i]>-1000){ 
		array_b1[length-1][i] =  board_b1_ch[i];
		arrayavg_b1[i] +=  array_b1[length-1][i]/double(length);
	      }
	      ////////////////////////////////////////////
	    }
	  }
	  //	th1_signal_b1->Fill(signal_b1);
	}
	else {
	  cout << "Error." << endl;
	}
	//board_b 2
	board_b=2;
	signal_b2=0.;
	maxchannelamp_b2 = 0.;
	file.seekg(framestart*sizeof(BufferData)+board_b*sizeof(BufferData)+4*frame*sizeof(BufferData));
	file.read ((char*)dataptr ,sizeof(BufferData));
	if (dataptr->sync_frame.device_nr==board_b){
	 //cout << frame << " " << dataptr->sync_frame.device_nr << endl;
	  for (int i = 0;i<128;i++){
	    board_b2_ch[i] = dataptr->sensor_data[i];
	    if (frame<1000){ 
	      board_b2_ch_bkg[i] +=  board_b2_ch[i]/1000.; //find baseline from the average of first 1000 events
	    }
	    else if (frame>=1000&& frame<2000){ 
	      board_b2_ch[i] -=  board_b2_ch_bkg[i]; //histogram the subtracted  baseline in the next 1000 events
	      th2_signal_vs_channel_bkg_b2->Fill(i,board_b2_ch[i]);
	      if (i==1) graph_bkg_b2->SetPoint(graph_bkg_b2->GetN(), frame, board_b2_ch[i]);

	    }
	    else if (frame>=2000) {
	      board_b2_ch[i]-=board_b2_ch_bkg[i]; // the rest background subtracted
	      board_b2_ch[i]*=calibration_b2[i]; //calibration factor
	      
	      th2_signal_vs_channel_b2->Fill(i,board_b2_ch[i]);
	      //  signal_b2 +=board_b2_ch[i] ;
	      if (board_b2_ch[i]> maxchannelamp_b2) {
		maxchannel_b2 = i;
		maxchannelamp_b2 = board_b2_ch[i];
	      }
	      //calculate a rolling average of the signal
	      arrayavg_b2[i] = 0;
	      for (int j = 1; j<length;j++){
		array_b2[j-1][i] = array_b2[j][i];
		arrayavg_b2[i] += array_b2[j-1][i]/double(length);

	      }
	      if( board_b2_ch[i]>-1000){ 
		array_b2[length-1][i] =  board_b2_ch[i];
		arrayavg_b2[i] +=  array_b2[length-1][i]/double(length);
	      }
	      ////////////////////////////////////////////
	    }
	  }
	  //	th1_signal_b2->Fill(signal_b2);
	}
	else {
	  cout << "Error." << endl;
	}

	//board_b 3
	board_b=3;
	signal_b3=0.;
	maxchannelamp_b3 = 0.;
	file.seekg(framestart*sizeof(BufferData)+board_b*sizeof(BufferData)+4*frame*sizeof(BufferData));
	file.read ((char*)dataptr ,sizeof(BufferData));
	if (dataptr->sync_frame.device_nr==board_b){
	  //cout << frame << " " << dataptr->sync_frame.device_nr << endl;
	  for (int i = 0;i<128;i++){
	    board_b3_ch[i] = dataptr->sensor_data[i];
	    if (frame<1000){ 
	      board_b3_ch_bkg[i] +=  board_b3_ch[i]/1000.; //find baseline from the average of first 1000 events
	    }
	    else if (frame>=1000&& frame<2000){ 
	      board_b3_ch[i] -=  board_b3_ch_bkg[i]; //histogram the subtracted  baseline in the next 1000 events
	      th2_signal_vs_channel_bkg_b3->Fill(i,board_b3_ch[i]);
	      if (i==1) graph_bkg_b3->SetPoint(graph_bkg_b3->GetN(), frame, board_b3_ch[i]);

	    }
	    else if (frame>=2000) {
	      board_b3_ch[i]-=board_b3_ch_bkg[i]; // the rest of the events are background subtracted
	      board_b3_ch[i]*=calibration_b3[i]; //with a calibration factor
	      
	      th2_signal_vs_channel_b3->Fill(i,board_b3_ch[i]);
	      //  signal_b3 +=board_b3_ch[i] ;
	      if (board_b3_ch[i]> maxchannelamp_b3) {
		maxchannel_b3 = i;
		maxchannelamp_b3 = board_b3_ch[i];
	      }
	      //calculate a rolling average of the signal
	      arrayavg_b3[i] = 0;
	      for (int j = 1; j<length;j++){
		array_b3[j-1][i] = array_b3[j][i];
		arrayavg_b3[i] += array_b3[j-1][i]/double(length);

	      }
	      if( board_b3_ch[i]>-1000){ 
		array_b3[length-1][i] =  board_b3_ch[i];
		arrayavg_b3[i] +=  array_b3[length-1][i]/double(length);
	      }
	      ////////////////////////////////////////////
	    }
	  }
	  //	th1_signal_b3->Fill(signal_b3);
	}
	else {
	  cout << "Error." << endl;
	}




	////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////
	//start the signal analysis
	////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////
	
	if (frame>=2000){
	  signal_b0 = 0.; 	  signal_b1 = 0.;	  signal_b2 = 0.;	  signal_b3 = 0.;
	  
	  for (int i = 1; i<128; i++){
	    
	   signal_b0 +=  board_b0_ch[i];
	   signal_b1 +=  board_b1_ch[i];
	   signal_b2 +=  board_b2_ch[i];
	   signal_b3 +=  board_b3_ch[i];
	   signal_avg = (signal_b0 + signal_b1 + signal_b2 + signal_b3)/4.;
	  }
	  if ( signal_avg> 2000 && ngraphs<100){
	    graph[ngraphs] = new TGraph(128,channellist,board_b0_ch); graph[ngraphs]->SetName(Form("graph_%i",ngraphs)); graph[ngraphs]->Write();
	    ngraphs++;
	  }
	  if ( signal_avg> 2000)  cout << signal_b0 << " " << signal_b1 << " " << signal_b2 << " " << signal_b3 << " " <<  signal_avg << " " << ngraphs << endl;
							
								
	  // rootTree->Fill();

	}//end of if (frame>=2000)
	if (frameend>0 && frame+framestart>=frameend) break;
	if (ngraphs==100) break;
      }//end of loop over frames
  

      rootFile->Write();
      rootFile->Close();
    
    }
    cout << eventID << " frames analysed." << endl;
    return 0;
  }
  else {
    cerr << "Error 1" << endl;
    return 1;
  }
  

}
