GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4FileSource.cxx
Go to the documentation of this file.
1 // $Id: TGo4FileSource.cxx 2130 2018-08-14 10:13:11Z 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 f�r 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(0),
33  fxTree(0),
34  fiMaxEvents(0),
35  fiCurrentEvent(0),
36  fiGlobalEvent(0),
37  fbActivated(kFALSE),
38  fxTopEvent(0),
39  fxFilesNames(0)
40 {
41  fxFilesNames = ProducesFilesList(GetName());
42 
43  if (!OpenNextFile())
44  ThrowError(66,0, Form("!!! ERROR: Cannot open source %s!!!", GetName()));
45 }
46 
48  TGo4EventSource(par->GetName()),
49  fxFile(0),
50  fxTree(0),
51  fiMaxEvents(0),
52  fiCurrentEvent(0),
53  fiGlobalEvent(0),
54  fbActivated(kFALSE),
55  fxTopEvent(0),
56  fxFilesNames(0)
57 {
58  fxFilesNames = ProducesFilesList(GetName());
59 
60  if (!OpenNextFile())
61  ThrowError(66,0, Form("!!! ERROR: Cannot open source %s!!!", GetName()));
62 }
63 
64 
66  TGo4EventSource("Go4FileSource"),
67  fxFile(0),
68  fxTree(0),
69  fiMaxEvents(0),
70  fiCurrentEvent(0),
71  fiGlobalEvent(0),
72  fbActivated(kFALSE),
73  fxTopEvent(0),
74  fxFilesNames(0)
75 {
76  // for streamer, do not open here!
77 }
78 
80 {
82 
83  if (fxFilesNames) {
84  delete fxFilesNames;
85  fxFilesNames = 0;
86  }
87 }
88 
89 TList* TGo4FileSource::ProducesFilesList(const char* mask)
90 {
91  if ((mask==0) || (strlen(mask)==0)) return 0;
92 
93  TString dirname, basename(mask);
94 
95  if (!basename.MaybeWildcard()) {
96 
97  // add default suffix
98  if(strstr(basename.Data(),TGo4FileStore::fgcFILESUF)==0)
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 WIN32
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==0) return 0;
125 
126  TList* lst = 0;
127 
128  TRegexp re(basename, kTRUE);
129  const char* file = 0;
130  while ((file = gSystem->GetDirEntry(dir)) != 0) {
131  if (!strcmp(file,".") || !strcmp(file,"..")) continue;
132  TString s = file;
133  if ( (basename!=s) && s.Index(re) == kNPOS) continue;
134  if (lst==0) {
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==0) || (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==0) ThrowError(66,0,Form("!!! ERROR: FILE %s not found !!!", fxCurrentFileName.Data()));
163  if (!fxFile->IsOpen()) ThrowError(66,0,Form("!!! ERROR: FILE %s cannot open !!!", fxCurrentFileName.Data()));
164 
165  TKey* kee = 0;
166  TIter iter(fxFile->GetListOfKeys());
167  while ( (kee=dynamic_cast<TKey*>(iter())) !=0 ) {
168  fxTree = dynamic_cast<TTree*>(kee->ReadObj());
169  if (fxTree) break; // we take first Tree in file, disregarding its name...
170  }
171  if (fxTree==0) {
172  ThrowError(77,0,"!!! ERROR: Tree not found !!!");
173  } else {
174  SetCreateStatus(0);
175  fiMaxEvents = fxTree->GetEntries();
176  }
177 
178  return kTRUE;
179 }
180 
181 
183 {
184  if (fxFile) {
185  delete fxFile;
186  TGo4Log::Info("TGo4FileSource: Close file %s", fxCurrentFileName.Data());
187  }
188 
189  fxFile = 0;
190  fxTree = 0;
191  fiMaxEvents = 0;
192  fiCurrentEvent = 0;
193  fxTopEvent = 0;
194  fbActivated = kFALSE;
195  fxCurrentFileName = "";
196 
197  return kTRUE;
198 }
199 
201 {
202  if(dest==0) ThrowError(0,22,"!!! ERROR BuildEvent: no destination event!!!");
203  if (fxTree==0) ThrowError(0,33,"!!! ERROR BuildEvent: no Tree !!!");
204 
205  if(fiCurrentEvent >= fiMaxEvents) {
206  if (!OpenNextFile())
207  ThrowEOF(0,44,"End at event %ld !!!", fiGlobalEvent);
208  }
209 
210  if(!fbActivated) {
211  // Event dest should be full event as filled into the tree
212  // the name of the event element may indicate the subpart
213  //(tree branchname) that should be read partially only
214  fxTopEvent = dest;
216  fbActivated = kTRUE;
217  TGo4Log::Info("TGo4FileSource: Open file %s", fxCurrentFileName.Data());
218  } // if(!fbActivated)
219  // end init section ////////////
220 
221  fiGlobalEvent++;
222 
223  return fxTree->GetEntry(fiCurrentEvent++) > 0;
224 }
void ThrowEOF(Int_t creastat, Int_t errstat, const char *message,...)
virtual ~TGo4FileSource()
void SetCreateStatus(Int_t status)
long int fiGlobalEvent
Long64_t fiCurrentEvent
TGo4EventElement * fxTopEvent
void ThrowError(Int_t creastat, Int_t errstat, const char *message,...)
TString fxCurrentFileName
Bool_t CloseCurrentFile()
virtual Bool_t BuildEvent(TGo4EventElement *dest)
Long64_t fiMaxEvents
virtual void synchronizeWithTree(TTree *tree, TGo4EventElement **var_ptr=0)
static TList * ProducesFilesList(const char *mask)
static const char * fgcFILESUF
Definition: TGo4FileStore.h:84
TList * fxFilesNames
static void Info(const char *text,...)
Definition: TGo4Log.cxx:283