Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "TYYYEventSource.h"
00015
00016 #include <stdlib.h>
00017
00018 #include "TClass.h"
00019
00020 #include "TGo4Log.h"
00021 #include "TGo4EventErrorException.h"
00022 #include "TGo4EventEndException.h"
00023 #include "TGo4EventTimeoutException.h"
00024 #include "TGo4UserSourceParameter.h"
00025 #include "TYYYRawEvent.h"
00026
00027 TYYYEventSource::TYYYEventSource(const char* name,
00028 const char* args,
00029 Int_t port) :
00030 TGo4EventSource(name),
00031 fbIsOpen(kFALSE),
00032 fxArgs(args),
00033 fiPort(port),
00034 fxFile(0)
00035 {
00036 Open();
00037 }
00038
00039 TYYYEventSource::TYYYEventSource(TGo4UserSourceParameter* par) :
00040 TGo4EventSource(" "),
00041 fbIsOpen(kFALSE),
00042 fxArgs(),
00043 fiPort(0),
00044 fxFile(0)
00045 {
00046 if(par) {
00047 SetName(par->GetName());
00048 SetPort(par->GetPort());
00049 SetArgs(par->GetExpression());
00050 Open();
00051 } else {
00052 TGo4Log::Error("TYYYEventSource constructor with zero parameter!");
00053 }
00054 }
00055
00056 TYYYEventSource::TYYYEventSource() :
00057 TGo4EventSource("default YYY source"),
00058 fbIsOpen(kFALSE),
00059 fxArgs(),
00060 fiPort(0),
00061 fxFile(0)
00062 {
00063 }
00064
00065 TYYYEventSource::~TYYYEventSource()
00066 {
00067 Close();
00068 }
00069
00070 Bool_t TYYYEventSource::CheckEventClass(TClass* cl)
00071 {
00072 return cl->InheritsFrom(TYYYRawEvent::Class());
00073 }
00074
00075 Bool_t TYYYEventSource::BuildEvent(TGo4EventElement* dest)
00076 {
00077 TYYYRawEvent* evnt = (TYYYRawEvent*) dest;
00078 if (evnt==0) return kFALSE;
00079
00080 char sbuf[1024], buffer[1024];
00081
00082
00083 do {
00084 fxFile->getline(sbuf, sizeof(sbuf), '\n' );
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 } while(strstr(sbuf,"#") || strstr(sbuf,"!") );
00093
00094 Int_t status=1;
00095
00096
00097 Int_t scanresult=0;
00098 Int_t numval=0;
00099 const char* cursor = sbuf;
00100 do {
00101 evnt->ReAllocate(numval+1);
00102 scanresult = sscanf(cursor,"%s",buffer);
00103
00104
00105 if(scanresult!=0 && scanresult!=-1) {
00106 evnt->fdData[numval] = atof(buffer);
00107 status=0;
00108 }
00109 numval++;
00110 cursor+=strlen(buffer)+1;
00111
00112 } while( scanresult!=0 && scanresult!=-1);
00113
00114
00115
00116 if(status!=0) {
00117 evnt->SetValid(kFALSE);
00118
00119 SetErrMess("YYY Event Source -- ERROR !!!");
00120 throw TGo4EventErrorException(this);
00121 }
00122
00123 return kTRUE;
00124 }
00125
00126 Int_t TYYYEventSource::Open()
00127 {
00128 if(fbIsOpen) return -1;
00129 TGo4Log::Info("Open of TYYYEventSource %s", GetName());
00130
00131 fxFile = new std::ifstream(GetName());
00132 if((fxFile==0) || !fxFile->good()) {
00133 delete fxFile; fxFile = 0;
00134 SetCreateStatus(1);
00135 SetErrMess(Form("Eror opening user file:%s",GetName()));
00136 throw TGo4EventErrorException(this);
00137 }
00138 fbIsOpen = kTRUE;
00139 return 0;
00140 }
00141
00142 Int_t TYYYEventSource::Close()
00143 {
00144 if(!fbIsOpen) return -1;
00145 TGo4Log::Info("Close of TYYYEventSource");
00146 Int_t status=0;
00147 delete fxFile;
00148 fbIsOpen=kFALSE;
00149 return status;
00150 }