Geant4 11.2.2
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// private functions
55//
56
57//_____________________________________________________________________________
58G4int G4RootMainNtupleManager::CreateNtupleFromBooking(
59 G4NtupleBooking* g4NtupleBooking, std::shared_ptr<G4RootFile> ntupleFile)
60{
61 // Do not create ntuple if it was deleted
62 if ( g4NtupleBooking->GetDeleted()) {
63 // G4cout << "Ntuple " << g4NtupleBooking->fNtupleId << " was deleted" << G4endl;
65 }
66
67 // The ntuple index
68 auto index = g4NtupleBooking->fNtupleId - fFirstId;
69
70 // Do not create ntuple if it already exists
71 if ( (index < G4int(fNtupleVector.size())) && (fNtupleVector[index] != nullptr) ) {
72 // G4cout << "Ntuple " << g4NtupleBooking->fNtupleId << " already exists" << G4endl;
74 }
75
76 // Get ntuple booking
77 const auto& ntupleBooking = g4NtupleBooking->fNtupleBooking;
78
79 Message(kVL4, "create", "main ntuple", ntupleBooking.name());
80
81 // Allocate the ntuple vector element(s) if needed
82 while ( index >= G4int(fNtupleVector.size()) ) {
83 fNtupleVector.push_back(nullptr);
84 }
85
86 // Create ntuple
87 auto ntuple = new tools::wroot::ntuple(*std::get<2>(*ntupleFile), ntupleBooking, fRowWise);
88 // ntuple object is deleted automatically when closing a file
89 auto basketSize = fNtupleBuilder->GetBasketSize();
90 ntuple->set_basket_size(basketSize);
91
92 // Save ntuple & ntuple description pair in vectors
93 fNtupleVector[index] = ntuple;
94
95 Message(kVL3, "create", "main ntuple", ntupleBooking.name());
96
97 return index;
98}
99
100//
101// protected functions
102//
103
104//_____________________________________________________________________________
106 G4bool warn)
107{
108// Create ntuple from booking if file was open
109
110 // Get/Create main ntuple file
111 auto ntupleFile = fFileManager->CreateNtupleFile(ntupleDescription, fFileNumber);
112 if ( ntupleFile == nullptr ) {
113 if ( warn ) {
114 Warn("Ntuple file must be defined first.\n"
115 "Cannot create main ntuple.",
116 fkClass, "CreateNtuple");
117 }
118 return;
119 }
120
121 // Create ntuple from g4 booking
122 auto g4NtupleBooking = ntupleDescription->GetG4NtupleBooking();
123 auto index = CreateNtupleFromBooking(g4NtupleBooking, ntupleFile);
124
125 // Return if ntuple was not created
126 if (index == G4Analysis::kInvalidId) return;
127
128 // Allocate the ntuple description pair vector element(s) if needed
129 while ( index >= G4int(fNtupleDescriptionVector.size()) ) {
130 fNtupleDescriptionVector.push_back(std::make_pair(nullptr, nullptr));
131 }
132
133 // Save ntuple description pair in vectors
134 fNtupleDescriptionVector[index] = std::make_pair(ntupleDescription, ntupleFile);
135}
136
137//_____________________________________________________________________________
139{
140 if (fNtupleVector.empty()) {
141 // Main ntuples are delete with each new cycle
142 return true;
143 }
144
145 // Proceed with deleting if vector is not empty
146
147 Message(kVL4, "delete", "main ntuple ntupleId: " + to_string(id));
148
149 // Get ntuple description
150 auto index = id - fFirstId;
151 if ( index < 0 || index >= G4int(fNtupleVector.size()) ) {
152 G4Analysis::Warn("Main ntuple " + to_string(id) + " does not exist.",
153 fkClass, "Delete");
154 return false;
155 }
156
157 // Delete main ntuple and update ntuple vector
158 delete fNtupleVector[index];
159 fNtupleVector[index] = nullptr;
160
161 Message(kVL3, "delete", "main ntuple ntupleId: " + to_string(id));
162
163 return true;
164}
165
166//_____________________________________________________________________________
168{
169 std::size_t counter = 0;
170
171 for ( auto ntuple : fNtupleVector ) {
172 // skip deleted ntuples
173 if (ntuple == nullptr) continue;
174
175 ntuple->merge_number_of_entries();
176
177 // Notify ntuple description that file is not empty
178 if (ntuple->entries() != 0u) {
179 auto ntupleDescription = fNtupleDescriptionVector.at(counter).first;
180 ntupleDescription->SetHasFill(true);
181 }
182 ++counter;
183 }
184
185 return true;
186}
187
188//_____________________________________________________________________________
190{
191 // The ntuples will be recreated with new cycle or new open file.
192 // Ntuple objects are deleted automatically when closing a file
193
194 fNtupleVector.clear();
195
196 return true;
197}
198
199//_____________________________________________________________________________
201{
202 fNtupleDescriptionVector.clear();
203 fNtupleVector.clear();
204
205 Message(G4Analysis::kVL2, "clear", "main ntuples");
206}
207
208//_____________________________________________________________________________
209std::shared_ptr<G4RootFile>
211{
212 auto perThread = false;
213 return fFileManager->GetNtupleFile(ntupleDescription, perThread, fFileNumber);
214}
215
216//
217// public functions
218//
219
220//_____________________________________________________________________________
222{
223// Create ntuples from booking (without creating ntuple description)
224// This function is triggered from workers at new cycle.
225
226 for (auto [ntupleDescription, ntupleFile] : fNtupleDescriptionVector) {
227 CreateNtupleFromBooking(ntupleDescription->GetG4NtupleBooking(), ntupleFile);
228 }
229
230 SetNewCycle(false);
231}
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)
G4NtupleBooking * GetG4NtupleBooking() const
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)
tools::ntuple_booking fNtupleBooking