我正在尝试从文本文件中导入数据并将其分配给变量,以便我可以使用函数对其进行分析。数据格式如下:
Run 141544 event 5
Njets 0
m1: pt,eta,phi,m= 231.277 0.496237 -2.22082 0.1 dptinv: 0.000370146
m2: pt,eta,phi,m= 222.408 -0.198471 0.942319 0.1 dptinv: 0.00038302
Run 141544 event 7
Njets 1
m1: pt,eta,phi,m= 281.327 -0.489914 1.12498 0.1 dptinv: 0.000406393
m2: pt,eta,phi,m= 238.38 0.128715 -2.07527 0.1 dptinv: 0.000399279
... 大约有 15000 个条目,每个条目有四行。在每一行上,值由空格分隔,每个条目之间有一个空行。因为条目的每一行都是不同的格式,所以我写了一个循环来分隔这些案例。我遇到的问题是分配变量的代码似乎有问题。当我使用循环输出某种类型的行时,一切运行良好。但是,一旦我尝试将每一行分解为变量并分配和打印变量,程序就会多次打印同一行并崩溃。这是我的代码:
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <numeric>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <iterator>
using namespace std;
using std::cout;
using std::endl;
struct rowtype1 // structure of lines containing run data
{
string runnumber;
string eventnumber;
};
struct rowtype2 // structure of lines containing data for muon1 and muon2
{
string ptvalue1;
string etavalue1;
string phivalue1;
string massvalue1;
};
vector<rowtype1> row1values;
vector<rowtype2> row2values;
int main()
{
string line;
ifstream inData;
inData.open("/Users/Edward/Downloads/muons.txt");
if (inData.is_open())
{
while ( inData.good() )
{
while (getline(inData,line))
{
if (line[0] == 'N') // recognizes and skips blank lines
{
continue;
}
else if (line[1] == 'u') // recognizes lines containing run information
{
istringstream ss(line);
istream_iterator<string> begin(ss), end;
vector<string> words(begin, end);
rowtype1 s { words[1], words[3]};
row1values.push_back(s);
for(auto && s : row1values)
cout << "run " << s.runnumber << " " << "event " << s.eventnumber << "\n";
}
else if (line[1] == '1') // recognizes lines containing muon1 information
{
istringstream ss(line);
istream_iterator<string> begin(ss), end;
vector<string> words(begin, end);
rowtype2 s { words[2], words[3], words[4], words[5] };
row2values.push_back(s);
for(auto && s : row2values)
cout << "m1 " << s.ptvalue1 << " " << s.etavalue1 << " " << s.phivalue1 << " " << s.massvalue1 << "\n";
}
else if (line[1] == '2') // recognizes lines containing muon2 information
{
istringstream ss(line);
istream_iterator<string> begin(ss), end;
vector<string> words(begin, end);
rowtype2 s { words[2], words[3], words[4], words[5] };
row2values.push_back(s);
for(auto && s : row2values)
cout << "m2 " << s.ptvalue1 << " " << s.etavalue1 << " " << s.phivalue1 << " " << s.massvalue1 << "\n";
}
}
}
inData.close();
}
return 0;
};
为了测试变量是否被正确分配,我让代码输出它们的值。不是循环遍历行并输出变量,而是输出如下所示:
run 141544 event 5
Run 141544 event 5
m1 231.277 0.496237 -2.22082 0.1
m2 231.277 0.496237 -2.22082 0.1
m2 222.408 -0.198471 0.942319 0.1
run 141544 event 5
run 141544 event 7
Run 141544 event 7
m1 231.277 0.496237 -2.22082 0.1
m1 222.408 -0.198471 0.942319 0.1
m1 281.327 -0.489914 1.12498 0.1
m2 231.277 0.496237 -2.22082 0.1
m2 222.408 -0.198471 0.942319 0.1
m2 281.327 -0.489914 1.12498 0.1
m2 238.38 0.128715 -2.07527 0.1
run 141544 event 5
run 141544 event 7
run 141572 event 2