CMS 3D CMS Logo

HiMixingModule.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: HiMixingModule
4 // Class: HiMixingModule
5 //
13 //
14 // Original Author: Yetkin Yilmaz
15 // Created: Tue Feb 17 17:32:06 EST 2009
16 //
17 //
18 
19 // system include files
20 #include <vector>
21 #include <memory>
22 #include <iostream>
23 
24 // user include files
27 
30 
35 
43 
54 
55 #include <string>
56 
57 using namespace std;
58 
59 //
60 // class decleration
61 //
62 
63 namespace edm {
64 
65  class HiMixingModule;
66 
68  public:
69  explicit HiMixingWorkerBase() { ; }
70 
71  HiMixingWorkerBase(std::string& object, std::vector<InputTag>& tags, std::string& label)
72  : object_(object), tags_(tags), label_(label) {
73  ;
74  }
75  virtual ~HiMixingWorkerBase() { ; }
76  // virtual void put(edm::Event &e) = 0;
77  virtual void addSignals(edm::Event& e) = 0;
78 
80  std::vector<InputTag> tags_;
82  };
83 
84  template <class T>
86  public:
87  HiMixingWorker(std::string& object, std::vector<InputTag>& tags, std::string& label)
88  : HiMixingWorkerBase(object, tags, label) {
89  ;
90  }
91  ~HiMixingWorker() override { ; }
92  void addSignals(edm::Event& e) override {
93  std::vector<Handle<std::vector<T> > > handles;
94  bool get = true;
95  for (size_t itag = 0; itag < tags_.size(); ++itag) {
96  LogInfo("HiEmbedding") << "itag " << itag;
97  LogInfo("HiEmbedding") << "label " << tags_[itag].label();
98  LogInfo("HiEmbedding") << "instance " << tags_[itag].instance();
99  Handle<std::vector<T> > hand;
100  handles.push_back(hand);
101  get = get && e.getByLabel(tags_[itag], handles[itag]);
102  if (!get)
103  LogWarning("Product inconsistency")
104  << "One of the sub-events is missing the product with type " << object_ << ", instance "
105  << tags_[itag].instance() << " whereas the other one is fine.";
106  }
107 
108  if (get) {
109  std::unique_ptr<CrossingFrame<T> > crFrame(new CrossingFrame<T>());
110  crFrame->addSignals(handles[0].product(), e.id());
111  for (size_t itag = 1; itag < tags_.size(); ++itag) {
112  std::vector<T>* product = const_cast<std::vector<T>*>(handles[itag].product());
113  EncodedEventId id(0, itag);
114  for (auto& item : *product) {
115  item.setEventId(id);
116  }
117  crFrame->addPileups(*product);
118  }
119  e.put(std::move(crFrame), label_);
120  }
121  }
122  };
123 
124  template <>
126  std::vector<Handle<HepMCProduct> > handles;
127  bool get = true;
128  for (size_t itag = 0; itag < tags_.size(); ++itag) {
130  handles.push_back(hand);
131  get = get && e.getByLabel(tags_[itag], handles[itag]);
132  if (!get)
133  LogWarning("Product inconsistency")
134  << "One of the sub-events is missing the product with type " << object_ << ", instance "
135  << tags_[itag].instance() << " whereas the other one is fine.";
136  }
137 
138  if (get) {
139  std::unique_ptr<CrossingFrame<HepMCProduct> > crFrame(new CrossingFrame<HepMCProduct>());
140  crFrame->addSignals(handles[0].product(), e.id());
141  for (size_t itag = 1; itag < tags_.size(); ++itag) {
142  HepMCProduct* product = const_cast<HepMCProduct*>(handles[itag].product());
143  crFrame->addPileups(*product);
144  }
145  e.put(std::move(crFrame), label_);
146  }
147  }
148 
150  public:
151  explicit HiMixingModule(const edm::ParameterSet&);
152  ~HiMixingModule() override;
153 
154  private:
155  //virtual void beginJob() override {}
156  void produce(edm::Event&, const edm::EventSetup&) override;
157  //virtual void endJob() override {}
158  bool verifyRegistry(std::string object, std::string subdet, InputTag& tag, std::string& label);
159  // ----------member data ---------------------------
160 
161  std::vector<HiMixingWorkerBase*> workers_;
162  };
163 
164  //
165  // constants, enums and typedefs
166  //
167 
168  //
169  // static data member definitions
170  //
171 
172  //
173  // constructors and destructor
174  //
175  HiMixingModule::HiMixingModule(const edm::ParameterSet& pset) {
176  ParameterSet ps = pset.getParameter<ParameterSet>("mixObjects");
177  std::vector<std::string> names = ps.getParameterNames();
178  std::vector<std::string> simtags = pset.getParameter<std::vector<std::string> >("srcSIM");
179  std::vector<std::string> gentags = pset.getParameter<std::vector<std::string> >("srcGEN");
180 
181  if (simtags.size() != gentags.size())
182  LogError("MixingInput") << "Generator and Simulation input lists are not matching each other" << endl;
183 
184  for (std::vector<string>::iterator it = names.begin(); it != names.end(); ++it) {
185  ParameterSet pstag = ps.getParameter<ParameterSet>((*it));
186  if (!pstag.exists("type"))
187  continue; //to allow replacement by empty pset
188  std::string object = pstag.getParameter<std::string>("type");
189  std::vector<InputTag> tags = pstag.getParameter<std::vector<InputTag> >("input");
190 
191  std::string signal;
192  for (size_t itag = 0; itag < tags.size(); ++itag) {
193  InputTag tag = tags[itag];
194  std::vector<InputTag> inputs;
195 
196  for (size_t input = 0; input < simtags.size(); ++input) {
197  if (object == "HepMCProduct")
198  signal = gentags[input];
199  else
200  signal = simtags[input];
201  inputs.push_back(InputTag(signal, tag.instance()));
202  }
203 
204  std::string label = tag.label() + tag.instance();
205  // verifyRegistry(object,std::string(""),tag,label);
206  if (object == "HepMCProduct") {
207  workers_.push_back(new HiMixingWorker<HepMCProduct>(object, inputs, label));
208  produces<CrossingFrame<HepMCProduct> >(label);
209  consumes<HepMCProduct>(tag);
210  } else if (object == "SimTrack") {
211  workers_.push_back(new HiMixingWorker<SimTrack>(object, inputs, label));
212  produces<CrossingFrame<SimTrack> >(label);
213  consumes<std::vector<SimTrack> >(tag);
214  } else if (object == "SimVertex") {
215  workers_.push_back(new HiMixingWorker<SimVertex>(object, inputs, label));
216  produces<CrossingFrame<SimVertex> >(label);
217  consumes<std::vector<SimVertex> >(tag);
218  } else if (object == "PSimHit") {
219  workers_.push_back(new HiMixingWorker<PSimHit>(object, inputs, label));
220  produces<CrossingFrame<PSimHit> >(label);
221  consumes<std::vector<PSimHit> >(tag);
222  } else if (object == "PCaloHit") {
223  workers_.push_back(new HiMixingWorker<PCaloHit>(object, inputs, label));
224  produces<CrossingFrame<PCaloHit> >(label);
225  consumes<std::vector<PCaloHit> >(tag);
226  } else
227  LogInfo("Error") << "What the hell is this object?!";
228 
229  LogInfo("HiMixingModule") << "Will mix " << object << "s with InputTag= " << tag.encode() << ", label will be "
230  << label;
231  }
232  }
233 
234  produces<PileupMixingContent>();
235  }
236 
237  HiMixingModule::~HiMixingModule() {
238  // do anything here that needs to be done at desctruction time
239  // (e.g. close files, deallocate resources etc.)
240  }
241 
242  //
243  // member functions
244  //
245 
246  // ------------ method called to produce the data ------------
247  void HiMixingModule::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
248  using namespace edm;
249 
250  for (size_t i = 0; i < workers_.size(); ++i) {
251  (workers_[i])->addSignals(iEvent);
252  }
253 
254  std::unique_ptr<PileupMixingContent> PileupMixing_ =
255  std::unique_ptr<PileupMixingContent>(new PileupMixingContent());
256  iEvent.put(std::move(PileupMixing_));
257  }
258 
259  bool HiMixingModule::verifyRegistry(std::string object, std::string subdet, InputTag& tag, std::string& label) {
260  // verify that the given product exists in the product registry
261  // and create the label to be given to the CrossingFrame
262 
264  // Loop over provenance of products in registry.
265  std::string lookfor;
266  if (object == "HepMCProduct")
267  lookfor = "edm::" + object; //exception for HepMCProduct
268  else if (object == "edm::HepMCProduct")
269  lookfor = object;
270  else
271  lookfor = "std::vector<" + object + ">";
272  bool found = false;
273  for (edm::ProductRegistry::ProductList::const_iterator it = reg->productList().begin();
274  it != reg->productList().end();
275  ++it) {
276  // See FWCore/Framework/interface/BranchDescription.h
277  // BranchDescription contains all the information for the product.
278  edm::BranchDescription desc = it->second;
279  if (desc.className() == lookfor && desc.moduleLabel() == tag.label() &&
280  desc.productInstanceName() == tag.instance()) {
281  label = desc.moduleLabel() + desc.productInstanceName();
282  found = true;
283  /*
284  wantedBranches_.push_back(desc.friendlyClassName() + '_' +
285  desc.moduleLabel() + '_' +
286  desc.productInstanceName());
287  */
288  break;
289  }
290  }
291  if (!found) {
292  LogWarning("MixingModule") << "!!!!!!!!!Could not find in registry requested object: " << object << " with "
293  << tag << ".\nWill NOT be considered for mixing!!!!!!!!!";
294  return false;
295  }
296 
297  return true;
298  }
299 
300  //define this as a plug-in
302 
303 } // namespace edm
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
HiMixingWorkerBase(std::string &object, std::vector< InputTag > &tags, std::string &label)
bool exists(std::string const &parameterName) const
checks if a parameter exists
const std::string names[nVars_]
std::string encode() const
Definition: InputTag.cc:159
~HiMixingWorker() override
static std::string const input
Definition: EdmProvDump.cc:48
char const * label
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::string const & className() const
std::string const & moduleLabel() const
std::vector< BaseVolumeHandle * > handles
void addSignals(edm::Event &e) override
std::string const & productInstanceName() const
HiMixingWorker(std::string &object, std::vector< InputTag > &tags, std::string &label)
std::vector< InputTag > tags_
std::vector< std::string > getParameterNames() const
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:488
std::vector< HiMixingWorkerBase * > workers_
std::string const & label() const
Definition: InputTag.h:36
edm::EventID id() const
Definition: EventBase.h:59
HLT enums.
std::string const & instance() const
Definition: InputTag.h:37
def move(src, dest)
Definition: eostools.py:511
ProductList const & productList() const