Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 // $Id: G4RootNtupleManager.cc 70604 2013-06-03 11:27:06Z ihrivnac $
27 
28 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
29 
30 #include "G4RootNtupleManager.hh"
32 #include "G4RootFileManager.hh"
34 #include "G4AnalysisUtilities.hh"
35 
36 #include "tools/wroot/file"
37 
38 using namespace G4Analysis;
39 
40 //_____________________________________________________________________________
42  G4int nofMainManagers)
43  : G4TNtupleManager<tools::wroot::ntuple>(state),
44  fCreateMode(G4NtupleCreateMode::kUndefined),
45  fFileManager(nullptr),
46  fNtupleDirectory(nullptr),
47  fMainNtupleManagers()
48 {
49  for ( G4int i=0; i<nofMainManagers; ++i) {
50  fMainNtupleManagers.push_back(
51  new G4RootMainNtupleManager(this, fState));
52  }
53 }
54 
55 //_____________________________________________________________________________
57 {}
58 
59 //
60 // private methods
61 //
62 
63 //_____________________________________________________________________________
64 void G4RootNtupleManager::SetCreateMode()
65 {
66 // Set create mode if not yet defined
67 
68 #ifdef G4VERBOSE
69  if ( fState.GetVerboseL4() )
71  ->Message("set", "ntuple create mode", "");
72 #endif
73 
74  G4String createMode;
75  if ( fCreateMode == G4NtupleCreateMode::kUndefined ) {
76  if ( fMainNtupleManagers.size() ) {
77  if ( fFileManager->GetNtupleFile(0) ) {
79  createMode = "G4NtupleCreateMode::kMainAfterOpen";
80  } else {
82  createMode = "G4NtupleCreateMode::kMainBeforeOpen";
83  }
84  }
85  else {
86  if ( fNtupleDirectory ) {
88  createMode = "G4NtupleCreateMode::kNoMergeAfterOpen";
89  } else {
91  createMode = "G4NtupleCreateMode::kNoMergeBeforeOpen";
92  }
93  }
94  }
95 
96 #ifdef G4VERBOSE
97  if ( fState.GetVerboseL2() )
99  ->Message("set", "ntuple create mode", createMode);
100 #endif
101 }
102 
103 //_____________________________________________________________________________
104 void G4RootNtupleManager::CreateTNtuple(
106  const G4String& name, const G4String& title)
107 {
108  // Set create mode if not yet defined
109  SetCreateMode();
110 
111  if ( fCreateMode == G4NtupleCreateMode::kNoMergeAfterOpen ) {
112  if ( ! fNtupleDirectory ) {
113  G4String inFunction = "G4RootNtupleManager::::CreateTNtuple";
114  G4ExceptionDescription description;
115  description << " "
116  << "Cannot create ntuple. Ntuple directory does not exist." << G4endl;
117  G4Exception(inFunction, "Analysis_W002", JustWarning, description);
118  return;
119  }
120 
121  ntupleDescription->fNtuple
122  = new tools::wroot::ntuple(*fNtupleDirectory, name, title);
123  ntupleDescription->fIsNtupleOwner = false;
124  // ntuple object is deleted automatically when closing a file
125  fNtupleVector.push_back(ntupleDescription->fNtuple);
126  }
127 }
128 
129 //_____________________________________________________________________________
130 void G4RootNtupleManager::CreateTNtupleFromBooking(
132 {
133  if ( fCreateMode == G4NtupleCreateMode::kNoMergeBeforeOpen ) {
134 
135  if ( ! fNtupleDirectory ) {
136  G4String inFunction = "G4RootNtupleManager::::CreateTNtuple";
137  G4ExceptionDescription description;
138  description << " "
139  << "Cannot create ntuple. Ntuple directory does not exist." << G4endl;
140  G4Exception(inFunction, "Analysis_W002", JustWarning, description);
141  return;
142  }
143 
144  ntupleDescription->fNtuple
145  = new tools::wroot::ntuple(
146  *fNtupleDirectory, ntupleDescription->fNtupleBooking);
147 
148  auto basketSize = fFileManager->GetBasketSize();
149  ntupleDescription->fNtuple->set_basket_size(basketSize);
150 
151  ntupleDescription->fIsNtupleOwner = false;
152  // ntuple object is deleted automatically when closing a file
153  fNtupleVector.push_back(ntupleDescription->fNtuple);
154  }
155 
156  if ( fCreateMode == G4NtupleCreateMode::kMainBeforeOpen ) {
157  auto counter = 0;
158  for ( auto manager : fMainNtupleManagers ) {
159  if ( ! manager->GetNtupleVector().size() ) {
160  // Create only once !!
161  manager->SetNtupleFile(fFileManager->GetNtupleFile(counter));
162  manager->SetNtupleDirectory(fFileManager->GetMainNtupleDirectory(counter++));
163  manager->CreateNtuplesFromBooking();
164  }
165  }
166  }
167 }
168 
169 //_____________________________________________________________________________
170 void G4RootNtupleManager::FinishTNtuple(
172 {
173 // Create main ntuples
174 
175  if ( fCreateMode == G4NtupleCreateMode::kMainAfterOpen ) {
176  auto counter = 0;
177  for ( auto manager : fMainNtupleManagers ) {
178  auto warn = true;
179  manager->SetNtupleFile(fFileManager->GetNtupleFile(counter));
180  manager->SetNtupleDirectory(fFileManager->GetMainNtupleDirectory(counter++));
181  manager->CreateNtuple(ntupleDescription->fNtupleBooking, warn);
182  }
183  }
184 }
185 
186 //_____________________________________________________________________________
187 G4bool G4RootNtupleManager::Reset(G4bool deleteNtuple)
188 {
190  // this will clear ntuple vector
191 
192  if ( fCreateMode == G4NtupleCreateMode::kNoMergeAfterOpen ) {
193  // clear also ntuple description vector
194  fNtupleDescriptionVector.clear();
195  }
196 
197  auto finalResult = true;
198  for ( auto manager : fMainNtupleManagers ) {
199  auto result = manager->Reset(false);
200  finalResult = result && finalResult;
201  }
202 
203  return finalResult;
204 }
205 
206 //_____________________________________________________________________________
207 G4bool G4RootNtupleManager::Merge()
208 {
209  auto finalResult = true;
210 
211  for ( auto manager : fMainNtupleManagers ) {
212  auto result = manager->Merge();
213  finalResult = result && finalResult;
214  }
215 
216  return finalResult;
217 }
218 
219 //_____________________________________________________________________________
220 G4RootMainNtupleManager* G4RootNtupleManager::GetMainNtupleManager(G4int index) const
221 {
222  if ( index < 0 || index >= G4int(fMainNtupleManagers.size()) ) {
223  G4String inFunction = "G4RootNtupleManager::::GetMainNtupleManager";
224  G4ExceptionDescription description;
225  description << " " << "main ntuple manager " << index << " does not exist.";
226  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
227  return nullptr;
228  }
229 
230  return fMainNtupleManagers[index];
231 }
232 
233 //_____________________________________________________________________________
234 unsigned int G4RootNtupleManager::GetBasketSize() const
235 {
236  if ( ! fFileManager ) {
237  G4String inFunction = "G4RootNtupleManager::::GetBasketSize";
238  G4ExceptionDescription description;
239  description << " " << "File manager must be defined first.";
240  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
241  return 0;
242  }
243 
244  return fFileManager->GetBasketSize();
245 }
246 
G4double G4ParticleHPJENDLHEData::G4double result
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
const XML_Char * name
Definition: expat.h:151
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4RootNtupleManager(const G4AnalysisManagerState &state, G4int nofMainManagers=0)
std::vector< G4TNtupleDescription< tools::wroot::ntuple > * > fNtupleDescriptionVector
int G4int
Definition: G4Types.hh:78
const G4AnalysisVerbose * GetVerboseL2() const
G4bool Reset(G4bool deleteNtuple)
const G4AnalysisVerbose * GetVerboseL4() const
bool G4bool
Definition: G4Types.hh:79
G4NtupleCreateMode
friend class G4RootMainNtupleManager
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
std::vector< tools::wroot::ntuple * > fNtupleVector
#define G4endl
Definition: G4ios.hh:61
tools::ntuple_booking fNtupleBooking
subroutine title
Definition: hijing1.383.f:5980
const G4AnalysisManagerState & fState