Re: [ROOT] faster TF1::GetRandom()

From: Daniel De Marco (ddm@GE.INFN.IT)
Date: Wed Jan 15 2003 - 18:43:23 MET


* Daniel De Marco <ddm@ge.infn.it> [15/01/2003 11:02]:
> I'm using TF1::GetRandom() to get random numbers distributed as a blackbody
> spectrum. I need random numbers above some threshold so I do:
> 
> f->SetRange(min, max);  // assume f is a TF1* properly declared
>                         // min and max are the bounds for the random number
> f->GetRandom();
>
> to 20 times slower using GetRandom. Do I do something wrong? There's a way
> to speed up the generation?

I found that calling SetRange the internal arrays that TF1 uses to compute
the random numbers are deleted and need to be recalculated at the next call
to GetRandom. I overcome this problem changing TF1 and adding a method
        Double_t TF1::GetRandom(Double_t min, Double_t max)
that generates a random number between min and max.
The routine is equal to Double_t TF1::GetRandom() until the end where after
the line "// return random number" becomes:

// return random number
   Double_t dx = (fXmax-fXmin)/fNpx;
   Int_t nbinmin=(min-fXmin)/dx;
   Int_t nbinmax=(max-fXmin)/dx+1;

    Double_t pmin=fIntegral[nbinmin];
    Double_t pmax=fIntegral[nbinmax];

    Double_t x;
    do {
       Double_t r  = gRandom->Uniform(pmin,pmax);

       bin  = TMath::BinarySearch(fNpx,fIntegral,r);
       rr = r - fIntegral[bin];

       if(fGamma[bin])
          xx = (-fBeta[bin] +
        TMath::Sqrt(fBeta[bin]*fBeta[bin]+2*fGamma[bin]*rr))/fGamma[bin]; 
       else
          xx = rr/fBeta[bin];
       x = fAlpha[bin] + xx;
    } while(x<min || x>max);
   return x;


Maybe this can be useful to someone else so I posted it here.

Ciao, Daniel.

--ddm



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:08 MET