forked from cmsromadaq/DQM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadBinary.cpp
272 lines (224 loc) · 7.25 KB
/
readBinary.cpp
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <fstream>
#include <iostream>
#include <vector>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <TFile.h>
#include <TTree.h>
#define MAX_ADC_CHANNELS 200
#define MAX_TDC_CHANNELS 200
struct treeStructData
{
// unsigned int runNumber; to be added soon
unsigned int evtNumber;
unsigned int evtTimeDist;
unsigned int evtTimeStart;
unsigned int evtTime;
unsigned int boardTriggerBit;
unsigned int triggerWord;
unsigned int nAdcChannels;
unsigned int adcBoard[MAX_ADC_CHANNELS];
unsigned int adcChannel[MAX_ADC_CHANNELS];
unsigned int adcData[MAX_ADC_CHANNELS];
unsigned int nTdcChannels;
unsigned int tdcBoard[MAX_TDC_CHANNELS];
unsigned int tdcChannel[MAX_TDC_CHANNELS];
unsigned int tdcData[MAX_TDC_CHANNELS];
};
struct adcData
{
unsigned int board;
unsigned int channel;
unsigned int adcReadout;
};
struct tdcData
{
unsigned int board;
unsigned int channel;
unsigned int tdcReadout;
};
struct myDAQEventData
{
unsigned int evtNumber;
unsigned int boardTriggerBit;
std::vector<bool> triggerWord;
std::vector<adcData> adcValues;
std::vector<tdcData> tdcValues;
unsigned int evtTimeDist;
unsigned int evtTimeStart;
unsigned int evtTime;
};
void cannotOpenFile(char * file)
{
fprintf (stderr,
"File %s cannot be opened\n",
file);
exit(-1);
}
void fillTreeData(myDAQEventData& data,treeStructData& treeData)
{
treeData.evtNumber=data.evtNumber;
treeData.evtTime=data.evtTime;
treeData.evtTimeStart=data.evtTimeStart;
treeData.evtTimeDist=data.evtTimeDist;
treeData.boardTriggerBit=data.boardTriggerBit;
treeData.triggerWord=0;
for (unsigned int i=0;i<fmin(32,data.triggerWord.size());++i)
treeData.triggerWord+=data.triggerWord[i]>>i;
treeData.nAdcChannels=data.adcValues.size();
for (unsigned int i=0;i<fmin(MAX_ADC_CHANNELS,data.adcValues.size());++i)
{
treeData.adcBoard[i]=data.adcValues[i].board;
treeData.adcChannel[i]=data.adcValues[i].channel;
treeData.adcData[i]=data.adcValues[i].adcReadout;
}
treeData.nTdcChannels=data.tdcValues.size();
for (unsigned int i=0;i<fmin(MAX_TDC_CHANNELS,data.tdcValues.size());++i)
{
treeData.tdcBoard[i]=data.tdcValues[i].board;
treeData.tdcChannel[i]=data.tdcValues[i].channel;
treeData.tdcData[i]=data.tdcValues[i].tdcReadout;
}
}
void createOutBranches(TTree* tree,treeStructData& treeData)
{
//Instantiate the tree branches
tree->Branch("evtNumber",&treeData.evtNumber,"evtNumber/i");
tree->Branch("evtTimeDist",&treeData.evtTimeDist,"evtTimeDist/i");
tree->Branch("evtTimeStart",&treeData.evtTimeStart,"evtTimeStart/i");
tree->Branch("evtTime",&treeData.evtTime,"evtTime/i");
tree->Branch("boardTriggerBit",&treeData.boardTriggerBit,"boardTriggerBit/i");
tree->Branch("triggerWord",&treeData.triggerWord,"triggerWord/i");
tree->Branch("nAdcChannels",&treeData.nAdcChannels,"nAdcChannels/i");
tree->Branch("adcBoard",treeData.adcBoard,"adcBoard[nAdcChannels]/i");
tree->Branch("adcChannel",treeData.adcChannel,"adcChannel[nAdcChannels]/i");
tree->Branch("adcData",treeData.adcData,"adcData[nAdcChannels]/i");
tree->Branch("nTdcChannels",&treeData.nTdcChannels,"nTdcChannels/i");
tree->Branch("tdcBoard",treeData.tdcBoard,"tdcBoard[nTdcChannels]/i");
tree->Branch("tdcChannel",treeData.tdcChannel,"tdcChannel[nTdcChannels]/i");
tree->Branch("tdcData",treeData.tdcData,"tdcData[nTdcChannels]/i");
}
int main(int argc, char *argv[])
{
char *filename = NULL;
char *outfile = NULL;
int c;
bool adc792=false;
while ((c = getopt (argc, argv, "7f:o:")) != -1)
switch (c)
{
case 'f':
filename = optarg;
break;
case 'o':
outfile = optarg;
break;
case '7':
adc792 = true;
break;
case '?':
if (optopt == 'f' || optopt == 'o')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
return 1;
default:
exit (-1);
}
std::ifstream myFile (filename, std::ios::in | std::ios::binary);
if (!myFile.is_open())
cannotOpenFile(filename);
std::streampos size;
myFile.seekg(0,std::ios::end);
size=myFile.tellg();
fprintf(stdout,
"File %s opened with size %d\n",
filename,(int)size
);
myFile.seekg(0,std::ios::beg);
TFile *out = TFile::Open(outfile,"RECREATE");
if (!out->IsOpen())
cannotOpenFile(outfile);
TTree* tree=new TTree("eventRawData","VME event RAW Data");
treeStructData thisTreeEvent;
createOutBranches(tree,thisTreeEvent);
unsigned int evtSize;
unsigned int adcChannels;
unsigned int eventHeaderSize;
myDAQEventData thisEvent;
unsigned int totEvents=0;
while (!myFile.eof())
{
myFile.read ((char*)&evtSize, sizeof(evtSize));
if (myFile.eof())
continue;
myFile.read ((char*)&thisEvent.evtNumber, sizeof(thisEvent.evtNumber));
if (thisEvent.evtNumber % 1000 == 0)
{
std::cout << "Event Number " << thisEvent.evtNumber << std::endl;
}
myFile.read ((char*)&thisEvent.boardTriggerBit, sizeof(thisEvent.boardTriggerBit));
thisEvent.triggerWord.clear();
thisEvent.adcValues.clear();
thisEvent.tdcValues.clear();
myFile.read ((char*)&adcChannels, sizeof(adcChannels));
myFile.read ((char*)&eventHeaderSize, sizeof(eventHeaderSize));
for (int i=0;i<adcChannels;++i)
{
unsigned int adcRawData;
myFile.read ((char*)&adcRawData, sizeof(adcRawData));
if (!adc792)
{
//FOR ADC265 DATA
adcData thisData;
thisData.board=0; //for the moment just 1 board
//These bit masks are valid for the ADC V265
thisData.channel = (adcRawData & 0xe000)>>13;
thisData.adcReadout = adcRawData & 0xfff;
thisEvent.adcValues.push_back(thisData);
}
else
{
short dt_type = adcRawData>>24 & 0x7; //dt_type 0 is datum,2 is BOE,4 is EOE
if (dt_type==0)
{
//FOR ADC792 DATA
adcData thisData;
thisData.board=0; //for the moment just 1 board
thisData.channel = adcRawData>>16 & 0x1F;
thisData.adcReadout = adcRawData & 0xFFF;
if ((adcRawData>>12) & 0x1) //overflow
thisData.adcReadout=4095;
thisEvent.adcValues.push_back(thisData);
}
if (dt_type==4)
{
unsigned int adcEvt=adcRawData & 0xFFFFFF;
if (adcEvt+1 != thisEvent.evtNumber)
std::cout << "HEY MISMATCH IN EVT NUMBER ADCEVT " << adcEvt+1 << " EVT " << thisEvent.evtNumber << std::endl;
}
//printf("WORD %d: %X %d\n",i,adcRawData,dt_type);
}
}
// std::cout << "This event has " << thisEvent.adcValues.size() << " ADC channels " << std::endl;
myFile.read ((char*)&thisEvent.evtTimeDist, sizeof(thisEvent.evtTimeDist));
myFile.read ((char*)&thisEvent.evtTimeStart, sizeof(thisEvent.evtTimeStart));
myFile.read ((char*)&thisEvent.evtTime, sizeof(thisEvent.evtTime));
fillTreeData(thisEvent,thisTreeEvent);
tree->Fill();
++totEvents;
}
std::cout << totEvents << " events were read in total" << std::endl;
tree->Write();
out->Close();
std::cout << outfile << " is closed" << std::endl;
myFile.close();
std::cout << filename << " is closed" << std::endl;
}