• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

onlinemonitor/trbmonitor/TTrbSource.cxx (r4864/r4070)

Go to the documentation of this file.
00001 #include "TTrbSource.h"
00002 
00003 #include "TClass.h"
00004 
00005 #include <stdlib.h>
00006 
00007 #include "TGo4EventErrorException.h"
00008 #include "TGo4EventEndException.h"
00009 #include "TGo4EventTimeoutException.h"
00010 #include "TGo4UserSourceParameter.h"
00011 #include "TGo4MbsEvent.h"
00012 #include "TGo4Log.h"
00013 
00014 #include "base/commons.h"
00015 
00016 TTrbSource::TTrbSource(const char* name,
00017                                                 const char* args,
00018                                                 Int_t port) :
00019         TGo4EventSource(name),
00020         fbIsOpen(kFALSE),
00021         fxArgs(args),
00022         fiPort(port),
00023         fxFile(0),
00024         fxBuffer(0),
00025         fxHadEvent(0),
00026         fxBufsize(0),
00027         fxCursor(0),
00028         fxBufEnd(0)
00029 {
00030    Open();
00031 }
00032 
00033 TTrbSource::TTrbSource(TGo4UserSourceParameter* par) :
00034         TGo4EventSource(" "),
00035         fbIsOpen(kFALSE),
00036         fxArgs(""),
00037         fiPort(0),
00038         fxFile(0),
00039         fxBuffer(0),
00040         fxHadEvent(0),
00041         fxBufsize(0),
00042         fxCursor(0),
00043         fxBufEnd(0)
00044 {
00045         if(par)
00046         {
00047                 SetName(par->GetName());
00048                 SetPort(par->GetPort());
00049                 SetArgs(par->GetExpression());
00050                 Open();
00051         }
00052         else
00053         {
00054                 TGo4Log::Error("TTrbSource constructor with zero parameter!");
00055         }
00056 }
00057 
00058 TTrbSource::TTrbSource() :
00059         TGo4EventSource("default Trb source"),
00060         fbIsOpen(kFALSE),
00061         fxArgs(""),
00062         fiPort(0),
00063         fxFile(0),
00064         fxBuffer(0),
00065         fxHadEvent(0),
00066         fxBufsize(0),
00067         fxCursor(0),
00068         fxBufEnd(0)
00069 {
00070 }
00071 
00072 TTrbSource::~TTrbSource()
00073 {
00074         Close();
00075 }
00076 
00077 Bool_t TTrbSource::CheckEventClass(TClass* cl)
00078 {
00079         return cl->InheritsFrom(TGo4MbsEvent::Class());
00080 }
00081 
00082 std::streamsize TTrbSource::ReadFile(Char_t* dest, size_t len)
00083 {
00084         fxFile->read(dest, len);
00085         if(fxFile->eof() || !fxFile->good())
00086         {
00087                 SetCreateStatus(1);
00088                 SetErrMess(Form("End of input file %s", GetName()));
00089                 SetEventStatus(1);
00090                 throw TGo4EventEndException(this);
00091         }
00092         //cout <<"ReadFile reads "<< (hex) << fxFile->gcount()<<" bytes to 0x"<< (hex) <<(int) dest<< endl;
00093         return fxFile->gcount();
00094 }
00095 
00096 Bool_t TTrbSource::NextBuffer()
00097 {
00100         ReadFile(fxBuffer, sizeof(Trb_Event));
00101         fxHadEvent = (Trb_Event*)(fxBuffer);
00102 
00104         size_t evlen = fxHadEvent->GetSize();
00105         
00107         while((evlen % 8) != 0)
00108         {
00109                 evlen++;
00110 //              cout << "Next Buffer extends for padding the length to " << evlen << endl;
00111         }
00112 //      cout << "Next Buffer reads event of size:" << evlen << endl;
00113         if(evlen > Trb_BUFSIZE)
00114         {
00115                 SetErrMess(Form("Next event length 0x%x bigger than read buffer limit 0x%x", (unsigned) evlen, (unsigned) Trb_BUFSIZE));
00116                 SetEventStatus(1);
00117                 throw TGo4EventEndException(this);
00118         }
00119         ReadFile(fxBuffer + sizeof(Trb_Event), evlen - sizeof(Trb_Event));
00120         fxBufsize = evlen;
00121         fxBufEnd = (Short_t*)(fxBuffer+evlen);
00122         return kTRUE;
00123 
00124 }
00125 
00126 Bool_t TTrbSource::NextEvent(TGo4MbsEvent* target)
00127 {
00130 
00131         fiEventLen = (fxBufsize/sizeof(Short_t)) ; // data payload of mbs is full hades event in buffer
00132         fxSubevHead.fsProcid = roc::proc_TRBEvent; // mark to be processed by TTrbProc
00133         int evcounter = fxHadEvent->GetSeqNr();
00134 //      cout <<"Next Event is filling mbs event, fullid:"<<fxSubevHead.fiFullid<<", nr:"<<evcounter<< endl;
00135         target->AddSubEvent(fxSubevHead.fiFullid, (Short_t*) fxBuffer, fiEventLen + 2, kTRUE);
00136         target->SetDlen(fiEventLen+2+4); // total length of pseudo mbs event
00137 //      cout <<"Next Event setting event length:"<<fiEventLen+2+4<< endl;
00138 
00139         target->SetCount(evcounter);
00140 
00141 //      return kFALSE; // this means we need another file buffer for this event, what is never the case since buffer contains just one event
00142         return kTRUE; // event is ready
00143 }
00144 
00145 Bool_t TTrbSource::BuildEvent(TGo4EventElement* dest)
00146 {
00147         TGo4MbsEvent* evnt = dynamic_cast<TGo4MbsEvent*> (dest);
00148         if (evnt==0) return kFALSE;
00149         // this generic loop is intended to handle buffers with several events. we keep it here,
00150         // although our "buffers" consists of single events.
00151         do      {
00152                 NextBuffer(); // note: next buffer will throw exception on end of file
00153         }       while (!NextEvent(evnt));
00154 
00155         return kTRUE;
00156 }
00157 
00158 Int_t TTrbSource::Open()
00159 {
00160         if(fbIsOpen) return -1;
00161         TGo4Log::Info("Open of TTrbSource");
00162 
00164         fxFile = new std::ifstream(GetName(), ios::binary);
00165         if((fxFile==0) || !fxFile->good())
00166         {
00167                 delete fxFile; fxFile = 0;
00168                 SetCreateStatus(1);
00169                 SetErrMess(Form("Eror opening user file:%s", GetName()));
00170                 throw TGo4EventErrorException(this);
00171         }
00172         fxBuffer = new Char_t[Trb_BUFSIZE];
00173 
00174         fbIsOpen = kTRUE;
00175         return 0;
00176 }
00177 
00178 Int_t TTrbSource::Close()
00179 {
00180         if(!fbIsOpen) return -1;
00181         TGo4Log::Info("Close of TTrbSource");
00182         delete fxBuffer;
00183         Int_t status = 0;       
00184         delete fxFile;
00185         fbIsOpen = kFALSE;
00186         return status;
00187 }

Generated on Tue Dec 10 2013 04:52:23 for ROCsoft by  doxygen 1.7.1