DABC (Data Acquisition Backbone Core)  2.9.9
TSaftProc.cxx
Go to the documentation of this file.
1 // $Id: TSaftProc.cxx 2208 2014-04-02 15:30:23Z 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 "TSaftProc.h"
15 
16 #include <cstdlib>
17 #include <cstdio>
18 #include <sstream>
19 
20 #include "TH1.h"
21 #include "TH2.h"
22 #include "TCutG.h"
23 #include "TCanvas.h"
24 #include "TLine.h"
25 
26 #include "TGo4MbsEvent.h"
27 #include "TGo4WinCond.h"
28 #include "TGo4PolyCond.h"
29 #include "TGo4CondArray.h"
30 #include "TGo4Picture.h"
31 #include "TGo4StepFactory.h"
32 #include "TGo4Analysis.h"
33 #include "TGo4Version.h"
34 
35 
36 #include "../saftdabc/Definitions.h"
37 #include "../../hadaq/hadaq/defines.h"
38 
39 
40 #define SAFT_PROCID 0xa
41 
42 
43 //extern "C"
44 //{
45 // INTS4 f_ut_utime(INTS4 l_sec, INTS4 l_msec, CHARS *pc_time);
46 //}
47 //
48 //#include "f_ut_utime.h"
49 
50 
51 
52 //***********************************************************
53 TSaftProc::TSaftProc() : TGo4EventProcessor(), fLastEventNumber(0), fLastTime(0),fLastTimeDeadline(0), fLastFlipTime(0)
54 {
55 
56 
57 }
58 
59 //***********************************************************
61 {
62  TGo4Log::Info("TSaftProc: Delete instance");
63 }
64 
65 //***********************************************************
66 // this one is used in standard factory
67 TSaftProc::TSaftProc(const char* name) : TGo4EventProcessor(name),fLastEventNumber(0), fLastTime(0)
68 {
69  TGo4Log::Info("TSaftProc: Create instance %s", name);
70  hDeltaN=MakeTH1('I',"DeltaN","MBS Eventnumber difference", 1000, 0,1000);
71  hDeltaT=MakeTH1('I',"DeltaT","Timing Events execution difference", 1000000, 0, 1000000,"us","N"); // resolution is milliseconds
72  hDeltaT_deadline=MakeTH1('I',"DeltaT_deadline","Timing Events deadline difference", 1000000, 0, 1000000,"us","N");
73 
74  hDeltaT_coarse=MakeTH1('I',"DeltaT_coarse","Coarse Timing Events execution difference",
75  10000, 0, 1.0e12,"ns","N"); // 100s range, 10 ms resolution
76 
77 
78  hDeltaT_deadline_fine=MakeTH1('I',"DeltaT_deadline_fine","Timing Events deadline fine difference", 1000000, 0, 1000000,"ns","N");
79 
80  hLostSequence=MakeTH1('I',"LostEdges","Lost rising or falling edges",2,0,2);
81  if(IsObjMade())
82  {
83  hLostSequence->GetXaxis()->SetBinLabel(1,"Rising");
84  hLostSequence->GetXaxis()->SetBinLabel(2,"Falling");
85  }
86 
87 
88  hOverflowCounts=MakeTH1('I',"OverflowCounts","Relative Action sink overflows",1000,0,1000);
89  hOverflowScaler=MakeTH1('I',"OverflowScaler","Action sink Overflow sum",2,0,2);
90  if(IsObjMade())
91  {
92  hOverflowScaler->GetXaxis()->SetBinLabel(1,"No loss");
93  hOverflowScaler->GetXaxis()->SetBinLabel(2,"Overflows");
94  }
95 
96 
97  fPar=dynamic_cast<TSaftParam*>(MakeParameter("SaftParam", "TSaftParam", "set_SaftParam.C"));
98 
99 }
100 
101 
102 
103 //-----------------------------------------------------------
104 // event function
105 Bool_t TSaftProc::BuildEvent(TGo4EventElement*)
106 { // called by framework. We dont fill any output event here at all
107  static saftdabc::Timing_Event lastEvent;
108 
109  if ((GetInputEvent()==0) || (GetInputEvent()->IsA() != TGo4MbsEvent::Class())) {
110  TGo4Log::Error("TSaftProc: no input MBS event found!");
111  return kFALSE;
112  }
113 
114  TGo4MbsEvent* evnt = (TGo4MbsEvent*) GetInputEvent();
115 
116  if(evnt->GetTrigger() > 11) {
117  TGo4Log::Info("TSaftProc: Skip trigger event");
118  return kFALSE;
119  }
120 
121 
122 
123  evnt->ResetIterator();
124  TGo4MbsSubEvent *psubevt(0);
125  while((psubevt = evnt->NextSubEvent()) != 0) { // loop over subevents
126 
127  //if (psubevt->GetProcid() != SAFT_PROCID) return kFALSE;
128  if (psubevt->GetFullId() != fPar->fSubeventID) return kFALSE;
129 
130  Int_t* pdata = psubevt->GetDataField();
131  Int_t* theEnd = psubevt->GetDataField() + psubevt->GetIntLen();
132  Int_t* evtEnd = theEnd;
133  Int_t* evtPaddedEnd = theEnd;
134  Int_t* subEnd = theEnd;
135  Int_t* subPaddedEnd = theEnd;
136  hadaq::RawEvent* hadevt=0;
137  hadaq::RawSubevent* hadsub =0;
138 
139  if(fPar->fHadaqMode)
140  {
141  // skip hades event and subevent headers:
142  hadevt = (hadaq::RawEvent*) pdata;
143  pdata += sizeof(hadaq::RawEvent)/sizeof(Int_t);
144  hadsub = (hadaq::RawSubevent*) pdata;
145  pdata += sizeof(hadaq::RawSubevent)/sizeof(Int_t);
146  // adjust end of payload with respect to hades padding
147  evtEnd= (Int_t*)( ((char*) hadevt) + hadevt->GetSize());
148  evtPaddedEnd= (Int_t*)( ((char*) hadevt) + hadevt->GetPaddedSize());
149  subEnd=(Int_t*)( ((char*) hadsub) + hadsub->GetSize());
150  subPaddedEnd=(Int_t*)( ((char*) hadsub) + hadsub->GetPaddedSize());
151  if(fPar->fVerbose)
152  {
153  printf("TSaftProc::BuildEvent sees first hadaq event in buffer of size %d, padded:%d, subevt size:%d padded:%d\n",
154  hadevt->GetSize(), hadevt->GetPaddedSize(),hadsub->GetSize(), hadsub->GetPaddedSize());
155  std::cout << std::endl;
156  }
157  }
158 
159 
160 
161 
162  char buf[1024];
163  saftdabc::Timing_Event theEvent;
164  uint64_t lo=0, hi=0;
165 
166  while(pdata<theEnd)
167  {
168  // check here for ends of hades events and subevents in mbs:
169  if(fPar->fHadaqMode)
170  {
171  if(pdata==evtEnd)
172  {
173 
174 
175 
176  // new hades event in buffer:
177  pdata=evtPaddedEnd+1;
178  if(pdata>=theEnd) break;
179  hadevt = (hadaq::RawEvent*) pdata;
180  pdata += sizeof(hadaq::RawEvent)/sizeof(Int_t);
181  hadsub = (hadaq::RawSubevent*) pdata;
182  pdata += sizeof(hadaq::RawSubevent)/sizeof(Int_t);
183  evtEnd= (Int_t*)( ((char*) hadevt) + hadevt->GetSize());
184  evtPaddedEnd= (Int_t*)( ((char*) hadevt) + hadevt->GetPaddedSize());
185  subEnd=(Int_t*)( ((char*) hadsub) + hadsub->GetSize());
186  subPaddedEnd=(Int_t*)( ((char*) hadsub) + hadsub->GetPaddedSize());
187  if(fPar->fVerbose)
188  {
189  printf("TSaftProc::BuildEvent sees new hadaq event of size %d, padded:%d, subevt size:%d padded:%d\n",
190  hadevt->GetSize(), hadevt->GetPaddedSize(),hadsub->GetSize(), hadsub->GetPaddedSize());
191  std::cout << std::endl;
192  }
193  continue;
194  }
195 
196  if(pdata==subEnd)
197  {
198  // new hades subevent in buffer
199  pdata=subPaddedEnd+1;
200  if(pdata>=theEnd) break;
201  hadsub = (hadaq::RawSubevent*) pdata;
202  pdata += sizeof(hadaq::RawSubevent)/sizeof(Int_t);
203  subEnd=(Int_t*)( ((char*) hadsub) + hadsub->GetSize());
204  subPaddedEnd=(Int_t*)( ((char*) hadsub) + hadsub->GetPaddedSize());
205  if(fPar->fVerbose)
206  {
207  printf("TSaftProc::BuildEvent sees new hadaq subevent of size:%d padded:%d\n",
208  hadsub->GetSize(), hadsub->GetPaddedSize());
209  std::cout << std::endl;
210  }
211 
212  continue;
213  }
214 
215  }
216 
217 
218 
219 
220  lo=(uint64_t)(*pdata++);
221  hi=(uint64_t)(*pdata++);
222  theEvent.fEvent = (hi<<32) | (lo & 0xFFFFFFFF);
223  lo=(uint64_t)(*pdata++);
224  hi=(uint64_t)(*pdata++);
225  theEvent.fParam = (hi<<32) | (lo & 0xFFFFFFFF);
226  lo=(uint64_t)(*pdata++);
227  hi=(uint64_t)(*pdata++);
228  theEvent.fDeadline = (hi<<32) | (lo & 0xFFFFFFFF);
229  lo=(uint64_t)(*pdata++);
230  hi=(uint64_t)(*pdata++);
231  theEvent.fExecuted = (hi<<32) | (lo & 0xFFFFFFFF);
232  lo=(uint64_t)(*pdata++);
233  hi=(uint64_t)(*pdata++);
234  theEvent.fFlags = (hi<<32) | (lo & 0xFFFFFFFF);
235  lo=(uint64_t)(*pdata++);
236  hi=(uint64_t)(*pdata++);
237  theEvent.fOverflows = (hi<<32) | (lo & 0xFFFFFFFF);
238 
239 
240  snprintf(theEvent.fDescription,SAFT_DABC_DESCRLEN, "%s", (const char*)(pdata));
241  if(theEvent.InfoMessage(buf,1024)<0)
242  {
243  printf("Problem with info message!");
244  pdata++;
245  continue;
246  }
247  if(fPar->fVerbose)
248  {
249  printf("TSaftProc::BuildEvent of event %d sees %s\n",evnt->GetCount(), buf);
250  printf("Date: %s",TSaftProc::FormatDate(theEvent.fExecuted, GO4_PMODE_VERBOSE).c_str());
251 
252  std::cout << std::endl;
253  }
254 
256 // if(strstr(buf,"IO1")==0)
257 // {
258 // printf("TSaftProc::BuildEvent unexpected timing event %s",buf);
259 // std::cout << std::endl;
260 // }
262  // here histograms of delta t and event numbers
263 
264  if(fLastTime>0)
265  {
266  uint64_t delta=theEvent.fExecuted - fLastTime;
267  hDeltaT_coarse->Fill(delta);
268  Double_t deltaus=delta/1.0e3; // ns to microseconds
269  hDeltaT->Fill(deltaus);
270 
271  uint64_t deltaDead=theEvent.fDeadline - fLastTimeDeadline;
272  Double_t deltadeadaus=deltaDead/1.0e3; // ns to microseconds
273  hDeltaT_deadline->Fill(deltadeadaus);
274 
275 
276 
277  hDeltaT_deadline_fine->Fill(deltaDead);
278  // DEBUG big deltas:
279  if(delta>1.0e+9)
280  {
281  printf("******** Found delta= 0x%lx units (%ld ns) (%E us)\n", delta, delta, deltaus);
282  printf("** %s\n",buf);
283  printf("** Last time:0x%lx\n",fLastTime);
284  lastEvent.InfoMessage(buf,1024);
285  printf("** Last Event: %s\n",buf);
286  uint64_t deltaflip=fLastTime - fLastFlipTime;
287  printf("** Delta since last flip=0x%lx units\n",deltaflip);
288  printf("*************************************");
289  std::cout << std::endl;
290 
291 
293  }
294 
295  if((lastEvent.fEvent & 0x1) == (theEvent.fEvent & 0x1))
296  {
297  // same type of input edges (rising or falling) since last event. not possible with pulser test
298 
299  if(theEvent.fEvent & 0x1)
300  hLostSequence->Fill(1); // we are rising edge and have lost a falling edge before
301  else
302  hLostSequence->Fill(0); // we are falling edge and have lost a rising before
303  }
304 
305  if(theEvent.fOverflows)
306  {
307  hOverflowCounts->Fill(theEvent.fOverflows);
308  hOverflowScaler->Fill(1,theEvent.fOverflows);
309  }
310  else
311  {
312  hOverflowScaler->Fill(0);
313  }
314 
315  } //if(fLastTime>0)
316  fLastTime=theEvent.fExecuted;
317  fLastTimeDeadline=theEvent.fDeadline;
318  lastEvent=theEvent;
319 
320 
321 
322  // skip size of description text:
323  int textsize=SAFT_DABC_DESCRLEN/sizeof(Int_t);
324  while(textsize--)
325  {
326  pdata++;
327  }
328 
329  } // while pdata
330 
331  }// while((psubevt
332 
333 
334  if(fLastEventNumber>0)
335  {
336  unsigned deltaN=evnt->GetCount() - fLastEventNumber;
337  hDeltaN->Fill(deltaN);
338  }
339  fLastEventNumber=evnt->GetCount();
340  return kTRUE;
341 }
342 
343 
345 std::string TSaftProc::FormatDate(uint64_t time, uint32_t pmode)
346 {
347  uint64_t ns = time % 1000000000;
348  time_t s = time / 1000000000;
349  struct tm *tm = gmtime(&s);
350  char date[40];
351  char full[80];
352  std::string temp;
353 
354  if (pmode & GO4_PMODE_VERBOSE) {
355  strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", tm);
356  snprintf(full, sizeof(full), "%s.%09ld", date, (long)ns);
357  }
358  else if (pmode & GO4_PMODE_DEC)
359  snprintf(full, sizeof(full), "0d%lu.%09ld",s,(long)ns);
360  else
361  snprintf(full, sizeof(full), "0x%016llx", (unsigned long long)time);
362 
363  temp = full;
364 
365  return temp;
366 } //tr_formatDate
367 
#define GO4_PMODE_DEC
Definition: TSaftProc.h:29
#define GO4_PMODE_VERBOSE
Definition: TSaftProc.h:31
Int_t fSubeventID
Definition: TSaftParam.h:31
Bool_t fVerbose
Definition: TSaftParam.h:29
Bool_t fHadaqMode
Definition: TSaftParam.h:33
TH1 * hDeltaN
sequence number diff of events
Definition: TSaftProc.h:53
TH1 * hDeltaT_deadline_fine
time difference of subsequent events, fine detail of deadline
Definition: TSaftProc.h:50
TH1 * hDeltaT
"executed" time difference of subsequent events
Definition: TSaftProc.h:41
unsigned fLastEventNumber
remember last mbs event number received
Definition: TSaftProc.h:66
static std::string FormatDate(uint64_t time, uint32_t pmode)
stolen from saftlib to check the real time stamp delivered:
Definition: TSaftProc.cxx:345
TH1 * hOverflowScaler
scaler for total overflow counter versus timing events without loss
Definition: TSaftProc.h:63
uint64_t fLastTime
remember last time stamp (executed) received
Definition: TSaftProc.h:69
TH1 * hLostSequence
counts lost events by checking leading/trailing edge alternation
Definition: TSaftProc.h:56
TH1 * hOverflowCounts
histogram of increase of overflow counter as taken from action sink.
Definition: TSaftProc.h:59
TH1 * hDeltaT_deadline
"deadline" time difference of subsequent events
Definition: TSaftProc.h:44
TSaftParam * fPar
Definition: TSaftProc.h:38
TH1 * hDeltaT_coarse
time difference of subsequent events, coarse range overview
Definition: TSaftProc.h:47
virtual ~TSaftProc()
Definition: TSaftProc.cxx:60
Bool_t BuildEvent(TGo4EventElement *)
Definition: TSaftProc.cxx:105
uint64_t fLastFlipTime
remember last time stamp of large deltat
Definition: TSaftProc.h:76
uint64_t fLastTimeDeadline
remember last time stamp (deadling) received
Definition: TSaftProc.h:73
The saftlib input event data structure.
Definition: Definitions.h:115
char fDescription[SAFT_DABC_DESCRLEN]
Definition: Definitions.h:125
int InfoMessage(char *str, size_t len)
Definition: Definitions.h:136
#define SAFT_DABC_DESCRLEN
length of descriptor text field in timing event structure
Definition: Definitions.h:50
uint32_t GetSize() const
Definition: defines.h:182
uint32_t GetPaddedSize() const
Definition: defines.h:184
Hadaq event structure.
Definition: defines.h:443
Hadaq subevent structure.
Definition: defines.h:262