00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "TGo4MbsFile.h"
00015
00016 #include <string.h>
00017
00018 #include "Riostream.h"
00019 #include "Riosfwd.h"
00020 #include "TSystem.h"
00021 #include "TROOT.h"
00022 #include "TList.h"
00023 #include "TObjString.h"
00024
00025 #include "TGo4Log.h"
00026
00027 #include "TGo4MbsFileParameter.h"
00028 #include "TGo4EventErrorException.h"
00029 #include "TGo4EventTimeoutException.h"
00030 #include "TGo4EventEndException.h"
00031 #include "TGo4FileSource.h"
00032 #include "TGo4AnalysisImp.h"
00033
00034 const char* TGo4MbsFile::fgcNOTAGFILE="GO4-NOLMDTAG";
00035 const char* TGo4MbsFile::fgcLMDSUF=".lmd";
00036 const char* TGo4MbsFile::fgcFILELISTSUF=".lml";
00037
00038 TGo4MbsFile::TGo4MbsFile() :
00039 TGo4MbsSource(),
00040 fxTagFile(),
00041 fxCurrentFile(),
00042 fxCurrentTag(),
00043 fxMultiFile(0),
00044 fbFileOpen(kFALSE),
00045 fbShowInfo(kTRUE)
00046 {
00047 GO4TRACE((15,"TGo4MbsFile::TGo4MbsFile()",__LINE__, __FILE__));
00048 }
00049
00050 TGo4MbsFile::TGo4MbsFile(const char* name) :
00051 TGo4MbsSource(name, GETEVT__FILE),
00052 fxTagFile(),
00053 fxCurrentFile(),
00054 fxCurrentTag(),
00055 fxMultiFile(0),
00056 fbFileOpen(kFALSE),
00057 fbShowInfo(kTRUE)
00058 {
00059 GO4TRACE((15,"TGo4MbsFile::TGo4MbsFile(const char*)",__LINE__, __FILE__));
00060
00061 TGo4Log::Debug(" New Event Source MbsFile %s: ", GetName());
00062 fxTagFile = fgcNOTAGFILE;
00063
00064 AddFileName(name, 0, kTRUE);
00065
00066 Open();
00067 }
00068
00069 TGo4MbsFile::TGo4MbsFile(TGo4MbsFileParameter* par) :
00070 TGo4MbsSource(par, GETEVT__FILE),
00071 fxTagFile(),
00072 fxCurrentFile(),
00073 fxCurrentTag(),
00074 fxMultiFile(0),
00075 fbFileOpen(kFALSE),
00076 fbShowInfo(kTRUE)
00077 {
00078 GO4TRACE((15,"TGo4MbsFile::TGo4MbsFile(TGo4MbsFileParameter*)",__LINE__, __FILE__));
00079
00080 if(par!=0) {
00081 fxTagFile = par->GetTagName();
00082
00083 AddFileName(GetName(), fxTagFile.Data(), par->NumMoreFiles()==0);
00084 for (Int_t n=0;n<par->NumMoreFiles();n++)
00085 AddFileName(par->GetMoreName(n), 0, kFALSE);
00086 }
00087
00088 TGo4Log::Debug("New Event Source MbsFile %s", GetName());
00089
00090 Open();
00091 }
00092
00093 TGo4MbsFile::~TGo4MbsFile()
00094 {
00095 GO4TRACE((15,"TGo4MbsFile::~TGo4MbsFile()",__LINE__, __FILE__));
00096 Close();
00097
00098 if(fxMultiFile) { delete fxMultiFile; fxMultiFile=0; }
00099 }
00100
00101 void TGo4MbsFile::AddFileName(const char* name, const char* tagname, bool isonly)
00102 {
00103 if ((name==0) || (*name==0)) return;
00104
00105 TString fname(name);
00106
00107 bool read_multi = false;
00108
00109
00110 if(fname[0]=='@') {
00111
00112 fname.Remove(0, 1);
00113 read_multi = true;
00114 } else
00115 if(fname.EndsWith(fgcFILELISTSUF)) {
00116 read_multi = true;
00117 } else
00118 if(fname.Contains("*") || fname.Contains("?")) {
00119
00120 TList* lst = TGo4FileSource::ProducesFilesList(fname.Data());
00121
00122 if (lst==0) {
00123 SetCreateStatus(GETEVT__NOFILE);
00124 SetErrMess(Form("No lmd file with mask %s", GetName()));
00125 throw TGo4EventErrorException(this);
00126 }
00127
00128 if (fxMultiFile==0) {
00129 fxMultiFile=lst;
00130 } else {
00131 lst->SetOwner(kFALSE);
00132 fxMultiFile->AddAll(lst);
00133 delete lst;
00134 }
00135
00136 } else
00137 if (!isonly) {
00138
00139
00140 if (fxMultiFile==0) { fxMultiFile = new TList; fxMultiFile->SetOwner(kTRUE); }
00141
00142 if ((tagname!=0) && (strcmp(tagname,fgcNOTAGFILE)!=0)) {
00143 fname += " ";
00144 fname += tagname;
00145 }
00146
00147 fxMultiFile->Add(new TObjString(fname));
00148 }
00149
00151
00152 if(read_multi) {
00153 std::ifstream ff(fname.Data());
00154 if(!ff) {
00155 SetCreateStatus(GETEVT__NOFILE);
00156 SetErrMess(Form("Error opening multiple file:%s", fname.Data()));
00157 throw TGo4EventErrorException(this);
00158 }
00159
00160 if (fxMultiFile==0) {
00161 fxMultiFile = new TList;
00162 fxMultiFile->SetOwner(kTRUE);
00163 }
00164
00165 char nextline[TGo4EventSource::fguTXTLEN];
00166
00167 while (!ff.eof()) {
00168 ff.getline(nextline, TGo4EventSource::fguTXTLEN, '\n');
00169
00170 fxMultiFile->Add(new TObjString(nextline));
00171 }
00172 }
00173
00174 if (isonly) SetName(fname);
00175 else SetName("LmdFilesSelection");
00176 }
00177
00178 Int_t TGo4MbsFile::NextEvent()
00179 {
00180 if (fbShowInfo) {
00181 fbShowInfo = kFALSE;
00182 TGo4Log::Info("Start file: %s tagfile:%s first:%lu last:%lu delta:%lu",GetCurrentFileName(),GetCurrentTagName(), fuStartEvent,fuStopEvent, fuEventInterval);
00183 }
00184
00185 GO4TRACE((12,"TGo4MbsFile::NextEvent()",__LINE__, __FILE__));
00186 try{
00187 Int_t skip = 0;
00188
00189 if(fuStopEvent!=0 && fuEventCounter>=fuStopEvent) {
00190 SetEventStatus(GETEVT__NOMORE);
00191 } else {
00192 if(fbFirstEvent) {
00193 if(fuStartEvent>0) {
00194
00195 skip = (Int_t) fuStartEvent-1;
00196 if(skip) fuEventCounter++;
00197 } else {
00198 skip = 0;
00199 }
00200 fbFirstEvent = kFALSE;
00201 } else {
00202 skip = (Int_t) fuEventInterval;
00203 }
00204 void* evptr = &fxEvent;
00205 Int_t status = f_evt_get_tagnext(fxInputChannel, skip, (Int_t **) evptr);
00206 if(skip)
00207 fuEventCounter+=skip;
00208 else
00209 fuEventCounter++;
00210 SetEventStatus(status);
00211 }
00212 if(GetEventStatus()!=0) {
00213 char buffer[TGo4EventSource::fguTXTLEN];
00214 f_evt_error(GetEventStatus(),buffer,1);
00215 SetErrMess(Form("%s file:%s", buffer, GetCurrentFileName()));
00216 }
00217
00218 if(GetEventStatus() == GETEVT__NOMORE) throw TGo4EventEndException(this);
00219 else if(GetEventStatus()!=0) throw TGo4EventErrorException(this);
00220
00221 return GetEventStatus();
00222
00223 }
00224
00225 catch(TGo4EventEndException& ex) {
00226 if(fxMultiFile) {
00227 ex.Handle();
00228
00229
00230 TGo4Log::Info("End file: %s",GetCurrentFileName());
00231
00232 while(NextFile()<0);
00233
00234
00235 SetErrMess("");
00236 NewFileAction();
00237 throw TGo4EventErrorException(this,0);
00238
00239
00240
00241
00242
00243
00244 } else {
00245 throw;
00246 }
00247 }
00248 }
00249
00250
00251 Int_t TGo4MbsFile::Close()
00252 {
00253 GO4TRACE((12,"TGo4MbsFile::Close()",__LINE__, __FILE__));
00254 if(!fbIsOpen) return -1;
00255
00256 Int_t rev = GetCreateStatus();
00257
00258 if(CloseFile() == GETEVT__SUCCESS) fbIsOpen = kFALSE;
00259
00260 if(fxMultiFile) { delete fxMultiFile; fxMultiFile=0; }
00261
00262 return rev;
00263 }
00264
00265 Int_t TGo4MbsFile::Open()
00266 {
00267 GO4TRACE((12,"TGo4MbsFile::Open()",__LINE__, __FILE__));
00268
00269 if(fbIsOpen) return -1;
00270
00272
00273 if(fxMultiFile!=0) {
00274
00275 while(NextFile()<0);
00276
00277 fbIsOpen = kTRUE;
00278 NewFileAction(kFALSE);
00279 } else {
00280
00281 if(NextFile()<0) {
00282
00283
00284
00285 fbIsOpen = kFALSE;
00286 } else {
00287 fbIsOpen = kTRUE;
00288 TGo4Log::Info("TGo4MbsFile: Open file %s", GetCurrentFileName());
00289 }
00290 }
00291
00292 return 0;
00293 }
00294
00295
00296 Int_t TGo4MbsFile::NextFile()
00297 {
00298 CloseFile();
00299 fuEventCounter=0;
00300
00301 if(fxMultiFile!=0) {
00302 TString nextline;
00303 char nextfile[TGo4EventSource::fguTXTLEN];
00304 char nexttag[TGo4EventSource::fguTXTLEN];
00305 const char* command=0;
00306 const char* rem1=0;
00307 const char* rem2=0;
00308 Int_t convs=0;
00309
00310 do {
00311
00312
00313 if((fxMultiFile==0) || (fxMultiFile->GetSize()==0)) {
00314
00315 SetCreateStatus(GETEVT__NOFILE);
00316 SetErrMess("End of files list");
00317
00318 throw TGo4EventEndException(this);
00319 }
00320
00321 TObject* obj = fxMultiFile->First();
00322 nextline = obj->GetName();
00323 fxMultiFile->Remove(fxMultiFile->FirstLink());
00324 delete obj;
00325
00326
00327 fbShowInfo = kTRUE;
00328
00329 rem1 = strstr(nextline.Data(), "!");
00330 rem2 = strstr(nextline.Data(), "#");
00331 command = strstr(nextline.Data(), "@");
00332 if(command!=0 && !(rem1!=0 && rem1<command) && !(rem2!=0 && rem2<command)) {
00333
00334
00335 command++;
00336 TGo4Analysis::Instance()->Message(1,"TGo4MbsFile list:%s-- executing command: %s ", GetName(), command);
00337
00338 gROOT->ProcessLineSync(command);
00339 }
00340 } while((nextline.Length()==0) || rem1!=0 || rem2!=0 || command!=0);
00341 convs = sscanf(nextline.Data(),"%s %s %lu %lu %lu",nextfile,nexttag,
00342 &fuStartEvent, &fuStopEvent, &fuEventInterval);
00343 if(convs<2) {
00344
00345 fuStartEvent=0;
00346 fuStopEvent=0;
00347 fuEventInterval=0;
00348 strncpy(nexttag, TGo4MbsFile::fgcNOTAGFILE, TGo4EventSource::fguTXTLEN);
00349 }
00350
00351
00352
00353 fxCurrentFile = nextfile;
00354 if(!strcmp(nexttag,"0") || !strcmp(nexttag,""))
00355 fxCurrentTag = TGo4MbsFile::fgcNOTAGFILE;
00356 else
00357 fxCurrentTag = nexttag;
00358 } else {
00359
00360 fxCurrentTag = GetTagName();
00361 fxCurrentFile = GetName();
00362 }
00363
00364 try {
00365 OpenFile();
00366 return 0;
00367 }
00368
00369 catch(TGo4EventErrorException& ex)
00370 {
00371 if(fxMultiFile) {
00372
00373 ex.Handle();
00374 CloseFile();
00375 return -1;
00376 } else {
00377 throw;
00378 }
00379
00380 }
00381 }
00382
00383 Int_t TGo4MbsFile::OpenFile()
00384 {
00385 if(fbFileOpen) return -1;
00386
00387 const char* tagfile = GetCurrentTagName();
00388 if(!strcmp(tagfile,TGo4MbsFile::fgcNOTAGFILE)) {
00389 tagfile=0;
00390 fxCurrentTag="none";
00391 }
00392 void* headptr=&fxInfoHeader;
00393 Int_t status = f_evt_get_tagopen(fxInputChannel,
00394 const_cast<char*>(tagfile),
00395 const_cast<char*>(GetCurrentFileName()),
00396 (Char_t**) headptr, 0);
00397 SetCreateStatus(status);
00398 if(GetCreateStatus() !=GETEVT__SUCCESS) {
00399 char buffer[TGo4EventSource::fguTXTLEN];
00400 f_evt_error(GetCreateStatus(),buffer,1);
00401 SetErrMess(Form("%s file:%s", buffer, GetCurrentFileName()));
00402 fbFileOpen = kFALSE;
00403 throw TGo4EventErrorException(this);
00404 } else {
00405 fbFileOpen = kTRUE;
00406 fbFirstEvent = kTRUE;
00407 TGo4Log::Debug(" Mbs File -- opened %s ", GetName());
00408 }
00409 return status;
00410 }
00411
00412
00413
00414 Int_t TGo4MbsFile::CloseFile()
00415 {
00416 if(!fbFileOpen) return -1;
00417 Int_t rev = f_evt_get_tagclose(fxInputChannel);
00418 if(rev == GETEVT__SUCCESS) fbFileOpen = kFALSE;
00419 return rev;
00420 }
00421
00422 Int_t TGo4MbsFile::NewFileAction(Bool_t dosave)
00423 {
00424 TGo4Analysis* ana = TGo4Analysis::Instance();
00425 ana->SetNewInputFile(kTRUE);
00426 ana->SetInputFileName(GetCurrentFileName());
00427 if(ana->IsAutoSaveFileChange()) {
00428 TString fname = GetCurrentFileName();
00429 fname.ReplaceAll(".lmd",4,"_ASF",4);
00430 if(dosave) ana->AutoSave();
00431 ana->ClearObjects("Histograms");
00432 TString asfname = fname+".root";
00433 std::cout << "Setting autosavefile to name " << asfname << std::endl;
00434 ana->SetAutoSaveFile(asfname.Data());
00435 if(dosave) ana->AutoSave();
00436 }
00437 return 0;
00438 }
00439
00440 const char* TGo4MbsFile::GetActiveName()
00441 {
00442 return GetCurrentFileName();
00443 }