Geant4-11
G4CsvHnRFileManager.icc
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, 26/08/2021 (ivana@ipno.in2p3.fr)
28
29#include "G4AnalysisUtilities.hh"
30
31#include "tools/rcsv_histo"
32
33//_____________________________________________________________________________
34template <typename HT>
35inline
36G4String G4CsvHnRFileManager<HT>::GetHnFileName(
37 const G4String& hnType, const G4String& hnName,
38 const G4String& fileName, G4bool isUserFileName) const
39{
40 if ( isUserFileName ) {
41 return fRFileManager->GetFullFileName(fileName);
42 }
43 else {
44 return fRFileManager->GetHnFileName(hnType, hnName);
45 }
46}
47
48//_____________________________________________________________________________
49template <typename HT>
50inline
51HT* G4CsvHnRFileManager<HT>::ReadT(
52 std::istream& htFile, const G4String& fileName, std::string_view inFunction)
53{
54 tools::rcsv::histo handler(htFile);
55 std::string objectTypeInFile;
56 void* object;
57 auto verbose = false;
58 if ( ! handler.read(G4cout, objectTypeInFile, object, verbose) ) {
59 G4Analysis::Warn(
60 "Cannot get " + G4Analysis::GetHnType<HT>() + " in file " + fileName,
61 fkClass, inFunction);
62 return nullptr;
63 }
64 if ( objectTypeInFile.compare(HT::s_class()) != 0 ) {
65 G4Analysis::Warn(
66 "Object type read in " + G4Analysis::GetHnType<HT>() +" does not match",
67 fkClass, inFunction);
68 return nullptr;
69 }
70
71 return static_cast<HT*>(object);
72}
73
74//_____________________________________________________________________________
75template <typename HT>
76inline
77HT* G4CsvHnRFileManager<HT>::Read(
78 const G4String& htName, const G4String& fileName, const G4String& dirName,
79 G4bool isUserFileName)
80{
81 // Get file name
82 auto htFileName =
83 GetHnFileName(G4Analysis::GetHnType<HT>(), htName, fileName, isUserFileName);
84
85 // Update directory path
86 if ( ! dirName.empty() ) {
87 htFileName = "./" + dirName + "/" + htFileName;
88 }
89
90 std::ifstream htFile(htFileName);
91 if ( ! htFile.is_open() ) {
92 G4Analysis::Warn("Cannot open file " + htFileName, fkClass, "Read");
93 return nullptr;
94 }
95
96 auto ht = ReadT(htFile, htFileName, "Read");
97 return ht;
98}