00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "TGo4MbsRandom.h"
00015
00016
00017 #include "TRandom.h"
00018 #include <math.h>
00019
00020 #include "TGo4Log.h"
00021 #include "TGo4MbsRandomParameter.h"
00022
00023
00024 extern "C"
00025 {
00026 #include "random-coll.h"
00027 }
00028
00029 double TGo4MbsRandom::fgdPeak[] = { 200., 302., 653., 1024., 2800.};
00030 double TGo4MbsRandom::fgdSigma[] = { 10., 22., 153., 104., 38.};
00031 double TGo4MbsRandom::fgdPeak2[] = { 300., 402., 853., 2000., 3024.};
00032 double TGo4MbsRandom::fgdSigma2[] = { 22., 153., 104., 100., 20.};
00033
00034 TGo4MbsRandom::TGo4MbsRandom() :
00035 TGo4MbsSource(),
00036 fiDLen(0),
00037 fiNumSub(0),
00038 fiNumDat(0),
00039 fxEventMem(0)
00040 {
00041 GO4TRACE((15,"TGo4MbsRandom::TGo4MbsRandom()",__LINE__, __FILE__));
00042 }
00043
00044
00045 TGo4MbsRandom::TGo4MbsRandom(TGo4MbsRandomParameter* par) :
00046 TGo4MbsSource(par, 1),
00047 fiDLen(0),
00048 fiNumSub(0),
00049 fiNumDat(0),
00050 fxEventMem(0)
00051 {
00052 GO4TRACE((15,"TGo4MbsRandom::TGo4MbsRandom(TGo4MbsRandomParameter*)",__LINE__, __FILE__));
00053
00054 TGo4Log::Debug(" New Event Source MbsRandom %s: ",GetName());
00055
00056 Open();
00057 }
00058
00059 TGo4MbsRandom::TGo4MbsRandom(const char* name) :
00060 TGo4MbsSource(name , 1),
00061 fiDLen(0),
00062 fiNumSub(0),
00063 fiNumDat(0),
00064 fxEventMem(0)
00065 {
00066 GO4TRACE((15,"TGo4MbsRandom::TGo4MbsRandom(const char*)",__LINE__, __FILE__));
00067
00068 TGo4Log::Debug(" New Event Source MbsRandom %s: ",name);
00069 Open();
00070 }
00071
00072
00073 TGo4MbsRandom::~TGo4MbsRandom()
00074 {
00075 GO4TRACE((15,"TGo4MbsRandom::~TGo4MbsRandom()",__LINE__, __FILE__));
00076 Close();
00077 }
00078
00079 Int_t TGo4MbsRandom::NextEvent()
00080 {
00081 GO4TRACE((12,"TGo4MbsRandom::NextEvent()",__LINE__, __FILE__));
00082
00083 void* clearfield=(void*) (fxEvent+1);
00084 size_t clearlen=fiDLen*2 - 8;
00085 memset(clearfield,0, clearlen);
00086 fxEvent->l_count++;
00087
00088
00089
00090 fxEvent->l_dlen=0;
00091 s_ves10_1* subevt=(s_ves10_1*) clearfield;
00092 for(Int_t i=0;i<fiNumSub;++i)
00093 {
00094
00095 int l_val_num = (int)(get_int(1., 7.)+0.5);
00096 if(l_val_num>fiNumDat) l_val_num=fiNumDat;
00097
00098 subevt->i_type=10;
00099 subevt->i_subtype=1;
00100 subevt->h_subcrate=i+1;
00101 subevt->h_control=2*i;
00102 subevt->i_procid=4*i;
00103
00104 subevt->l_dlen=l_val_num*2+2;
00105 fxEvent->l_dlen+=(l_val_num*sizeof(Int_t)) / 2 ;
00106
00107
00108 Int_t* subdata= (Int_t*) (subevt+1);
00109
00110 for(Int_t j=0;j<l_val_num;++j)
00111 {
00112 *(subdata+j)=rand_event(j+1);
00113
00114 }
00115 subevt=(s_ves10_1*) (subdata+l_val_num);
00116 }
00117 fxEvent->l_dlen+=(sizeof(s_ve10_1)-sizeof(s_evhe)+fiNumSub*sizeof(s_ves10_1))/2;
00118
00119 return 0;
00120 }
00121
00122
00123 Int_t TGo4MbsRandom::Open()
00124 {
00125 GO4TRACE((12,"TGo4MbsRandom::Open()",__LINE__, __FILE__));
00126
00127 if(fbIsOpen) return -1;
00128
00129 get_rand_seed();
00130 fiNumSub = 2;
00131 fiNumDat = 16;
00132 fiDLen = (sizeof(s_ve10_1)-sizeof(s_evhe)+fiNumSub*(sizeof(s_ves10_1)+fiNumDat*sizeof(Int_t))) / 2 ;
00133
00134 fxEventMem = new Short_t[fiDLen+sizeof(s_evhe)];
00135 fxEvent = (s_ve10_1*) fxEventMem;
00136 fxEvent->l_dlen=fiDLen;
00137 fxEvent->i_subtype=1;
00138 fxEvent->i_type=10;
00139 fxEvent->i_trigger=1;
00140 fxEvent->l_count=0;
00141
00142 fbIsOpen = kTRUE;
00143
00144 return 0;
00145 }
00146
00147
00148 Int_t TGo4MbsRandom::Close()
00149 {
00150 GO4TRACE((12,"TGo4MbsRandom::Close()",__LINE__, __FILE__));
00151 if(!fbIsOpen) return -1;
00152 delete [] fxEventMem;
00153 fxEventMem = 0;
00154 fxEvent = 0;
00155 fbIsOpen = kFALSE;
00156 return 0;
00157 }
00158
00160
00161 void TGo4MbsRandom::get_rand_seed()
00162 {
00163
00164
00165
00166
00167
00168
00169
00170 gRandom->SetSeed(0);
00171 }
00172
00173
00174 double TGo4MbsRandom::gauss_rnd(double mean, double sigma)
00175 {
00176 static int iset=0;
00177 static double gset;
00178 double v1, v2, s, u1;
00179
00180 if(sigma < 0.)
00181 {
00182 v1 = gRandom->Rndm();
00183 return (log(1-v1)/sigma + mean);
00184 }
00185 else
00186 {
00187 if(iset == 0)
00188 {
00189 do
00190 {
00191
00192
00193
00194 v1 = 2.*gRandom->Rndm()-1.;
00195 v2 = 2.*gRandom->Rndm()-1.;
00196
00197 s = v1*v1+v2*v2;
00198 } while (s >= 1.0 || s == 0.0);
00199
00200 u1 = sigma*v1*(sqrt(-2.*log(s)/s)) + mean;
00201 gset = u1;
00202 iset = 1;
00203
00204 return (sigma*v2*(sqrt(-2.*log(s)/s)) + mean);
00205 }
00206 else
00207 {
00208 iset = 0;
00209 return gset;
00210 }
00211 }
00212 }
00213
00214
00215 double TGo4MbsRandom::get_int(double low, double high)
00216 {
00217
00218 return ((high-low)*gRandom->Rndm()+low);
00219 }
00220
00221
00222 long TGo4MbsRandom::rand_event(long choice)
00223 {
00224 int cnt;
00225 switch(choice)
00226 {
00227 case 1:
00228 cnt = (int)(get_int(0., (double)NUM_PEAK));
00229 return ((long)(gauss_rnd(fgdPeak[cnt], fgdSigma[cnt])));
00230 break;
00231 case 2:
00232 cnt = (int)(get_int(0., (double)NUM_PEAK));
00233 return ((long)(p_dNormal(fgdPeak2[cnt], fgdSigma2[cnt], &fuSeed)));
00234 break;
00235 case 3:
00236 return ((long)(4096*p_dUniform(&fuSeed)));
00237 break;
00238 case 4:
00239 return ((long)(gauss_rnd(0., -.001)));
00240 break;
00241 case 5:
00242 return ((long)(p_dExponential(100., &fuSeed)));
00243 break;
00244 case 6:
00245 cnt = (int)(get_int(0., (double)NUM_PEAK));
00246 return ((long)((p_dExponential(200., &fuSeed)) + gauss_rnd(fgdPeak[cnt], fgdSigma[cnt])));
00247 break;
00248 case 7:
00249 cnt = (int)(get_int(3., (double)NUM_PEAK));
00250 return ((long)((4096*p_dUniform(&fuSeed)) + gauss_rnd(fgdPeak[cnt], fgdSigma[cnt])));
00251 break;
00252
00253 default:
00254 return 0;
00255 break;
00256 }
00257
00258 return 0;
00259 }
00260
00261 s_bufhe * TGo4MbsRandom::GetBufferHeader()
00262 {
00263 return 0;
00264 }