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 
32 
34 
37 
38 #include <map>
39 #include <vector>
40 #include <utility>
41 #include <string>
42 
43 #include "TH1F.h"
44 #include "TProfile.h"
45 
48 
50 
51 //
52 // class decleration
53 //
54 
56  public:
59 
60 private:
61  virtual void beginJob() override ;
62  virtual void beginRun(const edm::Run&, const edm::EventSetup&) override;
63  virtual void produce(edm::Event&, const edm::EventSetup&) override;
64  virtual void endJob() override ;
65 
66  bool isBadRun(const unsigned int) const;
67 
68  // ----------member data ---------------------------
69 
71  const std::vector<std::string> _defpartnames;
72  const std::vector<int> _defphases;
73  const bool _wantHistos;
74  const bool _useEC0;
75  const int _magicOffset;
76  const unsigned int m_maxLS;
77  const unsigned int m_LSfrac;
78 
80 
81  TH1F** _hsize;
82  TH1F** _hlresync;
83  TH1F** _hlOC0;
84  TH1F** _hlTE;
85  TH1F** _hlstart;
86  TH1F** _hlEC0;
87  TH1F** _hlHR;
88 
90  TH1F** _hdlresynclHR;
91 
92  std::vector<std::pair<unsigned int, unsigned int> > m_badruns;
93 
94  long long _lastResync;
95  long long _lastHardReset;
96  long long _lastStart;
97  long long _lastEventCounter0;
98  long long _lastOrbitCounter0;
99  long long _lastTestEnable;
100 
101 
102 };
103 
104 //
105 // constants, enums and typedefs
106 //
107 
108 
109 //
110 // static data member definitions
111 //
112 
113 //
114 // constructors and destructor
115 //
117  _l1tscollection(iConfig.getParameter<edm::InputTag>("l1TSCollection")),
118  _defpartnames(iConfig.getParameter<std::vector<std::string> >("defaultPartitionNames")),
119  _defphases(iConfig.getParameter<std::vector<int> >("defaultPhases")),
120  _wantHistos(iConfig.getUntrackedParameter<bool>("wantHistos",false)),
121  _useEC0(iConfig.getUntrackedParameter<bool>("useEC0",false)),
122  _magicOffset(iConfig.getUntrackedParameter<int>("magicOffset",8)),
123  m_maxLS(iConfig.getUntrackedParameter<unsigned int>("maxLSBeforeRebin",250)),
124  m_LSfrac(iConfig.getUntrackedParameter<unsigned int>("startingLSFraction",16)),
125  m_rhm(),
126  _hsize(0),_hlresync(0),_hlOC0(0),_hlTE(0),_hlstart(0),_hlEC0(0),_hlHR(0),_hdlec0lresync(0),_hdlresynclHR(0),
127  m_badruns(),
128  _lastResync(-1),_lastHardReset(-1),_lastStart(-1),
129  _lastEventCounter0(-1),_lastOrbitCounter0(-1),_lastTestEnable(-1)
130 {
131 
132  produces<APVCyclePhaseCollection,edm::InEvent>();
133 
134  m_badruns.push_back(std::pair<unsigned int, unsigned int>(0,131767));
135  m_badruns.push_back(std::pair<unsigned int, unsigned int>(193150,193733));
136 
137  //now do what ever other initialization is needed
138 
139  if(_wantHistos) {
140  _hsize = m_rhm.makeTH1F("size","Level1TriggerScalers Collection size",20,-0.5,19.5);
141 
142  _hlresync = m_rhm.makeTH1F("lresync","Orbit of last resync",m_LSfrac*m_maxLS,0,m_maxLS*262144);
143  _hlOC0 = m_rhm.makeTH1F("lOC0","Orbit of last OC0",m_LSfrac*m_maxLS,0,m_maxLS*262144);
144  _hlTE = m_rhm.makeTH1F("lTE","Orbit of last TestEnable",m_LSfrac*m_maxLS,0,m_maxLS*262144);
145  _hlstart = m_rhm.makeTH1F("lstart","Orbit of last Start",m_LSfrac*m_maxLS,0,m_maxLS*262144);
146  _hlEC0 = m_rhm.makeTH1F("lEC0","Orbit of last EC0",m_LSfrac*m_maxLS,0,m_maxLS*262144);
147  _hlHR = m_rhm.makeTH1F("lHR","Orbit of last HardReset",m_LSfrac*m_maxLS,0,m_maxLS*262144);
148  _hdlec0lresync = m_rhm.makeTH1F("dlec0lresync","Orbit difference EC0-Resync",4000,-1999.5,2000.5);
149  _hdlresynclHR = m_rhm.makeTH1F("dlresynclHR","Orbit difference Resync-HR",4000,-1999.5,2000.5);
150 
151  }
152 
153 
154 }
155 
156 
158 {
159 
160  // do anything here that needs to be done at desctruction time
161  // (e.g. close files, deallocate resources etc.)
162 
163 }
164 
165 
166 //
167 // member functions
168 //
169 
170 // ------------ method called to produce the data ------------
171 void
173 
174 {
175 
176  // reset offset vector
177 
178  if(_wantHistos) {
179 
180  m_rhm.beginRun(iRun);
181 
182  if(_hlresync && *_hlresync) {
183  (*_hlresync)->GetXaxis()->SetTitle("Orbit"); (*_hlresync)->GetYaxis()->SetTitle("Events");
184  (*_hlresync)->SetBit(TH1::kCanRebin);
185  }
186 
187  if(_hlOC0 && *_hlOC0) {
188  (*_hlOC0)->GetXaxis()->SetTitle("Orbit"); (*_hlOC0)->GetYaxis()->SetTitle("Events");
189  (*_hlOC0)->SetBit(TH1::kCanRebin);
190  }
191 
192  if(_hlTE && *_hlTE) {
193  (*_hlTE)->GetXaxis()->SetTitle("Orbit"); (*_hlTE)->GetYaxis()->SetTitle("Events");
194  (*_hlTE)->SetBit(TH1::kCanRebin);
195  }
196 
197  if(_hlstart && *_hlstart) {
198  (*_hlstart)->GetXaxis()->SetTitle("Orbit"); (*_hlstart)->GetYaxis()->SetTitle("Events");
199  (*_hlstart)->SetBit(TH1::kCanRebin);
200  }
201 
202  if(_hlEC0 && *_hlEC0) {
203  (*_hlEC0)->GetXaxis()->SetTitle("Orbit"); (*_hlEC0)->GetYaxis()->SetTitle("Events");
204  (*_hlEC0)->SetBit(TH1::kCanRebin);
205  }
206 
207  if(_hlHR && *_hlHR) {
208  (*_hlHR)->GetXaxis()->SetTitle("Orbit"); (*_hlHR)->GetYaxis()->SetTitle("Events");
209  (*_hlHR)->SetBit(TH1::kCanRebin);
210  }
211 
213  (*_hdlec0lresync)->GetXaxis()->SetTitle("lastEC0-lastResync");
214  }
215 
216  if(_hdlresynclHR && *_hdlresynclHR) {
217  (*_hdlresynclHR)->GetXaxis()->SetTitle("lastEC0-lastResync");
218  }
219 
220  }
221 
222  if(isBadRun(iRun.run())) {
223  LogDebug("UnreliableMissingL1TriggerScalers") <<
224  "In this run L1TriggerScalers is missing or unreliable for phase determination: invlid phase will be returned";
225  }
226 
227 }
228 
229 
230 void
232 
233  using namespace edm;
234 
235  std::auto_ptr<APVCyclePhaseCollection> apvphases(new APVCyclePhaseCollection() );
236 
237 
238  std::vector<int> phases(_defphases.size(),APVCyclePhaseCollection::invalid);
239 
240  const std::vector<std::string>& partnames = _defpartnames;
241 
242  int phasechange = 0;
243 
244 
246  iEvent.getByLabel(_l1tscollection,l1ts);
247 
248  if(_wantHistos && _hsize && *_hsize) (*_hsize)->Fill(l1ts->size());
249 
250  // offset computation
251 
252  long long orbitoffset = 0;
253 
254  if(l1ts->size()>0) {
255 
256  if((*l1ts)[0].lastResync()!=0) {
257  orbitoffset = _useEC0 ? (*l1ts)[0].lastEventCounter0() + _magicOffset : (*l1ts)[0].lastResync() + _magicOffset;
258  }
259 
260  if(_wantHistos) {
261  if(_hlresync && *_hlresync) (*_hlresync)->Fill((*l1ts)[0].lastResync());
262  if(_hlOC0 && *_hlOC0) (*_hlOC0)->Fill((*l1ts)[0].lastOrbitCounter0());
263  if(_hlTE && *_hlTE) (*_hlTE)->Fill((*l1ts)[0].lastTestEnable());
264  if(_hlstart && *_hlstart) (*_hlstart)->Fill((*l1ts)[0].lastStart());
265  if(_hlEC0 && *_hlEC0) (*_hlEC0)->Fill((*l1ts)[0].lastEventCounter0());
266  if(_hlHR && *_hlHR) (*_hlHR)->Fill((*l1ts)[0].lastHardReset());
267  }
268 
269  if(_lastResync != (*l1ts)[0].lastResync()) {
270  _lastResync = (*l1ts)[0].lastResync();
271  if(_wantHistos && _hdlec0lresync && *_hdlec0lresync) (*_hdlec0lresync)->Fill((*l1ts)[0].lastEventCounter0()-(*l1ts)[0].lastResync());
272  LogDebug("TTCSignalReceived") << "New Resync at orbit " << _lastResync ;
273  }
274  if(_lastHardReset != (*l1ts)[0].lastHardReset()) {
275  _lastHardReset = (*l1ts)[0].lastHardReset();
276  if(_wantHistos && _hdlresynclHR && *_hdlresynclHR) (*_hdlresynclHR)->Fill((*l1ts)[0].lastResync()-(*l1ts)[0].lastHardReset());
277  LogDebug("TTCSignalReceived") << "New HardReset at orbit " << _lastHardReset ;
278  }
279  if(_lastTestEnable != (*l1ts)[0].lastTestEnable()) {
280  _lastTestEnable = (*l1ts)[0].lastTestEnable();
281  // LogDebug("TTCSignalReceived") << "New TestEnable at orbit " << _lastTestEnable ;
282  }
283  if(_lastOrbitCounter0 != (*l1ts)[0].lastOrbitCounter0()) {
284  _lastOrbitCounter0 = (*l1ts)[0].lastOrbitCounter0();
285  LogDebug("TTCSignalReceived") << "New OrbitCounter0 at orbit " << _lastOrbitCounter0 ;
286  }
287  if(_lastEventCounter0 != (*l1ts)[0].lastEventCounter0()) {
288  _lastEventCounter0 = (*l1ts)[0].lastEventCounter0();
289  LogDebug("TTCSignalReceived") << "New EventCounter0 at orbit " << _lastEventCounter0 ;
290  }
291  if(_lastStart != (*l1ts)[0].lastStart()) {
292  _lastStart = (*l1ts)[0].lastStart();
293  LogDebug("TTCSignalReceived") << "New Start at orbit " << _lastStart ;
294  }
295 
296  if(!isBadRun(iEvent.run())) {
297  phasechange = ((long long)(orbitoffset*3564))%70;
298 
299  for(unsigned int ipart=0;ipart<phases.size();++ipart) {
300  phases[ipart] = (_defphases[ipart]+phasechange)%70;
301  }
302 
303  }
304  }
305 
306 
307  if(phases.size() < partnames.size() ) {
308  // throw exception
309  throw cms::Exception("InvalidAPVCyclePhases") << " Inconsistent phases/partitions vector sizes: "
310  << phases.size() << " "
311  << partnames.size();
312  }
313 
314  for(unsigned int ipart=0;ipart<partnames.size();++ipart) {
315  // if(phases[ipart]>=0) {
316  // apvphases->get()[partnames[ipart]] = (phases[ipart]+phasechange)%70;
317  apvphases->get()[partnames[ipart]] = phases[ipart];
318 
319  // }
320  }
321 
322 
323  iEvent.put(apvphases);
324 
325 }
326 
327 // ------------ method called once each job just before starting event loop ------------
328 void
330 {
331 }
332 
333 // ------------ method called once each job just after ending the event loop ------------
334 void
336 }
337 
338 bool
339 APVCyclePhaseProducerFromL1TS::isBadRun(const unsigned int run) const {
340 
341  for(std::vector<std::pair<unsigned int, unsigned int> >::const_iterator runpair = m_badruns.begin();runpair!=m_badruns.end();++runpair) {
342  if( run >= runpair->first && run <= runpair->second) return true;
343  }
344 
345  return false;
346 
347 }
348 
349 //define this as a plug-in
#define LogDebug(id)
RunNumber_t run() const
Definition: RunBase.h:42
const std::vector< std::string > _defpartnames
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
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
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
void beginRun(const edm::Run &iRun)
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