Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:55:04

0001 #include <cppunit/extensions/HelperMacros.h>
0002 
0003 #include <DD4hep/Filter.h>
0004 #include <memory>
0005 #include <string_view>
0006 #include <vector>
0007 
0008 #include "cppunit/TestAssert.h"
0009 #include "cppunit/TestFixture.h"
0010 
0011 using namespace dd4hep;
0012 using namespace dd4hep::dd;
0013 using namespace std;
0014 using namespace std::literals;
0015 
0016 class testFilter : public CppUnit::TestFixture {
0017   CPPUNIT_TEST_SUITE(testFilter);
0018   CPPUNIT_TEST(checkFilter);
0019   CPPUNIT_TEST_SUITE_END();
0020 
0021 public:
0022   void setUp() override;
0023   void tearDown() override {}
0024   void checkFilter();
0025 
0026 private:
0027   vector<unique_ptr<Filter>> filters_;
0028 };
0029 
0030 CPPUNIT_TEST_SUITE_REGISTRATION(testFilter);
0031 
0032 void testFilter::setUp() {
0033   vector<string_view> selections = {"//MB2P.*",
0034                                     "//MB2P.*/MB2SuperLayerPhi",
0035                                     "//MB2P.*/MB2SuperLayerPhi/MB2SLPhiLayer_.*Cells.*",
0036                                     "//MB2P.*/MB2SuperLayerPhi/MB2SLPhiLayer_.*Cells.*/MBSLPhiGas",
0037                                     "//MB2P.*/MB2SuperLayerZ",
0038                                     "//MB2P.*/MB2SuperLayerZ/MB2SLZLayer_.*Cells",
0039                                     "//MB2P.*/MB2SuperLayerZ/MB2SLZLayer_.*Cells/MB2SLZGas"};
0040   Filter* currentFilter = nullptr;
0041 
0042   for (const auto& i : selections) {
0043     vector<string_view> toks = split(i, "/");
0044     auto const& filter = find_if(begin(filters_), end(filters_), [&](auto const& f) {
0045       auto const& k = find_if(begin(f->skeys), end(f->skeys), [&](auto const& p) { return toks.front() == p; });
0046       if (k != end(f->skeys)) {
0047         currentFilter = f.get();
0048         return true;
0049       }
0050       return false;
0051     });
0052     if (filter == end(filters_)) {
0053       bool isRegex = dd4hep::dd::isRegex(toks.front());
0054       filters_.emplace_back(make_unique<Filter>());
0055       filters_.back()->isRegex.emplace_back(isRegex);
0056       filters_.back()->hasNamespace.emplace_back(dd4hep::dd::hasNamespace(toks.front()));
0057       if (isRegex) {
0058         filters_.back()->index.emplace_back(filters_.back()->keys.size());
0059         filters_.back()->keys.emplace_back(std::regex(std::begin(toks.front()), std::end(toks.front())));
0060       } else {
0061         filters_.back()->index.emplace_back(filters_.back()->skeys.size());
0062       }
0063       filters_.back()->skeys.emplace_back(toks.front());
0064       filters_.back()->up = nullptr;
0065       filters_.back()->spec = nullptr;
0066       currentFilter = filters_.back().get();
0067     }
0068     // all next levels
0069     for (size_t pos = 1; pos < toks.size(); ++pos) {
0070       if (currentFilter->next != nullptr) {
0071         currentFilter = currentFilter->next.get();
0072         auto const& l = find_if(
0073             begin(currentFilter->skeys), end(currentFilter->skeys), [&](auto const& p) { return toks[pos] == p; });
0074         if (l == end(currentFilter->skeys)) {
0075           bool isRegex = dd4hep::dd::isRegex(toks[pos]);
0076           currentFilter->isRegex.emplace_back(isRegex);
0077           currentFilter->hasNamespace.emplace_back(dd4hep::dd::hasNamespace(toks[pos]));
0078           if (isRegex) {
0079             currentFilter->index.emplace_back(currentFilter->keys.size());
0080             currentFilter->keys.emplace_back(std::regex(std::begin(toks[pos]), std::end(toks[pos])));
0081           } else {
0082             currentFilter->index.emplace_back(currentFilter->skeys.size());
0083           }
0084           currentFilter->skeys.emplace_back(toks[pos]);
0085         }
0086       } else {
0087         auto filter = std::make_unique<Filter>();
0088         bool isRegex = dd4hep::dd::isRegex(toks[pos]);
0089         filter->isRegex.emplace_back(isRegex);
0090         filter->hasNamespace.emplace_back(dd4hep::dd::hasNamespace(toks[pos]));
0091         if (isRegex) {
0092           filter->index.emplace_back(filters_.back()->keys.size());
0093           filter->keys.emplace_back(std::regex(toks[pos].begin(), toks[pos].end()));
0094         } else {
0095           filter->index.emplace_back(filters_.back()->skeys.size());
0096         }
0097         filter->skeys.emplace_back(toks[pos]);
0098         filter->next = nullptr;
0099         filter->up = currentFilter;
0100 
0101         currentFilter->next = std::move(filter);
0102       }
0103     }
0104   }
0105 }
0106 
0107 void testFilter::checkFilter() {
0108   string_view name = "MB2P.*"sv;
0109   CPPUNIT_ASSERT(std::regex_match(name.begin(), name.end(), filters_.front()->keys.front()) == 1);
0110   CPPUNIT_ASSERT(filters_.front()->up == nullptr);
0111   CPPUNIT_ASSERT(filters_.front()->next != nullptr);
0112   CPPUNIT_ASSERT(filters_.size() == 1);
0113 
0114   Filter* current = nullptr;
0115   for (auto const& i : filters_) {
0116     current = i.get();
0117     do {
0118       for (auto const& i : current->skeys) {
0119         std::cout << i << ", ";
0120       }
0121       std::cout << "\n";
0122       current = current->next.get();
0123     } while (current != nullptr);
0124   }
0125 }