Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:58:19

0001 #include <cppunit/CompilerOutputter.h>
0002 #include <cppunit/TestFixture.h>
0003 #include <cppunit/TestResult.h>
0004 #include <cppunit/TestResultCollector.h>
0005 #include <cppunit/TestRunner.h>
0006 #include <cppunit/TextTestProgressListener.h>
0007 #include <cppunit/extensions/HelperMacros.h>
0008 #include <cppunit/ui/text/TestRunner.h>
0009 
0010 #include <algorithm>
0011 #include <boost/foreach.hpp>
0012 #include <iterator>
0013 
0014 #include "CalibFormats/SiStripObjects/interface/SiStripGain.h"
0015 #include "CalibTracker/SiStripCommon/interface/SiStripDetInfoFileReader.h"
0016 #include "FWCore/ParameterSet/interface/FileInPath.h"
0017 
0018 #ifndef TestSiStripGain_cc
0019 #define TestSiStripGain_cc
0020 
0021 class TestSiStripGain : public CppUnit::TestFixture {
0022 public:
0023   TestSiStripGain() {}
0024   void setUp() {
0025     detId = 436282904;
0026 
0027     apvGain1 = new SiStripApvGain;
0028     std::vector<float> theSiStripVector;
0029     theSiStripVector.push_back(1.);
0030     theSiStripVector.push_back(0.8);
0031     theSiStripVector.push_back(1.2);
0032     theSiStripVector.push_back(2.);
0033     fillApvGain(apvGain1, detId, theSiStripVector);
0034 
0035     apvGain2 = new SiStripApvGain;
0036     theSiStripVector.clear();
0037     theSiStripVector.push_back(1.);
0038     theSiStripVector.push_back(1. / 0.8);
0039     theSiStripVector.push_back(1. / 1.2);
0040     theSiStripVector.push_back(2.);
0041     fillApvGain(apvGain2, detId, theSiStripVector);
0042   }
0043 
0044   void tearDown() {
0045     delete apvGain1;
0046     delete apvGain2;
0047   }
0048 
0049   void testConstructor() {
0050     // Test with normalization factor = 1
0051     apvGainsTest(1.);
0052     // Test with normalization factor != 1
0053     apvGainsTest(2.);
0054   }
0055 
0056   void testMultiply() {
0057     // Test with norm = 1
0058     multiplyTest(1., 1.);
0059 
0060     // Test with norm != 1
0061     multiplyTest(2., 3.);
0062   }
0063 
0064   void multiplyTest(const float &norm1, const float &norm2) {
0065     std::pair<std::string, std::string> recordLabelPair1("gainRcd1", "");
0066     std::pair<std::string, std::string> recordLabelPair2("gainRcd2", "");
0067     std::vector<std::pair<std::string, std::string>> recordLabelPairVector;
0068     recordLabelPairVector.push_back(recordLabelPair1);
0069     recordLabelPairVector.push_back(recordLabelPair2);
0070     std::vector<float> normVector;
0071     normVector.push_back(norm1);
0072     normVector.push_back(norm2);
0073 
0074     const auto detInfo =
0075         SiStripDetInfoFileReader::read(edm::FileInPath{SiStripDetInfoFileReader::kDefaultFile}.fullPath());
0076 
0077     SiStripGain gain(*apvGain1, norm1, recordLabelPair1, detInfo);
0078     gain.multiply(*apvGain2, norm2, recordLabelPair2, detInfo);
0079     SiStripApvGain::Range range = gain.getRange(detId);
0080 
0081     // Check multiplication
0082     CPPUNIT_ASSERT(float(gain.getApvGain(0, range)) == float(1. / norm1 * 1. / norm2));
0083     CPPUNIT_ASSERT(float(gain.getApvGain(1, range)) == float(0.8 / norm1 * 1. / (0.8 * norm2)));
0084     CPPUNIT_ASSERT(float(gain.getApvGain(2, range)) == float(1.2 / norm1 * 1. / (1.2 * norm2)));
0085     CPPUNIT_ASSERT(float(gain.getApvGain(3, range)) == float(2. / norm1 * 2. / norm2));
0086 
0087     checkTag(gain, normVector, normVector.size(), recordLabelPairVector);
0088   }
0089 
0090   void apvGainsTest(const float &norm) {
0091     const auto detInfo =
0092         SiStripDetInfoFileReader::read(edm::FileInPath{SiStripDetInfoFileReader::kDefaultFile}.fullPath());
0093 
0094     SiStripGain gain(*apvGain1, norm, detInfo);
0095     SiStripApvGain::Range range = gain.getRange(detId);
0096     CPPUNIT_ASSERT(float(gain.getApvGain(0, range)) == float(1. / norm));
0097     CPPUNIT_ASSERT(float(gain.getApvGain(1, range)) == float(0.8 / norm));
0098     CPPUNIT_ASSERT(float(gain.getApvGain(2, range)) == float(1.2 / norm));
0099     CPPUNIT_ASSERT(float(gain.getApvGain(3, range)) == float(2. / norm));
0100     checkTag(gain, norm, "", "");
0101 
0102     SiStripGain gain2(*apvGain2, norm, detInfo);
0103     SiStripApvGain::Range range2 = gain2.getRange(detId);
0104     CPPUNIT_ASSERT(float(gain2.getApvGain(0, range2)) == float(1. / norm));
0105     CPPUNIT_ASSERT(float(gain2.getApvGain(1, range2)) == float(1. / (norm * 0.8)));
0106     CPPUNIT_ASSERT(float(gain2.getApvGain(2, range2)) == float(1. / (norm * 1.2)));
0107     CPPUNIT_ASSERT(float(gain2.getApvGain(3, range2)) == float(2. / norm));
0108     checkTag(gain2, norm, "", "");
0109   }
0110 
0111   void checkTag(const SiStripGain &gain, const float &norm, const std::string &rcdName, const std::string &labelName) {
0112     std::vector<float> normVector;
0113     normVector.push_back(norm);
0114     std::vector<std::pair<std::string, std::string>> recordLabelPairVector;
0115     recordLabelPairVector.push_back(std::make_pair(rcdName, labelName));
0116     checkTag(gain, normVector, 1, recordLabelPairVector);
0117   }
0118   void checkTag(const SiStripGain &gain,
0119                 const std::vector<float> &norm,
0120                 const uint32_t tagNum,
0121                 const std::vector<std::pair<std::string, std::string>> &recordLabelPair) {
0122     CPPUNIT_ASSERT(gain.getNumberOfTags() == tagNum);
0123     for (unsigned int i = 0; i < tagNum; ++i) {
0124       CPPUNIT_ASSERT(float(gain.getTagNorm(i)) == norm[i]);
0125       CPPUNIT_ASSERT(gain.getRcdName(i) == recordLabelPair[i].first);
0126       CPPUNIT_ASSERT(gain.getLabelName(i) == recordLabelPair[i].second);
0127     }
0128   }
0129 
0130   void fillApvGain(SiStripApvGain *apvGain, const uint32_t detId, const std::vector<float> &theSiStripVector) {
0131     SiStripApvGain::Range range(theSiStripVector.begin(), theSiStripVector.end());
0132     apvGain->put(detId, range);
0133   }
0134 
0135   SiStripApvGain *apvGain1;
0136   SiStripApvGain *apvGain2;
0137   uint32_t detId;
0138 
0139   // Declare and build the test suite
0140   CPPUNIT_TEST_SUITE(TestSiStripGain);
0141   CPPUNIT_TEST(testConstructor);
0142   CPPUNIT_TEST(testMultiply);
0143   CPPUNIT_TEST_SUITE_END();
0144 };
0145 
0146 // Register the test suite in the registry.
0147 // This way we will have to only pass the registry to the runner
0148 // and it will contain all the registered test suites.
0149 CPPUNIT_TEST_SUITE_REGISTRATION(TestSiStripGain);
0150 
0151 #endif