Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-08 23:11:02

0001 #include <Utilities/Testing/interface/CppUnit_testdriver.icpp>
0002 #include <cppunit/extensions/HelperMacros.h>
0003 
0004 #include "DataFormats/EcalDigi/interface/EcalDigiCollections.h"
0005 #include "DataFormats/Common/interface/DataFrame.h"
0006 #include "DataFormats/Common/interface/DataFrameContainer.h"
0007 
0008 #include <vector>
0009 #include <algorithm>
0010 #include <functional>
0011 
0012 template <typename DigiCollection>
0013 class TestEcalDigi : public CppUnit::TestFixture {
0014   CPPUNIT_TEST_SUITE(TestEcalDigi);
0015   CPPUNIT_TEST(default_ctor);
0016   CPPUNIT_TEST(filling);
0017   CPPUNIT_TEST(iterator);
0018 
0019   CPPUNIT_TEST_SUITE_END();
0020 
0021 public:
0022   TestEcalDigi();
0023   ~TestEcalDigi() {}
0024   void setUp() {}
0025   void tearDown() {}
0026 
0027   void default_ctor();
0028   void filling();
0029   void iterator();
0030 
0031   void fill(int stride);
0032   //  void iter(int stride);
0033 
0034 public:
0035   std::vector<edm::DataFrame::data_type> sv;
0036 };
0037 
0038 CPPUNIT_TEST_SUITE_REGISTRATION(TestEcalDigi<EBDigiCollection>);
0039 CPPUNIT_TEST_SUITE_REGISTRATION(TestEcalDigi<EEDigiCollection>);
0040 
0041 template <typename DigiCollection>
0042 TestEcalDigi<DigiCollection>::TestEcalDigi() : sv(10) {
0043   edm::DataFrame::data_type v[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
0044   std::copy(v, v + 10, sv.begin());
0045 }
0046 
0047 template <typename DigiCollection>
0048 void TestEcalDigi<DigiCollection>::default_ctor() {
0049   DigiCollection frames;
0050   CPPUNIT_ASSERT(frames.stride() == 10);
0051   // against enum in ExDetID
0052   CPPUNIT_ASSERT(frames.subdetId() == DigiCollection::DetId::Subdet);
0053   // against static metod in ExDetID
0054   CPPUNIT_ASSERT(frames.subdetId() == DigiCollection::DetId::subdet());
0055   DigiCollection smallframes(1);
0056   CPPUNIT_ASSERT(smallframes.stride() == 1);
0057 }
0058 
0059 template <typename DigiCollection>
0060 void TestEcalDigi<DigiCollection>::filling() {
0061   fill(10);
0062   fill(1);
0063 }
0064 
0065 template <typename DigiCollection>
0066 void TestEcalDigi<DigiCollection>::fill(int stride) {
0067   std::vector<edm::DataFrame::data_type> v1(stride);
0068   std::copy(sv.begin(), sv.begin() + stride, v1.begin());
0069 
0070   DigiCollection frames(stride);
0071   for (int n = 1; n < 5; ++n) {
0072     typename DigiCollection::DetId id = DigiCollection::DetId::unhashIndex(n);
0073     frames.push_back(id);
0074     CPPUNIT_ASSERT(int(frames.size()) == n);
0075     typename DigiCollection::Digi df(frames.back());
0076     CPPUNIT_ASSERT(df.size() == stride);
0077     CPPUNIT_ASSERT(df.id() == id);
0078     if (n % 2 == 0) {
0079       // modern way of filling
0080       std::copy(sv.begin(), sv.begin() + stride, df.frame().begin());
0081 
0082       std::vector<edm::DataFrame::data_type> v2(stride);
0083       std::copy(frames.m_data.begin() + (n - 1) * stride, frames.m_data.begin() + n * stride, v2.begin());
0084       CPPUNIT_ASSERT(v1 == v2);
0085     } else {
0086       // classical way of filling
0087       for (int i = 0; i < stride; i++)
0088         df.setSample(i, sv[i]);
0089 
0090       std::vector<edm::DataFrame::data_type> v2(stride);
0091       std::copy(frames.m_data.begin() + (n - 1) * stride, frames.m_data.begin() + n * stride, v2.begin());
0092       CPPUNIT_ASSERT(v1 == v2);
0093     }
0094   }
0095 }
0096 
0097 namespace {
0098   template <typename Digi>
0099   struct VerifyIter {
0100     VerifyIter(std::vector<edm::DataFrame::data_type> const& v) : n(0), sv(v) {}
0101     void operator()(Digi const& df) {
0102       typedef typename Digi::key_type DetId;
0103       ++n;
0104       DetId id = DetId::unhashIndex(n);
0105       CPPUNIT_ASSERT(df.id() == id);
0106       {
0107         std::vector<edm::DataFrame::data_type> v2(10);
0108         std::copy(df.frame().begin(), df.frame().end(), v2.begin());
0109         CPPUNIT_ASSERT(sv == v2);
0110       }
0111       {
0112         std::vector<edm::DataFrame::data_type> v2(10);
0113         for (int i = 0; i < 10; i++) {
0114           EcalMGPASample s = df.sample(i);
0115           v2[i] = s.raw();
0116         }
0117         CPPUNIT_ASSERT(sv == v2);
0118       }
0119     }
0120     int n;
0121     std::vector<edm::DataFrame::data_type> const& sv;
0122   };
0123 
0124   void verifyBarrelId(edm::DataFrame::id_type id) {
0125     try {
0126       EBDetId detid{DetId(id)};
0127     } catch (...) {
0128       bool NotBarrelID = false;
0129       CPPUNIT_ASSERT(NotBarrelID);
0130     }
0131   }
0132   void verifyEndcapId(edm::DataFrame::id_type id) {
0133     try {
0134       EEDetId detid{DetId(id)};  // detid(id) does not throw
0135     } catch (...) {
0136       bool NotEndcapID = false;
0137       CPPUNIT_ASSERT(NotEndcapID);
0138     }
0139   }
0140 
0141   //one more way
0142   struct VerifyFrame {
0143     VerifyFrame(std::vector<edm::DataFrame::data_type> const& v) : n(0), b(v), e(v) {}
0144 
0145     void operator()(edm::DataFrame const& df) {
0146       DetId id(df.id());
0147       if (id.subdetId() == EcalBarrel) {
0148         b(df);
0149         n = b.n;
0150       } else if (id.subdetId() == EcalEndcap) {
0151         e(df);
0152         n = e.n;
0153       } else {
0154         bool WrongSubdetId = false;
0155         CPPUNIT_ASSERT(WrongSubdetId);
0156       }
0157     }
0158     int n;
0159     VerifyIter<EBDataFrame> b;
0160     VerifyIter<EEDataFrame> e;
0161   };
0162 
0163   // an alternative way
0164   void iterate(EcalDigiCollection const& frames) {
0165     std::function<void(edm::DataFrame::id_type)> verifyId;
0166     if (frames.subdetId() == EcalBarrel)
0167       verifyId = verifyBarrelId;
0168     else if (frames.subdetId() == EcalEndcap)
0169       verifyId = verifyEndcapId;
0170     else {
0171       bool WrongSubdetId = false;
0172       CPPUNIT_ASSERT(WrongSubdetId);
0173     }
0174 
0175     //    std::for_each(frames.begin(),frames.end(),
0176     //        std::bind(verifyId,std::bind(&edm::DataFrame::id, std::placeholders::_1)));
0177     // same as above....
0178     for (int n = 0; n < int(frames.size()); ++n) {
0179       edm::DataFrame df = frames[n];
0180       verifyId(df.id());
0181     }
0182   }
0183 
0184 }  // namespace
0185 
0186 template <typename DigiCollection>
0187 void TestEcalDigi<DigiCollection>::iterator() {
0188   DigiCollection frames;
0189   for (int n = 1; n < 5; ++n) {
0190     typename DigiCollection::DetId id = DigiCollection::DetId::unhashIndex(n);
0191     frames.push_back(id);
0192     typename DigiCollection::Digi df(frames.back());
0193     std::copy(sv.begin(), sv.end(), df.frame().begin());
0194   }
0195   // modern
0196   CPPUNIT_ASSERT(std::for_each(frames.begin(), frames.end(), VerifyIter<typename DigiCollection::Digi>(this->sv)).n ==
0197                  4);
0198   // classical
0199   VerifyIter<typename DigiCollection::Digi> vi(sv);
0200   for (int n = 0; n < int(frames.size()); ++n) {
0201     typename DigiCollection::Digi digi(frames[n]);
0202     vi(digi);
0203   }
0204   // alternatives
0205   iterate(frames);
0206   CPPUNIT_ASSERT(std::for_each(frames.begin(), frames.end(), VerifyFrame(sv)).n == 4);
0207 }