Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/Go4EventServer/TGo4MbsRandom.cxx

Go to the documentation of this file.
00001 //---------------------------------------------------------------
00002 //        Go4 Release Package v2.10-5 (build 21005) 
00003 //                      03-Nov-2005
00004 //---------------------------------------------------------------
00005 //       The GSI Online Offline Object Oriented (Go4) Project
00006 //       Experiment Data Processing at DVEE department, GSI
00007 //---------------------------------------------------------------
00008 //
00009 //Copyright (C) 2000- Gesellschaft f. Schwerionenforschung, GSI
00010 //                    Planckstr. 1, 64291 Darmstadt, Germany
00011 //Contact:            http://go4.gsi.de
00012 //----------------------------------------------------------------
00013 //This software can be used under the license agreements as stated
00014 //in Go4License.txt file which is part of the distribution.
00015 //----------------------------------------------------------------
00016 #include "TGo4MbsRandom.h"
00017 
00018 #include <stdlib.h>
00019 #include <unistd.h>
00020 #include <sys/time.h>
00021 #include <math.h>
00022 #include <iostream.h>
00023 
00024 #include "Go4EventServer/TGo4MbsRandomParameter.h"
00025 #include "Go4Log/TGo4Log.h"
00026 
00027 extern "C"
00028 {
00029 #include "random-coll.h"
00030 }
00031 
00032 
00033 double TGo4MbsRandom::fgdPeak[NUM_PEAK]   = { 200., 302., 653., 1024., 2800.};
00034 double TGo4MbsRandom::fgdSigma[NUM_PEAK]  = {  10.,  22., 153.,  104.,   38.};
00035 double TGo4MbsRandom::fgdPeak2[NUM_PEAK]  = { 300., 402., 853., 2000., 3024.};
00036 double TGo4MbsRandom::fgdSigma2[NUM_PEAK] = {  22., 153., 104.,  100.,   20.};
00037 
00038 
00039 TGo4MbsRandom::TGo4MbsRandom(TGo4MbsRandomParameter* par) : 
00040    TGo4MbsSource(par->GetName(), 1), fiDLen(0), fiNumSub(0), fiNumDat(0), fxEventMem(0)
00041 {
00042    TRACE((15,"TGo4MbsRandom::TGo4MbsRandom(TGo4MbsRandomParameter*)",__LINE__, __FILE__));
00043 
00044    TGo4Log::Debug(" New Event Source MbsRandom %s:  ",GetName());
00045    if(par)
00046       {
00047        // here we could specify special values of random source parameter
00048 
00049        }
00050   Open();
00051 }
00052 
00053 TGo4MbsRandom::TGo4MbsRandom(const char* name) : 
00054    TGo4MbsSource(name , 1), fiDLen(0), fiNumSub(0), fiNumDat(0), fxEventMem(0)
00055 {
00056    TRACE((15,"TGo4MbsRandom::TGo4MbsRandom(Text_t*)",__LINE__, __FILE__));
00057 
00058    TGo4Log::Debug(" New Event Source MbsRandom %s:  ",name);
00059    Open();
00060 }
00061 
00062 TGo4MbsRandom::TGo4MbsRandom()
00063 {
00064 TRACE((15,"TGo4MbsRandom::TGo4MbsRandom()",__LINE__, __FILE__));
00065 
00066 }
00067 
00068 
00069 TGo4MbsRandom::~TGo4MbsRandom()
00070 {
00071 TRACE((15,"TGo4MbsRandom::~TGo4MbsRandom()",__LINE__, __FILE__));
00072 Close();
00073 }
00074 
00075 
00076 
00077 Int_t TGo4MbsRandom::NextEvent()
00078 {
00079 TRACE((12,"TGo4MbsRandom::NextEvent()",__LINE__, __FILE__));
00080 // here we fill mbs event structure at fxEvent ptr.
00081 void* clearfield=(void*) (fxEvent+1); // we skip header and clear the rest
00082 size_t clearlen=fiDLen*2 - 8;
00083 memset(clearfield,0, clearlen); // clear old values
00084 fxEvent->l_count++;
00085 //cout <<"Eventcount "<<fxEvent->l_count << endl;
00086 //cout <<" Cleared "<< clearlen<<" bytes at "<<clearfield << endl;
00087 // now fill in some new values:
00088 fxEvent->l_dlen=0;
00089 s_ves10_1* subevt=(s_ves10_1*) clearfield;
00090 for(Int_t i=0;i<fiNumSub;++i)
00091    {
00092       //cout <<"\tSubevt "<<i <<" at "<< subevt<< endl;
00093       int l_val_num = (int)(get_int(1., 7.)+0.5); // random number for number of data longwords
00094       if(l_val_num>fiNumDat) l_val_num=fiNumDat; // never exceed allocated field
00095       // setup subevent header:
00096       subevt->i_type=10;
00097       subevt->i_subtype=1;
00098       subevt->h_subcrate=i+1; // set subevent id numbers:
00099       subevt->h_control=2*i;
00100       subevt->i_procid=4*i;
00101       //subevt->l_dlen=fiNumDat*2+2; // length in short units + 2
00102       subevt->l_dlen=l_val_num*2+2; // subevent length in short units + 2
00103       fxEvent->l_dlen+=(l_val_num*sizeof(Int_t)) / 2 ; // add datalength to total length in shorts
00104 
00105       //cout <<"\t dlen="<<subevt->l_dlen << endl;
00106       Int_t* subdata= (Int_t*) (subevt+1); // data starts after subevt
00107       //cout <<"\t data="<<subdata << endl;
00108       for(Int_t j=0;j<l_val_num;++j)
00109          {
00110              *(subdata+j)=rand_event(j+1); // later use random generator here
00111              //cout <<"\t\t"<<"filled "<<j<<" with "<<*(subdata+j) <<"at "<<(subdata+j) << endl;
00112          } // for (... numdat ...)
00113       subevt=(s_ves10_1*) (subdata+l_val_num); // next subheader after last data
00114    } // for(...numsub)
00115    fxEvent->l_dlen+=(sizeof(s_ve10_1)-sizeof(s_evhe)+fiNumSub*sizeof(s_ves10_1))/2;
00116 // finally, add length of headers  to totalevent length
00117 return 0;
00118 }
00119 
00120 
00121 Int_t TGo4MbsRandom::Open()
00122 {
00123 TRACE((12,"TGo4MbsRandom::Open()",__LINE__, __FILE__));
00124 //
00125 if(fbIsOpen)
00126    return -1;
00127 
00128 get_rand_seed();
00129 fiNumSub=2; // number of subevents, fix
00130 fiNumDat=16; // maximum allocated data longs per subevent
00131 fiDLen=(sizeof(s_ve10_1)-sizeof(s_evhe)+fiNumSub*(sizeof(s_ves10_1)+fiNumDat*sizeof(Int_t))) / 2 ;
00132    // fiDLen is not in char (=size_t), but short units
00133 fxEventMem =new Short_t[fiDLen+sizeof(s_evhe)];
00134 fxEvent= (s_ve10_1*) fxEventMem;
00135 fxEvent->l_dlen=fiDLen;
00136 fxEvent->i_subtype=1;
00137 fxEvent->i_type=10;
00138 fxEvent->i_trigger=1; 
00139 fxEvent->l_count=0;
00140 return 0;
00141 }
00142 
00143 
00144 Int_t TGo4MbsRandom::Close()
00145 {
00146 TRACE((12,"TGo4MbsRandom::Close()",__LINE__, __FILE__));
00147 if(!fbIsOpen)
00148    return -1;
00149 delete [] fxEventMem;
00150 fxEvent=0;
00151 return 0;
00152 }
00153 
00155 
00156 void TGo4MbsRandom::get_rand_seed()                         /* obtains seed for generator      */
00157 {
00158   struct timeval tv;
00159   struct timezone tz;
00160 
00161   gettimeofday(&tv, &tz);          /* may not be supported in your C  */
00162   fuSeed=tv.tv_sec + tv.tv_usec;     /* if not, replace with clock()    */
00163   srand48(fuSeed);
00164 
00165 }
00166 
00167 
00168 double TGo4MbsRandom::gauss_rnd(double mean, double sigma)
00169 {
00170   static int iset=0;
00171   static double gset;
00172   double v1, v2, s, u1;
00173 
00174   if(sigma < 0.)
00175     {
00176       v1 = drand48();
00177       return (log(1-v1)/sigma + mean);
00178     }
00179   else
00180     {
00181       if(iset == 0)
00182    {
00183      do
00184        {
00185          v1 = 2.*drand48()-1.;
00186          v2 = 2.*drand48()-1.;
00187 
00188          s = v1*v1+v2*v2;
00189        } while (s >= 1.0 || s == 0.0);
00190 
00191      u1 = sigma*v1*(sqrt(-2.*log(s)/s)) + mean;
00192      gset = u1;
00193      iset = 1;
00194 
00195      return (sigma*v2*(sqrt(-2.*log(s)/s)) + mean);
00196    }
00197       else
00198    {
00199      iset = 0;
00200      return gset;
00201    }
00202     }
00203 }
00204 
00205 
00206 double TGo4MbsRandom::get_int(double low, double high)
00207 {
00208   return ((high-low)*drand48()+low);
00209 }
00210 
00211 
00212 long TGo4MbsRandom::rand_event(long choice)
00213 {
00214   int cnt;
00215   switch(choice)
00216     {
00217   case 1:
00218     cnt = (int)(get_int(0., (double)NUM_PEAK));
00219     return ((long)(gauss_rnd(fgdPeak[cnt], fgdSigma[cnt])));
00220     break;
00221   case 2:
00222     cnt = (int)(get_int(0., (double)NUM_PEAK));
00223     return ((long)(p_dNormal(fgdPeak2[cnt], fgdSigma2[cnt], &fuSeed)));
00224     break;
00225   case 3:
00226     return ((long)(4096*p_dUniform(&fuSeed)));
00227     break;
00228   case 4:
00229     return ((long)(gauss_rnd(0., -.001)));
00230     break;
00231   case 5:
00232     return ((long)(p_dExponential(100., &fuSeed)));
00233     break;
00234   case 6:
00235     cnt = (int)(get_int(0., (double)NUM_PEAK));
00236     return ((long)((p_dExponential(200., &fuSeed)) + gauss_rnd(fgdPeak[cnt], fgdSigma[cnt])));
00237     break;
00238   case 7:
00239     cnt = (int)(get_int(3., (double)NUM_PEAK));
00240     return ((long)((4096*p_dUniform(&fuSeed)) + gauss_rnd(fgdPeak[cnt], fgdSigma[cnt])));
00241     break;
00242 
00243   default:
00244     return 0;
00245     break;
00246     }
00247 
00248   return 0;
00249 }
00250 
00251 
00252 s_bufhe * TGo4MbsRandom::GetBufferHeader()
00253 {
00254    return 0;
00255 }
00256 
00257 ClassImp(TGo4MbsRandom)
00258 
00259 //----------------------------END OF GO4 SOURCE FILE ---------------------

Generated on Tue Nov 8 10:55:53 2005 for Go4-v2.10-5 by doxygen1.2.15