00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "TGo4MbsSource.h"
00017
00018 #include <iostream.h>
00019
00020 #include "TGo4MbsEvent.h"
00021 #include "TGo4MbsSubEvent.h"
00022
00023 #include "Go4Log/TGo4Log.h"
00024 #include "Go4Event/TGo4EventErrorException.h"
00025 #include "Go4Event/TGo4EventTimeoutException.h"
00026 #include "Go4Event/TGo4EventEndException.h"
00027
00028 const UInt_t TGo4MbsSource::fguLONGBYCHAR= sizeof(Int_t) / sizeof(Char_t);
00029
00030 const UInt_t TGo4MbsSource::fguSHORTBYCHAR= sizeof(Short_t) / sizeof(Char_t);
00031
00032 const UInt_t TGo4MbsSource::fguLONGBYSHORT= sizeof(Int_t) / sizeof(Short_t);
00033
00034 const UInt_t TGo4MbsSource::fguEVHEBYCHAR= sizeof(s_evhe) / sizeof(Char_t);
00035
00036
00037 TGo4MbsSource::TGo4MbsSource(const char* name, Int_t mode)
00038 : TGo4EventSource(name), fiMode(mode),
00039 fxEvent(0), fxBuffer(0), fxInfoHeader(0),
00040 fbIsOpen(kFALSE), fbDataCopyMode(kFALSE), fuEventCounter(0),fuStartEvent(0) ,fuStopEvent(0),
00041 fuEventInterval(0), fiTimeout(-1)
00042 {
00043 TRACE((15,"TGo4MbsSource::TGo4MbsSource(Text_t*, Int_t)",__LINE__, __FILE__));
00044
00045
00046
00047 }
00048
00049
00050 TGo4MbsSource::TGo4MbsSource()
00051 : TGo4EventSource("default mbs source"),fiMode(0),
00052 fxEvent(0), fxBuffer(0), fxInfoHeader(0),
00053 fbIsOpen(kFALSE),fbDataCopyMode(kFALSE),fuEventCounter(0),fuStartEvent(0) ,fuStopEvent(0),
00054 fuEventInterval(0), fiTimeout(-1)
00055 {
00056 TRACE((15,"TGo4MbsSource::TGo4MbsSource()",__LINE__, __FILE__));
00057 }
00058
00059 TGo4MbsSource::~TGo4MbsSource()
00060 {
00061 TRACE((15,"TGo4MbsSource::~TGo4MbsSource()",__LINE__, __FILE__));
00062 Close();
00063 }
00064
00065 void TGo4MbsSource::SetPrintEvent(Int_t num, Int_t sid, Int_t longw,
00066 Int_t hexw, Int_t dataw)
00067 {
00068 fxPrEventPar.fiNum=num;
00069 fxPrEventPar.fiSid=sid;
00070 fxPrEventPar.fiLong=longw;
00071 fxPrEventPar.fiHex=hexw;
00072 fxPrEventPar.fiData=dataw;
00073 }
00074
00075
00076 void TGo4MbsSource::BuildMbsEvent(TGo4MbsEvent* target)
00077 {
00078 TRACE((12,"TGo4MbsSource::BuildMbsEvent(TGo4MbsEvent*)",__LINE__, __FILE__));
00079
00080 if(fxEvent!=0 && GetEventStatus()==GETEVT__SUCCESS)
00081 {
00082
00083 if(fxPrEventPar.fiNum>0)
00084 {
00085 f_evt_type(fxBuffer,
00086 (s_evhe *) fxEvent,
00087 fxPrEventPar.fiSid,
00088 fxPrEventPar.fiLong,
00089 fxPrEventPar.fiHex,
00090 fxPrEventPar.fiData);
00091 cout << endl;
00092 fxPrEventPar.fiNum--;
00093 }
00094
00095 Char_t* endofevent= (Char_t*) (fxEvent) +
00096 (fxEvent->l_dlen) * fguSHORTBYCHAR +
00097 fguEVHEBYCHAR ;
00098
00099 s_ves10_1* subevent;
00100 Char_t* subevtpointer;
00101 TGo4MbsSubEvent* subtarget;
00102 TGo4MbsSubEvent* subtargetindex;
00103
00104 target->SetValid(kTRUE);
00105 target->SetDlen(fxEvent->l_dlen);
00106 target->SetType(fxEvent->i_type);
00107 target->SetSubtype(fxEvent->i_subtype);
00108 target->SetTrigger(fxEvent->i_trigger);
00109 target->SetCount(fxEvent->l_count);
00110 target->SetDummy(fxEvent->i_dummy);
00111 Int_t totalsubdatalength=0;
00112 if(fxEvent->l_dlen > 4)
00113 {
00114
00115 subevent= (s_ves10_1*) (fxEvent + 1);
00116
00117
00118 Int_t datalength=0;
00119 Int_t fieldlength=0;
00120 while((datalength=subevent->l_dlen) >0)
00121 {
00122 totalsubdatalength+=datalength-2+sizeof(s_ves10_1)/sizeof(Short_t);
00123 if(datalength>fxEvent->l_dlen)
00124 {
00125 TGo4Log::Debug(" !!! MbsSource -- SUBEVENTS LENGTH mismatch!!! skipping event #%d",fxEvent->l_count);
00126 TGo4Log::Debug("\t sub dlen:%d, event dlen:%d ",datalength-2, fxEvent->l_dlen-4);
00127 target->SetValid(kFALSE);
00128 break;
00129 }
00130
00131 if(datalength>2)
00132 {
00133 fieldlength= (datalength-2) / fguLONGBYSHORT ;
00134 }
00135 else
00136 {
00137 TGo4Log::Debug(" !!! MbsSource -- EMPTY subevent #%d ",fxEvent->l_count);
00138 fieldlength =0;
00139 }
00140
00141 target->ResetIterator();
00142 subtarget=0;
00143 while ( ( subtargetindex= target->NextSubEvent(kTRUE) ) !=0 )
00144 {
00145
00146
00147
00148
00149
00150
00151
00152 Int_t* subtargetid= (Int_t*) &((subtargetindex->fxHeader).fsProcid);
00153 Int_t* subeventid= (Int_t*) &(subevent->i_procid);
00154 if(*subtargetid == *subeventid)
00155 {
00156
00157 if(!subtargetindex->fbIsFilled)
00158 {
00159
00160 subtarget=subtargetindex;
00161 break;
00162 }
00163 else
00164 {
00165
00166 }
00167 }
00168 else
00169 {
00170
00171 }
00172 }
00173 if(!subtarget)
00174 {
00175
00176 subtarget = new TGo4MbsSubEvent(fieldlength);
00177 if(!fbDataCopyMode)
00178 {
00179 subtarget->fbIsDataOwner=kFALSE;
00180 delete [] (subtarget->fiData);
00181 subtarget->fiAllocLen=0;
00182 }
00183 TGo4Log::Debug(" Created new output subevent for event %d\n\tpid:%d subcrate:%d ctrl:%d",
00184 fxEvent->l_count ,subevent->i_procid,subevent->h_subcrate, subevent->h_control);
00185 target->fxSubEvArray->AddLast(subtarget);
00186 }
00187 else
00188 { }
00190 subtarget->SetDlen(subevent->l_dlen);
00191 subtarget->SetType(subevent->i_type);
00192 subtarget->SetSubtype(subevent->i_subtype);
00193 subtarget->SetProcid(subevent->i_procid);
00194 subtarget->SetSubcrate(subevent->h_subcrate);
00195 subtarget->SetControl(subevent->h_control);
00196 void* data = (void*) (subevent+1);
00197 if(fbDataCopyMode)
00198 {
00199 subtarget->fbIsDataOwner=kTRUE;
00200 subtarget->ReAllocate(fieldlength);
00202
00203 {
00204 memcpy((void*) (subtarget->fiData),
00205 data,
00206 (datalength-2)*sizeof(Short_t) );
00207 }
00208 else
00209 {
00210
00211 }
00212 }
00213 else
00214 {
00215
00216 subtarget->fbIsDataOwner=kFALSE;
00217 subtarget->fiAllocLen=fieldlength;
00218 if(datalength>2)
00219 subtarget->fiData= (Int_t*) data;
00220 else
00221 subtarget->fiData= 0;
00222 }
00223 subtarget->fbIsFilled=kTRUE;
00225
00226 datalength * fguSHORTBYCHAR +
00227 fguEVHEBYCHAR );
00228 subevent = (s_ves10_1*) subevtpointer;
00229 if ((void*) (subevent) >= endofevent)
00230 {
00231
00232 break;
00233 }
00234 }
00235 if(totalsubdatalength!=fxEvent->l_dlen-4)
00236 {
00237 TGo4Log::Debug(" !!! MbsSource -- SUBEVENTS TOTAL LENGTH mismatch!!! disabling event #%d",fxEvent->l_count);
00238 TGo4Log::Debug("\t subdlen sum:%d, event dlen:%d ",totalsubdatalength, fxEvent->l_dlen-4);
00239 target->SetValid(kFALSE);
00240 }
00241 }
00242 else
00243 {
00244
00245 TGo4Log::Debug(" !!! MbsSource -- NO SUBEVENTS!!! ");
00246 SetErrMess("!!! BuildMbsEvent: -- NO SUBEVENTS!!!");
00247 throw TGo4EventTimeoutException(this);
00248 }
00249 }
00250 else
00251 {
00252
00253 TGo4Log::Debug(" !!! Mbs Source -- ERROR: %s !!! ",GetErrMess());
00254 throw TGo4EventErrorException(this);
00255 }
00256 }
00257
00258 Int_t TGo4MbsSource::NextEvent()
00259 {
00260 TRACE((12,"TGo4MbsSource::NextEvent()",__LINE__, __FILE__));
00261
00262 UInt_t eventstep;
00263 if(fuEventInterval)
00264 {
00265 eventstep=fuEventInterval;
00266 }
00267 else
00268 {
00269 eventstep=1;
00270 }
00271
00272 if(fuStopEvent!=0 && fuEventCounter>=fuStopEvent)
00273 {
00274 SetEventStatus(GETEVT__NOMORE);
00275 }
00276 else
00277 {
00278
00279 if(fuEventCounter<fuStartEvent)
00280 {
00281 while(fuEventCounter++<fuStartEvent){}
00282 }
00283 else {}
00284 UInt_t counter;
00285 for(counter=fuEventCounter; counter<fuEventCounter+eventstep; ++counter)
00286 {
00287
00288 Int_t status=f_evt_get_event(
00289 &fxInputChannel,
00290 (Int_t **) &fxEvent,
00291 (Int_t **) &fxBuffer);
00292 SetEventStatus(status);
00293 if(status!=0)
00294 break;
00295 }
00296 fuEventCounter=counter;
00297 }
00298 if(GetEventStatus()!=0)
00299 {
00300 Text_t buffer[TGo4EventSource::fguTXTLEN];
00301 f_evt_error(GetEventStatus(),buffer,1);
00302 SetErrMess(buffer);
00303 }
00304
00305 switch(GetEventStatus())
00306 {
00307 case 0:
00308 return 0;
00309 break;
00310 case GETEVT__TIMEOUT:
00311 throw TGo4EventTimeoutException(this);
00312 break;
00313 case GETEVT__NOMORE:
00314 throw TGo4EventEndException(this);
00315 break;
00316 default:
00317 throw TGo4EventErrorException(this);
00318 break;
00319 }
00320 cout << "MbsSource::NextEvent -- NEVER COME HERE" << endl;
00321 return GetEventStatus();
00322 }
00323
00324 Int_t TGo4MbsSource::Open()
00325 {
00326 TRACE((12,"TGo4MbsSource::Open()",__LINE__, __FILE__));
00327
00328 if(fbIsOpen)
00329 return -1;
00330
00331
00332 Int_t status = f_evt_get_open(
00333 fiMode,
00334 const_cast<Text_t*>( GetName() ),
00335 &fxInputChannel,
00336 (Char_t**) &fxInfoHeader,
00337 0,
00338 0
00339 );
00340
00341 SetCreateStatus(status);
00342 if(GetCreateStatus() !=GETEVT__SUCCESS)
00343 {
00344
00345
00346 Text_t buffer[TGo4EventSource::fguTXTLEN];
00347 f_evt_error(GetCreateStatus(),buffer,1);
00348
00349
00350
00351 SetErrMess(buffer);
00352 fbIsOpen=kFALSE;
00353 throw TGo4EventErrorException(this);
00354 }
00355 else
00356 {
00357 f_evt_timeout(&fxInputChannel, fiTimeout);
00358 TGo4Log::Debug(" Mbs Source -- opened input from type %d: %s . Timeout=%d s",
00359 fiMode, GetName(), fiTimeout);
00360
00361 fbIsOpen=kTRUE;
00362 }
00363 return status;
00364 }
00365
00366 Int_t TGo4MbsSource::Close()
00367 {
00368 TRACE((12,"TGo4MbsSource::Close()",__LINE__, __FILE__));
00369 if(!fbIsOpen)
00370 return -1;
00371
00372 Int_t rev=GetCreateStatus();
00373
00374 if(rev == GETEVT__SUCCESS)
00375 {
00376 f_evt_get_close(&fxInputChannel);
00377 fbIsOpen=kFALSE;
00378 }
00379 else
00380 {
00381
00382 }
00383
00384 return rev;
00385
00386
00387 }
00388
00389
00390 s_bufhe * TGo4MbsSource::GetBufferHeader()
00391 {
00392 return (s_bufhe*) f_evt_get_buffer_ptr(&fxInputChannel);
00393 }
00394
00395
00396 ClassImp(TGo4MbsSource)
00397
00398
00399
00400
00401