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 // 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" 35 #include "G4AnalysisUtilities.hh" 36 #include "globals.hh" 37 38 #include "toolx/hdf5/ntuple" 39 40 #include <memory> 41 #include <utility> 42 #include <vector> 43 44 class G4Hdf5FileManager; 45 46 // Types alias 47 using G4Hdf5File = std::tuple<hid_t, hid_t, hid_t>; 48 using Hdf5NtupleDescription = G4TNtupleDescription<toolx::hdf5::ntuple, G4Hdf5File>; 49 50 // template specialization used by this class defined below 51 52 template <> 53 template <> 54 G4bool G4TNtupleManager<toolx::hdf5::ntuple, G4Hdf5File>::FillNtupleTColumn( 55 G4int ntupleId, G4int columnId, const std::string& value); 56 57 58 class G4Hdf5NtupleManager : public G4TNtupleManager<toolx::hdf5::ntuple, 59 G4Hdf5File> 60 { 61 friend class G4Hdf5AnalysisManager; 62 friend class G4Hdf5NtupleFileManager; 63 64 public: 65 explicit G4Hdf5NtupleManager(const G4AnalysisManagerState& state); 66 G4Hdf5NtupleManager() = delete; 67 ~G4Hdf5NtupleManager() override = default; 68 69 private: 70 // Set methods 71 void SetFileManager(std::shared_ptr<G4Hdf5FileManager> fileManager); 72 73 // Access to ntuple vector (needed for Write()) 74 const std::vector<Hdf5NtupleDescription*>& GetNtupleDescriptionVector() const; 75 76 // Utility function 77 void CreateTNtuple(Hdf5NtupleDescription* ntupleDescription, G4bool warn); 78 79 // Methods from the templated base class 80 // 81 void CreateTNtupleFromBooking(Hdf5NtupleDescription* ntupleDescription) final; 82 83 void FinishTNtuple(Hdf5NtupleDescription* ntupleDescription, G4bool fromBooking) final; 84 85 // Static data members 86 static constexpr std::string_view fkClass { "G4Hdf5NtupleManager" }; 87 88 // Data members 89 std::shared_ptr<G4Hdf5FileManager> fFileManager { nullptr }; 90 }; 91 92 // inline functions 93 94 using std::to_string; 95 96 inline void 97 G4Hdf5NtupleManager::SetFileManager(std::shared_ptr<G4Hdf5FileManager> fileManager) 98 { 99 fFileManager = std::move(fileManager); 100 } 101 102 inline const std::vector<Hdf5NtupleDescription*>& 103 G4Hdf5NtupleManager::GetNtupleDescriptionVector() const 104 { return fNtupleDescriptionVector; } 105 106 template <> 107 template <> 108 inline G4bool G4TNtupleManager<toolx::hdf5::ntuple, G4Hdf5File>::FillNtupleTColumn( 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 == nullptr) return false; 119 120 // get generic column 121 auto index = columnId - fFirstNtupleColumnId; 122 if ( index < 0 || index >= G4int(ntuple->columns().size()) ) { 123 G4Analysis::Warn( 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<toolx::hdf5::ntuple::column_string* >(icolumn); 132 if (column == nullptr) { 133 G4Analysis::Warn( 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 142 if ( IsVerbose(G4Analysis::kVL4) ) { 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 153