LDMX Software
NtupleManagerTest.cxx File Reference

Test the operation of Framework processing. More...

#include <catch2/catch_approx.hpp>
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_vector.hpp>
#include "Framework/NtupleManager.h"
#include "TFile.h"
#include "TTreeReader.h"
#include "TTreeReaderArray.h"
#include "TTreeReaderValue.h"

Go to the source code of this file.

Functions

template<typename T >
std::vector< T > fromTTreeReaderArray (const TTreeReaderArray< T > &reader_val)
 TTreeReaderValue<std::vector<T>> fails when attempting to register in this test so we use TTreeReaderArray<T> instead.
 
 TEST_CASE ("Ntuple Manager Functions", "[Framework][functionality]")
 Test for NtupleManager.
 

Detailed Description

Test the operation of Framework processing.

Author
Tom Eichlersmith, University of Minnesota

Definition in file NtupleManagerTest.cxx.

Function Documentation

◆ fromTTreeReaderArray()

template<typename T >
std::vector< T > fromTTreeReaderArray ( const TTreeReaderArray< T > & reader_val)

TTreeReaderValue<std::vector<T>> fails when attempting to register in this test so we use TTreeReaderArray<T> instead.

This then functions properly but we then have to convert it back to a std::vector so that Catch2 can compare it to our correct answer.

Definition at line 25 of file NtupleManagerTest.cxx.

25 {
26 std::vector<T> v;
27 for (const auto& val : reader_val) {
28 v.push_back(val);
29 }
30 return v;
31}

Referenced by TEST_CASE().

◆ TEST_CASE()

TEST_CASE ( "Ntuple Manager Functions" ,
"" [Framework][functionality] )

Test for NtupleManager.

We check that the NtupleManager can fill a TTree with three entries of the following types.

  • bool, short, int, long, float, double
  • vectors of the above types

Definition at line 41 of file NtupleManagerTest.cxx.

