GSI Object Oriented Online Offline (Go4)  GO4-6.1.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4HDF5Source.cxx
Go to the documentation of this file.
1 // $Id: TGo4HDF5Source.cxx 2810 2020-05-13 12:22:03Z linev $
2 //-----------------------------------------------------------------------
3 // The GSI Online Offline Object Oriented (Go4) Project
4 // Experiment Data Processing at EE department, GSI
5 //-----------------------------------------------------------------------
6 // Copyright (C) 2000- GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
7 // Planckstr. 1, 64291 Darmstadt, Germany
8 // Contact: http://go4.gsi.de
9 //-----------------------------------------------------------------------
10 // This software can be used under the license agreements as stated
11 // in Go4License.txt file which is part of the distribution.
12 //-----------------------------------------------------------------------
13 
14 
15 
16 
17 #include "TGo4HDF5Source.h"
18 
19 #include "TKey.h"
20 #include "TFile.h"
21 #include "TTree.h"
22 #include "TList.h"
23 #include "TSystem.h"
24 #include "TObjString.h"
25 #include "TRegexp.h"
26 #include "TDataMember.h"
27 
28 #include "TGo4Log.h"
30 #include "TGo4EventElement.h"
32 // get static name constants from here
33 #include "TGo4HDF5Store.h"
34 
35 
36 
37 TGo4HDF5Source::TGo4HDF5Source(const char* name) :
38  TGo4EventSource(name), TGo4HDF5Adapter(), fxReadBuffer(0), fiReadOffset(0),fxFilesNames(0)
39 {
40  fxFilesNames = ProducesFilesList(GetName());
41 
42  if (!OpenNextFile())
43  ThrowError(66,0, Form("!!! ERROR: Cannot open source %s!!!", GetName()));
44 }
45 
47  TGo4EventSource(par->GetName()), TGo4HDF5Adapter(), fxReadBuffer(0), fiReadOffset(0),fxFilesNames(0)
48 {
49  // TODO: all file regexp magic into base class for all eventsources
50  fxFilesNames = ProducesFilesList(GetName());
51 
52  if (!OpenNextFile())
53  ThrowError(66,0, Form("!!! ERROR: Cannot open source %s!!!", GetName()));
54 }
55 
56 
58  TGo4EventSource("Go4HDF5Source"),TGo4HDF5Adapter(), fxReadBuffer(0), fiReadOffset(0),fxFilesNames(0)
59 {
60  // for streamer, do not open here!
61 }
62 
64 {
66 
67  if (fxFilesNames) {
68  delete fxFilesNames;
69  fxFilesNames = 0;
70  }
71 }
72 
73 TList* TGo4HDF5Source::ProducesFilesList(const char* mask)
74 {
75  if ((mask==0) || (strlen(mask)==0)) return 0;
76 
77  TString dirname, basename(mask);
78 
79  if (!basename.MaybeWildcard()) {
80 
81  // add default suffix
82  if(strstr(basename.Data(),TGo4HDF5Adapter::fgcFILESUF)==0)
83  basename += TGo4HDF5Adapter::fgcFILESUF;
84 
85  TList* lst = new TList();
86  lst->SetOwner(kTRUE);
87  lst->Add(new TObjString(basename));
88  return lst;
89  }
90 
91  Bool_t withdir = kFALSE;
92  Int_t slash = basename.Last('/');
93 
94 #ifdef WIN32
95  if (slash<0) slash = basename.Last('\\');
96 #endif
97 
98  if (slash>=0) {
99  dirname = basename(0, slash);
100  basename.Remove(0, slash+1);
101  withdir = kTRUE;
102  } else {
103  dirname = gSystem->WorkingDirectory();
104  }
105 
106  void *dir = gSystem->OpenDirectory(gSystem->ExpandPathName(dirname.Data()));
107 
108  if (dir==0) return 0;
109 
110  TList* lst = 0;
111 
112  TRegexp re(basename, kTRUE);
113  const char* file = 0;
114  while ((file = gSystem->GetDirEntry(dir)) != 0) {
115  if (!strcmp(file,".") || !strcmp(file,"..")) continue;
116  TString s = file;
117  if ( (basename!=s) && s.Index(re) == kNPOS) continue;
118  if (lst==0) {
119  lst = new TList;
120  lst->SetOwner(kTRUE);
121  }
122  if (withdir)
123  lst->Add(new TObjString(dirname + "/" + file));
124  else
125  lst->Add(new TObjString(file));
126  }
127  gSystem->FreeDirectory(dir);
128 
129  if (lst) lst->Sort();
130 
131  return lst;
132 }
133 
135 {
137 
138  if ((fxFilesNames==0) || (fxFilesNames->GetSize()==0)) return kFALSE;
139 
140  TObject* obj = fxFilesNames->First();
141  fxCurrentFileName = obj->GetName();
142  fxFilesNames->Remove(fxFilesNames->FirstLink());
143  delete obj;
144 
145  OpenFile(fxCurrentFileName.Data());
146  return kTRUE;
147 }
148 
149 
151 {
152  try{
153  CloseFile();
154  TGo4Log::Info("TGo4HDF5Source: Closed file %s", fxCurrentFileName.Data());
155  fxCurrentFileName = "";
156  return kTRUE;
157  }
158  catch(H5::Exception& ex)
159  {
160  TString msg= TString::Format( "Close File %s with HDF5 exception in %s : %s\n", fxCurrentFileName.Data(), ex. getCFuncName (), ex.getCDetailMsg ());
161  TGo4Log::Error("TGo4HDF5Source: %s", msg.Data());
162  SetErrMess(msg.Data());
163  throw TGo4EventSourceException(this);
164  }
165 }
166 
167 
168 void TGo4HDF5Source::OpenFile(const char* fname)
169 {
170  TString buffer(fname);
171  if (strstr(buffer.Data(), TGo4HDF5Adapter::fgcFILESUF) == 0)
172  buffer.Append(TGo4HDF5Adapter::fgcFILESUF);
173 
174 try{
175  CloseFile();
176  fxFile = new H5::H5File(buffer.Data(), H5F_ACC_RDONLY);
177  TGo4Log::Info("TGo4HDF5Source %s: Open file %s for reading", GetName(), buffer.Data());
178 }
179 catch(H5::Exception& ex)
180 {
181  TString msg= TString::Format( "OpenFile with HDF5 exception in %s : %s\n", ex. getCFuncName (), ex.getCDetailMsg ());
182  TGo4Log::Error("TGo4HDF5Source: %s", msg.Data());
183  SetErrMess(msg.Data());
184  throw TGo4EventSourceException(this);
185 }
186 
187 
188 }
189 
190 
191 
193 {
194  // TODO: all hdf5 file and dataset treatment in adapter class common to source and store
195  if(fbDataSetExists) return;
196  if (event==0 || fxFile==0) return;
197 
198  try{
199 
200  BuildDataType(event);
202  fbDataSetExists=kTRUE;
203 }
204 
205 catch(H5::Exception& ex)
206 {
207  TString msg= TString::Format( "BuildDataSet with HDF5 exception in %s : %s\n", ex.getCFuncName (), ex.getCDetailMsg ());
208  TGo4Log::Error("TGo4HDF5Source: %s", msg.Data());
209  SetErrMess(msg.Data());
210  throw TGo4EventSourceException(this);
211 
212 }
213 
214 }
215 
216 
218 {
219  delete fxReadBuffer;
221 }
222 
223 
224 
225 
227 {
228  if(dest==0) ThrowError(0,22,"!!! ERROR BuildEvent: no destination event!!!");
229 
230 
231  fxEvent = dest; // address of next event into event pointer
232  BuildDataSet(dest);
233 
234  try{
235 
236 #ifdef GO4HDF5_DEBUG
237  printf("TGo4HDF5Source: fxEvent=0x%lx\n", (unsigned long) fxEvent);
238  printf("TGo4HDF5Source: Eventname:%s\n", fxEvent->GetName());
239  printf("TGo4HDF5Source: is valid:%d\n", fxEvent->IsValid());
240  printf("Go4 event has eventsource pointer 0x%lx \n",(long) fxEvent->GetEventSource());
241  printf("TGo4HDF5Source: Event printout:\n");
242  fxEvent->PrintEvent();
243  printf("\n");
244 #endif
245 
246  //fxHandle->SetObjectPointer(dest); // do it here to account dynamic changes in structure
248 
249 #ifdef GO4HDF5_DEBUG
250  printf("Go4 event has eventsource pointer 0x%lx \n",(long) fxEvent->GetEventSource());
251  printf("Go4 event has identifier 0x%lx \n",(long) fxEvent->getId());
252 #endif
253 
254  return kTRUE;
255  }
256  catch(H5::Exception& ex)
257  {
258  TString msg= TString::Format( "BuildEvent() with HDF5 exception in %s : %s\n", ex.getCFuncName (), ex.getCDetailMsg ());
259  TGo4Log::Error("TGo4HDF5Source: %s", msg.Data());
260  SetErrMess(msg.Data());
261  throw TGo4EventSourceException(this);
262  }
263 
264 }
265 
266 
virtual void BuildReadDataset(H5::H5File *file, TGo4HDF5Source *parent)
static const char * fgcFILESUF
H5::H5File * fxFile
TString fxCurrentFileName
void BuildDataType(TGo4EventElement *event, TGo4HDF5DataHandle *parent=0, Int_t index=0)
void ThrowError(Int_t creastat, Int_t errstat, const char *message,...)
Bool_t CloseCurrentFile()
void SetErrMess(const char *txt)
TList * fxFilesNames
virtual void Read(hsize_t sequencenum, H5::H5File *file)
virtual void DeleteDataSet()
TGo4EventElement * fxEvent
virtual Bool_t BuildEvent(TGo4EventElement *dest)
virtual void DeleteDataSet()
virtual ~TGo4HDF5Source()
static TList * ProducesFilesList(const char *mask)
virtual void OpenFile(const char *fname)
virtual void BuildDataSet(TGo4EventElement *event)
Char_t * fxReadBuffer
virtual void CloseFile()
static void Error(const char *text,...)
Definition: TGo4Log.cxx:313
static void Info(const char *text,...)
Definition: TGo4Log.cxx:287
string msg
Definition: go4init.py:11
TGo4HDF5DataHandle * fxHandle