00001
00002
00003
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 "DataMixingEMWorker.h"
00018
00019
00020 using namespace std;
00021
00022 namespace edm
00023 {
00024
00025
00026
00027 DataMixingEMWorker::DataMixingEMWorker() { sel_=0;}
00028
00029
00030 DataMixingEMWorker::DataMixingEMWorker(const edm::ParameterSet& ps) :
00031 label_(ps.getParameter<std::string>("Label"))
00032
00033 {
00034
00035
00036
00037
00038
00039 if (label_.size()>0){
00040 sel_=new Selector( ModuleLabelSelector(label_));
00041 }
00042 else {
00043 sel_=new Selector( MatchAllSelector());
00044 }
00045
00046
00047
00048 EBProducer_ = ps.getParameter<edm::InputTag>("EBProducer");
00049 EEProducer_ = ps.getParameter<edm::InputTag>("EEProducer");
00050 ESProducer_ = ps.getParameter<edm::InputTag>("ESProducer");
00051 EBrechitCollection_ = ps.getParameter<edm::InputTag>("EBrechitCollection");
00052 EErechitCollection_ = ps.getParameter<edm::InputTag>("EErechitCollection");
00053 ESrechitCollection_ = ps.getParameter<edm::InputTag>("ESrechitCollection");
00054 EBRecHitCollectionDM_ = ps.getParameter<std::string>("EBRecHitCollectionDM");
00055 EERecHitCollectionDM_ = ps.getParameter<std::string>("EERecHitCollectionDM");
00056 ESRecHitCollectionDM_ = ps.getParameter<std::string>("ESRecHitCollectionDM");
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 }
00068
00069
00070
00071 DataMixingEMWorker::~DataMixingEMWorker() {
00072 delete sel_;
00073 sel_=0;
00074 }
00075
00076 void DataMixingEMWorker::addEMSignals(const edm::Event &e) {
00077
00078
00079 LogInfo("DataMixingEMWorker")<<"===============> adding MC signals for "<<e.id();
00080
00081
00082
00083 Handle< EBRecHitCollection > pEBRecHits;
00084
00085 const EBRecHitCollection* EBRecHits = 0;
00086
00087 if(e.getByLabel(EBProducer_.label(),EBrechitCollection_.label(), pEBRecHits) ){
00088 EBRecHits = pEBRecHits.product();
00089 LogDebug("DataMixingEMWorker") << "total # EB rechits: " << EBRecHits->size();
00090 }
00091
00092
00093 if (EBRecHits)
00094 {
00095
00096 for(EBRecHitCollection::const_iterator it = EBRecHits->begin();
00097 it != EBRecHits->end(); ++it) {
00098
00099 EBRecHitStorage_.insert(EBRecHitMap::value_type( ( it->id() ), *it ));
00100
00101 LogDebug("DataMixingEMWorker") << "processed EBRecHit with rawId: "
00102 << it->id().rawId() << "\n"
00103 << " rechit energy: " << it->energy();
00104
00105 }
00106 }
00107
00108
00109
00110 Handle< EERecHitCollection > pEERecHits;
00111
00112 const EERecHitCollection* EERecHits = 0;
00113
00114
00115 if(e.getByLabel(EEProducer_.label(),EErechitCollection_.label(), pEERecHits) ){
00116 EERecHits = pEERecHits.product();
00117 LogDebug("DataMixingEMWorker") << "total # EE rechits: " << EERecHits->size();
00118 }
00119
00120
00121 if (EERecHits)
00122 {
00123
00124 for(EERecHitCollection::const_iterator it = EERecHits->begin();
00125 it != EERecHits->end(); ++it) {
00126
00127 EERecHitStorage_.insert(EERecHitMap::value_type( ( it->id() ), *it ));
00128 #ifdef DEBUG
00129 LogDebug("DataMixingEMWorker") << "processed EERecHit with rawId: "
00130 << it->id().rawId() << "\n"
00131 << " rechit energy: " << it->energy();
00132 #endif
00133
00134 }
00135 }
00136
00137
00138 Handle< ESRecHitCollection > pESRecHits;
00139
00140 const ESRecHitCollection* ESRecHits = 0;
00141
00142
00143 if(e.getByLabel( ESProducer_.label(),ESrechitCollection_.label(), pESRecHits) ){
00144 ESRecHits = pESRecHits.product();
00145 #ifdef DEBUG
00146 LogDebug("DataMixingEMWorker") << "total # ES rechits: " << ESRecHits->size();
00147 #endif
00148 }
00149
00150
00151 if (ESRecHits)
00152 {
00153
00154 for(ESRecHitCollection::const_iterator it = ESRecHits->begin();
00155 it != ESRecHits->end(); ++it) {
00156
00157 ESRecHitStorage_.insert(ESRecHitMap::value_type( ( it->id() ), *it ));
00158
00159 #ifdef DEBUG
00160 LogDebug("DataMixingEMWorker") << "processed ESRecHit with rawId: "
00161 << it->id().rawId() << "\n"
00162 << " rechit energy: " << it->energy();
00163 #endif
00164
00165 }
00166 }
00167
00168 }
00169
00170 void DataMixingEMWorker::addEMPileups(const int bcr, Event *e, unsigned int eventNr) {
00171
00172 LogInfo("DataMixingEMWorker") <<"\n===============> adding pileups from event "<<e->id()<<" for bunchcrossing "<<bcr;
00173
00174
00175
00176
00177
00178 Handle< EBRecHitCollection > pEBRecHits;
00179 const EBRecHitCollection* EBRecHits = 0;
00180
00181
00182 if( e->getByLabel(EBProducer_.label(),EBrechitCollection_.label(), pEBRecHits) ){
00183 EBRecHits = pEBRecHits.product();
00184 #ifdef DEBUG
00185 LogDebug("DataMixingEMWorker") << "total # EB rechits: " << EBRecHits->size();
00186 #endif
00187 }
00188
00189
00190 if (EBRecHits)
00191 {
00192
00193 for(EBRecHitCollection::const_iterator it = EBRecHits->begin();
00194 it != EBRecHits->end(); ++it) {
00195
00196 EBRecHitStorage_.insert(EBRecHitMap::value_type( (it->id()), *it ));
00197
00198 #ifdef DEBUG
00199 LogDebug("DataMixingEMWorker") << "processed EBRecHit with rawId: "
00200 << it->id().rawId() << "\n"
00201 << " rechit energy: " << it->energy();
00202 #endif
00203 }
00204 }
00205
00206
00207 Handle< EERecHitCollection > pEERecHits;
00208 const EERecHitCollection* EERecHits = 0;
00209
00210
00211 if( e->getByLabel( EEProducer_.label(),EErechitCollection_.label(), pEERecHits) ){
00212 EERecHits = pEERecHits.product();
00213 #ifdef DEBUG
00214 LogDebug("DataMixingEMWorker") << "total # EE rechits: " << EERecHits->size();
00215 #endif
00216 }
00217
00218
00219 if (EERecHits)
00220 {
00221
00222 for(EERecHitCollection::const_iterator it = EERecHits->begin();
00223 it != EERecHits->end(); ++it) {
00224
00225 EERecHitStorage_.insert(EERecHitMap::value_type( (it->id()), *it ));
00226
00227 #ifdef DEBUG
00228 LogDebug("DataMixingEMWorker") << "processed EERecHit with rawId: "
00229 << it->id().rawId() << "\n"
00230 << " rechit energy: " << it->energy();
00231 #endif
00232 }
00233 }
00234
00235
00236 Handle< ESRecHitCollection > pESRecHits;
00237 const ESRecHitCollection* ESRecHits = 0;
00238
00239
00240 if( e->getByLabel( ESProducer_.label(),ESrechitCollection_.label(), pESRecHits) ){
00241 ESRecHits = pESRecHits.product();
00242 #ifdef DEBUG
00243 LogDebug("DataMixingEMWorker") << "total # ES rechits: " << ESRecHits->size();
00244 #endif
00245 }
00246
00247
00248 if (ESRecHits)
00249 {
00250
00251 for(ESRecHitCollection::const_iterator it = ESRecHits->begin();
00252 it != ESRecHits->end(); ++it) {
00253
00254 ESRecHitStorage_.insert(ESRecHitMap::value_type( (it->id()), *it ));
00255
00256 #ifdef DEBUG
00257 LogDebug("DataMixingEMWorker") << "processed ESRecHit with rawId: "
00258 << it->id().rawId() << "\n"
00259 << " rechit energy: " << it->energy();
00260 #endif
00261 }
00262 }
00263
00264 }
00265
00266 void DataMixingEMWorker::putEM(edm::Event &e) {
00267
00268
00269 std::auto_ptr< EBRecHitCollection > EBrechits( new EBRecHitCollection );
00270 std::auto_ptr< EERecHitCollection > EErechits( new EERecHitCollection );
00271 std::auto_ptr< ESRecHitCollection > ESrechits( new ESRecHitCollection );
00272
00273
00274 DetId formerID = 0;
00275 DetId currentID;
00276 float ESum = 0.;
00277 float EBTime = 0.;
00278 EcalRecHit OldHit;
00279 int nmatch=0;
00280
00281
00282
00283 EBRecHitMap::const_iterator iEBchk;
00284
00285 for(EBRecHitMap::const_iterator iEB = EBRecHitStorage_.begin();
00286 iEB != EBRecHitStorage_.end(); ++iEB) {
00287
00288 currentID = iEB->first;
00289
00290 if (currentID == formerID) {
00291 nmatch++;
00292 ESum+=(iEB->second).energy();
00293
00294 iEBchk = iEB;
00295 if((iEBchk++) == EBRecHitStorage_.end()) {
00296 EcalRecHit aHit(formerID, ESum, EBTime);
00297 EBrechits->push_back( aHit );
00298
00299 ESum = 0 ;
00300 nmatch=0;
00301 }
00302 }
00303 else {
00304 if(nmatch>0) {
00305 EcalRecHit aHit(formerID, ESum, EBTime);
00306 EBrechits->push_back( aHit );
00307
00308 ESum = 0 ;
00309 nmatch=0;
00310 }
00311 else {
00312 if(formerID>0) EBrechits->push_back( OldHit );
00313 }
00314
00315 iEBchk = iEB;
00316 if((iEBchk++) == EBRecHitStorage_.end()) {
00317 EBrechits->push_back( iEB->second );
00318 }
00319
00320
00321 OldHit = iEB->second;
00322 formerID = currentID;
00323 ESum = (iEB->second).energy();
00324 EBTime = (iEB->second).time();
00325 }
00326 }
00327
00328
00329
00330
00331 formerID = 0;
00332 ESum = 0.;
00333 float EETime = 0.;
00334
00335 nmatch=0;
00336
00337 EERecHitMap::const_iterator iEEchk;
00338
00339 for(EERecHitMap::const_iterator iEE = EERecHitStorage_.begin();
00340 iEE != EERecHitStorage_.end(); ++iEE) {
00341
00342 currentID = iEE->first;
00343
00344 if (currentID == formerID) {
00345 nmatch++;
00346 ESum+=(iEE->second).energy();
00347
00348 iEEchk = iEE;
00349 if((iEEchk++) == EERecHitStorage_.end()) {
00350 EcalRecHit aHit(formerID, ESum, EETime);
00351 EErechits->push_back( aHit );
00352
00353 ESum = 0 ;
00354 nmatch=0;
00355 }
00356 }
00357 else {
00358 if(nmatch>0) {
00359 EcalRecHit aHit(formerID, ESum, EETime);
00360 EErechits->push_back( aHit );
00361
00362 ESum = 0 ;
00363 nmatch=0;
00364 }
00365 else {
00366 if(formerID>0) EErechits->push_back( OldHit );
00367 }
00368
00369 iEEchk = iEE;
00370 if((iEEchk++) == EERecHitStorage_.end()) {
00371 EErechits->push_back( iEE->second );
00372 }
00373
00374
00375 OldHit = iEE->second;
00376 formerID = currentID;
00377 ESum = (iEE->second).energy();
00378 EETime = (iEE->second).time();
00379 }
00380 }
00381
00382
00383
00384
00385 formerID = 0;
00386 ESum = 0.;
00387 float ESTime = 0.;
00388 nmatch=0;
00389
00390 ESRecHitMap::const_iterator iESchk;
00391
00392 for(ESRecHitMap::const_iterator iES = ESRecHitStorage_.begin();
00393 iES != ESRecHitStorage_.end(); ++iES) {
00394
00395 currentID = iES->first;
00396
00397 if (currentID == formerID) {
00398 nmatch++;
00399 ESum+=(iES->second).energy();
00400
00401 iESchk = iES;
00402 if((iESchk++) == ESRecHitStorage_.end()) {
00403 EcalRecHit aHit(formerID, ESum, ESTime);
00404 ESrechits->push_back( aHit );
00405
00406 ESum = 0 ;
00407 nmatch=0;
00408 }
00409 }
00410 else {
00411 if(nmatch>0) {
00412 EcalRecHit aHit(formerID, ESum, ESTime);
00413 ESrechits->push_back( aHit );
00414
00415 ESum = 0 ;
00416 nmatch=0;
00417 }
00418 else {
00419 if(formerID>0) ESrechits->push_back( OldHit );
00420 }
00421
00422 iESchk = iES;
00423 if((iESchk++) == ESRecHitStorage_.end()) {
00424 ESrechits->push_back( iES->second );
00425 }
00426
00427
00428 OldHit = iES->second;
00429 formerID = currentID;
00430 ESum = (iES->second).energy();
00431 ESTime = (iES->second).time();
00432 }
00433 }
00434
00435
00436 LogInfo("DataMixingEMWorker") << "total # EB Merged rechits: " << EBrechits->size() ;
00437 LogInfo("DataMixingEMWorker") << "total # EE Merged rechits: " << EErechits->size() ;
00438 LogInfo("DataMixingEMWorker") << "total # ES Merged rechits: " << ESrechits->size() ;
00439
00440 e.put( EBrechits, EBRecHitCollectionDM_ );
00441 e.put( EErechits, EERecHitCollectionDM_ );
00442 e.put( ESrechits, ESRecHitCollectionDM_ );
00443
00444
00445
00446 EBRecHitStorage_.clear();
00447 EERecHitStorage_.clear();
00448 ESRecHitStorage_.clear();
00449
00450 }
00451
00452 }