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
00311 void MixingModule::branchesActivate(const std::string &friendlyName, const std::string &subdet, InputTag &tag, std::string &label) {
00312
00313 label=tag.label()+tag.instance();
00314 wantedBranches_.push_back(friendlyName + '_' +
00315 tag.label() + '_' +
00316 tag.instance());
00317
00318
00319 if (useCurrentProcessOnly_) {
00320 const std::string processName = edm::Service<edm::service::TriggerNamesService>()->getProcessName();
00321 tag = InputTag(tag.label(),tag.instance(),processName);
00322 }
00323
00324 }
00325
00326
00327 void MixingModule::checkSignal(const edm::Event &e){
00328 if (workers_.size()==0){
00329 for (unsigned int ii=0;ii<workersObjects_.size();ii++){
00330 if (workersObjects_[ii]->checkSignal(e)){
00331 workers_.push_back(workersObjects_[ii]);
00332 }
00333 }
00334 }
00335 }
00336
00337
00338 void MixingModule::createnewEDProduct() {
00339
00340 playbackInfo_=new CrossingFramePlaybackInfoExtended(minBunch_,maxBunch_,maxNbSources_);
00341
00342
00343 for (unsigned int ii=0;ii<workers_.size();ii++){
00344 workers_[ii]->createnewEDProduct();
00345 }
00346 }
00347
00348
00349
00350 MixingModule::~MixingModule() {
00351 for (unsigned int ii=0;ii<workersObjects_.size();ii++){
00352 delete workersObjects_[ii];}
00353 delete sel_;
00354 }
00355
00356 void MixingModule::addSignals(const edm::Event &e, const edm::EventSetup& setup) {
00357
00358
00359 LogDebug("MixingModule")<<"===============> adding signals for "<<e.id();
00360 for (unsigned int ii=0;ii<workers_.size();ii++){
00361 workers_[ii]->addSignals(e);
00362 }
00363 }
00364
00365
00366 void MixingModule::pileAllWorkers(EventPrincipal const& eventPrincipal,
00367
00368 int bunchCrossing, int eventId,
00369 int& vertexOffset)
00370 {
00371 for (unsigned int ii=0;ii<workers_.size();ii++) {
00372 LogDebug("MixingModule") <<" merging Event: id " << eventPrincipal.id();
00373
00374
00375 workers_[ii]->addPileups(bunchCrossing,eventPrincipal,
00376
00377 eventId,vertexoffset);
00378 }
00379 }
00380
00381
00382 void MixingModule::doPileUp(edm::Event &e, const edm::EventSetup& setup)
00383 {
00384
00385 std::vector<edm::EventID> recordEventID;
00386 edm::Handle<CrossingFramePlaybackInfoExtended> playbackInfo_H;
00387 if (playback_) {
00388 bool got=e.get((*sel_), playbackInfo_H);
00389 if (!got) {
00390 throw cms::Exception("MixingProductNotFound") << " No "
00391 "CrossingFramePlaybackInfoExtended on the input file, but playback "
00392 "option set!!!!! Please change the input file if you really want "
00393 "playback!!!!!!" << endl;
00394 }
00395 }
00396
00397
00398
00399 std::vector<int> PileupList;
00400 PileupList.clear();
00401 TrueNumInteractions_.clear();
00402
00403 boost::shared_ptr<PileUp> source0 = inputSources_[0];
00404
00405 if( (source0 && source0->doPileUp() ) && !playback_ ) {
00406
00407
00408
00409
00410 source0->CalculatePileup(minBunch_, maxBunch_, PileupList, TrueNumInteractions_);
00411
00412 }
00413
00414
00415
00416
00417
00418 int KeepTrackOfPileup = 0;
00419
00420 for (int bunchIdx = minBunch_; bunchIdx <= maxBunch_; ++bunchIdx) {
00421 for ( size_t setBcrIdx=0; setBcrIdx<workers_.size(); setBcrIdx++) {
00422 workers_[setBcrIdx]->setBcrOffset();
00423 }
00424
00425 for ( size_t readSrcIdx=0; readSrcIdx<maxNbSources_; readSrcIdx++ ) {
00426 boost::shared_ptr<PileUp> source = inputSources_[readSrcIdx];
00427
00428
00429
00430
00431 for ( size_t setSrcIdx=0; setSrcIdx<workers_.size(); setSrcIdx++) {
00432 workers_[setSrcIdx]->setSourceOffset(readSrcIdx);
00433 }
00434 if (!source || !source->doPileUp()) continue;
00435
00436 int NumPU_Events = 0;
00437
00438 if(readSrcIdx ==0 && !playback_) { NumPU_Events = PileupList[bunchIdx - minBunch_];}
00439 else { NumPU_Events = 1;}
00440
00441
00442 int vertexOffset = 0;
00443
00444 if (!playback_) {
00445 inputSources_[readSrcIdx]->readPileUp(recordEventID,
00446 boost::bind(&MixingModule::pileAllWorkers, boost::ref(*this), _1, bunchIdx,
00447 _2, vertexOffset), NumPU_Events
00448 );
00449 playbackInfo_->setStartEventId(recordEventID, readSrcIdx, bunchIdx, KeepTrackOfPileup);
00450 KeepTrackOfPileup+=NumPU_Events;
00451 } else {
00452 int dummyId = 0 ;
00453 const std::vector<edm::EventID>& playEventID =
00454 playbackInfo_H->getStartEventId(readSrcIdx, bunchIdx);
00455 if(readSrcIdx == 0) {
00456 PileupList.push_back(playEventID.size());
00457 TrueNumInteractions_.push_back(playEventID.size());
00458 }
00459 inputSources_[readSrcIdx]->playPileUp(
00460 playEventID,
00461 boost::bind(&MixingModule::pileAllWorkers, boost::ref(*this), _1, bunchIdx,
00462 dummyId, vertexOffset)
00463 );
00464 }
00465 }
00466 }
00467
00468
00469
00470 std::auto_ptr< PileupMixingContent > PileupMixing_;
00471
00472 std::vector<int> numInteractionList;
00473 std::vector<int> bunchCrossingList;
00474 std::vector<float> TrueInteractionList;
00475
00476
00477 for (int bunchCrossing=minBunch_;bunchCrossing<=maxBunch_;++bunchCrossing) {
00478 bunchCrossingList.push_back(bunchCrossing);
00479 if(!inputSources_[0] || !inputSources_[0]->doPileUp()) {
00480 numInteractionList.push_back(0);
00481 TrueInteractionList.push_back(0);
00482 }
00483 else {
00484 numInteractionList.push_back(PileupList[bunchCrossing-minBunch_]);
00485 TrueInteractionList.push_back((TrueNumInteractions_)[bunchCrossing-minBunch_]);
00486 }
00487 }
00488
00489
00490 PileupMixing_ = std::auto_ptr< PileupMixingContent >(new PileupMixingContent( bunchCrossingList,
00491 numInteractionList,
00492 TrueInteractionList));
00493
00494 e.put(PileupMixing_);
00495
00496
00497
00498 for (unsigned int ii=0;ii<workers_.size();ii++) {
00499
00500 if (!mixProdStep2_){
00501 workers_[ii]->setTof();
00502 }
00503 workers_[ii]->put(e);
00504 }
00505 }
00506
00507
00508 void MixingModule::put(edm::Event &e, const edm::EventSetup& setup) {
00509
00510 if (playbackInfo_) {
00511 std::auto_ptr<CrossingFramePlaybackInfoExtended> pOut(playbackInfo_);
00512 e.put(pOut);
00513 }
00514 }
00515
00516 }