evtgen is hosted by Hepforge, IPPP Durham
EvtGen  2.0.0
Monte Carlo generator of particle decays, in particular the weak decays of heavy flavour particles such as B mesons.
EvtPredGen.hh
Go to the documentation of this file.
1 
2 /***********************************************************************
3 * Copyright 1998-2020 CERN for the benefit of the EvtGen authors *
4 * *
5 * This file is part of EvtGen. *
6 * *
7 * EvtGen is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * EvtGen is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with EvtGen. If not, see <https://www.gnu.org/licenses/>. *
19 ***********************************************************************/
20 
21 #ifndef EVT_PRED_GEN_HH
22 #define EVT_PRED_GEN_HH
23 
24 #include <stdio.h>
25 
26 // A predicate is applied to a generator to get another generator.
27 // Accept-reject can be implemented in this way.
28 //
29 // Predicate
30 // Generator -> Generator
31 
32 template <class Generator, class Predicate>
33 class EvtPredGen {
34  public:
35  typedef typename Generator::result_type result_type;
36 
37  EvtPredGen() : itsTried( 0 ), itsPassed( 0 ) {}
38 
39  EvtPredGen( Generator gen, Predicate pred ) :
40  itsGen( gen ), itsPred( pred ), itsTried( 0 ), itsPassed( 0 )
41  {
42  }
43 
45  itsGen( other.itsGen ),
49  {
50  }
51 
53 
55  {
56  int i = 0;
57  int MAX = 10000;
58  while ( i++ < MAX ) {
59  itsTried++;
60  result_type point = itsGen();
61  if ( itsPred( point ) ) {
62  itsPassed++;
63  return point;
64  }
65  }
66 
67  printf( "No random point generated after %d attempts\n", MAX );
68  printf( "Sharp peak? Consider using pole compensation.\n" );
69  printf( "I will now pick a point at random to return.\n" );
70  return itsGen();
71  }
72 
73  inline int getTried() const { return itsTried; }
74  inline int getPassed() const { return itsPassed; }
75 
76  protected:
77  Generator itsGen;
78  Predicate itsPred;
79  int itsTried;
80  int itsPassed;
81 };
82 
83 #endif
int itsPassed
Definition: EvtPredGen.hh:80
result_type operator()()
Definition: EvtPredGen.hh:54
Predicate itsPred
Definition: EvtPredGen.hh:78
int getTried() const
Definition: EvtPredGen.hh:73
int itsTried
Definition: EvtPredGen.hh:79
EvtPredGen(Generator gen, Predicate pred)
Definition: EvtPredGen.hh:39
int getPassed() const
Definition: EvtPredGen.hh:74
EvtPredGen(const EvtPredGen &other)
Definition: EvtPredGen.hh:44
Generator::result_type result_type
Definition: EvtPredGen.hh:35
Index other(Index i, Index j)
Definition: EvtCyclic3.cpp:156
Generator itsGen
Definition: EvtPredGen.hh:77