-
Notifications
You must be signed in to change notification settings - Fork 0
/
WCDecoder.cpp
156 lines (114 loc) · 3.47 KB
/
WCDecoder.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
// g++ -Wall -o WCDecoder.exe `root-config --cflags --glibs` WCFunctions.cc WCDecoder.cpp
#include "WCFunctions.h"
#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <string>
#include <fstream>
#include <sstream>
#include <algorithm>
#include "TFile.h"
#include "TTree.h"
bool analyze_spill(std::ofstream& outFile, std::vector<int>* data, const bool& verbosity);
int main(int argc, char** argv)
{
//------------
// read infile
std::string inFileName(argv[1]);
std::ifstream inFile(Form("data/WC_capture_%s.txt",inFileName.c_str()),std::ios::binary);
std::ofstream outFile(Form("data/WC_capture_%s_decoded.txt",inFileName.c_str()),std::ios::out);
//--------------
// read the file
std::cout << ">>> reading file " << Form("data/WC_capture_%s.txt",inFileName.c_str()) << std::endl;
std::string line;
std::string str;
int spill = 0;
std::map<int,std::vector<int>* > spills;
while(1)
{
getline(inFile,line,'\n');
if( !inFile.good() ) break;
if( line.find("spill") != std::string::npos )
{
std::cout << line << std::endl;
++spill;
spills[spill] = new std::vector<int>;
continue;
}
//if( spill != 1 ) break;
//----------------
// read spill line
std::stringstream ss(line);
int token;
while( ss >> std::hex >> token )
{
int val = int(token);
spills[spill] -> push_back(val);
}
}
std::cout << std::endl;
bool verbosity = false;
for(std::map<int,std::vector<int>*>::const_iterator mapIt = spills.begin(); mapIt != spills.end(); ++mapIt)
{
int spill = mapIt -> first;
std::vector<int>* data = mapIt -> second;
if( verbosity )
{
std::cout << "\nspill: " << spill << " size: " << data->size() << std::endl;
}
if( data->size() > 20+12*16 ) analyze_spill(outFile,data,verbosity);
}
}
bool analyze_spill(std::ofstream& outFile, std::vector<int>* data, const bool& verbosity)
{
int counterOld = 0;
int counterNew = 0;
controllerHeader myControllerHeader;
std::map<int,TDCSpillHeader> myTDCSpillHeader;
std::map<int,TDCEventHeader> myTDCEventHeader;
counterNew = analyze_controllerHeader(myControllerHeader,data,counterOld,verbosity);
if( counterNew == counterOld )
{
std::cout << ">>> !!! error in controller header <<<" << std::endl;
return false;
}
else
{
counterOld = counterNew;
}
counterNew = analyze_TDCSpillHeader(myTDCSpillHeader,data,myControllerHeader,counterOld,verbosity);
if( counterNew == counterOld )
{
std::cout << ">>> !!! error in TDC spill header <<<" << std::endl;
return false;
}
else
{
counterOld = counterNew;
}
for(int iTrigger = 1; iTrigger <= myControllerHeader.triggerCount; ++iTrigger)
{
counterNew = analyze_TDCEventHeader(myTDCEventHeader,data,iTrigger,counterOld,verbosity);
if( counterNew == counterOld )
{
std::cout << ">>> !!! error in TDC event header <<<" << std::endl;
return false;
}
else
{
writeData(outFile,myControllerHeader,myTDCEventHeader);
counterOld = counterNew;
}
}
if( verbosity )
{
std::cout << "counter: " << counterNew << std::endl;
std::cout << "extra-bits begin " << std::endl;
for(int it = counterNew; it < data->size(); ++it)
std::cout << data->at(it) << " ";
std::cout << std::endl;
std::cout << "extra-bits end " << std::endl;
}
return true;
}