Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:59:09

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 
0027 // Author: Ivana Hrivnacova, 18/06/2013  (ivana@ipno.in2p3.fr)
0028 
0029 #include "G4AnalysisUtilities.hh"
0030 
0031 //_____________________________________________________________________________
0032 template <typename FT>
0033 inline
0034 G4TFileManager<FT>::G4TFileManager(const G4AnalysisManagerState& state)
0035  : fAMState(state)
0036 {}
0037 
0038 //_____________________________________________________________________________
0039 template <typename FT>
0040 inline
0041 G4TFileManager<FT>::~G4TFileManager()
0042 {
0043   for ( const auto& mapElement : fFileMap ) {
0044     delete mapElement.second;
0045   }
0046 }
0047 
0048 //
0049 // private functions
0050 //
0051 
0052 //_____________________________________________________________________________
0053 template <typename FT>
0054 inline
0055 void
0056 G4TFileManager<FT>::FileNotFoundWarning(const G4String& fileName,
0057                       std::string_view functionName) const
0058 {
0059   G4Analysis::Warn("Failed to get file " + fileName, fkClass, functionName);
0060 }
0061 
0062 //_____________________________________________________________________________
0063 template <typename FT>
0064 inline
0065 G4TFileInformation<FT>*
0066 G4TFileManager<FT>::GetFileInfoInFunction(const G4String& fileName,
0067                       std::string_view functionName, G4bool warn ) const
0068 {
0069   // Find the file information in the map
0070   auto it = fFileMap.find(fileName);
0071   if ( it == fFileMap.end() ) {
0072     if (warn) {
0073       FileNotFoundWarning(fileName, functionName);
0074     }
0075     return nullptr;
0076   }
0077 
0078   return it->second;
0079 }
0080 
0081 //_____________________________________________________________________________
0082 template <typename FT>
0083 inline
0084 std::shared_ptr<FT>
0085 G4TFileManager<FT>::GetFileInFunction(const G4String& fileName,
0086                       std::string_view functionName, G4bool warn) const
0087 {
0088   // Find the file information in the map
0089   auto fileInfo = GetFileInfoInFunction(fileName, functionName, warn);
0090   if (! fileInfo) return nullptr;
0091 
0092   // Check if the file is open
0093   if ( ! fileInfo->fFile ) {
0094     if (warn) {
0095       FileNotFoundWarning(fileName, functionName);
0096     }
0097     return nullptr;
0098   }
0099 
0100   return fileInfo->fFile;
0101 }
0102 
0103 //_____________________________________________________________________________
0104 template <typename FT>
0105 inline
0106 G4bool G4TFileManager<FT>::WriteTFile(std::shared_ptr<FT> file,
0107                                       [[maybe_unused]] const G4String& fileName)
0108 {
0109   fAMState.Message(G4Analysis::kVL4, "write", "file", fileName);
0110 
0111   // Write the found file
0112   auto result = WriteFileImpl(file);
0113 
0114   fAMState.Message(G4Analysis::kVL1, "write", "file", fileName, result);
0115 
0116   return result;
0117 }
0118 
0119 //_____________________________________________________________________________
0120 template <typename FT>
0121 inline
0122 G4bool G4TFileManager<FT>::CloseTFile(std::shared_ptr<FT> file,
0123                                       [[maybe_unused]] const G4String& fileName)
0124 {
0125   fAMState.Message(G4Analysis::kVL4, "close", "file", fileName);
0126 
0127   // Close the found file
0128   auto result = CloseFileImpl(file);
0129 
0130   fAMState.Message(G4Analysis::kVL1, "close", "file", fileName, result);
0131 
0132   return result;
0133 }
0134 
0135 //_____________________________________________________________________________
0136 template <typename FT>
0137 inline
0138 G4bool G4TFileManager<FT>::DeleteEmptyFile(const G4String& fileName)
0139 {
0140   fAMState.Message(G4Analysis::kVL4, "delete", "empty file", fileName);
0141 
0142   auto result = ! std::remove(fileName);
0143 
0144   fAMState.Message(G4Analysis::kVL1, "delete", "empty file", fileName, result);
0145 
0146   return result;
0147 }
0148 
0149 //_____________________________________________________________________________
0150 template <typename FT>
0151 inline
0152 void G4TFileManager<FT>::ClearData()
0153 {
0154   for ( const auto& mapElement : fFileMap ) {
0155     delete mapElement.second;
0156   }
0157   fFileMap.clear();
0158 
0159   fAMState.Message(G4Analysis::kVL2, "clear", "files");
0160 }
0161 
0162 //
0163 // public functions
0164 //
0165 
0166 //_____________________________________________________________________________
0167 template <typename FT>
0168 inline
0169 std::shared_ptr<FT> G4TFileManager<FT>::CreateTFile(const G4String& fileName)
0170 {
0171   // Just return the file if it already exists
0172   if ( auto file = GetFileInFunction(fileName, "CreateTFile", false);
0173       file != nullptr ) {
0174     return file;
0175     // should we also update fileInformation ??
0176   }
0177 
0178   auto fileInformation = GetFileInfoInFunction(fileName, "CreateTFile", false);
0179   if ( ! fileInformation ) {
0180     fAMState.Message(G4Analysis::kVL4, "create", "fileInformation", fileName);
0181 
0182     // Create file information and save it in the map
0183     fileInformation = new G4TFileInformation<FT>(fileName);
0184     fFileMap[fileName] = fileInformation;
0185   }
0186 
0187   // Create file and save it in fileInformation
0188   fAMState.Message(G4Analysis::kVL4, "create", "file", fileName);
0189 
0190   // Let concrete class create a file
0191   auto file = CreateFileImpl(fileName);
0192   if ( ! file ) {
0193     G4Analysis::Warn("Failed to create file " + fileName, fkClass, "CreateTFile");
0194     return nullptr;
0195   }
0196   // Save file in fileInformation
0197   fileInformation->fFile = file;
0198   fileInformation->fIsOpen = true;
0199   fileInformation->fIsEmpty = true;
0200   fileInformation->fIsDeleted = false;
0201 
0202   fAMState.Message(G4Analysis::kVL1, "create", "file", fileName);
0203 
0204   // Return created file
0205   return file;
0206 }
0207 
0208 //_____________________________________________________________________________
0209 template <typename FT>
0210 inline
0211 G4bool G4TFileManager<FT>::WriteTFile(const G4String& fileName)
0212 {
0213   // Find the file in the map
0214   auto file = GetFileInFunction(fileName, "WriteTFile");
0215   // Warning is issued in GetFileInfoFunction
0216   if (! file) return false;
0217 
0218   return WriteTFile(file, fileName);
0219 }
0220 
0221 //_____________________________________________________________________________
0222 template <typename FT>
0223 inline
0224 G4bool G4TFileManager<FT>::CloseTFile(const G4String& fileName)
0225 {
0226   // Find the file information in the map
0227   auto fileInfo = GetFileInfoInFunction(fileName, "CloseTFile");
0228   // Warning is issued in GetFileInfoFunction
0229   if ( ! fileInfo ) return false;
0230 
0231   // Do nothing if file is not open
0232   if ( ! fileInfo->fIsOpen ) return false;
0233 
0234   // Get file from the file information
0235   auto file = fileInfo->fFile;
0236   if ( ! file ) {
0237     FileNotFoundWarning(fileName, "CloseTFile");
0238     return false;
0239   }
0240 
0241   auto result = CloseTFile(file, fileName);
0242 
0243   // Update the file information
0244   fileInfo->fFile.reset();
0245   fileInfo->fIsOpen = false;
0246 
0247   return result;
0248 }
0249 
0250 //_____________________________________________________________________________
0251 template <typename FT>
0252 inline
0253 G4bool G4TFileManager<FT>::SetIsEmpty(const G4String& fileName, G4bool isEmpty)
0254 {
0255   // Find the file information in the map
0256   auto fileInfo = GetFileInfoInFunction(fileName, "SetIsEmpty");
0257 
0258   // Warning is issued in GetFileFunction
0259   if ( ! fileInfo ) return false;
0260 
0261   fAMState.Message(G4Analysis::kVL4, "notify not empty", "file", fileName);
0262 
0263   // Set notification to file information
0264   if ( fileInfo->fIsEmpty ) {
0265     // do not revert information if file is not empty
0266     fileInfo->fIsEmpty = isEmpty;
0267 
0268     if ( ! isEmpty ) {
0269       fAMState.Message(G4Analysis::kVL3, "notify not empty", "file", fileName);
0270     }
0271   }
0272 
0273   return true;
0274 }
0275 
0276 //_____________________________________________________________________________
0277 template <typename FT>
0278 inline
0279 std::shared_ptr<FT> G4TFileManager<FT>::GetTFile(
0280   const G4String& fileName, G4bool warn) const
0281 {
0282   // Find the file information in the map
0283   return GetFileInFunction(fileName, "GetTFile", warn);
0284 }
0285 
0286 //_____________________________________________________________________________
0287 template <typename FT>
0288 inline
0289 G4bool G4TFileManager<FT>::OpenFiles()
0290 {
0291   auto result = true;
0292   for ( const auto& mapElement : fFileMap ) {
0293      auto fileInformation = mapElement.second;
0294      // Do nothing if file was open by user explicitly
0295      if ( fileInformation->fFile ) {
0296        // G4cout << "... skipping open file for " << fileInformation->fFileName << G4endl;
0297        continue;
0298      }
0299 
0300      result &= (CreateTFile(fileInformation->fFileName) != nullptr);
0301   }
0302   return result;
0303 }
0304 
0305 //_____________________________________________________________________________
0306 template <typename FT>
0307 inline
0308 G4bool G4TFileManager<FT>::WriteFiles()
0309 {
0310   auto result = true;
0311   for ( const auto& mapElement : fFileMap ) {
0312      auto fileInformation = mapElement.second;
0313      if ( ! fileInformation->fIsOpen ) {
0314        // G4cout << "skipping write for file " << fileInformation->fFileName << G4endl;
0315        continue;
0316      }
0317      result &= WriteTFile(fileInformation->fFile, fileInformation->fFileName);
0318   }
0319   return result;
0320 }
0321 
0322 //_____________________________________________________________________________
0323 template <typename FT>
0324 inline
0325 G4bool G4TFileManager<FT>::CloseFiles()
0326 {
0327   auto result = true;
0328   for ( const auto& mapElement : fFileMap ) {
0329      auto fileInformation = mapElement.second;
0330      if ( ! fileInformation->fIsOpen ) {
0331        // G4cout << "skipping close for file " << fileInformation->fFileName << G4endl;
0332        continue;
0333      }
0334      result &= CloseTFile(fileInformation->fFile, fileInformation->fFileName);
0335 
0336      // Update file information
0337      fileInformation->fFile.reset();
0338      fileInformation->fIsOpen = false;
0339   }
0340 
0341   // As the files were set to nullptr, clear should not be needed
0342   // fFileMap.clear();
0343 
0344   return result;
0345 }
0346 
0347 //_____________________________________________________________________________
0348 template <typename FT>
0349 inline
0350 G4bool G4TFileManager<FT>::DeleteEmptyFiles()
0351 {
0352   auto result = true;
0353   for ( const auto& mapElement : fFileMap ) {
0354     auto fileInformation = mapElement.second;
0355     if ( (! fileInformation->fIsEmpty) || fileInformation->fIsDeleted ) {
0356       // G4cout << "skipping delete for file " << fileInformation->fFileName << G4endl;
0357       continue;
0358     }
0359 
0360     result &= DeleteEmptyFile(fileInformation->fFileName);
0361 
0362     // Update file information
0363     fileInformation->fIsDeleted = true;
0364   }
0365 
0366   return result;
0367 }