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 // $Id: HiMixingModule.cc,v 1.11 2013/03/01 00:13:36 wmtan Exp $
17 //
18 //
19 
20 
21 // system include files
22 #include <vector>
23 #include <memory>
24 #include <iostream>
25 
26 // user include files
29 
32 
37 
45 
57 
58 #include <string>
59 
60 using namespace std;
61 
62 //
63 // class decleration
64 //
65 
66 namespace edm{
67 
68  class HiMixingModule;
69 
71  public:
72  explicit HiMixingWorkerBase(){;}
73 
74  HiMixingWorkerBase(std::string& object, std::vector<InputTag>& tags, std::string& label) :
75  object_(object),
76  tags_(tags),
77  label_(label)
78  {;}
79  virtual ~HiMixingWorkerBase(){;}
80  // virtual void put(edm::Event &e) = 0;
81  virtual void addSignals(edm::Event &e) = 0;
82 
84  std::vector<InputTag> tags_;
86  };
87 
88 
89  template <class T>
91  public:
92  HiMixingWorker(std::string& object, std::vector<InputTag>& tags, std::string& label) : HiMixingWorkerBase(object,tags, label) {;}
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) LogWarning("Product inconsistency")<<"One of the sub-events is missing the product with type "
105  <<object_
106  <<", instance "
107  <<tags_[itag].instance()
108  <<" whereas the other one is fine.";
109  }
110 
111  if(get){
112  std::auto_ptr<CrossingFrame<T> > crFrame(new CrossingFrame<T>() );
113  crFrame->addSignals(handles[0].product(),e.id());
114  for(size_t itag = 1; itag < tags_.size(); ++itag){
115  crFrame->addPileups(0,const_cast< std::vector<T> * >(handles[itag].product()),itag);
116  }
117  e.put(crFrame,label_);
118  }
119  }
120  };
121 
122 template <>
124 
125  std::vector<Handle<HepMCProduct> > handles;
126  bool get = true;
127  for(size_t itag = 0; itag< tags_.size(); ++itag){
129  handles.push_back(hand);
130  get = get && e.getByLabel(tags_[itag],handles[itag]);
131  if(!get) LogWarning("Product inconsistency")<<"One of the sub-events is missing the product with type "
132  <<object_
133  <<", instance "
134  <<tags_[itag].instance()
135  <<" whereas the other one is fine.";
136  }
137 
138  if(get){
139  std::auto_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  crFrame->addPileups(0, const_cast<HepMCProduct *>(handles[itag].product()),itag);
143  }
144  e.put(crFrame,label_);
145  }
146 }
147 
149  public:
150  explicit HiMixingModule(const edm::ParameterSet&);
151  ~HiMixingModule();
152 
153  private:
154  virtual void beginJob() ;
155  virtual void produce(edm::Event&, const edm::EventSetup&) override;
156  virtual void endJob() ;
157  bool verifyRegistry(std::string object, std::string subdet, InputTag &tag,std::string &label);
158  // ----------member data ---------------------------
159 
160  std::vector<HiMixingWorkerBase *> workers_;
161 
162 };
163 
164 //
165 // constants, enums and typedefs
166 //
167 
168 
169 //
170 // static data member definitions
171 //
172 
173 //
174 // constructors and destructor
175 //
176 HiMixingModule::HiMixingModule(const edm::ParameterSet& pset)
177 {
178 
179  ParameterSet ps=pset.getParameter<ParameterSet>("mixObjects");
180  std::vector<std::string> names = ps.getParameterNames();
181  std::vector<std::string> simtags = pset.getParameter<std::vector<std::string> >("srcSIM");
182  std::vector<std::string> gentags = pset.getParameter<std::vector<std::string> >("srcGEN");
183 
184  if(simtags.size() != gentags.size()) 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 
188  ParameterSet pstag=ps.getParameter<ParameterSet>((*it));
189  if (!pstag.exists("type")) 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") signal = gentags[input];
200  else 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  }else if (object=="SimTrack"){
210  workers_.push_back(new HiMixingWorker<SimTrack>(object,inputs,label));
211  produces<CrossingFrame<SimTrack> >(label);
212  }else if (object=="SimVertex"){
213  workers_.push_back(new HiMixingWorker<SimVertex>(object,inputs,label));
214  produces<CrossingFrame<SimVertex> >(label);
215  }else if (object=="PSimHit"){
216  workers_.push_back(new HiMixingWorker<PSimHit>(object,inputs,label));
217  produces<CrossingFrame<PSimHit> >(label);
218  }else if (object=="PCaloHit"){
219  workers_.push_back(new HiMixingWorker<PCaloHit>(object,inputs,label));
220  produces<CrossingFrame<PCaloHit> >(label);
221  }else LogInfo("Error")<<"What the hell is this object?!";
222 
223  LogInfo("HiMixingModule") <<"Will mix "<<object<<"s with InputTag= "<<tag.encode()<<", label will be "<<label;
224  }
225  }
226 
227  produces<PileupMixingContent>();
228 }
229 
230 
231 HiMixingModule::~HiMixingModule()
232 {
233 
234  // do anything here that needs to be done at desctruction time
235  // (e.g. close files, deallocate resources etc.)
236 
237 }
238 
239 
240 //
241 // member functions
242 //
243 
244 // ------------ method called to produce the data ------------
245 void
246 HiMixingModule::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
247 {
248  using namespace edm;
249 
250  for(size_t i = 0; i < workers_.size(); ++i){
251  (workers_[i])->addSignals(iEvent);
252  }
253 
254  std::auto_ptr< PileupMixingContent > PileupMixing_ = std::auto_ptr< PileupMixingContent >(new PileupMixingContent());
255  iEvent.put(PileupMixing_);
256 
257 }
258 
259 // ------------ method called once each job just before starting event loop ------------
260 void
262 {
263 }
264 
265 // ------------ method called once each job just after ending the event loop ------------
266 void
267 HiMixingModule::endJob() {
268 }
269 
270 bool HiMixingModule::verifyRegistry(std::string object, std::string subdet, InputTag &tag,std::string &label) {
271  // verify that the given product exists in the product registry
272  // and create the label to be given to the CrossingFrame
273 
275  // Loop over provenance of products in registry.
276  std::string lookfor;
277  if (object=="HepMCProduct") lookfor="edm::"+object;//exception for HepMCProduct
278  else if (object=="edm::HepMCProduct") lookfor=object;
279  else lookfor="std::vector<"+object+">";
280  bool found=false;
281  for (edm::ProductRegistry::ProductList::const_iterator it = reg->productList().begin();
282  it != reg->productList().end(); ++it) {
283  // See FWCore/Framework/interface/BranchDescription.h
284  // BranchDescription contains all the information for the product.
285  edm::BranchDescription desc = it->second;
286  if (desc.className()==lookfor && desc.moduleLabel()==tag.label() && desc.productInstanceName()==tag.instance()) {
287  label=desc.moduleLabel()+desc.productInstanceName();
288  found=true;
289  /*
290  wantedBranches_.push_back(desc.friendlyClassName() + '_' +
291  desc.moduleLabel() + '_' +
292  desc.productInstanceName());
293  */
294  break;
295  }
296  }
297  if (!found) {
298  LogWarning("MixingModule")<<"!!!!!!!!!Could not find in registry requested object: "<<object<<" with "<<tag<<".\nWill NOT be considered for mixing!!!!!!!!!";
299  return false;
300  }
301 
302  return true;
303 }
304 
305 //define this as a plug-in
307 
308 }
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
int iEvent
Definition: GenABIO.cc:243
std::string const & className() const
std::string const & moduleLabel() const
std::string const & productInstanceName() const
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:94
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:361
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
void addSignals(edm::Event &e)