Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:29:59

0001 // File: DataMixingSiStripWorker.cc
0002 // Description:  see DataMixingSiStripWorker.h
0003 // Author:  Mike Hildreth, University of Notre Dame
0004 //
0005 //--------------------------------------------
0006 
0007 #include "DataFormats/Common/interface/Handle.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 #include <map>
0010 #include <memory>
0011 //
0012 //
0013 #include "DataMixingSiStripWorker.h"
0014 
0015 using namespace std;
0016 
0017 namespace edm {
0018 
0019   // Virtual constructor
0020 
0021   DataMixingSiStripWorker::DataMixingSiStripWorker() {}
0022 
0023   // Constructor
0024   DataMixingSiStripWorker::DataMixingSiStripWorker(const edm::ParameterSet &ps, edm::ConsumesCollector &&iC)
0025       : label_(ps.getParameter<std::string>("Label"))
0026 
0027   {
0028     // get the subdetector names
0029     //    this->getSubdetectorNames();  //something like this may be useful to
0030     //    check what we are supposed to do...
0031 
0032     // declare the products to produce
0033 
0034     SistripLabelSig_ = ps.getParameter<edm::InputTag>("SistripLabelSig");
0035     SiStripPileInputTag_ = ps.getParameter<edm::InputTag>("SiStripPileInputTag");
0036 
0037     SiStripDigiCollectionDM_ = ps.getParameter<std::string>("SiStripDigiCollectionDM");
0038 
0039     SiStripDigiToken_ = iC.consumes<edm::DetSetVector<SiStripDigi>>(SistripLabelSig_);
0040     SiStripDigiPToken_ = iC.consumes<edm::DetSetVector<SiStripDigi>>(SiStripPileInputTag_);
0041 
0042     // clear local storage for this event
0043     SiHitStorage_.clear();
0044   }
0045 
0046   // Virtual destructor needed.
0047   DataMixingSiStripWorker::~DataMixingSiStripWorker() {}
0048 
0049   void DataMixingSiStripWorker::addSiStripSignals(const edm::Event &e) {
0050     // fill in maps of hits
0051 
0052     Handle<edm::DetSetVector<SiStripDigi>> input;
0053 
0054     if (e.getByToken(SiStripDigiToken_, input)) {
0055       OneDetectorMap LocalMap;
0056 
0057       // loop on all detsets (detectorIDs) inside the input collection
0058       edm::DetSetVector<SiStripDigi>::const_iterator DSViter = input->begin();
0059       for (; DSViter != input->end(); DSViter++) {
0060 #ifdef DEBUG
0061         LogDebug("DataMixingSiStripWorker") << "Processing DetID " << DSViter->id;
0062 #endif
0063 
0064         LocalMap.clear();
0065         LocalMap.reserve((DSViter->data).size());
0066         LocalMap.insert(LocalMap.end(), (DSViter->data).begin(), (DSViter->data).end());
0067 
0068         SiHitStorage_.insert(SiGlobalIndex::value_type(DSViter->id, LocalMap));
0069       }
0070     }
0071   }  // end of addSiStripSignals
0072 
0073   void DataMixingSiStripWorker::addSiStripPileups(const int bcr,
0074                                                   const EventPrincipal *ep,
0075                                                   unsigned int eventNr,
0076                                                   ModuleCallingContext const *mcc) {
0077     LogDebug("DataMixingSiStripWorker") << "\n===============> adding pileups from event  " << ep->id()
0078                                         << " for bunchcrossing " << bcr;
0079 
0080     // fill in maps of hits; same code as addSignals, except now applied to the
0081     // pileup events
0082 
0083     std::shared_ptr<Wrapper<edm::DetSetVector<SiStripDigi>> const> inputPTR =
0084         getProductByTag<edm::DetSetVector<SiStripDigi>>(*ep, SiStripPileInputTag_, mcc);
0085 
0086     if (inputPTR) {
0087       const edm::DetSetVector<SiStripDigi> *input = const_cast<edm::DetSetVector<SiStripDigi> *>(inputPTR->product());
0088 
0089       // Handle< edm::DetSetVector<SiStripDigi> >  input;
0090 
0091       // if(
0092       // e->getByLabel(Sistripdigi_collectionPile_.label(),SistripLabelPile_.label(),input)
0093       // ) {
0094 
0095       OneDetectorMap LocalMap;
0096 
0097       // loop on all detsets (detectorIDs) inside the input collection
0098       edm::DetSetVector<SiStripDigi>::const_iterator DSViter = input->begin();
0099       for (; DSViter != input->end(); DSViter++) {
0100 #ifdef DEBUG
0101         LogDebug("DataMixingSiStripWorker") << "Pileups: Processing DetID " << DSViter->id;
0102 #endif
0103 
0104         // find correct local map (or new one) for this detector ID
0105 
0106         SiGlobalIndex::const_iterator itest;
0107 
0108         itest = SiHitStorage_.find(DSViter->id);
0109 
0110         if (itest != SiHitStorage_.end()) {  // this detID already has hits, add to existing map
0111 
0112           LocalMap = itest->second;
0113 
0114           // fill in local map with extra channels
0115           LocalMap.insert(LocalMap.end(), (DSViter->data).begin(), (DSViter->data).end());
0116           std::stable_sort(LocalMap.begin(), LocalMap.end(), DataMixingSiStripWorker::StrictWeakOrdering());
0117           SiHitStorage_[DSViter->id] = LocalMap;
0118 
0119         } else {  // fill local storage with this information, put in global
0120                   // collection
0121 
0122           LocalMap.clear();
0123           LocalMap.reserve((DSViter->data).size());
0124           LocalMap.insert(LocalMap.end(), (DSViter->data).begin(), (DSViter->data).end());
0125 
0126           SiHitStorage_.insert(SiGlobalIndex::value_type(DSViter->id, LocalMap));
0127         }
0128       }
0129     }
0130   }
0131 
0132   void DataMixingSiStripWorker::putSiStrip(edm::Event &e) {
0133     // collection of Digis to put in the event
0134     std::vector<edm::DetSet<SiStripDigi>> vSiStripDigi;
0135 
0136     // loop through our collection of detectors, merging hits and putting new ones
0137     // in the output
0138 
0139     // big loop over Detector IDs:
0140 
0141     for (SiGlobalIndex::const_iterator IDet = SiHitStorage_.begin(); IDet != SiHitStorage_.end(); IDet++) {
0142       edm::DetSet<SiStripDigi> SSD(IDet->first);  // Make empty collection with this detector ID
0143 
0144       OneDetectorMap LocalMap = IDet->second;
0145 
0146       // counter variables
0147       int formerStrip = -1;
0148       int currentStrip;
0149       int ADCSum = 0;
0150 
0151       OneDetectorMap::const_iterator iLocalchk;
0152       OneDetectorMap::const_iterator iLocal = LocalMap.begin();
0153       for (; iLocal != LocalMap.end(); ++iLocal) {
0154         currentStrip = iLocal->strip();
0155 
0156         if (currentStrip == formerStrip) {  // we have to add these digis together
0157           ADCSum += iLocal->adc();          // on every element...
0158         } else {
0159           if (formerStrip != -1) {
0160             if (ADCSum > 511)
0161               ADCSum = 255;
0162             else if (ADCSum > 253 && ADCSum < 512)
0163               ADCSum = 254;
0164             SiStripDigi aHit(formerStrip, ADCSum);
0165             SSD.push_back(aHit);
0166           }
0167           // save pointers for next iteration
0168           formerStrip = currentStrip;
0169           ADCSum = iLocal->adc();
0170         }
0171 
0172         iLocalchk = iLocal;
0173         if ((++iLocalchk) == LocalMap.end()) {  // make sure not to lose the last one
0174           if (ADCSum > 511)
0175             ADCSum = 255;
0176           else if (ADCSum > 253 && ADCSum < 512)
0177             ADCSum = 254;
0178           SSD.push_back(SiStripDigi(formerStrip, ADCSum));
0179         }  // end of loop over one detector
0180       }
0181       // stick this into the global vector of detector info
0182       vSiStripDigi.push_back(SSD);
0183 
0184     }  // end of big loop over all detector IDs
0185 
0186     // put the collection of digis in the event
0187     LogInfo("DataMixingSiStripWorker") << "total # Merged strips: " << vSiStripDigi.size();
0188 
0189     // make new digi collection
0190 
0191     std::unique_ptr<edm::DetSetVector<SiStripDigi>> MySiStripDigis(new edm::DetSetVector<SiStripDigi>(vSiStripDigi));
0192 
0193     // put collection
0194 
0195     e.put(std::move(MySiStripDigis), SiStripDigiCollectionDM_);
0196 
0197     // clear local storage for this event
0198     SiHitStorage_.clear();
0199   }
0200 
0201 }  // namespace edm