Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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  ntupleDescription->fNtuple
113  = new tools::wroot::ntuple(*fNtupleDirectory, name, title);
114  ntupleDescription->fIsNtupleOwner = false;
115  // ntuple object is deleted automatically when closing a file
116  fNtupleVector.push_back(ntupleDescription->fNtuple);
117  }
118 }
119 
120 //_____________________________________________________________________________
121 void G4RootNtupleManager::CreateTNtupleFromBooking(
123 {
124  if ( fCreateMode == G4NtupleCreateMode::kNoMergeBeforeOpen ) {
125  ntupleDescription->fNtuple
126  = new tools::wroot::ntuple(
127  *fNtupleDirectory, ntupleDescription->fNtupleBooking);
128 
129  auto basketSize = fFileManager->GetBasketSize();
130  ntupleDescription->fNtuple->set_basket_size(basketSize);
131 
132  ntupleDescription->fIsNtupleOwner = false;
133  // ntuple object is deleted automatically when closing a file
134  fNtupleVector.push_back(ntupleDescription->fNtuple);
135  }
136 
137  if ( fCreateMode == G4NtupleCreateMode::kMainBeforeOpen ) {
138  auto counter = 0;
139  for ( auto manager : fMainNtupleManagers ) {
140  if ( ! manager->GetNtupleVector().size() ) {
141  // Create only once !!
142  manager->SetNtupleFile(fFileManager->GetNtupleFile(counter));
143  manager->SetNtupleDirectory(fFileManager->GetMainNtupleDirectory(counter++));
144  manager->CreateNtuplesFromBooking();
145  }
146  }
147  }
148 }
149 
150 //_____________________________________________________________________________
151 void G4RootNtupleManager::FinishTNtuple(
153 {
154 // Create main ntuples
155 
156  if ( fCreateMode == G4NtupleCreateMode::kMainAfterOpen ) {
157  auto counter = 0;
158  for ( auto manager : fMainNtupleManagers ) {
159  auto warn = true;
160  manager->SetNtupleFile(fFileManager->GetNtupleFile(counter));
161  manager->SetNtupleDirectory(fFileManager->GetMainNtupleDirectory(counter++));
162  manager->CreateNtuple(ntupleDescription->fNtupleBooking, warn);
163  }
164  }
165 }
166 
167 //_____________________________________________________________________________
168 G4bool G4RootNtupleManager::Reset(G4bool deleteNtuple)
169 {
171  // this will clear ntuple vector
172 
173  if ( fCreateMode == G4NtupleCreateMode::kNoMergeAfterOpen ) {
174  // clear also ntuple description vector
175  fNtupleDescriptionVector.clear();
176  }
177 
178  auto finalResult = true;
179  for ( auto manager : fMainNtupleManagers ) {
180  auto result = manager->Reset(false);
181  finalResult = result && finalResult;
182  }
183 
184  return finalResult;
185 }
186 
187 //_____________________________________________________________________________
188 G4bool G4RootNtupleManager::Merge()
189 {
190  auto finalResult = true;
191 
192  for ( auto manager : fMainNtupleManagers ) {
193  auto result = manager->Merge();
194  finalResult = result && finalResult;
195  }
196 
197  return finalResult;
198 }
199 
200 //_____________________________________________________________________________
201 G4RootMainNtupleManager* G4RootNtupleManager::GetMainNtupleManager(G4int index) const
202 {
203  if ( index < 0 || index >= G4int(fMainNtupleManagers.size()) ) {
204  G4String inFunction = "G4RootNtupleManager::::GetMainNtupleManager";
205  G4ExceptionDescription description;
206  description << " " << "main ntuple manager " << index << " does not exist.";
207  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
208  return nullptr;
209  }
210 
211  return fMainNtupleManagers[index];
212 }
213 
214 //_____________________________________________________________________________
215 unsigned int G4RootNtupleManager::GetBasketSize() const
216 {
217  if ( ! fFileManager ) {
218  G4String inFunction = "G4RootNtupleManager::::GetBasketSize";
219  G4ExceptionDescription description;
220  description << " " << "File manager must be defined first.";
221  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
222  return 0;
223  }
224 
225  return fFileManager->GetBasketSize();
226 }
227 
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
tools::ntuple_booking fNtupleBooking
const G4AnalysisManagerState & fState