Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
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 // $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 "G4BaseNtupleManager.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 imt_ntuple;
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 
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  private:
154  tools::wroot::base_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>
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
177  std::vector<tools::wroot::imt_ntuple*> fNtupleVector;
178 };
179 
180 // inline functions
181 
182 //_____________________________________________________________________________
183 template <typename T>
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>
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 <>
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::base_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>
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::base_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>
349  return FillNtupleTColumn(0, columnId, value);
350 }
351 
352 #endif
353 
tools::wroot::ntuple * GetMainNtupleInFunction(G4int id, G4String function, G4bool warn=true) const
mutex & operator=(const mutex &)
const XML_Char * name
Definition: expat.h:151
const G4AnalysisVerbose * GetVerboseL2() const
G4double(* function)(G4double)
tools::wroot::base_pntuple * GetNtupleInFunction(G4int id, G4String function, G4bool warn=true) const
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:45
G4bool FillNtupleTColumn(G4int ntupleId, G4int columnId, const T &value)
virtual G4bool FillNtupleDColumn(G4int id, G4double value) final
virtual G4bool Merge() final
virtual G4bool Reset(G4bool deleteNtuple) final
virtual G4int CreateNtupleFColumn(G4int ntupleId, const G4String &name, std::vector< float > *vector) override
G4RootPNtupleManager(G4RootMainNtupleManager *main, const G4AnalysisManagerState &state)
TTree * ntuple
G4RootMainNtupleManager * fMainNtupleManager
#define G4endl
Definition: G4ios.hh:61
float G4float
Definition: G4Types.hh:77
virtual bool unlock()
virtual ~mutex()
virtual G4int GetNofNtupleBookings() const final
virtual G4bool FillNtupleIColumn(G4int id, G4int value) final
virtual G4int CreateNtupleDColumn(G4int ntupleId, const G4String &name, std::vector< double > *vector) override
tools::wroot::imutex parent
G4PNtupleCreateMode fCreateMode
double G4double
Definition: G4Types.hh:76
bool G4bool
Definition: G4Types.hh:79
virtual G4bool FillNtupleFColumn(G4int ntupleId, G4int columnId, G4float value) final
virtual G4bool FillNtupleSColumn(G4int ntupleId, G4int columnId, const G4String &value) final
const XML_Char int const XML_Char * value
Definition: expat.h:331
virtual G4int CreateNtupleIColumn(const G4String &name, std::vector< int > *vector) final
virtual G4bool FillNtupleSColumn(G4int id, const G4String &value) final
const G4AnalysisVerbose * GetVerboseL4() const
virtual void SetActivation(G4bool activation) final
virtual G4int CreateNtupleFColumn(const G4String &name, std::vector< float > *vector) final
std::vector< tools::wroot::imt_ntuple * > fNtupleVector
virtual G4bool GetActivation(G4int ntupleId) const final
const G4int kInvalidId
virtual G4int CreateNtupleIColumn(G4int ntupleId, const G4String &name, std::vector< int > *vector) override
const G4ThreeVector const G4double const
virtual G4bool IsEmpty() const final
int main(int argc, char **argv)
Definition: genwindef.cc:359
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65
virtual bool lock()
int G4int
Definition: G4Types.hh:78
virtual G4bool AddNtupleRow() final
virtual G4int CreateNtupleSColumn(G4int ntupleId, const G4String &name) override
TFile * file
virtual void FinishNtuple() final
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
G4GLOB_DLL std::ostream G4cout
G4AutoLock & m_mutex
mutex(const mutex &a_from)
mutex(G4AutoLock &a_mutex)
G4int CreateNtupleTColumn(G4int ntupleId, const G4String &name, std::vector< T > *vector)
virtual G4bool FillNtupleDColumn(G4int ntupleId, G4int columnId, G4double value) final
void CreateNtuple(G4RootPNtupleDescription *ntupleDescription, tools::wroot::ntuple *mainNtuple)
virtual G4int CreateNtupleSColumn(const G4String &name)
G4RootPNtupleDescription * GetNtupleDescriptionInFunction(G4int id, G4String function, G4bool warn=true) const
std::vector< G4RootPNtupleDescription * > fNtupleDescriptionVector
virtual G4bool FillNtupleFColumn(G4int id, G4float value) final
virtual G4int GetNofNtuples() const final
const G4AnalysisManagerState & fState
virtual G4bool FillNtupleIColumn(G4int ntupleId, G4int columnId, G4int value) final
virtual G4int CreateNtupleDColumn(const G4String &name, std::vector< double > *vector) final