Changeset 14743


Ignore:
Timestamp:
12/13/12 18:15:41 (12 years ago)
Author:
Jens Buss
Message:
changed random number generator to uniform numbers, add func
BootstrapVector, add func BootstrapTH1
File:
1 edited

Legend:

Unmodified
Added
Removed
  • fact/tools/rootmacros/PulseTemplates/Sample.C

    r14706 r14743  
    8686        int     max)
    8787{
    88     int rndNum = min + mRandom.Integer(max - min);
    89     cout << rndNum << endl;
     88//    int rndNum = min + mRandom.Integer(max - min);
     89    int rndNum = (int) mRandom.Uniform(min, max);
     90//    cout << rndNum << endl;
    9091    return rndNum;
    9192}
     
    166167}
    167168
     169
     170int
     171Sample::BootstrapVector(vector<double> *inVector, vector<double> *outVector)
     172{
     173    //get size of sample to be bootstrapped
     174    int sampleSize = inVector->size();
     175
     176    //Vector with positions in original sample
     177    vector<int> entryID (sampleSize,0);
     178
     179    //calculate wich entries from inVector will be put into outVector
     180    BootstrapSample(&entryID, 0, sampleSize, sampleSize );
     181
     182    vector<int>::iterator it;
     183
     184    //Loop over entryID vector to fill content from inVector to outVector
     185    int counter = 0;
     186    for ( it=entryID.begin() ; it != entryID.end(); it++ )
     187    {
     188        outVector->at(counter) = inVector->at(*it);
     189        counter++;
     190    }
     191
     192    return counter + 1;
     193}
     194
     195int
     196Sample::BootstrapTH1(TH1* inputHisto, TH1*  outHisto)
     197{
     198    //compute the median for 1-d histogram h1
     199    int nbins = inputHisto->GetXaxis()->GetNbins();
     200
     201    //we need to get the binning
     202
     203    //entries of TH1
     204    vector<float>    entries;
     205
     206    //quantity of entries in bin
     207    int quantity    = 0;
     208    float value       = 0;
     209    for (int i=0;i<nbins;i++)
     210    {
     211        value       = inputHisto->GetBinLowEdge(i);
     212        quantity    = inputHisto->GetBinContent(i);
     213//        cout << value << "(" << quantity << ") ";
     214        for (int j = 0; j < quantity; j++)
     215        {
     216            entries.push_back(value);
     217        }
     218    }
     219
     220    //get size of sample to be bootstrapped
     221    int sampleSize = entries.size();
     222
     223    //Vector with positions in original sample
     224    vector<int> entryID (sampleSize,0);
     225
     226    //calculate a list with random EntryIDs and fill it into entryID
     227    BootstrapSample(&entryID, 0, sampleSize, sampleSize );
     228
     229    //Loop over entryID vector to bootstrap the histogram
     230    int counter = 0;
     231    for ( unsigned int i = 0 ; i < entryID.size(); i++ )
     232    {
     233        outHisto->Fill(
     234                    entries.at( entryID.at(i) )
     235                    );
     236        counter++;
     237    }
     238
     239    return counter + 1;
     240}
     241
    168242void
    169243Sample::BootstrapSample()
     
    181255        int   size)
    182256{
     257    if (size == 0){
     258        cout << "size = 0; nothing to do" << endl;
     259        return;
     260    }
     261
     262    //resize the sample vector to size of boostrapped sample
    183263    sampleVector->resize(size);
    184264
     265    //list of rndnumbers pulled with putting back
    185266    multiset<int> sampleSet;
    186267
     268    //loop over samplesize to generate random numbers to fill into sampleset
    187269    for (int i = 0; i < size; i++)
    188270    {
     
    196278
    197279    int counter = 0;
     280    // loop over list of rndnumbers and fill their entries into sampleVector
     281    // entries are vector positions
    198282    for ( it=sampleSet.begin() ; it != sampleSet.end(); it++ )
    199283    {
     
    204288    return;
    205289}
     290//NOTE: crashes for some reason if size is smaller than max-min
Note: See TracChangeset for help on using the changeset viewer.