-
Notifications
You must be signed in to change notification settings - Fork 3
/
raw2root.C
226 lines (195 loc) · 4.83 KB
/
raw2root.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// C++ includes
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <stdio.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: ./raw2root input_file.dat" << endl;
cout << "Example: ./raw2root input_file.dat -o output_file.root" << 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.root",inputFileName);
cout << "Input File: " << inputFileName << endl;
cout << "Output File: " << outputFileName << endl;
long billion = pow(10,9);
string line;
ifstream ifile(inputFileName);
// ROOT output tree and branches
TTree* tree = new TTree("MM_data","MM_data");
int EventNum;
int Time_sec;
int Time_nsec;
int N_mm = 0;
vector<int> trig_BCID;
vector<int> trig_ph;
vector<int> mm_VMM;
vector<int> mm_CH;
vector<int> mm_PDO;
vector<int> mm_TDO;
vector<int> mm_BCID;
vector<int> mm_MMFE8;
vector<int> mm_FIFOcount;
tree->Branch("EventNum", &EventNum);
tree->Branch("Time_sec", &Time_sec);
tree->Branch("Time_nsec", &Time_nsec);
tree->Branch("trig_BCID", &trig_BCID);
tree->Branch("trig_ph", &trig_ph);
tree->Branch("N_mm", &N_mm);
tree->Branch("mm_VMM", &mm_VMM);
tree->Branch("mm_CH", &mm_CH);
tree->Branch("mm_PDO", &mm_PDO);
tree->Branch("mm_TDO", &mm_TDO);
tree->Branch("mm_BCID", &mm_BCID);
tree->Branch("mm_MMFE8", &mm_MMFE8);
tree->Branch("mm_FIFOcount", &mm_FIFOcount);
long machinetime;
float mt;
int fifocount;
int cycle;
long fifotrig;
long num_trig;
long bcid_trig;
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;
int eventnum = -1;
// Loop through the entire input file
if(ifile.is_open()){
while(getline(ifile,line)){
// 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;
while(p){
if(itok == 0){
std::stringstream sfifo;
sfifo << p;
sfifo >> MMFE8;
sfifo >> machinetime;
sfifo >> mt;
sfifo >> fifocount;
sfifo >> cycle;
sfifo >> std::hex >> fifotrig;
num_trig = fifotrig & 65535;
//num_trig = fifotrig & 1048575;
fifotrig = fifotrig >> 16;
ph_trig = fifotrig & 7;
fifotrig = fifotrig >> 4;
bcid_trig = fifotrig & 4095;
if(eventnum > 0 && num_trig != eventnum){
tree->Fill();
trig_BCID.clear();
trig_ph.clear();
mm_VMM.clear();
mm_CH.clear();
mm_PDO.clear();
mm_TDO.clear();
mm_BCID.clear();
mm_MMFE8.clear();
mm_FIFOcount.clear();
N_mm = 0;
}
eventnum = num_trig;
EventNum = num_trig + pow(2,20)*cycle;
Time_sec = machinetime/billion;
Time_nsec = machinetime%billion;
}
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);
mm_VMM.push_back(VMM);
mm_CH.push_back(CH);
mm_PDO.push_back(PDO);
mm_TDO.push_back(TDO);
mm_BCID.push_back(bcid_int);
mm_MMFE8.push_back(MMFE8);
trig_BCID.push_back(bcid_trig);
trig_ph.push_back(ph_trig);
mm_FIFOcount.push_back(fifocount/2);
N_mm++;
}
}
p = strtok(NULL, " ");
itok++;
} // End of line read
}
if(mm_VMM.size() > 0)
tree->Fill();
}
ifile.close();
TFile* fout = new TFile(outputFileName, "RECREATE");
fout->cd();
tree->Write();
fout->Close();
return 0;
}