Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4RootPNtupleManager.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$
27 //
28 // Author: Ivana Hrivnacova, 04/10/2016 (ivana@ipno.in2p3.fr)
29 
30 #include "G4RootPNtupleManager.hh"
32 #include "G4AnalysisUtilities.hh"
33 
34 #include "tools/wroot/file"
35 #include "tools/wroot/ntuple"
36 
37 // mutex in a file scope
38 namespace {
39  //Mutex to lock master manager when adding ntuple row
40  G4Mutex addRowMutex = G4MUTEX_INITIALIZER;
41  G4Mutex endFillMutex = G4MUTEX_INITIALIZER;
42 }
43 
44 //_____________________________________________________________________________
46  const G4AnalysisManagerState& state)
47  : G4VNtupleManager(state),
48  fCreateMode(G4PNtupleCreateMode::kUndefined),
49  fMainNtupleManager(main),
50  fNtupleVector()
51 {}
52 
53 //_____________________________________________________________________________
55 {
56  for ( auto ntupleDescription : fNtupleDescriptionVector ) {
57  delete ntupleDescription;
58  }
59 }
60 
61 //
62 // private functions
63 //
64 
65 //_____________________________________________________________________________
67 G4RootPNtupleManager::GetNtupleDescriptionInFunction(
68  G4int id, G4String functionName, G4bool warn) const
69 {
70  auto index = id - fFirstId;
71  if ( index < 0 || index >= G4int(fNtupleDescriptionVector.size()) ) {
72  if ( warn) {
73  G4String inFunction = "G4RootPNtupleManager::";
74  inFunction += functionName;
75  G4ExceptionDescription description;
76  description << " " << "ntuple " << id << " does not exist.";
77  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
78  }
79  return nullptr;
80  }
81 
82  return fNtupleDescriptionVector[index];
83 }
84 
85 //_____________________________________________________________________________
86 tools::wroot::pntuple* G4RootPNtupleManager::GetNtupleInFunction(
87  G4int id, G4String functionName, G4bool warn) const
88 {
89  auto ntupleDescription = GetNtupleDescriptionInFunction(id, functionName);
90  if ( ! ntupleDescription ) return nullptr;
91 
92  if ( ! ntupleDescription->fNtuple ) {
93  if ( warn ) {
94  G4String inFunction = "G4RootPNtupleManager::";
95  inFunction += functionName;
96  G4ExceptionDescription description;
97  description << " " << "ntupleId " << id << " does not exist.";
98  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
99  }
100  return nullptr;
101  }
102  return ntupleDescription->fNtuple;
103 }
104 
105 //_____________________________________________________________________________
106 tools::wroot::ntuple*
107 G4RootPNtupleManager::GetMainNtupleInFunction(
108  G4int id, G4String functionName, G4bool warn) const
109 {
110  auto& mainNtupleVector
111  = fMainNtupleManager->GetNtupleVector();
112 
113  auto index = id - fFirstId;
114  if ( index < 0 || index >= G4int(mainNtupleVector.size()) ) {
115  if ( warn) {
116  G4String inFunction = "G4RootPNtupleManager::";
117  inFunction += functionName;
118  G4ExceptionDescription description;
119  description << " " << "main ntuple " << id << " does not exist.";
120  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
121  }
122  return nullptr;
123  }
124 
125  return mainNtupleVector[index];
126 }
127 
128 
129 //
130 // protected functions
131 //
132 
133 //_____________________________________________________________________________
134 void G4RootPNtupleManager::CreateNtuple(G4RootPNtupleDescription* ntupleDescription,
135  tools::wroot::ntuple* mainNtuple)
136 {
137 #ifdef G4VERBOSE
138  if ( fState.GetVerboseL4() )
140  ->Message("create from main", "pntuple", mainNtuple->name());
141 #endif
142 
143  auto rfile = fMainNtupleManager->GetNtupleFile();
144  auto directory = fMainNtupleManager->GetNtupleDirectory();
145 
146  // Get parameters from main ntuple
147  ntupleDescription->fFile = rfile.get();
148  mainNtuple->get_branches(ntupleDescription->fMainBranches);
149 
150  std::vector<tools::uint32> basketSizes;
151  tools_vforcit(tools::wroot::branch*, ntupleDescription->fMainBranches, it) {
152  basketSizes.push_back((*it)->basket_size());
153  }
154 
155  // Create pntuple
156  G4bool verbose = true;
157  ntupleDescription->fNtuple
158  = new tools::wroot::pntuple(
159  G4cout, rfile->byte_swap(), rfile->compression(),
160  directory->seek_directory(), basketSizes,
161  ntupleDescription->fNtupleBooking, verbose);
162 
163  ntupleDescription->fIsNtupleOwner = true;
164  // pntuple object is not deleted automatically
165  fNtupleVector.push_back(ntupleDescription->fNtuple);
166 
167 #ifdef G4VERBOSE
168  if ( fState.GetVerboseL3() )
170  ->Message("create from main", "pntuple", mainNtuple->name());
171 #endif
172 }
173 
174 //_____________________________________________________________________________
175 void G4RootPNtupleManager::CreateNtuplesFromMain()
176 {
177 // Create ntuple from booking (if not yet done) and main ntuple
178 // This function is called from G4AnalysisManager::OpenFile.
179 
180  if ( fCreateMode == G4PNtupleCreateMode::kUndefined ) {
181  if ( fNtupleDescriptionVector.size() ) {
183  } else {
185  }
186  }
187 
188  if ( fCreateMode == G4PNtupleCreateMode::kSlaveAfterOpen ) {
189  // ntuples are not yet booked
190  return;
191  }
192 
193  auto& mainNtupleVector
194  = fMainNtupleManager->GetNtupleVector();
195 
196  G4int lcounter = 0;
197  for ( auto mainNtuple : mainNtupleVector ) {
198 
199  auto& ntupleDescription
200  = fNtupleDescriptionVector[lcounter++];
201  CreateNtuple(ntupleDescription, mainNtuple);
202  }
203 }
204 
205 //_____________________________________________________________________________
206 G4int G4RootPNtupleManager::CreateNtuple(
207  const G4String& name, const G4String& title)
208 {
209 // Create pntuple description with ntuple_booking
210 
211 #ifdef G4VERBOSE
212  if ( fState.GetVerboseL4() )
213  fState.GetVerboseL4()->Message("create", "pntuple booking", name);
214 #endif
215 
216  // Set create mode if not yet defined
217  if ( fCreateMode == G4PNtupleCreateMode::kUndefined ) {
218  if ( fMainNtupleManager->GetNtupleFile() ) {
220  } else {
222  }
223  }
224 
225  // Create ntuple description
226  auto index = fNtupleDescriptionVector.size();
227  auto ntupleDescription = new G4RootPNtupleDescription();
228  fNtupleDescriptionVector.push_back(ntupleDescription);
229 
230  // Save name & title in ntuple booking
231  ntupleDescription->fNtupleBooking.set_name(name);
232  ntupleDescription->fNtupleBooking.set_title(title);
233 
234  fLockFirstId = true;
235 
236 #ifdef G4VERBOSE
237  if ( fState.GetVerboseL2() ) {
238  G4ExceptionDescription description;
239  description << name << " ntupleId " << index + fFirstId;
240  fState.GetVerboseL2()->Message("create", "pntuple booking", description);
241  }
242 #endif
243 
244  return index + fFirstId;
245 }
246 
247 //_____________________________________________________________________________
248 G4int G4RootPNtupleManager::CreateNtupleIColumn(
249  const G4String& name, std::vector<int>* vector)
250 {
251  return CreateNtupleTColumn<int>(name, vector);
252 }
253 
254 //_____________________________________________________________________________
255 G4int G4RootPNtupleManager::CreateNtupleFColumn(
256  const G4String& name, std::vector<float>* vector)
257 {
258  return CreateNtupleTColumn<float>(name, vector);
259 }
260 
261 //_____________________________________________________________________________
262 G4int G4RootPNtupleManager::CreateNtupleDColumn(
263  const G4String& name, std::vector<double>* vector)
264 {
265  return CreateNtupleTColumn<double>(name, vector);
266 }
267 
268 //_____________________________________________________________________________
269 G4int G4RootPNtupleManager::CreateNtupleSColumn(
270  const G4String& name)
271 {
272  return CreateNtupleTColumn<std::string>(name, nullptr);
273 }
274 
275 //_____________________________________________________________________________
276 void G4RootPNtupleManager::FinishNtuple()
277 {
278  auto ntupleId = fNtupleDescriptionVector.size() + fFirstId - 1;
279  FinishNtuple(ntupleId);
280 }
281 
282 //_____________________________________________________________________________
283 G4int G4RootPNtupleManager::CreateNtupleIColumn(
284  G4int ntupleId, const G4String& name, std::vector<int>* vector)
285 {
286  return CreateNtupleTColumn<int>(ntupleId, name, vector);
287 }
288 
289 //_____________________________________________________________________________
290 G4int G4RootPNtupleManager::CreateNtupleFColumn(
291  G4int ntupleId, const G4String& name, std::vector<float>* vector)
292 {
293  return CreateNtupleTColumn<float>(ntupleId, name, vector);
294 }
295 
296 
297 //_____________________________________________________________________________
298 G4int G4RootPNtupleManager::CreateNtupleDColumn(
299  G4int ntupleId, const G4String& name, std::vector<double>* vector)
300 {
301  return CreateNtupleTColumn<double>(ntupleId, name, vector);
302 }
303 
304 //_____________________________________________________________________________
305 G4int G4RootPNtupleManager::CreateNtupleSColumn(
306  G4int ntupleId, const G4String& name)
307 {
308  return CreateNtupleTColumn<std::string>(ntupleId, name, nullptr);
309 }
310 
311 //_____________________________________________________________________________
312 void G4RootPNtupleManager::FinishNtuple(G4int ntupleId)
313 {
314 // create ntuple if file was open
315 
316  // if ( fMainNtupleManager->GetNtupleFile() ) {
317  if ( fCreateMode == G4PNtupleCreateMode::kSlaveAfterOpen ) {
318  auto ntupleDescription
319  = GetNtupleDescriptionInFunction(ntupleId, "FinishNtuple");
320  if ( ! ntupleDescription ) return;
321 
322  auto mainNtuple = GetMainNtupleInFunction(ntupleId, "FinishNtuple");
323  if ( ! mainNtuple ) return;
324 
325  CreateNtuple(ntupleDescription, mainNtuple);
326  }
327 }
328 
329 //_____________________________________________________________________________
330 G4bool G4RootPNtupleManager::FillNtupleIColumn(
331  G4int columnId, G4int value)
332 {
333  return FillNtupleTColumn<int>(columnId, value);
334 }
335 
336 //_____________________________________________________________________________
337 G4bool G4RootPNtupleManager::FillNtupleFColumn(
338  G4int columnId, G4float value)
339 {
340  return FillNtupleTColumn<float>(columnId, value);
341 }
342 
343 //_____________________________________________________________________________
344 G4bool G4RootPNtupleManager::FillNtupleDColumn(
345  G4int columnId, G4double value)
346 {
347  return FillNtupleTColumn<double>(columnId, value);
348 }
349 
350 //_____________________________________________________________________________
351 G4bool G4RootPNtupleManager::FillNtupleSColumn(
352  G4int columnId, const G4String& value)
353 {
354  return FillNtupleTColumn<std::string>(columnId, value);
355 }
356 
357 //_____________________________________________________________________________
358 G4bool G4RootPNtupleManager::AddNtupleRow()
359 {
360  return AddNtupleRow(fFirstId);
361 }
362 
363 //_____________________________________________________________________________
364 G4bool G4RootPNtupleManager::FillNtupleIColumn(
365  G4int ntupleId, G4int columnId, G4int value)
366 {
367  return FillNtupleTColumn<int>(ntupleId, columnId, value);
368 }
369 
370 //_____________________________________________________________________________
371 G4bool G4RootPNtupleManager::FillNtupleFColumn(
372  G4int ntupleId, G4int columnId, G4float value)
373 {
374  return FillNtupleTColumn<float>(ntupleId, columnId, value);
375 }
376 
377 //_____________________________________________________________________________
378 G4bool G4RootPNtupleManager::FillNtupleDColumn(
379  G4int ntupleId, G4int columnId, G4double value)
380 {
381  return FillNtupleTColumn<double>(ntupleId, columnId, value);
382 }
383 
384 //_____________________________________________________________________________
385 G4bool G4RootPNtupleManager::FillNtupleSColumn(
386  G4int ntupleId, G4int columnId, const G4String& value)
387 {
388  return FillNtupleTColumn<std::string>(ntupleId, columnId, value);
389 }
390 
391 //_____________________________________________________________________________
392 G4bool G4RootPNtupleManager::AddNtupleRow(G4int ntupleId)
393 {
394  if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
395  //G4cout << "Skipping AddNtupleRow for " << ntupleId << G4endl;
396  return false;
397  }
398 
399 #ifdef G4VERBOSE
400  if ( fState.GetVerboseL4() ) {
401  G4ExceptionDescription description;
402  description << " ntupleId " << ntupleId;
403  fState.GetVerboseL4()->Message("add", "pntuple row", description);
404  }
405 #endif
406 
407  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "AddNtupleRow");
408  if ( ! ntupleDescription ) return false;
409 
410  G4AutoLock lock(&addRowMutex);
411  mutex toolsLock(lock);
412  auto result
413  = ntupleDescription->fNtuple
414  ->add_row(toolsLock, *ntupleDescription->fFile, ntupleDescription->fMainBranches);
415 
416  if ( ! result ) {
417  G4ExceptionDescription description;
418  description << " " << " ntupleId " << ntupleId
419  << "adding row has failed.";
420  G4Exception("G4RootPNtupleManager::AddNtupleRow()",
421  "Analysis_W002", JustWarning, description);
422  }
423 
424 #ifdef G4VERBOSE
425  if ( fState.GetVerboseL3() ) {
426  G4ExceptionDescription description;
427  description << " ntupleId " << ntupleId;
428  fState.GetVerboseL3()->Message("add", "pntuple row", description);
429  }
430 #endif
431 
432  return true;
433 }
434 
435 //_____________________________________________________________________________
436 G4bool G4RootPNtupleManager::Merge()
437 {
438  for ( auto ntupleDescription : fNtupleDescriptionVector) {
439 
440  // skip inactivated ntuples
441  if ( ! ntupleDescription->fActivation ) continue;
442 
443 #ifdef G4VERBOSE
444  if ( fState.GetVerboseL4() ) {
446  ->Message("merge", "pntuple", ntupleDescription->fNtupleBooking.name());
447  }
448 #endif
449 
450  G4AutoLock lock(&endFillMutex);
451  mutex toolsLock(lock);
452  auto result
453  = ntupleDescription->fNtuple
454  ->end_fill(toolsLock, *ntupleDescription->fFile, ntupleDescription->fMainBranches);
455 
456  if ( ! result ) {
457  G4ExceptionDescription description;
458  description << " " << " ntuple " << ntupleDescription->fNtupleBooking.name()
459  << "end fill has failed.";
460  G4Exception("G4RootPNtupleManager::Merge()",
461  "Analysis_W002", JustWarning, description);
462  }
463 
464  delete ntupleDescription->fNtuple;
465  ntupleDescription->fNtuple = nullptr;
466 
467 #ifdef G4VERBOSE
468  if ( fState.GetVerboseL3() ) {
470  ->Message("merge", "pntuple", ntupleDescription->fNtupleBooking.name());
471  }
472 #endif
473  }
474  return true;
475 
476 }
477 
478 //_____________________________________________________________________________
479 G4bool G4RootPNtupleManager::Reset(G4bool deleteNtuple)
480 {
481  for ( auto ntupleDescription : fNtupleDescriptionVector ) {
482  if ( deleteNtuple ) {
483  delete ntupleDescription->fNtuple;
484  }
485  ntupleDescription->fNtuple = nullptr;
486  }
487 
488  fNtupleVector.clear();
489 
490  return true;
491 }
492 
493 //_____________________________________________________________________________
494 
495 void G4RootPNtupleManager::SetActivation(
496  G4bool activation)
497 {
498  for ( auto ntupleDescription : fNtupleDescriptionVector ) {
499  ntupleDescription->fActivation = activation;
500  }
501 }
502 
503 //_____________________________________________________________________________
504 
505 void G4RootPNtupleManager::SetActivation(
506  G4int ntupleId, G4bool activation)
507 {
508  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetActivation");
509  if ( ! ntupleDescription ) return;
510 
511  ntupleDescription->fActivation = activation;
512 }
513 
514 //_____________________________________________________________________________
515 G4bool G4RootPNtupleManager::GetActivation(
516  G4int ntupleId) const
517 {
518  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetActivation");
519  if ( ! ntupleDescription ) return false;
520 
521  return ntupleDescription->fActivation;
522 }
523 
524 //_____________________________________________________________________________
525 G4int G4RootPNtupleManager::GetNofNtuples() const
526 {
527  return fNtupleVector.size();
528 }
529 
530 //_____________________________________________________________________________
531 G4bool G4RootPNtupleManager::IsEmpty() const
532 {
533  return ! fNtupleDescriptionVector.size();
534 }
535 
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
std::shared_ptr< tools::wroot::file > GetNtupleFile() const
float G4float
Definition: G4Types.hh:77
std::vector< tools::wroot::branch * > fMainBranches
int main(int argc, char **argv)
Definition: genwindef.cpp:30
int G4int
Definition: G4Types.hh:78
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
const G4AnalysisVerbose * GetVerboseL2() const
const std::vector< tools::wroot::ntuple * > & GetNtupleVector()
const G4AnalysisVerbose * GetVerboseL3() const
G4GLOB_DLL std::ostream G4cout
const XML_Char int const XML_Char * value
Definition: expat.h:331
const G4AnalysisVerbose * GetVerboseL4() const
bool G4bool
Definition: G4Types.hh:79
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
tools::ntuple_booking fNtupleBooking
G4int G4Mutex
Definition: G4Threading.hh:173
tools::wroot::directory * GetNtupleDirectory() const
G4RootPNtupleManager(G4RootMainNtupleManager *main, const G4AnalysisManagerState &state)
double G4double
Definition: G4Types.hh:76
tools::wroot::pntuple * fNtuple
subroutine title
Definition: hijing1.383.f:5980
const G4AnalysisManagerState & fState