Geant4-11
G4RootPNtupleManager.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, 04/10/2016 (ivana@ipno.in2p3.fr)
28
33
34#include "tools/wroot/file"
35#include "tools/wroot/ntuple"
36
37using namespace G4Analysis;
38using std::to_string;
39
40// mutex in a file scope
41namespace {
42
43//Mutex to lock master manager when adding ntuple row and ending fill
45
46//_____________________________________________________________________________
47void NotExistWarning(const G4String& what, G4int id,
48 std::string_view className,
49 std::string_view functionName)
50{
51 Warn(what + " id= " + to_string(id) + " does not exist.",
52 className, functionName);
53
54}
55
56}
57
58//_____________________________________________________________________________
60 std::shared_ptr<G4NtupleBookingManager> bookingManger,
61 std::shared_ptr<G4RootMainNtupleManager> main,
62 G4bool rowWise, G4bool rowMode)
63 : G4BaseNtupleManager(state),
64 fBookingManager(std::move(bookingManger)),
65 fMainNtupleManager(std::move(main)),
66 fRowWise(rowWise),
67 fRowMode(rowMode)
68{}
69
70//_____________________________________________________________________________
72{
73 for ( auto ntupleDescription : fNtupleDescriptionVector ) {
74 delete ntupleDescription;
75 }
76}
77
78//
79// private functions
80//
81
82//_____________________________________________________________________________
85 G4int id, std::string_view functionName, G4bool warn) const
86{
87 auto index = id - fFirstId;
88 if ( index < 0 || index >= G4int(fNtupleDescriptionVector.size()) ) {
89 if ( warn) {
90 NotExistWarning("ntuple description", id, fkClass, functionName);
91 }
92 return nullptr;
93 }
94
95 return fNtupleDescriptionVector[index];
96}
97
98//_____________________________________________________________________________
99tools::wroot::base_pntuple*
101 G4int id, std::string_view functionName, G4bool warn) const
102{
103 auto ntupleDescription = GetNtupleDescriptionInFunction(id, functionName);
104 if ( ! ntupleDescription ) return nullptr;
105
106 if ( ! ntupleDescription->fBasePNtuple ) {
107 if ( warn ) {
108 NotExistWarning("ntuple", id, fkClass, functionName);
109 }
110 return nullptr;
111 }
112 return ntupleDescription->fBasePNtuple;
113}
114
115//_____________________________________________________________________________
116tools::wroot::ntuple*
118 G4int id, std::string_view functionName, G4bool warn) const
119{
120 auto& mainNtupleVector = fMainNtupleManager->GetNtupleVector();
121
122 auto index = id - fFirstId;
123 if ( index < 0 || index >= G4int(mainNtupleVector.size()) ) {
124 if ( warn) {
125 NotExistWarning("main ntuple", id, fkClass, functionName);
126 }
127 return nullptr;
128 }
129
130 return mainNtupleVector[index];
131}
132
133//
134// protected functions
135//
136
137//_____________________________________________________________________________
139 G4RootPNtupleDescription* ntupleDescription,
140 tools::wroot::ntuple* mainNtuple)
141{
142 Message(kVL4, "create from main", "pntuple", mainNtuple->name());
143
144 auto file = fMainNtupleManager->GetNtupleFile(&ntupleDescription->fDescription);
145 if ( ! file ) {
146 Warn("Cannot create pntuple. Main ntuple file does not exist.",
147 fkClass, "CreateNtupleFromMain");
148 return;
149 }
150
151 ntupleDescription->fDescription.fFile = file;
152
153 // Get parameters from ntupleDescription
154 mainNtuple->get_branches(ntupleDescription->fMainBranches);
155
156 auto rfile = std::get<0>(*file);
157 G4bool verbose = true;
158 if ( fRowWise ) {
159 auto mainBranch = mainNtuple->get_row_wise_branch();
160 auto mtNtuple
161 = new tools::wroot::mt_ntuple_row_wise(
162 G4cout, rfile->byte_swap(), rfile->compression(),
163 mainNtuple->dir().seek_directory(),
164 *mainBranch, mainBranch->basket_size(),
165 ntupleDescription->fDescription.fNtupleBooking, verbose);
166
167 ntupleDescription->fNtuple
168 = static_cast<tools::wroot::imt_ntuple*>(mtNtuple);
169 ntupleDescription->fBasePNtuple
170 = static_cast<tools::wroot::base_pntuple*>(mtNtuple);
171 }
172 else {
173 std::vector<tools::uint32> basketSizes;
174 tools_vforcit(tools::wroot::branch*, ntupleDescription->fMainBranches, it) {
175 basketSizes.push_back((*it)->basket_size());
176 }
177 auto basketEntries = fMainNtupleManager->GetBasketEntries();
178
179 auto mtNtuple =
180 new tools::wroot::mt_ntuple_column_wise(
181 G4cout, rfile->byte_swap(), rfile->compression(),
182 mainNtuple->dir().seek_directory(),
183 ntupleDescription->fMainBranches, basketSizes,
184 ntupleDescription->fDescription.fNtupleBooking,
185 fRowMode, basketEntries, verbose);
186
187 ntupleDescription->fNtuple
188 = static_cast<tools::wroot::imt_ntuple*>(mtNtuple);
189 ntupleDescription->fBasePNtuple
190 = static_cast<tools::wroot::base_pntuple*>(mtNtuple);
191 }
192
193 ntupleDescription->fDescription.fIsNtupleOwner = true;
194 // // pntuple object is not deleted automatically
195 fNtupleVector.push_back(ntupleDescription->fNtuple);
196
197 Message(kVL3, "create from main", "pntuple", mainNtuple->name());
198}
199
200//_____________________________________________________________________________
202{
203// Create ntuple from booking (if not yet done) and main ntuple
204// This function is called from the first Fill call.
205
206 // Create pntuple descriptions from ntuple booking.
207 auto g4NtupleBookings = fBookingManager->GetNtupleBookingVector();
208 for ( auto g4NtupleBooking : g4NtupleBookings ) {
209 auto ntupleDescription = new G4RootPNtupleDescription(g4NtupleBooking);
210 // Save g4booking, activation in pntuple booking
211 fNtupleDescriptionVector.push_back(ntupleDescription);
212 }
213
214 auto& mainNtupleVector = fMainNtupleManager->GetNtupleVector();
215
216 G4int lcounter = 0;
217 for ( auto mainNtuple : mainNtupleVector ) {
218 auto& ntupleDescription = fNtupleDescriptionVector[lcounter++];
219 CreateNtupleFromMain(ntupleDescription, mainNtuple);
220 }
221
222 fCreateNtuples = false;
223}
224
225//_____________________________________________________________________________
227{
228// Create pntuple from g4 ntuple booking.
229// Nothing to be done here.
230
232}
233
234//_____________________________________________________________________________
236 G4int ntupleId, G4int columnId, G4int value)
237{
238 return FillNtupleTColumn<int>(ntupleId, columnId, value);
239}
240
241//_____________________________________________________________________________
243 G4int ntupleId, G4int columnId, G4float value)
244{
245 return FillNtupleTColumn<float>(ntupleId, columnId, value);
246}
247
248//_____________________________________________________________________________
250 G4int ntupleId, G4int columnId, G4double value)
251{
252 return FillNtupleTColumn<double>(ntupleId, columnId, value);
253}
254
255//_____________________________________________________________________________
257 G4int ntupleId, G4int columnId, const G4String& value)
258{
259 return FillNtupleTColumn<std::string>(ntupleId, columnId, value);
260}
261
262//_____________________________________________________________________________
264{
265 if (fCreateNtuples) {
267 }
268
269 if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
270 //G4cout << "Skipping AddNtupleRow for " << ntupleId << G4endl;
271 return false;
272 }
273
274 if ( IsVerbose(kVL4) ) {
275 Message(kVL4, "add", "pntuple row", " ntupleId " + to_string(ntupleId));
276 }
277
278 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "AddNtupleRow");
279 if ( ! ntupleDescription ) return false;
280
281 auto rfile = std::get<0>(*ntupleDescription->fDescription.fFile);
282
284 lock.unlock();
285 mutex toolsLock(lock);
286 auto result
287 = ntupleDescription->fNtuple->add_row(toolsLock, *rfile);
288
289 if ( ! result ) {
290 Warn("NtupleId " + to_string(ntupleId) + "adding row failed.",
291 fkClass, "AddNtupleRow");
292 }
293
294 ntupleDescription->fDescription.fHasFill = true;
295
296 if ( IsVerbose(kVL3) ) {
297 Message(kVL3, "add", "pntuple row", " ntupleId " + to_string(ntupleId));
298 }
299
300
301 return true;
302}
303
304//_____________________________________________________________________________
306{
307 for ( auto ntupleDescription : fNtupleDescriptionVector) {
308
309 // skip inactivated ntuples
310 if(!ntupleDescription->fDescription.fActivation || !ntupleDescription->fNtuple) {
311 // G4cout << "skipping inactive ntuple " << G4endl;
312 continue;
313 }
314
315 if ( IsVerbose(kVL4) ) {
316 Message(kVL4, "merge", "pntuple", ntupleDescription->fDescription.fNtupleBooking.name());
317 }
318
319 auto rfile = std::get<0>(*ntupleDescription->fDescription.fFile);
320
322 lock.unlock();
323 mutex toolsLock(lock);
324 auto result
325 = ntupleDescription->fNtuple->end_fill(toolsLock, *rfile);
326
327 if ( ! result ) {
328 Warn("Ntuple " + ntupleDescription->fDescription.fNtupleBooking.name() +
329 "end fill has failed.", fkClass, "Merge");
330 }
331
332 delete ntupleDescription->fNtuple;
333 ntupleDescription->fNtuple = nullptr;
334
335 if ( IsVerbose(kVL3) ) {
336 Message(kVL3, "merge", "pntuple", ntupleDescription->fDescription.fNtupleBooking.name());
337 }
338
339 }
340 return true;
341
342}
343
344//_____________________________________________________________________________
346{
347 for ( auto ntupleDescription : fNtupleDescriptionVector ) {
348 delete ntupleDescription->fNtuple;
349 }
350
352 fNtupleVector.clear();
353
354 Message(kVL2, "clear", "pntuples");
355}
356
357//_____________________________________________________________________________
358
360 G4bool activation)
361{
362 for ( auto ntupleDescription : fNtupleDescriptionVector ) {
363 ntupleDescription->fDescription.fActivation = activation;
364 }
365}
366
367//_____________________________________________________________________________
368
370 G4int ntupleId, G4bool activation)
371{
372 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetActivation");
373 if ( ! ntupleDescription ) return;
374
375 ntupleDescription->fDescription.fActivation = activation;
376}
377
378//_____________________________________________________________________________
380 G4int ntupleId) const
381{
382 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetActivation");
383 if ( ! ntupleDescription ) return false;
384
385 return ntupleDescription->fDescription.fActivation;
386}
387
388//_____________________________________________________________________________
390{
391 return fNtupleVector.size();
392}
393
394//_____________________________________________________________________________
396{
397 fRowWise = rowWise;
398 fRowMode = rowMode;
399}
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:85
std::mutex G4Mutex
Definition: G4Threading.hh:81
float G4float
Definition: G4Types.hh:84
double G4double
Definition: G4Types.hh:83
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
static char className[]
Definition: G4Win32.cc:36
G4GLOB_DLL std::ostream G4cout
int main(int argc, char *argv[])
void Message(G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
const G4AnalysisManagerState & fState
G4bool IsVerbose(G4int verboseLevel) const
virtual void SetActivation(G4bool activation) final
G4RootPNtupleDescription * GetNtupleDescriptionInFunction(G4int id, std::string_view function, G4bool warn=true) const
virtual G4int CreateNtuple(G4NtupleBooking *booking) final
virtual G4bool FillNtupleDColumn(G4int ntupleId, G4int columnId, G4double value) final
std::vector< G4RootPNtupleDescription * > fNtupleDescriptionVector
virtual G4bool FillNtupleSColumn(G4int ntupleId, G4int columnId, const G4String &value) final
tools::wroot::base_pntuple * GetNtupleInFunction(G4int id, std::string_view function, G4bool warn=true) const
void SetNtupleRowWise(G4bool rowWise, G4bool rowMode)
G4RootPNtupleManager()=delete
std::shared_ptr< G4NtupleBookingManager > fBookingManager
static constexpr std::string_view fkClass
virtual void Clear() final
virtual G4bool Merge() final
virtual G4bool FillNtupleIColumn(G4int ntupleId, G4int columnId, G4int value) final
std::shared_ptr< G4RootMainNtupleManager > fMainNtupleManager
virtual G4int GetNofNtuples() const final
tools::wroot::ntuple * GetMainNtupleInFunction(G4int id, std::string_view function, G4bool warn=true) const
virtual G4bool GetActivation(G4int ntupleId) const final
std::vector< tools::wroot::imt_ntuple * > fNtupleVector
virtual G4bool FillNtupleFColumn(G4int ntupleId, G4int columnId, G4float value) final
void CreateNtupleFromMain(G4RootPNtupleDescription *ntupleDescription, tools::wroot::ntuple *mainNtuple)
virtual G4bool AddNtupleRow() final
constexpr G4int kVL2
constexpr G4int kVL3
constexpr G4int kVL4
constexpr G4int kInvalidId
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)
void NotExistWarning(const G4String &what, G4int id, std::string_view className, std::string_view functionName)
tools::wroot::base_pntuple * fBasePNtuple
tools::wroot::imt_ntuple * fNtuple
std::vector< tools::wroot::branch * > fMainBranches
RootNtupleDescription fDescription
tools::ntuple_booking fNtupleBooking
std::shared_ptr< FT > fFile