Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:11:04

0001 // system includes
0002 #include <vector>
0003 
0004 // user includes
0005 #include "CalibFormats/SiStripObjects/interface/SiStripDetCabling.h"
0006 #include "CalibTracker/Records/interface/SiStripDetCablingRcd.h"
0007 #include "DataFormats/Common/interface/DetSetVector.h"
0008 #include "DataFormats/Common/interface/Handle.h"
0009 #include "DataFormats/SiStripDigi/interface/SiStripDigi.h"
0010 #include "FWCore/Framework/interface/ESHandle.h"
0011 #include "FWCore/Framework/interface/Event.h"
0012 #include "FWCore/Framework/interface/EventSetup.h"
0013 #include "FWCore/Framework/interface/Run.h"
0014 #include "FWCore/Framework/interface/stream/EDProducer.h"
0015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0016 
0017 // ROOT includes
0018 #include "TRandom.h"
0019 
0020 /**
0021     @file EventFilter/SiStripRawToDigi/test/plugins/SiStripTrivialClusterSource.cc
0022     @class SiStripTrivialClusterSource
0023     @brief Creates a DetSetVector of SiStripDigis created using random
0024     number generators and attaches the collection to the Event. Allows
0025     to test the final DigiToRaw and RawToDigi/RawToCluster converters.  
0026  */
0027 
0028 class SiStripTrivialClusterSource : public edm::stream::EDProducer<> {
0029 public:
0030   SiStripTrivialClusterSource(const edm::ParameterSet&);
0031   ~SiStripTrivialClusterSource() override;
0032 
0033   void beginRun(const edm::Run&, const edm::EventSetup&) override;
0034   void produce(edm::Event&, const edm::EventSetup&) override;
0035 
0036 private:
0037   /** Check for space in module */
0038   bool available(const edm::DetSet<SiStripDigi>&, const uint16_t, const uint32_t);
0039 
0040   /** Add cluster to module */
0041   void addcluster(edm::DetSet<SiStripDigi>&, const uint16_t, const uint16_t);
0042 
0043   /** token */
0044   const edm::ESGetToken<SiStripDetCabling, SiStripDetCablingRcd> esTokenCabling_;
0045 
0046   /** Configurables */
0047   double minocc_;
0048   double maxocc_;
0049   double mincluster_;
0050   double maxcluster_;
0051   uint16_t separation_;
0052 
0053   /** Setup */
0054   edm::ESHandle<SiStripDetCabling> cabling_;
0055   std::vector<uint32_t> detids_;
0056   uint32_t nstrips_;
0057 
0058   /** Random */
0059   TRandom random_;
0060 };
0061 
0062 SiStripTrivialClusterSource::SiStripTrivialClusterSource(const edm::ParameterSet& pset)
0063     : esTokenCabling_(esConsumes<edm::Transition::BeginRun>()),
0064       minocc_(pset.getUntrackedParameter<double>("MinOccupancy", 0.001)),
0065       maxocc_(pset.getUntrackedParameter<double>("MaxOccupancy", 0.03)),
0066       mincluster_(pset.getUntrackedParameter<unsigned int>("MinCluster", 4)),
0067       maxcluster_(pset.getUntrackedParameter<unsigned int>("MaxCluster", 4)),
0068       separation_(pset.getUntrackedParameter<unsigned int>("Separation", 2)),
0069       cabling_(),
0070       detids_(),
0071       nstrips_(0),
0072       random_() {
0073   produces<edm::DetSetVector<SiStripDigi>>();
0074 }
0075 
0076 SiStripTrivialClusterSource::~SiStripTrivialClusterSource() = default;
0077 
0078 void SiStripTrivialClusterSource::beginRun(const edm::Run&, const edm::EventSetup& setup) {
0079   cabling_ = setup.getHandle(esTokenCabling_);
0080   cabling_->addAllDetectorsRawIds(detids_);
0081   for (unsigned int i = 0; i < detids_.size(); i++) {
0082     nstrips_ += cabling_->getConnections(detids_[i]).size() * 256;
0083   }
0084 }
0085 
0086 void SiStripTrivialClusterSource::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0087   auto clusters = std::make_unique<edm::DetSetVector<SiStripDigi>>();
0088 
0089   double occupancy = random_.Uniform(minocc_, maxocc_);
0090   double indexdigis = nstrips_ * occupancy;
0091   double indexcluster = random_.Uniform(mincluster_, maxcluster_);
0092   uint32_t ndigis = (indexdigis > 0.) ? static_cast<uint32_t>(indexdigis) : 0;
0093   uint16_t clustersize = (indexcluster > 0.) ? static_cast<uint16_t>(indexcluster) : 0;
0094 
0095   uint32_t counter = 0;
0096   while (counter < 10000) {
0097     if (clustersize && ndigis >= clustersize)
0098       ndigis -= clustersize;
0099     else
0100       break;
0101 
0102     while (counter < 10000) {
0103       double indexdet = random_.Uniform(0., detids_.size());
0104       uint32_t detid = detids_[static_cast<uint32_t>(indexdet)];
0105       uint32_t maxstrip = 256 * cabling_->getConnections(detid).size();
0106       double indexstrip = random_.Uniform(0., maxstrip - clustersize);
0107       uint16_t strip = static_cast<uint16_t>(indexstrip);
0108 
0109       edm::DetSet<SiStripDigi>& detset = clusters->find_or_insert(detid);
0110       detset.data.reserve(768);
0111 
0112       if (available(detset, strip, clustersize)) {
0113         addcluster(detset, strip, clustersize);
0114         counter = 0;
0115         break;
0116       }
0117 
0118       counter++;
0119     }
0120   }
0121 
0122   iEvent.put(std::move(clusters));
0123 }
0124 
0125 bool SiStripTrivialClusterSource::available(const edm::DetSet<SiStripDigi>& detset,
0126                                             const uint16_t firststrip,
0127                                             const uint32_t size) {
0128   for (edm::DetSet<SiStripDigi>::const_iterator idigi = detset.data.begin(); idigi != detset.data.end(); idigi++) {
0129     if (idigi->strip() >= (firststrip - separation_) &&
0130         idigi->strip() < static_cast<int>(firststrip + size + separation_) && idigi->adc()) {
0131       return false;
0132     }
0133   }
0134   return true;
0135 }
0136 
0137 void SiStripTrivialClusterSource::addcluster(edm::DetSet<SiStripDigi>& detset,
0138                                              const uint16_t firststrip,
0139                                              const uint16_t size) {
0140   for (unsigned int istrip = 0; istrip < size; ++istrip) {
0141     detset.data.push_back(SiStripDigi(firststrip + istrip, 0xFF));
0142   }
0143 }
0144 
0145 #include "FWCore/PluginManager/interface/ModuleDef.h"
0146 #include "FWCore/Framework/interface/MakerMacros.h"
0147 DEFINE_FWK_MODULE(SiStripTrivialClusterSource);