Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
G4XmlRNtupleManager.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, 25/07/2014 (ivana@ipno.in2p3.fr)
28
30#include "G4XmlRFileManager.hh"
33
34using namespace G4Analysis;
35using std::to_string;
36
37//
38// utility function (to be provided in tools)
39//
40
41namespace tools {
42namespace aida {
43template <class T>
44bool to_vector(base_ntu& a_ntu,std::vector<T>& a_vec) {
45 a_vec.clear();
46 const std::vector<base_col*>& cols = a_ntu.cols();
47 if(cols.empty()) return false;
48 base_col* _base_col = cols.front();
49 aida_col<T>* _col = safe_cast<base_col, aida_col<T> >(*_base_col);
50 if(!_col) return false;
51 a_ntu.start();
52 uint64 _rows = a_ntu.rows();
53 a_vec.resize(_rows);
54 T v;
55 {for(uint64 row=0;row<_rows;row++) {
56 if(!a_ntu.next()) {a_vec.clear();return false;}
57 if(!_col->get_entry(v)) {a_vec.clear();return false;}
58 a_vec[row] = v;
59 }}
60 return true;
61}
62}}
63
64//_____________________________________________________________________________
66 : G4TRNtupleManager<tools::aida::ntuple>(state)
67{}
68
69//
70// private methods
71//
72
73//_____________________________________________________________________________
74G4int G4XmlRNtupleManager::ReadNtupleImpl(const G4String& ntupleName,
75 const G4String& fileName,
76 const G4String& /*dirName*/,
77 G4bool isUserFileName)
78{
79 Message(kVL4, "read", "ntuple", ntupleName);
80
81 // Ntuples are saved per object and per thread
82 // but apply the ntuple name and the thread suffixes
83 // only if fileName is not provided explicitly
84 auto fullFileName = fileName;
85 if ( ! isUserFileName ) {
86 fullFileName = fFileManager->GetNtupleFileName(ntupleName);
87 }
88
89 auto handler = fFileManager->GetHandler<tools::aida::ntuple>(
90 fullFileName, ntupleName, "ReadNtupleImpl");
91 if (handler == nullptr) return kInvalidId;
92
93 auto rntuple = static_cast<tools::aida::ntuple*>(handler->object());
95
96 Message(kVL2, "read", "ntuple", ntupleName, id > kInvalidId);
97
98 return id;
99}
100
101//_____________________________________________________________________________
103 const G4String& columnName,
104 std::vector<G4int>& vector)
105{
106// Override base class default implementation
107
108 Message(kVL4, "set", "ntuple I column",
109 " ntupleId " + to_string(ntupleId) + " " + columnName);
110
111 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetNtupleIColumn");
112 if (ntupleDescription == nullptr) return false;
113
114 // not supported
115 //tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
116 //ntupleBinding->add_column(columnName, vector);
117
118 auto subNtuple = new tools::aida::ntuple(G4cout, columnName);
119 ntupleDescription->fIVectorBindingMap[subNtuple] = &vector;
120 tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
121 ntupleBinding->add_column_cid(columnName, *subNtuple);
122
123 Message(kVL2, "set", "ntuple I column",
124 " ntupleId " + to_string(ntupleId) + " " + columnName);
125
126 return true;
127}
128
129//_____________________________________________________________________________
131 const G4String& columnName,
132 std::vector<G4float>& vector)
133{
134// Override base class default implementation
135
136 Message(kVL4, "set", "ntuple F column",
137 " ntupleId " + to_string(ntupleId) + " " + columnName);
138
139
140 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetNtupleFColumn");
141 if (ntupleDescription == nullptr) return false;
142
143 // not supported
144 //tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
145 //ntupleBinding->add_column(columnName, vector);
146
147 auto subNtuple = new tools::aida::ntuple(G4cout, columnName);
148 ntupleDescription->fFVectorBindingMap[subNtuple] = &vector;
149 tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
150 ntupleBinding->add_column_cid(columnName, *subNtuple);
151
152 Message(kVL4, "set", "ntuple F column",
153 " ntupleId " + to_string(ntupleId) + " " + columnName);
154
155 return true;
156}
157
158//_____________________________________________________________________________
160 const G4String& columnName,
161 std::vector<G4double>& vector)
162{
163// Override base class default implementation
164
165 Message(kVL4, "set", "ntuple D column",
166 " ntupleId " + to_string(ntupleId) + " " + columnName);
167
168 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetNtupleDColumn");
169 if (ntupleDescription == nullptr) return false;
170
171 // not supported
172 //tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
173 //ntupleBinding->add_column(columnName, vector);
174
175 auto subNtuple = new tools::aida::ntuple(G4cout, columnName);
176 ntupleDescription->fDVectorBindingMap[subNtuple] = &vector;
177 tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
178 ntupleBinding->add_column_cid(columnName, *subNtuple);
179
180 Message(kVL2, "set", "ntuple D column",
181 " ntupleId " + to_string(ntupleId) + " " + columnName);
182
183 return true;
184}
185
186//_____________________________________________________________________________
188 const G4String& columnName,
189 std::vector<std::string>& vector)
190{
191// Override base class default implementation
192
193 Message(kVL4, "set", "ntuple S column",
194 " ntupleId " + to_string(ntupleId) + " " + columnName);
195
196 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetNtupleSColumn");
197 if (ntupleDescription == nullptr) return false;
198
199 // not supported
200 //tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
201 //ntupleBinding->add_column(columnName, vector);
202
203 auto subNtuple = new tools::aida::ntuple(G4cout, columnName);
204 ntupleDescription->fSVectorBindingMap[subNtuple] = &vector;
205 tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
206 ntupleBinding->add_column_cid(columnName, *subNtuple);
207
208 Message(kVL2, "set", "ntuple S column",
209 " ntupleId " + to_string(ntupleId) + " " + columnName);
210
211 return true;
212}
213
214//_____________________________________________________________________________
215G4bool G4XmlRNtupleManager::GetTNtupleRow(
217{
218 auto ntuple = ntupleDescription->fNtuple;
219
220 G4bool isInitialized = ntupleDescription->fIsInitialized;
221 if ( ! isInitialized ) {
222 tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding;
223 if ( ! ntuple->set_binding(std::cout, *ntupleBinding) ) {
224 Warn("Ntuple initialization failed !!", fkClass, "GetTNtupleRow");
225 return false;
226 }
227 ntupleDescription->fIsInitialized = true;
228 ntuple->start();
229 }
230
231 G4bool next = ntuple->next();
232 if ( next ) {
233 if ( ! ntuple->get_row() ) {
234 Warn("Ntuple get_row() failed !!", fkClass, "GetTNtupleRow");
235 return false;
236 }
237
238 // fill vector from sub ntuples
239 for ( auto [key, value] : ntupleDescription->fIVectorBindingMap) {
240 tools::aida::to_vector<int>(*key, *value);
241 }
242 for ( auto [key, value] : ntupleDescription->fFVectorBindingMap) {
243 tools::aida::to_vector<float>(*key, *value);
244 }
245 for ( auto [key, value] : ntupleDescription->fDVectorBindingMap) {
246 tools::aida::to_vector<double>(*key, *value);
247 }
248 for ( auto [key, value] : ntupleDescription->fSVectorBindingMap) {
249 tools::aida::to_vector<std::string>(*key, *value);
250 }
251 }
252
253 return next;
254}
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
G4TRNtupleDescription< tools::aida::ntuple > * GetNtupleDescriptionInFunction(G4int id, std::string_view function, G4bool warn=true) const
G4int SetNtuple(G4TRNtupleDescription< tools::aida::ntuple > *rntupleDescription)
G4bool SetNtupleIColumn(G4int ntupleId, const G4String &columnName, std::vector< G4int > &vector) final
G4bool SetNtupleFColumn(G4int ntupleId, const G4String &columnName, std::vector< G4float > &vector) final
G4bool SetNtupleSColumn(G4int ntupleId, const G4String &columnName, std::vector< std::string > &vector) final
G4bool SetNtupleDColumn(G4int ntupleId, const G4String &columnName, std::vector< G4double > &vector) final
G4XmlRNtupleManager()=delete
constexpr G4int kVL2
constexpr G4int kVL4
constexpr G4int kInvalidId
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)
bool to_vector(base_ntu &a_ntu, std::vector< T > &a_vec)
tools::ntuple_binding * fNtupleBinding
std::map< NT *, std::vector< std::string > * > fSVectorBindingMap
std::map< NT *, std::vector< float > * > fFVectorBindingMap
std::map< NT *, std::vector< double > * > fDVectorBindingMap
std::map< NT *, std::vector< int > * > fIVectorBindingMap