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