Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-02-18 08:23:05

0001 #include <memory>
0002 #include <iostream>
0003 #include <fstream>
0004 #include <vector>
0005 #include <TFile.h>
0006 #include <TTree.h>
0007 #include "TPRegexp.h"
0008 
0009 // user include files
0010 #include "FWCore/Framework/interface/Frameworkfwd.h"
0011 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0012 #include "FWCore/Framework/interface/Event.h"
0013 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0015 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0016 #include "FWCore/ServiceRegistry/interface/Service.h"
0017 #include "FWCore/Common/interface/TriggerNames.h"
0018 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0019 
0020 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
0021 #include "DataFormats/MuonReco/interface/Muon.h"
0022 #include "DataFormats/MuonReco/interface/MuonFwd.h"
0023 #include "DataFormats/MuonReco/interface/MuonSelectors.h"
0024 #include "DataFormats/VertexReco/interface/Vertex.h"
0025 #include "DataFormats/VertexReco/interface/VertexFwd.h"
0026 #include "DataFormats/TrackReco/interface/Track.h"
0027 
0028 //////////////trigger info////////////////////////////////////
0029 
0030 #include "DataFormats/Common/interface/TriggerResults.h"
0031 #include "DataFormats/HLTReco/interface/TriggerObject.h"
0032 #include "DataFormats/HLTReco/interface/TriggerEvent.h"
0033 #include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
0034 #include "DataFormats/EcalDetId/interface/EBDetId.h"
0035 #include "DataFormats/EcalDetId/interface/EEDetId.h"
0036 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
0037 #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
0038 #include "DataFormats/HcalRecHit/interface/HcalRecHitCollections.h"
0039 
0040 #include "HLTrigger/HLTcore/interface/HLTConfigProvider.h"
0041 #include "HLTrigger/HLTcore/interface/HLTConfigData.h"
0042 
0043 #include "CondFormats/HcalObjects/interface/HcalRespCorrs.h"
0044 #include "CondFormats/DataRecord/interface/HcalRespCorrsRcd.h"
0045 
0046 #include "CondFormats/DataRecord/interface/EcalChannelStatusRcd.h"
0047 #include "RecoLocalCalo/EcalRecAlgos/interface/EcalSeverityLevelAlgo.h"
0048 #include "RecoLocalCalo/EcalRecAlgos/interface/EcalSeverityLevelAlgoRcd.h"
0049 
0050 #include "CalibFormats/HcalObjects/interface/HcalCalibrations.h"
0051 #include "CalibFormats/HcalObjects/interface/HcalDbService.h"
0052 #include "CalibFormats/HcalObjects/interface/HcalDbRecord.h"
0053 
0054 #include "Calibration/IsolatedParticles/interface/CaloPropagateTrack.h"
0055 #include "Calibration/IsolatedParticles/interface/eECALMatrix.h"
0056 #include "Calibration/IsolatedParticles/interface/eHCALMatrix.h"
0057 
0058 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
0059 #include "Geometry/Records/interface/CaloGeometryRecord.h"
0060 #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
0061 #include "Geometry/Records/interface/CaloTopologyRecord.h"
0062 #include "Geometry/CaloTopology/interface/CaloSubdetectorTopology.h"
0063 #include "Geometry/CaloTopology/interface/HcalTopology.h"
0064 #include "Geometry/CaloTopology/interface/CaloTopology.h"
0065 #include "Geometry/HcalCommonData/interface/HcalDDDRecConstants.h"
0066 #include "Geometry/HcalTowerAlgo/interface/HcalGeometry.h"
0067 #include "MagneticField/Engine/interface/MagneticField.h"
0068 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
0069 
0070 //#define EDM_ML_DEBUG
0071 
0072 class HcalHBHEMuonAnalyzer : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::SharedResources> {
0073 public:
0074   explicit HcalHBHEMuonAnalyzer(const edm::ParameterSet&);
0075 
0076   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0077 
0078 private:
0079   void beginJob() override;
0080   void analyze(edm::Event const&, edm::EventSetup const&) override;
0081   void beginRun(edm::Run const&, edm::EventSetup const&) override;
0082   void endRun(edm::Run const&, edm::EventSetup const&) override {}
0083   void clearVectors();
0084   int matchId(const HcalDetId&, const HcalDetId&);
0085   double activeLength(const DetId&);
0086   bool isGoodVertex(const reco::Vertex& vtx);
0087   double respCorr(const DetId& id);
0088   double gainFactor(const HcalDbService* dbserv, const HcalDetId& id);
0089   int depth16HE(int ieta, int iphi);
0090   bool goodCell(const HcalDetId& hcid, const reco::Track* pTrack, const CaloGeometry* geo, const MagneticField* bField);
0091 
0092   // ----------member data ---------------------------
0093   HLTConfigProvider hltConfig_;
0094   const edm::InputTag hlTriggerResults_;
0095   const edm::InputTag labelEBRecHit_, labelEERecHit_, labelHBHERecHit_;
0096   const std::string labelVtx_, labelMuon_, fileInCorr_;
0097   const std::vector<std::string> triggers_;
0098   const int verbosity_, useRaw_;
0099   const bool unCorrect_, collapseDepth_, isItPlan1_;
0100   const bool ignoreHECorr_, isItPreRecHit_;
0101   const bool getCharge_, writeRespCorr_;
0102   const int maxDepth_;
0103   const std::string modnam_, procnm_;
0104 
0105   const edm::EDGetTokenT<edm::TriggerResults> tok_trigRes_;
0106   const edm::EDGetTokenT<reco::VertexCollection> tok_Vtx_;
0107   const edm::EDGetTokenT<EcalRecHitCollection> tok_EB_;
0108   const edm::EDGetTokenT<EcalRecHitCollection> tok_EE_;
0109   const edm::EDGetTokenT<HBHERecHitCollection> tok_HBHE_;
0110   const edm::EDGetTokenT<reco::MuonCollection> tok_Muon_;
0111 
0112   const edm::ESGetToken<HcalDDDRecConstants, HcalRecNumberingRecord> tok_ddrec_;
0113   const edm::ESGetToken<HcalTopology, HcalRecNumberingRecord> tok_htopo_;
0114   const edm::ESGetToken<HcalRespCorrs, HcalRespCorrsRcd> tok_respcorr_;
0115   const edm::ESGetToken<CaloGeometry, CaloGeometryRecord> tok_geom_;
0116   const edm::ESGetToken<MagneticField, IdealMagneticFieldRecord> tok_magField_;
0117   const edm::ESGetToken<EcalChannelStatus, EcalChannelStatusRcd> tok_chan_;
0118   const edm::ESGetToken<EcalSeverityLevelAlgo, EcalSeverityLevelAlgoRcd> tok_sevlv_;
0119   const edm::ESGetToken<CaloTopology, CaloTopologyRecord> tok_topo_;
0120   const edm::ESGetToken<HcalDbService, HcalDbRecord> tok_dbservice_;
0121 
0122   const HcalDDDRecConstants* hdc_;
0123   const HcalTopology* theHBHETopology_;
0124   const CaloGeometry* geo_;
0125   HcalRespCorrs* respCorrs_;
0126 
0127   bool mergedDepth_, useMyCorr_;
0128   int kount_;
0129 
0130   //////////////////////////////////////////////////////
0131   static const int depthMax_ = 7;
0132   TTree* tree_;
0133   unsigned int runNumber_, eventNumber_, lumiNumber_, bxNumber_;
0134   unsigned int goodVertex_;
0135   bool muon_is_good_, muon_global_, muon_tracker_;
0136   bool muon_is_tight_, muon_is_medium_;
0137   double ptGlob_, etaGlob_, phiGlob_, energyMuon_, pMuon_;
0138   float muon_trkKink_, muon_chi2LocalPosition_, muon_segComp_;
0139   int trackerLayer_, numPixelLayers_, tight_PixelHits_;
0140   bool innerTrack_, outerTrack_, globalTrack_;
0141   double chiTracker_, dxyTracker_, dzTracker_;
0142   double innerTrackpt_, innerTracketa_, innerTrackphi_;
0143   double tight_validFraction_, outerTrackChi_;
0144   double outerTrackPt_, outerTrackEta_, outerTrackPhi_;
0145   int outerTrackHits_, outerTrackRHits_;
0146   double globalTrckPt_, globalTrckEta_, globalTrckPhi_;
0147   int globalMuonHits_, matchedStat_;
0148   double chiGlobal_, tight_LongPara_, tight_TransImpara_;
0149   double isolationR04_, isolationR03_;
0150   double ecalEnergy_, hcalEnergy_, hoEnergy_;
0151   bool matchedId_, hcalHot_;
0152   double ecal3x3Energy_, hcal1x1Energy_;
0153   unsigned int ecalDetId_, hcalDetId_, ehcalDetId_;
0154   int hcal_ieta_, hcal_iphi_;
0155   double hcalDepthEnergy_[depthMax_];
0156   double hcalDepthActiveLength_[depthMax_];
0157   double hcalDepthEnergyHot_[depthMax_];
0158   double hcalDepthActiveLengthHot_[depthMax_];
0159   double hcalDepthChargeHot_[depthMax_];
0160   double hcalDepthChargeHotBG_[depthMax_];
0161   double hcalDepthEnergyCorr_[depthMax_];
0162   double hcalDepthEnergyHotCorr_[depthMax_];
0163   bool hcalDepthMatch_[depthMax_];
0164   bool hcalDepthMatchHot_[depthMax_];
0165   double hcalActiveLength_, hcalActiveLengthHot_;
0166   std::vector<std::string> all_triggers_;
0167   std::vector<int> hltresults_;
0168 
0169   std::vector<HcalDDDRecConstants::HcalActiveLength> actHB, actHE;
0170   std::map<DetId, double> corrValue_;
0171   ////////////////////////////////////////////////////////////
0172 };
0173 
0174 HcalHBHEMuonAnalyzer::HcalHBHEMuonAnalyzer(const edm::ParameterSet& iConfig)
0175     : hlTriggerResults_(iConfig.getParameter<edm::InputTag>("hlTriggerResults")),
0176       labelEBRecHit_(iConfig.getParameter<edm::InputTag>("labelEBRecHit")),
0177       labelEERecHit_(iConfig.getParameter<edm::InputTag>("labelEERecHit")),
0178       labelHBHERecHit_(iConfig.getParameter<edm::InputTag>("labelHBHERecHit")),
0179       labelVtx_(iConfig.getParameter<std::string>("labelVertex")),
0180       labelMuon_(iConfig.getParameter<std::string>("labelMuon")),
0181       fileInCorr_(iConfig.getUntrackedParameter<std::string>("fileInCorr", "")),
0182       triggers_(iConfig.getParameter<std::vector<std::string>>("triggers")),
0183       verbosity_(iConfig.getUntrackedParameter<int>("verbosity", 0)),
0184       useRaw_(iConfig.getParameter<int>("useRaw")),
0185       unCorrect_(iConfig.getParameter<bool>("unCorrect")),
0186       collapseDepth_(iConfig.getParameter<bool>("collapseDepth")),
0187       isItPlan1_(iConfig.getParameter<bool>("isItPlan1")),
0188       ignoreHECorr_(iConfig.getUntrackedParameter<bool>("ignoreHECorr", false)),
0189       isItPreRecHit_(iConfig.getUntrackedParameter<bool>("isItPreRecHit", false)),
0190       getCharge_(iConfig.getParameter<bool>("getCharge")),
0191       writeRespCorr_(iConfig.getUntrackedParameter<bool>("writeRespCorr", false)),
0192       maxDepth_(iConfig.getUntrackedParameter<int>("maxDepth", 4)),
0193       modnam_(iConfig.getUntrackedParameter<std::string>("moduleName", "")),
0194       procnm_(iConfig.getUntrackedParameter<std::string>("processName", "")),
0195       tok_trigRes_(consumes<edm::TriggerResults>(hlTriggerResults_)),
0196       tok_Vtx_((modnam_.empty()) ? consumes<reco::VertexCollection>(labelVtx_)
0197                                  : consumes<reco::VertexCollection>(edm::InputTag(modnam_, labelVtx_, procnm_))),
0198       tok_EB_(consumes<EcalRecHitCollection>(labelEBRecHit_)),
0199       tok_EE_(consumes<EcalRecHitCollection>(labelEERecHit_)),
0200       tok_HBHE_(consumes<HBHERecHitCollection>(labelHBHERecHit_)),
0201       tok_Muon_((modnam_.empty()) ? consumes<reco::MuonCollection>(labelMuon_)
0202                                   : consumes<reco::MuonCollection>(edm::InputTag(modnam_, labelMuon_, procnm_))),
0203       tok_ddrec_(esConsumes<HcalDDDRecConstants, HcalRecNumberingRecord, edm::Transition::BeginRun>()),
0204       tok_htopo_(esConsumes<HcalTopology, HcalRecNumberingRecord, edm::Transition::BeginRun>()),
0205       tok_respcorr_(esConsumes<HcalRespCorrs, HcalRespCorrsRcd, edm::Transition::BeginRun>()),
0206       tok_geom_(esConsumes<CaloGeometry, CaloGeometryRecord, edm::Transition::BeginRun>()),
0207       tok_magField_(esConsumes<MagneticField, IdealMagneticFieldRecord>()),
0208       tok_chan_(esConsumes<EcalChannelStatus, EcalChannelStatusRcd>()),
0209       tok_sevlv_(esConsumes<EcalSeverityLevelAlgo, EcalSeverityLevelAlgoRcd>()),
0210       tok_topo_(esConsumes<CaloTopology, CaloTopologyRecord>()),
0211       tok_dbservice_(esConsumes<HcalDbService, HcalDbRecord>()),
0212       hdc_(nullptr),
0213       theHBHETopology_(nullptr),
0214       respCorrs_(nullptr) {
0215   usesResource(TFileService::kSharedResource);
0216   //now do what ever initialization is needed
0217   kount_ = 0;
0218   mergedDepth_ = (!isItPreRecHit_) || (collapseDepth_);
0219 
0220   if (modnam_.empty()) {
0221     edm::LogVerbatim("HBHEMuon") << "Labels used: Trig " << hlTriggerResults_ << " Vtx " << labelVtx_ << " EB "
0222                                  << labelEBRecHit_ << " EE " << labelEERecHit_ << " HBHE " << labelHBHERecHit_ << " MU "
0223                                  << labelMuon_;
0224   } else {
0225     edm::LogVerbatim("HBHEMuon") << "Labels used Trig " << hlTriggerResults_ << "\n  Vtx  "
0226                                  << edm::InputTag(modnam_, labelVtx_, procnm_) << "\n  EB   " << labelEBRecHit_
0227                                  << "\n  EE   " << labelEERecHit_ << "\n  HBHE " << labelHBHERecHit_ << "\n  MU   "
0228                                  << edm::InputTag(modnam_, labelMuon_, procnm_);
0229   }
0230 
0231   if (!fileInCorr_.empty()) {
0232     std::ifstream infile(fileInCorr_.c_str());
0233     if (infile.is_open()) {
0234       while (true) {
0235         unsigned int id;
0236         double cfac;
0237         infile >> id >> cfac;
0238         if (!infile.good())
0239           break;
0240         corrValue_[DetId(id)] = cfac;
0241       }
0242       infile.close();
0243     }
0244   }
0245   useMyCorr_ = (!corrValue_.empty());
0246   edm::LogVerbatim("HBHEMuon") << "Flags used: UseRaw " << useRaw_ << " GetCharge " << getCharge_ << " UnCorrect "
0247                                << unCorrect_ << " IgnoreHECorr " << ignoreHECorr_ << " CollapseDepth " << collapseDepth_
0248                                << ":" << mergedDepth_ << " IsItPlan1 " << isItPlan1_ << " IsItPreRecHit "
0249                                << isItPreRecHit_ << " UseMyCorr " << useMyCorr_;
0250 }
0251 
0252 //
0253 // member functions
0254 //
0255 
0256 // ------------ method called for each event  ------------
0257 void HcalHBHEMuonAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0258   ++kount_;
0259   clearVectors();
0260   std::vector<bool> muon_is_good, muon_global, muon_tracker;
0261   std::vector<bool> muon_is_tight, muon_is_medium;
0262   std::vector<double> ptGlob, etaGlob, phiGlob, energyMuon, pMuon;
0263   std::vector<float> muon_trkKink, muon_chi2LocalPosition, muon_segComp;
0264   std::vector<int> trackerLayer, numPixelLayers, tight_PixelHits;
0265   std::vector<bool> innerTrack, outerTrack, globalTrack;
0266   std::vector<double> chiTracker, dxyTracker, dzTracker;
0267   std::vector<double> innerTrackpt, innerTracketa, innerTrackphi;
0268   std::vector<double> tight_validFraction, outerTrackChi;
0269   std::vector<double> outerTrackPt, outerTrackEta, outerTrackPhi;
0270   std::vector<int> outerTrackHits, outerTrackRHits;
0271   std::vector<double> globalTrckPt, globalTrckEta, globalTrckPhi;
0272   std::vector<int> globalMuonHits, matchedStat;
0273   std::vector<double> chiGlobal, tight_LongPara, tight_TransImpara;
0274   std::vector<double> isolationR04, isolationR03;
0275   std::vector<double> ecalEnergy, hcalEnergy, hoEnergy;
0276   std::vector<bool> matchedId, hcalHot;
0277   std::vector<double> ecal3x3Energy, hcal1x1Energy;
0278   std::vector<unsigned int> ecalDetId, hcalDetId, ehcalDetId;
0279   std::vector<int> hcal_ieta, hcal_iphi;
0280   std::vector<double> hcalDepthEnergy[depthMax_];
0281   std::vector<double> hcalDepthActiveLength[depthMax_];
0282   std::vector<double> hcalDepthEnergyHot[depthMax_];
0283   std::vector<double> hcalDepthActiveLengthHot[depthMax_];
0284   std::vector<double> hcalDepthChargeHot[depthMax_];
0285   std::vector<double> hcalDepthChargeHotBG[depthMax_];
0286   std::vector<double> hcalDepthEnergyCorr[depthMax_];
0287   std::vector<double> hcalDepthEnergyHotCorr[depthMax_];
0288   std::vector<bool> hcalDepthMatch[depthMax_];
0289   std::vector<bool> hcalDepthMatchHot[depthMax_];
0290   std::vector<double> hcalActiveLength, hcalActiveLengthHot;
0291   runNumber_ = iEvent.id().run();
0292   eventNumber_ = iEvent.id().event();
0293   lumiNumber_ = iEvent.id().luminosityBlock();
0294   bxNumber_ = iEvent.bunchCrossing();
0295 #ifdef EDM_ML_DEBUG
0296   edm::LogVerbatim("HBHEMuon") << "Run " << runNumber_ << " Event " << eventNumber_ << " Lumi " << lumiNumber_ << " BX "
0297                                << bxNumber_ << std::endl;
0298 #endif
0299   const edm::Handle<edm::TriggerResults> _Triggers = iEvent.getHandle(tok_trigRes_);
0300 #ifdef EDM_ML_DEBUG
0301   if ((verbosity_ / 10000) % 10 > 0)
0302     edm::LogVerbatim("HBHEMuon") << "Size of all triggers " << all_triggers_.size();
0303 #endif
0304   int Ntriggers = all_triggers_.size();
0305 #ifdef EDM_ML_DEBUG
0306   if ((verbosity_ / 10000) % 10 > 0)
0307     edm::LogVerbatim("HBHEMuon") << "Size of HLT MENU: " << _Triggers->size();
0308 #endif
0309   if (_Triggers.isValid()) {
0310     const edm::TriggerNames& triggerNames_ = iEvent.triggerNames(*_Triggers);
0311     std::vector<int> index;
0312     for (int i = 0; i < Ntriggers; i++) {
0313       index.push_back(triggerNames_.triggerIndex(all_triggers_[i]));
0314       int triggerSize = int(_Triggers->size());
0315 #ifdef EDM_ML_DEBUG
0316       if ((verbosity_ / 10000) % 10 > 0)
0317         edm::LogVerbatim("HBHEMuon") << "outside loop " << index[i] << "\ntriggerSize " << triggerSize;
0318 #endif
0319       if (index[i] < triggerSize) {
0320         hltresults_.push_back(_Triggers->accept(index[i]));
0321 #ifdef EDM_ML_DEBUG
0322         if ((verbosity_ / 10000) % 10 > 0)
0323           edm::LogVerbatim("HBHEMuon") << "Trigger_info " << triggerSize << " triggerSize " << index[i]
0324                                        << " trigger_index " << hltresults_.at(i) << " hltresult";
0325 #endif
0326       } else {
0327         if ((verbosity_ / 10000) % 10 > 0)
0328           edm::LogVerbatim("HBHEMuon") << "Requested HLT path \""
0329                                        << "\" does not exist";
0330       }
0331     }
0332   }
0333 
0334   // get handles to calogeometry and calotopology
0335   const MagneticField* bField = &iSetup.getData(tok_magField_);
0336   const EcalChannelStatus* theEcalChStatus = &iSetup.getData(tok_chan_);
0337   const EcalSeverityLevelAlgo* sevlv = &iSetup.getData(tok_sevlv_);
0338   const CaloTopology* caloTopology = &iSetup.getData(tok_topo_);
0339   const HcalDbService* conditions = &iSetup.getData(tok_dbservice_);
0340 
0341   // Relevant blocks from iEvent
0342   const edm::Handle<reco::VertexCollection> vtx = iEvent.getHandle(tok_Vtx_);
0343 
0344   edm::Handle<EcalRecHitCollection> barrelRecHitsHandle = iEvent.getHandle(tok_EB_);
0345   edm::Handle<EcalRecHitCollection> endcapRecHitsHandle = iEvent.getHandle(tok_EE_);
0346 
0347   edm::Handle<HBHERecHitCollection> hbhe = iEvent.getHandle(tok_HBHE_);
0348 
0349   const edm::Handle<reco::MuonCollection> _Muon = iEvent.getHandle(tok_Muon_);
0350 
0351   // require a good vertex
0352   math::XYZPoint pvx;
0353   goodVertex_ = 0;
0354   if (!vtx.isValid()) {
0355 #ifdef EDM_ML_DEBUG
0356     edm::LogVerbatim("HBHEMuon") << "No Good Vertex found == Reject";
0357 #endif
0358     return;
0359   }
0360   reco::VertexCollection::const_iterator firstGoodVertex = vtx->end();
0361   for (reco::VertexCollection::const_iterator it = vtx->begin(); it != vtx->end(); it++) {
0362     if (isGoodVertex(*it)) {
0363       if (firstGoodVertex == vtx->end())
0364         firstGoodVertex = it;
0365       ++goodVertex_;
0366     }
0367   }
0368   if (firstGoodVertex != vtx->end())
0369     pvx = firstGoodVertex->position();
0370 
0371   bool accept(false);
0372   if (_Muon.isValid() && barrelRecHitsHandle.isValid() && endcapRecHitsHandle.isValid() && hbhe.isValid()) {
0373     for (const auto& RecMuon : (*(_Muon.product()))) {
0374       muon_is_good.push_back(RecMuon.isPFMuon());
0375       muon_global.push_back(RecMuon.isGlobalMuon());
0376       muon_tracker.push_back(RecMuon.isTrackerMuon());
0377       ptGlob.push_back(RecMuon.pt());
0378       etaGlob.push_back(RecMuon.eta());
0379       phiGlob.push_back(RecMuon.phi());
0380       energyMuon.push_back(RecMuon.energy());
0381       pMuon.push_back(RecMuon.p());
0382 #ifdef EDM_ML_DEBUG
0383       edm::LogVerbatim("HBHEMuon") << "Energy:" << RecMuon.energy() << " P:" << RecMuon.p();
0384 #endif
0385       muon_is_tight.push_back(muon::isTightMuon(RecMuon, *firstGoodVertex));
0386       muon_is_medium.push_back(muon::isMediumMuon(RecMuon));
0387       muon_trkKink.push_back(RecMuon.combinedQuality().trkKink);
0388       muon_chi2LocalPosition.push_back(RecMuon.combinedQuality().chi2LocalPosition);
0389       muon_segComp.push_back(muon::segmentCompatibility(RecMuon));
0390       // acessing tracker hits info
0391       if (RecMuon.track().isNonnull()) {
0392         trackerLayer.push_back(RecMuon.track()->hitPattern().trackerLayersWithMeasurement());
0393       } else {
0394         trackerLayer.push_back(-1);
0395       }
0396       if (RecMuon.innerTrack().isNonnull()) {
0397         innerTrack.push_back(true);
0398         numPixelLayers.push_back(RecMuon.innerTrack()->hitPattern().pixelLayersWithMeasurement());
0399         chiTracker.push_back(RecMuon.innerTrack()->normalizedChi2());
0400         dxyTracker.push_back(fabs(RecMuon.innerTrack()->dxy(pvx)));
0401         dzTracker.push_back(fabs(RecMuon.innerTrack()->dz(pvx)));
0402         innerTrackpt.push_back(RecMuon.innerTrack()->pt());
0403         innerTracketa.push_back(RecMuon.innerTrack()->eta());
0404         innerTrackphi.push_back(RecMuon.innerTrack()->phi());
0405         tight_PixelHits.push_back(RecMuon.innerTrack()->hitPattern().numberOfValidPixelHits());
0406         tight_validFraction.push_back(RecMuon.innerTrack()->validFraction());
0407       } else {
0408         innerTrack.push_back(false);
0409         numPixelLayers.push_back(0);
0410         chiTracker.push_back(0);
0411         dxyTracker.push_back(0);
0412         dzTracker.push_back(0);
0413         innerTrackpt.push_back(0);
0414         innerTracketa.push_back(0);
0415         innerTrackphi.push_back(0);
0416         tight_PixelHits.push_back(0);
0417         tight_validFraction.push_back(-99);
0418       }
0419       // outer track info
0420       if (RecMuon.outerTrack().isNonnull()) {
0421         outerTrack.push_back(true);
0422         outerTrackPt.push_back(RecMuon.outerTrack()->pt());
0423         outerTrackEta.push_back(RecMuon.outerTrack()->eta());
0424         outerTrackPhi.push_back(RecMuon.outerTrack()->phi());
0425         outerTrackChi.push_back(RecMuon.outerTrack()->normalizedChi2());
0426         outerTrackHits.push_back(RecMuon.outerTrack()->numberOfValidHits());
0427         outerTrackRHits.push_back(RecMuon.outerTrack()->recHitsSize());
0428       } else {
0429         outerTrack.push_back(false);
0430         outerTrackPt.push_back(0);
0431         outerTrackEta.push_back(0);
0432         outerTrackPhi.push_back(0);
0433         outerTrackChi.push_back(0);
0434         outerTrackHits.push_back(0);
0435         outerTrackRHits.push_back(0);
0436       }
0437       // Tight Muon cuts
0438       if (RecMuon.globalTrack().isNonnull()) {
0439         globalTrack.push_back(true);
0440         chiGlobal.push_back(RecMuon.globalTrack()->normalizedChi2());
0441         globalMuonHits.push_back(RecMuon.globalTrack()->hitPattern().numberOfValidMuonHits());
0442         matchedStat.push_back(RecMuon.numberOfMatchedStations());
0443         globalTrckPt.push_back(RecMuon.globalTrack()->pt());
0444         globalTrckEta.push_back(RecMuon.globalTrack()->eta());
0445         globalTrckPhi.push_back(RecMuon.globalTrack()->phi());
0446         tight_TransImpara.push_back(fabs(RecMuon.muonBestTrack()->dxy(pvx)));
0447         tight_LongPara.push_back(fabs(RecMuon.muonBestTrack()->dz(pvx)));
0448       } else {
0449         globalTrack.push_back(false);
0450         chiGlobal.push_back(0);
0451         globalMuonHits.push_back(0);
0452         matchedStat.push_back(0);
0453         globalTrckPt.push_back(0);
0454         globalTrckEta.push_back(0);
0455         globalTrckPhi.push_back(0);
0456         tight_TransImpara.push_back(0);
0457         tight_LongPara.push_back(0);
0458       }
0459 
0460       isolationR04.push_back(
0461           ((RecMuon.pfIsolationR04().sumChargedHadronPt +
0462             std::max(0.,
0463                      RecMuon.pfIsolationR04().sumNeutralHadronEt + RecMuon.pfIsolationR04().sumPhotonEt -
0464                          (0.5 * RecMuon.pfIsolationR04().sumPUPt))) /
0465            RecMuon.pt()));
0466 
0467       isolationR03.push_back(
0468           ((RecMuon.pfIsolationR03().sumChargedHadronPt +
0469             std::max(0.,
0470                      RecMuon.pfIsolationR03().sumNeutralHadronEt + RecMuon.pfIsolationR03().sumPhotonEt -
0471                          (0.5 * RecMuon.pfIsolationR03().sumPUPt))) /
0472            RecMuon.pt()));
0473 
0474       ecalEnergy.push_back(RecMuon.calEnergy().emS9);
0475       hcalEnergy.push_back(RecMuon.calEnergy().hadS9);
0476       hoEnergy.push_back(RecMuon.calEnergy().hoS9);
0477 
0478       double eEcal(0), eHcal(0), activeLengthTot(0), activeLengthHotTot(0);
0479       double eHcalDepth[depthMax_], eHcalDepthHot[depthMax_];
0480       double eHcalDepthC[depthMax_], eHcalDepthHotC[depthMax_];
0481       double cHcalDepthHot[depthMax_], cHcalDepthHotBG[depthMax_];
0482       double activeL[depthMax_], activeHotL[depthMax_];
0483       bool matchDepth[depthMax_], matchDepthHot[depthMax_];
0484       HcalDetId eHcalDetId[depthMax_];
0485       unsigned int isHot(0);
0486       bool tmpmatch(false);
0487       int ieta(-1000), iphi(-1000);
0488       for (int i = 0; i < depthMax_; ++i) {
0489         eHcalDepth[i] = eHcalDepthHot[i] = 0;
0490         eHcalDepthC[i] = eHcalDepthHotC[i] = 0;
0491         cHcalDepthHot[i] = cHcalDepthHotBG[i] = 0;
0492         activeL[i] = activeHotL[i] = 0;
0493         matchDepth[i] = matchDepthHot[i] = true;
0494       }
0495       if (RecMuon.innerTrack().isNonnull()) {
0496         const reco::Track* pTrack = (RecMuon.innerTrack()).get();
0497         spr::propagatedTrackID trackID = spr::propagateCALO(pTrack, geo_, bField, (((verbosity_ / 100) % 10 > 0)));
0498         if ((RecMuon.p() > 10.0) && (trackID.okHCAL))
0499           accept = true;
0500 
0501         ecalDetId.push_back((trackID.detIdECAL)());
0502         hcalDetId.push_back((trackID.detIdHCAL)());
0503         ehcalDetId.push_back((trackID.detIdEHCAL)());
0504 
0505         HcalDetId check;
0506         std::pair<bool, HcalDetId> info = spr::propagateHCALBack(pTrack, geo_, bField, (((verbosity_ / 100) % 10 > 0)));
0507         if (info.first) {
0508           check = info.second;
0509         }
0510 
0511         bool okE = trackID.okECAL;
0512         if (okE) {
0513           const DetId isoCell(trackID.detIdECAL);
0514           std::pair<double, bool> e3x3 = spr::eECALmatrix(isoCell,
0515                                                           barrelRecHitsHandle,
0516                                                           endcapRecHitsHandle,
0517                                                           *theEcalChStatus,
0518                                                           geo_,
0519                                                           caloTopology,
0520                                                           sevlv,
0521                                                           1,
0522                                                           1,
0523                                                           -100.0,
0524                                                           -100.0,
0525                                                           -500.0,
0526                                                           500.0,
0527                                                           false);
0528           eEcal = e3x3.first;
0529           okE = e3x3.second;
0530         }
0531 #ifdef EDM_ML_DEBUG
0532         edm::LogVerbatim("HBHEMuon") << "Propagate Track to ECAL: " << okE << ":" << trackID.okECAL << " E " << eEcal;
0533 #endif
0534 
0535         if (trackID.okHCAL) {
0536           DetId closestCell(trackID.detIdHCAL);
0537           HcalDetId hcidt(closestCell.rawId());
0538           if ((hcidt.ieta() == check.ieta()) && (hcidt.iphi() == check.iphi()))
0539             tmpmatch = true;
0540 #ifdef EDM_ML_DEBUG
0541           edm::LogVerbatim("HBHEMuon") << "Front " << hcidt << " Back " << info.first << ":" << check << " Match "
0542                                        << tmpmatch;
0543 #endif
0544 
0545           HcalSubdetector subdet = hcidt.subdet();
0546           ieta = hcidt.ieta();
0547           iphi = hcidt.iphi();
0548           bool hborhe = (std::abs(ieta) == 16);
0549 
0550           eHcal = spr::eHCALmatrix(theHBHETopology_,
0551                                    closestCell,
0552                                    hbhe,
0553                                    0,
0554                                    0,
0555                                    false,
0556                                    true,
0557                                    -100.0,
0558                                    -100.0,
0559                                    -100.0,
0560                                    -100.0,
0561                                    -500.,
0562                                    500.,
0563                                    useRaw_);
0564           std::vector<std::pair<double, int>> ehdepth;
0565           spr::energyHCALCell((HcalDetId)closestCell,
0566                               hbhe,
0567                               ehdepth,
0568                               maxDepth_,
0569                               -100.0,
0570                               -100.0,
0571                               -100.0,
0572                               -100.0,
0573                               -500.0,
0574                               500.0,
0575                               useRaw_,
0576                               depth16HE(ieta, iphi),
0577                               (((verbosity_ / 1000) % 10) > 0));
0578           for (int i = 0; i < depthMax_; ++i)
0579             eHcalDetId[i] = HcalDetId();
0580           for (unsigned int i = 0; i < ehdepth.size(); ++i) {
0581             HcalSubdetector subdet0 =
0582                 (hborhe) ? ((ehdepth[i].second >= depth16HE(ieta, iphi)) ? HcalEndcap : HcalBarrel) : subdet;
0583             HcalDetId hcid0(subdet0, ieta, iphi, ehdepth[i].second);
0584             double actL = activeLength(DetId(hcid0));
0585             double ene = ehdepth[i].first;
0586             bool tmpC(false);
0587             if (ene > 0.0) {
0588               if (!(theHBHETopology_->validHcal(hcid0))) {
0589                 edm::LogWarning("HBHEMuon") << "(1) Invalid ID " << hcid0 << " with E = " << ene;
0590                 edm::LogWarning("HBHEMuon") << HcalDetId(closestCell) << " with " << ehdepth.size() << " depths:";
0591                 for (const auto& ehd : ehdepth)
0592                   edm::LogWarning("HBHEMuon") << " " << ehd.second << ":" << ehd.first;
0593               } else {
0594                 tmpC = goodCell(hcid0, pTrack, geo_, bField);
0595                 double enec(ene);
0596                 if (unCorrect_) {
0597                   double corr = (ignoreHECorr_ && (subdet0 == HcalEndcap)) ? 1.0 : respCorr(DetId(hcid0));
0598                   if (corr != 0)
0599                     ene /= corr;
0600 #ifdef EDM_ML_DEBUG
0601                   HcalDetId id = (isItPlan1_ && isItPreRecHit_) ? hdc_->mergedDepthDetId(hcid0) : hcid0;
0602                   edm::LogVerbatim("HBHEMuon") << hcid0 << ":" << id << " Corr " << corr;
0603 #endif
0604                 }
0605                 int depth = ehdepth[i].second - 1;
0606                 if (collapseDepth_) {
0607                   HcalDetId id = hdc_->mergedDepthDetId(hcid0);
0608                   depth = id.depth() - 1;
0609                 }
0610                 eHcalDepth[depth] += ene;
0611                 eHcalDepthC[depth] += enec;
0612                 activeL[depth] += actL;
0613                 activeLengthTot += actL;
0614                 matchDepth[depth] = (matchDepth[depth] && tmpC);
0615 #ifdef EDM_ML_DEBUG
0616                 if ((verbosity_ % 10) > 0)
0617                   edm::LogVerbatim("HBHEMuon")
0618                       << hcid0 << " E " << ene << ":" << enec << " L " << actL << " Match " << tmpC;
0619 #endif
0620               }
0621             }
0622           }
0623 #ifdef EDM_ML_DEBUG
0624           if ((verbosity_ % 10) > 0) {
0625             edm::LogVerbatim("HBHEMuon") << hcidt << " Match " << tmpmatch << " Depths " << ehdepth.size();
0626             for (unsigned int k = 0; k < ehdepth.size(); ++k)
0627               edm::LogVerbatim("HBHEMuon") << " [" << k << ":" << ehdepth[k].second << "] " << matchDepth[k];
0628           }
0629 #endif
0630           HcalDetId hotCell;
0631           spr::eHCALmatrix(geo_, theHBHETopology_, closestCell, hbhe, 1, 1, hotCell, false, useRaw_, false);
0632           isHot = matchId(closestCell, hotCell);
0633           if (hotCell != HcalDetId()) {
0634             subdet = HcalDetId(hotCell).subdet();
0635             ieta = HcalDetId(hotCell).ieta();
0636             iphi = HcalDetId(hotCell).iphi();
0637             hborhe = (std::abs(ieta) == 16);
0638             std::vector<std::pair<double, int>> ehdepth;
0639             spr::energyHCALCell(hotCell,
0640                                 hbhe,
0641                                 ehdepth,
0642                                 maxDepth_,
0643                                 -100.0,
0644                                 -100.0,
0645                                 -100.0,
0646                                 -100.0,
0647                                 -500.0,
0648                                 500.0,
0649                                 useRaw_,
0650                                 depth16HE(ieta, iphi),
0651                                 false);  //(((verbosity_/1000)%10)>0    ));
0652             for (int i = 0; i < depthMax_; ++i)
0653               eHcalDetId[i] = HcalDetId();
0654             for (unsigned int i = 0; i < ehdepth.size(); ++i) {
0655               HcalSubdetector subdet0 =
0656                   (hborhe) ? ((ehdepth[i].second >= depth16HE(ieta, iphi)) ? HcalEndcap : HcalBarrel) : subdet;
0657               HcalDetId hcid0(subdet0, ieta, iphi, ehdepth[i].second);
0658               double actL = activeLength(DetId(hcid0));
0659               double ene = ehdepth[i].first;
0660               bool tmpC(false);
0661               if (ene > 0.0) {
0662                 if (!(theHBHETopology_->validHcal(hcid0))) {
0663                   edm::LogWarning("HBHEMuon") << "(2) Invalid ID " << hcid0 << " with E = " << ene;
0664                   edm::LogWarning("HBHEMuon") << HcalDetId(hotCell) << " with " << ehdepth.size() << " depths:";
0665                   for (const auto& ehd : ehdepth)
0666                     edm::LogWarning("HBHEMuon") << " " << ehd.second << ":" << ehd.first;
0667                 } else {
0668                   tmpC = goodCell(hcid0, pTrack, geo_, bField);
0669                   double chg(ene), enec(ene);
0670                   if (unCorrect_) {
0671                     double corr = (ignoreHECorr_ && (subdet0 == HcalEndcap)) ? 1.0 : respCorr(DetId(hcid0));
0672                     if (corr != 0)
0673                       ene /= corr;
0674 #ifdef EDM_ML_DEBUG
0675                     HcalDetId id = (isItPlan1_ && isItPreRecHit_) ? hdc_->mergedDepthDetId(hcid0) : hcid0;
0676                     edm::LogVerbatim("HBHEMuon")
0677                         << hcid0 << ":" << id << " Corr " << corr << " E " << ene << ":" << enec;
0678 #endif
0679                   }
0680                   if (getCharge_) {
0681                     double gain = gainFactor(conditions, hcid0);
0682                     if (gain != 0)
0683                       chg /= gain;
0684 #ifdef EDM_ML_DEBUG
0685                     edm::LogVerbatim("HBHEMuon") << hcid0 << " Gain " << gain << " C " << chg;
0686 #endif
0687                   }
0688                   int depth = ehdepth[i].second - 1;
0689                   if (collapseDepth_) {
0690                     HcalDetId id = hdc_->mergedDepthDetId(hcid0);
0691                     depth = id.depth() - 1;
0692                   }
0693                   eHcalDepthHot[depth] += ene;
0694                   eHcalDepthHotC[depth] += enec;
0695                   cHcalDepthHot[depth] += chg;
0696                   activeHotL[depth] += actL;
0697                   activeLengthHotTot += actL;
0698                   matchDepthHot[depth] = (matchDepthHot[depth] && tmpC);
0699 #ifdef EDM_ML_DEBUG
0700                   if ((verbosity_ % 10) > 0)
0701                     edm::LogVerbatim("HBHEMuon") << hcid0 << " depth " << depth << " E " << ene << ":" << enec << " C "
0702                                                  << chg << " L " << actL << " Match " << tmpC;
0703 #endif
0704                 }
0705               }
0706             }
0707 
0708             HcalDetId oppCell(subdet, -ieta, iphi, HcalDetId(hotCell).depth());
0709             std::vector<std::pair<double, int>> ehdeptho;
0710             spr::energyHCALCell(oppCell,
0711                                 hbhe,
0712                                 ehdeptho,
0713                                 maxDepth_,
0714                                 -100.0,
0715                                 -100.0,
0716                                 -100.0,
0717                                 -100.0,
0718                                 -500.0,
0719                                 500.0,
0720                                 useRaw_,
0721                                 depth16HE(-ieta, iphi),
0722                                 false);  //(((verbosity_/1000)%10)>0));
0723             for (unsigned int i = 0; i < ehdeptho.size(); ++i) {
0724               HcalSubdetector subdet0 =
0725                   (hborhe) ? ((ehdeptho[i].second >= depth16HE(-ieta, iphi)) ? HcalEndcap : HcalBarrel) : subdet;
0726               HcalDetId hcid0(subdet0, -ieta, iphi, ehdeptho[i].second);
0727               double ene = ehdeptho[i].first;
0728               if (ene > 0.0) {
0729                 if (!(theHBHETopology_->validHcal(hcid0))) {
0730                   edm::LogWarning("HBHEMuon") << "(3) Invalid ID " << hcid0 << " with E = " << ene;
0731                   edm::LogWarning("HBHEMuon") << oppCell << " with " << ehdeptho.size() << " depths:";
0732                   for (const auto& ehd : ehdeptho)
0733                     edm::LogWarning("HBHEMuon") << " " << ehd.second << ":" << ehd.first;
0734                 } else {
0735                   double chg(ene);
0736                   if (unCorrect_) {
0737                     double corr = (ignoreHECorr_ && (subdet0 == HcalEndcap)) ? 1.0 : respCorr(DetId(hcid0));
0738                     if (corr != 0)
0739                       ene /= corr;
0740 #ifdef EDM_ML_DEBUG
0741                     HcalDetId id = (isItPlan1_ && isItPreRecHit_) ? hdc_->mergedDepthDetId(hcid0) : hcid0;
0742                     edm::LogVerbatim("HBHEMuon")
0743                         << hcid0 << ":" << id << " Corr " << corr << " E " << ene << ":" << ehdeptho[i].first;
0744 #endif
0745                   }
0746                   if (getCharge_) {
0747                     double gain = gainFactor(conditions, hcid0);
0748                     if (gain != 0)
0749                       chg /= gain;
0750 #ifdef EDM_ML_DEBUG
0751                     edm::LogVerbatim("HBHEMuon") << hcid0 << " Gain " << gain << " C " << chg;
0752 #endif
0753                   }
0754                   int depth = ehdeptho[i].second - 1;
0755                   if (collapseDepth_) {
0756                     HcalDetId id = hdc_->mergedDepthDetId(hcid0);
0757                     depth = id.depth() - 1;
0758                   }
0759                   cHcalDepthHotBG[depth] += chg;
0760 #ifdef EDM_ML_DEBUG
0761                   if ((verbosity_ % 10) > 0)
0762                     edm::LogVerbatim("HBHEMuon") << hcid0 << " Depth " << depth << " E " << ene << " C " << chg;
0763 #endif
0764                 }
0765               }
0766             }
0767           }
0768         }
0769 #ifdef EDM_ML_DEBUG
0770         edm::LogVerbatim("HBHEMuon") << "Propagate Track to HCAL: " << trackID.okHCAL << " Match " << tmpmatch
0771                                      << " Hot " << isHot << " Energy " << eHcal << std::endl;
0772 #endif
0773 
0774       } else {
0775         ecalDetId.push_back(0);
0776         hcalDetId.push_back(0);
0777         ehcalDetId.push_back(0);
0778       }
0779 
0780       matchedId.push_back(tmpmatch);
0781       ecal3x3Energy.push_back(eEcal);
0782       hcal1x1Energy.push_back(eHcal);
0783       hcal_ieta.push_back(ieta);
0784       hcal_iphi.push_back(iphi);
0785       for (int i = 0; i < depthMax_; ++i) {
0786         hcalDepthEnergy[i].push_back(eHcalDepth[i]);
0787         hcalDepthActiveLength[i].push_back(activeL[i]);
0788         hcalDepthEnergyHot[i].push_back(eHcalDepthHot[i]);
0789         hcalDepthActiveLengthHot[i].push_back(activeHotL[i]);
0790         hcalDepthEnergyCorr[i].push_back(eHcalDepthC[i]);
0791         hcalDepthEnergyHotCorr[i].push_back(eHcalDepthHotC[i]);
0792         hcalDepthChargeHot[i].push_back(cHcalDepthHot[i]);
0793         hcalDepthChargeHotBG[i].push_back(cHcalDepthHotBG[i]);
0794         hcalDepthMatch[i].push_back(matchDepth[i]);
0795         hcalDepthMatchHot[i].push_back(matchDepthHot[i]);
0796       }
0797       hcalActiveLength.push_back(activeLengthTot);
0798       hcalHot.push_back(isHot);
0799       hcalActiveLengthHot.push_back(activeLengthHotTot);
0800     }
0801   }
0802   if (accept) {
0803 #ifdef EDM_ML_DEBUG
0804     for (unsigned int i = 0; i < hcal_ieta.size(); ++i)
0805       edm::LogVerbatim("HBHEMuon") << "[" << i << "] ieta/iphi for entry to "
0806                                    << "HCAL has value of " << hcal_ieta[i] << ":" << hcal_iphi[i];
0807 #endif
0808     for (unsigned int k = 0; k < muon_is_good.size(); ++k) {
0809       muon_is_good_ = muon_is_good[k];
0810       muon_global_ = muon_global[k];
0811       muon_tracker_ = muon_tracker[k];
0812       muon_is_tight_ = muon_is_tight[k];
0813       muon_is_medium_ = muon_is_medium[k];
0814       ptGlob_ = ptGlob[k];
0815       etaGlob_ = etaGlob[k];
0816       phiGlob_ = phiGlob[k];
0817       energyMuon_ = energyMuon[k];
0818       pMuon_ = pMuon[k];
0819       muon_trkKink_ = muon_trkKink[k];
0820       muon_chi2LocalPosition_ = muon_chi2LocalPosition[k];
0821       muon_segComp_ = muon_segComp[k];
0822       trackerLayer_ = trackerLayer[k];
0823       numPixelLayers_ = numPixelLayers[k];
0824       tight_PixelHits_ = tight_PixelHits[k];
0825       innerTrack_ = innerTrack[k];
0826       outerTrack_ = outerTrack[k];
0827       globalTrack_ = globalTrack[k];
0828       chiTracker_ = chiTracker[k];
0829       dxyTracker_ = dxyTracker[k];
0830       dzTracker_ = dzTracker[k];
0831       innerTrackpt_ = innerTrackpt[k];
0832       innerTracketa_ = innerTracketa[k];
0833       innerTrackphi_ = innerTrackphi[k];
0834       tight_validFraction_ = tight_validFraction[k];
0835       outerTrackChi_ = outerTrackChi[k];
0836       outerTrackPt_ = outerTrackPt[k];
0837       outerTrackEta_ = outerTrackEta[k];
0838       outerTrackPhi_ = outerTrackPhi[k];
0839       outerTrackHits_ = outerTrackHits[k];
0840       outerTrackRHits_ = outerTrackRHits[k];
0841       globalTrckPt_ = globalTrckPt[k];
0842       globalTrckEta_ = globalTrckEta[k];
0843       globalTrckPhi_ = globalTrckPhi[k];
0844       globalMuonHits_ = globalMuonHits[k];
0845       matchedStat_ = matchedStat[k];
0846       chiGlobal_ = chiGlobal[k];
0847       tight_LongPara_ = tight_LongPara[k];
0848       tight_TransImpara_ = tight_TransImpara[k];
0849       isolationR04_ = isolationR04[k];
0850       isolationR03_ = isolationR03[k];
0851       ecalEnergy_ = ecalEnergy[k];
0852       hcalEnergy_ = hcalEnergy[k];
0853       hoEnergy_ = hoEnergy[k];
0854       matchedId_ = matchedId[k];
0855       hcalHot_ = hcalHot[k];
0856       ecal3x3Energy_ = ecal3x3Energy[k];
0857       hcal1x1Energy_ = hcal1x1Energy[k];
0858       ecalDetId_ = ecalDetId[k];
0859       hcalDetId_ = hcalDetId[k];
0860       ehcalDetId_ = ehcalDetId[k];
0861       hcal_ieta_ = hcal_ieta[k];
0862       hcal_iphi_ = hcal_iphi[k];
0863       for (int i = 0; i < depthMax_; ++i) {
0864         hcalDepthEnergy_[i] = hcalDepthEnergy[i][k];
0865         hcalDepthActiveLength_[i] = hcalDepthActiveLength[i][k];
0866         hcalDepthEnergyHot_[i] = hcalDepthEnergyHot[i][k];
0867         hcalDepthActiveLengthHot_[i] = hcalDepthActiveLengthHot[i][k];
0868         hcalDepthChargeHot_[i] = hcalDepthChargeHot[i][k];
0869         hcalDepthChargeHotBG_[i] = hcalDepthChargeHotBG[i][k];
0870         hcalDepthEnergyCorr_[i] = hcalDepthEnergyCorr[i][k];
0871         hcalDepthEnergyHotCorr_[i] = hcalDepthEnergyHotCorr[i][k];
0872         hcalDepthMatch_[i] = hcalDepthMatch[i][k];
0873         hcalDepthMatchHot_[i] = hcalDepthMatchHot[i][k];
0874       }
0875       hcalActiveLength_ = hcalActiveLength[k];
0876       hcalActiveLengthHot_ = hcalActiveLengthHot[k];
0877       tree_->Fill();
0878     }
0879   }
0880 }
0881 
0882 // ------------ method called once each job just before starting event loop  ------------
0883 void HcalHBHEMuonAnalyzer::beginJob() {
0884   edm::Service<TFileService> fs;
0885   tree_ = fs->make<TTree>("TREE", "TREE");
0886   tree_->Branch("Event_No", &eventNumber_);
0887   tree_->Branch("Run_No", &runNumber_);
0888   tree_->Branch("LumiNumber", &lumiNumber_);
0889   tree_->Branch("BXNumber", &bxNumber_);
0890   tree_->Branch("GoodVertex", &goodVertex_);
0891   tree_->Branch("PF_Muon", &muon_is_good_);
0892   tree_->Branch("Global_Muon", &muon_global_);
0893   tree_->Branch("Tracker_muon", &muon_tracker_);
0894   tree_->Branch("MuonIsTight", &muon_is_tight_);
0895   tree_->Branch("MuonIsMedium", &muon_is_medium_);
0896   tree_->Branch("pt_of_muon", &ptGlob_);
0897   tree_->Branch("eta_of_muon", &etaGlob_);
0898   tree_->Branch("phi_of_muon", &phiGlob_);
0899   tree_->Branch("energy_of_muon", &energyMuon_);
0900   tree_->Branch("p_of_muon", &pMuon_);
0901   tree_->Branch("muon_trkKink", &muon_trkKink_);
0902   tree_->Branch("muon_chi2LocalPosition", &muon_chi2LocalPosition_);
0903   tree_->Branch("muon_segComp", &muon_segComp_);
0904 
0905   tree_->Branch("TrackerLayer", &trackerLayer_);
0906   tree_->Branch("NumPixelLayers", &numPixelLayers_);
0907   tree_->Branch("InnerTrackPixelHits", &tight_PixelHits_);
0908   tree_->Branch("innerTrack", &innerTrack_);
0909   tree_->Branch("chiTracker", &chiTracker_);
0910   tree_->Branch("DxyTracker", &dxyTracker_);
0911   tree_->Branch("DzTracker", &dzTracker_);
0912   tree_->Branch("innerTrackpt", &innerTrackpt_);
0913   tree_->Branch("innerTracketa", &innerTracketa_);
0914   tree_->Branch("innerTrackphi", &innerTrackphi_);
0915   tree_->Branch("tight_validFraction", &tight_validFraction_);
0916 
0917   tree_->Branch("OuterTrack", &outerTrack_);
0918   tree_->Branch("OuterTrackChi", &outerTrackChi_);
0919   tree_->Branch("OuterTrackPt", &outerTrackPt_);
0920   tree_->Branch("OuterTrackEta", &outerTrackEta_);
0921   tree_->Branch("OuterTrackPhi", &outerTrackPhi_);
0922   tree_->Branch("OuterTrackHits", &outerTrackHits_);
0923   tree_->Branch("OuterTrackRHits", &outerTrackRHits_);
0924 
0925   tree_->Branch("GlobalTrack", &globalTrack_);
0926   tree_->Branch("GlobalTrckPt", &globalTrckPt_);
0927   tree_->Branch("GlobalTrckEta", &globalTrckEta_);
0928   tree_->Branch("GlobalTrckPhi", &globalTrckPhi_);
0929   tree_->Branch("Global_Muon_Hits", &globalMuonHits_);
0930   tree_->Branch("MatchedStations", &matchedStat_);
0931   tree_->Branch("GlobTrack_Chi", &chiGlobal_);
0932   tree_->Branch("Tight_LongitudinalImpactparameter", &tight_LongPara_);
0933   tree_->Branch("Tight_TransImpactparameter", &tight_TransImpara_);
0934 
0935   tree_->Branch("IsolationR04", &isolationR04_);
0936   tree_->Branch("IsolationR03", &isolationR03_);
0937   tree_->Branch("ecal_3into3", &ecalEnergy_);
0938   tree_->Branch("hcal_3into3", &hcalEnergy_);
0939   tree_->Branch("tracker_3into3", &hoEnergy_);
0940 
0941   tree_->Branch("matchedId", &matchedId_);
0942   tree_->Branch("hcal_cellHot", &hcalHot_);
0943 
0944   tree_->Branch("ecal_3x3", &ecal3x3Energy_);
0945   tree_->Branch("hcal_1x1", &hcal1x1Energy_);
0946   tree_->Branch("ecal_detID", &ecalDetId_);
0947   tree_->Branch("hcal_detID", &hcalDetId_);
0948   tree_->Branch("ehcal_detID", &ehcalDetId_);
0949   tree_->Branch("hcal_ieta", &hcal_ieta_);
0950   tree_->Branch("hcal_iphi", &hcal_iphi_);
0951 
0952   char name[100];
0953   for (int k = 0; k < maxDepth_; ++k) {
0954     sprintf(name, "hcal_edepth%d", (k + 1));
0955     tree_->Branch(name, &hcalDepthEnergy_[k]);
0956     sprintf(name, "hcal_activeL%d", (k + 1));
0957     tree_->Branch(name, &hcalDepthActiveLength_[k]);
0958     sprintf(name, "hcal_edepthHot%d", (k + 1));
0959     tree_->Branch(name, &hcalDepthEnergyHot_[k]);
0960     sprintf(name, "hcal_activeHotL%d", (k + 1));
0961     tree_->Branch(name, &hcalDepthActiveLengthHot_[k]);
0962     sprintf(name, "hcal_cdepthHot%d", (k + 1));
0963     tree_->Branch(name, &hcalDepthChargeHot_[k]);
0964     sprintf(name, "hcal_cdepthHotBG%d", (k + 1));
0965     tree_->Branch(name, &hcalDepthChargeHotBG_[k]);
0966     sprintf(name, "hcal_edepthCorrect%d", (k + 1));
0967     tree_->Branch(name, &hcalDepthEnergyCorr_[k]);
0968     sprintf(name, "hcal_edepthHotCorrect%d", (k + 1));
0969     tree_->Branch(name, &hcalDepthEnergyHotCorr_[k]);
0970     sprintf(name, "hcal_depthMatch%d", (k + 1));
0971     tree_->Branch(name, &hcalDepthMatch_[k]);
0972     sprintf(name, "hcal_depthMatchHot%d", (k + 1));
0973     tree_->Branch(name, &hcalDepthMatchHot_[k]);
0974   }
0975 
0976   tree_->Branch("activeLength", &hcalActiveLength_);
0977   tree_->Branch("activeLengthHot", &hcalActiveLengthHot_);
0978 
0979   tree_->Branch("hltresults", &hltresults_);
0980   tree_->Branch("all_triggers", &all_triggers_);
0981 }
0982 
0983 // ------------ method called when starting to processes a run  ------------
0984 void HcalHBHEMuonAnalyzer::beginRun(edm::Run const& iRun, edm::EventSetup const& iSetup) {
0985   hdc_ = &iSetup.getData(tok_ddrec_);
0986   actHB.clear();
0987   actHE.clear();
0988   actHB = hdc_->getThickActive(0);
0989   actHE = hdc_->getThickActive(1);
0990 #ifdef EDM_ML_DEBUG
0991   unsigned int k1(0), k2(0);
0992   edm::LogVerbatim("HBHEMuon") << actHB.size() << " Active Length for HB";
0993   for (const auto& act : actHB) {
0994     edm::LogVerbatim("HBHEMuon") << "[" << k1 << "] ieta " << act.ieta << " depth " << act.depth << " zside "
0995                                  << act.zside << " type " << act.stype << " phi " << act.iphis.size() << ":"
0996                                  << act.iphis[0] << " L " << act.thick;
0997     HcalDetId hcid1(HcalBarrel, (act.ieta) * (act.zside), act.iphis[0], act.depth);
0998     HcalDetId hcid2 = mergedDepth_ ? hdc_->mergedDepthDetId(hcid1) : hcid1;
0999     edm::LogVerbatim("HBHEMuon") << hcid1 << " | " << hcid2 << " L " << activeLength(DetId(hcid2));
1000     ++k1;
1001   }
1002   edm::LogVerbatim("HBHEMuon") << actHE.size() << " Active Length for HE";
1003   for (const auto& act : actHE) {
1004     edm::LogVerbatim("HBHEMuon") << "[" << k2 << "] ieta " << act.ieta << " depth " << act.depth << " zside "
1005                                  << act.zside << " type " << act.stype << " phi " << act.iphis.size() << ":"
1006                                  << act.iphis[0] << " L " << act.thick;
1007     HcalDetId hcid1(HcalEndcap, (act.ieta) * (act.zside), act.iphis[0], act.depth);
1008     HcalDetId hcid2 = mergedDepth_ ? hdc_->mergedDepthDetId(hcid1) : hcid1;
1009     edm::LogVerbatim("HBHEMuon") << hcid1 << " | " << hcid2 << " L " << activeLength(DetId(hcid2));
1010     ++k2;
1011   }
1012 #endif
1013 
1014   bool changed = true;
1015   all_triggers_.clear();
1016   if (hltConfig_.init(iRun, iSetup, "HLT", changed)) {
1017     // if init returns TRUE, initialisation has succeeded!
1018 #ifdef EDM_ML_DEBUG
1019     edm::LogVerbatim("HBHEMuon") << "HLT config with process name HLT successfully extracted";
1020 #endif
1021     unsigned int ntriggers = hltConfig_.size();
1022     for (unsigned int t = 0; t < ntriggers; ++t) {
1023       std::string hltname(hltConfig_.triggerName(t));
1024       for (unsigned int ik = 0; ik < triggers_.size(); ++ik) {
1025         if (hltname.find(triggers_[ik]) != std::string::npos) {
1026           all_triggers_.push_back(hltname);
1027           break;
1028         }
1029       }
1030     }  //loop over ntriggers
1031     edm::LogVerbatim("HBHEMuon") << "All triggers size in begin run " << all_triggers_.size();
1032   } else {
1033     edm::LogError("HBHEMuon") << "Error! HLT config extraction with process name HLT failed";
1034   }
1035 
1036   theHBHETopology_ = &iSetup.getData(tok_htopo_);
1037   const HcalRespCorrs* resp = &iSetup.getData(tok_respcorr_);
1038   respCorrs_ = new HcalRespCorrs(*resp);
1039   respCorrs_->setTopo(theHBHETopology_);
1040   geo_ = &iSetup.getData(tok_geom_);
1041 
1042   // Write correction factors for all HB/HE events
1043   if (writeRespCorr_) {
1044     const HcalGeometry* gHcal = static_cast<const HcalGeometry*>(geo_->getSubdetectorGeometry(DetId::Hcal, HcalBarrel));
1045     const std::vector<DetId>& ids = gHcal->getValidDetIds(DetId::Hcal, 0);
1046     edm::LogVerbatim("HBHEMuon") << "\nTable of Correction Factors for Run " << iRun.run() << "\n";
1047     for (auto const& id : ids) {
1048       if ((id.det() == DetId::Hcal) && ((id.subdetId() == HcalBarrel) || (id.subdetId() == HcalEndcap))) {
1049         edm::LogVerbatim("HBHEMuon") << HcalDetId(id) << " " << id.rawId() << " "
1050                                      << (respCorrs_->getValues(id))->getValue();
1051       }
1052     }
1053   }
1054 }
1055 
1056 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
1057 void HcalHBHEMuonAnalyzer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
1058   edm::ParameterSetDescription desc;
1059   desc.add<edm::InputTag>("hlTriggerResults", edm::InputTag("TriggerResults", "", "HLT"));
1060   desc.add<edm::InputTag>("labelEBRecHit", edm::InputTag("ecalRecHit", "EcalRecHitsEB"));
1061   desc.add<edm::InputTag>("labelEERecHit", edm::InputTag("ecalRecHit", "EcalRecHitsEE"));
1062   desc.add<edm::InputTag>("labelHBHERecHit", edm::InputTag("hbhereco"));
1063   desc.add<std::string>("labelVertex", "offlinePrimaryVertices");
1064   desc.add<std::string>("labelMuon", "muons");
1065   std::vector<std::string> trig = {"HLT_IsoMu17", "HLT_IsoMu20", "HLT_IsoMu24", "HLT_IsoMu27", "HLT_Mu45", "HLT_Mu50"};
1066   desc.add<std::vector<std::string>>("triggers", trig);
1067   desc.addUntracked<int>("verbosity", 0);
1068   desc.add<int>("useRaw", 0);
1069   desc.add<bool>("unCorrect", false);
1070   desc.add<bool>("getCharge", false);
1071   desc.add<bool>("collapseDepth", false);
1072   desc.add<bool>("isItPlan1", false);
1073   desc.addUntracked<bool>("ignoreHECorr", false);
1074   desc.addUntracked<bool>("isItPreRecHit", false);
1075   desc.addUntracked<std::string>("moduleName", "");
1076   desc.addUntracked<std::string>("processName", "");
1077   desc.addUntracked<int>("maxDepth", 4);
1078   desc.addUntracked<std::string>("fileInCorr", "");
1079   desc.addUntracked<bool>("writeRespCorr", false);
1080   descriptions.add("hcalHBHEMuon", desc);
1081 }
1082 
1083 void HcalHBHEMuonAnalyzer::clearVectors() {
1084   ///clearing vectots
1085   eventNumber_ = -99999;
1086   runNumber_ = -99999;
1087   lumiNumber_ = -99999;
1088   bxNumber_ = -99999;
1089   goodVertex_ = -99999;
1090 
1091   muon_is_good_ = false;
1092   muon_global_ = false;
1093   muon_tracker_ = false;
1094   ptGlob_ = 0;
1095   etaGlob_ = 0;
1096   phiGlob_ = 0;
1097   energyMuon_ = 0;
1098   pMuon_ = 0;
1099   muon_trkKink_ = 0;
1100   muon_chi2LocalPosition_ = 0;
1101   muon_segComp_ = 0;
1102   muon_is_tight_ = false;
1103   muon_is_medium_ = false;
1104 
1105   trackerLayer_ = 0;
1106   numPixelLayers_ = 0;
1107   tight_PixelHits_ = 0;
1108   innerTrack_ = false;
1109   chiTracker_ = 0;
1110   dxyTracker_ = 0;
1111   dzTracker_ = 0;
1112   innerTrackpt_ = 0;
1113   innerTracketa_ = 0;
1114   innerTrackphi_ = 0;
1115   tight_validFraction_ = 0;
1116 
1117   outerTrack_ = false;
1118   outerTrackPt_ = 0;
1119   outerTrackEta_ = 0;
1120   outerTrackPhi_ = 0;
1121   outerTrackHits_ = 0;
1122   outerTrackRHits_ = 0;
1123   outerTrackChi_ = 0;
1124 
1125   globalTrack_ = false;
1126   globalTrckPt_ = 0;
1127   globalTrckEta_ = 0;
1128   globalTrckPhi_ = 0;
1129   globalMuonHits_ = 0;
1130   matchedStat_ = 0;
1131   chiGlobal_ = 0;
1132   tight_LongPara_ = 0;
1133   tight_TransImpara_ = 0;
1134 
1135   isolationR04_ = 0;
1136   isolationR03_ = 0;
1137   ecalEnergy_ = 0;
1138   hcalEnergy_ = 0;
1139   hoEnergy_ = 0;
1140   matchedId_ = false;
1141   hcalHot_ = false;
1142   ecal3x3Energy_ = 0;
1143   hcal1x1Energy_ = 0;
1144   ecalDetId_ = 0;
1145   hcalDetId_ = 0;
1146   ehcalDetId_ = 0;
1147   hcal_ieta_ = 0;
1148   hcal_iphi_ = 0;
1149   for (int i = 0; i < maxDepth_; ++i) {
1150     hcalDepthEnergy_[i] = 0;
1151     hcalDepthActiveLength_[i] = 0;
1152     hcalDepthEnergyHot_[i] = 0;
1153     hcalDepthActiveLengthHot_[i] = 0;
1154     hcalDepthChargeHot_[i] = 0;
1155     hcalDepthChargeHotBG_[i] = 0;
1156     hcalDepthEnergyCorr_[i] = 0;
1157     hcalDepthEnergyHotCorr_[i] = 0;
1158     hcalDepthMatch_[i] = false;
1159     hcalDepthMatchHot_[i] = false;
1160   }
1161   hcalActiveLength_ = 0;
1162   hcalActiveLengthHot_ = 0;
1163   hltresults_.clear();
1164 }
1165 
1166 int HcalHBHEMuonAnalyzer::matchId(const HcalDetId& id1, const HcalDetId& id2) {
1167   HcalDetId kd1(id1.subdet(), id1.ieta(), id1.iphi(), 1);
1168   HcalDetId kd2(id1.subdet(), id2.ieta(), id2.iphi(), 1);
1169   int match = ((kd1 == kd2) ? 1 : 0);
1170   return match;
1171 }
1172 
1173 double HcalHBHEMuonAnalyzer::activeLength(const DetId& id_) {
1174   HcalDetId id(id_);
1175   int ieta = id.ietaAbs();
1176   int zside = id.zside();
1177   int iphi = id.iphi();
1178   std::vector<int> dpths;
1179   if (mergedDepth_) {
1180     std::vector<HcalDetId> ids;
1181     hdc_->unmergeDepthDetId(id, ids);
1182     for (auto idh : ids)
1183       dpths.emplace_back(idh.depth());
1184   } else {
1185     dpths.emplace_back(id.depth());
1186   }
1187   double lx(0);
1188   if (id.subdet() == HcalBarrel) {
1189     for (unsigned int i = 0; i < actHB.size(); ++i) {
1190       if ((ieta == actHB[i].ieta) && (zside == actHB[i].zside) &&
1191           (std::find(dpths.begin(), dpths.end(), actHB[i].depth) != dpths.end()) &&
1192           (std::find(actHB[i].iphis.begin(), actHB[i].iphis.end(), iphi) != actHB[i].iphis.end())) {
1193         lx += actHB[i].thick;
1194       }
1195     }
1196   } else {
1197     for (unsigned int i = 0; i < actHE.size(); ++i) {
1198       if ((ieta == actHE[i].ieta) && (zside == actHE[i].zside) &&
1199           (std::find(dpths.begin(), dpths.end(), actHE[i].depth) != dpths.end()) &&
1200           (std::find(actHE[i].iphis.begin(), actHE[i].iphis.end(), iphi) != actHE[i].iphis.end())) {
1201         lx += actHE[i].thick;
1202       }
1203     }
1204   }
1205   return lx;
1206 }
1207 
1208 bool HcalHBHEMuonAnalyzer::isGoodVertex(const reco::Vertex& vtx) {
1209   if (vtx.isFake())
1210     return false;
1211   if (vtx.ndof() < 4)
1212     return false;
1213   if (vtx.position().Rho() > 2.)
1214     return false;
1215   if (fabs(vtx.position().Z()) > 24.)
1216     return false;
1217   return true;
1218 }
1219 
1220 double HcalHBHEMuonAnalyzer::respCorr(const DetId& id) {
1221   double cfac(1.0);
1222   if (useMyCorr_) {
1223     auto itr = corrValue_.find(id);
1224     if (itr != corrValue_.end())
1225       cfac = itr->second;
1226   } else if (respCorrs_ != nullptr) {
1227     cfac = (respCorrs_->getValues(id))->getValue();
1228   }
1229   return cfac;
1230 }
1231 
1232 double HcalHBHEMuonAnalyzer::gainFactor(const HcalDbService* conditions, const HcalDetId& id) {
1233   double gain(0.0);
1234   const HcalCalibrations& calibs = conditions->getHcalCalibrations(id);
1235   for (int capid = 0; capid < 4; ++capid)
1236     gain += (0.25 * calibs.respcorrgain(capid));
1237   return gain;
1238 }
1239 
1240 int HcalHBHEMuonAnalyzer::depth16HE(int ieta, int iphi) {
1241   // Transition between HB/HE is special
1242   // For Run 1 or for Plan1 standard reconstruction it is 3
1243   // For runs beyond 2018 or in Plan1 for HEP17 it is 4
1244   int zside = (ieta > 0) ? 1 : -1;
1245   int depth = theHBHETopology_->dddConstants()->getMinDepth(1, 16, iphi, zside);
1246   if (isItPlan1_ && (!isItPreRecHit_))
1247     depth = 3;
1248 #ifdef EDM_ML_DEBUG
1249   edm::LogVerbatim("HBHEMuon") << "Plan1 " << isItPlan1_ << " PreRecHit " << isItPreRecHit_ << " phi " << iphi
1250                                << " depth " << depth;
1251 #endif
1252   return depth;
1253 }
1254 
1255 bool HcalHBHEMuonAnalyzer::goodCell(const HcalDetId& hcid,
1256                                     const reco::Track* pTrack,
1257                                     const CaloGeometry* geo,
1258                                     const MagneticField* bField) {
1259   std::pair<double, double> rz = hdc_->getRZ(hcid);
1260   bool typeRZ = (hcid.subdet() == HcalEndcap) ? false : true;
1261   bool match = spr::propagateHCAL(pTrack, geo, bField, typeRZ, rz, (((verbosity_ / 10000) % 10) > 0));
1262   return match;
1263 }
1264 
1265 //define this as a plug-in
1266 #include "FWCore/Framework/interface/MakerMacros.h"
1267 
1268 DEFINE_FWK_MODULE(HcalHBHEMuonAnalyzer);