Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4RootNtupleManager.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, 18/06/2013 ([email protected])
28
31#include "G4RootFileManager.hh"
34
35#include "tools/wroot/file"
36
37using namespace G4Analysis;
38using std::to_string;
39
40//_____________________________________________________________________________
42 const std::shared_ptr<G4NtupleBookingManager>& bookingManger,
43 G4int nofMainManagers, G4int nofFiles,
44 G4bool rowWise, G4bool rowMode)
45 : G4TNtupleManager<tools::wroot::ntuple, G4RootFile>(state),
46 fRowWise(rowWise),
47 fRowMode(rowMode)
48{
49 for ( G4int i=0; i<nofMainManagers; ++i) {
50 auto fileNumber = i;
51 if ( (i == 0) && (nofFiles == 0) ) {
52 // the main ntuple file will be merged in the default file
53 fileNumber = -1;
54 }
55 fMainNtupleManagers.push_back(
56 std::make_shared<G4RootMainNtupleManager>(
57 this, bookingManger, rowWise, fileNumber, fState));
58 }
59}
60
61//
62// private methods
63//
64
65//_____________________________________________________________________________
66void G4RootNtupleManager::CreateTNtupleFromBooking(
67 RootNtupleDescription* ntupleDescription)
68{
69 if (fMainNtupleManagers.size() == 0u) {
70 // No merging
71 if (ntupleDescription->GetNtuple() != nullptr) {
72 Warn("Cannot create ntuple. Ntuple already exists.",
73 fkClass, "CreateTNtupleFromBooking");
74 return;
75 }
76
77 // Create ntuple file from ntuple description
78 auto ntupleFile = fFileManager->CreateNtupleFile(ntupleDescription);
79 if ( ! ntupleFile ) {
80 Warn("Cannot create ntuple. Ntuple file does not exist.",
81 fkClass, "CreateTNtupleFromBooking");
82 return;
83 }
84
85 auto directory = std::get<2>(*ntupleFile);
86 ntupleDescription->SetNtuple(
87 new tools::wroot::ntuple(
88 *directory, ntupleDescription->GetNtupleBooking(), fRowWise));
89
90 auto basketSize = fFileManager->GetBasketSize();
91 ntupleDescription->GetNtuple()->set_basket_size(basketSize);
92 ntupleDescription->SetIsNtupleOwner(false);
93 // ntuple object is deleted automatically when closing a file
94 }
95 else {
96 // Merging activated
97 for ( const auto& manager : fMainNtupleManagers ) {
98 manager->SetFirstId(fFirstId);
99 manager->CreateNtuple(ntupleDescription);
100 }
101 }
102}
103
104//_____________________________________________________________________________
105void G4RootNtupleManager::FinishTNtuple(
106 RootNtupleDescription* /*ntupleDescription*/, G4bool /*fromBooking*/)
107{
108 // nothing to be done
109}
110
111//_____________________________________________________________________________
112G4bool G4RootNtupleManager::Reset()
113{
115 // this will clear ntuple vector
116
117 auto result = true;
118
119 for ( const auto& manager : fMainNtupleManagers ) {
120 result &= manager->Reset();
121 }
122
123 return result;
124}
125
126//_____________________________________________________________________________
127void G4RootNtupleManager::Clear()
128{
130 // this will clear ntuple vector
131
132 for ( const auto& manager : fMainNtupleManagers ) {
133 manager->ClearData();
134 }
135}
136
137//_____________________________________________________________________________
138G4bool G4RootNtupleManager::Delete(G4int id)
139{
141
142 for ( const auto& manager : fMainNtupleManagers ) {
143 result &= manager->Delete(id);
144 }
145
146 return result;
147}
148
149//_____________________________________________________________________________
150G4bool G4RootNtupleManager::Merge()
151{
152 auto result = true;
153
154 for ( const auto& manager : fMainNtupleManagers ) {
155 result &= manager->Merge();
156 }
157
158 return result;
159}
160
161//_____________________________________________________________________________
162void G4RootNtupleManager::SetFileManager(
163 const std::shared_ptr<G4RootFileManager>& fileManager)
164{
165 fFileManager = fileManager;
166
167 for ( const auto& manager : fMainNtupleManagers ) {
168 manager->SetFileManager(fileManager);
169 }
170}
171
172//_____________________________________________________________________________
173void G4RootNtupleManager::SetNtupleRowWise(G4bool rowWise, G4bool rowMode)
174{
175// Set rowWise mode and propagate it to main ntuple managers
176
177 fRowWise = rowWise;
178 fRowMode = rowMode;
179
180 for (auto& mainNtupleManager : fMainNtupleManagers ) {
181 mainNtupleManager->SetRowWise(rowWise);
182 }
183}
184
185//_____________________________________________________________________________
186void G4RootNtupleManager::SetNewCycle(G4bool value)
187{
189
190 for ( const auto& manager : fMainNtupleManagers ) {
191 manager->SetNewCycle(value);
192 }
193}
194
195//_____________________________________________________________________________
196std::shared_ptr<G4RootMainNtupleManager>
197G4RootNtupleManager::GetMainNtupleManager(G4int index) const
198{
199 if ( index < 0 || index >= G4int(fMainNtupleManagers.size()) ) {
200 Warn("main ntuple manager " + to_string(index) + " does not exist.",
201 fkClass, "GetMainNtupleManager");
202 return nullptr;
203 }
204
205 return fMainNtupleManagers[index];
206}
207
208//_____________________________________________________________________________
209unsigned int G4RootNtupleManager::GetBasketSize() const
210{
211 if ( ! fFileManager ) {
212 Warn("File manager must be defined first.", fkClass, "GetBasketSize");
213 return 0;
214 }
215
216 return fFileManager->GetBasketSize();
217}
218
219//_____________________________________________________________________________
220unsigned int G4RootNtupleManager::GetBasketEntries() const
221{
222 if ( ! fFileManager ) {
223 Warn("File manager must be defined first.", fkClass, "GetBasketEntries");
224 return 0;
225 }
226
227 return fFileManager->GetBasketEntries();
228}
std::tuple< std::shared_ptr< tools::wroot::file >, tools::wroot::directory *, tools::wroot::directory * > G4RootFile
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
const G4AnalysisManagerState & fState
G4RootNtupleManager()=delete
const tools::ntuple_booking & GetNtupleBooking() const
void SetIsNtupleOwner(G4bool isNtupleOwner)
void SetNewCycle(G4bool value) override
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)