Loading...
Searching...
No Matches
G4RootMpiPNtupleManager.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// Class for Root MPI pntuple management.
28// The class handles ntuples on the processing MPI ranks when MPI ntuple merging
29// is activated.
30// This class is temporarily provided with g4mpi,
31// it will be integrated in Geant4 analysis category in future.
32//
33// Author: Ivana Hrivnacova, 21/11/2018 (ivana@ipno.in2p3.fr)
34
35#ifndef G4RootMpiPNtupleManager_h
36#define G4RootMpiPNtupleManager_h 1
37
38#include "G4BaseNtupleManager.hh"
40#include "G4AnalysisManagerState.hh"
41#include "G4AnalysisUtilities.hh"
42#include "globals.hh"
43
44#include "tools/wroot/base_pntuple"
45
46#include <vector>
47#include <string_view>
48
49using std::to_string;
50
52
53namespace tools {
54namespace wroot {
55class file;
56class directory;
57class impi_ntuple;
58}
59}
60
62{
64
65 public:
67 tools::impi* impi, G4int mpiRank, G4int destinationRank);
69
70 private:
71 // Functions specific to the output type
72 // void SetNtupleDirectory(tools::wroot::directory* directory);
73 void SetFileManager(std::shared_ptr<G4RootFileManager> fileManager);
74
75 // Methods to manipulate ntuples
76 void CreateNtuple(G4RootMpiPNtupleDescription* ntupleDescription);
77 void CreateNtuplesFromBooking(const std::vector<G4NtupleBooking*>& ntupleBookings);
78
79 // Methods to create ntuples
80 //
81 virtual G4int CreateNtuple(G4NtupleBooking* booking) final;
82
83 // Methods to fill ntuples
84 // Methods for ntuple with id = FirstNtupleId (from base class)
85 using G4BaseNtupleManager::FillNtupleIColumn;
86 using G4BaseNtupleManager::FillNtupleFColumn;
87 using G4BaseNtupleManager::FillNtupleDColumn;
88 using G4BaseNtupleManager::FillNtupleSColumn;
89 using G4BaseNtupleManager::AddNtupleRow;
90 // Methods for ntuple with id > FirstNtupleId (when more ntuples exist)
91 virtual G4bool FillNtupleIColumn(G4int ntupleId, G4int columnId, G4int value) final;
92 virtual G4bool FillNtupleFColumn(G4int ntupleId, G4int columnId, G4float value) final;
93 virtual G4bool FillNtupleDColumn(G4int ntupleId, G4int columnId, G4double value) final;
94 virtual G4bool FillNtupleSColumn(G4int ntupleId, G4int columnId,
95 const G4String& value) final;
96 virtual G4bool AddNtupleRow(G4int ntupleId) final;
97 virtual G4bool Merge() final;
98
99 // Clear all data
100 virtual G4bool Reset() final;
101 virtual void Clear() final;
102
103 // Method to delete selected ntuple
104 G4bool Delete(G4int id) final;
105
106 // Activation option
107 //
108 virtual void SetActivation(G4bool activation) final;
109 virtual void SetActivation(G4int ntupleId, G4bool activation) final;
110 virtual G4bool GetActivation(G4int ntupleId) const final;
111
112 // New cycle option
113 void SetNewCycle(G4bool value) final;
114 G4bool GetNewCycle() const final;
115
116 // Access methods
117 virtual G4int GetNofNtuples() const final;
118
119 private:
121 GetNtupleDescriptionInFunction(G4int id, G4String function, G4bool warn = true) const;
122 tools::wroot::base_pntuple*
123 GetNtupleInFunction(G4int id, G4String function, G4bool warn = true) const;
124
125 template <typename T>
126 G4bool FillNtupleTColumn(G4int ntupleId, G4int columnId, const T& value);
127
128 // Static data members
129 static constexpr std::string_view fkClass { "G4RootMpiPNtupleManager" };
130
131 // Data members
132 // G4RootMpiMainNtupleManager* fMpiMainNtupleManager;
133 std::shared_ptr<G4RootFileManager> fFileManager;
134 std::vector<G4RootMpiPNtupleDescription*> fNtupleDescriptionVector;
135 std::vector<tools::wroot::impi_ntuple*> fNtupleVector;
136 tools::impi* fImpi;
137 G4int fMpiRank;
139 G4bool fNewCycle { false };
140};
141
142// inline functions
143
144inline void
145G4RootMpiPNtupleManager::SetFileManager(std::shared_ptr<G4RootFileManager> fileManager)
146{ fFileManager = fileManager; }
147
148//_____________________________________________________________________________
149template <>
151 G4int ntupleId, G4int columnId, const std::string& value)
152{
153 if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
154 G4cout << "Skipping FillNtupleIColumn for " << ntupleId << G4endl;
155 return false;
156 }
157
158 if ( IsVerbose(G4Analysis::kVL4) ) {
159 Message(G4Analysis::kVL4, "fill", "pntuple T column",
160 " ntupleId " + to_string(ntupleId) + " columnId " + to_string(columnId) +
161 " value " + G4Analysis::ToString(value));
162 }
163
164 auto ntuple = GetNtupleInFunction(ntupleId, "FillNtupleTColumn");
165 if ( ! ntuple ) return false;
166
167 auto index = columnId - fFirstNtupleColumnId;
168 if ( index < 0 || index >= G4int(ntuple->columns().size()) ) {
169 G4ExceptionDescription description;
170 description << " " << "ntupleId " << ntupleId
171 << " columnId " << columnId << " does not exist.";
172 G4Exception("G4RootNtupleManager::FillNtupleTColumn()",
173 "Analysis_W011", JustWarning, description);
174 return false;
175 }
176
177 auto icolumn = ntuple->columns()[index];
178 auto column = dynamic_cast<tools::wroot::base_pntuple::column_string* >(icolumn);
179 if ( ! column ) {
180 G4ExceptionDescription description;
181 description << " Column type does not match: "
182 << " ntupleId " << ntupleId
183 << " columnId " << columnId << " value " << value;
184 G4Exception("G4RootNtupleManager:FillNtupleColumn",
185 "Analysis_W011", JustWarning, description);
186 return false;
187 }
188
189 column->fill(value);
190
191 if ( IsVerbose(G4Analysis::kVL4) ) {
192 Message(G4Analysis::kVL4, "done fill", "pntuple T column",
193 " ntupleId " + to_string(ntupleId) +
194 " columnId " + to_string(columnId) +
195 " value " + value);
196 }
197
198 return true;
199}
200
201//_____________________________________________________________________________
202template <typename T>
204 G4int ntupleId, G4int columnId, const T& value)
205{
206 if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
207 G4cout << "Skipping FillNtupleIColumn for " << ntupleId << G4endl;
208 return false;
209 }
210
211 if ( IsVerbose(G4Analysis::kVL4) ) {
212 Message(G4Analysis::kVL4, "fill", "pntuple T column",
213 " ntupleId " + to_string(ntupleId) +
214 " columnId " + to_string(columnId) +
215 " value " + G4Analysis::ToString(value));
216 }
217
218 // get ntuple
219 auto ntuple = GetNtupleInFunction(ntupleId, "FillNtupleTColumn");
220 if ( ! ntuple ) return false;
221
222 // get generic column
223 auto index = columnId - fFirstNtupleColumnId;
224 if ( index < 0 || index >= G4int(ntuple->columns().size()) ) {
225 G4ExceptionDescription description;
226 description << " " << "ntupleId " << ntupleId
227 << " columnId " << columnId << " does not exist.";
228 G4Exception("G4TNtupleManager::FillNtupleTColumn()",
229 "Analysis_W011", JustWarning, description);
230 return false;
231 }
232 auto icolumn = ntuple->columns()[index];
233
234 // get column and check its type
235 auto column = dynamic_cast<tools::wroot::base_pntuple::column<T>* >(icolumn);
236 if ( ! column ) {
237 G4ExceptionDescription description;
238 description << " Column type does not match: "
239 << " ntupleId " << ntupleId
240 << " columnId " << columnId << " value " << value;
241 G4Exception("G4TNtupleManager:FillNtupleTColumn",
242 "Analysis_W011", JustWarning, description);
243 return false;
244 }
245
246 column->fill(value);
247
248 if ( IsVerbose(G4Analysis::kVL4) ) {
249 Message(G4Analysis::kVL4, "done fill", "pntuple T column",
250 " ntupleId " + to_string(ntupleId) +
251 " columnId " + to_string(columnId) +
252 " value " + G4Analysis::ToString(value));
253 }
254
255 return true;
256}
257
258#endif
virtual G4bool GetActivation(G4int ntupleId) const final
static constexpr std::string_view fkClass
void SetFileManager(std::shared_ptr< G4RootFileManager > fileManager)
virtual G4bool FillNtupleDColumn(G4int ntupleId, G4int columnId, G4double value) final
virtual G4bool AddNtupleRow(G4int ntupleId) final
virtual G4bool FillNtupleIColumn(G4int ntupleId, G4int columnId, G4int value) final
std::vector< tools::wroot::impi_ntuple * > fNtupleVector
tools::wroot::base_pntuple * GetNtupleInFunction(G4int id, G4String function, G4bool warn=true) const
void CreateNtuple(G4RootMpiPNtupleDescription *ntupleDescription)
G4RootMpiPNtupleDescription * GetNtupleDescriptionInFunction(G4int id, G4String function, G4bool warn=true) const
virtual G4bool FillNtupleSColumn(G4int ntupleId, G4int columnId, const G4String &value) final
virtual G4bool FillNtupleFColumn(G4int ntupleId, G4int columnId, G4float value) final
virtual void SetActivation(G4bool activation) final
std::vector< G4RootMpiPNtupleDescription * > fNtupleDescriptionVector
std::shared_ptr< G4RootFileManager > fFileManager
virtual G4int GetNofNtuples() const final
G4bool FillNtupleTColumn(G4int ntupleId, G4int columnId, const T &value)
void SetNewCycle(G4bool value) final
void CreateNtuplesFromBooking(const std::vector< G4NtupleBooking * > &ntupleBookings)

Applications | User Support | Publications | Collaboration