Test for NtupleManager.
We check that the NtupleManager can fill a TTree with three entries of the following types.
40 {
41 const char* ntuple_file = "/tmp/test_ntuplemanager.root";
42
43 TFile f(ntuple_file, "recreate");
45 REQUIRE_NOTHROW(n.create("test"));
46 CHECK_THROWS(n.create("test"));
47
48 REQUIRE_NOTHROW(n.addVar<bool>("test", "bool"));
49 REQUIRE_NOTHROW(n.addVar<short>("test", "short"));
50 REQUIRE_NOTHROW(n.addVar<int>("test", "int"));
51 REQUIRE_NOTHROW(n.addVar<long>("test", "long"));
52 REQUIRE_NOTHROW(n.addVar<float>("test", "float"));
53 REQUIRE_NOTHROW(n.addVar<double>("test", "double"));
54 REQUIRE_NOTHROW(n.addVar<std::vector<bool>>("test", "vector_bool"));
55 REQUIRE_NOTHROW(n.addVar<std::vector<short>>("test", "vector_short"));
56 REQUIRE_NOTHROW(n.addVar<std::vector<int>>("test", "vector_int"));
57 REQUIRE_NOTHROW(n.addVar<std::vector<long>>("test", "vector_long"));
58 REQUIRE_NOTHROW(n.addVar<std::vector<float>>("test", "vector_float"));
59 REQUIRE_NOTHROW(n.addVar<std::vector<double>>("test", "vector_double"));
60
61 CHECK_THROWS(n.addVar<float>("test", "float"));
62
63 std::vector<bool> bools = {true, false, true};
64 std::vector<short> shorts = {2, 3, 4};
65 std::vector<int> ints = {2, 3, 4};
66 std::vector<long> longs = {2, 3, 4};
67 std::vector<float> floats = {0.2, 0.3, 0.4};
68 std::vector<double> doubles = {0.2, 0.3, 0.4};
69
70 std::vector<std::vector<bool>> vector_bools = {
71 {true, true, true}, {false, false, false}, {true, false, false}};
72 std::vector<std::vector<short>> vector_shorts = {
73 {1, 2, 3}, {5, 7, 9}, {3, 4, 5}};
74 std::vector<std::vector<int>> vector_ints = {{1, 2, 3}, {5, 7, 9}, {3, 4, 5}};
75 std::vector<std::vector<long>> vector_longs = {
76 {1, 2, 3}, {5, 7, 9}, {3, 4, 5}};
77 std::vector<std::vector<float>> vector_floats = {
78 {0.1, 0.01, 0.001}, {2e4, 3e5, 4e6}, {0.9, 0.99, 0.999}};
79 std::vector<std::vector<double>> vector_doubles = {
80 {0.1, 0.01, 0.001}, {2e4, 3e5, 4e6}, {0.9, 0.99, 0.999}};
81
82 for (size_t i = 0; i < 3; i++) {
83
84
85
86
87 REQUIRE_NOTHROW(n.setVar("bool", bool(bools.at(i))));
88 REQUIRE_NOTHROW(n.setVar("short", shorts.at(i)));
89 REQUIRE_NOTHROW(n.setVar("int", ints.at(i)));
90 REQUIRE_NOTHROW(n.setVar("long", longs.at(i)));
91 REQUIRE_NOTHROW(n.setVar("float", floats.at(i)));
92 REQUIRE_NOTHROW(n.setVar("double", doubles.at(i)));
93 REQUIRE_NOTHROW(n.setVar("vector_bool", vector_bools.at(i)));
94 REQUIRE_NOTHROW(n.setVar("vector_short", vector_shorts.at(i)));
95 REQUIRE_NOTHROW(n.setVar("vector_int", vector_ints.at(i)));
96 REQUIRE_NOTHROW(n.setVar("vector_long", vector_longs.at(i)));
97 REQUIRE_NOTHROW(n.setVar("vector_float", vector_floats.at(i)));
98 REQUIRE_NOTHROW(n.setVar("vector_double", vector_doubles.at(i)));
99 CHECK_THROWS(n.setVar("bool", shorts.at(i)));
100 CHECK_THROWS(n.setVar("bool", vector_bools.at(i)));
101 n.fill();
102 n.clear();
103 }
104
105 f.Write();
106 f.Close();
107
108 TTreeReader r("test", TFile::Open(ntuple_file));
109 r.Restart();
110 TTreeReaderValue<bool> root_bool(r, "bool");
111 TTreeReaderValue<short> root_short(r, "short");
112 TTreeReaderValue<int> root_int(r, "int");
113
114
115
116
117 TTreeReaderValue<long long> root_long(r, "long");
118 TTreeReaderValue<float> root_float(r, "float");
119 TTreeReaderValue<double> root_double(r, "double");
120 TTreeReaderArray<bool> root_vector_bool(r, "vector_bool");
121 TTreeReaderArray<short> root_vector_short(r, "vector_short");
122 TTreeReaderArray<int> root_vector_int(r, "vector_int");
123 TTreeReaderArray<long> root_vector_long(r, "vector_long");
124 TTreeReaderArray<float> root_vector_float(r, "vector_float");
125 TTreeReaderArray<double> root_vector_double(r, "vector_double");
126
127 for (size_t i = 0; i < 3; i++) {
128 REQUIRE(r.Next());
129 CHECK(*root_bool == bools.at(i));
130 CHECK(*root_short == shorts.at(i));
131 CHECK(*root_int == ints.at(i));
132 CHECK(*root_long == longs.at(i));
133 CHECK(*root_float == floats.at(i));
134 CHECK(*root_double == doubles.at(i));
135
136
137
138
140 Catch::Matchers::UnorderedEquals(vector_bools.at(i)));
142 Catch::Matchers::UnorderedEquals(vector_shorts.at(i)));
144 Catch::Matchers::UnorderedEquals(vector_ints.at(i)));
146 Catch::Matchers::UnorderedEquals(vector_longs.at(i)));
148 Catch::Matchers::UnorderedEquals(vector_floats.at(i)));
150 Catch::Matchers::UnorderedEquals(vector_doubles.at(i)));
151 }
152
153}
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()