Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-06 07:38:49

0001 /** \file
0002  *
0003  *  \author M. Maggi -- INFN Bari
0004  */
0005 #include "RecoLocalMuon/GEMRecHit/plugins/GEMRecHitProducer.h"
0006 
0007 #include "Geometry/GEMGeometry/interface/GEMEtaPartition.h"
0008 #include "DataFormats/MuonDetId/interface/GEMDetId.h"
0009 #include "DataFormats/GEMRecHit/interface/GEMRecHit.h"
0010 
0011 #include "RecoLocalMuon/GEMRecHit/interface/GEMRecHitAlgoFactory.h"
0012 #include "DataFormats/GEMRecHit/interface/GEMRecHitCollection.h"
0013 
0014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0015 
0016 #include <string>
0017 #include <fstream>
0018 
0019 using namespace edm;
0020 using namespace std;
0021 
0022 GEMRecHitProducer::GEMRecHitProducer(const ParameterSet& config)
0023     : theGEMDigiToken(consumes<GEMDigiCollection>(config.getParameter<InputTag>("gemDigiLabel"))),
0024       // Get the concrete reconstruction algo from the factory
0025       theAlgo{GEMRecHitAlgoFactory::get()->create(config.getParameter<string>("recAlgo"),
0026                                                   config.getParameter<ParameterSet>("recAlgoConfig"))},
0027       maskSource_(MaskSource::EventSetup),
0028       deadSource_(MaskSource::EventSetup),
0029       gemGeomToken_(esConsumes<GEMGeometry, MuonGeometryRecord, edm::Transition::BeginRun>()) {
0030   produces<GEMRecHitCollection>();
0031 
0032   // Turns off GE2/1 demonstrator reconstruction in Run3
0033   ge21Off_ = config.getParameter<bool>("ge21Off");
0034   // Get masked- and dead-strip information from file
0035   applyMasking_ = config.getParameter<bool>("applyMasking");
0036   if (applyMasking_) {
0037     if (config.existsAs<edm::FileInPath>("maskFile")) {
0038       maskSource_ = MaskSource::File;
0039       std::ifstream inputFile(config.getParameter<edm::FileInPath>("maskFile").fullPath());
0040       if (!inputFile) {
0041         throw cms::Exception("GEMRecHitProducer") << "Masked Strips File cannot not be opened";
0042       }
0043       theGEMMaskedStripsObj = std::make_unique<GEMMaskedStrips>();
0044       while (inputFile.good()) {
0045         GEMMaskedStrips::MaskItem Item;
0046         inputFile >> Item.rawId >> Item.strip;
0047         if (inputFile.good())
0048           theGEMMaskedStripsObj->fillMaskVec(Item);
0049       }
0050       inputFile.close();
0051     }
0052 
0053     if (config.existsAs<edm::FileInPath>("deadFile")) {
0054       deadSource_ = MaskSource::File;
0055       std::ifstream inputFile(config.getParameter<edm::FileInPath>("deadFile").fullPath());
0056       if (!inputFile) {
0057         throw cms::Exception("GEMRecHitProducer") << "Dead Strips File cannot not be opened";
0058       }
0059       theGEMDeadStripsObj = std::make_unique<GEMDeadStrips>();
0060       while (inputFile.good()) {
0061         GEMDeadStrips::DeadItem Item;
0062         inputFile >> Item.rawId >> Item.strip;
0063         if (inputFile.good())
0064           theGEMDeadStripsObj->fillDeadVec(Item);
0065       }
0066       inputFile.close();
0067     }
0068     if (maskSource_ == MaskSource::EventSetup) {
0069       maskedStripsToken_ = esConsumes<GEMMaskedStrips, GEMMaskedStripsRcd, edm::Transition::BeginRun>();
0070     }
0071     if (deadSource_ == MaskSource::EventSetup) {
0072       deadStripsToken_ = esConsumes<GEMDeadStrips, GEMDeadStripsRcd, edm::Transition::BeginRun>();
0073     }
0074   }
0075 }
0076 
0077 GEMRecHitProducer::~GEMRecHitProducer() = default;
0078 
0079 void GEMRecHitProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0080   edm::ParameterSetDescription desc;
0081   edm::ParameterSetDescription recAlgoConfigDesc;
0082   desc.add<edm::ParameterSetDescription>("recAlgoConfig", recAlgoConfigDesc);
0083   desc.add<std::string>("recAlgo", "GEMRecHitStandardAlgo");
0084   desc.add<edm::InputTag>("gemDigiLabel", edm::InputTag("muonGEMDigis"));
0085   desc.add<bool>("applyMasking", false);
0086   desc.add<bool>("ge21Off", false);
0087   desc.addOptional<edm::FileInPath>("maskFile");
0088   desc.addOptional<edm::FileInPath>("deadFile");
0089   descriptions.add("gemRecHitsDef", desc);
0090 }
0091 
0092 void GEMRecHitProducer::beginRun(const edm::Run& r, const edm::EventSetup& setup) {
0093   // Get the GEM Geometry
0094   gemGeom_ = setup.getHandle(gemGeomToken_);
0095 
0096   if (applyMasking_) {
0097     // Getting the masked-strip information
0098     if (maskSource_ == MaskSource::EventSetup) {
0099       edm::ESHandle<GEMMaskedStrips> readoutMaskedStrips = setup.getHandle(maskedStripsToken_);
0100       theGEMMaskedStripsObj = std::make_unique<GEMMaskedStrips>(*readoutMaskedStrips.product());
0101     }
0102     // Getting the dead-strip information
0103     if (deadSource_ == MaskSource::EventSetup) {
0104       edm::ESHandle<GEMDeadStrips> readoutDeadStrips = setup.getHandle(deadStripsToken_);
0105       theGEMDeadStripsObj = std::make_unique<GEMDeadStrips>(*readoutDeadStrips.product());
0106     }
0107 
0108     for (auto gems : gemGeom_->etaPartitions()) {
0109       // Getting the EtaPartitionMask mask, that includes dead strips, for the given GEMDet
0110       GEMDetId gemId = gems->id();
0111       if (ge21Off_ && gemId.station() == 2) {
0112         continue;
0113       }
0114       EtaPartitionMask mask;
0115       const int rawId = gemId.rawId();
0116       for (const auto& tomask : theGEMMaskedStripsObj->getMaskVec()) {
0117         if (tomask.rawId == rawId) {
0118           const int bit = tomask.strip;
0119           mask.set(bit);
0120         }
0121       }
0122       for (const auto& tomask : theGEMDeadStripsObj->getDeadVec()) {
0123         if (tomask.rawId == rawId) {
0124           const int bit = tomask.strip;
0125           mask.set(bit);
0126         }
0127       }
0128       // add to masking map if masking present in etaPartition
0129       if (mask.any()) {
0130         gemMask_.emplace(gemId, mask);
0131       }
0132     }
0133   }
0134 }
0135 
0136 void GEMRecHitProducer::produce(Event& event, const EventSetup& setup) {
0137   // Get the digis from the event
0138   Handle<GEMDigiCollection> digis;
0139   event.getByToken(theGEMDigiToken, digis);
0140 
0141   // Pass the EventSetup to the algo
0142   theAlgo->setES(setup);
0143 
0144   // Create the pointer to the collection which will store the rechits
0145   auto recHitCollection = std::make_unique<GEMRecHitCollection>();
0146 
0147   // Iterate through all digi collections ordered by LayerId
0148   for (auto gemdgIt = digis->begin(); gemdgIt != digis->end(); ++gemdgIt) {
0149     // The layerId
0150     const GEMDetId& gemId = (*gemdgIt).first;
0151     if (ge21Off_ && gemId.station() == 2) {
0152       continue;
0153     }
0154 
0155     // Get the GeomDet from the setup
0156     const GEMEtaPartition* roll = gemGeom_->etaPartition(gemId);
0157     if (roll == nullptr) {
0158       edm::LogError("BadDigiInput") << "Failed to find GEMEtaPartition for ID " << gemId;
0159       continue;
0160     }
0161 
0162     // Get the iterators over the digis associated with this LayerId
0163     const GEMDigiCollection::Range& range = (*gemdgIt).second;
0164 
0165     // get mask from map
0166     EtaPartitionMask mask;
0167     if (applyMasking_) {
0168       auto gemmaskIt = gemMask_.find(gemId);
0169       if (gemmaskIt != gemMask_.end())
0170         mask = gemmaskIt->second;
0171     }
0172 
0173     // Call the reconstruction algorithm
0174     OwnVector<GEMRecHit> recHits = theAlgo->reconstruct(*roll, gemId, range, mask);
0175 
0176     if (!recHits.empty())  //FIXME: is it really needed?
0177       recHitCollection->put(gemId, recHits.begin(), recHits.end());
0178   }
0179 
0180   event.put(std::move(recHitCollection));
0181 }