Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-04-27 23:38:24

0001 // system includes
0002 #include <sstream>
0003 
0004 // user includes
0005 #include "DataFormats/Common/interface/DetSetVector.h"
0006 #include "DataFormats/Common/interface/DetSetVectorNew.h"
0007 #include "DataFormats/Common/interface/Handle.h"
0008 #include "DataFormats/SiStripCluster/interface/SiStripCluster.h"
0009 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0010 #include "FWCore/Framework/interface/Event.h"
0011 #include "FWCore/Framework/interface/EventSetup.h"
0012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0014 #include "FWCore/Utilities/interface/InputTag.h"
0015 
0016 class SiStripClusterValidator : public edm::one::EDAnalyzer<> {
0017 public:
0018   SiStripClusterValidator(const edm::ParameterSet& config);
0019   ~SiStripClusterValidator() override = default;
0020   virtual void endJob() override;
0021   virtual void analyze(const edm::Event& event, const edm::EventSetup& setup) override;
0022   void validate(const edm::DetSetVector<SiStripCluster>&, const edm::DetSetVector<SiStripCluster>&);
0023   void validate(const edmNew::DetSetVector<SiStripCluster>&, const edmNew::DetSetVector<SiStripCluster>&);
0024 
0025 private:
0026   inline const std::string& header() { return header_; }
0027 
0028   /// Input collections
0029   const edm::InputTag collection1Tag_;
0030   const edm::InputTag collection2Tag_;
0031   const bool dsvnew_;
0032   /// used to remember if there have been errors for message in endJob
0033   bool errors_;
0034 
0035   std::string header_;
0036 };
0037 
0038 std::ostream& operator<<(std::ostream&, const edmNew::DetSetVector<SiStripCluster>&);
0039 std::ostream& operator<<(std::ostream&, const edm::DetSetVector<SiStripCluster>&);
0040 
0041 SiStripClusterValidator::SiStripClusterValidator(const edm::ParameterSet& conf)
0042     : collection1Tag_(conf.getUntrackedParameter<edm::InputTag>("Collection1")),
0043       collection2Tag_(conf.getUntrackedParameter<edm::InputTag>("Collection2")),
0044       dsvnew_(conf.getUntrackedParameter<bool>("DetSetVectorNew", true)),
0045       errors_(false),
0046       header_() {
0047   std::stringstream ss;
0048   ss << " Collection1: "
0049      << " Type:Label:Instance:Process=\"" << (dsvnew_ ? "edmNew::DSV<SiStripClusters>" : "edm::DSV<SiStripClusters>")
0050      << ":" << collection1Tag_.label() << ":" << collection1Tag_.instance() << ":" << collection1Tag_.process() << "\""
0051      << std::endl
0052      << " Collection2: "
0053      << " Type:Label:Instance:Process=\"" << (dsvnew_ ? "edmNew::DSV<SiStripClusters>" : "edm::DSV<SiStripClusters>")
0054      << ":" << collection2Tag_.label() << ":" << collection2Tag_.instance() << ":" << collection2Tag_.process() << "\""
0055      << std::endl;
0056   header_ = ss.str();
0057   if (dsvnew_) {
0058     consumes<edmNew::DetSetVector<SiStripCluster> >(collection1Tag_);
0059     consumes<edmNew::DetSetVector<SiStripCluster> >(collection2Tag_);
0060   } else {
0061     consumes<edm::DetSetVector<SiStripCluster> >(collection1Tag_);
0062     consumes<edm::DetSetVector<SiStripCluster> >(collection2Tag_);
0063   }
0064 }
0065 
0066 void SiStripClusterValidator::endJob() {
0067   std::stringstream ss;
0068   ss << "[SiStripClusterValidator::" << __func__ << "]" << std::endl << header();
0069 
0070   if (!errors_) {
0071     ss << "Collections are identical in every event (assuming no exceptions thrown!)" << std::endl;
0072   } else {
0073     ss << "Differences were found" << std::endl;
0074   }
0075 
0076   if (!errors_) {
0077     edm::LogVerbatim("SiStripDigiValidator") << ss.str();
0078   } else {
0079     edm::LogError("SiStripDigiValidator") << ss.str();
0080   }
0081 }
0082 
0083 void SiStripClusterValidator::analyze(const edm::Event& event, const edm::EventSetup& setup) {
0084   if (dsvnew_) {
0085     edm::Handle<edmNew::DetSetVector<SiStripCluster> > collection1Handle;
0086     event.getByLabel(collection1Tag_, collection1Handle);
0087     edm::Handle<edmNew::DetSetVector<SiStripCluster> > collection2Handle;
0088     event.getByLabel(collection2Tag_, collection2Handle);
0089     validate(*collection1Handle, *collection2Handle);
0090   } else {
0091     edm::Handle<edm::DetSetVector<SiStripCluster> > collection1Handle;
0092     event.getByLabel(collection1Tag_, collection1Handle);
0093     edm::Handle<edm::DetSetVector<SiStripCluster> > collection2Handle;
0094     event.getByLabel(collection2Tag_, collection2Handle);
0095     validate(*collection1Handle, *collection2Handle);
0096   }
0097 }
0098 
0099 void SiStripClusterValidator::validate(const edmNew::DetSetVector<SiStripCluster>& collection1,
0100                                        const edmNew::DetSetVector<SiStripCluster>& collection2) {
0101   /// check number of DetSets
0102 
0103   if (collection1.size() != collection2.size()) {
0104     std::stringstream ss;
0105     ss << "[SiStripClusterValidator::" << __func__ << "]" << std::endl
0106        << header() << "Collection sizes do not match! (" << collection1.size() << " and " << collection2.size() << ")";
0107     edm::LogError("SiStripClusterValidator") << ss.str();
0108     errors_ = true;
0109     return;
0110   }
0111 
0112   /// loop over first collection DetSets comparing them to same DetSet in other collection
0113 
0114   edmNew::DetSetVector<SiStripCluster>::const_iterator iDetSet1 = collection1.begin();
0115   edmNew::DetSetVector<SiStripCluster>::const_iterator jDetSet1 = collection1.end();
0116   for (; iDetSet1 != jDetSet1; ++iDetSet1) {
0117     /// check that it exists
0118 
0119     edm::det_id_type id = iDetSet1->detId();
0120     edmNew::DetSetVector<SiStripCluster>::const_iterator iDetSet2 = collection2.find(id);
0121     if (iDetSet2 == collection2.end()) {
0122       std::stringstream ss;
0123       ss << "[SiStripClusterValidator::" << __func__ << "]" << std::endl
0124          << header() << "DetSet in collection 1 with id " << id << " is missing from collection 2!";
0125       edm::LogError("SiStripClusterValidator") << ss.str();
0126       errors_ = true;
0127       return;
0128     }
0129 
0130     /// check that the clusters are identical
0131 
0132     edmNew::DetSet<SiStripCluster>::const_iterator iCluster1 = iDetSet1->begin();
0133     edmNew::DetSet<SiStripCluster>::const_iterator iCluster2 = iDetSet2->begin();
0134     edmNew::DetSet<SiStripCluster>::const_iterator jCluster2 = iDetSet2->end();
0135     for (; iCluster2 != jCluster2; ++iCluster2) {
0136       if (std::equal(iCluster1->amplitudes().begin(), iCluster1->amplitudes().end(), iCluster2->amplitudes().begin()) &&
0137           iCluster1->firstStrip() == iCluster2->firstStrip())
0138         iCluster1++;
0139     }
0140 
0141     if (iCluster1 != iDetSet1->end()) {
0142       std::stringstream ss;
0143       ss << "[SiStripClusterValidator::" << __func__ << "]" << std::endl
0144          << header() << "No match for cluster in detector " << id << " with first strip number "
0145          << iCluster1->firstStrip();
0146       edm::LogError("SiStripClusterValidator") << ss.str();
0147       errors_ = true;
0148     }
0149   }
0150 }
0151 
0152 void SiStripClusterValidator::validate(const edm::DetSetVector<SiStripCluster>& collection1,
0153                                        const edm::DetSetVector<SiStripCluster>& collection2) {
0154   /// check number of DetSets
0155 
0156   if (collection1.size() != collection2.size()) {
0157     std::stringstream ss;
0158     ss << "[SiStripClusterValidator::" << __func__ << "]" << std::endl
0159        << header() << "Collection sizes do not match! (" << collection1.size() << " and " << collection2.size() << ")";
0160     errors_ = true;
0161     return;
0162   }
0163 
0164   /// loop over first collection DetSets comparing them to same DetSet in other collection
0165 
0166   edm::DetSetVector<SiStripCluster>::const_iterator iDetSet1 = collection1.begin();
0167   edm::DetSetVector<SiStripCluster>::const_iterator jDetSet1 = collection1.end();
0168   for (; iDetSet1 != jDetSet1; ++iDetSet1) {
0169     /// check that it exists
0170 
0171     edm::det_id_type id = iDetSet1->detId();
0172     edm::DetSetVector<SiStripCluster>::const_iterator iDetSet2 = collection2.find(id);
0173     if (iDetSet2 == collection2.end()) {
0174       std::stringstream ss;
0175       ss << "[SiStripClusterValidator::" << __func__ << "]" << std::endl
0176          << header() << "DetSet in collection 1 with id " << id << " is missing from collection 2!";
0177       edm::LogError("SiStripClusterValidator") << ss.str();
0178       errors_ = true;
0179       return;
0180     }
0181 
0182     /// check that the clusters are identical
0183 
0184     edm::DetSet<SiStripCluster>::const_iterator iCluster1 = iDetSet1->begin();
0185     edm::DetSet<SiStripCluster>::const_iterator iCluster2 = iDetSet2->begin();
0186     edm::DetSet<SiStripCluster>::const_iterator jCluster2 = iDetSet2->end();
0187     for (; iCluster2 != jCluster2; ++iCluster2) {
0188       if (std::equal(iCluster1->amplitudes().begin(), iCluster1->amplitudes().end(), iCluster2->amplitudes().begin()) &&
0189           iCluster1->firstStrip() == iCluster2->firstStrip())
0190         iCluster1++;
0191     }
0192 
0193     if (iCluster1 != iDetSet1->end()) {
0194       std::stringstream ss;
0195       ss << "[SiStripClusterValidator::" << __func__ << "]" << std::endl
0196          << header() << "No match for cluster in detector " << id << " with first strip number "
0197          << iCluster1->firstStrip();
0198       edm::LogError("SiStripClusterValidator") << ss.str();
0199       errors_ = true;
0200     }
0201   }
0202 }
0203 
0204 /// Debug for SiStripCluster collection
0205 
0206 std::ostream& operator<<(std::ostream& ss, const edmNew::DetSetVector<SiStripCluster>& clusters) {
0207   edmNew::DetSetVector<SiStripCluster>::const_iterator ids = clusters.begin();
0208   edmNew::DetSetVector<SiStripCluster>::const_iterator jds = clusters.end();
0209   for (; ids != jds; ++ids) {
0210     std::stringstream sss;
0211     uint16_t nd = 0;
0212     edmNew::DetSet<SiStripCluster>::const_iterator id = ids->begin();
0213     edmNew::DetSet<SiStripCluster>::const_iterator jd = ids->end();
0214     for (; id != jd; ++id) {
0215       nd++;
0216       if (uint16_t(id - ids->begin()) < 10) {
0217         sss << uint16_t(id - ids->begin()) << "/" << id->firstStrip() << "/" << id->amplitudes().size() << ", ";
0218       }
0219     }
0220     std::stringstream ss;
0221     ss << "[SiStripClusterValidator::" << __func__ << "]"
0222        << " DetId: " << ids->detId() << " size: " << ids->size() << " clusters: " << nd
0223        << " index/first-strip/width: " << sss.str() << std::endl;
0224     edm::LogError("SiStripClusterValidator") << ss.str();
0225   }
0226   return ss;
0227 }
0228 
0229 /// Debug for SiStripCluster collection
0230 
0231 std::ostream& operator<<(std::ostream& ss, const edm::DetSetVector<SiStripCluster>& clusters) {
0232   edm::DetSetVector<SiStripCluster>::const_iterator ids = clusters.begin();
0233   edm::DetSetVector<SiStripCluster>::const_iterator jds = clusters.end();
0234   for (; ids != jds; ++ids) {
0235     std::stringstream sss;
0236     uint16_t nd = 0;
0237     edm::DetSet<SiStripCluster>::const_iterator id = ids->begin();
0238     edm::DetSet<SiStripCluster>::const_iterator jd = ids->end();
0239     for (; id != jd; ++id) {
0240       nd++;
0241       if (uint16_t(id - ids->begin()) < 10) {
0242         sss << uint16_t(id - ids->begin()) << "/" << id->firstStrip() << "/" << id->amplitudes().size() << ", ";
0243       }
0244     }
0245     std::stringstream ss;
0246     ss << "[SiStripClusterValidator::" << __func__ << "]"
0247        << " DetId: " << ids->detId() << " size: " << ids->size() << " clusters: " << nd
0248        << " index/first-strip/width: " << sss.str() << std::endl;
0249     edm::LogError("SiStripClusterValidator") << ss.str();
0250   }
0251   return ss;
0252 }
0253 
0254 #include "FWCore/PluginManager/interface/ModuleDef.h"
0255 #include "FWCore/Framework/interface/MakerMacros.h"
0256 DEFINE_FWK_MODULE(SiStripClusterValidator);