5 #include "../src/output.h"
10 #include <boost/filesystem.hpp>
11 #include <boost/filesystem/fstream.hpp>
13 #include "../src/event.h"
14 #include "../src/hdf5_utils.h"
15 #include "../src/nucleus.h"
17 using namespace trento;
21 {
"normalization", 1.},
22 {
"reduced-thickness", 0.},
26 {
"cross-section", 6.4},
27 {
"nucleon-width", 0.5}
37 auto b = 4.*std::sqrt(random::canonical<>());
38 nucleusA->sample_nucleons(+.5*
b);
39 nucleusB->sample_nucleons(-.5*
b);
41 for (
auto&&
A : *nucleusA)
42 for (
auto&& B : *nucleusB)
45 event.compute(*nucleusA, *nucleusB,
profile);
47 SECTION(
"no output" ) {
54 SECTION(
"stdout only" ) {
56 auto first_line = [&
b, &
event](
int nev) {
61 std::getline(capture.
stream, line);
66 CHECK( first_line(1).substr(0, 1) ==
"0" );
67 CHECK( first_line(10).substr(0, 1) ==
"0" );
68 CHECK( first_line(11).substr(0, 2) ==
" 0" );
69 CHECK( first_line(100).substr(0, 2) ==
" 0" );
70 CHECK( first_line(101).substr(0, 3) ==
" 0" );
74 CHECK( first_line(1).substr(1) == first_line(1000).substr(3) );
78 double impact, mult, e2, e3, e4, e5;
84 capture.
stream >> num >> impact >> npart >> mult >> e2 >> e3 >> e4 >> e5 >> std::ws;
85 end = capture.
stream.get();
90 CHECK( impact == Approx(
b) );
93 CHECK( e2 == Approx(
event.eccentricity().at(2)) );
94 CHECK( e3 == Approx(
event.eccentricity().at(3)) );
95 CHECK( e4 == Approx(
event.eccentricity().at(4)) );
96 CHECK( e5 == Approx(
event.eccentricity().at(5)) );
99 CHECK( end == std::char_traits<char>::eof() );
102 SECTION(
"text and stdout" ) {
107 {
"no-header",
false},
108 {
"number-events", 50},
109 {
"output", temp.path}
123 while (std::getline(capture.
stream, line)) { ++
n; }
133 fs::ifstream ifs{temp.path/
"03.dat"};
137 std::getline(ifs, line);
138 CHECK( line ==
"# event 3" );
140 std::getline(ifs, line);
141 CHECK( line.substr(0, 10) ==
"# b = " );
142 CHECK( std::stod(line.substr(10)) == Approx(
b) );
144 std::getline(ifs, line);
145 CHECK( line.substr(0, 10) ==
"# npart = " );
146 CHECK( std::stoi(line.substr(10)) ==
event.npart() );
148 std::getline(ifs, line);
149 CHECK( line.substr(0, 10) ==
"# mult = " );
150 CHECK( std::stod(line.substr(10)) == Approx(
event.multiplicity()) );
152 for (
const auto& ecc :
event.eccentricity()) {
153 std::getline(ifs, line);
155 CHECK( std::stod(line.substr(10)) == Approx(ecc.second) );
159 const auto* iter =
event.reduced_thickness_grid().origin();
161 bool all_correct =
false;
163 all_correct = (check == Approx(*(iter++))) || all_correct;
164 CHECK( all_correct );
167 const auto* grid_end =
event.reduced_thickness_grid().origin() +
168 event.reduced_thickness_grid().num_elements();
169 CHECK( iter == grid_end );
174 fs::ifstream ifs{temp.path/
"27.dat"};
176 std::getline(ifs, line);
177 CHECK( line ==
"# event 27" );
181 CHECK_THROWS_AS(
Output{output_var_map}, std::runtime_error );
185 SECTION(
"hdf5 only" ) {
193 {
"number-events", nev},
194 {
"output", temp.path}
198 for (
auto n = 0;
n < nev; ++
n)
202 H5::H5File
file{temp.path.string(), H5F_ACC_RDONLY};
203 CHECK( static_cast<int>(
file.getNumObjs()) == nev );
205 auto name =
file.getObjnameByIdx(0);
211 Event::Grid grid_check{
event.reduced_thickness_grid()};
212 dataset.read(grid_check.data(), H5::PredType::NATIVE_DOUBLE);
215 auto grid_correct = std::equal(
217 grid_check.origin() + grid_check.num_elements(),
218 event.reduced_thickness_grid().origin(),
219 [](
const double& value_check,
const double&
value) {
220 return value_check == Approx(
value);
223 CHECK( grid_correct );
229 dataset.openAttribute(
"b").read(H5::PredType::NATIVE_DOUBLE, &double_check);
230 CHECK( double_check == Approx(
b) );
232 dataset.openAttribute(
"npart").read(H5::PredType::NATIVE_INT, &int_check);
235 dataset.openAttribute(
"mult").read(H5::PredType::NATIVE_DOUBLE, &double_check);
236 CHECK( double_check == Approx(
event.multiplicity()) );
238 for (
const auto& ecc :
event.eccentricity()) {
240 .read(H5::PredType::NATIVE_DOUBLE, &double_check);
241 CHECK( double_check == Approx(ecc.second) );
244 #if H5_VERSION_GE(1, 8, 14) // causes memory leak on earlier versions
250 CHECK_THROWS_AS(
Output{output_var_map}, std::runtime_error );
255 fs::ofstream{temp2.
path};
262 {
"number-events", 1},
263 {
"output", temp2.path}
267 CHECK( fs::file_size(temp2.path) > 0);
269 #endif // TRENTO_HDF5