Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

TGo4MbsFile.cxx

Go to the documentation of this file.
00001 //-------------------------------------------------------------
00002 //        Go4 Release Package v3.04-01 (build 30401)
00003 //                      28-November-2008
00004 //---------------------------------------------------------------
00005 //   The GSI Online Offline Object Oriented (Go4) Project
00006 //   Experiment Data Processing at EE 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 "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    // test if we had reached the last event:
00098    if(fuStopEvent!=0 && fuEventCounter>=fuStopEvent)
00099       {
00100          SetEventStatus(GETEVT__NOMORE);
00101       }
00102    else
00103    {
00104       if(fbFirstEvent)
00105          {
00106             if(fuStartEvent>0)
00107                { // we want to work with "real" event number for first event
00108                   skip=(Int_t) fuStartEvent-1;
00109                   if(skip) fuEventCounter++; // need to correct for init value of event counter below!
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; // some new compilers may warn if we directly dereference member variable in function argument
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); // provide text message for later output
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 } // try
00148 
00149 catch(TGo4EventEndException& ex)
00150 {
00151    if(fbMultipleMode)
00152       {
00153          ex.Handle(); // display message
00154         // catch here the end of file case only
00155         // try to open next file in list:
00156         while(NextFile()<0){;}
00157                    //skip filenames with open error until a file
00158                    // in the list opens properly (retval==0)
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                      // priority 0 means do not stop analysis
00169                 // we (mis-)use an error exception with no stop to
00170                // skip the processing of the previous event  in the
00171                // subsequent analysis for a second time
00172             // note that NextFile() throws an exception itself
00173             // if list of files is at end
00174       }
00175    else
00176       {
00177          throw;  // normal end of input for one file
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 //cout << "Close of TGo4MbsFile"<< endl;
00193 Int_t rev=0;
00194 rev=GetCreateStatus();
00195 // close connection/file
00196 if(CloseFile() == GETEVT__SUCCESS)
00197    {
00198       fbIsOpen=kFALSE;
00199    }
00200 else
00201    {
00202       // do nothing (for streamer)
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       //cout <<"Executing command "<<command << endl;
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 // evaluate wildcard input:
00225 if(*GetName()=='@') // old style: filelist name starts with @
00226    {
00227       //cout <<"Multiple mode with @" << endl;
00228       // name indicates steering file
00229       fbMultipleMode=kTRUE;
00230       fbWildcardMode=kFALSE;
00231       multiname=strstr(GetName(),"@");
00232       multiname++; // skip at character
00233       fxMultiName=multiname;
00234    }
00235 else if(fName.EndsWith(fgcFILELISTSUF))// new style: list mode list
00236    {
00237       //cout <<"Multiple mode for .lml " << endl;
00238       fbMultipleMode=kTRUE;
00239       fbWildcardMode=kFALSE;
00240       fxMultiName=GetName();
00241    }
00242 else if(strstr(GetName(),"*") || strstr(GetName(),"?"))
00243    {
00244       //cout <<"Wildcard mode" << endl;
00245       // name indicates wildcard expression
00246       fbWildcardMode=kTRUE;
00247       fbMultipleMode=kTRUE;
00248       multiname = fgcWILDFILE;
00249       // evaluate expression here and create input file:
00250       Text_t wildexpr[TGo4EventSource::fguTXTLEN];
00251       if(!strstr(GetName(),fgcLMDSUF))
00252          {
00253           //cout <<"adding suffix "<<fgcLMDSUF << endl;
00254           // wildcard name does not contain lmd suffix, we put it in
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       //cout <<"Executing command "<<command << endl;
00272       gSystem->Exec(command); // write input file from wildcard expression
00273 
00274    }
00275 else
00276    {
00277       // name is name of single input lmd file
00278       //cout <<"Simple mode" << endl;
00279       fbMultipleMode=kFALSE;
00280       fbWildcardMode=kFALSE;
00281    }
00282 
00283 
00285 // now treat different input modes:
00286 if(fbMultipleMode)
00287    {
00288       //cout <<"Opening multiple file "<<fxMultiName.Data() << endl;
00289       fxMultiFile=new std::ifstream(fxMultiName.Data());
00290       if(fxMultiFile==0)
00291          {
00292             SetCreateStatus(GETEVT__NOFILE);
00293             Text_t buffer[TGo4EventSource::fguTXTLEN];
00294             //f_evt_error(GetCreateStatus(),buffer,1); // provide text message for later output
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) {;} // skip invalid filenames
00301        // note that TGo4EventEndException will break this loop if no valid file in list
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       //cout <<"Open in single mode" << endl;
00315        if(NextFile()<0)
00316           fbIsOpen=kFALSE; // only for single mode the
00317                            // error result of first NextFile()
00318                            // will indicate that open failed
00319        else
00320           fbIsOpen=kTRUE;
00321    }
00322    // note that open flag indicates state of complete mbssource,
00323    // not the state of one single file in the multiple file list.
00324 
00325 
00326 
00328 //int fiNumSub=2; // number of subevents, fix
00329 //int fiNumDat=16; // maximum allocated data longs per subevent
00330 //int fiDLen=(sizeof(s_ve10_1)-sizeof(s_evhe)+fiNumSub*(sizeof(s_ves10_1)+fiNumDat*sizeof(Int_t))) / 2 ;
00331 //fxEvent= (s_ve10_1*) new Short_t[fiDLen+sizeof(s_evhe)];   ;
00332 //fxEvent->l_dlen=fiDLen;
00333 //fxEvent->i_subtype=1;
00334 //fxEvent->i_type=10;
00335 //fxEvent->l_count=0;
00336 //s_ves10_1* subevt=(s_ves10_1*) (void*) (fxEvent+1);
00337 //for(Int_t i=0;i<fiNumSub;++i)
00338 //   {
00339 //      //cout <<"\tSubevt "<<i <<" at "<< subevt<< endl;
00340 //      int l_val_num = (int)(10); // random number for number of data longwords
00341 //      if(l_val_num>fiNumDat) l_val_num=fiNumDat; // never exceed allocated field
00342 //      // setup subevent header:
00343 //      subevt->i_type=10;
00344 //      subevt->i_subtype=1;
00345 //      subevt->h_subcrate=i+1; // set subevent id numbers:
00346 //      subevt->h_control=2*i;
00347 //      subevt->i_procid=4*i;
00348 //      //subevt->l_dlen=fiNumDat*2+2; // length in short units + 2
00349 //      subevt->l_dlen=l_val_num*2+2; // subevent length in short units + 2
00350 //      fxEvent->l_dlen+=(l_val_num*sizeof(Int_t)) / 2 ; // add datalength to total length in shorts
00351 //
00352 //      //cout <<"\t dlen="<<subevt->l_dlen << endl;
00353 //      Int_t* subdata= (Int_t*) (subevt+1); // data starts after subevt
00354 //      //cout <<"\t data="<<subdata << endl;
00355 //      for(Int_t j=0;j<l_val_num;++j)
00356 //         {
00357 //             *(subdata+j)=j; // later use random generator here
00358 //             //cout <<"\t\t"<<"filled "<<j<<" with "<<*(subdata+j) <<"at "<<(subdata+j) << endl;
00359 //         } // for (... numdat ...)
00360 //      subevt=(s_ves10_1*) (subdata+l_val_num); // next subheader after last data
00361 //   } // for(...numsub)
00362 //   fxEvent->l_dlen+=(sizeof(s_ve10_1)-sizeof(s_evhe)+fiNumSub*sizeof(s_ves10_1))/2;
00365 
00366 return 0;
00367 }
00368 
00369 
00370 Int_t TGo4MbsFile::NextFile()
00371 {
00372 CloseFile();
00373 fuEventCounter=0;
00374  // read next name from namesfile
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      //static int cnt=0;
00385      do {
00386         fxMultiFile->getline(nextline,TGo4EventSource::fguTXTLEN, '\n' ); // read whole line
00387         //cout <<"read line "<<cnt++<<" : "<<nextline << endl;
00388         //if(fxMultiFile->rdstate()==ios::eofbit)
00389         if(fxMultiFile->eof() || !fxMultiFile->good())
00390           {
00391               // reached last filename, or read error?
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               //throw TGo4EventErrorException(this,3);
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             // keycharacter indicates we want to execute a root macro
00405             // treat the case that @command is commented out before!
00406             {
00407                command++; // skip @ letter
00408                TGo4Analysis::Instance()->Message(1,"TGo4MbsFile list:%s-- executing command: %s ", GetName(), command);
00409                //TGo4Log::Info("TGo4MbsFile list:%s-- executing command: %s ", GetName(), command);
00410                gROOT->ProcessLine(command);
00411             }
00412         }
00413      while(strlen(nextline)==0 || rem1!=0 || rem2!=0 || command!=0); // skip any comments and empty lines, and continue after macro execution
00414      convs=sscanf(nextline,"%s %s %lu %lu %lu",nextfile,nexttag,
00415               &fuStartEvent, &fuStopEvent, &fuEventInterval);
00416      if(convs<2)
00417         {
00418          // line contained not all parameters, reset remaining
00419          fuStartEvent=0;
00420          fuStopEvent=0;
00421          fuEventInterval=0;
00422          snprintf(nexttag,
00423             TGo4EventSource::fguTXTLEN,"%s",
00424             TGo4MbsFile::fgcNOTAGFILE);
00425         }
00426 //     cout <<"Read next filename "<<nextfile<<" and tag "<<nexttag << endl;
00427 //     cout <<"Got Start:"<<fuStartEvent<<". stop:"<<fuStopEvent,
00428 //     cout <<", interval:" <<fuEventInterval<< endl;
00429      fxCurrentFile=nextfile;
00430      if(!strcmp(nexttag,"0") || !strcmp(nexttag,""))
00431        {
00432          fxCurrentTag=TGo4MbsFile::fgcNOTAGFILE; // no tagfile if no name
00433        }
00434     else
00435        {
00436          fxCurrentTag=nexttag;
00437        }
00438    }
00439 else
00440    {
00441       //no multiple file: use default names
00442       fxCurrentTag=GetTagName();
00443       fxCurrentFile=GetName();
00444    }
00445 
00446 try
00447 {
00448    OpenFile();
00449    return 0;
00450 }// try
00451 
00452 catch(TGo4EventErrorException& ex)
00453 {
00454    if(fbMultipleMode)
00455       {
00456          // something went wrong opening next file, skip it
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"; // looks better in display message
00481    }
00482 void* headptr=&fxInfoHeader; // some new compilers may warn if we directly dereference member
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); // provide text message for later output
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 //----------------------------END OF GO4 SOURCE FILE ---------------------

Generated on Fri Nov 28 12:59:08 2008 for Go4-v3.04-1 by  doxygen 1.4.2