Back to home page

EIC code displayed by LXR

 
 

    


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

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_VAL_ERROR_HH
0022 #define EVT_VAL_ERROR_HH
0023 
0024 #include <assert.h>
0025 #include <iostream>
0026 #include <math.h>
0027 
0028 // Value and its associated error. E.g. this could be interval size and
0029 // the error associated with numerical integration.
0030 
0031 class EvtValError final {
0032   public:
0033     EvtValError();
0034     EvtValError( double val );
0035     EvtValError( double val, double err );
0036     EvtValError( const EvtValError& other );
0037 
0038     inline int valueKnown() const { return _valKnown; }
0039     inline double value() const
0040     {
0041         assert( _valKnown );
0042         return _val;
0043     }
0044     inline int errorKnown() const { return _errKnown; }
0045     inline double error() const
0046     {
0047         assert( _errKnown );
0048         return _err;
0049     }
0050 
0051     double prec() const;
0052     void operator=( const EvtValError& other );
0053     void operator*=( const EvtValError& other );
0054     void operator/=( const EvtValError& other );
0055     void operator+=( const EvtValError& other );
0056     void operator*=( double c );
0057 
0058     void print( std::ostream& ) const;
0059 
0060   private:
0061     int _valKnown;
0062     double _val;
0063     int _errKnown;
0064     double _err;
0065 };
0066 
0067 EvtValError operator*( const EvtValError& x1, const EvtValError& x2 );
0068 EvtValError operator/( const EvtValError& x1, const EvtValError& x2 );
0069 EvtValError operator+( const EvtValError& x1, const EvtValError& x2 );
0070 EvtValError operator*( const EvtValError& x, double c );
0071 EvtValError operator*( double c, const EvtValError& x );
0072 
0073 std::ostream& operator<<( std::ostream&, const EvtValError& );
0074 
0075 // Perform an accept/reject fraction count
0076 
0077 template <class InputIterator, class Predicate>
0078 EvtValError accept_reject( InputIterator it, InputIterator end, Predicate pred )
0079 {
0080     int itsTried = 0;
0081     int itsPassed = 0;
0082     while ( it != end ) {
0083         itsTried++;
0084         if ( pred( *it++ ) )
0085             itsPassed++;
0086     }
0087 
0088     return EvtValError( ( (double)itsPassed ) / ( (double)itsTried ),
0089                         sqrt( itsPassed ) / ( (double)itsTried ) );
0090 }
0091 
0092 #endif