File indexing completed on 2024-04-06 12:26:28
0001
0002 #include <functional>
0003 #include <numeric>
0004 #include <vector>
0005 #include <iostream>
0006 #include <sstream>
0007 #include <vector>
0008 #include <string>
0009 #include <memory>
0010
0011
0012 #include "FWCore/Framework/interface/Frameworkfwd.h"
0013 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0015 #include "RecoLocalTracker/SiStripClusterizer/interface/StripClusterizerAlgorithm.h"
0016 #include "RecoLocalTracker/SiStripClusterizer/interface/StripClusterizerAlgorithmFactory.h"
0017 #include "DataFormats/SiStripDigi/interface/SiStripDigi.h"
0018 #include "DataFormats/SiStripCluster/interface/SiStripCluster.h"
0019
0020 class ClusterizerUnitTester : public edm::one::EDAnalyzer<> {
0021 typedef edm::ParameterSet PSet;
0022 typedef std::vector<PSet> VPSet;
0023 typedef VPSet::const_iterator iter_t;
0024 typedef edmNew::DetSetVector<SiStripCluster> output_t;
0025
0026 public:
0027 explicit ClusterizerUnitTester(const PSet& conf);
0028 ~ClusterizerUnitTester() {}
0029
0030 private:
0031 void analyze(const edm::Event&, const edm::EventSetup&);
0032
0033 void initializeTheGroup(const PSet&, const edm::EventSetup&);
0034 void testTheGroup(const PSet&, const StripClusterizerAlgorithm*);
0035 void runTheTest(const PSet&, const StripClusterizerAlgorithm*);
0036
0037 void constructClusters(const VPSet&, output_t&);
0038 void constructDigis(const VPSet&, edmNew::DetSetVector<SiStripDigi>&);
0039
0040 static std::string printDigis(const VPSet&);
0041 static void assertIdentical(const output_t&, const output_t&);
0042 static bool clusterDSVsIdentical(const output_t&, const output_t&);
0043 static bool clusterDetSetsIdentical(const edmNew::DetSet<SiStripCluster>&, const edmNew::DetSet<SiStripCluster>&);
0044 static bool clustersIdentical(const SiStripCluster&, const SiStripCluster&);
0045 static std::string printDSV(const output_t&);
0046 static std::string printCluster(const SiStripCluster&);
0047
0048 VPSet testGroups;
0049 std::vector<std::unique_ptr<StripClusterizerAlgorithm>> clusterizers;
0050 uint32_t detId;
0051 };
0052
0053 ClusterizerUnitTester::ClusterizerUnitTester(const PSet& conf)
0054 : testGroups(conf.getParameter<VPSet>("ClusterizerTestGroups")) {
0055 for (const auto& group : testGroups) {
0056 clusterizers.push_back(StripClusterizerAlgorithmFactory::create(consumesCollector(),
0057 group.getParameter<PSet>("ClusterizerParameters")));
0058 }
0059 }
0060
0061 void ClusterizerUnitTester::analyze(const edm::Event&, const edm::EventSetup& es) {
0062 detId = 0;
0063 for (std::size_t i = 0; i != testGroups.size(); ++i) {
0064 clusterizers[i]->initialize(es);
0065 testTheGroup(testGroups[i], clusterizers[i].get());
0066 }
0067 }
0068
0069 void ClusterizerUnitTester::testTheGroup(const PSet& group, const StripClusterizerAlgorithm* clusterizer) {
0070 std::string label = group.getParameter<std::string>("Label");
0071 PSet params = group.getParameter<PSet>("ClusterizerParameters");
0072 VPSet tests = group.getParameter<VPSet>("Tests");
0073
0074 std::cout << "\nTesting group: \"" << label << "\"\n " << params << std::endl;
0075 for (const auto& test : tests) {
0076 runTheTest(test, clusterizer);
0077 detId++;
0078 }
0079 }
0080
0081 void ClusterizerUnitTester::runTheTest(const PSet& test, const StripClusterizerAlgorithm* clusterizer) {
0082 std::string label = test.getParameter<std::string>("Label");
0083 VPSet clusterset = test.getParameter<VPSet>("Clusters");
0084 VPSet digiset = test.getParameter<VPSet>("Digis");
0085 std::cout << "Testing: \"" << label << "\"\n";
0086
0087 edmNew::DetSetVector<SiStripDigi> digis;
0088 constructDigis(digiset, digis);
0089
0090 output_t expected;
0091 constructClusters(clusterset, expected);
0092
0093 output_t result;
0094 result.reserve(2 * clusterset.size(), 8 * clusterset.size());
0095
0096 try {
0097 clusterizer->clusterize(digis, result);
0098 assertIdentical(expected, result);
0099 if (test.getParameter<bool>("InvalidCharge"))
0100 throw cms::Exception("Failed") << "Charges are valid, contrary to expectation.\n";
0101 } catch (StripClusterizerAlgorithm::InvalidChargeException const&) {
0102 if (!test.getParameter<bool>("InvalidCharge"))
0103 throw;
0104 } catch (cms::Exception& e) {
0105 std::cout << (e << "Input:\n" << printDigis(digiset));
0106 }
0107 }
0108
0109 void ClusterizerUnitTester::constructDigis(const VPSet& stripset, edmNew::DetSetVector<SiStripDigi>& digis) {
0110 edmNew::DetSetVector<SiStripDigi>::TSFastFiller digisFF(digis, detId);
0111 for (iter_t strip = stripset.begin(); strip < stripset.end(); strip++) {
0112 digisFF.push_back(SiStripDigi(strip->getParameter<unsigned>("Strip"), strip->getParameter<unsigned>("ADC")));
0113 }
0114 if (digisFF.empty())
0115 digisFF.abort();
0116 }
0117
0118 void ClusterizerUnitTester::constructClusters(const VPSet& clusterset, output_t& clusters) {
0119 output_t::TSFastFiller clustersFF(clusters, detId);
0120 for (iter_t c = clusterset.begin(); c < clusterset.end(); c++) {
0121 uint16_t firststrip = c->getParameter<unsigned>("FirstStrip");
0122 std::vector<unsigned> amplitudes = c->getParameter<std::vector<unsigned>>("Amplitudes");
0123 std::vector<uint16_t> a16(amplitudes.begin(), amplitudes.end());
0124 clustersFF.push_back(SiStripCluster(firststrip, a16.begin(), a16.end()));
0125 }
0126 if (clustersFF.empty())
0127 clustersFF.abort();
0128 }
0129
0130 std::string ClusterizerUnitTester::printDigis(const VPSet& stripset) {
0131 std::stringstream s;
0132 for (iter_t strip = stripset.begin(); strip < stripset.end(); strip++) {
0133 s << "\t(" << strip->getParameter<unsigned>("Strip") << ", " << strip->getParameter<unsigned>("ADC") << ", "
0134 << strip->getParameter<double>("Noise") << ", " << strip->getParameter<double>("Gain") << ", "
0135 << (strip->getParameter<bool>("Quality") ? "good" : "bad") << " )\n";
0136 }
0137 return s.str();
0138 }
0139
0140 void ClusterizerUnitTester::assertIdentical(const output_t& L, const output_t& R) {
0141 if (!clusterDSVsIdentical(L, R))
0142 throw cms::Exception("Failed") << "Expected:\n" << printDSV(L) << "Actual:\n" << printDSV(R);
0143 }
0144
0145 bool ClusterizerUnitTester::clusterDSVsIdentical(const output_t& L, const output_t& R) {
0146 return L.size() == R.size() &&
0147 inner_product(L.begin(), L.end(), R.begin(), bool(true), std::logical_and<bool>(), clusterDetSetsIdentical);
0148 }
0149
0150 bool ClusterizerUnitTester::clusterDetSetsIdentical(const edmNew::DetSet<SiStripCluster>& L,
0151 const edmNew::DetSet<SiStripCluster>& R) {
0152 return L.size() == R.size() &&
0153 inner_product(L.begin(), L.end(), R.begin(), bool(true), std::logical_and<bool>(), clustersIdentical);
0154 }
0155
0156 bool ClusterizerUnitTester::clustersIdentical(const SiStripCluster& L, const SiStripCluster& R) {
0157 return L.firstStrip() == R.firstStrip() && L.amplitudes().size() == R.amplitudes().size() &&
0158 inner_product(L.amplitudes().begin(),
0159 L.amplitudes().end(),
0160 R.amplitudes().begin(),
0161 bool(true),
0162 std::logical_and<bool>(),
0163 std::equal_to<uint16_t>());
0164 }
0165
0166 std::string ClusterizerUnitTester::printDSV(const output_t& dsv) {
0167 std::stringstream s;
0168 for (output_t::const_iterator it = dsv.begin(); it < dsv.end(); it++)
0169 for (edmNew::DetSet<SiStripCluster>::const_iterator cluster = it->begin(); cluster < it->end(); cluster++)
0170 s << printCluster(*cluster);
0171 return s.str();
0172 }
0173
0174 std::string ClusterizerUnitTester::printCluster(const SiStripCluster& cluster) {
0175 std::stringstream s;
0176 s << "\t" << cluster.firstStrip() << " [ ";
0177 for (unsigned i = 0; i < cluster.amplitudes().size(); i++) {
0178 s << static_cast<int>(cluster.amplitudes()[i]) << " ";
0179 }
0180 s << "]" << std::endl;
0181 return s.str();
0182 }
0183
0184 #include "FWCore/Framework/interface/MakerMacros.h"
0185 DEFINE_FWK_MODULE(ClusterizerUnitTester);