Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:47

0001 #pragma once
0002 /**
0003 sslice.h : python style gs[start:stop] slice of genstep arrays/vectors
0004 ========================================================================
0005 
0006 gs_start
0007    gs index starting the slice
0008 
0009 gs_stop
0010    gs index stopping the slice, ie one beyond the last index
0011 
0012 ph_offset
0013    total photons before this slice
0014 
0015 ph_count
0016    total photons within this slice
0017 
0018 **/
0019 
0020 #include <vector>
0021 #include <sstream>
0022 #include <string>
0023 #include <iomanip>
0024 #include <cstdint>
0025 
0026 
0027 struct sslice
0028 {
0029     static constexpr const size_t M = 1000000 ;
0030     static constexpr const size_t G = 1000000000 ;
0031 
0032     size_t gs_start ;
0033     size_t gs_stop ;
0034     size_t ph_offset ;
0035     size_t ph_count ;
0036 
0037     bool matches(size_t start, size_t stop, size_t offset, size_t count ) const ;
0038 
0039     static std::string Label() ;
0040     std::string desc() const ;
0041     std::string idx_desc(int idx) const ;
0042     static std::string Desc(const std::vector<sslice>& sl );
0043 
0044     static size_t TotalPhoton(const std::vector<sslice>& sl );
0045     static size_t TotalPhoton(const std::vector<sslice>& sl, int i0, int i1);
0046 
0047     static void SetOffset(std::vector<sslice>& slice);
0048 };
0049 
0050 inline bool sslice::matches(size_t start, size_t stop, size_t offset, size_t count ) const
0051 {
0052     return gs_start == start && gs_stop == stop && ph_offset == offset && ph_count == count ;
0053 }
0054 
0055 inline std::string sslice::Label()
0056 {
0057     std::stringstream ss ;
0058     ss << "       "
0059        << " "
0060        << std::setw(8) << "start"
0061        << " "
0062        << std::setw(8) << "stop "
0063        << " "
0064        << std::setw(10) << "offset "
0065        << " "
0066        << std::setw(10) << "count "
0067        << " "
0068        << std::setw(10) << "count/M "
0069        ;
0070     std::string str = ss.str() ;
0071     return str ;
0072 }
0073 inline std::string sslice::desc() const
0074 {
0075     std::stringstream ss ;
0076     ss << "sslice "
0077        << "{"
0078        << std::setw(8) << gs_start
0079        << ","
0080        << std::setw(8) << gs_stop
0081        << ","
0082        << std::setw(10) << ph_offset
0083        << ","
0084        << std::setw(10) << ph_count
0085        << "}"
0086        << std::setw(10) << std::fixed << std::setprecision(6) << double(ph_count)/M
0087        ;
0088     std::string str = ss.str() ;
0089     return str ;
0090 }
0091 
0092 inline std::string sslice::idx_desc(int idx) const
0093 {
0094     std::stringstream ss ;
0095     ss << std::setw(4) << idx << " : " <<  desc() ;
0096     std::string str = ss.str() ;
0097     return str ;
0098 }
0099 
0100 
0101 inline std::string sslice::Desc(const std::vector<sslice>& sl)
0102 {
0103     int64_t tot_photon = TotalPhoton(sl) ;
0104     std::stringstream ss ;
0105     ss << "sslice::Desc"
0106        << " num_slice " << sl.size()
0107        << " TotalPhoton " << std::setw(10) << tot_photon
0108        << " TotalPhoton/M " << std::setw(10) << std::fixed << std::setprecision(6) << double(tot_photon)/M
0109        << "\n"
0110         ;
0111     ss << std::setw(4) << "" << "   " << Label() << "\n" ;
0112     for(int i=0 ; i < int(sl.size()) ; i++ ) ss << sl[i].idx_desc(i) << "\n" ;
0113     ss << std::setw(4) << "" << "   " << Label() << "\n" ;
0114     std::string str = ss.str() ;
0115     return str ;
0116 }
0117 
0118 inline size_t sslice::TotalPhoton(const std::vector<sslice>& slice)
0119 {
0120     return TotalPhoton(slice, 0, slice.size() );
0121 }
0122 
0123 /**
0124 sslice::TotalPhoton
0125 ----------------------
0126 
0127 NB i0, i1 use python style slice indexing, ie::
0128 
0129     In [4]: np.arange(10)[0:4]
0130     Out[4]: array([0, 1, 2, 3])
0131 
0132     In [5]: np.arange(10)[0:9]
0133     Out[5]: array([0, 1, 2, 3, 4, 5, 6, 7, 8])
0134 
0135     In [6]: np.arange(10)[0:10]
0136     Out[6]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
0137 
0138     In [7]: np.arange(10)
0139     Out[7]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
0140 
0141     In [8]: np.arange(10)[0:0]
0142     Out[8]: array([], dtype=int64)
0143 
0144 
0145 **/
0146 
0147 
0148 inline size_t sslice::TotalPhoton(const std::vector<sslice>& slice, int i0, int i1)
0149 {
0150     assert( i0 <= int(slice.size())) ;
0151     assert( i1 <= int(slice.size())) ;
0152     size_t tot = 0 ;
0153     for(int i=i0 ; i < i1 ; i++ ) tot += slice[i].ph_count ;
0154     return tot ;
0155 }
0156 
0157 inline void sslice::SetOffset(std::vector<sslice>& slice)
0158 {
0159     for(int i=0 ; i < int(slice.size()) ; i++ ) slice[i].ph_offset = TotalPhoton(slice,0,i) ;
0160 }
0161