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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/management/include/G4TFileManager.icc (Version 11.3.0) and /analysis/management/include/G4TFileManager.icc (Version 9.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, 18/06/2013  (ivan    
 28                                                   
 29 #include "G4AnalysisUtilities.hh"                 
 30                                                   
 31 //____________________________________________    
 32 template <typename FT>                            
 33 inline                                            
 34 G4TFileManager<FT>::G4TFileManager(const G4Ana    
 35  : fAMState(state)                                
 36 {}                                                
 37                                                   
 38 //____________________________________________    
 39 template <typename FT>                            
 40 inline                                            
 41 G4TFileManager<FT>::~G4TFileManager()             
 42 {                                                 
 43   for ( const auto& mapElement : fFileMap ) {     
 44     delete mapElement.second;                     
 45   }                                               
 46 }                                                 
 47                                                   
 48 //                                                
 49 // private functions                              
 50 //                                                
 51                                                   
 52 //____________________________________________    
 53 template <typename FT>                            
 54 inline                                            
 55 void                                              
 56 G4TFileManager<FT>::FileNotFoundWarning(const     
 57                       std::string_view functio    
 58 {                                                 
 59   G4Analysis::Warn("Failed to get file " + fil    
 60 }                                                 
 61                                                   
 62 //____________________________________________    
 63 template <typename FT>                            
 64 inline                                            
 65 G4TFileInformation<FT>*                           
 66 G4TFileManager<FT>::GetFileInfoInFunction(cons    
 67                       std::string_view functio    
 68 {                                                 
 69   // Find the file information in the map         
 70   auto it = fFileMap.find(fileName);              
 71   if ( it == fFileMap.end() ) {                   
 72     if (warn) {                                   
 73       FileNotFoundWarning(fileName, functionNa    
 74     }                                             
 75     return nullptr;                               
 76   }                                               
 77                                                   
 78   return it->second;                              
 79 }                                                 
 80                                                   
 81 //____________________________________________    
 82 template <typename FT>                            
 83 inline                                            
 84 std::shared_ptr<FT>                               
 85 G4TFileManager<FT>::GetFileInFunction(const G4    
 86                       std::string_view functio    
 87 {                                                 
 88   // Find the file information in the map         
 89   auto fileInfo = GetFileInfoInFunction(fileNa    
 90   if (! fileInfo) return nullptr;                 
 91                                                   
 92   // Check if the file is open                    
 93   if ( ! fileInfo->fFile ) {                      
 94     if (warn) {                                   
 95       FileNotFoundWarning(fileName, functionNa    
 96     }                                             
 97     return nullptr;                               
 98   }                                               
 99                                                   
100   return fileInfo->fFile;                         
101 }                                                 
102                                                   
103 //____________________________________________    
104 template <typename FT>                            
105 inline                                            
106 G4bool G4TFileManager<FT>::WriteTFile(std::sha    
107                                       [[maybe_    
108 {                                                 
109   fAMState.Message(G4Analysis::kVL4, "write",     
110                                                   
111   // Write the found file                         
112   auto result = WriteFileImpl(file);              
113                                                   
114   fAMState.Message(G4Analysis::kVL1, "write",     
115                                                   
116   return result;                                  
117 }                                                 
118                                                   
119 //____________________________________________    
120 template <typename FT>                            
121 inline                                            
122 G4bool G4TFileManager<FT>::CloseTFile(std::sha    
123                                       [[maybe_    
124 {                                                 
125   fAMState.Message(G4Analysis::kVL4, "close",     
126                                                   
127   // Close the found file                         
128   auto result = CloseFileImpl(file);              
129                                                   
130   fAMState.Message(G4Analysis::kVL1, "close",     
131                                                   
132   return result;                                  
133 }                                                 
134                                                   
135 //____________________________________________    
136 template <typename FT>                            
137 inline                                            
138 G4bool G4TFileManager<FT>::DeleteEmptyFile(con    
139 {                                                 
140   fAMState.Message(G4Analysis::kVL4, "delete",    
141                                                   
142   auto result = ! std::remove(fileName);          
143                                                   
144   fAMState.Message(G4Analysis::kVL1, "delete",    
145                                                   
146   return result;                                  
147 }                                                 
148                                                   
149 //____________________________________________    
150 template <typename FT>                            
151 inline                                            
152 void G4TFileManager<FT>::ClearData()              
153 {                                                 
154   for ( const auto& mapElement : fFileMap ) {     
155     delete mapElement.second;                     
156   }                                               
157   fFileMap.clear();                               
158                                                   
159   fAMState.Message(G4Analysis::kVL2, "clear",     
160 }                                                 
161                                                   
162 //                                                
163 // public functions                               
164 //                                                
165                                                   
166 //____________________________________________    
167 template <typename FT>                            
168 inline                                            
169 std::shared_ptr<FT> G4TFileManager<FT>::Create    
170 {                                                 
171   // Just return the file if it already exists    
172   if ( auto file = GetFileInFunction(fileName,    
173       file != nullptr ) {                         
174     return file;                                  
175     // should we also update fileInformation ?    
176   }                                               
177                                                   
178   auto fileInformation = GetFileInfoInFunction    
179   if ( ! fileInformation ) {                      
180     fAMState.Message(G4Analysis::kVL4, "create    
181                                                   
182     // Create file information and save it in     
183     fileInformation = new G4TFileInformation<F    
184     fFileMap[fileName] = fileInformation;         
185   }                                               
186                                                   
187   // Create file and save it in fileInformatio    
188   fAMState.Message(G4Analysis::kVL4, "create",    
189                                                   
190   // Let concrete class create a file             
191   auto file = CreateFileImpl(fileName);           
192   if ( ! file ) {                                 
193     G4Analysis::Warn("Failed to create file "     
194     return nullptr;                               
195   }                                               
196   // Save file in fileInformation                 
197   fileInformation->fFile = file;                  
198   fileInformation->fIsOpen = true;                
199   fileInformation->fIsEmpty = true;               
200   fileInformation->fIsDeleted = false;            
201                                                   
202   fAMState.Message(G4Analysis::kVL1, "create",    
203                                                   
204   // Return created file                          
205   return file;                                    
206 }                                                 
207                                                   
208 //____________________________________________    
209 template <typename FT>                            
210 inline                                            
211 G4bool G4TFileManager<FT>::WriteTFile(const G4    
212 {                                                 
213   // Find the file in the map                     
214   auto file = GetFileInFunction(fileName, "Wri    
215   // Warning is issued in GetFileInfoFunction     
216   if (! file) return false;                       
217                                                   
218   return WriteTFile(file, fileName);              
219 }                                                 
220                                                   
221 //____________________________________________    
222 template <typename FT>                            
223 inline                                            
224 G4bool G4TFileManager<FT>::CloseTFile(const G4    
225 {                                                 
226   // Find the file information in the map         
227   auto fileInfo = GetFileInfoInFunction(fileNa    
228   // Warning is issued in GetFileInfoFunction     
229   if ( ! fileInfo ) return false;                 
230                                                   
231   // Do nothing if file is not open               
232   if ( ! fileInfo->fIsOpen ) return false;        
233                                                   
234   // Get file from the file information           
235   auto file = fileInfo->fFile;                    
236   if ( ! file ) {                                 
237     FileNotFoundWarning(fileName, "CloseTFile"    
238     return false;                                 
239   }                                               
240                                                   
241   auto result = CloseTFile(file, fileName);       
242                                                   
243   // Update the file information                  
244   fileInfo->fFile.reset();                        
245   fileInfo->fIsOpen = false;                      
246                                                   
247   return result;                                  
248 }                                                 
249                                                   
250 //____________________________________________    
251 template <typename FT>                            
252 inline                                            
253 G4bool G4TFileManager<FT>::SetIsEmpty(const G4    
254 {                                                 
255   // Find the file information in the map         
256   auto fileInfo = GetFileInfoInFunction(fileNa    
257                                                   
258   // Warning is issued in GetFileFunction         
259   if ( ! fileInfo ) return false;                 
260                                                   
261   fAMState.Message(G4Analysis::kVL4, "notify n    
262                                                   
263   // Set notification to file information         
264   if ( fileInfo->fIsEmpty ) {                     
265     // do not revert information if file is no    
266     fileInfo->fIsEmpty = isEmpty;                 
267                                                   
268     if ( ! isEmpty ) {                            
269       fAMState.Message(G4Analysis::kVL3, "noti    
270     }                                             
271   }                                               
272                                                   
273   return true;                                    
274 }                                                 
275                                                   
276 //____________________________________________    
277 template <typename FT>                            
278 inline                                            
279 std::shared_ptr<FT> G4TFileManager<FT>::GetTFi    
280   const G4String& fileName, G4bool warn) const    
281 {                                                 
282   // Find the file information in the map         
283   return GetFileInFunction(fileName, "GetTFile    
284 }                                                 
285                                                   
286 //____________________________________________    
287 template <typename FT>                            
288 inline                                            
289 G4bool G4TFileManager<FT>::OpenFiles()            
290 {                                                 
291   auto result = true;                             
292   for ( const auto& mapElement : fFileMap ) {     
293      auto fileInformation = mapElement.second;    
294      // Do nothing if file was open by user ex    
295      if ( fileInformation->fFile ) {              
296        // G4cout << "... skipping open file fo    
297        continue;                                  
298      }                                            
299                                                   
300      result &= (CreateTFile(fileInformation->f    
301   }                                               
302   return result;                                  
303 }                                                 
304                                                   
305 //____________________________________________    
306 template <typename FT>                            
307 inline                                            
308 G4bool G4TFileManager<FT>::WriteFiles()           
309 {                                                 
310   auto result = true;                             
311   for ( const auto& mapElement : fFileMap ) {     
312      auto fileInformation = mapElement.second;    
313      if ( ! fileInformation->fIsOpen ) {          
314        // G4cout << "skipping write for file "    
315        continue;                                  
316      }                                            
317      result &= WriteTFile(fileInformation->fFi    
318   }                                               
319   return result;                                  
320 }                                                 
321                                                   
322 //____________________________________________    
323 template <typename FT>                            
324 inline                                            
325 G4bool G4TFileManager<FT>::CloseFiles()           
326 {                                                 
327   auto result = true;                             
328   for ( const auto& mapElement : fFileMap ) {     
329      auto fileInformation = mapElement.second;    
330      if ( ! fileInformation->fIsOpen ) {          
331        // G4cout << "skipping close for file "    
332        continue;                                  
333      }                                            
334      result &= CloseTFile(fileInformation->fFi    
335                                                   
336      // Update file information                   
337      fileInformation->fFile.reset();              
338      fileInformation->fIsOpen = false;            
339   }                                               
340                                                   
341   // As the files were set to nullptr, clear s    
342   // fFileMap.clear();                            
343                                                   
344   return result;                                  
345 }                                                 
346                                                   
347 //____________________________________________    
348 template <typename FT>                            
349 inline                                            
350 G4bool G4TFileManager<FT>::DeleteEmptyFiles()     
351 {                                                 
352   auto result = true;                             
353   for ( const auto& mapElement : fFileMap ) {     
354     auto fileInformation = mapElement.second;     
355     if ( (! fileInformation->fIsEmpty) || file    
356       // G4cout << "skipping delete for file "    
357       continue;                                   
358     }                                             
359                                                   
360     result &= DeleteEmptyFile(fileInformation-    
361                                                   
362     // Update file information                    
363     fileInformation->fIsDeleted = true;           
364   }                                               
365                                                   
366   return result;                                  
367 }                                                 
368