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