Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-24 08:20:56

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/unit_test.hpp>
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Utilities/BinUtility.hpp"
0013 #include "Acts/Utilities/BinningType.hpp"
0014 #include "ActsPlugins/Json/UtilitiesJsonConverter.hpp"
0015 #include "ActsTests/CommonHelpers/FloatComparisons.hpp"
0016 
0017 #include <cmath>
0018 #include <fstream>
0019 #include <initializer_list>
0020 #include <numbers>
0021 #include <string>
0022 #include <utility>
0023 #include <vector>
0024 
0025 #include <nlohmann/json.hpp>
0026 
0027 #include "EqualityHelpers.hpp"
0028 
0029 using namespace Acts;
0030 
0031 namespace ActsTests {
0032 
0033 BOOST_AUTO_TEST_SUITE(JsonSuite)
0034 
0035 BOOST_AUTO_TEST_CASE(BinUtilityRoundTripTests) {
0036   BinUtility reference(2, 0., 4., open, AxisDirection::AxisR);
0037 
0038   std::ofstream out;
0039 
0040   // Test in one dimension
0041   nlohmann::json joneDimOut;
0042   to_json(joneDimOut, reference);
0043   out.open("BinUtility_1D.json");
0044   out << joneDimOut.dump(2);
0045   out.close();
0046 
0047   auto in = std::ifstream("BinUtility_1D.json",
0048                           std::ifstream::in | std::ifstream::binary);
0049   BOOST_CHECK(in.good());
0050   nlohmann::json joneDimIn;
0051   in >> joneDimIn;
0052   in.close();
0053 
0054   BinUtility test;
0055   from_json(joneDimIn, test);
0056 
0057   BOOST_CHECK(isEqual(reference, test, 0.0001));
0058 
0059   // Increase to two dimensions
0060   reference += BinUtility(10., -std::numbers::pi, std::numbers::pi, closed,
0061                           AxisDirection::AxisPhi);
0062   nlohmann::json jtwoDimOut;
0063   to_json(jtwoDimOut, reference);
0064   out.open("BinUtility_2D.json");
0065   out << jtwoDimOut.dump(2);
0066   out.close();
0067 
0068   in = std::ifstream("BinUtility_2D.json",
0069                      std::ifstream::in | std::ifstream::binary);
0070   BOOST_CHECK(in.good());
0071   nlohmann::json jtwoDimIn;
0072   in >> jtwoDimIn;
0073   in.close();
0074 
0075   test = BinUtility();
0076   from_json(jtwoDimIn, test);
0077 
0078   BOOST_CHECK(isEqual(reference, test, 0.0001));
0079 
0080   // Increase to three dimensions
0081   std::vector<float> boundaries = {-4., -1.5, 0., 10.};
0082   reference += BinUtility(boundaries, open, AxisDirection::AxisZ);
0083   nlohmann::json jthreeDimOut;
0084   to_json(jthreeDimOut, reference);
0085   out.open("BinUtility_3D.json");
0086   out << jthreeDimOut.dump(2);
0087   out.close();
0088 
0089   in = std::ifstream("BinUtility_3D.json",
0090                      std::ifstream::in | std::ifstream::binary);
0091   BOOST_CHECK(in.good());
0092   nlohmann::json jthreeDimIn;
0093   in >> jthreeDimIn;
0094   in.close();
0095 
0096   test = BinUtility();
0097   from_json(jthreeDimIn, test);
0098 
0099   BOOST_CHECK(isEqual(reference, test, 0.0001));
0100 
0101   // One with transform
0102   Transform3 t;
0103   t = Eigen::AngleAxis(0.12334, Vector3(1., 2., 3).normalized());
0104   t.pretranslate(Vector3(1., 2., 3.));
0105 
0106   auto bData = reference.binningData()[0];
0107 
0108   reference = BinUtility(bData, t);
0109 
0110   nlohmann::json jtransformOut;
0111   to_json(jtransformOut, reference);
0112   out.open("BinUtility_Transform.json");
0113   out << jtransformOut.dump(2);
0114   out.close();
0115 
0116   in = std::ifstream("BinUtility_Transform.json",
0117                      std::ifstream::in | std::ifstream::binary);
0118   BOOST_CHECK(in.good());
0119   nlohmann::json jtransformIn;
0120   in >> jtransformIn;
0121   in.close();
0122 
0123   test = BinUtility();
0124   from_json(jtransformIn, test);
0125 
0126   BOOST_CHECK(isEqual(reference, test, 0.0001));
0127 }
0128 
0129 BOOST_AUTO_TEST_CASE(Range1DRoundTrip) {
0130   Range1D<double> r(-10., 100.);
0131 
0132   nlohmann::json jrange;
0133   jrange["range"] = r;
0134 
0135   Range1D<double> rIn = jrange["range"];
0136 
0137   CHECK_CLOSE_ABS(rIn.min(), -10., 10e-5);
0138   CHECK_CLOSE_ABS(rIn.max(), 100., 10e-5);
0139 }
0140 
0141 BOOST_AUTO_TEST_SUITE_END()
0142 
0143 }  // namespace ActsTests