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