Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:04:20

0001 #include "DataFormats/HGCalDigi/interface/HGCalDigiCollections.h"
0002 #include <iostream>
0003 #include <cassert>
0004 #include <string>
0005 #include <chrono>
0006 #include <random>
0007 
0008 // run for instance with:
0009 //
0010 //                         time HGCROCSampleTest  10000000000 => 8 sec
0011 //                         time HGCROCSampleTest  10000000000    y   [to require randomisation] =>
0012 //
0013 // for a measureble amount of time taken
0014 
0015 //wrap the procedure to assert TOT read is within quantization error
0016 //a truncation of two bits leads to an uncertainty of +/- 4 ADC counts
0017 bool totOK(uint16_t tot_orig, uint16_t tot_read) {
0018   int delta(tot_read - tot_orig);
0019   return (delta >= -4 && delta <= 4);
0020 }
0021 
0022 int main(int argc, char** argv) {
0023   std::cout << "Basic performance tests for HGCROCChannelDataFrame (pseudo-random seed set according to local time)\n"
0024             << std::endl;
0025   std::cout << "num parameters entered: " << argc << std::endl;
0026 
0027   // first command line argument is the number of trials
0028   unsigned long int repetitions = 100;
0029   if (argc > 1)
0030     repetitions = std::stoul(argv[1], nullptr, 0);
0031   std::cout << "\t + repetitions [int]: " << repetitions << std::endl;
0032   // second command line argument (whatever it is) will activate
0033   //                  the random choice of values for all inputs
0034   bool generateRandomValues = (argc > 2 ? true : false);
0035   std::cout << "\t + generateRandomValues [true/false]: " << generateRandomValues << "\n" << std::endl;
0036 
0037   // init static values
0038   uint16_t adc(125), adcm1(23), tot(10), toa(8);
0039   bool tc(false), tp(false), charMode(false);
0040 
0041   // http://www.cplusplus.com/reference/random/linear_congruential_engine/
0042   unsigned seed1 = std::chrono::system_clock::now().time_since_epoch().count();
0043   std::minstd_rand0 myrand(seed1);
0044 
0045   // do the trials: time/performance test and exploit randomisation to check
0046   unsigned long int u = 0;
0047   for (; u < repetitions; u++) {
0048     // randomise all inputs, if chosen at the command line
0049     if (generateRandomValues) {
0050       adc = myrand() % 1024;
0051       adcm1 = myrand() % 1024;
0052       tot = myrand() % 2048;
0053       toa = myrand() % 1024;
0054       tc = myrand() % 2;
0055       tp = myrand() % 2;
0056       charMode = myrand() % 2;
0057     }
0058 
0059     HGCROCChannelDataFrameSpec aSample;
0060     aSample.fill(charMode, tc, tp, adcm1, adc, tot, toa);
0061 
0062     bool tc_read = aSample.tc();
0063     bool tp_read = aSample.tp();
0064     uint16_t adc_read = aSample.adc(charMode);
0065     uint16_t adcm1_read = aSample.adcm1(charMode);
0066     uint16_t tot_read = aSample.tot(charMode);
0067     uint16_t toa_read = aSample.toa();
0068     assert(tc == tc_read);
0069     assert(tp == tp_read);
0070 
0071     //uncomment for a verbose output
0072     //std::cout << "Tc=" << tc << " Tp=" << tp << " adcm1=" << adcm1 << " adc=" << adc << " tot=" << tot << " toa=" << toa << " char mode=" << charMode << std::endl;
0073     //aSample.print(std::cout);
0074     //std::cout << "Tc'=" << tc_read << " Tp'=" << tp_read << " adcm1'=" << adcm1_read << " adc'=" << adc_read << " tot'=" << tot_read << " toa'=" << toa_read << std::endl;
0075 
0076     if (charMode) {
0077       assert(adcm1_read == 0);
0078       assert(adc == adc_read);
0079       assert(totOK(tot, tot_read));
0080       assert(toa == toa_read);
0081     } else {
0082       assert(adcm1 == adcm1_read);
0083       if (tc) {
0084         assert(adc_read == 0);
0085         assert(totOK(tot, tot_read));
0086       } else {
0087         assert(tot_read == 0);
0088         assert(adc == adc_read);
0089       }
0090     }
0091   }
0092 
0093   std::cout << "\nDone " << repetitions << "\t" << u << std::endl;
0094 
0095   return 0;
0096 }