-
Notifications
You must be signed in to change notification settings - Fork 4
/
read_methods.hpp
78 lines (62 loc) · 1.75 KB
/
read_methods.hpp
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
// particle track data
struct particleTrack{
float* x_pos;
float* y_pos;
float* z_pos;
float* u;
float* v;
float* w;
float* track_length;
float* energy;
unsigned int Ntracks;
};
// particle track file length
int file_len(const char* filename){
// file instance
std::fstream fp;
// open file
fp.open(filename);
std::string line;
int len = 0;
// read the number of lines in file
while (std::getline(fp, line)){
++len;
}
return len;
}
particleTrack read_array(const char* filename) {
// create instance of particle Track
particleTrack dataTrack;
// memory allocation
int len = file_len(filename);
dataTrack.x_pos = (float*) malloc(len * sizeof(float));
dataTrack.y_pos = (float*) malloc(len * sizeof(float));
dataTrack.z_pos = (float*) malloc(len * sizeof(float));
dataTrack.u = (float*) malloc(len * sizeof(float));
dataTrack.v = (float*) malloc(len * sizeof(float));
dataTrack.w = (float*) malloc(len * sizeof(float));
dataTrack.track_length = (float*) malloc(len * sizeof(float));
dataTrack.energy = (float*) malloc(len * sizeof(float));
// file instance
std::fstream fp;
// open file
fp.open(filename);
// save number of tracks
dataTrack.Ntracks = len;
// read columns from file
unsigned i = 0;
while (!fp.eof()) {
fp >> dataTrack.x_pos[i] >>
dataTrack.y_pos[i] >>
dataTrack.z_pos[i] >>
dataTrack.u[i] >>
dataTrack.v[i] >>
dataTrack.w[i] >>
dataTrack.track_length[i] >>
dataTrack.energy[i];
i++;
}
// close file
fp.close();
return dataTrack;
}