Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4RootMainNtupleManager.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
30#include "G4RootFileManager.hh"
33
34#include "tools/wroot/file"
35#include "tools/wroot/ntuple"
36
37using namespace G4Analysis;
38
39//_____________________________________________________________________________
41 G4RootNtupleManager* ntupleBuilder,
42 std::shared_ptr<G4NtupleBookingManager> bookingManager,
43 G4bool rowWise,
44 G4int fileNumber,
45 const G4AnalysisManagerState& state)
46 : G4BaseAnalysisManager(state),
47 fNtupleBuilder(ntupleBuilder),
48 fBookingManager(std::move(bookingManager)),
49 fRowWise(rowWise),
50 fFileNumber(fileNumber)
51{}
52
53//
54// protected functions
55//
56
57//_____________________________________________________________________________
59 G4bool warn)
60{
61// Create ntuple from booking if file was open
62
63 // Get/Create main ntuple file
64 auto ntupleFile = fFileManager->CreateNtupleFile(ntupleDescription, fFileNumber);
65 if ( ntupleFile == nullptr ) {
66 if ( warn ) {
67 Warn("Ntuple file must be defined first.\n"
68 "Cannot create main ntuple.",
69 fkClass, "CreateNtuple");
70 }
71 return;
72 }
73
74 // Get ntuple booking
75 const auto& ntupleBooking = ntupleDescription->GetNtupleBooking();
76
77 Message(kVL4, "create", "main ntuple", ntupleBooking.name());
78
79 // Create ntuple
80 auto ntuple = new tools::wroot::ntuple(*std::get<2>(*ntupleFile), ntupleBooking, fRowWise);
81 // ntuple object is deleted automatically when closing a file
82 auto basketSize = fNtupleBuilder->GetBasketSize();
83 ntuple->set_basket_size(basketSize);
84
85 fNtupleVector.push_back(ntuple);
86 fNtupleDescriptionVector.push_back(std::make_pair(ntupleDescription, ntupleFile));
87
88 Message(kVL3, "create", "main ntuple", ntupleBooking.name());
89}
90
91//_____________________________________________________________________________
93{
94 std::size_t counter = 0;
95
96 for ( auto ntuple : fNtupleVector ) {
97 ntuple->merge_number_of_entries();
98
99 // Notify ntuple description that file is not empty
100 if (ntuple->entries() != 0u) {
101 auto ntupleDescription = fNtupleDescriptionVector.at(counter).first;
102 ntupleDescription->SetHasFill(true);
103 }
104 ++counter;
105 }
106
107 return true;
108}
109
110//_____________________________________________________________________________
112{
113 // The ntuples will be recreated with new cycle or new open file.
114 // Ntuple objects are deleted automatically when closing a file
115
116 fNtupleVector.clear();
117
118 return true;
119}
120
121//_____________________________________________________________________________
123{
124 fNtupleDescriptionVector.clear();
125 fNtupleVector.clear();
126
127 Message(G4Analysis::kVL2, "clear", "main ntuples");
128}
129
130//_____________________________________________________________________________
131std::shared_ptr<G4RootFile>
133{
134 auto perThread = false;
135 return fFileManager->GetNtupleFile(ntupleDescription, perThread, fFileNumber);
136}
137
138//
139// public functions
140//
141
142//_____________________________________________________________________________
144{
145// Create ntuples from booking.
146// This function is triggered from workers at new cycle.
147
148 for (auto [ntupleDescription, ntupleFile] : fNtupleDescriptionVector) {
149
150 // Get ntuple booking
151 const auto& ntupleBooking = ntupleDescription->GetNtupleBooking();
152
153 Message(kVL4, "create", "main ntuple", ntupleBooking.name());
154
155 // Create ntuple
156 auto ntuple = new tools::wroot::ntuple(*std::get<2>(*ntupleFile), ntupleBooking, fRowWise);
157 // ntuple object is deleted automatically when closing a file
158 auto basketSize = fNtupleBuilder->GetBasketSize();
159 ntuple->set_basket_size(basketSize);
160
161 fNtupleVector.push_back(ntuple);
162
163 Message(kVL3, "create", "main ntuple", ntupleBooking.name());
164 }
165
166 SetNewCycle(false);
167}
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
void Message(G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
std::shared_ptr< G4RootFile > GetNtupleFile(RootNtupleDescription *ntupleDescription) const
G4RootMainNtupleManager()=delete
void CreateNtuple(RootNtupleDescription *ntupleDescription, G4bool warn=true)
const tools::ntuple_booking & GetNtupleBooking() const
constexpr G4int kVL2
constexpr G4int kVL3
constexpr G4int kVL4
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)