Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4RootRNtupleManager.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26
27// Author: Ivana Hrivnacova, 09/04/2014 ([email protected])
28
30#include "G4RootRFileManager.hh"
33
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"
39
40using namespace G4Analysis;
41
42//_____________________________________________________________________________
44 : G4TRNtupleManager<tools::rroot::ntuple>(state)
45{}
46
47//
48// private methods
49//
50
51//_____________________________________________________________________________
52G4int G4RootRNtupleManager::ReadNtupleImpl(const G4String& ntupleName,
53 const G4String& fileName,
54 const G4String& dirName,
55 G4bool isUserFileName)
56{
57 Message(kVL4, "read", "ntuple", ntupleName);
58
59 // Ntuples are saved per thread
60 // but do not apply the thread suffix if fileName is provided explicitly
61 auto isPerThread = true;
62 if ( isUserFileName ) isPerThread = false;
63
64 // Get or open a file
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);
69 }
70 auto rfile = std::get<0>(*rfileTuple);
71
72 // Get or open a directory if dirName is defined
73 tools::rroot::TDirectory* ntupleDirectory = nullptr;
74 if ( ntupleDirectory == nullptr ) {
75 // Retrieve directory only once as analysis manager supports only
76 // 1 ntuple directory par file)
77 if ( ! dirName.empty() ) {
78 ntupleDirectory = tools::rroot::find_dir(rfile->dir(), dirName);
79 if ( ntupleDirectory != nullptr ) {
80 std::get<2>(*rfileTuple) = ntupleDirectory;
81 }
82 else {
84 "Directory " + dirName + " not found in file " + fileName + ".",
85 fkClass, "ReadNtupleImpl");
86 return kInvalidId;
87 }
88 }
89 }
90
91 // Get key
92 tools::rroot::key* key = nullptr;
93 if ( ntupleDirectory != nullptr ) {
94 key = ntupleDirectory->find_key(ntupleName);
95 }
96 else {
97 key = rfile->dir().find_key(ntupleName);
98 }
99 if (key == nullptr) {
100 Warn("Key " + ntupleName + " for Ntuple not found in file " + fileName +
101 ", directory " + dirName, fkClass, "ReadNtupleImpl");
102 return kInvalidId;
103 }
104
105 unsigned int size;
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");
110 return kInvalidId;
111 }
112
113 auto verbose = false;
114 auto buffer
115 = new tools::rroot::buffer(G4cout, rfile->byte_swap(), size, charBuffer,
116 key->key_length(), verbose);
117 buffer->set_map_objs(true);
118
119 auto fac = new tools::rroot::fac(G4cout);
120
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");
126
127 delete buffer;
128 delete tree;
129 return kInvalidId;
130 }
131
132 auto rntuple = new tools::rroot::ntuple(*tree); //use the flat ntuple API.
133 auto rntupleDescription = new G4TRNtupleDescription<tools::rroot::ntuple>(rntuple);
134
135 auto id = SetNtuple(rntupleDescription);
136
137
138 Message(kVL2, "read", "ntuple", ntupleName, id > kInvalidId);
139
140 return id;
141}
142
143//_____________________________________________________________________________
144G4bool G4RootRNtupleManager::GetTNtupleRow(
146{
147 auto ntuple = ntupleDescription->fNtuple;
148 auto ntupleBinding = ntupleDescription->fNtupleBinding;
149
150 G4bool isInitialized = ntupleDescription->fIsInitialized;
151 if ( ! isInitialized ) {
152
153 if ( ! ntuple->initialize(G4cout, *ntupleBinding) ) {
154 Warn("Ntuple initialization failed !!", fkClass, "GetTNtupleRow");
155 return false;
156 }
157 ntupleDescription->fIsInitialized = true;
158 ntuple->start();
159 }
160
161 auto next = ntuple->next();
162 if ( next ) {
163 if ( ! ntuple->get_row() ) {
164 Warn("Ntuple get_row() failed !!", fkClass, "GetTNtupleRow");
165 return false;
166 }
167 }
168
169 return next;
170}
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
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 kVL2
constexpr G4int kVL4
constexpr G4int kInvalidId
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)
G4bool isInitialized()
tools::ntuple_binding * fNtupleBinding