/// 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 eventID = 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_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.680457, 0.70036, 0.729569, 0.768141, 0.776347, 0.785101, 0.799768, 0.819888, 0.817106, 0.830481, 0.872981, 0.866716, 0.898705, 0.8508, 0.851149, 0.913431, 0.908141, 0.920829, 0.954078, 1.0219, 1.05272, 1.01594, 1.09155, 1.07093, 1.06138, 1.04821, 1.04332, 1.07227, 1.09676, 1.13826, 1.16939, 1.16456, 1.11643, 1.16456, 1.08821, 1.11285, 1.0864, 1.05949, 1.10727, 1.13911, 1.13338, 1.1812, 1.14498, 1.15755, 1.20076, 1.1486, 1.15815, 1.13221, 1.18893, 1.20203, 1.21786, 1.28368, 1.08994, 1.09368, 1.12954, 1.11233, 1.11923, 1.13531, 1.13921, 1.12821, 1.10786, 1.14957, 1.29386, 1.3473, 1.01231, 0.992629, 0.983174, 0.987423, 0.990555, 0.987429, 1.01338, 1.02899, 1.03465, 1.0364, 1.03649, 1.04556, 1.0285, 1.0183, 1.0018, 1.00728, 1.01542, 1.00067, 0.995893, 0.998084, 0.969798, 0.982129, 0.957553, 0.975154, 0.971954, 0.975809, 0.945857, 0.947371, 0.930662, 0.946869, 0.950247, 0.939276, 0.925114, 0.944206, 0.934665, 0.921425, 0.938742, 0.931046, 0.924208, 0.927826, 0.906987, 0.89058, 0.884395, 0.871027, 0.878559, 0.862506, 0.8569, 0.829157, 0.820562, 0.833909, 0.815283, 0.806924, 0.823096, 0.811991, 0.808052, 0.79241, 0.812052, 0.817675, 0.814948, 0.829207, 0.839157, 0.846599, 0.829502, 0.787898};

double calibration_b1[128] = {0.688689, 0.775108, 0.887893, 0.993394, 0.898088, 0.878858, 1.20321, 0.945858, 0.898906, 0.872124, 0.878262, 0.860341, 0.860317, 0.895689, 0.906266, 0.879183, 0.871434, 0.868314, 1.07037, 0.9266, 0.90419, 0.918598, 0.911545, 0.940526, 0.947977, 0.967936, 0.936727, 0.932638, 0.960146, 0.93711, 0.936098, 0.943812, 0.9285, 0.921318, 0.9103, 0.921049, 0.914352, 0.91389, 0.92357, 0.927336, 0.929008, 0.924783, 0.93331, 0.967505, 0.958619, 1.06722, 1.14702, 1.14996, 1.09687, 0.987663, 1.01112, 1.02958, 1.00921, 1.08554, 1.09195, 1.01458, 1.02371, 1.01004, 1.00361, 1.0078, 1.0087, 1.00997, 1.03622, 1.04314, 0.894689, 0.842492, 0.912018, 0.926507, 0.936238, 0.939629, 0.959568, 0.982508, 1.31579, 0.966401, 0.977061, 0.971446, 0.989141, 1.03233, 1.05314, 1.06946, 1.04801, 1.09925, 1.54443, 1.37196, 1.39673, 1.31932, 1.11508, 1.00852, 1.01871, 1.153605, 1.06298, 0.997849, 1.00636, 1.03707, 1.03591, 1.02363, 1.01388, 1.06183, 1.10148, 1.21118, 1.26586, 1.07663, 1.07481, 1.07749, 1.0698, 1.04575, 1.06815, 1.06138, 1.06487, 1.04332, 1.01416, 1.0149, 0.989772, 0.989825, 0.985936, 0.985193, 0.959173, 0.943177, 0.993867, 0.936754, 0.934994, 0.948134, 0.922357, 0.911016, 0.805087, 0.737094, 1.03925, 0.921076};
                            
