00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "TGo4MbsFile.h"
00017
00018 #include <iosfwd>
00019 #include <iostream.h>
00020
00021 #include "TSystem.h"
00022 #include "TROOT.h"
00023
00024 #include "Go4Log/TGo4Log.h"
00025
00026 #include "Go4Event/TGo4EventErrorException.h"
00027 #include "Go4Event/TGo4EventTimeoutException.h"
00028 #include "Go4Event/TGo4EventEndException.h"
00029 #include "Go4Analysis/TGo4AnalysisImp.h"
00030
00031 const Text_t TGo4MbsFile::fgcNOTAGFILE[]="GO4-NOLMDTAG";
00032 const Text_t TGo4MbsFile::fgcWILDFILE[]=".go4inputs";
00033 const Text_t TGo4MbsFile::fgcLMDSUF[]=".lmd";
00034 const Text_t TGo4MbsFile::fgcFILELISTSUF[]=".lml";
00035
00036 TGo4MbsFile::TGo4MbsFile(const char* name)
00037 : TGo4MbsSource(name , GETEVT__FILE), fbFirstEvent(kTRUE),
00038 fbMultipleMode(kFALSE), fbWildcardMode(kFALSE), fxMultiFile(0), fbFileOpen(kFALSE)
00039 {
00040 TRACE((15,"TGo4MbsFile::TGo4MbsFile(Text_t*)",__LINE__, __FILE__));
00041
00042 TGo4Log::Debug(" New Event Source MbsFile %s: ",name);
00043 fxTagFile=fgcNOTAGFILE;
00044 Open();
00045 }
00046
00047
00048 TGo4MbsFile::TGo4MbsFile(TGo4MbsFileParameter* par)
00049 : TGo4MbsSource(par->GetName() , GETEVT__FILE),
00050 fbFirstEvent(kTRUE), fbMultipleMode(kFALSE),
00051 fbWildcardMode(kFALSE), fxMultiFile(0), fbFileOpen(kFALSE)
00052 {
00053 TRACE((15,"TGo4MbsFile::TGo4MbsFile(TGo4MbsFileParameter**)",__LINE__, __FILE__));
00054
00055 TGo4Log::Debug(" New Event Source MbsFile %s: ",GetName());
00056 if(par)
00057 {
00058 SetEventInterval(par->GetEventInterval());
00059 SetStartEvent(par->GetStartEvent());
00060 SetStopEvent(par->GetStopEvent());
00061 fxTagFile=par->GetTagName();
00062 }
00063 Open();
00064 }
00065
00066
00067
00068 TGo4MbsFile::TGo4MbsFile()
00069 {
00070 TRACE((15,"TGo4MbsFile::TGo4MbsFile()",__LINE__, __FILE__));
00071
00072 }
00073
00074
00075
00076 TGo4MbsFile::~TGo4MbsFile()
00077 {
00078 TRACE((15,"TGo4MbsFile::~TGo4MbsFile()",__LINE__, __FILE__));
00079 Close();
00080 }
00081
00082 Int_t TGo4MbsFile::NextEvent()
00083 {
00084 TRACE((12,"TGo4MbsFile::NextEvent()",__LINE__, __FILE__));
00085 try{
00086 Int_t skip=0;
00087
00088 if(fuStopEvent!=0 && fuEventCounter>=fuStopEvent)
00089 {
00090 SetEventStatus(GETEVT__NOMORE);
00091 }
00092 else
00093 {
00094 if(fbFirstEvent)
00095 {
00096 skip=(Int_t) fuStartEvent;
00097 fbFirstEvent=kFALSE;
00098 }
00099 else
00100 {
00101 skip= (Int_t) fuEventInterval;
00102 }
00103 Int_t status=f_evt_get_tagnext(
00104 &fxInputChannel,
00105 skip,
00106 (Int_t **) &fxEvent);
00107 if(skip)
00108 fuEventCounter+=skip;
00109 else
00110 fuEventCounter++;
00111 SetEventStatus(status);
00112 }
00113 if(GetEventStatus()!=0)
00114 {
00115 Text_t buffer[TGo4EventSource::fguTXTLEN];
00116 f_evt_error(GetEventStatus(),buffer,1);
00117 SetErrMess(buffer);
00118 }
00119
00120 if(GetEventStatus() ==GETEVT__NOMORE)
00121 throw TGo4EventEndException(this);
00122 else if(GetEventStatus()!=0)
00123 throw TGo4EventErrorException(this);
00124 else
00125 ;
00126 return GetEventStatus();
00127
00128 }
00129
00130 catch(TGo4EventEndException& ex)
00131 {
00132 if(fbMultipleMode)
00133 {
00134 ex.Handle();
00135
00136
00137 while(NextFile()<0){;}
00138
00139
00140 Text_t txt[TGo4EventSource::fguTXTLEN];
00141 snprintf(txt,
00142 TGo4EventSource::fguTXTLEN,
00143 "Changed input to: %s tagfile:%s first:%u last:%u delta:%u",
00144 GetCurrentFileName(),GetCurrentTagName(),
00145 fuStartEvent,fuStopEvent, fuEventInterval);
00146 SetErrMess(txt);
00147 NewFileAction();
00148 throw TGo4EventErrorException(this,0);
00149
00150
00151
00152
00153
00154
00155 }
00156 else
00157 {
00158 throw;
00159 }
00160 }
00161
00162
00163
00164
00165
00166 }
00167
00168 Int_t TGo4MbsFile::Close()
00169 {
00170 TRACE((12,"TGo4MbsFile::Close()",__LINE__, __FILE__));
00171 if(!fbIsOpen)
00172 return -1;
00173
00174 Int_t rev=0;
00175 rev=GetCreateStatus();
00176
00177 if(CloseFile() == GETEVT__SUCCESS)
00178 {
00179 fbIsOpen=kFALSE;
00180 }
00181 else
00182 {
00183
00184 }
00185 if(fxMultiFile) delete fxMultiFile;
00186 if(fbWildcardMode)
00187 {
00188 Text_t command[TGo4EventSource::fguTXTLEN];
00189 snprintf(command,TGo4EventSource::fguTXTLEN,
00190 "rm %s",fxMultiName.Data());
00191
00192 gSystem->Exec(command);
00193 }
00194 return rev;
00195
00196
00197 }
00198 Int_t TGo4MbsFile::Open()
00199 {
00200 TRACE((12,"TGo4MbsFile::Open()",__LINE__, __FILE__));
00201
00202 if(fbIsOpen)
00203 return -1;
00204 const char* multiname=0;
00205
00206 if(*GetName()=='@')
00207 {
00208
00209
00210 fbMultipleMode=kTRUE;
00211 fbWildcardMode=kFALSE;
00212 multiname=strstr(GetName(),"@");
00213 multiname++;
00214 fxMultiName=multiname;
00215 }
00216 else if(fName.EndsWith(fgcFILELISTSUF))
00217 {
00218
00219 fbMultipleMode=kTRUE;
00220 fbWildcardMode=kFALSE;
00221 fxMultiName=GetName();
00222 }
00223 else if(strstr(GetName(),"*") || strstr(GetName(),"?"))
00224 {
00225
00226
00227 fbWildcardMode=kTRUE;
00228 fbMultipleMode=kTRUE;
00229 multiname = fgcWILDFILE;
00230
00231 Text_t wildexpr[TGo4EventSource::fguTXTLEN];
00232 if(!strstr(GetName(),fgcLMDSUF))
00233 {
00234
00235
00236 snprintf(wildexpr,TGo4EventSource::fguTXTLEN,
00237 "%s%s",GetName(),fgcLMDSUF);
00238 }
00239 else
00240 {
00241 snprintf(wildexpr,TGo4EventSource::fguTXTLEN,
00242 "%s",GetName());
00243 }
00244 const Text_t* homedir=gSystem->Getenv("HOME");
00245 Text_t buf[TGo4EventSource::fguTXTLEN];
00246 snprintf(buf,TGo4EventSource::fguTXTLEN,
00247 "%s/%s",homedir,multiname);
00248 fxMultiName=buf;
00249 Text_t command[TGo4EventSource::fguTXTLEN];
00250 snprintf(command,TGo4EventSource::fguTXTLEN,
00251 "ls -1 %s > %s",wildexpr, fxMultiName.Data());
00252
00253 gSystem->Exec(command);
00254
00255 }
00256 else
00257 {
00258
00259
00260 fbMultipleMode=kFALSE;
00261 fbWildcardMode=kFALSE;
00262 }
00263
00264
00266
00267 if(fbMultipleMode)
00268 {
00269
00270 fxMultiFile=new std::ifstream(fxMultiName.Data());
00271 if(fxMultiFile==0)
00272 {
00273 SetCreateStatus(GETEVT__NOFILE);
00274 Text_t buffer[TGo4EventSource::fguTXTLEN];
00275
00276 snprintf(buffer,TGo4EventSource::fguTXTLEN,
00277 "Eror opening multiple infile:%s",fxMultiName.Data());
00278 SetErrMess(buffer);
00279 throw TGo4EventErrorException(this);
00280 }
00281 while(NextFile()<0) {;}
00282
00283 fbIsOpen=kTRUE;
00284 Text_t txt[TGo4EventSource::fguTXTLEN];
00285 snprintf(txt,
00286 TGo4EventSource::fguTXTLEN,
00287 "Eventsource TGo4MbsFile:%s Changed input to: %s tag:%s first:%u last:%u delta:%u",
00288 GetName(),GetCurrentFileName(),GetCurrentTagName(),
00289 fuStartEvent,fuStopEvent, fuEventInterval);
00290 NewFileAction(kFALSE);
00291 TGo4Analysis::Instance()->Message(1,txt);
00292 }
00293 else
00294 {
00295
00296 if(NextFile()<0)
00297 fbIsOpen=kFALSE;
00298
00299
00300 else
00301 fbIsOpen=kTRUE;
00302 }
00303
00304
00305
00306
00307
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00346
00347 return 0;
00348 }
00349
00350
00351 Int_t TGo4MbsFile::NextFile()
00352 {
00353 CloseFile();
00354 fuEventCounter=0;
00355
00356 if(fbMultipleMode && fxMultiFile!=0)
00357 {
00358 Text_t nextline[TGo4EventSource::fguTXTLEN];
00359 Text_t nextfile[TGo4EventSource::fguTXTLEN];
00360 Text_t nexttag[TGo4EventSource::fguTXTLEN];
00361 Text_t* command=0;
00362 Text_t* rem1=0;
00363 Text_t* rem2=0;
00364 Int_t convs=0;
00365
00366 do {
00367 fxMultiFile->getline(nextline,TGo4EventSource::fguTXTLEN, '\n' );
00368
00369
00370 if(fxMultiFile->eof() || !fxMultiFile->good())
00371 {
00372
00373 SetCreateStatus(GETEVT__NOFILE);
00374 Text_t buffer[TGo4EventSource::fguTXTLEN];
00375 snprintf(buffer,TGo4EventSource::fguTXTLEN,
00376 "End of multiple input namesfile %s", fxMultiName.Data());
00377 SetErrMess(buffer);
00378
00379 throw TGo4EventEndException(this);
00380 }
00381 rem1=strstr(nextline,"!");
00382 rem2=strstr(nextline,"#");
00383 command=strstr(nextline,"@");
00384 if(command!=0 && !(rem1!=0 && rem1<command) && !(rem2!=0 && rem2<command))
00385
00386
00387 {
00388 command++;
00389 TGo4Analysis::Instance()->Message(1,"TGo4MbsFile list:%s-- executing command: %s ", GetName(), command);
00390
00391 gROOT->ProcessLine(command);
00392 }
00393 }
00394 while(strlen(nextline)==0 || rem1!=0 || rem2!=0 || command!=0);
00395 convs=sscanf(nextline,"%s %s %u %u %u",nextfile,nexttag,
00396 &fuStartEvent, &fuStopEvent, &fuEventInterval);
00397 if(convs<2)
00398 {
00399
00400 fuStartEvent=0;
00401 fuStopEvent=0;
00402 fuEventInterval=0;
00403 snprintf(nexttag,
00404 TGo4EventSource::fguTXTLEN,"%s",
00405 TGo4MbsFile::fgcNOTAGFILE);
00406 }
00407
00408
00409
00410 fxCurrentFile=nextfile;
00411 if(!strcmp(nexttag,"0") || !strcmp(nexttag,""))
00412 {
00413 fxCurrentTag=TGo4MbsFile::fgcNOTAGFILE;
00414 }
00415 else
00416 {
00417 fxCurrentTag=nexttag;
00418 }
00419 }
00420 else
00421 {
00422
00423 fxCurrentTag=GetTagName();
00424 fxCurrentFile=GetName();
00425 }
00426
00427 try
00428 {
00429 OpenFile();
00430 return 0;
00431 }
00432
00433 catch(TGo4EventErrorException& ex)
00434 {
00435 if(fbMultipleMode)
00436 {
00437
00438 ex.Handle();
00439 CloseFile();
00440 return -1;
00441 }
00442 else
00443 {
00444 throw;
00445 }
00446
00447 }
00448
00449
00450 }
00451
00452 Int_t TGo4MbsFile::OpenFile()
00453 {
00454 if(fbFileOpen)
00455 return -1;
00456
00457 const char* tagfile = GetCurrentTagName();
00458 if(!strcmp(tagfile,TGo4MbsFile::fgcNOTAGFILE))
00459 {
00460 tagfile=0;
00461 fxCurrentTag="none";
00462 }
00463 Int_t status = f_evt_get_tagopen(
00464 &fxInputChannel,
00465 const_cast<Text_t*>(tagfile) ,
00466 const_cast<Text_t*>( GetCurrentFileName() ),
00467 (Char_t**) &fxInfoHeader,
00468 0);
00469 SetCreateStatus(status);
00470 if(GetCreateStatus() !=GETEVT__SUCCESS)
00471 {
00472 Text_t buffer[TGo4EventSource::fguTXTLEN];
00473 f_evt_error(GetCreateStatus(),buffer,1);
00474 SetErrMess(buffer);
00475 fbFileOpen=kFALSE;
00476 throw TGo4EventErrorException(this);
00477 }
00478 else
00479 {
00480 fbFileOpen=kTRUE;
00481 TGo4Log::Debug(" Mbs File -- opened %s ", GetName());
00482 }
00483 return status;
00484 }
00485
00486
00487
00488 Int_t TGo4MbsFile::CloseFile()
00489 {
00490 if(!fbFileOpen)
00491 return -1;
00492 Int_t rev=f_evt_get_tagclose(&fxInputChannel);
00493 if(rev == GETEVT__SUCCESS) fbFileOpen=kFALSE;
00494 return rev;
00495 }
00496
00497 Int_t TGo4MbsFile::NewFileAction(Bool_t dosave)
00498 {
00499 TGo4Analysis* ana=TGo4Analysis::Instance();
00500 ana->SetNewInputFile(kTRUE);
00501 if(ana->IsAutoSaveFileChange())
00502 {
00503 TString fname=GetCurrentFileName();
00504 fname.ReplaceAll(".lmd",4,"_ASF",4);
00505 if(dosave) ana->AutoSave();
00506 ana->ClearObjects("Histograms");
00507 TString asfname=fname+".root";
00508 cout <<"Setting autosavefile to name "<<asfname << endl;
00509 ana->SetAutoSaveFile(asfname.Data());
00510 if(dosave) ana->AutoSave();
00511 }
00512 return 0;
00513 }
00514
00515
00516 ClassImp(TGo4MbsFile)
00517
00518
00519
00520
00521