Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /** \file
0002  *
0003  *  \author M. Maggi -- INFN Bari
0004 */
0005 
0006 #include "RPCRecHitProducer.h"
0007 
0008 #include "Geometry/RPCGeometry/interface/RPCRoll.h"
0009 #include "DataFormats/MuonDetId/interface/RPCDetId.h"
0010 #include "DataFormats/RPCRecHit/interface/RPCRecHit.h"
0011 
0012 #include "RPCRecHitAlgoFactory.h"
0013 #include "DataFormats/RPCRecHit/interface/RPCRecHitCollection.h"
0014 
0015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0016 
0017 #include <string>
0018 #include <fstream>
0019 
0020 using namespace edm;
0021 using namespace std;
0022 
0023 RPCRecHitProducer::RPCRecHitProducer(const ParameterSet& config)
0024     : theRPCDigiLabel(consumes<RPCDigiCollection>(config.getParameter<InputTag>("rpcDigiLabel"))),
0025       theRPCGeomToken(esConsumes()),
0026       // Get the concrete reconstruction algo from the factory
0027       theAlgo{RPCRecHitAlgoFactory::get()->create(config.getParameter<string>("recAlgo"),
0028                                                   config.getParameter<ParameterSet>("recAlgoConfig"))},
0029       maskSource_(MaskSource::EventSetup),
0030       deadSource_(MaskSource::EventSetup) {
0031   // Set verbose output
0032   produces<RPCRecHitCollection>();
0033 
0034   // Get masked- and dead-strip information
0035   theRPCMaskedStripsObj = std::make_unique<RPCMaskedStrips>();
0036   theRPCDeadStripsObj = std::make_unique<RPCDeadStrips>();
0037 
0038   const string maskSource = config.getParameter<std::string>("maskSource");
0039   if (maskSource == "File") {
0040     maskSource_ = MaskSource::File;
0041     edm::FileInPath fp = config.getParameter<edm::FileInPath>("maskvecfile");
0042     std::ifstream inputFile(fp.fullPath().c_str(), std::ios::in);
0043     if (!inputFile) {
0044       std::cerr << "Masked Strips File cannot not be opened" << std::endl;
0045       exit(1);
0046     }
0047     while (inputFile.good()) {
0048       RPCMaskedStrips::MaskItem Item;
0049       inputFile >> Item.rawId >> Item.strip;
0050       if (inputFile.good())
0051         MaskVec.push_back(Item);
0052     }
0053     inputFile.close();
0054   } else {
0055     theReadoutMaskedStripsToken = esConsumes();
0056   }
0057 
0058   const string deadSource = config.getParameter<std::string>("deadSource");
0059   if (deadSource == "File") {
0060     deadSource_ = MaskSource::File;
0061     edm::FileInPath fp = config.getParameter<edm::FileInPath>("deadvecfile");
0062     std::ifstream inputFile(fp.fullPath().c_str(), std::ios::in);
0063     if (!inputFile) {
0064       std::cerr << "Dead Strips File cannot not be opened" << std::endl;
0065       exit(1);
0066     }
0067     while (inputFile.good()) {
0068       RPCDeadStrips::DeadItem Item;
0069       inputFile >> Item.rawId >> Item.strip;
0070       if (inputFile.good())
0071         DeadVec.push_back(Item);
0072     }
0073     inputFile.close();
0074   } else {
0075     theReadoutDeadStripsToken = esConsumes();
0076   }
0077 }
0078 
0079 void RPCRecHitProducer::beginRun(const edm::Run& r, const edm::EventSetup& setup) {
0080   // Getting the masked-strip information
0081   if (maskSource_ == MaskSource::EventSetup) {
0082     theRPCMaskedStripsObj->MaskVec = setup.getData(theReadoutMaskedStripsToken).MaskVec;
0083   } else if (maskSource_ == MaskSource::File) {
0084     std::vector<RPCMaskedStrips::MaskItem>::iterator posVec;
0085     for (posVec = MaskVec.begin(); posVec != MaskVec.end(); ++posVec) {
0086       RPCMaskedStrips::MaskItem Item;
0087       Item.rawId = (*posVec).rawId;
0088       Item.strip = (*posVec).strip;
0089       theRPCMaskedStripsObj->MaskVec.push_back(Item);
0090     }
0091   }
0092 
0093   // Getting the dead-strip information
0094   if (deadSource_ == MaskSource::EventSetup) {
0095     theRPCDeadStripsObj->DeadVec = setup.getData(theReadoutDeadStripsToken).DeadVec;
0096   } else if (deadSource_ == MaskSource::File) {
0097     std::vector<RPCDeadStrips::DeadItem>::iterator posVec;
0098     for (posVec = DeadVec.begin(); posVec != DeadVec.end(); ++posVec) {
0099       RPCDeadStrips::DeadItem Item;
0100       Item.rawId = (*posVec).rawId;
0101       Item.strip = (*posVec).strip;
0102       theRPCDeadStripsObj->DeadVec.push_back(Item);
0103     }
0104   }
0105 }
0106 
0107 void RPCRecHitProducer::produce(Event& event, const EventSetup& setup) {
0108   // Get the RPC Geometry
0109   auto const& rpcGeom = setup.getData(theRPCGeomToken);
0110 
0111   // Get the digis from the event
0112   Handle<RPCDigiCollection> digis;
0113   event.getByToken(theRPCDigiLabel, digis);
0114 
0115   // Pass the EventSetup to the algo
0116   theAlgo->setES(setup);
0117 
0118   // Create the pointer to the collection which will store the rechits
0119   auto recHitCollection = std::make_unique<RPCRecHitCollection>();
0120 
0121   // Iterate through all digi collections ordered by LayerId
0122 
0123   for (auto rpcdgIt = digis->begin(); rpcdgIt != digis->end(); ++rpcdgIt) {
0124     // The layerId
0125     const RPCDetId& rpcId = (*rpcdgIt).first;
0126 
0127     // Get the GeomDet from the setup
0128     const RPCRoll* roll = rpcGeom.roll(rpcId);
0129     if (roll == nullptr) {
0130       edm::LogError("BadDigiInput") << "Failed to find RPCRoll for ID " << rpcId;
0131       continue;
0132     }
0133 
0134     // Get the iterators over the digis associated with this LayerId
0135     const RPCDigiCollection::Range& range = (*rpcdgIt).second;
0136 
0137     // Getting the roll mask, that includes dead strips, for the given RPCDet
0138     RollMask mask;
0139     const int rawId = rpcId.rawId();
0140     for (const auto& tomask : theRPCMaskedStripsObj->MaskVec) {
0141       if (tomask.rawId == rawId) {
0142         const int bit = tomask.strip;
0143         mask.set(bit - 1);
0144       }
0145     }
0146 
0147     for (const auto& tomask : theRPCDeadStripsObj->DeadVec) {
0148       if (tomask.rawId == rawId) {
0149         const int bit = tomask.strip;
0150         mask.set(bit - 1);
0151       }
0152     }
0153 
0154     // Call the reconstruction algorithm
0155     OwnVector<RPCRecHit> recHits = theAlgo->reconstruct(*roll, rpcId, range, mask);
0156 
0157     if (!recHits.empty())  //FIXME: is it really needed?
0158       recHitCollection->put(rpcId, recHits.begin(), recHits.end());
0159   }
0160 
0161   event.put(std::move(recHitCollection));
0162 }