Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4CsvAnalysisReader.cc
이 파일의 문서화 페이지로 가기
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"
33 #include "G4AnalysisVerbose.hh"
34 #include "G4AnalysisUtilities.hh"
35 #include "G4Threading.hh"
36 
37 #include <tools/aida_ntuple>
38 #include <tools/rcsv_histo>
39 
40 #include <iostream>
41 #include <cstdio>
42 
43 using namespace G4Analysis;
44 
47 
48 //
49 // utility functions
50 //
51 
52 namespace {
53 
54 //_____________________________________________________________________________
55 void* ReadObject(std::istream& hnFile,
56  const G4String& objectType,
57  const G4String& fileName,
58  const G4String& inFunction)
59 {
60  tools::rcsv::histo handler(hnFile);
61  std::string objectTypeInFile;
62  void* object;
63  auto verbose = false;
64  if ( ! handler.read(G4cout, objectTypeInFile, object, verbose) ) {
65  G4ExceptionDescription description;
66  description
67  << " "
68  << "Cannot get "<< objectType << " in file " << fileName;
69  G4String inFunctionFull = "G4CsvAnalysisReader::";
70  inFunctionFull.append(inFunction);
71  G4Exception(inFunctionFull, "Analysis_WRnullptr11", JustWarning, description);
72  return nullptr;
73  }
74  if ( objectTypeInFile != objectType ) {
75  G4ExceptionDescription description;
76  description
77  << " "
78  << "Object type read in "<< fileName
79  << " does not match" << G4endl;
80  G4String inFunctionFull = "G4CsvAnalysisReader::";
81  inFunctionFull.append(inFunction);
82  G4Exception(inFunctionFull, "Analysis_WR011", JustWarning, description);
83  return nullptr;
84  }
85 
86  return object;
87 }
88 
89 }
90 
91 //_____________________________________________________________________________
93 {
94  if ( fgInstance == nullptr ) {
95  G4bool isMaster = ! G4Threading::IsWorkerThread();
96  fgInstance = new G4CsvAnalysisReader(isMaster);
97  }
98 
99  return fgInstance;
100 }
101 
102 //_____________________________________________________________________________
104  : G4ToolsAnalysisReader("Csv", isMaster),
105  fNtupleManager(nullptr),
106  fFileManager(nullptr)
107 {
108  if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
109  G4ExceptionDescription description;
110  description
111  << " "
112  << "G4CsvAnalysisReader already exists."
113  << "Cannot create another instance.";
114  G4Exception("G4CsvAnalysisReader::G4CsvAnalysisReader()",
115  "Analysis_F001", FatalException, description);
116  }
117  if ( isMaster ) fgMasterInstance = this;
118  fgInstance = this;
119 
120  // Create managers
123  // The managers will be deleted by the base class
124 
125  // Set managers to base class
128 }
129 
130 //_____________________________________________________________________________
132 {
133  if ( fState.GetIsMaster() ) fgMasterInstance = nullptr;
134  fgInstance = nullptr;
135 }
136 
137 //
138 // private methods
139 //
140 
141 //_____________________________________________________________________________
143  const G4String& hnType,
144  const G4String& hnName,
145  const G4String& fileName,
146  G4bool isUserFileName) const
147 {
148  if ( isUserFileName ) {
149  return fFileManager->GetFullFileName(fileName);
150  }
151  else {
152  return fFileManager->GetHnFileName(hnType, hnName);
153  }
154 }
155 
156 //_____________________________________________________________________________
158 {
159 // Reset histograms and ntuple
160 
161  auto finalResult = true;
162 
164  finalResult = finalResult && result;
165 
166  result = fNtupleManager->Reset();
167  finalResult = finalResult && result;
168 
169  return finalResult;
170 }
171 
172 //
173 // protected methods
174 //
175 
176 //_____________________________________________________________________________
178  const G4String& fileName,
179  const G4String& /*dirName*/,
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  const G4String& /*dirName*/,
222  G4bool isUserFileName)
223 {
224 #ifdef G4VERBOSE
225  if ( fState.GetVerboseL4() )
226  fState.GetVerboseL4()->Message("read", "h2", h2Name);
227 #endif
228 
229  // open file
230  auto h2FileName = GetHnFileName("h2", h2Name, fileName, isUserFileName);
231  std::ifstream hnFile(h2FileName);
232  if ( ! hnFile.is_open() ) {
233  G4ExceptionDescription description;
234  description << " " << "Cannot open file " << h2FileName;
235  G4Exception("G4CsvAnalysisReader::ReadH2Impl()",
236  "Analysis_WR001", JustWarning, description);
237  return kInvalidId;
238  }
239 #ifdef G4VERBOSE
240  if ( fState.GetVerboseL1() )
242  ->Message("open", "read file", h2FileName);
243 #endif
244 
245  void* object
246  = ReadObject(hnFile, tools::histo::h2d::s_class(), h2FileName, "ReadH2Impl");
247  if ( ! object ) return kInvalidId;
248 
249  auto h2 = static_cast<tools::histo::h2d*>(object);
250  auto id = fH2Manager->AddH2(h2Name, h2);
251 
252 #ifdef G4VERBOSE
253  if ( fState.GetVerboseL2() )
254  fState.GetVerboseL2()->Message("read", "h2", h2Name, id > kInvalidId);
255 #endif
256 
257  return id;
258 }
259 
260 //_____________________________________________________________________________
262  const G4String& fileName,
263  const G4String& /*dirName*/,
264  G4bool isUserFileName)
265 {
266 #ifdef G4VERBOSE
267  if ( fState.GetVerboseL4() )
268  fState.GetVerboseL4()->Message("read", "h3", h3Name);
269 #endif
270 
271  // open file
272  auto h3FileName = GetHnFileName("h3", h3Name, fileName, isUserFileName);
273  std::ifstream hnFile(h3FileName);
274  if ( ! hnFile.is_open() ) {
275  G4ExceptionDescription description;
276  description << " " << "Cannot open file " << h3FileName;
277  G4Exception("G4CsvAnalysisReader::ReadH3Impl()",
278  "Analysis_WR001", JustWarning, description);
279  return kInvalidId;
280  }
281 #ifdef G4VERBOSE
282  if ( fState.GetVerboseL1() )
284  ->Message("open", "read file", h3FileName);
285 #endif
286 
287  void* object
288  = ReadObject(hnFile, tools::histo::h3d::s_class(), h3FileName, "ReadH3Impl");
289  if ( ! object ) return kInvalidId;
290 
291  auto h3 = static_cast<tools::histo::h3d*>(object);
292  auto id = fH3Manager->AddH3(h3Name, h3);
293 
294 #ifdef G4VERBOSE
295  if ( fState.GetVerboseL2() )
296  fState.GetVerboseL2()->Message("read", "h3", h3Name, id > kInvalidId);
297 #endif
298 
299  return id;
300 }
301 
302 //_____________________________________________________________________________
304  const G4String& fileName,
305  const G4String& /*dirName*/,
306  G4bool isUserFileName)
307 {
308 #ifdef G4VERBOSE
309  if ( fState.GetVerboseL4() )
310  fState.GetVerboseL4()->Message("read", "p1", p1Name);
311 #endif
312 
313  // open file
314  G4String p1FileName = GetHnFileName("p1", p1Name, fileName, isUserFileName);
315  std::ifstream hnFile(p1FileName);
316  if ( ! hnFile.is_open() ) {
317  G4ExceptionDescription description;
318  description << " " << "Cannot open file " << p1FileName;
319  G4Exception("G4CsvAnalysisReader::ReadP1Impl()",
320  "Analysis_WR001", JustWarning, description);
321  return kInvalidId;
322  }
323 #ifdef G4VERBOSE
324  if ( fState.GetVerboseL1() )
326  ->Message("open", "read file", p1FileName);
327 #endif
328 
329  void* object
330  = ReadObject(hnFile, tools::histo::p1d::s_class(), fileName, "ReadP1Impl");
331  if ( ! object ) return kInvalidId;
332 
333  auto p1 = static_cast<tools::histo::p1d*>(object);
334  auto id = fP1Manager->AddP1(p1Name, p1);
335 
336 #ifdef G4VERBOSE
337  if ( fState.GetVerboseL2() )
338  fState.GetVerboseL2()->Message("read", "p1", p1Name, id > kInvalidId);
339 #endif
340 
341  return id;
342 }
343 
344 //_____________________________________________________________________________
346  const G4String& fileName,
347  const G4String& /*dirName*/,
348  G4bool isUserFileName)
349 {
350 #ifdef G4VERBOSE
351  if ( fState.GetVerboseL4() )
352  fState.GetVerboseL4()->Message("read", "p2", p2Name);
353 #endif
354 
355  // open file
356  G4String p2FileName = GetHnFileName("p2", p2Name, fileName, isUserFileName);
357  std::ifstream hnFile(p2FileName);
358  if ( ! hnFile.is_open() ) {
359  G4ExceptionDescription description;
360  description << " " << "Cannot open file " << p2FileName;
361  G4Exception("G4CsvAnalysisReader::ReadP2Impl()",
362  "Analysis_WR001", JustWarning, description);
363  return kInvalidId;
364  }
365 #ifdef G4VERBOSE
366  if ( fState.GetVerboseL1() )
368  ->Message("open", "read file", p2FileName);
369 #endif
370 
371  void* object
372  = ReadObject(hnFile, tools::histo::p2d::s_class(), p2FileName, "ReadP2Impl");
373  if ( ! object ) return kInvalidId;
374 
375  auto p2 = static_cast<tools::histo::p2d*>(object);
376  auto id = fP2Manager->AddP2(p2Name, p2);
377 
378 
379 #ifdef G4VERBOSE
380  if ( fState.GetVerboseL2() )
381  fState.GetVerboseL2()->Message("read", "p2", p2Name, id > kInvalidId);
382 #endif
383 
384  return id;
385 }
386 
387 //_____________________________________________________________________________
389  const G4String& fileName,
390  const G4String& /*dirName*/,
391  G4bool isUserFileName)
392 {
393 #ifdef G4VERBOSE
394  if ( fState.GetVerboseL4() )
395  fState.GetVerboseL4()->Message("read", "ntuple", ntupleName);
396 #endif
397 
398  // Ntuples are saved per object and per thread
399  // but apply the ntuple name and the thread suffixes
400  // only if fileName is not provided explicitly
401  G4String fullFileName = fileName;
402  if ( ! isUserFileName ) {
403  fullFileName = fFileManager->GetNtupleFileName(ntupleName);
404  }
405 
406  // Open file
407  if ( ! fFileManager->OpenRFile(fullFileName) ) return kInvalidId;
408  auto ntupleFile = fFileManager->GetRFile(fullFileName);
409 
410  // Create ntuple
411  auto rntuple = new tools::rcsv::ntuple(*ntupleFile);
413 
414 #ifdef G4VERBOSE
415  if ( fState.GetVerboseL2() )
416  fState.GetVerboseL2()->Message("read", "ntuple", ntupleName, id > kInvalidId);
417 #endif
418 
419  return id;
420 }
virtual G4int ReadH3Impl(const G4String &h3Name, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
const G4AnalysisVerbose * GetVerboseL2() const
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:45
G4H3ToolsManager * fH3Manager
G4AnalysisManagerState fState
G4String GetNtupleFileName(const G4String &ntupleName) const
TTree * ntuple
TH1F * h3
#define G4endl
Definition: G4ios.hh:61
G4CsvAnalysisReader(G4bool isMaster=true)
virtual G4int ReadNtupleImpl(const G4String &ntupleName, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
static G4CsvAnalysisReader * Instance()
virtual G4int ReadH2Impl(const G4String &h2Name, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
std::ifstream * GetRFile(const G4String &fileName) const
#define G4ThreadLocal
Definition: tls.hh:69
G4String GetFullFileName(const G4String &baseFileName="", G4bool isPerThread=true) const
TH1D * h1d
G4String GetHnFileName(const G4String &hnType, const G4String &hnName, const G4String &baseFileName, G4bool isUserFileName) const
void SetNtupleManager(G4VRNtupleManager *ntupleManager)
G4int AddH1(const G4String &name, tools::histo::h1d *h1d)
G4String GetHnFileName(const G4String &hnType, const G4String &hnName) const
void SetFileManager(G4BaseFileManager *fileManager)
static G4CsvAnalysisReader * fgMasterInstance
bool G4bool
Definition: G4Types.hh:79
G4H2ToolsManager * fH2Manager
G4int AddH3(const G4String &name, tools::histo::h3d *h3d)
G4bool IsWorkerThread()
Definition: G4Threading.cc:129
const G4AnalysisVerbose * GetVerboseL4() const
virtual G4int ReadP1Impl(const G4String &p1Name, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
G4int AddP1(const G4String &name, tools::histo::p1d *p1d)
G4P1ToolsManager * fP1Manager
const G4int kInvalidId
G4CsvRFileManager * fFileManager
G4double G4ParticleHPJENDLHEData::G4double result
TH1F * h2
virtual G4int ReadH1Impl(const G4String &h1Name, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65
G4int SetNtuple(G4TRNtupleDescription< TNTUPLE > *rntupleDescription)
virtual G4bool OpenRFile(const G4String &fileName)
int G4int
Definition: G4Types.hh:78
G4int AddP2(const G4String &name, tools::histo::p2d *p2d)
G4P2ToolsManager * fP2Manager
static G4ThreadLocal G4CsvAnalysisReader * fgInstance
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
G4GLOB_DLL std::ostream G4cout
G4int AddH2(const G4String &name, tools::histo::h2d *h2d)
TH1F * h1
G4String & append(const G4String &)
virtual G4int ReadP2Impl(const G4String &p2Name, const G4String &fileName, const G4String &dirName, G4bool isUserFileName) final
G4H1ToolsManager * fH1Manager
const G4AnalysisVerbose * GetVerboseL1() const
G4CsvRNtupleManager * fNtupleManager