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