Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-02-20 23:14:38

0001 #include "FWCore/Framework/interface/Frameworkfwd.h"
0002 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0003 
0004 #include "FWCore/Framework/interface/Event.h"
0005 #include "FWCore/Framework/interface/EventSetup.h"
0006 #include "FWCore/Framework/interface/MakerMacros.h"
0007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0008 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0009 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0010 #include "FWCore/Utilities/interface/InputTag.h"
0011 #include "FWCore/Utilities/interface/Exception.h"
0012 
0013 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
0014 #include "DataFormats/HcalDetId/interface/HcalTestNumbering.h"
0015 #include "SimDataFormats/CaloHit/interface/PCaloHit.h"
0016 #include "SimDataFormats/CaloHit/interface/PCaloHitContainer.h"
0017 #include "Geometry/CaloTopology/interface/HcalTopology.h"
0018 #include "Geometry/HcalCommonData/interface/HcalDDDRecConstants.h"
0019 #include "Geometry/HcalCommonData/interface/HcalHitRelabeller.h"
0020 #include "Geometry/HcalTowerAlgo/interface/HcalGeometry.h"
0021 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
0022 #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
0023 #include "Geometry/Records/interface/CaloGeometryRecord.h"
0024 #include "Geometry/Records/interface/HcalRecNumberingRecord.h"
0025 
0026 #include <memory>
0027 #include <iostream>
0028 #include <fstream>
0029 #include <vector>
0030 #include <string>
0031 
0032 class HcalTestSimHitID : public edm::one::EDAnalyzer<> {
0033 public:
0034   HcalTestSimHitID(const edm::ParameterSet& ps);
0035   ~HcalTestSimHitID() override = default;
0036   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0037 
0038 protected:
0039   void beginJob() override {}
0040   void endJob() override {}
0041   void analyze(const edm::Event& e, const edm::EventSetup& c) override;
0042 
0043 private:
0044   const std::string g4Label_, hitLab_;
0045   const bool testN_, dumpHits_;
0046   const int maxEvent_;
0047   const edm::ESGetToken<HcalDDDRecConstants, HcalRecNumberingRecord> tok_hrdd_;
0048   const edm::ESGetToken<HcalTopology, HcalRecNumberingRecord> tok_topo_;
0049   const edm::ESGetToken<CaloGeometry, CaloGeometryRecord> tok_geom_;
0050   int nevt_;
0051   edm::EDGetTokenT<edm::PCaloHitContainer> toks_calo_;
0052 };
0053 
0054 HcalTestSimHitID::HcalTestSimHitID(const edm::ParameterSet& ps)
0055     : g4Label_(ps.getUntrackedParameter<std::string>("moduleLabel", "g4SimHits")),
0056       hitLab_(ps.getUntrackedParameter<std::string>("hcCollection", "HcalHits")),
0057       testN_(ps.getUntrackedParameter<bool>("testNumbering", false)),
0058       dumpHits_(ps.getUntrackedParameter<bool>("dumpHits", false)),
0059       maxEvent_(ps.getUntrackedParameter<int>("maxEvent", 100)),
0060       tok_hrdd_(esConsumes<HcalDDDRecConstants, HcalRecNumberingRecord>()),
0061       tok_topo_(esConsumes<HcalTopology, HcalRecNumberingRecord>()),
0062       tok_geom_(esConsumes<CaloGeometry, CaloGeometryRecord>()),
0063       nevt_(0) {
0064   // register for data access
0065   toks_calo_ = consumes<edm::PCaloHitContainer>(edm::InputTag(g4Label_, hitLab_));
0066 
0067   edm::LogVerbatim("HcalSim") << "HcalTestSimHitID::Module Label: " << g4Label_ << "   Hits: " << hitLab_
0068                               << " MaxEvent: " << maxEvent_ << " Numbering scheme: " << testN_ << " (0 normal; 1 test)";
0069 }
0070 
0071 void HcalTestSimHitID::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0072   edm::ParameterSetDescription desc;
0073   desc.addUntracked<std::string>("moduleLabel", "g4SimHits");
0074   desc.addUntracked<std::string>("hcCollection", "HcalHits");
0075   desc.addUntracked<bool>("testNumbering", false);
0076   desc.addUntracked<bool>("dumpHits", false);
0077   desc.addUntracked<int>("maxEvent", 100);
0078   descriptions.add("hcalTestSimHitID", desc);
0079 }
0080 
0081 void HcalTestSimHitID::analyze(const edm::Event& e, const edm::EventSetup& iS) {
0082   ++nevt_;
0083   edm::LogVerbatim("HcalSim") << "HcalTestSimHitID::Serial # " << nevt_ << " Run # " << e.id().run() << " Event # "
0084                               << e.id().event();
0085   const HcalDDDRecConstants* hcr = &iS.getData(tok_hrdd_);
0086   const HcalTopology* theHBHETopology = &iS.getData(tok_topo_);
0087   const CaloGeometry* caloGeom = &iS.getData(tok_geom_);
0088   const HcalGeometry* hcalGeom =
0089       static_cast<const HcalGeometry*>(caloGeom->getSubdetectorGeometry(DetId::Hcal, HcalBarrel));
0090 
0091   if (nevt_ <= maxEvent_) {
0092     std::vector<PCaloHit> hcHits;
0093     const edm::Handle<edm::PCaloHitContainer>& hitsCalo = e.getHandle(toks_calo_);
0094     if (hitsCalo.isValid()) {
0095       std::vector<PCaloHit> hits;
0096       hits.insert(hits.end(), hitsCalo->begin(), hitsCalo->end());
0097       edm::LogVerbatim("HcalSim") << "HcalValidation: Hit buffer " << hits.size();
0098 
0099       //Now the testing
0100       unsigned int good(0);
0101       for (unsigned int i = 0; i < hits.size(); i++) {
0102         unsigned int id = hits[i].id();
0103         HcalDetId hid;
0104         GlobalPoint p1, p2;
0105         if (testN_) {
0106           p1 = hcalGeom->getPosition(id, true);
0107           hid = HcalDetId(HcalHitRelabeller::relabel(id, hcr));
0108         } else {
0109           hid = HcalDetId(id);
0110         }
0111         p2 = hcalGeom->getPosition(DetId(hid));
0112         if (theHBHETopology->validHcal(hid)) {
0113           ++good;
0114           if (dumpHits_) {
0115             edm::LogVerbatim("HcalSim") << "Hit[" << i << "] " << hid;
0116             if (testN_)
0117               edm::LogVerbatim("HcalSim") << "Test " << p1 << " from HcalDetId " << p2;
0118           }
0119         } else {
0120           edm::LogVerbatim("HcalSim") << "Hit[" << i << "] " << hid << " ***** ERROR *****";
0121         }
0122       }
0123       edm::LogVerbatim("HcalSim") << "\nHcalTestSimHitID:: " << good << " among " << hits.size() << " hits";
0124     }
0125   }
0126 }
0127 
0128 //define this as a plug-in
0129 DEFINE_FWK_MODULE(HcalTestSimHitID);