/
remove_bad_events.cpp
219 lines (179 loc) · 4.17 KB
/
remove_bad_events.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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;
void closefiles();
void clearbuffers();
bool my_getline(istream&, string&);
string skipToNextEvent(istream&);
ifstream file_input;
ofstream file_output;
string buffer=""; //Buffer string for event header
string templine;
int main(int argc, char** argv)
{
if(argc != 3)
{
cout << "Usage: cleaner <inputfile> <outputfile> <multiplicity>" << endl;
return 1;
}
string inputfile = argv[1];
string outputfile = argv[2];
cout << "Reading from file: " << inputfile << " and writing out to file: " << outputfile << endl;
//Opening files
file_input.open(inputfile.c_str());
file_output.open(outputfile.c_str());
string check;
stringstream ss;
unsigned int i, mult, events=0, bad_events=0;
bool last_event_good = true;
while(true)
{
if(last_event_good) //If the previous event was skipped, then the first line of header is already read
{
//Getting first line
if(!(file_input.good()))
{
cout << "Reading finished" << endl;
if(file_input.eof())
cout << "End of file reached" << endl;
else
{
cerr << "Reading error" << endl;
closefiles();
return 2;
}
break;
}
if(!(my_getline(file_input,templine))) //I expect "UQMD" word here
{
cerr << "Inputfile corrupted. Last good event: " << events << endl;
//cout << "Line: " << templine << endl;
return 3;
}
else
{
buffer.append(templine);
buffer.append("\n");
}
}
else
last_event_good = true;
if((++events%100)==0)
cout << "Event: " << events << endl;
//Next 16 lines from header
for(i=0;i<16;i++)
{
if(my_getline(file_input,templine))
{
cout << "Event: " << events << " - corrupted header" << endl;
//cout << "Line: " << templine << endl;
return 4;
}
else
{
buffer.append(templine);
buffer.append("\n");
}
}
//Multiplicity line
if(my_getline(file_input, templine))
{
cout << "Event: " << events << " - could not find multiplicity line" << endl;
closefiles();
return 5;
}
else
{
buffer.append(templine);
buffer.append("\n");
}
ss.str(templine);
ss.clear();
ss >> mult;
//Collisions line
if(my_getline(file_input, templine))
{
cout << "Event: " << events << " - could not find collisions line" << endl;
closefiles();
return 6;
}
else
{
buffer.append(templine);
buffer.append("\n");
}
last_event_good = true;
//Particle list
for(i=0;i<mult;i++)
{
if(my_getline(file_input, templine))
{
string uqmdline = templine;
cout << "Event: " << events << " - corrupted particle list" << endl;
//Cleaning buffers because we don't need corrupted event
clearbuffers();
//Appending first line from new event to fresh buffer
buffer.append(uqmdline);
buffer.append("\n");
//Jumping back to the top of this loop
last_event_good = false;
++bad_events;
break;
}
else
{
buffer.append(templine);
buffer.append("\n");
}
}
if(!last_event_good)
continue;
//If we are here, then the event has been read correctly. Now it will be re-written to the output file
//Writing to output file from buffer
file_output << buffer;
//Clear strings
clearbuffers();
}
closefiles();
cout << "Number of skipped bad events: " << bad_events << endl;
return 0;
}
bool my_getline(istream& is, string& line_buffer)
{
static stringstream sstream;
static string firstword;
firstword = "";
getline(is, line_buffer);
sstream.str(line_buffer);
sstream.clear();
sstream >> firstword;
if(firstword.compare("UQMD")==0)
return true;
else
return false;
}
string skipToNextEvent(istream& is)
{
static string nothing = "";
unsigned int counter = 0;
while(!my_getline(is,nothing)) //Fast skipping until UQMD word is found. TODO - handle the case of end-of-file
++counter;
//If we are here, the "UQMD" word was finally found
cout << "Skipped " << counter << " lines" << endl;
//Returning the line which was found
return nothing;
}
void closefiles()
{
file_input.close();
file_output.close();
}
void clearbuffers()
{
templine.clear();
buffer.clear();
}