#include "stdio.h"
 
#include "TROOT.h"
 
#include "TSystem.h"
 
#include "TFile.h"
 
#include "TH1F.h"
 
#include "TH2F.h"
 
#include "TF1.h"
 
#include "TMath.h"
 
 
 
#define NCH 32
 
 
 
#define HEADER_WORD_1 0xFFFF
 
#define HEADER_WORD_2 0xEA0C
 
#define ID_ADC_HG 0x81
 
#define ID_ADC_LG 0x60
 
#define ID_TDC 0xCC
 
#define PRINT_FREQUENCY 10000
 
 
 
void e2r(char* dfile0="test", int dbg=0)
 
{
 
    char fullname[256], sbuff[256];    
 
    
 
        FILE *dfp;
 
        char dfile[256];
 
    sprintf(dfile, "data/%s", dfile0);
 
    
 
    if((dfp=fopen(dfile,"rb")) == NULL) {
 
        printf("Cannot open data file %s !!!\n", dfile);
 
        return;
 
    } else {
 
        printf("Opened data file %s.\n", dfile);
 
    }
 
    
 
    if(sizeof(int) != 4) {
 
        printf("sizeof(int) != 4, sizeof(int) = %d !!!\n", sizeof(int));
 
        return;
 
    }
 
    int dum32;
 
    
 
    
 
    
 
        char hname[256], htitle[256];
 
        TH1F *htdc[2][NCH], *hadc_hg[NCH], *hadc_lg[NCH];
 
    
 
    //opens ROOT file
 
        TFile *rootfile; char fnameroot[256];
 
        sprintf(fnameroot, "root/%s.root", dfile0); 
 
        //rootfile = (TFile *) gROOT->FindObjectAny(dfile0); 
 
        //if (rootfile!=NULL) {printf("!!!\n");rootfile->Close();}
 
        //rootfile = new TFile(fnameroot);
 
        //if(rootfile) rootfile->Close();
 
        rootfile = new TFile(fnameroot,"RECREATE",dfile0);
 
    
 
    for(int i=0; i<NCH; i++) {
 
        sprintf(hname, "htdc_le_%d", i);
 
        htdc[0][i] = (TH1F*)gROOT->FindObject(hname);
 
        if(htdc[0][i]) delete htdc[0][i];
 
        htdc[0][i] = new TH1F(hname, hname, 256, -0.5, 255.5);
 
        
 
        sprintf(hname, "htdc_te_%d", i);
 
        htdc[1][i] = (TH1F*)gROOT->FindObject(hname);
 
        if(htdc[1][i]) delete htdc[1][i];
 
        htdc[1][i] = new TH1F(hname, hname, 256, -0.5, 255.5);
 
        
 
        sprintf(hname, "hadc_hg%d", i);
 
        hadc_hg[i] = (TH1F*)gROOT->FindObject(hname);
 
        if(hadc_hg[i]) delete hadc_hg[i];
 
        hadc_hg[i] = new TH1F(hname, hname, 4096, -0.5, 4095.5);
 
        
 
        sprintf(hname, "hadc_lg%d", i);
 
        hadc_lg[i] = (TH1F*)gROOT->FindObject(hname);
 
        if(hadc_lg[i]) delete hadc_lg[i];
 
        hadc_lg[i] = new TH1F(hname, hname, 4096, -0.5, 4095.5);
 
    }
 
   
 
    // ------------- data ----------------------------------------------------
 
    
 
    int ceve = 0;
 
    while(!feof(dfp)) {
 
        fread( &dum32, sizeof(int), 1, dfp);
 
        
 
        int hw1 = (dum32 >> 16) & 0xFFFF;
 
        int hw2 = dum32 & 0xFFFF;
 
        if( (hw1 == HEADER_WORD_1) && (hw2 == HEADER_WORD_2) ) {
 
            fread( &dum32, sizeof(int), 1, dfp);
 
            int Number_of_word = dum32 & 0xFFFF;
 
            fread( &dum32, sizeof(int), 1, dfp);
 
            int EventCounter = (dum32 >> 16) & 0xFFFF;
 
            if(dbg) printf("\n>>>>>> Number_of_word = %d | EventCounter = %d\n", Number_of_word, EventCounter);
 
            //~ else if( !(EventCounter%PRINT_FREQUENCY) ) printf("    EventConter = %d\n", EventCounter);
 
            else if( !(EventCounter%PRINT_FREQUENCY) ) printf(".");
 
            ceve++;
 
            continue;
 
        }
 
            
 
        int id = (dum32 >> 24) & 0xFF;
 
        int ch, overflow, edge, data;
 
        switch(id) {
 
            case ID_ADC_HG:
 
                ch = (dum32 >> 16) & 0x1F;
 
                overflow = (dum32 >> 13) & 0x1;
 
                data = (dum32) & 0xFFF;
 
                //~ printf("ID_ADC_HG: ch = %d | X = %d | data = %d\n", ch, overflow, data);
 
                if(dbg) printf("HG[%2d]=%4d ", ch, data);
 
                hadc_hg[ch]->Fill(data);
 
                break;
 
            case ID_ADC_LG:
 
                ch = (dum32 >> 16) & 0x1F;
 
                overflow = (dum32 >> 13) & 0x1;
 
                data = (dum32) & 0xFFF;
 
                //~ printf("ID_ADC_LG: ch = %d | X = %d | data = %d\n", ch, overflow, data);
 
                if(dbg) printf("LG[%2d]=%4d ", ch, data);
 
                hadc_lg[ch]->Fill(data);
 
                break;
 
            case ID_TDC:
 
                ch = (dum32 >> 16) & 0x1F;
 
                edge = (dum32 >> 15) & 0x1;
 
                data = (dum32) & 0x3FFF;
 
                printf("ID_TDC: ch = %d | X = %d | data = %d\n", ch, overflow, data);
 
                if(edge < 2) htdc[edge][ch]->Fill(data);
 
                break;
 
            default:
 
                printf("default: dum32 = 0x%X!!!\n", dum32);
 
                break;
 
        }
 
        //~ ceve++;
 
        //~ if( !(ceve%PRINT_FREQUENCY) ) printf(".");
 
        if(dbg && (ceve > dbg) ) break;
 
    }
 
    printf("\nProcessed events = %d\n", ceve);
 
    
 
    // ------------------------------------------------------------------------
 
    
 
    if(dfp) fclose(dfp);
 
    
 
    if(dbg) return;     
 
        if(rootfile) {
 
                rootfile->Write();
 
                printf("Saved to %s\n", fnameroot);
 
                rootfile->Close();
 
        }               
 
        
 
    gSystem->Exit(1); 
 
}