Back to home page

EIC code displayed by LXR

 
 

    


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

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_AMPLITUDE_SUM_HH
0022 #define EVT_AMPLITUDE_SUM_HH
0023 
0024 #include "EvtGenBase/EvtAmplitude.hh"
0025 
0026 #include <assert.h>
0027 #include <memory>
0028 #include <vector>
0029 
0030 template <class T>
0031 class EvtAmplitudeSum : public EvtAmplitude<T> {
0032   public:
0033     EvtAmplitudeSum() {}
0034     EvtAmplitudeSum( const EvtAmplitudeSum<T>& other ) :
0035         EvtAmplitude<T>( other )
0036     {
0037         int i;
0038         for ( i = 0; i < other.nTerms(); i++ ) {
0039             EvtComplex c = other.c( i );
0040             _c.push_back( c );
0041             EvtAmplitude<T>* amp = other.getTerm( i );
0042             assert( amp );
0043             EvtAmplitude<T>* amp1 = amp->clone();
0044             assert( amp1 );
0045             _term.push_back( amp1 );
0046         }
0047     }
0048 
0049     virtual ~EvtAmplitudeSum()
0050     {
0051         for ( size_t i = 0; i < _term.size(); i++ ) {
0052             delete _term[i];
0053         }
0054     }
0055 
0056     EvtAmplitudeSum<T>* clone() const override
0057     {
0058         return new EvtAmplitudeSum<T>( *this );
0059     }
0060 
0061     void addTerm( EvtComplex c, const EvtAmplitude<T>& amp )
0062     {
0063         _c.push_back( c );
0064         _term.push_back( amp.clone() );
0065     }
0066 
0067     void addOwnedTerm( EvtComplex c, std::unique_ptr<EvtAmplitude<T>> amp )
0068     {
0069         assert( amp );
0070         _c.push_back( c );
0071         _term.push_back( amp.release() );
0072     }
0073 
0074     int nTerms() const { return _term.size(); }    // number of terms
0075 
0076     void print() const
0077     {
0078         int N = nTerms();
0079         printf( "Amplitude has %d terms\n", N );
0080         int i;
0081         for ( i = 0; i < N; i++ ) {
0082             printf( "c%d = (%f,%f)\n", i, real( _c[i] ), imag( _c[i] ) );
0083             assert( _term[i] );
0084         }
0085     }
0086 
0087     inline EvtComplex c( int i ) const { return _c[i]; }
0088     inline EvtAmplitude<T>* getTerm( int i ) const { return _term[i]; }
0089 
0090   protected:
0091     EvtComplex amplitude( const T& p ) const override
0092     {
0093         if ( _term.size() == 0 )
0094             printf( "Warning: amplitude sum has zero terms\n" );
0095 
0096         EvtComplex value = 0.;
0097 
0098         for ( size_t i = 0; i < _term.size(); i++ ) {
0099             value += _c[i] * _term[i]->evaluate( p );
0100         }
0101         return value;
0102     }
0103 
0104   private:
0105     std::vector<EvtComplex> _c;             // coefficients
0106     std::vector<EvtAmplitude<T>*> _term;    // pointers to amplitudes
0107 };
0108 
0109 #endif