CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
APVCyclePhaseProducerFromL1TS.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: SiStripTools
4 // Class: APVCyclePhaseProducerFromL1TS
5 //
13 //
14 // Original Author: Andrea Venturi
15 // Created: Mon Jan 12 09:05:45 CET 2009
16 //
17 //
18 
19 
20 // system include files
21 #include <memory>
22 
23 // user include files
26 
30 
34 
36 
38 
41 
42 #include <map>
43 #include <vector>
44 #include <utility>
45 #include <string>
46 #include <iostream>
47 
48 #include "TH1F.h"
49 #include "TProfile.h"
50 
53 
56 
58 
59 //
60 // class decleration
61 //
62 
64  public:
67 
68 private:
69  virtual void beginJob() override ;
70  virtual void beginRun(const edm::Run&, const edm::EventSetup&) override;
71  virtual void produce(edm::Event&, const edm::EventSetup&) override;
72  virtual void endJob() override ;
73 
74  bool isBadRun(const unsigned int) const;
75  void printConfiguration(std::stringstream& ss) const;
76 
77  // ----------member data ---------------------------
78 
81  const bool m_ignoreDB;
83  std::vector<std::string> _defpartnames;
84  std::vector<int> _defphases;
85  const bool _wantHistos;
86  bool _useEC0;
88  bool m_badRun;
89  const unsigned int m_maxLS;
90  const unsigned int m_LSfrac;
91 
93 
94  TH1F** _hsize;
95  TH1F** _hlresync;
96  TH1F** _hlOC0;
97  TH1F** _hlTE;
98  TH1F** _hlstart;
99  TH1F** _hlEC0;
100  TH1F** _hlHR;
101 
104 
105  std::vector<std::pair<unsigned int, unsigned int> > m_badruns;
106 
107  long long _lastResync;
108  long long _lastHardReset;
109  long long _lastStart;
112  long long _lastTestEnable;
113 
114 
115 };
116 
117 //
118 // constants, enums and typedefs
119 //
120 
121 
122 //
123 // static data member definitions
124 //
125 
126 //
127 // constructors and destructor
128 //
130  m_eswatcher(),
131  _l1tscollection(iConfig.getParameter<edm::InputTag>("l1TSCollection")),
132  m_ignoreDB(iConfig.getUntrackedParameter<bool>("ignoreDB",false)),
133  m_rcdLabel(iConfig.getUntrackedParameter<std::string>("recordLabel","apvphaseoffsets")),
134  _defpartnames(iConfig.getParameter<std::vector<std::string> >("defaultPartitionNames")),
135  _defphases(iConfig.getParameter<std::vector<int> >("defaultPhases")),
136  _wantHistos(iConfig.getUntrackedParameter<bool>("wantHistos",false)),
137  _useEC0(iConfig.getUntrackedParameter<bool>("useEC0",false)),
138  _magicOffset(iConfig.getUntrackedParameter<int>("magicOffset",8)),
139  m_badRun(false),
140  m_maxLS(iConfig.getUntrackedParameter<unsigned int>("maxLSBeforeRebin",250)),
141  m_LSfrac(iConfig.getUntrackedParameter<unsigned int>("startingLSFraction",16)),
142  m_rhm(),
143  _hsize(0),_hlresync(0),_hlOC0(0),_hlTE(0),_hlstart(0),_hlEC0(0),_hlHR(0),_hdlec0lresync(0),_hdlresynclHR(0),
144  m_badruns(),
145  _lastResync(-1),_lastHardReset(-1),_lastStart(-1),
146  _lastEventCounter0(-1),_lastOrbitCounter0(-1),_lastTestEnable(-1)
147 {
148 
149  std::stringstream ss;
150  printConfiguration(ss);
151  edm::LogInfo("ConfigurationAtConstruction") << ss.str();
152 
153 
154  produces<APVCyclePhaseCollection,edm::InEvent>();
155 
156  m_badruns.push_back(std::pair<unsigned int, unsigned int>(0,131767));
157  m_badruns.push_back(std::pair<unsigned int, unsigned int>(193150,193733));
158 
159  //now do what ever other initialization is needed
160 
161  if(_wantHistos) {
162  _hsize = m_rhm.makeTH1F("size","Level1TriggerScalers Collection size",20,-0.5,19.5);
163 
164  _hlresync = m_rhm.makeTH1F("lresync","Orbit of last resync",m_LSfrac*m_maxLS,0,m_maxLS*262144);
165  _hlOC0 = m_rhm.makeTH1F("lOC0","Orbit of last OC0",m_LSfrac*m_maxLS,0,m_maxLS*262144);
166  _hlTE = m_rhm.makeTH1F("lTE","Orbit of last TestEnable",m_LSfrac*m_maxLS,0,m_maxLS*262144);
167  _hlstart = m_rhm.makeTH1F("lstart","Orbit of last Start",m_LSfrac*m_maxLS,0,m_maxLS*262144);
168  _hlEC0 = m_rhm.makeTH1F("lEC0","Orbit of last EC0",m_LSfrac*m_maxLS,0,m_maxLS*262144);
169  _hlHR = m_rhm.makeTH1F("lHR","Orbit of last HardReset",m_LSfrac*m_maxLS,0,m_maxLS*262144);
170  _hdlec0lresync = m_rhm.makeTH1F("dlec0lresync","Orbit difference EC0-Resync",4000,-1999.5,2000.5);
171  _hdlresynclHR = m_rhm.makeTH1F("dlresynclHR","Orbit difference Resync-HR",4000,-1999.5,2000.5);
172 
173  }
174 
175 
176 }
177 
178 
180 {
181 
182  // do anything here that needs to be done at desctruction time
183  // (e.g. close files, deallocate resources etc.)
184 
185 }
186 
187 
188 //
189 // member functions
190 //
191 
192 // ------------ method called to produce the data ------------
193 void
195 
196 {
197 
198  // update the parameters from DB
199 
200  if(!m_ignoreDB && m_eswatcher.check(iSetup)) {
202  iSetup.get<SiStripConfObjectRcd>().get(m_rcdLabel,confObj);
203 
204  std::stringstream summary;
205  confObj->printDebug(summary);
206  LogDebug("SiStripConfObjectSummary") << summary.str();
207 
208  _defpartnames = confObj->get<std::vector<std::string> >("defaultPartitionNames");
209  _defphases = confObj->get<std::vector<int> >("defaultPhases");
210  _useEC0 = confObj->get<bool>("useEC0");
211  m_badRun = confObj->get<bool>("badRun");
212  _magicOffset = confObj->get<int>("magicOffset");
213 
214  std::stringstream ss;
215  printConfiguration(ss);
216  edm::LogInfo("UpdatedConfiguration") << ss.str();
217 
218 
219  }
220 
221  if(_wantHistos) {
222 
223  m_rhm.beginRun(iRun);
224 
225  if(_hlresync && *_hlresync) {
226  (*_hlresync)->GetXaxis()->SetTitle("Orbit"); (*_hlresync)->GetYaxis()->SetTitle("Events");
227  (*_hlresync)->SetBit(TH1::kCanRebin);
228  }
229 
230  if(_hlOC0 && *_hlOC0) {
231  (*_hlOC0)->GetXaxis()->SetTitle("Orbit"); (*_hlOC0)->GetYaxis()->SetTitle("Events");
232  (*_hlOC0)->SetBit(TH1::kCanRebin);
233  }
234 
235  if(_hlTE && *_hlTE) {
236  (*_hlTE)->GetXaxis()->SetTitle("Orbit"); (*_hlTE)->GetYaxis()->SetTitle("Events");
237  (*_hlTE)->SetBit(TH1::kCanRebin);
238  }
239 
240  if(_hlstart && *_hlstart) {
241  (*_hlstart)->GetXaxis()->SetTitle("Orbit"); (*_hlstart)->GetYaxis()->SetTitle("Events");
242  (*_hlstart)->SetBit(TH1::kCanRebin);
243  }
244 
245  if(_hlEC0 && *_hlEC0) {
246  (*_hlEC0)->GetXaxis()->SetTitle("Orbit"); (*_hlEC0)->GetYaxis()->SetTitle("Events");
247  (*_hlEC0)->SetBit(TH1::kCanRebin);
248  }
249 
250  if(_hlHR && *_hlHR) {
251  (*_hlHR)->GetXaxis()->SetTitle("Orbit"); (*_hlHR)->GetYaxis()->SetTitle("Events");
252  (*_hlHR)->SetBit(TH1::kCanRebin);
253  }
254 
256  (*_hdlec0lresync)->GetXaxis()->SetTitle("lastEC0-lastResync");
257  }
258 
259  if(_hdlresynclHR && *_hdlresynclHR) {
260  (*_hdlresynclHR)->GetXaxis()->SetTitle("lastEC0-lastResync");
261  }
262 
263  }
264 
265  if(isBadRun(iRun.run())) {
266  LogDebug("UnreliableMissingL1TriggerScalers") <<
267  "In this run L1TriggerScalers is missing or unreliable for phase determination: invlid phase will be returned";
268  }
269 
270 }
271 
272 
273 void
275 
276  using namespace edm;
277 
278  std::auto_ptr<APVCyclePhaseCollection> apvphases(new APVCyclePhaseCollection() );
279 
280 
281  std::vector<int> phases(_defphases.size(),APVCyclePhaseCollection::invalid);
282 
283  const std::vector<std::string>& partnames = _defpartnames;
284 
285  int phasechange = 0;
286 
287 
289  iEvent.getByLabel(_l1tscollection,l1ts);
290 
291  if(_wantHistos && _hsize && *_hsize) (*_hsize)->Fill(l1ts->size());
292 
293  // offset computation
294 
295  long long orbitoffset = 0;
296 
297  if(l1ts->size()>0) {
298 
299  if((*l1ts)[0].lastResync()!=0) {
300  orbitoffset = _useEC0 ? (*l1ts)[0].lastEventCounter0() + _magicOffset : (*l1ts)[0].lastResync() + _magicOffset;
301  }
302 
303  if(_wantHistos) {
304  if(_hlresync && *_hlresync) (*_hlresync)->Fill((*l1ts)[0].lastResync());
305  if(_hlOC0 && *_hlOC0) (*_hlOC0)->Fill((*l1ts)[0].lastOrbitCounter0());
306  if(_hlTE && *_hlTE) (*_hlTE)->Fill((*l1ts)[0].lastTestEnable());
307  if(_hlstart && *_hlstart) (*_hlstart)->Fill((*l1ts)[0].lastStart());
308  if(_hlEC0 && *_hlEC0) (*_hlEC0)->Fill((*l1ts)[0].lastEventCounter0());
309  if(_hlHR && *_hlHR) (*_hlHR)->Fill((*l1ts)[0].lastHardReset());
310  }
311 
312  if(_lastResync != (*l1ts)[0].lastResync()) {
313  _lastResync = (*l1ts)[0].lastResync();
314  if(_wantHistos && _hdlec0lresync && *_hdlec0lresync) (*_hdlec0lresync)->Fill((*l1ts)[0].lastEventCounter0()-(*l1ts)[0].lastResync());
315  LogDebug("TTCSignalReceived") << "New Resync at orbit " << _lastResync ;
316  }
317  if(_lastHardReset != (*l1ts)[0].lastHardReset()) {
318  _lastHardReset = (*l1ts)[0].lastHardReset();
319  if(_wantHistos && _hdlresynclHR && *_hdlresynclHR) (*_hdlresynclHR)->Fill((*l1ts)[0].lastResync()-(*l1ts)[0].lastHardReset());
320  LogDebug("TTCSignalReceived") << "New HardReset at orbit " << _lastHardReset ;
321  }
322  if(_lastTestEnable != (*l1ts)[0].lastTestEnable()) {
323  _lastTestEnable = (*l1ts)[0].lastTestEnable();
324  // LogDebug("TTCSignalReceived") << "New TestEnable at orbit " << _lastTestEnable ;
325  }
326  if(_lastOrbitCounter0 != (*l1ts)[0].lastOrbitCounter0()) {
327  _lastOrbitCounter0 = (*l1ts)[0].lastOrbitCounter0();
328  LogDebug("TTCSignalReceived") << "New OrbitCounter0 at orbit " << _lastOrbitCounter0 ;
329  }
330  if(_lastEventCounter0 != (*l1ts)[0].lastEventCounter0()) {
331  _lastEventCounter0 = (*l1ts)[0].lastEventCounter0();
332  LogDebug("TTCSignalReceived") << "New EventCounter0 at orbit " << _lastEventCounter0 ;
333  }
334  if(_lastStart != (*l1ts)[0].lastStart()) {
335  _lastStart = (*l1ts)[0].lastStart();
336  LogDebug("TTCSignalReceived") << "New Start at orbit " << _lastStart ;
337  }
338 
339  if(!isBadRun(iEvent.run())) {
340  phasechange = ((long long)(orbitoffset*3564))%70;
341 
342  for(unsigned int ipart=0;ipart<phases.size();++ipart) {
343  phases[ipart] = (_defphases[ipart]+phasechange)%70;
344  }
345 
346  }
347  }
348 
349 
350  if(phases.size() < partnames.size() ) {
351  // throw exception
352  throw cms::Exception("InvalidAPVCyclePhases") << " Inconsistent phases/partitions vector sizes: "
353  << phases.size() << " "
354  << partnames.size();
355  }
356 
357  for(unsigned int ipart=0;ipart<partnames.size();++ipart) {
358  // if(phases[ipart]>=0) {
359  // apvphases->get()[partnames[ipart]] = (phases[ipart]+phasechange)%70;
360  apvphases->get()[partnames[ipart]] = phases[ipart];
361 
362  // }
363  }
364 
365 
366  iEvent.put(apvphases);
367 
368 }
369 
370 // ------------ method called once each job just before starting event loop ------------
371 void
373 {
374 }
375 
376 // ------------ method called once each job just after ending the event loop ------------
377 void
379 }
380 
381 bool
382 APVCyclePhaseProducerFromL1TS::isBadRun(const unsigned int run) const {
383 
384  for(std::vector<std::pair<unsigned int, unsigned int> >::const_iterator runpair = m_badruns.begin();runpair!=m_badruns.end();++runpair) {
385  if( run >= runpair->first && run <= runpair->second) return true;
386  }
387 
388  return m_badRun;
389 
390 }
391 
392 void
394 
395  ss << _defpartnames.size() << " default partition names: ";
396  for(std::vector<std::string>::const_iterator part=_defpartnames.begin();part!=_defpartnames.end();++part) {
397  ss << *part << " ";
398  }
399  ss << std::endl;
400  ss << _defphases.size() << " default phases: ";
401  for(std::vector<int>::const_iterator phase=_defphases.begin();phase!=_defphases.end();++phase) {
402  ss << *phase << " ";
403  }
404  ss << std::endl;
405  ss << " Magic offset: " << _magicOffset << std::endl;
406  ss << " use ECO: " << _useEC0 << std::endl;
407  ss << " bad run: " << m_badRun << std::endl;
408 
409 }
410 //define this as a plug-in
#define LogDebug(id)
RunNumber_t run() const
Definition: RunBase.h:42
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
edm::ESWatcher< SiStripConfObjectRcd > m_eswatcher
virtual void beginRun(const edm::Run &, const edm::EventSetup &) override
U second(std::pair< T, U > const &p)
TH1F ** makeTH1F(const char *name, const char *title, const unsigned int nbinx, const double xmin, const double xmax)
int iEvent
Definition: GenABIO.cc:243
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
RunNumber_t run() const
Definition: Event.h:88
void printConfiguration(std::stringstream &ss) const
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:390
bool isBadRun(const unsigned int) const
virtual void produce(edm::Event &, const edm::EventSetup &) override
part
Definition: HCALResponse.h:20
const T & get() const
Definition: EventSetup.h:55
void beginRun(const edm::Run &iRun)
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:58
std::vector< std::pair< unsigned int, unsigned int > > m_badruns
APVCyclePhaseProducerFromL1TS(const edm::ParameterSet &)
volatile std::atomic< bool > shutdown_flag false
Definition: Run.h:41