Subversion Repositories f9daq

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
291 f9daq 1
#include "stdio.h"
2
#include "TROOT.h"
3
#include "TSystem.h"
4
#include "TFile.h"
5
#include "TH1F.h"
6
#include "TH2F.h"
7
#include "TF1.h"
8
#include "TMath.h"
9
 
10
#define NCH 32
11
 
12
#define HEADER_WORD_1 0xFFFF
13
#define HEADER_WORD_2 0xEA0C
14
#define ID_ADC_HG 0x81
15
#define ID_ADC_LG 0x60
16
#define ID_TDC 0xCC
17
#define PRINT_FREQUENCY 10000
18
 
19
void e2r(char* dfile0="test", int dbg=0)
20
{
21
    char fullname[256], sbuff[256];    
22
 
23
        FILE *dfp;
24
        char dfile[256];
25
    sprintf(dfile, "data/%s", dfile0);
26
 
27
    if((dfp=fopen(dfile,"rb")) == NULL) {
28
        printf("Cannot open data file %s !!!\n", dfile);
29
        return;
30
    } else {
31
        printf("Opened data file %s.\n", dfile);
32
    }
33
 
34
    if(sizeof(int) != 4) {
35
        printf("sizeof(int) != 4, sizeof(int) = %d !!!\n", sizeof(int));
36
        return;
37
    }
38
    int dum32;
39
 
40
 
41
 
42
        char hname[256], htitle[256];
43
        TH1F *htdc[2][NCH], *hadc_hg[NCH], *hadc_lg[NCH];
44
 
45
    //opens ROOT file
46
        TFile *rootfile; char fnameroot[256];
47
        sprintf(fnameroot, "root/%s.root", dfile0);
48
        //rootfile = (TFile *) gROOT->FindObjectAny(dfile0); 
49
        //if (rootfile!=NULL) {printf("!!!\n");rootfile->Close();}
50
        //rootfile = new TFile(fnameroot);
51
        //if(rootfile) rootfile->Close();
52
        rootfile = new TFile(fnameroot,"RECREATE",dfile0);
53
 
54
    for(int i=0; i<NCH; i++) {
55
        sprintf(hname, "htdc_le_%d", i);
56
        htdc[0][i] = (TH1F*)gROOT->FindObject(hname);
57
        if(htdc[0][i]) delete htdc[0][i];
58
        htdc[0][i] = new TH1F(hname, hname, 256, -0.5, 255.5);
59
 
60
        sprintf(hname, "htdc_te_%d", i);
61
        htdc[1][i] = (TH1F*)gROOT->FindObject(hname);
62
        if(htdc[1][i]) delete htdc[1][i];
63
        htdc[1][i] = new TH1F(hname, hname, 256, -0.5, 255.5);
64
 
65
        sprintf(hname, "hadc_hg%d", i);
66
        hadc_hg[i] = (TH1F*)gROOT->FindObject(hname);
67
        if(hadc_hg[i]) delete hadc_hg[i];
68
        hadc_hg[i] = new TH1F(hname, hname, 4096, -0.5, 4095.5);
69
 
70
        sprintf(hname, "hadc_lg%d", i);
71
        hadc_lg[i] = (TH1F*)gROOT->FindObject(hname);
72
        if(hadc_lg[i]) delete hadc_lg[i];
73
        hadc_lg[i] = new TH1F(hname, hname, 4096, -0.5, 4095.5);
74
    }
75
 
76
    // ------------- data ----------------------------------------------------
77
 
78
    int ceve = 0;
79
    while(!feof(dfp)) {
80
        fread( &dum32, sizeof(int), 1, dfp);
81
 
82
        int hw1 = (dum32 >> 16) & 0xFFFF;
83
        int hw2 = dum32 & 0xFFFF;
84
        if( (hw1 == HEADER_WORD_1) && (hw2 == HEADER_WORD_2) ) {
85
            fread( &dum32, sizeof(int), 1, dfp);
86
            int Number_of_word = dum32 & 0xFFFF;
87
            fread( &dum32, sizeof(int), 1, dfp);
88
            int EventCounter = (dum32 >> 16) & 0xFFFF;
89
            if(dbg) printf("\n>>>>>> Number_of_word = %d | EventCounter = %d\n", Number_of_word, EventCounter);
90
            //~ else if( !(EventCounter%PRINT_FREQUENCY) ) printf("    EventConter = %d\n", EventCounter);
91
            else if( !(EventCounter%PRINT_FREQUENCY) ) printf(".");
92
            ceve++;
93
            continue;
94
        }
95
 
96
        int id = (dum32 >> 24) & 0xFF;
97
        int ch, overflow, edge, data;
98
        switch(id) {
99
            case ID_ADC_HG:
100
                ch = (dum32 >> 16) & 0x1F;
101
                overflow = (dum32 >> 13) & 0x1;
102
                data = (dum32) & 0xFFF;
103
                //~ printf("ID_ADC_HG: ch = %d | X = %d | data = %d\n", ch, overflow, data);
104
                if(dbg) printf("HG[%2d]=%4d ", ch, data);
105
                hadc_hg[ch]->Fill(data);
106
                break;
107
            case ID_ADC_LG:
108
                ch = (dum32 >> 16) & 0x1F;
109
                overflow = (dum32 >> 13) & 0x1;
110
                data = (dum32) & 0xFFF;
111
                //~ printf("ID_ADC_LG: ch = %d | X = %d | data = %d\n", ch, overflow, data);
112
                if(dbg) printf("LG[%2d]=%4d ", ch, data);
113
                hadc_lg[ch]->Fill(data);
114
                break;
115
            case ID_TDC:
116
                ch = (dum32 >> 16) & 0x1F;
117
                edge = (dum32 >> 15) & 0x1;
118
                data = (dum32) & 0x3FFF;
119
                printf("ID_TDC: ch = %d | X = %d | data = %d\n", ch, overflow, data);
120
                if(edge < 2) htdc[edge][ch]->Fill(data);
121
                break;
122
            default:
123
                printf("default: dum32 = 0x%X!!!\n", dum32);
124
                break;
125
        }
126
        //~ ceve++;
127
        //~ if( !(ceve%PRINT_FREQUENCY) ) printf(".");
128
        if(dbg && (ceve > dbg) ) break;
129
    }
130
    printf("\nProcessed events = %d\n", ceve);
131
 
132
    // ------------------------------------------------------------------------
133
 
134
    if(dfp) fclose(dfp);
135
 
136
    if(dbg) return;    
137
        if(rootfile) {
138
                rootfile->Write();
139
                printf("Saved to %s\n", fnameroot);
140
                rootfile->Close();
141
        }              
142
 
143
    gSystem->Exit(1);
144
}