Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #pragma once
0002 /**
0003 SProfile.h
0004 ===========
0005 
0006 NOW RECOMMEND TO USE THE MORE GENERAL SProf.hh SProf.cc APPROACH RATHER THAN THIS
0007 
0008 For example used to collect timestamps from junoSD_PMT_v2::ProcessHits
0009 for persisting into .npy files for analysis, using NPX.h functionality
0010 
0011 
0012 The static fixing of template parameter N is inconvenient when
0013 needing to change the N. But trying to do that dynamically for example
0014 with placement new would get complicated and would need in anycase
0015 an oversized buffer. Hence, just be pragmatic and fix N larger
0016 than typically needed (eg 16) so will then not have to change it very often.
0017 
0018 See tests/SProfile_test.cc for example of use.
0019 In summary::
0020 
0021     // in header, forward decl
0022     template<int N> struct SProfile ;
0023 
0024     // member
0025     SProfile<16>*        m_profile ;
0026 
0027 
0028 
0029     // in impl
0030     #include "SProfile.h"
0031     template<>
0032     std::vector<SProfile<16>> SProfile<16>::RECORD = {}  ;
0033 
0034     // instanciate m_profile in ctor
0035        m_profile(new SProfile<16>),
0036 
0037     // at appropriate juncture
0038 
0039        m_profile->zero();
0040 
0041        m_profile->stamp(0);
0042        m_profile->stamp(1);
0043        m_profile->stamp(2);
0044        ..
0045 
0046        m_profile->add();
0047 
0048 
0049 
0050 **/
0051 
0052 #include <cstdint>
0053 #include <vector>
0054 #include <chrono>
0055 #include "NPX.h"
0056 
0057 template<int N>
0058 struct SProfile
0059 {
0060     uint64_t idx ;
0061     uint64_t t[N] ;
0062 
0063     static std::vector<SProfile<N>> RECORD ;
0064     void add(){ RECORD.push_back(*this) ; }
0065 
0066     static uint64_t Now()
0067     {
0068         std::chrono::time_point<std::chrono::system_clock> t0 = std::chrono::system_clock::now();
0069         return std::chrono::duration_cast<std::chrono::microseconds>(t0.time_since_epoch()).count() ;
0070     }
0071     void zero(){ idx = 0 ; for(int i=0 ; i < N ; i++) t[i] = 0 ; }
0072     void stamp(int i){ t[i] = Now(); }
0073 
0074     static constexpr const char* NAME = "SProfile.npy" ;
0075     static NP* Array(){ return NPX::ArrayFromVec<uint64_t,SProfile<N>>(RECORD,1+N); }
0076     static void Save(const char* dir, const char* reldir=nullptr){ NP* a = Array(); a->save(dir, reldir, NAME) ; }
0077     static void Clear(){ RECORD.clear() ; }
0078 };
0079