CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch1/src/SimGeneral/DataMixingModule/plugins/DataMixingSiStripWorker.cc

Go to the documentation of this file.
00001 // File: DataMixingSiStripWorker.cc
00002 // Description:  see DataMixingSiStripWorker.h
00003 // Author:  Mike Hildreth, University of Notre Dame
00004 //
00005 //--------------------------------------------
00006 
00007 #include <map>
00008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00009 #include "FWCore/Utilities/interface/EDMException.h"
00010 #include "FWCore/Framework/interface/ConstProductRegistry.h"
00011 #include "FWCore/ServiceRegistry/interface/Service.h"
00012 #include "DataFormats/Common/interface/Handle.h"
00013 #include "DataFormats/Provenance/interface/Provenance.h"
00014 #include "DataFormats/Provenance/interface/BranchDescription.h"
00015 //
00016 //
00017 #include "DataMixingSiStripWorker.h"
00018 
00019 using namespace std;
00020 
00021 namespace edm
00022 {
00023 
00024   // Virtual constructor
00025 
00026   DataMixingSiStripWorker::DataMixingSiStripWorker() { sel_=0;}
00027 
00028   // Constructor 
00029   DataMixingSiStripWorker::DataMixingSiStripWorker(const edm::ParameterSet& ps) : 
00030                                                             label_(ps.getParameter<std::string>("Label"))
00031 
00032   {                                                         
00033 
00034     // get the subdetector names
00035     //    this->getSubdetectorNames();  //something like this may be useful to check what we are supposed to do...
00036 
00037     // create input selector
00038     if (label_.size()>0){
00039       sel_=new Selector( ModuleLabelSelector(label_));
00040     }
00041     else {
00042       sel_=new Selector( MatchAllSelector());
00043     }
00044 
00045     // declare the products to produce
00046 
00047     SistripLabelSig_   = ps.getParameter<edm::InputTag>("SistripLabelSig");
00048     SiStripPileInputTag_ = ps.getParameter<edm::InputTag>("SiStripPileInputTag");
00049 
00050     SiStripDigiCollectionDM_  = ps.getParameter<std::string>("SiStripDigiCollectionDM");
00051 
00052     // clear local storage for this event                                                                     
00053     SiHitStorage_.clear();
00054 
00055   }
00056                
00057 
00058   // Virtual destructor needed.
00059   DataMixingSiStripWorker::~DataMixingSiStripWorker() { 
00060     delete sel_;
00061     sel_=0;
00062   }  
00063 
00064 
00065 
00066   void DataMixingSiStripWorker::addSiStripSignals(const edm::Event &e) { 
00067     // fill in maps of hits
00068 
00069     Handle< edm::DetSetVector<SiStripDigi> >  input;
00070 
00071     if( e.getByLabel(SistripLabelSig_,input) ) {
00072       OneDetectorMap LocalMap;
00073 
00074       //loop on all detsets (detectorIDs) inside the input collection
00075       edm::DetSetVector<SiStripDigi>::const_iterator DSViter=input->begin();
00076       for (; DSViter!=input->end();DSViter++){
00077 
00078 #ifdef DEBUG
00079         LogDebug("DataMixingSiStripWorker")  << "Processing DetID " << DSViter->id;
00080 #endif
00081 
00082         LocalMap.clear();
00083         LocalMap.reserve((DSViter->data).size());
00084         LocalMap.insert(LocalMap.end(),(DSViter->data).begin(),(DSViter->data).end());  
00085         
00086         SiHitStorage_.insert( SiGlobalIndex::value_type( DSViter->id, LocalMap ) );
00087       }
00088  
00089     }
00090   } // end of addSiStripSignals
00091 
00092 
00093 
00094   void DataMixingSiStripWorker::addSiStripPileups(const int bcr, const EventPrincipal *ep, unsigned int eventNr) {
00095     LogDebug("DataMixingSiStripWorker") <<"\n===============> adding pileups from event  "<<ep->id()<<" for bunchcrossing "<<bcr;
00096 
00097     // fill in maps of hits; same code as addSignals, except now applied to the pileup events
00098 
00099     boost::shared_ptr<Wrapper<edm::DetSetVector<SiStripDigi> >  const> inputPTR =
00100       getProductByTag<edm::DetSetVector<SiStripDigi> >(*ep, SiStripPileInputTag_ );
00101 
00102     if(inputPTR ) {
00103 
00104       const edm::DetSetVector<SiStripDigi>  *input = const_cast< edm::DetSetVector<SiStripDigi> * >(inputPTR->product());
00105 
00106       // Handle< edm::DetSetVector<SiStripDigi> >  input;
00107 
00108       // if( e->getByLabel(Sistripdigi_collectionPile_.label(),SistripLabelPile_.label(),input) ) {
00109 
00110       OneDetectorMap LocalMap;
00111 
00112       //loop on all detsets (detectorIDs) inside the input collection
00113       edm::DetSetVector<SiStripDigi>::const_iterator DSViter=input->begin();
00114       for (; DSViter!=input->end();DSViter++){
00115 
00116 #ifdef DEBUG
00117         LogDebug("DataMixingSiStripWorker")  << "Pileups: Processing DetID " << DSViter->id;
00118 #endif
00119 
00120         // find correct local map (or new one) for this detector ID
00121 
00122         SiGlobalIndex::const_iterator itest;
00123 
00124         itest = SiHitStorage_.find(DSViter->id);
00125 
00126         if(itest!=SiHitStorage_.end()) {  // this detID already has hits, add to existing map
00127 
00128           LocalMap = itest->second;
00129 
00130           // fill in local map with extra channels
00131           LocalMap.insert(LocalMap.end(),(DSViter->data).begin(),(DSViter->data).end());
00132           std::stable_sort(LocalMap.begin(),LocalMap.end(),DataMixingSiStripWorker::StrictWeakOrdering());
00133           SiHitStorage_[DSViter->id]=LocalMap;
00134           
00135         }
00136         else{ // fill local storage with this information, put in global collection
00137 
00138           LocalMap.clear();
00139           LocalMap.reserve((DSViter->data).size());
00140           LocalMap.insert(LocalMap.end(),(DSViter->data).begin(),(DSViter->data).end());
00141 
00142           SiHitStorage_.insert( SiGlobalIndex::value_type( DSViter->id, LocalMap ) );
00143         }
00144       }
00145     }
00146   }
00147 
00148 
00149  
00150   void DataMixingSiStripWorker::putSiStrip(edm::Event &e) {
00151 
00152     // collection of Digis to put in the event
00153     std::vector< edm::DetSet<SiStripDigi> > vSiStripDigi;
00154 
00155     // loop through our collection of detectors, merging hits and putting new ones in the output
00156 
00157     // big loop over Detector IDs:
00158 
00159     for(SiGlobalIndex::const_iterator IDet = SiHitStorage_.begin();
00160         IDet != SiHitStorage_.end(); IDet++) {
00161 
00162       edm::DetSet<SiStripDigi> SSD(IDet->first); // Make empty collection with this detector ID
00163         
00164       OneDetectorMap LocalMap = IDet->second;
00165 
00166       //counter variables
00167       int formerStrip = -1;
00168       int currentStrip;
00169       int ADCSum = 0;
00170 
00171       OneDetectorMap::const_iterator iLocalchk;
00172       OneDetectorMap::const_iterator iLocal  = LocalMap.begin();
00173       for(;iLocal != LocalMap.end(); ++iLocal) {
00174 
00175         currentStrip = iLocal->strip(); 
00176 
00177         if (currentStrip == formerStrip) { // we have to add these digis together
00178           ADCSum+=iLocal->adc();          // on every element...
00179         }
00180         else{
00181           if(formerStrip!=-1){
00182             if (ADCSum > 511) ADCSum = 255;
00183             else if (ADCSum > 253 && ADCSum < 512) ADCSum = 254;
00184             SiStripDigi aHit(formerStrip, ADCSum);
00185             SSD.push_back( aHit );        
00186           }
00187           // save pointers for next iteration
00188           formerStrip = currentStrip;
00189           ADCSum = iLocal->adc();
00190         }
00191 
00192         iLocalchk = iLocal;
00193         if((++iLocalchk) == LocalMap.end()) {  //make sure not to lose the last one
00194           if (ADCSum > 511) ADCSum = 255;
00195           else if (ADCSum > 253 && ADCSum < 512) ADCSum = 254;
00196           SSD.push_back( SiStripDigi(formerStrip, ADCSum) );      
00197         } // end of loop over one detector
00198         
00199       }
00200       // stick this into the global vector of detector info
00201       vSiStripDigi.push_back(SSD);
00202 
00203     } // end of big loop over all detector IDs
00204 
00205     // put the collection of digis in the event   
00206     LogInfo("DataMixingSiStripWorker") << "total # Merged strips: " << vSiStripDigi.size() ;
00207 
00208     // make new digi collection
00209     
00210     std::auto_ptr< edm::DetSetVector<SiStripDigi> > MySiStripDigis(new edm::DetSetVector<SiStripDigi>(vSiStripDigi) );
00211 
00212     // put collection
00213 
00214     e.put( MySiStripDigis, SiStripDigiCollectionDM_ );
00215 
00216     // clear local storage for this event
00217     SiHitStorage_.clear();
00218   }
00219 
00220 } //edm