Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "DataFormats/HGCDigi/interface/HGCSample.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 HGCSampleTest  10000000000
0011 //                         time HGCSampleTest  1000000000 => 14 sec ; 11 sec after restructuring set()
0012 //                         time HGCSampleTest  10000000000    y   [to require randomisation]
0013 //
0014 // for a measureble amount of time taken
0015 
0016 int main(int argc, char** argv) {
0017   std::cout << "Basic performance tests for HGCSample (pseudo-random seed set according to local time)\n" << std::endl;
0018   std::cout << "num parameters entered: " << argc << std::endl;
0019 
0020   // first command line argument is the number of trials
0021   unsigned long int repetitions = 100;
0022   if (argc > 1)
0023     repetitions = std::stoul(argv[1], nullptr, 0);
0024   std::cout << "\t + repetitions [int]: " << repetitions << std::endl;
0025   // second command line argument (whatever it is) will activate
0026   //                  the random choice of values for all inputs
0027   bool generateRandomValues = (argc > 2 ? true : false);
0028   std::cout << "\t + generateRandomValues [true/false]: " << generateRandomValues << "\n" << std::endl;
0029 
0030   // init static values
0031   uint32_t adc = 124;
0032   uint32_t gain = 15;
0033   const uint32_t thrADC = 900;
0034   bool thr = adc > thrADC;
0035   uint32_t toa = 0;
0036   bool mode = false;
0037   bool toaFired = true;
0038 
0039   // http://www.cplusplus.com/reference/random/linear_congruential_engine/
0040   unsigned seed1 = std::chrono::system_clock::now().time_since_epoch().count();
0041   std::minstd_rand0 myrand(seed1);
0042 
0043   // do the trials: time/performance test and exploit randomisation to check
0044   unsigned long int u = 0;
0045   for (; u < repetitions; u++) {
0046     // randomise all inputs, if chosen at the command line
0047     if (generateRandomValues) {
0048       adc = myrand() % 4096;
0049       toa = myrand() % 1024;
0050       gain = myrand() % 16;
0051       mode = myrand() % 2;
0052       thr = myrand() % 2;
0053       toaFired = myrand() % 2;
0054     }
0055 
0056     HGCSample aSample;
0057     // writing on an empty container first
0058     aSample.set(thr, mode, gain, toa, adc);
0059     aSample.setToAValid(toaFired);
0060     // check the values that went in also come out
0061     assert(thr == aSample.threshold());
0062     assert(mode == aSample.mode());
0063     assert(toa == aSample.toa());
0064     assert(gain == aSample.gain());
0065     assert(adc == aSample.data());
0066     assert(thr == aSample.threshold());
0067     assert(toaFired == aSample.getToAValid());
0068 
0069     // std::cout << aSample.raw() << "\t" << thr << "\t" << mode << "\t" << toaFired << "\t" << gain << "\t" << toa << "\t"
0070     //          << adc << std::endl;
0071 
0072     HGCSample ASample;
0073     ASample.setThreshold(thr);
0074     ASample.setMode(mode);
0075     ASample.setGain(gain);
0076     ASample.setToA(toa);
0077     ASample.setData(adc);
0078     ASample.setToAValid(toaFired);
0079     // check that using the individual setters yields the same result as the generic set
0080     assert(ASample.threshold() == aSample.threshold());
0081     assert(ASample.mode() == aSample.mode());
0082     assert(ASample.gain() == aSample.gain());
0083     assert(ASample.toa() == aSample.toa());
0084     assert(ASample.data() == aSample.data());
0085     assert(ASample.getToAValid() == aSample.getToAValid());
0086 
0087     HGCSample bSample;
0088     bSample.setThreshold(thr);
0089     bSample.setMode(mode);
0090     bSample.setGain(gain + 100);
0091     bSample.setToA(toa + 100);
0092     bSample.setData(adc + 100);
0093     bSample.setToAValid(toaFired);
0094 
0095     // cover the case where we write on a container with numbers already set
0096     bSample.setThreshold(thr);
0097     bSample.setMode(mode);
0098     bSample.setGain(gain);
0099     bSample.setToA(toa);
0100     bSample.setData(adc);
0101     bSample.setToAValid(toaFired);
0102     assert(thr == aSample.threshold() && thr == bSample.threshold());
0103     assert(mode == aSample.mode() && mode == bSample.mode());
0104     assert(gain == aSample.gain() && gain == bSample.gain());
0105     assert(toa == aSample.toa() && toa == bSample.toa());
0106     assert(adc == aSample.data() && adc == bSample.data());
0107     assert(toaFired == aSample.getToAValid() && toaFired == bSample.getToAValid());
0108   }
0109 
0110   std::cout << "\nDone " << repetitions << "\t" << u << std::endl;
0111 
0112   return 0;
0113 }