Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
/*
 * SoAStreamer_t.cpp
 * 
 * A test validating and the serialization of SoA Layouts to a ROOT file
 */

#include <cstdlib>
#include <memory>

#include <TFile.h>
#include <TTree.h>

#include "FakeSoA.h"

void writeSoA() {
  std::cout << "write begin" << std::endl;
  constexpr size_t nElements = 128;

  auto buffer = std::make_unique<std::byte[]>(FakeSoA::computeBufferSize(nElements));
  FakeSoA fsoa(buffer.get(), nElements);
  fsoa.dump();
  fsoa.fill();
  if (not fsoa.check()) {
    exit(EXIT_FAILURE);
  }

  std::unique_ptr<TFile> myFile(TFile::Open("serializerNoTObj.root", "RECREATE"));
  TTree tt("serializerNoTObjTree", "A SoA TTree");
  // In CMSSW, we will get a branch of objects (each row from the branched corresponding to an event)
  // So we have a branch with one element for the moment.
  [[maybe_unused]] auto Branch = tt.Branch("FakeSoA", &fsoa);
  std::cout << "In writeFile(), about to Fill()" << std::endl;
  fsoa.dump();
  auto prevGDebug = gDebug;
  gDebug = 5;
  tt.Fill();
  gDebug = prevGDebug;
  tt.Write();
  myFile->Close();
  std::cout << "write end" << std::endl;
}

void readSoA() {
  std::cout << "read begin" << std::endl;
  std::unique_ptr<TFile> myFile(TFile::Open("serializerNoTObj.root", "READ"));
  myFile->ls();
  std::unique_ptr<TTree> fakeSoATree((TTree *)myFile->Get("serializerNoTObjTree"));
  fakeSoATree->ls();
  auto prevGDebug = gDebug;
  //gDebug = 3;
  FakeSoA *fakeSoA = nullptr;
  fakeSoATree->SetBranchAddress("FakeSoA", &fakeSoA);
  fakeSoATree->GetEntry(0);
  gDebug = prevGDebug;
  std::cout << "fakeSoAAddress=" << fakeSoA << std::endl;
  fakeSoA->dump();
  fakeSoA->dumpData();
  std::cout << "Checking SoA readback...";
  if (not fakeSoA->check()) {
    exit(EXIT_FAILURE);
  }
  std::cout << " OK" << std::endl;
}

int main() {
  writeSoA();
  readSoA();
  return EXIT_SUCCESS;
}