CMS 3D CMS Logo

APVCyclePhaseProducerFromL1ABC.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: SiStripTools
4 // Class: APVCyclePhaseProducerFromL1ABC
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 <string>
41 
42 #include "TH1F.h"
43 #include "TProfile.h"
44 
47 
49 
50 //
51 // class decleration
52 //
53 
55  public:
58 
59 private:
60  void beginJob() override ;
61  void beginRun(const edm::Run&, const edm::EventSetup&) override;
62  void endRun(const edm::Run&, const edm::EventSetup&) override;
63  void produce(edm::Event&, const edm::EventSetup&) override;
64  void endJob() override ;
65 
66  // ----------member data ---------------------------
67 
69  const std::vector<std::string> _defpartnames;
70  const std::vector<int> _defphases;
71  const int _orbitoffsetSOR;
72  const bool _wantHistos;
73 
75 
76  TH1F** _hbx;
77  TH1F** _hdbx;
78  TH1F** _hdorbit;
79  const unsigned int _firstgoodrun;
80  std::map<edm::EventNumber_t, long long> _offsets;
81  long long _curroffset;
83 
84 };
85 
86 //
87 // constants, enums and typedefs
88 //
89 
90 
91 //
92 // static data member definitions
93 //
94 
95 //
96 // constructors and destructor
97 //
99  _l1abccollectionToken(mayConsume<L1AcceptBunchCrossingCollection>(iConfig.getParameter<edm::InputTag>("l1ABCCollection"))),
100  _defpartnames(iConfig.getParameter<std::vector<std::string> >("defaultPartitionNames")),
101  _defphases(iConfig.getParameter<std::vector<int> >("defaultPhases")),
102  _orbitoffsetSOR(iConfig.getParameter<int>("StartOfRunOrbitOffset")),
103  _wantHistos(iConfig.getUntrackedParameter<bool>("wantHistos",false)),
107 {
108 
109  produces<APVCyclePhaseCollection,edm::InEvent>();
110 
111  //now do what ever other initialization is needed
112 
113  if(_wantHistos) {
114  _hbx = m_rhm.makeTH1F("l1abcbx","BX number from L1ABC",4096,-0.5,4095.5);
115  _hdbx = m_rhm.makeTH1F("dbx","BX number difference",4096*2-1,-4095.5,4095.5);
116  _hdorbit = m_rhm.makeTH1F("dorbit","Orbit Number difference",9999,-4999.5,4999.5);
117  }
118 
119 
120 }
121 
122 
124 {
125 
126  // do anything here that needs to be done at desctruction time
127  // (e.g. close files, deallocate resources etc.)
128 
129 }
130 
131 
132 //
133 // member functions
134 //
135 
136 // ------------ method called to produce the data ------------
137 void
139 
140 {
141 
142  // reset offset vector
143 
144  _offsets.clear();
145  edm::LogInfo("AbsoluteBXOffsetReset") << "Absolute BX offset map reset";
146 
147 
148  if(_wantHistos) {
149 
150  m_rhm.beginRun(iRun);
151 
152  if(_hbx && *_hbx) {
153  (*_hbx)->GetXaxis()->SetTitle("BX"); (*_hbx)->GetYaxis()->SetTitle("Events");
154  }
155 
156  if(_hdbx && *_hdbx) {
157  (*_hdbx)->GetXaxis()->SetTitle("#DeltaBX"); (*_hdbx)->GetYaxis()->SetTitle("Events");
158  }
159 
160  if(_hdorbit && *_hdorbit) {
161  (*_hdorbit)->GetXaxis()->SetTitle("#Deltaorbit"); (*_hdorbit)->GetYaxis()->SetTitle("Events");
162  }
163 
164  }
165 
166  if(iRun.run() < _firstgoodrun) {
167  edm::LogInfo("UnreliableMissingL1AcceptBunchCrossingCollection") <<
168  "In this run L1AcceptBunchCrossingCollection is missing or unreliable: default phases will be used";
169  }
170 
171 }
172 
173 void
175 {
176  // summary of absolute bx offset vector
177 
178  edm::LogInfo("L1AcceptBunchCrossingAbsoluteBXOffsetSummary") << "Absolute BX offset summary:";
179  for(std::map<edm::EventNumber_t, long long>::const_iterator offset=_offsets.begin();offset!=_offsets.end();++offset) {
180  edm::LogVerbatim("L1AcceptBunchCrossingAbsoluteBXOffsetSummary") << offset->first << " " << offset->second;
181  }
182 
183 }
184 
185 
186 void
188 
189  using namespace edm;
190 
191  std::unique_ptr<APVCyclePhaseCollection> apvphases(new APVCyclePhaseCollection() );
192 
193 
194  const std::vector<int>& phases = _defphases;
195  const std::vector<std::string>& partnames = _defpartnames;
196 
197  // Look for the L1AcceptBunchCrossingCollection
198 
199  int phasechange = 0;
200 
201  if(iEvent.run() >= _firstgoodrun ) {
202 
204  iEvent.getByToken(_l1abccollectionToken,pIn);
205 
206  // offset computation
207 
208  long long orbitoffset = _orbitoffsetSOR;
209  int bxoffset = 0;
210 
211  for(L1AcceptBunchCrossingCollection::const_iterator l1abc=pIn->begin();l1abc!=pIn->end();++l1abc) {
212  if(l1abc->l1AcceptOffset()==0) {
213  if(l1abc->eventType()!=0) {
214  orbitoffset = (long long)iEvent.orbitNumber() - (long long)l1abc->orbitNumber() ;
215  bxoffset = iEvent.bunchCrossing() - l1abc->bunchCrossing();
216 
217  if(_wantHistos) {
218  if(_hbx && *_hbx) (*_hbx)->Fill(l1abc->bunchCrossing());
219  if(_hdbx && *_hdbx) (*_hdbx)->Fill(bxoffset);
220  if(_hdorbit && *_hdorbit) (*_hdorbit)->Fill(orbitoffset);
221  }
222  }
223  else {
224  edm::LogWarning("L1AcceptBunchCrossingNoType") << "L1AcceptBunchCrossing with no type found: ";
225  for(L1AcceptBunchCrossingCollection::const_iterator debu=pIn->begin();debu!=pIn->end();++debu) {
226  edm::LogPrint("L1AcceptBunchCrossingNoType") << *debu;
227  }
228  }
229  }
230  }
231 
232  long long absbxoffset = orbitoffset*3564 + bxoffset;
233 
234  if(orbitoffset != _orbitoffsetSOR) phasechange = (orbitoffset*3564)%70;
235 
236  if(_offsets.empty()) {
237  _curroffset = absbxoffset;
238  _curroffevent = iEvent.id().event();
239  _offsets[iEvent.id().event()] = absbxoffset;
240  }
241  else {
242  if(_curroffset != absbxoffset || iEvent.id().event() < _curroffevent ) {
243 
244  if( _curroffset != absbxoffset) {
245  edm::LogInfo("L1AcceptBunchCrossingAbsoluteBXOffsetChanged") << "Absolute BX offset changed from "
246  << _curroffset << " to "
247  << absbxoffset << " at orbit "
248  << iEvent.orbitNumber() << " and BX "
249  << iEvent.bunchCrossing();
250  for(L1AcceptBunchCrossingCollection::const_iterator l1abc=pIn->begin();l1abc!=pIn->end();++l1abc) {
251  edm::LogVerbatim("L1AcceptBunchCrossingAbsoluteBXOffsetChanged") << *l1abc;
252  }
253  }
254 
255  _curroffset = absbxoffset;
256  _curroffevent = iEvent.id().event();
257  _offsets[iEvent.id().event()] = absbxoffset;
258  }
259  }
260 
261 
262  }
263 
264 
265 
266  if(phases.size() < partnames.size() ) {
267  // throw exception
268  throw cms::Exception("InvalidAPVCyclePhases") << " Inconsistent phases/partitions vector sizes: "
269  << phases.size() << " "
270  << partnames.size();
271  }
272 
273  for(unsigned int ipart=0;ipart<partnames.size();++ipart) {
274  if(phases[ipart]>=0) {
275  apvphases->get()[partnames[ipart]] = (phases[ipart]+phasechange)%70;
276 
277  }
278  }
279 
280 
281  iEvent.put(std::move(apvphases));
282 
283 }
284 
285 // ------------ method called once each job just before starting event loop ------------
286 void
288 {
289 }
290 
291 // ------------ method called once each job just after ending the event loop ------------
292 void
294 }
295 
296 //define this as a plug-in
EventNumber_t event() const
Definition: EventID.h:41
std::map< edm::EventNumber_t, long long > _offsets
const std::vector< std::string > _defpartnames
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:137
RunNumber_t run() const
Definition: RunBase.h:40
APVCyclePhaseProducerFromL1ABC(const edm::ParameterSet &)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
void produce(edm::Event &, const edm::EventSetup &) override
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
int bunchCrossing() const
Definition: EventBase.h:66
unsigned long long EventNumber_t
edm::EDGetTokenT< L1AcceptBunchCrossingCollection > _l1abccollectionToken
#define nullptr
TH1F ** makeTH1F(const char *name, const char *title, const unsigned int nbinx, const double xmin, const double xmax)
int iEvent
Definition: GenABIO.cc:230
void endRun(const edm::Run &, const edm::EventSetup &) override
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
RunNumber_t run() const
Definition: Event.h:109
std::vector< L1AcceptBunchCrossing > L1AcceptBunchCrossingCollection
int orbitNumber() const
Definition: EventBase.h:67
void beginRun(const edm::Run &, const edm::EventSetup &) override
std::vector< int > phases
void beginRun(const edm::Run &iRun)
edm::EventID id() const
Definition: EventBase.h:60
HLT enums.
EDGetTokenT< ProductType > mayConsume(edm::InputTag const &tag)
def move(src, dest)
Definition: eostools.py:510
Definition: Run.h:44