Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:31:40

0001 #include <cppunit/TestFixture.h>
0002 #include <cppunit/extensions/HelperMacros.h>
0003 #include <cppunit/TestResult.h>
0004 #include <cppunit/TestRunner.h>
0005 #include <cppunit/ui/text/TestRunner.h>
0006 #include <cppunit/TestResultCollector.h>
0007 #include <cppunit/TextTestProgressListener.h>
0008 #include <cppunit/CompilerOutputter.h>
0009 
0010 #include <algorithm>
0011 #include <iterator>
0012 
0013 #include "MuonAnalysis/MomentumScaleCalibration/interface/BackgroundHandler.h"
0014 
0015 #ifndef TestBackgroundHandler_cc
0016 #define TestBackgroundHandler_cc
0017 
0018 class TestBackgroundHandler : public CppUnit::TestFixture {
0019 public:
0020   TestBackgroundHandler() {}
0021   void setUp() {
0022     fill_n(back_inserter(identifiers), 3, 2);
0023     // fill_n(back_inserter(leftWindowFactors), 3, 2);
0024     // fill_n(back_inserter(rightWindowFactors), 3, 2);
0025 
0026     leftWindowBorders.push_back(2.5);
0027     rightWindowBorders.push_back(3.5);
0028 
0029     leftWindowBorders.push_back(8.1);
0030     rightWindowBorders.push_back(12.2);
0031 
0032     leftWindowBorders.push_back(80.);
0033     rightWindowBorders.push_back(100.);
0034 
0035     double tempResMass[] = {91.1876, 10.3552, 10.0233, 9.4603, 3.68609, 3.0969};
0036     std::copy(tempResMass, tempResMass + 6, ResMass);
0037     double tempMassWindowHalfWidth[] = {20., 0.5, 0.5, 0.5, 0.2, 0.2};
0038     std::copy(tempMassWindowHalfWidth, tempMassWindowHalfWidth + 6, massWindowHalfWidth);
0039 
0040     backgroundHandler_ =
0041         new BackgroundHandler(identifiers, leftWindowBorders, rightWindowBorders, ResMass, massWindowHalfWidth);
0042   }
0043   void tearDown() { delete backgroundHandler_; }
0044   void testConstructor() {
0045     CPPUNIT_ASSERT(backgroundHandler_->resonanceWindow_.size() == 6);
0046 
0047     // Check that the resonance windows contain the correct masses and bounds
0048     unsigned int i = 0;
0049     for (auto const& resonanceWindow : backgroundHandler_->resonanceWindow_) {
0050       CPPUNIT_ASSERT(resonanceWindow.mass() == ResMass[i]);
0051       // Convert to float because of precision problems with doubles
0052       CPPUNIT_ASSERT(float(resonanceWindow.lowerBound()) == float(ResMass[i] - massWindowHalfWidth[i]));
0053       CPPUNIT_ASSERT(float(resonanceWindow.upperBound()) == float(ResMass[i] + massWindowHalfWidth[i]));
0054       ++i;
0055     }
0056 
0057     // Check the background windows
0058     CPPUNIT_ASSERT(backgroundHandler_->backgroundWindow_.size() == 3);
0059     // Check masses and bounds of the background windows
0060     double resMassForRegion[3];
0061     resMassForRegion[0] = ResMass[0];
0062     resMassForRegion[1] = (ResMass[1] + ResMass[2] + ResMass[3]) / 3;
0063     resMassForRegion[2] = (ResMass[4] + ResMass[5]) / 2;
0064     i = 0;
0065     for (auto const& backgroundWindow : backgroundHandler_->backgroundWindow_) {
0066       CPPUNIT_ASSERT(backgroundWindow.mass() == resMassForRegion[i]);
0067       CPPUNIT_ASSERT(float(backgroundWindow.lowerBound()) == float(leftWindowBorders[i]));
0068       CPPUNIT_ASSERT(float(backgroundWindow.upperBound()) == float(rightWindowBorders[i]));
0069       ++i;
0070     }
0071   }
0072 
0073   void testInitializeParNums() {
0074     CPPUNIT_ASSERT(backgroundHandler_->parNumsRegions_[1] ==
0075                    backgroundHandler_->backgroundWindow_[0].backgroundFunction()->parNum());
0076     CPPUNIT_ASSERT(backgroundHandler_->parNumsResonances_[3] ==
0077                    6 * (backgroundHandler_->backgroundWindow_[0].backgroundFunction()->parNum()));
0078   }
0079 
0080   void testBackgroundFunction() {
0081     bool resConsidered[6] = {false};
0082     resConsidered[5] = true;
0083     // backgroundFunctionBase * bkFun = backgroundHandler_->backgroundWindow_[0].backgroundFunction();
0084     double parval[] = {0., 0., 0., 0., 0.3, 0.8};
0085     double mass = 3.;
0086     std::pair<double, double> result = backgroundHandler_->backgroundFunction(
0087         true, parval, 6, 5, resConsidered, ResMass, massWindowHalfWidth, 1, mass, 0., 0.);
0088     double lowerBound = backgroundHandler_->backgroundWindow_[2].lowerBound();
0089     double upperBound = backgroundHandler_->backgroundWindow_[2].upperBound();
0090     CPPUNIT_ASSERT(result.first == parval[4]);
0091     CPPUNIT_ASSERT(float(result.second) == float(-parval[5] * exp(-parval[5] * mass) /
0092                                                  (exp(-parval[5] * upperBound) - exp(-parval[5] * lowerBound))));
0093   }
0094 
0095   void testSetParameters() {
0096     //double Start
0097     //backgroundHandler_.setParameters(double* Start, double* Step, double* Mini, double* Maxi, int* ind, TString* parname, const std::vector<double> & parBgr, const std::vector<int> & parBgrOrder, const int muonType)
0098   }
0099 
0100   std::vector<int> identifiers;
0101   std::vector<double> leftWindowBorders;
0102   std::vector<double> rightWindowBorders;
0103   double ResMass[6];
0104   double massWindowHalfWidth[6];
0105 
0106   BackgroundHandler* backgroundHandler_;
0107 
0108   // Declare and build the test suite
0109   CPPUNIT_TEST_SUITE(TestBackgroundHandler);
0110   CPPUNIT_TEST(testConstructor);
0111   CPPUNIT_TEST(testInitializeParNums);
0112   CPPUNIT_TEST(testBackgroundFunction);
0113   CPPUNIT_TEST(testSetParameters);
0114   CPPUNIT_TEST_SUITE_END();
0115 };
0116 
0117 // Register the test suite in the registry.
0118 // This way we will have to only pass the registry to the runner
0119 // and it will contain all the registered test suites.
0120 CPPUNIT_TEST_SUITE_REGISTRATION(TestBackgroundHandler);
0121 
0122 #endif