34#include "tools/rroot/file"
35#include "tools/rroot/rall"
36#include "tools/rroot/streamers"
37#include "tools/rroot/fac"
38#include "tools/rroot/tree"
52G4int G4RootRNtupleManager::ReadNtupleImpl(
const G4String& ntupleName,
61 auto isPerThread =
true;
62 if ( isUserFileName ) isPerThread =
false;
65 auto rfileTuple = fFileManager->GetRFile(fileName, isPerThread);
66 if (rfileTuple ==
nullptr) {
67 if ( ! fFileManager->OpenRFile(fileName, isPerThread) )
return kInvalidId;
68 rfileTuple = fFileManager->GetRFile(fileName, isPerThread);
70 auto rfile = std::get<0>(*rfileTuple);
73 tools::rroot::TDirectory* ntupleDirectory =
nullptr;
74 if ( ntupleDirectory ==
nullptr ) {
77 if ( ! dirName.empty() ) {
78 ntupleDirectory = tools::rroot::find_dir(rfile->dir(), dirName);
79 if ( ntupleDirectory !=
nullptr ) {
80 std::get<2>(*rfileTuple) = ntupleDirectory;
84 "Directory " + dirName +
" not found in file " + fileName +
".",
85 fkClass,
"ReadNtupleImpl");
92 tools::rroot::key* key =
nullptr;
93 if ( ntupleDirectory !=
nullptr ) {
94 key = ntupleDirectory->find_key(ntupleName);
97 key = rfile->dir().find_key(ntupleName);
100 Warn(
"Key " + ntupleName +
" for Ntuple not found in file " + fileName +
101 ", directory " + dirName, fkClass,
"ReadNtupleImpl");
106 char* charBuffer = key->get_object_buffer(*rfile, size);
107 if (charBuffer ==
nullptr) {
108 Warn(
"Cannot get data buffer for Ntuple " + ntupleName +
109 " in file " + fileName, fkClass,
"ReadNtupleImpl");
113 auto verbose =
false;
115 =
new tools::rroot::buffer(
G4cout, rfile->byte_swap(), size, charBuffer,
116 key->key_length(), verbose);
117 buffer->set_map_objs(
true);
119 auto fac =
new tools::rroot::fac(
G4cout);
121 auto tree =
new tools::rroot::tree(*rfile, *fac);
122 if ( ! tree->stream(*buffer) ) {
123 Warn(
"TTree streaming failed for Ntuple " + ntupleName +
124 " in file " + fileName,
125 fkClass,
"ReadNtupleImpl");
132 auto rntuple =
new tools::rroot::ntuple(*tree);
144G4bool G4RootRNtupleManager::GetTNtupleRow(
147 auto ntuple = ntupleDescription->
fNtuple;
151 if ( ! isInitialized ) {
153 if ( ! ntuple->initialize(
G4cout, *ntupleBinding) ) {
154 Warn(
"Ntuple initialization failed !!", fkClass,
"GetTNtupleRow");
161 auto next = ntuple->next();
163 if ( ! ntuple->get_row() ) {
164 Warn(
"Ntuple get_row() failed !!", fkClass,
"GetTNtupleRow");
G4GLOB_DLL std::ostream G4cout
void Message(G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
G4RootRNtupleManager()=delete
G4int SetNtuple(G4TRNtupleDescription< tools::rroot::ntuple > *rntupleDescription)
constexpr G4int kInvalidId
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)
tools::ntuple_binding * fNtupleBinding