13 #include <boost/filesystem.hpp>
14 #include <boost/filesystem/fstream.hpp>
15 #include <boost/program_options/variables_map.hpp>
26 void write_stream(std::ostream&
os,
int width,
27 int num,
double impact_param,
const Event&
event) {
29 using std::setprecision;
31 using std::scientific;
34 os << setprecision(10)
36 << setw(15) << fixed << impact_param
37 << setw(5) <<
event.npart();
38 if (event.with_ncoll()) os << setw(8) <<
event.ncoll();
39 os << setw(18) << scientific <<
event.multiplicity()
42 for (
const auto& ecc : event.eccentricity())
43 os << setw(14) << ecc.second;
52 int num,
double impact_param,
const Event& event,
bool header) {
55 std::ostringstream padded_fname{};
56 padded_fname << std::setw(width) << std::setfill(
'0') << num <<
".dat";
57 fs::ofstream ofs{output_dir / padded_fname.str()};
61 ofs << std::setprecision(10)
62 <<
"# event " << num <<
'\n'
63 <<
"# b = " << impact_param <<
'\n'
64 <<
"# npart = " <<
event.npart() <<
'\n'
65 <<
"# mult = " <<
event.multiplicity() <<
'\n';
67 for (
const auto& ecc : event.eccentricity())
68 ofs <<
"# e" << ecc.first <<
" = " << ecc.second <<
'\n';
70 for (
const auto& psi : event.participant_plane())
71 ofs <<
"# psi" << psi.first <<
" = " << psi.second <<
'\n';
79 if (event.density_grid().shape()[2] == 1) is3d =
false;
83 for (
const auto& slice : event.density_grid()) {
84 for (
const auto& row : slice) {
85 for (
const auto& item : row) {
88 if (is3d) ofs << std::endl;
90 if (!is3d) ofs << std::endl;
103 void operator()(
int num,
double impact_param,
const Event& event)
const;
118 template <
typename T>
119 void hdf5_add_scalar_attr(
121 const auto& datatype = hdf5::type<T>();
122 auto attr = group.createAttribute(name, datatype, H5::DataSpace{});
123 attr.write(datatype, &value);
127 : file_(filename.
string(), H5F_ACC_TRUNC)
130 void HDF5Writer::operator()(
131 int num,
double impact_param,
const Event& event)
const {
132 const auto& grid1 =
event.density_grid();
133 const auto& grid2 =
event.TAB_grid();
137 const std::string sd_name{gp_name +
"/matter_density"};
138 const std::string tab_name{gp_name +
"/Ncoll_density"};
141 auto group = H5::Group(file_.createGroup(gp_name));
143 hdf5_add_scalar_attr(group,
"b", impact_param);
144 hdf5_add_scalar_attr(group,
"npart", event.npart());
145 hdf5_add_scalar_attr(group,
"ncoll", event.ncoll());
146 hdf5_add_scalar_attr(group,
"mult", event.multiplicity());
147 hdf5_add_scalar_attr(group,
"dxy", event.dxy());
148 hdf5_add_scalar_attr(group,
"deta", event.deta());
149 hdf5_add_scalar_attr(group,
"Ny", grid1.shape()[0]);
150 hdf5_add_scalar_attr(group,
"Nx", grid1.shape()[1]);
151 hdf5_add_scalar_attr(group,
"Nz", grid1.shape()[2]);
152 for (
const auto& ecc : event.eccentricity())
153 hdf5_add_scalar_attr(group,
"e" +
std::to_string(ecc.first), ecc.second);
154 for (
const auto& psi : event.participant_plane())
155 hdf5_add_scalar_attr(group,
"psi" +
std::to_string(psi.first), psi.second);
160 const auto& datatype1 = hdf5::type<Event::Grid3D::element>();
161 std::array<hsize_t, Event::Grid3D::dimensionality> shape1;
162 std::copy(grid1.shape(), grid1.shape() + shape1.size(), shape1.begin());
163 auto dataspace1 = hdf5::make_dataspace(shape1);
166 H5::DSetCreatPropList proplist1{};
171 proplist1.setChunk(shape1.size(), shape1.data());
173 proplist1.setDeflate(4);
176 auto dataset1 = file_.createDataSet(sd_name, datatype1, dataspace1, proplist1);
177 dataset1.write(grid1.data(), datatype1);
181 const auto& datatype2 = hdf5::type<Event::Grid::element>();
182 std::array<hsize_t, Event::Grid::dimensionality> shape2;
183 std::copy(grid2.shape(), grid2.shape() + shape2.size(), shape2.begin());
184 auto dataspace2 = hdf5::make_dataspace(shape2);
187 H5::DSetCreatPropList proplist2{};
192 proplist1.setChunk(shape2.size(), shape2.data());
194 proplist1.setDeflate(4);
197 auto dataset2 = file_.createDataSet(tab_name, datatype2, dataspace2, proplist2);
198 dataset2.write(grid2.data(), datatype2);
201 #endif // TRENTO_HDF5
209 auto nevents = var_map[
"number-events"].as<
int>();
210 auto width =
static_cast<int>(std::ceil(std::log10(
nevents)));
213 if (!var_map[
"quiet"].as<bool>()) {
215 [width](
int num,
double impact_param,
const Event& event) {
216 write_stream(std::cout, width, num, impact_param, event);
222 if (var_map.count(
"output")) {
223 const auto& output_path = var_map[
"output"].as<
fs::path>();
226 if (
fs::exists(output_path) && !fs::is_empty(output_path))
227 throw std::runtime_error{
"file '" + output_path.string() +
228 "' exists, will not overwrite"};
229 writers_.emplace_back(HDF5Writer{output_path});
231 throw std::runtime_error{
"HDF5 output was not compiled"};
232 #endif // TRENTO_HDF5
239 if (!fs::is_empty(output_path)) {
240 throw std::runtime_error{
"output directory '" + output_path.string() +
244 fs::create_directories(output_path);
246 auto header = !var_map[
"no-header"].as<
bool>();
248 [output_path, width, header](
249 int num,
double impact_param,
const Event& event) {
250 write_text_file(output_path, width, num, impact_param, event, header);