rntviewer/rntviewer.cpp

80 lines
1.6 KiB
C++
Raw Normal View History

2024-07-10 15:04:11 +00:00
#include <ROOT/RNTupleReader.hxx>
2024-07-10 15:48:23 +00:00
#include <ROOT/RNTuple.hxx>
#include <TFile.h>
2024-07-10 15:04:11 +00:00
#include <cstdio>
2024-07-10 15:48:23 +00:00
#include <cstdint>
2024-07-10 15:04:11 +00:00
#include <sys/mman.h>
2024-07-10 15:48:23 +00:00
#include <unistd.h>
#ifdef DEBUG
#include <sanitizer/asan_interface.h>
#endif
#include "types.h"
#include "defer.hpp"
#include "mem.h"
#include "mem.cpp"
2024-07-10 15:04:11 +00:00
using namespace ROOT::Experimental;
static size_t file_size(FILE *f)
{
fseek(f, 0, SEEK_END);
size_t res = ftell(f);
fseek(f, 0, SEEK_SET);
return res;
}
int main(int argc, char **argv)
{
2024-07-10 15:48:23 +00:00
if (argc < 3) {
fprintf(stderr, "Usage: %s <ntuple_name> <ntuple_file.root>\n", argv[0]);
2024-07-10 15:04:11 +00:00
return 1;
}
2024-07-10 15:48:23 +00:00
// Collect arguments
const char *ntpl_name = argv[1];
const char *fname = argv[2];
// Allocate memory
Arena *arena = arena_alloc();
if (!arena) {
fprintf(stderr, "Failed to allocate memory\n");
return 1;
}
defer { arena_release(arena); };
2024-07-10 15:04:11 +00:00
2024-07-10 15:48:23 +00:00
// Open and map the file
2024-07-10 15:04:11 +00:00
FILE *file = fopen(fname, "rb");
defer { fclose(file); };
const int fd = fileno(file);
const size_t fsize = file_size(file);
void *fmem = mmap(0, fsize, PROT_READ, MAP_SHARED_VALIDATE, fd, 0);
if (!fmem) {
fprintf(stderr, "Failed to open file %s\n", fname);
return 1;
}
defer { munmap(fmem, fsize); };
2024-07-10 15:48:23 +00:00
// Open the TFile
TFile *tfile = TFile::Open(fname, "READ");
if (!tfile) {
fprintf(stderr, "Failed to open TFile.\n");
return 1;
}
defer { delete tfile; };
2024-07-10 15:04:11 +00:00
// Get the RNTuple information
2024-07-10 15:48:23 +00:00
const RNTuple *anchor = tfile->Get<RNTuple>(ntpl_name);
if (!anchor) {
fprintf(stderr, "RNTuple '%s' not found in %s.\n", ntpl_name, fname);
return 1;
}
2024-07-10 15:04:11 +00:00
return 0;
}