Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-10-10 04:02:16

0001 // user include files
0002 
0003 #include "DQMServices/Core/interface/DQMEDAnalyzer.h"
0004 #include "FWCore/Framework/interface/Frameworkfwd.h"
0005 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0006 
0007 #include "FWCore/Framework/interface/Event.h"
0008 #include "FWCore/Framework/interface/MakerMacros.h"
0009 
0010 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0011 
0012 #include "DQMServices/Core/interface/DQMStore.h"
0013 
0014 #include "DataFormats/DetId/interface/DetId.h"
0015 #include "DataFormats/CaloRecHit/interface/CaloClusterFwd.h"
0016 #include "DataFormats/HGCRecHit/interface/HGCRecHitCollections.h"
0017 #include "DataFormats/ParticleFlowReco/interface/PFCluster.h"
0018 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
0019 #include "DataFormats/EgammaCandidates/interface/Photon.h"
0020 
0021 #include "SimDataFormats/CaloAnalysis/interface/CaloParticle.h"
0022 #include "SimDataFormats/CaloAnalysis/interface/SimCluster.h"
0023 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticle.h"
0024 #include "SimDataFormats/TrackingAnalysis/interface/TrackingVertex.h"
0025 
0026 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
0027 #include "Geometry/Records/interface/CaloGeometryRecord.h"
0028 
0029 #include "RecoLocalCalo/HGCalRecAlgos/interface/RecHitTools.h"
0030 
0031 #include <map>
0032 #include <array>
0033 #include <string>
0034 #include <numeric>
0035 
0036 class HGCalShowerSeparation : public DQMEDAnalyzer {
0037 public:
0038   explicit HGCalShowerSeparation(const edm::ParameterSet&);
0039   ~HGCalShowerSeparation() override = default;
0040 
0041   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0042 
0043 private:
0044   void bookHistograms(DQMStore::IBooker&, edm::Run const&, edm::EventSetup const&) override;
0045   void analyze(const edm::Event&, const edm::EventSetup&) override;
0046 
0047   void fillWithRecHits(std::unordered_map<DetId, const HGCRecHit*>&, DetId, unsigned int, float, int&, float&);
0048 
0049   edm::EDGetTokenT<std::unordered_map<DetId, const HGCRecHit*>> hitMap_;
0050   edm::EDGetTokenT<std::vector<CaloParticle>> caloParticles_;
0051   const edm::ESGetToken<CaloGeometry, CaloGeometryRecord> tok_geom_;
0052 
0053   int debug_;
0054   bool filterOnEnergyAndCaloP_;
0055   hgcal::RecHitTools recHitTools_;
0056 
0057   MonitorElement* eta1_;
0058   MonitorElement* eta2_;
0059   MonitorElement* energy1_;
0060   MonitorElement* energy2_;
0061   MonitorElement* energytot_;
0062   MonitorElement* scEnergy_;
0063   MonitorElement* showerProfile_;
0064   MonitorElement* layerEnergy_;
0065   MonitorElement* layerDistance_;
0066   MonitorElement* etaPhi_;
0067   MonitorElement* deltaEtaPhi_;
0068   std::vector<MonitorElement*> profileOnLayer_;
0069   std::vector<MonitorElement*> globalProfileOnLayer_;
0070   std::vector<MonitorElement*> distanceOnLayer_;
0071   std::vector<MonitorElement*> idealDistanceOnLayer_;
0072   std::vector<MonitorElement*> idealDeltaXY_;
0073   std::vector<MonitorElement*> centers_;
0074 
0075   static constexpr int layers_ = 52;
0076 };
0077 
0078 HGCalShowerSeparation::HGCalShowerSeparation(const edm::ParameterSet& iConfig)
0079     : tok_geom_(esConsumes<CaloGeometry, CaloGeometryRecord>()),
0080       debug_(iConfig.getParameter<int>("debug")),
0081       filterOnEnergyAndCaloP_(iConfig.getParameter<bool>("filterOnEnergyAndCaloP")) {
0082   auto hitMapInputTag = iConfig.getParameter<edm::InputTag>("hitMapTag");
0083   auto caloParticles = iConfig.getParameter<edm::InputTag>("caloParticles");
0084   hitMap_ = consumes<std::unordered_map<DetId, const HGCRecHit*>>(hitMapInputTag);
0085   caloParticles_ = consumes<std::vector<CaloParticle>>(caloParticles);
0086 }
0087 
0088 void HGCalShowerSeparation::bookHistograms(DQMStore::IBooker& ibooker, edm::Run const& iRun, edm::EventSetup const&) {
0089   ibooker.cd();
0090   ibooker.setCurrentFolder("HGCalShowerSeparation");
0091   scEnergy_ = ibooker.book1D("SCEnergy", "SCEnergy", 240, 0., 120.);
0092   eta1_ = ibooker.book1D("eta1", "eta1", 80, 0., 4.);
0093   eta2_ = ibooker.book1D("eta2", "eta2", 80, 0., 4.);
0094   energy1_ = ibooker.book1D("energy1", "energy1", 240, 0., 120.);
0095   energy2_ = ibooker.book1D("energy2", "energy2", 240, 0., 120.);
0096   energytot_ = ibooker.book1D("energytot", "energytot", 200, 100., 200.);
0097   showerProfile_ = ibooker.book2D("ShowerProfile", "ShowerProfile", 800, -400., 400., layers_, 0., (float)layers_);
0098   layerEnergy_ = ibooker.book2D("LayerEnergy", "LayerEnergy", 60, 0., 60., 50, 0., 0.1);
0099   layerDistance_ = ibooker.book2D("LayerDistance", "LayerDistance", 60, 0., 60., 400, -400., 400.);
0100   etaPhi_ = ibooker.book2D("EtaPhi", "EtaPhi", 800, -4., 4., 800, -4., 4.);
0101   deltaEtaPhi_ = ibooker.book2D("DeltaEtaPhi", "DeltaEtaPhi", 100, -0.5, 0.5, 100, -0.5, 0.5);
0102   for (int i = 0; i < layers_; ++i) {
0103     profileOnLayer_.push_back(ibooker.book2D(std::string("ProfileOnLayer_") + std::to_string(i),
0104                                              std::string("ProfileOnLayer_") + std::to_string(i),
0105                                              120,
0106                                              -600.,
0107                                              600.,
0108                                              120,
0109                                              -600.,
0110                                              600.));
0111     globalProfileOnLayer_.push_back(ibooker.book2D(std::string("GlobalProfileOnLayer_") + std::to_string(i),
0112                                                    std::string("GlobalProfileOnLayer_") + std::to_string(i),
0113                                                    320,
0114                                                    -160.,
0115                                                    160.,
0116                                                    320,
0117                                                    -160.,
0118                                                    160.));
0119     distanceOnLayer_.push_back(ibooker.book1D(std::string("DistanceOnLayer_") + std::to_string(i),
0120                                               std::string("DistanceOnLayer_") + std::to_string(i),
0121                                               120,
0122                                               -600.,
0123                                               600.));
0124     idealDistanceOnLayer_.push_back(ibooker.book1D(std::string("IdealDistanceOnLayer_") + std::to_string(i),
0125                                                    std::string("IdealDistanceOnLayer_") + std::to_string(i),
0126                                                    120,
0127                                                    -600.,
0128                                                    600.));
0129     idealDeltaXY_.push_back(ibooker.book2D(std::string("IdealDeltaXY_") + std::to_string(i),
0130                                            std::string("IdealDeltaXY_") + std::to_string(i),
0131                                            800,
0132                                            -400.,
0133                                            400.,
0134                                            800,
0135                                            -400.,
0136                                            400.));
0137     centers_.push_back(ibooker.book2D(std::string("Centers_") + std::to_string(i),
0138                                       std::string("Centers_") + std::to_string(i),
0139                                       320,
0140                                       -1600.,
0141                                       1600.,
0142                                       320,
0143                                       -1600.,
0144                                       1600.));
0145   }
0146 }
0147 
0148 void HGCalShowerSeparation::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0149   recHitTools_.setGeometry(iSetup.getData(tok_geom_));
0150 
0151   const edm::Handle<std::vector<CaloParticle>>& caloParticleHandle = iEvent.getHandle(caloParticles_);
0152   const std::vector<CaloParticle>& caloParticles = *(caloParticleHandle.product());
0153 
0154   edm::Handle<std::unordered_map<DetId, const HGCRecHit*>> hitMapHandle;
0155   iEvent.getByToken(hitMap_, hitMapHandle);
0156   const auto hitmap = *hitMapHandle;
0157 
0158   // loop over caloParticles
0159   IfLogTrace(debug_ > 0, "HGCalShowerSeparation") << "Number of caloParticles: " << caloParticles.size() << std::endl;
0160   if (caloParticles.size() == 2) {
0161     auto eta1 = caloParticles[0].eta();
0162     auto phi1 = caloParticles[0].phi();
0163     auto theta1 = 2. * std::atan(exp(-eta1));
0164     auto eta2 = caloParticles[1].eta();
0165     auto phi2 = caloParticles[1].phi();
0166     auto theta2 = 2. * std::atan(exp(-eta2));
0167     eta1_->Fill(eta1);
0168     eta2_->Fill(eta2);
0169 
0170     // Select event only if the sum of the energy of its recHits
0171     // is close enough to the gen energy
0172     int count = 0;
0173     int size = 0;
0174     float energy = 0.;
0175     float energy_tmp = 0.;
0176     for (const auto& it_caloPart : caloParticles) {
0177       count++;
0178       const SimClusterRefVector& simClusterRefVector = it_caloPart.simClusters();
0179       size += simClusterRefVector.size();
0180       for (const auto& it_sc : simClusterRefVector) {
0181         const SimCluster& simCluster = (*(it_sc));
0182         const std::vector<std::pair<uint32_t, float>>& hits_and_fractions = simCluster.hits_and_fractions();
0183         for (const auto& it_haf : hits_and_fractions) {
0184           if (hitmap.count(it_haf.first))
0185             energy += hitmap.at(it_haf.first)->energy() * it_haf.second;
0186         }  //hits and fractions
0187       }    // simcluster
0188       if (count == 1) {
0189         energy1_->Fill(energy);
0190         energy_tmp = energy;
0191       } else {
0192         energy2_->Fill(energy - energy_tmp);
0193       }
0194     }  // caloParticle
0195     energytot_->Fill(energy);
0196     if (filterOnEnergyAndCaloP_ && (energy < 2. * 0.8 * 80 or size != 2))
0197       return;
0198 
0199     deltaEtaPhi_->Fill(eta1 - eta2, phi1 - phi2);
0200 
0201     for (const auto& it_caloPart : caloParticles) {
0202       const SimClusterRefVector& simClusterRefVector = it_caloPart.simClusters();
0203       IfLogTrace(debug_ > 0, "HGCalShowerSeparation") << ">>> " << simClusterRefVector.size() << std::endl;
0204       for (const auto& it_sc : simClusterRefVector) {
0205         const SimCluster& simCluster = (*(it_sc));
0206         if (simCluster.energy() < 80 * 0.8)
0207           continue;
0208         scEnergy_->Fill(simCluster.energy());
0209         IfLogTrace(debug_ > 1, "HGCalShowerSeparation")
0210             << ">>> SC.energy(): " << simCluster.energy() << " SC.simEnergy(): " << simCluster.simEnergy() << std::endl;
0211         const std::vector<std::pair<uint32_t, float>>& hits_and_fractions = simCluster.hits_and_fractions();
0212 
0213         for (const auto& it_haf : hits_and_fractions) {
0214           if (!hitmap.count(it_haf.first))
0215             continue;
0216           unsigned int hitlayer = recHitTools_.getLayerWithOffset(it_haf.first);
0217           auto global = recHitTools_.getPosition(it_haf.first);
0218           float globalx = global.x();
0219           float globaly = global.y();
0220           float globalz = global.z();
0221           if (globalz == 0)
0222             continue;
0223           auto rho1 = globalz * tan(theta1);
0224           auto rho2 = globalz * tan(theta2);
0225           auto x1 = rho1 * cos(phi1);
0226           auto y1 = rho1 * sin(phi1);
0227           auto x2 = rho2 * cos(phi2);
0228           auto y2 = rho2 * sin(phi2);
0229           auto half_point_x = (x1 + x2) / 2.;
0230           auto half_point_y = (y1 + y2) / 2.;
0231           auto half_point = sqrt((x1 - half_point_x) * (x1 - half_point_x) + (y1 - half_point_y) * (y1 - half_point_y));
0232           auto d_len = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
0233           auto dn_x = (x2 - x1) / d_len;
0234           auto dn_y = (y2 - y1) / d_len;
0235           auto distance = (globalx - x1) * dn_x + (globaly - y1) * dn_y;
0236           distance -= half_point;
0237           auto idealDistance = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
0238           if (hitmap.count(it_haf.first)) {
0239             profileOnLayer_[hitlayer]->Fill(10. * (globalx - half_point_x),
0240                                             10. * (globaly - half_point_y),
0241                                             hitmap.at(it_haf.first)->energy() * it_haf.second);
0242             profileOnLayer_[55]->Fill(10. * (globalx - half_point_x),
0243                                       10. * (globaly - half_point_y),
0244                                       hitmap.at(it_haf.first)->energy() * it_haf.second);
0245             globalProfileOnLayer_[hitlayer]->Fill(globalx, globaly, hitmap.at(it_haf.first)->energy() * it_haf.second);
0246             globalProfileOnLayer_[55]->Fill(globalx, globaly, hitmap.at(it_haf.first)->energy() * it_haf.second);
0247             layerEnergy_->Fill(hitlayer, hitmap.at(it_haf.first)->energy());
0248             layerDistance_->Fill(hitlayer, std::abs(10. * distance), hitmap.at(it_haf.first)->energy() * it_haf.second);
0249             etaPhi_->Fill(global.eta(), global.phi());
0250             distanceOnLayer_[hitlayer]->Fill(10. * distance);                  //,
0251             idealDistanceOnLayer_[hitlayer]->Fill(10. * idealDistance);        //,
0252             idealDeltaXY_[hitlayer]->Fill(10. * (x1 - x2), 10. * (y1 - y2));   //,
0253             centers_[hitlayer]->Fill(10. * half_point_x, 10. * half_point_y);  //,
0254             IfLogTrace(debug_ > 0, "HGCalShowerSeparation")
0255                 << ">>> " << distance << " " << hitlayer << " " << hitmap.at(it_haf.first)->energy() * it_haf.second
0256                 << std::endl;
0257             showerProfile_->Fill(10. * distance, hitlayer, hitmap.at(it_haf.first)->energy() * it_haf.second);
0258           }
0259         }  // end simHit
0260       }    // end simCluster
0261     }      // end caloparticle
0262   }
0263 }
0264 
0265 // ------------ method fills 'descriptions' with the allowed parameters for the
0266 // module  ------------
0267 void HGCalShowerSeparation::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0268   edm::ParameterSetDescription desc;
0269   desc.add<int>("debug", 1);
0270   desc.add<bool>("filterOnEnergyAndCaloP", false);
0271   desc.add<edm::InputTag>("caloParticles", edm::InputTag("mix", "MergedCaloTruth"));
0272   desc.add<edm::InputTag>("hitMapTag", edm::InputTag("hgcalRecHitMapProducer"));
0273   descriptions.add("hgcalShowerSeparationDefault", desc);
0274 }
0275 
0276 // define this as a plug-in
0277 DEFINE_FWK_MODULE(HGCalShowerSeparation);