Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
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 // $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  : G4BaseNtupleManager(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 //_____________________________________________________________________________
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::base_pntuple*
88  G4int id, G4String functionName, G4bool warn) const
89 {
90  auto ntupleDescription = GetNtupleDescriptionInFunction(id, functionName);
91  if ( ! ntupleDescription ) return nullptr;
92 
93  if ( ! ntupleDescription->fBasePNtuple ) {
94  if ( warn ) {
95  G4String inFunction = "G4RootPNtupleManager::";
96  inFunction += functionName;
97  G4ExceptionDescription description;
98  description << " " << "ntupleId " << id << " does not exist.";
99  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
100  }
101  return nullptr;
102  }
103  return ntupleDescription->fBasePNtuple;
104 }
105 
106 //_____________________________________________________________________________
109  G4int id, G4String functionName, G4bool warn) const
110 {
111  auto& mainNtupleVector
113 
114  auto index = id - fFirstId;
115  if ( index < 0 || index >= G4int(mainNtupleVector.size()) ) {
116  if ( warn) {
117  G4String inFunction = "G4RootPNtupleManager::";
118  inFunction += functionName;
119  G4ExceptionDescription description;
120  description << " " << "main ntuple " << id << " does not exist.";
121  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
122  }
123  return nullptr;
124  }
125 
126  return mainNtupleVector[index];
127 }
128 
129 
130 //
131 // protected functions
132 //
133 
134 //_____________________________________________________________________________
136  tools::wroot::ntuple* mainNtuple)
137 {
138 #ifdef G4VERBOSE
139  if ( fState.GetVerboseL4() )
141  ->Message("create from main", "pntuple", mainNtuple->name());
142 #endif
143 
144  auto rfile = fMainNtupleManager->GetNtupleFile();
145  // auto directory = fMainNtupleManager->GetNtupleDirectory();
146 
147  // Get parameters from main ntuple
148  auto mainBranch = mainNtuple->get_row_wise_branch();
149  auto rowWise = mainBranch ? true : false;
150 
151  ntupleDescription->fFile = rfile.get();
152  mainNtuple->get_branches(ntupleDescription->fMainBranches);
153 
154  G4bool verbose = true;
155  if ( rowWise ) {
156  tools::wroot::mt_ntuple_row_wise* mtNtuple
157  = new tools::wroot::mt_ntuple_row_wise(
158  G4cout, rfile->byte_swap(), rfile->compression(),
159  mainNtuple->dir().seek_directory(),
160  *mainBranch, mainBranch->basket_size(),
161  ntupleDescription->fNtupleBooking, verbose);
162 
163  ntupleDescription->fNtuple
164  = static_cast<tools::wroot::imt_ntuple*>(mtNtuple);
165  ntupleDescription->fBasePNtuple
166  = static_cast<tools::wroot::base_pntuple*>(mtNtuple);
167  }
168  else {
169  std::vector<tools::uint32> basketSizes;
170  tools_vforcit(tools::wroot::branch*, ntupleDescription->fMainBranches, it) {
171  basketSizes.push_back((*it)->basket_size());
172  }
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, verbose);
180 
181  ntupleDescription->fNtuple
182  = static_cast<tools::wroot::imt_ntuple*>(mtNtuple);
183  ntupleDescription->fBasePNtuple
184  = static_cast<tools::wroot::base_pntuple*>(mtNtuple);
185  }
186 
187  ntupleDescription->fIsNtupleOwner = true;
188  // // pntuple object is not deleted automatically
189  fNtupleVector.push_back(ntupleDescription->fNtuple);
190 
191 
192 #ifdef G4VERBOSE
193  if ( fState.GetVerboseL3() )
195  ->Message("create from main", "pntuple", mainNtuple->name());
196 #endif
197 }
198 
199 //_____________________________________________________________________________
201 {
202 // Create ntuple from booking (if not yet done) and main ntuple
203 // This function is called from G4AnalysisManager::OpenFile.
204 
206  if ( fNtupleDescriptionVector.size() ) {
208  // G4cout << "Create mode: kSlaveBeforeOpen" << G4endl;
209  } else {
211  // G4cout << "Create mode: kSlaveAfterOpen" << G4endl;
212  }
213  }
214 
216  // ntuples are not yet booked
217  // G4cout << "Ntuples are not yet booked ?" << G4endl;
218  return;
219  }
220 
221  auto& mainNtupleVector
223 
224  G4int lcounter = 0;
225  for ( auto mainNtuple : mainNtupleVector ) {
226 
227  auto& ntupleDescription
228  = fNtupleDescriptionVector[lcounter++];
229  CreateNtuple(ntupleDescription, mainNtuple);
230  }
231 }
232 
233 //_____________________________________________________________________________
235  const G4String& name, const G4String& title)
236 {
237 // Create pntuple description with ntuple_booking
238 
239 #ifdef G4VERBOSE
240  if ( fState.GetVerboseL4() )
241  fState.GetVerboseL4()->Message("create", "pntuple booking", name);
242 #endif
243 
244  // Set create mode if not yet defined
248  } else {
250  }
251  }
252 
253  // Create ntuple description
254  auto index = fNtupleDescriptionVector.size();
255  auto ntupleDescription = new G4RootPNtupleDescription();
256  fNtupleDescriptionVector.push_back(ntupleDescription);
257 
258  // Save name & title in ntuple booking
259  ntupleDescription->fNtupleBooking.set_name(name);
260  ntupleDescription->fNtupleBooking.set_title(title);
261 
262  fLockFirstId = true;
263 
264 #ifdef G4VERBOSE
265  if ( fState.GetVerboseL2() ) {
266  G4ExceptionDescription description;
267  description << name << " ntupleId " << index + fFirstId;
268  fState.GetVerboseL2()->Message("create", "pntuple booking", description);
269  }
270 #endif
271 
272  return index + fFirstId;
273 }
274 
275 //_____________________________________________________________________________
277  G4int ntupleId, const G4String& name, std::vector<int>* vector)
278 {
279  return CreateNtupleTColumn<int>(ntupleId, name, vector);
280 }
281 
282 //_____________________________________________________________________________
284  G4int ntupleId, const G4String& name, std::vector<float>* vector)
285 {
286  return CreateNtupleTColumn<float>(ntupleId, name, vector);
287 }
288 
289 
290 //_____________________________________________________________________________
292  G4int ntupleId, const G4String& name, std::vector<double>* vector)
293 {
294  return CreateNtupleTColumn<double>(ntupleId, name, vector);
295 }
296 
297 //_____________________________________________________________________________
299  G4int ntupleId, const G4String& name)
300 {
301  return CreateNtupleTColumn<std::string>(ntupleId, name, nullptr);
302 }
303 
304 //_____________________________________________________________________________
306 {
307 // create ntuple if file was open
308 
309  // if ( fMainNtupleManager->GetNtupleFile() ) {
311  auto ntupleDescription
312  = GetNtupleDescriptionInFunction(ntupleId, "FinishNtuple");
313  if ( ! ntupleDescription ) return;
314 
315  auto mainNtuple = GetMainNtupleInFunction(ntupleId, "FinishNtuple");
316  if ( ! mainNtuple ) return;
317 
318  CreateNtuple(ntupleDescription, mainNtuple);
319  }
320 }
321 
322 //_____________________________________________________________________________
324  G4int ntupleId, G4int columnId, G4int value)
325 {
326  return FillNtupleTColumn<int>(ntupleId, columnId, value);
327 }
328 
329 //_____________________________________________________________________________
331  G4int ntupleId, G4int columnId, G4float value)
332 {
333  return FillNtupleTColumn<float>(ntupleId, columnId, value);
334 }
335 
336 //_____________________________________________________________________________
338  G4int ntupleId, G4int columnId, G4double value)
339 {
340  return FillNtupleTColumn<double>(ntupleId, columnId, value);
341 }
342 
343 //_____________________________________________________________________________
345  G4int ntupleId, G4int columnId, const G4String& value)
346 {
347  return FillNtupleTColumn<std::string>(ntupleId, columnId, value);
348 }
349 
350 //_____________________________________________________________________________
352 {
353  if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
354  //G4cout << "Skipping AddNtupleRow for " << ntupleId << G4endl;
355  return false;
356  }
357 
358 #ifdef G4VERBOSE
359  if ( fState.GetVerboseL4() ) {
360  G4ExceptionDescription description;
361  description << " ntupleId " << ntupleId;
362  fState.GetVerboseL4()->Message("add", "pntuple row", description);
363  }
364 #endif
365 
366  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "AddNtupleRow");
367  if ( ! ntupleDescription ) return false;
368 
369  G4AutoLock lock(&addRowMutex);
370  lock.unlock();
371  mutex toolsLock(lock);
372  auto result
373  = ntupleDescription->fNtuple
374  ->add_row(toolsLock, *ntupleDescription->fFile);
375 
376  if ( ! result ) {
377  G4ExceptionDescription description;
378  description << " " << " ntupleId " << ntupleId
379  << "adding row has failed.";
380  G4Exception("G4RootPNtupleManager::AddNtupleRow()",
381  "Analysis_W002", JustWarning, description);
382  }
383 
384 #ifdef G4VERBOSE
385  if ( fState.GetVerboseL3() ) {
386  G4ExceptionDescription description;
387  description << " ntupleId " << ntupleId;
388  fState.GetVerboseL3()->Message("add", "pntuple row", description);
389  }
390 #endif
391 
392  return true;
393 }
394 
395 //_____________________________________________________________________________
397 {
398  for ( auto ntupleDescription : fNtupleDescriptionVector) {
399 
400  // skip inactivated ntuples
401  if ( ! ntupleDescription->fActivation ) continue;
402 
403 #ifdef G4VERBOSE
404  if ( fState.GetVerboseL4() ) {
406  ->Message("merge", "pntuple", ntupleDescription->fNtupleBooking.name());
407  }
408 #endif
409 
410  G4AutoLock lock(&endFillMutex);
411  lock.unlock();
412  mutex toolsLock(lock);
413  auto result
414  = ntupleDescription->fNtuple
415  ->end_fill(toolsLock, *ntupleDescription->fFile);
416 
417  if ( ! result ) {
418  G4ExceptionDescription description;
419  description << " " << " ntuple " << ntupleDescription->fNtupleBooking.name()
420  << "end fill has failed.";
421  G4Exception("G4RootPNtupleManager::Merge()",
422  "Analysis_W002", JustWarning, description);
423  }
424 
425  delete ntupleDescription->fNtuple;
426  ntupleDescription->fNtuple = nullptr;
427 
428 #ifdef G4VERBOSE
429  if ( fState.GetVerboseL3() ) {
431  ->Message("merge", "pntuple", ntupleDescription->fNtupleBooking.name());
432  }
433 #endif
434  }
435  return true;
436 
437 }
438 
439 //_____________________________________________________________________________
441 {
442  for ( auto ntupleDescription : fNtupleDescriptionVector ) {
443  if ( deleteNtuple ) {
444  delete ntupleDescription->fNtuple;
445  }
446  ntupleDescription->fNtuple = nullptr;
447  }
448 
449  fNtupleVector.clear();
450 
451  return true;
452 }
453 
454 //_____________________________________________________________________________
455 
457  G4bool activation)
458 {
459  for ( auto ntupleDescription : fNtupleDescriptionVector ) {
460  ntupleDescription->fActivation = activation;
461  }
462 }
463 
464 //_____________________________________________________________________________
465 
467  G4int ntupleId, G4bool activation)
468 {
469  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetActivation");
470  if ( ! ntupleDescription ) return;
471 
472  ntupleDescription->fActivation = activation;
473 }
474 
475 //_____________________________________________________________________________
477  G4int ntupleId) const
478 {
479  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetActivation");
480  if ( ! ntupleDescription ) return false;
481 
482  return ntupleDescription->fActivation;
483 }
484 
485 //_____________________________________________________________________________
487 {
488  return fNtupleVector.size();
489 }
490 
491 //_____________________________________________________________________________
493 {
494  return fNtupleDescriptionVector.size();
495 }
496 
497 //_____________________________________________________________________________
499 {
500  return ! fNtupleDescriptionVector.size();
501 }
502 
tools::wroot::ntuple * GetMainNtupleInFunction(G4int id, G4String function, G4bool warn=true) const
const XML_Char * name
Definition: expat.h:151
const G4AnalysisVerbose * GetVerboseL2() const
tools::ntuple_booking fNtupleBooking
tools::wroot::base_pntuple * GetNtupleInFunction(G4int id, G4String function, G4bool warn=true) const
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:45
virtual G4bool Merge() final
virtual G4bool Reset(G4bool deleteNtuple) final
virtual G4int CreateNtupleFColumn(G4int ntupleId, const G4String &name, std::vector< float > *vector) override
G4RootPNtupleManager(G4RootMainNtupleManager *main, const G4AnalysisManagerState &state)
TTree * ntuple
G4RootMainNtupleManager * fMainNtupleManager
float G4float
Definition: G4Types.hh:77
std::shared_ptr< tools::wroot::file > GetNtupleFile() const
tools::wroot::base_pntuple * fBasePNtuple
virtual G4int GetNofNtupleBookings() const final
virtual G4int CreateNtupleDColumn(G4int ntupleId, const G4String &name, std::vector< double > *vector) override
G4PNtupleCreateMode fCreateMode
double G4double
Definition: G4Types.hh:76
bool G4bool
Definition: G4Types.hh:79
virtual G4bool FillNtupleFColumn(G4int ntupleId, G4int columnId, G4float value) final
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:88
virtual G4bool FillNtupleSColumn(G4int ntupleId, G4int columnId, const G4String &value) final
const XML_Char int const XML_Char * value
Definition: expat.h:331
std::vector< tools::wroot::branch * > fMainBranches
const G4AnalysisVerbose * GetVerboseL4() const
virtual void SetActivation(G4bool activation) final
std::vector< tools::wroot::imt_ntuple * > fNtupleVector
virtual G4bool GetActivation(G4int ntupleId) const final
virtual G4int CreateNtupleIColumn(G4int ntupleId, const G4String &name, std::vector< int > *vector) override
const G4AnalysisVerbose * GetVerboseL3() const
virtual G4bool IsEmpty() const final
int main(int argc, char **argv)
Definition: genwindef.cc:359
const std::vector< tools::wroot::ntuple * > & GetNtupleVector()
G4double G4ParticleHPJENDLHEData::G4double result
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65
int G4int
Definition: G4Types.hh:78
tools::wroot::imt_ntuple * fNtuple
virtual G4bool AddNtupleRow() final
virtual G4int CreateNtupleSColumn(G4int ntupleId, const G4String &name) override
virtual void FinishNtuple() final
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
G4GLOB_DLL std::ostream G4cout
virtual G4bool FillNtupleDColumn(G4int ntupleId, G4int columnId, G4double value) final
void CreateNtuple(G4RootPNtupleDescription *ntupleDescription, tools::wroot::ntuple *mainNtuple)
G4RootPNtupleDescription * GetNtupleDescriptionInFunction(G4int id, G4String function, G4bool warn=true) const
std::vector< G4RootPNtupleDescription * > fNtupleDescriptionVector
virtual G4int GetNofNtuples() const final
const G4AnalysisManagerState & fState
virtual G4bool FillNtupleIColumn(G4int ntupleId, G4int columnId, G4int value) final
std::mutex G4Mutex
Definition: G4Threading.hh:84