Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 26 27 // Author: Ivana Hrivnacova, 18/06/2013 (ivan 27 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr) 28 28 29 #include "G4XmlFileManager.hh" 29 #include "G4XmlFileManager.hh" 30 #include "G4XmlHnFileManager.hh" 30 #include "G4XmlHnFileManager.hh" 31 #include "G4AnalysisManagerState.hh" 31 #include "G4AnalysisManagerState.hh" 32 #include "G4AnalysisUtilities.hh" 32 #include "G4AnalysisUtilities.hh" 33 33 34 #include "tools/waxml/begend" 34 #include "tools/waxml/begend" 35 35 36 using namespace G4Analysis; 36 using namespace G4Analysis; 37 using namespace tools; 37 using namespace tools; 38 38 39 //____________________________________________ 39 //_____________________________________________________________________________ 40 G4XmlFileManager::G4XmlFileManager(const G4Ana 40 G4XmlFileManager::G4XmlFileManager(const G4AnalysisManagerState& state) 41 : G4VTFileManager<std::ofstream>(state) 41 : G4VTFileManager<std::ofstream>(state) 42 { 42 { 43 // Create helpers defined in the base class 43 // Create helpers defined in the base class 44 fH1FileManager = std::make_shared<G4XmlHnFil 44 fH1FileManager = std::make_shared<G4XmlHnFileManager<histo::h1d>>(this); 45 fH2FileManager = std::make_shared<G4XmlHnFil 45 fH2FileManager = std::make_shared<G4XmlHnFileManager<histo::h2d>>(this); 46 fH3FileManager = std::make_shared<G4XmlHnFil 46 fH3FileManager = std::make_shared<G4XmlHnFileManager<histo::h3d>>(this); 47 fP1FileManager = std::make_shared<G4XmlHnFil 47 fP1FileManager = std::make_shared<G4XmlHnFileManager<histo::p1d>>(this); 48 fP2FileManager = std::make_shared<G4XmlHnFil 48 fP2FileManager = std::make_shared<G4XmlHnFileManager<histo::p2d>>(this); 49 } 49 } 50 50 51 // 51 // 52 // private methods 52 // private methods 53 // 53 // 54 54 55 //____________________________________________ 55 //_____________________________________________________________________________ 56 G4String G4XmlFileManager::GetNtupleFileName(X 56 G4String G4XmlFileManager::GetNtupleFileName(XmlNtupleDescription* ntupleDescription) 57 { 57 { 58 // get ntuple file name 58 // get ntuple file name 59 auto ntupleFileName = ntupleDescription->Get 59 auto ntupleFileName = ntupleDescription->GetFileName(); 60 if (ntupleFileName.size() != 0u) { 60 if (ntupleFileName.size() != 0u) { 61 // update filename per object per thread 61 // update filename per object per thread 62 ntupleFileName = 62 ntupleFileName = 63 GetTnFileName(ntupleFileName, GetFileTyp 63 GetTnFileName(ntupleFileName, GetFileType()/*, ntupleDescription->GetCycle()*/); 64 } 64 } 65 else { 65 else { 66 // compose ntuple file name from the defau 66 // compose ntuple file name from the default file name 67 ntupleFileName = GetNtupleFileName( 67 ntupleFileName = GetNtupleFileName( 68 ntupleDescription->GetNtupleBooking().na 68 ntupleDescription->GetNtupleBooking().name()/*, ntupleDescription->GetCycle()*/); 69 } 69 } 70 70 71 return ntupleFileName; 71 return ntupleFileName; 72 } 72 } 73 73 74 // 74 // 75 // protected methods 75 // protected methods 76 // 76 // 77 77 78 //____________________________________________ 78 //_____________________________________________________________________________ 79 std::shared_ptr<std::ofstream> G4XmlFileManage 79 std::shared_ptr<std::ofstream> G4XmlFileManager::CreateFileImpl(const G4String& fileName) 80 { 80 { 81 std::shared_ptr<std::ofstream> file = std::m 81 std::shared_ptr<std::ofstream> file = std::make_shared<std::ofstream>(fileName); 82 if ( file->fail() ) { 82 if ( file->fail() ) { 83 Warn(G4String("Cannot create file ") + fil 83 Warn(G4String("Cannot create file ") + fileName, fkClass, "CreateFileImpl"); 84 return nullptr; 84 return nullptr; 85 } 85 } 86 86 87 waxml::begin(*file); 87 waxml::begin(*file); 88 return file; 88 return file; 89 } 89 } 90 90 91 //____________________________________________ 91 //_____________________________________________________________________________ 92 G4bool G4XmlFileManager::WriteFileImpl(std::sh 92 G4bool G4XmlFileManager::WriteFileImpl(std::shared_ptr<std::ofstream> /*file*/) 93 { 93 { 94 // Nothing to be done here 94 // Nothing to be done here 95 return true; 95 return true; 96 } 96 } 97 97 98 //____________________________________________ 98 //_____________________________________________________________________________ 99 G4bool G4XmlFileManager::CloseFileImpl(std::sh 99 G4bool G4XmlFileManager::CloseFileImpl(std::shared_ptr<std::ofstream> file) 100 { 100 { 101 if ( ! file ) return false; 101 if ( ! file ) return false; 102 102 103 // close file 103 // close file 104 waxml::end(*file); 104 waxml::end(*file); 105 file->close(); 105 file->close(); 106 106 107 return true; 107 return true; 108 } 108 } 109 109 110 // 110 // 111 // public methods 111 // public methods 112 // 112 // 113 113 114 //____________________________________________ 114 //_____________________________________________________________________________ 115 G4bool G4XmlFileManager::OpenFile(const G4Stri 115 G4bool G4XmlFileManager::OpenFile(const G4String& fileName) 116 { 116 { 117 // Keep and locks file name 117 // Keep and locks file name 118 fFileName = fileName; 118 fFileName = fileName; 119 auto name = GetFullFileName(fFileName); 119 auto name = GetFullFileName(fFileName); 120 120 121 if ( fFile ) { 121 if ( fFile ) { 122 Warn(G4String("File ") + fileName + " alre 122 Warn(G4String("File ") + fileName + " already exists.", fkClass, "OpenFile"); 123 fFile.reset(); 123 fFile.reset(); 124 } 124 } 125 125 126 // Create histograms file (on master) 126 // Create histograms file (on master) 127 if ( fState.GetIsMaster() ) { 127 if ( fState.GetIsMaster() ) { 128 // Create file (and save in in the file ma 128 // Create file (and save in in the file map (on master only) 129 fFile = CreateTFile(name); 129 fFile = CreateTFile(name); 130 if ( ! fFile) { 130 if ( ! fFile) { 131 Warn(G4String("Failed to create file") + 131 Warn(G4String("Failed to create file") + fileName, fkClass, "OpenFile"); 132 return false; 132 return false; 133 } 133 } 134 } 134 } 135 135 136 fIsOpenFile = true; 136 fIsOpenFile = true; 137 137 138 return true; 138 return true; 139 } 139 } 140 140 141 //____________________________________________ 141 //_____________________________________________________________________________ 142 G4bool G4XmlFileManager::CreateNtupleFile( 142 G4bool G4XmlFileManager::CreateNtupleFile( 143 XmlNtupleDescription* ntupleDescription) 143 XmlNtupleDescription* ntupleDescription) 144 { 144 { 145 // Get ntuple file name per object (if defin 145 // Get ntuple file name per object (if defined) 146 auto ntupleFileName = GetNtupleFileName(ntup 146 auto ntupleFileName = GetNtupleFileName(ntupleDescription); 147 147 148 // Create ntuple file name if it does not ye 148 // Create ntuple file name if it does not yet exist 149 auto ntupleFile = GetTFile(ntupleFileName, f 149 auto ntupleFile = GetTFile(ntupleFileName, false); 150 if ( ntupleFile == nullptr) { 150 if ( ntupleFile == nullptr) { 151 ntupleFile = CreateTFile(ntupleFileName); 151 ntupleFile = CreateTFile(ntupleFileName); 152 } 152 } 153 153 154 ntupleDescription->SetFile(std::move(ntupleF << 154 ntupleDescription->SetFile(ntupleFile); 155 155 156 return (ntupleDescription->GetFile() != null 156 return (ntupleDescription->GetFile() != nullptr); 157 } 157 } 158 158 159 //____________________________________________ 159 //_____________________________________________________________________________ 160 G4bool G4XmlFileManager::CloseNtupleFile( 160 G4bool G4XmlFileManager::CloseNtupleFile( 161 XmlNtupleDescription* ntupleDescription) 161 XmlNtupleDescription* ntupleDescription) 162 { 162 { 163 // Notify not empty file 163 // Notify not empty file 164 auto ntupleFileName = GetNtupleFileName(ntup 164 auto ntupleFileName = GetNtupleFileName(ntupleDescription); 165 auto result = SetIsEmpty(ntupleFileName, ! n 165 auto result = SetIsEmpty(ntupleFileName, ! ntupleDescription->GetHasFill()); 166 166 167 // Ntuple files are registered in file manag 167 // Ntuple files are registered in file manager map. 168 // they will be closed with CloseFiles() cal 168 // they will be closed with CloseFiles() calls 169 ntupleDescription->GetFile().reset(); 169 ntupleDescription->GetFile().reset(); 170 170 171 return result; 171 return result; 172 } 172 } 173 173