Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[DF] Add tests for Snapshotting of vectors/arrays of bool
  • Loading branch information
eguiraud committed Oct 6, 2018
commit 2c30bc252f4f33f320a0b492ea5034b73c03bdfd
73 changes: 55 additions & 18 deletions tree/dataframe/test/dataframe_snapshot.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,34 @@ class RDFSnapshotArrays : public ::testing::Test {
for (const auto &fname : kFileNames) {
TFile f(fname.c_str(), "RECREATE");
TTree t("arrayTree", "arrayTree");

// doubles, floats
const unsigned int fixedSize = 4u;
float fixedSizeArr[fixedSize];
t.Branch("fixedSizeArr", fixedSizeArr, ("fixedSizeArr[" + std::to_string(fixedSize) + "]/F").c_str());
unsigned int size = 0u;
t.Branch("size", &size);
double varSizeArr[eventsPerFile * 100u];
t.Branch("varSizeArr", varSizeArr, "varSizeArr[size]/D");

// bools. std::vector<bool> makes bool treatment in RDF special
bool fixedSizeBoolArr[fixedSize];
t.Branch("fixedSizeBoolArr", fixedSizeBoolArr,
("fixedSizeBoolArr[" + std::to_string(fixedSize) + "]/O").c_str());
bool varSizeBoolArr[eventsPerFile * 100u];
t.Branch("varSizeBoolArr", varSizeBoolArr, "varSizeBoolArr[size]/O");

// for each event, fill array elements
for (auto i : ROOT::TSeqU(eventsPerFile)) {
for (auto j : ROOT::TSeqU(4))
for (auto j : ROOT::TSeqU(4)) {
fixedSizeArr[j] = curEvent * j;
fixedSizeBoolArr[j] = j % 2 == 0;
}
size = (i + 1) * 100u;
for (auto j : ROOT::TSeqU(size))
for (auto j : ROOT::TSeqU(size)) {
varSizeArr[j] = curEvent * j;
varSizeBoolArr[j] = j % 2 == 0;
}
t.Fill();
++curEvent;
}
Expand Down Expand Up @@ -235,25 +249,33 @@ void checkSnapshotArrayFile(RResultPtr<RInterface<RLoopManager>> &df, unsigned i
// fixedSizeArr and varSizeArr are RResultPtr<vector<vector<T>>>
auto fixedSizeArr = df->Take<RVec<float>>("fixedSizeArr");
auto varSizeArr = df->Take<RVec<double>>("varSizeArr");
auto fixedSizeBoolArr = df->Take<RVec<bool>>("fixedSizeBoolArr");
auto varSizeBoolArr = df->Take<RVec<bool>>("varSizeBoolArr");
auto size = df->Take<unsigned int>("size");

// check contents of fixedSizeArr
// check contents of fixed sized arrays
const auto nEvents = fixedSizeArr->size();
const auto fixedSizeSize = fixedSizeArr->front().size();
EXPECT_EQ(nEvents, kNEvents);
EXPECT_EQ(fixedSizeSize, 4u);
for (auto i = 0u; i < nEvents; ++i) {
for (auto j = 0u; j < fixedSizeSize; ++j)
for (auto j = 0u; j < fixedSizeSize; ++j) {
EXPECT_DOUBLE_EQ(fixedSizeArr->at(i).at(j), i * j);
EXPECT_EQ(fixedSizeBoolArr->at(i).at(j), j % 2 == 0);
}
}

// check contents of varSizeArr
// check contents of variable sized arrays
for (auto i = 0u; i < nEvents; ++i) {
const auto &v = varSizeArr->at(i);
const auto thisSize = size->at(i);
EXPECT_EQ(thisSize, v.size());
for (auto j = 0u; j < thisSize; ++j)
EXPECT_DOUBLE_EQ(v[j], i * j);
const auto &dv = varSizeArr->at(i);
const auto &bv = varSizeBoolArr->at(i);
EXPECT_EQ(thisSize, dv.size());
EXPECT_EQ(thisSize, bv.size());
for (auto j = 0u; j < thisSize; ++j) {
EXPECT_DOUBLE_EQ(dv[j], i * j);
EXPECT_EQ(bv[j], j % 2 == 0);
}
}
}

Expand All @@ -262,8 +284,9 @@ TEST_F(RDFSnapshotArrays, SingleThread)
RDataFrame tdf("arrayTree", kFileNames);
// template Snapshot
// "size" _must_ be listed before "varSizeArr"!
auto dt = tdf.Snapshot<RVec<float>, unsigned int, RVec<double>>(
"outTree", "test_snapshotRVecoutST.root", {"fixedSizeArr", "size", "varSizeArr"});
auto dt = tdf.Snapshot<RVec<float>, unsigned int, RVec<double>, RVec<bool>, RVec<bool>>(
"outTree", "test_snapshotRVecoutST.root",
{"fixedSizeArr", "size", "varSizeArr", "varSizeBoolArr", "fixedSizeBoolArr"});

checkSnapshotArrayFile(dt, kNEvents);
}
Expand All @@ -273,7 +296,8 @@ TEST_F(RDFSnapshotArrays, SingleThreadJitted)
RDataFrame tdf("arrayTree", kFileNames);
// jitted Snapshot
// "size" _must_ be listed before "varSizeArr"!
auto dj = tdf.Snapshot("outTree", "test_snapshotRVecoutSTJitted.root", {"fixedSizeArr", "size", "varSizeArr"});
auto dj = tdf.Snapshot("outTree", "test_snapshotRVecoutSTJitted.root",
{"fixedSizeArr", "size", "varSizeArr", "varSizeBoolArr", "fixedSizeBoolArr"});

checkSnapshotArrayFile(dj, kNEvents);
}
Expand Down Expand Up @@ -361,10 +385,13 @@ TEST(RDFSnapshotMore, ReadWriteStdVec)
TFile f(fname, "RECREATE");
TTree t(treename, treename);
std::vector<int> v({42});
std::vector<bool> vb({true, false, true}); // std::vector<bool> is special, not in a good way
t.Branch("v", &v);
t.Branch("vb", &vb);
t.Fill();
// as an extra test, make sure that the vector reallocates between first and second entry
v = std::vector<int>(100000, 84);
vb = std::vector<bool>(100000, true);
t.Fill();
t.Write();
f.Close();
Expand All @@ -374,27 +401,35 @@ TEST(RDFSnapshotMore, ReadWriteStdVec)
TFile f2(filename);
TTreeReader r(treename, &f2);
TTreeReaderArray<int> rv(r, "v");
TTreeReaderArray<bool> rvb(r, "vb");
r.Next();
EXPECT_EQ(rv.GetSize(), 1u);
EXPECT_EQ(rv[0], 42);
EXPECT_EQ(rvb.GetSize(), 3u);
EXPECT_TRUE(rvb[0]);
EXPECT_FALSE(rvb[1]);
EXPECT_TRUE(rvb[2]);
r.Next();
EXPECT_EQ(rv.GetSize(), 100000u);
for (auto &e : rv)
EXPECT_EQ(rvb.GetSize(), 100000u);
for (auto e : rv)
EXPECT_EQ(e, 84);
for (auto e : rvb)
EXPECT_TRUE(e);
};

