Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:27:39

0001 #include "FWCore/Framework/interface/MakerMacros.h"
0002 
0003 #include "DataFormats/Common/interface/DetSetVector.h"
0004 #include "DataFormats/Common/interface/DetSet.h"
0005 #include "FWCore/Framework/interface/stream/EDProducer.h"
0006 #include "FWCore/Framework/interface/Event.h"
0007 #include "FWCore/Framework/interface/EventSetup.h"
0008 #include "DataFormats/Common/interface/Handle.h"
0009 #include "FWCore/Framework/interface/ESHandle.h"
0010 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0011 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0012 
0013 #include "FWCore/Framework/interface/ESWatcher.h"
0014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0015 #include "FWCore/Utilities/interface/InputTag.h"
0016 #include "FWCore/Utilities/interface/ESGetToken.h"
0017 
0018 #include "Geometry/Records/interface/VeryForwardRealGeometryRecord.h"
0019 
0020 #include "DataFormats/CTPPSReco/interface/CTPPSPixelRecHit.h"
0021 #include "DataFormats/CTPPSReco/interface/CTPPSPixelLocalTrack.h"
0022 #include "DataFormats/DetId/interface/DetId.h"
0023 
0024 #include "RecoPPS/Local/interface/RPixDetPatternFinder.h"
0025 #include "RecoPPS/Local/interface/RPixDetTrackFinder.h"
0026 
0027 #include <memory>
0028 
0029 #include <string>
0030 #include <vector>
0031 
0032 #include "FWCore/Framework/interface/Frameworkfwd.h"
0033 
0034 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0035 
0036 #include "DataFormats/CTPPSDetId/interface/CTPPSPixelDetId.h"
0037 
0038 #include "RecoPPS/Local/interface/RPixRoadFinder.h"
0039 #include "RecoPPS/Local/interface/RPixPlaneCombinatoryTracking.h"
0040 
0041 #include "CondFormats/PPSObjects/interface/CTPPSPixelAnalysisMask.h"
0042 #include "CondFormats/DataRecord/interface/CTPPSPixelAnalysisMaskRcd.h"
0043 
0044 namespace {
0045   constexpr int rocMask = 0xE000;
0046   constexpr int rocOffset = 13;
0047   constexpr int rocSizeInPixels = 4160;
0048 }  // namespace
0049 
0050 class CTPPSPixelLocalTrackProducer : public edm::stream::EDProducer<> {
0051 public:
0052   explicit CTPPSPixelLocalTrackProducer(const edm::ParameterSet &parameterSet);
0053 
0054   ~CTPPSPixelLocalTrackProducer() override;
0055 
0056   void produce(edm::Event &, const edm::EventSetup &) override;
0057   static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
0058 
0059 private:
0060   const int verbosity_;
0061   const int maxHitPerPlane_;
0062   const int maxHitPerRomanPot_;
0063   const int maxTrackPerRomanPot_;
0064   const int maxTrackPerPattern_;
0065 
0066   const edm::EDGetTokenT<edm::DetSetVector<CTPPSPixelRecHit>> tokenCTPPSPixelRecHit_;
0067   const edm::ESGetToken<CTPPSGeometry, VeryForwardRealGeometryRecord> tokenCTPPSGeometry_;
0068   edm::ESWatcher<VeryForwardRealGeometryRecord> geometryWatcher_;
0069 
0070   const edm::ESGetToken<CTPPSPixelAnalysisMask, CTPPSPixelAnalysisMaskRcd> tokenCTPPSPixelAnalysisMask_;
0071 
0072   const uint32_t numberOfPlanesPerPot_;
0073 
0074   std::unique_ptr<RPixDetPatternFinder> patternFinder_;
0075   std::unique_ptr<RPixDetTrackFinder> trackFinder_;
0076 };
0077 
0078 //------------------------------------------------------------------------------------------------//
0079 
0080 CTPPSPixelLocalTrackProducer::CTPPSPixelLocalTrackProducer(const edm::ParameterSet &parameterSet)
0081     : verbosity_(parameterSet.getUntrackedParameter<int>("verbosity")),
0082       maxHitPerPlane_(parameterSet.getParameter<int>("maxHitPerPlane")),
0083       maxHitPerRomanPot_(parameterSet.getParameter<int>("maxHitPerRomanPot")),
0084       maxTrackPerRomanPot_(parameterSet.getParameter<int>("maxTrackPerRomanPot")),
0085       maxTrackPerPattern_(parameterSet.getParameter<int>("maxTrackPerPattern")),
0086       tokenCTPPSPixelRecHit_(
0087           consumes<edm::DetSetVector<CTPPSPixelRecHit>>(parameterSet.getParameter<edm::InputTag>("tag"))),
0088       tokenCTPPSGeometry_(esConsumes<CTPPSGeometry, VeryForwardRealGeometryRecord>()),
0089       tokenCTPPSPixelAnalysisMask_(esConsumes<CTPPSPixelAnalysisMask, CTPPSPixelAnalysisMaskRcd>()),
0090       numberOfPlanesPerPot_(parameterSet.getParameter<int>("numberOfPlanesPerPot")) {
0091   std::string patternFinderAlgorithm = parameterSet.getParameter<std::string>("patternFinderAlgorithm");
0092   std::string trackFitterAlgorithm = parameterSet.getParameter<std::string>("trackFinderAlgorithm");
0093 
0094   // pattern algorithm selector
0095   if (patternFinderAlgorithm == "RPixRoadFinder") {
0096     patternFinder_ = std::make_unique<RPixRoadFinder>(parameterSet);
0097   } else {
0098     throw cms::Exception("CTPPSPixelLocalTrackProducer")
0099         << "Pattern finder algorithm" << patternFinderAlgorithm << " does not exist";
0100   }
0101 
0102   std::vector<uint32_t> listOfAllPlanes;
0103   for (uint32_t i = 0; i < numberOfPlanesPerPot_; ++i) {
0104     listOfAllPlanes.push_back(i);
0105   }
0106 
0107   //tracking algorithm selector
0108   if (trackFitterAlgorithm == "RPixPlaneCombinatoryTracking") {
0109     trackFinder_ = std::make_unique<RPixPlaneCombinatoryTracking>(parameterSet);
0110   } else {
0111     throw cms::Exception("CTPPSPixelLocalTrackProducer")
0112         << "Tracking fitter algorithm" << trackFitterAlgorithm << " does not exist";
0113   }
0114   trackFinder_->setListOfPlanes(listOfAllPlanes);
0115   trackFinder_->initialize();
0116   produces<edm::DetSetVector<CTPPSPixelLocalTrack>>();
0117 }
0118 
0119 //------------------------------------------------------------------------------------------------//
0120 
0121 CTPPSPixelLocalTrackProducer::~CTPPSPixelLocalTrackProducer() {}
0122 
0123 //------------------------------------------------------------------------------------------------//
0124 
0125 void CTPPSPixelLocalTrackProducer::fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
0126   edm::ParameterSetDescription desc;
0127 
0128   desc.add<edm::InputTag>("tag", edm::InputTag("ctppsPixelRecHits"))
0129       ->setComment("inputTag of the RecHits input for the tracking algorithm");
0130   desc.add<std::string>("patternFinderAlgorithm", "RPixRoadFinder")->setComment("algorithm type for pattern finder");
0131   desc.add<std::string>("trackFinderAlgorithm", "RPixPlaneCombinatoryTracking")
0132       ->setComment("algorithm type for track finder");
0133   desc.add<uint>("trackMinNumberOfPoints", 3)->setComment("minimum number of planes to produce a track");
0134   desc.addUntracked<int>("verbosity", 0)->setComment("verbosity for track producer");
0135   desc.add<double>("maximumChi2OverNDF", 5.)->setComment("maximum Chi2OverNDF for accepting the track");
0136   desc.add<double>("maximumXLocalDistanceFromTrack", 0.2)
0137       ->setComment("maximum x distance in mm to associate a point not used for fit to the track");
0138   desc.add<double>("maximumYLocalDistanceFromTrack", 0.3)
0139       ->setComment("maximum y distance in mm to associate a point not used for fit to the track");
0140   desc.add<int>("maxHitPerPlane", 20)
0141       ->setComment("maximum hits per plane, events with higher number will not be fitted");
0142   desc.add<int>("maxHitPerRomanPot", 60)
0143       ->setComment("maximum hits per roman pot, events with higher number will not be fitted");
0144   desc.add<int>("maxTrackPerRomanPot", 10)
0145       ->setComment("maximum tracks per roman pot, events with higher track will not be saved");
0146   desc.add<int>("maxTrackPerPattern", 5)
0147       ->setComment("maximum tracks per pattern, events with higher track will not be saved");
0148   desc.add<int>("numberOfPlanesPerPot", 6)->setComment("number of planes per pot");
0149   desc.add<double>("roadRadius", 1.0)->setComment("radius of pattern search window");
0150   desc.add<int>("minRoadSize", 3)->setComment("minimum number of points in a pattern");
0151   desc.add<int>("maxRoadSize", 20)->setComment("maximum number of points in a pattern");
0152   //parameter for 2-plane-pot reconstruction patch in run 3
0153   desc.add<double>("roadRadiusBadPot", 0.5)->setComment("radius of pattern search window for 2 plane pot");
0154 
0155   descriptions.add("ctppsPixelLocalTracks", desc);
0156 }
0157 
0158 //------------------------------------------------------------------------------------------------//
0159 
0160 void CTPPSPixelLocalTrackProducer::produce(edm::Event &iEvent, const edm::EventSetup &iSetup) {
0161   // Step A: get inputs
0162 
0163   edm::Handle<edm::DetSetVector<CTPPSPixelRecHit>> recHits;
0164   iEvent.getByToken(tokenCTPPSPixelRecHit_, recHits);
0165   edm::DetSetVector<CTPPSPixelRecHit> recHitVector(*recHits);
0166 
0167   // get geometry
0168   edm::ESHandle<CTPPSGeometry> geometryHandler = iSetup.getHandle(tokenCTPPSGeometry_);
0169   const CTPPSGeometry &geometry = *geometryHandler;
0170   geometryWatcher_.check(iSetup);
0171 
0172   // get mask
0173 
0174   // 2 planes pot flag vector 0=45-220, 1=45-210, 2=56-210, 3=56-220
0175   bool is2PlanePotV[4] = {false};
0176 
0177   if (!recHits->empty()) {
0178     const auto &mask = iSetup.getData(tokenCTPPSPixelAnalysisMask_);
0179 
0180     // Read Mask checking if 4 planes are masked and pot needs special treatment
0181     std::map<uint32_t, CTPPSPixelROCAnalysisMask> const &maschera = mask.analysisMask;
0182 
0183     // vector of masked rocs
0184     bool maskV[4][6][6] = {{{false}}};
0185 
0186     for (auto const &det : maschera) {
0187       CTPPSPixelDetId detId(det.first);
0188       unsigned int rocNum = (det.first & rocMask) >> rocOffset;
0189       if (rocNum > 5 || detId.plane() > 5)
0190         throw cms::Exception("InvalidRocOrPlaneNumber") << "roc number from mask: " << rocNum;
0191 
0192       if (detId.arm() == 0 && detId.rp() == 3) {
0193         if (detId.station() == 2) {                                                                // pot 45-220-far
0194           if (det.second.maskedPixels.size() == rocSizeInPixels || det.second.fullMask == true) {  // roc fully masked
0195             maskV[0][detId.plane()][rocNum] = true;
0196           }
0197         } else if (detId.station() == 0) {                                                         // pot 45-210-far
0198           if (det.second.maskedPixels.size() == rocSizeInPixels || det.second.fullMask == true) {  // roc fully masked
0199             maskV[1][detId.plane()][rocNum] = true;
0200           }
0201         }
0202       } else if (detId.arm() == 1 && detId.rp() == 3) {
0203         if (detId.station() == 0) {                                                                // pot 56-210-far
0204           if (det.second.maskedPixels.size() == rocSizeInPixels || det.second.fullMask == true) {  // roc fully masked
0205             maskV[2][detId.plane()][rocNum] = true;
0206           }
0207         } else if (detId.station() == 2) {                                                         // pot 56-220-far
0208           if (det.second.maskedPixels.size() == rocSizeInPixels || det.second.fullMask == true) {  // roc fully masked
0209             maskV[3][detId.plane()][rocNum] = true;
0210           }
0211         }
0212       }
0213     }
0214     // search for specific pattern that requires special reconstruction (use of two plane tracks)
0215 
0216     for (unsigned int i = 0; i < 4; i++) {
0217       unsigned int numberOfMaskedPlanes = 0;
0218       for (unsigned int j = 0; j < 6; j++) {
0219         if (maskV[i][j][0] && maskV[i][j][1] && maskV[i][j][4] && maskV[i][j][5])
0220           numberOfMaskedPlanes++;
0221       }
0222       if (numberOfMaskedPlanes == 4)
0223         is2PlanePotV[i] = true;  // search for exactly 4 planes fully masked
0224       edm::LogInfo("CTPPSPixelLocalTrackProducer") << " Masked planes in Pot #" << i << " = " << numberOfMaskedPlanes;
0225       if (is2PlanePotV[i])
0226         edm::LogInfo("CTPPSPixelLocalTrackProducer")
0227             << " Enabling 2 plane track reconstruction for pot #" << i << " (0=45-220, 1=45-210, 2=56-210, 3=56-220) ";
0228     }
0229   }
0230   std::vector<CTPPSPixelDetId> listOfPotWithHighOccupancyPlanes;
0231   std::map<CTPPSPixelDetId, uint32_t> mapHitPerPot;
0232 
0233   for (const auto &recHitSet : recHitVector) {
0234     if (verbosity_ > 2)
0235       edm::LogInfo("CTPPSPixelLocalTrackProducer")
0236           << "Hits found in plane = " << recHitSet.detId() << " number = " << recHitSet.size();
0237     CTPPSPixelDetId tmpRomanPotId = CTPPSPixelDetId(recHitSet.detId()).rpId();
0238     uint32_t hitOnPlane = recHitSet.size();
0239 
0240     //Get the number of hits per pot
0241     if (mapHitPerPot.find(tmpRomanPotId) == mapHitPerPot.end()) {
0242       mapHitPerPot[tmpRomanPotId] = hitOnPlane;
0243     } else
0244       mapHitPerPot[tmpRomanPotId] += hitOnPlane;
0245 
0246     //check is the plane occupancy is too high and save the corresponding pot
0247     if (maxHitPerPlane_ >= 0 && hitOnPlane > (uint32_t)maxHitPerPlane_) {
0248       if (verbosity_ > 2)
0249         edm::LogInfo("CTPPSPixelLocalTrackProducer")
0250             << " ---> Too many hits in the plane, pot will be excluded from tracking cleared";
0251       listOfPotWithHighOccupancyPlanes.push_back(tmpRomanPotId);
0252     }
0253   }
0254 
0255   //remove rechit for pot with too many hits or containing planes with too many hits
0256   for (const auto &recHitSet : recHitVector) {
0257     const auto tmpDetectorId = CTPPSPixelDetId(recHitSet.detId());
0258     const auto tmpRomanPotId = tmpDetectorId.rpId();
0259 
0260     if ((maxHitPerRomanPot_ >= 0 && mapHitPerPot[tmpRomanPotId] > (uint32_t)maxHitPerRomanPot_) ||
0261         find(listOfPotWithHighOccupancyPlanes.begin(), listOfPotWithHighOccupancyPlanes.end(), tmpRomanPotId) !=
0262             listOfPotWithHighOccupancyPlanes.end()) {
0263       edm::DetSet<CTPPSPixelRecHit> &tmpDetSet = recHitVector[tmpDetectorId];
0264       tmpDetSet.clear();
0265     }
0266   }
0267 
0268   edm::DetSetVector<CTPPSPixelLocalTrack> foundTracks;
0269 
0270   // Pattern finder
0271 
0272   patternFinder_->clear();
0273   patternFinder_->setHits(&recHitVector);
0274   patternFinder_->setGeometry(&geometry);
0275   patternFinder_->findPattern(is2PlanePotV);
0276   std::vector<RPixDetPatternFinder::Road> patternVector = patternFinder_->getPatterns();
0277 
0278   //loop on all the patterns
0279   int numberOfTracks = 0;
0280 
0281   for (const auto &pattern : patternVector) {
0282     CTPPSPixelDetId firstHitDetId = CTPPSPixelDetId(pattern.at(0).detId);
0283     CTPPSPixelDetId romanPotId = firstHitDetId.rpId();
0284 
0285     std::map<CTPPSPixelDetId, std::vector<RPixDetPatternFinder::PointInPlane>>
0286         hitOnPlaneMap;  //hit of the pattern organized by plane
0287 
0288     //loop on all the hits of the pattern
0289     for (const auto &hit : pattern) {
0290       CTPPSPixelDetId hitDetId = CTPPSPixelDetId(hit.detId);
0291       CTPPSPixelDetId tmpRomanPotId = hitDetId.rpId();
0292 
0293       if (tmpRomanPotId != romanPotId) {  //check that the hits belong to the same tracking station
0294         throw cms::Exception("CTPPSPixelLocalTrackProducer")
0295             << "Hits in the pattern must belong to the same tracking station";
0296       }
0297 
0298       if (hitOnPlaneMap.find(hitDetId) ==
0299           hitOnPlaneMap.end()) {  //add the plane key in case it is the first hit of that plane
0300         std::vector<RPixDetPatternFinder::PointInPlane> hitOnPlane;
0301         hitOnPlane.push_back(hit);
0302         hitOnPlaneMap[hitDetId] = hitOnPlane;
0303       } else
0304         hitOnPlaneMap[hitDetId].push_back(hit);  //add the hit to an existing plane key
0305     }
0306 
0307     trackFinder_->clear();
0308     trackFinder_->setRomanPotId(romanPotId);
0309     trackFinder_->setHits(&hitOnPlaneMap);
0310     trackFinder_->setGeometry(&geometry);
0311     trackFinder_->setZ0(geometry.rpTranslation(romanPotId).z());
0312     trackFinder_->findTracks(iEvent.getRun().id().run());
0313     std::vector<CTPPSPixelLocalTrack> tmpTracksVector = trackFinder_->getLocalTracks();
0314 
0315     if (verbosity_ > 2)
0316       edm::LogInfo("CTPPSPixelLocalTrackProducer") << "tmpTracksVector = " << tmpTracksVector.size();
0317     if (maxTrackPerPattern_ >= 0 && tmpTracksVector.size() > (uint32_t)maxTrackPerPattern_) {
0318       if (verbosity_ > 2)
0319         edm::LogInfo("CTPPSPixelLocalTrackProducer") << " ---> To many tracks in the pattern, cleared";
0320       continue;
0321     }
0322 
0323     for (const auto &track : tmpTracksVector) {
0324       ++numberOfTracks;
0325       edm::DetSet<CTPPSPixelLocalTrack> &tmpDetSet = foundTracks.find_or_insert(romanPotId);
0326       tmpDetSet.push_back(track);
0327     }
0328   }
0329 
0330   if (verbosity_ > 1)
0331     edm::LogInfo("CTPPSPixelLocalTrackProducer") << "Number of tracks will be saved = " << numberOfTracks;
0332 
0333   for (const auto &track : foundTracks) {
0334     if (verbosity_ > 1)
0335       edm::LogInfo("CTPPSPixelLocalTrackProducer")
0336           << "Track found in detId = " << track.detId() << " number = " << track.size();
0337     if (maxTrackPerRomanPot_ >= 0 && track.size() > (uint32_t)maxTrackPerRomanPot_) {
0338       if (verbosity_ > 1)
0339         edm::LogInfo("CTPPSPixelLocalTrackProducer") << " ---> Too many tracks in the pot, cleared";
0340       CTPPSPixelDetId tmpRomanPotId = CTPPSPixelDetId(track.detId());
0341       edm::DetSet<CTPPSPixelLocalTrack> &tmpDetSet = foundTracks[tmpRomanPotId];
0342       tmpDetSet.clear();
0343     }
0344   }
0345 
0346   iEvent.put(std::make_unique<edm::DetSetVector<CTPPSPixelLocalTrack>>(foundTracks));
0347 
0348   return;
0349 }
0350 
0351 DEFINE_FWK_MODULE(CTPPSPixelLocalTrackProducer);