Geant4-11
G4Hdf5NtupleManager.hh
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// Manager class for Hdf5 ntuples
28//
29// Author: Ivana Hrivnacova, 20/07/2017 (ivana@ipno.in2p3.fr)
30
31#ifndef G4Hdf5NtupleManager_h
32#define G4Hdf5NtupleManager_h 1
33
34#include "G4TNtupleManager.hh"
36#include "globals.hh"
37
38#include "tools/hdf5/ntuple"
39
40#include <vector>
41#include <memory>
42
44
45// Types alias
46using G4Hdf5File = std::tuple<hid_t, hid_t, hid_t>;
48
49// template specialization used by this class defined below
50
51template <>
52template <>
54 G4int ntupleId, G4int columnId, const std::string& value);
55
56
57class G4Hdf5NtupleManager : public G4TNtupleManager<tools::hdf5::ntuple,
58 G4Hdf5File>
59{
62
63 public:
64 explicit G4Hdf5NtupleManager(const G4AnalysisManagerState& state);
66 virtual ~G4Hdf5NtupleManager() = default;
67
68 private:
69 // Set methods
70 void SetFileManager(std::shared_ptr<G4Hdf5FileManager> fileManager);
71
72 // Access to ntuple vector (needed for Write())
73 const std::vector<Hdf5NtupleDescription*>& GetNtupleDescriptionVector() const;
74
75 // Utility function
76 void CreateTNtuple(Hdf5NtupleDescription* ntupleDescription, G4bool warn);
77
78 // Methods from the templated base class
79 //
80 virtual void CreateTNtupleFromBooking(
81 Hdf5NtupleDescription* ntupleDescription) final;
82
83 virtual void FinishTNtuple(
84 Hdf5NtupleDescription* ntupleDescription,
85 G4bool fromBooking) final;
86
87 // Static data members
88 static constexpr std::string_view fkClass { "G4Hdf5NtupleManager" };
89
90 // Data members
91 std::shared_ptr<G4Hdf5FileManager> fFileManager { nullptr };
92};
93
94// inline functions
95
96using std::to_string;
97
98inline void
99G4Hdf5NtupleManager::SetFileManager(std::shared_ptr<G4Hdf5FileManager> fileManager)
100{ fFileManager = fileManager; }
101
102inline const std::vector<Hdf5NtupleDescription*>&
104{ return fNtupleDescriptionVector; }
105
106template <>
107template <>
109 G4int ntupleId, G4int columnId, const std::string& value)
110{
111 if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
112 //G4cout << "Skipping FillNtupleIColumn for " << ntupleId << G4endl;
113 return false;
114 }
115
116 // get ntuple
117 auto ntuple = GetNtupleInFunction(ntupleId, "FillNtupleTColumn");
118 if ( ! ntuple ) return false;
119
120 // get generic column
121 auto index = columnId - fFirstNtupleColumnId;
122 if ( index < 0 || index >= G4int(ntuple->columns().size()) ) {
124 "ntupleId " + to_string(ntupleId) + " columnId " + to_string(columnId) +
125 " does not exist.", fkClass, "FillNtupleTColumn");
126 return false;
127 }
128 auto icolumn = ntuple->columns()[index];
129
130 // get column and check its type
131 auto column = dynamic_cast<tools::hdf5::ntuple::column_string* >(icolumn);
132 if ( ! column ) {
134 "Column type does not match: ntupleId " + to_string(ntupleId) +
135 " columnId " + to_string(columnId) + " value " + value,
136 fkClass, "FillNtupleTColumn");
137 return false;
138 }
139
140 column->fill(value);
141
143 Message(G4Analysis::kVL4, "fill", "ntuple T column",
144 " ntupleId " + to_string(ntupleId) +
145 " columnId " + to_string(columnId) +
146 " value " + G4Analysis::ToString(value));
147 }
148
149 return true;
150}
151
152#endif
std::tuple< hid_t, hid_t, hid_t > G4Hdf5File
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
void Message(G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
const G4AnalysisManagerState & fState
G4bool IsVerbose(G4int verboseLevel) const
virtual void FinishTNtuple(Hdf5NtupleDescription *ntupleDescription, G4bool fromBooking) final
virtual ~G4Hdf5NtupleManager()=default
const std::vector< Hdf5NtupleDescription * > & GetNtupleDescriptionVector() const
virtual void CreateTNtupleFromBooking(Hdf5NtupleDescription *ntupleDescription) final
void SetFileManager(std::shared_ptr< G4Hdf5FileManager > fileManager)
G4Hdf5NtupleManager()=delete
void CreateTNtuple(Hdf5NtupleDescription *ntupleDescription, G4bool warn)
std::shared_ptr< G4Hdf5FileManager > fFileManager
static constexpr std::string_view fkClass
virtual G4bool GetActivation(G4int ntupleId) const final
G4bool FillNtupleTColumn(G4int ntupleId, G4int columnId, const T &value)
std::vector< G4TNtupleDescription< tools::hdf5::ntuple, G4Hdf5File > * > fNtupleDescriptionVector
static constexpr std::string_view fkClass
NT * GetNtupleInFunction(G4int id, std::string_view function, G4bool warn=true) const
std::string ToString(const T &value)
constexpr G4int kVL4
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)