-
Notifications
You must be signed in to change notification settings - Fork 1
/
decode.C
96 lines (82 loc) · 2.53 KB
/
decode.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <string.h>
#include <stdio.h>
#include "TFile.h"
#include "TTree.h"
#include "TString.h"
#include <iostream>
struct Header_t {
char event_header[4];
unsigned int serial_number;
unsigned short year;
unsigned short month;
unsigned short day;
unsigned short hour;
unsigned short minute;
unsigned short second;
unsigned short millisecond;
unsigned short reserved1;
float time[1024];
};
struct Waveform_t {
char chn1_header[4];
unsigned short chn1[1024];
char chn2_header[4];
unsigned short chn2[1024];
char chn3_header[4];
unsigned short chn3[1024];
char chn4_header[4];
unsigned short chn4[1024];
};
void decode(char *filename) {
Header_t header;
Waveform_t waveform;
Double_t t[1024], chn1[1024], chn2[1024], chn3[1024], chn4[1024];
Int_t n;
Int_t k =0;
// cout << "0" << endl;
// open the binary waveform file
FILE *f;
f = fopen(Form("%s.dat", filename), "r");
// cout << "1" << endl;
//open the root file
TFile *outfile = new TFile(Form("%s.root", filename), "RECREATE");
// define the output tree
TTree *rec = new TTree("rec","rec");
rec->Branch("t", &t ,"t[1024]/D");
rec->Branch("n", &k ,"n/I");
// cout << "2" << endl;
// Create branches for the channels
rec->Branch("chn1", &chn1 ,"chn1[1024]/D");
rec->Branch("chn2", &chn2 ,"chn2[1024]/D");
rec->Branch("chn3", &chn3 ,"chn3[1024]/D");
rec->Branch("chn4", &chn4 ,"chn4[1024]/D");
// cout << "21" << endl;
// loop over all events in data file
for (n=0 ; fread(&header, sizeof(header), 1, f) > 0; n++) {
// cout << "30" << endl;
if (n%1000 == 0){
cout << n << endl;
}
// decode time
for (Int_t i=0; i<1024; i++)
t[i] = (Double_t) header.time[i];
// cout << "3" << endl;
fread(&waveform, sizeof(waveform), 1, f);
// decode amplitudes in mV
for (Int_t i=0; i<1024; i++) {
chn1[i] = (Double_t) ((waveform.chn1[i]) / 65535. - 0.5) * 1000;
chn2[i] = (Double_t) ((waveform.chn2[i]) / 65535. - 0.5) * 1000;
chn3[i] = (Double_t) ((waveform.chn3[i]) / 65535. - 0.5) * 1000;
chn4[i] = (Double_t) ((waveform.chn4[i]) / 65535. - 0.5) * 1000;
}
//cout << "5" << endl;
rec->Fill();
k++;
}
// print number of events
cout<<n<<" events processed"<<endl;
cout<<"\""<<Form("%s.root", filename)<<"\" written"<<endl;
// save and close root file
rec->Write();
outfile->Close();
}