ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4RootMpiPNtupleManager.hh
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4RootMpiPNtupleManager.hh
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"
41 #include "G4AnalysisUtilities.hh"
42 #include "globals.hh"
43 
44 #include "tools/wroot/base_pntuple"
45 
46 #include <vector>
47 
48 class G4RootFileManager;
49 
50 namespace tools {
51 namespace wroot {
52 class file;
53 class directory;
54 class impi_ntuple;
55 }
56 }
57 
59 {
61  friend class G4RootAnalysisManager;
62  friend class G4RootNtupleManager;
63 
64  public:
65  explicit G4RootMpiPNtupleManager(const G4AnalysisManagerState& state,
66  tools::impi* impi, G4int mpiRank, G4int destinationRank);
68 
69  private:
70  enum class G4PNtupleCreateMode {
71  kSlaveBeforeOpen,
72  kSlaveAfterOpen,
74  };
75 
76  // Functions specific to the output type
77  void SetNtupleDirectory(tools::wroot::directory* directory);
78  void SetFileManager(std::shared_ptr<G4RootFileManager> fileManager);
79 
80  // Methods to manipulate ntuples
81  void CreateNtuple(G4RootMpiPNtupleDescription* ntupleDescription);
83 
84  // Methods to create ntuples
85  //
86  virtual G4int CreateNtuple(const G4String& name, const G4String& title) final;
87  // Create columns in the ntuple with given id
88  virtual G4int CreateNtupleIColumn(G4int ntupleId,
89  const G4String& name, std::vector<int>* vector) final;
90  virtual G4int CreateNtupleFColumn(G4int ntupleId,
91  const G4String& name, std::vector<float>* vector) final;
92  virtual G4int CreateNtupleDColumn(G4int ntupleId,
93  const G4String& name, std::vector<double>* vector) final;
94  virtual G4int CreateNtupleSColumn(G4int ntupleId, const G4String& name) final;
95  virtual void FinishNtuple(G4int ntupleId) final;
96 
97  // Methods to fill ntuples
98  // Methods for ntuple with id > FirstNtupleId (when more ntuples exist)
99  virtual G4bool FillNtupleIColumn(G4int ntupleId, G4int columnId, G4int value) final;
100  virtual G4bool FillNtupleFColumn(G4int ntupleId, G4int columnId, G4float value) final;
101  virtual G4bool FillNtupleDColumn(G4int ntupleId, G4int columnId, G4double value) final;
102  virtual G4bool FillNtupleSColumn(G4int ntupleId, G4int columnId,
103  const G4String& value) final;
104  virtual G4bool AddNtupleRow(G4int ntupleId) final;
105  virtual G4bool Merge() final;
106 
107  // Reset
108  virtual G4bool Reset(G4bool deleteNtuple) final;
109 
110  // Activation option
111  //
112  virtual void SetActivation(G4bool activation) final;
113  virtual void SetActivation(G4int ntupleId, G4bool activation) final;
114  virtual G4bool GetActivation(G4int ntupleId) const final;
115  virtual G4bool IsEmpty() const final;
116 
117  // Access methods
118  virtual G4int GetNofNtuples() const final;
119  virtual G4int GetNofNtupleBookings() const final;
120  const std::vector<G4RootMpiPNtupleDescription*>& GetNtupleDescriptionVector() const;
121  unsigned int GetBasketSize() const;
122 
123  private:
126  tools::wroot::base_pntuple*
127  GetNtupleInFunction(G4int id, G4String function, G4bool warn = true) const;
128 
129  template <typename T>
130  G4int CreateNtupleTColumn(G4int ntupleId,
131  const G4String& name, std::vector<T>* vector);
132 
133  template <typename T>
135  const G4String& name, std::vector<T>* vector);
136 
137  template <typename T>
138  G4bool FillNtupleTColumn(G4int ntupleId, G4int columnId, const T& value);
139 
140  // Data members
141  // G4RootMpiMainNtupleManager* fMpiMainNtupleManager;
143  tools::wroot::directory* fNtupleDirectory;
145  std::vector<tools::wroot::impi_ntuple*> fNtupleVector;
146  tools::impi* fImpi;
149 };
150 
151 // inline functions
152 
153 inline void
154 G4RootMpiPNtupleManager::SetNtupleDirectory(tools::wroot::directory* directory)
155 { fNtupleDirectory = directory; }
156 
157 inline void
158 G4RootMpiPNtupleManager::SetFileManager(std::shared_ptr<G4RootFileManager> fileManager)
159 { fFileManager = fileManager; }
160 
161 inline const std::vector<G4RootMpiPNtupleDescription*>&
163 { return fNtupleDescriptionVector; }
164 
165 
166 //_____________________________________________________________________________
167 template <typename T>
169  G4int ntupleId, const G4String& name, std::vector<T>* vector)
170 {
171 #ifdef G4VERBOSE
172  if ( fState.GetVerboseL4() ) {
173  G4ExceptionDescription description;
174  description << name << " ntupleId " << ntupleId;
175  fState.GetVerboseL4()->Message("create", "pntuple T column", description);
176  }
177 #endif
178 
179  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "CreateNtupleTColumn");
180  if ( ! ntupleDescription ) return G4Analysis::kInvalidId;
181 
182  // Save column info in booking
183  auto& ntupleBooking = ntupleDescription->fNtupleBooking;
184  auto index = ntupleBooking.columns().size();
185  if ( ! vector )
186  ntupleBooking.template add_column<T>(name);
187  else
188  ntupleBooking.template add_column<T>(name, *vector);
189 
191 
192 #ifdef G4VERBOSE
193  if ( fState.GetVerboseL2() ) {
194  G4ExceptionDescription description;
195  description << name << " ntupleId " << ntupleId;
196  fState.GetVerboseL2()->Message("create", "pntuple T column", description);
197  }
198 #endif
199 
200  return index + fFirstNtupleColumnId;
201 
202 }
203 
204 //_____________________________________________________________________________
205 template <>
207  G4int ntupleId, G4int columnId, const std::string& value)
208 {
209  if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
210  G4cout << "Skipping FillNtupleIColumn for " << ntupleId << G4endl;
211  return false;
212  }
213 
214 #ifdef G4VERBOSE
215  if ( fState.GetVerboseL4() ) {
216  G4ExceptionDescription description;
217  description << " ntupleId " << ntupleId
218  << " columnId " << columnId << " value " << value;
219  fState.GetVerboseL4()->Message("fill", "pntuple T column", description);
220  }
221 #endif
222 
223  auto ntuple = GetNtupleInFunction(ntupleId, "FillNtupleTColumn");
224  if ( ! ntuple ) return false;
225 
226  auto index = columnId - fFirstNtupleColumnId;
227  if ( index < 0 || index >= G4int(ntuple->columns().size()) ) {
228  G4ExceptionDescription description;
229  description << " " << "ntupleId " << ntupleId
230  << " columnId " << columnId << " does not exist.";
231  G4Exception("G4RootNtupleManager::FillNtupleTColumn()",
232  "Analysis_W011", JustWarning, description);
233  return false;
234  }
235 
236  auto icolumn = ntuple->columns()[index];
237  auto column = dynamic_cast<tools::wroot::base_pntuple::column_string* >(icolumn);
238  if ( ! column ) {
239  G4ExceptionDescription description;
240  description << " Column type does not match: "
241  << " ntupleId " << ntupleId
242  << " columnId " << columnId << " value " << value;
243  G4Exception("G4RootNtupleManager:FillNtupleColumn",
244  "Analysis_W011", JustWarning, description);
245  return false;
246  }
247 
248  column->fill(value);
249 
250 #ifdef G4VERBOSE
251  if ( fState.GetVerboseL4() ) {
252  G4ExceptionDescription description;
253  description << " ntupleId " << ntupleId
254  << " columnId " << columnId << " value " << value;
255  fState.GetVerboseL4()->Message("done fill", "pntuple T column", description);
256  }
257 #endif
258  return true;
259 }
260 
261 //_____________________________________________________________________________
262 template <typename T>
264  G4int ntupleId, G4int columnId, const T& value)
265 {
266  if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
267  G4cout << "Skipping FillNtupleIColumn for " << ntupleId << G4endl;
268  return false;
269  }
270 
271 #ifdef G4VERBOSE
272  if ( fState.GetVerboseL4() ) {
273  G4ExceptionDescription description;
274  description << " ntupleId " << ntupleId
275  << " columnId " << columnId << " value " << value;
276  fState.GetVerboseL4()->Message("fill", "pntuple T column", description);
277  }
278 #endif
279 
280  // get ntuple
281  auto ntuple = GetNtupleInFunction(ntupleId, "FillNtupleTColumn");
282  if ( ! ntuple ) return false;
283 
284  // get generic column
285  auto index = columnId - fFirstNtupleColumnId;
286  if ( index < 0 || index >= G4int(ntuple->columns().size()) ) {
287  G4ExceptionDescription description;
288  description << " " << "ntupleId " << ntupleId
289  << " columnId " << columnId << " does not exist.";
290  G4Exception("G4TNtupleManager::FillNtupleTColumn()",
291  "Analysis_W011", JustWarning, description);
292  return false;
293  }
294  auto icolumn = ntuple->columns()[index];
295 
296  // get column and check its type
297  auto column = dynamic_cast<tools::wroot::base_pntuple::column<T>* >(icolumn);
298  if ( ! column ) {
299  G4ExceptionDescription description;
300  description << " Column type does not match: "
301  << " ntupleId " << ntupleId
302  << " columnId " << columnId << " value " << value;
303  G4Exception("G4TNtupleManager:FillNtupleTColumn",
304  "Analysis_W011", JustWarning, description);
305  return false;
306  }
307 
308  column->fill(value);
309 
310 #ifdef G4VERBOSE
311  if ( fState.GetVerboseL4() ) {
312  G4ExceptionDescription description;
313  description << " ntupleId " << ntupleId
314  << " columnId " << columnId << " value " << value;
315  fState.GetVerboseL4()->Message("done fill", "pntuple T column", description);
316  }
317 #endif
318  return true;
319 }
320 
321 #endif
322