ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4RootMpiPNtupleManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4RootMpiPNtupleManager.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, 21/11/2018 (ivana@ipno.in2p3.fr)
28 
30 #include "G4RootFileManager.hh"
31 #include "G4AnalysisUtilities.hh"
32 
33 #include "tools/wroot/file"
34 #include "tools/wroot/mpi_ntuple_row_wise"
35 #include "tools/wroot/mpi_ntuple_column_wise"
36 
37 const int kTAG_NTUPLE = 1004; // This constant is defined in G4MPImanager
38  // (should be passed from the application)
39 
40 //_____________________________________________________________________________
42  const G4AnalysisManagerState& state,
43  tools::impi* impi, G4int mpiRank, G4int destinationRank)
44  : G4BaseNtupleManager(state),
45  fNtupleVector(),
46  fImpi(impi),
47  fMpiRank(mpiRank),
48  fDestinationRank(destinationRank)
49 {
50 }
51 
52 //_____________________________________________________________________________
54 {
55  for ( auto ntupleDescription : fNtupleDescriptionVector ) {
56  delete ntupleDescription;
57  }
58 }
59 
60 //
61 // private functions
62 //
63 
64 //_____________________________________________________________________________
67  G4int id, G4String functionName, G4bool warn) const
68 {
69  auto index = id - fFirstId;
70  if ( index < 0 || index >= G4int(fNtupleDescriptionVector.size()) ) {
71  if ( warn) {
72  G4String inFunction = "G4RootMpiPNtupleManager::";
73  inFunction += functionName;
74  G4ExceptionDescription description;
75  description << " " << "ntuple " << id << " does not exist.";
76  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
77  }
78  return nullptr;
79  }
80 
81  return fNtupleDescriptionVector[index];
82 }
83 
84 //_____________________________________________________________________________
86  G4int id, G4String functionName, G4bool warn) const
87 {
88  auto ntupleDescription = GetNtupleDescriptionInFunction(id, functionName);
89  if ( ! ntupleDescription ) return nullptr;
90 
91  if ( ! ntupleDescription->fBasePNtuple ) {
92  if ( warn ) {
93  G4String inFunction = "G4RootMpiPNtupleManager::";
94  inFunction += functionName;
95  G4ExceptionDescription description;
96  description << " " << "ntupleId " << id << " does not exist.";
97  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
98  }
99  return nullptr;
100  }
101  return ntupleDescription->fBasePNtuple;
102 }
103 
104 //
105 // protected functions
106 //
107 
108 //_____________________________________________________________________________
110 {
111 #ifdef G4VERBOSE
112  if ( fState.GetVerboseL4() )
114  ->Message("create from booking", "mpi pntuple", ntupleDescription->fNtupleBooking.name());
115 #endif
116 
117  // Wait for the ntuple data from main
118 
119  G4bool verbose = ( fState.GetVerboseL2() );
120 
121  G4cout << "Go to wait_buffer from " << fDestinationRank << G4endl;
122  fImpi->pack_reset();
123  int probe_src;
124  if ( ! fImpi->wait_buffer(fMpiRank, fDestinationRank, kTAG_NTUPLE, probe_src, verbose)) {
125  G4cerr << "G4RootMpiPNtupleManager::CreateNtuple: wait_buffer() failed."<< G4endl;
126  return;
127  }
128  G4cout << "After wait_buffer with " << fImpi << G4endl;
129 
130  tools::uint32 mainNtupleId;
131  bool rowWise;
132  bool byteSwap;
133  unsigned int compression;
134  tools::wroot::seek seekDirectory;
135  tools::uint32 basketSize;
136  bool rowMode;
137  std::vector<tools::uint32> basketSizes;
138  unsigned int basketEntries;
139 
140  if ( ! fImpi->unpack(mainNtupleId) ) {
141  G4cerr << "bunpack(byteSwap) failed."<< G4endl;
142  return;
143  }
144  // G4cout << "unpack 1" << G4endl;
145 
146  if ( ! fImpi->bunpack(rowWise) ) {
147  G4cerr << "bunpack(rowWise) failed."<< G4endl;
148  return;
149  }
150  // G4cout << "unpack 1/2" << G4endl;
151 
152  if ( ! fImpi->bunpack(byteSwap) ) {
153  G4cerr << "bunpack(byteSwap) failed."<< G4endl;
154  return;
155  }
156  // G4cout << "unpack 2" << G4endl;
157 
158  if ( ! fImpi->unpack(compression) ) {
159  G4cerr << "unpack(compression) failed."<< G4endl;
160  return;
161  }
162  // G4cout << "unpack 3" << G4endl;
163 
164  if ( ! fImpi->unpack(seekDirectory) ) {
165  G4cerr << "unpack(seek) failed."<< G4endl;
166  return;
167  }
168  // G4cout << "unpack 4" << G4endl;
169 
170  if (rowWise) {
171  if ( ! fImpi->unpack(basketSize) ) {
172  G4cerr << "unpack(basketSize) failed."<< G4endl;
173  return;
174  }
175  } else {
176  if ( ! fImpi->bunpack(rowMode) ) {
177  G4cerr << "bpack(rowMode) failed." << G4endl;
178  return;
179  }
180  if ( ! fImpi->vunpack(basketSizes) ) {
181  G4cerr << "vunpack(basketSizes) failed."<< G4endl;
182  return;
183  }
184  if( ! fImpi->unpack(basketEntries) ) {
185  G4cerr << "unpack(basketEntries) failed." << G4endl;
186  return;
187  }
188  }
189  // G4cout << "unpack 5" << G4endl;
190 
191  // G4cout << "rank = " << fMpiRank
192  // << ", verbose = " << verbose
193  // << ", mainNtupleId = " << mainNtupleId
194  // << ", byteSwap = " << byteSwap
195  // << ", compression = " << compression
196  // << ", seekDirectory = " << seekDirectory
197  // << ", basketSizes.size() = " << basketSizes.size()
198  // << G4endl;
199 
200  // Create MPI pntuple
201  if ( rowWise ) {
202  tools::wroot::mpi_ntuple_row_wise* ntuple
203  = new tools::wroot::mpi_ntuple_row_wise(
204  mainNtupleId, G4cout, byteSwap, compression, seekDirectory,
205  basketSize, ntupleDescription->fNtupleBooking, verbose);
206  ntupleDescription->fNtuple = ntuple;
207  ntupleDescription->fBasePNtuple = ntuple;
208  } else {
209  tools::wroot::mpi_ntuple_column_wise* ntuple
210  = new tools::wroot::mpi_ntuple_column_wise(
211  mainNtupleId, G4cout, byteSwap, compression, seekDirectory,
212  basketSizes, ntupleDescription->fNtupleBooking,
213  rowMode, basketEntries, verbose);
214  ntupleDescription->fNtuple = ntuple;
215  ntupleDescription->fBasePNtuple = ntuple;
216  }
217 
218  ntupleDescription->fIsNtupleOwner = true;
219  ntupleDescription->fImpi = fImpi;
220  // should be not needed
221  // pntuple object is not deleted automatically
222  fNtupleVector.push_back(ntupleDescription->fNtuple);
223 
224 #ifdef G4VERBOSE
225  if ( fState.GetVerboseL3() )
227  ->Message("create from booking", "mpi pntuple", ntupleDescription->fNtupleBooking.name());
228 #endif
229 }
230 
231 //_____________________________________________________________________________
233 {
234 // Create ntuple from ntuple_booking & the buffer from main rank
235 
236  // G4cout << "Start G4RootMpiPNtupleManager::CreateNtuplesFromBooking" << G4endl;
237 
238  // Do not create ntuples if NtupleVector is not empty
239  if ( fNtupleVector.size() ) return;
240 
241  for ( auto ntupleDescription : fNtupleDescriptionVector ) {
242 
243  // Do not create ntuple if it is inactivated
244  if ( fState.GetIsActivation() && ( ! ntupleDescription->fActivation ) ) continue;
245 
246  // Do not create ntuple if it already exists
247  if ( ntupleDescription->fNtuple ) continue;
248 
249 #ifdef G4VERBOSE
250  if ( fState.GetVerboseL4() )
252  ->Message("create from booking", "mpi pntuple",
253  ntupleDescription->fNtupleBooking.name());
254 #endif
255 
256  // create ntuple
257  CreateNtuple(ntupleDescription);
258 
259  // finish created ntuple
260  // (nothing to be done ?)
261  // FinishTNtuple(ntupleDescription);
262 
263 #ifdef G4VERBOSE
264  if ( fState.GetVerboseL3() )
266  ->Message("create from booking", "mpi pntuple",
267  ntupleDescription->fNtupleBooking.name());
268 #endif
269  }
270 
271 }
272 
273 //_____________________________________________________________________________
275  const G4String& name, const G4String& title)
276 {
277 // Create pntuple description with ntuple_booking
278 
279 #ifdef G4VERBOSE
280  if ( fState.GetVerboseL4() )
281  fState.GetVerboseL4()->Message("create", "pntuple booking", name);
282 #endif
283 
284  // Create ntuple description
285  auto index = fNtupleDescriptionVector.size();
286  auto ntupleDescription = new G4RootMpiPNtupleDescription();
287  ntupleDescription->fMainNtupleRank = fDestinationRank;
288  // make this flexible
289  fNtupleDescriptionVector.push_back(ntupleDescription);
290 
291  // Save name & title in ntuple booking
292  ntupleDescription->fNtupleBooking.set_name(name);
293  ntupleDescription->fNtupleBooking.set_title(title);
294 
295  fLockFirstId = true;
296 
297 #ifdef G4VERBOSE
298  if ( fState.GetVerboseL2() ) {
299  G4ExceptionDescription description;
300  description << name << " ntupleId " << index + fFirstId;
301  fState.GetVerboseL2()->Message("create", "pntuple booking", description);
302  }
303 #endif
304 
305  return index + fFirstId;
306 }
307 
308 //_____________________________________________________________________________
310  G4int ntupleId, const G4String& name, std::vector<int>* vector)
311 {
312  return CreateNtupleTColumn<int>(ntupleId, name, vector);
313 }
314 
315 //_____________________________________________________________________________
317  G4int ntupleId, const G4String& name, std::vector<float>* vector)
318 {
319  return CreateNtupleTColumn<float>(ntupleId, name, vector);
320 }
321 
322 //_____________________________________________________________________________
324  G4int ntupleId, const G4String& name, std::vector<double>* vector)
325 {
326  return CreateNtupleTColumn<double>(ntupleId, name, vector);
327 }
328 
329 //_____________________________________________________________________________
331  G4int ntupleId, const G4String& name)
332 {
333  return CreateNtupleTColumn<std::string>(ntupleId, name, nullptr);
334 }
335 
336 //_____________________________________________________________________________
338 {}
339 
340 //_____________________________________________________________________________
342  G4int ntupleId, G4int columnId, G4int value)
343 {
344  return FillNtupleTColumn<int>(ntupleId, columnId, value);
345 }
346 
347 //_____________________________________________________________________________
349  G4int ntupleId, G4int columnId, G4float value)
350 {
351  return FillNtupleTColumn<float>(ntupleId, columnId, value);
352 }
353 
354 //_____________________________________________________________________________
356  G4int ntupleId, G4int columnId, G4double value)
357 {
358  return FillNtupleTColumn<double>(ntupleId, columnId, value);
359 }
360 
361 //_____________________________________________________________________________
363  G4int ntupleId, G4int columnId, const G4String& value)
364 {
365  return FillNtupleTColumn<std::string>(ntupleId, columnId, value);
366 }
367 
368 //_____________________________________________________________________________
370 {
371  if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
372  //G4cout << "Skipping AddNtupleRow for " << ntupleId << G4endl;
373  return false;
374  }
375 
376 #ifdef G4VERBOSE
377  if ( fState.GetVerboseL4() ) {
378  G4ExceptionDescription description;
379  description << " ntupleId " << ntupleId;
380  fState.GetVerboseL4()->Message("add", "pntuple row", description);
381  }
382 #endif
383 
384  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "AddNtupleRow");
385  if ( ! ntupleDescription ) return false;
386 
387  // if(!_ntuple->add_row(_impi,rank_src,tag)) {
388  auto result
389  = ntupleDescription->fNtuple
390  ->add_row(*fImpi, ntupleDescription->fMainNtupleRank, kTAG_NTUPLE );
391 
392  if ( ! result ) {
393  G4ExceptionDescription description;
394  description << " " << " ntupleId " << ntupleId
395  << "adding row has failed.";
396  G4Exception("G4RootMpiPNtupleManager::AddNtupleRow()",
397  "Analysis_W002", JustWarning, description);
398  }
399 
400 #ifdef G4VERBOSE
401  if ( fState.GetVerboseL3() ) {
402  G4ExceptionDescription description;
403  description << " ntupleId " << ntupleId;
404  fState.GetVerboseL3()->Message("add", "pntuple row", description);
405  }
406 #endif
407 
408  return true;
409 }
410 
411 //_____________________________________________________________________________
413 {
414 
415  // G4cout << "Start G4RootMpiPNtupleManager::Merge" << G4endl;
416 
417  auto finalResult = true;
418 
419  for ( auto ntupleDescription : fNtupleDescriptionVector) {
420 
421  // skip inactivated ntuples
422  if ( ! ntupleDescription->fActivation ) continue;
423 
424  // skip if ntuple was already merged and deleted
425  // (this happend when the main rank re-opens a new file for merged data)
426  if ( ! ntupleDescription->fNtuple ) continue;
427 
428 #ifdef G4VERBOSE
429  if ( fState.GetVerboseL4() ) {
431  ->Message("end_fill", "pntuple", ntupleDescription->fNtupleBooking.name());
432  }
433 #endif
434 
435  // if(!_ntuple->end_fill(_impi,rank_src,tag)) {} //important.
436  G4cout << "call end_fill " << fImpi
437  << " to rank " << ntupleDescription->fMainNtupleRank
438  << " pntuple: " << ntupleDescription->fNtuple << G4endl;
439  auto result
440  = ntupleDescription->fNtuple
441  ->end_fill(*fImpi, ntupleDescription->fMainNtupleRank, kTAG_NTUPLE);
442 
443  if ( ! result ) {
444  G4ExceptionDescription description;
445  description << " " << " ntuple " << ntupleDescription->fNtupleBooking.name()
446  << "end fill has failed.";
447  G4Exception("G4RootMpiPNtupleManager::Merge()",
448  "Analysis_W002", JustWarning, description);
449  }
450 
451  delete ntupleDescription->fNtuple;
452  ntupleDescription->fNtuple = nullptr;
453 
454 #ifdef G4VERBOSE
455  if ( fState.GetVerboseL3() ) {
457  ->Message("end_fill", "pntuple", ntupleDescription->fNtupleBooking.name());
458  }
459 #endif
460  }
461 
462  return finalResult;
463 }
464 
465 //_____________________________________________________________________________
467 {
468  for ( auto ntupleDescription : fNtupleDescriptionVector ) {
469  if ( deleteNtuple ) {
470  delete ntupleDescription->fNtuple;
471  }
472  ntupleDescription->fNtuple = nullptr;
473  }
474 
475  fNtupleVector.clear();
476 
477  return true;
478 }
479 
480 //_____________________________________________________________________________
481 
483  G4bool activation)
484 {
485  for ( auto ntupleDescription : fNtupleDescriptionVector ) {
486  ntupleDescription->fActivation = activation;
487  }
488 }
489 
490 //_____________________________________________________________________________
491 
493  G4int ntupleId, G4bool activation)
494 {
495  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetActivation");
496  if ( ! ntupleDescription ) return;
497 
498  ntupleDescription->fActivation = activation;
499 }
500 
501 //_____________________________________________________________________________
503  G4int ntupleId) const
504 {
505  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetActivation");
506  if ( ! ntupleDescription ) return false;
507 
508  return ntupleDescription->fActivation;
509 }
510 
511 //_____________________________________________________________________________
513 {
514  return fNtupleVector.size();
515 }
516 
517 //_____________________________________________________________________________
519 {
520  return fNtupleDescriptionVector.size();
521 }
522 
523 //_____________________________________________________________________________
525 {
526  return ! fNtupleDescriptionVector.size();
527 }
528 
529 //_____________________________________________________________________________
531 {
532  if ( ! fFileManager ) {
533  G4String inFunction = "G4RootMpiPNtupleManager::::GetBasketSize";
534  G4ExceptionDescription description;
535  description << " " << "File manager must be defined first.";
536  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
537  return 0;
538  }
539 
540  return fFileManager->GetBasketSize();
541 }