Geant4 10.7.0
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;
38
39//_____________________________________________________________________________
41 std::shared_ptr<G4NtupleBookingManager> bookingManger,
42 G4int nofMainManagers, G4int nofFiles,
43 G4bool rowWise, G4bool rowMode)
44 : G4TNtupleManager<tools::wroot::ntuple, G4RootFile>(state),
45 fFileManager(nullptr),
46 fMainNtupleManagers(),
47 fNtupleFile(nullptr),
48 fRowWise(rowWise),
49 fRowMode(rowMode)
50{
51 for ( G4int i=0; i<nofMainManagers; ++i) {
52 auto fileNumber = i;
53 if ( (i == 0) && (nofFiles == 0) ) {
54 // the main ntuple file will be merged in the default file
55 fileNumber = -1;
56 }
57 fMainNtupleManagers.push_back(
58 std::make_shared<G4RootMainNtupleManager>(
59 this, bookingManger.get(), rowWise, fileNumber, fState));
60 }
61}
62
63//_____________________________________________________________________________
65{}
66
67//
68// private methods
69//
70
71//_____________________________________________________________________________
72void G4RootNtupleManager::CreateTNtupleFromBooking(
73 RootNtupleDescription* ntupleDescription)
74{
75 if ( ! fMainNtupleManagers.size() ) {
76 // No merging
77 if ( ntupleDescription->fNtuple ) {
78 G4String inFunction = "G4RootNtupleManager::::CreateTNtupleFromBooking";
79 G4ExceptionDescription description;
80 description << "Cannot create ntuple. Ntuple already exists." << G4endl;
81 G4Exception(inFunction, "Analysis_W002", JustWarning, description);
82 return;
83 }
84
85 // Create ntuple file from ntuple description
86 auto ntupleFile = fFileManager->CreateNtupleFile(ntupleDescription);
87 if ( ! ntupleFile ) {
88 G4String inFunction = "G4RootNtupleManager::::CreateTNtupleFromBooking";
89 G4ExceptionDescription description;
90 description << "Cannot create ntuple. Ntuple file does not exist." << G4endl;
91 G4Exception(inFunction, "Analysis_W002", JustWarning, description);
92 return;
93 }
94
95 auto directory = std::get<2>(*ntupleFile);
96 ntupleDescription->fNtuple
97 = new tools::wroot::ntuple(
98 *directory, ntupleDescription->fNtupleBooking, fRowWise);
99
100 auto basketSize = fFileManager->GetBasketSize();
101 ntupleDescription->fNtuple->set_basket_size(basketSize);
102
103 ntupleDescription->fIsNtupleOwner = false;
104 // ntuple object is deleted automatically when closing a file
105 fNtupleVector.push_back(ntupleDescription->fNtuple);
106 }
107 else {
108 // Merging activated
109 for ( auto manager : fMainNtupleManagers ) {
110 manager->CreateNtuple(ntupleDescription);
111 }
112 }
113}
114
115//_____________________________________________________________________________
116void G4RootNtupleManager::FinishTNtuple(
117 RootNtupleDescription* /*ntupleDescription*/, G4bool /*fromBooking*/)
118{
119 // nothing to be done
120}
121
122//_____________________________________________________________________________
123G4bool G4RootNtupleManager::Reset(G4bool deleteNtuple)
124{
126 // this will clear ntuple vector
127
128 auto finalResult = true;
129
130 for ( auto manager : fMainNtupleManagers ) {
131 auto result = manager->Reset(false);
132 finalResult = result && finalResult;
133 }
134
135 return finalResult;
136}
137
138//_____________________________________________________________________________
139G4bool G4RootNtupleManager::Merge()
140{
141 auto finalResult = true;
142
143 for ( auto manager : fMainNtupleManagers ) {
144 auto result = manager->Merge();
145 finalResult = result && finalResult;
146 }
147
148 return finalResult;
149}
150
151//_____________________________________________________________________________
152void G4RootNtupleManager::SetFileManager(std::shared_ptr<G4RootFileManager> fileManager)
153{
154 fFileManager = fileManager;
155
156 for ( auto mainNtupleManager : fMainNtupleManagers) {
157 mainNtupleManager->SetFileManager(fileManager);
158 }
159}
160
161//_____________________________________________________________________________
162void G4RootNtupleManager::SetNtupleRowWise(G4bool rowWise, G4bool rowMode)
163{
164// Set rowWise mode and propagate it to main ntuple managers
165
166 fRowWise = rowWise;
167 fRowMode = rowMode;
168
169 for (auto& mainNtupleManager : fMainNtupleManagers ) {
170 mainNtupleManager->SetRowWise(rowWise);
171 }
172}
173
174//_____________________________________________________________________________
175std::shared_ptr<G4RootMainNtupleManager>
176G4RootNtupleManager::GetMainNtupleManager(G4int index) const
177{
178 if ( index < 0 || index >= G4int(fMainNtupleManagers.size()) ) {
179 G4String inFunction = "G4RootNtupleManager::::GetMainNtupleManager";
180 G4ExceptionDescription description;
181 description << " " << "main ntuple manager " << index << " does not exist.";
182 G4Exception(inFunction, "Analysis_W011", JustWarning, description);
183 return nullptr;
184 }
185
186 return fMainNtupleManagers[index];
187}
188
189//_____________________________________________________________________________
190unsigned int G4RootNtupleManager::GetBasketSize() const
191{
192 if ( ! fFileManager ) {
193 G4String inFunction = "G4RootNtupleManager::::GetBasketSize";
194 G4ExceptionDescription description;
195 description << " " << "File manager must be defined first.";
196 G4Exception(inFunction, "Analysis_W011", JustWarning, description);
197 return 0;
198 }
199
200 return fFileManager->GetBasketSize();
201}
202
203//_____________________________________________________________________________
204unsigned int G4RootNtupleManager::GetBasketEntries() const
205{
206 if ( ! fFileManager ) {
207 G4String inFunction = "G4RootNtupleManager::::GetBasketEntries";
208 G4ExceptionDescription description;
209 description << " " << "File manager must be defined first.";
210 G4Exception(inFunction, "Analysis_W011", JustWarning, description);
211 return 0;
212 }
213
214 return fFileManager->GetBasketEntries();
215}
@ 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
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
#define G4endl
Definition: G4ios.hh:57
const G4AnalysisManagerState & fState
G4RootNtupleManager(const G4AnalysisManagerState &state, std::shared_ptr< G4NtupleBookingManager > bookingManger, G4int nofMainManagers, G4int nofReducedFiles, G4bool rowWise, G4bool rowMode)
std::vector< tools::wroot::ntuple * > fNtupleVector
tools::ntuple_booking fNtupleBooking