Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-10-01 01:02:32

0001 // system include files
0002 #include <cmath>
0003 #include <iostream>
0004 #include <fstream>
0005 #include <vector>
0006 #include <map>
0007 #include <string>
0008 
0009 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0010 
0011 #include "DataFormats/ForwardDetId/interface/ForwardSubdetector.h"
0012 #include "DataFormats/ForwardDetId/interface/HFNoseDetId.h"
0013 #include "DataFormats/ForwardDetId/interface/HGCalDetId.h"
0014 #include "DataFormats/ForwardDetId/interface/HGCSiliconDetId.h"
0015 #include "DataFormats/ForwardDetId/interface/HGCScintillatorDetId.h"
0016 #include "DataFormats/HGCDigi/interface/HGCDigiCollections.h"
0017 
0018 #include "FWCore/Framework/interface/Frameworkfwd.h"
0019 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0020 #include "FWCore/Framework/interface/Event.h"
0021 #include "FWCore/Framework/interface/EventSetup.h"
0022 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0023 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0024 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0025 #include "FWCore/ServiceRegistry/interface/Service.h"
0026 #include "FWCore/Utilities/interface/transform.h"
0027 #include "FWCore/Utilities/interface/InputTag.h"
0028 
0029 #include "Geometry/HGCalCommonData/interface/HGCalGeometryMode.h"
0030 #include "Geometry/HGCalCommonData/interface/HGCalDDDConstants.h"
0031 #include "Geometry/HGCalGeometry/interface/HGCalGeometry.h"
0032 #include "Geometry/Records/interface/IdealGeometryRecord.h"
0033 
0034 #include "SimDataFormats/CaloHit/interface/PCaloHitContainer.h"
0035 #include "SimDataFormats/CaloTest/interface/HGCalTestNumbering.h"
0036 #include "SimDataFormats/CaloHit/interface/PCaloHit.h"
0037 
0038 #include "CLHEP/Geometry/Point3D.h"
0039 #include "CLHEP/Geometry/Transform3D.h"
0040 #include "CLHEP/Geometry/Vector3D.h"
0041 #include "CLHEP/Units/GlobalSystemOfUnits.h"
0042 #include "CLHEP/Units/GlobalPhysicalConstants.h"
0043 
0044 #include "TH2D.h"
0045 
0046 class HGCalWaferStudy : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::SharedResources> {
0047 public:
0048   explicit HGCalWaferStudy(const edm::ParameterSet&);
0049   ~HGCalWaferStudy() override = default;
0050   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0051 
0052 protected:
0053   void beginJob() override {}
0054   void beginRun(edm::Run const&, edm::EventSetup const&) override;
0055   void analyze(edm::Event const&, edm::EventSetup const&) override;
0056   void endRun(edm::Run const&, edm::EventSetup const&) override {}
0057 
0058 private:
0059   void analyzeHits(int, const std::string&, const std::vector<PCaloHit>&);
0060 
0061   // ----------member data ---------------------------
0062   const std::vector<std::string> nameDetectors_, caloHitSources_;
0063   const std::vector<edm::InputTag> digiSources_;
0064   const std::vector<edm::ESGetToken<HGCalDDDConstants, IdealGeometryRecord>> ddTokens_;
0065   const std::vector<edm::ESGetToken<HGCalGeometry, IdealGeometryRecord>> geomTokens_;
0066   const int verbosity_, nBinHit_, nBinDig_;
0067   const double xyMinHit_, xyMaxHit_;
0068   const double xyMinDig_, xyMaxDig_;
0069   const bool ifNose_;
0070   std::vector<int> layerMnSim_, layerMxSim_;
0071   std::vector<int> layerMnDig_, layerMxDig_;
0072   std::vector<int> layerSim_, layerDig_, layerFront_;
0073   std::vector<const HGCalDDDConstants*> hgcons_;
0074   std::vector<const HGCalGeometry*> hgeoms_;
0075   std::vector<edm::EDGetTokenT<edm::PCaloHitContainer>> tok_hits_;
0076   std::vector<edm::EDGetTokenT<HGCalDigiCollection>> tok_digi_;
0077 
0078   //histogram related stuff
0079   static const int nType = 2;
0080   std::vector<TH2D*> h_XYsi1_[nType], h_XYsi2_[nType];
0081   std::vector<TH2D*> h_XYdi1_[nType], h_XYdi2_[nType];
0082 };
0083 
0084 HGCalWaferStudy::HGCalWaferStudy(const edm::ParameterSet& iConfig)
0085     : nameDetectors_(iConfig.getParameter<std::vector<std::string>>("detectorNames")),
0086       caloHitSources_(iConfig.getParameter<std::vector<std::string>>("caloHitSources")),
0087       digiSources_(iConfig.getParameter<std::vector<edm::InputTag>>("digiSources")),
0088       ddTokens_{
0089           edm::vector_transform(nameDetectors_,
0090                                 [this](const std::string& name) {
0091                                   return esConsumes<HGCalDDDConstants, IdealGeometryRecord, edm::Transition::BeginRun>(
0092                                       edm::ESInputTag{"", name});
0093                                 })},
0094       geomTokens_{edm::vector_transform(
0095           nameDetectors_,
0096           [this](const std::string& name) {
0097             return esConsumes<HGCalGeometry, IdealGeometryRecord, edm::Transition::BeginRun>(edm::ESInputTag{"", name});
0098           })},
0099       verbosity_(iConfig.getUntrackedParameter<int>("verbosity", 0)),
0100       nBinHit_(iConfig.getUntrackedParameter<int>("nBinHit", 600)),
0101       nBinDig_(iConfig.getUntrackedParameter<int>("nBinDig", 600)),
0102       xyMinHit_(iConfig.getUntrackedParameter<double>("xyMinHit", -3000.0)),
0103       xyMaxHit_(iConfig.getUntrackedParameter<double>("xyMaxHit", 3000.0)),
0104       xyMinDig_(iConfig.getUntrackedParameter<double>("xyMinDig", -300.0)),
0105       xyMaxDig_(iConfig.getUntrackedParameter<double>("xyMaxDig", 300.0)),
0106       ifNose_(iConfig.getUntrackedParameter<bool>("ifNose", false)),
0107       layerMnSim_(iConfig.getUntrackedParameter<std::vector<int>>("layerMinSim")),
0108       layerMxSim_(iConfig.getUntrackedParameter<std::vector<int>>("layerMaxSim")),
0109       layerMnDig_(iConfig.getUntrackedParameter<std::vector<int>>("layerMinDig")),
0110       layerMxDig_(iConfig.getUntrackedParameter<std::vector<int>>("layerMaxDig")) {
0111   usesResource(TFileService::kSharedResource);
0112 
0113   for (auto const& source : caloHitSources_) {
0114     tok_hits_.emplace_back(consumes<edm::PCaloHitContainer>(edm::InputTag("g4SimHits", source)));
0115     edm::LogVerbatim("HGCalValidation") << "SimHitSource: " << source;
0116   }
0117   for (auto const& source : digiSources_) {
0118     tok_digi_.emplace_back(consumes<HGCalDigiCollection>(source));
0119     edm::LogVerbatim("HGCalValidation") << "DigiSource: " << source;
0120   }
0121 }
0122 
0123 void HGCalWaferStudy::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0124   edm::ParameterSetDescription desc;
0125   std::vector<std::string> names = {"HGCalEESensitive", "HGCalHESiliconSensitive"};
0126   std::vector<std::string> simSources = {"HGCHitsEE", "HGCHitsHEfront"};
0127   std::vector<edm::InputTag> digSources = {edm::InputTag("simHGCalUnsuppressedDigis", "EE"),
0128                                            edm::InputTag("simHGCalUnsuppressedDigis", "HEfront")};
0129   std::vector<int> layers = {28, 24};
0130   std::vector<int> layerMin = {1, 1};
0131   desc.add<std::vector<std::string>>("detectorNames", names);
0132   desc.add<std::vector<std::string>>("caloHitSources", simSources);
0133   desc.add<std::vector<edm::InputTag>>("digiSources", digSources);
0134   desc.addUntracked<int>("verbosity", 0);
0135   desc.addUntracked<int>("nBinHit", 600);
0136   desc.addUntracked<int>("nBinDig", 600);
0137   desc.addUntracked<double>("xyMinHit", -3000.0);
0138   desc.addUntracked<double>("xyMaxHit", 3000.0);
0139   desc.addUntracked<double>("xyMinDig", -300.0);
0140   desc.addUntracked<double>("xyMaxDig", 300.0);
0141   desc.addUntracked<bool>("ifNose", false);
0142   desc.addUntracked<std::vector<int>>("layerMaxSim", layers);
0143   desc.addUntracked<std::vector<int>>("layerMaxDig", layers);
0144   desc.addUntracked<std::vector<int>>("layerMinSim", layerMin);
0145   desc.addUntracked<std::vector<int>>("layerMinDig", layerMin);
0146   descriptions.add("hgcalWaferStudy", desc);
0147 }
0148 
0149 void HGCalWaferStudy::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0150   //First the hits
0151   for (unsigned int k = 0; k < tok_hits_.size(); ++k) {
0152     const edm::Handle<edm::PCaloHitContainer>& theCaloHitContainers = iEvent.getHandle(tok_hits_[k]);
0153     if (theCaloHitContainers.isValid()) {
0154       if (verbosity_ > 0)
0155         edm::LogVerbatim("HGCalValidation")
0156             << " PcalohitItr = " << theCaloHitContainers->size() << " Geometry Pointer " << hgcons_[k] << " # of Hists "
0157             << h_XYsi1_[k].size() << ":" << h_XYsi2_[k].size();
0158       int kount(0);
0159       for (auto const& hit : *(theCaloHitContainers)) {
0160         unsigned int id = hit.id();
0161         std::pair<float, float> xy;
0162         int layer(0), zside(1);
0163         bool wvtype(true);
0164         if (ifNose_) {
0165           HFNoseDetId detId = HFNoseDetId(id);
0166           layer = detId.layer();
0167           zside = detId.zside();
0168           wvtype = hgcons_[k]->waferVirtual(layer, detId.waferU(), detId.waferV());
0169           xy = hgcons_[k]->locateCell(layer, detId.waferU(), detId.waferV(), detId.cellU(), detId.cellV(), false, true);
0170         } else if (hgcons_[k]->waferHexagon8()) {
0171           HGCSiliconDetId detId = HGCSiliconDetId(id);
0172           layer = detId.layer();
0173           zside = detId.zside();
0174           wvtype = hgcons_[k]->waferVirtual(layer, detId.waferU(), detId.waferV());
0175           xy = hgcons_[k]->locateCell(layer, detId.waferU(), detId.waferV(), detId.cellU(), detId.cellV(), false, true);
0176         } else {
0177           int subdet, sector, type, cell;
0178           HGCalTestNumbering::unpackHexagonIndex(id, subdet, zside, layer, sector, type, cell);
0179           xy = hgcons_[k]->locateCell(cell, layer, sector, false);
0180           wvtype = hgcons_[k]->waferVirtual(layer, sector, 0);
0181         }
0182         double xp = (zside < 0) ? -xy.first : xy.first;
0183         double yp = xy.second;
0184         int ll = layer - layerMnSim_[k];
0185         if (verbosity_ > 1)
0186           edm::LogVerbatim("HGCalValidation")
0187               << "Hit [" << kount << "] Layer " << layer << ":" << ll << " x|y " << xp << ":" << yp << " Type "
0188               << wvtype << " Hist pointers " << h_XYsi1_[k][ll] << ":" << h_XYsi2_[k][ll];
0189         if (layer >= layerMnSim_[k] && layer <= layerMxSim_[k]) {
0190           if (wvtype)
0191             h_XYsi2_[k][ll]->Fill(xp, yp);
0192           else
0193             h_XYsi1_[k][ll]->Fill(xp, yp);
0194         }
0195         ++kount;
0196       }
0197     } else if (verbosity_ > 0) {
0198       edm::LogVerbatim("HGCalValidation") << "PCaloHitContainer does not "
0199                                           << "exist for " << nameDetectors_[k];
0200     }
0201   }
0202 
0203   //Then the digis
0204   for (unsigned int k = 0; k < tok_digi_.size(); ++k) {
0205     const edm::Handle<HGCalDigiCollection>& theHGCDigiContainer = iEvent.getHandle(tok_digi_[k]);
0206     if (theHGCDigiContainer.isValid()) {
0207       if (verbosity_ > 0)
0208         edm::LogVerbatim("HGCalValidation")
0209             << nameDetectors_[k] << " with " << theHGCDigiContainer->size() << " element(s) Geometry Pointer "
0210             << hgeoms_[k] << " # of Hists " << h_XYdi1_[k].size() << ":" << h_XYdi2_[k].size();
0211       int kount(0);
0212       for (const auto& it : *(theHGCDigiContainer.product())) {
0213         DetId id = it.id();
0214         int layer(0);
0215         bool wvtype(true);
0216         if (ifNose_) {
0217           HFNoseDetId detId = HFNoseDetId(id);
0218           layer = detId.layer();
0219           wvtype = hgcons_[k]->waferVirtual(layer, detId.waferU(), detId.waferV());
0220         } else if (hgcons_[k]->waferHexagon8()) {
0221           HGCSiliconDetId detId = HGCSiliconDetId(id);
0222           layer = detId.layer();
0223           wvtype = hgcons_[k]->waferVirtual(layer, detId.waferU(), detId.waferV());
0224         } else {
0225           HGCalDetId detId = HGCalDetId(id);
0226           layer = detId.layer();
0227           int wafer = detId.wafer();
0228           wvtype = hgcons_[k]->waferVirtual(layer, wafer, 0);
0229         }
0230         int ll = layer - layerMnDig_[k];
0231         const GlobalPoint& gcoord = hgeoms_[k]->getPosition(id);
0232         double xp = gcoord.x();
0233         double yp = gcoord.y();
0234         if (verbosity_ > 1)
0235           edm::LogVerbatim("HGCalValidation")
0236               << "Digi [" << kount << "] Layer " << layer << ":" << ll << " x|y " << xp << ":" << yp << " Type "
0237               << wvtype << " Hist pointers " << h_XYdi1_[k][ll] << ":" << h_XYdi2_[k][ll];
0238         if (layer >= layerMnDig_[k] && layer <= layerMxDig_[k]) {
0239           if (wvtype)
0240             h_XYdi2_[k][ll]->Fill(xp, yp);
0241           else
0242             h_XYdi1_[k][ll]->Fill(xp, yp);
0243         }
0244         ++kount;
0245       }
0246     } else {
0247       edm::LogVerbatim("HGCalValidation")
0248           << "DigiCollection handle " << digiSources_[k] << " does not exist for " << nameDetectors_[k] << " !!!";
0249     }
0250   }
0251 }
0252 
0253 // ------------ method called when starting to processes a run  ------------
0254 void HGCalWaferStudy::beginRun(const edm::Run&, const edm::EventSetup& iSetup) {
0255   for (unsigned int k = 0; k < nameDetectors_.size(); ++k) {
0256     const auto& pHGDC = iSetup.getData(ddTokens_[k]);
0257     hgcons_.emplace_back(&pHGDC);
0258     layerSim_.emplace_back(hgcons_.back()->layers(false));
0259     layerDig_.emplace_back(hgcons_.back()->layers(true));
0260     layerFront_.emplace_back(hgcons_.back()->firstLayer());
0261     layerMnSim_[k] = std::max(layerMnSim_[k], layerFront_[k]);
0262     layerMnDig_[k] = std::max(layerMnDig_[k], layerFront_[k]);
0263     layerMxSim_[k] = std::min((layerFront_[k] + layerSim_[k] - 1), layerMxSim_[k]);
0264     layerMxDig_[k] = std::min((layerFront_[k] + layerDig_[k] - 1), layerMxDig_[k]);
0265 
0266     const auto& geom = iSetup.getData(geomTokens_[k]);
0267     hgeoms_.emplace_back(&geom);
0268     if (verbosity_ > 0)
0269       edm::LogVerbatim("HGCalValidation")
0270           << nameDetectors_[k] << " defined with " << layerFront_[k] << ":" << layerSim_[k] << ":" << layerDig_[k]
0271           << " Layers which gives limits " << layerMnSim_[k] << ":" << layerMxSim_[k] << " for Sim " << layerMnDig_[k]
0272           << ":" << layerMxDig_[k] << " for Digi "
0273           << "\nLimits for plots " << nBinHit_ << ":" << xyMinHit_ << ":" << xyMaxHit_ << " (Sim) " << nBinDig_ << ":"
0274           << xyMinDig_ << ":" << xyMaxDig_ << " (Digi) with Pointers " << hgcons_.back() << ":" << hgeoms_.back();
0275   }
0276 
0277   // Now define the histograms
0278   edm::Service<TFileService> fs;
0279   std::ostringstream name, title;
0280   for (unsigned int ih = 0; ih <= nameDetectors_.size(); ++ih) {
0281     for (int i = layerMnSim_[ih]; i <= layerMxSim_[ih]; ++i) {
0282       name.str("");
0283       title.str("");
0284       name << "XY_" << nameDetectors_[ih] << "L" << i << "SimR";
0285       title << "y vs x (Layer " << i << ") real wafers for " << nameDetectors_[ih] << " at SimHit level";
0286       if (verbosity_ > 0)
0287         edm::LogVerbatim("HGCalValidation") << i << " book " << name.str() << ":" << title.str();
0288       h_XYsi1_[ih].emplace_back(fs->make<TH2D>(
0289           name.str().c_str(), title.str().c_str(), nBinHit_, xyMinHit_, xyMaxHit_, nBinHit_, xyMinHit_, xyMaxHit_));
0290       name.str("");
0291       title.str("");
0292       name << "XY_" << nameDetectors_[ih] << "L" << i << "SimV";
0293       title << "y vs x (Layer " << i << ") virtual wafers for " << nameDetectors_[ih] << " at SimHit level";
0294       if (verbosity_ > 0)
0295         edm::LogVerbatim("HGCalValidation") << i << " book " << name.str() << ":" << title.str();
0296       h_XYsi2_[ih].emplace_back(fs->make<TH2D>(
0297           name.str().c_str(), title.str().c_str(), nBinHit_, xyMinHit_, xyMaxHit_, nBinHit_, xyMinHit_, xyMaxHit_));
0298     }
0299     edm::LogVerbatim("HGCalValidation") << "Complete booking of Sim Plots for " << nameDetectors_[ih];
0300 
0301     for (int i = layerMnDig_[ih]; i <= layerMxDig_[ih]; ++i) {
0302       name.str("");
0303       title.str("");
0304       name << "XY_" << nameDetectors_[ih] << "L" << i << "DigR";
0305       title << "y vs x (Layer " << i << ") real wafers for " << nameDetectors_[ih] << " at Digi level";
0306       if (verbosity_ > 0)
0307         edm::LogVerbatim("HGCalValidation") << i << " book " << name.str() << ":" << title.str();
0308       h_XYdi1_[ih].emplace_back(fs->make<TH2D>(
0309           name.str().c_str(), title.str().c_str(), nBinDig_, xyMinDig_, xyMaxDig_, nBinDig_, xyMinDig_, xyMaxDig_));
0310       name.str("");
0311       title.str("");
0312       name << "XY_" << nameDetectors_[ih] << "L" << i << "DigV";
0313       title << "y vs x (Layer " << i << ") virtual wafers for " << nameDetectors_[ih] << " at Digi level";
0314       if (verbosity_ > 0)
0315         edm::LogVerbatim("HGCalValidation") << i << " book " << name.str() << ":" << title.str();
0316       h_XYdi2_[ih].emplace_back(fs->make<TH2D>(
0317           name.str().c_str(), title.str().c_str(), nBinDig_, xyMinDig_, xyMaxDig_, nBinDig_, xyMinDig_, xyMaxDig_));
0318     }
0319     edm::LogVerbatim("HGCalValidation") << "Complete booking of Digi Plots for " << nameDetectors_[ih];
0320   }
0321 }
0322 
0323 #include "FWCore/Framework/interface/MakerMacros.h"
0324 //define this as a plug-in
0325 DEFINE_FWK_MODULE(HGCalWaferStudy);