Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:26:53

0001 /** \file
0002  *
0003  *  \author N. Amapane - CERN
0004  *
0005  *  \modified by R. Radogna & C. Calabria & A. Sharma
0006  *  \modified by R. Radogna & C. Calabria
0007  *  \modified by D. Nash
0008  */
0009 
0010 #include <RecoMuon/DetLayers/interface/MuonDetLayerGeometry.h>
0011 
0012 #include <FWCore/Utilities/interface/Exception.h>
0013 #include <TrackingTools/DetLayers/interface/DetLayer.h>
0014 #include <DataFormats/MuonDetId/interface/CSCDetId.h>
0015 #include <DataFormats/MuonDetId/interface/DTChamberId.h>
0016 #include <DataFormats/MuonDetId/interface/RPCDetId.h>
0017 #include <DataFormats/MuonDetId/interface/GEMDetId.h>
0018 #include <DataFormats/MuonDetId/interface/ME0DetId.h>
0019 
0020 #include <Utilities/General/interface/precomputed_value_sort.h>
0021 #include <DataFormats/GeometrySurface/interface/GeometricSorting.h>
0022 
0023 #include <algorithm>
0024 
0025 using namespace std;
0026 using namespace geomsort;
0027 
0028 MuonDetLayerGeometry::MuonDetLayerGeometry() {}
0029 
0030 MuonDetLayerGeometry::~MuonDetLayerGeometry() {
0031   for (vector<const DetLayer*>::const_iterator it = allDetLayers.begin(); it != allDetLayers.end(); ++it) {
0032     delete *it;
0033   }
0034 }
0035 
0036 void MuonDetLayerGeometry::addCSCLayers(const pair<vector<DetLayer*>, vector<DetLayer*> >& csclayers) {
0037   for (auto const it : csclayers.first) {
0038     cscLayers_fw.push_back(it);
0039     allForward.push_back(it);
0040 
0041     detLayersMap[makeDetLayerId(it)] = it;
0042   }
0043 
0044   for (auto const it : csclayers.second) {
0045     cscLayers_bk.push_back(it);
0046     allBackward.push_back(it);
0047 
0048     detLayersMap[makeDetLayerId(it)] = it;
0049   }
0050 }
0051 
0052 void MuonDetLayerGeometry::addGEMLayers(const pair<vector<DetLayer*>, vector<DetLayer*> >& gemlayers) {
0053   LogDebug("Muon|RecoMuon|MuonDetLayerGeometry") << "Adding GEMlayers " << std::endl;
0054   for (auto const it : gemlayers.first) {
0055     gemLayers_fw.push_back(it);
0056     allForward.push_back(it);
0057     detLayersMap[makeDetLayerId(it)] = it;
0058     LogDebug("Muon|RecoMuon|MuonDetLayerGeometry") << "Adding GEMforward " << std::endl;
0059   }
0060   for (auto const it : gemlayers.second) {
0061     gemLayers_bk.push_back(it);
0062     allBackward.push_back(it);
0063     detLayersMap[makeDetLayerId(it)] = it;
0064     LogDebug("Muon|RecoMuon|MuonDetLayerGeometry") << "Adding GEMbackward " << std::endl;
0065   }
0066 }
0067 
0068 void MuonDetLayerGeometry::addME0Layers(const pair<vector<DetLayer*>, vector<DetLayer*> >& me0layers) {
0069   LogDebug("Muon|RecoMuon|MuonDetLayerGeometry") << "Adding ME0layers " << std::endl;
0070 
0071   for (auto const it : me0layers.first) {
0072     me0Layers_fw.push_back(it);
0073     allForward.push_back(it);
0074 
0075     detLayersMap[makeDetLayerId(it)] = it;
0076     LogDebug("Muon|RecoMuon|MuonDetLayerGeometry") << "Adding ME0forward " << std::endl;
0077   }
0078   for (auto const it : me0layers.second) {
0079     me0Layers_bk.push_back(it);
0080     allBackward.push_back(it);
0081 
0082     detLayersMap[makeDetLayerId(it)] = it;
0083     LogDebug("Muon|RecoMuon|MuonDetLayerGeometry") << "Adding ME0backward " << std::endl;
0084   }
0085 }
0086 
0087 void MuonDetLayerGeometry::addRPCLayers(const vector<DetLayer*>& barrelLayers,
0088                                         const pair<vector<DetLayer*>, vector<DetLayer*> >& endcapLayers) {
0089   for (auto const it : barrelLayers) {
0090     rpcLayers_barrel.push_back(it);
0091     allBarrel.push_back(it);
0092 
0093     detLayersMap[makeDetLayerId(it)] = it;
0094   }
0095   for (auto const it : endcapLayers.first) {
0096     rpcLayers_fw.push_back(it);
0097     allForward.push_back(it);
0098 
0099     detLayersMap[makeDetLayerId(it)] = it;
0100   }
0101 
0102   for (auto const it : endcapLayers.second) {
0103     rpcLayers_bk.push_back(it);
0104     allBackward.push_back(it);
0105 
0106     detLayersMap[makeDetLayerId(it)] = it;
0107   }
0108 }
0109 
0110 void MuonDetLayerGeometry::addDTLayers(const vector<DetLayer*>& dtlayers) {
0111   for (auto const it : dtlayers) {
0112     dtLayers.push_back(it);
0113     allBarrel.push_back(it);
0114 
0115     detLayersMap[makeDetLayerId(it)] = it;
0116   }
0117 }
0118 
0119 DetId MuonDetLayerGeometry::makeDetLayerId(const DetLayer* detLayer) const {
0120   if (detLayer->subDetector() == GeomDetEnumerators::CSC) {
0121     CSCDetId id(detLayer->basicComponents().front()->geographicalId().rawId());
0122 
0123     if (id.station() == 1) {
0124       if (id.ring() == 1 || id.ring() == 4)
0125         return CSCDetId(id.endcap(), 1, 1, 0, 0);
0126       else if (id.ring() == 2 || id.ring() == 3)
0127         return CSCDetId(id.endcap(), 1, 2, 0, 0);
0128       else
0129         throw cms::Exception("InvalidCSCRing") << " Invalid CSC Ring: " << id.ring() << endl;
0130     } else
0131       return CSCDetId(id.endcap(), id.station(), 0, 0, 0);
0132 
0133   } else if (detLayer->subDetector() == GeomDetEnumerators::DT) {
0134     DTChamberId id(detLayer->basicComponents().front()->geographicalId().rawId());
0135     return DTChamberId(0, id.station(), 0);
0136   } else if (detLayer->subDetector() == GeomDetEnumerators::RPCBarrel ||
0137              detLayer->subDetector() == GeomDetEnumerators::RPCEndcap) {
0138     RPCDetId id(detLayer->basicComponents().front()->geographicalId().rawId());
0139     return RPCDetId(id.region(), 0, id.station(), 0, id.layer(), 0, 0);
0140   } else if (detLayer->subDetector() == GeomDetEnumerators::GEM) {
0141     GEMDetId id(detLayer->basicComponents().front()->geographicalId().rawId());
0142     return GEMDetId(id.region(), 1, id.station(), id.layer(), 0, 0);
0143   } else if (detLayer->subDetector() == GeomDetEnumerators::ME0) {
0144     ME0DetId id(detLayer->basicComponents().front()->geographicalId().rawId());
0145     return ME0DetId(id.region(), id.layer(), 0, 0);
0146   } else
0147     throw cms::Exception("InvalidModuleIdentification");  // << detLayer->module();
0148 }
0149 
0150 const vector<const DetLayer*>& MuonDetLayerGeometry::allDTLayers() const { return dtLayers; }
0151 
0152 const vector<const DetLayer*>& MuonDetLayerGeometry::allCSCLayers() const { return cscLayers_all; }
0153 
0154 const vector<const DetLayer*>& MuonDetLayerGeometry::forwardCSCLayers() const { return cscLayers_fw; }
0155 
0156 const vector<const DetLayer*>& MuonDetLayerGeometry::backwardCSCLayers() const { return cscLayers_bk; }
0157 
0158 ////////////////////////////GEMs
0159 const vector<const DetLayer*>& MuonDetLayerGeometry::allGEMLayers() const { return gemLayers_all; }
0160 
0161 const vector<const DetLayer*>& MuonDetLayerGeometry::forwardGEMLayers() const { return gemLayers_fw; }
0162 
0163 const vector<const DetLayer*>& MuonDetLayerGeometry::backwardGEMLayers() const { return gemLayers_bk; }
0164 
0165 //////////////////////////////////////////
0166 
0167 //////////////////// ME0s
0168 
0169 const vector<const DetLayer*>& MuonDetLayerGeometry::allME0Layers() const { return me0Layers_all; }
0170 
0171 const vector<const DetLayer*>& MuonDetLayerGeometry::forwardME0Layers() const { return me0Layers_fw; }
0172 
0173 const vector<const DetLayer*>& MuonDetLayerGeometry::backwardME0Layers() const { return me0Layers_bk; }
0174 
0175 ////////////////////
0176 
0177 const vector<const DetLayer*>& MuonDetLayerGeometry::allRPCLayers() const { return rpcLayers_all; }
0178 
0179 const vector<const DetLayer*>& MuonDetLayerGeometry::barrelRPCLayers() const { return rpcLayers_barrel; }
0180 
0181 const vector<const DetLayer*>& MuonDetLayerGeometry::endcapRPCLayers() const { return rpcLayers_endcap; }
0182 
0183 const vector<const DetLayer*>& MuonDetLayerGeometry::forwardRPCLayers() const { return rpcLayers_fw; }
0184 
0185 const vector<const DetLayer*>& MuonDetLayerGeometry::backwardRPCLayers() const { return rpcLayers_bk; }
0186 
0187 const vector<const DetLayer*>& MuonDetLayerGeometry::allLayers() const { return allDetLayers; }
0188 
0189 const vector<const DetLayer*>& MuonDetLayerGeometry::allBarrelLayers() const { return allBarrel; }
0190 
0191 const vector<const DetLayer*>& MuonDetLayerGeometry::allEndcapLayers() const { return allEndcap; }
0192 
0193 const vector<const DetLayer*>& MuonDetLayerGeometry::allForwardLayers() const { return allForward; }
0194 
0195 const vector<const DetLayer*>& MuonDetLayerGeometry::allBackwardLayers() const { return allBackward; }
0196 
0197 //////////////////////////////GEMs
0198 //
0199 const vector<const DetLayer*>& MuonDetLayerGeometry::allEndcapCscGemLayers() const { return allEndcapCscGem; }
0200 
0201 const vector<const DetLayer*>& MuonDetLayerGeometry::allCscGemForwardLayers() const { return allCscGemForward; }
0202 
0203 const vector<const DetLayer*>& MuonDetLayerGeometry::allCscGemBackwardLayers() const { return allCscGemBackward; }
0204 
0205 //////////////////// ME0s
0206 
0207 const vector<const DetLayer*>& MuonDetLayerGeometry::allEndcapCscME0Layers() const { return allEndcapCscME0; }
0208 
0209 const vector<const DetLayer*>& MuonDetLayerGeometry::allCscME0ForwardLayers() const { return allCscME0Forward; }
0210 
0211 const vector<const DetLayer*>& MuonDetLayerGeometry::allCscME0BackwardLayers() const { return allCscME0Backward; }
0212 
0213 ////////////////////////////////////////////////////
0214 
0215 const DetLayer* MuonDetLayerGeometry::idToLayer(const DetId& detId) const {
0216   DetId id;
0217 
0218   if (detId.subdetId() == MuonSubdetId::CSC) {
0219     CSCDetId cscId(detId.rawId());
0220 
0221     if (cscId.station() == 1) {
0222       if (cscId.ring() == 1 || cscId.ring() == 4)
0223         id = CSCDetId(cscId.endcap(), 1, 1, 0, 0);
0224       else if (cscId.ring() == 2 || cscId.ring() == 3)
0225         id = CSCDetId(cscId.endcap(), 1, 2, 0, 0);
0226       else
0227         throw cms::Exception("InvalidCSCRing") << " Invalid CSC Ring: " << cscId.ring() << endl;
0228     } else
0229       id = CSCDetId(cscId.endcap(), cscId.station(), 0, 0, 0);
0230   }
0231 
0232   else if (detId.subdetId() == MuonSubdetId::DT) {
0233     DTChamberId dtId(detId.rawId());
0234     id = DTChamberId(0, dtId.station(), 0);
0235   } else if (detId.subdetId() == MuonSubdetId::RPC) {
0236     RPCDetId rpcId(detId.rawId());
0237     id = RPCDetId(rpcId.region(), 0, rpcId.station(), 0, rpcId.layer(), 0, 0);
0238   } else if (detId.subdetId() == MuonSubdetId::GEM) {
0239     GEMDetId gemId(detId.rawId());
0240     id = GEMDetId(gemId.region(), 1, gemId.station(), gemId.layer(), 0, 0);
0241   } else if (detId.subdetId() == MuonSubdetId::ME0) {
0242     ME0DetId me0Id(detId.rawId());
0243     id = ME0DetId(me0Id.region(), me0Id.layer(), 0, 0);
0244     LogDebug("Muon|RecoMuon|MuonDetLayerGeometry")
0245         << " Found an ME0DetId:  " << me0Id.rawId() << ",id: " << id.rawId() << std::endl;
0246 
0247   } else
0248     throw cms::Exception("InvalidSubdetId") << detId.subdetId();
0249 
0250   std::map<DetId, const DetLayer*>::const_iterator layer = detLayersMap.find(id);
0251   if (layer == detLayersMap.end())
0252     return nullptr;
0253   return layer->second;
0254 }
0255 
0256 // Quick way to sort barrel det layers by increasing R,
0257 // do not abuse!
0258 #include <TrackingTools/DetLayers/interface/BarrelDetLayer.h>
0259 struct ExtractBarrelDetLayerR {
0260   typedef Surface::Scalar result_type;
0261   result_type operator()(const DetLayer* p) const {
0262     const BarrelDetLayer* bdl = dynamic_cast<const BarrelDetLayer*>(p);
0263     if (bdl)
0264       return bdl->specificSurface().radius();
0265     else
0266       return -1.;
0267   }
0268 };
0269 
0270 void MuonDetLayerGeometry::sortLayers() {
0271   // The following are filled inside-out, no need to re-sort
0272   // precomputed_value_sort(dtLayers.begin(), dtLayers.end(),ExtractR<DetLayer,float>());
0273   // precomputed_value_sort(cscLayers_fw.begin(), cscLayers_fw.end(),ExtractAbsZ<DetLayer,float>());
0274   // precomputed_value_sort(cscLayers_bk.begin(), cscLayers_bk.end(),ExtractAbsZ<DetLayer,float>());
0275   // precomputed_value_sort(rpcLayers_fw.begin(), rpcLayers_fw.end(),ExtractAbsZ<DetLayer,float>());
0276   // precomputed_value_sort(rpcLayers_bk.begin(), rpcLayers_bk.end(),ExtractAbsZ<DetLayer,float>());
0277   // precomputed_value_sort(rpcLayers_barrel.begin(), rpcLayers_barrel.end(), ExtractR<DetLayer,float>());
0278 
0279   // Sort these inside-out
0280   precomputed_value_sort(allBarrel.begin(), allBarrel.end(), ExtractBarrelDetLayerR());
0281   precomputed_value_sort(allBackward.begin(), allBackward.end(), ExtractAbsZ<DetLayer, float>());
0282   precomputed_value_sort(allForward.begin(), allForward.end(), ExtractAbsZ<DetLayer, float>());
0283 
0284   // Build more complicated vectors with correct sorting
0285 
0286   //cscLayers_all: from -Z to +Z
0287   cscLayers_all.reserve(cscLayers_bk.size() + cscLayers_fw.size());
0288   std::copy(cscLayers_bk.begin(), cscLayers_bk.end(), back_inserter(cscLayers_all));
0289   std::reverse(cscLayers_all.begin(), cscLayers_all.end());
0290   std::copy(cscLayers_fw.begin(), cscLayers_fw.end(), back_inserter(cscLayers_all));
0291 
0292   //gemLayers_all: from -Z to +Z
0293   gemLayers_all.reserve(gemLayers_bk.size() + gemLayers_fw.size());
0294   std::copy(gemLayers_bk.begin(), gemLayers_bk.end(), back_inserter(gemLayers_all));
0295   std::reverse(gemLayers_all.begin(), gemLayers_all.end());
0296   std::copy(gemLayers_fw.begin(), gemLayers_fw.end(), back_inserter(gemLayers_all));
0297 
0298   //me0Layers_all: from -Z to +Z
0299   me0Layers_all.reserve(me0Layers_bk.size() + me0Layers_fw.size());
0300   std::copy(me0Layers_bk.begin(), me0Layers_bk.end(), back_inserter(me0Layers_all));
0301   std::reverse(me0Layers_all.begin(), me0Layers_all.end());
0302   std::copy(me0Layers_fw.begin(), me0Layers_fw.end(), back_inserter(me0Layers_all));
0303 
0304   //rpcLayers_endcap: from -Z to +Z
0305   rpcLayers_endcap.reserve(rpcLayers_bk.size() + rpcLayers_fw.size());
0306   std::copy(rpcLayers_bk.begin(), rpcLayers_bk.end(), back_inserter(rpcLayers_endcap));
0307   std::reverse(rpcLayers_endcap.begin(), rpcLayers_endcap.end());
0308   std::copy(rpcLayers_fw.begin(), rpcLayers_fw.end(), back_inserter(rpcLayers_endcap));
0309 
0310   //rpcLayers_all: order is bw, barrel, fw
0311   rpcLayers_all.reserve(rpcLayers_bk.size() + rpcLayers_barrel.size() + rpcLayers_fw.size());
0312   std::copy(rpcLayers_bk.begin(), rpcLayers_bk.end(), back_inserter(rpcLayers_all));
0313   std::reverse(rpcLayers_all.begin(), rpcLayers_all.end());
0314   std::copy(rpcLayers_barrel.begin(), rpcLayers_barrel.end(), back_inserter(rpcLayers_all));
0315   std::copy(rpcLayers_fw.begin(), rpcLayers_fw.end(), back_inserter(rpcLayers_all));
0316 
0317   // allEndcap: order is  all bw, all fw
0318   allEndcap.reserve(allBackward.size() + allForward.size());
0319   std::copy(allBackward.begin(), allBackward.end(), back_inserter(allEndcap));
0320   std::reverse(allEndcap.begin(), allEndcap.end());
0321   std::copy(allForward.begin(), allForward.end(), back_inserter(allEndcap));
0322 
0323   // allEndcapCSCGEM: order is  all bw, all fw
0324   allEndcapCscGem.reserve(cscLayers_bk.size() + cscLayers_fw.size() + gemLayers_bk.size() + gemLayers_fw.size());
0325   std::copy(cscLayers_bk.begin(), cscLayers_bk.end(), back_inserter(allEndcapCscGem));
0326   std::copy(gemLayers_bk.begin(), gemLayers_bk.end(), back_inserter(allEndcapCscGem));
0327   std::reverse(allEndcapCscGem.begin(), allEndcapCscGem.end());
0328   std::copy(cscLayers_fw.begin(), cscLayers_fw.end(), back_inserter(allEndcapCscGem));
0329   std::copy(gemLayers_fw.begin(), gemLayers_fw.end(), back_inserter(allEndcapCscGem));
0330 
0331   // allCscGemForward
0332   allCscGemForward.reserve(cscLayers_fw.size() + gemLayers_fw.size());
0333   std::copy(cscLayers_fw.begin(), cscLayers_fw.end(), back_inserter(allCscGemForward));
0334   std::copy(gemLayers_fw.begin(), gemLayers_fw.end(), back_inserter(allCscGemForward));
0335 
0336   // allCscGemBackward
0337   allCscGemBackward.reserve(cscLayers_bk.size() + gemLayers_bk.size());
0338   std::copy(cscLayers_bk.begin(), cscLayers_bk.end(), back_inserter(allCscGemBackward));
0339   std::copy(gemLayers_bk.begin(), gemLayers_bk.end(), back_inserter(allCscGemBackward));
0340 
0341   // allCscME0Forward
0342   allCscME0Forward.reserve(cscLayers_fw.size() + me0Layers_fw.size());
0343   std::copy(cscLayers_fw.begin(), cscLayers_fw.end(), back_inserter(allCscME0Forward));
0344   std::copy(me0Layers_fw.begin(), me0Layers_fw.end(), back_inserter(allCscME0Forward));
0345 
0346   // allCscME0Backward
0347   allCscME0Backward.reserve(cscLayers_bk.size() + me0Layers_bk.size());
0348   std::copy(cscLayers_bk.begin(), cscLayers_bk.end(), back_inserter(allCscME0Backward));
0349   std::copy(me0Layers_bk.begin(), me0Layers_bk.end(), back_inserter(allCscME0Backward));
0350 
0351   // allEndcapCSCME0: order is  all bw, all fw
0352   allEndcapCscME0.reserve(cscLayers_bk.size() + cscLayers_fw.size() + me0Layers_bk.size() + me0Layers_fw.size());
0353   std::copy(cscLayers_bk.begin(), cscLayers_bk.end(), back_inserter(allEndcapCscME0));
0354   std::copy(me0Layers_bk.begin(), me0Layers_bk.end(), back_inserter(allEndcapCscME0));
0355   std::reverse(allEndcapCscME0.begin(), allEndcapCscME0.end());
0356   std::copy(cscLayers_fw.begin(), cscLayers_fw.end(), back_inserter(allEndcapCscME0));
0357   std::copy(me0Layers_fw.begin(), me0Layers_fw.end(), back_inserter(allEndcapCscME0));
0358 
0359   // allDetLayers: order is  all bw, all barrel, all fw
0360   allDetLayers.reserve(allBackward.size() + allBarrel.size() + allForward.size());
0361   std::copy(allBackward.begin(), allBackward.end(), back_inserter(allDetLayers));
0362   std::reverse(allDetLayers.begin(), allDetLayers.end());
0363   std::copy(allBarrel.begin(), allBarrel.end(), back_inserter(allDetLayers));
0364   std::copy(allForward.begin(), allForward.end(), back_inserter(allDetLayers));
0365 
0366   // number layers
0367   int sq = 0;
0368   for (auto l : allDetLayers)
0369     (*const_cast<DetLayer*>(l)).setSeqNum(sq++);
0370 }