double calibration_b2[128] = {0.792171, 0.859978, 0.879552, 0.89414, 0.898794, 0.921472, 0.867588, 0.880401, 0.902657, 0.89956, 0.913535, 0.903335, 0.904012, 0.87603, 0.892562, 0.920291, 0.93814, 0.912946, 0.917767, 0.936739, 0.953897, 0.953534, 0.982874, 0.974959, 0.987859, 1.01291, 1.06464, 1.02794, 1.04071, 1.00659, 1.00576, 0.982194, 0.972739, 1.00489, 1.01822, 1.00853, 1.0207, 1.03832, 1.04594, 1.02243, 1.03193, 1.03358, 1.03278, 1.04483, 1.04611, 1.09238, 1.06066, 1.06584, 1.06113, 1.06598, 1.07107, 1.07641, 1.0793, 1.07425, 1.06559, 1.06963, 1.07979, 1.0577, 1.06135, 1.08137, 1.09443, 1.06233, 1.07256, 1.05033, 1.03563, 1.04431, 1.04057, 1.05344, 1.05814, 1.03095, 1.04926, 1.05164, 1.03729, 1.02816, 1.04004, 1.02974, 1.04938, 1.04055, 1.0446, 1.01919, 1.03717, 0.987029, 1.00179, 1.01179, 0.997439, 0.991328, 0.997722, 0.987031, 0.992538, 0.983989, 0.984387, 0.992612, 1.01848, 0.995258, 1.003, 0.993957, 0.992327, 0.990547, 1.00074, 0.996346, 1.01041, 1.01222, 1.00646, 1.03651, 1.00847, 1.01119, 0.999438, 0.979914, 1.02522, 0.973713, 0.987926, 0.957933, 0.958882, 0.950941, 0.941317, 0.933169, 0.92743, 0.936562, 0.932922, 0.913018, 0.920427, 0.909679, 0.918784, 0.933422, 0.937952, 0.950281, 0.908778, 0.859974};

double calibration_b3[128] = {0.870684, 0.722589, 0.801319, 0.899014, 0.943942, 0.951251, 0.962286, 0.943771, 0.955227, 0.950627, 0.971831, 0.974907, 0.977956, 0.986517, 0.954527, 0.96526, 0.939636, 1.11263, 0.957583, 0.977304, 0.99436, 0.995887, 0.993831, 0.989441, 1.02161, 1.01742, 1.07226, 1.05602, 1.03457, 1.04393, 1.0073, 0.992433, 0.990369, 1.018, 0.983336, 1.00309, 0.996907, 0.987621, 1.00206, 0.984202, 0.990234, 0.978864, 0.982685, 0.988023, 1.00838, 0.991885, 1.01678, 0.984361, 1.01042, 0.979532, 1.00935, 0.995128, 1.00761, 1.00347, 0.999018, 0.992036, 0.994325, 0.999203, 0.991379, 0.966761, 0.923005, 0.98845, 0.944313, 0.917033, 0.966649, 0.934711, 0.934409, 0.97089, 1.00189, 1.01703, 1.01652, 1.02944, 1.03673, 1.01944, 1.04403, 1.02387, 1.04477, 1.0339, 1.03241, 1.02265, 0.987512, 1.0377, 1.01763, 1.01628, 1.02939, 1.00738, 1.02215, 1.01216, 1.00725, 1.01186, 1.02182, 1.0041, 1.01231, 1.03678, 1.02598, 1.10276, 1.03272, 1.04435, 1.03799, 1.03239, 1.05349, 1.06976, 1.0274, 1.04304, 1.03917, 1.04833, 1.04097, 1.04549, 1.06443, 1.02653, 1.03372, 1.01791, 1.02118, 1.01925, 1.01674, 1.02208, 0.990651, 1.0075, 1.00273, 1.00169, 0.999678, 1.00178, 0.981252, 0.996679, 0.960323, 0.841463, 0.724784, 0.873291};


