Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:11:04

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