// read and write using RDataFrame

const auto outfname1 = "out_readwritestdvec1.root";
RDataFrame(treename, fname).Snapshot<std::vector<int>>(treename, outfname1, {"v"});
RDataFrame(treename, fname).Snapshot<std::vector<int>, std::vector<bool>>(treename, outfname1, {"v", "vb"});
outputChecker(outfname1);

const auto outfname2 = "out_readwritestdvec2.root";
RDataFrame(treename, fname).Snapshot(treename, outfname2);
outputChecker(outfname2);

const auto outfname3 = "out_readwritestdvec3.root";
RDataFrame(treename, fname).Snapshot<RVec<int>>(treename, outfname3, {"v"});
RDataFrame(treename, fname).Snapshot<RVec<int>, RVec<bool>>(treename, outfname3, {"v", "vb"});
outputChecker(outfname3);

gSystem->Unlink(fname);
Expand Down Expand Up @@ -546,8 +581,9 @@ TEST_F(RDFSnapshotArrays, MultiThread)
ROOT::EnableImplicitMT(4);

RDataFrame tdf("arrayTree", kFileNames);
auto dt = tdf.Snapshot<RVec<float>, unsigned int, RVec<double>>(
"outTree", "test_snapshotRVecoutMT.root", {"fixedSizeArr", "size", "varSizeArr"});
auto dt = tdf.Snapshot<RVec<float>, unsigned int, RVec<double>, RVec<bool>, RVec<bool>>(
"outTree", "test_snapshotRVecoutMT.root",
{"fixedSizeArr", "size", "varSizeArr", "varSizeBoolArr", "fixedSizeBoolArr"});

checkSnapshotArrayFileMT(dt, kNEvents);

Expand All @@ -559,7 +595,8 @@ TEST_F(RDFSnapshotArrays, MultiThreadJitted)
ROOT::EnableImplicitMT(4);

RDataFrame tdf("arrayTree", kFileNames);
auto dj = tdf.Snapshot("outTree", "test_snapshotRVecoutMTJitted.root", {"fixedSizeArr", "size", "varSizeArr"});
auto dj = tdf.Snapshot("outTree", "test_snapshotRVecoutMTJitted.root",
{"fixedSizeArr", "size", "varSizeArr", "varSizeBoolArr", "fixedSizeBoolArr"});

checkSnapshotArrayFileMT(dj, kNEvents);

Expand Down