ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4RootPNtupleManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4RootPNtupleManager.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, 04/10/2016 (ivana@ipno.in2p3.fr)
28 
29 #include "G4RootPNtupleManager.hh"
31 #include "G4AnalysisUtilities.hh"
32 
33 #include "tools/wroot/file"
34 #include "tools/wroot/ntuple"
35 
36 // mutex in a file scope
37 namespace {
38  //Mutex to lock master manager when adding ntuple row and ending fill
39  G4Mutex pntupleMutex = G4MUTEX_INITIALIZER;
40 }
41 
42 //_____________________________________________________________________________
44  const G4AnalysisManagerState& state,
45  G4bool rowWise, G4bool rowMode)
46  : G4BaseNtupleManager(state),
47  fCreateMode(G4PNtupleCreateMode::kUndefined),
48  fMainNtupleManager(main),
49  fNtupleVector(),
50  fRowWise(rowWise),
51  fRowMode(rowMode)
52 {}
53 
54 //_____________________________________________________________________________
56 {
57  for ( auto ntupleDescription : fNtupleDescriptionVector ) {
58  delete ntupleDescription;
59  }
60 }
61 
62 //
63 // private functions
64 //
65 
66 //_____________________________________________________________________________
69  G4int id, G4String functionName, G4bool warn) const
70 {
71  auto index = id - fFirstId;
72  if ( index < 0 || index >= G4int(fNtupleDescriptionVector.size()) ) {
73  if ( warn) {
74  G4String inFunction = "G4RootPNtupleManager::";
75  inFunction += functionName;
76  G4ExceptionDescription description;
77  description << " " << "ntuple " << id << " does not exist.";
78  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
79  }
80  return nullptr;
81  }
82 
83  return fNtupleDescriptionVector[index];
84 }
85 
86 //_____________________________________________________________________________
87 tools::wroot::base_pntuple*
89  G4int id, G4String functionName, G4bool warn) const
90 {
91  auto ntupleDescription = GetNtupleDescriptionInFunction(id, functionName);
92  if ( ! ntupleDescription ) return nullptr;
93 
94  if ( ! ntupleDescription->fBasePNtuple ) {
95  if ( warn ) {
96  G4String inFunction = "G4RootPNtupleManager::";
97  inFunction += functionName;
98  G4ExceptionDescription description;
99  description << " " << "ntupleId " << id << " does not exist.";
100  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
101  }
102  return nullptr;
103  }
104  return ntupleDescription->fBasePNtuple;
105 }
106 
107 //_____________________________________________________________________________
110  G4int id, G4String functionName, G4bool warn) const
111 {
112  auto& mainNtupleVector
114 
115  auto index = id - fFirstId;
116  if ( index < 0 || index >= G4int(mainNtupleVector.size()) ) {
117  if ( warn) {
118  G4String inFunction = "G4RootPNtupleManager::";
119  inFunction += functionName;
120  G4ExceptionDescription description;
121  description << " " << "main ntuple " << id << " does not exist.";
122  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
123  }
124  return nullptr;
125  }
126 
127  return mainNtupleVector[index];
128 }
129 
130 
131 //
132 // protected functions
133 //
134 
135 //_____________________________________________________________________________
137  tools::wroot::ntuple* mainNtuple)
138 {
139 #ifdef G4VERBOSE
140  if ( fState.GetVerboseL4() )
142  ->Message("create from main", "pntuple", mainNtuple->name());
143 #endif
144 
146  // auto directory = fMainNtupleManager->GetNtupleDirectory();
147 
148  // Get parameters from ntupleDescription
149  ntupleDescription->fFile = rfile.get();
150  mainNtuple->get_branches(ntupleDescription->fMainBranches);
151 
152  G4bool verbose = true;
153  if ( fRowWise ) {
154  auto mainBranch = mainNtuple->get_row_wise_branch();
155  tools::wroot::mt_ntuple_row_wise* mtNtuple
156  = new tools::wroot::mt_ntuple_row_wise(
157  G4cout, rfile->byte_swap(), rfile->compression(),
158  mainNtuple->dir().seek_directory(),
159  *mainBranch, mainBranch->basket_size(),
160  ntupleDescription->fNtupleBooking, verbose);
161 
162  ntupleDescription->fNtuple
163  = static_cast<tools::wroot::imt_ntuple*>(mtNtuple);
164  ntupleDescription->fBasePNtuple
165  = static_cast<tools::wroot::base_pntuple*>(mtNtuple);
166  }
167  else {
168  std::vector<tools::uint32> basketSizes;
169  tools_vforcit(tools::wroot::branch*, ntupleDescription->fMainBranches, it) {
170  basketSizes.push_back((*it)->basket_size());
171  }
172  auto basketEntries = fMainNtupleManager->GetBasketEntries();
173 
174  tools::wroot::mt_ntuple_column_wise* mtNtuple =
175  new tools::wroot::mt_ntuple_column_wise(
176  G4cout, rfile->byte_swap(), rfile->compression(),
177  mainNtuple->dir().seek_directory(),
178  ntupleDescription->fMainBranches, basketSizes,
179  ntupleDescription->fNtupleBooking,
180  fRowMode, basketEntries, verbose);
181 
182  ntupleDescription->fNtuple
183  = static_cast<tools::wroot::imt_ntuple*>(mtNtuple);
184  ntupleDescription->fBasePNtuple
185  = static_cast<tools::wroot::base_pntuple*>(mtNtuple);
186  }
187 
188  ntupleDescription->fIsNtupleOwner = true;
189  // // pntuple object is not deleted automatically
190  fNtupleVector.push_back(ntupleDescription->fNtuple);
191 
192 
193 #ifdef G4VERBOSE
194  if ( fState.GetVerboseL3() )
196  ->Message("create from main", "pntuple", mainNtuple->name());
197 #endif
198 }
199 
200 //_____________________________________________________________________________
202 {
203 // Create ntuple from booking (if not yet done) and main ntuple
204 // This function is called from G4AnalysisManager::OpenFile.
205 
207  if ( fNtupleDescriptionVector.size() ) {
208  fCreateMode = G4PNtupleCreateMode::kSlaveBeforeOpen;
209  // G4cout << "Create mode: kSlaveBeforeOpen" << G4endl;
210  } else {
211  fCreateMode = G4PNtupleCreateMode::kSlaveAfterOpen;
212  // G4cout << "Create mode: kSlaveAfterOpen" << G4endl;
213  }
214  }
215 
216  if ( fCreateMode == G4PNtupleCreateMode::kSlaveAfterOpen ) {
217  // ntuples are not yet booked
218  // G4cout << "Ntuples are not yet booked ?" << G4endl;
219  return;
220  }
221 
222  auto& mainNtupleVector
224 
225  G4int lcounter = 0;
226  for ( auto mainNtuple : mainNtupleVector ) {
227 
228  auto& ntupleDescription
229  = fNtupleDescriptionVector[lcounter++];
230  CreateNtuple(ntupleDescription, mainNtuple);
231  }
232 }
233 
234 //_____________________________________________________________________________
236  const G4String& name, const G4String& title)
237 {
238 // Create pntuple description with ntuple_booking
239 
240 #ifdef G4VERBOSE
241  if ( fState.GetVerboseL4() )
242  fState.GetVerboseL4()->Message("create", "pntuple booking", name);
243 #endif
244 
245  // Set create mode if not yet defined
248  fCreateMode = G4PNtupleCreateMode::kSlaveAfterOpen;
249  } else {
250  fCreateMode = G4PNtupleCreateMode::kSlaveBeforeOpen;
251  }
252  }
253 
254  // Create ntuple description
255  auto index = fNtupleDescriptionVector.size();
256  auto ntupleDescription = new G4RootPNtupleDescription();
257  fNtupleDescriptionVector.push_back(ntupleDescription);
258 
259  // Save name & title in ntuple booking
260  ntupleDescription->fNtupleBooking.set_name(name);
261  ntupleDescription->fNtupleBooking.set_title(title);
262 
263  fLockFirstId = true;
264 
265 #ifdef G4VERBOSE
266  if ( fState.GetVerboseL2() ) {
267  G4ExceptionDescription description;
268  description << name << " ntupleId " << index + fFirstId;
269  fState.GetVerboseL2()->Message("create", "pntuple booking", description);
270  }
271 #endif
272 
273  return index + fFirstId;
274 }
275 
276 //_____________________________________________________________________________
278  G4int ntupleId, const G4String& name, std::vector<int>* vector)
279 {
280  return CreateNtupleTColumn<int>(ntupleId, name, vector);
281 }
282 
283 //_____________________________________________________________________________
285  G4int ntupleId, const G4String& name, std::vector<float>* vector)
286 {
287  return CreateNtupleTColumn<float>(ntupleId, name, vector);
288 }
289 
290 
291 //_____________________________________________________________________________
293  G4int ntupleId, const G4String& name, std::vector<double>* vector)
294 {
295  return CreateNtupleTColumn<double>(ntupleId, name, vector);
296 }
297 
298 //_____________________________________________________________________________
300  G4int ntupleId, const G4String& name)
301 {
302  return CreateNtupleTColumn<std::string>(ntupleId, name, nullptr);
303 }
304 
305 //_____________________________________________________________________________
307 {
308 // create ntuple if file was open
309 
310  // if ( fMainNtupleManager->GetNtupleFile() ) {
311  if ( fCreateMode == G4PNtupleCreateMode::kSlaveAfterOpen ) {
312  auto ntupleDescription
313  = GetNtupleDescriptionInFunction(ntupleId, "FinishNtuple");
314  if ( ! ntupleDescription ) return;
315 
316  auto mainNtuple = GetMainNtupleInFunction(ntupleId, "FinishNtuple");
317  if ( ! mainNtuple ) return;
318 
319  CreateNtuple(ntupleDescription, mainNtuple);
320  }
321 }
322 
323 //_____________________________________________________________________________
325  G4int ntupleId, G4int columnId, G4int value)
326 {
327  return FillNtupleTColumn<int>(ntupleId, columnId, value);
328 }
329 
330 //_____________________________________________________________________________
332  G4int ntupleId, G4int columnId, G4float value)
333 {
334  return FillNtupleTColumn<float>(ntupleId, columnId, value);
335 }
336 
337 //_____________________________________________________________________________
339  G4int ntupleId, G4int columnId, G4double value)
340 {
341  return FillNtupleTColumn<double>(ntupleId, columnId, value);
342 }
343 
344 //_____________________________________________________________________________
346  G4int ntupleId, G4int columnId, const G4String& value)
347 {
348  return FillNtupleTColumn<std::string>(ntupleId, columnId, value);
349 }
350 
351 //_____________________________________________________________________________
353 {
354  if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
355  //G4cout << "Skipping AddNtupleRow for " << ntupleId << G4endl;
356  return false;
357  }
358 
359 #ifdef G4VERBOSE
360  if ( fState.GetVerboseL4() ) {
361  G4ExceptionDescription description;
362  description << " ntupleId " << ntupleId;
363  fState.GetVerboseL4()->Message("add", "pntuple row", description);
364  }
365 #endif
366 
367  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "AddNtupleRow");
368  if ( ! ntupleDescription ) return false;
369 
370  G4AutoLock lock(&pntupleMutex);
371  lock.unlock();
372  mutex toolsLock(lock);
373  auto result
374  = ntupleDescription->fNtuple
375  ->add_row(toolsLock, *ntupleDescription->fFile);
376 
377  if ( ! result ) {
378  G4ExceptionDescription description;
379  description << " " << " ntupleId " << ntupleId
380  << "adding row has failed.";
381  G4Exception("G4RootPNtupleManager::AddNtupleRow()",
382  "Analysis_W002", JustWarning, description);
383  }
384 
385 #ifdef G4VERBOSE
386  if ( fState.GetVerboseL3() ) {
387  G4ExceptionDescription description;
388  description << " ntupleId " << ntupleId;
389  fState.GetVerboseL3()->Message("add", "pntuple row", description);
390  }
391 #endif
392 
393  return true;
394 }
395 
396 //_____________________________________________________________________________
398 {
399  for ( auto ntupleDescription : fNtupleDescriptionVector) {
400 
401  // skip inactivated ntuples
402  if ( ! ntupleDescription->fActivation ) continue;
403 
404 #ifdef G4VERBOSE
405  if ( fState.GetVerboseL4() ) {
407  ->Message("merge", "pntuple", ntupleDescription->fNtupleBooking.name());
408  }
409 #endif
410 
411  G4AutoLock lock(&pntupleMutex);
412  lock.unlock();
413  mutex toolsLock(lock);
414  auto result
415  = ntupleDescription->fNtuple
416  ->end_fill(toolsLock, *ntupleDescription->fFile);
417 
418  if ( ! result ) {
419  G4ExceptionDescription description;
420  description << " " << " ntuple " << ntupleDescription->fNtupleBooking.name()
421  << "end fill has failed.";
422  G4Exception("G4RootPNtupleManager::Merge()",
423  "Analysis_W002", JustWarning, description);
424  }
425 
426  delete ntupleDescription->fNtuple;
427  ntupleDescription->fNtuple = nullptr;
428 
429 #ifdef G4VERBOSE
430  if ( fState.GetVerboseL3() ) {
432  ->Message("merge", "pntuple", ntupleDescription->fNtupleBooking.name());
433  }
434 #endif
435  }
436  return true;
437 
438 }
439 
440 //_____________________________________________________________________________
442 {
443  for ( auto ntupleDescription : fNtupleDescriptionVector ) {
444  if ( deleteNtuple ) {
445  delete ntupleDescription->fNtuple;
446  }
447  ntupleDescription->fNtuple = nullptr;
448  }
449 
450  fNtupleVector.clear();
451 
452  return true;
453 }
454 
455 //_____________________________________________________________________________
456 
458  G4bool activation)
459 {
460  for ( auto ntupleDescription : fNtupleDescriptionVector ) {
461  ntupleDescription->fActivation = activation;
462  }
463 }
464 
465 //_____________________________________________________________________________
466 
468  G4int ntupleId, G4bool activation)
469 {
470  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetActivation");
471  if ( ! ntupleDescription ) return;
472 
473  ntupleDescription->fActivation = activation;
474 }
475 
476 //_____________________________________________________________________________
478  G4int ntupleId) const
479 {
480  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetActivation");
481  if ( ! ntupleDescription ) return false;
482 
483  return ntupleDescription->fActivation;
484 }
485 
486 //_____________________________________________________________________________
488 {
489  return fNtupleVector.size();
490 }
491 
492 //_____________________________________________________________________________
494 {
495  return fNtupleDescriptionVector.size();
496 }
497 
498 //_____________________________________________________________________________
500 {
501  return ! fNtupleDescriptionVector.size();
502 }
503 
504 //_____________________________________________________________________________
506 {
507  fRowWise = rowWise;
508  fRowMode = rowMode;
509 }