Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4GenericAnalysisManager.cc
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
27// Author: Ivana Hrivnacova, 18/06/2013 ([email protected])
28
36#include "G4Exception.hh"
37
38using namespace G4Analysis;
39using std::to_string;
40
41// mutex in a file scope
42
43namespace {
44
45//_____________________________________________________________________________
46void WriteHnWarning(const G4String& hnType, G4int id,
47 std::string_view inClass,
48 std::string_view inFunction)
49{
50 Warn("Failed to get " + hnType + " id " + to_string(id), inClass, inFunction);
51}
52
53}
54
55//_____________________________________________________________________________
57{
59 fgIsInstance = true;
60 return instance.Instance();
61}
62
63//_____________________________________________________________________________
65{
66 return fgIsInstance;
67}
68
69//_____________________________________________________________________________
70G4GenericAnalysisManager::G4GenericAnalysisManager()
72{
73 if ( ! G4Threading::IsWorkerThread() ) fgMasterInstance = this;
74
75 // File manager
76 fFileManager = std::make_shared<G4GenericFileManager>(fState);
77 SetFileManager(fFileManager);
78}
79
80//_____________________________________________________________________________
82{
83 if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
84 fgIsInstance = false;
85}
86
87//
88// private methods
89//
90
91//_____________________________________________________________________________
92void G4GenericAnalysisManager::CreateNtupleFileManager(const G4String& fileName)
93{
94 if ( fNtupleFileManager ) {
95 Warn("The ntuple file manager already exists.",
96 fkClass, "CreateNtupleFileManager");
97 return;
98 }
99
100 auto fileType = GetExtension(fileName);
101 auto output = G4Analysis::GetOutput(fileType);
102 if ( output == G4AnalysisOutput::kNone ) {
103 Warn("The file type " + fileType + "is not supported.",
104 fkClass, "CreateNtupleFileManager");
105 return;
106 }
107
108 // Set file type to booked ntuples
109 fNtupleBookingManager->SetFileType(fileType);
110
111 Message(kVL4, "create", "ntuple file manager", fileType);
112
113 fNtupleFileManager = fFileManager->CreateNtupleFileManager(output);
114 if (fNtupleFileManager) {
115 SetNtupleFileManager(fNtupleFileManager);
116 fNtupleFileManager->SetBookingManager(fNtupleBookingManager);
117
118 if ( fNtupleFileManager->IsNtupleMergingSupported() ) {
119 // set merginng
120 fNtupleFileManager->SetNtupleMerging(fMergeNtuples, fNofNtupleFiles);
121 fNtupleFileManager->SetNtupleRowWise(fNtupleRowWise, fNtupleRowMode);
122 fNtupleFileManager->SetBasketSize(fBasketSize);
123 fNtupleFileManager->SetBasketEntries(fBasketEntries);
124 }
125 else if ( fIsNtupleMergingSet && fMergeNtuples ) {
126 Warn("Ntuple merging is not available with " + fileType + " output.\n" +
127 "Setting is ignored.",
128 fkClass, "CreateNtupleFileManager");
129 }
130 }
131
132 Message(kVL3, "create", "ntuple file manager", fileType);
133}
134
135//
136// protected methods
137//
138
139//_____________________________________________________________________________
141{
142 Message(kVL4, "open", "file", fileName);
143
144 // Add file name extension, if missing
145 auto fullFileName = fileName;
146 if (GetExtension(fileName).size() == 0u) {
147 auto defaultFileType = fFileManager->GetDefaultFileType();
148 // G4cout << "File type is not defined, using default: " << defaultFileType << G4endl;
149 if (defaultFileType.size() == 0u) {
150 G4Exception("G4GenericAnalysisManager::OpenFileImpl", "Analysis_F001",
152 G4String("Cannot open file \"" + fileName + "\".\n"
153 "Please, use a file name with an extension or define the default file type\n"
154 "via G4AnalysisManager::SetDefaultFileType()"));
155 }
156
157 fullFileName = fileName + "." + fFileManager->GetDefaultFileType();
158 }
159
160 // Create ntuple file manager if there are booked ntuples
161 if (! fNtupleFileManager) {
162 CreateNtupleFileManager(fullFileName);
163 }
164
165 auto result = true;
166 if (fNtupleFileManager) {
167 result &= G4ToolsAnalysisManager::OpenFileImpl(fullFileName);
168 }
169 else {
170 // no ntuples (check if this mode is supported)
171 result &= fFileManager->OpenFile(fullFileName);
172 }
173
174 Message(kVL3, "open", "file", fileName, result);
175
176 return result;
177}
178
179//_____________________________________________________________________________
181{
183
184 fFileManager->SetDefaultFileType(value);
185}
186
187//_____________________________________________________________________________
189{
190 // Experimental extra write
191
192 // Do not write histo on worker (redundant and fails in hdf5 )
193 // If default file is not used, users have to call Merge from their code
194 if ( G4Threading::IsWorkerThread() ) return false;
195
196 auto h1d = GetH1(id, false);
197 if (h1d == nullptr) {
198 WriteHnWarning("H1", id, fkClass, "WriteH1");
199 return false;
200 }
201
202 auto h1Name = GetH1Name(id);
203 return fFileManager->WriteTExtra<tools::histo::h1d>(fileName, h1d, h1Name);
204}
205
206//_____________________________________________________________________________
208{
209 // Experimental extra write
210
211 // Do not write histo on worker (redundant and fails in hdf5 )
212 // If default file is not used, users have to call Merge from their code
213 if ( G4Threading::IsWorkerThread() ) return false;
214
215 auto h2d = GetH2(id, false);
216 if (h2d == nullptr) {
217 WriteHnWarning("H2", id, fkClass, "WriteH2");
218 return false;
219 }
220
221 auto h2Name = GetH2Name(id);
222 return fFileManager->WriteTExtra<tools::histo::h2d>(fileName, h2d, h2Name);
223}
224//_____________________________________________________________________________
226{
227 // Experimental extra write
228
229 // Do not write histo on worker (redundant and fails in hdf5 )
230 // If default file is not used, users have to call Merge from their code
231 if ( G4Threading::IsWorkerThread() ) return false;
232
233 auto h3d = GetH3(id, false);
234 if (h3d == nullptr) {
235 WriteHnWarning("H3", id, fkClass, "WriteH3");
236 return false;
237 }
238
239 auto h3Name = GetH3Name(id);
240 return fFileManager->WriteTExtra<tools::histo::h3d>(fileName, h3d, h3Name);
241}
242
243//_____________________________________________________________________________
245{
246 // Experimental extra write
247
248 // Do not write histo on worker (redundant and fails in hdf5 )
249 // If default file is not used, users have to call Merge from their code
250 if ( G4Threading::IsWorkerThread() ) return false;
251
252 auto p1d = GetP1(id, false);
253 if (p1d == nullptr) {
254 WriteHnWarning("P1", id, fkClass, "WriteP1");
255 return false;
256 }
257
258 auto p1Name = GetP1Name(id);
259 return fFileManager->WriteTExtra<tools::histo::p1d>(fileName, p1d, p1Name);
260}
261
262//_____________________________________________________________________________
264{
265 // Experimental extra write
266
267 // Do not write histo on worker (redundant and fails in hdf5 )
268 // If default file is not used, users have to call Merge from their code
269 if ( G4Threading::IsWorkerThread() ) return false;
270
271 auto p2d = GetP2(id, false);
272 if (p2d == nullptr) {
273 WriteHnWarning("P2", id, fkClass, "WriteP2");
274 return false;
275 }
276
277 auto p2Name = GetP2Name(id);
278 return fFileManager->WriteTExtra<tools::histo::p2d>(fileName, p2d, p2Name);
279}
280
281//_____________________________________________________________________________
283 G4bool warn, G4bool onlyIfActive) const
284{
285 return fNtupleBookingManager->GetNtuple(warn, onlyIfActive);
286}
287
288//_____________________________________________________________________________
290 G4int ntupleId, G4bool warn, G4bool onlyIfActive) const
291{
292 return fNtupleBookingManager->GetNtuple(ntupleId, warn, onlyIfActive);
293}
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
G4bool WriteP2(G4int id, const G4String &fileName)
G4bool WriteH2(G4int id, const G4String &fileName)
tools::ntuple_booking * GetNtuple(G4bool warn=true, G4bool onlyIfActive=true) const
G4bool WriteH3(G4int id, const G4String &fileName)
G4bool WriteH1(G4int id, const G4String &fileName)
G4bool WriteP1(G4int id, const G4String &fileName)
static G4GenericAnalysisManager * Instance()
G4bool OpenFileImpl(const G4String &fileName) override
void SetDefaultFileTypeImpl(const G4String &value) override
tools::histo::h3d * GetH3(G4int id, G4bool warn=true, G4bool onlyIfActive=true) const
tools::histo::p1d * GetP1(G4int id, G4bool warn=true, G4bool onlyIfActive=true) const
tools::histo::p2d * GetP2(G4int id, G4bool warn=true, G4bool onlyIfActive=true) const
G4bool OpenFileImpl(const G4String &fileName) override
tools::histo::h2d * GetH2(G4int id, G4bool warn=true, G4bool onlyIfActive=true) const
tools::histo::h1d * GetH1(G4int id, G4bool warn=true, G4bool onlyIfActive=true) const
virtual void SetDefaultFileTypeImpl(const G4String &value)
G4String GetH1Name(G4int id) const
G4String GetH2Name(G4int id) const
G4String GetP2Name(G4int id) const
std::shared_ptr< G4NtupleBookingManager > fNtupleBookingManager
void Message(G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
G4AnalysisManagerState fState
G4String GetP1Name(G4int id) const
G4String GetH3Name(G4int id) const
void SetFileManager(std::shared_ptr< G4VFileManager > fileManager)
void SetNtupleFileManager(std::shared_ptr< G4VNtupleFileManager > ntupleFileManager)
G4String GetExtension(const G4String &fileName, const G4String &defaultExtension="")
constexpr G4int kVL3
G4AnalysisOutput GetOutput(const G4String &outputName, G4bool warn=true)
constexpr G4int kVL4
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)
G4bool IsWorkerThread()