Geant4-11
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 (ivana@ipno.in2p3.fr)
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//_____________________________________________________________________________
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 rfile = fFileManager->GetRFile(fileName, isPerThread);
66 if ( ! rfile ) {
67 if ( ! fFileManager->OpenRFile(fileName, isPerThread) ) return kInvalidId;
68 rfile = fFileManager->GetRFile(fileName, isPerThread);
69 }
70
71 // Get key
72 tools::rroot::key* key = nullptr;
73 tools::rroot::TDirectory* newDir = nullptr;
74 if ( ! dirName.empty() ) {
75 newDir = tools::rroot::find_dir(rfile->dir(), dirName);
76 if ( newDir ) {
77 key = newDir->find_key(ntupleName);
78 }
79 else {
81 "Directory " + dirName + " not found in file " + fileName + ".",
82 fkClass, "ReadNtupleImpl");
83 return kInvalidId;
84 }
85 }
86 else {
87 key = rfile->dir().find_key(ntupleName);
88 }
89
90 if ( ! key ) {
91 Warn("Key " + ntupleName + " for Ntuple not found in file " + fileName +
92 ", directory " + dirName, fkClass, "ReadNtupleImpl");
93 delete newDir;
94 return kInvalidId;
95 }
96
97 unsigned int size;
98 char* charBuffer = key->get_object_buffer(*rfile, size);
99 if ( ! charBuffer ) {
100 Warn("Cannot get data buffer for Ntuple " + ntupleName +
101 " in file " + fileName, fkClass, "ReadNtupleImpl");
102 delete newDir;
103 return kInvalidId;
104 }
105
106 auto verbose = false;
107 auto buffer
108 = new tools::rroot::buffer(G4cout, rfile->byte_swap(), size, charBuffer,
109 key->key_length(), verbose);
110 buffer->set_map_objs(true);
111
112 auto fac = new tools::rroot::fac(G4cout);
113
114 auto tree = new tools::rroot::tree(*rfile, *fac);
115 if ( ! tree->stream(*buffer) ) {
116 Warn("TTree streaming failed for Ntuple " + ntupleName +
117 " in file " + fileName,
118 fkClass, "ReadNtupleImpl");
119
120 delete buffer;
121 delete tree;
122 delete newDir;
123 return kInvalidId;
124 }
125
126 auto rntuple = new tools::rroot::ntuple(*tree); //use the flat ntuple API.
127 auto rntupleDescription = new G4TRNtupleDescription<tools::rroot::ntuple>(rntuple);
128
129 auto id = SetNtuple(rntupleDescription);
130
131 Message(kVL2, "read", "ntuple", ntupleName, id > kInvalidId);
132
133 return id;
134}
135
136//_____________________________________________________________________________
139{
140 auto ntuple = ntupleDescription->fNtuple;
141 auto ntupleBinding = ntupleDescription->fNtupleBinding;
142
143 G4bool isInitialized = ntupleDescription->fIsInitialized;
144 if ( ! isInitialized ) {
145
146 if ( ! ntuple->initialize(G4cout, *ntupleBinding) ) {
147 Warn("Ntuple initialization failed !!", fkClass, "GetTNtupleRow");
148 return false;
149 }
150 ntupleDescription->fIsInitialized = true;
151 ntuple->start();
152 }
153
154 auto next = ntuple->next();
155 if ( next ) {
156 if ( ! ntuple->get_row() ) {
157 Warn("Ntuple get_row() failed !!", fkClass, "GetTNtupleRow");
158 return false;
159 }
160 }
161
162 return next;
163}
static const G4double fac
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
std::shared_ptr< G4RootRFileManager > fFileManager
virtual G4int ReadNtupleImpl(const G4String &ntupleName, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
G4RootRNtupleManager()=delete
static constexpr std::string_view fkClass
virtual G4bool GetTNtupleRow(G4TRNtupleDescription< tools::rroot::ntuple > *ntupleDescription) final
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
#define buffer
Definition: xmlparse.cc:628