CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/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() { }
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     // declare the products to produce
00038 
00039     SistripLabelSig_   = ps.getParameter<edm::InputTag>("SistripLabelSig");
00040     SiStripPileInputTag_ = ps.getParameter<edm::InputTag>("SiStripPileInputTag");
00041 
00042     SiStripDigiCollectionDM_  = ps.getParameter<std::string>("SiStripDigiCollectionDM");
00043 
00044     // clear local storage for this event                                                                     
00045     SiHitStorage_.clear();
00046 
00047   }
00048                
00049 
00050   // Virtual destructor needed.
00051   DataMixingSiStripWorker::~DataMixingSiStripWorker() { 
00052   }  
00053 
00054 
00055 
00056   void DataMixingSiStripWorker::addSiStripSignals(const edm::Event &e) { 
00057     // fill in maps of hits
00058 
00059     Handle< edm::DetSetVector<SiStripDigi> >  input;
00060 
00061     if( e.getByLabel(SistripLabelSig_,input) ) {
00062       OneDetectorMap LocalMap;
00063 
00064       //loop on all detsets (detectorIDs) inside the input collection
00065       edm::DetSetVector<SiStripDigi>::const_iterator DSViter=input->begin();
00066       for (; DSViter!=input->end();DSViter++){
00067 
00068 #ifdef DEBUG
00069         LogDebug("DataMixingSiStripWorker")  << "Processing DetID " << DSViter->id;
00070 #endif
00071 
00072         LocalMap.clear();
00073         LocalMap.reserve((DSViter->data).size());
00074         LocalMap.insert(LocalMap.end(),(DSViter->data).begin(),(DSViter->data).end());  
00075         
00076         SiHitStorage_.insert( SiGlobalIndex::value_type( DSViter->id, LocalMap ) );
00077       }
00078  
00079     }
00080   } // end of addSiStripSignals
00081 
00082 
00083 
00084   void DataMixingSiStripWorker::addSiStripPileups(const int bcr, const EventPrincipal *ep, unsigned int eventNr) {
00085     LogDebug("DataMixingSiStripWorker") <<"\n===============> adding pileups from event  "<<ep->id()<<" for bunchcrossing "<<bcr;
00086 
00087     // fill in maps of hits; same code as addSignals, except now applied to the pileup events
00088 
00089     boost::shared_ptr<Wrapper<edm::DetSetVector<SiStripDigi> >  const> inputPTR =
00090       getProductByTag<edm::DetSetVector<SiStripDigi> >(*ep, SiStripPileInputTag_ );
00091 
00092     if(inputPTR ) {
00093 
00094       const edm::DetSetVector<SiStripDigi>  *input = const_cast< edm::DetSetVector<SiStripDigi> * >(inputPTR->product());
00095 
00096       // Handle< edm::DetSetVector<SiStripDigi> >  input;
00097 
00098       // if( e->getByLabel(Sistripdigi_collectionPile_.label(),SistripLabelPile_.label(),input) ) {
00099 
00100       OneDetectorMap LocalMap;
00101 
00102       //loop on all detsets (detectorIDs) inside the input collection
00103       edm::DetSetVector<SiStripDigi>::const_iterator DSViter=input->begin();
00104       for (; DSViter!=input->end();DSViter++){
00105 
00106 #ifdef DEBUG
00107         LogDebug("DataMixingSiStripWorker")  << "Pileups: Processing DetID " << DSViter->id;
00108 #endif
00109 
00110         // find correct local map (or new one) for this detector ID
00111 
00112         SiGlobalIndex::const_iterator itest;
00113 
00114         itest = SiHitStorage_.find(DSViter->id);
00115 
00116         if(itest!=SiHitStorage_.end()) {  // this detID already has hits, add to existing map
00117 
00118           LocalMap = itest->second;
00119 
00120           // fill in local map with extra channels
00121           LocalMap.insert(LocalMap.end(),(DSViter->data).begin(),(DSViter->data).end());
00122           std::stable_sort(LocalMap.begin(),LocalMap.end(),DataMixingSiStripWorker::StrictWeakOrdering());
00123           SiHitStorage_[DSViter->id]=LocalMap;
00124           
00125         }
00126         else{ // fill local storage with this information, put in global collection
00127 
00128           LocalMap.clear();
00129           LocalMap.reserve((DSViter->data).size());
00130           LocalMap.insert(LocalMap.end(),(DSViter->data).begin(),(DSViter->data).end());
00131 
00132           SiHitStorage_.insert( SiGlobalIndex::value_type( DSViter->id, LocalMap ) );
00133         }
00134       }
00135     }
00136   }
00137 
00138 
00139  
00140   void DataMixingSiStripWorker::putSiStrip(edm::Event &e) {
00141 
00142     // collection of Digis to put in the event
00143     std::vector< edm::DetSet<SiStripDigi> > vSiStripDigi;
00144 
00145     // loop through our collection of detectors, merging hits and putting new ones in the output
00146 
00147     // big loop over Detector IDs:
00148 
00149     for(SiGlobalIndex::const_iterator IDet = SiHitStorage_.begin();
00150         IDet != SiHitStorage_.end(); IDet++) {
00151 
00152       edm::DetSet<SiStripDigi> SSD(IDet->first); // Make empty collection with this detector ID
00153         
00154       OneDetectorMap LocalMap = IDet->second;
00155 
00156       //counter variables
00157       int formerStrip = -1;
00158       int currentStrip;
00159       int ADCSum = 0;
00160 
00161       OneDetectorMap::const_iterator iLocalchk;
00162       OneDetectorMap::const_iterator iLocal  = LocalMap.begin();
00163       for(;iLocal != LocalMap.end(); ++iLocal) {
00164 
00165         currentStrip = iLocal->strip(); 
00166 
00167         if (currentStrip == formerStrip) { // we have to add these digis together
00168           ADCSum+=iLocal->adc();          // on every element...
00169         }
00170         else{
00171           if(formerStrip!=-1){
00172             if (ADCSum > 511) ADCSum = 255;
00173             else if (ADCSum > 253 && ADCSum < 512) ADCSum = 254;
00174             SiStripDigi aHit(formerStrip, ADCSum);
00175             SSD.push_back( aHit );        
00176           }
00177           // save pointers for next iteration
00178           formerStrip = currentStrip;
00179           ADCSum = iLocal->adc();
00180         }
00181 
00182         iLocalchk = iLocal;
00183         if((++iLocalchk) == LocalMap.end()) {  //make sure not to lose the last one
00184           if (ADCSum > 511) ADCSum = 255;
00185           else if (ADCSum > 253 && ADCSum < 512) ADCSum = 254;
00186           SSD.push_back( SiStripDigi(formerStrip, ADCSum) );      
00187         } // end of loop over one detector
00188         
00189       }
00190       // stick this into the global vector of detector info
00191       vSiStripDigi.push_back(SSD);
00192 
00193     } // end of big loop over all detector IDs
00194 
00195     // put the collection of digis in the event   
00196     LogInfo("DataMixingSiStripWorker") << "total # Merged strips: " << vSiStripDigi.size() ;
00197 
00198     // make new digi collection
00199     
00200     std::auto_ptr< edm::DetSetVector<SiStripDigi> > MySiStripDigis(new edm::DetSetVector<SiStripDigi>(vSiStripDigi) );
00201 
00202     // put collection
00203 
00204     e.put( MySiStripDigis, SiStripDigiCollectionDM_ );
00205 
00206     // clear local storage for this event
00207     SiHitStorage_.clear();
00208   }
00209 
00210 } //edm