ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4RootPNtupleManager.hh
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4RootPNtupleManager.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 pntuple management.
28 //
29 // Author: Ivana Hrivnacova, 04/10/2016 (ivana@ipno.in2p3.fr)
30 
31 #ifndef G4RootPNtupleManager_h
32 #define G4RootPNtupleManager_h 1
33 
34 #include "G4BaseNtupleManager.hh"
37 #include "G4AnalysisUtilities.hh"
38 #include "G4AutoLock.hh"
39 #include "globals.hh"
40 
41 #include <vector>
42 
44 
45 namespace tools {
46 namespace wroot {
47 class file;
48 class ntuple;
49 class imt_ntuple;
50 }
51 }
52 
53 // Mutex implementation as in pwroot.cpp
54 // with replacement tools::mutex -> G4Mutex
55 
56 class mutex : public virtual tools::wroot::imutex {
57  typedef tools::wroot::imutex parent;
58 public:
59  virtual bool lock() {
60  // G4cout << "!!! Mutex lock" << G4endl;
61  m_mutex.lock();
62  return true;}
63  virtual bool unlock() {
64  m_mutex.unlock();
65  // G4cout << "!!! Mutex unlock" << G4endl;
66  return true; }
67  //virtual bool trylock() {return m_mutex.trylock();}
68 public:
69  mutex(G4AutoLock& a_mutex):m_mutex(a_mutex){}
70  virtual ~mutex(){}
71 protected:
72  mutex(const mutex& a_from):parent(a_from),m_mutex(a_from.m_mutex){}
73  mutex& operator=(const mutex&){return *this;}
74 protected:
76 };
77 
79 {
80  friend class G4RootAnalysisManager;
81 
82  public:
84  const G4AnalysisManagerState& state,
85  G4bool rowWise, G4bool rowMode);
87 
88  private:
89  enum class G4PNtupleCreateMode {
90  kSlaveBeforeOpen,
91  kSlaveAfterOpen,
93  };
94 
95  // Methods to manipulate ntuples
96  void CreateNtuple(G4RootPNtupleDescription* ntupleDescription,
97  tools::wroot::ntuple* mainNtuple);
98  void CreateNtuplesFromMain();
99 
100  // Methods to create ntuples
101  //
102  virtual G4int CreateNtuple(const G4String& name, const G4String& title) final;
103 
104  // Create columns in the last created ntuple (from base class)
110 
111  // Create columns in the ntuple with given id
112  virtual G4int CreateNtupleIColumn(G4int ntupleId,
113  const G4String& name, std::vector<int>* vector) override;
114  virtual G4int CreateNtupleFColumn(G4int ntupleId,
115  const G4String& name, std::vector<float>* vector) override;
116  virtual G4int CreateNtupleDColumn(G4int ntupleId,
117  const G4String& name, std::vector<double>* vector) override;
118  virtual G4int CreateNtupleSColumn(G4int ntupleId, const G4String& name) override;
119  virtual void FinishNtuple(G4int ntupleId) override;
120 
121  // Methods to fill ntuples
122  // Methods for ntuple with id = FirstNtupleId (from base class)
128  // Methods for ntuple with id > FirstNtupleId (when more ntuples exist)
129  virtual G4bool FillNtupleIColumn(G4int ntupleId, G4int columnId, G4int value) final;
130  virtual G4bool FillNtupleFColumn(G4int ntupleId, G4int columnId, G4float value) final;
131  virtual G4bool FillNtupleDColumn(G4int ntupleId, G4int columnId, G4double value) final;
132  virtual G4bool FillNtupleSColumn(G4int ntupleId, G4int columnId,
133  const G4String& value) final;
134  virtual G4bool AddNtupleRow(G4int ntupleId) final;
135  virtual G4bool Merge() final;
136 
137  // Reset
138  virtual G4bool Reset(G4bool deleteNtuple) final;
139 
140  // Activation option
141  //
142  virtual void SetActivation(G4bool activation) final;
143  virtual void SetActivation(G4int ntupleId, G4bool activation) final;
144  virtual G4bool GetActivation(G4int ntupleId) const final;
145  virtual G4bool IsEmpty() const final;
146 
147  // Access methods
148  virtual G4int GetNofNtuples() const final;
149  virtual G4int GetNofNtupleBookings() const final;
150 
151  // Set methods
152  void SetNtupleRowWise(G4bool rowWise, G4bool rowMode);
153 
154  private:
157  tools::wroot::base_pntuple*
158  GetNtupleInFunction(G4int id, G4String function, G4bool warn = true) const;
159  tools::wroot::ntuple*
160  GetMainNtupleInFunction(G4int id, G4String function, G4bool warn = true) const;
161 
162  template <typename T>
163  G4int CreateNtupleTColumn(G4int ntupleId,
164  const G4String& name, std::vector<T>* vector);
165 
166  template <typename T>
168  const G4String& name, std::vector<T>* vector);
169 
170  template <typename T>
171  G4bool FillNtupleTColumn(G4int ntupleId, G4int columnId, const T& value);
172 
173  template <typename T>
174  G4bool FillNtupleTColumn(G4int columnId, const T& value);
175 
176  // Data members
180  std::vector<tools::wroot::imt_ntuple*> fNtupleVector;
183 };
184 
185 // inline functions
186 
187 //_____________________________________________________________________________
188 template <typename T>
190  G4int ntupleId, const G4String& name, std::vector<T>* vector)
191 {
192 #ifdef G4VERBOSE
193  if ( fState.GetVerboseL4() ) {
194  G4ExceptionDescription description;
195  description << name << " ntupleId " << ntupleId;
196  fState.GetVerboseL4()->Message("create", "pntuple T column", description);
197  }
198 #endif
199 
200  auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "CreateNtupleTColumn");
201  if ( ! ntupleDescription ) return G4Analysis::kInvalidId;
202 
203  // Save column info in booking
204  auto& ntupleBooking = ntupleDescription->fNtupleBooking;
205  auto index = ntupleBooking.columns().size();
206  if ( ! vector )
207  ntupleBooking.template add_column<T>(name);
208  else
209  ntupleBooking.template add_column<T>(name, *vector);
210 
212 
213 #ifdef G4VERBOSE
214  if ( fState.GetVerboseL2() ) {
215  G4ExceptionDescription description;
216  description << name << " ntupleId " << ntupleId;
217  fState.GetVerboseL2()->Message("create", "pntuple T column", description);
218  }
219 #endif
220 
221  return index + fFirstNtupleColumnId;
222 
223 }
224 
225 //_____________________________________________________________________________
226 template <typename T>
228  const G4String& name, std::vector<T>* vector)
229 {
230  auto ntupleId = fNtupleDescriptionVector.size() + fFirstId - 1;
231  return CreateNtupleTColumn<T>(ntupleId, name, vector);
232 }
233 
234 //_____________________________________________________________________________
235 template <>
237  G4int ntupleId, G4int columnId, const std::string& value)
238 {
239  if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
240  G4cout << "Skipping FillNtupleIColumn for " << ntupleId << G4endl;
241  return false;
242  }
243 
244 #ifdef G4VERBOSE
245  if ( fState.GetVerboseL4() ) {
246  G4ExceptionDescription description;
247  description << " ntupleId " << ntupleId
248  << " columnId " << columnId << " value " << value;
249  fState.GetVerboseL4()->Message("fill", "pntuple T column", description);
250  }
251 #endif
252 
253  auto ntuple = GetNtupleInFunction(ntupleId, "FillNtupleTColumn");
254  if ( ! ntuple ) return false;
255 
256  auto index = columnId - fFirstNtupleColumnId;
257  if ( index < 0 || index >= G4int(ntuple->columns().size()) ) {
258  G4ExceptionDescription description;
259  description << " " << "ntupleId " << ntupleId
260  << " columnId " << columnId << " does not exist.";
261  G4Exception("G4RootNtupleManager::FillNtupleTColumn()",
262  "Analysis_W011", JustWarning, description);
263  return false;
264  }
265 
266  auto icolumn = ntuple->columns()[index];
267  auto column = dynamic_cast<tools::wroot::base_pntuple::column_string* >(icolumn);
268  if ( ! column ) {
269  G4ExceptionDescription description;
270  description << " Column type does not match: "
271  << " ntupleId " << ntupleId
272  << " columnId " << columnId << " value " << value;
273  G4Exception("G4RootNtupleManager:FillNtupleColumn",
274  "Analysis_W011", JustWarning, description);
275  return false;
276  }
277 
278  column->fill(value);
279 
280 #ifdef G4VERBOSE
281  if ( fState.GetVerboseL4() ) {
282  G4ExceptionDescription description;
283  description << " ntupleId " << ntupleId
284  << " columnId " << columnId << " value " << value;
285  fState.GetVerboseL4()->Message("done fill", "pntuple T column", description);
286  }
287 #endif
288  return true;
289 }
290 
291 //_____________________________________________________________________________
292 template <typename T>
294  G4int ntupleId, G4int columnId, const T& value)
295 {
296  if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
297  G4cout << "Skipping FillNtupleIColumn for " << ntupleId << G4endl;
298  return false;
299  }
300 
301 #ifdef G4VERBOSE
302  if ( fState.GetVerboseL4() ) {
303  G4ExceptionDescription description;
304  description << " ntupleId " << ntupleId
305  << " columnId " << columnId << " value " << value;
306  fState.GetVerboseL4()->Message("fill", "pntuple T column", description);
307  }
308 #endif
309 
310  // get ntuple
311  auto ntuple = GetNtupleInFunction(ntupleId, "FillNtupleTColumn");
312  if ( ! ntuple ) return false;
313 
314  // get generic column
315  auto index = columnId - fFirstNtupleColumnId;
316  if ( index < 0 || index >= G4int(ntuple->columns().size()) ) {
317  G4ExceptionDescription description;
318  description << " " << "ntupleId " << ntupleId
319  << " columnId " << columnId << " does not exist.";
320  G4Exception("G4TNtupleManager::FillNtupleTColumn()",
321  "Analysis_W011", JustWarning, description);
322  return false;
323  }
324  auto icolumn = ntuple->columns()[index];
325 
326  // get column and check its type
327  auto column = dynamic_cast<tools::wroot::base_pntuple::column<T>* >(icolumn);
328  if ( ! column ) {
329  G4ExceptionDescription description;
330  description << " Column type does not match: "
331  << " ntupleId " << ntupleId
332  << " columnId " << columnId << " value " << value;
333  G4Exception("G4TNtupleManager:FillNtupleTColumn",
334  "Analysis_W011", JustWarning, description);
335  return false;
336  }
337 
338  column->fill(value);
339 
340 #ifdef G4VERBOSE
341  if ( fState.GetVerboseL4() ) {
342  G4ExceptionDescription description;
343  description << " ntupleId " << ntupleId
344  << " columnId " << columnId << " value " << value;
345  fState.GetVerboseL4()->Message("done fill", "pntuple T column", description);
346  }
347 #endif
348  return true;
349 }
350 
351 //_____________________________________________________________________________
352 template <typename T>
354  return FillNtupleTColumn(0, columnId, value);
355 }
356 
357 #endif