File indexing completed on 2024-04-06 12:11:00
0001
0002
0003 #include "EventFilter/SiPixelRawToDigi/interface/PixelUnpackingRegions.h"
0004
0005 #include "FWCore/Framework/interface/ESHandle.h"
0006 #include "FWCore/Framework/interface/ESTransientHandle.h"
0007 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009
0010 #include "Geometry/CommonDetUnit/interface/PixelGeomDetUnit.h"
0011
0012 #include "DataFormats/SiPixelDetId/interface/PixelSubdetector.h"
0013 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
0014 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
0015 #include "DataFormats/Candidate/interface/LeafCandidate.h"
0016 #include "FWCore/Framework/interface/ConsumesCollector.h"
0017 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
0018
0019 #include <algorithm>
0020 #include <iterator>
0021
0022
0023 namespace {
0024 bool isBPIXModule(unsigned int id) { return DetId(id).subdetId() == PixelSubdetector::PixelBarrel; }
0025 bool isFPIXModule(unsigned int id) { return DetId(id).subdetId() == PixelSubdetector::PixelEndcap; }
0026 }
0027
0028 PixelUnpackingRegions::PixelUnpackingRegions(const edm::ParameterSet& conf, edm::ConsumesCollector&& iC) {
0029 edm::ParameterSet regPSet = conf.getParameter<edm::ParameterSet>("Regions");
0030 beamSpotTag_ = regPSet.getParameter<edm::InputTag>("beamSpot");
0031 inputs_ = regPSet.getParameter<std::vector<edm::InputTag> >("inputs");
0032 dPhi_ = regPSet.getParameter<std::vector<double> >("deltaPhi");
0033 maxZ_ = regPSet.getParameter<std::vector<double> >("maxZ");
0034 trackerGeomToken_ = iC.esConsumes<TrackerGeometry, TrackerDigiGeometryRecord>();
0035 cablingMapToken_ = iC.esConsumes<SiPixelFedCablingMap, SiPixelFedCablingMapRcd>();
0036
0037 tBeamSpot = iC.consumes<reco::BeamSpot>(beamSpotTag_);
0038 for (unsigned int t = 0; t < inputs_.size(); t++)
0039 tCandidateView.push_back(iC.consumes<reco::CandidateView>(inputs_[t]));
0040
0041 if (inputs_.size() != dPhi_.size() || dPhi_.size() != maxZ_.size()) {
0042 edm::LogError("PixelUnpackingRegions")
0043 << "Not the same size of config parameters vectors!\n"
0044 << " inputs " << inputs_.size() << " deltaPhi " << dPhi_.size() << " maxZ " << maxZ_.size();
0045 }
0046 }
0047
0048 void PixelUnpackingRegions::run(const edm::Event& e, const edm::EventSetup& es) {
0049 feds_.clear();
0050 modules_.clear();
0051 nreg_ = 0;
0052
0053 initialize(es);
0054
0055 edm::Handle<reco::BeamSpot> beamSpot;
0056 e.getByToken(tBeamSpot, beamSpot);
0057 beamSpot_ = beamSpot->position();
0058
0059
0060 size_t ninputs = inputs_.size();
0061 for (size_t input = 0; input < ninputs; ++input) {
0062 edm::Handle<reco::CandidateView> h;
0063 e.getByToken(tCandidateView[input], h);
0064
0065 size_t n = h->size();
0066 for (size_t i = 0; i < n; ++i) {
0067 const reco::Candidate& c = (*h)[i];
0068
0069
0070 Region r(c.momentum(), dPhi_[input], maxZ_[input]);
0071 addRegion(r);
0072 }
0073 }
0074 }
0075
0076 void PixelUnpackingRegions::initialize(const edm::EventSetup& es) {
0077
0078
0079 if (watcherSiPixelFedCablingMap_.check(es)) {
0080 edm::ESHandle<SiPixelFedCablingMap> cablingMap = es.getHandle(cablingMapToken_);
0081 cabling_ = cablingMap->cablingTree();
0082
0083
0084 edm::ESHandle<TrackerGeometry> geom = es.getHandle(trackerGeomToken_);
0085
0086
0087 unsigned int fedMin = FEDNumbering::MINSiPixelFEDID;
0088 unsigned int fedMax = FEDNumbering::MAXSiPixelFEDID;
0089 if ((geom->isThere(GeomDetEnumerators::P1PXB)) && (geom->isThere(GeomDetEnumerators::P1PXEC))) {
0090 fedMin = FEDNumbering::MINSiPixeluTCAFEDID;
0091 fedMax = FEDNumbering::MAXSiPixeluTCAFEDID;
0092 }
0093
0094 phiBPIX_.clear();
0095 phiFPIXp_.clear();
0096 phiFPIXm_.clear();
0097
0098 phiBPIX_.reserve(1024);
0099 phiFPIXp_.reserve(512);
0100 phiFPIXm_.reserve(512);
0101
0102 auto it = geom->dets().begin();
0103 for (; it != geom->dets().end(); ++it) {
0104 int subdet = (*it)->geographicalId().subdetId();
0105 if (!(subdet == PixelSubdetector::PixelBarrel || subdet == PixelSubdetector::PixelEndcap))
0106 continue;
0107
0108 Module m;
0109
0110 m.x = (*it)->position().x();
0111 m.y = (*it)->position().y();
0112 m.z = (*it)->position().z();
0113
0114 m.phi = (*it)->position().phi();
0115
0116 m.id = (*it)->geographicalId().rawId();
0117 const std::vector<sipixelobjects::CablingPathToDetUnit> path2det = cabling_->pathToDetUnit(m.id);
0118
0119 m.fed = path2det[0].fed;
0120 assert((m.fed <= fedMax) && (m.fed >= fedMin));
0121
0122 if (subdet == PixelSubdetector::PixelBarrel) {
0123 phiBPIX_.push_back(m);
0124 } else if (subdet == PixelSubdetector::PixelEndcap) {
0125 if (m.z > 0.)
0126 phiFPIXp_.push_back(m);
0127 else
0128 phiFPIXm_.push_back(m);
0129 }
0130 }
0131
0132
0133 std::sort(phiBPIX_.begin(), phiBPIX_.end());
0134 std::sort(phiFPIXp_.begin(), phiFPIXp_.end());
0135 std::sort(phiFPIXm_.begin(), phiFPIXm_.end());
0136 }
0137 }
0138
0139 void PixelUnpackingRegions::addRegion(Region& r) {
0140 ++nreg_;
0141
0142 float phi = r.v.phi();
0143
0144 Module lo(phi - r.dPhi);
0145 Module hi(phi + r.dPhi);
0146
0147 addRegionLocal(r, phiBPIX_, lo, hi);
0148 if (r.v.eta() > 1.) {
0149 addRegionLocal(r, phiFPIXp_, lo, hi);
0150 }
0151 if (r.v.eta() < -1.) {
0152 addRegionLocal(r, phiFPIXm_, lo, hi);
0153 }
0154 }
0155
0156 void PixelUnpackingRegions::addRegionLocal(Region& r,
0157 std::vector<Module>& container,
0158 const Module& _lo,
0159 const Module& _hi) {
0160 Module lo = _lo;
0161 Module hi = _hi;
0162 Module pi_m(-M_PI);
0163 Module pi_p(M_PI);
0164
0165 std::vector<Module>::const_iterator a, b;
0166
0167 if (lo.phi >= -M_PI && hi.phi <= M_PI)
0168 {
0169 a = lower_bound(container.begin(), container.end(), lo);
0170 b = upper_bound(container.begin(), container.end(), hi);
0171 gatherFromRange(r, a, b);
0172 } else
0173 {
0174 if (hi.phi > M_PI)
0175 hi.phi -= 2. * M_PI;
0176 a = lower_bound(container.begin(), container.end(), pi_m);
0177 b = upper_bound(container.begin(), container.end(), hi);
0178 gatherFromRange(r, a, b);
0179
0180 if (lo.phi < -M_PI)
0181 lo.phi += 2. * M_PI;
0182 a = lower_bound(container.begin(), container.end(), lo);
0183 b = upper_bound(container.begin(), container.end(), pi_p);
0184 gatherFromRange(r, a, b);
0185 }
0186 }
0187
0188 void PixelUnpackingRegions::gatherFromRange(Region& r,
0189 std::vector<Module>::const_iterator a,
0190 std::vector<Module>::const_iterator b) {
0191 for (; a != b; ++a) {
0192
0193 float zmodule = a->z - ((a->x - beamSpot_.x()) * r.cosphi + (a->y - beamSpot_.y()) * r.sinphi) * r.atantheta;
0194
0195
0196 if (std::abs(zmodule) > r.maxZ)
0197 continue;
0198
0199 feds_.insert(a->fed);
0200 modules_.insert(a->id);
0201 }
0202 }
0203
0204 bool PixelUnpackingRegions::mayUnpackFED(unsigned int fed_n) const {
0205 if (feds_.count(fed_n))
0206 return true;
0207 return false;
0208 }
0209
0210 bool PixelUnpackingRegions::mayUnpackModule(unsigned int id) const {
0211 if (modules_.count(id))
0212 return true;
0213 return false;
0214 }
0215
0216 unsigned int PixelUnpackingRegions::nBarrelModules() const {
0217 return std::count_if(modules_.begin(), modules_.end(), isBPIXModule);
0218 }
0219
0220 unsigned int PixelUnpackingRegions::nForwardModules() const {
0221 return std::count_if(modules_.begin(), modules_.end(), isFPIXModule);
0222 }