00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "TGo4FileSource.h"
00015
00016 #include "TKey.h"
00017 #include "TFile.h"
00018 #include "TTree.h"
00019 #include "TList.h"
00020 #include "TSystem.h"
00021 #include "TObjString.h"
00022 #include "TRegexp.h"
00023
00024 #include "TGo4Log.h"
00025 #include "TGo4FileSourceParameter.h"
00026 #include "TGo4CompositeEvent.h"
00027
00028 #include "TGo4FileStore.h"
00029
00030
00031
00032 TGo4FileSource::TGo4FileSource(const char* name) :
00033 TGo4EventSource(name),
00034 fxFile(0),
00035 fxTree(0),
00036 fiMaxEvents(0),
00037 fiCurrentEvent(0),
00038 fiGlobalEvent(0),
00039 fbActivated(kFALSE),
00040 fxTopEvent(0),
00041 fxFilesNames(0)
00042 {
00043 fxFilesNames = ProducesFilesList(GetName());
00044
00045 if (!OpenNextFile())
00046 ThrowError(66,0, Form("!!! ERROR: Cannot open source %s!!!", GetName()));
00047 }
00048
00049 TGo4FileSource::TGo4FileSource(TGo4FileSourceParameter* par) :
00050 TGo4EventSource(par->GetName()),
00051 fxFile(0),
00052 fxTree(0),
00053 fiMaxEvents(0),
00054 fiCurrentEvent(0),
00055 fiGlobalEvent(0),
00056 fbActivated(kFALSE),
00057 fxTopEvent(0),
00058 fxFilesNames(0)
00059 {
00060 fxFilesNames = ProducesFilesList(GetName());
00061
00062 if (!OpenNextFile())
00063 ThrowError(66,0, Form("!!! ERROR: Cannot open source %s!!!", GetName()));
00064 }
00065
00066
00067 TGo4FileSource::TGo4FileSource() :
00068 TGo4EventSource("Go4FileSource"),
00069 fxFile(0),
00070 fxTree(0),
00071 fiMaxEvents(0),
00072 fiCurrentEvent(0),
00073 fiGlobalEvent(0),
00074 fbActivated(kFALSE),
00075 fxTopEvent(0),
00076 fxFilesNames(0)
00077 {
00078
00079 }
00080
00081 TGo4FileSource::~TGo4FileSource()
00082 {
00083 CloseCurrentFile();
00084
00085 if (fxFilesNames) {
00086 delete fxFilesNames;
00087 fxFilesNames = 0;
00088 }
00089 }
00090
00091 TList* TGo4FileSource::ProducesFilesList(const char* mask)
00092 {
00093 if ((mask==0) || (strlen(mask)==0)) return 0;
00094
00095 TString dirname, basename(mask);
00096
00097 if (!basename.MaybeWildcard()) {
00098
00099
00100 if(strstr(basename.Data(),TGo4FileStore::fgcFILESUF)==0)
00101 basename += TGo4FileStore::fgcFILESUF;
00102
00103 TList* lst = new TList();
00104 lst->SetOwner(kTRUE);
00105 lst->Add(new TObjString(basename));
00106 return lst;
00107 }
00108
00109 Bool_t withdir = kFALSE;
00110 Int_t slash = basename.Last('/');
00111
00112 #ifdef WIN32
00113 if (slash<0) slash = basename.Last('\\');
00114 #endif
00115
00116 if (slash>=0) {
00117 dirname = basename(0, slash);
00118 basename.Remove(0, slash+1);
00119 withdir = kTRUE;
00120 } else {
00121 dirname = gSystem->WorkingDirectory();
00122 }
00123
00124 void *dir = gSystem->OpenDirectory(gSystem->ExpandPathName(dirname.Data()));
00125
00126 if (dir==0) return 0;
00127
00128 TList* lst = 0;
00129
00130 TRegexp re(basename, kTRUE);
00131 const char* file = 0;
00132 while ((file = gSystem->GetDirEntry(dir)) != 0) {
00133 if (!strcmp(file,".") || !strcmp(file,"..")) continue;
00134 TString s = file;
00135 if ( (basename!=s) && s.Index(re) == kNPOS) continue;
00136 if (lst==0) {
00137 lst = new TList;
00138 lst->SetOwner(kTRUE);
00139 }
00140 if (withdir)
00141 lst->Add(new TObjString(dirname + "/" + file));
00142 else
00143 lst->Add(new TObjString(file));
00144 }
00145 gSystem->FreeDirectory(dir);
00146
00147 if (lst) lst->Sort();
00148
00149 return lst;
00150 }
00151
00152 Bool_t TGo4FileSource::OpenNextFile()
00153 {
00154 CloseCurrentFile();
00155
00156 if ((fxFilesNames==0) || (fxFilesNames->GetSize()==0)) return kFALSE;
00157
00158 TObject* obj = fxFilesNames->First();
00159 fxCurrentFileName = obj->GetName();
00160 fxFilesNames->Remove(fxFilesNames->FirstLink());
00161 delete obj;
00162
00163 fxFile = TFile::Open(fxCurrentFileName.Data(), "READ");
00164 if(fxFile==0) ThrowError(66,0,Form("!!! ERROR: FILE %s not found !!!", fxCurrentFileName.Data()));
00165 if (!fxFile->IsOpen()) ThrowError(66,0,Form("!!! ERROR: FILE %s cannot open !!!", fxCurrentFileName.Data()));
00166
00167 TKey* kee = 0;
00168 TIter iter(fxFile->GetListOfKeys());
00169 while ( (kee=dynamic_cast<TKey*>(iter())) !=0 ) {
00170 fxTree = dynamic_cast<TTree*>(kee->ReadObj());
00171 if (fxTree) break;
00172 }
00173 if (fxTree==0) {
00174 ThrowError(77,0,"!!! ERROR: Tree not found !!!");
00175 } else {
00176 SetCreateStatus(0);
00177 fiMaxEvents = fxTree->GetEntries();
00178 }
00179
00180 return kTRUE;
00181 }
00182
00183
00184 Bool_t TGo4FileSource::CloseCurrentFile()
00185 {
00186 if (fxFile) {
00187 delete fxFile;
00188 TGo4Log::Info("TGo4FileSource: Close file %s", fxCurrentFileName.Data());
00189 }
00190
00191 fxFile = 0;
00192 fxTree = 0;
00193 fiMaxEvents = 0;
00194 fiCurrentEvent = 0;
00195 fxTopEvent = 0;
00196 fbActivated = kFALSE;
00197 fxCurrentFileName = "";
00198
00199 return kTRUE;
00200 }
00201
00202 Bool_t TGo4FileSource::BuildEvent(TGo4EventElement* dest)
00203 {
00204 if(dest==0) ThrowError(0,22,"!!! ERROR BuildEvent: no destination event!!!");
00205 if (fxTree==0) ThrowError(0,33,"!!! ERROR BuildEvent: no Tree !!!");
00206
00207 if(fiCurrentEvent >= fiMaxEvents) {
00208 if (!OpenNextFile())
00209 ThrowEOF(0,44,"End at event %ld !!!", fiGlobalEvent);
00210 }
00211
00212 if(!fbActivated) {
00213
00214
00215
00216 fxTopEvent = dest;
00217 dest->synchronizeWithTree(fxTree, &fxTopEvent);
00218 fbActivated = kTRUE;
00219 TGo4Log::Info("TGo4FileSource: Open file %s", fxCurrentFileName.Data());
00220 }
00221
00222
00223 fiGlobalEvent++;
00224
00225 return fxTree->GetEntry(fiCurrentEvent++) > 0;
00226 }