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