-
Notifications
You must be signed in to change notification settings - Fork 3
/
raw2evtdat.C
212 lines (184 loc) · 4.97 KB
/
raw2evtdat.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// C++ includes
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <ctime>
#include <bitset>
#include <cmath>
// ROOT includes
#include <TROOT.h>
#include <TFile.h>
#include <TTree.h>
#include <TChain.h>
using namespace std;
long gray_to_int(long gray, int nbit = 16){
std::bitset<16> b_gray(gray);
std::bitset<16> b_int(gray);
for(int i = 15; i > 0; i--){
if(b_int[i] == b_gray[i-1])
b_int[i-1] = 0;
else
b_int[i-1] = 1;
}
return b_int.to_ulong();
}
/// Main function that runs the analysis algorithm on the
/// specified input files
int main(int argc, char* argv[]) {
char inputFileName[400];
char outputFileName[400];
if ( argc < 2 ){
cout << "Error at Input: please specify an input .dat file";
cout << " and an output filename" << endl;
cout << "Example: ./raw2dat input_file.dat" << endl;
cout << "Example: ./raw2dat input_file.dat -o output_file.dat" << endl;
return 1;
}
bool user_output = false;
for (int i=0;i<argc;i++){
sscanf(argv[1],"%s", inputFileName);
if (strncmp(argv[i],"-o",2)==0){
sscanf(argv[i+1],"%s", outputFileName);
user_output = true;
}
}
if(!user_output)
sprintf(outputFileName,"%s.dat",inputFileName);
cout << "Input File: " << inputFileName << endl;
cout << "Output File: " << outputFileName << endl;
long billion = pow(10,9);
string line;
ifstream ifile(inputFileName);
ofstream ofile;
ofile.open(outputFileName);
long eventnum = 0;
long num_trig = -1;
long num_trig_new = -1;
long bcid_trig;
long bcid_trig_new = -1;
bool new_event = false;
// Loop through the entire input file
if(ifile.is_open()){
while(getline(ifile,line)){
long machinetime;
int fifocount;
int cycle;
long fifotrig;
long ph_trig;
string sword0;
string sword1;
long iword0;
long iword1;
long bcid_int;
long bcid_gray;
int MMFE8;
int VMM;
int CH;
int TDO;
int PDO;
// Read line into buffer
char sline[1000];
sprintf(sline,"%s",line.c_str());
// Break up line by spaces
char* p = strtok(sline, " ");
int itok = 0;
//std::cout << p << std::endl;
while(p){
if(itok == 0){
std::stringstream sfifo;
std::string head;
sfifo << p;
sfifo >> head;
//std::cout << head << std::endl;
if (head == "new"){
new_event = true;
p = strtok(NULL, " ");
continue;
}
else{
MMFE8 = atoi(head.c_str());
//sfifo >> MMFE8;
float mt;
sfifo >> machinetime;
sfifo >> mt;
sfifo >> fifocount;
sfifo >> cycle;
sfifo >> std::hex >> fifotrig;
num_trig_new = fifotrig & 65535;
fifotrig = fifotrig >> 16;
ph_trig = fifotrig & 7;
fifotrig = fifotrig >> 4;
bcid_trig_new = fifotrig & 4095;
if (num_trig_new != num_trig){
num_trig = num_trig_new;
}
// if (num_trig_new != num_trig && (MMFE8 != 122 && MMFE8 != 126 && MMFE8 != 106 && MMFE8 != 124 && MMFE8 != 119)){
// num_trig = num_trig_new;
// }
// else if (num_trig_new != num_trig && (MMFE8 == 122 || MMFE8 == 126 || MMFE8 == 106 || MMFE8 == 124) && (fabs(bcid_trig_new-bcid_trig) > 1)){
// num_trig = num_trig_new;
// }
bcid_trig = bcid_trig_new;
}
}
if(itok > 1){
if(itok%2 == 0){
std::stringstream sword;
sword << std::hex << p;
sword >> iword0;
sword0 = string(p);
}
else {
std::stringstream sword;
sword << std::hex << p;
sword >> iword1;
sword1 = string(p);
iword0 = iword0 >> 2;
CH = (iword0 & 63) + 1;
if(iword0 <= 0)
CH = 0;
iword0 = iword0 >> 6;
PDO = iword0 & 1023;
iword0 = iword0 >> 10;
TDO = iword0 & 255;
iword0 = iword0 >> 8;
VMM = iword0 & 7;
bcid_gray = iword1 & 4095;
bcid_int = gray_to_int(bcid_gray);
if(new_event){
//if(num_trig != eventnum){
eventnum++;
ofile << "EventNum " << eventnum;
//ofile << "EventNum " << num_trig + pow(2,16)*cycle;
ofile << " Sec " << machinetime/billion;
ofile << " NS " << machinetime%billion;
//ofile << " BCIDtrig " << bcid_trig;
ofile << endl;
new_event = false;
}
//eventnum = num_trig;
ofile << std::setw(3) << left << VMM;
ofile << std::setw(4) << left << CH;
ofile << std::setw(6) << left << PDO;
ofile << std::setw(6) << left << TDO;
ofile << std::setw(6) << left << bcid_int;
ofile << std::setw(5) << left << MMFE8;
ofile << std::setw(6) << left << bcid_trig;
ofile << std::setw(3) << left << ph_trig;
ofile << std::setw(4) << left << fifocount/2;
ofile << endl;
}
}
p = strtok(NULL, " ");
itok++;
} // End of line read
}
}
ifile.close();
ofile.close();
return 0;
}