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