Warning, /include/ThePEG/Utilities/VSelector.tcc is written in an unsupported language. File is not indexed.
0001 // -*- C++ -*-
0002 //
0003 // VSelector.tcc is a part of ThePEG - Toolkit for HEP Event Generation
0004 // Copyright (C) 1999-2019 Leif Lonnblad
0005 //
0006 // ThePEG is licenced under version 3 of the GPL, see COPYING for details.
0007 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
0008 //
0009
0010 namespace ThePEG {
0011
0012 template <typename T, typename WeightType>
0013 WeightType VSelector<T,WeightType>::erase(const T & t) {
0014 theSum = WeightType();
0015 int j = 0;
0016 for ( int i = 0, N = theWeights.size(); i < N; ++i ) {
0017 if ( theObjects[i] == t ) continue;
0018 theSums[j] = (theSum += theWeights[i]);
0019 if ( i != j ) {
0020 theWeights[j] = theWeights[i];
0021 theObjects[j] = theObjects[i];
0022 }
0023 ++j;
0024 }
0025 theSums.erase(theSums.begin() + j, theSums.end());
0026 theWeights.erase(theWeights.begin() + j, theWeights.end());
0027 theObjects.erase(theObjects.begin() + j, theObjects.end());
0028 return theSum;
0029 }
0030
0031 template <typename T, typename WeightType>
0032 WeightType VSelector<T,WeightType>::reweight(WeightType d, const T & t) {
0033 d = max(d, WeightType());
0034 theSum = WeightType();
0035 for ( int i = 0, N = theWeights.size(); i < N; ++i ) {
0036 if ( theObjects[i] == t ) theWeights[i] = d;
0037 theSums[i] = ( theSum += theWeights[i] );
0038 }
0039 return theSum;
0040 }
0041
0042 template <typename T, typename WeightType>
0043 typename VSelector<T,WeightType>::size_type VSelector<T,WeightType>::
0044 iselect(double rnd, double * remainder) const {
0045 if ( rnd <= 0 )
0046 throw range_error("Random number out of range in VSelector::select.");
0047 WeightType sum = rnd*theSum;
0048 WIterator it = upper_bound(theSums.begin(), theSums.end(), sum);
0049 if ( it == theSums.end() )
0050 throw range_error("Empty Selector, or random number out of range "
0051 "in Selector::select");
0052 size_type i = it - theSums.begin();
0053 if ( remainder ) *remainder = 1.0 - (theSums[i] - sum)/theWeights[i];
0054 return i;
0055 }
0056
0057 template <typename T, typename WeightType>
0058 template <typename OStream>
0059 void VSelector<T,WeightType>::output(OStream & os) const {
0060 os << theSum << theSums << theWeights << theObjects;
0061 }
0062
0063 template <typename T, typename WeightType>
0064 template <typename IStream>
0065 void VSelector<T,WeightType>::input(IStream & is) {
0066 clear();
0067 is >> theSum >> theSums >> theWeights >> theObjects;
0068 }
0069
0070 }