Geant4  10.03
G4RootNtupleManager.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: G4RootNtupleManager.hh 70604 2013-06-03 11:27:06Z ihrivnac $
27 
28 // Manager class for Root ntuples.
29 // It implements functions specific to Root ntuples.
30 //
31 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
32 
33 #ifndef G4RootNtupleManager_h
34 #define G4RootNtupleManager_h 1
35 
36 #include "G4TNtupleManager.hh"
37 #include "globals.hh"
38 
39 #include "tools/wroot/ntuple"
40 
42 class G4RootFileManager;
43 
44 namespace tools {
45 namespace wroot {
46 class directory;
47 }
48 }
49 
50 enum class G4NtupleCreateMode {
56 };
57 
58 // template specializations used by this class defined below
59 
60 template <>
61 template <>
64  const G4String& name, std::vector<std::string>* vector);
65 
66 template <>
67 template <>
69  G4int ntupleId, G4int columnId, const std::string& value);
70 
71 
72 class G4RootNtupleManager : public G4TNtupleManager<tools::wroot::ntuple>
73 {
74  friend class G4RootAnalysisManager;
76 
77  public:
78  explicit G4RootNtupleManager(const G4AnalysisManagerState& state,
79  G4int nofMainManagers = 0);
80  virtual ~G4RootNtupleManager();
81 
82  private:
83  // Types alias
84  using NtupleType = tools::wroot::ntuple;
86 
87  // Functions specific to the output type
88 
89  void SetNtupleDirectory(tools::wroot::directory* directory);
90  void SetFileManager(std::shared_ptr<G4RootFileManager> fileManager);
91 
92  // Methods from the templated base class
93  //
94  virtual void CreateTNtuple(
95  NtupleDescriptionType* ntupleDescription,
96  const G4String& name, const G4String& title) final;
97  virtual void CreateTNtupleFromBooking(
98  NtupleDescriptionType* ntupleDescription) final;
99 
100  virtual void FinishTNtuple(
101  NtupleDescriptionType* ntupleDescription) final;
102 
103  virtual G4bool Reset(G4bool deleteNtuple);
104 
105  // Method for merging
106  //
107  G4bool Merge();
108 
109  // Access functions
110  //
111  const std::vector<NtupleDescriptionType*>& GetNtupleDescriptionVector() const;
113  unsigned int GetBasketSize() const;
114 
115  // Utility functions
116  //
117  void SetCreateMode();
118 
119  // data members
120  //
122  std::shared_ptr<G4RootFileManager> fFileManager;
123  tools::wroot::directory* fNtupleDirectory;
124  std::vector<G4RootMainNtupleManager*> fMainNtupleManagers;
125 };
126 
127 // inline functions
128 
129 inline void
130 G4RootNtupleManager::SetNtupleDirectory(tools::wroot::directory* directory)
131 { fNtupleDirectory = directory; }
132 
133 inline void
134 G4RootNtupleManager::SetFileManager(std::shared_ptr<G4RootFileManager> fileManager)
135 { fFileManager = fileManager; }
136 
137 inline const std::vector<G4TNtupleDescription<tools::wroot::ntuple>* >&
139 { return fNtupleDescriptionVector; }
140 
141 //_____________________________________________________________________________
142 template <>
143 template <>
146  const G4String& name, std::vector<std::string>* vector)
147 {
148  if ( ntupleDescription->fNtuple ) {
149  if ( vector == nullptr ) {
150  ntupleDescription->fNtuple->create_column_string(name);
151  }
152  else {
153  G4ExceptionDescription description;
154  description << "Column of vector<std::string> type is not supported";
155  G4Exception("G4RootNtupleManager::CreateTColumnInNtuple()",
156  "Analysis_W001", JustWarning, description);
157  }
158  }
159 }
160 
161 //_____________________________________________________________________________
162 template <>
163 template <>
165  G4int ntupleId, G4int columnId, const std::string& value)
166 {
167  if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
168  //G4cout << "Skipping FillNtupleIColumn for " << ntupleId << G4endl;
169  return false;
170  }
171 
172  auto ntuple = GetNtupleInFunction(ntupleId, "FillNtupleTColumn");
173  if ( ! ntuple ) return false;
174 
175  auto index = columnId - fFirstNtupleColumnId;
176  if ( index < 0 || index >= G4int(ntuple->columns().size()) ) {
177  G4ExceptionDescription description;
178  description << " " << "ntupleId " << ntupleId
179  << " columnId " << columnId << " does not exist.";
180  G4Exception("G4RootNtupleManager::FillNtupleTColumn()",
181  "Analysis_W011", JustWarning, description);
182  return false;
183  }
184 
185  auto icolumn = ntuple->columns()[index];
186  auto column = dynamic_cast<tools::wroot::ntuple::column_string* >(icolumn);
187  if ( ! column ) {
188  G4ExceptionDescription description;
189  description << " Column type does not match: "
190  << " ntupleId " << ntupleId
191  << " columnId " << columnId << " value " << value;
192  G4Exception("G4RootNtupleManager:FillNtupleColumn",
193  "Analysis_W011", JustWarning, description);
194  return false;
195  }
196 
197  column->fill(value);
198 
199 #ifdef G4VERBOSE
200  if ( fState.GetVerboseL4() ) {
201  G4ExceptionDescription description;
202  description << " ntupleId " << ntupleId
203  << " columnId " << columnId << " value " << value;
204  fState.GetVerboseL4()->Message("fill", "ntuple T column", description);
205  }
206 #endif
207  return true;
208 }
209 
210 #endif
211 
212 
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
G4bool FillNtupleTColumn(G4int ntupleId, G4int columnId, const T &value)
std::shared_ptr< G4RootFileManager > fFileManager
std::vector< G4RootMainNtupleManager * > fMainNtupleManagers
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4RootNtupleManager(const G4AnalysisManagerState &state, G4int nofMainManagers=0)
void SetFileManager(std::shared_ptr< G4RootFileManager > fileManager)
tools::wroot::directory * fNtupleDirectory
std::vector< G4TNtupleDescription< tools::wroot::ntuple > * > fNtupleDescriptionVector
const char * name(G4int ptype)
int G4int
Definition: G4Types.hh:78
tools::wroot::ntuple NtupleType
virtual void CreateTNtupleFromBooking(NtupleDescriptionType *ntupleDescription) final
G4RootMainNtupleManager * GetMainNtupleManager(G4int index) const
const G4AnalysisVerbose * GetVerboseL4() const
bool G4bool
Definition: G4Types.hh:79
virtual G4bool GetActivation(G4int ntupleId) const final
G4NtupleCreateMode
G4NtupleCreateMode fCreateMode
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
virtual G4bool Reset(G4bool deleteNtuple)
virtual void FinishTNtuple(NtupleDescriptionType *ntupleDescription) final
void CreateTColumnInNtuple(G4TNtupleDescription< TNTUPLE > *ntupleDescription, const G4String &name, std::vector< T > *vector)
virtual void CreateTNtuple(NtupleDescriptionType *ntupleDescription, const G4String &name, const G4String &title) final
TNTUPLE * GetNtupleInFunction(G4int id, G4String function, G4bool warn=true) const
unsigned int GetBasketSize() const
const G4AnalysisManagerState & fState
void SetNtupleDirectory(tools::wroot::directory *directory)
const std::vector< NtupleDescriptionType * > & GetNtupleDescriptionVector() const