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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/factory/src/G4GenericFileManager.cc (Version 11.3.0) and /analysis/factory/src/G4GenericFileManager.cc (Version 8.0)


  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, 15/06/2011  (ivan    
 28                                                   
 29 #include "G4GenericFileManager.hh"                
 30 #include "G4AnalysisManagerState.hh"              
 31 #include "G4AnalysisUtilities.hh"                 
 32 #include "G4CsvFileManager.hh"                    
 33 #include "G4CsvNtupleFileManager.hh"              
 34 #ifdef TOOLS_USE_HDF5                             
 35 #include "G4Hdf5FileManager.hh"                   
 36 #include "G4Hdf5NtupleFileManager.hh"             
 37 #endif                                            
 38 #include "G4RootFileManager.hh"                   
 39 #include "G4RootNtupleFileManager.hh"             
 40 #include "G4XmlFileManager.hh"                    
 41 #include "G4XmlNtupleFileManager.hh"              
 42                                                   
 43 using namespace G4Analysis;                       
 44                                                   
 45 namespace {                                       
 46                                                   
 47 //____________________________________________    
 48 void FileManagerWarning(const G4String& fileNa    
 49                         std::string_view class    
 50                         std::string_view funct    
 51                         G4bool hdf5Warn = true    
 52 {                                                 
 53   if ( GetExtension(fileName) == "hdf5" && ( !    
 54                                                   
 55   Warn("Cannot get file manager for " + fileNa    
 56        className, functionName);                  
 57 }                                                 
 58                                                   
 59 }                                                 
 60                                                   
 61 //____________________________________________    
 62 G4GenericFileManager::G4GenericFileManager(con    
 63  : G4VFileManager(state)                          
 64 {}                                                
 65                                                   
 66 //                                                
 67 // private methods                                
 68 //                                                
 69                                                   
 70 //____________________________________________    
 71 void G4GenericFileManager::CreateFileManager(G    
 72 {                                                 
 73   Message(kVL4, "create", "file manager", GetO    
 74                                                   
 75   auto outputId = static_cast<size_t>(output);    
 76   if ( fFileManagers[outputId] ) {                
 77     Warn("The file manager of " + G4Analysis::    
 78          " type already exists.",                 
 79          fkClass, "CreateFileManager");           
 80     return;                                       
 81   }                                               
 82                                                   
 83   // Create the manager                           
 84   switch ( output ) {                             
 85     case G4AnalysisOutput::kCsv:                  
 86       fCsvFileManager = std::make_shared<G4Csv    
 87       fFileManagers[outputId] = fCsvFileManage    
 88       break;                                      
 89     case G4AnalysisOutput::kHdf5:                 
 90 #ifdef TOOLS_USE_HDF5                             
 91       fHdf5FileManager = std::make_shared<G4Hd    
 92       fFileManagers[outputId] = fHdf5FileManag    
 93 #else                                             
 94       if ( fHdf5Warn) {                           
 95         Warn("Hdf5 type is not available.", fk    
 96         fHdf5Warn = false;                        
 97       }                                           
 98 #endif                                            
 99       break;                                      
100     case G4AnalysisOutput::kRoot:                 
101       fRootFileManager = std::make_shared<G4Ro    
102       fFileManagers[outputId] = fRootFileManag    
103       break;                                      
104     case G4AnalysisOutput::kXml:                  
105       fXmlFileManager = std::make_shared<G4Xml    
106       fFileManagers[outputId] = fXmlFileManage    
107       break;                                      
108     case G4AnalysisOutput::kNone:                 
109       Warn(G4Analysis::GetOutputName(output) +    
110         fkClass, "CreateFileManager");            
111       return;                                     
112   }                                               
113                                                   
114   // Pass directory names (only if set)           
115   if ( ! GetHistoDirectoryName().empty() ) {      
116     fFileManagers[outputId]->SetHistoDirectory    
117   }                                               
118   if ( ! GetNtupleDirectoryName().empty() ) {     
119     fFileManagers[outputId]->SetNtupleDirector    
120   }                                               
121                                                   
122   Message(kVL3, "create", "file manager", GetO    
123 }                                                 
124                                                   
125 //____________________________________________    
126 std::shared_ptr<G4VFileManager>                   
127 G4GenericFileManager::GetFileManager(G4Analysi    
128 {                                                 
129   return fFileManagers[static_cast<size_t>(out    
130 }                                                 
131                                                   
132 //____________________________________________    
133 std::shared_ptr<G4VFileManager>                   
134 G4GenericFileManager::GetFileManager(const G4S    
135 {                                                 
136   // Get file extension                           
137   G4String extension = GetExtension(fileName);    
138   if (extension.size() == 0u) {                   
139     // use the default                            
140     extension = fDefaultFileType;                 
141   }                                               
142                                                   
143   auto output = G4Analysis::GetOutput(extensio    
144   if ( output == G4AnalysisOutput::kNone ) {      
145     Warn("The file extension " + extension + "    
146       fkClass, "GetFileManager");                 
147     return nullptr;                               
148   }                                               
149                                                   
150   std::shared_ptr<G4VFileManager> fileManager     
151   if ( ! GetFileManager(output) ) {               
152     CreateFileManager(output);                    
153     fileManager = GetFileManager(output);         
154   }                                               
155                                                   
156   return GetFileManager(output);                  
157 }                                                 
158                                                   
159 //                                                
160 // public methods                                 
161 //                                                
162                                                   
163 //____________________________________________    
164 G4bool G4GenericFileManager::OpenFile(const G4    
165 {                                                 
166   auto fileManager = GetFileManager(fileName);    
167   if ( ! fileManager ) return false;              
168                                                   
169   if ( fDefaultFileManager && (fDefaultFileMan    
170     // Print warning if default output changed    
171     // (maybe be not needed?)                     
172     Warn("Default file manager changed "          
173          "(old: " +fDefaultFileManager->GetFil    
174          ", new:" + fileManager->GetFileType()    
175          fkClass, "OpenFile");                    
176   }                                               
177   fDefaultFileManager = fileManager;              
178   fDefaultFileType = fileManager->GetFileType(    
179                                                   
180   Message(kVL4, "open", "analysis file", fileN    
181                                                   
182   auto result = true;                             
183                                                   
184   // Save the default file name                   
185   // both in the generic file manager and the     
186   result &= SetFileName(fileName);                
187   result &= fDefaultFileManager->SetFileName(f    
188   result &= fDefaultFileManager->OpenFile(file    
189                                                   
190   LockDirectoryNames();                           
191   fIsOpenFile = true;                             
192                                                   
193   Message(kVL1, "open", "analysis file", fileN    
194                                                   
195   return result;                                  
196 }                                                 
197                                                   
198 //____________________________________________    
199 G4bool G4GenericFileManager::OpenFiles()          
200 {                                                 
201 // Open all files regeistered with objects        
202                                                   
203   Message(kVL4, "open", "analysis files");        
204                                                   
205   auto result = true;                             
206                                                   
207   // process names registered in base file man    
208   for ( const auto& fileName : GetFileNames()     
209     auto fileManager = GetFileManager(fileName    
210     if ( ! fileManager ) {                        
211       FileManagerWarning(fileName, fkClass, "O    
212       continue;                                   
213     }                                             
214                                                   
215     // filenames for csv need to be updated       
216     auto newFileName = fileName;                  
217     if (fileManager == fCsvFileManager) {         
218       newFileName = fileManager->GetHnFileName    
219     }                                             
220                                                   
221     result &= fileManager->CreateFile(newFileN    
222   }                                               
223                                                   
224   Message(kVL3, "open", "analysis files", "",     
225                                                   
226   return result;                                  
227 }                                                 
228                                                   
229 //____________________________________________    
230 G4bool G4GenericFileManager::WriteFiles()         
231 {                                                 
232 // Finish write for all files registered with     
233                                                   
234   Message(kVL4, "write", "analysis files");       
235                                                   
236   auto result = true;                             
237                                                   
238   for ( const auto& fileManager : fFileManager    
239     if ( ! fileManager ) continue;                
240                                                   
241     Message(kVL4, "write", fileManager->GetFil    
242                                                   
243     result &= fileManager->WriteFiles();          
244   }                                               
245                                                   
246   Message(kVL3, "write", "analysis files", "",    
247                                                   
248   return result;                                  
249 }                                                 
250                                                   
251 //____________________________________________    
252 G4bool G4GenericFileManager::CloseFiles()         
253 {                                                 
254 // Close all files regeistered with objects       
255                                                   
256   Message(kVL4, "close", "analysis files");       
257                                                   
258   auto result = true;                             
259                                                   
260   for ( const auto& fileManager : fFileManager    
261     if ( ! fileManager ) continue;                
262                                                   
263     Message(kVL4, "close", fileManager->GetFil    
264                                                   
265     result &= fileManager->CloseFiles();          
266   }                                               
267                                                   
268   fIsOpenFile = false;                            
269                                                   
270   Message(kVL3, "close", "analysis files", "",    
271                                                   
272   return result;                                  
273 }                                                 
274                                                   
275 //____________________________________________    
276 G4bool G4GenericFileManager::DeleteEmptyFiles(    
277 {                                                 
278 // Close all files regeistered with objects       
279                                                   
280   Message(kVL4, "delete", "empty files");         
281                                                   
282   auto result = true;                             
283                                                   
284   for ( const auto& fileManager : fFileManager    
285     if ( ! fileManager ) continue;                
286                                                   
287     Message(kVL4, "delete", fileManager->GetFi    
288                                                   
289     result &= fileManager->DeleteEmptyFiles();    
290   }                                               
291                                                   
292   // Clear all saved file names in base file m    
293   G4BaseFileManager::ClearData();                 
294                                                   
295   Message(kVL3, "delete", "empty files", "", r    
296                                                   
297   return result;                                  
298 }                                                 
299                                                   
300 //____________________________________________    
301 void G4GenericFileManager::Clear()                
302 {                                                 
303 // Clear files data                               
304                                                   
305   for ( const auto& fileManager : fFileManager    
306     if ( ! fileManager ) continue;                
307                                                   
308     fileManager->Clear();                         
309   }                                               
310   UnlockDirectoryNames();                         
311 }                                                 
312                                                   
313 //____________________________________________    
314 G4bool G4GenericFileManager::CreateFile(const     
315 {                                                 
316 // New prototype, fully implemented in templat    
317                                                   
318   auto fileManager = GetFileManager(fileName);    
319   if ( ! fileManager ) {                          
320     FileManagerWarning(fileName, fkClass, "Cre    
321     return false;                                 
322   }                                               
323                                                   
324   return fileManager->CreateFile(fileName);       
325 }                                                 
326                                                   
327 //____________________________________________    
328 G4bool G4GenericFileManager::WriteFile(const G    
329 {                                                 
330 // New prototype, fully implemented in templat    
331                                                   
332   auto fileManager = GetFileManager(fileName);    
333   if ( ! fileManager ) {                          
334     FileManagerWarning(fileName, fkClass, "Wri    
335     return false;                                 
336   }                                               
337                                                   
338   return fileManager->WriteFile(fileName);        
339 }                                                 
340                                                   
341 //____________________________________________    
342 G4bool G4GenericFileManager::CloseFile(const G    
343 {                                                 
344 // New prototype, fully implemented in templat    
345                                                   
346   auto fileManager = GetFileManager(fileName);    
347   if ( ! fileManager ) {                          
348     FileManagerWarning(fileName, fkClass, "Clo    
349     return false;                                 
350   }                                               
351                                                   
352   return fileManager->CloseFile(fileName);        
353 }                                                 
354                                                   
355 //____________________________________________    
356 G4bool G4GenericFileManager::SetIsEmpty(const     
357 {                                                 
358   auto fileManager = GetFileManager(fileName);    
359   if ( ! fileManager ) {                          
360     FileManagerWarning(fileName, fkClass, "Set    
361     return false;                                 
362   }                                               
363                                                   
364   return fileManager->SetIsEmpty(fileName, isE    
365 }                                                 
366                                                   
367 //____________________________________________    
368 G4bool G4GenericFileManager::SetHistoDirectory    
369 {                                                 
370   auto result = G4VFileManager::SetHistoDirect    
371                                                   
372   for (auto& fileManager : fFileManagers ) {      
373     if ( fileManager != nullptr ) {               
374       result &= fileManager->SetHistoDirectory    
375     }                                             
376   }                                               
377   return result;                                  
378 }                                                 
379                                                   
380 //____________________________________________    
381 G4bool G4GenericFileManager::SetNtupleDirector    
382 {                                                 
383   auto result = G4VFileManager::SetNtupleDirec    
384                                                   
385   for (auto& fileManager : fFileManagers ) {      
386     if ( fileManager != nullptr ) {               
387       result &= fileManager->SetNtupleDirector    
388     }                                             
389   }                                               
390   return result;                                  
391 }                                                 
392                                                   
393 //____________________________________________    
394 void G4GenericFileManager::SetCompressionLevel    
395 {                                                 
396   G4BaseFileManager::SetCompressionLevel(level    
397                                                   
398   for (auto& fileManager : fFileManagers ) {      
399     if ( fileManager != nullptr ) {               
400       fileManager->SetCompressionLevel(level);    
401     }                                             
402   }                                               
403 }                                                 
404                                                   
405 //____________________________________________    
406 void G4GenericFileManager::SetDefaultFileType(    
407 {                                                 
408   // Check if value correspond to a valid file    
409   auto output = G4Analysis::GetOutput(value);     
410   if ( output == G4AnalysisOutput::kNone ) {      
411     Warn("The file type " + value + "is not su    
412          "The default type " + fDefaultFileTyp    
413          fkClass, "SetDeafultFileType");          
414     return;                                       
415   }                                               
416                                                   
417   fDefaultFileType = value;                       
418 }                                                 
419                                                   
420 //____________________________________________    
421 std::shared_ptr<G4VNtupleFileManager>             
422 G4GenericFileManager::CreateNtupleFileManager(    
423 {                                                 
424   if ( ! GetFileManager(output) ) {               
425     CreateFileManager(output);                    
426   }                                               
427                                                   
428   std::shared_ptr<G4VNtupleFileManager> vNtupl    
429   G4String failure;                               
430                                                   
431   switch ( output ) {                             
432     case G4AnalysisOutput::kCsv: {                
433       auto ntupleFileManager = std::make_share    
434       ntupleFileManager->SetFileManager(fCsvFi    
435       vNtupleFileManager = ntupleFileManager;     
436       break;                                      
437     }                                             
438     case G4AnalysisOutput::kHdf5: {               
439 #ifdef TOOLS_USE_HDF5                             
440       auto ntupleFileManager = std::make_share    
441       ntupleFileManager->SetFileManager(fHdf5F    
442       vNtupleFileManager = ntupleFileManager;     
443 #else                                             
444       failure = " Hdf5 is not available";         
445 #endif                                            
446       break;                                      
447     }                                             
448     case G4AnalysisOutput::kRoot: {               
449       auto ntupleFileManager = std::make_share    
450       ntupleFileManager->SetFileManager(fRootF    
451       vNtupleFileManager = ntupleFileManager;     
452       break;                                      
453     }                                             
454     case G4AnalysisOutput::kXml: {                
455       auto ntupleFileManager = std::make_share    
456       ntupleFileManager->SetFileManager(fXmlFi    
457       vNtupleFileManager = ntupleFileManager;     
458       break;                                      
459     }                                             
460     case G4AnalysisOutput::kNone:                 
461       break;                                      
462   }                                               
463                                                   
464   if ( ! vNtupleFileManager ) {                   
465       Warn("Failed to create ntuple file manag    
466            G4Analysis::GetOutputName(output) +    
467            fkClass, "CreateNtupleFileManager")    
468   }                                               
469                                                   
470   return vNtupleFileManager;                      
471 }                                                 
472