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