CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/SimGeneral/DataMixingModule/plugins/DataMixingHcalDigiWorker.cc

Go to the documentation of this file.
00001 // File: DataMixingHcalDigiWorker.cc
00002 // Description:  see DataMixingHcalDigiWorker.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 "FWCore/Framework/interface/Selector.h"
00013 #include "FWCore/Framework/interface/ESHandle.h"
00014 #include "FWCore/Framework/interface/EventSetup.h"
00015 #include "DataFormats/Common/interface/Handle.h"
00016 #include "DataFormats/Provenance/interface/Provenance.h"
00017 #include "DataFormats/Provenance/interface/BranchDescription.h"
00018 // calibration headers, for future reference 
00019 #include "CalibFormats/HcalObjects/interface/HcalCoderDb.h"   
00020 #include "CalibFormats/HcalObjects/interface/HcalCalibrations.h" 
00021 #include "CalibFormats/HcalObjects/interface/HcalDbService.h"  
00022 #include "CalibFormats/HcalObjects/interface/HcalDbRecord.h"   
00023 //
00024 //
00025 #include "DataMixingHcalDigiWorker.h"
00026 
00027 
00028 using namespace std;
00029 
00030 namespace edm
00031 {
00032 
00033   // Virtual constructor
00034 
00035   DataMixingHcalDigiWorker::DataMixingHcalDigiWorker() {sel_=0;} 
00036 
00037   // Constructor 
00038   DataMixingHcalDigiWorker::DataMixingHcalDigiWorker(const edm::ParameterSet& ps) : 
00039                                                             label_(ps.getParameter<std::string>("Label"))
00040 
00041   {                                                         
00042 
00043     // get the subdetector names
00044     //    this->getSubdetectorNames();  //something like this may be useful to check what we are supposed to do...
00045 
00046     // create input selector
00047     if (label_.size()>0){
00048       sel_=new Selector( ModuleLabelSelector(label_));
00049     }
00050     else {
00051       sel_=new Selector( MatchAllSelector());
00052     }
00053 
00054     // declare the products to produce
00055 
00056     // Hcal 
00057 
00058     HBHEdigiCollectionSig_  = ps.getParameter<edm::InputTag>("HBHEdigiCollectionSig");
00059     HOdigiCollectionSig_    = ps.getParameter<edm::InputTag>("HOdigiCollectionSig");
00060     HFdigiCollectionSig_    = ps.getParameter<edm::InputTag>("HFdigiCollectionSig");
00061     ZDCdigiCollectionSig_   = ps.getParameter<edm::InputTag>("ZDCdigiCollectionSig");
00062 
00063     HBHEPileInputTag_ = ps.getParameter<edm::InputTag>("HBHEPileInputTag");
00064     HOPileInputTag_ = ps.getParameter<edm::InputTag>("HOPileInputTag");
00065     HFPileInputTag_ = ps.getParameter<edm::InputTag>("HFPileInputTag");
00066     ZDCPileInputTag_ = ps.getParameter<edm::InputTag>("ZDCPileInputTag");
00067 
00068     DoZDC_ = false;
00069     if(ZDCPileInputTag_.label() != "") DoZDC_ = true;
00070 
00071     HBHEDigiCollectionDM_ = ps.getParameter<std::string>("HBHEDigiCollectionDM");
00072     HODigiCollectionDM_   = ps.getParameter<std::string>("HODigiCollectionDM");
00073     HFDigiCollectionDM_   = ps.getParameter<std::string>("HFDigiCollectionDM");
00074     ZDCDigiCollectionDM_  = ps.getParameter<std::string>("ZDCDigiCollectionDM");
00075 
00076 
00077   }
00078                
00079   // Virtual destructor needed.
00080   DataMixingHcalDigiWorker::~DataMixingHcalDigiWorker() { 
00081     delete sel_;
00082     sel_=0;
00083   }  
00084 
00085   void DataMixingHcalDigiWorker::addHcalSignals(const edm::Event &e,const edm::EventSetup& ES) { 
00086     // Calibration stuff will look like this:                                                 
00087 
00088     // get conditions                                                                         
00089     edm::ESHandle<HcalDbService> conditions;                                                
00090     ES.get<HcalDbRecord>().get(conditions);                                         
00091 
00092     const HcalQIEShape* shape = conditions->getHcalShape (); // this one is generic         
00093 
00094 
00095     // fill in maps of hits
00096 
00097     LogInfo("DataMixingHcalDigiWorker")<<"===============> adding MC signals for "<<e.id();
00098 
00099     // HBHE first
00100 
00101    Handle< HBHEDigiCollection > pHBHEDigis;
00102 
00103    const HBHEDigiCollection*  HBHEDigis = 0;
00104 
00105    if( e.getByLabel( HBHEdigiCollectionSig_, pHBHEDigis) ) {
00106      HBHEDigis = pHBHEDigis.product(); // get a ptr to the product
00107      LogDebug("DataMixingHcalDigiWorker") << "total # HBHE digis: " << HBHEDigis->size();
00108    } 
00109    //   else { std::cout << "NO HBHE Digis " << HBHEdigiCollectionSig_.label() << std::endl;}
00110    
00111  
00112    if (HBHEDigis)
00113      {
00114        // loop over digis, storing them in a map so we can add pileup later
00115        for(HBHEDigiCollection::const_iterator it  = HBHEDigis->begin(); 
00116            it != HBHEDigis->end(); ++it) {
00117 
00118          // calibration, for future reference:  (same block for all Hcal types)               
00119 
00120          HcalDetId cell = it->id();                                                         
00121          //         const HcalCalibrations& calibrations=conditions->getHcalCalibrations(cell);        
00122          const HcalQIECoder* channelCoder = conditions->getHcalCoder (cell);                
00123          HcalCoderDb coder (*channelCoder, *shape);                                         
00124 
00125          CaloSamples tool;
00126          coder.adc2fC((*it),tool);
00127 
00128          // put sample values back into digi?
00129 
00130 
00131          // RecHit MyHit = reco_.reconstruct(*it,coder,calibrations));                         
00132          //... can now fish calibrated information from RecHit                                
00133 
00134 
00135          HBHEDigiStorage_.insert(HBHEDigiMap::value_type( ( it->id() ), tool ));
00136          
00137 #ifdef DEBUG     
00138          LogDebug("DataMixingHcalDigiWorker") << "processed HBHEDigi with rawId: "
00139                                       << it->id() << "\n"
00140                                       << " digi energy: " << it->energy();
00141 #endif
00142 
00143        }
00144      }
00145 
00146    // HO next
00147 
00148    Handle< HODigiCollection > pHODigis;
00149 
00150    const HODigiCollection*  HODigis = 0;
00151 
00152    if( e.getByLabel( HOdigiCollectionSig_, pHODigis) ){
00153      HODigis = pHODigis.product(); // get a ptr to the product
00154 #ifdef DEBUG
00155      LogDebug("DataMixingHcalDigiWorker") << "total # HO digis: " << HODigis->size();
00156 #endif
00157    } 
00158    
00159  
00160    if (HODigis)
00161      {
00162        // loop over digis, storing them in a map so we can add pileup later
00163        for(HODigiCollection::const_iterator it  = HODigis->begin();     
00164            it != HODigis->end(); ++it) {
00165 
00166          // calibration, for future reference:  (same block for all Hcal types)                                
00167          HcalDetId cell = it->id();
00168          //         const HcalCalibrations& calibrations=conditions->getHcalCalibrations(cell);                
00169          const HcalQIECoder* channelCoder = conditions->getHcalCoder (cell);
00170          HcalCoderDb coder (*channelCoder, *shape);
00171 
00172          CaloSamples tool;
00173          coder.adc2fC((*it),tool);
00174 
00175          HODigiStorage_.insert(HODigiMap::value_type( ( it->id() ), tool ));
00176          
00177 #ifdef DEBUG     
00178          LogDebug("DataMixingHcalDigiWorker") << "processed HODigi with rawId: "
00179                                       << it->id() << "\n"
00180                                       << " digi energy: " << it->energy();
00181 #endif
00182 
00183        }
00184      }
00185 
00186    // HF next
00187 
00188    Handle< HFDigiCollection > pHFDigis;
00189 
00190    const HFDigiCollection*  HFDigis = 0;
00191 
00192    if( e.getByLabel( HFdigiCollectionSig_, pHFDigis) ) {
00193      HFDigis = pHFDigis.product(); // get a ptr to the product
00194 #ifdef DEBUG
00195      LogDebug("DataMixingHcalDigiWorker") << "total # HF digis: " << HFDigis->size();
00196 #endif
00197    } 
00198    
00199  
00200    if (HFDigis)
00201      {
00202        // loop over digis, storing them in a map so we can add pileup later
00203        for(HFDigiCollection::const_iterator it  = HFDigis->begin();     
00204            it != HFDigis->end(); ++it) {
00205 
00206          // calibration, for future reference:  (same block for all Hcal types)                                
00207          HcalDetId cell = it->id();
00208          //         const HcalCalibrations& calibrations=conditions->getHcalCalibrations(cell);                
00209          const HcalQIECoder* channelCoder = conditions->getHcalCoder (cell);
00210          HcalCoderDb coder (*channelCoder, *shape);
00211 
00212          CaloSamples tool;
00213          coder.adc2fC((*it),tool);
00214 
00215          HFDigiStorage_.insert(HFDigiMap::value_type( ( it->id() ), tool ));
00216          
00217 #ifdef DEBUG     
00218          LogDebug("DataMixingHcalDigiWorker") << "processed HFDigi with rawId: "
00219                                       << it->id() << "\n"
00220                                       << " digi energy: " << it->energy();
00221 #endif
00222 
00223        }
00224      }
00225 
00226    // ZDC next
00227 
00228    Handle< ZDCDigiCollection > pZDCDigis;
00229 
00230    const ZDCDigiCollection*  ZDCDigis = 0;
00231 
00232    if( e.getByLabel( ZDCdigiCollectionSig_, pZDCDigis) ) {
00233      ZDCDigis = pZDCDigis.product(); // get a ptr to the product
00234 #ifdef DEBUG
00235      LogDebug("DataMixingHcalDigiWorker") << "total # ZDC digis: " << ZDCDigis->size();
00236 #endif
00237    } 
00238    
00239  
00240    if (ZDCDigis)
00241      {
00242        // loop over digis, storing them in a map so we can add pileup later
00243        for(ZDCDigiCollection::const_iterator it  = ZDCDigis->begin();   
00244            it != ZDCDigis->end(); ++it) {
00245 
00246          // calibration, for future reference:  (same block for all Hcal types)                                
00247          HcalDetId cell = it->id();
00248          //         const HcalCalibrations& calibrations=conditions->getHcalCalibrations(cell);                
00249          const HcalQIECoder* channelCoder = conditions->getHcalCoder (cell);
00250          HcalCoderDb coder (*channelCoder, *shape);
00251 
00252          CaloSamples tool;
00253          coder.adc2fC((*it),tool);
00254 
00255          ZDCDigiStorage_.insert(ZDCDigiMap::value_type( ( it->id() ), tool ));
00256          
00257 #ifdef DEBUG     
00258          LogDebug("DataMixingHcalDigiWorker") << "processed ZDCDigi with rawId: "
00259                                       << it->id() << "\n"
00260                                       << " digi energy: " << it->energy();
00261 #endif
00262 
00263        }
00264      }
00265     
00266   } // end of addHCalSignals
00267 
00268   void DataMixingHcalDigiWorker::addHcalPileups(const int bcr, const EventPrincipal *ep, unsigned int eventNr,const edm::EventSetup& ES) {
00269   
00270     LogDebug("DataMixingHcalDigiWorker") <<"\n===============> adding pileups from event  "<<ep->id()<<" for bunchcrossing "<<bcr;
00271 
00272     // get conditions                                                                                                             
00273     edm::ESHandle<HcalDbService> conditions;
00274     ES.get<HcalDbRecord>().get(conditions);
00275 
00276     const HcalQIEShape* shape = conditions->getHcalShape (); // this one is generic         
00277 
00278     // fill in maps of hits; same code as addSignals, except now applied to the pileup events
00279 
00280     // HBHE first
00281 
00282     boost::shared_ptr<Wrapper<HBHEDigiCollection>  const> HBHEDigisPTR = 
00283           getProductByTag<HBHEDigiCollection>(*ep, HBHEPileInputTag_ );
00284  
00285     if(HBHEDigisPTR ) {
00286 
00287      const HBHEDigiCollection*  HBHEDigis = const_cast< HBHEDigiCollection * >(HBHEDigisPTR->product());
00288 
00289      LogInfo("DataMixingHcalDigiWorker") << "total # HBHE digis: " << HBHEDigis->size();
00290 
00291        // loop over digis, adding these to the existing maps
00292        for(HBHEDigiCollection::const_iterator it  = HBHEDigis->begin();
00293            it != HBHEDigis->end(); ++it) {
00294 
00295          // calibration, for future reference:  (same block for all Hcal types)                                
00296          HcalDetId cell = it->id();
00297          //         const HcalCalibrations& calibrations=conditions->getHcalCalibrations(cell);                
00298          const HcalQIECoder* channelCoder = conditions->getHcalCoder (cell);
00299          HcalCoderDb coder (*channelCoder, *shape);
00300 
00301          CaloSamples tool;
00302          coder.adc2fC((*it),tool);
00303 
00304          HBHEDigiStorage_.insert(HBHEDigiMap::value_type( (it->id()), tool ));
00305          
00306 #ifdef DEBUG     
00307          LogDebug("DataMixingHcalDigiWorker") << "processed HBHEDigi with rawId: "
00308                                       << it->id() << "\n"
00309                                       << " digi energy: " << it->energy();
00310 #endif
00311        }
00312     }
00313     // HO Next
00314 
00315     boost::shared_ptr<Wrapper<HODigiCollection>  const> HODigisPTR = 
00316           getProductByTag<HODigiCollection>(*ep, HOPileInputTag_ );
00317  
00318     if(HODigisPTR ) {
00319 
00320      const HODigiCollection*  HODigis = const_cast< HODigiCollection * >(HODigisPTR->product());
00321 
00322      LogDebug("DataMixingHcalDigiWorker") << "total # HO digis: " << HODigis->size();
00323 
00324        // loop over digis, adding these to the existing maps
00325        for(HODigiCollection::const_iterator it  = HODigis->begin();
00326            it != HODigis->end(); ++it) {
00327 
00328          // calibration, for future reference:  (same block for all Hcal types)                                
00329          HcalDetId cell = it->id();
00330          //         const HcalCalibrations& calibrations=conditions->getHcalCalibrations(cell);                
00331          const HcalQIECoder* channelCoder = conditions->getHcalCoder (cell);
00332          HcalCoderDb coder (*channelCoder, *shape);
00333 
00334          CaloSamples tool;
00335          coder.adc2fC((*it),tool);
00336 
00337          HODigiStorage_.insert(HODigiMap::value_type( (it->id()), tool ));
00338          
00339 #ifdef DEBUG     
00340          LogDebug("DataMixingHcalDigiWorker") << "processed HODigi with rawId: "
00341                                       << it->id() << "\n"
00342                                       << " digi energy: " << it->energy();
00343 #endif
00344        }
00345     }
00346 
00347 
00348     // HF Next
00349 
00350     boost::shared_ptr<Wrapper<HFDigiCollection>  const> HFDigisPTR = 
00351           getProductByTag<HFDigiCollection>(*ep, HFPileInputTag_ );
00352  
00353     if(HFDigisPTR ) {
00354 
00355      const HFDigiCollection*  HFDigis = const_cast< HFDigiCollection * >(HFDigisPTR->product());
00356 
00357      LogDebug("DataMixingHcalDigiWorker") << "total # HF digis: " << HFDigis->size();
00358 
00359        // loop over digis, adding these to the existing maps
00360        for(HFDigiCollection::const_iterator it  = HFDigis->begin();
00361            it != HFDigis->end(); ++it) {
00362 
00363          // calibration, for future reference:  (same block for all Hcal types)                                
00364          HcalDetId cell = it->id();
00365          //         const HcalCalibrations& calibrations=conditions->getHcalCalibrations(cell);                
00366          const HcalQIECoder* channelCoder = conditions->getHcalCoder (cell);
00367          HcalCoderDb coder (*channelCoder, *shape);
00368 
00369          CaloSamples tool;
00370          coder.adc2fC((*it),tool);
00371 
00372          HFDigiStorage_.insert(HFDigiMap::value_type( (it->id()), tool ));
00373          
00374 #ifdef DEBUG     
00375          LogDebug("DataMixingHcalDigiWorker") << "processed HFDigi with rawId: "
00376                                       << it->id() << "\n"
00377                                       << " digi energy: " << it->energy();
00378 #endif
00379        }
00380     }
00381 
00382 
00383     // ZDC Next
00384 
00385     if(DoZDC_) {
00386 
00387 
00388       boost::shared_ptr<Wrapper<ZDCDigiCollection>  const> ZDCDigisPTR = 
00389         getProductByTag<ZDCDigiCollection>(*ep, ZDCPileInputTag_ );
00390  
00391       if(ZDCDigisPTR ) {
00392 
00393         const ZDCDigiCollection*  ZDCDigis = const_cast< ZDCDigiCollection * >(ZDCDigisPTR->product());
00394 
00395         LogDebug("DataMixingHcalDigiWorker") << "total # ZDC digis: " << ZDCDigis->size();
00396 
00397         // loop over digis, adding these to the existing maps
00398         for(ZDCDigiCollection::const_iterator it  = ZDCDigis->begin();
00399             it != ZDCDigis->end(); ++it) {
00400 
00401           // calibration, for future reference:  (same block for all Hcal types)                                
00402           HcalDetId cell = it->id();
00403           //         const HcalCalibrations& calibrations=conditions->getHcalCalibrations(cell);                
00404           const HcalQIECoder* channelCoder = conditions->getHcalCoder (cell);
00405           HcalCoderDb coder (*channelCoder, *shape);
00406 
00407           CaloSamples tool;
00408           coder.adc2fC((*it),tool);
00409 
00410           ZDCDigiStorage_.insert(ZDCDigiMap::value_type( (it->id()), tool ));
00411          
00412 #ifdef DEBUG     
00413           LogDebug("DataMixingHcalDigiWorker") << "processed ZDCDigi with rawId: "
00414                                                << it->id() << "\n"
00415                                                << " digi energy: " << it->energy();
00416 #endif
00417         }
00418       }
00419     }
00420 
00421 
00422   }
00423  
00424   void DataMixingHcalDigiWorker::putHcal(edm::Event &e,const edm::EventSetup& ES) {
00425 
00426     // collection of digis to put in the event
00427     std::auto_ptr< HBHEDigiCollection > HBHEdigis( new HBHEDigiCollection );
00428     std::auto_ptr< HODigiCollection > HOdigis( new HODigiCollection );
00429     std::auto_ptr< HFDigiCollection > HFdigis( new HFDigiCollection );
00430     std::auto_ptr< ZDCDigiCollection > ZDCdigis( new ZDCDigiCollection );
00431 
00432     // get conditions                                                                                                             
00433     edm::ESHandle<HcalDbService> conditions;
00434     ES.get<HcalDbRecord>().get(conditions);
00435 
00436     // un-calibrate
00437     const HcalQIEShape* shape = conditions->getHcalShape (); // this one is generic                            
00438 
00439     // loop over the maps we have, re-making individual hits or digis if necessary.
00440     DetId formerID = 0;
00441     DetId currentID;
00442 
00443     CaloSamples HB_old;
00444 
00445     double fC_new;
00446     double fC_old;
00447     double fC_sum;
00448 
00449     // HB first...
00450 
00451     HBHEDigiMap::const_iterator iHBchk;
00452 
00453     for(HBHEDigiMap::const_iterator iHB  = HBHEDigiStorage_.begin();
00454         iHB != HBHEDigiStorage_.end(); ++iHB) {
00455 
00456       currentID = iHB->first; 
00457 
00458       if (currentID == formerID) { // we have to add these digis together
00459 
00460         //loop over digi samples in each CaloSample                                                  
00461 
00462         unsigned int sizenew = (iHB->second).size();
00463         unsigned int sizeold = HB_old.size();
00464 
00465         bool usenew = false;
00466 
00467         if(sizenew > sizeold) usenew = true;
00468 
00469         unsigned int max_samp = std::max(sizenew, sizeold);
00470 
00471         CaloSamples HB_bigger(currentID,max_samp);      
00472 
00473         // HB_old.setSize(max_samp);  --> can't do this...
00474 
00475         // samples from different events can be of different lengths - sum all                      
00476         // that overlap.                
00477 
00478         for(unsigned int isamp = 0; isamp<max_samp; isamp++) {
00479           if(isamp < sizenew) {
00480             fC_new = (iHB->second)[isamp]; // should return nominal_fC();
00481           }
00482           else { fC_new = 0;}
00483 
00484           if(isamp < sizeold) {
00485             fC_old = HB_old[isamp];
00486           }
00487           else { fC_old = 0;}
00488 
00489           // add values   
00490           fC_sum = fC_new + fC_old;
00491           
00492           //uint fCS = int(fC_sum);
00493           //const HcalQIESample fC(fCS); 
00494           //HB_old.setSample(isamp, fC);
00495 
00496           if(usenew) {HB_bigger[isamp] = fC_sum; }
00497           else { HB_old[isamp] = fC_sum; }  // overwrite old sample, adding new info     
00498 
00499         }
00500         if(usenew) HB_old = HB_bigger; // save new, larger sized sample in "old" slot
00501 
00502       }
00503       else {
00504         if(formerID>0) {
00505           // make new digi
00506           HBHEdigis->push_back(HBHEDataFrame(formerID));          
00507 
00508           // set up information to convert back
00509 
00510           HcalDetId cell = HB_old.id();
00511           const HcalQIECoder* channelCoder = conditions->getHcalCoder (cell);
00512           HcalCoderDb coder (*channelCoder, *shape);
00513 
00514           unsigned int sizeold = HB_old.size();
00515           for(unsigned int isamp = 0; isamp<sizeold; isamp++) {
00516             coder.fC2adc(HB_old,(HBHEdigis->back()), 0 );   // as per simulation, capid=0???
00517           }
00518         }
00519         //save pointers for next iteration                                                                 
00520         formerID = currentID;
00521         HB_old = iHB->second;
00522         //OldUpAdd = HB_old.id(); 
00523       }
00524 
00525       iHBchk = iHB;
00526       if((++iHBchk) == HBHEDigiStorage_.end()) {  //make sure not to lose the last one                         
00527 
00528         // make new digi                                                                                     
00529         HBHEdigis->push_back(HBHEDataFrame(currentID));
00530 
00531         // set up information to convert back                                                                
00532 
00533         HcalDetId cell = (iHB->second).id();
00534         const HcalQIECoder* channelCoder = conditions->getHcalCoder (cell);
00535         HcalCoderDb coder (*channelCoder, *shape);
00536 
00537         unsigned int sizenew = (iHB->second).size();
00538         for(unsigned int isamp = 0; isamp<sizenew; isamp++) {
00539           coder.fC2adc(HB_old,(HBHEdigis->back()), 0 );  // as per simulation, capid=0???
00540         }
00541       }
00542     }
00543 
00544 
00545     // HO next...
00546 
00547     // loop over the maps we have, re-making individual hits or digis if necessary.
00548     formerID = 0;
00549     CaloSamples HO_old;
00550 
00551     HODigiMap::const_iterator iHOchk;
00552 
00553     for(HODigiMap::const_iterator iHO  = HODigiStorage_.begin();
00554         iHO != HODigiStorage_.end(); ++iHO) {
00555 
00556       currentID = iHO->first; 
00557 
00558       if (currentID == formerID) { // we have to add these digis together
00559 
00560         //loop over digi samples in each CaloSample                                                           
00561         unsigned int sizenew = (iHO->second).size();
00562         unsigned int sizeold = HO_old.size();
00563 
00564         unsigned int max_samp = std::max(sizenew, sizeold);
00565 
00566         CaloSamples HO_bigger(currentID,max_samp);
00567 
00568         bool usenew = false;
00569 
00570         if(sizenew > sizeold) usenew = true;
00571 
00572         // samples from different events can be of different lengths - sum all                               
00573         // that overlap.                                                                                     
00574 
00575         for(unsigned int isamp = 0; isamp<max_samp; isamp++) {
00576           if(isamp < sizenew) {
00577             fC_new = (iHO->second)[isamp];
00578           }
00579           else { fC_new = 0;}
00580 
00581           if(isamp < sizeold) {
00582             fC_old = HO_old[isamp];
00583           }
00584           else { fC_old = 0;}
00585 
00586           // add values                                                                                      
00587           fC_sum = fC_new + fC_old;
00588 
00589           if(usenew) {HO_bigger[isamp] = fC_sum; }
00590           else { HO_old[isamp] = fC_sum; }  // overwrite old sample, adding new info     
00591 
00592         }
00593         if(usenew) HO_old = HO_bigger; // save new, larger sized sample in "old" slot
00594       
00595       }
00596       else {
00597         if(formerID>0) {
00598           // make new digi
00599           HOdigis->push_back(HODataFrame(formerID));      
00600 
00601           // set up information to convert back
00602 
00603           HcalDetId cell = HO_old.id();
00604           const HcalQIECoder* channelCoder = conditions->getHcalCoder (cell);
00605           HcalCoderDb coder (*channelCoder, *shape);
00606 
00607           unsigned int sizeold = HO_old.size();
00608           for(unsigned int isamp = 0; isamp<sizeold; isamp++) {
00609             coder.fC2adc(HO_old,(HOdigis->back()), 0 );   // as per simulation, capid=0???
00610           }
00611         }
00612         //save pointers for next iteration                                                                 
00613         formerID = currentID;
00614         HO_old = iHO->second;
00615       }
00616 
00617       iHOchk = iHO;
00618       if((++iHOchk) == HODigiStorage_.end()) {  //make sure not to lose the last one                         
00619           // make new digi
00620           HOdigis->push_back(HODataFrame(currentID));     
00621 
00622           // set up information to convert back
00623 
00624           HcalDetId cell = (iHO->second).id();
00625           const HcalQIECoder* channelCoder = conditions->getHcalCoder (cell);
00626           HcalCoderDb coder (*channelCoder, *shape);
00627 
00628           unsigned int sizeold = (iHO->second).size();
00629           for(unsigned int isamp = 0; isamp<sizeold; isamp++) {
00630             coder.fC2adc(HO_old,(HOdigis->back()), 0 );   // as per simulation, capid=0???
00631           }
00632 
00633       }
00634     }
00635 
00636     // HF next...
00637 
00638     // loop over the maps we have, re-making individual hits or digis if necessary.
00639     formerID = 0;
00640     CaloSamples HF_old;
00641 
00642     HFDigiMap::const_iterator iHFchk;
00643 
00644     for(HFDigiMap::const_iterator iHF  = HFDigiStorage_.begin();
00645         iHF != HFDigiStorage_.end(); ++iHF) {
00646 
00647       currentID = iHF->first; 
00648 
00649       if (currentID == formerID) { // we have to add these digis together
00650 
00651         //loop over digi samples in each CaloSample                                                           
00652         unsigned int sizenew = (iHF->second).size();
00653         unsigned int sizeold = HF_old.size();
00654 
00655         unsigned int max_samp = std::max(sizenew, sizeold);
00656 
00657         CaloSamples HF_bigger(currentID,max_samp);
00658 
00659         bool usenew = false;
00660 
00661         if(sizenew > sizeold) usenew = true;
00662 
00663         // samples from different events can be of different lengths - sum all                               
00664         // that overlap.                                                                                     
00665 
00666         for(unsigned int isamp = 0; isamp<max_samp; isamp++) {
00667           if(isamp < sizenew) {
00668             fC_new = (iHF->second)[isamp];
00669           }
00670           else { fC_new = 0;}
00671 
00672           if(isamp < sizeold) {
00673             fC_old = HF_old[isamp];
00674           }
00675           else { fC_old = 0;}
00676 
00677           // add values                                                                                      
00678           fC_sum = fC_new + fC_old;
00679 
00680           if(usenew) {HF_bigger[isamp] = fC_sum; }
00681           else { HF_old[isamp] = fC_sum; }  // overwrite old sample, adding new info     
00682 
00683         }
00684         if(usenew) HF_old = HF_bigger; // save new, larger sized sample in "old" slot
00685       
00686       }
00687       else {
00688         if(formerID>0) {
00689           // make new digi
00690           HFdigis->push_back(HFDataFrame(formerID));      
00691 
00692           // set up information to convert back
00693 
00694           HcalDetId cell = HF_old.id();
00695           const HcalQIECoder* channelCoder = conditions->getHcalCoder (cell);
00696           HcalCoderDb coder (*channelCoder, *shape);
00697 
00698           unsigned int sizeold = HF_old.size();
00699           for(unsigned int isamp = 0; isamp<sizeold; isamp++) {
00700             coder.fC2adc(HF_old,(HFdigis->back()), 0 );   // as per simulation, capid=0???
00701           }
00702         }
00703         //save pointers for next iteration                                                                 
00704         formerID = currentID;
00705         HF_old = iHF->second;
00706       }
00707 
00708       iHFchk = iHF;
00709       if((++iHFchk) == HFDigiStorage_.end()) {  //make sure not to lose the last one                         
00710           // make new digi
00711           HFdigis->push_back(HFDataFrame(currentID));     
00712 
00713           // set up information to convert back
00714 
00715           HcalDetId cell = (iHF->second).id();
00716           const HcalQIECoder* channelCoder = conditions->getHcalCoder (cell);
00717           HcalCoderDb coder (*channelCoder, *shape);
00718 
00719           unsigned int sizeold = (iHF->second).size();
00720           for(unsigned int isamp = 0; isamp<sizeold; isamp++) {
00721             coder.fC2adc(HF_old,(HFdigis->back()), 0 );   // as per simulation, capid=0???
00722           }
00723 
00724       }
00725     }
00726 
00727 
00728     // ZDC next...
00729 
00730     // loop over the maps we have, re-making individual hits or digis if necessary.
00731     formerID = 0;
00732     CaloSamples ZDC_old;
00733 
00734     ZDCDigiMap::const_iterator iZDCchk;
00735 
00736     for(ZDCDigiMap::const_iterator iZDC  = ZDCDigiStorage_.begin();
00737         iZDC != ZDCDigiStorage_.end(); ++iZDC) {
00738 
00739       currentID = iZDC->first; 
00740 
00741       if (currentID == formerID) { // we have to add these digis together
00742 
00743         //loop over digi samples in each CaloSample                                                           
00744         unsigned int sizenew = (iZDC->second).size();
00745         unsigned int sizeold = ZDC_old.size();
00746 
00747         unsigned int max_samp = std::max(sizenew, sizeold);
00748 
00749         CaloSamples ZDC_bigger(currentID,max_samp);
00750 
00751         bool usenew = false;
00752 
00753         if(sizenew > sizeold) usenew = true;
00754 
00755         // samples from different events can be of different lengths - sum all                               
00756         // that overlap.                                                                                     
00757 
00758         for(unsigned int isamp = 0; isamp<max_samp; isamp++) {
00759           if(isamp < sizenew) {
00760             fC_new = (iZDC->second)[isamp];
00761           }
00762           else { fC_new = 0;}
00763 
00764           if(isamp < sizeold) {
00765             fC_old = ZDC_old[isamp];
00766           }
00767           else { fC_old = 0;}
00768 
00769           // add values                                                                                      
00770           fC_sum = fC_new + fC_old;
00771 
00772           if(usenew) {ZDC_bigger[isamp] = fC_sum; }
00773           else { ZDC_old[isamp] = fC_sum; }  // overwrite old sample, adding new info     
00774 
00775         }
00776         if(usenew) ZDC_old = ZDC_bigger; // save new, larger sized sample in "old" slot
00777       
00778       }
00779       else {
00780         if(formerID>0) {
00781           // make new digi
00782           ZDCdigis->push_back(ZDCDataFrame(formerID));    
00783 
00784           // set up information to convert back
00785 
00786           HcalDetId cell = ZDC_old.id();
00787           const HcalQIECoder* channelCoder = conditions->getHcalCoder (cell);
00788           HcalCoderDb coder (*channelCoder, *shape);
00789 
00790           unsigned int sizeold = ZDC_old.size();
00791           for(unsigned int isamp = 0; isamp<sizeold; isamp++) {
00792             coder.fC2adc(ZDC_old,(ZDCdigis->back()), 0 );   // as per simulation, capid=0???
00793           }
00794         }
00795         //save pointers for next iteration                                                                 
00796         formerID = currentID;
00797         ZDC_old = iZDC->second;
00798       }
00799 
00800       iZDCchk = iZDC;
00801       if((++iZDCchk) == ZDCDigiStorage_.end()) {  //make sure not to lose the last one                         
00802           // make new digi
00803           ZDCdigis->push_back(ZDCDataFrame(currentID));   
00804 
00805           // set up information to convert back
00806 
00807           HcalDetId cell = (iZDC->second).id();
00808           const HcalQIECoder* channelCoder = conditions->getHcalCoder (cell);
00809           HcalCoderDb coder (*channelCoder, *shape);
00810 
00811           unsigned int sizeold = (iZDC->second).size();
00812           for(unsigned int isamp = 0; isamp<sizeold; isamp++) {
00813             coder.fC2adc(ZDC_old,(ZDCdigis->back()), 0 );   // as per simulation, capid=0???
00814           }
00815 
00816       }
00817     }
00818 
00819 
00820   
00821    //done merging
00822 
00823     // put the collection of recunstructed hits in the event   
00824     LogInfo("DataMixingHcalDigiWorker") << "total # HBHE Merged digis: " << HBHEdigis->size() ;
00825     LogInfo("DataMixingHcalDigiWorker") << "total # HO Merged digis: " << HOdigis->size() ;
00826     LogInfo("DataMixingHcalDigiWorker") << "total # HF Merged digis: " << HFdigis->size() ;
00827     LogInfo("DataMixingHcalDigiWorker") << "total # ZDC Merged digis: " << ZDCdigis->size() ;
00828 
00829     e.put( HBHEdigis, HBHEDigiCollectionDM_ );
00830     e.put( HOdigis, HODigiCollectionDM_ );
00831     e.put( HFdigis, HFDigiCollectionDM_ );
00832     e.put( ZDCdigis, ZDCDigiCollectionDM_ );
00833 
00834     // clear local storage after this event
00835     HBHEDigiStorage_.clear();
00836     HODigiStorage_.clear();
00837     HFDigiStorage_.clear();
00838     ZDCDigiStorage_.clear();
00839 
00840   }
00841 
00842 } //edm