ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4Hdf5RFileManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4Hdf5RFileManager.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 
27 // Author: Ivana Hrivnacova, 20/07/2017 (ivana@ipno.in2p3.fr)
28 
29 #include "G4Hdf5RFileManager.hh"
31 #include "G4AnalysisUtilities.hh"
32 
33 #include "tools/hdf5/h2file"
34 #include <tools/zlib>
35 
36 #include <iostream>
37 #include <cstdio>
38 
39 using namespace G4Analysis;
40 
41 //_____________________________________________________________________________
43 
44 //_____________________________________________________________________________
46  : G4BaseFileManager(state),
47  fRFiles()
48 {
49 }
50 
51 //_____________________________________________________________________________
53 {
54 }
55 
56 //
57 // private methods
58 //
59 
60 //_____________________________________________________________________________
62  G4bool isPerThread)
63 {
64  // Get full file name
65  G4String name = GetFullFileName(fileName, isPerThread);
66 
67 #ifdef G4VERBOSE
68  if ( fState.GetVerboseL4() )
69  fState.GetVerboseL4()->Message("open", "read analysis file", name);
70 #endif
71 
72  // create new file
73  hid_t newFile = H5Fopen(name, H5F_ACC_RDONLY, H5P_DEFAULT);
74  if ( newFile < 0 ) {
75  G4ExceptionDescription description;
76  description << " " << "Cannot open file " << name;
77  G4Exception("G4Hdf5RFileManager::OpenFile()",
78  "Analysis_WR001", JustWarning, description);
79  return false;
80  }
81 
82  // newFile->add_unziper('Z',tools::decompress_buffer);
83 
84  // add file in a map
85  std::map<G4String, hid_t>::iterator it
86  = fRFiles.find(name);
87  if ( it != fRFiles.end() ) {
88  it->second = newFile;
89  }
90  else {
91  fRFiles[name] = newFile;
92  }
93 
94 #ifdef G4VERBOSE
95  if ( fState.GetVerboseL1() )
97  ->Message("open", "read analysis file", name);
98 #endif
99 
100  return true;
101 }
102 
103 //_____________________________________________________________________________
104 hid_t G4Hdf5RFileManager::OpenDirectory(hid_t file, const G4String& directoryName)
105 {
106 #ifdef G4VERBOSE
107  if ( fState.GetVerboseL4() ) {
108  fState.GetVerboseL4()->Message("open", "read directory", directoryName);
109  }
110 #endif
111 
112  auto directory = tools_H5Gopen(file, directoryName);
113  if ( directory < 0 ) {
114  G4ExceptionDescription description;
115  description << " "
116  << "cannot open directory " << directoryName;
117  G4Exception("G4Hdf5RFileManager::OpenDirectory()",
118  "Analysis_W001", JustWarning, description);
119  return kInvalidId;
120  }
121  else {
122 #ifdef G4VERBOSE
123  if ( fState.GetVerboseL2() ) {
124  fState.GetVerboseL2()->Message("open", "read directory", directoryName);
125  }
126 #endif
127  return directory;
128  }
129 }
130 
131 //_____________________________________________________________________________
132 hid_t G4Hdf5RFileManager::GetRDirectory(const G4String& directoryType,
133  const G4String& fileName,
134  const G4String& dirName,
135  G4bool isPerThread)
136 {
137  // Get or open a file
138  auto rfile = GetRFile(fileName, isPerThread);
139  if ( rfile < 0 ) {
140  // Try to open it if not found in the map
141  if ( ! OpenRFile(fileName, isPerThread) ) return kInvalidId;
142  rfile = GetRFile(fileName, isPerThread);
143  }
144 
145  // Use default directory name if not specified
146  auto newDirName = dirName;
147  if ( newDirName == "" ) {
148  // if ( fDefaultDirectory > 0 ) {
149  // // Return the default directory if the name is not set and the default directory
150  // // already exists
151  // directory = fDefaultDirectory;
152  // return true;
153  // } else {
154  // Create the default directory if the name is not set and the default directory
155  // does not yet exist
156  newDirName = fgkDefaultDirectoryName;
157  newDirName += "_";
158  newDirName += directoryType;
159  }
160 
161  // Open directory
162  return OpenDirectory(rfile, newDirName);
163 }
164 
165 
166 //
167 // public methods
168 //
169 
170 //_____________________________________________________________________________
172  G4bool isPerThread) const
173 {
174  // Get full file name
175  G4String name = GetFullFileName(fileName, isPerThread);
176 
177  std::map<G4String, hid_t>::const_iterator it
178  = fRFiles.find(name);
179  if ( it != fRFiles.end() )
180  return it->second;
181  else {
182  return kInvalidId;
183  }
184 }
185 
186 //_____________________________________________________________________________
187 hid_t G4Hdf5RFileManager::GetHistoRDirectory(const G4String& fileName, const G4String& dirName,
188  G4bool isPerThread)
189 {
190  return GetRDirectory("histograms", fileName, dirName, isPerThread);
191 }
192 
193 //_____________________________________________________________________________
194 hid_t G4Hdf5RFileManager::GetNtupleRDirectory(const G4String& fileName, const G4String& dirName,
195  G4bool isPerThread)
196 {
197  return GetRDirectory("ntuples", fileName, dirName, isPerThread);
198 }
199 
200