Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:11:44

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