GSI Object Oriented Online Offline (Go4)  GO4-6.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4MbsRandom.cxx
Go to the documentation of this file.
1 // $Id: TGo4MbsRandom.cxx 3062 2021-03-12 15:19:41Z 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 fuer 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 "TGo4MbsRandom.h"
15 
16 
17 #include "TRandom.h"
18 #include <cmath>
19 
20 #include "TGo4Log.h"
21 #include "TGo4MbsRandomParameter.h"
22 
23 
24 extern "C"
25 {
26 #include "random-coll.h"
27 }
28 
29 double TGo4MbsRandom::fgdPeak[] = { 200., 302., 653., 1024., 2800.};
30 double TGo4MbsRandom::fgdSigma[] = { 10., 22., 153., 104., 38.};
31 double TGo4MbsRandom::fgdPeak2[] = { 300., 402., 853., 2000., 3024.};
32 double TGo4MbsRandom::fgdSigma2[] = { 22., 153., 104., 100., 20.};
33 
35  TGo4MbsSource(),
36  fiDLen(0),
37  fiNumSub(0),
38  fiNumDat(0),
39  fxEventMem(0)
40 {
41  GO4TRACE((15,"TGo4MbsRandom::TGo4MbsRandom()",__LINE__, __FILE__));
42 }
43 
44 
46  TGo4MbsSource(par, 1),
47  fiDLen(0),
48  fiNumSub(0),
49  fiNumDat(0),
50  fxEventMem(0)
51 {
52  GO4TRACE((15,"TGo4MbsRandom::TGo4MbsRandom(TGo4MbsRandomParameter*)",__LINE__, __FILE__));
53 
54  TGo4Log::Debug(" New Event Source MbsRandom %s: ",GetName());
55 
56  Open();
57 }
58 
59 TGo4MbsRandom::TGo4MbsRandom(const char* name) :
60  TGo4MbsSource(name , 1),
61  fiDLen(0),
62  fiNumSub(0),
63  fiNumDat(0),
64  fxEventMem(0)
65 {
66  GO4TRACE((15,"TGo4MbsRandom::TGo4MbsRandom(const char*)",__LINE__, __FILE__));
67 
68  TGo4Log::Debug(" New Event Source MbsRandom %s: ",name);
69  Open();
70 }
71 
72 
74 {
75  GO4TRACE((15,"TGo4MbsRandom::~TGo4MbsRandom()",__LINE__, __FILE__));
76  Close();
77 }
78 
80 {
81  GO4TRACE((12,"TGo4MbsRandom::NextEvent()",__LINE__, __FILE__));
82  // here we fill mbs event structure at fxEvent ptr.
83  void* clearfield=(void*) (fxEvent+1); // we skip header and clear the rest
84  size_t clearlen=fiDLen*2 - 8;
85  memset(clearfield,0, clearlen); // clear old values
86  fxEvent->l_count++;
87  //std::cout <<"Eventcount "<<fxEvent->l_count << std::endl;
88  //std::cout <<" Cleared "<< clearlen<<" bytes at "<<clearfield << std::endl;
89  // now fill in some new values:
90  fxEvent->l_dlen=0;
91  s_ves10_1* subevt=(s_ves10_1*) clearfield;
92  for(Int_t i=0;i<fiNumSub;++i)
93  {
94  //std::cout <<"\tSubevt "<<i <<" at "<< subevt<< std::endl;
95  int l_val_num = (int)(get_int(1., 7.)+0.5); // random number for number of data longwords
96  if(l_val_num>fiNumDat) l_val_num=fiNumDat; // never exceed allocated field
97  // setup subevent header:
98  subevt->i_type=10;
99  subevt->i_subtype=1;
100  subevt->h_subcrate=i+1; // set subevent id numbers:
101  subevt->h_control=2*i;
102  subevt->i_procid=4*i;
103  //subevt->l_dlen=fiNumDat*2+2; // length in short units + 2
104  subevt->l_dlen=l_val_num*2+2; // subevent length in short units + 2
105  fxEvent->l_dlen+=(l_val_num*sizeof(Int_t)) / 2 ; // add datalength to total length in shorts
106 
107  //std::cout <<"\t dlen="<<subevt->l_dlen << std::endl;
108  Int_t* subdata= (Int_t*) (subevt+1); // data starts after subevt
109  //std::cout <<"\t data="<<subdata << std::endl;
110  for(Int_t j=0;j<l_val_num;++j)
111  {
112  *(subdata+j)=rand_event(j+1); // later use random generator here
113  //std::cout <<"\t\t"<<"filled "<<j<<" with "<<*(subdata+j) <<"at "<<(subdata+j) << std::endl;
114  } // for (... numdat ...)
115  subevt=(s_ves10_1*) (subdata+l_val_num); // next subheader after last data
116  } // for(...numsub)
117  fxEvent->l_dlen+=(sizeof(s_ve10_1)-sizeof(s_evhe)+fiNumSub*sizeof(s_ves10_1))/2;
118  // finally, add length of headers to totalevent length
119  return 0;
120 }
121 
122 
124 {
125  GO4TRACE((12,"TGo4MbsRandom::Open()",__LINE__, __FILE__));
126  //
127  if(fbIsOpen) return -1;
128 
129  get_rand_seed();
130  fiNumSub = 2; // number of subevents, fix
131  fiNumDat = 16; // maximum allocated data longs per subevent
132  fiDLen = (sizeof(s_ve10_1)-sizeof(s_evhe)+fiNumSub*(sizeof(s_ves10_1)+fiNumDat*sizeof(Int_t))) / 2 ;
133  // fiDLen is not in char (=size_t), but short units
134  fxEventMem = new Short_t[fiDLen+sizeof(s_evhe)];
136  fxEvent->l_dlen=fiDLen;
137  fxEvent->i_subtype=1;
138  fxEvent->i_type=10;
139  fxEvent->i_trigger=1;
140  fxEvent->l_count=0;
141 
142  fbIsOpen = kTRUE;
143 
144  return 0;
145 }
146 
147 
149 {
150  GO4TRACE((12,"TGo4MbsRandom::Close()",__LINE__, __FILE__));
151  if(!fbIsOpen) return -1;
152  delete [] fxEventMem;
153  fxEventMem = 0;
154  fxEvent = 0;
155  fbIsOpen = kFALSE;
156  return 0;
157 }
158 
160 
161 void TGo4MbsRandom::get_rand_seed() /* obtains seed for generator */
162 {
163 // struct timeval tv;
164 // struct timezone tz;
165 
166 // gettimeofday(&tv, &tz); /* may not be supported in your C */
167 // fuSeed=tv.tv_sec + tv.tv_usec; /* if not, replace with clock() */
168 // srand48(fuSeed);
169 
170  gRandom->SetSeed(0);
171 }
172 
173 
174 double TGo4MbsRandom::gauss_rnd(double mean, double sigma)
175 {
176  static int iset=0;
177  static double gset;
178  double v1, v2, s, u1;
179 
180  if(sigma < 0.)
181  {
182  v1 = gRandom->Rndm(); //drand48();
183  return (log(1-v1)/sigma + mean);
184  }
185  else
186  {
187  if(iset == 0)
188  {
189  do
190  {
191  //v1 = 2.*drand48()-1.;
192  //v2 = 2.*drand48()-1.;
193 
194  v1 = 2.*gRandom->Rndm()-1.;
195  v2 = 2.*gRandom->Rndm()-1.;
196 
197  s = v1*v1+v2*v2;
198  } while (s >= 1.0 || s == 0.0);
199 
200  u1 = sigma*v1*(sqrt(-2.*log(s)/s)) + mean;
201  gset = u1;
202  iset = 1;
203 
204  return (sigma*v2*(sqrt(-2.*log(s)/s)) + mean);
205  }
206  else
207  {
208  iset = 0;
209  return gset;
210  }
211  }
212 }
213 
214 
215 double TGo4MbsRandom::get_int(double low, double high)
216 {
217 // return ((high-low)*drand48()+low);
218  return ((high-low)*gRandom->Rndm()+low);
219 }
220 
221 
222 long TGo4MbsRandom::rand_event(long choice)
223 {
224  int cnt;
225  switch(choice) {
226  case 1:
227  cnt = (int)(get_int(0., (double)NUM_PEAK));
228  return ((long)(gauss_rnd(fgdPeak[cnt], fgdSigma[cnt])));
229  case 2:
230  cnt = (int)(get_int(0., (double)NUM_PEAK));
231  return ((long)(p_dNormal(fgdPeak2[cnt], fgdSigma2[cnt], &fuSeed)));
232  case 3:
233  return ((long)(4096*p_dUniform(&fuSeed)));
234  case 4:
235  return ((long)(gauss_rnd(0., -.001)));
236  case 5:
237  return ((long)(p_dExponential(100., &fuSeed)));
238  case 6:
239  cnt = (int)(get_int(0., (double)NUM_PEAK));
240  return ((long)((p_dExponential(200., &fuSeed)) + gauss_rnd(fgdPeak[cnt], fgdSigma[cnt])));
241  case 7:
242  cnt = (int)(get_int(3., (double)NUM_PEAK));
243  return ((long)((4096*p_dUniform(&fuSeed)) + gauss_rnd(fgdPeak[cnt], fgdSigma[cnt])));
244  }
245 
246  return 0;
247 }
248 
250 {
251  return 0;
252 }
double p_dUniform(unsigned *seed)
Definition: random-coll.c:126
virtual Int_t Open()
long rand_event(long choice)
CHARS h_subcrate
Definition: s_ves10_1.h:41
static double fgdSigma2[]
Definition: TGo4MbsRandom.h:81
double p_dNormal(double mean, double sigma, unsigned int *seed)
Definition: random-coll.c:422
#define NUM_PEAK
Definition: TGo4MbsRandom.h:19
Short_t * fxEventMem
Definition: TGo4MbsRandom.h:65
Definition: s_evhe.h:32
INTS2 i_procid
Definition: s_ves10_1.h:40
double gauss_rnd(double mean, double sigma)
INTS4 l_dlen
Definition: s_ve10_1.h:37
static double fgdSigma[]
Definition: TGo4MbsRandom.h:79
INTS4 l_dlen
Definition: s_ves10_1.h:37
virtual ~TGo4MbsRandom()
virtual Int_t NextEvent()
double p_dExponential(double lambda, unsigned int *seed)
Definition: random-coll.c:229
s_ve10_1 * fxEvent
virtual s_bufhe * GetBufferHeader()
virtual Int_t Close()
static double fgdPeak2[]
Definition: TGo4MbsRandom.h:80
#define GO4TRACE(X)
Definition: TGo4Log.h:26
INTS2 i_type
Definition: s_ves10_1.h:38
double get_int(double low, double high)
static double fgdPeak[]
Definition: TGo4MbsRandom.h:78
INTS2 i_subtype
Definition: s_ves10_1.h:39
INTS4 l_count
Definition: s_ve10_1.h:42
static void Debug(const char *text,...)
Definition: TGo4Log.cxx:284
CHARS h_control
Definition: s_ves10_1.h:42