Geant4-11
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 (ivana@ipno.in2p3.fr)
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//_____________________________________________________________________________
67 RootNtupleDescription* ntupleDescription)
68{
69 if ( ! fMainNtupleManagers.size() ) {
70 // No merging
71 if ( ntupleDescription->fNtuple ) {
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->fNtuple
87 = new tools::wroot::ntuple(
88 *directory, ntupleDescription->fNtupleBooking, fRowWise);
89
90 auto basketSize = fFileManager->GetBasketSize();
91 ntupleDescription->fNtuple->set_basket_size(basketSize);
92
93 ntupleDescription->fIsNtupleOwner = false;
94 // ntuple object is deleted automatically when closing a file
95 fNtupleVector.push_back(ntupleDescription->fNtuple);
96 }
97 else {
98 // Merging activated
99 for ( const auto& manager : fMainNtupleManagers ) {
100 manager->CreateNtuple(ntupleDescription);
101 }
102 }
103}
104
105//_____________________________________________________________________________
107 RootNtupleDescription* /*ntupleDescription*/, G4bool /*fromBooking*/)
108{
109 // nothing to be done
110}
111
112//_____________________________________________________________________________
114{
116 // this will clear ntuple vector
117
118 auto result = true;
119
120 for ( const auto& manager : fMainNtupleManagers ) {
121 result &= manager->Reset();
122 }
123
124 return result;
125}
126
127//_____________________________________________________________________________
129{
131 // this will clear ntuple vector
132
133 for ( const auto& manager : fMainNtupleManagers ) {
134 manager->ClearData();
135 }
136}
137
138//_____________________________________________________________________________
140{
141 auto result = true;
142
143 for ( const auto& manager : fMainNtupleManagers ) {
144 result &= manager->Merge();
145 }
146
147 return result;
148}
149
150//_____________________________________________________________________________
152 const std::shared_ptr<G4RootFileManager>& fileManager)
153{
154 fFileManager = fileManager;
155
156 for ( const auto& manager : fMainNtupleManagers ) {
157 manager->SetFileManager(fileManager);
158 }
159}
160
161//_____________________________________________________________________________
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>
177{
178 if ( index < 0 || index >= G4int(fMainNtupleManagers.size()) ) {
179 Warn("main ntuple manager " + to_string(index) + " does not exist.",
180 fkClass, "GetMainNtupleManager");
181 return nullptr;
182 }
183
184 return fMainNtupleManagers[index];
185}
186
187//_____________________________________________________________________________
189{
190 if ( ! fFileManager ) {
191 Warn("File manager must be defined first.", fkClass, "GetBasketSize");
192 return 0;
193 }
194
195 return fFileManager->GetBasketSize();
196}
197
198//_____________________________________________________________________________
200{
201 if ( ! fFileManager ) {
202 Warn("File manager must be defined first.", fkClass, "GetBasketEntries");
203 return 0;
204 }
205
206 return fFileManager->GetBasketEntries();
207}
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
std::shared_ptr< G4RootMainNtupleManager > GetMainNtupleManager(G4int index) const
void SetNtupleRowWise(G4bool rowWise, G4bool rowMode)
virtual G4bool Reset() final
virtual void FinishTNtuple(RootNtupleDescription *ntupleDescription, G4bool fromBooking) final
static constexpr std::string_view fkClass
G4RootNtupleManager()=delete
unsigned int GetBasketSize() const
unsigned int GetBasketEntries() const
virtual void Clear() final
std::vector< std::shared_ptr< G4RootMainNtupleManager > > fMainNtupleManagers
void SetFileManager(const std::shared_ptr< G4RootFileManager > &fileManager)
std::shared_ptr< G4RootFileManager > fFileManager
virtual void CreateTNtupleFromBooking(RootNtupleDescription *ntupleDescription) final
virtual G4bool Reset()
std::vector< tools::wroot::ntuple * > fNtupleVector
virtual void Clear() override
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)
tools::ntuple_booking fNtupleBooking