00001
00002
00003
00004
00005
00006
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 #include "FWCore/Utilities/interface/EDMException.h"
00009 #include "FWCore/Utilities/interface/Algorithms.h"
00010 #include "FWCore/Framework/interface/TriggerNamesService.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 #include "SimDataFormats/CrossingFrame/interface/CrossingFramePlaybackInfoExtended.h"
00016 #include "FWCore/Utilities/interface/TypeID.h"
00017 #include "MixingModule.h"
00018 #include "MixingWorker.h"
00019
00020 #include "FWCore/Services/src/Memory.h"
00021 using namespace std;
00022
00023 namespace edm
00024 {
00025
00026
00027 MixingModule::MixingModule(const edm::ParameterSet& ps_mix) :
00028 BMixingModule(ps_mix),
00029 labelPlayback_(ps_mix.getParameter<std::string>("LabelPlayback")),
00030 mixProdStep2_(ps_mix.getParameter<bool>("mixProdStep2")),
00031 mixProdStep1_(ps_mix.getParameter<bool>("mixProdStep1"))
00032 {
00033 if (!mixProdStep1_ && !mixProdStep2_) LogInfo("MixingModule") << " The MixingModule was run in the Standard mode.";
00034 if (mixProdStep1_) LogInfo("MixingModule") << " The MixingModule was run in the Step1 mode. It produces a mixed secondary source.";
00035 if (mixProdStep2_) LogInfo("MixingModule") << " The MixingModule was run in the Step2 mode. It uses a mixed secondary source.";
00036
00037 useCurrentProcessOnly_=false;
00038 if (ps_mix.exists("useCurrentProcessOnly")) {
00039 useCurrentProcessOnly_=ps_mix.getParameter<bool>("useCurrentProcessOnly");
00040 LogInfo("MixingModule") <<" using given Parameter 'useCurrentProcessOnly' ="<<useCurrentProcessOnly_;
00041 }
00042 if (labelPlayback_.size()>0){
00043 sel_=new Selector( ModuleLabelSelector(labelPlayback_));
00044 }
00045 else {
00046 sel_=new Selector( MatchAllSelector());
00047 }
00048
00049 ParameterSet ps=ps_mix.getParameter<ParameterSet>("mixObjects");
00050 std::vector<std::string> names = ps.getParameterNames();
00051 for (std::vector<string>::iterator it=names.begin();it!= names.end();++it)
00052 {
00053 ParameterSet pset=ps.getParameter<ParameterSet>((*it));
00054 if (!pset.exists("type")) continue;
00055 std::string object = pset.getParameter<std::string>("type");
00056 std::vector<InputTag> tags=pset.getParameter<std::vector<InputTag> >("input");
00057
00058 if (mixProdStep2_){
00059
00060 if (object=="SimTrackPCrossingFrame") {
00061 InputTag tag;
00062 if (tags.size()>0) tag=tags[0];
00063 std::string label;
00064 branchesActivate(TypeID(typeid(PCrossingFrame<SimTrack>)).friendlyClassName(),std::string(""),tag,label);
00065
00066 LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00067
00068
00069
00070
00071 if (find(names.begin(), names.end(), "mixTracks") != names.end())
00072 {
00073 edm::ParameterSet psin=ps.getParameter<edm::ParameterSet>("mixTracks");
00074 if (!psin.exists("type")) continue;
00075 std::string object = psin.getParameter<std::string>("type");
00076 std::vector<InputTag> tags=psin.getParameter<std::vector<InputTag> >("input");
00077
00078 InputTag tagCF;
00079 if (tags.size()>0) tagCF=tags[0];
00080 std::string labelCF;
00081
00082 branchesActivate(TypeID(typeid(std::vector<SimTrack>)).friendlyClassName(),std::string(""),tagCF,labelCF);
00083 workersObjects_.push_back(new MixingWorker<SimTrack>(minBunch_,maxBunch_,bunchSpace_,std::string(""),label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));
00084
00085 produces<CrossingFrame<SimTrack> >(labelCF);
00086 }
00087
00088
00089 }else if (object=="SimVertexPCrossingFrame") {
00090 InputTag tag;
00091 if (tags.size()>0) tag=tags[0];
00092 std::string label;
00093 branchesActivate(TypeID(typeid(PCrossingFrame<SimVertex>)).friendlyClassName(),std::string(""),tag,label);
00094
00095 LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag "<<tag.encode()<<", label will be "<<label;
00096
00097
00098
00099
00100 if (find(names.begin(), names.end(), "mixVertices") != names.end())
00101 {
00102 edm::ParameterSet psin=ps.getParameter<edm::ParameterSet>("mixVertices");
00103 if (!psin.exists("type")) continue;
00104 std::string object = psin.getParameter<std::string>("type");
00105 std::vector<InputTag> tags=psin.getParameter<std::vector<InputTag> >("input");
00106
00107 InputTag tagCF;
00108 if (tags.size()>0) tagCF=tags[0];
00109 std::string labelCF;
00110
00111 branchesActivate(TypeID(typeid(std::vector<SimVertex>)).friendlyClassName(),std::string(""),tagCF,labelCF);
00112 workersObjects_.push_back(new MixingWorker<SimVertex>(minBunch_,maxBunch_,bunchSpace_,std::string(""),label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));
00113
00114 produces<CrossingFrame<SimVertex> >(labelCF);
00115 }
00116
00117 }
00118
00119 else if (object=="HepMCProductPCrossingFrame") {
00120 InputTag tag;
00121 if (tags.size()>0) tag=tags[0];
00122 std::string label;
00123 branchesActivate(TypeID(typeid(PCrossingFrame<HepMCProduct>)).friendlyClassName(),std::string(""),tag,label);
00124
00125 LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00126
00127
00128
00129
00130 if (find(names.begin(), names.end(), "mixHepMC") != names.end())
00131 {
00132 edm::ParameterSet psin=ps.getParameter<edm::ParameterSet>("mixHepMC");
00133 if (!psin.exists("type")) continue;
00134 std::string object = psin.getParameter<std::string>("type");
00135 std::vector<InputTag> tags=psin.getParameter<std::vector<InputTag> >("input");
00136
00137 InputTag tagCF;
00138 if (tags.size()>0) tagCF=tags[0];
00139 std::string labelCF;
00140
00141 branchesActivate(TypeID(typeid(HepMCProduct)).friendlyClassName(),std::string(""),tagCF,labelCF);
00142 workersObjects_.push_back(new MixingWorker<HepMCProduct>(minBunch_,maxBunch_,bunchSpace_,std::string(""),label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));
00143
00144 produces<CrossingFrame<HepMCProduct> >(labelCF);
00145 }
00146
00147 }else if (object=="PCaloHitPCrossingFrame") {
00148 std::vector<std::string> subdets=pset.getParameter<std::vector<std::string> >("subdets");
00149 for (unsigned int ii=0;ii<subdets.size();ii++) {
00150 InputTag tag;
00151 if (tags.size()==1) tag=tags[0];
00152 else if(tags.size()>1) tag=tags[ii];
00153 std::string label;
00154 branchesActivate(TypeID(typeid(PCrossingFrame<PCaloHit>)).friendlyClassName(),subdets[ii],tag,label);
00155
00156 LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00157
00158
00159
00160
00161 if (find(names.begin(), names.end(), "mixCH") != names.end())
00162 {
00163 edm::ParameterSet psin=ps.getParameter<edm::ParameterSet>("mixCH");
00164 if (!psin.exists("type")) continue;
00165 std::string object = psin.getParameter<std::string>("type");
00166 std::vector<InputTag> tags=psin.getParameter<std::vector<InputTag> >("input");
00167
00168 InputTag tagCF;
00169 if (tags.size()==1) tagCF=tags[0];
00170 else if(tags.size()>1) tagCF=tags[ii];
00171 std::string labelCF;
00172
00173 branchesActivate(TypeID(typeid(std::vector<PCaloHit>)).friendlyClassName(),subdets[ii],tagCF,labelCF);
00174 workersObjects_.push_back(new MixingWorker<PCaloHit>(minBunch_,maxBunch_,bunchSpace_,subdets[ii],label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));
00175
00176 produces<CrossingFrame<PCaloHit> > (labelCF);
00177 }
00178
00179 }
00180
00181 }else if (object=="PSimHitPCrossingFrame") {
00182 std::vector<std::string> subdets=pset.getParameter<std::vector<std::string> >("subdets");
00183 for (unsigned int ii=0;ii<subdets.size();ii++) {
00184 InputTag tag;
00185 if (tags.size()==1) tag=tags[0];
00186 else if(tags.size()>1) tag=tags[ii];
00187 std::string label;
00188 branchesActivate(TypeID(typeid(PCrossingFrame<PSimHit>)).friendlyClassName(),subdets[ii],tag,label);
00189
00190 LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00191
00192
00193
00194
00195
00196 if (find(names.begin(), names.end(), "mixSH") != names.end())
00197 {
00198 edm::ParameterSet psin=ps.getParameter<edm::ParameterSet>("mixSH");
00199 if (!psin.exists("type")) continue;
00200 std::string object = psin.getParameter<std::string>("type");
00201 std::vector<InputTag> tags=psin.getParameter<std::vector<InputTag> >("input");
00202
00203 InputTag tagCF;
00204 if (tags.size()==1) tagCF=tags[0];
00205 else if(tags.size()>1) tagCF=tags[ii];
00206 std::string labelCF;
00207 branchesActivate(TypeID(typeid(std::vector<PSimHit>)).friendlyClassName(),subdets[ii],tagCF,labelCF);
00208 workersObjects_.push_back(new MixingWorker<PSimHit>(minBunch_,maxBunch_,bunchSpace_,subdets[ii],label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));
00209
00210 produces<CrossingFrame<PSimHit> > (labelCF);
00211 }
00212
00213 }
00214
00215 }
00216 }
00217
00218
00219 if (!mixProdStep2_){
00220
00221 InputTag tagCF = InputTag();
00222 std::string labelCF = " ";
00223
00224
00225 if (object=="SimTrack") {
00226 InputTag tag;
00227 if (tags.size()>0) tag=tags[0];
00228 std::string label;
00229
00230 branchesActivate(TypeID(typeid(std::vector<SimTrack>)).friendlyClassName(),std::string(""),tag,label);
00231 workersObjects_.push_back(new MixingWorker<SimTrack>(minBunch_,maxBunch_,bunchSpace_,std::string(""),label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));
00232
00233 produces<CrossingFrame<SimTrack> >(label);
00234 LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00235
00236
00237
00238 }else if (object=="SimVertex") {
00239 InputTag tag;
00240 if (tags.size()>0) tag=tags[0];
00241 std::string label;
00242
00243 branchesActivate(TypeID(typeid(std::vector<SimVertex>)).friendlyClassName(),std::string(""),tag,label);
00244
00245 workersObjects_.push_back(new MixingWorker<SimVertex>(minBunch_,maxBunch_,bunchSpace_,std::string(""),label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));
00246 produces<CrossingFrame<SimVertex> >(label);
00247 LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag "<<tag.encode()<<", label will be "<<label;
00248
00249
00250 }else if (object=="HepMCProduct") {
00251 InputTag tag;
00252 if (tags.size()>0) tag=tags[0];
00253 std::string label;
00254
00255 branchesActivate(TypeID(typeid(HepMCProduct)).friendlyClassName(),std::string(""),tag,label);
00256 workersObjects_.push_back(new MixingWorker<HepMCProduct>(minBunch_,maxBunch_,bunchSpace_,std::string(""),label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));
00257
00258 produces<CrossingFrame<HepMCProduct> >(label);
00259 LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00260
00261
00262
00263 }else if (object=="PCaloHit") {
00264 std::vector<std::string> subdets=pset.getParameter<std::vector<std::string> >("subdets");
00265 for (unsigned int ii=0;ii<subdets.size();ii++) {
00266 InputTag tag;
00267 if (tags.size()==1) tag=tags[0];
00268 else if(tags.size()>1) tag=tags[ii];
00269 std::string label;
00270
00271 branchesActivate(TypeID(typeid(std::vector<PCaloHit>)).friendlyClassName(),subdets[ii],tag,label);
00272 workersObjects_.push_back(new MixingWorker<PCaloHit>(minBunch_,maxBunch_,bunchSpace_,subdets[ii],label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));
00273
00274 produces<CrossingFrame<PCaloHit> > (label);
00275 LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00276
00277
00278 }
00279
00280 }else if (object=="PSimHit") {
00281 std::vector<std::string> subdets=pset.getParameter<std::vector<std::string> >("subdets");
00282 for (unsigned int ii=0;ii<subdets.size();ii++) {
00283 InputTag tag;
00284 if (tags.size()==1) tag=tags[0];
00285 else if(tags.size()>1) tag=tags[ii];
00286 std::string label;
00287
00288 branchesActivate(TypeID(typeid(std::vector<PSimHit>)).friendlyClassName(),subdets[ii],tag,label);
00289 workersObjects_.push_back(new MixingWorker<PSimHit>(minBunch_,maxBunch_,bunchSpace_,subdets[ii],label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));
00290
00291 produces<CrossingFrame<PSimHit> > (label);
00292 LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
00293
00294 }
00295 }
00296 else LogWarning("MixingModule") <<"You have asked to mix an unknown type of object("<<object<<").\n If you want to include it in mixing, please contact the authors of the MixingModule!";
00297 }
00298 }
00299
00300 sort_all(wantedBranches_);
00301 for (unsigned int branch=0;branch<wantedBranches_.size();++branch) LogDebug("MixingModule")<<"Will keep branch "<<wantedBranches_[branch]<<" for mixing ";
00302
00303 dropUnwantedBranches(wantedBranches_);
00304
00305 produces<PileupMixingContent>();
00306
00307 produces<CrossingFramePlaybackInfoExtended>();
00308 }
00309
00310 void MixingModule::reload(const edm::EventSetup & setup){
00311
00312 edm::ESHandle<MixingModuleConfig> config;
00313 setup.get<MixingRcd>().get(config);
00314 minBunch_=config->minBunch();
00315 maxBunch_=config->maxBunch();
00316 bunchSpace_=config->bunchSpace();
00317
00318
00319 for (unsigned int ii=0;ii<workersObjects_.size();ii++){
00320 workersObjects_[ii]->reload(setup);
00321 }
00322 }
00323
00324 void MixingModule::branchesActivate(const std::string &friendlyName, const std::string &subdet, InputTag &tag, std::string &label) {
00325
00326 label=tag.label()+tag.instance();
00327 wantedBranches_.push_back(friendlyName + '_' +
00328 tag.label() + '_' +
00329 tag.instance());
00330
00331
00332 if (useCurrentProcessOnly_) {
00333 const std::string processName = edm::Service<edm::service::TriggerNamesService>()->getProcessName();
00334 tag = InputTag(tag.label(),tag.instance(),processName);
00335 }
00336
00337 }
00338
00339
00340 void MixingModule::checkSignal(const edm::Event &e){
00341 if (workers_.size()==0){
00342 for (unsigned int ii=0;ii<workersObjects_.size();ii++){
00343 if (workersObjects_[ii]->checkSignal(e)){
00344 workers_.push_back(workersObjects_[ii]);
00345 }
00346 }
00347 }
00348 }
00349
00350
00351 void MixingModule::createnewEDProduct() {
00352
00353 playbackInfo_=new CrossingFramePlaybackInfoExtended(minBunch_,maxBunch_,maxNbSources_);
00354
00355
00356 for (unsigned int ii=0;ii<workers_.size();ii++){
00357 workers_[ii]->createnewEDProduct();
00358 }
00359 }
00360
00361
00362
00363 MixingModule::~MixingModule() {
00364 for (unsigned int ii=0;ii<workersObjects_.size();ii++){
00365 delete workersObjects_[ii];}
00366 delete sel_;
00367 }
00368
00369 void MixingModule::addSignals(const edm::Event &e, const edm::EventSetup& setup) {
00370
00371
00372 LogDebug("MixingModule")<<"===============> adding signals for "<<e.id();
00373 for (unsigned int ii=0;ii<workers_.size();ii++){
00374 workers_[ii]->addSignals(e);
00375 }
00376 }
00377
00378
00379 void MixingModule::pileAllWorkers(EventPrincipal const& eventPrincipal,
00380
00381 int bunchCrossing, int eventId,
00382 int& vertexOffset)
00383 {
00384 for (unsigned int ii=0;ii<workers_.size();ii++) {
00385 LogDebug("MixingModule") <<" merging Event: id " << eventPrincipal.id();
00386
00387
00388 workers_[ii]->addPileups(bunchCrossing,eventPrincipal,
00389
00390 eventId,vertexoffset);
00391 }
00392 }
00393
00394
00395 void MixingModule::doPileUp(edm::Event &e, const edm::EventSetup& setup)
00396 {
00397
00398 std::vector<edm::EventID> recordEventID;
00399 edm::Handle<CrossingFramePlaybackInfoExtended> playbackInfo_H;
00400 if (playback_) {
00401 bool got=e.get((*sel_), playbackInfo_H);
00402 if (!got) {
00403 throw cms::Exception("MixingProductNotFound") << " No "
00404 "CrossingFramePlaybackInfoExtended on the input file, but playback "
00405 "option set!!!!! Please change the input file if you really want "
00406 "playback!!!!!!" << endl;
00407 }
00408 }
00409
00410
00411
00412 std::vector<int> PileupList;
00413 PileupList.clear();
00414 TrueNumInteractions_.clear();
00415
00416 boost::shared_ptr<PileUp> source0 = inputSources_[0];
00417
00418 if( (source0 && source0->doPileUp() ) && !playback_ ) {
00419
00420
00421
00422
00423 source0->CalculatePileup(minBunch_, maxBunch_, PileupList, TrueNumInteractions_);
00424
00425 }
00426
00427
00428
00429
00430
00431 int KeepTrackOfPileup = 0;
00432
00433 for (int bunchIdx = minBunch_; bunchIdx <= maxBunch_; ++bunchIdx) {
00434 for ( size_t setBcrIdx=0; setBcrIdx<workers_.size(); setBcrIdx++) {
00435 workers_[setBcrIdx]->setBcrOffset();
00436 }
00437
00438 for ( size_t readSrcIdx=0; readSrcIdx<maxNbSources_; readSrcIdx++ ) {
00439 boost::shared_ptr<PileUp> source = inputSources_[readSrcIdx];
00440
00441
00442
00443
00444 for ( size_t setSrcIdx=0; setSrcIdx<workers_.size(); setSrcIdx++) {
00445 workers_[setSrcIdx]->setSourceOffset(readSrcIdx);
00446 }
00447 if (!source || !source->doPileUp()) continue;
00448
00449 int NumPU_Events = 0;
00450
00451 if(readSrcIdx ==0 && !playback_) { NumPU_Events = PileupList[bunchIdx - minBunch_];}
00452 else { NumPU_Events = 1;}
00453
00454
00455 int vertexOffset = 0;
00456
00457 if (!playback_) {
00458 inputSources_[readSrcIdx]->readPileUp(e.id(), recordEventID,
00459 boost::bind(&MixingModule::pileAllWorkers, boost::ref(*this), _1, bunchIdx,
00460 _2, vertexOffset), NumPU_Events
00461 );
00462 playbackInfo_->setStartEventId(recordEventID, readSrcIdx, bunchIdx, KeepTrackOfPileup);
00463 KeepTrackOfPileup+=NumPU_Events;
00464 } else {
00465 int dummyId = 0 ;
00466 const std::vector<edm::EventID>& playEventID =
00467 playbackInfo_H->getStartEventId(readSrcIdx, bunchIdx);
00468 if(readSrcIdx == 0) {
00469 PileupList.push_back(playEventID.size());
00470 TrueNumInteractions_.push_back(playEventID.size());
00471 }
00472 inputSources_[readSrcIdx]->playPileUp(
00473 playEventID,
00474 boost::bind(&MixingModule::pileAllWorkers, boost::ref(*this), _1, bunchIdx,
00475 dummyId, vertexOffset)
00476 );
00477 }
00478 }
00479 }
00480
00481
00482
00483 std::auto_ptr< PileupMixingContent > PileupMixing_;
00484
00485 std::vector<int> numInteractionList;
00486 std::vector<int> bunchCrossingList;
00487 std::vector<float> TrueInteractionList;
00488
00489
00490 for (int bunchCrossing=minBunch_;bunchCrossing<=maxBunch_;++bunchCrossing) {
00491 bunchCrossingList.push_back(bunchCrossing);
00492 if(!inputSources_[0] || !inputSources_[0]->doPileUp()) {
00493 numInteractionList.push_back(0);
00494 TrueInteractionList.push_back(0);
00495 }
00496 else {
00497 numInteractionList.push_back(PileupList[bunchCrossing-minBunch_]);
00498 TrueInteractionList.push_back((TrueNumInteractions_)[bunchCrossing-minBunch_]);
00499 }
00500 }
00501
00502
00503 PileupMixing_ = std::auto_ptr< PileupMixingContent >(new PileupMixingContent( bunchCrossingList,
00504 numInteractionList,
00505 TrueInteractionList));
00506
00507 e.put(PileupMixing_);
00508
00509
00510
00511 for (unsigned int ii=0;ii<workers_.size();ii++) {
00512
00513 if (!mixProdStep2_){
00514 workers_[ii]->setTof();
00515 }
00516 workers_[ii]->put(e);
00517 }
00518 }
00519
00520
00521 void MixingModule::put(edm::Event &e, const edm::EventSetup& setup) {
00522
00523 if (playbackInfo_) {
00524 std::auto_ptr<CrossingFramePlaybackInfoExtended> pOut(playbackInfo_);
00525 e.put(pOut);
00526 }
00527 }
00528
00529 }