Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4CsvAnalysisReader.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, 05/09/2014 ([email protected])
28
30#include "G4CsvRFileManager.hh"
32#include "G4AnalysisVerbose.hh"
34#include "G4Threading.hh"
35
36#include <tools/aida_ntuple>
37#include <tools/rcsv_histo>
38
39#include <iostream>
40#include <cstdio>
41
42using namespace G4Analysis;
43
44G4CsvAnalysisReader* G4CsvAnalysisReader::fgMasterInstance = nullptr;
45G4ThreadLocal G4CsvAnalysisReader* G4CsvAnalysisReader::fgInstance = nullptr;
46
47//
48// utility functions
49//
50
51namespace {
52
53//_____________________________________________________________________________
54void* ReadObject(std::istream& hnFile,
55 const G4String& objectType,
56 const G4String& fileName,
57 const G4String& inFunction)
58{
59 tools::rcsv::histo handler(hnFile);
60 std::string objectTypeInFile;
61 void* object;
62 auto verbose = false;
63 if ( ! handler.read(G4cout, objectTypeInFile, object, verbose) ) {
64 G4ExceptionDescription description;
65 description
66 << " "
67 << "Cannot get "<< objectType << " in file " << fileName;
68 G4String inFunctionFull = "G4CsvAnalysisReader::";
69 inFunctionFull.append(inFunction);
70 G4Exception(inFunctionFull, "Analysis_WR011", JustWarning, description);
71 return nullptr;
72 }
73 if ( objectTypeInFile != objectType ) {
74 G4ExceptionDescription description;
75 description
76 << " "
77 << "Object type read in "<< fileName
78 << " does not match" << G4endl;
79 G4String inFunctionFull = "G4CsvAnalysisReader::";
80 inFunctionFull.append(inFunction);
81 G4Exception(inFunctionFull, "Analysis_WR011", JustWarning, description);
82 return nullptr;
83 }
84
85 return object;
86}
87
88}
89
90//_____________________________________________________________________________
92{
93 if ( fgInstance == nullptr ) {
95 fgInstance = new G4CsvAnalysisReader(isMaster);
96 }
97
98 return fgInstance;
99}
100
101//_____________________________________________________________________________
103 : G4ToolsAnalysisReader("Csv", isMaster),
104 fNtupleManager(nullptr),
105 fFileManager(nullptr)
106{
107 if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
108 G4ExceptionDescription description;
109 description
110 << " "
111 << "G4CsvAnalysisReader already exists."
112 << "Cannot create another instance.";
113 G4Exception("G4CsvAnalysisReader::G4CsvAnalysisReader()",
114 "Analysis_F001", FatalException, description);
115 }
116 if ( isMaster ) fgMasterInstance = this;
117 fgInstance = this;
118
119 // Create managers
120 fNtupleManager = new G4CsvRNtupleManager(fState);
121 fFileManager = new G4CsvRFileManager(fState);
122 // The managers will be deleted by the base class
123
124 // Set managers to base class
125 SetNtupleManager(fNtupleManager);
126 SetFileManager(fFileManager);
127}
128
129//_____________________________________________________________________________
131{
132 if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
133 fgInstance = nullptr;
134}
135
136//
137// private methods
138//
139
140//_____________________________________________________________________________
141G4String G4CsvAnalysisReader::GetHnFileName(
142 const G4String& hnType,
143 const G4String& hnName,
144 const G4String& fileName,
145 G4bool isUserFileName) const
146{
147 if ( isUserFileName ) {
148 return fFileManager->GetFullFileName(fileName);
149 }
150 else {
151 return fFileManager->GetHnFileName(hnType, hnName);
152 }
153}
154
155//_____________________________________________________________________________
156G4bool G4CsvAnalysisReader::Reset()
157{
158// Reset histograms and ntuple
159
160 auto finalResult = true;
161
162 auto result = G4ToolsAnalysisReader::Reset();
163 finalResult = finalResult && result;
164
165 result = fNtupleManager->Reset();
166 finalResult = finalResult && result;
167
168 return finalResult;
169}
170
171//
172// protected methods
173//
174
175//_____________________________________________________________________________
177 const G4String& fileName,
178 const G4String& /*dirName*/,
179 G4bool isUserFileName)
180{
181#ifdef G4VERBOSE
182 if ( fState.GetVerboseL4() )
183 fState.GetVerboseL4()->Message("get", "h1", h1Name);
184#endif
185
186 // open file
187 auto h1FileName = GetHnFileName("h1", h1Name, fileName, isUserFileName);
188 std::ifstream hnFile(h1FileName);
189 if ( ! hnFile.is_open() ) {
190 G4ExceptionDescription description;
191 description << " " << "Cannot open file " << h1FileName;
192 G4Exception("G4CsvAnalysisReader::ReadH1Impl()",
193 "Analysis_WR001", JustWarning, description);
194 return kInvalidId;
195 }
196#ifdef G4VERBOSE
197 if ( fState.GetVerboseL1() )
199 ->Message("open", "read file", h1FileName);
200#endif
201
202 void* object
203 = ReadObject(hnFile, tools::histo::h1d::s_class(), h1FileName, "ReadH1Impl");
204 if ( ! object ) return kInvalidId;
205
206 auto h1 = static_cast<tools::histo::h1d*>(object);
207 auto id = fH1Manager->AddH1(h1Name, h1);
208
209#ifdef G4VERBOSE
210 if ( fState.GetVerboseL2() )
211 fState.GetVerboseL2()->Message("read", "h1", h1Name, id > kInvalidId);
212#endif
213
214 return id;
215}
216
217//_____________________________________________________________________________
219 const G4String& fileName,
220 const G4String& /*dirName*/,
221 G4bool isUserFileName)
222{
223#ifdef G4VERBOSE
224 if ( fState.GetVerboseL4() )
225 fState.GetVerboseL4()->Message("read", "h2", h2Name);
226#endif
227
228 // open file
229 auto h2FileName = GetHnFileName("h2", h2Name, fileName, isUserFileName);
230 std::ifstream hnFile(h2FileName);
231 if ( ! hnFile.is_open() ) {
232 G4ExceptionDescription description;
233 description << " " << "Cannot open file " << h2FileName;
234 G4Exception("G4CsvAnalysisReader::ReadH2Impl()",
235 "Analysis_WR001", JustWarning, description);
236 return kInvalidId;
237 }
238#ifdef G4VERBOSE
239 if ( fState.GetVerboseL1() )
241 ->Message("open", "read file", h2FileName);
242#endif
243
244 void* object
245 = ReadObject(hnFile, tools::histo::h2d::s_class(), h2FileName, "ReadH2Impl");
246 if ( ! object ) return kInvalidId;
247
248 auto h2 = static_cast<tools::histo::h2d*>(object);
249 auto id = fH2Manager->AddH2(h2Name, h2);
250
251#ifdef G4VERBOSE
252 if ( fState.GetVerboseL2() )
253 fState.GetVerboseL2()->Message("read", "h2", h2Name, id > kInvalidId);
254#endif
255
256 return id;
257}
258
259//_____________________________________________________________________________
261 const G4String& fileName,
262 const G4String& /*dirName*/,
263 G4bool isUserFileName)
264{
265#ifdef G4VERBOSE
266 if ( fState.GetVerboseL4() )
267 fState.GetVerboseL4()->Message("read", "h3", h3Name);
268#endif
269
270 // open file
271 auto h3FileName = GetHnFileName("h3", h3Name, fileName, isUserFileName);
272 std::ifstream hnFile(h3FileName);
273 if ( ! hnFile.is_open() ) {
274 G4ExceptionDescription description;
275 description << " " << "Cannot open file " << h3FileName;
276 G4Exception("G4CsvAnalysisReader::ReadH3Impl()",
277 "Analysis_WR001", JustWarning, description);
278 return kInvalidId;
279 }
280#ifdef G4VERBOSE
281 if ( fState.GetVerboseL1() )
283 ->Message("open", "read file", h3FileName);
284#endif
285
286 void* object
287 = ReadObject(hnFile, tools::histo::h3d::s_class(), h3FileName, "ReadH3Impl");
288 if ( ! object ) return kInvalidId;
289
290 auto h3 = static_cast<tools::histo::h3d*>(object);
291 auto id = fH3Manager->AddH3(h3Name, h3);
292
293#ifdef G4VERBOSE
294 if ( fState.GetVerboseL2() )
295 fState.GetVerboseL2()->Message("read", "h3", h3Name, id > kInvalidId);
296#endif
297
298 return id;
299}
300
301//_____________________________________________________________________________
303 const G4String& fileName,
304 const G4String& /*dirName*/,
305 G4bool isUserFileName)
306{
307#ifdef G4VERBOSE
308 if ( fState.GetVerboseL4() )
309 fState.GetVerboseL4()->Message("read", "p1", p1Name);
310#endif
311
312 // open file
313 G4String p1FileName = GetHnFileName("p1", p1Name, fileName, isUserFileName);
314 std::ifstream hnFile(p1FileName);
315 if ( ! hnFile.is_open() ) {
316 G4ExceptionDescription description;
317 description << " " << "Cannot open file " << p1FileName;
318 G4Exception("G4CsvAnalysisReader::ReadP1Impl()",
319 "Analysis_WR001", JustWarning, description);
320 return kInvalidId;
321 }
322#ifdef G4VERBOSE
323 if ( fState.GetVerboseL1() )
325 ->Message("open", "read file", p1FileName);
326#endif
327
328 void* object
329 = ReadObject(hnFile, tools::histo::p1d::s_class(), fileName, "ReadP1Impl");
330 if ( ! object ) return kInvalidId;
331
332 auto p1 = static_cast<tools::histo::p1d*>(object);
333 auto id = fP1Manager->AddP1(p1Name, p1);
334
335#ifdef G4VERBOSE
336 if ( fState.GetVerboseL2() )
337 fState.GetVerboseL2()->Message("read", "p1", p1Name, id > kInvalidId);
338#endif
339
340 return id;
341}
342
343//_____________________________________________________________________________
345 const G4String& fileName,
346 const G4String& /*dirName*/,
347 G4bool isUserFileName)
348{
349#ifdef G4VERBOSE
350 if ( fState.GetVerboseL4() )
351 fState.GetVerboseL4()->Message("read", "p2", p2Name);
352#endif
353
354 // open file
355 G4String p2FileName = GetHnFileName("p2", p2Name, fileName, isUserFileName);
356 std::ifstream hnFile(p2FileName);
357 if ( ! hnFile.is_open() ) {
358 G4ExceptionDescription description;
359 description << " " << "Cannot open file " << p2FileName;
360 G4Exception("G4CsvAnalysisReader::ReadP2Impl()",
361 "Analysis_WR001", JustWarning, description);
362 return kInvalidId;
363 }
364#ifdef G4VERBOSE
365 if ( fState.GetVerboseL1() )
367 ->Message("open", "read file", p2FileName);
368#endif
369
370 void* object
371 = ReadObject(hnFile, tools::histo::p2d::s_class(), p2FileName, "ReadP2Impl");
372 if ( ! object ) return kInvalidId;
373
374 auto p2 = static_cast<tools::histo::p2d*>(object);
375 auto id = fP2Manager->AddP2(p2Name, p2);
376
377
378#ifdef G4VERBOSE
379 if ( fState.GetVerboseL2() )
380 fState.GetVerboseL2()->Message("read", "p2", p2Name, id > kInvalidId);
381#endif
382
383 return id;
384}
385
386//_____________________________________________________________________________
388 const G4String& fileName,
389 const G4String& /*dirName*/,
390 G4bool isUserFileName)
391{
392#ifdef G4VERBOSE
393 if ( fState.GetVerboseL4() )
394 fState.GetVerboseL4()->Message("read", "ntuple", ntupleName);
395#endif
396
397 // Ntuples are saved per object and per thread
398 // but apply the ntuple name and the thread suffixes
399 // only if fileName is not provided explicitly
400 G4String fullFileName = fileName;
401 if ( ! isUserFileName ) {
402 fullFileName = fFileManager->GetNtupleFileName(ntupleName);
403 }
404
405 // Open file
406 if ( ! fFileManager->OpenRFile(fullFileName) ) return kInvalidId;
407 auto ntupleFile = fFileManager->GetRFile(fullFileName);
408
409 // Create ntuple
410 auto rntuple = new tools::rcsv::ntuple(*ntupleFile);
411 auto id = fNtupleManager->SetNtuple(new G4TRNtupleDescription<tools::rcsv::ntuple>(rntuple));
412
413#ifdef G4VERBOSE
414 if ( fState.GetVerboseL2() )
415 fState.GetVerboseL2()->Message("read", "ntuple", ntupleName, id > kInvalidId);
416#endif
417
418 return id;
419}
@ JustWarning
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:40
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
const G4AnalysisVerbose * GetVerboseL2() const
const G4AnalysisVerbose * GetVerboseL1() const
const G4AnalysisVerbose * GetVerboseL4() const
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
G4String GetFullFileName(const G4String &baseFileName="", G4bool isPerThread=true) const
G4String GetHnFileName(const G4String &hnType, const G4String &hnName) const
G4String GetNtupleFileName(const G4String &ntupleName) const
virtual G4int ReadNtupleImpl(const G4String &ntupleName, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
G4CsvAnalysisReader(G4bool isMaster=true)
static G4CsvAnalysisReader * Instance()
virtual G4int ReadP1Impl(const G4String &p1Name, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
virtual G4int ReadH3Impl(const G4String &h3Name, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
virtual G4int ReadH1Impl(const G4String &h1Name, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
virtual G4int ReadH2Impl(const G4String &h2Name, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
virtual G4int ReadP2Impl(const G4String &p2Name, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
std::ifstream * GetRFile(const G4String &fileName) const
virtual G4bool OpenRFile(const G4String &fileName)
G4int AddH1(const G4String &name, tools::histo::h1d *h1d)
G4int AddH2(const G4String &name, tools::histo::h2d *h2d)
G4int AddH3(const G4String &name, tools::histo::h3d *h3d)
G4int AddP1(const G4String &name, tools::histo::p1d *p1d)
G4int AddP2(const G4String &name, tools::histo::p2d *p2d)
G4String & append(const G4String &)
G4int SetNtuple(G4TRNtupleDescription< TNTUPLE > *rntupleDescription)
G4H3ToolsManager * fH3Manager
G4P1ToolsManager * fP1Manager
G4P2ToolsManager * fP2Manager
G4H1ToolsManager * fH1Manager
G4H2ToolsManager * fH2Manager
void SetFileManager(G4BaseFileManager *fileManager)
void SetNtupleManager(G4VRNtupleManager *ntupleManager)
G4AnalysisManagerState fState
const G4int kInvalidId
G4bool IsWorkerThread()
Definition: G4Threading.cc:123
#define G4ThreadLocal
Definition: tls.hh:77