Changeset View
Changeset View
Standalone View
Standalone View
EvtGenBase/EvtPredGen.hh
Show All 28 Lines | |||||
// Predicate | // Predicate | ||||
// Generator -> Generator | // Generator -> Generator | ||||
template <class Generator, class Predicate> | template <class Generator, class Predicate> | ||||
class EvtPredGen { | class EvtPredGen { | ||||
public: | public: | ||||
typedef typename Generator::result_type result_type; | typedef typename Generator::result_type result_type; | ||||
EvtPredGen() : itsTried( 0 ), itsPassed( 0 ) {} | EvtPredGen() : m_tried( 0 ), m_passed( 0 ) {} | ||||
EvtPredGen( Generator gen, Predicate pred ) : | EvtPredGen( Generator gen, Predicate pred ) : | ||||
itsGen( gen ), itsPred( pred ), itsTried( 0 ), itsPassed( 0 ) | m_gen( gen ), m_pred( pred ), m_tried( 0 ), m_passed( 0 ) | ||||
{ | { | ||||
} | } | ||||
EvtPredGen( const EvtPredGen& other ) : | EvtPredGen( const EvtPredGen& other ) : | ||||
itsGen( other.itsGen ), | m_gen( other.m_gen ), | ||||
itsPred( other.itsPred ), | m_pred( other.m_pred ), | ||||
itsTried( other.itsTried ), | m_tried( other.m_tried ), | ||||
itsPassed( other.itsPassed ) | m_passed( other.m_passed ) | ||||
{ | { | ||||
} | } | ||||
~EvtPredGen() {} | ~EvtPredGen() {} | ||||
result_type operator()() | result_type operator()() | ||||
{ | { | ||||
int i = 0; | int i = 0; | ||||
int MAX = 10000; | int MAX = 10000; | ||||
while ( i++ < MAX ) { | while ( i++ < MAX ) { | ||||
itsTried++; | m_tried++; | ||||
result_type point = itsGen(); | result_type point = m_gen(); | ||||
if ( itsPred( point ) ) { | if ( m_pred( point ) ) { | ||||
itsPassed++; | m_passed++; | ||||
return point; | return point; | ||||
} | } | ||||
} | } | ||||
printf( "No random point generated after %d attempts\n", MAX ); | printf( "No random point generated after %d attempts\n", MAX ); | ||||
printf( "Sharp peak? Consider using pole compensation.\n" ); | printf( "Sharp peak? Consider using pole compensation.\n" ); | ||||
printf( "I will now pick a point at random to return.\n" ); | printf( "I will now pick a point at random to return.\n" ); | ||||
return itsGen(); | return m_gen(); | ||||
} | } | ||||
inline int getTried() const { return itsTried; } | inline int getTried() const { return m_tried; } | ||||
inline int getPassed() const { return itsPassed; } | inline int getPassed() const { return m_passed; } | ||||
protected: | protected: | ||||
Generator itsGen; | Generator m_gen; | ||||
Predicate itsPred; | Predicate m_pred; | ||||
int itsTried; | int m_tried; | ||||
int itsPassed; | int m_passed; | ||||
}; | }; | ||||
#endif | #endif |