Geant4  10.02.p01
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 
41 
42 namespace tools {
43 namespace wroot {
44 class directory;
45 }
46 }
47 
48 // template specializations used by this class defined below
49 
50 template <>
51 template <>
54  const G4String& name, std::vector<std::string>* vector);
55 
56 template <>
57 template <>
59  G4int ntupleId, G4int columnId, const std::string& value);
60 
61 
62 class G4RootNtupleManager : public G4TNtupleManager<tools::wroot::ntuple>
63 {
64  friend class G4RootAnalysisManager;
65 
66  public:
67  explicit G4RootNtupleManager(const G4AnalysisManagerState& state);
68  virtual ~G4RootNtupleManager();
69 
70  private:
71  // Types alias
72  using NtupleType = tools::wroot::ntuple;
74 
75  // Functions specific to the output type
76 
77  void SetNtupleDirectory(tools::wroot::directory* directory);
78 
79  // Methods from the templated base class
80  //
81  virtual void CreateTNtuple(
82  NtupleDescriptionType* ntupleDescription,
83  const G4String& name, const G4String& title) final;
84  virtual void CreateTNtupleFromBooking(
85  NtupleDescriptionType* ntupleDescription) final;
86 
87  virtual void FinishTNtuple(
88  NtupleDescriptionType* ntupleDescription) final;
89 
90  // data members
91  //
92  tools::wroot::directory* fNtupleDirectory;
93 };
94 
95 // inline functions
96 
97 inline void
98 G4RootNtupleManager::SetNtupleDirectory(tools::wroot::directory* directory)
99 { fNtupleDirectory = directory; }
100 
101 //_____________________________________________________________________________
102 template <>
103 template <>
106  const G4String& name, std::vector<std::string>* vector)
107 {
108  if ( ntupleDescription->fNtuple ) {
109  if ( vector == nullptr ) {
110  ntupleDescription->fNtuple->create_column_string(name);
111  }
112  else {
113  G4ExceptionDescription description;
114  description << "Column of vector<std::string> type is not supported";
115  G4Exception("G4RootNtupleManager::CreateTColumnInNtuple()",
116  "Analysis_W001", JustWarning, description);
117  }
118  }
119 }
120 
121 //_____________________________________________________________________________
122 template <>
123 template <>
125  G4int ntupleId, G4int columnId, const std::string& value)
126 {
127  if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
128  //G4cout << "Skipping FillNtupleIColumn for " << ntupleId << G4endl;
129  return false;
130  }
131 
132  auto ntuple = GetNtupleInFunction(ntupleId, "FillNtupleTColumn");
133  if ( ! ntuple ) return false;
134 
135  auto index = columnId - fFirstNtupleColumnId;
136  if ( index < 0 || index >= G4int(ntuple->columns().size()) ) {
137  G4ExceptionDescription description;
138  description << " " << "ntupleId " << ntupleId
139  << " columnId " << columnId << " does not exist.";
140  G4Exception("G4RootNtupleManager::FillNtupleTColumn()",
141  "Analysis_W011", JustWarning, description);
142  return false;
143  }
144 
145  auto icolumn = ntuple->columns()[index];
146  auto column = dynamic_cast<tools::wroot::ntuple::column_string* >(icolumn);
147  if ( ! column ) {
148  G4ExceptionDescription description;
149  description << " Column type does not match: "
150  << " ntupleId " << ntupleId
151  << " columnId " << columnId << " value " << value;
152  G4Exception("G4RootNtupleManager:FillNtupleColumn",
153  "Analysis_W011", JustWarning, description);
154  return false;
155  }
156 
157  column->fill(value);
158 
159 #ifdef G4VERBOSE
160  if ( fState.GetVerboseL4() ) {
161  G4ExceptionDescription description;
162  description << " ntupleId " << ntupleId
163  << " columnId " << columnId << " value " << value;
164  fState.GetVerboseL4()->Message("fill", "ntuple T column", description);
165  }
166 #endif
167  return true;
168 }
169 
170 #endif
171 
172 
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::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4String name
Definition: TRTMaterials.hh:40
tools::wroot::directory * fNtupleDirectory
int G4int
Definition: G4Types.hh:78
tools::wroot::ntuple NtupleType
virtual void CreateTNtupleFromBooking(NtupleDescriptionType *ntupleDescription) final
const G4AnalysisVerbose * GetVerboseL4() const
bool G4bool
Definition: G4Types.hh:79
virtual G4bool GetActivation(G4int ntupleId) const final
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
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
G4RootNtupleManager(const G4AnalysisManagerState &state)
TNTUPLE * GetNtupleInFunction(G4int id, G4String function, G4bool warn=true) const
const G4AnalysisManagerState & fState
void SetNtupleDirectory(tools::wroot::directory *directory)