-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticleSamplePhasespace.cpp
More file actions
313 lines (273 loc) · 10.7 KB
/
ParticleSamplePhasespace.cpp
File metadata and controls
313 lines (273 loc) · 10.7 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* This file is a part of runjam <https://github.com/idthic/runjam>.
Copyright (C) 2014-2020, Koichi Murase <myoga.murase at gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA */
#include <cstdio>
#include <cstdlib>
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include <cstdint>
#include "ParticleSample.hpp"
namespace idt {
namespace runjam {
namespace {
class ParticleSampleReadPhasespaceData: public ParticleSampleBase {
typedef ParticleSampleBase base;
std::string fname_phasespace_dat;
private:
double m_overSamplingFactor; // イベントの一行目の $2 から読み取る
int m_numberOfSamples;
int m_currentSampleIndex;
public:
void setNumberOfSamples(int value) {
this->m_numberOfSamples = value;
}
int getNumberOfSamples() const {
return m_numberOfSamples;
}
virtual double getOverSamplingFactor() const override {
return m_overSamplingFactor;
}
public:
ParticleSampleReadPhasespaceData(runjam_context const& ctx, std::string const& fname_phasespace_dat):
fname_phasespace_dat(fname_phasespace_dat),
m_overSamplingFactor(ctx.get_config("runjam_oversampling_factor", 1.0))
{
this->m_currentSampleIndex = -1;
this->m_numberOfSamples = -1;
}
private:
std::vector<std::size_t> pcache;
void checkCacheAvailability() {
if (m_currentSampleIndex < 0) {
std::cerr << "ParticleSampleReadPhasespaceData: A phasespace file has not been read." << std::endl;
std::exit(EXIT_FAILURE);
} else if ((std::size_t) m_currentSampleIndex >= this->pcache.size() - 1) {
std::cerr << "ParticleSampleReadPhasespaceData: No more samples." << std::endl;
std::exit(EXIT_FAILURE);
}
}
public:
virtual Particle* begin() override {
this->checkCacheAvailability();
return base::begin() + pcache[m_currentSampleIndex];
}
virtual Particle* end() override {
this->checkCacheAvailability();
return base::begin() + pcache[m_currentSampleIndex + 1];
}
private:
void readPhasespaceDat() {
this->clearParticleList();
bool eventCountKnown = m_numberOfSamples >= 0;
this->pcache.clear();
this->pcache.emplace_back(0);
if (eventCountKnown)
this->pcache.reserve(this->m_numberOfSamples + 1);
int iline;
{
std::ifstream ifs(this->fname_phasespace_dat.c_str());
if (!ifs) goto error_failed_to_open;
int isample;
std::string line;
iline = 0;
for (isample = 0; ; isample++) {
iline++;
if (!std::getline(ifs, line)) goto error_invalid_format;
int npart;
if (2 != std::sscanf(line.c_str(), " %d %lf", &npart, &m_overSamplingFactor)) break;
if (isample == this->m_numberOfSamples) goto error_invalid_format;
if (npart < 0) goto error_invalid_format;
for (int i = 0; i < npart; i++) {
iline++;
if (!std::getline(ifs, line)) goto error_invalid_format;
int kc, kf;
double px, py, pz, m;
double x, y, z, t;
if (10 != std::sscanf(
line.c_str(), " %d %d %lf %lf %lf %lf %lf %lf %lf %lf",
&kc, &kf, &px, &py, &pz, &m, &x, &y, &z, &t
)) goto error_invalid_format;
this->addParticleCartesian(kf, px, py, pz, m, x, y, z, t);
}
this->pcache.emplace_back(base::plist.size());
}
// 最終行の中身が -999 かどうかチェックする。
// 条件: line に最終行が入っていること。
int lastNumber;
if (1 != std::sscanf(line.c_str(), " %d", &lastNumber) || lastNumber != -999)
goto error_invalid_format;
if (eventCountKnown && isample != m_numberOfSamples)
goto error_invalid_format;
return;
}
error_failed_to_open:
std::cerr
<< "ParticleSamplePhasespace.cpp(ParticleSamplePhasespace::update): failed to open the file ("
<< this->fname_phasespace_dat << ")" << std::endl;
std::exit(EXIT_FAILURE);
return; /*NOTREACHED*/
error_invalid_format:
std::cerr
<< this->fname_phasespace_dat << ":" << iline << ": invalid format (@ParticleSampleReadPhasespaceData::update)"
<< std::endl;
std::exit(EXIT_FAILURE);
return; /*NOTREACHED*/
}
virtual void update() {
if (this->m_currentSampleIndex < 0) {
this->readPhasespaceDat();
this->m_currentSampleIndex = 0;
} else {
this->m_currentSampleIndex++;
}
}
};
class ParticleSampleReadPhasespaceBinary: public ParticleSampleBase {
typedef ParticleSampleBase base;
std::string fname_phasespace_bin;
private:
double m_overSamplingFactor;
int m_numberOfSamples;
int m_currentSampleIndex;
public:
void setNumberOfSamples(int value) {
this->m_numberOfSamples = value;
}
int getNumberOfSamples() const {
return m_numberOfSamples;
}
// この枠組ではファイルに保存されていた粒子集合の oversampling が
// 分からないので、環境に設定されていた値を信じてそのまま返す。
virtual double getOverSamplingFactor() const override {
return m_overSamplingFactor;
}
public:
ParticleSampleReadPhasespaceBinary(runjam_context const& ctx, std::string const& fname_phasespace_bin):
fname_phasespace_bin(fname_phasespace_bin),
m_overSamplingFactor(ctx.get_config("runjam_oversampling_factor", 1.0))
{
this->m_currentSampleIndex = -1;
this->m_numberOfSamples = 1000;
}
private:
std::vector<std::size_t> pcache;
void checkCacheAvailability() {
if (m_currentSampleIndex < 0) {
std::cerr << "ParticleSampleReadPhasespaceBinary: A phasespace file has not been read." << std::endl;
std::exit(EXIT_FAILURE);
} else if ((std::size_t) m_currentSampleIndex >= this->pcache.size() - 1) {
std::cerr << "ParticleSampleReadPhasespaceBinary: No more samples." << std::endl;
std::exit(EXIT_FAILURE);
}
}
public:
virtual Particle* begin() override {
this->checkCacheAvailability();
return base::begin() + pcache[m_currentSampleIndex];
}
virtual Particle* end() override {
this->checkCacheAvailability();
return base::begin() + pcache[m_currentSampleIndex + 1];
}
private:
void readPhasespaceBin() {
this->clearParticleList();
bool eventCountKnown = m_numberOfSamples >= 0;
this->pcache.clear();
this->pcache.emplace_back();
if (eventCountKnown)
this->pcache.reserve(this->m_numberOfSamples + 1);
std::ifstream ifs(this->fname_phasespace_bin.c_str(), std::ios::binary);
int isample = 0;
if (!ifs) goto error_failed_to_open;
{
std::string line;
for (isample = 0; ; isample++) {
std::ifstream::pos_type const pos_start = ifs.tellg();
char magic[4];
if (!ifs.read(magic, sizeof magic)) {
if (ifs.tellg() == pos_start || ifs.tellg() == (std::ifstream::pos_type) -1) break;
goto error_invalid_format;
} else if (std::memcmp(magic, "EvPh", sizeof magic) != 0) {
goto error_invalid_format;
}
std::uint32_t nhadron;
if (!ifs.read((char*) &nhadron, sizeof nhadron))
goto error_invalid_format;
for (std::uint32_t i = 0; i < nhadron; i++) {
std::int32_t ks, kf;
float px, py, pz, m;
float x, y, z, t;
if (!ifs.read((char*) &ks, sizeof ks)) goto error_invalid_format;
if (!ifs.read((char*) &kf, sizeof kf)) goto error_invalid_format;
if (!ifs.read((char*) &px, sizeof px)) goto error_invalid_format;
if (!ifs.read((char*) &py, sizeof py)) goto error_invalid_format;
if (!ifs.read((char*) &pz, sizeof pz)) goto error_invalid_format;
if (!ifs.read((char*) &m, sizeof m )) goto error_invalid_format;
if (!ifs.read((char*) &x, sizeof x)) goto error_invalid_format;
if (!ifs.read((char*) &y, sizeof y)) goto error_invalid_format;
if (!ifs.read((char*) &z, sizeof z)) goto error_invalid_format;
if (!ifs.read((char*) &t, sizeof t)) goto error_invalid_format;
this->addParticleCartesian(kf, px, py, pz, m, x, y, z, t);
}
this->pcache.emplace_back(base::plist.size());
}
if (eventCountKnown && isample != m_numberOfSamples)
goto error_invalid_format;
return;
}
error_failed_to_open:
std::cerr
<< "ParticleSamplePhasespace (ParticleSampleReadPhasespaceBinary): failed to open the file ("
<< this->fname_phasespace_bin << ")" << std::endl;
std::exit(EXIT_FAILURE);
return; /*NOTREACHED*/
error_invalid_format:
std::ostringstream s;
s << this->fname_phasespace_bin
<< ":#" << std::hex << ifs.tellg()
<< ":isample=" << std::dec << isample;
std::cerr << s.str() << ": invalid format (ParticleSampleReadPhasespaceBinary)." << std::endl;
std::exit(EXIT_FAILURE);
return; /*NOTREACHED*/
}
virtual void update() {
if (this->m_currentSampleIndex < 0) {
this->readPhasespaceBin();
this->m_currentSampleIndex = 0;
} else {
this->m_currentSampleIndex++;
}
}
};
class ParticleSampleFactory: ParticleSampleFactoryBase {
virtual std::unique_ptr<ParticleSampleBase> CreateInstance(runjam_context const& ctx, std::string const& type, std::string const& inputfile) {
if (type == "phase1" || type == "phase") {
auto psamp = std::make_unique<ParticleSampleReadPhasespaceData>(ctx, inputfile);
if (type == "phase1") psamp->setNumberOfSamples(1);
return psamp;
} else if (type == "phbin") {
auto psamp = std::make_unique<ParticleSampleReadPhasespaceBinary>(ctx, inputfile);
psamp->setNumberOfSamples(ctx.get_config("runjam_initial_phbin_size", 1000));
return psamp;
}
return nullptr;
}
} instance;
}
}
}