Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-06 09:20:03

0001 
0002 /***********************************************************************
0003 * Copyright 1998-2020 CERN for the benefit of the EvtGen authors       *
0004 *                                                                      *
0005 * This file is part of EvtGen.                                         *
0006 *                                                                      *
0007 * EvtGen is free software: you can redistribute it and/or modify       *
0008 * it under the terms of the GNU General Public License as published by *
0009 * the Free Software Foundation, either version 3 of the License, or    *
0010 * (at your option) any later version.                                  *
0011 *                                                                      *
0012 * EvtGen is distributed in the hope that it will be useful,            *
0013 * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
0014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
0015 * GNU General Public License for more details.                         *
0016 *                                                                      *
0017 * You should have received a copy of the GNU General Public License    *
0018 * along with EvtGen.  If not, see <https://www.gnu.org/licenses/>.     *
0019 ***********************************************************************/
0020 
0021 #ifndef EVT_PRED_GEN_HH
0022 #define EVT_PRED_GEN_HH
0023 
0024 #include <stdio.h>
0025 
0026 // A predicate is applied to a generator to get another generator.
0027 // Accept-reject can be implemented in this way.
0028 //
0029 //           Predicate
0030 // Generator    ->     Generator
0031 
0032 template <class Generator, class Predicate>
0033 class EvtPredGen {
0034   public:
0035     typedef typename Generator::result_type result_type;
0036 
0037     EvtPredGen() : itsTried( 0 ), itsPassed( 0 ) {}
0038 
0039     EvtPredGen( Generator gen, Predicate pred ) :
0040         itsGen( gen ), itsPred( pred ), itsTried( 0 ), itsPassed( 0 )
0041     {
0042     }
0043 
0044     EvtPredGen( const EvtPredGen& other ) :
0045         itsGen( other.itsGen ),
0046         itsPred( other.itsPred ),
0047         itsTried( other.itsTried ),
0048         itsPassed( other.itsPassed )
0049     {
0050     }
0051 
0052     ~EvtPredGen() {}
0053 
0054     result_type operator()()
0055     {
0056         int i = 0;
0057         int MAX = 10000;
0058         while ( i++ < MAX ) {
0059             itsTried++;
0060             result_type point = itsGen();
0061             if ( itsPred( point ) ) {
0062                 itsPassed++;
0063                 return point;
0064             }
0065         }
0066 
0067         printf( "No random point generated after %d attempts\n", MAX );
0068         printf( "Sharp peak? Consider using pole compensation.\n" );
0069         printf( "I will now pick a point at random to return.\n" );
0070         return itsGen();
0071     }
0072 
0073     inline int getTried() const { return itsTried; }
0074     inline int getPassed() const { return itsPassed; }
0075 
0076   protected:
0077     Generator itsGen;
0078     Predicate itsPred;
0079     int itsTried;
0080     int itsPassed;
0081 };
0082 
0083 #endif