LDMX Software
Functions
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"

Go to the source code of this file.

Functions

 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

◆ 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 24 of file NtupleManagerTest.cxx.

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

References framework::NtupleManager::getInstance().