Geant4-11
G4XmlHnFileManager.icc
Go to the documentation of this file.
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, 15/09/2020 (ivana@ipno.in2p3.fr)
28
29#include "G4AnalysisUtilities.hh"
30
31#include "tools/waxml/histos"
32#include "tools/waxml/begend"
33
34//_____________________________________________________________________________
35template <typename HT>
36G4bool G4XmlHnFileManager<HT>::WriteExtra(
37 HT* ht, const G4String& htName, const G4String& fileName)
38{
39 // create a new file
40 std::ofstream hnFile(fileName);
41
42 // Do nothing if there is no file
43 if ( ! hnFile.is_open() ) return false;
44
45 // Write file opening
46 tools::waxml::begin(hnFile);
47
48 // no directory supported in this mode
49 G4String path = "/";
50
51 // write histo
52 auto result = tools::waxml::write(hnFile, *ht, path, htName);
53 if ( ! result) {
54 // TO DO: give an exception about failure
55 return false;
56 }
57
58 // close file
59 tools::waxml::end(hnFile);
60 hnFile.close();
61 return true;
62}
63
64//_____________________________________________________________________________
65template <typename HT>
66G4bool G4XmlHnFileManager<HT>::Write(
67 HT* ht, const G4String& htName, G4String& fileName)
68{
69 if ( fileName.empty()) {
70 // should not happen
71 G4cerr << "!!! Xml file name not defined." << G4endl;
72 G4cerr << "!!! Write " << htName << " failed." << G4endl;
73 return false;
74 }
75
76 auto hnFile = fFileManager->GetTFile(fileName);
77 if ( hnFile == nullptr ) {
78 // This prototype can be used only with OpenFile() mode
79 G4Analysis::Warn(
80 G4String("Failed to get Xml file ") + fileName, fkClass, "Write");
81 return false;
82 }
83
84 G4String path = "/";
85 path.append(fFileManager->GetHistoDirectoryName());
86
87 auto result = tools::waxml::write(*hnFile, *ht, path, htName);
88 fFileManager->LockDirectoryNames();
89 return result;
90}