Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:05:16

0001 /*
0002  * SoAStreamer_t.cpp
0003  * 
0004  * A test validating and the serialization of SoA Layouts to a ROOT file
0005  */
0006 
0007 #include <cstdlib>
0008 #include <memory>
0009 
0010 #include <TFile.h>
0011 #include <TTree.h>
0012 
0013 #include "FakeSoA.h"
0014 
0015 void writeSoA() {
0016   std::cout << "write begin" << std::endl;
0017   constexpr size_t nElements = 128;
0018 
0019   auto buffer = std::make_unique<std::byte[]>(FakeSoA::computeBufferSize(nElements));
0020   FakeSoA fsoa(buffer.get(), nElements);
0021   fsoa.dump();
0022   fsoa.fill();
0023   if (not fsoa.check()) {
0024     exit(EXIT_FAILURE);
0025   }
0026 
0027   std::unique_ptr<TFile> myFile(TFile::Open("serializerNoTObj.root", "RECREATE"));
0028   TTree tt("serializerNoTObjTree", "A SoA TTree");
0029   // In CMSSW, we will get a branch of objects (each row from the branched corresponding to an event)
0030   // So we have a branch with one element for the moment.
0031   [[maybe_unused]] auto Branch = tt.Branch("FakeSoA", &fsoa);
0032   std::cout << "In writeFile(), about to Fill()" << std::endl;
0033   fsoa.dump();
0034   auto prevGDebug = gDebug;
0035   gDebug = 5;
0036   tt.Fill();
0037   gDebug = prevGDebug;
0038   tt.Write();
0039   myFile->Close();
0040   std::cout << "write end" << std::endl;
0041 }
0042 
0043 void readSoA() {
0044   std::cout << "read begin" << std::endl;
0045   std::unique_ptr<TFile> myFile(TFile::Open("serializerNoTObj.root", "READ"));
0046   myFile->ls();
0047   std::unique_ptr<TTree> fakeSoATree((TTree *)myFile->Get("serializerNoTObjTree"));
0048   fakeSoATree->ls();
0049   auto prevGDebug = gDebug;
0050   //gDebug = 3;
0051   FakeSoA *fakeSoA = nullptr;
0052   fakeSoATree->SetBranchAddress("FakeSoA", &fakeSoA);
0053   fakeSoATree->GetEntry(0);
0054   gDebug = prevGDebug;
0055   std::cout << "fakeSoAAddress=" << fakeSoA << std::endl;
0056   fakeSoA->dump();
0057   fakeSoA->dumpData();
0058   std::cout << "Checking SoA readback...";
0059   if (not fakeSoA->check()) {
0060     exit(EXIT_FAILURE);
0061   }
0062   std::cout << " OK" << std::endl;
0063 }
0064 
0065 int main() {
0066   writeSoA();
0067   readSoA();
0068   return EXIT_SUCCESS;
0069 }