CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
20 // system include files
21 #include <vector>
22 #include <memory>
23 #include <iostream>
24 
25 // user include files
28 
31 
36 
44 
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),
75  tags_(tags),
76  label_(label)
77  {;}
78  virtual ~HiMixingWorkerBase(){;}
79  // virtual void put(edm::Event &e) = 0;
80  virtual void addSignals(edm::Event &e) = 0;
81 
83  std::vector<InputTag> tags_;
85  };
86 
87 
88  template <class T>
90  public:
91  HiMixingWorker(std::string& object, std::vector<InputTag>& tags, std::string& label) : HiMixingWorkerBase(object,tags, label) {;}
93  void addSignals(edm::Event &e) override {
94  std::vector<Handle<std::vector<T> > > handles;
95  bool get = true;
96  for(size_t itag = 0; itag < tags_.size(); ++itag){
97  LogInfo("HiEmbedding")<<"itag "<<itag;
98  LogInfo("HiEmbedding")<<"label "<<tags_[itag].label();
99  LogInfo("HiEmbedding")<<"instance "<<tags_[itag].instance();
100  Handle<std::vector<T> > hand;
101  handles.push_back(hand);
102  get = get && e.getByLabel(tags_[itag],handles[itag]);
103  if(!get) LogWarning("Product inconsistency")<<"One of the sub-events is missing the product with type "
104  <<object_
105  <<", instance "
106  <<tags_[itag].instance()
107  <<" whereas the other one is fine.";
108  }
109 
110  if(get){
111  std::auto_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(crFrame,label_);
122  }
123  }
124  };
125 
126 template <>
128 
129  std::vector<Handle<HepMCProduct> > handles;
130  bool get = true;
131  for(size_t itag = 0; itag< tags_.size(); ++itag){
133  handles.push_back(hand);
134  get = get && e.getByLabel(tags_[itag],handles[itag]);
135  if(!get) LogWarning("Product inconsistency")<<"One of the sub-events is missing the product with type "
136  <<object_
137  <<", instance "
138  <<tags_[itag].instance()
139  <<" whereas the other one is fine.";
140  }
141 
142  if(get){
143  std::auto_ptr<CrossingFrame<HepMCProduct> > crFrame(new CrossingFrame<HepMCProduct>() );
144  crFrame->addSignals(handles[0].product(),e.id());
145  for(size_t itag = 1; itag < tags_.size(); ++itag){
146  HepMCProduct* product = const_cast<HepMCProduct*>(handles[itag].product());
147  crFrame->addPileups(*product);
148  }
149  e.put(crFrame,label_);
150  }
151 }
152 
154  public:
155  explicit HiMixingModule(const edm::ParameterSet&);
156  ~HiMixingModule();
157 
158  private:
159  virtual void beginJob() override ;
160  virtual void produce(edm::Event&, const edm::EventSetup&) override;
161  virtual void endJob() override ;
162  bool verifyRegistry(std::string object, std::string subdet, InputTag &tag,std::string &label);
163  // ----------member data ---------------------------
164 
165  std::vector<HiMixingWorkerBase *> workers_;
166 
167 };
168 
169 //
170 // constants, enums and typedefs
171 //
172 
173 
174 //
175 // static data member definitions
176 //
177 
178 //
179 // constructors and destructor
180 //
181 HiMixingModule::HiMixingModule(const edm::ParameterSet& pset)
182 {
183 
184  ParameterSet ps=pset.getParameter<ParameterSet>("mixObjects");
185  std::vector<std::string> names = ps.getParameterNames();
186  std::vector<std::string> simtags = pset.getParameter<std::vector<std::string> >("srcSIM");
187  std::vector<std::string> gentags = pset.getParameter<std::vector<std::string> >("srcGEN");
188 
189  if(simtags.size() != gentags.size()) LogError("MixingInput")<<"Generator and Simulation input lists are not matching each other"<<endl;
190 
191  for (std::vector<string>::iterator it=names.begin();it!= names.end();++it){
192 
193  ParameterSet pstag=ps.getParameter<ParameterSet>((*it));
194  if (!pstag.exists("type")) continue; //to allow replacement by empty pset
195  std::string object = pstag.getParameter<std::string>("type");
196  std::vector<InputTag> tags=pstag.getParameter<std::vector<InputTag> >("input");
197 
198  std::string signal;
199  for(size_t itag = 0; itag < tags.size(); ++itag){
200  InputTag tag=tags[itag];
201  std::vector<InputTag> inputs;
202 
203  for(size_t input = 0; input < simtags.size(); ++input){
204  if (object=="HepMCProduct") signal = gentags[input];
205  else signal = simtags[input];
206  inputs.push_back(InputTag(signal,tag.instance()));
207  }
208 
209  std::string label=tag.label()+tag.instance();
210  // verifyRegistry(object,std::string(""),tag,label);
211  if (object=="HepMCProduct"){
212  workers_.push_back(new HiMixingWorker<HepMCProduct>(object,inputs,label));
213  produces<CrossingFrame<HepMCProduct> >(label);
214  consumes<HepMCProduct>(tag);
215  }else if (object=="SimTrack"){
216  workers_.push_back(new HiMixingWorker<SimTrack>(object,inputs,label));
217  produces<CrossingFrame<SimTrack> >(label);
218  consumes<std::vector<SimTrack> >(tag);
219  }else if (object=="SimVertex"){
220  workers_.push_back(new HiMixingWorker<SimVertex>(object,inputs,label));
221  produces<CrossingFrame<SimVertex> >(label);
222  consumes<std::vector<SimVertex> >(tag);
223  }else if (object=="PSimHit"){
224  workers_.push_back(new HiMixingWorker<PSimHit>(object,inputs,label));
225  produces<CrossingFrame<PSimHit> >(label);
226  consumes<std::vector<PSimHit> >(tag);
227  }else if (object=="PCaloHit"){
228  workers_.push_back(new HiMixingWorker<PCaloHit>(object,inputs,label));
229  produces<CrossingFrame<PCaloHit> >(label);
230  consumes<std::vector<PCaloHit> >(tag);
231  }else LogInfo("Error")<<"What the hell is this object?!";
232 
233  LogInfo("HiMixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
234  }
235  }
236 
237  produces<PileupMixingContent>();
238 }
239 
240 
241 HiMixingModule::~HiMixingModule()
242 {
243 
244  // do anything here that needs to be done at desctruction time
245  // (e.g. close files, deallocate resources etc.)
246 
247 }
248 
249 
250 //
251 // member functions
252 //
253 
254 // ------------ method called to produce the data ------------
255 void
256 HiMixingModule::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
257 {
258  using namespace edm;
259 
260  for(size_t i = 0; i < workers_.size(); ++i){
261  (workers_[i])->addSignals(iEvent);
262  }
263 
264  std::auto_ptr< PileupMixingContent > PileupMixing_ = std::auto_ptr< PileupMixingContent >(new PileupMixingContent());
265  iEvent.put(PileupMixing_);
266 
267 }
268 
269 // ------------ method called once each job just before starting event loop ------------
270 void
272 {
273 }
274 
275 // ------------ method called once each job just after ending the event loop ------------
276 void
277 HiMixingModule::endJob() {
278 }
279 
280 bool HiMixingModule::verifyRegistry(std::string object, std::string subdet, InputTag &tag,std::string &label) {
281  // verify that the given product exists in the product registry
282  // and create the label to be given to the CrossingFrame
283 
285  // Loop over provenance of products in registry.
286  std::string lookfor;
287  if (object=="HepMCProduct") lookfor="edm::"+object;//exception for HepMCProduct
288  else if (object=="edm::HepMCProduct") lookfor=object;
289  else lookfor="std::vector<"+object+">";
290  bool found=false;
291  for (edm::ProductRegistry::ProductList::const_iterator it = reg->productList().begin();
292  it != reg->productList().end(); ++it) {
293  // See FWCore/Framework/interface/BranchDescription.h
294  // BranchDescription contains all the information for the product.
295  edm::BranchDescription desc = it->second;
296  if (desc.className()==lookfor && desc.moduleLabel()==tag.label() && desc.productInstanceName()==tag.instance()) {
297  label=desc.moduleLabel()+desc.productInstanceName();
298  found=true;
299  /*
300  wantedBranches_.push_back(desc.friendlyClassName() + '_' +
301  desc.moduleLabel() + '_' +
302  desc.productInstanceName());
303  */
304  break;
305  }
306  }
307  if (!found) {
308  LogWarning("MixingModule")<<"!!!!!!!!!Could not find in registry requested object: "<<object<<" with "<<tag<<".\nWill NOT be considered for mixing!!!!!!!!!";
309  return false;
310  }
311 
312  return true;
313 }
314 
315 //define this as a plug-in
317 
318 }
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
static const HistoName names[]
HiMixingWorkerBase(std::string &object, std::vector< InputTag > &tags, std::string &label)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
bool exists(std::string const &parameterName) const
checks if a parameter exists
std::string encode() const
Definition: InputTag.cc:164
void beginJob()
Definition: Breakpoints.cc:15
static std::string const input
Definition: EdmProvDump.cc:44
int iEvent
Definition: GenABIO.cc:230
std::string const & className() const
std::string const & moduleLabel() const
void addSignals(edm::Event &e) override
std::string const & productInstanceName() const
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:113
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:402
tuple tags
Definition: o2o.py:248
std::vector< HiMixingWorkerBase * > workers_
list object
Definition: dbtoconf.py:77
std::string const & label() const
Definition: InputTag.h:42
edm::EventID id() const
Definition: EventBase.h:56
std::string const & instance() const
Definition: InputTag.h:43
ProductList const & productList() const