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 beginRun(const edm::Run&, const edm::EventSetup&) override;
70  virtual void produce(edm::Event&, const edm::EventSetup&) override;
71 
72  bool isBadRun(const unsigned int) const;
73  void printConfiguration(std::stringstream& ss) const;
74 
75  // ----------member data ---------------------------
76 
79  const bool m_ignoreDB;
81  std::vector<std::string> _defpartnames;
82  std::vector<int> _defphases;
83  const bool _wantHistos;
84  bool _useEC0;
86  bool m_badRun;
87  const unsigned int m_maxLS;
88  const unsigned int m_LSfrac;
89 
91 
92  TH1F** _hsize;
93  TH1F** _hlresync;
94  TH1F** _hlOC0;
95  TH1F** _hlTE;
96  TH1F** _hlstart;
97  TH1F** _hlEC0;
98  TH1F** _hlHR;
99 
102 
103  std::vector<std::pair<unsigned int, unsigned int> > m_badruns;
104 
105  long long _lastResync;
106  long long _lastHardReset;
107  long long _lastStart;
110  long long _lastTestEnable;
111 
112 
113 };
114 
115 //
116 // constants, enums and typedefs
117 //
118 
119 
120 //
121 // static data member definitions
122 //
123 
124 //
125 // constructors and destructor
126 //
128  m_eswatcher(),
129  _l1tscollectionToken(consumes<Level1TriggerScalersCollection>(iConfig.getParameter<edm::InputTag>("l1TSCollection"))),
130  m_ignoreDB(iConfig.getUntrackedParameter<bool>("ignoreDB",false)),
131  m_rcdLabel(iConfig.getUntrackedParameter<std::string>("recordLabel","apvphaseoffsets")),
132  _defpartnames(iConfig.getParameter<std::vector<std::string> >("defaultPartitionNames")),
133  _defphases(iConfig.getParameter<std::vector<int> >("defaultPhases")),
134  _wantHistos(iConfig.getUntrackedParameter<bool>("wantHistos",false)),
135  _useEC0(iConfig.getUntrackedParameter<bool>("useEC0",false)),
136  _magicOffset(iConfig.getUntrackedParameter<int>("magicOffset",8)),
137  m_badRun(false),
138  m_maxLS(iConfig.getUntrackedParameter<unsigned int>("maxLSBeforeRebin",250)),
139  m_LSfrac(iConfig.getUntrackedParameter<unsigned int>("startingLSFraction",16)),
140  m_rhm(consumesCollector()),
141  _hsize(0),_hlresync(0),_hlOC0(0),_hlTE(0),_hlstart(0),_hlEC0(0),_hlHR(0),_hdlec0lresync(0),_hdlresynclHR(0),
142  m_badruns(),
143  _lastResync(-1),_lastHardReset(-1),_lastStart(-1),
144  _lastEventCounter0(-1),_lastOrbitCounter0(-1),_lastTestEnable(-1)
145 {
146 
147  std::stringstream ss;
148  printConfiguration(ss);
149  edm::LogInfo("ConfigurationAtConstruction") << ss.str();
150 
151 
152  produces<APVCyclePhaseCollection,edm::InEvent>();
153 
154  m_badruns.push_back(std::pair<unsigned int, unsigned int>(0,131767));
155  m_badruns.push_back(std::pair<unsigned int, unsigned int>(193150,193733));
156 
157  //now do what ever other initialization is needed
158 
159  if(_wantHistos) {
160  _hsize = m_rhm.makeTH1F("size","Level1TriggerScalers Collection size",20,-0.5,19.5);
161 
162  _hlresync = m_rhm.makeTH1F("lresync","Orbit of last resync",m_LSfrac*m_maxLS,0,m_maxLS*262144);
163  _hlOC0 = m_rhm.makeTH1F("lOC0","Orbit of last OC0",m_LSfrac*m_maxLS,0,m_maxLS*262144);
164  _hlTE = m_rhm.makeTH1F("lTE","Orbit of last TestEnable",m_LSfrac*m_maxLS,0,m_maxLS*262144);
165  _hlstart = m_rhm.makeTH1F("lstart","Orbit of last Start",m_LSfrac*m_maxLS,0,m_maxLS*262144);
166  _hlEC0 = m_rhm.makeTH1F("lEC0","Orbit of last EC0",m_LSfrac*m_maxLS,0,m_maxLS*262144);
167  _hlHR = m_rhm.makeTH1F("lHR","Orbit of last HardReset",m_LSfrac*m_maxLS,0,m_maxLS*262144);
168  _hdlec0lresync = m_rhm.makeTH1F("dlec0lresync","Orbit difference EC0-Resync",4000,-1999.5,2000.5);
169  _hdlresynclHR = m_rhm.makeTH1F("dlresynclHR","Orbit difference Resync-HR",4000,-1999.5,2000.5);
170 
171  }
172 
173 
174 }
175 
176 
178 {
179 
180  // do anything here that needs to be done at desctruction time
181  // (e.g. close files, deallocate resources etc.)
182 
183 }
184 
185 
186 //
187 // member functions
188 //
189 
190 // ------------ method called to produce the data ------------
191 void
193 
194 {
195 
196  // update the parameters from DB
197 
198  if(!m_ignoreDB && m_eswatcher.check(iSetup)) {
200  iSetup.get<SiStripConfObjectRcd>().get(m_rcdLabel,confObj);
201 
202  std::stringstream summary;
203  confObj->printDebug(summary);
204  LogDebug("SiStripConfObjectSummary") << summary.str();
205 
206  _defpartnames = confObj->get<std::vector<std::string> >("defaultPartitionNames");
207  _defphases = confObj->get<std::vector<int> >("defaultPhases");
208  _useEC0 = confObj->get<bool>("useEC0");
209  m_badRun = confObj->get<bool>("badRun");
210  _magicOffset = confObj->get<int>("magicOffset");
211 
212  std::stringstream ss;
213  printConfiguration(ss);
214  edm::LogInfo("UpdatedConfiguration") << ss.str();
215 
216 
217  }
218 
219  if(_wantHistos) {
220 
221  m_rhm.beginRun(iRun);
222 
223  if(_hlresync && *_hlresync) {
224  (*_hlresync)->GetXaxis()->SetTitle("Orbit"); (*_hlresync)->GetYaxis()->SetTitle("Events");
225  (*_hlresync)->SetBit(TH1::kCanRebin);
226  }
227 
228  if(_hlOC0 && *_hlOC0) {
229  (*_hlOC0)->GetXaxis()->SetTitle("Orbit"); (*_hlOC0)->GetYaxis()->SetTitle("Events");
230  (*_hlOC0)->SetBit(TH1::kCanRebin);
231  }
232 
233  if(_hlTE && *_hlTE) {
234  (*_hlTE)->GetXaxis()->SetTitle("Orbit"); (*_hlTE)->GetYaxis()->SetTitle("Events");
235  (*_hlTE)->SetBit(TH1::kCanRebin);
236  }
237 
238  if(_hlstart && *_hlstart) {
239  (*_hlstart)->GetXaxis()->SetTitle("Orbit"); (*_hlstart)->GetYaxis()->SetTitle("Events");
240  (*_hlstart)->SetBit(TH1::kCanRebin);
241  }
242 
243  if(_hlEC0 && *_hlEC0) {
244  (*_hlEC0)->GetXaxis()->SetTitle("Orbit"); (*_hlEC0)->GetYaxis()->SetTitle("Events");
245  (*_hlEC0)->SetBit(TH1::kCanRebin);
246  }
247 
248  if(_hlHR && *_hlHR) {
249  (*_hlHR)->GetXaxis()->SetTitle("Orbit"); (*_hlHR)->GetYaxis()->SetTitle("Events");
250  (*_hlHR)->SetBit(TH1::kCanRebin);
251  }
252 
254  (*_hdlec0lresync)->GetXaxis()->SetTitle("lastEC0-lastResync");
255  }
256 
257  if(_hdlresynclHR && *_hdlresynclHR) {
258  (*_hdlresynclHR)->GetXaxis()->SetTitle("lastEC0-lastResync");
259  }
260 
261  }
262 
263  if(isBadRun(iRun.run())) {
264  LogDebug("UnreliableMissingL1TriggerScalers") <<
265  "In this run L1TriggerScalers is missing or unreliable for phase determination: invlid phase will be returned";
266  }
267 
268 }
269 
270 
271 void
273 
274  using namespace edm;
275 
276  std::auto_ptr<APVCyclePhaseCollection> apvphases(new APVCyclePhaseCollection() );
277 
278 
279  std::vector<int> phases(_defphases.size(),APVCyclePhaseCollection::invalid);
280 
281  const std::vector<std::string>& partnames = _defpartnames;
282 
283  int phasechange = 0;
284 
285 
287  iEvent.getByToken(_l1tscollectionToken,l1ts);
288 
289  if(_wantHistos && _hsize && *_hsize) (*_hsize)->Fill(l1ts->size());
290 
291  // offset computation
292 
293  long long orbitoffset = 0;
294 
295  if(l1ts->size()>0) {
296 
297  if((*l1ts)[0].lastResync()!=0) {
298  orbitoffset = _useEC0 ? (*l1ts)[0].lastEventCounter0() + _magicOffset : (*l1ts)[0].lastResync() + _magicOffset;
299  }
300 
301  if(_wantHistos) {
302  if(_hlresync && *_hlresync) (*_hlresync)->Fill((*l1ts)[0].lastResync());
303  if(_hlOC0 && *_hlOC0) (*_hlOC0)->Fill((*l1ts)[0].lastOrbitCounter0());
304  if(_hlTE && *_hlTE) (*_hlTE)->Fill((*l1ts)[0].lastTestEnable());
305  if(_hlstart && *_hlstart) (*_hlstart)->Fill((*l1ts)[0].lastStart());
306  if(_hlEC0 && *_hlEC0) (*_hlEC0)->Fill((*l1ts)[0].lastEventCounter0());
307  if(_hlHR && *_hlHR) (*_hlHR)->Fill((*l1ts)[0].lastHardReset());
308  }
309 
310  if(_lastResync != (*l1ts)[0].lastResync()) {
311  _lastResync = (*l1ts)[0].lastResync();
312  if(_wantHistos && _hdlec0lresync && *_hdlec0lresync) (*_hdlec0lresync)->Fill((*l1ts)[0].lastEventCounter0()-(*l1ts)[0].lastResync());
313  LogDebug("TTCSignalReceived") << "New Resync at orbit " << _lastResync ;
314  }
315  if(_lastHardReset != (*l1ts)[0].lastHardReset()) {
316  _lastHardReset = (*l1ts)[0].lastHardReset();
317  if(_wantHistos && _hdlresynclHR && *_hdlresynclHR) (*_hdlresynclHR)->Fill((*l1ts)[0].lastResync()-(*l1ts)[0].lastHardReset());
318  LogDebug("TTCSignalReceived") << "New HardReset at orbit " << _lastHardReset ;
319  }
320  if(_lastTestEnable != (*l1ts)[0].lastTestEnable()) {
321  _lastTestEnable = (*l1ts)[0].lastTestEnable();
322  // LogDebug("TTCSignalReceived") << "New TestEnable at orbit " << _lastTestEnable ;
323  }
324  if(_lastOrbitCounter0 != (*l1ts)[0].lastOrbitCounter0()) {
325  _lastOrbitCounter0 = (*l1ts)[0].lastOrbitCounter0();
326  LogDebug("TTCSignalReceived") << "New OrbitCounter0 at orbit " << _lastOrbitCounter0 ;
327  }
328  if(_lastEventCounter0 != (*l1ts)[0].lastEventCounter0()) {
329  _lastEventCounter0 = (*l1ts)[0].lastEventCounter0();
330  LogDebug("TTCSignalReceived") << "New EventCounter0 at orbit " << _lastEventCounter0 ;
331  }
332  if(_lastStart != (*l1ts)[0].lastStart()) {
333  _lastStart = (*l1ts)[0].lastStart();
334  LogDebug("TTCSignalReceived") << "New Start at orbit " << _lastStart ;
335  }
336 
337  if(!isBadRun(iEvent.run())) {
338  phasechange = ((long long)(orbitoffset*3564))%70;
339 
340  for(unsigned int ipart=0;ipart<phases.size();++ipart) {
341  phases[ipart] = (_defphases[ipart]+phasechange)%70;
342  }
343 
344  }
345  }
346 
347 
348  if(phases.size() < partnames.size() ) {
349  // throw exception
350  throw cms::Exception("InvalidAPVCyclePhases") << " Inconsistent phases/partitions vector sizes: "
351  << phases.size() << " "
352  << partnames.size();
353  }
354 
355  for(unsigned int ipart=0;ipart<partnames.size();++ipart) {
356  // if(phases[ipart]>=0) {
357  // apvphases->get()[partnames[ipart]] = (phases[ipart]+phasechange)%70;
358  apvphases->get()[partnames[ipart]] = phases[ipart];
359 
360  // }
361  }
362 
363 
364  iEvent.put(apvphases);
365 
366 }
367 
368 bool
369 APVCyclePhaseProducerFromL1TS::isBadRun(const unsigned int run) const {
370 
371  for(std::vector<std::pair<unsigned int, unsigned int> >::const_iterator runpair = m_badruns.begin();runpair!=m_badruns.end();++runpair) {
372  if( run >= runpair->first && run <= runpair->second) return true;
373  }
374 
375  return m_badRun;
376 
377 }
378 
379 void
381 
382  ss << _defpartnames.size() << " default partition names: ";
383  for(std::vector<std::string>::const_iterator part=_defpartnames.begin();part!=_defpartnames.end();++part) {
384  ss << *part << " ";
385  }
386  ss << std::endl;
387  ss << _defphases.size() << " default phases: ";
388  for(std::vector<int>::const_iterator phase=_defphases.begin();phase!=_defphases.end();++phase) {
389  ss << *phase << " ";
390  }
391  ss << std::endl;
392  ss << " Magic offset: " << _magicOffset << std::endl;
393  ss << " use ECO: " << _useEC0 << std::endl;
394  ss << " bad run: " << m_badRun << std::endl;
395 
396 }
397 //define this as a plug-in
#define LogDebug(id)
RunNumber_t run() const
Definition: RunBase.h:42
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:434
#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:230
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
m_rhm(consumesCollector())
RunNumber_t run() const
Definition: Event.h:88
void printConfiguration(std::stringstream &ss) const
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)
std::vector< Level1TriggerScalers > Level1TriggerScalersCollection
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
edm::EDGetTokenT< Level1TriggerScalersCollection > _l1tscollectionToken
Definition: Run.h:41