DABC (Data Acquisition Backbone Core)  2.9.9
GeneratorInput.cxx
Go to the documentation of this file.
1 // $Id: GeneratorInput.cxx 4479 2020-04-15 14:30:52Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #include "mbs/GeneratorInput.h"
17 
18 #include <cstdlib>
19 #include <cmath>
20 
21 #include "mbs/Iterator.h"
22 
23 
24 double Gauss_Rnd(double mean, double sigma)
25 {
26 
27  double x, y, z;
28 
29 // srand(10);
30 
31  z = 1.* rand() / RAND_MAX;
32  y = 1.* rand() / RAND_MAX;
33 
34  x = z * 6.28318530717958623;
35  return mean + sigma*sin(x)*sqrt(-2*log(y));
36 }
37 
38 
40  dabc::DataInput(),
41  fEventCount(0),
42  fNumSubevents(2),
43  fFirstProcId(0),
44  fSubeventSize(32),
45  fIsGo4RandomFormat(true),
46  fFullId(0),
47  fTotalSize(0),
48  fTotalSizeLimit(0),
49  fGenerTimeout(0.)
50 {
51  fEventCount = url.GetOptionInt("first", 0);
52 
53  fNumSubevents = url.GetOptionInt("numsub", 2);
54  fFirstProcId = url.GetOptionInt("procid", 0);
55  fSubeventSize = url.GetOptionInt("size", 32);
56  fIsGo4RandomFormat = url.GetOptionBool("go4", true);
57  fFullId = url.GetOptionInt("fullid", 0);
58  fTotalSizeLimit = url.GetOptionInt("total", 0);
59  fGenerTimeout = url.GetOptionInt("tmout", 0)*0.001;
60  fTimeoutSwitch = false;
61 }
62 
64 {
65  return dabc::DataInput::Read_Init(wrk,cmd);
66 }
67 
69 {
70  if (fGenerTimeout > 0.) {
71  fTimeoutSwitch = !fTimeoutSwitch;
72  if (fTimeoutSwitch) return dabc::di_RepeatTimeOut;
73  }
74 
75  if ((fTotalSizeLimit>0) && (fTotalSize / 1024. / 1024. > fTotalSizeLimit)) return dabc::di_EndOfStream;
76 
77  return dabc::di_DfltBufSize;
78 }
79 
80 
82 {
83 
84  mbs::WriteIterator iter(buf);
85 
86  while (iter.NewEvent(fEventCount)) {
87 
88  bool eventdone = true;
89 
90  for (unsigned subcnt = 0; subcnt < fNumSubevents; subcnt++)
91  if (iter.NewSubevent(fSubeventSize, fFirstProcId + subcnt + 1, fFirstProcId + subcnt)) {
92 
93  if ((fFullId!=0) && (fNumSubevents==1))
94  iter.subevnt()->fFullId = fFullId;
95 
96  unsigned subsz = fSubeventSize;
97 
98  uint32_t* value = (uint32_t*) iter.rawdata();
99 
100  if (fIsGo4RandomFormat) {
101  unsigned numval = fSubeventSize / sizeof(uint32_t);
102  for (unsigned nval=0;nval<numval;nval++)
103  *value++ = (uint32_t) Gauss_Rnd(nval*100 + 2000, 500./(nval+1));
104 
105  subsz = numval * sizeof(uint32_t);
106  } else {
107  if (subsz>0) *value++ = fEventCount;
108  if (subsz>4) *value++ = fFirstProcId + subcnt;
109  }
110 
111  iter.FinishSubEvent(subsz);
112  } else {
113  eventdone = false;
114  break;
115  }
116 
117  if (!eventdone) break;
118 
119  if (!iter.FinishEvent()) break;
120 
121  fEventCount++;
122  }
123 
124  // When close iterator - take back buffer with correctly modified length field
125  buf = iter.Close();
126 
127  fTotalSize += buf.GetTotalSize();
128 
129  return dabc::di_Ok;
130 }
double Gauss_Rnd(double mean, double sigma)
Reference on memory from memory pool.
Definition: Buffer.h:135
BufferSize_t GetTotalSize() const
Return total size of all buffer segments.
Definition: Buffer.cxx:91
Represents command with its arguments.
Definition: Command.h:99
virtual bool Read_Init(const WorkerRef &wrk, const Command &cmd)
Initialize data input, using port and command.
Definition: DataIO.h:82
Uniform Resource Locator interpreter.
Definition: Url.h:33
bool GetOptionBool(const std::string &optname, bool dflt=false) const
Definition: Url.cxx:314
int GetOptionInt(const std::string &optname, int dflt=0) const
Definition: Url.cxx:290
Reference on dabc::Worker
Definition: Worker.h:466
uint32_t fEventCount
current event id
bool fIsGo4RandomFormat
is subevents should be filled with random numbers
virtual unsigned Read_Complete(dabc::Buffer &buf)
Complete reading of the buffer from source,.
virtual unsigned Read_Size()
Defines required buffer size for next operation.
uint32_t fSubeventSize
size of each subevent
uint16_t fNumSubevents
number of subevents to generate
double fGenerTimeout
timeout used to avoid 100% CPU load
uint16_t fFirstProcId
procid of first subevent
uint32_t fFullId
subevent id, if number subevents==1 and nonzero
GeneratorInput(const dabc::Url &url)
uint64_t fTotalSizeLimit
limit of generated events size
virtual bool Read_Init(const dabc::WorkerRef &wrk, const dabc::Command &cmd)
Initialize data input, using port and command.
bool fTimeoutSwitch
boolean used to generate timeouts
Write iterator for MBS events/subevents.
Definition: Iterator.h:97
bool FinishSubEvent(uint32_t rawdatasz=0)
Definition: Iterator.cxx:378
SubeventHeader * subevnt() const
Definition: Iterator.h:157
bool NewSubevent(uint32_t minrawsize=0, uint8_t crate=0, uint16_t procid=0, uint8_t control=0)
Definition: Iterator.cxx:329
void * rawdata() const
Definition: Iterator.h:158
bool NewEvent(EventNumType event_number=0, uint32_t subeventsize=0)
Definition: Iterator.cxx:304
dabc::Buffer Close()
Definition: Iterator.cxx:237
Event manipulation API.
Definition: api.h:23
@ di_Ok
Definition: DataIO.h:38
@ di_DfltBufSize
Definition: DataIO.h:42
@ di_RepeatTimeOut
Definition: DataIO.h:36
@ di_EndOfStream
Definition: DataIO.h:37