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