Geant4-11
G4RootNtupleFileManager.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, 15/09/2020 (ivana@ipno.in2p3.fr)
28
30#include "G4RootFileManager.hh"
34#include "G4VAnalysisManager.hh"
37
38#include "G4Threading.hh"
39#include "G4AutoLock.hh"
40
41using namespace G4Analysis;
42using std::make_shared;
43using std::to_string;
44
45
46
48
49//_____________________________________________________________________________
51 : G4VNtupleFileManager(state, "root")
52{
54
55 // Do not merge ntuples by default
56 // Merging may require user code migration as analysis manager
57 // must be created both on master and workers.
58 auto mergeNtuples = false;
60}
61
62//_____________________________________________________________________________
64{
65 if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
66}
67
68//
69// private methods
70//
71
72//_____________________________________________________________________________
74 G4int nofNtupleFiles)
75
76{
77 Message(kVL4, "set", "ntuple merging mode");
78
79 auto canMerge = true;
80
81 // Illegal situations
82 if ( mergeNtuples && ( ! G4Threading::IsMultithreadedApplication() ) ) {
83 Warn("Merging ntuples is not applicable in sequential application.\n"
84 "Setting was ignored.",
85 fkClass, "SetNtupleMergingMode");
86 canMerge = false;
87 }
88
89 // Illegal situations
90 if ( mergeNtuples && G4Threading::IsMultithreadedApplication() &&
91 ( ! fgMasterInstance ) ) {
92 Warn("Merging ntuples requires G4AnalysisManager instance on master.\n"
93 "Setting was ignored.",
94 fkClass, "SetNtupleMergingMode");
95 canMerge = false;
96 }
97
98 G4String mergingMode;
99 if ( ( ! mergeNtuples ) || ( ! canMerge ) ) {
101 mergingMode = "G4NtupleMergeMode::kNone";
102 }
103 else {
104 // Set the number of reduced ntuple files
105 fNofNtupleFiles = nofNtupleFiles;
106
107 // Check the number of reduced ntuple files
108 if ( fNofNtupleFiles < 0 ) {
109 Warn("Number of reduced files must be [0, nofThreads].\n"
110 "Cannot set " + to_string(nofNtupleFiles) +" files.\n" +
111 "Setting was ignored.",
112 fkClass, "SetNtupleMergingMode");
113 fNofNtupleFiles = 0;
114 }
115
116 // Forced merging mode
117 G4bool isMaster = ! G4Threading::IsWorkerThread();
118 if ( isMaster ) {
120 mergingMode = "G4NtupleMergeMode::kMain";
121 } else {
123 mergingMode = "G4NtupleMergeMode::kSlave";
124 }
125 }
126
127 Message(kVL2, "set", "ntuple merging mode", mergingMode);
128}
129
130//_____________________________________________________________________________
132{
133 if ( ! fNofNtupleFiles ) return 0;
134
135 G4int nofMainManagers = fNofNtupleFiles;
136 if ( ! nofMainManagers ) nofMainManagers = 1;
137
138 auto fileNumber = G4Threading::G4GetThreadId() % nofMainManagers;
139 return fileNumber;
140}
141
142//_____________________________________________________________________________
144{
145 // Close ntuple files
146
147 auto result = true;
148 auto ntupleVector = fNtupleManager->GetNtupleDescriptionVector();
149 for ( auto ntupleDescription : ntupleVector) {
150 result &= fFileManager->CloseNtupleFile(ntupleDescription);
151 }
152
153 return result;
154}
155
156//
157// public methods
158//
159
160//_____________________________________________________________________________
162 G4int nofNtupleFiles)
163
164{
165 if ( fIsInitialized ) {
166 Warn("Cannot change merging mode.\n"
167 "The function must be called before OpenFile().",
168 fkClass, "SetNtupleMerging");
169 return;
170 }
171
172 // Set ntuple merging mode
173 SetNtupleMergingMode(mergeNtuples, nofNtupleFiles);
174}
175
176//_____________________________________________________________________________
178{
179
180 // Print info even when setting makes no effect
181 // (as we do not get printed the default setting in the output)
182 G4String rowWiseMode;
183 if ( rowWise ) {
184 rowWiseMode = "row-wise with extra branch";
185 }
186 else if ( rowMode ) {
187 rowWiseMode = "row-wise";
188 }
189 else {
190 rowWiseMode = "column-wise";
191 }
192
193 Message(kVL1, "set", "ntuple merging row mode", rowWiseMode);
194
195 // Do nothing if the mode is not changed
196 if ( fNtupleRowWise == rowWise && fNtupleRowMode == rowMode ) return;
197
198 fNtupleRowWise = rowWise;
199 fNtupleRowMode = rowMode;
200
201 if ( fNtupleManager ) {
202 fNtupleManager->SetNtupleRowWise(rowWise, rowMode);
203 }
204
205 if ( fSlaveNtupleManager ) {
206 fSlaveNtupleManager->SetNtupleRowWise(rowWise, rowMode);
207 }
208}
209
210//_____________________________________________________________________________
211void G4RootNtupleFileManager::SetBasketSize(unsigned int basketSize)
212{
213 fFileManager->SetBasketSize(basketSize);
214}
215
216//_____________________________________________________________________________
217void G4RootNtupleFileManager::SetBasketEntries(unsigned int basketEntries)
218{
219 fFileManager->SetBasketEntries(basketEntries);
220}
221
222//_____________________________________________________________________________
223std::shared_ptr<G4VNtupleManager> G4RootNtupleFileManager::CreateNtupleManager()
224{
225// Create and return ntuple manager.
226// If ntuple merging is activated, managers of different types are created
227// on master/worker.
228
229 Message(kVL4, "create", "ntuple manager");
230
231 // Check that file manager and anaysis manager are set !
232
233 std::shared_ptr<G4VNtupleManager> activeNtupleManager = nullptr;
234 switch ( fNtupleMergeMode )
235 {
238 = make_shared<G4RootNtupleManager>(
240 fNtupleManager->SetFileManager(fFileManager);
241 activeNtupleManager = fNtupleManager;
242 break;
243
245 G4int nofMainManagers = fNofNtupleFiles;
246 if ( ! nofMainManagers ) nofMainManagers = 1;
247 // create one manager if merging required into the histos & profiles files
249 = make_shared<G4RootNtupleManager>(
251 fNtupleManager->SetFileManager(fFileManager);
252 activeNtupleManager = fNtupleManager;
253 break;
254 }
255
258 // The master class is used only in Get* functions
259 auto mainNtupleManager
260 = fNtupleManager->GetMainNtupleManager(GetNtupleFileNumber());
262 = make_shared<G4RootPNtupleManager>(
264 activeNtupleManager = fSlaveNtupleManager;
265 break;
266 }
267
268 G4String mergeMode;
269 switch ( fNtupleMergeMode ) {
271 mergeMode = "";
272 break;
274 mergeMode = "main ";
275 break;
277 mergeMode = "slave ";
278 break;
279 }
280 Message(kVL3, "create", mergeMode + "ntuple manager");
281
282 fIsInitialized = true;
283
284 return activeNtupleManager;
285}
286
287//_____________________________________________________________________________
289{
290 // Check if fNtupleBookingManager is set
291
294
295 G4String objectType = "analysis file";
297 objectType = "main analysis file";
298 }
299 Message(kVL4, "open", objectType, fileName);
300
301 // Creating files is triggered from CreateNtuple
302 fNtupleManager->CreateNtuplesFromBooking(
303 fBookingManager->GetNtupleBookingVector());
304
305 Message(kVL1, "open", objectType, fileName);
306 }
307
308 // Creating ntuples from main is triggered from the first Fill call
309 // if ( fNtupleMergeMode == G4NtupleMergeMode::kSlave ) {
310 // // G4cout << "Going to create slave ntuples from main" << G4endl;
311 // fSlaveNtupleManager->CreateNtuplesFromMain();
312 // }
313
314 return true;
315}
316
317//_____________________________________________________________________________
319{
321 return true;
322 }
323
324 auto result = true;
325
326 G4String ntupleType;
327 if ( fNtupleMergeMode == G4NtupleMergeMode::kMain ) ntupleType = "main ntuples";
328 if ( fNtupleMergeMode == G4NtupleMergeMode::kSlave ) ntupleType = "slave ntuples";
329
330 Message(kVL4, "merge", ntupleType);
331
333 result &= fNtupleManager->Merge();
334 }
335
337 result &= fSlaveNtupleManager->Merge();
338 }
339
340 Message(kVL1, "merge", ntupleType, "", result);
341
342 return result;
343}
344
345//_____________________________________________________________________________
347{
348 auto result = true;
349
350 // close files
352 result &= CloseNtupleFiles();
353 }
354
355 if ( ! reset ) {
356 // The ntuples must be always reset when closing file)
357 if ( ! Reset() ) {
358 Warn("Resetting data failed", fkClass, "ActionAtCloseFile");
359 result = false;
360 }
361 }
362
363 return result;
364}
365
366//_____________________________________________________________________________
368{
369// Reset ntuples
370
371 auto result = true;
372
375 result &= fNtupleManager->Reset();
376 }
377
378 return result;
379}
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
virtual void SetNtupleMerging(G4bool mergeNtuples, G4int nofReducedNtupleFiles=0) override
virtual void SetNtupleRowWise(G4bool rowWise, G4bool rowMode=true) override
virtual void SetBasketSize(unsigned int basketSize) override
static G4RootNtupleFileManager * fgMasterInstance
virtual std::shared_ptr< G4VNtupleManager > CreateNtupleManager() override
static constexpr std::string_view fkClass
virtual G4bool ActionAtCloseFile(G4bool reset) override
virtual G4bool ActionAtWrite() override
std::shared_ptr< G4RootPNtupleManager > fSlaveNtupleManager
void SetNtupleMergingMode(G4bool mergeNtuples, G4int nofNtupleFiles)
virtual void SetBasketEntries(unsigned int basketEntries) override
virtual G4bool Reset() override
std::shared_ptr< G4RootNtupleManager > fNtupleManager
virtual G4bool ActionAtOpenFile(const G4String &fileName) override
G4RootNtupleFileManager()=delete
std::shared_ptr< G4RootFileManager > fFileManager
std::shared_ptr< G4NtupleBookingManager > fBookingManager
void Message(G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
const G4AnalysisManagerState & fState
constexpr G4int kVL1
constexpr G4int kVL2
constexpr G4int kVL3
constexpr G4int kVL4
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)
G4bool IsWorkerThread()
Definition: G4Threading.cc:123
G4bool IsMultithreadedApplication()
Definition: G4Threading.cc:130
G4bool IsMasterThread()
Definition: G4Threading.cc:124
G4int G4GetThreadId()
Definition: G4Threading.cc:122