Commit 0874ac58 authored by legoc's avatar legoc

Improved lstexporter128 with the possibility to filter a set of channels with a ratio.

parent 1fc17707
Version 1.3.3, ?
-------------------------
- Improved lstexporter128 with the possibility to filter a set of channels with a ratio.
Version 1.3.2, 2019-05-28
-------------------------
- Added GGDMS and GGFMCPICKUP boards.
......
......@@ -31,33 +31,6 @@ using namespace lstdpp128;
const int BUFFER_SIZE = 1 << 10;
std::vector<int32_t> split(const std::string& content) {
vector<int32_t> result;
int lastIndex = 0;
int index = content.find(',');
while (index != string::npos) {
istringstream is(content.substr(lastIndex, index - lastIndex));
int32_t value;
is >> value;
result.push_back(value);
lastIndex = index + 1;
index = content.find(',', lastIndex);
}
istringstream is(content.substr(lastIndex));
int32_t value;
is >> value;
result.push_back(value);
return result;
}
std::string eventColumnsToCSV() {
ostringstream os;
......@@ -191,7 +164,53 @@ std::string eventDataToCSV(Event const & event) {
return os.str();
}
void process(int32_t * buffer, int32_t size, int32_t crate, int32_t board, int32_t channel, int numberOfEvents, int32_t& eventId, ofstream & outfile) {
struct Filter {
int32_t crate;
int32_t board;
int32_t channel;
float ratio;
int32_t kept;
int32_t all;
Filter() : crate(-1), board(-1), channel(-1), ratio(-1.f), kept(0), all(0) {}
};
bool keepEvent(const Event& event, std::vector<Filter>& filters) {
if (filters.empty()) {
return true;
}
for (auto& f : filters) {
if ((f.crate == -1 || (f.crate == event.crate))
&& (f.board == -1 || (f.board == event.board))
&& (f.channel == -1 || (f.channel == event.channel))) {
f.all++;
if (f.ratio == -1.f) {
return true;
}
else {
float r = static_cast<float>(f.kept + 1) / static_cast<float>(f.all);
//cout << "event " << static_cast<float>(f.kept + 1) << " " << static_cast<float>(f.all) << " " << r << " " << f.ratio << endl;
if (r < f.ratio) {
f.kept++;
//cout << " kept" << endl;
return true;
}
else {
return false;
}
}
}
}
return false;
}
void process(int32_t * buffer, int32_t size, std::vector<Filter>& filters, int numberOfEvents, int32_t& eventId, ofstream & outfile) {
// Reading next block.
int32_t index = 0;
......@@ -203,9 +222,7 @@ void process(int32_t * buffer, int32_t size, int32_t crate, int32_t board, int32
Event event;
if (readEvent(event, bufferPtr + index)) {
bool display = ((crate == -1 || (crate == event.crate))
&& (board == -1 || (board == event.board))
&& (channel == -1 || (channel == event.channel)));
bool display = keepEvent(event, filters);
if (display) {
outfile << eventToCSV(event) << eventDataToCSV(event) << endl;
......@@ -230,42 +247,115 @@ void process(int32_t * buffer, int32_t size, int32_t crate, int32_t board, int32
}
}
std::vector<string> split(const std::string& content) {
//cout << "split " << content << endl;
vector<string> result;
int lastIndex = 0;
int index = content.find(';');
while (index != string::npos) {
result.push_back(content.substr(lastIndex, index - lastIndex));
lastIndex = index + 1;
index = content.find(';', lastIndex);
}
result.push_back(content.substr(lastIndex));
return result;
}
std::vector<string> splitValues(const std::string& content) {
//cout << "split values " << content << endl;
vector<string> result;
int lastIndex = 0;
int index = content.find(',');
while (index != string::npos) {
result.push_back(content.substr(lastIndex, index - lastIndex));
lastIndex = index + 1;
index = content.find(',', lastIndex);
}
result.push_back(content.substr(lastIndex));
return result;
}
Filter splitFilter(const std::string& valuesString) {
//cout << "split filter " << valuesString << endl;
string valuesSplit = valuesString.substr(1, valuesString.length() - 2);
vector<string> values = splitValues(valuesSplit);
cout << "filter:" << endl;
Filter filter;
if (values.size() > 0) {
istringstream is(values[0]);
is >> filter.crate;
cout << "crate " << filter.crate << endl;
}
if (values.size() > 1) {
istringstream is(values[1]);
is >> filter.board;
cout << "board " << filter.board << endl;
}
if (values.size() > 2) {
istringstream is(values[2]);
is >> filter.channel;
cout << "channel " << filter.channel << endl;
}
if (values.size() > 3) {
istringstream is(values[3]);
is >> filter.ratio;
cout << "ratio " << filter.ratio << endl;
}
return filter;
}
int main(int argc, char * argv[]) {
if (argc < 3) {
cerr << "usage : lstreader128 <file> <outfile> [filter] [number of events]" << endl;
cerr << " filter is [crate,board,channel] or [crate,board] or [crate]" << endl;
cerr << "usage : lstexporter128 <file> <outfile> [filter] [number of events]" << endl;
cerr << " filter is a set of:" << endl;
cerr << " [crate,board,channel,ratio]" << endl;
cerr << " [crate,board,channel]" << endl;
cerr << " [crate,board]" << endl;
cerr << " [crate]" << endl;
cerr << " example: \"[[0,0];[0,1,3,0.2];[0,1,4]]\"" << endl;
cerr << " it is necessary to put \" around the filters." << endl;
return EXIT_FAILURE;
}
string fileName = argv[1];
int32_t crate = -1;
int32_t board = -1;
int32_t channel = -1;
string outfileName = argv[2];
if (argc >= 4) {
string valuesString(argv[3]);
valuesString = valuesString.substr(1, valuesString.length() - 2);
vector<int32_t> values = split(valuesString);
cout << "filter:" << endl;
vector<Filter> filters;
if (values.size() > 0) {
crate = values[0];
cout << "crate " << crate << endl;
}
if (argc >= 4) {
string filtersString(argv[3]);
cout << "filters " << filtersString << endl;
filtersString = filtersString.substr(1, filtersString.length() - 2);
if (values.size() > 1) {
board = values[1];
cout << "board " << board << endl;
}
vector<string> filtersStrings = split(filtersString);
if (values.size() > 2) {
channel = values[2];
cout << "channel " << channel << endl;
for (auto f : filtersStrings) {
Filter filter = splitFilter(f);
filters.push_back(filter);
}
}
......@@ -326,7 +416,7 @@ int main(int argc, char * argv[]) {
}
// Processing.
process(reader.buffer(), size, crate, board, channel, numberOfEvents, eventId, outfile);
process(reader.buffer(), size, filters, numberOfEvents, eventId, outfile);
// Break if number of events is reached.
if (numberOfEvents != -1 && eventId >= numberOfEvents) {
......
......@@ -31,7 +31,7 @@ using namespace lstdpp128;
const int BUFFER_SIZE = 1 << 10;
std::vector<int32_t> split(const std::string& content) {
std::vector<int32_t> splitValues(const std::string& content) {
vector<int32_t> result;
......@@ -120,7 +120,7 @@ int main(int argc, char * argv[]) {
if (argc >= 4) {
string valuesString(argv[3]);
valuesString = valuesString.substr(1, valuesString.length() - 2);
vector<int32_t> values = split(valuesString);
vector<int32_t> values = splitValues(valuesString);
cout << "filter:" << endl;
......
......@@ -36,7 +36,7 @@ using namespace lstdpp128;
const int BUFFER_SIZE = 1 << 10;
std::vector<int32_t> split(const std::string& content) {
std::vector<int32_t> splitValues(const std::string& content) {
vector<int32_t> result;
......@@ -328,7 +328,7 @@ int main(int argc, char * argv[]) {
if (argc >= 3) {
string valuesString(argv[2]);
valuesString = valuesString.substr(1, valuesString.length() - 2);
vector<int32_t> values = split(valuesString);
vector<int32_t> values = splitValues(valuesString);
cout << "filter:" << endl;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment