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