Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

TGo4MbsRandom.cxx

Go to the documentation of this file.
00001 //-------------------------------------------------------------
00002 //        Go4 Release Package v3.04-01 (build 30401)
00003 //                      28-November-2008
00004 //---------------------------------------------------------------
00005 //   The GSI Online Offline Object Oriented (Go4) Project
00006 //   Experiment Data Processing at EE 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 <sys/time.h>
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        // here we could specify special values of random source parameter
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 // here we fill mbs event structure at fxEvent ptr.
00090 void* clearfield=(void*) (fxEvent+1); // we skip header and clear the rest
00091 size_t clearlen=fiDLen*2 - 8;
00092 memset(clearfield,0, clearlen); // clear old values
00093 fxEvent->l_count++;
00094 //cout <<"Eventcount "<<fxEvent->l_count << endl;
00095 //cout <<" Cleared "<< clearlen<<" bytes at "<<clearfield << endl;
00096 // now fill in some new values:
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       //cout <<"\tSubevt "<<i <<" at "<< subevt<< endl;
00102       int l_val_num = (int)(get_int(1., 7.)+0.5); // random number for number of data longwords
00103       if(l_val_num>fiNumDat) l_val_num=fiNumDat; // never exceed allocated field
00104       // setup subevent header:
00105       subevt->i_type=10;
00106       subevt->i_subtype=1;
00107       subevt->h_subcrate=i+1; // set subevent id numbers:
00108       subevt->h_control=2*i;
00109       subevt->i_procid=4*i;
00110       //subevt->l_dlen=fiNumDat*2+2; // length in short units + 2
00111       subevt->l_dlen=l_val_num*2+2; // subevent length in short units + 2
00112       fxEvent->l_dlen+=(l_val_num*sizeof(Int_t)) / 2 ; // add datalength to total length in shorts
00113 
00114       //cout <<"\t dlen="<<subevt->l_dlen << endl;
00115       Int_t* subdata= (Int_t*) (subevt+1); // data starts after subevt
00116       //cout <<"\t data="<<subdata << endl;
00117       for(Int_t j=0;j<l_val_num;++j)
00118          {
00119              *(subdata+j)=rand_event(j+1); // later use random generator here
00120              //cout <<"\t\t"<<"filled "<<j<<" with "<<*(subdata+j) <<"at "<<(subdata+j) << endl;
00121          } // for (... numdat ...)
00122       subevt=(s_ves10_1*) (subdata+l_val_num); // next subheader after last data
00123    } // for(...numsub)
00124    fxEvent->l_dlen+=(sizeof(s_ve10_1)-sizeof(s_evhe)+fiNumSub*sizeof(s_ves10_1))/2;
00125 // finally, add length of headers  to totalevent length
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; // number of subevents, fix
00139 fiNumDat=16; // maximum allocated data longs per subevent
00140 fiDLen=(sizeof(s_ve10_1)-sizeof(s_evhe)+fiNumSub*(sizeof(s_ves10_1)+fiNumDat*sizeof(Int_t))) / 2 ;
00141    // fiDLen is not in char (=size_t), but short units
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()                         /* obtains seed for generator      */
00166 {
00167 //  struct timeval tv;
00168 //  struct timezone tz;
00169 
00170 //  gettimeofday(&tv, &tz);          /* may not be supported in your C  */
00171 //  fuSeed=tv.tv_sec + tv.tv_usec;     /* if not, replace with clock()    */
00172 //  srand48(fuSeed);
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(); //drand48();
00187       return (log(1-v1)/sigma + mean);
00188     }
00189   else
00190     {
00191       if(iset == 0)
00192    {
00193      do
00194        {
00195          //v1 = 2.*drand48()-1.;
00196          //v2 = 2.*drand48()-1.;
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 //   return ((high-low)*drand48()+low);
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 //----------------------------END OF GO4 SOURCE FILE ---------------------

Generated on Fri Nov 28 12:59:08 2008 for Go4-v3.04-1 by  doxygen 1.4.2