Line data Source code
1 : /*******************************************************************************
2 : * Project: BaBar detector at the SLAC PEP-II B-factory
3 : * Package: EvtGenBase
4 : * File: $Id: EvtPredGen.hh,v 1.2 2009-03-16 16:40:16 robbep Exp $
5 : * Author: Alexei Dvoretskii, dvoretsk@slac.stanford.edu, 2001-2002
6 : *
7 : * Copyright (C) 2002 Caltech
8 : *******************************************************************************/
9 :
10 : // A predicate is applied to a generator to get another generator.
11 : // Accept-reject can be implemented in this way.
12 : //
13 : // Predicate
14 : // Generator -> Generator
15 :
16 : #ifndef EVT_PRED_GEN_HH
17 : #define EVT_PRED_GEN_HH
18 :
19 : #include <stdio.h>
20 :
21 : template <class Generator, class Predicate>
22 : class EvtPredGen {
23 :
24 : public:
25 :
26 : typedef typename Generator::result_type result_type;
27 :
28 : EvtPredGen()
29 : : itsTried(0), itsPassed(0)
30 : {}
31 :
32 : EvtPredGen(Generator gen, Predicate pred)
33 0 : : itsGen(gen), itsPred(pred), itsTried(0), itsPassed(0)
34 0 : {}
35 :
36 : EvtPredGen(const EvtPredGen& other)
37 : : itsGen(other.itsGen), itsPred(other.itsPred),
38 : itsTried(other.itsTried), itsPassed(other.itsPassed)
39 : {}
40 :
41 : ~EvtPredGen()
42 0 : {}
43 :
44 : result_type operator()() {
45 :
46 : int i = 0;
47 : int MAX = 10000;
48 0 : while(i++ < MAX) {
49 :
50 0 : itsTried++;
51 0 : result_type point = itsGen();
52 0 : if(itsPred(point)) {
53 0 : itsPassed++;
54 0 : return point;
55 : }
56 0 : }
57 :
58 0 : printf("No random point generated after %d attempts\n",MAX);
59 0 : printf("Sharp peak? Consider using pole compensation.\n");
60 0 : printf("I will now pick a point at random to return.\n");
61 0 : return itsGen();
62 0 : }
63 :
64 : inline int getTried() const { return itsTried; }
65 : inline int getPassed() const { return itsPassed; }
66 :
67 : protected:
68 :
69 : Generator itsGen;
70 : Predicate itsPred;
71 : int itsTried;
72 : int itsPassed;
73 :
74 : };
75 :
76 : #endif
77 :
|