Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:30:33

0001 // File: DataMixingSiStripRawWorker.cc
0002 // Description:  see DataMixingSiStripRawWorker.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 "DataMixingSiStripRawWorker.h"
0014 
0015 using namespace std;
0016 
0017 namespace edm {
0018 
0019   // Virtual constructor
0020 
0021   DataMixingSiStripRawWorker::DataMixingSiStripRawWorker() {}
0022 
0023   // Constructor
0024   DataMixingSiStripRawWorker::DataMixingSiStripRawWorker(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     Sistripdigi_collectionSig_ = ps.getParameter<edm::InputTag>("SistripdigiCollectionSig");
0035     SistripLabelSig_ = ps.getParameter<edm::InputTag>("SistripLabelSig");
0036 
0037     SiStripPileInputTag_ = ps.getParameter<edm::InputTag>("SiStripPileInputTag");
0038     SiStripRawInputTag_ = ps.getParameter<edm::InputTag>("SiStripRawInputTag");
0039 
0040     SiStripDigiCollectionDM_ = ps.getParameter<std::string>("SiStripDigiCollectionDM");
0041 
0042     SiStripRawDigiSource_ = ps.getParameter<std::string>("SiStripRawDigiSource");
0043 
0044     // clear local storage for this event
0045     SiHitStorage_.clear();
0046 
0047     edm::InputTag tag = edm::InputTag(Sistripdigi_collectionSig_.label(), SistripLabelSig_.label());
0048 
0049     SiStripInputTok_ = iC.consumes<edm::DetSetVector<SiStripDigi>>(tag);
0050     SiStripRawInputTok_ = iC.consumes<edm::DetSetVector<SiStripRawDigi>>(SiStripRawInputTag_);
0051   }
0052 
0053   // Virtual destructor needed.
0054   DataMixingSiStripRawWorker::~DataMixingSiStripRawWorker() {}
0055 
0056   void DataMixingSiStripRawWorker::addSiStripSignals(const edm::Event &e) {
0057     edm::Handle<edm::DetSetVector<SiStripDigi>> hSSD;
0058     edm::Handle<edm::DetSetVector<SiStripRawDigi>> hSSRD;
0059 
0060     if (SiStripRawDigiSource_ == "SIGNAL") {
0061       e.getByToken(SiStripRawInputTok_, hSSRD);
0062       rawdigicollection_ = hSSRD.product();
0063     } else if (SiStripRawDigiSource_ == "PILEUP") {
0064       e.getByToken(SiStripInputTok_, hSSD);
0065       digicollection_ = hSSD.product();
0066     }  // else {
0067        // std::cout << "you shouldn't be here" << std::endl;
0068        //}
0069 
0070   }  // end of addSiStripSignals
0071 
0072   void DataMixingSiStripRawWorker::addSiStripPileups(const int bcr,
0073                                                      const EventPrincipal *ep,
0074                                                      unsigned int eventNr,
0075                                                      ModuleCallingContext const *mcc) {
0076     LogDebug("DataMixingSiStripRawWorker")
0077         << "\n===============> adding pileups from event  " << ep->id() << " for bunchcrossing " << bcr;
0078 
0079     std::shared_ptr<Wrapper<edm::DetSetVector<SiStripDigi>> const> pSSD;
0080     std::shared_ptr<Wrapper<edm::DetSetVector<SiStripRawDigi>> const> pSSRD;
0081 
0082     if (SiStripRawDigiSource_ == "SIGNAL") {
0083       pSSD = getProductByTag<edm::DetSetVector<SiStripDigi>>(*ep, SiStripPileInputTag_, mcc);
0084       digicollection_ = const_cast<edm::DetSetVector<SiStripDigi> *>(pSSD->product());
0085     } else if (SiStripRawDigiSource_ == "PILEUP") {
0086       pSSRD = getProductByTag<edm::DetSetVector<SiStripRawDigi>>(*ep, SiStripRawInputTag_, mcc);
0087       rawdigicollection_ = const_cast<edm::DetSetVector<SiStripRawDigi> *>(pSSRD->product());
0088     } else {
0089       std::cout << "you shouldn't be here" << std::endl;
0090     }
0091 
0092   }  // end of addSiStripPileups
0093 
0094   void DataMixingSiStripRawWorker::putSiStrip(edm::Event &e) {
0095     //------------------
0096     //  (1) Fill a map from the Digi collection
0097     //
0098 
0099     // fill in maps of SiStripDigis
0100     OneDetectorMap LocalMap;
0101 
0102     // loop on all detsets (detectorIDs) inside the input collection
0103     edm::DetSetVector<SiStripDigi>::const_iterator DSViter = digicollection_->begin();
0104     for (; DSViter != digicollection_->end(); DSViter++) {
0105 #ifdef DEBUG
0106       LogDebug("DataMixingSiStripRawWorker") << "Processing DetID " << DSViter->id;
0107 #endif
0108 
0109       LocalMap.clear();
0110       LocalMap.reserve((DSViter->data).size());
0111       LocalMap.insert(LocalMap.end(), (DSViter->data).begin(), (DSViter->data).end());
0112 
0113       SiHitStorage_.insert(SiGlobalIndex::value_type(DSViter->id, LocalMap));
0114     }
0115 
0116     //------------------
0117     //  (2) Loop over the input RawDigi collection and add the Digis from the map
0118     //
0119 
0120     // collection of RawDigis to put back in the event
0121     std::vector<edm::DetSet<SiStripRawDigi>> vSiStripRawDigi;
0122 
0123     // loop on all detsets (detectorIDs) inside the SiStripRawDigis collection
0124     edm::DetSetVector<SiStripRawDigi>::const_iterator rawDSViter = rawdigicollection_->begin();
0125     for (; rawDSViter != rawdigicollection_->end(); rawDSViter++) {
0126       // Make empty collection with this detID
0127       edm::DetSet<SiStripRawDigi> SSRD(rawDSViter->id);
0128 
0129       // find local map (if it exists) for this detector ID
0130       SiGlobalIndex::const_iterator itest;
0131       itest = SiHitStorage_.find(rawDSViter->id);
0132 
0133       // if detID already has digis in existing map, add them to rawdigis
0134       if (itest != SiHitStorage_.end()) {
0135 #ifdef DEBUG
0136         LogDebug("DataMixingSiStripRawWorker") << "Pileups: Processing DetID " << rawDSViter->id;
0137 #endif
0138 
0139         // get the map from storage
0140         LocalMap = itest->second;
0141         OneDetectorMap::const_iterator iLocal = LocalMap.begin();
0142 
0143         // loop on all strips in rawdigi detset
0144         int currentstrip = 0;
0145         edm::DetSet<SiStripRawDigi>::const_iterator iRawDigi = rawDSViter->begin();
0146         while (iRawDigi != rawDSViter->end()) {
0147           int ADCSum = iRawDigi->adc();
0148 
0149           // if current strip exists in map, add ADC values
0150           if (iLocal->strip() == currentstrip) {
0151             ADCSum += iLocal->adc();
0152             iLocal++;
0153           }
0154 
0155           // put ADC sum in DetSet and go to next strip
0156           SSRD.push_back(SiStripRawDigi(ADCSum));
0157           iRawDigi++;
0158           currentstrip++;
0159         }
0160 
0161         // copy combined digi+rawdigi into rawdigi DetSetVector
0162         vSiStripRawDigi.push_back(SSRD);
0163 
0164         // otherwise, just copy the rawdigis from the background event to the
0165         // output
0166       } else {
0167         vSiStripRawDigi.push_back(*rawDSViter);
0168       }
0169     }
0170 
0171     //------------------
0172     //  (3) Put the new RawDigi collection back into the event
0173     //
0174 
0175     // make new raw digi collection
0176     std::unique_ptr<edm::DetSetVector<SiStripRawDigi>> MySiStripRawDigis(
0177         new edm::DetSetVector<SiStripRawDigi>(vSiStripRawDigi));
0178 
0179     // put collection
0180     e.put(std::move(MySiStripRawDigis), SiStripDigiCollectionDM_);
0181 
0182     // clear local storage for this event
0183     SiHitStorage_.clear();
0184   }
0185 
0186 }  // namespace edm