GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4MbsSource.cxx
Go to the documentation of this file.
1 // $Id: TGo4MbsSource.cxx 999 2013-07-25 11:58:59Z linev $
2 //-----------------------------------------------------------------------
3 // The GSI Online Offline Object Oriented (Go4) Project
4 // Experiment Data Processing at EE department, GSI
5 //-----------------------------------------------------------------------
6 // Copyright (C) 2000- GSI Helmholtzzentrum f�r Schwerionenforschung GmbH
7 // Planckstr. 1, 64291 Darmstadt, Germany
8 // Contact: http://go4.gsi.de
9 //-----------------------------------------------------------------------
10 // This software can be used under the license agreements as stated
11 // in Go4License.txt file which is part of the distribution.
12 //-----------------------------------------------------------------------
13 
14 #include "TGo4MbsSource.h"
15 
16 #include <stdlib.h>
17 
18 #include "TObjArray.h"
19 #include "TClass.h"
20 #include "TSystem.h"
21 
22 #include "TGo4MbsEvent.h"
23 #include "TGo4MbsSubEvent.h"
24 #include "TGo4MbsSourceParameter.h"
25 
26 #include "TGo4Log.h"
29 #include "TGo4EventEndException.h"
30 
31 const UInt_t TGo4MbsSource::fguLONGBYCHAR = sizeof(Int_t) / sizeof(Char_t);
32 const UInt_t TGo4MbsSource::fguSHORTBYCHAR = sizeof(Short_t) / sizeof(Char_t);
33 const UInt_t TGo4MbsSource::fguLONGBYSHORT = sizeof(Int_t) / sizeof(Short_t);
34 const UInt_t TGo4MbsSource::fguEVHEBYCHAR = sizeof(s_evhe) / sizeof(Char_t);
35 
37  TGo4EventSource(par->GetName()),
38  fiMode(mode),
39  fiRetryCnt(par->GetRetryCnt()),
40  fxEvent(0), fxBuffer(0), fxInfoHeader(0),
41  fbIsOpen(kFALSE), fbDataCopyMode(kFALSE),
42  fuEventCounter(0), fbFirstEvent(kTRUE),
43  fuStartEvent(par->GetStartEvent()) ,
44  fuStopEvent(par->GetStopEvent()),
45  fuEventInterval(par->GetEventInterval()),
46  fiTimeout(par->GetTimeout()),
47  fiPort(par->GetPort())
48 {
50  GO4TRACE((15,"TGo4MbsSource::TGo4MbsSource(const char*, Int_t)",__LINE__, __FILE__));
51  // Open() call will be done by subclasses ctors, so we can overwrite Open() method
52  //std::cout <<"TGo4MbsSource with data copy mode="<<fbDataCopyMode << std::endl;
53 }
54 
55 
56 TGo4MbsSource::TGo4MbsSource(const char* name, Int_t mode) :
57  TGo4EventSource(name),
58  fiMode(mode),
59  fiRetryCnt(0),
60  fxEvent(0), fxBuffer(0), fxInfoHeader(0),
61  fbIsOpen(kFALSE), fbDataCopyMode(kFALSE),
62  fuEventCounter(0), fbFirstEvent(kTRUE),
63  fuStartEvent(0) ,fuStopEvent(0), fuEventInterval(0),
64  fiTimeout(-1), fiPort(0)
65 {
67  GO4TRACE((15,"TGo4MbsSource::TGo4MbsSource(const char*, Int_t)",__LINE__, __FILE__));
68 
70 
71  // Open() call will be done by subclasses ctors, so we can overwrite Open() method
72  //std::cout <<"TGo4MbsSource with data copy mode="<<fbDataCopyMode << std::endl;
73 }
74 
75 
77  TGo4EventSource("default mbs source"),
78  fiMode(0),
79  fiRetryCnt(0),
80  fxEvent(0), fxBuffer(0), fxInfoHeader(0),
81  fbIsOpen(kFALSE), fbDataCopyMode(kFALSE),
82  fuEventCounter(0), fbFirstEvent(kTRUE),
83  fuStartEvent(0) ,fuStopEvent(0), fuEventInterval(0),
84  fiTimeout(-1), fiPort(0)
85 {
87  GO4TRACE((15,"TGo4MbsSource::TGo4MbsSource()",__LINE__, __FILE__));
88 }
89 
91 {
92  GO4TRACE((15,"TGo4MbsSource::~TGo4MbsSource()",__LINE__, __FILE__));
93  Close();
95 }
96 
97 void TGo4MbsSource::SetPrintEvent(Int_t num, Int_t sid, Int_t longw, Int_t hexw, Int_t dataw)
98 {
99  fxPrEventPar.fiNum = num;
100  fxPrEventPar.fiSid = sid;
101  fxPrEventPar.fiLong = longw;
102  fxPrEventPar.fiHex = hexw;
103  fxPrEventPar.fiData = dataw;
104 }
105 
107 {
108  return cl->InheritsFrom(TGo4MbsEvent::Class());
109 }
110 
112 {
113  Int_t rev = NextEvent();
114  if(rev != GETEVT__SUCCESS) {
115  TGo4Log::Debug(" Mbs Event -- !!! NextEvent() error: %s !!! ", GetErrMess());
116  return kFALSE;
117  }
118 
119  BuildMbsEvent((TGo4MbsEvent*) dest);
120  return kTRUE;
121 }
122 
123 
125 {
126  GO4TRACE((12,"TGo4MbsSource::BuildMbsEvent(TGo4MbsEvent*)",__LINE__, __FILE__));
127  TGo4MbsSubEvent* subtarget=0;
128  if(fxEvent!=0 && GetEventStatus()==GETEVT__SUCCESS) {
129  // check for printevent mode here:
130  if(fxPrEventPar.fiNum>0) {
132  (s_evhe *) fxEvent,
137  std::cout << std::endl; // flush std::cout buffer
139  }
140  // we have a valid event, proceed
141  Char_t* endofevent = (Char_t*) (fxEvent) +
143  //std::cout << "end of event "<< endofevent << std::endl;
144  target->SetValid(kTRUE); // reset target if previously was set to false
145  target->SetDlen(fxEvent->l_dlen);
146  target->SetType(fxEvent->i_type);
147  target->SetSubtype(fxEvent->i_subtype);
148 
149 
150  //**************************************************************************
151  //**************************************************************************
152  // Event Type 10:
153  if(fxEvent->i_type==10) {
154  s_ves10_1* subevent; // pointer to subevent
155  Char_t* subevtpointer; // dito, in bytes
156  target->SetTrigger(fxEvent->i_trigger);
157  target->SetCount(fxEvent->l_count);
158  target->SetDummy(fxEvent->i_dummy);
159  Int_t totalsubdatalength=0; // check counter for total datalength of subevents
160  if(fxEvent->l_dlen > 4) {
161  // we have subevent data after the event header, proceed:
162  subevent = (s_ves10_1*) (fxEvent + 1);
163  // first subevent header starts after event header
164  // loop over subevents:
165  Int_t datalength = 0; // direct dlen from subevent header (in Short_t!)
166  // Int_t fieldlength=0; // actual size of the target Int_t data field
167  while((datalength = subevent->l_dlen) >0 ) {
168  totalsubdatalength+=datalength-2+sizeof(s_ves10_1)/sizeof(Short_t);
169  if(datalength>fxEvent->l_dlen) {
170  TGo4Log::Debug(" !!! MbsSource -- SUBEVENTS LENGTH mismatch!!! skipping event #%d",fxEvent->l_count);
171  TGo4Log::Debug("\t sub dlen:%d, event dlen:%d ",datalength-2, fxEvent->l_dlen-4);
172  target->SetValid(kFALSE);
173  break;
174  }
175  Int_t * subeventid= (Int_t *) (subevent) + 2; // full id starts 2 ints after subevent head anyway
176  //Int_t* subeventid= (Int_t*) &(subevent->i_procid); // full id is lw from control, subcrate, procid fields - some compilers complain here!
177  Short_t* data = (Short_t*) (subevent+1); // data starts after subevent header
178  subtarget = target->AddSubEvent(*subeventid, data, datalength, fbDataCopyMode); // find subevent that matches id and fill it
179  subtarget->SetType(subevent->i_type); // need to set ids manually afterwards
180  subtarget->SetSubtype(subevent->i_subtype);
181  subevtpointer = (Char_t*) (subevent) +
182  datalength * fguSHORTBYCHAR + fguEVHEBYCHAR;
183  subevent = (s_ves10_1*) subevtpointer;
184  if ((Char_t*) subevent >= endofevent) {
185  //std::cout << "found end of event, breaking.."<< std::endl;
186  break;
187  }
188  } // while((datalength=subevent->l_dlen) >0)
189  if(totalsubdatalength!=fxEvent->l_dlen-4) {
190  TGo4Log::Debug(" !!! MbsSource -- SUBEVENTS TOTAL LENGTH mismatch!!! disabling event #%d",fxEvent->l_count);
191  TGo4Log::Debug("\t subdlen sum:%d, event dlen:%d ",totalsubdatalength, fxEvent->l_dlen-4);
192  target->SetValid(kFALSE);
193  }
194  } else { // if(fxEvent->dlen>4)
195  // sorry, no subevents after event header
196  TGo4Log::Debug(" !!! MbsSource -- NO SUBEVENTS!!! ");
197  SetErrMess("!!! BuildMbsEvent: -- NO SUBEVENTS!!!");
198  throw TGo4EventTimeoutException(this); // no subevts=timeout
199  } // end if (fxEvent->dlen>0)
200  } else
201  //**************************************************************************
202  //**************************************************************************
203  // Event Type 4:
204  if(fxEvent->i_type==4) {
205  // std::cout <<"found event type 4" << std::endl;
206  s_evhe* eventfourone= (s_evhe*) fxEvent; // points to event 4 1 start
207  // copy pseudo event header information to our target:
208  target->SetTrigger(4);
209  target->SetCount(1);
210  target->SetDummy(0);
211  if(fxEvent->l_dlen > 0) {
212  Int_t subeventid= 4; // arbitrarily defined here for type 4,1
213  Short_t* data = (Short_t*) (eventfourone+1); // data starts after subevent header
214  Int_t datalength=eventfourone->l_dlen+2; // length of later subevent header (in Short_t!)
215  // add 2 to direct dlen from 4,1 event header to account subevent header
216  subtarget = target->AddSubEvent(subeventid, data, datalength, fbDataCopyMode); // find subevent that matches id and fill it
217  subtarget->SetType(4);
218  subtarget->SetSubtype(1);
219  } else { // if(fxEvent->dlen>0)
220  // sorry, no subevents after event header
221  TGo4Log::Debug(" !!! MbsSource -- NO Data in event 4,1!!! ");
222  SetErrMess("!!! BuildMbsEvent: -- NO Data in event 4,1!!!");
223  throw TGo4EventTimeoutException(this); // no data=timeout
224  } // end if (fxEvent->dlen>0)
225  } else {
226  TGo4Log::Debug(" !!! Mbs Source -- ERROR: Unknown event type %d !!! ",fxEvent->i_type);
227  throw TGo4EventErrorException(this);
228  } //if(fxEvent->i_type==...)
229 
230  } else {
231  // somethings wrong, display error message from f_evt_error()
232  TGo4Log::Debug(" !!! Mbs Source -- ERROR: %s !!! ",GetErrMess());
233  throw TGo4EventErrorException(this);
234  } //if(fxEvent!=0 && GetEventStatus()==GETEVT__SUCCESS)
235 }
236 
237 
238 TGo4MbsSubEvent* TGo4MbsSource::BuildMbsSubEvent(TGo4MbsEvent * target, Int_t fullID, Short_t* source, Int_t datalength)
239 {
240  // code moved into TGo4MbsEvent class, method kept for compatibility
241 
242  return target->AddSubEvent(fullID, source, datalength, fbDataCopyMode);
243 }
244 
246 {
247 
248 frombegin:
249 
250  GO4TRACE((12,"TGo4MbsSource::NextEvent()",__LINE__, __FILE__));
251  // skip and sample mode introduced without changed gsievt functions for first tests
252  ULong_t eventstep;
253 
254  if (fbFirstEvent) {
255  fbFirstEvent = kFALSE;
256  eventstep = fuStartEvent + 1;
257  TGo4Log::Info("MBS source:: First event = %lu step = %lu\n", fuStartEvent, fuEventInterval);
258 
259  } else
260  if(fuEventInterval > 0)
261  eventstep = fuEventInterval;
262  else
263  eventstep = 1;
264 
265  // test if we had reached the last event:
268  else {
269  // check possible overflow of our counter:
270  if(fuEventCounter+eventstep<fuEventCounter)
271  {
272  TGo4Log::Warn("TGo4MbsSource::NextEvent(): Overflow of eventcounter at %d, reset to 0",fuEventCounter),
273  fuEventCounter=0;
274  }
275  while (eventstep > 0) {
276  // retrieve the event, skip all events until end of the step
277  Int_t status = f_evt_get_event(fxInputChannel,
278  (Int_t **) (void*) &fxEvent,
279  (Int_t **) (void*) &fxBuffer);
280  SetEventStatus(status);
281  if(status!=0) break;
282  eventstep--;
283  fuEventCounter++;
284  }
285  }
286 
287  if(GetEventStatus()!=0) {
288  char buffer[TGo4EventSource::fguTXTLEN];
289  f_evt_error(GetEventStatus(),buffer,1); // provide text message for later output
290  SetErrMess(Form("%s name:%s", buffer, GetName()));
291  }
292 
293  switch(GetEventStatus()) {
294  case 0:
295  return 0;
296  break;
297  case GETEVT__TIMEOUT:
298  throw TGo4EventTimeoutException(this);
299  break;
300  case GETEVT__NOMORE:
301  throw TGo4EventEndException(this);
302  break;
303  default: {
305 
306  printf("Error code %d mess %s\n", GetEventStatus(), GetErrMess());
307 
308  Close();
309  Int_t cnt = fiRetryCnt;
310 
311  while (cnt-->0) {
312  gSystem->Sleep(1000);
313 
314  //if (TGo4Analysis::Instance())
315  // if (TGo4Analysis::Instance()->IsStopWorking()) return GetEventStatus();
316  try {
317  Open();
318  if (fbIsOpen) {
319  printf("Retry %d successful\n", cnt);
320  fflush(stdout);
321  goto frombegin;
322  }
323  } catch (TGo4EventErrorException e) {
324  }
325  printf("Retry %d failed\n", cnt);
326  fflush(stdout);
327  }
328  }
329 
330  throw TGo4EventErrorException(this);
331  break;
332  }
333  }
334 
335  TGo4Log::Error("MbsSource::NextEvent -- NEVER COME HERE");
336  return GetEventStatus();
337 }
338 
340 {
341  GO4TRACE((12,"TGo4MbsSource::Open()",__LINE__, __FILE__));
342 
343  if(fbIsOpen) return -1;
344  //std::cout << "Open of TGo4MbsSource"<< std::endl;
345  // open connection/file
346 
347  char name[5000];
348  strncpy(name, GetName(), sizeof(name));
349  int nport = fiPort;
350 
351  char* separ = strrchr(name, ':');
352  if ((nport<=0) && (separ!=0) && (GetMode() != GETEVT__FILE)) {
353  if ((sscanf(separ+1,"%d",&nport)==1) && (nport>0)) {
354  *separ = 0;
355  } else {
356  nport = fiPort;
357  }
358  }
359 
360  Int_t status = f_evt_source_port(nport);
361  SetCreateStatus(status);
363  char buffer[TGo4EventSource::fguTXTLEN];
364  f_evt_error(GetCreateStatus(), buffer, 1); // provide text message for later output
365  SetErrMess(Form("%s name:%s port:%d", buffer, name, nport));
366  throw TGo4EventErrorException(this);
367  }
368 
369  void* headptr= &fxInfoHeader; // suppress type-punned pointer warning
370  f_evt_timeout(fxInputChannel, fiTimeout); // have to set timeout before open now JAM
371  status = f_evt_get_open(
372  fiMode,
373  name,
375  (Char_t**) headptr,
376  0,
377  0);
378  SetCreateStatus(status);
380  char buffer[TGo4EventSource::fguTXTLEN];
381  f_evt_error(GetCreateStatus(),buffer,1); // provide text message for later output
382  SetErrMess(Form("%s name:%s", buffer, GetName()));
383  fbIsOpen = kFALSE;
384  throw TGo4EventErrorException(this);
385  } else {
386  TGo4Log::Debug(" Mbs Source -- opened input from type %d: %s . Timeout=%d s",
387  fiMode, GetName(), fiTimeout);
388  fbIsOpen = kTRUE;
389  }
390  return status;
391 }
392 
394 {
395  GO4TRACE((12,"TGo4MbsSource::Close()",__LINE__, __FILE__));
396  if(!fbIsOpen) return -1;
397  //std::cout << "Close of TGo4MbsSource"<< std::endl;
398  Int_t rev = GetCreateStatus();
399  // close connection/file
400  if(rev == GETEVT__SUCCESS) {
402  fbIsOpen=kFALSE;
403  }
404 
405  return rev;
406 }
407 
409 {
411 }
#define GETEVT__SUCCESS
Definition: f_evt.h:138
void SetCount(Int_t count)
Definition: TGo4MbsEvent.h:148
TGo4MbsSubEvent * AddSubEvent(Int_t fullID, Short_t *source, Int_t datalength, Bool_t copydata=kFALSE)
INTS4 f_evt_type(s_bufhe *ps_bufhe, s_evhe *ps_evhe, INTS4 l_subid, INTS4 l_long, INTS4 l_hex, INTS4 l_data)
Definition: f_evt.c:306
s_evt_channel * fxInputChannel
INTS4 f_evt_get_close(s_evt_channel *ps_chan)
Definition: f_evt.c:1113
#define GETEVT__TIMEOUT
Definition: f_evt.h:147
void SetValid(Bool_t on)
INTS4 f_evt_get_open(INTS4 l_mode, CHARS *pc_server, s_evt_channel *ps_chan, CHARS **ps_info, INTS4 l_sample, INTS4 l_param)
Definition: f_evt.c:561
INTS4 f_evt_timeout(s_evt_channel *ps_chan, INTS4 l_sec)
Definition: f_evt.c:1858
s_filhe * fxInfoHeader
void SetCreateStatus(Int_t status)
Definition: s_evhe.h:32
const char * GetErrMess() const
s_evt_channel * f_evt_control()
Definition: f_evt.c:2845
INTS2 i_type
Definition: s_ve10_1.h:38
static void Warn(const char *text,...)
Definition: TGo4Log.cxx:296
Bool_t fbDataCopyMode
virtual Bool_t CheckEventClass(TClass *cl)
INTS4 f_evt_source_port(INTS4 l_port)
Definition: f_evt.c:495
INTS4 f_evt_error(INTS4 l_error, CHARS *pc_dest, INTS4 l_out)
Definition: f_evt.c:1610
#define GETEVT__EVENT
Definition: f_evt.h:130
#define GETEVT__NOMORE
Definition: f_evt.h:141
CHARS * f_evt_get_buffer_ptr(s_evt_channel *ps_chan)
Definition: f_evt.c:1955
virtual ~TGo4MbsSource()
ULong_t fuEventCounter
ULong_t fuEventInterval
INTS4 l_dlen
Definition: s_ve10_1.h:37
virtual Int_t NextEvent()
#define GETEVT__TRANS
Definition: f_evt.h:129
INTS4 l_dlen
Definition: s_ves10_1.h:37
void BuildMbsEvent(TGo4MbsEvent *target)
void SetErrMess(const char *txt)
Int_t GetMode() const
number of allowed reconnect retries
void SetTimeout(Int_t time)
INTS2 i_dummy
Definition: s_ve10_1.h:40
void SetSubtype(Short_t subtype)
Definition: TGo4MbsEvent.h:139
Int_t GetCreateStatus() const
void SetDummy(Short_t dummy)
Definition: TGo4MbsEvent.h:142
void SetDlen(Int_t dlen)
Definition: TGo4MbsEvent.h:131
s_bufhe * fxBuffer
static const UInt_t fguLONGBYCHAR
void SetType(Short_t type)
Definition: TGo4MbsEvent.h:136
#define GETEVT__REVSERV
Definition: f_evt.h:131
INTS2 i_trigger
Definition: s_ve10_1.h:41
static const UInt_t fguSHORTBYCHAR
virtual Int_t Close()
s_ve10_1 * fxEvent
static const UInt_t fguEVHEBYCHAR
static const Int_t fgiTIMEOUTDEFAULT
void SetType(Short_t type)
Int_t GetEventStatus() const
Bool_t fbFirstEvent
void SetEventStatus(Int_t status)
#define GO4TRACE(X)
Definition: TGo4Log.h:26
INTS4 l_dlen
Definition: s_evhe.h:34
virtual Int_t Open()
INTS2 i_type
Definition: s_ves10_1.h:38
ULong_t fuStartEvent
void SetPrintEvent(Int_t num=1, Int_t sid=-1, Int_t longw=1, Int_t hexw=1, Int_t dataw=0)
INTS2 i_subtype
Definition: s_ve10_1.h:39
virtual Bool_t BuildEvent(TGo4EventElement *dest)
ULong_t fuStopEvent
virtual s_bufhe * GetBufferHeader()
INTS4 f_evt_get_event(s_evt_channel *ps_chan, INTS4 **ppl_buffer, INTS4 **ppl_goobuf)
Definition: f_evt.c:923
TGo4MbsSubEvent * BuildMbsSubEvent(TGo4MbsEvent *target, Int_t fullID, Short_t *source, Int_t datalength)
void SetSubtype(Short_t subtype)
#define GETEVT__FILE
Definition: f_evt.h:127
static void Error(const char *text,...)
Definition: TGo4Log.cxx:309
TGo4MbsSourcePrintPar fxPrEventPar
static void Info(const char *text,...)
Definition: TGo4Log.cxx:283
INTS2 i_subtype
Definition: s_ves10_1.h:39
#define GETEVT__STREAM
Definition: f_evt.h:128
static const UInt_t fguLONGBYSHORT
INTS4 l_count
Definition: s_ve10_1.h:42
void SetTrigger(Short_t trigger)
Definition: TGo4MbsEvent.h:145
static void Debug(const char *text,...)
Definition: TGo4Log.cxx:270