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