GSI Object Oriented Online Offline (Go4)  GO4-6.3.0
TGo4FileSource.cxx
Go to the documentation of this file.
1 // $Id$
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 #include "TGo4FileSource.h"
15 
16 #include "TKey.h"
17 #include "TFile.h"
18 #include "TTree.h"
19 #include "TList.h"
20 #include "TSystem.h"
21 #include "TObjString.h"
22 #include "TRegexp.h"
23 
24 #include "TGo4Log.h"
26 #include "TGo4EventElement.h"
27 // get static name constants from here
28 #include "TGo4FileStore.h"
29 
30 TGo4FileSource::TGo4FileSource(const char *name) :
31  TGo4EventSource(name),
32  fxFile(nullptr),
33  fxTree(nullptr),
34  fiMaxEvents(0),
35  fiCurrentEvent(0),
36  fiGlobalEvent(0),
37  fbActivated(kFALSE),
38  fxTopEvent(nullptr),
39  fxFilesNames(nullptr)
40 {
41  fxFilesNames = ProducesFilesList(GetName());
42 
43  if (!OpenNextFile())
44  ThrowError(66,0, "!!! ERROR: Cannot open source %s!!!", GetName());
45 }
46 
48  TGo4EventSource(par->GetName()),
49  fxFile(nullptr),
50  fxTree(nullptr),
51  fiMaxEvents(0),
52  fiCurrentEvent(0),
53  fiGlobalEvent(0),
54  fbActivated(kFALSE),
55  fxTopEvent(nullptr),
56  fxFilesNames(nullptr)
57 {
58  fxFilesNames = ProducesFilesList(GetName());
59 
60  if (!OpenNextFile())
61  ThrowError(66,0, "!!! ERROR: Cannot open source %s!!!", GetName());
62 }
63 
64 
66  TGo4EventSource("Go4FileSource"),
67  fxFile(nullptr),
68  fxTree(nullptr),
69  fiMaxEvents(0),
70  fiCurrentEvent(0),
71  fiGlobalEvent(0),
72  fbActivated(kFALSE),
73  fxTopEvent(nullptr),
74  fxFilesNames(nullptr)
75 {
76  // for streamer, do not open here!
77 }
78 
80 {
82 
83  if (fxFilesNames) {
84  delete fxFilesNames;
85  fxFilesNames = nullptr;
86  }
87 }
88 
89 TList *TGo4FileSource::ProducesFilesList(const char *mask)
90 {
91  if (!mask || (strlen(mask) == 0)) return nullptr;
92 
93  TString dirname, basename(mask);
94 
95  if (!basename.MaybeWildcard()) {
96 
97  // add default suffix
98  if(!strstr(basename.Data(),TGo4FileStore::fgcFILESUF))
99  basename += TGo4FileStore::fgcFILESUF;
100 
101  TList *lst = new TList();
102  lst->SetOwner(kTRUE);
103  lst->Add(new TObjString(basename));
104  return lst;
105  }
106 
107  Bool_t withdir = kFALSE;
108  Int_t slash = basename.Last('/');
109 
110 #ifdef _MSC_VER
111  if (slash < 0) slash = basename.Last('\\');
112 #endif
113 
114  if (slash >= 0) {
115  dirname = basename(0, slash);
116  basename.Remove(0, slash+1);
117  withdir = kTRUE;
118  } else {
119  dirname = gSystem->WorkingDirectory();
120  }
121 
122  void *dir = gSystem->OpenDirectory(gSystem->ExpandPathName(dirname.Data()));
123 
124  if (!dir) return nullptr;
125 
126  TList *lst = nullptr;
127 
128  TRegexp re(basename, kTRUE);
129  const char *file = nullptr;
130  while ((file = gSystem->GetDirEntry(dir)) != nullptr) {
131  if (!strcmp(file,".") || !strcmp(file,"..")) continue;
132  TString s = file;
133  if ((basename != s) && (s.Index(re) == kNPOS)) continue;
134  if (!lst) {
135  lst = new TList;
136  lst->SetOwner(kTRUE);
137  }
138  if (withdir)
139  lst->Add(new TObjString(dirname + "/" + file));
140  else
141  lst->Add(new TObjString(file));
142  }
143  gSystem->FreeDirectory(dir);
144 
145  if (lst) lst->Sort();
146 
147  return lst;
148 }
149 
151 {
153 
154  if (!fxFilesNames || (fxFilesNames->GetSize() == 0)) return kFALSE;
155 
156  TObject *obj = fxFilesNames->First();
157  fxCurrentFileName = obj->GetName();
158  fxFilesNames->Remove(fxFilesNames->FirstLink());
159  delete obj;
160 
161  fxFile = TFile::Open(fxCurrentFileName.Data(), "READ"); // in such way rfio etc is also supported!
162  if(!fxFile) ThrowError(66,0, "!!! ERROR: FILE %s not found !!!", fxCurrentFileName.Data());
163  if(!fxFile->IsOpen()) ThrowError(66,0, "!!! ERROR: FILE %s cannot open !!!", fxCurrentFileName.Data());
164 
165  TIter iter(fxFile->GetListOfKeys());
166  while (auto kee = dynamic_cast<TKey *>(iter())) {
167  fxTree = dynamic_cast<TTree *>(kee->ReadObj());
168  if (fxTree) break; // we take first Tree in file, disregarding its name...
169  }
170  if (!fxTree) {
171  ThrowError(77,0, "!!! ERROR: Tree not found !!!");
172  } else {
173  SetCreateStatus(0);
174  fiMaxEvents = fxTree->GetEntries();
175  }
176 
177  return kTRUE;
178 }
179 
180 
182 {
183  if (fxFile) {
184  delete fxFile;
185  TGo4Log::Info("TGo4FileSource: Close file %s", fxCurrentFileName.Data());
186  }
187 
188  fxFile = nullptr;
189  fxTree = nullptr;
190  fiMaxEvents = 0;
191  fiCurrentEvent = 0;
192  fxTopEvent = nullptr;
193  fbActivated = kFALSE;
194  fxCurrentFileName = "";
195 
196  return kTRUE;
197 }
198 
200 {
201  if(!dest) ThrowError(0,22,"!!! ERROR BuildEvent: no destination event!!!");
202  if (!fxTree) ThrowError(0,33,"!!! ERROR BuildEvent: no Tree !!!");
203 
204  if(fiCurrentEvent >= fiMaxEvents) {
205  if (!OpenNextFile())
206  ThrowEOF(0,44,"End at event %ld !!!", fiGlobalEvent);
207  }
208 
209  if(!fbActivated) {
210  // Event dest should be full event as filled into the tree
211  // the name of the event element may indicate the subpart
212  //(tree branchname) that should be read partially only
213  fxTopEvent = dest;
215  fbActivated = kTRUE;
216  TGo4Log::Info("TGo4FileSource: Open file %s", fxCurrentFileName.Data());
217  } // if(!fbActivated)
218  // end init section ////////////
219 
220  fiGlobalEvent++;
221 
222  return fxTree->GetEntry(fiCurrentEvent++) > 0;
223 }
void ThrowEOF(Int_t creastat, Int_t errstat, const char *message,...)
Bool_t BuildEvent(TGo4EventElement *dest) override
static void Info(const char *text,...) GO4_PRINTF_ARGS
Definition: TGo4Log.cxx:294
virtual ~TGo4FileSource()
void SetCreateStatus(Int_t status)
long int fiGlobalEvent
Long64_t fiCurrentEvent
TGo4EventElement * fxTopEvent
virtual void synchronizeWithTree(TTree *tree, TGo4EventElement **var_ptr=nullptr)
void ThrowError(Int_t creastat, Int_t errstat, const char *message,...)
TString fxCurrentFileName
Bool_t CloseCurrentFile()
Long64_t fiMaxEvents
static TList * ProducesFilesList(const char *mask)
static const char * fgcFILESUF
Definition: TGo4FileStore.h:84
TList * fxFilesNames