/// 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;
    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 > 3)  int block = atoi(argv[3]);
    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(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(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(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(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){




	  ////////////////////////////////////////////////////////////
	  //boxcar smoothing filter
	  ////////////////////////////////////////////////////////////
	  if (smooth_on){
	    maxchannelamp_b0 = 0.0;
	    maxchannelamp_b1 = 0.0;
	    maxchannelamp_b2 = 0.0;
	    maxchannelamp_b3 = 0.0;
	  }
	  boxcar.clear();
	  boxcar.push_back(board_b0_ch[0]);
	  boxcar.push_back(board_b0_ch[1]);
	  //     cout << frame << endl;
	  for (int i = 0;i<128;i++){
	    channellist[i] = i;
	    if (i<126) {
	      boxcar.push_back(board_b0_ch[i+2]);
	    }
	    if ( (i<126&&boxcar.size()>5) || (i>=126&&boxcar.size()>3) ) boxcar.erase(boxcar.cbegin());
	    board_b0_smooth_ch[i] = gsl_stats_mean(boxcar.data(), 1, 5 );
	    if (smooth_on && board_b0_smooth_ch[i]> maxchannelamp_b0) {
		maxchannel_b0 = i;
		maxchannelamp_b0 = board_b0_smooth_ch[i];
	      }
	  }
	  if (eventID>2001&&maxchannelamp_b0>400&&!graphsaved_b0) {
	    gr_b0 = new TGraph(128, channellist, board_b0_ch);
	    gr_b0->SetTitle("RawData_b0");
	    gr_b0->SetName("RawData_b0");
	    gr_b0->Write();
	    gr_sm_b0 = new TGraph(128, channellist, board_b0_smooth_ch);
	    gr_sm_b0->SetTitle("SmoothedData_b0");
	    gr_sm_b0->SetName("SmoothedData_b0");
	    gr_sm_b0->Write();
	    graphsaved_b0=true;
	  }

	  boxcar.clear();
	  boxcar.push_back(board_b1_ch[0]);
	  boxcar.push_back(board_b1_ch[1]);
	  //     cout << frame << endl;
	  for (int i = 0;i<128;i++){
	    channellist[i] = i;
	    if (i<126) {
	      boxcar.push_back(board_b1_ch[i+2]);
	    }
	    if ( (i<126&&boxcar.size()>5) || (i>=126&&boxcar.size()>3) ) boxcar.erase(boxcar.cbegin());
	    board_b1_smooth_ch[i] = gsl_stats_mean(boxcar.data(), 1, 5 );
	    if (smooth_on && board_b1_smooth_ch[i]> maxchannelamp_b1) {
		maxchannel_b1 = i;
		maxchannelamp_b1 = board_b1_smooth_ch[i];
	      }
	  }
	  if (eventID>2001&&maxchannelamp_b1>400&&!graphsaved_b1) {
	    gr_b1 = new TGraph(128, channellist, board_b1_ch);
	    gr_b1->SetTitle("RawData_b1");
	    gr_b1->SetName("RawData_b1");
	    gr_b1->Write();
	    gr_sm_b1 = new TGraph(128, channellist, board_b1_smooth_ch);
	    gr_sm_b1->SetTitle("SmoothedData_b1");
	    gr_sm_b1->SetName("SmoothedData_b1");
	    gr_sm_b1->Write();
	    graphsaved_b1=true;
	  }

	  boxcar.clear();
	  boxcar.push_back(board_b2_ch[0]);
	  boxcar.push_back(board_b2_ch[1]);
	  //     cout << frame << endl;
	  for (int i = 0;i<128;i++){
	    channellist[i] = i;
	    if (i<126) {
	      boxcar.push_back(board_b2_ch[i+2]);
	    }
	    if ( (i<126&&boxcar.size()>5) || (i>=126&&boxcar.size()>3) ) boxcar.erase(boxcar.cbegin());
	    board_b2_smooth_ch[i] = gsl_stats_mean(boxcar.data(), 1, 5 );
	    if (smooth_on && board_b2_smooth_ch[i]> maxchannelamp_b2) {
		maxchannel_b2 = i;
		maxchannelamp_b2 = board_b2_smooth_ch[i];
	      }
	  }
	  if (eventID>2001&&maxchannelamp_b2>400&&!graphsaved_b2) {
	    gr_b2 = new TGraph(128, channellist, board_b2_ch);
	    gr_b2->SetTitle("RawData_b2");
	    gr_b2->SetName("RawData_b2");
	    gr_b2->Write();
	    gr_sm_b2 = new TGraph(128, channellist, board_b2_smooth_ch);
	    gr_sm_b2->SetTitle("SmoothedData_b2");
	    gr_sm_b2->SetName("SmoothedData_b2");
	    gr_sm_b2->Write();
	    graphsaved_b2=true;
	  }


	  boxcar.clear();
	  boxcar.push_back(board_b3_ch[0]);
	  boxcar.push_back(board_b3_ch[1]);
	  //     cout << frame << endl;
	  for (int i = 0;i<128;i++){
	    channellist[i] = i;
	    if (i<126) {
	      boxcar.push_back(board_b3_ch[i+2]);
	    }
	    if ( (i<126&&boxcar.size()>5) || (i>=126&&boxcar.size()>3) ) boxcar.erase(boxcar.cbegin());
	    board_b3_smooth_ch[i] = gsl_stats_mean(boxcar.data(), 1, 5 );
	    if (smooth_on && board_b3_smooth_ch[i]> maxchannelamp_b3) {
		maxchannel_b3 = i;
		maxchannelamp_b3 = board_b3_smooth_ch[i];
	      }
	  }
	  if (eventID>2001&&maxchannelamp_b3>400&&!graphsaved_b3) {
	    gr_b3 = new TGraph(128, channellist, board_b3_ch);
	    gr_b3->SetTitle("RawData_b3");
	    gr_b3->SetName("RawData_b3");
	    gr_b3->Write();
	    gr_sm_b3 = new TGraph(128, channellist, board_b3_smooth_ch);
	    gr_sm_b3->SetTitle("SmoothedData_b3");
	    gr_sm_b3->SetName("SmoothedData_b3");
	    gr_sm_b3->Write();
	    graphsaved_b3=true;
	  }



	////////////////////////////////////////////////////////////
	//find the approx FWHM
	////////////////////////////////////////////////////////////
	
	  nfwhm_b0 =0;
	  nfwhm_b1 =0;
	  nfwhm_b2 =0;
	  nfwhm_b3 =0;
	  if (smooth_on){
	    for (int i = 0;i<128;i++){
	      if (board_b0_smooth_ch[i] > maxchannelamp_b0/2.) nfwhm_b0++;
	      if (board_b1_smooth_ch[i] > maxchannelamp_b1/2.) nfwhm_b1++;
	      if (board_b2_smooth_ch[i] > maxchannelamp_b2/2.) nfwhm_b2++;
	      if (board_b3_smooth_ch[i] > maxchannelamp_b3/2.) nfwhm_b3++;
	      signal_gsl_b0[i] = 0.;
	      signal_gsl_b1[i] = 0.;
	      signal_gsl_b2[i] = 0.;
	      signal_gsl_b3[i] = 0.;

	    }

	  }
	  else {
	    for (int i = 0;i<128;i++){
	      if (board_b0_ch[i] > maxchannelamp_b0/2.) nfwhm_b0++;
	      if (board_b1_ch[i] > maxchannelamp_b1/2.) nfwhm_b1++;
	      if (board_b2_ch[i] > maxchannelamp_b2/2.) nfwhm_b2++;
	      if (board_b3_ch[i] > maxchannelamp_b3/2.) nfwhm_b3++;
	      signal_gsl_b0[i] = 0.;
	      signal_gsl_b1[i] = 0.;
	      signal_gsl_b2[i] = 0.;
	      signal_gsl_b3[i] = 0.;

	    }
	  }


	  ////////////////////////////////////////////////////////////
	  //integrate the sidebands first to check for baseline shift
	  ////////////////////////////////////////////////////////////
	  /*
	  beamSidebandNoise_b0 = 0.;
	  sidenumtocalc_b0 = 0;
	  for (int i =0; i<maxchannel_b0-nfwhm_b0;  i++){
	    if (i>=0 && i<=127){
	      beamSidebandNoise_b0 +=  board_b0_ch[i]; //integrate the noise outside the peak
	      sidenumtocalc_b0++;
	    }
	  }
	    for (int i = maxchannel_b0+nfwhm_b0; i <  128;  i++){
	      if (i>=0 && i<=127){
		beamSidebandNoise_b0 +=  board_b0_ch[i]; //integrate the noise outside the peak
		sidenumtocalc_b0++;
	      }
	    }
	    if (sidenumtocalc_b0>0) beamSidebandNoise_b0 = beamSidebandNoise_b0 /double(sidenumtocalc_b0);  // channel baseline shift

	    beamSidebandNoise_b1 = 0.;
	    sidenumtocalc_b1 = 0;
	    for (int i =0; i<maxchannel_b1-nfwhm_b1;  i++){
	      if (i>=0 && i<=127){
		beamSidebandNoise_b1 +=  board_b1_ch[i]; //integrate the noise outside the peak
		sidenumtocalc_b1++;
	      }
	    }
	      for (int i = maxchannel_b1+nfwhm_b1; i <  128;  i++){
		if (i>=0 && i<=127){
		  beamSidebandNoise_b1 +=  board_b1_ch[i]; //integrate the noise outside the peak
		  sidenumtocalc_b1++;
		}
	      }
	      if (sidenumtocalc_b1>0) beamSidebandNoise_b1 = beamSidebandNoise_b1 /double(sidenumtocalc_b1);  // channel baseline shift

	      beamSidebandNoise_b2 = 0.;
	      sidenumtocalc_b2 = 0;
	      for (int i =0; i<maxchannel_b2-nfwhm_b2;  i++){
		if (i>=0 && i<=127){
		  beamSidebandNoise_b2 +=  board_b2_ch[i]; //integrate the noise outside the peak
		  sidenumtocalc_b2++;
		}
	      }
		for (int i = maxchannel_b2+nfwhm_b2; i <  128;  i++){
		  if (i>=0 && i<=127){
		    beamSidebandNoise_b2 +=  board_b2_ch[i]; //integrate the noise outside the peak
		    sidenumtocalc_b2++;
		  }
		}
		if (sidenumtocalc_b2>0) beamSidebandNoise_b2 = beamSidebandNoise_b2 /double(sidenumtocalc_b2);  // channel baseline shift

		beamSidebandNoise_b3 = 0.;
		sidenumtocalc_b3 = 0;
		for (int i =0; i<maxchannel_b3-nfwhm_b3;  i++){
		  if (i>=0 && i<=127){
		    beamSidebandNoise_b3 +=  board_b3_ch[i]; //integrate the noise outside the peak
		    sidenumtocalc_b3++;
		  }
		}
		  for (int i = maxchannel_b3+nfwhm_b3; i <  128;  i++){
		    if (i>=0 && i<=127){
		      beamSidebandNoise_b3 +=  board_b3_ch[i]; //integrate the noise outside the peak
		      sidenumtocalc_b3++;
		    }
		  }
		  if (sidenumtocalc_b3>0) beamSidebandNoise_b3 = beamSidebandNoise_b3 /double(sidenumtocalc_b3);  // channel baseline shift


	  */


	  ////////////////////////////////////////////////////////////
	  //integrate under the approximate peak
	  //build the channel list for statistical analysis
	  ////////////////////////////////////////////////////////////

	  numtocalc_b0 = 0;
	  numtocalc_b1 = 0;
	  numtocalc_b2 = 0;
	  numtocalc_b3 = 0;
	  signal_b0_left = 0.;
	  signal_b0_right = 0.;
	  for (int i = maxchannel_b0-nfwhm_b0 ; i <= maxchannel_b0 + nfwhm_b0; i++){
	    if (i>=0 && i<=127 && board_b0_ch[i]>50){
	      signal_b0 +=board_b0_ch[i]-beamSidebandNoise_b0  ;
	      if (i<64) {signal_b0_left +=board_b0_ch[i]-beamSidebandNoise_b0  ;}
	      else {signal_b0_right +=board_b0_ch[i]-beamSidebandNoise_b0  ;}
	      signal_gsl_b0[numtocalc_b0]=board_b0_ch[i]-beamSidebandNoise_b0 ;
	      // channellist_gsl_b0[numtocalc_b0] = i;
	      channellist_gsl_b0[numtocalc_b0] = pos[i];
	      numtocalc_b0++;
	    }
	  }
	  th1_signal_b0->Fill(signal_b0);

	  signal_b1_left = 0.;
	  signal_b1_right = 0.;
	  for (int i = maxchannel_b1-nfwhm_b1 ; i <= maxchannel_b1 + nfwhm_b1; i++){
	    if (i>=0 && i<=127&&board_b1_ch[i]>50){
	      signal_b1 +=board_b1_ch[i]-beamSidebandNoise_b1  ;
	      if (i<64) {signal_b1_left +=board_b1_ch[i]-beamSidebandNoise_b1  ;}
	      else {signal_b1_right +=board_b1_ch[i]-beamSidebandNoise_b1  ;}
	      signal_gsl_b1[numtocalc_b1]=board_b1_ch[i]-beamSidebandNoise_b1 ;
	      // channellist_gsl_b1[numtocalc_b1] = i;
	      channellist_gsl_b1[numtocalc_b1] = pos[i];
	      numtocalc_b1++;
	    }
	  }
	  th1_signal_b0->Fill(signal_b1);

	  signal_b2_left = 0.;
	  signal_b2_right = 0.;
	  for (int i = maxchannel_b2-nfwhm_b2 ; i <= maxchannel_b2 + nfwhm_b2; i++){
	    if (i>=0 && i<=127&&board_b2_ch[i]>50){
	      signal_b2 +=board_b2_ch[i]-beamSidebandNoise_b2  ;
	      if (i<64) {signal_b2_left +=board_b2_ch[i]-beamSidebandNoise_b2  ;}
	      else {signal_b2_right +=board_b2_ch[i]-beamSidebandNoise_b2  ;}
	      signal_gsl_b2[numtocalc_b2]=board_b2_ch[i]-beamSidebandNoise_b2 ;
	      // channellist_gsl_b2[numtocalc_b2] = i;
	      channellist_gsl_b2[numtocalc_b2] = pos[i];
	      numtocalc_b2++;
	    }
	  }
	  th1_signal_b0->Fill(signal_b2);

	  signal_b3_left = 0.;
	  signal_b3_right = 0.;
	  for (int i = maxchannel_b3-nfwhm_b3 ; i <= maxchannel_b3 + nfwhm_b3; i++){
	    if (i>=0 && i<=127&&board_b3_ch[i]>50){
	      signal_b3 +=board_b3_ch[i]-beamSidebandNoise_b3 ;
	      if (i<64) {signal_b3_left +=board_b3_ch[i]-beamSidebandNoise_b3  ;}
	      else {signal_b3_right +=board_b3_ch[i]-beamSidebandNoise_b3  ;}
	      signal_gsl_b3[numtocalc_b3]=board_b3_ch[i]-beamSidebandNoise_b3 ;
	      //channellist_gsl_b3[numtocalc_b3] = i;
	      channellist_gsl_b3[numtocalc_b3] = pos[i];
	      numtocalc_b3++;
	    }
	  }
	  th1_signal_b0->Fill(signal_b3);


	  for (int i=0;i<128;i++){
	    if(signal_b0>7000) {
	      th2_signal_vs_channel_sub_b0->Fill(i,board_b0_ch[i]-beamSidebandNoise_b0);
	      sumvector_b0[i] += board_b0_ch[i];//-beamSidebandNoise_b0;
     
	    }
	    if(signal_b1>7000) {
	      th2_signal_vs_channel_sub_b1->Fill(i,board_b1_ch[i]-beamSidebandNoise_b1);
	      sumvector_b1[i] += board_b1_ch[i];//-beamSidebandNoise_b1;
	    }
	    if(signal_b2>7000){
	      th2_signal_vs_channel_sub_b2->Fill(i,board_b2_ch[i]-beamSidebandNoise_b2);
	      sumvector_b2[i] += board_b2_ch[i];//-beamSidebandNoise_b2;
	    }
	    if(signal_b3>7000) {
	      th2_signal_vs_channel_sub_b3->Fill(i,board_b3_ch[i]-beamSidebandNoise_b3);
	      sumvector_b3[i] += board_b3_ch[i];//-beamSidebandNoise_b3;
	    }
	  }
	  if(signal_b0>7000) beamontime[0]+=1.0;
	  if(signal_b1>7000) beamontime[1]+=1.0;
	  if(signal_b2>7000) beamontime[2]+=1.0;
	  if(signal_b3>7000) beamontime[3]+=1.0;



	  ///add gsl stuff here.
	  /*	cout << maxchannel_b0 << " " << maxchannel_b1 << " "<< maxchannel_b2 << " "<< maxchannel_b3 << " " << endl;
		cout << maxchannelamp_b0 << " " << maxchannelamp_b1 << " "<< maxchannelamp_b2 << " "<< maxchannelamp_b3 << " " << endl;
		cout << nfwhm_b0 << " " << nfwhm_b1 << " " << nfwhm_b2 << " " << nfwhm_b3 << " " << endl;
		cout << endl;*/

	  beamPosX_b0 = gsl_stats_wmean(signal_gsl_b0,1,channellist_gsl_b0,1,numtocalc_b0); //calculate the weighted mean
	  beamFocusX_b0 = gsl_stats_wsd_with_fixed_mean(signal_gsl_b0,1,channellist_gsl_b0,1,numtocalc_b0,beamPosX_b0); //Standard Deviation
	  beamSkewX_b0 = gsl_stats_wskew_m_sd(signal_gsl_b0,1,channellist_gsl_b0,1,numtocalc_b0,beamPosX_b0,beamFocusX_b0); //skewness (symmetry)
	  beamKurtX_b0 = gsl_stats_wkurtosis_m_sd(signal_gsl_b0,1,channellist_gsl_b0,1,numtocalc_b0,beamPosX_b0,beamFocusX_b0); //excess kurtosis (well behaved tails)
	  beamNumX_b0 = numtocalc_b0;
	  beamFocusX_b0 *=2.3548;//SD-->FWHM

	  beamPosX_b1 = gsl_stats_wmean(signal_gsl_b1,1,channellist_gsl_b1,1,numtocalc_b1); //calculate the weighted mean
	  beamFocusX_b1 = gsl_stats_wsd_with_fixed_mean(signal_gsl_b1,1,channellist_gsl_b1,1,numtocalc_b1,beamPosX_b1); //Standard Deviation
	  beamSkewX_b1 = gsl_stats_wskew_m_sd(signal_gsl_b1,1,channellist_gsl_b1,1,numtocalc_b1,beamPosX_b1,beamFocusX_b1); //skewness (symmetry)
	  beamKurtX_b1 = gsl_stats_wkurtosis_m_sd(signal_gsl_b1,1,channellist_gsl_b1,1,numtocalc_b1,beamPosX_b1,beamFocusX_b1); //excess kurtosis (well behaved tails)
	  beamNumX_b1 = numtocalc_b1;
	  beamFocusX_b1 *=2.3548;//SD-->FWHM

	  beamPosX_b2 = gsl_stats_wmean(signal_gsl_b2,1,channellist_gsl_b2,1,numtocalc_b2); //calculate the weighted mean
	  beamFocusX_b2 = gsl_stats_wsd_with_fixed_mean(signal_gsl_b2,1,channellist_gsl_b2,1,numtocalc_b2,beamPosX_b2); //Standard Deviation
	  beamSkewX_b2 = gsl_stats_wskew_m_sd(signal_gsl_b2,1,channellist_gsl_b2,1,numtocalc_b2,beamPosX_b2,beamFocusX_b2); //skewness (symmetry)
	  beamKurtX_b2 = gsl_stats_wkurtosis_m_sd(signal_gsl_b2,1,channellist_gsl_b2,1,numtocalc_b2,beamPosX_b2,beamFocusX_b2); //excess kurtosis (well behaved tails)
	  beamNumX_b2 = numtocalc_b2;
	  beamFocusX_b2 *=2.3548;//SD-->FWHM

	  beamPosX_b3 = gsl_stats_wmean(signal_gsl_b3,1,channellist_gsl_b3,1,numtocalc_b3); //calculate the weighted mean
	  beamFocusX_b3 = gsl_stats_wsd_with_fixed_mean(signal_gsl_b3,1,channellist_gsl_b3,1,numtocalc_b3,beamPosX_b3); //Standard Deviation
	  beamSkewX_b3 = gsl_stats_wskew_m_sd(signal_gsl_b3,1,channellist_gsl_b3,1,numtocalc_b3,beamPosX_b3,beamFocusX_b3); //skewness (symmetry)
	  beamKurtX_b3 = gsl_stats_wkurtosis_m_sd(signal_gsl_b3,1,channellist_gsl_b3,1,numtocalc_b3,beamPosX_b3,beamFocusX_b3); //excess kurtosis (well behaved tails)
	  beamNumX_b3 = numtocalc_b3;
	  beamFocusX_b3 *=2.3548;//SD-->FWHM


	  /////////////////////////////////////////////////
	  /////Add fitting algorithm here
	  /////////////////////////////////////////////////
	  /*

	    use board_b0_ch[i] to fill a TGraph. 
	    fit with a gaussian
	    subtract the difference
	    fill a THF2 with the difference, delta_amp vs channel
	   */



	  rootTree->Fill();

	}
      }//end of loop over frames
      /*    th2_signal_vs_channel_b0->Write();
      th2_signal_vs_channel_b1->Write();
      th2_signal_vs_channel_b2->Write();
      th2_signal_vs_channel_b3->Write();

      th2_signal_vs_channel_bkg_b0->Write();
      th2_signal_vs_channel_bkg_b1->Write();
      th2_signal_vs_channel_bkg_b2->Write();
      th2_signal_vs_channel_bkg_b3->Write();

      th1_signal_b0->Write();
      th1_signal_b1->Write();
      th1_signal_b2->Write();
      th1_signal_b3->Write();*/
      graph_bkg_b0->SetName("graph_bkg_b0"); // graph_bkg_b0->Write();
      graph_bkg_b1->SetName("graph_bkg_b1"); // graph_bkg_b1->Write();
      graph_bkg_b2->SetName("graph_bkg_b2"); // graph_bkg_b2->Write();
      graph_bkg_b3->SetName("graph_bkg_b3"); // graph_bkg_b3->Write();
      sumvector_b0_ptr->Write("sumvector_b0");
      sumvector_b1_ptr->Write("sumvector_b1");
      sumvector_b2_ptr->Write("sumvector_b2");
      sumvector_b3_ptr->Write("sumvector_b3");
      beamontime_ptr->Write("beamontime");
     

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

}
