CMS 3D CMS Logo

L1TStage2Layer2Producer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: L1Trigger/skeleton
4 // Class: skeleton
5 //
13 //
14 // Original Author: James Brooke
15 // Created: Thu, 05 Dec 2013 17:39:27 GMT
16 //
17 //
18 
19 
20 // system include files
21 #include <memory>
22 
23 // user include files
24 
36 
40 
44 
50 
51 //
52 // class declaration
53 //
54 
55 using namespace l1t;
56 
58  public:
59  explicit L1TStage2Layer2Producer(const edm::ParameterSet& ps);
60  ~L1TStage2Layer2Producer() override;
61 
62  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions)
63  ;
64 
65  private:
66  void produce(edm::Event&, const edm::EventSetup&) override;
67 
68  void beginRun(edm::Run const&, edm::EventSetup const&) override;
69  void endRun(edm::Run const&, edm::EventSetup const&) override;
70  //virtual void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
71  //virtual void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
72 
73  // ----------member data ---------------------------
74 
75  // input token
77 
78  // put tokens
89 
90 
91  // parameters
92  unsigned long long m_paramsCacheId;
93  unsigned m_fwv;
95 
96  // the processor
97  std::shared_ptr<Stage2MainProcessor> m_processor;
98 
99  // use static config for fw testing
101 
102  };
103 
104 
105 
107 
108  // register what you produce
109  m_towerMPToken = produces<CaloTowerBxCollection> ("MP");
110  m_clusterMPToken = produces<CaloClusterBxCollection> ("MP");
111  m_egammaMPToken = produces<EGammaBxCollection> ("MP");
112  m_tauMPToken = produces<TauBxCollection> ("MP");
113  m_jetMPToken = produces<JetBxCollection> ("MP");
114  m_etMPToken = produces<EtSumBxCollection> ("MP");
115  m_egammaToken = produces<EGammaBxCollection> ();
116  m_tauToken = produces<TauBxCollection> ();
117  m_jetToken = produces<JetBxCollection> ();
118  m_etToken = produces<EtSumBxCollection> ();
119 
120  // register what you consume and keep token for later access:
121  m_towerToken = consumes<CaloTowerBxCollection>(ps.getParameter<edm::InputTag>("towerToken"));
122 
123  // placeholder for the parameters
124  m_params = new CaloParamsHelper;
125 
126  // set firmware version from python config for now
127  m_fwv = ps.getParameter<int>("firmware");
128 
129  // get static config flag
130  m_useStaticConfig = ps.getParameter<bool>("useStaticConfig");
131 
132  //initialize
133  m_paramsCacheId=0;
134 
135 }
136 
138 
139  delete m_params;
140 
141 }
142 
143 // ------------ method called to produce the data ------------
144 void
146 {
147 
148  using namespace edm;
149 
150  using namespace l1t;
151 
152  LogDebug("l1t|stage 2") << "L1TStage2Layer2Producer::produce function called..." << std::endl;
153 
154 
155  //inputs
157  iEvent.getByToken(m_towerToken,towers);
158 
159  int bxFirst = towers->getFirstBX();
160  int bxLast = towers->getLastBX();
161 
162  LogDebug("L1TDebug") << "First BX=" << bxFirst << ", last BX=" << bxLast << std::endl;
163 
164  //outputs
165  CaloTowerBxCollection outTowers(0, bxFirst, bxLast);
166  CaloClusterBxCollection clusters(0, bxFirst, bxLast);
167  EGammaBxCollection mpegammas(0, bxFirst, bxLast);
168  TauBxCollection mptaus(0, bxFirst, bxLast);
169  JetBxCollection mpjets(0, bxFirst, bxLast);
170  EtSumBxCollection mpsums(0, bxFirst, bxLast);
171  EGammaBxCollection egammas(0, bxFirst, bxLast);
172  TauBxCollection taus(0, bxFirst, bxLast);
173  JetBxCollection jets(0, bxFirst, bxLast);
174  EtSumBxCollection etsums(0, bxFirst, bxLast);
175 
176  // loop over BX
177  for(int ibx = bxFirst; ibx < bxLast+1; ++ibx) {
178  std::vector<CaloTower> localTowers (CaloTools::caloTowerHashMax()+1);
179  std::vector<CaloTower> localOutTowers;
180  std::vector<CaloCluster> localClusters;
181  std::vector<EGamma> localMPEGammas;
182  std::vector<Tau> localMPTaus;
183  std::vector<Jet> localMPJets;
184  std::vector<EtSum> localMPEtSums;
185  std::vector<EGamma> localEGammas;
186  std::vector<Tau> localTaus;
187  std::vector<Jet> localJets;
188  std::vector<EtSum> localEtSums;
189 
190  LogDebug("L1TDebug") << "BX=" << ibx << ", N(Towers)=" << towers->size(ibx) << std::endl;
191 
192  for(std::vector<CaloTower>::const_iterator tower = towers->begin(ibx);
193  tower != towers->end(ibx);
194  ++tower) {
195 
196  CaloTower tow(tower->p4(),
197  tower->etEm(),
198  tower->etHad(),
199  tower->hwPt(),
200  tower->hwEta(),
201  tower->hwPhi(),
202  tower->hwQual(),
203  tower->hwEtEm(),
204  tower->hwEtHad(),
205  tower->hwEtRatio());
206 
207  localTowers.at(CaloTools::caloTowerHash(tow.hwEta(),tow.hwPhi())) = tow;
208  }
209 
210  LogDebug("L1TDebug") << "BX=" << ibx << ", N(Towers)=" << localTowers.size() << std::endl;
211 
212  m_processor->processEvent(localTowers,
213  localOutTowers,
214  localClusters,
215  localMPEGammas,
216  localMPTaus,
217  localMPJets,
218  localMPEtSums,
219  localEGammas,
220  localTaus,
221  localJets,
222  localEtSums);
223 
224  for( auto tow = localOutTowers.begin(); tow != localOutTowers.end(); ++tow)
225  outTowers.push_back(ibx, *tow);
226  for( auto clus = localClusters.begin(); clus != localClusters.end(); ++clus)
227  clusters.push_back(ibx, *clus);
228  for( auto eg = localMPEGammas.begin(); eg != localMPEGammas.end(); ++eg)
229  mpegammas.push_back(ibx, CaloTools::egP4MP(*eg));
230  for( auto tau = localMPTaus.begin(); tau != localMPTaus.end(); ++tau)
231  mptaus.push_back(ibx, CaloTools::tauP4MP(*tau));
232  for( auto jet = localMPJets.begin(); jet != localMPJets.end(); ++jet)
233  mpjets.push_back(ibx, CaloTools::jetP4MP(*jet));
234  for( auto etsum = localMPEtSums.begin(); etsum != localMPEtSums.end(); ++etsum)
235  mpsums.push_back(ibx, CaloTools::etSumP4MP(*etsum));
236  for( auto eg = localEGammas.begin(); eg != localEGammas.end(); ++eg)
237  egammas.push_back(ibx, CaloTools::egP4Demux(*eg));
238  for( auto tau = localTaus.begin(); tau != localTaus.end(); ++tau)
239  taus.push_back(ibx, CaloTools::tauP4Demux(*tau));
240  for( auto jet = localJets.begin(); jet != localJets.end(); ++jet)
241  jets.push_back(ibx, CaloTools::jetP4Demux(*jet));
242  for( auto etsum = localEtSums.begin(); etsum != localEtSums.end(); ++etsum)
243  etsums.push_back(ibx, CaloTools::etSumP4Demux(*etsum));
244 
245 
246  LogDebug("L1TDebug") << "BX=" << ibx << ", N(Cluster)=" << localClusters.size() << ", N(EG)=" << localEGammas.size() << ", N(Tau)=" << localTaus.size() << ", N(Jet)=" << localJets.size() << ", N(Sums)=" << localEtSums.size() << std::endl;
247 
248  }
249 
250  iEvent.emplace(m_towerMPToken, std::move(outTowers));
251  iEvent.emplace(m_clusterMPToken, std::move(clusters));
252  iEvent.emplace(m_egammaMPToken, std::move(mpegammas));
253  iEvent.emplace(m_tauMPToken, std::move(mptaus));
254  iEvent.emplace(m_jetMPToken, std::move(mpjets));
255  iEvent.emplace(m_etMPToken, std::move(mpsums));
256  iEvent.emplace(m_egammaToken, std::move(egammas));
257  iEvent.emplace(m_tauToken, std::move(taus));
258  iEvent.emplace(m_jetToken, std::move(jets));
259  iEvent.emplace(m_etToken, std::move(etsums));
260 
261 }
262 
263 // ------------ method called when starting to processes a run ------------
264 void
266 {
267 
268  // update parameters and algorithms at run start, if they have changed
269  // update params first because the firmware factory relies on pointer to params
270 
271  // parameters
272 
273  unsigned long long id = iSetup.get<L1TCaloParamsRcd>().cacheIdentifier();
274 
275  if (id != m_paramsCacheId) {
276 
277  m_paramsCacheId = id;
278 
279  // fetch payload corresponding to the current run from the CondDB
280  edm::ESHandle<CaloParams> candidateHandle;
281  iSetup.get<L1TCaloParamsRcd>().get(candidateHandle);
282  std::unique_ptr<l1t::CaloParams> candidate(new l1t::CaloParams( *candidateHandle.product() ));
283 
284 
285  if(!m_useStaticConfig){
286 
287  // fetch the latest greatest prototype (equivalent of static payload)
288  edm::ESHandle<CaloParams> o2oProtoHandle;
289  iSetup.get<L1TCaloParamsO2ORcd>().get(o2oProtoHandle);
290  std::unique_ptr<l1t::CaloParams> prototype(new l1t::CaloParams( *o2oProtoHandle.product() ));
291 
292  // prepare to set the emulator's configuration
293  // and then replace our local copy of the parameters with a new one using placement new
294  m_params->~CaloParamsHelper();
295 
296  // compare the candidate payload misses some of the pnodes compared to the prototype,
297  // if this is the case - the candidate is an old payload that'll crash the Stage2 emulator
298  // and we better use the prototype for the emulator's configuration
299  if( ((CaloParamsHelper*)candidate.get())->getNodes().size() < ((CaloParamsHelper*)prototype.get())->getNodes().size())
300  m_params = new (m_params) CaloParamsHelper( *o2oProtoHandle.product() );
301  else
302  m_params = new (m_params) CaloParamsHelper( *candidateHandle.product() );
303  // KK: the nifty tricks above (placement new) work as long as current definition of
304  // CaloParams takes more space than the one obtained from the record
305 
306  } else {
307  m_params->~CaloParamsHelper();
308  m_params = new (m_params) CaloParamsHelper( *candidateHandle.product() );
309  }
310 
311  LogDebug("L1TDebug") << *m_params << std::endl;
312 
313  if (! m_params){
314  edm::LogError("l1t|caloStage2") << "Could not retrieve params from Event Setup" << std::endl;
315  }
316 
317  }
318 
319  // firmware
320 
321  if ( !m_processor ) { // in future, also check if the firmware cache ID has changed !
322 
323  // m_fwv = ; // get new firmware version in future
324 
325  // Set the current algorithm version based on DB pars from database:
326  Stage2Layer2FirmwareFactory m_factory;
327  m_processor = m_factory.create(m_fwv, m_params);
328 
329  if (! m_processor) {
330  // we complain here once per run
331  edm::LogError("l1t|caloStage2") << "Firmware could not be configured.\n";
332  }
333 
334  LogDebug("L1TDebug") << "Processor object : " << (m_processor?1:0) << std::endl;
335 
336  }
337 
338 
339 }
340 
341 
342 // ------------ method called when ending the processing of a run ------------
343 void
345 {
346 }
347 
348 // ------------ method called when starting to processes a luminosity block ------------
349 /*
350 void
351 L1TStage2Layer2Producer::beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup cons
352 t&)
353 {
354 }
355 */
356 
357 // ------------ method called when ending the processing of a luminosity block ------------
358 /*
359 void
360 L1TStage2Layer2Producer::endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&
361 )
362 {
363 }
364 */
365 
366 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
367 void
369  //The following says we do not know what parameters are allowed so do no validation
370  // Please change this to state exactly what you do use, even if it is no parameters
372  desc.setUnknown();
373  descriptions.addDefault(desc);
374 }
375 
376 //define this as a plug-in
#define LogDebug(id)
T getParameter(std::string const &) const
static l1t::Tau tauP4MP(l1t::Tau &)
Definition: CaloTools.cc:411
std::shared_ptr< Stage2MainProcessor > m_processor
static l1t::EtSum etSumP4MP(l1t::EtSum &)
Definition: CaloTools.cc:452
edm::EDPutTokenT< TauBxCollection > m_tauMPToken
def getNodes(rootNode, name)
edm::EDPutTokenT< EGammaBxCollection > m_egammaToken
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
static l1t::EGamma egP4MP(l1t::EGamma &)
Definition: CaloTools.cc:389
void beginRun(edm::Run const &, edm::EventSetup const &) override
edm::EDPutTokenT< TauBxCollection > m_tauToken
edm::EDPutTokenT< CaloTowerBxCollection > m_towerMPToken
unsigned long long m_paramsCacheId
delete x;
Definition: CaloConfig.h:22
edm::EDGetTokenT< CaloTowerBxCollection > m_towerToken
ReturnType create(unsigned fwv, CaloParamsHelper const *params)
static size_t caloTowerHash(int iEta, int iPhi)
Definition: CaloTools.cc:73
edm::EDPutTokenT< EtSumBxCollection > m_etMPToken
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void addDefault(ParameterSetDescription const &psetDescription)
vector< PseudoJet > jets
static l1t::EGamma egP4Demux(l1t::EGamma &)
Definition: CaloTools.cc:300
L1TStage2Layer2Producer(const edm::ParameterSet &ps)
static l1t::Jet jetP4MP(l1t::Jet &)
Definition: CaloTools.cc:431
static l1t::Jet jetP4Demux(l1t::Jet &)
Definition: CaloTools.cc:343
edm::EDPutTokenT< JetBxCollection > m_jetMPToken
edm::EDPutTokenT< EtSumBxCollection > m_etToken
static l1t::Tau tauP4Demux(l1t::Tau &)
Definition: CaloTools.cc:322
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
OrphanHandle< PROD > emplace(EDPutTokenT< PROD > token, Args &&...args)
puts a new product
Definition: Event.h:413
static size_t caloTowerHashMax()
Definition: CaloTools.cc:93
edm::EDPutTokenT< EGammaBxCollection > m_egammaMPToken
edm::EDPutTokenT< CaloClusterBxCollection > m_clusterMPToken
HLT enums.
T get() const
Definition: EventSetup.h:71
edm::EDPutTokenT< JetBxCollection > m_jetToken
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
T const * product() const
Definition: ESHandle.h:86
void produce(edm::Event &, const edm::EventSetup &) override
static l1t::EtSum etSumP4Demux(l1t::EtSum &)
Definition: CaloTools.cc:366
def move(src, dest)
Definition: eostools.py:511
Definition: Run.h:45
void push_back(int bx, T object)
void endRun(edm::Run const &, edm::EventSetup const &) override