CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
L1TStage1Layer2Producer.cc
Go to the documentation of this file.
1 
10 
11 // system include files
12 #include <boost/shared_ptr.hpp>
13 
14 // user include files
15 
26 
32 
33 #include <vector>
35 
36 //#include "CondFormats/DataRecord/interface/CaloParamsRcd.h"
37 //#include "CondFormats/L1TCalorimeter/interface/CaloParams.h"
40 //#include "CondFormats/L1TObjects/interface/FirmwareVersion.h"
42 
45 
48 
54 
57 
58 // print statements
59 //#include <stdio.h>
60 
61 using namespace std;
62 using namespace edm;
63 using namespace l1t;
64 
65 
66 //
67 // class declaration
68 //
69 
71  public:
72  explicit L1TStage1Layer2Producer(const ParameterSet&);
74 
75  static void fillDescriptions(ConfigurationDescriptions& descriptions);
76 
77  private:
78  virtual void produce(Event&, EventSetup const&);
79  virtual void beginJob();
80  virtual void endJob();
81  virtual void beginRun(Run const&iR, EventSetup const&iE);
82  virtual void endRun(Run const& iR, EventSetup const& iE);
83 
84  // ----------member data ---------------------------
85  unsigned long long m_paramsCacheId; // Cache-ID from current parameters, to check if needs to be updated.
86  unsigned long long m_configCacheId; // Cache-ID from current parameters, to check if needs to be updated.
89 
90 
91  boost::shared_ptr<Stage1Layer2MainProcessor> m_fw; // Firmware to run per event, depends on database parameters.
92 
93  Stage1Layer2FirmwareFactory m_factory; // Factory to produce algorithms based on DB parameters
94 
96 
97  // to be extended with other "consumes" stuff
100 
101  };
102 
103  //
104  // constructors and destructor
105  //
107  {
108  // register what you produce
109  produces<BXVector<EGamma>>();
110  produces<BXVector<Tau>>("rlxTaus");
111  produces<BXVector<Tau>>("isoTaus");
112  produces<BXVector<Jet>>();
113  produces<BXVector<Jet>>("preGtJets");
114  produces<BXVector<EtSum>>();
115  produces<BXVector<CaloSpare>>("HFRingSums");
116  produces<BXVector<CaloSpare>>("HFBitCounts");
117 
118  // register what you consume and keep token for later access:
119  regionToken = consumes<BXVector<CaloRegion>>(iConfig.getParameter<InputTag>("CaloRegions"));
120  candsToken = consumes<BXVector<CaloEmCand>>(iConfig.getParameter<InputTag>("CaloEmCands"));
121  //int ifwv=iConfig.getParameter<unsigned>("FirmwareVersion"); // LenA make configurable for now
122 
123  m_conditionsLabel = iConfig.getParameter<std::string>("conditionsLabel");
124 
125  m_params = new CaloParamsHelper;
126 
127  // set cache id to zero, will be set at first beginRun:
128  m_paramsCacheId = 0;
129  m_configCacheId = 0;
130  }
131 
132 
134  {
135  }
136 
137 
138 
139 //
140 // member functions
141 //
142 
143 // ------------ method called to produce the data ------------
144 void
146 {
147 
148  LogDebug("l1t|stage 1 jets") << "L1TStage1Layer2Producer::produce function called...\n";
149 
150  //return;
151 
152  //inputs
153  Handle<BXVector<CaloRegion>> caloRegions;
154  iEvent.getByToken(regionToken,caloRegions);
155 
156  Handle<BXVector<CaloEmCand>> caloEmCands;
157  iEvent.getByToken(candsToken, caloEmCands);
158 
159  int bxFirst = caloRegions->getFirstBX();
160  int bxLast = caloRegions->getLastBX();
161 
162  //outputs
163  std::auto_ptr<EGammaBxCollection> egammas (new EGammaBxCollection);
164  std::auto_ptr<TauBxCollection> taus (new TauBxCollection);
165  std::auto_ptr<TauBxCollection> isoTaus (new TauBxCollection);
166  std::auto_ptr<JetBxCollection> jets (new JetBxCollection);
167  std::auto_ptr<JetBxCollection> preGtJets (new JetBxCollection);
168  std::auto_ptr<EtSumBxCollection> etsums (new EtSumBxCollection);
169  std::auto_ptr<CaloSpareBxCollection> hfSums (new CaloSpareBxCollection);
170  std::auto_ptr<CaloSpareBxCollection> hfCounts (new CaloSpareBxCollection);
171 
172  egammas->setBXRange(bxFirst, bxLast);
173  taus->setBXRange(bxFirst, bxLast);
174  isoTaus->setBXRange(bxFirst, bxLast);
175  jets->setBXRange(bxFirst, bxLast);
176  preGtJets->setBXRange(bxFirst, bxLast);
177  etsums->setBXRange(bxFirst, bxLast);
178  hfSums->setBXRange(bxFirst, bxLast);
179  hfCounts->setBXRange(bxFirst, bxLast);
180 
181  //producer is responsible for splitting the BXVector into pieces for
182  //the firmware to handle
183  for(int i = bxFirst; i <= bxLast; ++i)
184  {
185  //make local inputs
186  std::vector<CaloRegion> *localRegions = new std::vector<CaloRegion>();
187  std::vector<CaloEmCand> *localEmCands = new std::vector<CaloEmCand>();
188 
189  //make local outputs
190  std::vector<EGamma> *localEGammas = new std::vector<EGamma>();
191  std::vector<Tau> *localTaus = new std::vector<Tau>();
192  std::vector<Tau> *localIsoTaus = new std::vector<Tau>();
193  std::vector<Jet> *localJets = new std::vector<Jet>();
194  std::vector<Jet> *localPreGtJets = new std::vector<Jet>();
195  std::vector<EtSum> *localEtSums = new std::vector<EtSum>();
196  CaloSpare *localHfSums = new CaloSpare();
197  localHfSums->setType(CaloSpare::HFRingSum);
198  CaloSpare *localHfCounts = new CaloSpare();
199  localHfCounts->setType(CaloSpare::HFBitCount);
200 
201  // copy over the inputs -> there must be a better way to do this
202  for(std::vector<CaloRegion>::const_iterator region = caloRegions->begin(i);
203  region != caloRegions->end(i); ++region)
204  localRegions->push_back(*region);
205  for(std::vector<CaloEmCand>::const_iterator emcand = caloEmCands->begin(i);
206  emcand != caloEmCands->end(i); ++emcand)
207  localEmCands->push_back(*emcand);
208 
209  //run the firmware on one event
210  m_fw->processEvent(*localEmCands, *localRegions,
211  localEGammas, localTaus, localIsoTaus, localJets, localPreGtJets, localEtSums,
212  localHfSums, localHfCounts);
213 
214  // copy the output into the BXVector -> there must be a better way
215  for(std::vector<EGamma>::const_iterator eg = localEGammas->begin(); eg != localEGammas->end(); ++eg)
216  egammas->push_back(i, *eg);
217  for(std::vector<Tau>::const_iterator tau = localTaus->begin(); tau != localTaus->end(); ++tau)
218  taus->push_back(i, *tau);
219  for(std::vector<Tau>::const_iterator isotau = localIsoTaus->begin(); isotau != localIsoTaus->end(); ++isotau)
220  isoTaus->push_back(i, *isotau);
221  for(std::vector<Jet>::const_iterator jet = localJets->begin(); jet != localJets->end(); ++jet)
222  jets->push_back(i, *jet);
223  for(std::vector<Jet>::const_iterator jet = localPreGtJets->begin(); jet != localPreGtJets->end(); ++jet)
224  preGtJets->push_back(i, *jet);
225  for(std::vector<EtSum>::const_iterator etsum = localEtSums->begin(); etsum != localEtSums->end(); ++etsum)
226  etsums->push_back(i, *etsum);
227  hfSums->push_back(i, *localHfSums);
228  hfCounts->push_back(i, *localHfCounts);
229 
230  delete localRegions;
231  delete localEmCands;
232  delete localEGammas;
233  delete localTaus;
234  delete localIsoTaus;
235  delete localJets;
236  delete localPreGtJets;
237  delete localEtSums;
238  delete localHfSums;
239  delete localHfCounts;
240  }
241 
242 
243  iEvent.put(egammas);
244  iEvent.put(taus,"rlxTaus");
245  iEvent.put(isoTaus,"isoTaus");
246  iEvent.put(jets);
247  iEvent.put(preGtJets,"preGtJets");
248  iEvent.put(etsums);
249  iEvent.put(hfSums,"HFRingSums");
250  iEvent.put(hfCounts,"HFBitCounts");
251 }
252 
253 // ------------ method called once each job just before starting event loop ------------
254 void
256 {
257 }
258 
259 // ------------ method called once each job just after ending the event loop ------------
260 void
262 }
263 
264 // ------------ method called when starting to processes a run ------------
265 
267  unsigned long long id = 0;
268 
269  id = iE.get<L1TCaloParamsRcd>().cacheIdentifier();
270  if (id != m_paramsCacheId) {
271 
272  m_paramsCacheId = id;
273 
274  edm::ESHandle<CaloParams> paramsHandle;
275 
276  iE.get<L1TCaloParamsRcd>().get(m_conditionsLabel, paramsHandle);
277 
278  // replace our local copy of the parameters with a new one using placement new
279  m_params->~CaloParamsHelper();
280  m_params = new (m_params) CaloParamsHelper(*paramsHandle.product());
281 
282  LogDebug("L1TDebug") << *m_params << std::endl;
283 
284  if (! m_params){
285  edm::LogError("l1t|caloStage1") << "Could not retrieve params from Event Setup" << std::endl;
286  }
287 
288  }
289 
290  id = iE.get<L1TCaloConfigRcd>().cacheIdentifier();
291  if (id != m_configCacheId) {
292  m_configCacheId = id;
293 
294  edm::ESHandle<CaloConfig> configHandle;
295 
296  iE.get<L1TCaloConfigRcd>().get(m_conditionsLabel, configHandle);
297 
298 
299  if (! configHandle.product()){
300  edm::LogError("l1t|caloStage1") << "Could not retrieve config from Event Setup" << std::endl;
301  } else {
302  //update our DB payload in CaloConfigHelper:
303  m_config.UpdatePayload(configHandle.product());
304  //cout << "DEBUG: L1T Calo Config reports the Layer2 firmware version is " << m_config.fwv() << "\n";
305  }
306  }
307 
308 
309 
310 
311 
312 
313  LogDebug("l1t|stage 1 jets") << "L1TStage1Layer2Producer::beginRun function called...\n";
314 
315  //get the proper scales for conversion to physical et AND gt scales
317  iE.get< L1EmEtScaleRcd >().get( m_conditionsLabel, emScale ) ;
318  m_params->setEmScale(*emScale);
319 
321  iE.get< L1JetEtScaleRcd >().get( m_conditionsLabel, jetScale ) ;
322  m_params->setJetScale(*jetScale);
323 
324  edm::ESHandle< L1CaloEtScale > HtMissScale;
325  iE.get< L1HtMissScaleRcd >().get( m_conditionsLabel, HtMissScale ) ;
326  m_params->setHtMissScale(*HtMissScale);
327 
328  //not sure if I need this one
329  edm::ESHandle< L1CaloEtScale > HfRingScale;
330  iE.get< L1HfRingEtScaleRcd >().get( m_conditionsLabel, HfRingScale );
331  m_params->setHfRingScale(*HfRingScale);
332 
333 
334  //unsigned long long id = iE.get<CaloParamsRcd>().cacheIdentifier();
335 
336  //if (id != m_paramsCacheId)
337  { // Need to update:
338  //m_paramsCacheId = id;
339 
340  //ESHandle<CaloParams> parameters;
341  //iE.get<CaloParamsRcd>().get(parameters);
342 
343  // LenA move the setting of the firmware version to the L1TStage1Layer2Producer constructor
344 
345  //m_params = boost::shared_ptr<const CaloParams>(parameters.product());
346  //m_fwv = boost::shared_ptr<const FirmwareVersion>(new FirmwareVersion());
347  //printf("Begin.\n");
348  //m_fwv = boost::shared_ptr<FirmwareVersion>(new FirmwareVersion()); //not const during testing
349  //printf("Success m_fwv.\n");
350  //m_fwv->setFirmwareVersion(1); //hardcode for now, 1=HI, 2=PP
351  //printf("Success m_fwv version set.\n");
352 
353  // if (! m_params){
354  // LogError("l1t|stage 1 jets") << "L1TStage1Layer2Producer: could not retreive DB params from Event Setup\n";
355  // }
356 
357  // Set the current algorithm version based on DB pars from database:
358  //m_fw = m_factory.create(*m_fwv /*,*m_params*/);
359  //printf("Success create.\n");
360 
361  //if (! m_fw) {
362  // // we complain here once per run
363  // LogError("l1t|stage 1 jets") << "L1TStage1Layer2Producer: firmware could not be configured.\n";
364  //}
365  }
366 
367 
368 
369  int ifwv=m_config.fwv();
370  //cout << "DEBUG: ifwv is " << ifwv << "\n";
371  //m_fwv = boost::shared_ptr<FirmwareVersion>(new FirmwareVersion()); //not const during testing
372  if (ifwv == 1){
373  LogDebug("l1t|stage1firmware") << "L1TStage1Layer2Producer -- Running HI implementation\n";
374  //std::cout << "L1TStage1Layer2Producer -- Running HI implementation\n";
375  }else if (ifwv == 2){
376  LogDebug("l1t|stage1firmware") << "L1TStage1Layer2Producer -- Running pp implementation\n";
377  //std::cout << "L1TStage1Layer2Producer -- Running pp implementation\n";
378  } else if (ifwv == 3){
379  LogDebug("l1t|stage1firmware") << "L1TStage1Layer2Producer -- Running SimpleHW implementation\n";
380  //std::cout << "L1TStage1Layer2Producer -- Running SimpleHW implementation -- for testing only\n";
381  }else{
382  LogError("l1t|stage1firmware") << "L1TStage1Layer2Producer -- Unknown implementation.\n";
383  //std::cout << "L1TStage1Layer2Producer -- Unknown implementation.\n";
384  }
385  //m_fwv->setFirmwareVersion(ifwv); // =1 HI, =2 PP
386  // m_fw = m_factory.create(*m_fwv /*,*m_params*/);
387  //m_fwv = ifwv;
388  m_fw = m_factory.create(ifwv ,m_params);
389  //printf("Success create.\n");
390  if (! m_fw) {
391  // we complain here once per job
392  LogError("l1t|stage1firmware") << "L1TStage1Layer2Producer: firmware could not be configured.\n";
393  }
394 
395 
396 
397 
398 }
399 
400 // ------------ method called when ending the processing of a run ------------
401 void L1TStage1Layer2Producer::endRun(Run const& iR, EventSetup const& iE){
402 
403 }
404 
405 
406 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
407 void
409  //The following says we do not know what parameters are allowed so do no validation
410  // Please change this to state exactly what you do use, even if it is no parameters
412  desc.setUnknown();
413  descriptions.addDefault(desc);
414 }
415 
416 //define this as a plug-in
#define LogDebug(id)
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:464
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
virtual void endRun(Run const &iR, EventSetup const &iE)
static void fillDescriptions(ConfigurationDescriptions &descriptions)
unsigned long long m_paramsCacheId
void beginJob()
Definition: Breakpoints.cc:15
void setType(CaloSpareType type)
Definition: CaloSpare.cc:32
L1TStage1Layer2Producer(const ParameterSet &)
int iEvent
Definition: GenABIO.cc:230
void addDefault(ParameterSetDescription const &psetDescription)
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:120
virtual void beginRun(Run const &iR, EventSetup const &iE)
vector< PseudoJet > jets
boost::shared_ptr< Stage1Layer2MainProcessor > m_fw
Stage1Layer2FirmwareFactory m_factory
const T & get() const
Definition: EventSetup.h:56
T const * product() const
Definition: ESHandle.h:86
unsigned long long m_configCacheId
virtual void produce(Event &, EventSetup const &)
Definition: Run.h:43