Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:15

0001 #include "FWCore/SOA/interface/Table.h"
0002 #include "FWCore/SOA/interface/TableView.h"
0003 #include "FWCore/SOA/interface/Column.h"
0004 
0005 #include <cppunit/extensions/HelperMacros.h>
0006 
0007 class testTableFilling : public CppUnit::TestFixture {
0008   CPPUNIT_TEST_SUITE(testTableFilling);
0009 
0010   CPPUNIT_TEST(soaDeclareDefaultTest1);
0011   CPPUNIT_TEST(soaDeclareDefaultTest2);
0012   CPPUNIT_TEST(soaDeclareDefaultTest3);
0013   CPPUNIT_TEST(soaDeclareDefaultTest4);
0014   CPPUNIT_TEST(soaDeclareDefaultTest5);
0015   CPPUNIT_TEST_SUITE_END();
0016 
0017 public:
0018   void setUp() {}
0019   void tearDown() {}
0020 
0021   void soaDeclareDefaultTest1();
0022   void soaDeclareDefaultTest2();
0023   void soaDeclareDefaultTest3();
0024   void soaDeclareDefaultTest4();
0025   void soaDeclareDefaultTest5();
0026 };
0027 
0028 namespace ts {
0029 
0030   namespace col {
0031     SOA_DECLARE_COLUMN(Eta, float, "eta");
0032     SOA_DECLARE_COLUMN(Phi, float, "phi");
0033     SOA_DECLARE_COLUMN(Energy, float, "energy");
0034     SOA_DECLARE_COLUMN(ID, int, "id");
0035     SOA_DECLARE_COLUMN(Label, std::string, "label");
0036 
0037     SOA_DECLARE_COLUMN(Px, double, "p_x");
0038     SOA_DECLARE_COLUMN(Py, double, "p_y");
0039     SOA_DECLARE_COLUMN(Pz, double, "p_z");
0040   }  // namespace col
0041 
0042   using EtaPhiTable = edm::soa::Table<ts::col::Eta, ts::col::Phi>;
0043   using EtaPhiTableView = edm::soa::ViewFromTable_t<EtaPhiTable>;
0044 }  // namespace ts
0045 
0046 namespace edm::soa {
0047 
0048   // The SOA_DECLARE_DEFAULT macro should be used in the edm::soa namespace
0049   SOA_DECLARE_DEFAULT(ts::col::Eta, eta());
0050   SOA_DECLARE_DEFAULT(ts::col::Phi, phi());
0051   SOA_DECLARE_DEFAULT(ts::col::Energy, energy());
0052   SOA_DECLARE_DEFAULT(ts::col::ID, id());
0053   SOA_DECLARE_DEFAULT(ts::col::Label, label());
0054   SOA_DECLARE_DEFAULT(ts::col::Px, px());
0055   SOA_DECLARE_DEFAULT(ts::col::Py, py());
0056   SOA_DECLARE_DEFAULT(ts::col::Pz, pz());
0057 
0058 }  // namespace edm::soa
0059 
0060 ///registration of the test so that the runner can find it
0061 CPPUNIT_TEST_SUITE_REGISTRATION(testTableFilling);
0062 
0063 namespace {
0064   template <class Object>
0065   void validateEtaPhiTable(ts::EtaPhiTableView table, std::vector<Object> const& objects) {
0066     int iRow = 0;
0067     for (auto const& row : table) {
0068       CPPUNIT_ASSERT(row.get<ts::col::Eta>() == objects[iRow].eta_);
0069       CPPUNIT_ASSERT(row.get<ts::col::Phi>() == objects[iRow].phi_);
0070       ++iRow;
0071     }
0072   }
0073 }  // namespace
0074 
0075 struct JetType1 {
0076   // jet type that is fully compatible with the
0077   // SOA_DECLARE_DEFAULT-generated value_for_column functions
0078   auto eta() const { return eta_; }
0079   auto phi() const { return phi_; }
0080 
0081   const float eta_;
0082   const float phi_;
0083 };
0084 
0085 void testTableFilling::soaDeclareDefaultTest1() {
0086   std::vector<JetType1> jets = {{1., 3.14}, {2., 0.}, {4., 1.3}};
0087   std::vector<std::string> labels = {{"jet0", "jet1", "jet2"}};
0088 
0089   ts::EtaPhiTable table(jets);
0090 
0091   validateEtaPhiTable(table, jets);
0092 }
0093 
0094 struct JetType2 {
0095   // jet type that is NOT compatible with the SOA_DECLARE_DEFAULT-generated
0096   // value_for_column functions...
0097   auto eta() const { return eta_; }
0098 
0099   const float eta_;
0100   const float phi_;
0101 };
0102 
0103 // ...so we need to write our own value_for_column function for JetType2
0104 double value_for_column(JetType2 const& iJ, ts::col::Phi*) { return iJ.phi_; }
0105 
0106 void testTableFilling::soaDeclareDefaultTest2() {
0107   std::vector<JetType2> jets = {{1., 3.14}, {2., 0.}, {4., 1.3}};
0108   std::vector<std::string> labels = {{"jet0", "jet1", "jet2"}};
0109 
0110   ts::EtaPhiTable table(jets);
0111 
0112   validateEtaPhiTable(table, jets);
0113 }
0114 
0115 namespace ts::reco {
0116 
0117   struct JetType3 {
0118     auto eta() const { return eta_; }
0119 
0120     const float eta_;
0121     const float phi_;
0122   };
0123 
0124   double value_for_column(ts::reco::JetType3 const& iJ, ts::col::Phi*) { return iJ.phi_; }
0125 }  // namespace ts::reco
0126 
0127 // same tests as for JetType2 but with the type and the value_for_column
0128 // defined in their own test namespace
0129 void testTableFilling::soaDeclareDefaultTest3() {
0130   std::vector<ts::reco::JetType3> jets = {{1., 3.14}, {2., 0.}, {4., 1.3}};
0131   std::vector<std::string> labels = {{"jet0", "jet1", "jet2"}};
0132 
0133   ts::EtaPhiTable table(jets);
0134 
0135   validateEtaPhiTable(table, jets);
0136 }
0137 
0138 struct JetType4 {
0139   // jet type that is compatible with the SOA_DECLARE_DEFAULT-generated
0140   // value_for_column functions, but it would not give the right result.
0141   // This is to check that the custom value_for_column defined below is
0142   // prioritized.
0143   auto eta() const { return eta_; }
0144   auto phi() const { return 0.f; }
0145 
0146   const float eta_;
0147   const float phi_;
0148 };
0149 
0150 double value_for_column(JetType4 const& iJ, ts::col::Phi*) { return iJ.phi_; }
0151 
0152 void testTableFilling::soaDeclareDefaultTest4() {
0153   std::vector<JetType4> jets = {{1., 3.14}, {2., 0.}, {4., 1.3}};
0154   std::vector<std::string> labels = {{"jet0", "jet1", "jet2"}};
0155 
0156   ts::EtaPhiTable table(jets);
0157 
0158   validateEtaPhiTable(table, jets);
0159 }
0160 
0161 namespace ts::reco {
0162 
0163   struct JetType5 {
0164     // jet type that is compatible with the SOA_DECLARE_DEFAULT-generated
0165     // value_for_column functions, but it would not give the right result.
0166     // This is to check that the custom value_for_column defined below is
0167     // prioritized.
0168     auto eta() const { return eta_; }
0169     auto phi() const { return 0.f; }
0170 
0171     const float eta_;
0172     const float phi_;
0173   };
0174 
0175   double value_for_column(ts::reco::JetType5 const& iJ, ts::col::Phi*) { return iJ.phi_; }
0176 
0177 }  // namespace ts::reco
0178 
0179 void testTableFilling::soaDeclareDefaultTest5() {
0180   std::vector<ts::reco::JetType5> jets = {{1., 3.14}, {2., 0.}, {4., 1.3}};
0181   std::vector<std::string> labels = {{"jet0", "jet1", "jet2"}};
0182 
0183   ts::EtaPhiTable table(jets);
0184 
0185   validateEtaPhiTable(table, jets);
0186 }
0187 
0188 #include <Utilities/Testing/interface/CppUnit_testdriver.icpp>