StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StRandomSelector.cxx
1 // $Id: StRandomSelector.cxx,v 1.1 2009/09/03 11:56:43 rfatemi Exp $
2 //
3 // $Log: StRandomSelector.cxx,v $
4 // Revision 1.1 2009/09/03 11:56:43 rfatemi
5 // This module allows users to randomly remove members of any group in a set container
6 //
7 //
8 
9 /* StRandomSelector.cxx
10  *
11  * Written by Wayne Witzke for the University of Kentucky Department of
12  * Physics and Astronomy.
13  *
14  * This random selector will allow a developer to randomly pick TObjects from
15  * a TCollection.
16  */
17 
18 #include <iostream>
19 #include <cmath>
20 #include "StRandomSelector.h"
21 
22 // This is required so that the .cxx and .h files are tied together correctly
23 // for loading into root macros.
24 ClassImp(StRandomSelector)
25 
26 TObject * StRandomSelector::GetNextRandom()
27 {
28  // This is just for convenience and speed, in theory.
29  double totalElem = GetTotalNumber();
30 
31  // This is so that we can easily calculate when we're close enough to
32  // zero in the calculations below. It needs to be just a *little* bit
33  // less than the smallest double that still indicates that
34  // mTotalElemReturned and mTotalElemSkipped are too large to indicate that
35  // the target probability has been reached.
36  double tolerance = 1/(totalElem+1/totalElem);
37 
38  if ( mAbsoluteThreshold )
39  {
40  if (
41  std::abs((double)mTotalElemReturned/totalElem - mProbability) < tolerance
42  )
43  {
44  Skip( GetTotalNumber() );
45  return NULL;
46  }
47  }
48 
49  while (
50  mRand.Rndm() > mProbability
51  && mTotalElemSkipped+mTotalElemReturned < totalElem
52  )
53  {
54  // I'd rather not do this every time, but I don't think I have a
55  // choice. Also, notice the strange way that the check on the
56  // probability is being calculated. That's a work around for double
57  // precision problems.
58  if (
59  mAbsoluteThreshold
60  && std::abs((double)mTotalElemSkipped/totalElem - 1.0 + mProbability) < tolerance
61  )
62  break;
63 
64  Skip();
65  }
66  return GetNext();
67 }