CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MixingModule.cc
Go to the documentation of this file.
1 // File: MixingModule.cc
2 // Description: see MixingModule.h
3 // Author: Ursula Berthon, LLR Palaiseau
4 //
5 //--------------------------------------------
6 
17 #include "MixingModule.h"
18 #include "MixingWorker.h"
19 
21 using namespace std;
22 
23 namespace edm
24 {
25 
26  // Constructor
27  MixingModule::MixingModule(const edm::ParameterSet& ps_mix) :
28  BMixingModule(ps_mix),
29  labelPlayback_(ps_mix.getParameter<std::string>("LabelPlayback")),
30  mixProdStep2_(ps_mix.getParameter<bool>("mixProdStep2")),
31  mixProdStep1_(ps_mix.getParameter<bool>("mixProdStep1"))
32  {
33  if (!mixProdStep1_ && !mixProdStep2_) LogInfo("MixingModule") << " The MixingModule was run in the Standard mode.";
34  if (mixProdStep1_) LogInfo("MixingModule") << " The MixingModule was run in the Step1 mode. It produces a mixed secondary source.";
35  if (mixProdStep2_) LogInfo("MixingModule") << " The MixingModule was run in the Step2 mode. It uses a mixed secondary source.";
36 
38  if (ps_mix.exists("useCurrentProcessOnly")) {
39  useCurrentProcessOnly_=ps_mix.getParameter<bool>("useCurrentProcessOnly");
40  LogInfo("MixingModule") <<" using given Parameter 'useCurrentProcessOnly' ="<<useCurrentProcessOnly_;
41  }
42  if (labelPlayback_.size()>0){
44  }
45  else {
47  }
48 
49  ParameterSet ps=ps_mix.getParameter<ParameterSet>("mixObjects");
50  std::vector<std::string> names = ps.getParameterNames();
51  for (std::vector<string>::iterator it=names.begin();it!= names.end();++it)
52  {
54  if (!pset.exists("type")) continue; //to allow replacement by empty pset
55  std::string object = pset.getParameter<std::string>("type");
56  std::vector<InputTag> tags=pset.getParameter<std::vector<InputTag> >("input");
57 
58  if (mixProdStep2_){
59  //SimTracks
60  if (object=="SimTrackPCrossingFrame") {
61  InputTag tag;
62  if (tags.size()>0) tag=tags[0];
63  std::string label;
64  branchesActivate(TypeID(typeid(PCrossingFrame<SimTrack>)).friendlyClassName(),std::string(""),tag,label);
65 
66  LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
67 
68  //---------------------------------
69  // set an appropriate label for the CrossingFrame
70  if (find(names.begin(), names.end(), "mixTracks") != names.end())
71  {
72  edm::ParameterSet psin=ps.getParameter<edm::ParameterSet>("mixTracks");
73  if (!psin.exists("type")) continue; //to allow replacement by empty pset
74  std::string object = psin.getParameter<std::string>("type");
75  std::vector<InputTag> tags=psin.getParameter<std::vector<InputTag> >("input");
76 
77  InputTag tagCF;
78  if (tags.size()>0) tagCF=tags[0];
79  std::string labelCF;
80 
81  branchesActivate(TypeID(typeid(std::vector<SimTrack>)).friendlyClassName(),std::string(""),tagCF,labelCF);
82  workersObjects_.push_back(new MixingWorker<SimTrack>(minBunch_,maxBunch_,bunchSpace_,std::string(""),label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));
83 
84  produces<CrossingFrame<SimTrack> >(labelCF);
85  }
86  //--------------------------------
87 
88  }else if (object=="SimVertexPCrossingFrame") {
89  InputTag tag;
90  if (tags.size()>0) tag=tags[0];
91  std::string label;
92  branchesActivate(TypeID(typeid(PCrossingFrame<SimVertex>)).friendlyClassName(),std::string(""),tag,label);
93 
94  LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag "<<tag.encode()<<", label will be "<<label;
95 
96  //---------------------------------
97  // set an appropriate label for the CrossingFrame
98  if (find(names.begin(), names.end(), "mixVertices") != names.end())
99  {
100  edm::ParameterSet psin=ps.getParameter<edm::ParameterSet>("mixVertices");
101  if (!psin.exists("type")) continue; //to allow replacement by empty pset
102  std::string object = psin.getParameter<std::string>("type");
103  std::vector<InputTag> tags=psin.getParameter<std::vector<InputTag> >("input");
104 
105  InputTag tagCF;
106  if (tags.size()>0) tagCF=tags[0];
107  std::string labelCF;
108 
109  branchesActivate(TypeID(typeid(std::vector<SimVertex>)).friendlyClassName(),std::string(""),tagCF,labelCF);
110  workersObjects_.push_back(new MixingWorker<SimVertex>(minBunch_,maxBunch_,bunchSpace_,std::string(""),label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));
111 
112  produces<CrossingFrame<SimVertex> >(labelCF);
113  }
114  //---------------------------------
115  }
116 
117  else if (object=="HepMCProductPCrossingFrame") {
118  InputTag tag;
119  if (tags.size()>0) tag=tags[0];
120  std::string label;
121  branchesActivate(TypeID(typeid(PCrossingFrame<HepMCProduct>)).friendlyClassName(),std::string(""),tag,label);
122 
123  LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
124 
125  //---------------------------------
126  // set an appropriate label for the CrossingFrame
127  if (find(names.begin(), names.end(), "mixHepMC") != names.end())
128  {
129  edm::ParameterSet psin=ps.getParameter<edm::ParameterSet>("mixHepMC");
130  if (!psin.exists("type")) continue; //to allow replacement by empty pset
131  std::string object = psin.getParameter<std::string>("type");
132  std::vector<InputTag> tags=psin.getParameter<std::vector<InputTag> >("input");
133 
134  InputTag tagCF;
135  if (tags.size()>0) tagCF=tags[0];
136  std::string labelCF;
137 
138  branchesActivate(TypeID(typeid(HepMCProduct)).friendlyClassName(),std::string(""),tagCF,labelCF);
139  workersObjects_.push_back(new MixingWorker<HepMCProduct>(minBunch_,maxBunch_,bunchSpace_,std::string(""),label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));
140 
141  produces<CrossingFrame<HepMCProduct> >(labelCF);
142  }
143  //--------------------------------
144  }else if (object=="PCaloHitPCrossingFrame") {
145  std::vector<std::string> subdets=pset.getParameter<std::vector<std::string> >("subdets");
146  for (unsigned int ii=0;ii<subdets.size();ii++) {
147  InputTag tag;
148  if (tags.size()==1) tag=tags[0];
149  else if(tags.size()>1) tag=tags[ii];
150  std::string label;
151  branchesActivate(TypeID(typeid(PCrossingFrame<PCaloHit>)).friendlyClassName(),subdets[ii],tag,label);
152 
153  LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
154 
155  //---------------------------------
156  // set an appropriate label for the product CrossingFrame
157  if (find(names.begin(), names.end(), "mixCH") != names.end())
158  {
160  if (!psin.exists("type")) continue; //to allow replacement by empty pset
161  std::string object = psin.getParameter<std::string>("type");
162  std::vector<InputTag> tags=psin.getParameter<std::vector<InputTag> >("input");
163 
164  InputTag tagCF;
165  if (tags.size()==1) tagCF=tags[0];
166  else if(tags.size()>1) tagCF=tags[ii];
167  std::string labelCF;
168 
169  branchesActivate(TypeID(typeid(std::vector<PCaloHit>)).friendlyClassName(),subdets[ii],tagCF,labelCF);
170  workersObjects_.push_back(new MixingWorker<PCaloHit>(minBunch_,maxBunch_,bunchSpace_,subdets[ii],label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));
171 
172  produces<CrossingFrame<PCaloHit> > (labelCF);
173  }
174  //--------------------------------
175  }
176 
177  }else if (object=="PSimHitPCrossingFrame") {
178  std::vector<std::string> subdets=pset.getParameter<std::vector<std::string> >("subdets");
179  for (unsigned int ii=0;ii<subdets.size();ii++) {
180  InputTag tag;
181  if (tags.size()==1) tag=tags[0];
182  else if(tags.size()>1) tag=tags[ii];
183  std::string label;
184  branchesActivate(TypeID(typeid(PCrossingFrame<PSimHit>)).friendlyClassName(),subdets[ii],tag,label);
185 
186  LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
187 
188 
189  //---------------------------------
190  // set an appropriate label for the CrossingFrame
191  if (find(names.begin(), names.end(), "mixSH") != names.end())
192  {
194  if (!psin.exists("type")) continue; //to allow replacement by empty pset
195  std::string object = psin.getParameter<std::string>("type");
196  std::vector<InputTag> tags=psin.getParameter<std::vector<InputTag> >("input");
197 
198  InputTag tagCF;
199  if (tags.size()==1) tagCF=tags[0];
200  else if(tags.size()>1) tagCF=tags[ii];
201  std::string labelCF;
202  branchesActivate(TypeID(typeid(std::vector<PSimHit>)).friendlyClassName(),subdets[ii],tagCF,labelCF);
203  workersObjects_.push_back(new MixingWorker<PSimHit>(minBunch_,maxBunch_,bunchSpace_,subdets[ii],label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));
204 
205  produces<CrossingFrame<PSimHit> > (labelCF);
206  }
207  //-------------------------------
208  }
209 
210  }
211  }//mixProdStep2
212 
213 
214  if (!mixProdStep2_){
215 
216  InputTag tagCF = InputTag();
217  std::string labelCF = " ";
218 
219  //SimTracks
220  if (object=="SimTrack") {
221  InputTag tag;
222  if (tags.size()>0) tag=tags[0];
223  std::string label;
224 
225  branchesActivate(TypeID(typeid(std::vector<SimTrack>)).friendlyClassName(),std::string(""),tag,label);
226  workersObjects_.push_back(new MixingWorker<SimTrack>(minBunch_,maxBunch_,bunchSpace_,std::string(""),label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));
227 
228  produces<CrossingFrame<SimTrack> >(label);
229  LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
230 
231 
232  }else if (object=="SimVertex") {
233  InputTag tag;
234  if (tags.size()>0) tag=tags[0];
235  std::string label;
236 
237  branchesActivate(TypeID(typeid(std::vector<SimVertex>)).friendlyClassName(),std::string(""),tag,label);
238 
239  workersObjects_.push_back(new MixingWorker<SimVertex>(minBunch_,maxBunch_,bunchSpace_,std::string(""),label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));
240  produces<CrossingFrame<SimVertex> >(label);
241  LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag "<<tag.encode()<<", label will be "<<label;
242 
243  }else if (object=="HepMCProduct") {
244  InputTag tag;
245  if (tags.size()>0) tag=tags[0];
246  std::string label;
247 
248  branchesActivate(TypeID(typeid(HepMCProduct)).friendlyClassName(),std::string(""),tag,label);
249  workersObjects_.push_back(new MixingWorker<HepMCProduct>(minBunch_,maxBunch_,bunchSpace_,std::string(""),label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));
250 
251  produces<CrossingFrame<HepMCProduct> >(label);
252  LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
253 
254 
255  }else if (object=="PCaloHit") {
256  std::vector<std::string> subdets=pset.getParameter<std::vector<std::string> >("subdets");
257  for (unsigned int ii=0;ii<subdets.size();ii++) {
258  InputTag tag;
259  if (tags.size()==1) tag=tags[0];
260  else if(tags.size()>1) tag=tags[ii];
261  std::string label;
262 
263  branchesActivate(TypeID(typeid(std::vector<PCaloHit>)).friendlyClassName(),subdets[ii],tag,label);
264  workersObjects_.push_back(new MixingWorker<PCaloHit>(minBunch_,maxBunch_,bunchSpace_,subdets[ii],label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));
265 
266  produces<CrossingFrame<PCaloHit> > (label);
267  LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
268 
269  }
270 
271  }else if (object=="PSimHit") {
272  std::vector<std::string> subdets=pset.getParameter<std::vector<std::string> >("subdets");
273  for (unsigned int ii=0;ii<subdets.size();ii++) {
274  InputTag tag;
275  if (tags.size()==1) tag=tags[0];
276  else if(tags.size()>1) tag=tags[ii];
277  std::string label;
278 
279  branchesActivate(TypeID(typeid(std::vector<PSimHit>)).friendlyClassName(),subdets[ii],tag,label);
280  workersObjects_.push_back(new MixingWorker<PSimHit>(minBunch_,maxBunch_,bunchSpace_,subdets[ii],label,labelCF,maxNbSources_,tag,tagCF,mixProdStep2_));
281 
282  produces<CrossingFrame<PSimHit> > (label);
283  LogInfo("MixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
284  }
285  }
286  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!";
287  }//if for mixProdStep2
288  }//while over the mixObjects parameters
289 
291  for (unsigned int branch=0;branch<wantedBranches_.size();++branch) LogDebug("MixingModule")<<"Will keep branch "<<wantedBranches_[branch]<<" for mixing ";
292 
294 
295  produces<PileupMixingContent>();
296 
297  produces<CrossingFramePlaybackInfoExtended>();
298  }
299 
300 
301  void MixingModule::branchesActivate(const std::string &friendlyName, const std::string &subdet, InputTag &tag, std::string &label) {
302 
303  label=tag.label()+tag.instance();
304  wantedBranches_.push_back(friendlyName + '_' +
305  tag.label() + '_' +
306  tag.instance());
307 
308  //if useCurrentProcessOnly, we have to change the input tag
310  const std::string processName = edm::Service<edm::service::TriggerNamesService>()->getProcessName();
311  tag = InputTag(tag.label(),tag.instance(),processName);
312  }
313 
314  }
315 
316 
318  if (workers_.size()==0){
319  for (unsigned int ii=0;ii<workersObjects_.size();ii++){
320  if (workersObjects_[ii]->checkSignal(e)){
321  workers_.push_back(workersObjects_[ii]);
322  }
323  }
324  }
325  }
326 
327 
329  //create playback info
331 
332  //and CrossingFrames
333  for (unsigned int ii=0;ii<workers_.size();ii++){
334  workers_[ii]->createnewEDProduct();
335  }
336  }
337 
338 
339  // Virtual destructor needed.
341  for (unsigned int ii=0;ii<workersObjects_.size();ii++){
342  delete workersObjects_[ii];}
343  delete sel_;
344  }
345 
346  void MixingModule::addSignals(const edm::Event &e, const edm::EventSetup& setup) {
347  // fill in signal part of CrossingFrame
348 
349  LogDebug("MixingModule")<<"===============> adding signals for "<<e.id();
350  for (unsigned int ii=0;ii<workers_.size();ii++){
351  workers_[ii]->addSignals(e);
352  }
353  }
354 
356  { // we first loop over workers
357  // in order not to keep all CrossingFrames in memory simultaneously
358  //
359  for (unsigned int ii=0;ii<workers_.size();ii++) {
360  // we have to loop over bunchcrossings first since added objects are all stored in one vector,
361  // ordered by bunchcrossing
362  for (int bunchCrossing=minBunch_;bunchCrossing<=maxBunch_;++bunchCrossing) {
363  workers_[ii]->setBcrOffset();
364  for (unsigned int isource=0;isource<maxNbSources_;++isource) {
365  workers_[ii]->setSourceOffset(isource);
366  if (doit_[isource]) {
367  merge(bunchCrossing, (pileup_[isource])[bunchCrossing-minBunch_],ii,setup);
368  }
369  }
370  }
371  }
372 
373  std::auto_ptr< PileupMixingContent > PileupMixing_;
374 
375  std::vector<int> bunchCrossingList;
376  std::vector<int> numInteractionList;
377 
378  //Makin' a list:
379  for (int bunchCrossing=minBunch_;bunchCrossing<=maxBunch_;++bunchCrossing) {
380  bunchCrossingList.push_back(bunchCrossing);
381  if(!doit_[0]) {
382  numInteractionList.push_back(0);
383  }
384  else {
385  numInteractionList.push_back(((pileup_[0])[bunchCrossing-minBunch_]).size());
386  }
387  }
388 
389 
390  PileupMixing_ = std::auto_ptr< PileupMixingContent >(new PileupMixingContent( bunchCrossingList,
391  numInteractionList));
392 
393  e.put(PileupMixing_);
394 
395 
396  // we have to do the ToF transformation for PSimHits once all pileup has been added
397  for (unsigned int ii=0;ii<workers_.size();ii++) {
398  //not apply setTof in Step2 mode because it was done in the Step1
399  if (!mixProdStep2_){
400  workers_[ii]->setTof();
401  }
402  workers_[ii]->put(e);
403  }
404  }
405 
406  void MixingModule::addPileups(const int bcr, EventPrincipal *ep, unsigned int eventNr,unsigned int worker, const edm::EventSetup& setup) {
407  // fill in pileup part of CrossingFrame
408  LogDebug("MixingModule") <<"\n===============> adding objects from event "<<ep->id()<<" for bunchcrossing "<<bcr;
409 
410  workers_[worker]->addPileups(bcr,ep,eventNr,vertexoffset);
411  }
412 
413  void MixingModule::setEventStartInfo(const unsigned int s) {
415  }
416 
418 
419  if (playbackInfo_) {
420  std::auto_ptr<CrossingFramePlaybackInfoExtended> pOut(playbackInfo_);
421  e.put(pOut);
422  }
423  }
424 
425  void MixingModule::getEventStartInfo(edm::Event & e, const unsigned int s) {
426  if (playback_) {
427 
429  bool got=e.get((*sel_), playbackInfo_H);
430  if (got) {
431  playbackInfo_H->getEventStartInfo(vectorEventIDs_,s);
432  }else{
433  throw cms::Exception("MixingProductNotFound") << " No CrossingFramePlaybackInfoExtended on the input file, but playback option set!!!!! Please change the input file if you really want playback!!!!!!" << endl;
434  }
435  }
436  }
437 }//edm
#define LogDebug(id)
Selector * sel_
Definition: MixingModule.h:72
T getParameter(std::string const &) const
virtual ~MixingModule()
void setEventStartInfo(std::vector< std::vector< edm::EventID > > &id, const unsigned int s)
static int vertexoffset
Definition: BMixingModule.h:66
const std::string & label
Definition: MVAComputer.cc:186
virtual void createnewEDProduct()
std::vector< EventPrincipalVector > pileup_[4]
Definition: BMixingModule.h:81
virtual void setEventStartInfo(const unsigned int s)
virtual void addSignals(const edm::Event &e, const edm::EventSetup &es)
EventID const & id() const
bool exists(std::string const &parameterName) const
checks if a parameter exists
std::vector< std::vector< edm::EventID > > vectorEventIDs_
Definition: BMixingModule.h:76
void merge(const int bcr, const EventPrincipalVector &vec, unsigned int worker, const edm::EventSetup &c)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
std::string labelPlayback_
Definition: MixingModule.h:67
std::string encode() const
Definition: InputTag.cc:72
virtual void put(edm::Event &e, const edm::EventSetup &es)
virtual void getEventStartInfo(edm::Event &e, const unsigned int s)
virtual void addPileups(const int bcr, EventPrincipal *, unsigned int EventId, unsigned int worker, const edm::EventSetup &e)
virtual void checkSignal(const edm::Event &e)
std::string friendlyName(std::string const &iFullName)
static std::string subdets[11]
std::vector< MixingWorkerBase * > workers_
Definition: MixingModule.h:74
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:84
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:267
static const unsigned int maxNbSources_
Definition: BMixingModule.h:79
tuple pset
Definition: CrabTask.py:85
std::vector< MixingWorkerBase * > workersObjects_
Definition: MixingModule.h:75
std::vector< std::string > getParameterNames() const
bool useCurrentProcessOnly_
Definition: MixingModule.h:77
virtual void branchesActivate(const std::string &friendlyName, const std::string &subdet, InputTag &tag, std::string &label)
tuple tags
Definition: o2o.py:248
void sort_all(RandomAccessSequence &s)
wrappers for std::sort
Definition: Algorithms.h:120
std::vector< std::string > wantedBranches_
Definition: MixingModule.h:76
std::string const & label() const
Definition: InputTag.h:25
edm::EventID id() const
Definition: EventBase.h:56
CrossingFramePlaybackInfoExtended * playbackInfo_
Definition: MixingModule.h:70
void dropUnwantedBranches(std::vector< std::string > const &wantedBranches)
string s
Definition: asciidump.py:422
static const HistoName names[]
std::string const & instance() const
Definition: InputTag.h:26
tuple size
Write out results.
virtual void doPileUp(edm::Event &e, const edm::EventSetup &es)