StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
EvtPredGen.hh
1 /*******************************************************************************
2  * Project: BaBar detector at the SLAC PEP-II B-factory
3  * Package: EvtGenBase
4  * File: $Id: EvtPredGen.hh,v 1.1 2016/09/23 18:37:32 jwebb 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  : itsGen(gen), itsPred(pred), itsTried(0), itsPassed(0)
34  {}
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  {}
43 
44  result_type operator()() {
45 
46  int i = 0;
47  int MAX = 10000;
48  while(i++ < MAX) {
49 
50  itsTried++;
51  result_type point = itsGen();
52  if(itsPred(point)) {
53  itsPassed++;
54  return point;
55  }
56  }
57 
58  printf("No random point generated after %d attempts\n",MAX);
59  printf("Sharp peak? Consider using pole compensation.\n");
60  printf("I will now pick a point at random to return.\n");
61  return itsGen();
62  }
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