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