Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 // File: DataMixingEMWorker.cc
0002 // Description:  see DataMixingEMWorker.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 "DataMixingEMWorker.h"
0014 
0015 using namespace std;
0016 
0017 namespace edm {
0018 
0019   // Virtual constructor
0020 
0021   DataMixingEMWorker::DataMixingEMWorker() {}
0022 
0023   // Constructor
0024   DataMixingEMWorker::DataMixingEMWorker(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, retrieve
0033 
0034     EBProducerSig_ = ps.getParameter<edm::InputTag>("EBProducerSig");
0035     EEProducerSig_ = ps.getParameter<edm::InputTag>("EEProducerSig");
0036     ESProducerSig_ = ps.getParameter<edm::InputTag>("ESProducerSig");
0037 
0038     EBRecHitToken_ = iC.consumes<EBRecHitCollection>(EBProducerSig_);
0039     EERecHitToken_ = iC.consumes<EERecHitCollection>(EEProducerSig_);
0040     ESRecHitToken_ = iC.consumes<ESRecHitCollection>(ESProducerSig_);
0041 
0042     EBrechitCollectionSig_ = ps.getParameter<edm::InputTag>("EBrechitCollectionSig");
0043     EErechitCollectionSig_ = ps.getParameter<edm::InputTag>("EErechitCollectionSig");
0044     ESrechitCollectionSig_ = ps.getParameter<edm::InputTag>("ESrechitCollectionSig");
0045 
0046     EBPileRecHitInputTag_ = ps.getParameter<edm::InputTag>("EBPileRecHitInputTag");
0047     EEPileRecHitInputTag_ = ps.getParameter<edm::InputTag>("EEPileRecHitInputTag");
0048     ESPileRecHitInputTag_ = ps.getParameter<edm::InputTag>("ESPileRecHitInputTag");
0049 
0050     EBPileRecHitToken_ = iC.consumes<EBRecHitCollection>(EBPileRecHitInputTag_);
0051     EEPileRecHitToken_ = iC.consumes<EERecHitCollection>(EEPileRecHitInputTag_);
0052     ESPileRecHitToken_ = iC.consumes<ESRecHitCollection>(ESPileRecHitInputTag_);
0053 
0054     EBRecHitCollectionDM_ = ps.getParameter<std::string>("EBRecHitCollectionDM");
0055     EERecHitCollectionDM_ = ps.getParameter<std::string>("EERecHitCollectionDM");
0056     ESRecHitCollectionDM_ = ps.getParameter<std::string>("ESRecHitCollectionDM");
0057   }
0058 
0059   // Virtual destructor needed.
0060   DataMixingEMWorker::~DataMixingEMWorker() {}
0061 
0062   void DataMixingEMWorker::addEMSignals(const edm::Event &e) {
0063     // fill in maps of hits
0064 
0065     LogInfo("DataMixingEMWorker") << "===============> adding MC signals for " << e.id();
0066 
0067     // EB first
0068 
0069     Handle<EBRecHitCollection> pEBRecHits;
0070 
0071     const EBRecHitCollection *EBRecHits = nullptr;
0072 
0073     if (e.getByToken(EBRecHitToken_, pEBRecHits)) {
0074       EBRecHits = pEBRecHits.product();  // get a ptr to the product
0075       LogDebug("DataMixingEMWorker") << "total # EB rechits: " << EBRecHits->size();
0076     }
0077 
0078     if (EBRecHits) {
0079       // loop over rechits, storing them in a map so we can add pileup later
0080       for (EBRecHitCollection::const_iterator it = EBRecHits->begin(); it != EBRecHits->end(); ++it) {
0081         EBRecHitStorage_.insert(EBRecHitMap::value_type((it->id()), *it));
0082 
0083         LogDebug("DataMixingEMWorker") << "processed EBRecHit with rawId: " << it->id().rawId() << "\n"
0084                                        << " rechit energy: " << it->energy();
0085       }
0086     }
0087 
0088     // EE next
0089 
0090     Handle<EERecHitCollection> pEERecHits;
0091 
0092     const EERecHitCollection *EERecHits = nullptr;
0093 
0094     if (e.getByToken(EERecHitToken_, pEERecHits)) {
0095       EERecHits = pEERecHits.product();  // get a ptr to the product
0096       LogDebug("DataMixingEMWorker") << "total # EE rechits: " << EERecHits->size();
0097     }
0098 
0099     if (EERecHits) {
0100       // loop over rechits, storing them in a map so we can add pileup later
0101       for (EERecHitCollection::const_iterator it = EERecHits->begin(); it != EERecHits->end(); ++it) {
0102         EERecHitStorage_.insert(EERecHitMap::value_type((it->id()), *it));
0103 #ifdef DEBUG
0104         LogDebug("DataMixingEMWorker") << "processed EERecHit with rawId: " << it->id().rawId() << "\n"
0105                                        << " rechit energy: " << it->energy();
0106 #endif
0107       }
0108     }
0109     // ES next
0110 
0111     Handle<ESRecHitCollection> pESRecHits;
0112 
0113     const ESRecHitCollection *ESRecHits = nullptr;
0114 
0115     if (e.getByToken(ESRecHitToken_, pESRecHits)) {
0116       ESRecHits = pESRecHits.product();  // get a ptr to the product
0117 #ifdef DEBUG
0118       LogDebug("DataMixingEMWorker") << "total # ES rechits: " << ESRecHits->size();
0119 #endif
0120     }
0121 
0122     if (ESRecHits) {
0123       // loop over rechits, storing them in a map so we can add pileup later
0124       for (ESRecHitCollection::const_iterator it = ESRecHits->begin(); it != ESRecHits->end(); ++it) {
0125         ESRecHitStorage_.insert(ESRecHitMap::value_type((it->id()), *it));
0126 
0127 #ifdef DEBUG
0128         LogDebug("DataMixingEMWorker") << "processed ESRecHit with rawId: " << it->id().rawId() << "\n"
0129                                        << " rechit energy: " << it->energy();
0130 #endif
0131       }
0132     }
0133 
0134   }  // end of addEMSignals
0135 
0136   void DataMixingEMWorker::addEMPileups(const int bcr,
0137                                         const EventPrincipal *ep,
0138                                         unsigned int eventNr,
0139                                         ModuleCallingContext const *mcc) {
0140     LogInfo("DataMixingEMWorker") << "\n===============> adding pileups from event  " << ep->id()
0141                                   << " for bunchcrossing " << bcr;
0142 
0143     // fill in maps of hits; same code as addSignals, except now applied to the
0144     // pileup events
0145 
0146     // EB first
0147 
0148     std::shared_ptr<Wrapper<EBRecHitCollection> const> EBRecHitsPTR =
0149         getProductByTag<EBRecHitCollection>(*ep, EBPileRecHitInputTag_, mcc);
0150 
0151     if (EBRecHitsPTR) {
0152       const EBRecHitCollection *EBRecHits = const_cast<EBRecHitCollection *>(EBRecHitsPTR->product());
0153 
0154       LogDebug("DataMixingEMWorker") << "total # EB rechits: " << EBRecHits->size();
0155 
0156       // loop over digis, adding these to the existing maps
0157       for (EBRecHitCollection::const_iterator it = EBRecHits->begin(); it != EBRecHits->end(); ++it) {
0158         EBRecHitStorage_.insert(EBRecHitMap::value_type((it->id()), *it));
0159 
0160 #ifdef DEBUG
0161         LogDebug("DataMixingEMWorker") << "processed EBRecHit with rawId: " << it->id().rawId() << "\n"
0162                                        << " rechit energy: " << it->energy();
0163 #endif
0164       }
0165     }
0166 
0167     // EE Next
0168 
0169     std::shared_ptr<Wrapper<EERecHitCollection> const> EERecHitsPTR =
0170         getProductByTag<EERecHitCollection>(*ep, EEPileRecHitInputTag_, mcc);
0171 
0172     if (EERecHitsPTR) {
0173       const EERecHitCollection *EERecHits = const_cast<EERecHitCollection *>(EERecHitsPTR->product());
0174 
0175       LogDebug("DataMixingEMWorker") << "total # EE rechits: " << EERecHits->size();
0176 
0177       // loop over digis, adding these to the existing maps
0178       for (EERecHitCollection::const_iterator it = EERecHits->begin(); it != EERecHits->end(); ++it) {
0179         EERecHitStorage_.insert(EERecHitMap::value_type((it->id()), *it));
0180 
0181 #ifdef DEBUG
0182         LogDebug("DataMixingEMWorker") << "processed EERecHit with rawId: " << it->id().rawId() << "\n"
0183                                        << " rechit energy: " << it->energy();
0184 #endif
0185       }
0186     }
0187 
0188     // ES Next
0189 
0190     std::shared_ptr<Wrapper<ESRecHitCollection> const> ESRecHitsPTR =
0191         getProductByTag<ESRecHitCollection>(*ep, ESPileRecHitInputTag_, mcc);
0192 
0193     if (ESRecHitsPTR) {
0194       const ESRecHitCollection *ESRecHits = const_cast<ESRecHitCollection *>(ESRecHitsPTR->product());
0195 
0196       LogDebug("DataMixingEMWorker") << "total # ES rechits: " << ESRecHits->size();
0197 
0198       // loop over digis, adding these to the existing maps
0199       for (ESRecHitCollection::const_iterator it = ESRecHits->begin(); it != ESRecHits->end(); ++it) {
0200         ESRecHitStorage_.insert(ESRecHitMap::value_type((it->id()), *it));
0201 
0202 #ifdef DEBUG
0203         LogDebug("DataMixingEMWorker") << "processed ESRecHit with rawId: " << it->id().rawId() << "\n"
0204                                        << " rechit energy: " << it->energy();
0205 #endif
0206       }
0207     }
0208   }
0209 
0210   void DataMixingEMWorker::putEM(edm::Event &e) {
0211     // collection of rechits to put in the event
0212     std::unique_ptr<EBRecHitCollection> EBrechits(new EBRecHitCollection);
0213     std::unique_ptr<EERecHitCollection> EErechits(new EERecHitCollection);
0214     std::unique_ptr<ESRecHitCollection> ESrechits(new ESRecHitCollection);
0215 
0216     // loop over the maps we have, re-making individual hits or digis if
0217     // necessary.
0218     DetId formerID = 0;
0219     DetId currentID;
0220     float ESum = 0.;
0221     float EBTime = 0.;
0222 
0223     // EB first...
0224 
0225     EBRecHitMap::const_iterator iEBchk;
0226 
0227     for (EBRecHitMap::const_iterator iEB = EBRecHitStorage_.begin(); iEB != EBRecHitStorage_.end(); ++iEB) {
0228       currentID = iEB->first;
0229 
0230       if (currentID == formerID) {  // we have to add these rechits together
0231 
0232         ESum += (iEB->second).energy();
0233       } else {
0234         if (formerID > 0) {
0235           // cutoff for ESum?
0236           EcalRecHit aHit(formerID, ESum, EBTime);
0237           EBrechits->push_back(aHit);
0238         }
0239         // save pointers for next iteration
0240         formerID = currentID;
0241         ESum = (iEB->second).energy();
0242         EBTime = (iEB->second).time();  // take time of first hit in sequence - is this ok?
0243       }
0244 
0245       iEBchk = iEB;
0246       if ((++iEBchk) == EBRecHitStorage_.end()) {  // make sure not to lose the last one
0247         EcalRecHit aHit(formerID, ESum, EBTime);
0248         EBrechits->push_back(aHit);
0249       }
0250     }
0251 
0252     // EE next...
0253 
0254     // loop over the maps we have, re-making individual hits or digis if
0255     // necessary.
0256     formerID = 0;
0257     ESum = 0.;
0258     float EETime = 0.;
0259 
0260     EERecHitMap::const_iterator iEEchk;
0261 
0262     for (EERecHitMap::const_iterator iEE = EERecHitStorage_.begin(); iEE != EERecHitStorage_.end(); ++iEE) {
0263       currentID = iEE->first;
0264 
0265       if (currentID == formerID) {  // we have to add these rechits together
0266 
0267         ESum += (iEE->second).energy();
0268       } else {
0269         if (formerID > 0) {
0270           // cutoff for ESum?
0271           EcalRecHit aHit(formerID, ESum, EETime);
0272           EErechits->push_back(aHit);
0273         }
0274         // save pointers for next iteration
0275         formerID = currentID;
0276         ESum = (iEE->second).energy();
0277         EETime = (iEE->second).time();  // take time of first hit in sequence - is this ok?
0278       }
0279 
0280       iEEchk = iEE;
0281       if ((++iEEchk) == EERecHitStorage_.end()) {  // make sure not to lose the last one
0282         EcalRecHit aHit(formerID, ESum, EETime);
0283         EErechits->push_back(aHit);
0284       }
0285     }
0286 
0287     // ES next...
0288 
0289     // loop over the maps we have, re-making individual hits or digis if
0290     // necessary.
0291     formerID = 0;
0292     ESum = 0.;
0293     float ESTime = 0.;
0294 
0295     ESRecHitMap::const_iterator iESchk;
0296 
0297     for (ESRecHitMap::const_iterator iES = ESRecHitStorage_.begin(); iES != ESRecHitStorage_.end(); ++iES) {
0298       currentID = iES->first;
0299 
0300       if (currentID == formerID) {  // we have to add these rechits together
0301 
0302         ESum += (iES->second).energy();
0303       } else {
0304         if (formerID > 0) {
0305           // cutoff for ESum?
0306           EcalRecHit aHit(formerID, ESum, ESTime);
0307           ESrechits->push_back(aHit);
0308         }
0309         // save pointers for next iteration
0310         formerID = currentID;
0311         ESum = (iES->second).energy();
0312         ESTime = (iES->second).time();  // take time of first hit in sequence - is this ok?
0313       }
0314 
0315       iESchk = iES;
0316       if ((++iESchk) == ESRecHitStorage_.end()) {  // make sure not to lose the last one
0317         EcalRecHit aHit(formerID, ESum, ESTime);
0318         ESrechits->push_back(aHit);
0319       }
0320     }
0321 
0322     // done merging
0323 
0324     // put the collection of reconstructed hits in the event
0325     LogInfo("DataMixingEMWorker") << "total # EB Merged rechits: " << EBrechits->size();
0326     LogInfo("DataMixingEMWorker") << "total # EE Merged rechits: " << EErechits->size();
0327     LogInfo("DataMixingEMWorker") << "total # ES Merged rechits: " << ESrechits->size();
0328 
0329     e.put(std::move(EBrechits), EBRecHitCollectionDM_);
0330     e.put(std::move(EErechits), EERecHitCollectionDM_);
0331     e.put(std::move(ESrechits), ESRecHitCollectionDM_);
0332 
0333     // clear local storage after this event
0334 
0335     EBRecHitStorage_.clear();
0336     EERecHitStorage_.clear();
0337     ESRecHitStorage_.clear();
0338   }
0339 
0340 }  // namespace edm
0341 
0342 //  LocalWords:  ESProducerSig