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] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /analysis/root/src/G4RootPNtupleManager.cc (Version 11.3.0) and /analysis/root/src/G4RootPNtupleManager.cc (Version 9.1.p2)


  1 //                                                  1 
  2 // *******************************************    
  3 // * License and Disclaimer                       
  4 // *                                              
  5 // * The  Geant4 software  is  copyright of th    
  6 // * the Geant4 Collaboration.  It is provided    
  7 // * conditions of the Geant4 Software License    
  8 // * LICENSE and available at  http://cern.ch/    
  9 // * include a list of copyright holders.         
 10 // *                                              
 11 // * Neither the authors of this software syst    
 12 // * institutes,nor the agencies providing fin    
 13 // * work  make  any representation or  warran    
 14 // * regarding  this  software system or assum    
 15 // * use.  Please see the license in the file     
 16 // * for the full disclaimer and the limitatio    
 17 // *                                              
 18 // * This  code  implementation is the result     
 19 // * technical work of the GEANT4 collaboratio    
 20 // * By using,  copying,  modifying or  distri    
 21 // * any work based  on the software)  you  ag    
 22 // * use  in  resulting  scientific  publicati    
 23 // * acceptance of all terms of the Geant4 Sof    
 24 // *******************************************    
 25 //                                                
 26 //                                                
 27 // Author: Ivana Hrivnacova, 04/10/2016  (ivan    
 28                                                   
 29 #include "G4NtupleBookingManager.hh"              
 30 #include "G4RootPNtupleManager.hh"                
 31 #include "G4AnalysisUtilities.hh"                 
 32                                                   
 33 #include "tools/wroot/file"                       
 34 #include "tools/wroot/ntuple"                     
 35                                                   
 36 using namespace G4Analysis;                       
 37 using std::to_string;                             
 38                                                   
 39 // mutex in a file scope                          
 40 namespace {                                       
 41                                                   
 42 //Mutex to lock master manager when adding ntu    
 43 G4Mutex pntupleMutex = G4MUTEX_INITIALIZER;       
 44 //Mutex to lock master manager when createing     
 45 G4Mutex createMainMutex = G4MUTEX_INITIALIZER;    
 46                                                   
 47 //____________________________________________    
 48 void NotExistWarning(const G4String& what, G4i    
 49                      std::string_view classNam    
 50                      std::string_view function    
 51 {                                                 
 52   Warn(what + " id= " + to_string(id) + " does    
 53     className, functionName);                     
 54                                                   
 55 }                                                 
 56                                                   
 57 }                                                 
 58                                                   
 59 //____________________________________________    
 60 G4RootPNtupleManager::G4RootPNtupleManager(con    
 61                         std::shared_ptr<G4Ntup    
 62                         std::shared_ptr<G4Root    
 63                         G4bool rowWise, G4bool    
 64  : G4BaseNtupleManager(state),                    
 65    fBookingManager(std::move(bookingManger)),     
 66    fMainNtupleManager(std::move(main)),           
 67    fRowWise(rowWise),                             
 68    fRowMode(rowMode)                              
 69 {}                                                
 70                                                   
 71 //____________________________________________    
 72 G4RootPNtupleManager::~G4RootPNtupleManager()     
 73 {                                                 
 74   for ( auto ntupleDescription : fNtupleDescri    
 75     delete ntupleDescription;                     
 76   }                                               
 77 }                                                 
 78                                                   
 79 //                                                
 80 // private functions                              
 81 //                                                
 82                                                   
 83 //____________________________________________    
 84 G4RootPNtupleDescription*                         
 85 G4RootPNtupleManager::GetNtupleDescriptionInFu    
 86   G4int id, std::string_view functionName, G4b    
 87 {                                                 
 88   auto index = id - fFirstId;                     
 89   if ( index < 0 || index >= G4int(fNtupleDesc    
 90     if ( warn) {                                  
 91       NotExistWarning("ntuple description", id    
 92     }                                             
 93     return nullptr;                               
 94   }                                               
 95                                                   
 96   return fNtupleDescriptionVector[index];         
 97 }                                                 
 98                                                   
 99 //____________________________________________    
100 tools::wroot::base_pntuple*                       
101  G4RootPNtupleManager::GetNtupleInFunction(       
102   G4int id, std::string_view functionName, G4b    
103 {                                                 
104   auto ntupleDescription = GetNtupleDescriptio    
105   if (ntupleDescription == nullptr) return nul    
106                                                   
107   if (ntupleDescription->GetBasePNtuple() == n    
108     if ( warn ) {                                 
109       NotExistWarning("ntuple", id, fkClass, f    
110     }                                             
111     return nullptr;                               
112   }                                               
113   return ntupleDescription->GetBasePNtuple();     
114 }                                                 
115                                                   
116 //____________________________________________    
117 tools::wroot::ntuple*                             
118 G4RootPNtupleManager::GetMainNtupleInFunction(    
119   G4int id, std::string_view functionName, G4b    
120 {                                                 
121   auto& mainNtupleVector = fMainNtupleManager-    
122                                                   
123   auto index = id - fFirstId;                     
124   if ( index < 0 || index >= G4int(mainNtupleV    
125     if ( warn) {                                  
126       NotExistWarning("main ntuple", id, fkCla    
127     }                                             
128     return nullptr;                               
129   }                                               
130                                                   
131   return mainNtupleVector[index];                 
132 }                                                 
133                                                   
134 //                                                
135 // protected functions                            
136 //                                                
137                                                   
138 //____________________________________________    
139 void G4RootPNtupleManager::CreateNtupleFromMai    
140                              G4RootPNtupleDesc    
141                              tools::wroot::ntu    
142 {                                                 
143   // Do not create pntuple if ntuple was delet    
144   if (mainNtuple == nullptr) {                    
145     ntupleDescription->SetNtuple(nullptr);        
146     ntupleDescription->SetBasePNtuple(nullptr)    
147     return;                                       
148   }                                               
149                                                   
150   Message(kVL4, "create from main", "pntuple",    
151                                                   
152   auto file = fMainNtupleManager->GetNtupleFil    
153   if ( ! file ) {                                 
154     Warn("Cannot create pntuple. Main ntuple f    
155       fkClass, "CreateNtupleFromMain");           
156     return;                                       
157   }                                               
158                                                   
159   ntupleDescription->GetDescription().SetFile(    
160                                                   
161   // Get parameters from ntupleDescription        
162   mainNtuple->get_branches(ntupleDescription->    
163                                                   
164   auto rfile = std::get<0>(*file);                
165   G4bool verbose = true;                          
166   if ( fRowWise ) {                               
167     auto mainBranch = mainNtuple->get_row_wise    
168     auto mtNtuple                                 
169       = new tools::wroot::mt_ntuple_row_wise(     
170               G4cout, rfile->byte_swap(), rfil    
171               mainNtuple->dir().seek_directory    
172               *mainBranch, mainBranch->basket_    
173               ntupleDescription->GetDescriptio    
174                                                   
175     ntupleDescription->SetNtuple(                 
176       static_cast<tools::wroot::imt_ntuple*>(m    
177     ntupleDescription->SetBasePNtuple(            
178       static_cast<tools::wroot::base_pntuple*>    
179   }                                               
180   else {                                          
181     std::vector<tools::uint32> basketSizes;       
182     tools_vforcit(tools::wroot::branch*, ntupl    
183       basketSizes.push_back((*it)->basket_size    
184     }                                             
185     auto basketEntries = fMainNtupleManager->G    
186                                                   
187     auto mtNtuple =                               
188       new tools::wroot::mt_ntuple_column_wise(    
189             G4cout, rfile->byte_swap(), rfile-    
190             mainNtuple->dir().seek_directory()    
191             ntupleDescription->GetMainBranches    
192             ntupleDescription->GetDescription(    
193             fRowMode, basketEntries, verbose);    
194                                                   
195     ntupleDescription->SetNtuple(                 
196       static_cast<tools::wroot::imt_ntuple*>(m    
197     ntupleDescription->SetBasePNtuple(            
198       static_cast<tools::wroot::base_pntuple*>    
199   }                                               
200                                                   
201   ntupleDescription->GetDescription().SetIsNtu    
202   //        // pntuple object is not deleted a    
203   fNtupleVector.push_back(ntupleDescription->G    
204                                                   
205   Message(kVL3, "create from main", "pntuple",    
206 }                                                 
207                                                   
208 //____________________________________________    
209 void G4RootPNtupleManager::CreateNtupleDescrip    
210 {                                                 
211 // Create ntuple descriptions from booking.       
212 // This function is called from the first Fill    
213                                                   
214   // Create pntuple descriptions from ntuple b    
215   auto g4NtupleBookings = fBookingManager->Get    
216                                                   
217   for ( auto g4NtupleBooking : g4NtupleBooking    
218     auto ntupleDescription = new G4RootPNtuple    
219     // Save g4booking, activation in pntuple b    
220     fNtupleDescriptionVector.push_back(ntupleD    
221   }                                               
222 }                                                 
223                                                   
224 //____________________________________________    
225 void G4RootPNtupleManager::CreateNtuplesFromMa    
226 {                                                 
227 // Create slave ntuples from  main ntuple.        
228 // This function is called from the first Fill    
229                                                   
230   auto& mainNtupleVector = fMainNtupleManager-    
231                                                   
232   G4int lcounter = 0;                             
233   for ( auto mainNtuple : mainNtupleVector ) {    
234     auto& ntupleDescription = fNtupleDescripti    
235     CreateNtupleFromMain(ntupleDescription, ma    
236   }                                               
237 }                                                 
238                                                   
239 //____________________________________________    
240 void G4RootPNtupleManager::CreateNtuplesIfNeed    
241 {                                                 
242 // The ntuples on workers are created at first    
243 // (if only columns of vector type) call.         
244 // When writing multiple times in teh same fil    
245 // to be recreated as well.                       
246                                                   
247   // G4cout << "G4RootPNtupleManager::CreateNt    
248   //   << " fCreateNtuple: " << fCreateNtuples    
249   //   << " fNewCycle: " << fNewCycle             
250   //   << " fMainNtupleManager->GetNewCycle():    
251   //   << " fNtupleDescriptionVector.size(): "    
252   //   << " fNtupleVector.size(): " << fNtuple    
253   //   << G4endl;                                 
254                                                   
255   if (fCreateNtuples) {                           
256     // create ntuple descriptions                 
257     CreateNtupleDescriptionsFromBooking();        
258                                                   
259     // create main ntuples if needed              
260     G4AutoLock lock(&createMainMutex);            
261     if (fMainNtupleManager->GetNewCycle()) {      
262       fMainNtupleManager->CreateNtuplesFromBoo    
263     }                                             
264     lock.unlock();                                
265                                                   
266     // create slave ntuples                       
267     CreateNtuplesFromMain();                      
268     fCreateNtuples = false;                       
269   }                                               
270                                                   
271   if (fNewCycle) {                                
272     // create main ntuples if needed              
273     G4AutoLock lock(&createMainMutex);            
274     if (fMainNtupleManager->GetNewCycle()) {      
275       fMainNtupleManager->CreateNtuplesFromBoo    
276     }                                             
277     lock.unlock();                                
278                                                   
279     // create slave ntuples                       
280     CreateNtuplesFromMain();                      
281     fNewCycle = false;                            
282   }                                               
283 }                                                 
284                                                   
285 //____________________________________________    
286 G4int G4RootPNtupleManager::CreateNtuple(G4Ntu    
287 {                                                 
288 // Create pntuple from g4 ntuple booking.         
289 // Nothing to be done here.                       
290                                                   
291   return G4Analysis::kInvalidId;                  
292 }                                                 
293                                                   
294 //____________________________________________    
295 G4bool G4RootPNtupleManager::FillNtupleIColumn    
296   G4int ntupleId, G4int columnId, G4int value)    
297 {                                                 
298   return FillNtupleTColumn<int>(ntupleId, colu    
299 }                                                 
300                                                   
301 //____________________________________________    
302 G4bool G4RootPNtupleManager::FillNtupleFColumn    
303   G4int ntupleId, G4int columnId, G4float valu    
304 {                                                 
305   return FillNtupleTColumn<float>(ntupleId, co    
306 }                                                 
307                                                   
308 //____________________________________________    
309 G4bool G4RootPNtupleManager::FillNtupleDColumn    
310   G4int ntupleId, G4int columnId, G4double val    
311 {                                                 
312   return FillNtupleTColumn<double>(ntupleId, c    
313 }                                                 
314                                                   
315 //____________________________________________    
316 G4bool G4RootPNtupleManager::FillNtupleSColumn    
317   G4int ntupleId, G4int columnId, const G4Stri    
318 {                                                 
319   return FillNtupleTColumn<std::string>(ntuple    
320 }                                                 
321                                                   
322 //____________________________________________    
323 G4bool G4RootPNtupleManager::AddNtupleRow(G4in    
324 {                                                 
325   if ( fState.GetIsActivation() && ( ! GetActi    
326     //G4cout << "Skipping AddNtupleRow for " <    
327     return false;                                 
328   }                                               
329                                                   
330   if ( IsVerbose(kVL4) ) {                        
331     Message(kVL4, "add", "pntuple row", " ntup    
332   }                                               
333                                                   
334   // Creating ntuples on workers is triggered     
335   // or AddRow (in only columns of vector type    
336   CreateNtuplesIfNeeded();                        
337                                                   
338   auto ntupleDescription = GetNtupleDescriptio    
339   if (ntupleDescription == nullptr) return fal    
340                                                   
341   auto rfile = std::get<0>(*ntupleDescription-    
342                                                   
343   G4AutoLock lock(&pntupleMutex);                 
344   lock.unlock();                                  
345   mutex toolsLock(lock);                          
346   auto result                                     
347     = ntupleDescription->GetNtuple()->add_row(    
348                                                   
349   if ( ! result ) {                               
350     Warn("NtupleId " + to_string(ntupleId) + "    
351       fkClass, "AddNtupleRow");                   
352   }                                               
353                                                   
354   ntupleDescription->GetDescription().SetHasFi    
355                                                   
356   if ( IsVerbose(kVL3) ) {                        
357     Message(kVL3, "add", "pntuple row", " ntup    
358   }                                               
359                                                   
360                                                   
361   return true;                                    
362 }                                                 
363                                                   
364 //____________________________________________    
365 G4bool G4RootPNtupleManager::Merge()              
366 {                                                 
367   for ( auto ntupleDescription : fNtupleDescri    
368                                                   
369     // skip inactivated ntuples                   
370     if (! ntupleDescription->GetDescription().    
371         (ntupleDescription->GetNtuple() == nul    
372       // G4cout << "skipping inactive ntuple "    
373       continue;                                   
374     }                                             
375                                                   
376     if ( IsVerbose(kVL4) ) {                      
377       Message(kVL4, "merge", "pntuple",           
378         ntupleDescription->GetDescription().Ge    
379     }                                             
380                                                   
381     auto rfile = std::get<0>(*ntupleDescriptio    
382                                                   
383     G4AutoLock lock(&pntupleMutex);               
384     lock.unlock();                                
385     mutex toolsLock(lock);                        
386     auto result                                   
387       = ntupleDescription->GetNtuple()->end_fi    
388                                                   
389     if ( ! result ) {                             
390       Warn("Ntuple " + ntupleDescription->GetD    
391            "end fill has failed.", fkClass, "M    
392     }                                             
393                                                   
394     if ( IsVerbose(kVL3) ) {                      
395       Message(kVL3, "merge", "pntuple",           
396         ntupleDescription->GetDescription().Ge    
397     }                                             
398                                                   
399   }                                               
400                                                   
401   // Set new cycle                                
402   fNewCycle = true;                               
403                                                   
404   return true;                                    
405 }                                                 
406                                                   
407 //____________________________________________    
408 G4bool G4RootPNtupleManager::Reset()              
409 {                                                 
410   // Reset ntuple description, this will delet    
411   // we have its ownership.                       
412   // The ntuples will be recreated with new cy    
413                                                   
414   for ( auto ntupleDescription : fNtupleDescri    
415     ntupleDescription->Reset();                   
416   }                                               
417                                                   
418   fNtupleVector.clear();                          
419                                                   
420   return true;                                    
421 }                                                 
422                                                   
423 //____________________________________________    
424 void G4RootPNtupleManager::Clear()                
425 {                                                 
426   for ( auto ntupleDescription : fNtupleDescri    
427     delete ntupleDescription->GetNtuple();        
428   }                                               
429                                                   
430   fNtupleDescriptionVector.clear();               
431   fNtupleVector.clear();                          
432                                                   
433   Message(kVL2, "clear", "pntuples");             
434 }                                                 
435                                                   
436 //____________________________________________    
437 G4bool G4RootPNtupleManager::Delete(G4int id)     
438 {                                                 
439   if ( IsVerbose(G4Analysis::kVL4) ) {            
440     Message(G4Analysis::kVL4, "delete", "pntup    
441   }                                               
442                                                   
443   auto ntupleDescription = GetNtupleDescriptio    
444                                                   
445   if (ntupleDescription == nullptr) return fal    
446                                                   
447   // Delete/clear ntuple data                     
448   delete ntupleDescription->GetNtuple();          
449   ntupleDescription->SetNtuple(nullptr);          
450   ntupleDescription->SetBasePNtuple(nullptr);     
451   ntupleDescription->GetMainBranches().clear()    
452                                                   
453   // Update ntuple vector                         
454   auto index = id - GetFirstId();                 
455   fNtupleVector[index] = nullptr;                 
456                                                   
457   Message(G4Analysis::kVL2, "delete", "pntuple    
458                                                   
459   return true;                                    
460 }                                                 
461                                                   
462 //____________________________________________    
463                                                   
464 void  G4RootPNtupleManager::SetActivation(        
465   G4bool activation)                              
466 {                                                 
467   for ( auto ntupleDescription : fNtupleDescri    
468     ntupleDescription->GetDescription().SetAct    
469   }                                               
470 }                                                 
471                                                   
472 //____________________________________________    
473                                                   
474 void  G4RootPNtupleManager::SetActivation(        
475   G4int ntupleId, G4bool activation)              
476 {                                                 
477   auto ntupleDescription = GetNtupleDescriptio    
478   if (ntupleDescription == nullptr) return;       
479                                                   
480   ntupleDescription->GetDescription().SetActiv    
481 }                                                 
482                                                   
483 //____________________________________________    
484 G4bool  G4RootPNtupleManager::GetActivation(      
485   G4int ntupleId) const                           
486 {                                                 
487   auto ntupleDescription = GetNtupleDescriptio    
488   if (ntupleDescription == nullptr) return fal    
489                                                   
490   return ntupleDescription->GetDescription().G    
491 }                                                 
492                                                   
493 //____________________________________________    
494 void G4RootPNtupleManager::SetNewCycle(G4bool     
495 {                                                 
496   fNewCycle = value;                              
497 }                                                 
498                                                   
499 //____________________________________________    
500 G4bool G4RootPNtupleManager::GetNewCycle() con    
501 {                                                 
502   return fNewCycle;                               
503 }                                                 
504                                                   
505 //____________________________________________    
506 void G4RootPNtupleManager::SetNtupleRowWise(G4    
507 {                                                 
508   fRowWise = rowWise;                             
509   fRowMode = rowMode;                             
510 }                                                 
511