Geant4-11
G4Hdf5HnFileManager.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/hdf5/header"
32#include "tools/hdf5/h2file"
33#include "tools/histo/h1d"
34#include "tools/histo/h2d"
35#include "tools/histo/h3d"
36#include "tools/histo/p1d"
37#include "tools/histo/p2d"
38
39#include "tools/hdf5/h2file"
40
41//_____________________________________________________________________________
42template <>
43inline
44G4bool G4Hdf5HnFileManager<tools::histo::p1d>::WriteImpl(
45 hid_t hdirectory, tools::histo::p1d* ht, const G4String& htName)
46{
47 return tools::hdf5::write_profile(G4cout, hdirectory, htName, *ht);
48}
49
50//_____________________________________________________________________________
51template <>
52inline
53G4bool G4Hdf5HnFileManager<tools::histo::p2d>::WriteImpl(
54 hid_t hdirectory, tools::histo::p2d* ht, const G4String& htName)
55{
56 return tools::hdf5::write_profile(G4cout, hdirectory, htName, *ht);
57}
58
59//_____________________________________________________________________________
60template <typename HT>
61inline
62G4bool G4Hdf5HnFileManager<HT>::WriteImpl(
63 hid_t hdirectory, HT* ht, const G4String& htName)
64{
65 return tools::hdf5::write_histo(G4cout, hdirectory, htName, *ht);
66}
67
68//_____________________________________________________________________________
69template <typename HT>
70inline
71G4bool G4Hdf5HnFileManager<HT>::WriteExtra(
72 HT* ht, const G4String& htName, const G4String& fileName)
73{
74 // create a new file
75 // create new file
76 hid_t hfile = ::H5Fcreate(fileName, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
77
78 // Do nothing if there is no file
79 // (te error should be handled by caller)
80 if ( hfile < 0 ) {
81 G4cerr << "::H5Fcreate failed " << G4endl;
82 return false;
83 }
84
85 // create a header with general infos :
86 if(!tools::hdf5::write_header(hfile)) {
87 G4cerr << "tools::hdf5::write_header() failed." << std::endl;
88 return false;
89 }
90
91 // Create directories
92 hid_t hdirectory = tools_H5Gcreate(hfile, "histograms", 0);
93 if ( hdirectory < 0 ) {
94 G4cerr << "tools_H5Gcreate failed " << G4endl;
95 return false;
96 }
97
98 auto result = tools::hdf5::write_atb(hdirectory, "type", "directory");
99 if ( ! result) {
100 G4cerr << "tools::hdf5::write_atb failed " << G4endl;
101 return false;
102 }
103
104 result = WriteImpl(hdirectory, ht, htName);
105 if ( ! result) {
106 G4cerr << "Writing HT failed" << G4endl;
107 return false;
108 }
109
110 // close file
111 ::H5Gclose(hdirectory);
112 ::H5Gclose(hfile);
113 return true;
114}
115
116//_____________________________________________________________________________
117template <typename HT>
118inline
119G4bool G4Hdf5HnFileManager<HT>::Write(
120 HT* ht, const G4String& htName, G4String& fileName)
121{
122 if (fileName.empty()) {
123 // should not happen
124 G4cerr << "!!! Hdf5 file name not defined." << G4endl;
125 G4cerr << "!!! Write " << htName << " failed." << G4endl;
126 return false;
127 }
128
129 auto hdirectory = std::get<1>(*fFileManager->GetTFile(fileName));
130 if ( hdirectory < 0 ) {
131 G4Analysis::Warn(
132 "Failed to get Hdf5 file " + fileName + " histo directory.",
133 fkClass, "Write");
134 return false;
135 }
136
137 auto result = WriteImpl(hdirectory, ht, htName);
138 fFileManager->LockDirectoryNames();
139 return result;
140}