41 {
42 const char* ntuple_file = "/tmp/test_ntuplemanager.root";
43
44 TFile f(ntuple_file, "recreate");
46 REQUIRE_NOTHROW(n.create("test"));
47 CHECK_THROWS(n.create("test"));
48
49 REQUIRE_NOTHROW(n.addVar<bool>("test", "bool"));
50 REQUIRE_NOTHROW(n.addVar<short>("test", "short"));
51 REQUIRE_NOTHROW(n.addVar<int>("test", "int"));
52 REQUIRE_NOTHROW(n.addVar<long>("test", "long"));
53 REQUIRE_NOTHROW(n.addVar<float>("test", "float"));
54 REQUIRE_NOTHROW(n.addVar<double>("test", "double"));
55 REQUIRE_NOTHROW(n.addVar<std::vector<bool>>("test", "vector_bool"));
56 REQUIRE_NOTHROW(n.addVar<std::vector<short>>("test", "vector_short"));
57 REQUIRE_NOTHROW(n.addVar<std::vector<int>>("test", "vector_int"));
58 REQUIRE_NOTHROW(n.addVar<std::vector<long>>("test", "vector_long"));
59 REQUIRE_NOTHROW(n.addVar<std::vector<float>>("test", "vector_float"));
60 REQUIRE_NOTHROW(n.addVar<std::vector<double>>("test", "vector_double"));
61
62 CHECK_THROWS(n.addVar<float>("test", "float"));
63
64 std::vector<bool> bools = {true, false, true};
65 std::vector<short> shorts = {2, 3, 4};
66 std::vector<int> ints = {2, 3, 4};
67 std::vector<long> longs = {2, 3, 4};
68 std::vector<float> floats = {0.2, 0.3, 0.4};
69 std::vector<double> doubles = {0.2, 0.3, 0.4};
70
71 std::vector<std::vector<bool>> vector_bools = {
72 {true, true, true}, {false, false, false}, {true, false, false}};
73 std::vector<std::vector<short>> vector_shorts = {
74 {1, 2, 3}, {5, 7, 9}, {3, 4, 5}};
75 std::vector<std::vector<int>> vector_ints = {{1, 2, 3}, {5, 7, 9}, {3, 4, 5}};
76 std::vector<std::vector<long>> vector_longs = {
77 {1, 2, 3}, {5, 7, 9}, {3, 4, 5}};
78 std::vector<std::vector<float>> vector_floats = {
79 {0.1, 0.01, 0.001}, {2e4, 3e5, 4e6}, {0.9, 0.99, 0.999}};
80 std::vector<std::vector<double>> vector_doubles = {
81 {0.1, 0.01, 0.001}, {2e4, 3e5, 4e6}, {0.9, 0.99, 0.999}};
82
83 for (size_t i = 0; i < 3; i++) {
84 // to save space, a std::vector<bool>
85 // compresses each bool into a one-bit
86 // type that can be converted back to the
87 // 8-bit bool type.
88 REQUIRE_NOTHROW(n.setVar("bool", bool(bools.at(i))));
89 REQUIRE_NOTHROW(n.setVar("short", shorts.at(i)));
90 REQUIRE_NOTHROW(n.setVar("int", ints.at(i)));
91 REQUIRE_NOTHROW(n.setVar("long", longs.at(i)));
92 REQUIRE_NOTHROW(n.setVar("float", floats.at(i)));
93 REQUIRE_NOTHROW(n.setVar("double", doubles.at(i)));
94 REQUIRE_NOTHROW(n.setVar("vector_bool", vector_bools.at(i)));
95 REQUIRE_NOTHROW(n.setVar("vector_short", vector_shorts.at(i)));
96 REQUIRE_NOTHROW(n.setVar("vector_int", vector_ints.at(i)));
97 REQUIRE_NOTHROW(n.setVar("vector_long", vector_longs.at(i)));
98 REQUIRE_NOTHROW(n.setVar("vector_float", vector_floats.at(i)));
99 REQUIRE_NOTHROW(n.setVar("vector_double", vector_doubles.at(i)));
100 CHECK_THROWS(n.setVar("bool", shorts.at(i)));
101 CHECK_THROWS(n.setVar("bool", vector_bools.at(i)));
102 n.fill();
103 n.clear();
104 }
105
106 f.Write();
107 f.Close();
108
109 TTreeReader r("test", TFile::Open(ntuple_file));
110 r.Restart();
111 TTreeReaderValue<bool> root_bool(r, "bool");
112 TTreeReaderValue<short> root_short(r, "short");
113 TTreeReaderValue<int> root_int(r, "int");
114 // root serializes 'long' (which on some systems is 64-bit)
115 // depending on the number of bits, so we need to read
116 // it in with 'long long' (which is at least 64-bit)
117 // to make sure we don't fail
118 TTreeReaderValue<long long> root_long(r, "long");
119 TTreeReaderValue<float> root_float(r, "float");
120 TTreeReaderValue<double> root_double(r, "double");
121 TTreeReaderArray<bool> root_vector_bool(r, "vector_bool");
122 TTreeReaderArray<short> root_vector_short(r, "vector_short");
123 TTreeReaderArray<int> root_vector_int(r, "vector_int");
124 TTreeReaderArray<long> root_vector_long(r, "vector_long");
125 TTreeReaderArray<float> root_vector_float(r, "vector_float");
126 TTreeReaderArray<double> root_vector_double(r, "vector_double");
127
128 for (size_t i = 0; i < 3; i++) {
129 REQUIRE(r.Next());
130 CHECK(*root_bool == bools.at(i));
131 CHECK(*root_short == shorts.at(i));
132 CHECK(*root_int == ints.at(i));
133 CHECK(*root_long == longs.at(i));
134 CHECK(*root_float == floats.at(i));
135 CHECK(*root_double == doubles.at(i));
136 // root takes the liberty to permut vectors when serializing
137 // in order to try to save space
138 // This means we need to use unordered equals.
139 // Not too much loss in efficiency becuase we keep our vectors short.
140 CHECK_THAT(fromTTreeReaderArray(root_vector_bool),
141 Catch::Matchers::UnorderedEquals(vector_bools.at(i)));
142 CHECK_THAT(fromTTreeReaderArray(root_vector_short),
143 Catch::Matchers::UnorderedEquals(vector_shorts.at(i)));
144 CHECK_THAT(fromTTreeReaderArray(root_vector_int),
145 Catch::Matchers::UnorderedEquals(vector_ints.at(i)));
146 CHECK_THAT(fromTTreeReaderArray(root_vector_long),
147 Catch::Matchers::UnorderedEquals(vector_longs.at(i)));
148 CHECK_THAT(fromTTreeReaderArray(root_vector_float),
149 Catch::Matchers::UnorderedEquals(vector_floats.at(i)));
150 CHECK_THAT(fromTTreeReaderArray(root_vector_double),
151 Catch::Matchers::UnorderedEquals(vector_doubles.at(i)));
152 }
153
154} // process test
std::vector< T > fromTTreeReaderArray(const TTreeReaderArray< T > &reader_val)
TTreeReaderValue<std::vector<T>> fails when attempting to register in this test so we use TTreeReader...
Singleton class used to manage the creation and pooling of ntuples.
static NtupleManager & getInstance()

References fromTTreeReaderArray(), and framework::NtupleManager::getInstance().