Geant4-11
G4RootHnRFileManager.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/rroot/file"
32#include "tools/rroot/rall"
33#include "tools/rroot/streamers"
34
35//_____________________________________________________________________________
36template <typename HT>
37inline
38std::tuple<tools::rroot::buffer*, tools::rroot::TDirectory*>
39G4RootHnRFileManager<HT>::GetBuffer(
40 const G4String& fileName, const G4String& dirName, const G4String& objectName)
41{
42// Get buffer for reading histogram or profile specified by objectNmae
43// for a file specified by fileName;
44// Open the file if it was not yet open
45
46 // Histograms and profiles are not saved per thread
47 G4bool isPerThread = false;
48
49 // Get or open a file
50 auto rfile = fRFileManager->GetRFile(fileName, isPerThread);
51 if ( ! rfile ) {
52 if ( ! fRFileManager->OpenRFile(fileName, isPerThread) ) {
53 return std::tuple(nullptr, nullptr);
54 }
55 rfile = fRFileManager->GetRFile(fileName, isPerThread);
56 }
57
58 // Get key
59 tools::rroot::key* key = nullptr;
60 tools::rroot::TDirectory* newDir = nullptr;
61 if ( ! dirName.empty() ) {
62 newDir = tools::rroot::find_dir(rfile->dir(), dirName);
63 if ( newDir ) {
64 key = newDir->find_key(objectName);
65 }
66 else {
67 G4Analysis::Warn(
68 "Directory " + dirName + " not found in file " + fileName + ".",
69 fkClass, "GetBuffer");
70 return std::tuple(nullptr, nullptr);
71 }
72 }
73 else {
74 key = rfile->dir().find_key(objectName);
75 }
76
77 if ( ! key ) {
78 G4Analysis::Warn(
79 "Key " + objectName + " for Histogram/Profile not found in file " +
80 fileName + ", directory " + dirName, fkClass, "GetBuffer");
81 return std::tuple(nullptr, newDir);
82 }
83
84 unsigned int size;
85 char* charBuffer = key->get_object_buffer(*rfile, size);
86
87 if ( ! charBuffer ) {
88 G4Analysis::Warn(
89 "Cannot get " + objectName + " in file " + fileName,
90 fkClass, "GetBuffer");
91 return std::tuple(nullptr, newDir);
92 }
93
94 auto verbose = false;
95 return std::tuple(new tools::rroot::buffer(G4cout, rfile->byte_swap(), size, charBuffer,
96 key->key_length(), verbose), newDir);
97}
98
99//_____________________________________________________________________________
100template <>
101inline
102tools::histo::h1d* G4RootHnRFileManager<tools::histo::h1d>::ReadT(
103 tools::rroot::buffer* buffer)
104{
105 return tools::rroot::TH1D_stream(*buffer);
106}
107
108//_____________________________________________________________________________
109template <>
110inline
111tools::histo::h2d* G4RootHnRFileManager<tools::histo::h2d>::ReadT(
112 tools::rroot::buffer* buffer)
113{
114 return tools::rroot::TH2D_stream(*buffer);
115}
116
117//_____________________________________________________________________________
118template <>
119inline
120tools::histo::h3d* G4RootHnRFileManager<tools::histo::h3d>::ReadT(
121 tools::rroot::buffer* buffer)
122{
123 return tools::rroot::TH3D_stream(*buffer);
124}
125
126//_____________________________________________________________________________
127template <>
128inline
129tools::histo::p1d* G4RootHnRFileManager<tools::histo::p1d>::ReadT(
130 tools::rroot::buffer* buffer)
131{
132 return tools::rroot::TProfile_stream(*buffer);
133}
134
135//_____________________________________________________________________________
136template <>
137inline
138tools::histo::p2d* G4RootHnRFileManager<tools::histo::p2d>::ReadT(
139 tools::rroot::buffer* buffer)
140{
141 return tools::rroot::TProfile2D_stream(*buffer);
142}
143
144//_____________________________________________________________________________
145template <typename HT>
146inline
147HT* G4RootHnRFileManager<HT>::Read(
148 const G4String& htName, const G4String& fileName, const G4String& dirName,
149 G4bool /*isUserFileName*/)
150{
151 auto [buffer, newDir] = GetBuffer(fileName, dirName, htName);
152 if ( ! buffer ) {
153 delete newDir;
154 return nullptr;
155 }
156
157 auto ht = ReadT(buffer);
158 delete buffer;
159 delete newDir;
160
161 if ( ! ht ) {
162 G4Analysis::Warn(
163 "Streaming " + htName + " in file " + fileName + " failed.",
164 fkClass, "Read");
165 return nullptr;
166 }
167
168 return ht;
169}