ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4RootNtupleManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4RootNtupleManager.cc
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 
29 #include "G4RootNtupleManager.hh"
31 #include "G4RootFileManager.hh"
33 #include "G4AnalysisUtilities.hh"
34 
35 #include "tools/wroot/file"
36 
37 using namespace G4Analysis;
38 
39 //_____________________________________________________________________________
41  G4int nofMainManagers,
42  G4bool rowWise, G4bool rowMode)
43  : G4TNtupleManager<tools::wroot::ntuple>(state),
44  fCreateMode(G4NtupleCreateMode::kUndefined),
45  fFileManager(nullptr),
46  fNtupleDirectory(nullptr),
47  fMainNtupleManagers(),
48  fRowWise(rowWise),
49  fRowMode(rowMode)
50 {
51  for ( G4int i=0; i<nofMainManagers; ++i) {
52  fMainNtupleManagers.push_back(
53  new G4RootMainNtupleManager(this, rowWise, fState));
54  }
55 }
56 
57 //_____________________________________________________________________________
59 {}
60 
61 //
62 // private methods
63 //
64 
65 //_____________________________________________________________________________
67 {
68 // Set create mode if not yet defined, or reset it if ntuples are defined both
69 // before and after open file
70 
71 #ifdef G4VERBOSE
72  if ( fState.GetVerboseL4() )
74  ->Message("set", "ntuple create mode", "");
75 #endif
76 
77  G4String createMode;
79  if ( fMainNtupleManagers.size() ) {
80  if ( fFileManager->GetNtupleFile(0) ) {
81  fCreateMode = G4NtupleCreateMode::kMainAfterOpen;
82  createMode = "G4NtupleCreateMode::kMainAfterOpen";
83  } else {
84  fCreateMode = G4NtupleCreateMode::kMainBeforeOpen;
85  createMode = "G4NtupleCreateMode::kMainBeforeOpen";
86  }
87  }
88  else {
89  if ( fNtupleDirectory ) {
90  fCreateMode = G4NtupleCreateMode::kNoMergeAfterOpen;
91  createMode = "G4NtupleCreateMode::kNoMergeAfterOpen";
92  } else {
93  fCreateMode = G4NtupleCreateMode::kNoMergeBeforeOpen;
94  createMode = "G4NtupleCreateMode::kNoMergeBeforeOpen";
95  }
96  }
97  }
98 
99  if ( fCreateMode == G4NtupleCreateMode::kNoMergeBeforeOpen &&
100  fFileManager->GetNtupleFile(0) ) {
101  fCreateMode = G4NtupleCreateMode::kNoMergeAfterOpen;
102  createMode = "G4NtupleCreateMode::kNoMergeAfterOpen";
103  }
104 
105  if ( fCreateMode == G4NtupleCreateMode::kMainBeforeOpen &&
106  fFileManager->GetNtupleFile(0) ) {
107  fCreateMode = G4NtupleCreateMode::kMainAfterOpen;
108  createMode = "G4NtupleCreateMode::kMainAfterOpen";
109  }
110 
111 #ifdef G4VERBOSE
112  if ( fState.GetVerboseL2() )
114  ->Message("set", "ntuple create mode", createMode);
115 #endif
116 }
117 
118 //_____________________________________________________________________________
120 {
121 // Set rowWise mode and propagate it to main ntuple managers
122 
123  fRowWise = rowWise;
124  fRowMode = rowMode;
125 
126  for (auto& mainNtupleManager : fMainNtupleManagers ) {
127  mainNtupleManager->SetRowWise(rowWise);
128  }
129 }
130 
131 
132 //_____________________________________________________________________________
135 {
136  if ( ! fNtupleDirectory ) {
137  G4String inFunction = "G4RootNtupleManager::::CreateTNtuple";
138  G4ExceptionDescription description;
139  description << " "
140  << "Cannot create ntuple. Ntuple directory does not exist." << G4endl;
141  G4Exception(inFunction, "Analysis_W002", JustWarning, description);
142  return;
143  }
144 
145  ntupleDescription->fNtuple
146  = new tools::wroot::ntuple(
147  *fNtupleDirectory, ntupleDescription->fNtupleBooking, fRowWise);
148 
149  auto basketSize = fFileManager->GetBasketSize();
150  ntupleDescription->fNtuple->set_basket_size(basketSize);
151 
152  ntupleDescription->fIsNtupleOwner = false;
153  // ntuple object is deleted automatically when closing a file
154  fNtupleVector.push_back(ntupleDescription->fNtuple);
155 }
156 
157 //_____________________________________________________________________________
160 {
161  if ( fCreateMode == G4NtupleCreateMode::kNoMergeBeforeOpen ) {
162 
163  CreateTNtuple(ntupleDescription);
164  }
165 
166  if ( fCreateMode == G4NtupleCreateMode::kMainBeforeOpen ) {
167  auto counter = 0;
168  for ( auto manager : fMainNtupleManagers ) {
169  if ( ! manager->GetNtupleVector().size() ) {
170  // Create only once !!
171  manager->SetNtupleFile(fFileManager->GetNtupleFile(counter));
172  manager->SetNtupleDirectory(fFileManager->GetMainNtupleDirectory(counter++));
173  manager->CreateNtuplesFromBooking();
174  }
175  }
176  }
177 }
178 
179 //_____________________________________________________________________________
182  G4bool fromBooking)
183 {
184 // Create main ntuples
185 
186  // Set create mode if not yet defined
187  if ( ! fromBooking ) {
188  SetCreateMode();
189  }
190 
191  // Create ntuple if file is open
192  if ( fCreateMode == G4NtupleCreateMode::kNoMergeAfterOpen ) {
193  CreateTNtuple(ntupleDescription);
194  }
195 
196  // Create main ntuples if file is open
197  if ( fCreateMode == G4NtupleCreateMode::kMainAfterOpen ) {
198  auto counter = 0;
199  for ( auto manager : fMainNtupleManagers ) {
200  auto warn = true;
201  manager->SetNtupleFile(fFileManager->GetNtupleFile(counter));
202  manager->SetNtupleDirectory(fFileManager->GetMainNtupleDirectory(counter++));
203  manager->CreateNtuple(ntupleDescription->fNtupleBooking, warn);
204  }
205  }
206 }
207 
208 //_____________________________________________________________________________
210 {
212  // this will clear ntuple vector
213 
214  if ( fCreateMode == G4NtupleCreateMode::kNoMergeAfterOpen ) {
215  // clear also ntuple description vector
216  fNtupleDescriptionVector.clear();
217  }
218 
219  auto finalResult = true;
220  for ( auto manager : fMainNtupleManagers ) {
221  auto result = manager->Reset(false);
222  finalResult = result && finalResult;
223  }
224 
225  return finalResult;
226 }
227 
228 //_____________________________________________________________________________
230 {
231  auto finalResult = true;
232 
233  for ( auto manager : fMainNtupleManagers ) {
234  auto result = manager->Merge();
235  finalResult = result && finalResult;
236  }
237 
238  return finalResult;
239 }
240 
241 //_____________________________________________________________________________
243 {
244  if ( index < 0 || index >= G4int(fMainNtupleManagers.size()) ) {
245  G4String inFunction = "G4RootNtupleManager::::GetMainNtupleManager";
246  G4ExceptionDescription description;
247  description << " " << "main ntuple manager " << index << " does not exist.";
248  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
249  return nullptr;
250  }
251 
252  return fMainNtupleManagers[index];
253 }
254 
255 //_____________________________________________________________________________
257 {
258  if ( ! fFileManager ) {
259  G4String inFunction = "G4RootNtupleManager::::GetBasketSize";
260  G4ExceptionDescription description;
261  description << " " << "File manager must be defined first.";
262  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
263  return 0;
264  }
265 
266  return fFileManager->GetBasketSize();
267 }
268 
269 //_____________________________________________________________________________
271 {
272  if ( ! fFileManager ) {
273  G4String inFunction = "G4RootNtupleManager::::GetBasketEntries";
274  G4ExceptionDescription description;
275  description << " " << "File manager must be defined first.";
276  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
277  return 0;
278  }
279 
280  return fFileManager->GetBasketEntries();
281 }