Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/Go4EventServer/TGo4MbsFile.cxx

Go to the documentation of this file.
00001 //---------------------------------------------------------------
00002 //        Go4 Release Package v2.10-5 (build 21005) 
00003 //                      03-Nov-2005
00004 //---------------------------------------------------------------
00005 //       The GSI Online Offline Object Oriented (Go4) Project
00006 //       Experiment Data Processing at DVEE department, GSI
00007 //---------------------------------------------------------------
00008 //
00009 //Copyright (C) 2000- Gesellschaft f. Schwerionenforschung, GSI
00010 //                    Planckstr. 1, 64291 Darmstadt, Germany
00011 //Contact:            http://go4.gsi.de
00012 //----------------------------------------------------------------
00013 //This software can be used under the license agreements as stated
00014 //in Go4License.txt file which is part of the distribution.
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    // test if we had reached the last event:
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); // provide text message for later output
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 } // try
00129 
00130 catch(TGo4EventEndException& ex)
00131 {
00132    if(fbMultipleMode)
00133       {
00134          ex.Handle(); // display message
00135         // catch here the end of file case only
00136         // try to open next file in list:
00137         while(NextFile()<0){;}
00138                    //skip filenames with open error until a file
00139                    // in the list opens properly (retval==0)
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                      // priority 0 means do not stop analysis
00150                 // we (mis-)use an error exception with no stop to
00151                // skip the processing of the previous event  in the
00152                // subsequent analysis for a second time
00153             // note that NextFile() throws an exception itself
00154             // if list of files is at end
00155       }
00156    else
00157       {
00158          throw;  // normal end of input for one file
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 //cout << "Close of TGo4MbsFile"<< endl;
00174 Int_t rev=0;
00175 rev=GetCreateStatus();
00176 // close connection/file
00177 if(CloseFile() == GETEVT__SUCCESS)
00178    {
00179       fbIsOpen=kFALSE;
00180    }
00181 else
00182    {
00183       // do nothing (for streamer)
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       //cout <<"Executing command "<<command << endl;
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 // evaluate wildcard input:
00206 if(*GetName()=='@') // old style: filelist name starts with @
00207    {
00208       //cout <<"Multiple mode with @" << endl;
00209       // name indicates steering file
00210       fbMultipleMode=kTRUE;
00211       fbWildcardMode=kFALSE;
00212       multiname=strstr(GetName(),"@");
00213       multiname++; // skip at character
00214       fxMultiName=multiname;
00215    }
00216 else if(fName.EndsWith(fgcFILELISTSUF))// new style: list mode list
00217    {
00218       //cout <<"Multiple mode for .lml " << endl;
00219       fbMultipleMode=kTRUE;
00220       fbWildcardMode=kFALSE;
00221       fxMultiName=GetName();
00222    }   
00223 else if(strstr(GetName(),"*") || strstr(GetName(),"?")) 
00224    {
00225       //cout <<"Wildcard mode" << endl;
00226       // name indicates wildcard expression
00227       fbWildcardMode=kTRUE;
00228       fbMultipleMode=kTRUE;
00229       multiname = fgcWILDFILE;
00230       // evaluate expression here and create input file:
00231       Text_t wildexpr[TGo4EventSource::fguTXTLEN];
00232       if(!strstr(GetName(),fgcLMDSUF))
00233          {
00234           //cout <<"adding suffix "<<fgcLMDSUF << endl;
00235           // wildcard name does not contain lmd suffix, we put it in
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       //cout <<"Executing command "<<command << endl;
00253       gSystem->Exec(command); // write input file from wildcard expression
00254 
00255    }
00256 else
00257    {
00258       // name is name of single input lmd file
00259       //cout <<"Simple mode" << endl;
00260       fbMultipleMode=kFALSE;
00261       fbWildcardMode=kFALSE;
00262    }
00263 
00264 
00266 // now treat different input modes:
00267 if(fbMultipleMode)
00268    {
00269       //cout <<"Opening multiple file "<<fxMultiName.Data() << endl;
00270       fxMultiFile=new std::ifstream(fxMultiName.Data());
00271       if(fxMultiFile==0)
00272          {
00273             SetCreateStatus(GETEVT__NOFILE);
00274             Text_t buffer[TGo4EventSource::fguTXTLEN];
00275             //f_evt_error(GetCreateStatus(),buffer,1); // provide text message for later output
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) {;} // skip invalid filenames
00282        // note that TGo4EventEndException will break this loop if no valid file in list   
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       //cout <<"Open in single mode" << endl;
00296        if(NextFile()<0)
00297           fbIsOpen=kFALSE; // only for single mode the
00298                            // error result of first NextFile()
00299                            // will indicate that open failed
00300        else
00301           fbIsOpen=kTRUE;
00302    }
00303    // note that open flag indicates state of complete mbssource,
00304    // not the state of one single file in the multiple file list.
00305 
00306 
00307   
00309 //int fiNumSub=2; // number of subevents, fix
00310 //int fiNumDat=16; // maximum allocated data longs per subevent
00311 //int fiDLen=(sizeof(s_ve10_1)-sizeof(s_evhe)+fiNumSub*(sizeof(s_ves10_1)+fiNumDat*sizeof(Int_t))) / 2 ;
00312 //fxEvent= (s_ve10_1*) new Short_t[fiDLen+sizeof(s_evhe)];   ;
00313 //fxEvent->l_dlen=fiDLen;
00314 //fxEvent->i_subtype=1;
00315 //fxEvent->i_type=10;
00316 //fxEvent->l_count=0;
00317 //s_ves10_1* subevt=(s_ves10_1*) (void*) (fxEvent+1);
00318 //for(Int_t i=0;i<fiNumSub;++i)
00319 //   {
00320 //      //cout <<"\tSubevt "<<i <<" at "<< subevt<< endl;
00321 //      int l_val_num = (int)(10); // random number for number of data longwords
00322 //      if(l_val_num>fiNumDat) l_val_num=fiNumDat; // never exceed allocated field
00323 //      // setup subevent header:
00324 //      subevt->i_type=10;
00325 //      subevt->i_subtype=1;
00326 //      subevt->h_subcrate=i+1; // set subevent id numbers:
00327 //      subevt->h_control=2*i;
00328 //      subevt->i_procid=4*i;
00329 //      //subevt->l_dlen=fiNumDat*2+2; // length in short units + 2
00330 //      subevt->l_dlen=l_val_num*2+2; // subevent length in short units + 2
00331 //      fxEvent->l_dlen+=(l_val_num*sizeof(Int_t)) / 2 ; // add datalength to total length in shorts
00332 //
00333 //      //cout <<"\t dlen="<<subevt->l_dlen << endl;
00334 //      Int_t* subdata= (Int_t*) (subevt+1); // data starts after subevt
00335 //      //cout <<"\t data="<<subdata << endl;
00336 //      for(Int_t j=0;j<l_val_num;++j)
00337 //         {
00338 //             *(subdata+j)=j; // later use random generator here
00339 //             //cout <<"\t\t"<<"filled "<<j<<" with "<<*(subdata+j) <<"at "<<(subdata+j) << endl;
00340 //         } // for (... numdat ...)
00341 //      subevt=(s_ves10_1*) (subdata+l_val_num); // next subheader after last data
00342 //   } // for(...numsub)
00343 //   fxEvent->l_dlen+=(sizeof(s_ve10_1)-sizeof(s_evhe)+fiNumSub*sizeof(s_ves10_1))/2;
00346 
00347 return 0;
00348 }
00349 
00350 
00351 Int_t TGo4MbsFile::NextFile()
00352 {
00353 CloseFile();
00354 fuEventCounter=0;
00355  // read next name from namesfile
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      //static int cnt=0;
00366      do {
00367         fxMultiFile->getline(nextline,TGo4EventSource::fguTXTLEN, '\n' ); // read whole line
00368         //cout <<"read line "<<cnt++<<" : "<<nextline << endl;
00369         //if(fxMultiFile->rdstate()==ios::eofbit)
00370         if(fxMultiFile->eof() || !fxMultiFile->good())
00371           {
00372               // reached last filename, or read error?
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               //throw TGo4EventErrorException(this,3);
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             // keycharacter indicates we want to execute a root macro
00386             // treat the case that @command is commented out before!
00387             {
00388                command++; // skip @ letter
00389                TGo4Analysis::Instance()->Message(1,"TGo4MbsFile list:%s-- executing command: %s ", GetName(), command);
00390                //TGo4Log::Info("TGo4MbsFile list:%s-- executing command: %s ", GetName(), command);
00391                gROOT->ProcessLine(command);
00392             }        
00393         }
00394      while(strlen(nextline)==0 || rem1!=0 || rem2!=0 || command!=0); // skip any comments and empty lines, and continue after macro execution
00395      convs=sscanf(nextline,"%s %s %u %u %u",nextfile,nexttag,
00396               &fuStartEvent, &fuStopEvent, &fuEventInterval);
00397      if(convs<2)
00398         {
00399          // line contained not all parameters, reset remaining
00400          fuStartEvent=0;
00401          fuStopEvent=0;
00402          fuEventInterval=0;
00403          snprintf(nexttag,
00404             TGo4EventSource::fguTXTLEN,"%s",
00405             TGo4MbsFile::fgcNOTAGFILE);
00406         }
00407 //     cout <<"Read next filename "<<nextfile<<" and tag "<<nexttag << endl;
00408 //     cout <<"Got Start:"<<fuStartEvent<<". stop:"<<fuStopEvent,
00409 //     cout <<", interval:" <<fuEventInterval<< endl;
00410      fxCurrentFile=nextfile;
00411      if(!strcmp(nexttag,"0") || !strcmp(nexttag,""))
00412        {
00413          fxCurrentTag=TGo4MbsFile::fgcNOTAGFILE; // no tagfile if no name
00414        }
00415     else
00416        {
00417          fxCurrentTag=nexttag;
00418        }
00419    }
00420 else
00421    {
00422       //no multiple file: use default names
00423       fxCurrentTag=GetTagName();
00424       fxCurrentFile=GetName();
00425    }
00426 
00427 try
00428 {
00429    OpenFile();
00430    return 0;
00431 }// try
00432 
00433 catch(TGo4EventErrorException& ex)
00434 {
00435    if(fbMultipleMode)
00436       {
00437          // something went wrong opening next file, skip it
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"; // looks better in display message
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); // provide text message for later output
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 //----------------------------END OF GO4 SOURCE FILE ---------------------

Generated on Tue Nov 8 10:55:53 2005 for Go4-v2.10-5 by doxygen1.2.15