Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /**
0002 NPFold_clear_test.sh
0003 ======================
0004 
0005 ::
0006 
0007     ~/opticks/sysrap/tests/NPFold_clear_test.sh
0008 
0009 Without the NPFold::clear call get assert::
0010 
0011     NPFold::add_ FATAL : have_key_already [photon.npy]
0012 
0013 **/
0014 
0015 
0016 #include "sprof.h"
0017 #include "ssys.h"
0018 #include "sstamp.h"
0019 
0020 #include "NPFold.h"
0021 #include "NP.hh"
0022 
0023 
0024 struct SEV
0025 {
0026     static constexpr const char* PHOTON = "photon" ; 
0027     static constexpr const char* SAVE_COMP = "photon" ; 
0028     static std::vector<int>* NUM ; 
0029     static int GetNumEvt(); 
0030     static int GetNum(int idx); 
0031 
0032     int idx ; 
0033     NPFold* fold ; 
0034 
0035     SEV(); 
0036 
0037     NP* gatherPhoton() const ; 
0038     void gather_components() ; 
0039     void save(const char* dir); 
0040     void clear(); 
0041 };
0042 
0043 std::vector<int>* SEV::NUM = ssys::getenv_ParseIntSpecList("NUM", "M1,2,3,4,3,2,1" ); 
0044 
0045 int SEV::GetNumEvt()
0046 {
0047     return NUM ? NUM->size() : 0 ; 
0048 }
0049 int SEV::GetNum(int idx) // 
0050 {
0051     return NUM && idx < int(NUM->size()) ? (*NUM)[idx] : -1 ; 
0052 }
0053 
0054 inline SEV::SEV()
0055     :
0056     idx(-1),
0057     fold(new NPFold)
0058 {
0059 }
0060 
0061 inline NP* SEV::gatherPhoton() const 
0062 {
0063     assert( idx > -1 ); 
0064     int num = GetNum(idx); 
0065     NP* a = NP::Make<float>( num, 4, 4 ) ; 
0066     return a ;  
0067 }
0068 
0069 /**
0070 SEV::gather_components
0071 ------------------------
0072 
0073 NPFold::add asserts that the key is not already present 
0074 
0075 **/
0076 
0077 inline void SEV::gather_components()
0078 {
0079     NP* a = gatherPhoton(); 
0080     fold->add(PHOTON, a ) ;   // NB the fold must have been cleared for this to work beyond first cycle 
0081 }
0082 
0083 inline void SEV::save(const char* dir)
0084 {
0085     gather_components(); 
0086     bool shallow = true ;   // no ownership, just copy pointers 
0087     NPFold* save_fold = fold->copy(SAVE_COMP, shallow) ;
0088     save_fold->save(dir);
0089 }
0090 inline void SEV::clear()
0091 {
0092     fold->clear();  // deletes the arrays 
0093 }
0094 
0095 
0096 struct NPFold_clear_test
0097 {
0098     static constexpr const int M = 1000000 ; 
0099     static constexpr const int K = 1000 ; 
0100 
0101     static void t0(); 
0102     static void main(); 
0103 };
0104 
0105 
0106 void NPFold_clear_test::t0()
0107 {
0108 
0109     bool CLEAR = getenv("CLEAR") != nullptr ; 
0110     bool DELETE = getenv("DELETE") != nullptr ; 
0111 
0112     int num_event = SEV::GetNumEvt(); 
0113 
0114     NP* run = NP::Make<int>(num_event); 
0115     run->set_meta<std::string>("TEST", "t0" ) ; 
0116     run->set_meta<std::string>("CLEAR", CLEAR ? "YES" : "NO" ) ; 
0117     run->set_meta<std::string>("DELETE", DELETE ? "YES" : "NO" ) ; 
0118     int* rr = run->values<int>(); 
0119 
0120     SEV* ev = new SEV ; 
0121 
0122 
0123     for(int idx=0 ; idx < num_event  ; idx++)
0124     {
0125         ev->idx = idx ; 
0126         int num = SEV::GetNum(idx); 
0127         rr[idx] = num ;  
0128         std::cout << std::setw(4) << idx << " : " << num << std::endl ; 
0129 
0130         std::string head = U::FormName_("head_", idx, nullptr, 3 ) ; 
0131         sstamp::sleep_us(100000); 
0132         run->set_meta<std::string>(head.c_str(), sprof::Now() ); 
0133 
0134         std::string dir = sstr::FormatIndexDefault_( idx, "$FOLD/");  
0135         ev->save(dir.c_str()) ;  
0136 
0137         std::string body = U::FormName_("body_", idx, nullptr, 3 ) ; 
0138         sstamp::sleep_us(100000); 
0139         run->set_meta<std::string>(body.c_str(), sprof::Now() ); 
0140 
0141         ev->clear();      
0142 
0143         std::string tail = U::FormName_("tail_", idx, nullptr, 3 ) ; 
0144         sstamp::sleep_us(100000); 
0145         run->set_meta<std::string>(tail.c_str(), sprof::Now() ); 
0146     }
0147 
0148 
0149     NPFold* out = new NPFold ;  
0150     out->add( "run", run ); 
0151     out->add( "runprof", run->makeMetaKVProfileArray() ); 
0152     out->save("$FOLD"); 
0153 }
0154 
0155 void NPFold_clear_test::main()
0156 {
0157     char* TEST = getenv("TEST") ; 
0158     int test = TEST && strlen(TEST) > 0 && TEST[0] == 't'  ? strtol(TEST+1, nullptr, 10) : -1 ; 
0159     switch(test)
0160     {
0161         case 0: t0() ; break ; 
0162     }
0163 }
0164 
0165 int main()
0166 {
0167     NPFold_clear_test::main(); 
0168     return 0 ; 
0169 }
0170 
0171 // ~/opticks/sysrap/tests/NPFold_clear_test.sh
0172