EvtGen 2.2.0
Monte Carlo generator of particle decays, in particular the weak decays of heavy flavour particles such as B mesons.
Loading...
Searching...
No Matches
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
32template <class Generator, class Predicate>
34 public:
35 typedef typename Generator::result_type result_type;
36
37 EvtPredGen() : m_tried( 0 ), m_passed( 0 ) {}
38
39 EvtPredGen( Generator gen, Predicate pred ) :
40 m_gen( gen ), m_pred( pred ), m_tried( 0 ), m_passed( 0 )
41 {
42 }
43
44 EvtPredGen( const EvtPredGen& other ) :
45 m_gen( other.m_gen ),
46 m_pred( other.m_pred ),
47 m_tried( other.m_tried ),
48 m_passed( other.m_passed )
49 {
50 }
51
53
55 {
56 int i = 0;
57 int MAX = 10000;
58 while ( i++ < MAX ) {
59 m_tried++;
60 result_type point = m_gen();
61 if ( m_pred( point ) ) {
62 m_passed++;
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 m_gen();
71 }
72
73 inline int getTried() const { return m_tried; }
74 inline int getPassed() const { return m_passed; }
75
76 protected:
77 Generator m_gen;
78 Predicate m_pred;
81};
82
83#endif
Predicate m_pred
Definition EvtPredGen.hh:78
int getPassed() const
Definition EvtPredGen.hh:74
result_type operator()()
Definition EvtPredGen.hh:54
Generator m_gen
Definition EvtPredGen.hh:77
EvtPredGen(Generator gen, Predicate pred)
Definition EvtPredGen.hh:39
Generator::result_type result_type
Definition EvtPredGen.hh:35
int getTried() const
Definition EvtPredGen.hh:73
EvtPredGen(const EvtPredGen &other)
Definition EvtPredGen.hh:44