00001 // $Id: StRandomSelector.cxx,v 1.1 2009/09/03 11:56:43 rfatemi Exp $ 00002 // 00003 // $Log: StRandomSelector.cxx,v $ 00004 // Revision 1.1 2009/09/03 11:56:43 rfatemi 00005 // This module allows users to randomly remove members of any group in a set container 00006 // 00007 // 00008 00009 /* StRandomSelector.cxx 00010 * 00011 * Written by Wayne Witzke for the University of Kentucky Department of 00012 * Physics and Astronomy. 00013 * 00014 * This random selector will allow a developer to randomly pick TObjects from 00015 * a TCollection. 00016 */ 00017 00018 #include <iostream> 00019 #include <cmath> 00020 #include "StRandomSelector.h" 00021 00022 // This is required so that the .cxx and .h files are tied together correctly 00023 // for loading into root macros. 00024 ClassImp(StRandomSelector) 00025 00026 TObject * StRandomSelector::GetNextRandom() 00027 { 00028 // This is just for convenience and speed, in theory. 00029 double totalElem = GetTotalNumber(); 00030 00031 // This is so that we can easily calculate when we're close enough to 00032 // zero in the calculations below. It needs to be just a *little* bit 00033 // less than the smallest double that still indicates that 00034 // mTotalElemReturned and mTotalElemSkipped are too large to indicate that 00035 // the target probability has been reached. 00036 double tolerance = 1/(totalElem+1/totalElem); 00037 00038 if ( mAbsoluteThreshold ) 00039 { 00040 if ( 00041 std::abs((double)mTotalElemReturned/totalElem - mProbability) < tolerance 00042 ) 00043 { 00044 Skip( GetTotalNumber() ); 00045 return NULL; 00046 } 00047 } 00048 00049 while ( 00050 mRand.Rndm() > mProbability 00051 && mTotalElemSkipped+mTotalElemReturned < totalElem 00052 ) 00053 { 00054 // I'd rather not do this every time, but I don't think I have a 00055 // choice. Also, notice the strange way that the check on the 00056 // probability is being calculated. That's a work around for double 00057 // precision problems. 00058 if ( 00059 mAbsoluteThreshold 00060 && std::abs((double)mTotalElemSkipped/totalElem - 1.0 + mProbability) < tolerance 00061 ) 00062 break; 00063 00064 Skip(); 00065 } 00066 return GetNext(); 00067 }
1.5.9