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